xref: /haiku/docs/user/drivers/USB3.dox (revision c9ad965c81b08802fed0827fd1dd16f45297928a)
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 struct 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  Once 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 they can be used to match a vendor and/or product.
113  If any field has the value \c 0, it 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 supported 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>The transfer succeeded.</td>
305      </tr>
306      <tr>
307        <td><em>B_CANCELED</em></td><td>The transfer was cancelled by the user
308          via a 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 occurred. Unfortunately, the stack cannot give you more
313          information.</td>
314      </tr>
315      <tr>
316        <td><em>B_DEV_STALLED</em></td><td>The endpoint is stalled. You can use
317          usb_module_info::clear_feature() method with the associated pipe and
318          the USB_FEATURE_ENDPOINT_HALT arguments.</td>
319      </tr>
320      <tr>
321        <td><em>B_DEV_DATA_OVERRUN</em></td><td>Incoming transfer: more data
322          flowing in than the size of the buffer.</td>
323      </tr>
324      <tr>
325        <td><em>B_DEV_DATA_UNDERRUN</em></td><td>Outgoing transfer: more data
326          is flowing out than the endpoint accepts.</td>
327      </tr>
328      <tr>
329        <td><em>B_DEV_CRC_ERROR</em></td><td>The internal data consistency
330          checks of the USB protocol failed. It is best to retry. If you keep
331          on getting this error there might be something wrong with the
332          device.</td>
333      </tr>
334      <tr>
335        <td><em>B_DEV_UNEXPECTED_PID</em></td><td>There was an internal error.
336          You should retry your transfer.</td>
337      </tr>
338      <tr>
339        <td><em>B_DEV_FIFO_OVERRUN</em></td><td>internal error.
340          You should retry your transfer.</td>
341      </tr>
342      <tr>
343         <td><em>B_DEV_FIFO_UNDERRUN</em></td><td>There was an internal error.
344           You should retry your transfer.</td>
345      </tr>
346    </table>
347  \param data The provided buffer.
348  \param actualLength The amount of bytes read or written during the transfer.
349*/
350
351///// usb_module_info /////
352
353/*!
354  \struct usb_module_info
355  \brief Interface for drivers to interact with Haiku's USB stack.
356*/
357
358/*!
359  \var usb_module_info::binfo
360  \brief Instance of the bus_manager_info object.
361*/
362
363/*!
364  \fn status_t (*usb_module_info::register_driver)(const char *driverName, const usb_support_descriptor *supportDescriptors, size_t supportDescriptorCount, const char *optionalRepublishDriverName)
365  \brief Register your driver.
366
367  To let the USB stack know that a driver is available to support devices, a
368  driver needs to register itself first. To let the stack know about devices
369  it needs to notify the driver of, have a look at usb_support_descriptor.
370
371  It is possible to supply a list of support constructors. You should allocate
372  an array of support constructors and give the amount of constructors in the
373  array using the \a supportDescriptorCount parameter.
374
375  In case your driver supports all devices or, more likely, you want to
376  monitor all devices plugged in and removed, it is safe to pass \c NULL to the
377  \a supportDescriptors paramater and zero (0) to \a supportDescriptorCount.
378
379  \param driverName A unique name that identifies your driver. Avoid names like
380    \c webcam or \c mouse, instead use vendor names and device types to avoid
381    nameclashes. The install_notify() and uninstall_notify() functions use the
382    driver name as an identifier.
383  \param supportDescriptors An array of the type usb_support_descriptor. Pass
384    the amount of objects in the next parameter.
385  \param supportDescriptorCount The number of objects in the array supplied in
386    the previous parameter.
387  \param optionalRepublishDriverName Unused parameter. You should pass \c NULL.
388  \retval B_OK The driver is registered. You can now call install_notify()
389  \retval B_BAD_VALUE You passed \c NULL as \a driverName.
390  \retval B_ERROR General internal error in the USB stack. You may retry the
391    request in this case.
392  \retval B_NO_MEMORY Error allocating some internal objects. The system is
393    out of memory.
394*/
395
396/*!
397  \fn status_t (*usb_module_info::install_notify)(const char *driverName, const usb_notify_hooks *hooks)
398  \brief Install notify hooks for your driver.
399
400  After your driver is registered, you need to pass hooks to your driver that
401  are called whenever a device that matches your \link usb_support_descriptor
402  support descriptor \endlink .
403
404  As soon as the hooks are installed, you'll receive callbacks for devices that
405  are already attached; so make sure your driver is initialized properly when
406  calling this method.
407
408  \param driverName The name you passed in register_driver().
409  \param hooks The hooks the stack should call in case the status of devices
410    that match your support descriptor changes.
411  \retval B_OK Hooks are installed succesfully.
412  \retval B_NAME_NOT_FOUND Invalid \a driverName.
413
414  \see usb_notify_hooks for information on how your hooks should behave.
415  \see uninstall_notify()
416*/
417
418/*!
419  \fn status_t (*usb_module_info::uninstall_notify)(const char *driverName)
420  \brief Uninstall notify hooks for your driver.
421
422  If your driver needs to stop, you can uninstall the notifier hooks. This will
423  clear the stored hooks in the driver, and you will not receive any
424  notifications when new devices are attached. This method will also call
425  usb_notify_hooks::device_removed() for all the devices that you are using and
426  all the stack's resources that are allocated to your driver are cleared.
427
428  \param driverName The name you passed in register_driver().
429  \retval B_OK Hooks are uninstalled.
430  \retval B_NAME_NOT_FOUND Invalid \a driverName.
431*/
432
433/*!
434  \fn const usb_device_descriptor *(*usb_module_info::get_device_descriptor)(usb_device device)
435  \brief Get the device descriptor.
436
437  \param device The id of the device you want to query.
438  \return The standard usb_device_descriptor, or \c NULL in case of an error.
439*/
440
441/*!
442  \fn const usb_configuration_info	*(*usb_module_info::get_nth_configuration)(usb_device device, uint index)
443  \brief Get a configuration descriptor by index.
444
445  \param device The id of the device you want to query.
446  \param index The (zero based) offset of the list of configurations.
447  \return This will normally return the usb_configuration_info with the
448    standard usb configuration descriptor.  \c NULL will be returned if the
449    \a id is invalid or the \a index is out of bounds.
450*/
451
452/*!
453  \fn const usb_configuration_info	*(*usb_module_info::get_configuration)(usb_device device)
454  \brief Get the current configuration.
455
456  \param id The id of the device you want to query.
457  \retval This will return usb_configuration_info with the standard usb
458    configuration descriptor, or it will return\c NULL if the \a id is invalid.
459*/
460
461/*!
462  \fn status_t (*usb_module_info::set_configuration)(usb_device device, const usb_configuration_info *configuration)
463  \brief Change the current configuration.
464
465  Changing the configuration will destroy all the current endpoints. If the
466  \a configuration points to the current configuration, the request will be
467  ignored and \c B_OK will be returned.
468
469  \param device The id of the device you want to query.
470  \param configuration The pointer to the new configuration you want to set.
471  \retval B_OK The new configuration is set succesfully.
472  \retval B_DEV_INVALID_PIPE The \a device parameter is invalid.
473  \retval B_BAD_VALUE The configuration does not exist.
474
475  \note This method also allows you to completely unconfigure the device, which
476    means that all the current endpoints, pipes and transfers will be freed.
477    Pass \c NULL to the parameter \a configuration if you want to do that.
478*/
479
480/*!
481  \fn status_t (*usb_module_info::set_alt_interface)(usb_device device, const usb_interface_info *interface)
482  \brief Set an alternative interface. Not implemented.
483
484  This method currently always returns \c B_ERROR.
485*/
486
487/*!
488  \fn status_t (*usb_module_info::set_feature)(usb_id handle, uint16 selector)
489  \brief Convenience function for standard control pipe set feature requests.
490
491  Both the set_feature() and clear_feature() requests work on all the Stack's
492  objects: devices, interfaces and pipes.
493
494  \param handle The object you want to query.
495  \param selector The value you want to pass in the feature request.
496  \return \c B_OK in case the request succeeded and the device responded
497    positively, or an error code in case it failed.
498*/
499
500/*!
501  \fn status_t (*usb_module_info::clear_feature)(usb_id handle, uint16 selector)
502  \brief Convenience function for standard control pipe clear feature requests.
503
504  \see set_feature() to see how this method works.
505*/
506
507/*!
508  \fn status_t (*usb_module_info::get_status)(usb_id handle, uint16 *status)
509  \brief Convenience function for standard usb status requests.
510
511  \param[in] handle The object you want to query.
512  \param[out] status A variable in which the device can store it's status.
513  \return \c B_OK is returned in case the request succeeded and the device
514   responded positively, or an error code is returned in case it failed.
515*/
516
517/*!
518  \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)
519  \brief Convenience function to get a descriptor from a device.
520
521  \param[in] device The device you want to query.
522  \param[in] descriptorType The type of descriptor you are requesting.
523  \param[in] index In case there are multiple descriptors of this type, you
524    select which one you want.
525  \param[in] languageID The language you want the descriptor in (if applicable,
526    as with string_descriptors).
527  \param[out] data The buffer in which the descriptor can be written.
528  \param[in] dataLength The size of the buffer (in bytes).
529  \param[out] actualLength A pointer to a variable in which the actual number
530    of bytes written can be stored.
531  \retval B_OK The request succeeded, and the descriptor is written.
532  \retval B_DEV_INVALID_PIPE Invalid \a device parameter.
533  \retval "other errors" Request failed.
534*/
535
536/*!
537  \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)
538  \brief Send a generic, synchronous request over the default control pipe.
539
540  See queue_request() for an asynchronous version of this method.
541
542  Most of the standard values of a request are defined in USB_spec.h.
543
544  \param[in] device The device you want to query.
545  \param[in] requestType The request type.
546  \param[in] request The request you want to perform.
547  \param[in] value The value of the request.
548  \param[in] index The index for the request.
549  \param[in] length  The size of the buffer pointed by \a data
550  \param[out] data The buffer where to put the result in.
551  \param[out] actualLength The actual numbers of bytes written.
552
553  \retval B_OK The request succeeded.
554  \retval B_DEV_INVALID_PIPE Invalid \a device parameter.
555  \retval "other errors" Request failed.
556*/
557
558/*!
559  \fn status_t (*usb_module_info::queue_interrupt)(usb_pipe pipe, void *data, size_t dataLength, usb_callback_func callback, void *callbackCookie)
560  \brief Asynchronously queue an interrupt transfer.
561
562  \param pipe The id of the pipe you want to query.
563  \param data The data buffer you want to pass.
564  \param dataLength The size of the data buffer.
565  \param callback The callback function the stack should call after finishing.
566  \param callbackCookie A cookie that will be supplied to your callback
567    function when the transfer is finished.
568
569  \return This will return a value indicating whether or not the queueing of
570    the transfer went well. The return value won't tell you if the transfer
571    actually succeeded.
572  \retval B_OK The interrupt transfer is queued.
573  \retval B_NO_MEMORY Error allocating objects.
574  \retval B_DEV_INVALID_PIPE The \a pipe is not a valid interrupt pipe.
575*/
576
577/*!
578  \fn status_t (*usb_module_info::queue_bulk)(usb_pipe pipe, void *data, size_t dataLength, usb_callback_func callback, void *callbackCookie)
579  \brief Asynchronously queue a bulk transfer.
580
581  This method behaves like the queue_interrupt() method, except that it queues
582  a bulk transfer.
583*/
584
585/*!
586  \fn status_t (*usb_module_info::queue_bulk_v)(usb_pipe pipe, iovec *vector, size_t vectorCount, usb_callback_func callback, void *callbackCookie)
587  \brief Asynchronously queue a bulk vector.
588
589  This method behaves like the queue_interrupt() method, except that it queues
590  bulk transfers and that it is based on an (array of) io vectors.
591
592  \param vector One or more io vectors. IO vectors are standard POSIX entities.
593  \param vectorCount The number of elements in the \a vector array.
594*/
595
596/*!
597  \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)
598  \brief Asynchronously queue a isochronous transfer. Not implemented.
599
600  This is not implemented in the current Haiku USB Stack.
601*/
602
603/*!
604  \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)
605  \brief Asynchronously queue a control pipe request.
606
607  This method does roughly the same as send_request(), however, it works
608  asynchronously. This means that the method will return as soon as the
609  transfer is queued.
610
611  \param callback The callback function for when the transfer is done.
612  \param callbackCookie The cookie that the stack should pass to your callback
613    function.
614    \return Whether or not the queueing of the transfer went well. The return
615   value won't tell you if the transfer actually succeeded.
616  \retval B_OK The control transfer is queued.
617  \retval B_NO_MEMORY Error allocating objects.
618  \retval B_DEV_INVALID_PIPE The \a device argument is invalid.
619*/
620
621/*!
622  \fn status_t (*usb_module_info::set_pipe_policy)(usb_pipe pipe, uint8 maxNumQueuedPackets, uint16 maxBufferDurationMS, uint16 sampleSize)
623  \brief Set some pipe features.
624
625  The USB standard specifies some properties that should be able to be set on
626  isochronous pipes. If your driver requires the properties to be changed, you
627  should use this method.
628
629  \param pipe The id of the isochronous pipe you want to alter.
630  \param maxNumQueuedPackets The maximum number of queued packets allowed on
631    this pipe.
632  \param maxBufferDurationMS The maximum time in ms that the buffers are valid.
633  \param sampleSize The size of the samples through this pipe.
634  \retval B_OK Pipe policy changed.
635  \retval B_DEV_INVALID_PIPE The \a pipe argument is invalid or not an
636    isochronous pipe.
637*/
638
639/*!
640  \fn status_t (*usb_module_info::cancel_queued_transfers)(usb_pipe pipe)
641  \brief Cancel pending transfers on a pipe.
642
643  All the pending transfers will be cancelled. The stack will perform the
644  callback on all of them that are cancelled.
645
646  \attention There might be transfers that are being executed the moment you
647    call this method. These will be executed, and their callbacks will be
648    performed. Make sure you don't delete any buffers that could still be used
649    by these transfers.
650
651  \param pipe The id of the pipe to clear.
652
653  \retval B_OK All the pending transfers on this pipe are deleted.
654  \retval B_DEV_INVALID_PIPE The supplied usb_id is not a valid pipe.
655  \retval "other errors" There was an error clearing the pipe.
656*/
657
658/*!
659  \fn status_t (*usb_module_info::usb_ioctl)(uint32 opcode, void *buffer, size_t bufferSize)
660  \brief Low level commands to the USB stack.
661
662  This method is used to give lowlevel commands to the Stack. There are
663  currently no uses documented.
664*/
665
666///// B_USB_MODULE_NAME /////
667
668/*!
669  \def B_USB_MODULE_NAME
670  \brief The identifier string for the USB Stack interface module.
671*/
672