usb.h 35 KB

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