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