linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [RFC] USB: create module_usb_serial_driver macro
@ 2012-02-24 23:38 Greg KH
  2012-02-25 11:53 ` Lars-Peter Clausen
  0 siblings, 1 reply; 4+ messages in thread
From: Greg KH @ 2012-02-24 23:38 UTC (permalink / raw)
  To: Lars-Peter Clausen; +Cc: Alan Stern, linux-usb, linux-kernel

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.

Lars-Peter, or anyone else, am I missing something here and we can use
module_driver() somehow, or even modify it, to work for subsystems that
need 2 parameters for their function calls to register/deregister?

Actually, if we want to be tricky, we can go back to assigning the
usb_driver in the usb_serial_driver structure, and just use that pointer
in the first structure in the list as that option, but that's getting
pretty opaque just to reuse a single macro...

Thoughts anyone?

thanks,

greg k-h


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>



--- a/drivers/usb/serial/pl2303.c
+++ b/drivers/usb/serial/pl2303.c
@@ -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");
diff --git a/include/linux/usb/serial.h b/include/linux/usb/serial.h
index 34c06a7..7b1db84 100644
--- a/include/linux/usb/serial.h
+++ b/include/linux/usb/serial.h
@@ -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 */
 

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [RFC] USB: create module_usb_serial_driver macro
  2012-02-24 23:38 [RFC] USB: create module_usb_serial_driver macro Greg KH
@ 2012-02-25 11:53 ` Lars-Peter Clausen
  2012-03-10  0:30   ` Greg KH
  0 siblings, 1 reply; 4+ messages in thread
From: Lars-Peter Clausen @ 2012-02-25 11:53 UTC (permalink / raw)
  To: Greg KH; +Cc: Alan Stern, linux-usb, linux-kernel

On 02/25/2012 12:38 AM, Greg KH wrote:
> 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.
> 
> Lars-Peter, or anyone else, am I missing something here and we can use
> module_driver() somehow, or even modify it, to work for subsystems that
> need 2 parameters for their function calls to register/deregister?
> 

I suppose we can make module_driver a variadic macro and pass any additional
parameters to the register and unregister functions. Patch attached.

- Lars

8<-------------------------------------------------------------------->8
>From 0d731970249b787748eccd6e81890c012a44e5a6 Mon Sep 17 00:00:00 2001
From: Lars-Peter Clausen <lars@metafoo.de>
Date: Sat, 25 Feb 2012 11:25:58 +0100
Subject: [PATCH] driver-core: Allow additional parameters for module_driver

Allow module_driver take additional parameters which will be passed to the
register and unregister function calls. This allows it to be used in cases
where additional parameters are required (e.g. usb_serial_register_drivers).

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
---
 include/linux/device.h |    7 ++++---
 1 files changed, 4 insertions(+), 3 deletions(-)

diff --git a/include/linux/device.h b/include/linux/device.h
index b63fb39..bccccef 100644
--- a/include/linux/device.h
+++ b/include/linux/device.h
@@ -1007,19 +1007,20 @@ extern long sysfs_deprecated;
  * @__driver: driver name
  * @__register: register function for this driver type
  * @__unregister: unregister function for this driver type
+ * @...: Additional arguments to be passed to __register and __unregister.
  *
  * Use this macro to construct bus specific macros for registering
  * drivers, and do not use it on its own.
  */
-#define module_driver(__driver, __register, __unregister) \
+#define module_driver(__driver, __register, __unregister, ...) \
 static int __init __driver##_init(void) \
 { \
-	return __register(&(__driver)); \
+	return __register(&(__driver) , ##__VA_ARGS__); \
 } \
 module_init(__driver##_init); \
 static void __exit __driver##_exit(void) \
 { \
-	__unregister(&(__driver)); \
+	__unregister(&(__driver) , ##__VA_ARGS__); \
 } \
 module_exit(__driver##_exit);

-- 
1.7.2.5

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [RFC] USB: create module_usb_serial_driver macro
  2012-02-25 11:53 ` Lars-Peter Clausen
