diff options
author | Alan Stern <stern@rowland.harvard.edu> | 2012-02-23 14:55:59 -0500 |
---|---|---|
committer | Greg Kroah-Hartman <gregkh@linuxfoundation.org> | 2012-02-24 15:39:17 -0500 |
commit | 765e0ba62613fb90f09c1b5926750df0aa56f349 (patch) | |
tree | a283130f8c0e4b70978e24012fbeafe4001b5dea /drivers | |
parent | c69263c66e5b2a5d0c7e5a41c189b1846ae1de92 (diff) |
usb-serial: new API for driver registration
This patch (as1522) adds two new routines to the usb-serial core, for
registering and unregistering serial drivers. Instead of registering
the usb_driver and usb_serial_drivers separately, with error checking
for each one, the drivers can all be registered and unregistered by a
single function call. This reduces duplicated code.
More importantly, the new core routines change the order in which the
drivers are registered. Currently the usb-serial drivers are all
registered first and the usb_driver is done last, which leaves a
window for problems. A udev script may quickly add a new dynamic-ID
for a usb-serial driver, causing the corresponding usb_driver to be
probed. If the usb_driver hasn't been registered yet then an oops
will occur.
The new routine prevents such problems by registering the usb_driver
first. To insure that it gets probed properly for already-attached
serial devices, we call driver_attach() after all the usb-serial
drivers have been registered.
Along with adding the new routines, the patch modifies the "generic"
serial driver to use them. Further patches will similarly modify all
the other in-tree USB serial drivers.
Signed-off-by: Alan Stern <stern@rowland.harvard.edu>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Diffstat (limited to 'drivers')
-rw-r--r-- | drivers/usb/serial/generic.c | 17 | ||||
-rw-r--r-- | drivers/usb/serial/usb-serial.c | 75 |
2 files changed, 81 insertions, 11 deletions
diff --git a/drivers/usb/serial/generic.c b/drivers/usb/serial/generic.c index 2a2fa2d0489..664deb63807 100644 --- a/drivers/usb/serial/generic.c +++ b/drivers/usb/serial/generic.c | |||
@@ -54,7 +54,6 @@ static struct usb_driver generic_driver = { | |||
54 | .probe = generic_probe, | 54 | .probe = generic_probe, |
55 | .disconnect = usb_serial_disconnect, | 55 | .disconnect = usb_serial_disconnect, |
56 | .id_table = generic_serial_ids, | 56 | .id_table = generic_serial_ids, |
57 | .no_dynamic_id = 1, | ||
58 | }; | 57 | }; |
59 | 58 | ||
60 | /* All of the device info needed for the Generic Serial Converter */ | 59 | /* All of the device info needed for the Generic Serial Converter */ |
@@ -64,7 +63,6 @@ struct usb_serial_driver usb_serial_generic_device = { | |||
64 | .name = "generic", | 63 | .name = "generic", |
65 | }, | 64 | }, |
66 | .id_table = generic_device_ids, | 65 | .id_table = generic_device_ids, |
67 | .usb_driver = &generic_driver, | ||
68 | .num_ports = 1, | 66 | .num_ports = 1, |
69 | .disconnect = usb_serial_generic_disconnect, | 67 | .disconnect = usb_serial_generic_disconnect, |
70 | .release = usb_serial_generic_release, | 68 | .release = usb_serial_generic_release, |
@@ -73,6 +71,10 @@ struct usb_serial_driver usb_serial_generic_device = { | |||
73 | .resume = usb_serial_generic_resume, | 71 | .resume = usb_serial_generic_resume, |
74 | }; | 72 | }; |
75 | 73 | ||
74 | static struct usb_serial_driver * const serial_drivers[] = { | ||
75 | &usb_serial_generic_device, NULL | ||
76 | }; | ||
77 | |||
76 | static int generic_probe(struct usb_interface *interface, | 78 | static int generic_probe(struct usb_interface *interface, |
77 | const struct usb_device_id *id) | 79 | const struct usb_device_id *id) |
78 | { | 80 | { |
@@ -97,13 +99,7 @@ int usb_serial_generic_register(int _debug) | |||
97 | USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_PRODUCT; | 99 | USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_PRODUCT; |
98 | 100 | ||
99 | /* register our generic driver with ourselves */ | 101 | /* register our generic driver with ourselves */ |
100 | retval = usb_serial_register(&usb_serial_generic_device); | 102 | retval = usb_serial_register_drivers(&generic_driver, serial_drivers); |
101 | if (retval) | ||
102 | goto exit; | ||
103 | retval = usb_register(&generic_driver); | ||
104 | if (retval) | ||
105 | usb_serial_deregister(&usb_serial_generic_device); | ||
106 | exit: | ||
107 | #endif | 103 | #endif |
108 | return retval; | 104 | return retval; |
109 | } | 105 | } |
@@ -112,8 +108,7 @@ void usb_serial_generic_deregister(void) | |||
112 | { | 108 | { |
113 | #ifdef CONFIG_USB_SERIAL_GENERIC | 109 | #ifdef CONFIG_USB_SERIAL_GENERIC |
114 | /* remove our generic driver */ | 110 | /* remove our generic driver */ |
115 | usb_deregister(&generic_driver); | 111 | usb_serial_deregister_drivers(&generic_driver, serial_drivers); |
116 | usb_serial_deregister(&usb_serial_generic_device); | ||
117 | #endif | 112 | #endif |
118 | } | 113 | } |
119 | 114 | ||
diff --git a/drivers/usb/serial/usb-serial.c b/drivers/usb/serial/usb-serial.c index 611b206591c..45b3658c601 100644 --- a/drivers/usb/serial/usb-serial.c +++ b/drivers/usb/serial/usb-serial.c | |||
@@ -1338,6 +1338,11 @@ static void fixup_generic(struct usb_serial_driver *device) | |||
1338 | set_to_generic_if_null(device, prepare_write_buffer); | 1338 | set_to_generic_if_null(device, prepare_write_buffer); |
1339 | } | 1339 | } |
1340 | 1340 | ||
1341 | /* | ||
1342 | * The next two routines are mainly for internal use. | ||
1343 | * They are exported only for out-of-tree modules. | ||
1344 | * New drivers should call usb_serial_{de}register_drivers() instead. | ||
1345 | */ | ||
1341 | int usb_serial_register(struct usb_serial_driver *driver) | 1346 | int usb_serial_register(struct usb_serial_driver *driver) |
1342 | { | 1347 | { |
1343 | int retval; | 1348 | int retval; |
@@ -1386,6 +1391,76 @@ void usb_serial_deregister(struct usb_serial_driver *device) | |||
1386 | } | 1391 | } |
1387 | EXPORT_SYMBOL_GPL(usb_serial_deregister); | 1392 | EXPORT_SYMBOL_GPL(usb_serial_deregister); |
1388 | 1393 | ||
1394 | /** | ||
1395 | * usb_serial_register_drivers - register drivers for a usb-serial module | ||
1396 | * @udriver: usb_driver used for matching devices/interfaces | ||
1397 | * @serial_drivers: NULL-terminated array of pointers to drivers to be registered | ||
1398 | * | ||
1399 | * Registers @udriver and all the drivers in the @serial_drivers array. | ||
1400 | * Automatically fills in the .no_dynamic_id field in @udriver and | ||
1401 | * the .usb_driver field in each serial driver. | ||
1402 | */ | ||
1403 | int usb_serial_register_drivers(struct usb_driver *udriver, | ||
1404 | struct usb_serial_driver * const serial_drivers[]) | ||
1405 | { | ||
1406 | int rc; | ||
1407 | const struct usb_device_id *saved_id_table; | ||
1408 | struct usb_serial_driver * const *sd; | ||
1409 | |||
1410 | /* | ||
1411 | * udriver must be registered before any of the serial drivers, | ||
1412 | * because the store_new_id() routine for the serial drivers (in | ||
1413 | * bus.c) probes udriver. | ||
1414 | * | ||
1415 | * Performance hack: We don't want udriver to be probed until | ||
1416 | * the serial drivers are registered, because the probe would | ||
1417 | * simply fail for lack of a matching serial driver. | ||
1418 | * Therefore save off udriver's id_table until we are all set. | ||
1419 | */ | ||
1420 | saved_id_table = udriver->id_table; | ||
1421 | udriver->id_table = NULL; | ||
1422 | |||
1423 | udriver->no_dynamic_id = 1; | ||
1424 | rc = usb_register(udriver); | ||
1425 | if (rc) | ||
1426 | return rc; | ||
1427 | |||
1428 | for (sd = serial_drivers; *sd; ++sd) { | ||
1429 | (*sd)->usb_driver = udriver; | ||
1430 | rc = usb_serial_register(*sd); | ||
1431 | if (rc) | ||
1432 | goto failed; | ||
1433 | } | ||
1434 | |||
1435 | /* Now restore udriver's id_table and look for matches */ | ||
1436 | udriver->id_table = saved_id_table; | ||
1437 | rc = driver_attach(&udriver->drvwrap.driver); | ||
1438 | return 0; | ||
1439 | |||
1440 | failed: | ||
1441 | while (sd-- > serial_drivers) | ||
1442 | usb_serial_deregister(*sd); | ||
1443 | usb_deregister(udriver); | ||
1444 | return rc; | ||
1445 | } | ||
1446 | EXPORT_SYMBOL_GPL(usb_serial_register_drivers); | ||
1447 | |||
1448 | /** | ||
1449 | * usb_serial_deregister_drivers - deregister drivers for a usb-serial module | ||
1450 | * @udriver: usb_driver to unregister | ||
1451 | * @serial_drivers: NULL-terminated array of pointers to drivers to be deregistered | ||
1452 | * | ||
1453 | * Deregisters @udriver and all the drivers in the @serial_drivers array. | ||
1454 | */ | ||
1455 | void usb_serial_deregister_drivers(struct usb_driver *udriver, | ||
1456 | struct usb_serial_driver * const serial_drivers[]) | ||
1457 | { | ||
1458 | for (; *serial_drivers; ++serial_drivers) | ||
1459 | usb_serial_deregister(*serial_drivers); | ||
1460 | usb_deregister(udriver); | ||
1461 | } | ||
1462 | EXPORT_SYMBOL_GPL(usb_serial_deregister_drivers); | ||
1463 | |||
1389 | /* Module information */ | 1464 | /* Module information */ |
1390 | MODULE_AUTHOR(DRIVER_AUTHOR); | 1465 | MODULE_AUTHOR(DRIVER_AUTHOR); |
1391 | MODULE_DESCRIPTION(DRIVER_DESC); | 1466 | MODULE_DESCRIPTION(DRIVER_DESC); |