3 * Gerry Hamel, geh@ti.com, Texas Instruments
5 * Based on linux/drivers/usbd/usbd.h
7 * Copyright (c) 2000, 2001, 2002 Lineo
8 * Copyright (c) 2001 Hewlett Packard
11 * Stuart Lynne <sl@lineo.com>,
12 * Tom Rushworth <tbr@lineo.com>,
13 * Bruce Balden <balden@lineo.com>
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
31 #ifndef __USBDCORE_H__
32 #define __USBDCORE_H__
35 #include "usbdescriptors.h"
36 #include <usb_dfu_descriptors.h>
39 #define MAX_URBS_QUEUED 5
43 #define usberr(fmt,args...) serial_printf("ERROR: %s(), %d: "fmt"\n",__FUNCTION__,__LINE__,##args)
45 #define usberr(fmt,args...) do{}while(0)
49 #define usbdbg(fmt,args...) serial_printf("debug: %s(), %d: "fmt"\n",__FUNCTION__,__LINE__,##args)
51 #define usbdbg(fmt,args...) do{}while(0)
55 #define usbinfo(fmt,args...) serial_printf("info: %s(), %d: "fmt"\n",__FUNCTION__,__LINE__,##args)
57 #define usbinfo(fmt,args...) do{}while(0)
61 #define le16_to_cpu(x) (x)
65 #define inb(p) (*(volatile u8*)(p))
69 #define outb(val,p) (*(volatile u8*)(p) = (val))
73 #define inw(p) (*(volatile u16*)(p))
77 #define outw(val,p) (*(volatile u16*)(p) = (val))
81 #define inl(p) (*(volatile u32*)(p))
85 #define outl(val,p) (*(volatile u32*)(p) = (val))
89 #define insw(p,to,len) mmio_insw(p,to,len)
93 #define outsw(p,from,len) mmio_outsw(p,from,len)
97 #define insb(p,to,len) mmio_insb(p,to,len)
101 #define mmio_insw(r,b,l) ({ int __i ; \
104 for (__i = 0; __i < l; __i++) { \
105 *(__b2 + __i) = inw(r); \
111 #define mmio_outsw(r,b,l) ({ int __i; \
114 for (__i = 0; __i < l; __i++) { \
115 outw( *(__b2 + __i), r); \
121 #define mmio_insb(r,b,l) ({ int __i ; \
124 for (__i = 0; __i < l; __i++) { \
125 *(__b2 + __i) = inb(r); \
131 #define MIN(a,b) ((a) < (b) ? (a) : (b))
134 #define MAX(a,b) ((a) > (b) ? (a) : (b))
139 * Structure member address manipulation macros.
140 * These are used by client code (code using the urb_link routines), since
141 * the urb_link structure is embedded in the client data structures.
143 * Note: a macro offsetof equivalent to member_offset is defined in stddef.h
144 * but this is kept here for the sake of portability.
146 * p2surround returns a pointer to the surrounding structure given
147 * type of the surrounding structure, the name memb of the structure
148 * member pointed at by ptr. For example, if you have:
156 * char *cp = &thingy.z;
160 * &thingy == p2surround(struct foo, z, cp)
164 #define _cv_(ptr) ((char*)(void*)(ptr))
165 #define member_offset(type,memb) (_cv_(&(((type*)0)->memb))-(char*)0)
166 #define p2surround(type,memb,ptr) ((type*)(void*)(_cv_(ptr)-member_offset(type,memb)))
170 struct usb_endpoint_instance
;
171 struct usb_interface_instance
;
172 struct usb_configuration_instance
;
173 struct usb_device_instance
;
174 struct usb_bus_instance
;
177 * Device and/or Interface Class codes
179 #define USB_CLASS_PER_INTERFACE 0 /* for DeviceClass */
180 #define USB_CLASS_AUDIO 1
181 #define USB_CLASS_COMM 2
182 #define USB_CLASS_HID 3
183 #define USB_CLASS_PHYSICAL 5
184 #define USB_CLASS_PRINTER 7
185 #define USB_CLASS_MASS_STORAGE 8
186 #define USB_CLASS_HUB 9
187 #define USB_CLASS_DATA 10
188 #define USB_CLASS_APP_SPEC 0xfe
189 #define USB_CLASS_VENDOR_SPEC 0xff
194 #define USB_TYPE_STANDARD (0x00 << 5)
195 #define USB_TYPE_CLASS (0x01 << 5)
196 #define USB_TYPE_VENDOR (0x02 << 5)
197 #define USB_TYPE_RESERVED (0x03 << 5)
202 #define USB_RECIP_DEVICE 0x00
203 #define USB_RECIP_INTERFACE 0x01
204 #define USB_RECIP_ENDPOINT 0x02
205 #define USB_RECIP_OTHER 0x03
210 #define USB_DIR_OUT 0
211 #define USB_DIR_IN 0x80
216 #define USB_DT_DEVICE 0x01
217 #define USB_DT_CONFIG 0x02
218 #define USB_DT_STRING 0x03
219 #define USB_DT_INTERFACE 0x04
220 #define USB_DT_ENDPOINT 0x05
222 #define USB_DT_HID (USB_TYPE_CLASS | 0x01)
223 #define USB_DT_REPORT (USB_TYPE_CLASS | 0x02)
224 #define USB_DT_PHYSICAL (USB_TYPE_CLASS | 0x03)
225 #define USB_DT_HUB (USB_TYPE_CLASS | 0x09)
228 * Descriptor sizes per descriptor type
230 #define USB_DT_DEVICE_SIZE 18
231 #define USB_DT_CONFIG_SIZE 9
232 #define USB_DT_INTERFACE_SIZE 9
233 #define USB_DT_ENDPOINT_SIZE 7
234 #define USB_DT_ENDPOINT_AUDIO_SIZE 9 /* Audio extension */
235 #define USB_DT_HUB_NONVAR_SIZE 7
236 #define USB_DT_HID_SIZE 9
241 #define USB_ENDPOINT_NUMBER_MASK 0x0f /* in bEndpointAddress */
242 #define USB_ENDPOINT_DIR_MASK 0x80
244 #define USB_ENDPOINT_XFERTYPE_MASK 0x03 /* in bmAttributes */
245 #define USB_ENDPOINT_XFER_CONTROL 0
246 #define USB_ENDPOINT_XFER_ISOC 1
247 #define USB_ENDPOINT_XFER_BULK 2
248 #define USB_ENDPOINT_XFER_INT 3
251 * USB Packet IDs (PIDs)
253 #define USB_PID_UNDEF_0 0xf0
254 #define USB_PID_OUT 0xe1
255 #define USB_PID_ACK 0xd2
256 #define USB_PID_DATA0 0xc3
257 #define USB_PID_PING 0xb4 /* USB 2.0 */
258 #define USB_PID_SOF 0xa5
259 #define USB_PID_NYET 0x96 /* USB 2.0 */
260 #define USB_PID_DATA2 0x87 /* USB 2.0 */
261 #define USB_PID_SPLIT 0x78 /* USB 2.0 */
262 #define USB_PID_IN 0x69
263 #define USB_PID_NAK 0x5a
264 #define USB_PID_DATA1 0x4b
265 #define USB_PID_PREAMBLE 0x3c /* Token mode */
266 #define USB_PID_ERR 0x3c /* USB 2.0: handshake mode */
267 #define USB_PID_SETUP 0x2d
268 #define USB_PID_STALL 0x1e
269 #define USB_PID_MDATA 0x0f /* USB 2.0 */
274 #define USB_REQ_GET_STATUS 0x00
275 #define USB_REQ_CLEAR_FEATURE 0x01
276 #define USB_REQ_SET_FEATURE 0x03
277 #define USB_REQ_SET_ADDRESS 0x05
278 #define USB_REQ_GET_DESCRIPTOR 0x06
279 #define USB_REQ_SET_DESCRIPTOR 0x07
280 #define USB_REQ_GET_CONFIGURATION 0x08
281 #define USB_REQ_SET_CONFIGURATION 0x09
282 #define USB_REQ_GET_INTERFACE 0x0A
283 #define USB_REQ_SET_INTERFACE 0x0B
284 #define USB_REQ_SYNCH_FRAME 0x0C
286 #define USBD_DEVICE_REQUESTS(x) (((unsigned int)x <= USB_REQ_SYNCH_FRAME) ? usbd_device_requests[x] : "UNKNOWN")
291 #define USB_REQ_GET_REPORT 0x01
292 #define USB_REQ_GET_IDLE 0x02
293 #define USB_REQ_GET_PROTOCOL 0x03
294 #define USB_REQ_SET_REPORT 0x09
295 #define USB_REQ_SET_IDLE 0x0A
296 #define USB_REQ_SET_PROTOCOL 0x0B
300 * USB Spec Release number
303 #define USB_BCD_VERSION 0x0110
307 * Device Requests (c.f Table 9-2)
310 #define USB_REQ_DIRECTION_MASK 0x80
311 #define USB_REQ_TYPE_MASK 0x60
312 #define USB_REQ_RECIPIENT_MASK 0x1f
314 #define USB_REQ_DEVICE2HOST 0x80
315 #define USB_REQ_HOST2DEVICE 0x00
317 #define USB_REQ_TYPE_STANDARD 0x00
318 #define USB_REQ_TYPE_CLASS 0x20
319 #define USB_REQ_TYPE_VENDOR 0x40
321 #define USB_REQ_RECIPIENT_DEVICE 0x00
322 #define USB_REQ_RECIPIENT_INTERFACE 0x01
323 #define USB_REQ_RECIPIENT_ENDPOINT 0x02
324 #define USB_REQ_RECIPIENT_OTHER 0x03
330 #define USB_STATUS_SELFPOWERED 0x01
331 #define USB_STATUS_REMOTEWAKEUP 0x02
333 #define USB_STATUS_HALT 0x01
339 #define USB_DESCRIPTOR_TYPE_DEVICE 0x01
340 #define USB_DESCRIPTOR_TYPE_CONFIGURATION 0x02
341 #define USB_DESCRIPTOR_TYPE_STRING 0x03
342 #define USB_DESCRIPTOR_TYPE_INTERFACE 0x04
343 #define USB_DESCRIPTOR_TYPE_ENDPOINT 0x05
344 #define USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER 0x06
345 #define USB_DESCRIPTOR_TYPE_OTHER_SPEED_CONFIGURATION 0x07
346 #define USB_DESCRIPTOR_TYPE_INTERFACE_POWER 0x08
347 #define USB_DESCRIPTOR_TYPE_HID 0x21
348 #define USB_DESCRIPTOR_TYPE_REPORT 0x22
350 #define USBD_DEVICE_DESCRIPTORS(x) (((unsigned int)x <= USB_DESCRIPTOR_TYPE_INTERFACE_POWER) ? \
351 usbd_device_descriptors[x] : "UNKNOWN")
354 * standard feature selectors
356 #define USB_ENDPOINT_HALT 0x00
357 #define USB_DEVICE_REMOTE_WAKEUP 0x01
358 #define USB_TEST_MODE 0x02
365 struct usb_device_request
{
371 } __attribute__ ((packed
));
377 typedef enum urb_send_status
{
387 * Device State (c.f USB Spec 2.0 Figure 9-1)
389 * What state the usb device is in.
391 * Note the state does not change if the device is suspended, we simply set a
392 * flag to show that it is suspended.
395 typedef enum usb_device_state
{
396 STATE_INIT
, /* just initialized */
397 STATE_CREATED
, /* just created */
398 STATE_ATTACHED
, /* we are attached */
399 STATE_POWERED
, /* we have seen power indication (electrical bus signal) */
400 STATE_DEFAULT
, /* we been reset */
401 STATE_ADDRESSED
, /* we have been addressed (in default configuration) */
402 STATE_CONFIGURED
, /* we have seen a set configuration device command */
403 STATE_UNKNOWN
, /* destroyed */
404 } usb_device_state_t
;
406 #define USBD_DEVICE_STATE(x) (((unsigned int)x <= STATE_UNKNOWN) ? usbd_device_states[x] : "UNKNOWN")
413 typedef enum usb_device_status
{
414 USBD_OPENING
, /* we are currently opening */
415 USBD_OK
, /* ok to use */
416 USBD_SUSPENDED
, /* we are currently suspended */
417 USBD_CLOSING
, /* we are currently closing */
418 } usb_device_status_t
;
420 #define USBD_DEVICE_STATUS(x) (((unsigned int)x <= USBD_CLOSING) ? usbd_device_status[x] : "UNKNOWN")
425 * These are defined in the USB Spec (c.f USB Spec 2.0 Figure 9-1).
427 * There are additional events defined to handle some extra actions we need
431 typedef enum usb_device_event
{
433 DEVICE_UNKNOWN
, /* bi - unknown event */
434 DEVICE_INIT
, /* bi - initialize */
435 DEVICE_CREATE
, /* bi - */
436 DEVICE_HUB_CONFIGURED
, /* bi - bus has been plugged int */
437 DEVICE_RESET
, /* bi - hub has powered our port */
439 DEVICE_ADDRESS_ASSIGNED
, /* ep0 - set address setup received */
440 DEVICE_CONFIGURED
, /* ep0 - set configure setup received */
441 DEVICE_SET_INTERFACE
, /* ep0 - set interface setup received */
443 DEVICE_SET_FEATURE
, /* ep0 - set feature setup received */
444 DEVICE_CLEAR_FEATURE
, /* ep0 - clear feature setup received */
446 DEVICE_DE_CONFIGURED
, /* ep0 - set configure setup received for ?? */
448 DEVICE_BUS_INACTIVE
, /* bi - bus in inactive (no SOF packets) */
449 DEVICE_BUS_ACTIVITY
, /* bi - bus is active again */
451 DEVICE_POWER_INTERRUPTION
, /* bi - hub has depowered our port */
452 DEVICE_HUB_RESET
, /* bi - bus has been unplugged */
453 DEVICE_DESTROY
, /* bi - device instance should be destroyed */
455 DEVICE_HOTPLUG
, /* bi - a hotplug event has occured */
457 DEVICE_FUNCTION_PRIVATE
, /* function - private */
459 } usb_device_event_t
;
462 typedef struct urb_link
{
463 struct urb_link
*next
;
464 struct urb_link
*prev
;
467 /* USB Data structure - for passing data around.
469 * This is used for both sending and receiving data.
471 * The callback function is used to let the function driver know when
472 * transmitted data has been sent.
474 * The callback function is set by the alloc_recv function when an urb is
475 * allocated for receiving data for an endpoint and used to call the
476 * function driver to inform it that data has arrived.
479 #ifdef CONFIG_USBD_DFU
480 #define URB_BUF_SIZE (128+CONFIG_USBD_DFU_XFER_SIZE)
482 #define URB_BUF_SIZE 128 /* in linux we'd malloc this, but in u-boot we prefer static data */
486 struct usb_endpoint_instance
*endpoint
;
487 struct usb_device_instance
*device
;
489 struct usb_device_request device_request
; /* contents of received SETUP packet */
491 struct urb_link link
; /* embedded struct for circular doubly linked list of urbs */
494 unsigned int buffer_length
;
495 unsigned int actual_length
;
497 urb_send_status_t status
;
500 u16 buffer_data
[URB_BUF_SIZE
]; /* data received (OUT) or being sent (IN) */
503 /* Endpoint configuration
505 * Per endpoint configuration data. Used to track which function driver owns
509 struct usb_endpoint_instance
{
510 int endpoint_address
; /* logical endpoint address */
513 int status
; /* halted */
514 int state
; /* available for use by bus interface driver */
517 struct urb_link rcv
; /* received urbs */
518 struct urb_link rdy
; /* empty urbs ready to receive */
519 struct urb
*rcv_urb
; /* active urb */
520 int rcv_attributes
; /* copy of bmAttributes from endpoint descriptor */
521 int rcv_packetSize
; /* maximum packet size from endpoint descriptor */
522 int rcv_transferSize
; /* maximum transfer size from function driver */
526 struct urb_link tx
; /* urbs ready to transmit */
527 struct urb_link done
; /* transmitted urbs */
528 struct urb
*tx_urb
; /* active urb */
529 int tx_attributes
; /* copy of bmAttributes from endpoint descriptor */
530 int tx_packetSize
; /* maximum packet size from endpoint descriptor */
531 int tx_transferSize
; /* maximum transfer size from function driver */
534 int sent
; /* data already sent */
535 int last
; /* data sent in last packet XXX do we need this */
538 struct usb_alternate_instance
{
539 struct usb_interface_descriptor
*interface_descriptor
;
542 int *endpoint_transfersize_array
;
543 struct usb_endpoint_descriptor
**endpoints_descriptor_array
;
546 struct usb_interface_instance
{
548 struct usb_alternate_instance
*alternates_instance_array
;
551 struct usb_configuration_instance
{
553 struct usb_configuration_descriptor
*configuration_descriptor
;
554 struct usb_interface_instance
*interface_instance_array
;
558 /* USB Device Instance
560 * For each physical bus interface we create a logical device structure. This
561 * tracks all of the required state to track the USB HOST's view of the device.
563 * Keep track of the device configuration for a real physical bus interface,
564 * this includes the bus interface, multiple function drivers, the current
565 * configuration and the current state.
568 * the specific bus interface driver
569 * the default endpoint 0 driver
570 * the configured function driver
576 struct usb_device_instance
{
580 struct usb_device_descriptor
*device_descriptor
; /* per device descriptor */
582 void (*event
) (struct usb_device_instance
*device
, usb_device_event_t event
, int data
);
584 /* Do cdc device specific control requests */
585 int (*cdc_recv_setup
)(struct usb_device_request
*request
, struct urb
*urb
);
588 struct usb_bus_instance
*bus
; /* which bus interface driver */
590 /* configuration descriptors */
592 struct usb_configuration_instance
*configuration_instance_array
;
595 usb_device_state_t device_state
; /* current USB Device state */
596 usb_device_state_t device_previous_state
; /* current USB Device state */
598 u8 address
; /* current address (zero is default) */
599 u8 configuration
; /* current show configuration (zero is default) */
600 u8 interface
; /* current interface (zero is default) */
601 u8 alternate
; /* alternate flag */
603 usb_device_status_t status
; /* device status */
605 int urbs_queued
; /* number of submitted urbs */
607 /* Shouldn't need to make this atomic, all we need is a change indicator */
608 unsigned long usbd_rxtx_timestamp
;
609 unsigned long usbd_last_rxtx_timestamp
;
611 #ifdef CONFIG_USBD_DFU
612 const struct usb_device_descriptor
*dfu_dev_desc
;
613 const struct _dfu_desc
*dfu_cfg_desc
;
614 enum dfu_state dfu_state
;
619 /* Bus Interface configuration structure
621 * This is allocated for each configured instance of a bus interface driver.
623 * The privdata pointer may be used by the bus interface driver to store private
624 * per instance state information.
626 struct usb_bus_instance
{
628 struct usb_device_instance
*device
;
629 struct usb_endpoint_instance
*endpoint_array
; /* array of available configured endpoints */
631 int max_endpoints
; /* maximimum number of rx enpoints */
632 unsigned char maxpacketsize
;
634 unsigned int serial_number
;
635 char *serial_number_str
;
636 void *privdata
; /* private data for the bus interface */
640 extern char *usbd_device_events
[];
641 extern char *usbd_device_states
[];
642 extern char *usbd_device_status
[];
643 extern char *usbd_device_requests
[];
644 extern char *usbd_device_descriptors
[];
646 extern struct usb_string_descriptor
**usb_strings
;
648 void urb_link_init (urb_link
* ul
);
649 void urb_detach (struct urb
*urb
);
650 urb_link
*first_urb_link (urb_link
* hd
);
651 struct urb
*first_urb (urb_link
* hd
);
652 struct urb
*first_urb_detached (urb_link
* hd
);
653 void urb_append (urb_link
* hd
, struct urb
*urb
);
655 struct urb
*usbd_alloc_urb (struct usb_device_instance
*device
, struct usb_endpoint_instance
*endpoint
);
656 void usbd_dealloc_urb (struct urb
*urb
);
659 * usbd_device_event is used by bus interface drivers to tell the higher layers that
660 * certain events have taken place.
662 void usbd_device_event_irq (struct usb_device_instance
*conf
, usb_device_event_t
, int);
663 void usbd_device_event (struct usb_device_instance
*conf
, usb_device_event_t
, int);
667 * Various ways of finding descriptors based on the current device and any
668 * possible configuration / interface / endpoint for it.
670 struct usb_configuration_descriptor
*usbd_device_configuration_descriptor (struct usb_device_instance
*, int, int);
671 struct usb_function_instance
*usbd_device_function_instance (struct usb_device_instance
*, unsigned int);
672 struct usb_interface_instance
*usbd_device_interface_instance (struct usb_device_instance
*, int, int, int);
673 struct usb_alternate_instance
*usbd_device_alternate_instance (struct usb_device_instance
*, int, int, int, int);
674 struct usb_interface_descriptor
*usbd_device_interface_descriptor (struct usb_device_instance
*, int, int, int, int);
675 struct usb_endpoint_descriptor
*usbd_device_endpoint_descriptor_index (struct usb_device_instance
*, int, int, int, int, int);
676 struct usb_class_descriptor
*usbd_device_class_descriptor_index (struct usb_device_instance
*, int, int, int, int, int);
677 struct usb_class_report_descriptor
*usbd_device_class_report_descriptor_index( struct usb_device_instance
*, int , int , int , int , int );
678 struct usb_endpoint_descriptor
*usbd_device_endpoint_descriptor (struct usb_device_instance
*, int, int, int, int, int);
679 int usbd_device_endpoint_transfersize (struct usb_device_instance
*, int, int, int, int, int);
680 struct usb_string_descriptor
*usbd_get_string (u8
);
681 struct usb_device_descriptor
*usbd_device_device_descriptor (struct usb_device_instance
*, int);
683 int usbd_endpoint_halted (struct usb_device_instance
*device
, int endpoint
);
684 void usbd_rcv_complete(struct usb_endpoint_instance
*endpoint
, int len
, int urb_bad
);
685 void usbd_tx_complete (struct usb_endpoint_instance
*endpoint
);
688 UDC_CTRL_PULLUP_ENABLE
,
689 UDC_CTRL_500mA_ENABLE
,
692 void udc_ctrl(enum usbd_event event
, int param
);