1 // USB functions and data.
5 #include "stacks.h" // struct mutex_s
7 // Information on a USB end point.
11 struct usb_pipe *freenext;
21 // Common information for usb devices.
24 struct usb_pipe *defpipe;
27 struct usb_config_descriptor *config;
28 struct usb_interface_descriptor *iface;
34 // Common information for usb controllers.
36 struct usb_pipe *freelist;
37 struct mutex_s resetlock;
38 struct pci_device *pci;
43 // Information for enumerating USB hubs
45 struct usbhub_op_s *op;
46 struct usbdevice_s *usbdev;
56 // Hub callback (32bit) info
58 int (*detect)(struct usbhub_s *hub, u32 port);
59 int (*reset)(struct usbhub_s *hub, u32 port);
60 void (*disconnect)(struct usbhub_s *hub, u32 port);
63 #define USB_TYPE_UHCI 1
64 #define USB_TYPE_OHCI 2
65 #define USB_TYPE_EHCI 3
66 #define USB_TYPE_XHCI 4
68 #define USB_FULLSPEED 0
69 #define USB_LOWSPEED 1
70 #define USB_HIGHSPEED 2
71 #define USB_SUPERSPEED 3
73 #define USB_MAXADDR 127
76 /****************************************************************
77 * usb structs and flags
78 ****************************************************************/
80 // USB mandated timings (in ms)
81 #define USB_TIME_SIGATT 100
82 #define USB_TIME_ATTDB 100
83 #define USB_TIME_DRST 10
84 #define USB_TIME_DRSTR 50
85 #define USB_TIME_RSTRCY 10
87 #define USB_TIME_STATUS 50
88 #define USB_TIME_DATAIN 500
89 #define USB_TIME_COMMAND 5000
91 #define USB_TIME_SETADDR_RECOVERY 2
93 #define USB_PID_OUT 0xe1
94 #define USB_PID_IN 0x69
95 #define USB_PID_SETUP 0x2d
97 #define USB_DIR_OUT 0 /* to device */
98 #define USB_DIR_IN 0x80 /* to host */
100 #define USB_TYPE_MASK (0x03 << 5)
101 #define USB_TYPE_STANDARD (0x00 << 5)
102 #define USB_TYPE_CLASS (0x01 << 5)
103 #define USB_TYPE_VENDOR (0x02 << 5)
104 #define USB_TYPE_RESERVED (0x03 << 5)
106 #define USB_RECIP_MASK 0x1f
107 #define USB_RECIP_DEVICE 0x00
108 #define USB_RECIP_INTERFACE 0x01
109 #define USB_RECIP_ENDPOINT 0x02
110 #define USB_RECIP_OTHER 0x03
112 #define USB_REQ_GET_STATUS 0x00
113 #define USB_REQ_CLEAR_FEATURE 0x01
114 #define USB_REQ_SET_FEATURE 0x03
115 #define USB_REQ_SET_ADDRESS 0x05
116 #define USB_REQ_GET_DESCRIPTOR 0x06
117 #define USB_REQ_SET_DESCRIPTOR 0x07
118 #define USB_REQ_GET_CONFIGURATION 0x08
119 #define USB_REQ_SET_CONFIGURATION 0x09
120 #define USB_REQ_GET_INTERFACE 0x0A
121 #define USB_REQ_SET_INTERFACE 0x0B
122 #define USB_REQ_SYNCH_FRAME 0x0C
124 struct usb_ctrlrequest {
132 #define USB_DT_DEVICE 0x01
133 #define USB_DT_CONFIG 0x02
134 #define USB_DT_STRING 0x03
135 #define USB_DT_INTERFACE 0x04
136 #define USB_DT_ENDPOINT 0x05
137 #define USB_DT_DEVICE_QUALIFIER 0x06
138 #define USB_DT_OTHER_SPEED_CONFIG 0x07
139 #define USB_DT_ENDPOINT_COMPANION 0x30
141 struct usb_device_descriptor {
156 u8 bNumConfigurations;
159 #define USB_CLASS_PER_INTERFACE 0 /* for DeviceClass */
160 #define USB_CLASS_AUDIO 1
161 #define USB_CLASS_COMM 2
162 #define USB_CLASS_HID 3
163 #define USB_CLASS_PHYSICAL 5
164 #define USB_CLASS_STILL_IMAGE 6
165 #define USB_CLASS_PRINTER 7
166 #define USB_CLASS_MASS_STORAGE 8
167 #define USB_CLASS_HUB 9
169 struct usb_config_descriptor {
175 u8 bConfigurationValue;
181 struct usb_interface_descriptor {
186 u8 bAlternateSetting;
189 u8 bInterfaceSubClass;
190 u8 bInterfaceProtocol;
194 struct usb_endpoint_descriptor {
204 #define USB_ENDPOINT_NUMBER_MASK 0x0f /* in bEndpointAddress */
205 #define USB_ENDPOINT_DIR_MASK 0x80
207 #define USB_ENDPOINT_XFERTYPE_MASK 0x03 /* in bmAttributes */
208 #define USB_ENDPOINT_XFER_CONTROL 0
209 #define USB_ENDPOINT_XFER_ISOC 1
210 #define USB_ENDPOINT_XFER_BULK 2
211 #define USB_ENDPOINT_XFER_INT 3
212 #define USB_ENDPOINT_MAX_ADJUSTABLE 0x80
214 #define USB_CONTROL_SETUP_SIZE 8
217 /****************************************************************
218 * usb mass storage flags
219 ****************************************************************/
221 #define US_SC_ATAPI_8020 0x02
222 #define US_SC_ATAPI_8070 0x05
223 #define US_SC_SCSI 0x06
225 #define US_PR_BULK 0x50 /* bulk-only transport */
226 #define US_PR_UAS 0x62 /* usb attached scsi */
228 /****************************************************************
230 ****************************************************************/
233 int usb_send_bulk(struct usb_pipe *pipe, int dir, void *data, int datasize);
234 int usb_poll_intr(struct usb_pipe *pipe, void *data);
235 int usb_32bit_pipe(struct usb_pipe *pipe_fl);
236 struct usb_pipe *usb_alloc_pipe(struct usbdevice_s *usbdev
237 , struct usb_endpoint_descriptor *epdesc);
238 void usb_free_pipe(struct usbdevice_s *usbdev, struct usb_pipe *pipe);
239 int usb_send_default_control(struct usb_pipe *pipe
240 , const struct usb_ctrlrequest *req, void *data);
241 int usb_is_freelist(struct usb_s *cntl, struct usb_pipe *pipe);
242 void usb_add_freelist(struct usb_pipe *pipe);
243 struct usb_pipe *usb_get_freelist(struct usb_s *cntl, u8 eptype);
244 void usb_desc2pipe(struct usb_pipe *pipe, struct usbdevice_s *usbdev
245 , struct usb_endpoint_descriptor *epdesc);
246 int usb_get_period(struct usbdevice_s *usbdev
247 , struct usb_endpoint_descriptor *epdesc);
248 int usb_xfer_time(struct usb_pipe *pipe, int datalen);
249 struct usb_endpoint_descriptor *usb_find_desc(struct usbdevice_s *usbdev
250 , int type, int dir);
251 void usb_enumerate(struct usbhub_s *hub);
252 void usb_setup(void);