Linux串口驱动分析write

转自:http://blog.csdn.net/longwang155069/article/details/42780331


  1. /*和read的分析过程一样, 我们首先分析tty_write*/  
  2.   
  3. /*最重要的就是do_tty_write函数。 前面都是一些合法性判断*/  
  4. static ssize_t tty_write(struct file *file, const char __user *buf,size_t count, loff_t *ppos)  
  5. {  
  6.     struct inode *inode = file->f_path.dentry->d_inode;  
  7.     struct tty_struct *tty = file_tty(file);  
  8.     struct tty_ldisc *ld;  
  9.     ssize_t ret;  
  10.   
  11.     if (tty_paranoia_check(tty, inode, "tty_write"))  
  12.         return -EIO;  
  13.     if (!tty || !tty->ops->write ||  
  14.         (test_bit(TTY_IO_ERROR, &tty->flags)))  
  15.             return -EIO;  
  16.     /* Short term debug to catch buggy drivers */  
  17.     if (tty->ops->write_room == NULL)  
  18.         printk(KERN_ERR "tty driver %s lacks a write_room method.\n",  
  19.             tty->driver->name);  
  20.     ld = tty_ldisc_ref_wait(tty);  
  21.     if (!ld->ops->write)  
  22.         ret = -EIO;  
  23.     else  
  24.         /*调用tty_ldisc_N_TTY中的write函数*/  
  25.         ret = do_tty_write(ld->ops->write, tty, file, buf, count);  
  26.     tty_ldisc_deref(ld);  
  27.     return ret;  
  28. }  
  29.   
  30. /*调用uart_ops中的write函数*/  
  31. static ssize_t n_tty_write(struct tty_struct *tty, struct file *file,const unsigned char *buf, size_t nr)  
  32. {  
  33.     c = tty->ops->write(tty, b, nr);  
  34. }  
  35.   
  36. static int uart_write(struct tty_struct *tty,const unsigned char *buf, int count)  
  37. {  
  38.     struct uart_state *state = tty->driver_data;  
  39.     struct uart_port *port;  
  40.     struct circ_buf *circ;  
  41.     unsigned long flags;  
  42.     int c, ret = 0;  
  43.   
  44.     /* 
  45.      * This means you called this function _after_ the port was 
  46.      * closed.  No cookie for you. 
  47.      */  
  48.     if (!state) {  
  49.         WARN_ON(1);  
  50.         return -EL3HLT;  
  51.     }  
  52.   
  53.   /*取出所对应的port和循环缓冲buf*/  
  54.     port = state->uart_port;  
  55.     circ = &state->xmit;  
  56.   
  57.     if (!circ->buf)  
  58.         return 0;  
  59.   
  60.     spin_lock_irqsave(&port->lock, flags);  
  61.     while (1) {  
  62.         /*计算循环缓冲的剩余空间 */  
  63.         c = CIRC_SPACE_TO_END(circ->head, circ->tail, UART_XMIT_SIZE);  
  64.         if (count < c)  
  65.             c = count;  
  66.         if (c <= 0)  
  67.             break;  
  68.         /*拷贝数据到循环缓冲区*/  
  69.         memcpy(circ->buf + circ->head, buf, c);  
  70.         /*调正循环缓冲head的位置*/  
  71.         circ->head = (circ->head + c) & (UART_XMIT_SIZE - 1);  
  72.         /*挪动buf当前的指针位置*/  
  73.         buf += c;  
  74.         count -= c;  
  75.         ret += c;  
  76.     }  
  77.     spin_unlock_irqrestore(&port->lock, flags);  
  78.   
  79.     uart_start(tty);  
  80.     return ret;  
  81. }  
  82.   
  83. /*判断循环缓冲是否为空等。 然后调用注册驱动时的ops。 也就是s3c24xx_serial_ops*/  
  84. static void __uart_start(struct tty_struct *tty)  
  85. {  
  86.     struct uart_state *state = tty->driver_data;  
  87.     struct uart_port *port = state->uart_port;  
  88.   
  89.     if (!uart_circ_empty(&state->xmit) && state->xmit.buf &&  
  90.         !tty->stopped && !tty->hw_stopped)  
  91.         port->ops->start_tx(port);  
  92. }  
  93.   
  94. /*判断端口是否使能,如果没使能则使能端口。 然后使能tx中断 
  95. * 使能tx中断,则当有数据来时,则会触发tx中断,调用中断函数 
  96. */  
  97. static void s3c24xx_serial_start_tx(struct uart_port *port)  
  98. {  
  99.     struct s3c24xx_uart_port *ourport = to_ourport(port);  
  100.     static int a =1;//temp  
  101.     if (port->line == 3) {  
  102. //      printk("485_start_tx\n");  
  103.   
  104.         if(a){  
  105.             s3c_gpio_cfgpin(S3C64XX_GPK(5), S3C_GPIO_SFN(1));  
  106.             a=0;  
  107.         }  
  108.         gpio_set_value(S3C64XX_GPK(5), 1);  
  109.     }  
  110.     if (!tx_enabled(port)) {  
  111.         if (port->flags & UPF_CONS_FLOW)  
  112.             s3c24xx_serial_rx_disable(port);  
  113.   
  114.         enable_irq(ourport->tx_irq);  
  115.         tx_enabled(port) = 1;  
  116.     }  
  117. }  
  118.   
  119. /*tx中断触发函数*/  
  120. static irqreturn_t s3c24xx_serial_tx_chars(int irq, void *id)  
  121. {  
  122.     struct s3c24xx_uart_port *ourport = id;  
  123.     struct uart_port *port = &ourport->port;  
  124.     struct circ_buf *xmit = &port->state->xmit;  
  125.     int count = 256;  
  126.   
  127.     /*判断x_char是否存在,如果存在则写进UTXH寄存器。清空x_char*/  
  128.     if (port->x_char) {  
  129.         wr_regb(port, S3C2410_UTXH, port->x_char);  
  130.         port->icount.tx++;  
  131.         port->x_char = 0;  
  132.         goto out;  
  133.     }  
  134.   
  135.     /* if there isn't anything more to transmit, or the uart is now 
  136.      * stopped, disable the uart and exit 
  137.     */  
  138.   
  139.     /*判断循环缓冲是否为空,或者tx是否为停止状态。 如果是则停止发送*/  
  140.     if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {  
  141.         s3c24xx_serial_stop_tx(port);  
  142.         goto out;  
  143.     }  
  144.   
  145.     /* try and drain the buffer... */  
  146.   
  147.     /*当循环缓冲buff不为空,而且count是大于0的。则进入while循环*/  
  148.     while (!uart_circ_empty(xmit) && count-- > 0) {  
  149.           
  150.         /*首先读取UFSTAT寄存器,然后判断tx_fifo是否为0.是则退出*/  
  151.         if (rd_regl(port, S3C2410_UFSTAT) & ourport->info->tx_fifofull)  
  152.             break;  
  153.   
  154.         /*然后将循环buff中的数据读出到UTXH寄存器。然后设置tail的指针位置*/  
  155.         wr_regb(port, S3C2410_UTXH, xmit->buf[xmit->tail]);  
  156.         xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);  
  157.         port->icount.tx++;  
  158.     }  
  159.   
  160.     /*判断循环缓冲中的数据是否小于WAKEUP_CHARS, 小于则启动接受*/  
  161.     if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)  
  162.         uart_write_wakeup(port);  
  163.           
  164.     /*如果循环buff为空,则停止发送,退出*/  
  165.     if (uart_circ_empty(xmit))  
  166.         s3c24xx_serial_stop_tx(port);  
  167. }  

猜你喜欢

转载自blog.csdn.net/lzpdz/article/details/68064172
今日推荐