1 /*
2  * Driver for USB ported from CoreBoot
3  *
4  * Copyright (C) 2014 BALATON Zoltan
5  *
6  * This file was part of the libpayload project.
7  *
8  * Copyright (C) 2008 coresystems GmbH
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. The name of the author may not be used to endorse or promote products
19  *    derived from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #ifndef __USB_H
35 #define __USB_H
36 #include <drivers/pci.h>
37 
38 typedef enum { host_to_device = 0, device_to_host = 1 } dev_req_dir;
39 typedef enum { standard_type = 0, class_type = 1, vendor_type =
40 		2, reserved_type = 3
41 } dev_req_type;
42 typedef enum { dev_recp = 0, iface_recp = 1, endp_recp = 2, other_recp = 3
43 } dev_req_recp;
44 
45 typedef enum {
46 	GET_STATUS = 0,
47 	CLEAR_FEATURE = 1,
48 	SET_FEATURE = 3,
49 	SET_ADDRESS = 5,
50 	GET_DESCRIPTOR = 6,
51 	SET_DESCRIPTOR = 7,
52 	GET_CONFIGURATION = 8,
53 	SET_CONFIGURATION = 9,
54 	GET_INTERFACE = 10,
55 	SET_INTERFACE = 11,
56 	SYNCH_FRAME = 12
57 } bRequest_Codes;
58 
59 typedef enum {
60 	ENDPOINT_HALT = 0,
61 	DEVICE_REMOTE_WAKEUP = 1,
62 	TEST_MODE = 2
63 } feature_selectors;
64 
65 enum {
66 	audio_device      = 0x01,
67 	comm_device       = 0x02,
68 	hid_device        = 0x03,
69 	physical_device   = 0x05,
70 	imaging_device    = 0x06,
71 	printer_device    = 0x07,
72 	msc_device        = 0x08,
73 	hub_device        = 0x09,
74 	cdc_device        = 0x0a,
75 	ccid_device       = 0x0b,
76 	security_device   = 0x0d,
77 	video_device      = 0x0e,
78 	healthcare_device = 0x0f,
79 	diagnostic_device = 0xdc,
80 	wireless_device   = 0xe0,
81 	misc_device       = 0xef,
82 };
83 
84 enum { hid_subclass_none = 0, hid_subclass_boot = 1 };
85 
86 enum {
87 	hid_boot_proto_none = 0,
88 	hid_boot_proto_keyboard = 1,
89 	hid_boot_proto_mouse = 2
90 };
91 
92 typedef struct {
93 	union {
94 		struct {
95 #ifdef CONFIG_BIG_ENDIAN
96 			dev_req_dir data_dir:1;
97 			dev_req_type req_type:2;
98 			dev_req_recp req_recp:5;
99 #else
100 			dev_req_recp req_recp:5;
101 			dev_req_type req_type:2;
102 			dev_req_dir data_dir:1;
103 #endif
104 		} __attribute__ ((packed));
105 		unsigned char bmRequestType;
106 	} __attribute__ ((packed));
107 	unsigned char bRequest;
108 	unsigned short wValue;
109 	unsigned short wIndex;
110 	unsigned short wLength;
111 } __attribute__ ((packed)) dev_req_t;
112 
113 struct usbdev_hc;
114 typedef struct usbdev_hc hci_t;
115 
116 struct usbdev;
117 typedef struct usbdev usbdev_t;
118 
119 typedef enum { SETUP, IN, OUT } direction_t;
120 typedef enum { CONTROL = 0, ISOCHRONOUS = 1, BULK = 2, INTERRUPT = 3
121 } endpoint_type;
122 
123 typedef struct {
124 	usbdev_t *dev;
125 	int endpoint;
126 	direction_t direction;
127 	int toggle;
128 	int maxpacketsize;
129 	endpoint_type type;
130 	int interval; /* expressed as binary logarithm of the number
131 			 of microframes (i.e. t = 125us * 2^interval) */
132 } endpoint_t;
133 
134 enum { FULL_SPEED = 0, LOW_SPEED = 1, HIGH_SPEED = 2, SUPER_SPEED = 3 };
135 
136 struct usbdev {
137 	hci_t *controller;
138 	endpoint_t endpoints[32];
139 	int num_endp;
140 	int address;		// usb address
141 	int hub;		// hub, device is attached to
142 	int port;		// port where device is attached
143 	int speed;		// 1: lowspeed, 0: fullspeed, 2: highspeed
144 	u32 quirks;		// quirks field. got to love usb
145 	void *data;
146 	u8 *descriptor;
147 	u8 *configuration;
148 	void (*init) (usbdev_t *dev);
149 	void (*destroy) (usbdev_t *dev);
150 	void (*poll) (usbdev_t *dev);
151 };
152 
153 typedef enum { OHCI = 0, UHCI = 1, EHCI = 2, XHCI = 3} hc_type;
154 
155 struct usbdev_hc {
156 	hci_t *next;
157 	u32 reg_base;
158 	hc_type type;
159 	usbdev_t *devices[128];	// dev 0 is root hub, 127 is last addressable
160 
161 	/* start():     Resume operation. */
162 	void (*start) (hci_t *controller);
163 	/* stop():      Stop operation but keep controller initialized. */
164 	void (*stop) (hci_t *controller);
165 	/* reset():     Perform a controller reset. The controller needs to
166 	                be (re)initialized afterwards to work (again). */
167 	void (*reset) (hci_t *controller);
168 	/* init():      Initialize a (previously reset) controller
169 	                to a working state. */
170 	void (*init) (hci_t *controller);
171 	/* shutdown():  Stop operation, detach host controller and shutdown
172 	                this driver instance. After calling shutdown() any
173 			other usage of this hci_t* is invalid. */
174 	void (*shutdown) (hci_t *controller);
175 
176 	int (*bulk) (endpoint_t *ep, int size, u8 *data, int finalize);
177 	int (*control) (usbdev_t *dev, direction_t pid, int dr_length,
178 			void *devreq, int data_length, u8 *data);
179 	void* (*create_intr_queue) (endpoint_t *ep, int reqsize, int reqcount, int reqtiming);
180 	void (*destroy_intr_queue) (endpoint_t *ep, void *queue);
181 	u8* (*poll_intr_queue) (void *queue);
182 	void *instance;
183 
184 	/* set_address():		Tell the usb device its address and
185 					return it. xHCI controllers want to
186 					do this by themself. Also, the usbdev
187 					structure has to be allocated and
188 					initialized. */
189 	int (*set_address) (hci_t *controller, int speed, int hubport, int hubaddr);
190 	/* finish_device_config():	Another hook for xHCI,
191 					returns 0 on success. */
192 	int (*finish_device_config) (usbdev_t *dev);
193 	/* destroy_device():		Finally, destroy all structures that
194 					were allocated during set_address()
195 					and finish_device_config(). */
196 	void (*destroy_device) (hci_t *controller, int devaddr);
197 };
198 
199 typedef struct {
200 	unsigned char bDescLength;
201 	unsigned char bDescriptorType;
202 	unsigned char bNbrPorts;
203 	union {
204 		struct {
205 #ifdef CONFIG_BIG_ENDIAN
206 			unsigned long:8;
207 			unsigned long arePortIndicatorsSupported:1;
208 			unsigned long ttThinkTime:2;
209 			unsigned long overcurrentProtectionMode:2;
210 			unsigned long isCompoundDevice:1;
211 			unsigned long logicalPowerSwitchingMode:2;
212 #else
213 			unsigned long logicalPowerSwitchingMode:2;
214 			unsigned long isCompoundDevice:1;
215 			unsigned long overcurrentProtectionMode:2;
216 			unsigned long ttThinkTime:2;
217 			unsigned long arePortIndicatorsSupported:1;
218 			unsigned long:8;
219 #endif
220 		} __attribute__ ((packed));
221 		unsigned short wHubCharacteristics;
222 	} __attribute__ ((packed));
223 	unsigned char bPowerOn2PwrGood;
224 	unsigned char bHubContrCurrent;
225 	char DeviceRemovable[];
226 } __attribute__ ((packed)) hub_descriptor_t;
227 
228 typedef struct {
229 	unsigned char bLength;
230 	unsigned char bDescriptorType;
231 	unsigned short bcdUSB;
232 	unsigned char bDeviceClass;
233 	unsigned char bDeviceSubClass;
234 	unsigned char bDeviceProtocol;
235 	unsigned char bMaxPacketSize0;
236 	unsigned short idVendor;
237 	unsigned short idProduct;
238 	unsigned short bcdDevice;
239 	unsigned char iManufacturer;
240 	unsigned char iProduct;
241 	unsigned char iSerialNumber;
242 	unsigned char bNumConfigurations;
243 } __attribute__ ((packed)) device_descriptor_t;
244 
245 typedef struct {
246 	unsigned char bLength;
247 	unsigned char bDescriptorType;
248 	unsigned short wTotalLength;
249 	unsigned char bNumInterfaces;
250 	unsigned char bConfigurationValue;
251 	unsigned char iConfiguration;
252 	unsigned char bmAttributes;
253 	unsigned char bMaxPower;
254 } __attribute__ ((packed)) configuration_descriptor_t;
255 
256 typedef struct {
257 	unsigned char bLength;
258 	unsigned char bDescriptorType;
259 	unsigned char bInterfaceNumber;
260 	unsigned char bAlternateSetting;
261 	unsigned char bNumEndpoints;
262 	unsigned char bInterfaceClass;
263 	unsigned char bInterfaceSubClass;
264 	unsigned char bInterfaceProtocol;
265 	unsigned char iInterface;
266 } __attribute__ ((packed)) interface_descriptor_t;
267 
268 typedef struct {
269 	unsigned char bLength;
270 	unsigned char bDescriptorType;
271 	unsigned char bEndpointAddress;
272 	unsigned char bmAttributes;
273 	unsigned short wMaxPacketSize;
274 	unsigned char bInterval;
275 } __attribute__ ((packed)) endpoint_descriptor_t;
276 
277 typedef struct {
278 	unsigned char bLength;
279 	unsigned char bDescriptorType;
280 	unsigned short bcdHID;
281 	unsigned char bCountryCode;
282 	unsigned char bNumDescriptors;
283 	unsigned char bReportDescriptorType;
284 	unsigned short wReportDescriptorLength;
285 } __attribute__ ((packed)) hid_descriptor_t;
286 
287 hci_t *new_controller (void);
288 void detach_controller (hci_t *controller);
289 void usb_poll (void);
290 void init_device_entry (hci_t *controller, int num);
291 
292 void set_feature (usbdev_t *dev, int endp, int feature, int rtype);
293 void get_status (usbdev_t *dev, int endp, int rtype, int len, void *data);
294 void set_configuration (usbdev_t *dev);
295 int clear_feature (usbdev_t *dev, int endp, int feature, int rtype);
296 int clear_stall (endpoint_t *ep);
297 
298 void usb_hub_init (usbdev_t *dev);
299 void usb_hid_init (usbdev_t *dev);
300 void usb_msc_init (usbdev_t *dev);
301 void usb_generic_init (usbdev_t *dev);
302 
303 u8 *get_descriptor (usbdev_t *dev, unsigned char bmRequestType,
304 		    int descType, int descIdx, int langID);
305 
306 static inline unsigned char
gen_bmRequestType(dev_req_dir dir,dev_req_type type,dev_req_recp recp)307 gen_bmRequestType (dev_req_dir dir, dev_req_type type, dev_req_recp recp)
308 {
309 	return (dir << 7) | (type << 5) | recp;
310 }
311 
312 /* default "set address" handler */
313 int generic_set_address (hci_t *controller, int speed, int hubport, int hubaddr);
314 
315 void usb_detach_device(hci_t *controller, int devno);
316 int usb_attach_device(hci_t *controller, int hubaddress, int port, int speed);
317 
318 u32 usb_quirk_check(u16 vendor, u16 device);
319 int usb_interface_check(u16 vendor, u16 device);
320 
321 #define USB_QUIRK_MSC_FORCE_PROTO_SCSI		(1 <<  0)
322 #define USB_QUIRK_MSC_FORCE_PROTO_ATAPI		(1 <<  1)
323 #define USB_QUIRK_MSC_FORCE_PROTO_UFI		(1 <<  2)
324 #define USB_QUIRK_MSC_FORCE_PROTO_RBC		(1 <<  3)
325 #define USB_QUIRK_MSC_FORCE_TRANS_BBB		(1 <<  4)
326 #define USB_QUIRK_MSC_FORCE_TRANS_CBI		(1 <<  5)
327 #define USB_QUIRK_MSC_FORCE_TRANS_CBI_I		(1 <<  6)
328 #define USB_QUIRK_MSC_NO_TEST_UNIT_READY	(1 <<  7)
329 #define USB_QUIRK_MSC_SHORT_INQUIRY		(1 <<  8)
330 #define USB_QUIRK_TEST				(1 << 31)
331 #define USB_QUIRK_NONE				 0
332 
333 #ifdef CONFIG_DEBUG_USB
334 #define usb_debug(fmt, args...)  do { printk(fmt , ##args); } while (0)
335 #else
336 #define usb_debug(fmt, args...)
337 #endif
338 
339 /**
340  * To be implemented by libpayload-client. It's called by the USB stack
341  * when a new USB device is found which isn't claimed by a built in driver,
342  * so the client has the chance to know about it.
343  *
344  * @param dev descriptor for the USB device
345  */
346 void __attribute__((weak)) usb_generic_create (usbdev_t *dev);
347 
348 /**
349  * To be implemented by libpayload-client. It's called by the USB stack
350  * when it finds out that a USB device is removed which wasn't claimed by a
351  * built in driver.
352  *
353  * @param dev descriptor for the USB device
354  */
355 void __attribute__((weak)) usb_generic_remove (usbdev_t *dev);
356 
357 #endif
358