1 // USB functions and data.
5 #include "util.h" // struct mutex_s
16 // Common information for usb controllers.
18 struct usb_pipe *defaultpipe;
19 struct mutex_s resetlock;
25 #define USB_TYPE_UHCI 1
26 #define USB_TYPE_OHCI 2
28 #define USB_MAXADDR 127
31 /****************************************************************
32 * usb structs and flags
33 ****************************************************************/
35 // USB mandated timings (in ms)
36 #define USB_TIME_SIGATT 100
37 #define USB_TIME_ATTDB 100
38 #define USB_TIME_DRST 10
39 #define USB_TIME_DRSTR 50
40 #define USB_TIME_RSTRCY 10
42 #define USB_TIME_SETADDR_RECOVERY 2
44 #define USB_PID_OUT 0xe1
45 #define USB_PID_IN 0x69
46 #define USB_PID_SETUP 0x2d
48 #define USB_DIR_OUT 0 /* to device */
49 #define USB_DIR_IN 0x80 /* to host */
51 #define USB_TYPE_MASK (0x03 << 5)
52 #define USB_TYPE_STANDARD (0x00 << 5)
53 #define USB_TYPE_CLASS (0x01 << 5)
54 #define USB_TYPE_VENDOR (0x02 << 5)
55 #define USB_TYPE_RESERVED (0x03 << 5)
57 #define USB_RECIP_MASK 0x1f
58 #define USB_RECIP_DEVICE 0x00
59 #define USB_RECIP_INTERFACE 0x01
60 #define USB_RECIP_ENDPOINT 0x02
61 #define USB_RECIP_OTHER 0x03
63 #define USB_REQ_GET_STATUS 0x00
64 #define USB_REQ_CLEAR_FEATURE 0x01
65 #define USB_REQ_SET_FEATURE 0x03
66 #define USB_REQ_SET_ADDRESS 0x05
67 #define USB_REQ_GET_DESCRIPTOR 0x06
68 #define USB_REQ_SET_DESCRIPTOR 0x07
69 #define USB_REQ_GET_CONFIGURATION 0x08
70 #define USB_REQ_SET_CONFIGURATION 0x09
71 #define USB_REQ_GET_INTERFACE 0x0A
72 #define USB_REQ_SET_INTERFACE 0x0B
73 #define USB_REQ_SYNCH_FRAME 0x0C
75 struct usb_ctrlrequest {
83 #define USB_DT_DEVICE 0x01
84 #define USB_DT_CONFIG 0x02
85 #define USB_DT_STRING 0x03
86 #define USB_DT_INTERFACE 0x04
87 #define USB_DT_ENDPOINT 0x05
88 #define USB_DT_DEVICE_QUALIFIER 0x06
89 #define USB_DT_OTHER_SPEED_CONFIG 0x07
91 struct usb_device_descriptor {
106 u8 bNumConfigurations;
109 #define USB_CLASS_PER_INTERFACE 0 /* for DeviceClass */
110 #define USB_CLASS_AUDIO 1
111 #define USB_CLASS_COMM 2
112 #define USB_CLASS_HID 3
113 #define USB_CLASS_PHYSICAL 5
114 #define USB_CLASS_STILL_IMAGE 6
115 #define USB_CLASS_PRINTER 7
116 #define USB_CLASS_MASS_STORAGE 8
117 #define USB_CLASS_HUB 9
119 struct usb_config_descriptor {
125 u8 bConfigurationValue;
131 struct usb_interface_descriptor {
136 u8 bAlternateSetting;
139 u8 bInterfaceSubClass;
140 u8 bInterfaceProtocol;
144 struct usb_endpoint_descriptor {
154 #define USB_ENDPOINT_NUMBER_MASK 0x0f /* in bEndpointAddress */
155 #define USB_ENDPOINT_DIR_MASK 0x80
157 #define USB_ENDPOINT_XFERTYPE_MASK 0x03 /* in bmAttributes */
158 #define USB_ENDPOINT_XFER_CONTROL 0
159 #define USB_ENDPOINT_XFER_ISOC 1
160 #define USB_ENDPOINT_XFER_BULK 2
161 #define USB_ENDPOINT_XFER_INT 3
162 #define USB_ENDPOINT_MAX_ADJUSTABLE 0x80
165 /****************************************************************
167 ****************************************************************/
170 void usb_setup(void);
171 struct usb_pipe *usb_set_address(struct usb_s *cntl, int lowspeed);
172 int configure_usb_device(struct usb_pipe *pipe);
173 int send_default_control(struct usb_pipe *pipe, const struct usb_ctrlrequest *req
175 int usb_send_bulk(struct usb_pipe *pipe, int dir, void *data, int datasize);
176 void free_pipe(struct usb_pipe *pipe);
177 struct usb_pipe *alloc_bulk_pipe(struct usb_pipe *pipe
178 , struct usb_endpoint_descriptor *epdesc);
179 struct usb_pipe *alloc_intr_pipe(struct usb_pipe *pipe
180 , struct usb_endpoint_descriptor *epdesc);
181 int usb_poll_intr(struct usb_pipe *pipe, void *data);
182 struct usb_endpoint_descriptor *findEndPointDesc(
183 struct usb_interface_descriptor *iface, int imax, int type, int dir);
184 u32 mkendpFromDesc(struct usb_pipe *pipe
185 , struct usb_endpoint_descriptor *epdesc);