1 // USB functions and data.
5 #include "util.h" // struct mutex_s
11 // Local information for a usb controller.
16 struct usb_pipe *defaultpipe;
17 struct mutex_s resetlock;
22 void *control_qh, *bulk_qh, *framelist;
25 struct ohci_regs *regs;
30 #define USB_TYPE_UHCI 1
31 #define USB_TYPE_OHCI 2
33 extern struct usb_s USBControllers[];
35 #define USB_MAXADDR 127
39 struct usb_pipe *usb_set_address(struct usb_s *cntl, int lowspeed);
40 int configure_usb_device(struct usb_pipe *pipe);
41 struct usb_ctrlrequest;
42 int send_default_control(struct usb_pipe *pipe, const struct usb_ctrlrequest *req
44 int usb_send_bulk(struct usb_pipe *pipe, int dir, void *data, int datasize);
45 void free_pipe(struct usb_pipe *pipe);
46 struct usb_pipe *alloc_bulk_pipe(u32 endp);
47 struct usb_pipe *alloc_intr_pipe(u32 endp, int period);
48 int usb_poll_intr(struct usb_pipe *pipe, void *data);
49 struct usb_interface_descriptor;
50 struct usb_endpoint_descriptor *findEndPointDesc(
51 struct usb_interface_descriptor *iface, int imax, int type, int dir);
52 u32 mkendpFromDesc(struct usb_pipe *pipe
53 , struct usb_endpoint_descriptor *epdesc);
56 /****************************************************************
58 ****************************************************************/
61 mkendp(struct usb_s *cntl, u8 devaddr, u8 ep, u8 lowspeed, u8 maxsize)
63 u8 bus = cntl-USBControllers;
64 u8 size = __ffs(maxsize);
65 return (size<<25) | (lowspeed<<24) | (bus<<16) | (devaddr<<8) | ep;
68 static inline u8 endp2ep(u32 endp) {
71 static inline u8 endp2devaddr(u32 endp) {
74 static inline struct usb_s *endp2cntl(u32 endp) {
76 return &USBControllers[bus];
78 static inline u8 endp2speed(u32 endp) {
79 return (endp>>24) & 1;
81 static inline u8 endp2maxsize(u32 endp) {
82 return 1 << (endp>>25);
86 /****************************************************************
87 * usb structs and flags
88 ****************************************************************/
90 // USB mandated timings (in ms)
91 #define USB_TIME_SIGATT 100
92 #define USB_TIME_ATTDB 100
93 #define USB_TIME_DRST 10
94 #define USB_TIME_DRSTR 50
95 #define USB_TIME_RSTRCY 10
97 #define USB_TIME_SETADDR_RECOVERY 2
99 #define USB_PID_OUT 0xe1
100 #define USB_PID_IN 0x69
101 #define USB_PID_SETUP 0x2d
103 #define USB_DIR_OUT 0 /* to device */
104 #define USB_DIR_IN 0x80 /* to host */
106 #define USB_TYPE_MASK (0x03 << 5)
107 #define USB_TYPE_STANDARD (0x00 << 5)
108 #define USB_TYPE_CLASS (0x01 << 5)
109 #define USB_TYPE_VENDOR (0x02 << 5)
110 #define USB_TYPE_RESERVED (0x03 << 5)
112 #define USB_RECIP_MASK 0x1f
113 #define USB_RECIP_DEVICE 0x00
114 #define USB_RECIP_INTERFACE 0x01
115 #define USB_RECIP_ENDPOINT 0x02
116 #define USB_RECIP_OTHER 0x03
118 #define USB_REQ_GET_STATUS 0x00
119 #define USB_REQ_CLEAR_FEATURE 0x01
120 #define USB_REQ_SET_FEATURE 0x03
121 #define USB_REQ_SET_ADDRESS 0x05
122 #define USB_REQ_GET_DESCRIPTOR 0x06
123 #define USB_REQ_SET_DESCRIPTOR 0x07
124 #define USB_REQ_GET_CONFIGURATION 0x08
125 #define USB_REQ_SET_CONFIGURATION 0x09
126 #define USB_REQ_GET_INTERFACE 0x0A
127 #define USB_REQ_SET_INTERFACE 0x0B
128 #define USB_REQ_SYNCH_FRAME 0x0C
130 struct usb_ctrlrequest {
138 #define USB_DT_DEVICE 0x01
139 #define USB_DT_CONFIG 0x02
140 #define USB_DT_STRING 0x03
141 #define USB_DT_INTERFACE 0x04
142 #define USB_DT_ENDPOINT 0x05
143 #define USB_DT_DEVICE_QUALIFIER 0x06
144 #define USB_DT_OTHER_SPEED_CONFIG 0x07
146 struct usb_device_descriptor {
161 u8 bNumConfigurations;
164 #define USB_CLASS_PER_INTERFACE 0 /* for DeviceClass */
165 #define USB_CLASS_AUDIO 1
166 #define USB_CLASS_COMM 2
167 #define USB_CLASS_HID 3
168 #define USB_CLASS_PHYSICAL 5
169 #define USB_CLASS_STILL_IMAGE 6
170 #define USB_CLASS_PRINTER 7
171 #define USB_CLASS_MASS_STORAGE 8
172 #define USB_CLASS_HUB 9
174 struct usb_config_descriptor {
180 u8 bConfigurationValue;
186 struct usb_interface_descriptor {
191 u8 bAlternateSetting;
194 u8 bInterfaceSubClass;
195 u8 bInterfaceProtocol;
199 struct usb_endpoint_descriptor {
209 #define USB_ENDPOINT_NUMBER_MASK 0x0f /* in bEndpointAddress */
210 #define USB_ENDPOINT_DIR_MASK 0x80
212 #define USB_ENDPOINT_XFERTYPE_MASK 0x03 /* in bmAttributes */
213 #define USB_ENDPOINT_XFER_CONTROL 0
214 #define USB_ENDPOINT_XFER_ISOC 1
215 #define USB_ENDPOINT_XFER_BULK 2
216 #define USB_ENDPOINT_XFER_INT 3
217 #define USB_ENDPOINT_MAX_ADJUSTABLE 0x80