Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

writing_usb_driver.rst: Enrich its ReST representation

The pandoc conversion is not perfect. Do handwork in order to:

- add a title to this chapter;
- adjust function and struct references;
- use monospaced fonts for C code names;
- some other minor adjustments to make it better to read in
text mode and in html.

Signed-off-by: Mauro Carvalho Chehab <mchehab@s-opensource.com>
Acked-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: Jonathan Corbet <corbet@lwn.net>

authored by

Mauro Carvalho Chehab and committed by
Jonathan Corbet
0e8c46d0 e1cfb8ca

+82 -100
+82 -100
Documentation/driver-api/usb/writing_usb_driver.rst
··· 1 + .. _writing-usb-driver: 2 + 1 3 ========================== 2 4 Writing USB Device Drivers 3 5 ========================== ··· 50 48 the Linux USB subsystem, giving it some information about which devices 51 49 the driver supports and which functions to call when a device supported 52 50 by the driver is inserted or removed from the system. All of this 53 - information is passed to the USB subsystem in the usb_driver structure. 54 - The skeleton driver declares a usb_driver as: 55 - 56 - :: 51 + information is passed to the USB subsystem in the :c:type:`usb_driver` 52 + structure. The skeleton driver declares a :c:type:`usb_driver` as:: 57 53 58 54 static struct usb_driver skel_driver = { 59 - .name = "skeleton", 60 - .probe = skel_probe, 61 - .disconnect = skel_disconnect, 62 - .fops = &skel_fops, 63 - .minor = USB_SKEL_MINOR_BASE, 64 - .id_table = skel_table, 55 + .name = "skeleton", 56 + .probe = skel_probe, 57 + .disconnect = skel_disconnect, 58 + .fops = &skel_fops, 59 + .minor = USB_SKEL_MINOR_BASE, 60 + .id_table = skel_table, 65 61 }; 66 62 67 63 68 64 The variable name is a string that describes the driver. It is used in 69 65 informational messages printed to the system log. The probe and 70 66 disconnect function pointers are called when a device that matches the 71 - information provided in the id_table variable is either seen or 67 + information provided in the ``id_table`` variable is either seen or 72 68 removed. 73 69 74 70 The fops and minor variables are optional. Most USB drivers hook into ··· 76 76 interface. But for drivers that do not have a matching kernel subsystem, 77 77 such as MP3 players or scanners, a method of interacting with user space 78 78 is needed. The USB subsystem provides a way to register a minor device 79 - number and a set of file_operations function pointers that enable this 80 - user-space interaction. The skeleton driver needs this kind of 79 + number and a set of :c:type:`file_operations` function pointers that enable 80 + this user-space interaction. The skeleton driver needs this kind of 81 81 interface, so it provides a minor starting number and a pointer to its 82 - file_operations functions. 82 + :c:type:`file_operations` functions. 83 83 84 - The USB driver is then registered with a call to usb_register, usually 85 - in the driver's init function, as shown here: 86 - 87 - :: 84 + The USB driver is then registered with a call to :c:func:`usb_register`, 85 + usually in the driver's init function, as shown here:: 88 86 89 87 static int __init usb_skel_init(void) 90 88 { 91 - int result; 89 + int result; 92 90 93 - /* register this driver with the USB subsystem */ 94 - result = usb_register(&skel_driver); 95 - if (result < 0) { 96 - err("usb_register failed for the "__FILE__ "driver." 97 - "Error number %d", result); 98 - return -1; 99 - } 91 + /* register this driver with the USB subsystem */ 92 + result = usb_register(&skel_driver); 93 + if (result < 0) { 94 + err("usb_register failed for the "__FILE__ "driver." 95 + "Error number %d", result); 96 + return -1; 97 + } 100 98 101 - return 0; 99 + return 0; 102 100 } 103 101 module_init(usb_skel_init); 104 102 105 103 106 104 When the driver is unloaded from the system, it needs to deregister 107 - itself with the USB subsystem. This is done with the usb_deregister 108 - function: 109 - 110 - :: 105 + itself with the USB subsystem. This is done with the :c:func:`usb_deregister` 106 + function:: 111 107 112 108 static void __exit usb_skel_exit(void) 113 109 { 114 - /* deregister this driver with the USB subsystem */ 115 - usb_deregister(&skel_driver); 110 + /* deregister this driver with the USB subsystem */ 111 + usb_deregister(&skel_driver); 116 112 } 117 113 module_exit(usb_skel_exit); 118 114 119 115 120 116 To enable the linux-hotplug system to load the driver automatically when 121 - the device is plugged in, you need to create a MODULE_DEVICE_TABLE. 117 + the device is plugged in, you need to create a ``MODULE_DEVICE_TABLE``. 122 118 The following code tells the hotplug scripts that this module supports a 123 - single device with a specific vendor and product ID: 124 - 125 - :: 119 + single device with a specific vendor and product ID:: 126 120 127 121 /* table of devices that work with this driver */ 128 122 static struct usb_device_id skel_table [] = { 129 - { USB_DEVICE(USB_SKEL_VENDOR_ID, USB_SKEL_PRODUCT_ID) }, 130 - { } /* Terminating entry */ 123 + { USB_DEVICE(USB_SKEL_VENDOR_ID, USB_SKEL_PRODUCT_ID) }, 124 + { } /* Terminating entry */ 131 125 }; 132 126 MODULE_DEVICE_TABLE (usb, skel_table); 133 127 134 128 135 - There are other macros that can be used in describing a usb_device_id 136 - for drivers that support a whole class of USB drivers. See usb.h for 137 - more information on this. 129 + There are other macros that can be used in describing a struct 130 + :c:type:`usb_device_id` for drivers that support a whole class of USB 131 + drivers. See :ref:`usb.h <usb_header>` for more information on this. 138 132 139 133 Device operation 140 134 ================ 141 135 142 136 When a device is plugged into the USB bus that matches the device ID 143 137 pattern that your driver registered with the USB core, the probe 144 - function is called. The usb_device structure, interface number and the 145 - interface ID are passed to the function: 146 - 147 - :: 138 + function is called. The :c:type:`usb_device` structure, interface number and 139 + the interface ID are passed to the function:: 148 140 149 141 static int skel_probe(struct usb_interface *interface, 150 - const struct usb_device_id *id) 142 + const struct usb_device_id *id) 151 143 152 144 153 145 The driver now needs to verify that this device is actually one that it ··· 158 166 any pending urbs that are in the USB system. 159 167 160 168 Now that the device is plugged into the system and the driver is bound 161 - to the device, any of the functions in the file_operations structure 169 + to the device, any of the functions in the :c:type:`file_operations` structure 162 170 that were passed to the USB subsystem will be called from a user program 163 171 trying to talk to the device. The first function called will be open, as 164 172 the program tries to open the device for I/O. We increment our private 165 173 usage count and save a pointer to our internal structure in the file 166 174 structure. This is done so that future calls to file operations will 167 175 enable the driver to determine which device the user is addressing. All 168 - of this is done with the following code: 169 - 170 - :: 176 + of this is done with the following code:: 171 177 172 178 /* increment our usage count for the module */ 173 179 ++skel->open_count; ··· 175 185 176 186 177 187 After the open function is called, the read and write functions are 178 - called to receive and send data to the device. In the skel_write 188 + called to receive and send data to the device. In the ``skel_write`` 179 189 function, we receive a pointer to some data that the user wants to send 180 190 to the device and the size of the data. The function determines how much 181 191 data it can send to the device based on the size of the write urb it has 182 192 created (this size depends on the size of the bulk out end point that 183 193 the device has). Then it copies the data from user space to kernel 184 194 space, points the urb to the data and submits the urb to the USB 185 - subsystem. This can be seen in the following code: 186 - 187 - :: 195 + subsystem. This can be seen in the following code:: 188 196 189 197 /* we can only write as much as 1 urb will hold */ 190 198 bytes_written = (count > skel->bulk_out_size) ? skel->bulk_out_size : count; ··· 192 204 193 205 /* set up our urb */ 194 206 usb_fill_bulk_urb(skel->write_urb, 195 - skel->dev, 196 - usb_sndbulkpipe(skel->dev, skel->bulk_out_endpointAddr), 197 - skel->write_urb->transfer_buffer, 198 - bytes_written, 199 - skel_write_bulk_callback, 200 - skel); 207 + skel->dev, 208 + usb_sndbulkpipe(skel->dev, skel->bulk_out_endpointAddr), 209 + skel->write_urb->transfer_buffer, 210 + bytes_written, 211 + skel_write_bulk_callback, 212 + skel); 201 213 202 214 /* send the data out the bulk port */ 203 215 result = usb_submit_urb(skel->write_urb); 204 216 if (result) { 205 - err("Failed submitting write urb, error %d", result); 217 + err("Failed submitting write urb, error %d", result); 206 218 } 207 219 208 220 209 221 When the write urb is filled up with the proper information using the 210 - usb_fill_bulk_urb function, we point the urb's completion callback to 211 - call our own skel_write_bulk_callback function. This function is 222 + :c:func:`usb_fill_bulk_urb` function, we point the urb's completion callback 223 + to call our own ``skel_write_bulk_callback`` function. This function is 212 224 called when the urb is finished by the USB subsystem. The callback 213 225 function is called in interrupt context, so caution must be taken not to 214 226 do very much processing at that time. Our implementation of 215 - skel_write_bulk_callback merely reports if the urb was completed 227 + ``skel_write_bulk_callback`` merely reports if the urb was completed 216 228 successfully or not and then returns. 217 229 218 230 The read function works a bit differently from the write function in 219 231 that we do not use an urb to transfer data from the device to the 220 - driver. Instead we call the usb_bulk_msg function, which can be used 232 + driver. Instead we call the :c:func:`usb_bulk_msg` function, which can be used 221 233 to send or receive data from a device without having to create urbs and 222 - handle urb completion callback functions. We call the usb_bulk_msg 234 + handle urb completion callback functions. We call the :c:func:`usb_bulk_msg` 223 235 function, giving it a buffer into which to place any data received from 224 236 the device and a timeout value. If the timeout period expires without 225 237 receiving any data from the device, the function will fail and return an 226 - error message. This can be shown with the following code: 227 - 228 - :: 238 + error message. This can be shown with the following code:: 229 239 230 240 /* do an immediate bulk read to get data from the device */ 231 241 retval = usb_bulk_msg (skel->dev, 232 - usb_rcvbulkpipe (skel->dev, 233 - skel->bulk_in_endpointAddr), 234 - skel->bulk_in_buffer, 235 - skel->bulk_in_size, 236 - &count, HZ*10); 242 + usb_rcvbulkpipe (skel->dev, 243 + skel->bulk_in_endpointAddr), 244 + skel->bulk_in_buffer, 245 + skel->bulk_in_size, 246 + &count, HZ*10); 237 247 /* if the read was successful, copy the data to user space */ 238 248 if (!retval) { 239 - if (copy_to_user (buffer, skel->bulk_in_buffer, count)) 240 - retval = -EFAULT; 241 - else 242 - retval = count; 249 + if (copy_to_user (buffer, skel->bulk_in_buffer, count)) 250 + retval = -EFAULT; 251 + else 252 + retval = count; 243 253 } 244 254 245 255 246 - The usb_bulk_msg function can be very useful for doing single reads or 247 - writes to a device; however, if you need to read or write constantly to 256 + The :c:func:`usb_bulk_msg` function can be very useful for doing single reads 257 + or writes to a device; however, if you need to read or write constantly to 248 258 a device, it is recommended to set up your own urbs and submit them to 249 259 the USB subsystem. 250 260 251 261 When the user program releases the file handle that it has been using to 252 262 talk to the device, the release function in the driver is called. In 253 263 this function we decrement our private usage count and wait for possible 254 - pending writes: 255 - 256 - :: 264 + pending writes:: 257 265 258 266 /* decrement our usage count for the device */ 259 267 --skel->open_count; ··· 260 276 system at any point in time, even if a program is currently talking to 261 277 it. It needs to be able to shut down any current reads and writes and 262 278 notify the user-space programs that the device is no longer there. The 263 - following code (function :c:func:`skel_delete()`) is an example of 264 - how to do this: 265 - 266 - :: 279 + following code (function ``skel_delete``) is an example of how to do 280 + this:: 267 281 268 282 static inline void skel_delete (struct usb_skel *dev) 269 283 { 270 - kfree (dev->bulk_in_buffer); 271 - if (dev->bulk_out_buffer != NULL) 272 - usb_free_coherent (dev->udev, dev->bulk_out_size, 273 - dev->bulk_out_buffer, 274 - dev->write_urb->transfer_dma); 275 - usb_free_urb (dev->write_urb); 276 - kfree (dev); 284 + kfree (dev->bulk_in_buffer); 285 + if (dev->bulk_out_buffer != NULL) 286 + usb_free_coherent (dev->udev, dev->bulk_out_size, 287 + dev->bulk_out_buffer, 288 + dev->write_urb->transfer_dma); 289 + usb_free_urb (dev->write_urb); 290 + kfree (dev); 277 291 } 278 292 279 293 ··· 279 297 flag ``device_present``. For every read, write, release and other 280 298 functions that expect a device to be present, the driver first checks 281 299 this flag to see if the device is still present. If not, it releases 282 - that the device has disappeared, and a -ENODEV error is returned to the 300 + that the device has disappeared, and a ``-ENODEV`` error is returned to the 283 301 user-space program. When the release function is eventually called, it 284 302 determines if there is no device and if not, it does the cleanup that 285 - the skel_disconnect function normally does if there are no open files 303 + the ``skel_disconnect`` function normally does if there are no open files 286 304 on the device (see Listing 5). 287 305 288 306 Isochronous Data ··· 309 327 ========= 310 328 311 329 The Linux USB Project: 312 - `http://www.linux-usb.org/ <http://www.linux-usb.org>`__ 330 + http://www.linux-usb.org/ 313 331 314 332 Linux Hotplug Project: 315 - `http://linux-hotplug.sourceforge.net/ <http://linux-hotplug.sourceforge.net>`__ 333 + http://linux-hotplug.sourceforge.net/ 316 334 317 335 Linux USB Working Devices List: 318 - `http://www.qbik.ch/usb/devices/ <http://www.qbik.ch/usb/devices>`__ 336 + http://www.qbik.ch/usb/devices/ 319 337 320 338 linux-usb-devel Mailing List Archives: 321 339 http://marc.theaimsgroup.com/?l=linux-usb-devel