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 * Corresponds to: 8 * /trunk/headers/os/drivers/USB3.h rev 19915 9 */ 10 11/*! 12 \file USB3.h 13 \ingroup drivers 14 \brief Interface for the USB module. 15*/ 16 17/*! 18 \typedef struct usb_module_info usb_module_info 19 \brief The main interface object. See the usb_module_info documentation. 20*/ 21 22/*! 23 \typedef uint32 usb_id 24 \brief Uniquely identify various USB objects that are used in the module. 25*/ 26 27/*! 28 \typedef usb_id usb_device 29 \brief Uniquely identify USB devices. 30*/ 31 32/*! 33 \typedef usb_id usb_interface 34 \brief Uniquely identify USB interfaces. 35*/ 36 37/*! 38 \typedef usb_id usb_pipe 39 \brief Uniquely identify USB pipes. 40*/ 41 42/*! 43 \typedef struct usb_endpoint_info usb_endpoint_info 44 \brief Container for USB endpoint descriptors. 45 \see Documentation for usb_endpoint_info. 46*/ 47 48/*! 49 \typedef struct usb_interface_info usb_interface_info 50 \brief Container for USB interface descriptors. 51 \see Documentation for usb_interface_info. 52*/ 53 54/*! 55 \typedef usb_interface_list usb_interface_list 56 \brief Container that holds a list of USB interface descriptors. 57 \see Documentation for usb_interface_list. 58*/ 59 60/*! 61 \typedef struct usb_configuration_info usb_configuration_info 62 \brief Container for USB configuration descriptors. 63 \see Documentation for usb_configuration_info. 64*/ 65 66///// usb_notify_hooks ///// 67 68/*! 69 \struct usb_notify_hooks 70 \brief Hooks that the USB stack can callback in case of events. 71*/ 72 73/*! 74 \fn status_t (*usb_notify_hooks::device_added)(usb_device device, void **cookie) 75 \brief Called by the stack in case a device is added. 76 77 As soon as you have registered hooks using the 78 usb_module_info::install_notify() method, this hook will be called as soon as 79 a device is inserted that matches your provided usb_support_descriptor. 80 81 \param device A unique id that identifies this USB device. 82 \param[in] cookie You can store a pointer to an object in this variable. 83 When the device is removed, this cookie will be provided to you. 84 \return You should return \c B_OK in case of success. The USB stack will then 85 request the kernel to republish your device names, so that the new device 86 will be shown in the \c /dev tree. If you return an error value, the 87 \a device id will become invalid and you will not be notified if this 88 device is removed. 89 \see device_removed() 90*/ 91 92/*! 93 \var status_t (*usb_notify_hooks::device_removed)(void *cookie) 94 \brief Called by the stack in case a device you are using is removed. 95 96 If you have accepted a device in the device_added() hook, this hook will 97 be called as soon as the device is removed. 98 99 \param cookie The cookie you provided in the device_added() hook. Make sure 100 that you free the cookie, if necessary. 101 \return Currently the return value of this hook is ignored. It is recommended 102 to return \c B_OK though. 103*/ 104 105///// usb_support_descriptor ///// 106 107/*! 108 \struct usb_support_descriptor 109 \brief Description of device descriptor that the driver can handle. 110 111 Support descriptors can be used to match any form of class, subclass or 112 protocol, or a vendor and/or product. If any field has the value \c 0, it 113 is treated as a wildcard. 114 115 For example, if you want to watch for all the hubs, which have a device 116 class of \c 0x09, you would pass this descriptor: 117 118 \code 119 usb_support_descriptor hub_devs = { 9, 0, 0, 0, 0 }; 120 \endcode 121 122 See usb_module_info::register_driver() for more information on how to use 123 this object. 124*/ 125 126/*! 127 \var usb_support_descriptor::dev_class 128 \brief The supported device classes. 129*/ 130 131/*! 132 \var usb_support_descriptor::dev_subclass 133 \brief The suported device subclasses. 134*/ 135 136/*! 137 \var usb_support_descriptor::dev_protocol 138 \brief The supported device protocols. 139*/ 140 141/*! 142 \var usb_support_descriptor::vendor 143 \brief The supported device vendor. 144*/ 145 146/*! 147 \var usb_support_descriptor::product 148 \brief The supported device products. 149*/ 150 151///// usb_endpoint_info ///// 152 153/*! 154 \struct usb_endpoint_info 155 \brief Container for endpoint descriptors and their Haiku USB stack 156 identifiers. 157*/ 158 159/*! 160 \var usb_endpoint_descriptor *usb_endpoint_info::descr 161 \brief Pointer to the descriptor of the endpoint. 162*/ 163 164/*! 165 \var usb_pipe usb_endpoint_info::handle 166 \brief Handle to use when using the stack to transfer data to and from this 167 endpoint. 168*/ 169 170///// usb_interface_info ///// 171 172/*! 173 \struct usb_interface_info 174 \brief Container for interface descriptors and their Haiku USB stack 175 identifiers. 176*/ 177 178//! @{ 179 180/*! 181 \var usb_interface_descriptor *usb_interface_info::descr 182 \brief Pointer to the descriptor of the interface. 183*/ 184 185/*! 186 \var usb_interface usb_interface_info::handle 187 \brief Handle to use when using the stack to manipulate this interface. 188*/ 189 190//! @} 191 192/*! 193 \name Endpoints 194*/ 195 196//! @{ 197 198/*! 199 \var size_t usb_interface_info::endpoint_count 200 \brief The number of endpoints in this interface. 201*/ 202 203/*! 204 \var usb_endpoint_info *usb_interface_info::endpoint 205 \brief An array of endpoints that are associated to this interface. 206*/ 207 208//! @} 209 210/*! 211 \name Unparsed descriptors 212*/ 213 214//! @{ 215 216/*! 217 \var size_t usb_interface_info::generic_count 218 \brief The number of unparsed descriptors in this interface. 219*/ 220 221/*! 222 \var usb_descriptor **usb_interface_info::generic 223 \brief Unparsed descriptors in this interface. 224*/ 225 226//! @} 227 228///// usb_interface_list ///// 229 230/*! 231 \struct usb_interface_list 232 \brief List of interfaces available to a configuration. 233*/ 234 235/*! 236 \var size_t usb_interface_list::alt_count 237 \brief Number of available interfaces. 238*/ 239 240/*! 241 \var usb_interface_info *usb_interface_list::alt 242 \brief Array of available interfaces. 243*/ 244 245/*! 246 \var usb_interface_info *usb_interface_list::active 247 \brief Pointer to active interface. 248*/ 249 250///// usb_configuration_info ///// 251 252/*! 253 \struct usb_configuration_info 254 \brief Container for a specific configuration descriptor of a device. 255*/ 256 257/*! 258 \var usb_configuration_descriptor *usb_configuration_info::descr 259 \brief The configuration descriptor. 260*/ 261 262/*! 263 \var size_t usb_configuration_info::interface_count 264 \brief The number of interfaces in this configuration. 265*/ 266 267/*! 268 \var usb_interface_list *usb_configuration_info::interface 269 \brief The list of interfaces available to this configuration. 270*/ 271 272///// usb_iso_packet_descriptor ///// 273 274/*! 275 \struct usb_iso_packet_descriptor 276 \brief The descriptor for data packets of isochronous transfers. 277*/ 278 279/*! 280 \var int16 usb_iso_packet_descriptor::req_len 281 \brief Length of the request. 282*/ 283 284/*! 285 \var int16 usb_iso_packet_descriptor::act_len 286 \brief The USB stack writes the actual transferred length in this variable. 287*/ 288 289/*! 290 \var status_t usb_iso_packet_descriptor::status 291 \brief The status of the transfer. 292*/ 293 294///// usb_callback_func ///// 295 296/*! 297 \typedef typedef void (*usb_callback_func)(void *cookie, status_t status, void *data, size_t actualLength) 298 \brief Callback function for asynchronous transfers. 299 300 \param cookie The cookie you supplied when you queued the transfer. 301 \param status The status of the transfer. This is one of the following: 302 <table> 303 <tr> 304 <td><em>B_OK</em></td><td>Transfer succeeded.</td> 305 </tr> 306 <tr> 307 <td><em>B_CANCELED</em></td><td>Transfer cancelled by the user via a 308 usb_module_info::cancel_queued_transfers() call.</td> 309 </tr> 310 <tr> 311 <td><em>B_DEV_MULTIPLE_ERRORS</em></td><td>More than one of the errors 312 below. Unfortunately, the stack cannot give you more information.</td> 313 </tr> 314 <tr> 315 <td><em>B_DEV_STALLED</em></td><td>The endpoint is stalled. You can use 316 usb_module_info::clear_feature() method with the associated pipe and 317 the USB_FEATURE_ENDPOINT_HALT arguments.</td> 318 </tr> 319 <tr> 320 <td><em>B_DEV_DATA_OVERRUN</em></td><td>Incoming transfer: more data 321 flowing in than the size of the buffer.</td> 322 </tr> 323 <tr> 324 <td><em>B_DEV_DATA_UNDERRUN</em></td><td>Outgoing transfer: more data 325 flowing out than the endpoint accepts.</td> 326 </tr> 327 <tr> 328 <td><em>B_DEV_CRC_ERROR</em></td><td>The internal data consistency 329 checks of the USB protocol failed. You are best to retry. If you keep 330 on getting this error, there might be something wrong with the 331 device.</td> 332 </tr> 333 <tr> 334 <td><em>B_DEV_UNEXPECTED_PID</em></td><td>Internal error. You should 335 retry your transfer.</td> 336 </tr> 337 <tr> 338 <td><em>B_DEV_FIFO_OVERRUN</em></td><td>Internal error. You should 339 retry your transfer.</td> 340 </tr> 341 <tr> 342 <td><em>B_DEV_FIFO_UNDERRUN</em></td><td>Internal error. You should 343 retry your transfer.</td> 344 </tr> 345 </table> 346 \param data The provided buffer. 347 \param actualLength The amount of bytes read or written during the transfer. 348*/ 349 350///// usb_module_info ///// 351 352/*! 353 \struct usb_module_info 354 \brief Interface for drivers to interact with Haiku's USB stack. 355*/ 356 357/*! 358 \var usb_module_info::binfo 359 \brief Instance of the bus_manager_info object. 360*/ 361 362/*! 363 \fn status_t (*usb_module_info::register_driver)(const char *driverName, const usb_support_descriptor *supportDescriptors, size_t supportDescriptorCount, const char *optionalRepublishDriverName) 364 \brief Register your driver. 365 366 To let the USB stack know that a driver is available to support devices, a 367 driver needs to register itself first. To let the stack know which devices 368 it needs to notify the driver of, have a look at usb_support_descriptor. 369 370 It is possible to supply a list of support constructors. You should allocate 371 an array of support constructors, and give the amount of constructors in the 372 array using the \a supportDescriptorCount parameter. 373 374 In case your driver supports all devices, or more likely, in case you want to 375 monitor all devices plugged in and removed, it is safe to pass \c NULL to the 376 \a supportDescriptors paramater and zero (0) to \a supportDescriptorCount. 377 378 \param driverName A unique name that identifies your driver. Avoid names like 379 \c webcam or \c mouse, instead use vendor names and device types to avoid 380 nameclashes. The install_notify() and uninstall_notify() functions use the 381 driver name as an identifier. 382 \param supportDescriptors An array of the type usb_support_descriptor. Pass 383 the amount of objects in the next parameter. 384 \param supportDescriptorCount The number of objects in the array supplied in 385 the previous parameter. 386 \param optionalRepublishDriverName Unused parameter. You should pass \c NULL. 387 \retval B_OK The driver is registered. You can now call install_notify() 388 \retval B_BAD_VALUE You passed \c NULL as \a driverName. 389 \retval B_ERROR General internal error in the USB stack. You may retry the 390 request in this case. 391 \retval B_NO_MEMORY Error allocating some internal objects. The system is 392 out of memory. 393*/ 394 395/*! 396 \fn status_t (*usb_module_info::install_notify)(const char *driverName, const usb_notify_hooks *hooks) 397 \brief Install notify hooks for your driver. 398 399 After your driver is registered, you need to pass hooks to your driver that 400 are called whenever a device that matches your \link usb_support_descriptor 401 support descriptor \endlink . 402 403 As soon as the hooks are installed you'll receive callbacks for devices that 404 are already attached, so make sure your driver is initialized properly when 405 calling this method. 406 407 \param driverName The name you passed in register_driver(). 408 \param hooks The hooks the stack should call in case the status of devices 409 that match your support descriptor changes. 410 \retval B_OK Hooks are installed succesfully. 411 \retval B_NAME_NOT_FOUND Invalid \a driverName. 412 413 \see usb_notify_hooks for information on how your hooks should behave. 414 \see uninstall_notify() 415*/ 416 417/*! 418 \fn status_t (*usb_module_info::uninstall_notify)(const char *driverName) 419 \brief Uninstall notify hooks for your driver. 420 421 If your driver needs to stop, you can uninstall the notifier hooks. This will 422 clear the stored hooks in the driver and you will not receive any 423 notifications when new devices are attached. This method will also call 424 usb_notify_hooks::device_removed() for all the devices that you are using and 425 all the stack's resources that are allocated to your driver are cleared. 426 427 \param driverName The name you passed in register_driver(). 428 \retval B_OK Hooks are uninstalled. 429 \retval B_NAME_NOT_FOUND Invalid \a driverName. 430*/ 431 432/*! 433 \fn const usb_device_descriptor *(*usb_module_info::get_device_descriptor)(usb_device device) 434 \brief Get the device descriptor. 435 436 \param device The id of the device you want to query. 437 \return The standard usb_device_descriptor, or \c NULL in case of an error. 438*/ 439 440/*! 441 \fn const usb_configuration_info *(*usb_module_info::get_nth_configuration)(usb_device device, uint index) 442 \brief Get a configuration descriptor by index. 443 444 \param device The id of the device you want to query. 445 \param index The (zero based) offset of the list of configurations. 446 \return The usb_configuration_info with the standard usb configuration 447 descriptor, or \c NULL if the \a id is invalid or the \a index is out of 448 bounds. 449*/ 450 451/*! 452 \fn const usb_configuration_info *(*usb_module_info::get_configuration)(usb_device device) 453 \brief Get the current configuration. 454 455 \param id The id of the device you want to query. 456 \retval The usb_configuration_info with the standard usb configuration 457 descriptor, or \c NULL if the \a id is invalid. 458*/ 459 460/*! 461 \fn status_t (*usb_module_info::set_configuration)(usb_device device, const usb_configuration_info *configuration) 462 \brief Change the current configuration. 463 464 Changing the configuration will destroy all the current endpoints. If the 465 \a configuration points to the current configuration, the request will be 466 ignored and \c B_OK will be returned. 467 468 \param device The id of the device you want to query. 469 \param configuration The pointer to the new configuration you want to set. 470 \retval B_OK The new configuration is set succesfully. 471 \retval B_DEV_INVALID_PIPE The \a device parameter is invalid. 472 \retval B_BAD_VALUE The configuration does not exist. 473 474 \note This method also allows you to completely unconfigure the device, which 475 means that all the current endpoints, pipes and transfers will be freed. 476 Pass \c NULL to the parameter \a configuration if you want to do that. 477*/ 478 479/*! 480 \fn status_t (*usb_module_info::set_alt_interface)(usb_device device, const usb_interface_info *interface) 481 \brief Set an alternative interface. Not implemented. 482 483 This method currently always returns \c B_ERROR. 484*/ 485 486/*! 487 \fn status_t (*usb_module_info::set_feature)(usb_id handle, uint16 selector) 488 \brief Convenience function for standard control pipe set feature requests. 489 490 Both the set_feature() and clear_feature() requests work on all the Stack's 491 objects: devices, interfaces and pipes. 492 493 \param handle The object you want to query. 494 \param selector The value you want to pass in the feature request. 495 \return \c B_OK in case the request succeeded and the device responded 496 positively, or an error code in case it failed. 497*/ 498 499/*! 500 \fn status_t (*usb_module_info::clear_feature)(usb_id handle, uint16 selector) 501 \brief Convenience function for standard control pipe clear feature requests. 502 503 \see set_feature() to see how this method works. 504*/ 505 506/*! 507 \fn status_t (*usb_module_info::get_status)(usb_id handle, uint16 *status) 508 \brief Convenience function for standard usb status requests. 509 510 \param[in] handle The object you want to query. 511 \param[out] status A variable in which the device can store it's status. 512 \return \c B_OK in case the request succeeded and the device responded 513 positively, or an error code in case it failed. 514*/ 515 516/*! 517 \fn status_t (*usb_module_info::get_descriptor)(usb_device device, uint8 descriptorType, uint8 index, uint16 languageID, void *data, size_t dataLength, size_t *actualLength) 518 \brief Convenience function to get a descriptor from a device. 519 520 \param[in] device The device you want to query. 521 \param[in] descriptorType The type of descriptor you are requesting. 522 \param[in] index In case there are multiple descriptors of this type, you 523 select which one you want. 524 \param[in] languageID The language you want the descriptor in (if applicable, 525 like with string_descriptors). 526 \param[out] data The buffer in which the descriptor can be written. 527 \param[in] dataLength The size of the buffer (in bytes). 528 \param[out] actualLength A pointer to a variable in which the actual number 529 of bytes written can be stored. 530 \retval B_OK The request succeeded, and the descriptor is written. 531 \retval B_DEV_INVALID_PIPE Invalid \a device parameter. 532 \retval "other errors" Request failed. 533*/ 534 535/*! 536 \fn status_t (*usb_module_info::send_request)(usb_device device, uint8 requestType, uint8 request, uint16 value, uint16 index, uint16 length, void *data, size_t *actualLength) 537 \brief Send a generic, synchronous request over the default control pipe. 538 539 See queue_request() for an asynchronous version of this method. 540 541 Most of the standard values of a request are defined in USB_spec.h. 542 543 \param[in] device The device you want to query. 544 \param[in] requestType The request type. 545 \param[in] request The request you want to perform. 546 \param[in] value The value of the request. 547 \param[in] index The index for the request. 548 \param[in] length The size of the buffer pointed by \a data 549 \param[out] data The buffer where to put the result in. 550 \param[out] actualLength The actual numbers of bytes written. 551 552 \retval B_OK The request succeeded. 553 \retval B_DEV_INVALID_PIPE Invalid \a device parameter. 554 \retval "other errors" Request failed. 555*/ 556 557/*! 558 \fn status_t (*usb_module_info::queue_interrupt)(usb_pipe pipe, void *data, size_t dataLength, usb_callback_func callback, void *callbackCookie) 559 \brief Asynchronously queue an interrupt transfer. 560 561 \param pipe The id of the pipe you want to query. 562 \param data The data buffer you want to pass. 563 \param dataLength The size of the data buffer. 564 \param callback The callback function the stack should call after finishing. 565 \param callbackCookie A cookie that will be supplied to your callback 566 function when the transfer is finished. 567 568 \return Whether or not the queueing of the transfer went well. The return 569 value won't tell you if the transfer actually succeeded. 570 \retval B_OK The interrupt transfer is queued. 571 \retval B_NO_MEMORY Error allocating objects. 572 \retval B_DEV_INVALID_PIPE The \a pipe is not a valid interrupt pipe. 573*/ 574 575/*! 576 \fn status_t (*usb_module_info::queue_bulk)(usb_pipe pipe, void *data, size_t dataLength, usb_callback_func callback, void *callbackCookie) 577 \brief Asynchronously queue a bulk transfer. 578 579 This method behaves like the queue_interrupt() method, except that it queues 580 a bulk transfer. 581*/ 582 583/*! 584 \fn status_t (*usb_module_info::queue_bulk_v)(usb_pipe pipe, iovec *vector, size_t vectorCount, usb_callback_func callback, void *callbackCookie) 585 \brief Asynchronously queue a bulk vector. 586 587 This method behaves like the queue_interrupt() method, except that it queues 588 bulk transfers and that it is based on an (array of) io vectors. 589 590 \param vector One or more io vectors. IO vectors are standard POSIX entities. 591 \param vectorCount The number of elements in the \a vector array. 592*/ 593 594/*! 595 \fn status_t (*usb_module_info::queue_isochronous)(usb_pipe pipe, void *data, size_t dataLength, usb_iso_packet_descriptor *packetDesc, uint32 packetCount, uint32 *startingFrameNumber, uint32 flags, usb_callback_func callback, void *callbackCookie) 596 \brief Asynchronously queue a isochronous transfer. Not implemented. 597 598 Not implemented in the current Haiku USB Stack. 599*/ 600 601/*! 602 \fn status_t (*usb_module_info::queue_request)(usb_device device, uint8 requestType, uint8 request, uint16 value, uint16 index, uint16 length, void *data, usb_callback_func callback, void *callbackCookie) 603 \brief Asynchronously queue a control pipe request. 604 605 This method does roughly the same as send_request(), however, it works 606 asynchronously. This means that the method will return as soon as the 607 transfer is queued. 608 609 \param callback The callback function for when the transfer is done. 610 \param callbackCookie The cookie that the stack should pass to your callback 611 function. 612 \return Whether or not the queueing of the transfer went well. The return 613 value won't tell you if the transfer actually succeeded. 614 \retval B_OK The control transfer is queued. 615 \retval B_NO_MEMORY Error allocating objects. 616 \retval B_DEV_INVALID_PIPE The \a device argument is invalid. 617*/ 618 619/*! 620 \fn status_t (*usb_module_info::set_pipe_policy)(usb_pipe pipe, uint8 maxNumQueuedPackets, uint16 maxBufferDurationMS, uint16 sampleSize) 621 \brief Set some pipe features. 622 623 The USB standard specifies some properties that should be able to be set on 624 isochronous pipes. If your driver requires the properties to be changed, you 625 should use this method. 626 627 \param pipe The id of the isochronous pipe you want to alter. 628 \param maxNumQueuedPackets The maximum number of queued packets allowed on 629 this pipe. 630 \param maxBufferDurationMS The maximum time in ms that the buffers are valid. 631 \param sampleSize The size of the samples through this pipe. 632 \retval B_OK Pipe policy changed. 633 \retval B_DEV_INVALID_PIPE The \a pipe argument is invalid or not an 634 isochronous pipe. 635*/ 636 637/*! 638 \fn status_t (*usb_module_info::cancel_queued_transfers)(usb_pipe pipe) 639 \brief Cancel pending transfers on a pipe. 640 641 All the pending transfers will be cancelled. The stack will perform the 642 callback on all of them that are cancelled. 643 644 \attention There might be transfers that are being executed the moment you 645 call this method. These will be executed, and their callbacks will be 646 performed. Make sure you don't delete any buffers that could still be used 647 by these transfers. 648 649 \param pipe The id of the pipe to clear. 650 651 \retval B_OK All the pending transfers on this pipe are deleted. 652 \retval B_DEV_INVALID_PIPE The supplied usb_id is not a valid pipe. 653 \retval "other errors" There was an error clearing the pipe. 654*/ 655 656/*! 657 \fn status_t (*usb_module_info::usb_ioctl)(uint32 opcode, void *buffer, size_t bufferSize) 658 \brief Low level commands to the USB stack. 659 660 This method is used to give lowlevel commands to the Stack. There are 661 currently no uses documented. 662*/ 663 664///// B_USB_MODULE_NAME ///// 665 666/*! 667 \def B_USB_MODULE_NAME 668 \brief The identifier string for the USB Stack interface module. 669*/ 670