usb.h 35 KB

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