From 1143832eca8f1d64da7d85642c956ae9d25c69e1 Mon Sep 17 00:00:00 2001 From: Greg Kroah-Hartman Date: Thu, 6 Jun 2013 10:32:00 -0700 Subject: USB: serial: ports: add minor and port number The usb_serial_port structure had the number field, which was the minor number for the port, which almost no one really cared about. They really wanted the number of the port within the device, which you had to subtract from the minor of the parent usb_serial_device structure. To clean this up, provide the real minor number of the port, and the number of the port within the serial device separately, as these numbers might not be related in the future. Bonus is that this cleans up a lot of logic in the drivers, and saves lines overall. Tested-by: Tobias Winter Reviewed-by: Johan Hovold Signed-off-by: Greg Kroah-Hartman -- drivers/staging/serqt_usb2/serqt_usb2.c | 21 +++-------- drivers/usb/serial/ark3116.c | 2 - drivers/usb/serial/bus.c | 6 +-- drivers/usb/serial/console.c | 2 - drivers/usb/serial/cp210x.c | 2 - drivers/usb/serial/cypress_m8.c | 4 +- drivers/usb/serial/digi_acceleport.c | 6 --- drivers/usb/serial/f81232.c | 5 +- drivers/usb/serial/garmin_gps.c | 6 +-- drivers/usb/serial/io_edgeport.c | 58 ++++++++++++-------------------- drivers/usb/serial/io_ti.c | 21 ++++------- drivers/usb/serial/keyspan.c | 29 +++++++--------- drivers/usb/serial/metro-usb.c | 4 +- drivers/usb/serial/mos7720.c | 37 +++++++++----------- drivers/usb/serial/mos7840.c | 52 +++++++++------------------- drivers/usb/serial/opticon.c | 2 - drivers/usb/serial/pl2303.c | 2 - drivers/usb/serial/quatech2.c | 7 +-- drivers/usb/serial/sierra.c | 2 - drivers/usb/serial/ti_usb_3410_5052.c | 10 ++--- drivers/usb/serial/usb-serial.c | 7 ++- drivers/usb/serial/usb_wwan.c | 2 - drivers/usb/serial/whiteheat.c | 20 +++++------ include/linux/usb/serial.h | 6 ++- 24 files changed, 133 insertions(+), 180 deletions(-) --- drivers/usb/serial/usb-serial.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'drivers/usb/serial/usb-serial.c') diff --git a/drivers/usb/serial/usb-serial.c b/drivers/usb/serial/usb-serial.c index 5f6b1ff9d29e..a47fa715aaba 100644 --- a/drivers/usb/serial/usb-serial.c +++ b/drivers/usb/serial/usb-serial.c @@ -105,9 +105,10 @@ static struct usb_serial *get_free_serial(struct usb_serial *serial, *minor = i; j = 0; dev_dbg(&serial->interface->dev, "%s - minor base = %d\n", __func__, *minor); - for (i = *minor; (i < (*minor + num_ports)) && (i < SERIAL_TTY_MINORS); ++i) { + for (i = *minor; (i < (*minor + num_ports)) && (i < SERIAL_TTY_MINORS); ++i, ++j) { serial_table[i] = serial; - serial->port[j++]->number = i; + serial->port[j]->minor = i; + serial->port[j]->port_number = i - *minor; } mutex_unlock(&table_lock); return serial; @@ -1048,7 +1049,7 @@ static int usb_serial_probe(struct usb_interface *interface, /* register all of the individual ports with the driver core */ for (i = 0; i < num_ports; ++i) { port = serial->port[i]; - dev_set_name(&port->dev, "ttyUSB%d", port->number); + dev_set_name(&port->dev, "ttyUSB%d", port->minor); dev_dbg(ddev, "registering %s", dev_name(&port->dev)); device_enable_async_suspend(&port->dev); -- cgit v1.2.3 From e5b1e2062e0535e8ffef79bb34d857e21380d101 Mon Sep 17 00:00:00 2001 From: Greg Kroah-Hartman Date: Fri, 7 Jun 2013 11:04:28 -0700 Subject: USB: serial: make minor allocation dynamic This moves the allocation of minor device numbers from a static array to be dynamic, using the idr interface. This means that you could potentially get "gaps" in a minor number range for a single USB serial device with multiple ports, but all should still work properly. We remove the 'minor' field from the usb_serial structure, as it no longer makes any sense for it (use the field in the usb_serial_port structure if you really want to know this number), and take the fact that we were overloading a number in this field to determine if we had initialized the minor numbers or not, and just use a flag variable instead. Note, we still have the limitation of 255 USB to serial devices in the system, as that is all we are registering with the TTY layer at this point in time. Tested-by: Tobias Winter Reviewed-by: Johan Hovold Signed-off-by: Greg Kroah-Hartman --- drivers/usb/serial/usb-serial.c | 123 ++++++++++++++++++---------------------- 1 file changed, 55 insertions(+), 68 deletions(-) (limited to 'drivers/usb/serial/usb-serial.c') diff --git a/drivers/usb/serial/usb-serial.c b/drivers/usb/serial/usb-serial.c index a47fa715aaba..e2e131f85002 100644 --- a/drivers/usb/serial/usb-serial.c +++ b/drivers/usb/serial/usb-serial.c @@ -37,6 +37,7 @@ #include #include #include +#include #include "pl2303.h" #define DRIVER_AUTHOR "Greg Kroah-Hartman " @@ -49,82 +50,75 @@ drivers depend on it. */ -static struct usb_serial *serial_table[SERIAL_TTY_MINORS]; +static DEFINE_IDR(serial_minors); static DEFINE_MUTEX(table_lock); static LIST_HEAD(usb_serial_driver_list); /* - * Look up the serial structure. If it is found and it hasn't been - * disconnected, return with its disc_mutex held and its refcount - * incremented. Otherwise return NULL. + * Look up the serial port structure. If it is found and it hasn't been + * disconnected, return with the parent usb_serial structure's disc_mutex held + * and its refcount incremented. Otherwise return NULL. */ -struct usb_serial *usb_serial_get_by_index(unsigned index) +struct usb_serial_port *usb_serial_port_get_by_minor(unsigned minor) { struct usb_serial *serial; + struct usb_serial_port *port; mutex_lock(&table_lock); - serial = serial_table[index]; - - if (serial) { - mutex_lock(&serial->disc_mutex); - if (serial->disconnected) { - mutex_unlock(&serial->disc_mutex); - serial = NULL; - } else { - kref_get(&serial->kref); - } + port = idr_find(&serial_minors, minor); + if (!port) + goto exit; + + serial = port->serial; + mutex_lock(&serial->disc_mutex); + if (serial->disconnected) { + mutex_unlock(&serial->disc_mutex); + port = NULL; + } else { + kref_get(&serial->kref); } +exit: mutex_unlock(&table_lock); - return serial; + return port; } -static struct usb_serial *get_free_serial(struct usb_serial *serial, - int num_ports, unsigned int *minor) +static int allocate_minors(struct usb_serial *serial, int num_ports) { + struct usb_serial_port *port; unsigned int i, j; - int good_spot; + int minor; dev_dbg(&serial->interface->dev, "%s %d\n", __func__, num_ports); - *minor = 0; mutex_lock(&table_lock); - for (i = 0; i < SERIAL_TTY_MINORS; ++i) { - if (serial_table[i]) - continue; - - good_spot = 1; - for (j = 1; j <= num_ports-1; ++j) - if ((i+j >= SERIAL_TTY_MINORS) || (serial_table[i+j])) { - good_spot = 0; - i += j; - break; - } - if (good_spot == 0) - continue; - - *minor = i; - j = 0; - dev_dbg(&serial->interface->dev, "%s - minor base = %d\n", __func__, *minor); - for (i = *minor; (i < (*minor + num_ports)) && (i < SERIAL_TTY_MINORS); ++i, ++j) { - serial_table[i] = serial; - serial->port[j]->minor = i; - serial->port[j]->port_number = i - *minor; - } - mutex_unlock(&table_lock); - return serial; + for (i = 0; i < num_ports; ++i) { + port = serial->port[i]; + minor = idr_alloc(&serial_minors, port, 0, 0, GFP_KERNEL); + if (minor < 0) + goto error; + port->minor = minor; + port->port_number = i; } + serial->minors_reserved = 1; mutex_unlock(&table_lock); - return NULL; + return 0; +error: + /* unwind the already allocated minors */ + for (j = 0; j < i; ++j) + idr_remove(&serial_minors, serial->port[j]->minor); + mutex_unlock(&table_lock); + return minor; } -static void return_serial(struct usb_serial *serial) +static void release_minors(struct usb_serial *serial) { int i; mutex_lock(&table_lock); for (i = 0; i < serial->num_ports; ++i) - serial_table[serial->minor + i] = NULL; + idr_remove(&serial_minors, serial->port[i]->minor); mutex_unlock(&table_lock); + serial->minors_reserved = 0; } static void destroy_serial(struct kref *kref) @@ -136,8 +130,8 @@ static void destroy_serial(struct kref *kref) serial = to_usb_serial(kref); /* return the minor range that this device had */ - if (serial->minor != SERIAL_TTY_NO_MINOR) - return_serial(serial); + if (serial->minors_reserved) + release_minors(serial); if (serial->attached && serial->type->release) serial->type->release(serial); @@ -186,13 +180,11 @@ static int serial_install(struct tty_driver *driver, struct tty_struct *tty) struct usb_serial_port *port; int retval = -ENODEV; - serial = usb_serial_get_by_index(idx); - if (!serial) + port = usb_serial_port_get_by_minor(idx); + if (!port) return retval; - port = serial->port[idx - serial->minor]; - if (!port) - goto error_no_port; + serial = port->serial; if (!try_module_get(serial->type->driver.owner)) goto error_module_get; @@ -219,7 +211,6 @@ static int serial_install(struct tty_driver *driver, struct tty_struct *tty) error_get_interface: module_put(serial->type->driver.owner); error_module_get: - error_no_port: usb_serial_put(serial); mutex_unlock(&serial->disc_mutex); return retval; @@ -453,14 +444,16 @@ static int serial_break(struct tty_struct *tty, int break_state) static int serial_proc_show(struct seq_file *m, void *v) { struct usb_serial *serial; + struct usb_serial_port *port; int i; char tmp[40]; seq_puts(m, "usbserinfo:1.0 driver:2.0\n"); for (i = 0; i < SERIAL_TTY_MINORS; ++i) { - serial = usb_serial_get_by_index(i); - if (serial == NULL) + port = usb_serial_port_get_by_minor(i); + if (port == NULL) continue; + serial = port->serial; seq_printf(m, "%d:", i); if (serial->type->driver.owner) @@ -472,7 +465,7 @@ static int serial_proc_show(struct seq_file *m, void *v) le16_to_cpu(serial->dev->descriptor.idVendor), le16_to_cpu(serial->dev->descriptor.idProduct)); seq_printf(m, " num_ports:%d", serial->num_ports); - seq_printf(m, " port:%d", i - serial->minor + 1); + seq_printf(m, " port:%d", port->port_number); usb_make_path(serial->dev, tmp, sizeof(tmp)); seq_printf(m, " path:%s", tmp); @@ -614,7 +607,7 @@ static struct usb_serial *create_serial(struct usb_device *dev, serial->interface = usb_get_intf(interface); kref_init(&serial->kref); mutex_init(&serial->disc_mutex); - serial->minor = SERIAL_TTY_NO_MINOR; + serial->minors_reserved = 0; return serial; } @@ -723,7 +716,6 @@ static int usb_serial_probe(struct usb_interface *interface, struct usb_endpoint_descriptor *bulk_out_endpoint[MAX_NUM_PORTS]; struct usb_serial_driver *type = NULL; int retval; - unsigned int minor; int buffer_size; int i; int j; @@ -1040,11 +1032,10 @@ static int usb_serial_probe(struct usb_interface *interface, */ serial->disconnected = 1; - if (get_free_serial(serial, num_ports, &minor) == NULL) { - dev_err(ddev, "No more free serial devices\n"); + if (allocate_minors(serial, num_ports)) { + dev_err(ddev, "No more free serial minor numbers\n"); goto probe_error; } - serial->minor = minor; /* register all of the individual ports with the driver core */ for (i = 0; i < num_ports; ++i) { @@ -1060,7 +1051,7 @@ static int usb_serial_probe(struct usb_interface *interface, serial->disconnected = 0; - usb_serial_console_init(minor); + usb_serial_console_init(serial->port[0]->minor); exit: module_put(type->driver.owner); return 0; @@ -1224,7 +1215,6 @@ static struct usb_driver usb_serial_driver = { static int __init usb_serial_init(void) { - int i; int result; usb_serial_tty_driver = alloc_tty_driver(SERIAL_TTY_MINORS); @@ -1232,9 +1222,6 @@ static int __init usb_serial_init(void) return -ENOMEM; /* Initialize our global data */ - for (i = 0; i < SERIAL_TTY_MINORS; ++i) - serial_table[i] = NULL; - result = bus_register(&usb_serial_bus_type); if (result) { pr_err("%s - registering bus driver failed\n", __func__); -- cgit v1.2.3 From 455b4f7e18e741c0603f9030f9a1897f4c5150c1 Mon Sep 17 00:00:00 2001 From: Greg Kroah-Hartman Date: Thu, 6 Jun 2013 10:31:35 -0700 Subject: USB: serial: increase the number of devices we support We had the limit of 255 USB to serial devices on one system for almost 15 years, with no complaints. But now it's time to move on from these tiny "baby" systems, and bump the number up to 512, which should last us a few more years: "512 is a nice number" -- Tobias Winter Note, this is still a static value, and uses up tty core memory with this many tty devices allocated. Converting the driver to use TTY_DRIVER_DYNAMIC_DEV is the next thing to do in order to remove this limitation. Reported-by: Tobias Winter Tested-by: Tobias Winter Reviewed-by: Johan Hovold Signed-off-by: Greg Kroah-Hartman --- drivers/usb/serial/usb-serial.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'drivers/usb/serial/usb-serial.c') diff --git a/drivers/usb/serial/usb-serial.c b/drivers/usb/serial/usb-serial.c index e2e131f85002..cb27fcb2fc90 100644 --- a/drivers/usb/serial/usb-serial.c +++ b/drivers/usb/serial/usb-serial.c @@ -43,6 +43,9 @@ #define DRIVER_AUTHOR "Greg Kroah-Hartman " #define DRIVER_DESC "USB Serial Driver core" +#define USB_SERIAL_TTY_MAJOR 188 +#define USB_SERIAL_TTY_MINORS 512 /* should be enough for a while */ + /* There is no MODULE_DEVICE_TABLE for usbserial.c. Instead the MODULE_DEVICE_TABLE declarations in each serial driver cause the "hotplug" program to pull in whatever module is necessary @@ -449,7 +452,7 @@ static int serial_proc_show(struct seq_file *m, void *v) char tmp[40]; seq_puts(m, "usbserinfo:1.0 driver:2.0\n"); - for (i = 0; i < SERIAL_TTY_MINORS; ++i) { + for (i = 0; i < USB_SERIAL_TTY_MINORS; ++i) { port = usb_serial_port_get_by_minor(i); if (port == NULL) continue; @@ -1217,7 +1220,7 @@ static int __init usb_serial_init(void) { int result; - usb_serial_tty_driver = alloc_tty_driver(SERIAL_TTY_MINORS); + usb_serial_tty_driver = alloc_tty_driver(USB_SERIAL_TTY_MINORS); if (!usb_serial_tty_driver) return -ENOMEM; @@ -1230,7 +1233,7 @@ static int __init usb_serial_init(void) usb_serial_tty_driver->driver_name = "usbserial"; usb_serial_tty_driver->name = "ttyUSB"; - usb_serial_tty_driver->major = SERIAL_TTY_MAJOR; + usb_serial_tty_driver->major = USB_SERIAL_TTY_MAJOR; usb_serial_tty_driver->minor_start = 0; usb_serial_tty_driver->type = TTY_DRIVER_TYPE_SERIAL; usb_serial_tty_driver->subtype = SERIAL_TYPE_NORMAL; -- cgit v1.2.3