1 <!-- Copyright (C) 2002 Red Hat, Inc. -->
2 <!-- This material may be distributed only subject to the terms -->
3 <!-- and conditions set forth in the Open Publication License, v1.0 -->
4 <!-- or later (the latest version is presently available at -->
5 <!-- http://www.opencontent.org/openpub/). -->
6 <!-- Distribution of substantively modified versions of this -->
7 <!-- document is prohibited without the explicit permission of the -->
8 <!-- copyright holder. -->
9 <!-- Distribution of the work or derivative of the work in any -->
10 <!-- standard (paper) book form is prohibited unless prior -->
11 <!-- permission is obtained from the copyright holder. -->
15 >Writing a USB Device Driver</TITLE
16 ><meta name="MSSmartTagsPreventParsing" content="TRUE">
19 CONTENT="Modular DocBook HTML Stylesheet Version 1.64
22 TITLE="eCos USB Slave Support"
23 HREF="io-usb-slave.html"><LINK
25 TITLE="Data Endpoints"
26 HREF="usbs-data.html"><LINK
29 HREF="usbs-testing.html"></HEAD
48 >eCos USB Slave Support</TH
69 HREF="usbs-testing.html"
80 >Writing a USB Device Driver</A
89 >Writing a USB Device Driver -- USB Device Driver Porting Guide</DIV
98 >Often the best way to write a USB device driver will be to start with
99 an existing one and modify it as necessary. The information given here
100 is intended primarily as an outline rather than as a complete guide.</P
108 >At the time of writing only one USB device driver has been
109 implemented. Hence it is possible, perhaps probable, that some
110 portability issues have not yet been addressed. One issue
111 involves the different types of transfer, for example the initial
112 target hardware had no support for isochronous or interrupt transfers,
113 so additional functionality may be needed to switch between transfer
114 types. Another issue would be hardware where a given endpoint number,
115 say endpoint 1, could be used for either receiving or transmitting
116 data, but not both because a single fifo is used. Issues like these
117 will have to be resolved as and when additional USB device drivers are
128 >The Control Endpoint</H2
130 >A USB device driver should provide a single <A
131 HREF="usbs-control.html"
134 >usbs_control_endpoint</SPAN
137 data structure for every USB device. Typical peripherals will have
138 only one USB port so there will be just one such data structure in the
139 entire system, but theoretically it is possible to have multiple USB
140 devices. These may all involve the same chip, in which case a single
141 device driver should support multiple device instances, or they may
142 involve different chips. The name or names of these data structures
143 are determined by the device driver, but appropriate care should be
144 taken to avoid name clashes. </P
146 >A USB device cannot be used unless the control endpoint data structure
147 exists. However, the presence of USB hardware in the target processor
148 or board does not guarantee that the application will necessarily want
149 to use that hardware. To avoid unwanted code or data overheads, the
150 device driver can provide a configuration option to determine whether
151 or not the endpoint 0 data structure is actually provided. A default
154 >CYGINT_IO_USB_SLAVE_CLIENTS</TT
156 the USB driver will be enabled automatically if higher-level code does
157 require USB support, while leaving ultimate control to the user.</P
159 >The USB device driver is responsible for filling in the
177 > fields. Usually this can
178 be achieved by static initialization. The driver is also largely
179 responsible for maintaining the <TT
191 used to hold the first packet of a control message. The
197 > and other fields related to data
198 transfers will be managed <A
199 HREF="usbs-control.html#AEN578"
201 > by higher-level code and
202 the device driver. The remaining fields are generally filled in by
203 higher-level code, although the driver should initialize them to NULL
206 >Hardware permitting, the USB device should be inactive until the
212 > is invoked, for example by
213 tristating the appropriate pins. This prevents the host from
214 interacting with the peripheral before all other parts of the system
215 have initialized. It is expected that the
221 > will only be invoked once, shortly
224 >Where possible the device driver should detect state changes, such as
225 when the connection between host and peripheral is established, and
227 HREF="usbs-control.html#AEN515"
229 > these to higher-level
236 any. The state change to and from configured state cannot easily be
237 handled by the device driver itself, instead higher-level code such as
238 the common USB slave package will take care of this.</P
240 >Once the connection between host and peripheral has been established,
241 the peripheral must be ready to accept control messages at all times,
242 and must respond to these within certain time constraints. For
243 example, the standard set-address control message must be handled
244 within 50ms. The USB specification provides more information on these
245 constraints. The device driver is responsible for receiving the
246 initial packet of a control message. This packet will always be eight
247 bytes and should be stored in the
253 > field. Certain standard
254 control messages should be detected and handled by the device driver
255 itself. The most important is set-address, but usually the get-status,
256 set-feature and clear-feature requests when applied to halted
257 endpoints should also be handled by the driver. Other standard control
258 messages should first be passed on to the
262 >standard_control_fn</I
264 > callback (if any), and
265 finally to the default handler
268 >usbs_handle_standard_control</TT
270 common USB slave package. Class, vendor and reserved control messages
271 should always be dispatched to the appropriate callback and there is
272 no default handler for these.</P
274 >Some control messages will involve further data transfer, not just the
275 initial packet. The device driver must handle this in accordance with
276 the USB specification and the <A
277 HREF="usbs-control.html#AEN578"
278 >buffer management strategy</A
280 driver is also responsible for keeping track of whether or not the
281 control operation has succeeded and generating an ACK or STALL
284 >The polling support is optional and may not be feasible on all
285 hardware. It is only used in certain specialised environments such as
286 RedBoot. A typical implementation of the polling function would just
287 check whether or not an interrupt would have occurred and, if so, call
288 the same code that the interrupt handler would.</P
298 >In addition to the control endpoint data structure, a USB device
299 driver should also provide appropriate <A
300 HREF="usbs-data.html"
303 > data structures. Obviously this is only relevant if
304 the USB support generally is desired, that is if the control endpoint is
305 provided. In addition, higher-level code may not require all the
306 endpoints, so it may be useful to provide configuration options that
307 control the presence of each endpoint. For example, the intended
308 application might only involve a single transmit endpoint and of
309 course control messages, so supporting receive endpoints might waste
312 >Conceptually, data endpoints are much simpler than the control
313 endpoint. The device driver has to supply two functions, one for
314 data transfers and another to control the halted condition. These
315 implement the functionality for
317 HREF="usbs-start-rx.html"
320 >usbs_start_rx_buffer</TT
324 HREF="usbs-start-tx.html"
327 >usbs_start_tx_buffer</TT
331 HREF="usbs-halt.html"
334 >usbs_set_rx_endpoint_halted</TT
338 HREF="usbs-halt.html"
341 >usbs_set_tx_endpoint_halted</TT
344 The device driver is also responsible for maintaining the
352 >For data transfers, higher-level code will have filled in the
376 > fields. The transfer function
377 should arrange for the transfer to start, allowing the host to send or
378 receive packets. Typically this will result in an interrupt at the end
379 of the transfer or after each packet. Once the entire transfer has
380 been completed, the driver's interrupt handling code should invoke the
381 completion function. This can happen either in DSR context or thread
382 context, depending on the driver's implementation. There are a number
383 of special cases to consider. If the endpoint is halted when the
384 transfer is started then the completion function can be invoked
388 >. If the transfer cannot be
389 completed because the connection is broken then the completion
390 function should be invoked with <TT
394 endpoint is stalled during the transfer, either because of a standard
395 control message or because higher-level code calls the appropriate
401 >, then again the completion
402 function should be invoked with <TT
408 >usbs_start_rx_endpoint_wait</TT
412 >usbs_start_tx_endpoint_wait</TT
414 calling the device driver's data transfer function with a buffer size
423 >Giving a buffer size of 0 bytes a special meaning is problematical
424 because it prevents transfers of that size. Such transfers are allowed
425 by the USB protocol, consisting of just headers and acknowledgements
426 and an empty data phase, although rarely useful. A future modification
427 of the device driver specification will address this issue, although
428 care has to be taken that the functionality remains accessible through
429 devtab entries as well as via low-level accesses.</P
441 >For some applications or higher-level packages it may be more
442 convenient to use traditional open/read/write I/O calls rather than
443 the non-blocking USB I/O calls. To support this the device driver can
444 provide a devtab entry for each endpoint, for example:</P
452 CLASS="PROGRAMLISTING"
453 >#ifdef CYGVAR_DEVS_USB_SA11X0_EP1_DEVTAB_ENTRY
455 static CHAR_DEVIO_TABLE(usbs_sa11x0_ep1_devtab_functions,
456 &cyg_devio_cwrite,
457 &usbs_devtab_cread,
458 &cyg_devio_bwrite,
459 &cyg_devio_bread,
460 &cyg_devio_select,
461 &cyg_devio_get_config,
462 &cyg_devio_set_config);
464 static CHAR_DEVTAB_ENTRY(usbs_sa11x0_ep1_devtab_entry,
465 CYGDAT_DEVS_USB_SA11X0_DEVTAB_BASENAME "1r",
467 &usbs_sa11x0_ep1_devtab_functions,
468 &usbs_sa11x0_devtab_dummy_init,
470 (void*) &usbs_sa11x0_ep1);
476 >Again care must be taken to avoid name clashes. This can be achieved
477 by having a configuration option to control the base name, with a
478 default value of e.g. <TT
482 endpoint-specific string. This gives the application developer
483 sufficient control to eliminate any name clashes. The common USB slave
484 package provides functions <TT
486 >usbs_devtab_cwrite</TT
490 >usbs_devtab_cread</TT
491 >, which can be used in the
492 function tables for transmit and receive endpoints respectively. The
498 > of the devtab entry
499 should be a pointer to the underlying endpoint data structure.</P
501 >Because devtab entries are never accessed directly, only indirectly,
502 they would usually be eliminated by the linker. To avoid this the
503 devtab entries should normally be defined in a separate source file
504 which ends up the special library <TT
508 rather than in the default library <TT
513 >Not all applications or higher-level packages will want to use the
514 devtab entries and the blocking I/O facilities. It may be appropriate
515 for the device driver to provide additional configuration options that
516 control whether or not any or all of the devtab entries should be
517 provided, to avoid unnecessary memory overheads.</P
525 >Interrupt Handling</H2
527 >A typical USB device driver will need to service interrupts for all of
528 the endpoints and possibly for additional USB events such as entering
529 or leaving suspended mode. Usually these interrupts need not be
530 serviced directly by the ISR. Instead, they can be left to a DSR. If
531 the peripheral is not able to accept or send another packet just yet,
532 the hardware will generate a NAK and the host will just retry a little
533 bit later. If high throughput is required then it may be desirable to
534 handle the bulk transfer protocol largely at ISR level, that is take
535 care of each packet in the ISR and only activate the DSR once the
536 whole transfer has completed.</P
538 >Control messages may involve invoking arbitrary callback functions in
539 higher-level code. This should normally happen at DSR level. Doing it
540 at ISR level could seriously affect the system's interrupt latency and
541 impose unacceptable constraints on what operations can be performed by
542 those callbacks. If the device driver requires a thread anyway then it
543 may be appropriate to use this thread for invoking the callbacks, but
544 usually it is not worthwhile to add a new thread to the system just
545 for this; higher-level code is expected to write callbacks that
546 function sensibly at DSR level. Much the same applies to the
547 completion functions associated with data transfers. These should also
548 be invoked at DSR or thread level.</P
556 >Support for USB Testing</H2
558 >Optionally a USB device driver can provide support for the
560 HREF="usbs-testing.html"
561 >USB test software</A
563 defining a number of additional data structures, allowing the
564 generic test code to work out just what the hardware is capable of and
565 hence what testing can be performed.</P
567 >The key data structure is
570 >usbs_testing_endpoint</SPAN
573 >cyg/io/usb/usbs.h</TT
575 commonly required constants are provided by the common USB package in
578 >cyg/io/usb/usb.h</TT
582 >usbs_testing_endpoint</SPAN
583 > structure should be
584 defined for each supported endpoint. The following fields need to be
600 > This specifies the type of endpoint and should be one of
603 >USB_ENDPOINT_DESCRIPTOR_ATTR_CONTROL</TT
627 > This identifies the number that should be used by the host
628 to address this endpoint. For a control endpoint it should
629 be 0. For other types of endpoints it should be between
637 >endpoint_direction</I
642 > For control endpoints this field is irrelevant. For other
643 types of endpoint it should be either
646 >USB_ENDPOINT_DESCRIPTOR_ENDPOINT_IN</TT
650 >USB_ENDPOINT_DESCRIPTOR_ENDPOINT_OUT</TT
652 endpoint number can be used for traffic in both directions then
653 there should be two entries in the array, one for each direction.
665 > This should be a pointer to the appropriate
668 >usbs_control_endpoint</SPAN
672 >usbs_rx_endpoint</SPAN
676 >usbs_tx_endpoint</SPAN
677 > structure, allowing the
678 generic testing code to perform low-level I/O.
690 > If the endpoint also has an entry in the system's device table then
691 this field should give the corresponding string, for example
694 >"/dev/usbs1r"</TT
696 generic testing code to access the device via higher-level
715 > This indicates the smallest transfer size that the hardware can
716 support on this endpoint. Typically this will be one.
725 > Strictly speaking a minimum size of one is not quite right since it
726 is valid for a USB transfer to involve zero bytes, in other words a
727 transfer that involves just headers and acknowledgements and an
728 empty data phase, and that should be tested as well. However current
729 device drivers interpret a transfer size of 0 as special, so that
730 would have to be resolved first.
744 > Similarly, this specifies the largest transfer size. For control
745 endpoints the USB protocol uses only two bytes to hold the transfer
746 length, so there is an upper bound of 65535 bytes. In practice
747 it is very unlikely that any control transfers would ever need to
748 be this large, and in fact such transfers would take a long time
749 and probably violate timing constraints. For other types of endpoint
750 any of the protocol, the hardware, or the device driver may impose
751 size limits. For example a given device driver might be unable to
752 cope with transfers larger than 65535 bytes. If it should be
753 possible to transfer arbitrary amounts of data then a value of
757 > indicates no upper limit, and transfer
758 sizes will be limited by available memory and by the capabilities
771 > This field is needed on some hardware where it is impossible to
772 send packets of a certain size. For example the hardware may be
773 incapable of sending an empty bulk packet to terminate a transfer
774 that is an exact multiple of the 64-byte bulk packet size.
775 Instead the driver has to do some padding and send an extra byte,
776 and the host has to be prepared to receive this extra byte. Such a
777 driver should specify a value of <TT
781 padding field. For most drivers this field should be set to
788 > A better solution would be for the device driver to supply a
789 fragment of Tcl code that would adjust the receive buffer size
790 only when necessary, rather than for every transfer. Forcing
791 receive padding on all transfers when only certain transfers
792 will actually be padded reduces the accuracy of certain tests.
804 > On some hardware data transfers may need to be aligned to certain
805 boundaries, for example a word boundary or a cacheline boundary.
806 Although in theory device drivers could hide such alignment
807 restrictions from higher-level code by having their own buffers and
808 performing appropriate copying, that would be expensive in terms of
809 both memory and cpu cycles. Instead the generic testing code will
810 align any buffers passed to the device driver to the specified
811 boundary. For example, if the driver requires that buffers be
812 aligned to a word boundary then it should specify an alignment
819 >The device driver should provide an array of these structures
822 >usbs_testing_endpoints[]</TT
823 >. The USB testing code
824 examines this array and uses the information to perform appropriate
825 tests. Because different USB devices support different numbers of
826 endpoints the number of entries in the array is not known in advance,
827 so instead the testing code looks for a special terminator
830 >USBS_TESTING_ENDPOINTS_TERMINATOR</TT
832 array, showing just the control endpoint and the terminator, might
841 CLASS="PROGRAMLISTING"
842 >usbs_testing_endpoint usbs_testing_endpoints[] = {
844 endpoint_type : USB_ENDPOINT_DESCRIPTOR_ATTR_CONTROL,
846 endpoint_direction : USB_ENDPOINT_DESCRIPTOR_ENDPOINT_IN,
847 endpoint : (void*) &ep0.common,
848 devtab_entry : (const char*) 0,
855 USBS_TESTING_ENDPOINTS_TERMINATOR
867 >The use of a single array <TT
869 >usbs_testing_endpoints</TT
871 limits USB testing to platforms with a single USB device: if there
872 were multiple devices, each defining their own instance of this array,
873 then there would a collision at link time. In practice this should not
874 be a major problem since typical USB peripherals only interact with a
875 single host machine via a single slave port. In addition, even if a
876 peripheral did have multiple slave ports the current USB testing code
877 would not support this since it would not know which port to use.</P
896 HREF="usbs-data.html"
904 HREF="io-usb-slave.html"
912 HREF="usbs-testing.html"