xref: /illumos-gate/usr/src/man/man9e/usba_hcdi.9e (revision c193478586214940af708897e19c9a878b6a6223)
1.\"
2.\" This file and its contents are supplied under the terms of the
3.\" Common Development and Distribution License ("CDDL"), version 1.0.
4.\" You may only use this file in accordance with the terms of version
5.\" 1.0 of the CDDL.
6.\"
7.\" A full copy of the text of the CDDL should have accompanied this
8.\" source.  A copy of the CDDL is also available via the Internet at
9.\" http://www.illumos.org/license/CDDL.
10.\"
11.\"
12.\" Copyright 2016 Joyent, Inc.
13.\"
14.Dd November 18, 2016
15.Dt USBA_HCDI 9E
16.Os
17.Sh NAME
18.Nm usba_hcdi
19.Nd USB Host Controller Driver Interface
20.Sh SYNOPSIS
21.In sys/usb/usba/hcdi.h
22.Sh INTERFACE LEVEL
23.Sy Volatile -
24illumos USB HCD private function
25.Pp
26This describes private interfaces that are not part of the stable DDI.
27This may be removed or changed at any time.
28.Sh DESCRIPTION
29.Sy hcdi
30drivers are device drivers that support USB host controller hardware.
31USB host controllers provide an interface between the operating system
32and USB devices. They abstract the interface to the devices, often
33provide ways of performing DMA, and also act as the root hub.
34.Pp
35.Sy hcdi
36drivers are part of the illumos USB Architecture (USBA). The
37.Xr usba 7D
38driver provides support for many of the surrounding needs of an
39.Sy hcdi
40driver and requires that such drivers implement a specific operations
41vector,
42.Xr usba_hcdi_ops 9S .
43These functions cover everything from initialization to performing I/O
44to USB devices on behalf of client device drivers.
45.Ss USB Speed and Version Background
46USB devices are often referred to in two different ways. The first way
47is the USB version that they conform to. In the wild this looks like USB
481.1, USB 2.0, USB 3.0, etc.. However, devices are also referred to as
49.Sq full- ,
50.Sq low- ,
51.Sq high- ,
52.Sq super-
53speed devices.
54.Pp
55The latter description describes the maximum theoretical speed of a
56given device. For example, a super-speed device theoretically caps out
57around 5 Gbit/s, whereas a low-speed device caps out at 1.5 Mbit/s.
58.Pp
59In general, each speed usually corresponds to a specific USB protocol
60generation. For example, all USB 3.0 devices are super-speed devices.
61All 'high-speed' devices are USB 2.x devices. Full-speed devices are
62special in that they can either be USB 1.x or USB 2.x devices. Low-speed
63devices are only a USB 1.x thing, they did not jump the fire line to USB
642.x.
65.Pp
66USB 3.0 devices and ports generally have the wiring for both USB 2.0 and
67USB 3.0. When a USB 3.0 device is plugged into a USB 2.0 port or hub,
68then it will report its version as USB 2.1, to indicate that it is
69actually a USB 3.0 device.
70.Ss USB Endpoint Background
71To understand the organization of the functions that make up the hcdi
72operations vector, it helps to understand how USB devices are organized
73and work at a high level.
74.Pp
75A given USB device is made up of
76.Em endpoints .
77A request, or transfer, is made to a specific USB endpoint. These
78endpoints can provide different services and have different expectations
79around the size of the data that'll be used in a given request and the
80periodicity of requests. Endpoints themselves are either used to make
81one-shot requests, for example, making requests to a mass storage device
82for a given sector, or for making periodic requests where you end up
83polling on the endpoint, for example, polling on a USB keyboard for
84keystrokes.
85.Pp
86Each endpoint encodes two different pieces of information: a direction
87and a type. There are two different directions: IN and OUT. These refer
88to the general direction that data moves relative to the operating
89system. For example, an IN transfer transfers data in to the operating
90system, from the device. An OUT transfer transfers data from the
91operating system, out to the device.
92.Pp
93There are four different kinds of endpoints:
94.Bl -tag -width Sy -offset indent
95.It Sy BULK
96These transfers are large transfers of data to or from a device.  The
97most common use for bulk transfers is for mass storage devices.  Though
98they are often also used by network devices and more. Bulk endpoints do
99not have an explicit time component to them. They are always used for
100one-shot transfers.
101.It Sy CONTROL
102These transfers are used to manipulate devices themselves and are used
103for USB protocol level operations (whether device-specific,
104class-specific, or generic across all of USB). Unlike other transfers,
105control transfers are always bi-directional and use different kinds of
106transfers.
107.It Sy INTERRUPT
108Interrupt transfers are used for small transfers that happen
109infrequently, but need reasonable latency. A good example of interrupt
110transfers is to receive input from a USB keyboard. Interrupt-IN
111transfers are generally polled. Meaning that a client (device driver)
112opens up an interrupt-IN endpoint to poll on it, and receives periodic
113updates whenever there is information available. However, Interrupt
114transfers can be used as one-shot transfers both going IN and OUT.
115.It Sy ISOCHRONOUS
116These transfers are things that happen once per time-interval at a very
117regular rate. A good example of these transfers are for audio and video.
118A device may describe an interval as 10ms at which point it will read or
119write the next batch of data every 10ms and transform it for the user.
120There are no one-shot Isochronous-IN transfers. There are one-shot
121Isochronous-OUT transfers, but these are used by device drivers to
122always provide the system with sufficient data.
123.El
124.Pp
125To find out information about the endpoints, USB devices have a series
126of descriptors that cover different aspects of the device. For example,
127there are endpoint descriptors which cover the properties of endpoints
128such as the maximum packet size or polling interval.
129.Pp
130Descriptors exist at all levels of USB. For example, there are general
131descriptors for every device. The USB device descriptor is described in
132.Xr usb_dev_descr 9S .
133Host controllers will look at these descriptors to ensure that they
134program the device correctly; however, they are more often used by
135client device drivers. There are also descriptors that exist at a class
136level. For example, the hub class has a class-specific descriptor which
137describes properties of the hub. That information is requested for and
138used by the hub driver.
139.Pp
140All of the different descriptors are gathered by the system and placed
141into a tree, with device descriptors, configurations, endpoints, and
142more. Client device drivers gain access to this tree and then use them
143to then open endpoints, which are called pipes in USBA (and some
144revisions of the USB specification).
145.Pp
146Each pipe gives access to a specific endpoint on the device which can be
147used to perform transfers of a specific type and direction. For example,
148a mass storage device often has three different endpoints, the default
149control endpoint (which every device has), a Bulk-IN endpoint, and a
150Bulk-OUT endpoint. The device driver ends up with three open pipes. One
151to the default control endpoint to configure the device, and then the
152other two are used to perform I/O.
153.Pp
154These routines translate more or less directly into calls to a host
155controller driver. A request to open a pipe takes an endpoint descriptor
156that describes the properties of the pipe, and the host controller
157driver goes through and does any work necessary to allow the client
158device driver to access it. Once the pipe is open, it either makes
159one-shot transfers specific to the transfer type or it starts performing
160a periodic poll of an endpoint.
161.Pp
162All of these different actions translate into requests to the host
163controller. The host controller driver itself is in charge of making
164sure that all of the required resources for polling are allocated with a
165request and then proceed to give the driver's periodic callbacks.
166.Pp
167For each of the different operations described above, there is a
168corresponding entry in
169.Xr usba_hcdi_ops 9S .
170For example, open an endpoint, the host controller has to implement
171.Xr usba_hcdi_pipe_open 9E
172and for each transfer type, there is a different transfer function. One
173example is
174.Xr usba_hcdi_bulk_xfer 9E .
175See
176.Xr usba_hcdi_ops 9S
177for a full list of the different function endpoints.
178.Ss HCDI Initialization
179hcdi drivers are traditional character device drivers. To start with, an
180hcdi driver should define traditional
181.Xr dev_ops 9S
182and
183.Xr cb_ops 9S
184structures. To get started, the device driver should perform normal
185device initialization in an
186.Xr attach 9E
187entry point. For example, PCI devices should setup the device's
188registers and program them. In addition, all devices should
189configure interrupts, before getting ready to call into the USBA. Each
190instance of a device must be initialized and registered with the USBA.
191.Pp
192To initialize a device driver with the USBA, it must first call
193.Xr usba_alloc_hcdi_ops 9F .
194This provides a device driver with the
195.Xr usba_hcdi_ops 9S
196structure that it must fill out. Please see
197.Xr usba_hcdi_ops 9S
198for instructions on how it should be filled out. Once filled out, the
199driver should call
200.Xr usba_hcdi_register 9F .
201.Pp
202If the call to register fails for whatever reason, the device driver
203should fail its
204.Xr attach 9E
205entry point. After this call successfully completes, the driver should
206assume that any of the functions it registered with the call to
207.Xr usba_hcdi_register 9F
208will be called at this point.
209.Ss Binding the Root Hub
210Once this is set up, the hcdi driver must initialize its root hub by
211calling
212Xr usba_hcdi_bind_root_hub 9F .
213To bind the root hub, the device driver is responsible for providing a
214device descriptor that represents the hardware. Depending on the
215hardware, this descriptor may be either static or dynamic.
216.Pp
217This device descriptor should be a packed descriptor that is the same
218that would be read off of the device. The device descriptor should match
219a hub of a USB generation equivalent to the maximum speed of the device.
220For example, a USB 3.0 host controller would use a USB 3.0 hub's device
221descriptor.  Similarly, a USB 2.0 host controller would use a USB 2.0
222hub's device descriptor.
223.Pp
224The descriptor first starts with a USB configuration descriptor, as
225defined in
226.Xr usb_cfg_descr 9S .
227It is then followed by an interface descriptor. The definition for it
228can be found in
229.Xr usb_if_descr 9S .
230Next is the endpoint descriptor for the single Interrupt-IN endpoint
231that all hubs have as defined in
232.Xr usb_ep_descr 9S .
233Finally, any required companion descriptors should be used. For example,
234a USB 3.x hub will have a
235.Xr usb_ep_ss_comp_descr 9S
236appended to the structure.
237.Pp
238Note, that the structure needs to be packed, as though it were read from
239a device. The structures types referenced in
240.Xr usb_cfg_descr 9S ,
241.Xr usb_if_descr 9S ,
242.Xr usb_ep_descr 9S ,
243and
244.Xr usb_ep_ss_comp_descr 9S
245are not packed for this purpose. They should not be used as they have
246gaps added by the compiler for alignment.
247.Pp
248Once assembled, the device driver should call
249.Xr usb_hubdi_bind_root_hub 9F .
250This will cause an instance of the
251.Xr hubd 7D
252driver to be attached and associated with the root controller. As such,
253driver writers need to ensure that all initialization is done prior to
254loading the root hub. Once successfully loaded, driver writers should
255assume that they'll get other calls into the driver's operation vector
256before the call to
257.Xr usb_hcdi_bind_root_hub 9F.
258.Pp
259If the call to
260.Xr usb_hcdi_bind_root_hub 9F
261failed for whatever reason, the driver should unregister from USBA (see
262the next section), unwind all of the resources it has allocated, and
263return
264.Dv DDI_FAILURE .
265.Pp
266Otherwise, at this point it's safe to assume that the instance of the
267device has initialized successfully and the driver should return
268.Dv DDI_SUCCESS .
269.Ss Driver Teardown
270When a driver's
271.Xr detach 9E
272entry point has been called, before anything else is done, the device
273driver should unbind its instance of the root hub and then unregister
274from the USBA.
275.Pp
276To unbind the root hub, the instance of the driver should call
277.Xr usba_hubdi_unbind_root_hub 9F .
278If for some reason that function does not return
279.Sy USB_SUCCESS ,
280then the device driver should fail the call to
281.Xr detach 9E
282and return
283.Dv DDI_FAILURE .
284.Pp
285Once the root hub has been unbound, the device driver can continue by
286removing its hcdi registration with USBA. To do this, the driver should
287call
288.Xr usba_hcdi_unregister 9F .
289As this call always succeeds, at this point, it is safe for the driver
290to tear down all the rest of its resources and successfully detach.
291.Ss State Tracking and Minor Numbers
292Because a host controller driver is also a root hub, there are a few
293constraints around how the device must store its per-instance state and
294how its minor numbers are used.
295.Pp
296hcdi drivers
297.Em must not
298store any data with
299.Xr ddi_get_driver_private 9F .
300This private data is used by USBA. If it has been called before the
301device registers, then it will fail to register successfully with the
302USBA. However, setting it after that point will corrupt the state of the
303USBA and likely lead to data corruption and crashes.
304.Pp
305Similarly, part of the minor number space is utilized to represent
306various devices like the root hub. Whenever a device driver is presented
307with a
308.Ft dev_t
309and it's trying to extract the minor number, it must take into account
310the constant
311.Dv HUBD_IS_ROOT_HUB .
312The following shows how to perform this, given a
313.Ft dev_t
314called
315.Ft dev :
316.Bd -literal -offset indent
317minor_t minor = getminor(dev) & ~HUBD_IS_ROOT_HUB;
318.Ed
319.Ss Required Character and Device Operations
320The USBA handles many character and device operations entry points for a
321device driver or has strict rules on what a device driver must do in
322them. This section summarizes those constraints.
323.Pp
324In the
325.Xr dev_ops 9S
326structure, the following members have special significance:
327.Bl -tag -offset indent -width Sy
328.It Sy devo_bus_ops
329The
330.Sy devo_bus_ops
331member should be set to the symbol
332.Sy usba_hubdi_busops .
333See
334.Xr usba_hubdi_devops 9F
335for more information.
336.It Sy devo_power
337The
338.Sy devo_power
339member should be set to the symbol
340.Sy usba_hubdi_root_hub_power .
341See
342.Xr usba_hubdi_Devops 9F
343for more information.
344.El
345.Pp
346The other standard entry points for character devices,
347.Sy devo_getinfo ,
348.Sy devo_attach ,
349and
350.Sy devo_detach
351should be implemented normally as per
352.Xr getinfo 9E ,
353.Xr attach 9E ,
354and
355.Xr detach 9E
356respectively.
357.Pp
358The following members of the
359.Xr cb_ops 9S
360operations vector must be implemented and set:
361.Bl -tag -offset indent -width Sy
362.It Sy cb_open
363The device driver should implement an
364.Xr open 9E
365entry point that obtains access to its
366.Sy dev_info_t
367and then calls
368.Xr usba_hubdi_open 9F . See
369.Xr usba_hcdi_cb_open 9E
370for more information.
371.It Sy cb_close
372The device driver should implement a
373.Xr close 9E
374entry point that obtains access to its
375.Sy dev_info_t
376and then calls
377.Xr usba_hubdi_close 9F . See
378.Xr usba_hcdi_cb_close 9E
379for more information.
380.It Sy cb_ioctl
381The device driver should implement a
382.Xr ioctl 9E
383entry point that obtains access to its
384.Sy dev_info_t
385and then calls
386.Xr usba_hubdi_ioctl 9F .
387.Pp
388If the device driver wishes to have private ioctls, it may check the
389ioctl command before calling
390.Xr usba_hubdi_ioctl 9F .
391Because the
392.Xr usba_hubdi_ioctl 9F
393function normally takes care of checking for the proper privileges,
394device drivers must verify that a caller has appropriate privileges
395before processing any private ioctls.
396.Pp
397See
398.Xr usba_hcdi_cb_ioctl 9E
399for more information.
400.It Sy cb_prop_op
401The
402.Sy cb_prop_op
403member should be set to
404.Xr ddi_prop_op 9F .
405.It Sy cb_flag
406The
407.Sy cb_flag
408member should be set to the bitwise-inclusive-OR of the
409.Sy D_MP
410flag
411and the
412.Sy D_HOTPLUG
413flag.
414.El
415.Pp
416All other members of the
417.Xr cb_ops 9S
418structure should not be implemented and set to the appropriate value,
419such as
420.Xr nodev 9F
421or
422.Xr nochpoll 9F .
423.Ss Locking
424In general, the USBA calls into a device driver through one of the
425functions that it has register in the
426.Xr usba_hcdi_ops 9S
427structure. However, in response to a data transfer, the device driver
428will need to call back into the USBA by calling
429.Xr usba_hcdi_cb 9F .
430.Pp
431A device driver must hold
432.Em no locks
433across the call to
434.Xr usba_hcdi_cb 9F .
435Returning an I/O to the USBA, particularly an error, may result in
436another call back to one of the
437.Xr usba_hcdi_cb 9F
438vectors.
439.Pp
440Outside of that constraint, the device driver should perform locking of
441its data structures. It should assume that many of its entry points will
442be called in parallel across the many devices that exist.
443.Pp
444There are certain occasions where a device driver may have to enter the
445.Sy p_mutex
446member of the
447.Xr usba_pipe_handle_data 9S
448structure when duplicating isochronous or interrupt requests. The USBA
449should in general, not hold this lock across calls to the HCD driver,
450and in turn, the HCD driver should not hold this lock across any calls
451back to the USBA. As such, the HCD driver should make sure to
452incorporate the lock ordering of this mutex into its broader lock
453ordering and operational theory. Generally, the
454.Sy p_mutex
455mutex will be entered after any HCD-specific locks.
456.Pp
457The final recommendation is that due to the fact that the host
458controller driver provides services to a multitude of USB devices at
459once, it should strive not to hold its own internal locks while waiting
460for I/O to complete, such as an issued command. This is particularly
461true if the device driver uses coarse grained locking. If the device
462driver does not pay attention to these conditions, it can easily lead to
463service stalls.
464.Ss Synchronous and Asynchronous Entry Points
465The majority of the entry points that a host controller driver has to
466implement are
467.Em synchronous .
468All actions that the entry point implies must be completed before the
469entry point returns. However, the various transfer routines:
470.Xr usba_hcdi_pipe_bulk_xfer 9E ,
471.Xr usba_hcdi_pipe_ctrl_xfer 9E ,
472.Xr usba_hcdi_pipe_intr_xfer 9E ,
473and
474.Xr usba_hcdi_pipe_isoc_xfer 9E ,
475are ultimately
476.Em asynchronous
477entry points.
478.Pp
479Each of the above entry points begins one-shot or periodic I/O. When the
480driver returns
481.Sy USB_SUCCESS
482from one of those functions, it is expected that it will later call
483.Xr usba_hcdi_cb 9F
484when the I/O completes, whether successful or not. It is the driver's
485responsibility to keep track of these outstanding transfers and time
486them out. For more information on timeouts, see the section
487.Sx Endpoint Timeouts .
488.Pp
489If for some reason, the driver fails to initialize the I/O transfer and
490indicates this by returning a value other than
491.Sy USB_SUCCESS
492from its entry point, then it must not call
493.Xr usba_hcdi_cb 9F
494for that transfer.
495.Ss Short Transfers
496Not all USB transfers will always return the full amount of data
497requested in the transfer. Host controller drivers need to be ready for
498this and report it. Each request structure has an attribute to indicate
499whether or not short transfers are OK. If a short transfer is OK, then
500the driver should update the transfer length. Otherwise, it should
501instead return an error. See the individual entry point pages for more
502information.
503.Ss Root Hub Management
504As was mentioned earlier, every host controller is also a root hub. The
505USBA interfaces with the root hub no differently than any other hub. The
506USBA will open pipes and issue both control and periodic interrupt-IN
507transfers to the root hub.
508.Pp
509In the host controller driver's
510.Xr usba_hcdi_pipe_open 9E
511entry point, it already has to look at the pipe handle it's been given
512to determine the attributes of the endpoint it's looking at. However,
513before it does that it needs to look at the USB address of the device
514the handle corresponds to. If the device address matches the macro
515.Sy ROOT_HUB_ADDR ,
516then this is a time where the USBA is opening one of the root hub's
517endpoints.
518.Pp
519Because the root hub is generally not a real device, the driver will
520likely need to handle this in a different manner from traditional pipes.
521.Pp
522The device driver will want to check for the presence of the device's
523address with the following major entry points and change its behavior as
524described:
525.Bl -tag -width Fn
526.It Fn usba_hcdi_pipe_ctrl_xfer
527The device driver needs to intercept control transfers to the root hub
528and translate them into the appropriate form for the device. For
529example, the device driver may be asked to get a port's status. It
530should determine the appropriate way to perform this, such as reading a
531PCI memory-mapped register, and then create the appropriate response.
532.Pp
533The device driver needs to implement all of the major hub specific
534request types. It is recommended that driver writers see what existing
535host controller drivers implement and what the hub driver currently
536requires to implement this.
537.Pp
538Aside from the fact that the request is not being issued to a specific
539USB device, a request to the root hub follows the normal rules for a
540transfer and the device driver will need to call
541.Xr usba_hcdi_cb 9F
542to indicate that it has finished.
543.It Fn usba_hcdi_pipe_bulk_xfer
544The root hub does not support bulk transfers. If for some reason one is
545requested on the root hub, the driver should return
546.Sy USB_NOT_SUPPORTED .
547.It Fn usba_hcdi_pipe_intr_xfer
548The root hub only supports periodic interrupt-IN transfers. If an
549interrupt-OUT transfer or an interrupt-IN transfer with the
550.Sy USB_ATTRS_ONE_XFER
551attribute is set, then the driver should return
552.Sy USB_NOT_SUPPORTED .
553.Pp
554Otherwise, this represents a request to begin polling on the status
555endpoint for a hub. This is a periodic request, see the section
556.Sx Device Addressing
557Every USB device has an address assigned to it. The addresses assigned
558to each controller are independent. The root hub of a given controller
559always has an address of
560.Dv ROOT_HUB_ADDR .
561.Pp
562In general, addresses are assigned by the USBA and stored in the
563.Sy usb_addr
564member of a
565.Xr usba_device_t 9S .
566However, some controllers, such as xHCI, require that they control the
567device addressing themselves to facilitate their functionality. In such
568a case, the USBA still assigns every device an address; however, the
569actual address on the bus will be different and assigned by the HCD
570driver.  An HCD driver that needs to address devices itself must
571implement the
572.Xr usba_hcdi_device_address 9E
573entry point.
574.Sx Endpoint Polling
575more on the semantics of polling and periodic requests.
576.Pp
577Here, the device driver will need to provide data and perform a callback
578whenever the state of one of the ports changes on its virtual hub.
579Different drivers have different ways to perform this. For example, some
580hardware will provide an interrupt to indicate that a change has
581occurred. Other hardware does not, so this must be simulated.
582.Pp
583The way that the status data responses must be laid out is based in the
584USB specification. Generally, there is one bit per port and the driver
585sets the bit for the corresponding port that has had a change.
586.It Fn usba_hcdi_pipe_isoc_xfer
587The root hub does not support isochronous transfers. If for some reason
588one is requested on the root hub, the driver should return
589.It Fn usba_hcdi_pipe_close
590When a pipe to the root hub is closed, the device driver should tear
591down whatever it created as part of opening the pipe. In addition, if
592the pipe was an interrupt-IN pipe, if it has not already had polling
593stop, it should stop the polling as part of closing the pipe.
594.It Fn usba_hcdi_pipe_stop_intr_polling
595When a request to stop interrupt polling comes in and it is directed
596towards the root hub, the device driver should cease delivering
597callbacks upon changes in port status being detected. However, it should
598continue keeping track of what changes have occurred for the next time
599that polling starts.
600.Pp
601The primary request that was used to start polling should be returned,
602as with any other request to stop interrupt polling.
603.It Fn usba_hcdi_pipe_stop_isoc_polling
604The root hub does not support isochronous transfers. If for some reason
605it calls asking to stop polling on an isochronous transfer, the device
606driver should log an error and return
607.Sy USB_NOT_SUPPORTED .
608.El
609.Ss Endpoint Polling
610Both interrupt-IN and isochronous-IN endpoints are generally periodic or
611polled endpoints. interrupt-IN polling is indicated by the lack of the
612.Sy USB_ATTRS_ONE_XFER
613flag being set. All isochronous-IN transfer requests are requests for
614polling.
615.Pp
616Polling operates in a different fashion from traditional transfers. With
617a traditional transfer, a single request is made and a single callback
618is made for it, no more and no less. With a polling request, things are
619different. A single transfer request comes in; however, the driver needs
620to keep ensuring that transfers are being made within the polling bounds
621until a request to stop polling comes in or a fatal error is
622encountered.
623.Pp
624In many cases, as part of initializing the request, the driver will
625prepare several transfers such that there is always an active transfer,
626even if there is some additional latency in the system. This ensures
627that even if there is a momentary delay in the device driver processing
628a given transfer, I/O data will not be lost.
629.Pp
630The driver must not use the original request structure until it is ready
631to return due to a request to stop polling or an error. To obtain new
632interrupt and isochronous request structures, the driver should use the
633.Xr usba_hcdi_dup_intr_req 9F
634and
635.Xr usba_hcdi_dup_isoc_req 9F
636functions. These functions also allocate the resulting message blocks
637that data should be copied into. Note, it is possible that memory will
638not be available to duplicate such a request. In this case, the driver
639should use the original request to return an error and stop polling.
640.Ss Request Memory and DMA
641Each of the four transfer operations,
642.Xr usba_hcdi_pipe_ctrl_xfer 9E ,
643.Xr usba_hcdi_pipe_bulk_xfer 9E ,
644.Xr usba_hcdi_pipe_intr_xfer 9E ,
645and
646.Xr usba_hcdi_pipe_isoc_xfer 9E
647give data to hcdi drivers in the form of
648.Xr mblk 9S
649structures. To perform the individual transfers, most systems devices
650will leverage DMA. Drivers should allocate memory suitable for DMA for
651each transfer that they need to perform and copy the data to and from
652the message blocks.
653.Pp
654Device drivers should not use
655.Xr desballoc 9F
656to try and bind the memory used for DMA transfers to a message block nor
657should they bind the message block's read pointer to a DMA handle using
658.Xr ddi_dma_addr_bind_handle 9F .
659.Pp
660While this isn't a strict rule, the general framework does not assume
661that there are going to be outstanding message blocks that may be in use
662by the controller or belong to the controller outside of the boundaries
663of a given call to one of the transfer functions and its corresponding
664callback.
665.Ss Endpoint Timeouts
666The host controller is in charge of watching I/Os for timeouts. For any
667request that's not periodic (an interrupt-IN or isochronous-IN)
668transfer, the host controller must set up a timeout handler. If that
669timeout expires, it needs to stop the endpoint, remove that request, and
670return to the caller.
671.Pp
672The timeouts are specified in seconds in the request structures. For
673bulk timeouts, the request is in the
674.Sy bulk_timeout
675member of the
676.Xr usb_bulk_req 9S
677structure. The interrupt and control transfers also have a similar
678member in their request structures, see
679.Xr usb_intr_req 9S
680and
681.Xr usb_ctrl_req 9S .
682If any of the times is set to zero, the default USBA timeout should be
683used. In that case, drivers should set the value to the macro
684.Sy HCDI_DEFAULT_TIMEOUT ,
685which is a time in seconds.
686.Pp
687Isochronous-OUT transfers do not have a timeout defined on their request
688structure, the
689.Xr usb_isoc_req 9S .
690Due to the periodic nature of even outbound requests, it is less likely
691that a timeout will occur; however, driver writers are encouraged to
692still set up the default timeout,
693.Sy HCDI_DEFAULT_TIMEOUT ,
694on those transfers.
695.Pp
696The exact means of performing the timeout is best left to the driver
697writer as the way that hardware exposes scheduling of different
698endpoints will vary. One strategy to consider is to use the
699.Xr timeout 9F
700function at a one second period while I/O is ongoing on a per-endpoint
701basis. Because the time is measured in seconds, a driver writer can
702decrement a counter for a given outstanding transfer once a second and
703then if it reaches zero, interject and stop the endpoint and clean up.
704.Pp
705This has the added benefit that when no I/O is scheduled, then there
706will be no timer activity, reducing overall system load.
707.Ss Notable Types and Structures
708The following are data structures and types that are used throughout
709host controller drivers:
710.Bl -tag -width Vt
711.It Sy usb_cfg_descr
712The configuration descriptor. A device may have one or more
713configurations that it supports that can be switched between. The
714descriptor is documented in
715.Xr usb_cfg_descr 9S .
716.It Sy usb_dev_descr
717The device descriptor. A device descriptor contains basic properties of
718the device such as the USB version, device and vendor information, and
719the maximum packet size. This will often be used when setting up a
720device for the first time. It is documented in
721.Xr usb_dev_descr 9S .
722.It Sy usb_ep_descr
723The endpoint descriptor. An endpoint descriptor contains the basic
724properties of an endpoints such as its type and packet size. Every
725endpoint on a given USB device has an endpoint descriptor. It is
726documented in
727.Xr usb_ep_descr 9S .
728.It Sy usb_xep_descr
729The extended endpoint descriptor. This structure is used to contain the
730endpoint descriptor, but also additional endpoint companion descriptors
731which are a part of newer USB standards. It is documented in
732.Xr usb_ep_xdescr 9S .
733.It Sy usb_bulk_req
734This structure is filled out by client device drivers that want to make
735a bulk transfer request. Host controllers use this and act on it to
736perform bulk transfers to USB devices. The structure is documented in
737.Xr usb_bulk_req 9S .
738.It Sy usb_ctrl_req
739This structure is filled out by client device drivers that want to make
740a control transfer request. Host controllers use this and act on it to
741perform bulk transfers to USB devices. The structure is documented in
742.Xr usb_ctrl_req 9S .
743.It Sy usb_intr_req
744This structure is filled out by client device drivers that want to make
745an interrupt transfer request. Host controllers use this and act on it to
746perform bulk transfers to USB devices. The structure is documented in
747.Xr usb_intr_req 9S .
748.It Sy usb_isoc_req
749This structure is filled out by client device drivers that want to make
750an isochronous transfer request. Host controllers use this and act on it to
751perform bulk transfers to USB devices. The structure is documented in
752.Xr usb_isoc_req 9S .
753.It Vt usb_flags_t
754These define a set of flags that are used on certain entry points. These
755generally determine whether or not the entry points should block for
756memory allocation. Individual manual pages indicate the flags that
757drivers should consult.
758.It Vt usb_port_status_t
759The
760.Sy usb_port_status_t
761determines the current negotiated speed of the device. The following are
762valid values that this may be:
763.Bl -tag -width Sy
764.It Sy USBA_LOW_SPEED_DEV
765The device is running as a low speed device. This may be a USB 1.x or
766USB 2.0 device.
767.It Sy USBA_FULL_SPEED_DEV
768The device is running as a full speed device. This may be a USB 1.x or
769USB 2.0 device.
770.It Sy USBA_HIGH_SPEED_DEV
771The device is running as a high speed device. This is a USB 2.x device.
772.It Sy USBA_SUPER_SPEED_DEV
773The device is running as a super speed device. This is a USB 3.0 device.
774.It Vt usb_cr_t
775This is a set of codes that may be returned as a part of the call to
776.Xr usb_hcdi_cb 9F .
777The best place for the full set of these is currently in the source
778control headers.
779.El
780.El
781.Ss Interrupts
782While some hardware supports more than one interrupt queue, a single
783interrupt is generally sufficient for most host controllers. If the
784controller supports interrupt coalescing, then the driver should
785generally enable it and set it to a moderate rate.
786.Ss driver.conf considerations
787Due to the way host controller drivers need to interact with hotplug,
788drivers should generally set the
789.Sy ddi-forceattach
790property to one in their
791.Xr driver.conf 4
792file.
793.Sh SEE ALSO
794.Xr driver.conf 4 ,
795.Xr hubd 7D ,
796.Xr usba 7D ,
797.Xr attach 9E ,
798.Xr close 9E ,
799.Xr detach 9E ,
800.Xr getinfo 9E ,
801.Xr ioctl 9E ,
802.Xr open 9E ,
803.Xr usba_hcdi_bulk_xfer 9E ,
804.Xr usba_hcdi_cb_close 9E ,
805.Xr usba_hcdi_cb_ioctl 9E ,
806.Xr usba_hcdi_cb_open 9E ,
807.Xr usba_hcdi_pipe_bulk_xfer 9E ,
808.Xr usba_hcdi_pipe_ctrl_xfer 9E ,
809.Xr usba_hcdi_pipe_intr_xfer 9E ,
810.Xr usba_hcdi_pipe_isoc_xfer 9E ,
811.Xr usba_hcdi_pipe_open 9E ,
812.Xr ddi_dma_addr_bind_handle 9F ,
813.Xr ddi_get_driver_private 9F ,
814.Xr ddi_prop_op 9F ,
815.Xr desballoc 9F ,
816.Xr nochpoll 9F ,
817.Xr nodev 9F ,
818.Xr timeout 9F ,
819.Xr usb_hcdi_bind_root_hub 9F ,
820.Xr usb_hubdi_bind_root_hub 9F ,
821.Xr usba_alloc_hcdi_ops 9F ,
822.Xr usba_hcdi_cb 9F ,
823.Xr usba_hcdi_dup_intr_req 9F ,
824.Xr usba_hcdi_dup_isoc_req 9F ,
825.Xr usba_hcdi_register 9F ,
826.Xr usba_hcdi_unregister 9F ,
827.Xr usba_hubdi_close 9F ,
828.Xr usba_hubdi_devops 9F ,
829.Xr usba_hubdi_Devops 9F ,
830.Xr usba_hubdi_ioctl 9F ,
831.Xr usba_hubdi_open 9F ,
832.Xr usba_hubdi_unbind_root_hub 9F ,
833.Xr usb_hcdi_bind_root_hub 9F. ,
834.Xr cb_ops 9S ,
835.Xr dev_ops 9S ,
836.Xr mblk 9S ,
837.Xr usb_bulk_req 9S ,
838.Xr usb_cfg_descr 9S ,
839.Xr usb_ctrl_req 9S ,
840.Xr usb_dev_descr 9S ,
841.Xr usb_ep_descr 9S ,
842.Xr usb_ep_ss_comp_descr 9S ,
843.Xr usb_if_descr 9S ,
844.Xr usb_intr_req 9S ,
845.Xr usb_isoc_req 9S ,
846.Xr usba_hcdi_ops 9S
847