Skip to contentSkip to conversation

Docs: usb: update struct usb_driver, __init and __exit

PATCH Superseded Philipp Hortmann 2 participants 3 messages Oct 20, 2021 – Oct 22, 2021

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 Docs: usb: update struct usb_driver, __init and __exit

update struct usb_driver from usb-skeleton.c. update __init and __exit functions that are moved from usb-skeleton.c to common used multi-stage macros.

---
V1 -> V2: changed :c:func:usb_register to usb_register()
changed the :c:func:usb_deregister to usb_deregister() used literal blocks for makro module_usb_driver and added one more stage of multi-stage macros.

 .../driver-api/usb/writing_usb_driver.rst     | 70 ++++++++++---------
 1 file changed, 36 insertions(+), 34 deletions(-)
Documentation/driver-api/usb/writing_usb_driver.rst +36 -34
index 2176297e5765..12e0481cceae 100644
@@ -54,12 +54,15 @@ information is passed to the USB subsystem in the :c:type:`usb_driver`
 structure. The skeleton driver declares a :c:type:`usb_driver` as::
 
     static struct usb_driver skel_driver = {
-	    .name        = "skeleton",
-	    .probe       = skel_probe,
-	    .disconnect  = skel_disconnect,
-	    .fops        = &skel_fops,
-	    .minor       = USB_SKEL_MINOR_BASE,
-	    .id_table    = skel_table,
+           .name        = "skeleton",
+           .probe       = skel_probe,
+           .disconnect  = skel_disconnect,
+           .suspend     = skel_suspend,
+           .resume      = skel_resume,
+           .pre_reset   = skel_pre_reset,
+           .post_reset  = skel_post_reset,
+           .id_table    = skel_table,
+           .supports_autosuspend = 1,
     };
 
 
@@ -81,36 +84,35 @@ this user-space interaction. The skeleton driver needs this kind of
 interface, so it provides a minor starting number and a pointer to its
 :c:type:`file_operations` functions.
 
-The USB driver is then registered with a call to :c:func:`usb_register`,
-usually in the driver's init function, as shown here::
-
-    static int __init usb_skel_init(void)
-    {
-	    int result;
-
-	    /* register this driver with the USB subsystem */
-	    result = usb_register(&skel_driver);
-	    if (result < 0) {
-		    err("usb_register failed for the "__FILE__ "driver."
-			"Error number %d", result);
-		    return -1;
-	    }
-
-	    return 0;
-    }
-    module_init(usb_skel_init);
-
+The USB driver is then registered with a call to usb_register()
+which is usually in the driver's init function. Since this functionality
+is usable with many USB drivers, it is hidden behind multi-stage macros.
+While the first macros are USB specific the later macros are used in different
+subsystems. This removes a lot of boilerplate code.
 
 When the driver is unloaded from the system, it needs to deregister
