usb.h 33 KB

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