1/* 2 * Copyright 2007, Haiku, Inc. All Rights Reserved. 3 * Distributed under the terms of the MIT License. 4 * 5 * Documentation by: 6 * Niels Sascha Reedijk <niels.reedijk@gmail.com> 7 */ 8 9/*! 10 \page usb_modules Writing drivers for USB devices 11 12 The introduction of USB standardized the way many devices connected to a 13 whole range of different computers and operating systems. It introduced a 14 standard that was capable of getting rid of all the legacy systems, such as 15 the LPT, the PS/2 and serial ports. The plug and play nature of the standard 16 were revolutional at the time of it's introduction, and it changed the way 17 in which operating systems interacted with devices. 18 19 With the grand standard that USB has become, Haiku has an implementation 20 of it. It supports both the USB 1.1 and USB 2.0 specifications, and when 21 Haiku R1 is released, it will support the three host controller standards: 22 UHCI, OHCI and EHCI. The modularized design of Haiku's USB stack also paves 23 the way for easy implementation of any future specifications, such as 24 Wireless USB. 25 26 \section usb_modules_scope The Scope of this Document 27 28 This document is written for driver developers that need to interact with 29 USB devices. The USB specification standardizes the communication between 30 the host controller and the devices, and how devices should transfer data, 31 but it does not prescribe a standard environment that Operating Systems 32 should provide to the driver interfaces. As such, every operating system has 33 it's own interface for drivers, and so does Haiku. 34 35 This document will point driver developers to relevant parts of the USB 36 module API and give a general impression of the workings of the USB stack. 37 This document will not give information on the basics of writing drivers, or 38 on how to use modules. Have a look elsewhere in this documentation for that. 39 This document also asumes a basic knowledge of the USB specification, and on 40 how you are supposed to interact with devices. See \ref usb_modules_resources 41 for tutorials on the web if you are looking for a basic introduction on 42 communication with the USB protocol. 43 44 \section usb_modules_structure Structure of the Stack 45 46 This section will outline how Haiku's USB stack is structured, and how you 47 can interact with this stack. 48 49 The goal of the USB stack is to provide a few basic features for drivers 50 interacting with USB devices. It is important that the stack maintains a 51 continually updated device grid, so that the driver modules are always 52 aware of the latest status. The stack should also facilitate communication 53 between drivers and the devices, by abstracting the actual transfering of 54 bits via the host controller hardware in the computer. The stack therefore 55 should implement a inituitive interface to give driver developers access to 56 all features and possibilities the USB specification offers, and at the same 57 time it should abstract many of the small requirements and peculiarities of 58 that specification. 59 60 The stack internally can be divided into two parts. The first part is the 61 core module. This module, called \c usb_busmanager, performs all the 62 operations required by the USB specification. For example, it performs the 63 necessary lowlevel initialization when new devices are connected, or all the 64 requirements when it comes to performing transfers. The core module also 65 is the module that provides the abstractions to driver developers. The other 66 part of the USB stack are the individual modules that control the different 67 host controllers. Haiku supports the three types in existence: UHCI, OHCI 68 and EHCI. These modules perform the communication between the core module 69 and the hardware. As driver developer, you won't have to interact with these 70 modules: the core module provides all the abstractions you need. 71 72 Thus, as a driver developer you are interfacing with the \c usb_busmanager 73 module. On Haiku, this module implements two API's. The \c v2 API, identical 74 to the API offered by BeOS R5, can be found in the \c USB2.h file. However, 75 for new drivers, or for ports, the recomended API is the \c v3 API, defined 76 in the USB3.h file. This API is identical to the one provided by Zeta. The 77 \c v2 API should be considered to be deprecated. 78 79 \section usb_modules_registration Initial Steps: Driver Registration 80 81 In order to be able to start using the USB stack to communicate with your 82 devices, you will need to perform some actions. This section will outline 83 those actions and will point you to their appropriate locations. 84 85 \note The code examples are based on the \c usb_hid driver written by 86 Jerome Duval. Have a look at this driver for a complete working example. 87 88 The following example gives an overview of the requirements to open the 89 USB module, and to start your driver registration in order to receive 90 connect and disconnect events. 91 92 \code 93// Global variables and constants 94usb_module_info *gUsb; 95const char *kDriverName = "usb_hid"; 96 97static usb_support_descriptor sSupportedDevices[1] = { 98 { USB_HID_DEVICE_CLASS, 0, 0, 0, 0 }, 99}; 100 101// Prototype for the hooks that are called when devices are added or removed 102static status_t hid_device_added(const usb_device *dev, void **cookie); 103static status_t hid_device_removed(void *cookie); 104 105static usb_notify_hooks sNotifyHooks = { 106 hid_device_added, 107 hid_device_removed 108}; 109 110// Driver initialization, called by the kernel when the driver is loaded 111status_t 112init_driver(void) 113{ 114 if (get_module(B_USB_MODULE_NAME, (module_info **)&gUsb) != B_OK) 115 return B_ERROR; 116 117 gUsb->register_driver(kDriverName, sSupportedDevices, 118 1, NULL); 119 gUsb->install_notify(kDriverName, &sNotifyHooks); 120 121 return B_OK; 122} 123 \endcode 124 125 Basically, this boils down to three steps. The first step is to acquire the 126 usb_module_info module. This struct contains a set of function pointers that 127 you use to communicate with the stack. You can retrieve it like you would 128 retrieve any other module. 129 130 As soon as you have done that you can start registering your driver in the 131 stack. What you do is you pass a unique identifier to identify your driver, 132 zero or more \link usb_support_descriptor support descriptors \endlink 133 to provide the stack with information on which devices you support, and the 134 number of support descriptors you provided. The stack is very flexible with 135 what patterns it accepts, so even the most complex driver will be able to 136 pass it's credentials. Have a look at the \c usb_support_descriptor struct 137 and the \c usb_module_info::register_driver() call for all the details. 138 139 The last step in initialization is to provide the stack with notification 140 hooks. These are functions in your driver that the stack should call as soon 141 as a device is attached or removed. Please perform this call after your 142 internal driver data structures are initialized, because as soon as you 143 perform this call, the usb stack will start searching for already attached 144 devices that match the credentials. Have a look at 145 \c usb_module_info::install_notify() and the structure \c usb_notify_hooks 146 for the details on the signatures of your hooks. 147 148 \section usb_modules_changes Handling Device Changes 149 150 The USB stack will notify you of device connects and disconnects when they 151 occur. You will receive notifications as soon as you have supplied the hooks 152 to the stack, using \c usb_module_info::install_notify() . This section will 153 explain some of the details when it comes to handling device changes. 154 155 When a device is added, your supplied usb_notify_hooks::device_added() hook 156 will be called if its credentials matches one of your support descriptors. 157 Because the stack runs through all the registered drivers, it could be that 158 two or more drivers operate on the same device. The stack does not provide 159 a locking mechanism to prevent two conflicting drivers to get in each others 160 way. It is up to the device maker to have supplied such a mechanism. 161 162 \note In reality, it is very likely that your device will match at least one 163 other driver, because Haiku supplies the \c usb_raw driver. This driver 164 provides userland access to the usb devices and therefore it has a blank 165 support descriptor that matches everything. The \c usb_raw driver will 166 not conflict with your device interaction though (except when there is an 167 userland application that tries to meddle with your device). 168 169 If your driver is willing to accept the supplied device, and your 170 device_added() hook returns B_OK, the USB stack will ask the kernel to reload 171 your published devices, so that your device is visible in userspace in the 172 \c /dev tree. 173 174 The other event that the stack reports, device disconnection, should be 175 handled by your \c usb_notify_hooks::device_removed() hook. Because "plug and 176 play" also means "unplug and leave", you should make sure your driver is 177 capable of cleaning up in the likely event that the user removes their 178 device, even during transfers. In your hook function, you have the ability to 179 do clean up whatever there is to clean up, however, make sure that you cancel 180 all the pending transfers. Use the usb_module_info::cancel_queued_transfers() 181 call for that end. Also, don't forget to free the cookie you supplied in your 182 device_added() hook. 183 184 \section usb_modules_standard Standard USB Operations 185 186 One of the many conveniences of the Haiku USB API is the fact that many of 187 the standard operations can be performed by simple function calls. As such, 188 you won't have to build many of the standard requests the USB specification 189 defines by hand. This section will outline all the different conveniences and 190 will point you to where to look if you do need something more advanced. 191 192 \subsection usb_modules_standard_descriptors Configurations, Interfaces and Descriptors 193 194 Many standard USB operations have to do with configurations, interfaces and 195 descriptors. All these operations are accessible by convenience functions. 196 197 The device descriptor is one of the first things you will be interested in if 198 you want to check out a device. The device descriptor can be retrieved quite 199 easily using the \c usb_module_info::get_device_descriptor() call. The 200 retrieved descriptor complies to the one dictated by the USB standard. 201 202 Also important are configurations. Since every device has at least one 203 configuration, you should be able to retrieve and manipulate configurations. 204 You can use \c usb_module_info::get_nth_configuration() to get them. To set 205 a configuration, you should use \c usb_module_info::set_configuration(). To 206 get the active configuration, use \c usb_module_info::get_configuration(). 207 208 \attention By default, Haiku's stack will set the configuration at offset 209 zero, which is according to the standard, the default configuration. 210 Do not rely on that if you first get the device, that the currently active 211 configuration is the default configuration though. Another driver might 212 have manipulated this device already. 213 214 Every configuration has associated interfaces. To make life easier, the stack 215 automatically gets the interface descriptors (and their associated 216 endpoints), and stores them in the \c usb_configuration_info structure. This 217 structure has a member called \link usb_configuration_info::interface 218 \c interface \endlink which is of the type \c usb_interface_list. That object 219 containts all the interfaces, including a pointer to the interface that is 220 currently active. Each interface is described as a \c usb_interface_info, 221 which is a container for the interface, its associated endpoints and any 222 unparsed descriptors. In order to change the active interface, you can use 223 the stack's \c usb_module_info::set_alt_interface() call. 224 225 Endpoints, the basic units with which you can communicate, are stored as 226 \c usb_endpoint_info structures. Each of these structures carries the actual 227 endpoint descriptor, and the accompanying usb_pipe handle that you can use to 228 actually send and receive data. 229 230 The last point of interest are descriptors. As you have seen, Haiku caches 231 all the relevant descriptors itself, however, you might want to retrieve any 232 other type of descriptor that could be relevant for your device. The 233 convenience function to use in such a case is the 234 \c usb_module_info::get_descriptor() call. This function takes all the 235 parameters needed to build the actual descriptor, and performs the request 236 over the default control pipe. 237 238 \subsection usb_modules_standard_features Features 239 240 Another one of the building blocks of USB are features. Every device should 241 provide for a number of standard features, but the USB specification also 242 leaves the option to using custom device specific features. Feature requests 243 can be performed on devices, interfaces and pipes (which are tied to 244 endpoints). 245 246 To set a feature, you can use the \c usb_module_info::set_feature() call. To 247 clear a feature, use the \c usb_module_info::clear_feature() call. One of the 248 most used feature calls is the call to clear a \c USB_FEATURE_ENDPOINT_HALT . 249 250 \subsection usb_modules_standard_other Other Standard Calls 251 252 To get the status of a device, an interface or an endpoint, you can use the 253 \c usb_module_info::get_status() call. 254 255 If you are using isochronous transfers, you can use the 256 \c usb_module_info::set_pipe_policy() to set the properties of the 257 isochronous pipe. 258 259 \section usb_modules_transfers Data Transfers 260 261 Transfering data is one of the basic building blocks of the USB protocol. 262 This section will demonstrate how to perform transfers via the four different 263 protocols the USB stack offers. 264 265 But first it is essential to show how to perform the transfers using the 266 \c usb_module_info interface. The interface provides five \c queue_* 267 functions, with the asterix being one of the following: \c bulk, \c bulk_v 268 (bulk transfers using a vector), \c interrupt, \c isochronous or \c request 269 (over the standard control pipe). These five functions work asynchronously, 270 which means that your driver is called back from a different thread when your 271 transfer is finished. 272 273 The five functions share some arguments. The first argument is always the 274 pipe that is associated with the endpoint (except for control transfers, 275 these only work on the device in general). All of the functions accept a data 276 buffer, and the length of that buffer. All of the functions require a 277 \c #usb_callback_func, a function in your driver that can be called in case a 278 transfer is finished. The functions also require a cookie that is provided to 279 the callback function. 280 281 The working order is as follows: first you queue a transfer, then you handle 282 the result in the callback function when it's done. The callback function 283 will be called with a \a status argument, in which you can check whether or 284 not the transfer actually succeeded. See this \link #usb_callback_func 285 description \endlink for how your callback function should behave and what 286 kind of status there might have been. 287 288 Finally, before going into the different transfer types, a note on buffer 289 ownership. The usb stack keeps the internal buffers tidy, but the buffer you 290 provide to the \c queue_* functions are yours. You are responsible for 291 allocating and freeing them, and you may do with them whatever you like, 292 \e except between queueing your transfer and the callback. During that period 293 you should consider the USB stack the owner of the buffer. 294 295 \subsection usb_modules_transfers_control Control Requests 296 297 Control requests are done over the device wide control pipe which is provided 298 by every device. Haiku's stack has two functions that you can use to perform 299 custom requests (opposed to many of the \ref usb_modules_standard 300 "standard operations"). Control transfers are the only transfers that you can 301 perform synchronously as well as asynchronously. The functions you can use 302 are \c usb_module_info::send_request() for synchronous requests and 303 \c usb_module_info::queue_request() for asynchronous requests. 304 305 Many of the constants that you should use when performing can be found in 306 the USB_spec.h file which is automatically included if you include the main 307 USB header. Have a look of how to use these constants in the following 308 example: 309 310 \code 311 // Send a request that is defined by the standard of this class. We retrieve 312 // a report from the device on one of its interfaces. 313 // This request is specified by the HID specification. 314 315 status = usb->send_request(dev, 316 USB_REQTYPE_INTERFACE_IN | USB_REQTYPE_CLASS, 317 USB_REQUEST_HID_GET_REPORT, 318 0x0100 | report_id, interfaceNumber, device->total_report_size, 319 device->buffer, device->total_report_size, &actual); 320 \endcode 321 322 \warning Both the \link usb_module_info::send_request() \a send_request() 323 \endlink and \link usb_module_info::queue_request() \a queue_request() 324 \endlink functions can be used to perform standard usb requests. Avoid 325 low-level operations, because the stack needs to keep its internal 326 data structures consistent. If you need to perform one of the 327 \ref usb_modules_standard "standard operations", use the provided 328 convenience functions. 329 330 \subsection usb_modules_transfers_interrupt Interrupt 331 332 Interrupt transfers apply to endpoints that receive data, or that can be 333 polled in several instances of time. The intervals are determined by the 334 endpoint descriptor. 335 336 To schedule a transfer, use usb_module_info::queue_interrupt(). You only have 337 to supply a buffer, the stack schedule the transfer in such a way that it 338 will be performed within a certain timeframe. To create a continuous 339 interrupt system, you should queue the next transfer in the callback function 340 of the previous. The stack will make sure that the new transfer will be 341 performed exactly after the required interval. 342 343 \subsection usb_modules_transfers_bulk Bulk 344 345 Bulk transfers are very similar to control transfers. They will be performed 346 as soon as possible without stalling other transfers, and they transfer data. 347 Bulk transfers are designed to transfer up to large amounts of data as 348 efficiently as possible. Performing bulk transfers isn't difficult, you 349 merely supply a buffer and the endpoint that should execute the request, and 350 you're set. 351 352 Bulk transfers come in two flavours. The first is 353 usb_module_info::queue_bulk(), which takes a standard data buffer. The second 354 flavour is the usb_module_info::queue_bulk_v() function, which is designed to 355 operate on (an array of) POSIX vectors. These functions only differ in the 356 buffer they accept, they function in exactly the same way. 357 358 \subsection usb_modules_transfers_isochronous Isochronous 359 360 Isochronous transfers are not implemented on Haiku yet. As soon as they are, 361 this section should contain information on how to queue them. 362 363 \section usb_modules_cleanup Cleaning Up 364 365 This section describes how to gracefully leave the stack after your driver is 366 requested to shut down. 367 368 There are truely only two simple actions to perform. The first is to 369 uninstall your notification hooks, using 370 \c usb_module_info::uninstall_notify(). The second action is to 'put' the 371 module. 372 373 \code 374void 375uninit_driver(void) 376{ 377 usb->uninstall_notify(kDriverName); 378 379 put_module(B_USB_MODULE_NAME); 380} 381 \endcode 382 383 \section usb_modules_resources More Resources 384 385 This section should list more resources on the web. 386*/ 387