usb转串口设备需要进行收发,主机接到uart rx线上的数据,把主机数据发送到 uart tx线上,对应的就是read函数和write函数,以上两个Part提到了tty有关的函数,在以下结构体被定义,write被指定为serial_write,read在open里面被调用
static const struct tty_operations serial_ops = {
.open = serial_open,
.close = serial_close,
.write = serial_write,
.hangup = serial_hangup,
.write_room = serial_write_room,
.ioctl = serial_ioctl,
.set_termios = serial_set_termios,
.throttle = serial_throttle,
.unthrottle = serial_unthrottle,
.break_ctl = serial_break,
.chars_in_buffer = serial_chars_in_buffer,
.wait_until_sent = serial_wait_until_sent,
.tiocmget = serial_tiocmget,
.tiocmset = serial_tiocmset,
.get_icount = serial_get_icount,
.set_serial = serial_set_serial,
.get_serial = serial_get_serial,
.cleanup = serial_cleanup,
.install = serial_install,
.proc_show = serial_proc_show,
};
write:
write的调用流程是这样的:
此write被指定为generic write:
static void usb_serial_operations_init(struct usb_serial_driver *device)
{
set_to_generic_if_null(device, open);
set_to_generic_if_null(device, write);
set_to_generic_if_null(device, close);
set_to_generic_if_null(device, write_room);
set_to_generic_if_null(device, chars_in_buffer);
if (device->tx_empty)
set_to_generic_if_null(device, wait_until_sent);
set_to_generic_if_null(device, read_bulk_callback);
set_to_generic_if_null(device, write_bulk_callback);
set_to_generic_if_null(device, process_read_urb);
set_to_generic_if_null(device, prepare_write_buffer);
}
在generic write中,buf也就是tty要下发的值,存在了write fifo里面
在write start里面,调用prepare_write_buffer,就像上面所说,也被指定为了generic
int usb_serial_generic_prepare_write_buffer(struct usb_serial_port *port,
void *dest, size_t size)
{
return kfifo_out_locked(&port->write_fifo, dest, size, &port->lock);
}
这个操作把write fifo里面的待发送数据放到urb里面,然后submit即可:
read:
可以看到,open usb转串口设备的时候就会开始read
最后会调用到read urb,然后submit urb即可