USB: create module_usb_serial_driver macro
authorGreg KH <gregkh@linuxfoundation.org>
Fri, 24 Feb 2012 23:38:14 +0000 (15:38 -0800)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Tue, 28 Feb 2012 19:21:48 +0000 (11:21 -0800)
Now that Alan Stern has cleaned up the usb serial driver registration,
we have the ability to create a module_usb_serial_driver macro to make
things a bit simpler, like the other *_driver macros created.

But, as we need two functions here, we can't reuse the existing
module_driver() macro, so we need to roll our own.

Here's a patch implementing module_usb_serial_driver() and it converts
the pl2303 driver to use it, showing a nice cleanup.

Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
drivers/usb/serial/pl2303.c
include/linux/usb/serial.h

index cc65d810c8f5c5ff7271397544a580279b5fb6e6..ff4a174fa5de8e536572584b32b08a41defc7e1e 100644 (file)
@@ -855,23 +855,7 @@ static struct usb_serial_driver * const serial_drivers[] = {
        &pl2303_device, NULL
 };
 
-static int __init pl2303_init(void)
-{
-       int retval;
-
-       retval = usb_serial_register_drivers(&pl2303_driver, serial_drivers);
-       if (retval == 0)
-               printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_DESC "\n");
-       return retval;
-}
-
-static void __exit pl2303_exit(void)
-{
-       usb_serial_deregister_drivers(&pl2303_driver, serial_drivers);
-}
-
-module_init(pl2303_init);
-module_exit(pl2303_exit);
+module_usb_serial_driver(pl2303_driver, serial_drivers);
 
 MODULE_DESCRIPTION(DRIVER_DESC);
 MODULE_LICENSE("GPL");
index 34c06a723dcc03fffdfc1193ee544e74eed85b86..7b1db841e2a895d5e3d03dfe7de7e1b5d4157dc1 100644 (file)
@@ -406,5 +406,33 @@ do {                                                                       \
        }                                                               \
 } while (0)
 
+/*
+ * module_usb_serial_driver() - Helper macro for registering a USB Serial driver
+ * @__usb_driver: usb_driver struct to register
+ * @__serial_drivers: list of usb_serial drivers to register
+ *
+ * Helper macro for USB serial drivers which do not do anything special
+ * in module init/exit. This eliminates a lot of boilerplate. Each
+ * module may only use this macro once, and calling it replaces
+ * module_init() and module_exit()
+ *
+ * Note, we can't use the generic module_driver() call here, due to the
+ * two parameters in the usb_serial_* functions, so we roll our own here
+ * :(
+ */
+#define module_usb_serial_driver(__usb_driver, __serial_drivers)       \
+static int __init usb_serial_driver_init(void)                         \
+{                                                                      \
+       return usb_serial_register_drivers(&(__usb_driver),             \
+                                          (__serial_drivers));         \
+}                                                                      \
+module_init(usb_serial_driver_init);                                   \
+static void __exit usb_serial_driver_exit(void)                                \
+{                                                                      \
+       return usb_serial_deregister_drivers(&(__usb_driver),           \
+                                            (__serial_drivers));       \
+}                                                                      \
+module_exit(usb_serial_driver_exit);
+
 #endif /* __LINUX_USB_SERIAL_H */