最近在调试一个usb转串口的设备,发现插着开机和开机后再插,生成的设备节点不一样。原因是机器有接4G模块,不接usb转串口设备开机时4G模块就会分配ttyUSB0~ttyUSB3这四个节点,接usb转串口设备开机时,就为其分配了节点ttyUSB0,4G模块为ttyUSB1~ttyUSB4。以USB转串口为例,通常设备节点名为ttyUSBx(x为0~n),Linux内核会根据插入设备的先后顺序进行编号的分配,比如第一个插入的设备编号为0,然后依此加1。
为了保证插某个usbx口,就对应节点ttyUSBx,可以USB的设备名去识别USB串口插入的是哪个U口,再不同的U口给他分配一个不同的节点。笔者这里只设置了两个usb口,USB转串设备插入第一个USB口,dev名字为2-1.3,插入第二个USB口名字为2-1.3,分别对应ttyUSB5和ttyUSB6。代码实现如下:
--- a/drivers/usb/serial/usb-serial.c +++ b/drivers/usb/serial/usb-serial.c @@ -85,7 +85,7 @@ exit: return port; } -static int allocate_minors(struct usb_serial *serial, int num_ports) +static int allocate_minors(struct usb_serial *serial, int num_ports, const char *dev_name) { struct usb_serial_port *port; unsigned int i, j; @@ -97,6 +97,16 @@ static int allocate_minors(struct usb_serial *serial, int num_ports) for (i = 0; i < num_ports; ++i) { port = serial->port[i]; minor = idr_alloc(&serial_minors, port, 0, 0, GFP_KERNEL); + //wmc add start + if(!strcmp(dev_name,"2-1.3")) + { + minor = 5; + } + else if(!strcmp(dev_name,"2-1.1")) + { + minor = 6; + } + //wmc add end if (minor < 0) goto error; port->minor = minor; @@ -1058,7 +1068,7 @@ static int usb_serial_probe(struct usb_interface *interface, */ serial->disconnected = 1; - if (allocate_minors(serial, num_ports)) { + if (allocate_minors(serial, num_ports,dev_name(&port->serial->dev->dev))) { dev_err(ddev, "No more free serial minor numbers "); goto probe_error; }
参考:https://blog.csdn.net/mrdeath/article/details/107506452
https://blog.csdn.net/kangear/article/details/46302741?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-4.control&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-4.control