-itself with the USB subsystem. This is done with the :c:func:`usb_deregister`
-function::
-
-    static void __exit usb_skel_exit(void)
-    {
-	    /* deregister this driver with the USB subsystem */
-	    usb_deregister(&skel_driver);
-    }
-    module_exit(usb_skel_exit);
+itself with the USB subsystem. This is done with usb_deregister()
+which is also hidden behind multi-stage macros.
+
+The init and exit functions are included in the macro module_usb_driver.
+Find the first three stages of macros below::
+
+    module_usb_driver(skel_driver);
+                         |
+                         V
+    module_driver(__usb_driver, usb_register, usb_deregister)
+                         |               \               \
+                         V                ----------      ----------
+    static int __init __driver##_init(void) \      |               |
+    { \                 v---------------------------               |
+            return __register(&(__driver) , ##__VA_ARGS__); \      |
+    } \                                                            |
+    module_init(__driver##_init); \                                |
+    static void __exit __driver##_exit(void) \                     |
+    { \            v------------------------------------------------
+            __unregister(&(__driver) , ##__VA_ARGS__); \
+    } \
+    module_exit(__driver##_exit);
 
 
 To enable the linux-hotplug system to load the driver automatically when
Signature · 1 line
2.25.1
maintainer
#1 lore raw
Quoted 27 lines · Philipp Hortmann

update struct usb_driver from usb-skeleton.c. update __init and __exit functions that are moved from usb-skeleton.c to common used multi-stage macros.

---
V1 -> V2: changed :c:func:usb_register to usb_register()
changed the :c:func:usb_deregister to usb_deregister() used literal blocks for makro module_usb_driver and added one more stage of multi-stage macros.

 .../driver-api/usb/writing_usb_driver.rst     | 70 ++++++++++---------
 1 file changed, 36 insertions(+), 34 deletions(-)
Documentation/driver-api/usb/writing_usb_driver.rst +0 -5
index 2176297e5765..12e0481cceae 100644
@@ -54,12 +54,15 @@ information is passed to the USB subsystem in the :c:type:`usb_driver`
 structure. The skeleton driver declares a :c:type:`usb_driver` as::
 
     static struct usb_driver skel_driver = {
-	    .name        = "skeleton",
-	    .probe       = skel_probe,
-	    .disconnect  = skel_disconnect,
-	    .fops        = &skel_fops,
-	    .minor       = USB_SKEL_MINOR_BASE,

Documentation/driver-api/usb/writing_usb_driver.rst information is passed to the USB subsystem in the :c:type:`usb_driver` · line 65

Quoted 10 lines
Documentation/driver-api/usb/writing_usb_driver.rst +9 -1
-	    .id_table    = skel_table,
+           .name        = "skeleton",
+           .probe       = skel_probe,
+           .disconnect  = skel_disconnect,
+           .suspend     = skel_suspend,
+           .resume      = skel_resume,
+           .pre_reset   = skel_pre_reset,
+           .post_reset  = skel_post_reset,
+           .id_table    = skel_table,
+           .supports_autosuspend = 1,

Why remove the tabs? Is that needed here?

Quoted 19 lines
Documentation/driver-api/usb/writing_usb_driver.rst +0 -12
     };
 
 
@@ -81,36 +84,35 @@ this user-space interaction. The skeleton driver needs this kind of
 interface, so it provides a minor starting number and a pointer to its
 :c:type:`file_operations` functions.
 
-The USB driver is then registered with a call to :c:func:`usb_register`,
-usually in the driver's init function, as shown here::
-
-    static int __init usb_skel_init(void)
-    {
-	    int result;
-
-	    /* register this driver with the USB subsystem */
-	    result = usb_register(&skel_driver);
-	    if (result < 0) {
-		    err("usb_register failed for the "__FILE__ "driver."
-			"Error number %d", result);

Documentation/driver-api/usb/writing_usb_driver.rst this user-space interaction. The skeleton driver needs this kind of · line 89

Quoted 10 lines
Documentation/driver-api/usb/writing_usb_driver.rst +3 -7
-		    return -1;
-	    }
-
-	    return 0;
-    }
-    module_init(usb_skel_init);
-
+The USB driver is then registered with a call to usb_register()
+which is usually in the driver's init function. Since this functionality
+is usable with many USB drivers, it is hidden behind multi-stage macros.

I don't understand the need for the "multi-stage macros" term here.

And what functionality is referred to here by "this"?

Documentation/driver-api/usb/writing_usb_driver.rst this user-space interaction. The skeleton driver needs this kind of · line 91

Quoted 2 lines
Documentation/driver-api/usb/writing_usb_driver.rst +2 -0
+While the first macros are USB specific the later macros are used in different
+subsystems. This removes a lot of boilerplate code.

What later macros? Is that really needed to describe here?

I think the above code example should remain, as it is good for learning and understanding, and maybe just add something that says "Or you can use the following macro to replace all of the above common code."

Quoted 23 lines
Documentation/driver-api/usb/writing_usb_driver.rst +12 -9
 When the driver is unloaded from the system, it needs to deregister
-itself with the USB subsystem. This is done with the :c:func:`usb_deregister`
-function::
-
-    static void __exit usb_skel_exit(void)
-    {
-	    /* deregister this driver with the USB subsystem */
-	    usb_deregister(&skel_driver);
-    }
-    module_exit(usb_skel_exit);
+itself with the USB subsystem. This is done with usb_deregister()
+which is also hidden behind multi-stage macros.
+
+The init and exit functions are included in the macro module_usb_driver.
+Find the first three stages of macros below::
+
+    module_usb_driver(skel_driver);
+                         |
+                         V
+    module_driver(__usb_driver, usb_register, usb_deregister)
+                         |               \               \
+                         V                ----------      ----------

Documentation/driver-api/usb/writing_usb_driver.rst this user-space interaction. The skeleton driver needs this kind of · line 115

Quoted 10 lines
Documentation/driver-api/usb/writing_usb_driver.rst +10 -0
+    static int __init __driver##_init(void) \      |               |
+    { \                 v---------------------------               |
+            return __register(&(__driver) , ##__VA_ARGS__); \      |
+    } \                                                            |
+    module_init(__driver##_init); \                                |
+    static void __exit __driver##_exit(void) \                     |
+    { \            v------------------------------------------------
+            __unregister(&(__driver) , ##__VA_ARGS__); \
+    } \
+    module_exit(__driver##_exit);

As the one who wrote these macros, I can't really understand the ascii-art here, so I worry about anyone else :)

Again, do not think trying to show an implementation detail like this is needed.

thanks,

greg k-h

#2 lore raw
Quoted 32 lines · Greg KH
Quoted 31 lines · Philipp Hortmann

update struct usb_driver from usb-skeleton.c. update __init and __exit functions that are moved from usb-skeleton.c to common used multi-stage macros.

---
V1 -> V2: changed :c:func:usb_register to usb_register()
changed the :c:func:usb_deregister to usb_deregister() used literal blocks for makro module_usb_driver and added one more stage of multi-stage macros.

  .../driver-api/usb/writing_usb_driver.rst     | 70 ++++++++++---------
  1 file changed, 36 insertions(+), 34 deletions(-)
Documentation/driver-api/usb/writing_usb_driver.rst +3 -6
index 2176297e5765..12e0481cceae 100644
@@ -54,12 +54,15 @@ information is passed to the USB subsystem in the :c:type:`usb_driver`
  structure. The skeleton driver declares a :c:type:`usb_driver` as::
 
      static struct usb_driver skel_driver = {
-	    .name        = "skeleton",
-	    .probe       = skel_probe,
-	    .disconnect  = skel_disconnect,
-	    .fops        = &skel_fops,
-	    .minor       = USB_SKEL_MINOR_BASE,
-	    .id_table    = skel_table,
+           .name        = "skeleton",
+           .probe       = skel_probe,
+           .disconnect  = skel_disconnect,
Quoted 8 lines

Documentation/driver-api/usb/writing_usb_driver.rst information is passed to the USB subsystem in the :c:type:`usb_driver` · line 65

Quoted 6 lines
Documentation/driver-api/usb/writing_usb_driver.rst +6 -0
+           .suspend     = skel_suspend,
+           .resume      = skel_resume,
+           .pre_reset   = skel_pre_reset,
+           .post_reset  = skel_post_reset,
+           .id_table    = skel_table,
+           .supports_autosuspend = 1,

Why remove the tabs? Is that needed here?

You are right will be changed.

Quoted 24 lines
Quoted 24 lines
Documentation/driver-api/usb/writing_usb_driver.rst +0 -16
      };
 
 
@@ -81,36 +84,35 @@ this user-space interaction. The skeleton driver needs this kind of
  interface, so it provides a minor starting number and a pointer to its
  :c:type:`file_operations` functions.
 
-The USB driver is then registered with a call to :c:func:`usb_register`,
-usually in the driver's init function, as shown here::
-
-    static int __init usb_skel_init(void)
-    {
-	    int result;
-
-	    /* register this driver with the USB subsystem */
-	    result = usb_register(&skel_driver);
-	    if (result < 0) {
-		    err("usb_register failed for the "__FILE__ "driver."
-			"Error number %d", result);
-		    return -1;
-	    }
-
-	    return 0;
Quoted 8 lines

Documentation/driver-api/usb/writing_usb_driver.rst this user-space interaction. The skeleton driver needs this kind of · line 89

Quoted 6 lines
Documentation/driver-api/usb/writing_usb_driver.rst +3 -3
-    }
-    module_init(usb_skel_init);
-
+The USB driver is then registered with a call to usb_register()
+which is usually in the driver's init function. Since this functionality
+is usable with many USB drivers, it is hidden behind multi-stage macros.

I don't understand the need for the "multi-stage macros" term here.

I am not a native English speaker so "multi-stage macros" is just not a fitting wording. May be “staged macros” is better or something else…

Quoted 2 lines

And what functionality is referred to here by "this"?

The “this” is replacing the “init function” but when this is unclear I will change in a later proposal…

Quoted 6 lines

Documentation/driver-api/usb/writing_usb_driver.rst this user-space interaction. The skeleton driver needs this kind of · line 91

Quoted 4 lines
Documentation/driver-api/usb/writing_usb_driver.rst +2 -0
+While the first macros are USB specific the later macros are used in different
+subsystems. This removes a lot of boilerplate code.

What later macros? Is that really needed to describe here?

I will improve wording...

Quoted 3 lines

I think the above code example should remain, as it is good for learning and understanding, and maybe just add something that says "Or you can use the following macro to replace all of the above common code."

I understand the need for keeping the code examples. But I would like to inform the reader about the macros first.

Quoted 30 lines
Quoted 30 lines
Documentation/driver-api/usb/writing_usb_driver.rst +17 -9
  When the driver is unloaded from the system, it needs to deregister
-itself with the USB subsystem. This is done with the :c:func:`usb_deregister`
-function::
-
-    static void __exit usb_skel_exit(void)
-    {
-	    /* deregister this driver with the USB subsystem */
-	    usb_deregister(&skel_driver);
-    }
-    module_exit(usb_skel_exit);
+itself with the USB subsystem. This is done with usb_deregister()
+which is also hidden behind multi-stage macros.
+
+The init and exit functions are included in the macro module_usb_driver.
+Find the first three stages of macros below::
+
+    module_usb_driver(skel_driver);
+                         |
+                         V
+    module_driver(__usb_driver, usb_register, usb_deregister)
+                         |               \               \
+                         V                ----------      ----------
+    static int __init __driver##_init(void) \      |               |
+    { \                 v---------------------------               |
+            return __register(&(__driver) , ##__VA_ARGS__); \      |
+    } \                                                            |
+    module_init(__driver##_init); \                                |
Quoted 8 lines

Documentation/driver-api/usb/writing_usb_driver.rst this user-space interaction. The skeleton driver needs this kind of · line 115

Quoted 5 lines
Documentation/driver-api/usb/writing_usb_driver.rst +5 -0
+    static void __exit __driver##_exit(void) \                     |
+    { \            v------------------------------------------------
+            __unregister(&(__driver) , ##__VA_ARGS__); \
+    } \
+    module_exit(__driver##_exit);

As the one who wrote these macros, I can't really understand the ascii-art here, so I worry about anyone else :)

Code is just better readable, even when code uses more lines. Will be changed in next proposal.

Quoted 3 lines

Again, do not think trying to show an implementation detail like this is needed.

The big question for me is for whom is this document written? For the USB subsystem maintainer that has even written the code by himself? I guess not, but may be I am wrong. Or for the kernel newbies like me? Please consider that the changed lines are may be not so much of use for me anymore as I am in the details.

When I saw the __init and __exit code example first, I was very happy to see it and then I was searching in the code for it. I did not find “init” and “exit” and was very frustrated. I want to help others to get into this example more smoothly.

Quoted 5 lines

thanks,

greg k-h

Thanks that you replied at all.
Thanks for your very fast reply.

Kernelnewbie Philipp G. Hortmann