@ 2012-03-10  0:30   ` Greg KH
  0 siblings, 0 replies; 4+ messages in thread
From: Greg KH @ 2012-03-10  0:30 UTC (permalink / raw)
  To: Lars-Peter Clausen; +Cc: Alan Stern, linux-usb, linux-kernel

On Sat, Feb 25, 2012 at 12:53:51PM +0100, Lars-Peter Clausen wrote:
> On 02/25/2012 12:38 AM, Greg KH wrote:
> > 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.
> > 
> > Lars-Peter, or anyone else, am I missing something here and we can use
> > module_driver() somehow, or even modify it, to work for subsystems that
> > need 2 parameters for their function calls to register/deregister?
> > 
> 
> I suppose we can make module_driver a variadic macro and pass any additional
> parameters to the register and unregister functions. Patch attached.
> 
> - Lars
> 
> 8<-------------------------------------------------------------------->8
> >From 0d731970249b787748eccd6e81890c012a44e5a6 Mon Sep 17 00:00:00 2001
> From: Lars-Peter Clausen <lars@metafoo.de>
> Date: Sat, 25 Feb 2012 11:25:58 +0100
> Subject: [PATCH] driver-core: Allow additional parameters for module_driver
> 
> Allow module_driver take additional parameters which will be passed to the
> register and unregister function calls. This allows it to be used in cases
> where additional parameters are required (e.g. usb_serial_register_drivers).
> 
> Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
> ---
>  include/linux/device.h |    7 ++++---
>  1 files changed, 4 insertions(+), 3 deletions(-)
> 
> diff --git a/include/linux/device.h b/include/linux/device.h
> index b63fb39..bccccef 100644
> --- a/include/linux/device.h
> +++ b/include/linux/device.h
> @@ -1007,19 +1007,20 @@ extern long sysfs_deprecated;
>   * @__driver: driver name
>   * @__register: register function for this driver type
>   * @__unregister: unregister function for this driver type
> + * @...: Additional arguments to be passed to __register and __unregister.
>   *
>   * Use this macro to construct bus specific macros for registering
>   * drivers, and do not use it on its own.
>   */
> -#define module_driver(__driver, __register, __unregister) \
> +#define module_driver(__driver, __register, __unregister, ...) \
>  static int __init __driver##_init(void) \
>  { \
> -	return __register(&(__driver)); \
> +	return __register(&(__driver) , ##__VA_ARGS__); \
>  } \
>  module_init(__driver##_init); \
>  static void __exit __driver##_exit(void) \
>  { \
> -	__unregister(&(__driver)); \
> +	__unregister(&(__driver) , ##__VA_ARGS__); \
>  } \
>  module_exit(__driver##_exit);

Nice, let me play with this and see how it works out, thanks.

greg k-h

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [RFC] USB: create module_usb_serial_driver macro
@ 2012-02-24 22:47 Greg KH
  0 siblings, 0 replies; 4+ messages in thread
From: Greg KH @ 2012-02-24 22:47 UTC (permalink / raw)
  To: Lars-Peter Clausen; +Cc: Alan Stern, linux-usb, linux-kernel

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.

Lars-Peter, or anyone else, am I missing something here and we can use
module_driver() somehow, or even modify it, to work for subsystems that
need 2 parameters for their function calls to register/deregister?

Actually, if we want to be tricky, we can go back to assigning the
usb_driver in the usb_serial_driver structure, and just use that pointer
in the first structure in the list as that option, but that's getting
pretty opaque just to reuse a single macro...

Thoughts anyone?

thanks,

greg k-h


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>



--- a/drivers/usb/serial/pl2303.c
+++ b/drivers/usb/serial/pl2303.c
@@ -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");
diff --git a/include/linux/usb/serial.h b/include/linux/usb/serial.h
index 34c06a7..7b1db84 100644
--- a/include/linux/usb/serial.h
+++ b/include/linux/usb/serial.h
@@ -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 */
 

^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2012-03-10  0:31 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-02-24 23:38 [RFC] USB: create module_usb_serial_driver macro Greg KH
2012-02-25 11:53 ` Lars-Peter Clausen
2012-03-10  0:30   ` Greg KH
  -- strict thread matches above, loose matches on Subject: below --
2012-02-24 22:47 Greg KH

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).