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