1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3  * (C) Copyright 2001
4  * Denis Peter, MPL AG Switzerland
5  *
6  * Adapted for U-Boot driver model
7  * (C) Copyright 2015 Google, Inc
8  * Note: Part of this code has been derived from linux
9  *
10  */
11 #ifndef _USB_H_
12 #define _USB_H_
13 
14 #include <fdtdec.h>
15 #include <usb_defs.h>
16 #include <linux/usb/ch9.h>
17 #include <asm/cache.h>
18 #include <part.h>
19 
20 /*
21  * The EHCI spec says that we must align to at least 32 bytes.  However,
22  * some platforms require larger alignment.
23  */
24 #if ARCH_DMA_MINALIGN > 32
25 #define USB_DMA_MINALIGN	ARCH_DMA_MINALIGN
26 #else
27 #define USB_DMA_MINALIGN	32
28 #endif
29 
30 /* Everything is aribtrary */
31 #define USB_ALTSETTINGALLOC		4
32 #define USB_MAXALTSETTING		128	/* Hard limit */
33 
34 #define USB_MAX_DEVICE			32
35 #define USB_MAXCONFIG			8
36 #define USB_MAXINTERFACES		8
37 #define USB_MAXENDPOINTS		16
38 #define USB_MAXCHILDREN			8	/* This is arbitrary */
39 #define USB_MAX_HUB			16
40 
41 #define USB_CNTL_TIMEOUT 100 /* 100ms timeout */
42 
43 /*
44  * This is the timeout to allow for submitting an urb in ms. We allow more
45  * time for a BULK device to react - some are slow.
46  */
47 #define USB_TIMEOUT_MS(pipe) (usb_pipebulk(pipe) ? 5000 : 1000)
48 
49 /* device request (setup) */
50 struct devrequest {
51 	__u8	requesttype;
52 	__u8	request;
53 	__le16	value;
54 	__le16	index;
55 	__le16	length;
56 } __attribute__ ((packed));
57 
58 /* Interface */
59 struct usb_interface {
60 	struct usb_interface_descriptor desc;
61 
62 	__u8	no_of_ep;
63 	__u8	num_altsetting;
64 	__u8	act_altsetting;
65 
66 	struct usb_endpoint_descriptor ep_desc[USB_MAXENDPOINTS];
67 	/*
68 	 * Super Speed Device will have Super Speed Endpoint
69 	 * Companion Descriptor  (section 9.6.7 of usb 3.0 spec)
70 	 * Revision 1.0 June 6th 2011
71 	 */
72 	struct usb_ss_ep_comp_descriptor ss_ep_comp_desc[USB_MAXENDPOINTS];
73 } __attribute__ ((packed));
74 
75 /* Configuration information.. */
76 struct usb_config {
77 	struct usb_config_descriptor desc;
78 
79 	__u8	no_of_if;	/* number of interfaces */
80 	struct usb_interface if_desc[USB_MAXINTERFACES];
81 } __attribute__ ((packed));
82 
83 enum {
84 	/* Maximum packet size; encoded as 0,1,2,3 = 8,16,32,64 */
85 	PACKET_SIZE_8   = 0,
86 	PACKET_SIZE_16  = 1,
87 	PACKET_SIZE_32  = 2,
88 	PACKET_SIZE_64  = 3,
89 };
90 
91 /**
92  * struct usb_device - information about a USB device
93  *
94  * With driver model both UCLASS_USB (the USB controllers) and UCLASS_USB_HUB
95  * (the hubs) have this as parent data. Hubs are children of controllers or
96  * other hubs and there is always a single root hub for each controller.
97  * Therefore struct usb_device can always be accessed with
98  * dev_get_parent_priv(dev), where dev is a USB device.
99  *
100  * Pointers exist for obtaining both the device (could be any uclass) and
101  * controller (UCLASS_USB) from this structure. The controller does not have
102  * a struct usb_device since it is not a device.
103  */
104 struct usb_device {
105 	int	devnum;			/* Device number on USB bus */
106 	enum usb_device_speed speed;	/* full/low/high */
107 	char	mf[32];			/* manufacturer */
108 	char	prod[32];		/* product */
109 	char	serial[32];		/* serial number */
110 
111 	/* Maximum packet size; one of: PACKET_SIZE_* */
112 	int maxpacketsize;
113 	/* one bit for each endpoint ([0] = IN, [1] = OUT) */
114 	unsigned int toggle[2];
115 	/* endpoint halts; one bit per endpoint # & direction;
116 	 * [0] = IN, [1] = OUT
117 	 */
118 	unsigned int halted[2];
119 	int epmaxpacketin[16];		/* INput endpoint specific maximums */
120 	int epmaxpacketout[16];		/* OUTput endpoint specific maximums */
121 
122 	int configno;			/* selected config number */
123 	/* Device Descriptor */
124 	struct usb_device_descriptor descriptor
125 		__attribute__((aligned(ARCH_DMA_MINALIGN)));
126 	struct usb_config config; /* config descriptor */
127 
128 	int have_langid;		/* whether string_langid is valid yet */
129 	int string_langid;		/* language ID for strings */
130 	int (*irq_handle)(struct usb_device *dev);
131 	unsigned long irq_status;
132 	int irq_act_len;		/* transferred bytes */
133 	void *privptr;
134 	/*
135 	 * Child devices -  if this is a hub device
136 	 * Each instance needs its own set of data structures.
137 	 */
138 	unsigned long status;
139 	unsigned long int_pending;	/* 1 bit per ep, used by int_queue */
140 	int act_len;			/* transferred bytes */
141 	int maxchild;			/* Number of ports if hub */
142 	int portnr;			/* Port number, 1=first */
143 #if !CONFIG_IS_ENABLED(DM_USB)
144 	/* parent hub, or NULL if this is the root hub */
145 	struct usb_device *parent;
146 	struct usb_device *children[USB_MAXCHILDREN];
147 	void *controller;		/* hardware controller private data */
148 #endif
149 	/* slot_id - for xHCI enabled devices */
150 	unsigned int slot_id;
151 #if CONFIG_IS_ENABLED(DM_USB)
152 	struct udevice *dev;		/* Pointer to associated device */
153 	struct udevice *controller_dev;	/* Pointer to associated controller */
154 #endif
155 };
156 
157 struct int_queue;
158 
159 /*
160  * You can initialize platform's USB host or device
161  * ports by passing this enum as an argument to
162  * board_usb_init().
163  */
164 enum usb_init_type {
165 	USB_INIT_HOST,
166 	USB_INIT_DEVICE
167 };
168 
169 /**********************************************************************
170  * this is how the lowlevel part communicate with the outer world
171  */
172 
173 int usb_lowlevel_init(int index, enum usb_init_type init, void **controller);
174 int usb_lowlevel_stop(int index);
175 
176 #if defined(CONFIG_USB_MUSB_HOST) || CONFIG_IS_ENABLED(DM_USB)
177 int usb_reset_root_port(struct usb_device *dev);
178 #else
179 #define usb_reset_root_port(dev)
180 #endif
181 
182 int submit_bulk_msg(struct usb_device *dev, unsigned long pipe,
183 			void *buffer, int transfer_len);
184 int submit_control_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
185 			int transfer_len, struct devrequest *setup);
186 int submit_int_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
187 			int transfer_len, int interval, bool nonblock);
188 
189 #if defined CONFIG_USB_EHCI_HCD || defined CONFIG_USB_MUSB_HOST \
190 	|| CONFIG_IS_ENABLED(DM_USB)
191 struct int_queue *create_int_queue(struct usb_device *dev, unsigned long pipe,
192 	int queuesize, int elementsize, void *buffer, int interval);
193 int destroy_int_queue(struct usb_device *dev, struct int_queue *queue);
194 void *poll_int_queue(struct usb_device *dev, struct int_queue *queue);
195 #endif
196 
197 /* Defines */
198 #define USB_UHCI_VEND_ID	0x8086
199 #define USB_UHCI_DEV_ID		0x7112
200 
201 /*
202  * PXA25x can only act as USB device. There are drivers
203  * which works with USB CDC gadgets implementations.
204  * Some of them have common routines which can be used
205  * in boards init functions e.g. udc_disconnect() used for
206  * forced device disconnection from host.
207  */
208 extern void udc_disconnect(void);
209 
210 /*
211  * board-specific hardware initialization, called by
212  * usb drivers and u-boot commands
213  *
214  * @param index USB controller number
215  * @param init initializes controller as USB host or device
216  */
217 int board_usb_init(int index, enum usb_init_type init);
218 
219 /*
220  * can be used to clean up after failed USB initialization attempt
221  * vide: board_usb_init()
222  *
223  * @param index USB controller number for selective cleanup
224  * @param init usb_init_type passed to board_usb_init()
225  */
226 int board_usb_cleanup(int index, enum usb_init_type init);
227 
228 #ifdef CONFIG_USB_STORAGE
229 
230 #define USB_MAX_STOR_DEV 7
231 int usb_stor_scan(int mode);
232 int usb_stor_info(void);
233 
234 #endif
235 
236 #ifdef CONFIG_USB_HOST_ETHER
237 
238 #define USB_MAX_ETH_DEV 5
239 int usb_host_eth_scan(int mode);
240 
241 #endif
242 
243 #ifdef CONFIG_USB_KEYBOARD
244 
245 /*
246  * USB Keyboard reports are 8 bytes in boot protocol.
247  * Appendix B of HID Device Class Definition 1.11
248  */
249 #define USB_KBD_BOOT_REPORT_SIZE 8
250 
251 int drv_usb_kbd_init(void);
252 int usb_kbd_deregister(int force);
253 
254 #endif
255 /* routines */
256 int usb_init(void); /* initialize the USB Controller */
257 int usb_stop(void); /* stop the USB Controller */
258 int usb_detect_change(void); /* detect if a USB device has been (un)plugged */
259 
260 
261 int usb_set_protocol(struct usb_device *dev, int ifnum, int protocol);
262 int usb_set_idle(struct usb_device *dev, int ifnum, int duration,
263 			int report_id);
264 int usb_control_msg(struct usb_device *dev, unsigned int pipe,
265 			unsigned char request, unsigned char requesttype,
266 			unsigned short value, unsigned short index,
267 			void *data, unsigned short size, int timeout);
268 int usb_bulk_msg(struct usb_device *dev, unsigned int pipe,
269 			void *data, int len, int *actual_length, int timeout);
270 int usb_int_msg(struct usb_device *dev, unsigned long pipe,
271 		void *buffer, int transfer_len, int interval, bool nonblock);
272 int usb_lock_async(struct usb_device *dev, int lock);
273 int usb_disable_asynch(int disable);
274 int usb_maxpacket(struct usb_device *dev, unsigned long pipe);
275 int usb_get_configuration_no(struct usb_device *dev, int cfgno,
276 			unsigned char *buffer, int length);
277 int usb_get_configuration_len(struct usb_device *dev, int cfgno);
278 int usb_get_report(struct usb_device *dev, int ifnum, unsigned char type,
279 			unsigned char id, void *buf, int size);
280 int usb_get_class_descriptor(struct usb_device *dev, int ifnum,
281 			unsigned char type, unsigned char id, void *buf,
282 			int size);
283 int usb_clear_halt(struct usb_device *dev, int pipe);
284 int usb_string(struct usb_device *dev, int index, char *buf, size_t size);
285 int usb_set_interface(struct usb_device *dev, int interface, int alternate);
286 int usb_get_port_status(struct usb_device *dev, int port, void *data);
287 
288 /* big endian -> little endian conversion */
289 /* some CPUs are already little endian e.g. the ARM920T */
290 #define __swap_16(x) \
291 	({ unsigned short x_ = (unsigned short)x; \
292 	 (unsigned short)( \
293 		((x_ & 0x00FFU) << 8) | ((x_ & 0xFF00U) >> 8)); \
294 	})
295 #define __swap_32(x) \
296 	({ unsigned long x_ = (unsigned long)x; \
297 	 (unsigned long)( \
298 		((x_ & 0x000000FFUL) << 24) | \
299 		((x_ & 0x0000FF00UL) <<	 8) | \
300 		((x_ & 0x00FF0000UL) >>	 8) | \
301 		((x_ & 0xFF000000UL) >> 24)); \
302 	})
303 
304 #ifdef __LITTLE_ENDIAN
305 # define swap_16(x) (x)
306 # define swap_32(x) (x)
307 #else
308 # define swap_16(x) __swap_16(x)
309 # define swap_32(x) __swap_32(x)
310 #endif
311 
312 /*
313  * Calling this entity a "pipe" is glorifying it. A USB pipe
314  * is something embarrassingly simple: it basically consists
315  * of the following information:
316  *  - device number (7 bits)
317  *  - endpoint number (4 bits)
318  *  - current Data0/1 state (1 bit)
319  *  - direction (1 bit)
320  *  - speed (2 bits)
321  *  - max packet size (2 bits: 8, 16, 32 or 64)
322  *  - pipe type (2 bits: control, interrupt, bulk, isochronous)
323  *
324  * That's 18 bits. Really. Nothing more. And the USB people have
325  * documented these eighteen bits as some kind of glorious
326  * virtual data structure.
327  *
328  * Let's not fall in that trap. We'll just encode it as a simple
329  * unsigned int. The encoding is:
330  *
331  *  - max size:		bits 0-1	(00 = 8, 01 = 16, 10 = 32, 11 = 64)
332  *  - direction:	bit 7		(0 = Host-to-Device [Out],
333  *					(1 = Device-to-Host [In])
334  *  - device:		bits 8-14
335  *  - endpoint:		bits 15-18
336  *  - Data0/1:		bit 19
337  *  - pipe type:	bits 30-31	(00 = isochronous, 01 = interrupt,
338  *					 10 = control, 11 = bulk)
339  *
340  * Why? Because it's arbitrary, and whatever encoding we select is really
341  * up to us. This one happens to share a lot of bit positions with the UHCI
342  * specification, so that much of the uhci driver can just mask the bits
343  * appropriately.
344  */
345 /* Create various pipes... */
346 #define create_pipe(dev,endpoint) \
347 		(((dev)->devnum << 8) | ((endpoint) << 15) | \
348 		(dev)->maxpacketsize)
349 #define default_pipe(dev) ((dev)->speed << 26)
350 
351 #define usb_sndctrlpipe(dev, endpoint)	((PIPE_CONTROL << 30) | \
352 					 create_pipe(dev, endpoint))
353 #define usb_rcvctrlpipe(dev, endpoint)	((PIPE_CONTROL << 30) | \
354 					 create_pipe(dev, endpoint) | \
355 					 USB_DIR_IN)
356 #define usb_sndisocpipe(dev, endpoint)	((PIPE_ISOCHRONOUS << 30) | \
357 					 create_pipe(dev, endpoint))
358 #define usb_rcvisocpipe(dev, endpoint)	((PIPE_ISOCHRONOUS << 30) | \
359 					 create_pipe(dev, endpoint) | \
360 					 USB_DIR_IN)
361 #define usb_sndbulkpipe(dev, endpoint)	((PIPE_BULK << 30) | \
362 					 create_pipe(dev, endpoint))
363 #define usb_rcvbulkpipe(dev, endpoint)	((PIPE_BULK << 30) | \
364 					 create_pipe(dev, endpoint) | \
365 					 USB_DIR_IN)
366 #define usb_sndintpipe(dev, endpoint)	((PIPE_INTERRUPT << 30) | \
367 					 create_pipe(dev, endpoint))
368 #define usb_rcvintpipe(dev, endpoint)	((PIPE_INTERRUPT << 30) | \
369 					 create_pipe(dev, endpoint) | \
370 					 USB_DIR_IN)
371 #define usb_snddefctrl(dev)		((PIPE_CONTROL << 30) | \
372 					 default_pipe(dev))
373 #define usb_rcvdefctrl(dev)		((PIPE_CONTROL << 30) | \
374 					 default_pipe(dev) | \
375 					 USB_DIR_IN)
376 
377 /* The D0/D1 toggle bits */
378 #define usb_gettoggle(dev, ep, out) (((dev)->toggle[out] >> ep) & 1)
379 #define usb_dotoggle(dev, ep, out)  ((dev)->toggle[out] ^= (1 << ep))
380 #define usb_settoggle(dev, ep, out, bit) ((dev)->toggle[out] = \
381 						((dev)->toggle[out] & \
382 						 ~(1 << ep)) | ((bit) << ep))
383 
384 /* Endpoint halt control/status */
385 #define usb_endpoint_out(ep_dir)	(((ep_dir >> 7) & 1) ^ 1)
386 #define usb_endpoint_halt(dev, ep, out) ((dev)->halted[out] |= (1 << (ep)))
387 #define usb_endpoint_running(dev, ep, out) ((dev)->halted[out] &= ~(1 << (ep)))
388 #define usb_endpoint_halted(dev, ep, out) ((dev)->halted[out] & (1 << (ep)))
389 
390 #define usb_packetid(pipe)	(((pipe) & USB_DIR_IN) ? USB_PID_IN : \
391 				 USB_PID_OUT)
392 
393 #define usb_pipeout(pipe)	((((pipe) >> 7) & 1) ^ 1)
394 #define usb_pipein(pipe)	(((pipe) >> 7) & 1)
395 #define usb_pipedevice(pipe)	(((pipe) >> 8) & 0x7f)
396 #define usb_pipe_endpdev(pipe)	(((pipe) >> 8) & 0x7ff)
397 #define usb_pipeendpoint(pipe)	(((pipe) >> 15) & 0xf)
398 #define usb_pipedata(pipe)	(((pipe) >> 19) & 1)
399 #define usb_pipetype(pipe)	(((pipe) >> 30) & 3)
400 #define usb_pipeisoc(pipe)	(usb_pipetype((pipe)) == PIPE_ISOCHRONOUS)
401 #define usb_pipeint(pipe)	(usb_pipetype((pipe)) == PIPE_INTERRUPT)
402 #define usb_pipecontrol(pipe)	(usb_pipetype((pipe)) == PIPE_CONTROL)
403 #define usb_pipebulk(pipe)	(usb_pipetype((pipe)) == PIPE_BULK)
404 
405 #define usb_pipe_ep_index(pipe)	\
406 		usb_pipecontrol(pipe) ? (usb_pipeendpoint(pipe) * 2) : \
407 				((usb_pipeendpoint(pipe) * 2) - \
408 				 (usb_pipein(pipe) ? 0 : 1))
409 
410 /**
411  * struct usb_device_id - identifies USB devices for probing and hotplugging
412  * @match_flags: Bit mask controlling which of the other fields are used to
413  *	match against new devices. Any field except for driver_info may be
414  *	used, although some only make sense in conjunction with other fields.
415  *	This is usually set by a USB_DEVICE_*() macro, which sets all
416  *	other fields in this structure except for driver_info.
417  * @idVendor: USB vendor ID for a device; numbers are assigned
418  *	by the USB forum to its members.
419  * @idProduct: Vendor-assigned product ID.
420  * @bcdDevice_lo: Low end of range of vendor-assigned product version numbers.
421  *	This is also used to identify individual product versions, for
422  *	a range consisting of a single device.
423  * @bcdDevice_hi: High end of version number range.  The range of product
424  *	versions is inclusive.
425  * @bDeviceClass: Class of device; numbers are assigned
426  *	by the USB forum.  Products may choose to implement classes,
427  *	or be vendor-specific.  Device classes specify behavior of all
428  *	the interfaces on a device.
429  * @bDeviceSubClass: Subclass of device; associated with bDeviceClass.
430  * @bDeviceProtocol: Protocol of device; associated with bDeviceClass.
431  * @bInterfaceClass: Class of interface; numbers are assigned
432  *	by the USB forum.  Products may choose to implement classes,
433  *	or be vendor-specific.  Interface classes specify behavior only
434  *	of a given interface; other interfaces may support other classes.
435  * @bInterfaceSubClass: Subclass of interface; associated with bInterfaceClass.
436  * @bInterfaceProtocol: Protocol of interface; associated with bInterfaceClass.
437  * @bInterfaceNumber: Number of interface; composite devices may use
438  *	fixed interface numbers to differentiate between vendor-specific
439  *	interfaces.
440  * @driver_info: Holds information used by the driver.  Usually it holds
441  *	a pointer to a descriptor understood by the driver, or perhaps
442  *	device flags.
443  *
444  * In most cases, drivers will create a table of device IDs by using
445  * USB_DEVICE(), or similar macros designed for that purpose.
446  * They will then export it to userspace using MODULE_DEVICE_TABLE(),
447  * and provide it to the USB core through their usb_driver structure.
448  *
449  * See the usb_match_id() function for information about how matches are
450  * performed.  Briefly, you will normally use one of several macros to help
451  * construct these entries.  Each entry you provide will either identify
452  * one or more specific products, or will identify a class of products
453  * which have agreed to behave the same.  You should put the more specific
454  * matches towards the beginning of your table, so that driver_info can
455  * record quirks of specific products.
456  */
457 struct usb_device_id {
458 	/* which fields to match against? */
459 	u16 match_flags;
460 
461 	/* Used for product specific matches; range is inclusive */
462 	u16 idVendor;
463 	u16 idProduct;
464 	u16 bcdDevice_lo;
465 	u16 bcdDevice_hi;
466 
467 	/* Used for device class matches */
468 	u8 bDeviceClass;
469 	u8 bDeviceSubClass;
470 	u8 bDeviceProtocol;
471 
472 	/* Used for interface class matches */
473 	u8 bInterfaceClass;
474 	u8 bInterfaceSubClass;
475 	u8 bInterfaceProtocol;
476 
477 	/* Used for vendor-specific interface matches */
478 	u8 bInterfaceNumber;
479 
480 	/* not matched against */
481 	ulong driver_info;
482 };
483 
484 /* Some useful macros to use to create struct usb_device_id */
485 #define USB_DEVICE_ID_MATCH_VENDOR		0x0001
486 #define USB_DEVICE_ID_MATCH_PRODUCT		0x0002
487 #define USB_DEVICE_ID_MATCH_DEV_LO		0x0004
488 #define USB_DEVICE_ID_MATCH_DEV_HI		0x0008
489 #define USB_DEVICE_ID_MATCH_DEV_CLASS		0x0010
490 #define USB_DEVICE_ID_MATCH_DEV_SUBCLASS	0x0020
491 #define USB_DEVICE_ID_MATCH_DEV_PROTOCOL	0x0040
492 #define USB_DEVICE_ID_MATCH_INT_CLASS		0x0080
493 #define USB_DEVICE_ID_MATCH_INT_SUBCLASS	0x0100
494 #define USB_DEVICE_ID_MATCH_INT_PROTOCOL	0x0200
495 #define USB_DEVICE_ID_MATCH_INT_NUMBER		0x0400
496 
497 /* Match anything, indicates this is a valid entry even if everything is 0 */
498 #define USB_DEVICE_ID_MATCH_NONE		0x0800
499 #define USB_DEVICE_ID_MATCH_ALL			0x07ff
500 
501 /**
502  * struct usb_driver_entry - Matches a driver to its usb_device_ids
503  * @driver: Driver to use
504  * @match: List of match records for this driver, terminated by {}
505  */
506 struct usb_driver_entry {
507 	struct driver *driver;
508 	const struct usb_device_id *match;
509 };
510 
511 #define USB_DEVICE_ID_MATCH_DEVICE \
512 		(USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_PRODUCT)
513 
514 /**
515  * USB_DEVICE - macro used to describe a specific usb device
516  * @vend: the 16 bit USB Vendor ID
517  * @prod: the 16 bit USB Product ID
518  *
519  * This macro is used to create a struct usb_device_id that matches a
520  * specific device.
521  */
522 #define USB_DEVICE(vend, prod) \
523 	.match_flags = USB_DEVICE_ID_MATCH_DEVICE, \
524 	.idVendor = (vend), \
525 	.idProduct = (prod)
526 
527 #define U_BOOT_USB_DEVICE(__name, __match) \
528 	ll_entry_declare(struct usb_driver_entry, __name, usb_driver_entry) = {\
529 		.driver = llsym(struct driver, __name, driver), \
530 		.match = __match, \
531 		}
532 
533 /*************************************************************************
534  * Hub Stuff
535  */
536 struct usb_port_status {
537 	unsigned short wPortStatus;
538 	unsigned short wPortChange;
539 } __attribute__ ((packed));
540 
541 struct usb_hub_status {
542 	unsigned short wHubStatus;
543 	unsigned short wHubChange;
544 } __attribute__ ((packed));
545 
546 /*
547  * Hub Device descriptor
548  * USB Hub class device protocols
549  */
550 #define USB_HUB_PR_FS		0 /* Full speed hub */
551 #define USB_HUB_PR_HS_NO_TT	0 /* Hi-speed hub without TT */
552 #define USB_HUB_PR_HS_SINGLE_TT	1 /* Hi-speed hub with single TT */
553 #define USB_HUB_PR_HS_MULTI_TT	2 /* Hi-speed hub with multiple TT */
554 #define USB_HUB_PR_SS		3 /* Super speed hub */
555 
556 /* Transaction Translator Think Times, in bits */
557 #define HUB_TTTT_8_BITS		0x00
558 #define HUB_TTTT_16_BITS	0x20
559 #define HUB_TTTT_24_BITS	0x40
560 #define HUB_TTTT_32_BITS	0x60
561 
562 /* Hub descriptor */
563 struct usb_hub_descriptor {
564 	unsigned char  bLength;
565 	unsigned char  bDescriptorType;
566 	unsigned char  bNbrPorts;
567 	unsigned short wHubCharacteristics;
568 	unsigned char  bPwrOn2PwrGood;
569 	unsigned char  bHubContrCurrent;
570 	/* 2.0 and 3.0 hubs differ here */
571 	union {
572 		struct {
573 			/* add 1 bit for hub status change; round to bytes */
574 			__u8 DeviceRemovable[(USB_MAXCHILDREN + 1 + 7) / 8];
575 			__u8 PortPowerCtrlMask[(USB_MAXCHILDREN + 1 + 7) / 8];
576 		} __attribute__ ((packed)) hs;
577 
578 		struct {
579 			__u8 bHubHdrDecLat;
580 			__le16 wHubDelay;
581 			__le16 DeviceRemovable;
582 		} __attribute__ ((packed)) ss;
583 	} u;
584 } __attribute__ ((packed));
585 
586 
587 struct usb_hub_device {
588 	struct usb_device *pusb_dev;
589 	struct usb_hub_descriptor desc;
590 
591 	ulong connect_timeout;		/* Device connection timeout in ms */
592 	ulong query_delay;		/* Device query delay in ms */
593 	int overcurrent_count[USB_MAXCHILDREN];	/* Over-current counter */
594 	int hub_depth;			/* USB 3.0 hub depth */
595 	struct usb_tt tt;		/* Transaction Translator */
596 };
597 
598 #if CONFIG_IS_ENABLED(DM_USB)
599 /**
600  * struct usb_plat - Platform data about a USB controller
601  *
602  * Given a USB controller (UCLASS_USB) dev this is dev_get_plat(dev)
603  */
604 struct usb_plat {
605 	enum usb_init_type init_type;
606 };
607 
608 /**
609  * struct usb_dev_plat - Platform data about a USB device
610  *
611  * Given a USB device dev this structure is dev_get_parent_plat(dev).
612  * This is used by sandbox to provide emulation data also.
613  *
614  * @id:		ID used to match this device
615  * @devnum:	Device address on the USB bus
616  * @udev:	usb-uclass internal use only do NOT use
617  * @strings:	List of descriptor strings (for sandbox emulation purposes)
618  * @desc_list:	List of descriptors (for sandbox emulation purposes)
619  */
620 struct usb_dev_plat {
621 	struct usb_device_id id;
622 	int devnum;
623 	/*
624 	 * This pointer is used to pass the usb_device used in usb_scan_device,
625 	 * to get the usb descriptors before the driver is known, to the
626 	 * actual udevice once the driver is known and the udevice is created.
627 	 * This will be NULL except during probe, do NOT use.
628 	 *
629 	 * This should eventually go away.
630 	 */
631 	struct usb_device *udev;
632 #ifdef CONFIG_SANDBOX
633 	struct usb_string *strings;
634 	/* NULL-terminated list of descriptor pointers */
635 	struct usb_generic_descriptor **desc_list;
636 #endif
637 	int configno;
638 };
639 
640 /**
641  * struct usb_bus_priv - information about the USB controller
642  *
643  * Given a USB controller (UCLASS_USB) 'dev', this is
644  * dev_get_uclass_priv(dev).
645  *
646  * @next_addr:	Next device address to allocate minus 1. Incremented by 1
647  *		each time a new device address is set, so this holds the
648  *		number of devices on the bus
649  * @desc_before_addr:	true if we can read a device descriptor before it
650  *		has been assigned an address. For XHCI this is not possible
651  *		so this will be false.
652  * @companion:  True if this is a companion controller to another USB
653  *		controller
654  */
655 struct usb_bus_priv {
656 	int next_addr;
657 	bool desc_before_addr;
658 	bool companion;
659 };
660 
661 /**
662  * struct usb_emul_plat - platform data about the USB emulator
663  *
664  * Given a USB emulator (UCLASS_USB_EMUL) 'dev', this is
665  * dev_get_uclass_plat(dev).
666  *
667  * @port1:	USB emulator device port number on the parent hub
668  */
669 struct usb_emul_plat {
670 	int port1;	/* Port number (numbered from 1) */
671 };
672 
673 /**
674  * struct dm_usb_ops - USB controller operations
675  *
676  * This defines the operations supoorted on a USB controller. Common
677  * arguments are:
678  *
679  * @bus:	USB bus (i.e. controller), which is in UCLASS_USB.
680  * @udev:	USB device parent data. Controllers are not expected to need
681  *		this, since the device address on the bus is encoded in @pipe.
682  *		It is used for sandbox, and can be handy for debugging and
683  *		logging.
684  * @pipe:	An assortment of bitfields which provide address and packet
685  *		type information. See create_pipe() above for encoding
686  *		details
687  * @buffer:	A buffer to use for sending/receiving. This should be
688  *		DMA-aligned.
689  * @length:	Buffer length in bytes
690  */
691 struct dm_usb_ops {
692 	/**
693 	 * control() - Send a control message
694 	 *
695 	 * Most parameters are as above.
696 	 *
697 	 * @setup: Additional setup information required by the message
698 	 */
699 	int (*control)(struct udevice *bus, struct usb_device *udev,
700 		       unsigned long pipe, void *buffer, int length,
701 		       struct devrequest *setup);
702 	/**
703 	 * bulk() - Send a bulk message
704 	 *
705 	 * Parameters are as above.
706 	 */
707 	int (*bulk)(struct udevice *bus, struct usb_device *udev,
708 		    unsigned long pipe, void *buffer, int length);
709 	/**
710 	 * interrupt() - Send an interrupt message
711 	 *
712 	 * Most parameters are as above.
713 	 *
714 	 * @interval: Interrupt interval
715 	 */
716 	int (*interrupt)(struct udevice *bus, struct usb_device *udev,
717 			 unsigned long pipe, void *buffer, int length,
718 			 int interval, bool nonblock);
719 
720 	/**
721 	 * create_int_queue() - Create and queue interrupt packets
722 	 *
723 	 * Create and queue @queuesize number of interrupt usb packets of
724 	 * @elementsize bytes each. @buffer must be atleast @queuesize *
725 	 * @elementsize bytes.
726 	 *
727 	 * Note some controllers only support a queuesize of 1.
728 	 *
729 	 * @interval: Interrupt interval
730 	 *
731 	 * @return A pointer to the created interrupt queue or NULL on error
732 	 */
733 	struct int_queue * (*create_int_queue)(struct udevice *bus,
734 				struct usb_device *udev, unsigned long pipe,
735 				int queuesize, int elementsize, void *buffer,
736 				int interval);
737 
738 	/**
739 	 * poll_int_queue() - Poll an interrupt queue for completed packets
740 	 *
741 	 * Poll an interrupt queue for completed packets. The return value
742 	 * points to the part of the buffer passed to create_int_queue()
743 	 * corresponding to the completed packet.
744 	 *
745 	 * @queue: queue to poll
746 	 *
747 	 * @return Pointer to the data of the first completed packet, or
748 	 *         NULL if no packets are ready
749 	 */
750 	void * (*poll_int_queue)(struct udevice *bus, struct usb_device *udev,
751 				 struct int_queue *queue);
752 
753 	/**
754 	 * destroy_int_queue() - Destroy an interrupt queue
755 	 *
756 	 * Destroy an interrupt queue created by create_int_queue().
757 	 *
758 	 * @queue: queue to poll
759 	 *
760 	 * @return 0 if OK, -ve on error
761 	 */
762 	int (*destroy_int_queue)(struct udevice *bus, struct usb_device *udev,
763 				 struct int_queue *queue);
764 
765 	/**
766 	 * alloc_device() - Allocate a new device context (XHCI)
767 	 *
768 	 * Before sending packets to a new device on an XHCI bus, a device
769 	 * context must be created. If this method is not NULL it will be
770 	 * called before the device is enumerated (even before its descriptor
771 	 * is read). This should be NULL for EHCI, which does not need this.
772 	 */
773 	int (*alloc_device)(struct udevice *bus, struct usb_device *udev);
774 
775 	/**
776 	 * reset_root_port() - Reset usb root port
777 	 */
778 	int (*reset_root_port)(struct udevice *bus, struct usb_device *udev);
779 
780 	/**
781 	 * update_hub_device() - Update HCD's internal representation of hub
782 	 *
783 	 * After a hub descriptor is fetched, notify HCD so that its internal
784 	 * representation of this hub can be updated (xHCI)
785 	 */
786 	int (*update_hub_device)(struct udevice *bus, struct usb_device *udev);
787 
788 	/**
789 	 * get_max_xfer_size() - Get HCD's maximum transfer bytes
790 	 *
791 	 * The HCD may have limitation on the maximum bytes to be transferred
792 	 * in a USB transfer. USB class driver needs to be aware of this.
793 	 */
794 	int (*get_max_xfer_size)(struct udevice *bus, size_t *size);
795 
796 	/**
797 	 * lock_async() - Keep async schedule after a transfer
798 	 *
799 	 * It may be desired to keep the asynchronous schedule running even
800 	 * after a transfer finishes, usually when doing multiple transfers
801 	 * back-to-back. This callback allows signalling the USB controller
802 	 * driver to do just that.
803 	 */
804 	int (*lock_async)(struct udevice *udev, int lock);
805 };
806 
807 #define usb_get_ops(dev)	((struct dm_usb_ops *)(dev)->driver->ops)
808 #define usb_get_emul_ops(dev)	((struct dm_usb_ops *)(dev)->driver->ops)
809 
810 /**
811  * usb_get_dev_index() - look up a device index number
812  *
813  * Look up devices using their index number (starting at 0). This works since
814  * in U-Boot device addresses are allocated starting at 1 with no gaps.
815  *
816  * TODO(sjg@chromium.org): Remove this function when usb_ether.c is modified
817  * to work better with driver model.
818  *
819  * @bus:	USB bus to check
820  * @index:	Index number of device to find (0=first). This is just the
821  *		device address less 1.
822  */
823 struct usb_device *usb_get_dev_index(struct udevice *bus, int index);
824 
825 /**
826  * usb_setup_device() - set up a device ready for use
827  *
828  * @dev:	USB device pointer. This need not be a real device - it is
829  *		common for it to just be a local variable with its ->dev
830  *		member (i.e. @dev->dev) set to the parent device and
831  *		dev->portnr set to the port number on the hub (1=first)
832  * @do_read:	true to read the device descriptor before an address is set
833  *		(should be false for XHCI buses, true otherwise)
834  * @parent:	Parent device (either UCLASS_USB or UCLASS_USB_HUB)
835  * @return 0 if OK, -ve on error */
836 int usb_setup_device(struct usb_device *dev, bool do_read,
837 		     struct usb_device *parent);
838 
839 /**
840  * usb_hub_is_root_hub() - Test whether a hub device is root hub or not
841  *
842  * @hub:	USB hub device to test
843  * @return:	true if the hub device is root hub, false otherwise.
844  */
845 bool usb_hub_is_root_hub(struct udevice *hub);
846 
847 /**
848  * usb_hub_scan() - Scan a hub and find its devices
849  *
850  * @hub:	Hub device to scan
851  */
852 int usb_hub_scan(struct udevice *hub);
853 
854 /**
855  * usb_scan_device() - Scan a device on a bus
856  *
857  * Scan a device on a bus. It has already been detected and is ready to
858  * be enumerated. This may be either the root hub (@parent is a bus) or a
859  * normal device (@parent is a hub)
860  *
861  * @parent:	Parent device
862  * @port:	Hub port number (numbered from 1)
863  * @speed:	USB speed to use for this device
864  * @devp:	Returns pointer to device if all is well
865  * @return 0 if OK, -ve on error
866  */
867 int usb_scan_device(struct udevice *parent, int port,
868 		    enum usb_device_speed speed, struct udevice **devp);
869 
870 /**
871  * usb_get_bus() - Find the bus for a device
872  *
873  * Search up through parents to find the bus this device is connected to. This
874  * will be a device with uclass UCLASS_USB.
875  *
876  * @dev:	Device to check
877  * @return The bus, or NULL if not found (this indicates a critical error in
878  *	the USB stack
879  */
880 struct udevice *usb_get_bus(struct udevice *dev);
881 
882 /**
883  * usb_select_config() - Set up a device ready for use
884  *
885  * This function assumes that the device already has an address and a driver
886  * bound, and is ready to be set up.
887  *
888  * This re-reads the device and configuration descriptors and sets the
889  * configuration
890  *
891  * @dev:	Device to set up
892  */
893 int usb_select_config(struct usb_device *dev);
894 
895 /**
896  * usb_child_pre_probe() - Pre-probe function for USB devices
897  *
898  * This is called on all children of hubs and USB controllers (i.e. UCLASS_USB
899  * and UCLASS_USB_HUB) when a new device is about to be probed. It sets up the
900  * device from the saved platform data and calls usb_select_config() to
901  * finish set up.
902  *
903  * Once this is done, the device's normal driver can take over, knowing the
904  * device is accessible on the USB bus.
905  *
906  * This function is for use only by the internal USB stack.
907  *
908  * @dev:	Device to set up
909  */
910 int usb_child_pre_probe(struct udevice *dev);
911 
912 struct ehci_ctrl;
913 
914 /**
915  * usb_setup_ehci_gadget() - Set up a USB device as a gadget
916  *
917  * TODO(sjg@chromium.org): Tidy this up when USB gadgets can use driver model
918  *
919  * This provides a way to tell a controller to start up as a USB device
920  * instead of as a host. It is untested.
921  */
922 int usb_setup_ehci_gadget(struct ehci_ctrl **ctlrp);
923 
924 /**
925  * usb_remove_ehci_gadget() - Remove a gadget USB device
926  *
927  * TODO(sjg@chromium.org): Tidy this up when USB gadgets can use driver model
928  *
929  * This provides a way to tell a controller to remove a USB device
930  */
931 int usb_remove_ehci_gadget(struct ehci_ctrl **ctlrp);
932 
933 /**
934  * usb_stor_reset() - Prepare to scan USB storage devices
935  *
936  * Empty the list of USB storage devices in preparation for scanning them.
937  * This must be called before a USB scan.
938  */
939 void usb_stor_reset(void);
940 
941 #else /* !CONFIG_IS_ENABLED(DM_USB) */
942 
943 struct usb_device *usb_get_dev_index(int index);
944 
945 #endif
946 
947 bool usb_device_has_child_on_port(struct usb_device *parent, int port);
948 
949 int usb_hub_probe(struct usb_device *dev, int ifnum);
950 void usb_hub_reset(void);
951 
952 /*
953  * usb_find_usb2_hub_address_port() - Get hub address and port for TT setting
954  *
955  * Searches for the first HS hub above the given device. If a
956  * HS hub is found, the hub address and the port the device is
957  * connected to is return, as required for SPLIT transactions
958  *
959  * @param: udev full speed or low speed device
960  */
961 void usb_find_usb2_hub_address_port(struct usb_device *udev,
962 				    uint8_t *hub_address, uint8_t *hub_port);
963 
964 /**
965  * usb_alloc_new_device() - Allocate a new device
966  *
967  * @devp: returns a pointer of a new device structure. With driver model this
968  *		is a device pointer, but with legacy USB this pointer is
969  *		driver-specific.
970  * @return 0 if OK, -ENOSPC if we have found out of room for new devices
971  */
972 int usb_alloc_new_device(struct udevice *controller, struct usb_device **devp);
973 
974 /**
975  * usb_free_device() - Free a partially-inited device
976  *
977  * This is an internal function. It is used to reverse the action of
978  * usb_alloc_new_device() when we hit a problem during init.
979  */
980 void usb_free_device(struct udevice *controller);
981 
982 int usb_new_device(struct usb_device *dev);
983 
984 int usb_alloc_device(struct usb_device *dev);
985 
986 /**
987  * usb_update_hub_device() - Update HCD's internal representation of hub
988  *
989  * After a hub descriptor is fetched, notify HCD so that its internal
990  * representation of this hub can be updated.
991  *
992  * @dev:		Hub device
993  * @return 0 if OK, -ve on error
994  */
995 int usb_update_hub_device(struct usb_device *dev);
996 
997 /**
998  * usb_get_max_xfer_size() - Get HCD's maximum transfer bytes
999  *
1000  * The HCD may have limitation on the maximum bytes to be transferred
1001  * in a USB transfer. USB class driver needs to be aware of this.
1002  *
1003  * @dev:		USB device
1004  * @size:		maximum transfer bytes
1005  * @return 0 if OK, -ve on error
1006  */
1007 int usb_get_max_xfer_size(struct usb_device *dev, size_t *size);
1008 
1009 /**
1010  * usb_emul_setup_device() - Set up a new USB device emulation
1011  *
1012  * This is normally called when a new emulation device is bound. It tells
1013  * the USB emulation uclass about the features of the emulator.
1014  *
1015  * @dev:		Emulation device
1016  * @strings:		List of USB string descriptors, terminated by a NULL
1017  *			entry
1018  * @desc_list:		List of points or USB descriptors, terminated by NULL.
1019  *			The first entry must be struct usb_device_descriptor,
1020  *			and others follow on after that.
1021  * @return 0 if OK, -ENOSYS if not implemented, other -ve on error
1022  */
1023 int usb_emul_setup_device(struct udevice *dev, struct usb_string *strings,
1024 			  void **desc_list);
1025 
1026 /**
1027  * usb_emul_control() - Send a control packet to an emulator
1028  *
1029  * @emul:	Emulator device
1030  * @udev:	USB device (which the emulator is causing to appear)
1031  * See struct dm_usb_ops for details on other parameters
1032  * @return 0 if OK, -ve on error
1033  */
1034 int usb_emul_control(struct udevice *emul, struct usb_device *udev,
1035 		     unsigned long pipe, void *buffer, int length,
1036 		     struct devrequest *setup);
1037 
1038 /**
1039  * usb_emul_bulk() - Send a bulk packet to an emulator
1040  *
1041  * @emul:	Emulator device
1042  * @udev:	USB device (which the emulator is causing to appear)
1043  * See struct dm_usb_ops for details on other parameters
1044  * @return 0 if OK, -ve on error
1045  */
1046 int usb_emul_bulk(struct udevice *emul, struct usb_device *udev,
1047 		  unsigned long pipe, void *buffer, int length);
1048 
1049 /**
1050  * usb_emul_int() - Send an interrupt packet to an emulator
1051  *
1052  * @emul:	Emulator device
1053  * @udev:	USB device (which the emulator is causing to appear)
1054  * See struct dm_usb_ops for details on other parameters
1055  * @return 0 if OK, -ve on error
1056  */
1057 int usb_emul_int(struct udevice *emul, struct usb_device *udev,
1058 		  unsigned long pipe, void *buffer, int length, int interval,
1059 		  bool nonblock);
1060 
1061 /**
1062  * usb_emul_find() - Find an emulator for a particular device
1063  *
1064  * Check @pipe and @port1 to find a device number on bus @bus and return it.
1065  *
1066  * @bus:	USB bus (controller)
1067  * @pipe:	Describes pipe being used, and includes the device number
1068  * @port1:	Describes port number on the parent hub
1069  * @emulp:	Returns pointer to emulator, or NULL if not found
1070  * @return 0 if found, -ve on error
1071  */
1072 int usb_emul_find(struct udevice *bus, ulong pipe, int port1,
1073 		  struct udevice **emulp);
1074 
1075 /**
1076  * usb_emul_find_for_dev() - Find an emulator for a particular device
1077  *
1078  * @dev:	USB device to check
1079  * @emulp:	Returns pointer to emulator, or NULL if not found
1080  * @return 0 if found, -ve on error
1081  */
1082 int usb_emul_find_for_dev(struct udevice *dev, struct udevice **emulp);
1083 
1084 /**
1085  * usb_emul_find_descriptor() - Find a USB descriptor of a particular device
1086  *
1087  * @ptr:	a pointer to a list of USB descriptor pointers
1088  * @type:	type of USB descriptor to find
1089  * @index:	if @type is USB_DT_CONFIG, this is the configuration value
1090  * @return a pointer to the USB descriptor found, NULL if not found
1091  */
1092 struct usb_generic_descriptor **usb_emul_find_descriptor(
1093 		struct usb_generic_descriptor **ptr, int type, int index);
1094 
1095 /**
1096  * usb_show_tree() - show the USB device tree
1097  *
1098  * This shows a list of active USB devices along with basic information about
1099  * each.
1100  */
1101 void usb_show_tree(void);
1102 
1103 #endif /*_USB_H_ */
1104