usb.h 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913
  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_parentdata(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; /* transfered 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; /* transfered 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. #if defined(CONFIG_USB_UHCI) || defined(CONFIG_USB_OHCI) || \
  154. defined(CONFIG_USB_EHCI) || defined(CONFIG_USB_OHCI_NEW) || \
  155. defined(CONFIG_USB_SL811HS) || defined(CONFIG_USB_ISP116X_HCD) || \
  156. defined(CONFIG_USB_R8A66597_HCD) || defined(CONFIG_USB_DAVINCI) || \
  157. defined(CONFIG_USB_OMAP3) || defined(CONFIG_USB_DA8XX) || \
  158. defined(CONFIG_USB_BLACKFIN) || defined(CONFIG_USB_AM35X) || \
  159. defined(CONFIG_USB_MUSB_DSPS) || defined(CONFIG_USB_MUSB_AM35X) || \
  160. defined(CONFIG_USB_MUSB_OMAP2PLUS) || defined(CONFIG_USB_MUSB_SUNXI) || \
  161. defined(CONFIG_USB_XHCI) || defined(CONFIG_USB_DWC2) || \
  162. defined(CONFIG_USB_EMUL)
  163. int usb_lowlevel_init(int index, enum usb_init_type init, void **controller);
  164. int usb_lowlevel_stop(int index);
  165. #if defined(CONFIG_MUSB_HOST) || defined(CONFIG_DM_USB)
  166. int usb_reset_root_port(void);
  167. #else
  168. #define usb_reset_root_port()
  169. #endif
  170. int submit_bulk_msg(struct usb_device *dev, unsigned long pipe,
  171. void *buffer, int transfer_len);
  172. int submit_control_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
  173. int transfer_len, struct devrequest *setup);
  174. int submit_int_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
  175. int transfer_len, int interval);
  176. #if defined CONFIG_USB_EHCI || defined CONFIG_MUSB_HOST
  177. struct int_queue *create_int_queue(struct usb_device *dev, unsigned long pipe,
  178. int queuesize, int elementsize, void *buffer, int interval);
  179. int destroy_int_queue(struct usb_device *dev, struct int_queue *queue);
  180. void *poll_int_queue(struct usb_device *dev, struct int_queue *queue);
  181. #endif
  182. /* Defines */
  183. #define USB_UHCI_VEND_ID 0x8086
  184. #define USB_UHCI_DEV_ID 0x7112
  185. /*
  186. * PXA25x can only act as USB device. There are drivers
  187. * which works with USB CDC gadgets implementations.
  188. * Some of them have common routines which can be used
  189. * in boards init functions e.g. udc_disconnect() used for
  190. * forced device disconnection from host.
  191. */
  192. #elif defined(CONFIG_USB_GADGET_PXA2XX)
  193. extern void udc_disconnect(void);
  194. #endif
  195. /*
  196. * board-specific hardware initialization, called by
  197. * usb drivers and u-boot commands
  198. *
  199. * @param index USB controller number
  200. * @param init initializes controller as USB host or device
  201. */
  202. int board_usb_init(int index, enum usb_init_type init);
  203. /*
  204. * can be used to clean up after failed USB initialization attempt
  205. * vide: board_usb_init()
  206. *
  207. * @param index USB controller number for selective cleanup
  208. * @param init usb_init_type passed to board_usb_init()
  209. */
  210. int board_usb_cleanup(int index, enum usb_init_type init);
  211. #ifdef CONFIG_USB_STORAGE
  212. #define USB_MAX_STOR_DEV 5
  213. block_dev_desc_t *usb_stor_get_dev(int index);
  214. int usb_stor_scan(int mode);
  215. int usb_stor_info(void);
  216. #endif
  217. #ifdef CONFIG_USB_HOST_ETHER
  218. #define USB_MAX_ETH_DEV 5
  219. int usb_host_eth_scan(int mode);
  220. #endif
  221. #ifdef CONFIG_USB_KEYBOARD
  222. int drv_usb_kbd_init(void);
  223. int usb_kbd_deregister(int force);
  224. #endif
  225. /* routines */
  226. int usb_init(void); /* initialize the USB Controller */
  227. int usb_stop(void); /* stop the USB Controller */
  228. int usb_set_protocol(struct usb_device *dev, int ifnum, int protocol);
  229. int usb_set_idle(struct usb_device *dev, int ifnum, int duration,
  230. int report_id);
  231. int usb_control_msg(struct usb_device *dev, unsigned int pipe,
  232. unsigned char request, unsigned char requesttype,
  233. unsigned short value, unsigned short index,
  234. void *data, unsigned short size, int timeout);
  235. int usb_bulk_msg(struct usb_device *dev, unsigned int pipe,
  236. void *data, int len, int *actual_length, int timeout);
  237. int usb_submit_int_msg(struct usb_device *dev, unsigned long pipe,
  238. void *buffer, int transfer_len, int interval);
  239. int usb_disable_asynch(int disable);
  240. int usb_maxpacket(struct usb_device *dev, unsigned long pipe);
  241. int usb_get_configuration_no(struct usb_device *dev, unsigned char *buffer,
  242. 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. /* big endian -> little endian conversion */
  252. /* some CPUs are already little endian e.g. the ARM920T */
  253. #define __swap_16(x) \
  254. ({ unsigned short x_ = (unsigned short)x; \
  255. (unsigned short)( \
  256. ((x_ & 0x00FFU) << 8) | ((x_ & 0xFF00U) >> 8)); \
  257. })
  258. #define __swap_32(x) \
  259. ({ unsigned long x_ = (unsigned long)x; \
  260. (unsigned long)( \
  261. ((x_ & 0x000000FFUL) << 24) | \
  262. ((x_ & 0x0000FF00UL) << 8) | \
  263. ((x_ & 0x00FF0000UL) >> 8) | \
  264. ((x_ & 0xFF000000UL) >> 24)); \
  265. })
  266. #ifdef __LITTLE_ENDIAN
  267. # define swap_16(x) (x)
  268. # define swap_32(x) (x)
  269. #else
  270. # define swap_16(x) __swap_16(x)
  271. # define swap_32(x) __swap_32(x)
  272. #endif
  273. /*
  274. * Calling this entity a "pipe" is glorifying it. A USB pipe
  275. * is something embarrassingly simple: it basically consists
  276. * of the following information:
  277. * - device number (7 bits)
  278. * - endpoint number (4 bits)
  279. * - current Data0/1 state (1 bit)
  280. * - direction (1 bit)
  281. * - speed (2 bits)
  282. * - max packet size (2 bits: 8, 16, 32 or 64)
  283. * - pipe type (2 bits: control, interrupt, bulk, isochronous)
  284. *
  285. * That's 18 bits. Really. Nothing more. And the USB people have
  286. * documented these eighteen bits as some kind of glorious
  287. * virtual data structure.
  288. *
  289. * Let's not fall in that trap. We'll just encode it as a simple
  290. * unsigned int. The encoding is:
  291. *
  292. * - max size: bits 0-1 (00 = 8, 01 = 16, 10 = 32, 11 = 64)
  293. * - direction: bit 7 (0 = Host-to-Device [Out],
  294. * (1 = Device-to-Host [In])
  295. * - device: bits 8-14
  296. * - endpoint: bits 15-18
  297. * - Data0/1: bit 19
  298. * - pipe type: bits 30-31 (00 = isochronous, 01 = interrupt,
  299. * 10 = control, 11 = bulk)
  300. *
  301. * Why? Because it's arbitrary, and whatever encoding we select is really
  302. * up to us. This one happens to share a lot of bit positions with the UHCI
  303. * specification, so that much of the uhci driver can just mask the bits
  304. * appropriately.
  305. */
  306. /* Create various pipes... */
  307. #define create_pipe(dev,endpoint) \
  308. (((dev)->devnum << 8) | ((endpoint) << 15) | \
  309. (dev)->maxpacketsize)
  310. #define default_pipe(dev) ((dev)->speed << 26)
  311. #define usb_sndctrlpipe(dev, endpoint) ((PIPE_CONTROL << 30) | \
  312. create_pipe(dev, endpoint))
  313. #define usb_rcvctrlpipe(dev, endpoint) ((PIPE_CONTROL << 30) | \
  314. create_pipe(dev, endpoint) | \
  315. USB_DIR_IN)
  316. #define usb_sndisocpipe(dev, endpoint) ((PIPE_ISOCHRONOUS << 30) | \
  317. create_pipe(dev, endpoint))
  318. #define usb_rcvisocpipe(dev, endpoint) ((PIPE_ISOCHRONOUS << 30) | \
  319. create_pipe(dev, endpoint) | \
  320. USB_DIR_IN)
  321. #define usb_sndbulkpipe(dev, endpoint) ((PIPE_BULK << 30) | \
  322. create_pipe(dev, endpoint))
  323. #define usb_rcvbulkpipe(dev, endpoint) ((PIPE_BULK << 30) | \
  324. create_pipe(dev, endpoint) | \
  325. USB_DIR_IN)
  326. #define usb_sndintpipe(dev, endpoint) ((PIPE_INTERRUPT << 30) | \
  327. create_pipe(dev, endpoint))
  328. #define usb_rcvintpipe(dev, endpoint) ((PIPE_INTERRUPT << 30) | \
  329. create_pipe(dev, endpoint) | \
  330. USB_DIR_IN)
  331. #define usb_snddefctrl(dev) ((PIPE_CONTROL << 30) | \
  332. default_pipe(dev))
  333. #define usb_rcvdefctrl(dev) ((PIPE_CONTROL << 30) | \
  334. default_pipe(dev) | \
  335. USB_DIR_IN)
  336. /* The D0/D1 toggle bits */
  337. #define usb_gettoggle(dev, ep, out) (((dev)->toggle[out] >> ep) & 1)
  338. #define usb_dotoggle(dev, ep, out) ((dev)->toggle[out] ^= (1 << ep))
  339. #define usb_settoggle(dev, ep, out, bit) ((dev)->toggle[out] = \
  340. ((dev)->toggle[out] & \
  341. ~(1 << ep)) | ((bit) << ep))
  342. /* Endpoint halt control/status */
  343. #define usb_endpoint_out(ep_dir) (((ep_dir >> 7) & 1) ^ 1)
  344. #define usb_endpoint_halt(dev, ep, out) ((dev)->halted[out] |= (1 << (ep)))
  345. #define usb_endpoint_running(dev, ep, out) ((dev)->halted[out] &= ~(1 << (ep)))
  346. #define usb_endpoint_halted(dev, ep, out) ((dev)->halted[out] & (1 << (ep)))
  347. #define usb_packetid(pipe) (((pipe) & USB_DIR_IN) ? USB_PID_IN : \
  348. USB_PID_OUT)
  349. #define usb_pipeout(pipe) ((((pipe) >> 7) & 1) ^ 1)
  350. #define usb_pipein(pipe) (((pipe) >> 7) & 1)
  351. #define usb_pipedevice(pipe) (((pipe) >> 8) & 0x7f)
  352. #define usb_pipe_endpdev(pipe) (((pipe) >> 8) & 0x7ff)
  353. #define usb_pipeendpoint(pipe) (((pipe) >> 15) & 0xf)
  354. #define usb_pipedata(pipe) (((pipe) >> 19) & 1)
  355. #define usb_pipetype(pipe) (((pipe) >> 30) & 3)
  356. #define usb_pipeisoc(pipe) (usb_pipetype((pipe)) == PIPE_ISOCHRONOUS)
  357. #define usb_pipeint(pipe) (usb_pipetype((pipe)) == PIPE_INTERRUPT)
  358. #define usb_pipecontrol(pipe) (usb_pipetype((pipe)) == PIPE_CONTROL)
  359. #define usb_pipebulk(pipe) (usb_pipetype((pipe)) == PIPE_BULK)
  360. #define usb_pipe_ep_index(pipe) \
  361. usb_pipecontrol(pipe) ? (usb_pipeendpoint(pipe) * 2) : \
  362. ((usb_pipeendpoint(pipe) * 2) - \
  363. (usb_pipein(pipe) ? 0 : 1))
  364. /**
  365. * struct usb_device_id - identifies USB devices for probing and hotplugging
  366. * @match_flags: Bit mask controlling which of the other fields are used to
  367. * match against new devices. Any field except for driver_info may be
  368. * used, although some only make sense in conjunction with other fields.
  369. * This is usually set by a USB_DEVICE_*() macro, which sets all
  370. * other fields in this structure except for driver_info.
  371. * @idVendor: USB vendor ID for a device; numbers are assigned
  372. * by the USB forum to its members.
  373. * @idProduct: Vendor-assigned product ID.
  374. * @bcdDevice_lo: Low end of range of vendor-assigned product version numbers.
  375. * This is also used to identify individual product versions, for
  376. * a range consisting of a single device.
  377. * @bcdDevice_hi: High end of version number range. The range of product
  378. * versions is inclusive.
  379. * @bDeviceClass: Class of device; numbers are assigned
  380. * by the USB forum. Products may choose to implement classes,
  381. * or be vendor-specific. Device classes specify behavior of all
  382. * the interfaces on a device.
  383. * @bDeviceSubClass: Subclass of device; associated with bDeviceClass.
  384. * @bDeviceProtocol: Protocol of device; associated with bDeviceClass.
  385. * @bInterfaceClass: Class of interface; numbers are assigned
  386. * by the USB forum. Products may choose to implement classes,
  387. * or be vendor-specific. Interface classes specify behavior only
  388. * of a given interface; other interfaces may support other classes.
  389. * @bInterfaceSubClass: Subclass of interface; associated with bInterfaceClass.
  390. * @bInterfaceProtocol: Protocol of interface; associated with bInterfaceClass.
  391. * @bInterfaceNumber: Number of interface; composite devices may use
  392. * fixed interface numbers to differentiate between vendor-specific
  393. * interfaces.
  394. * @driver_info: Holds information used by the driver. Usually it holds
  395. * a pointer to a descriptor understood by the driver, or perhaps
  396. * device flags.
  397. *
  398. * In most cases, drivers will create a table of device IDs by using
  399. * USB_DEVICE(), or similar macros designed for that purpose.
  400. * They will then export it to userspace using MODULE_DEVICE_TABLE(),
  401. * and provide it to the USB core through their usb_driver structure.
  402. *
  403. * See the usb_match_id() function for information about how matches are
  404. * performed. Briefly, you will normally use one of several macros to help
  405. * construct these entries. Each entry you provide will either identify
  406. * one or more specific products, or will identify a class of products
  407. * which have agreed to behave the same. You should put the more specific
  408. * matches towards the beginning of your table, so that driver_info can
  409. * record quirks of specific products.
  410. */
  411. struct usb_device_id {
  412. /* which fields to match against? */
  413. u16 match_flags;
  414. /* Used for product specific matches; range is inclusive */
  415. u16 idVendor;
  416. u16 idProduct;
  417. u16 bcdDevice_lo;
  418. u16 bcdDevice_hi;
  419. /* Used for device class matches */
  420. u8 bDeviceClass;
  421. u8 bDeviceSubClass;
  422. u8 bDeviceProtocol;
  423. /* Used for interface class matches */
  424. u8 bInterfaceClass;
  425. u8 bInterfaceSubClass;
  426. u8 bInterfaceProtocol;
  427. /* Used for vendor-specific interface matches */
  428. u8 bInterfaceNumber;
  429. /* not matched against */
  430. ulong driver_info;
  431. };
  432. /* Some useful macros to use to create struct usb_device_id */
  433. #define USB_DEVICE_ID_MATCH_VENDOR 0x0001
  434. #define USB_DEVICE_ID_MATCH_PRODUCT 0x0002
  435. #define USB_DEVICE_ID_MATCH_DEV_LO 0x0004
  436. #define USB_DEVICE_ID_MATCH_DEV_HI 0x0008
  437. #define USB_DEVICE_ID_MATCH_DEV_CLASS 0x0010
  438. #define USB_DEVICE_ID_MATCH_DEV_SUBCLASS 0x0020
  439. #define USB_DEVICE_ID_MATCH_DEV_PROTOCOL 0x0040
  440. #define USB_DEVICE_ID_MATCH_INT_CLASS 0x0080
  441. #define USB_DEVICE_ID_MATCH_INT_SUBCLASS 0x0100
  442. #define USB_DEVICE_ID_MATCH_INT_PROTOCOL 0x0200
  443. #define USB_DEVICE_ID_MATCH_INT_NUMBER 0x0400
  444. /* Match anything, indicates this is a valid entry even if everything is 0 */
  445. #define USB_DEVICE_ID_MATCH_NONE 0x0800
  446. #define USB_DEVICE_ID_MATCH_ALL 0x07ff
  447. /**
  448. * struct usb_driver_entry - Matches a driver to its usb_device_ids
  449. * @compatible: Compatible string
  450. * @data: Data for this compatible string
  451. */
  452. struct usb_driver_entry {
  453. struct driver *driver;
  454. const struct usb_device_id *match;
  455. };
  456. #define USB_DEVICE(__name, __match) \
  457. ll_entry_declare(struct usb_driver_entry, __name, usb_driver_entry) = {\
  458. .driver = llsym(struct driver, __name, driver), \
  459. .match = __match, \
  460. }
  461. /*************************************************************************
  462. * Hub Stuff
  463. */
  464. struct usb_port_status {
  465. unsigned short wPortStatus;
  466. unsigned short wPortChange;
  467. } __attribute__ ((packed));
  468. struct usb_hub_status {
  469. unsigned short wHubStatus;
  470. unsigned short wHubChange;
  471. } __attribute__ ((packed));
  472. /* Hub descriptor */
  473. struct usb_hub_descriptor {
  474. unsigned char bLength;
  475. unsigned char bDescriptorType;
  476. unsigned char bNbrPorts;
  477. unsigned short wHubCharacteristics;
  478. unsigned char bPwrOn2PwrGood;
  479. unsigned char bHubContrCurrent;
  480. unsigned char DeviceRemovable[(USB_MAXCHILDREN+1+7)/8];
  481. unsigned char PortPowerCtrlMask[(USB_MAXCHILDREN+1+7)/8];
  482. /* DeviceRemovable and PortPwrCtrlMask want to be variable-length
  483. bitmaps that hold max 255 entries. (bit0 is ignored) */
  484. } __attribute__ ((packed));
  485. struct usb_hub_device {
  486. struct usb_device *pusb_dev;
  487. struct usb_hub_descriptor desc;
  488. };
  489. #ifdef CONFIG_DM_USB
  490. /**
  491. * struct usb_platdata - Platform data about a USB controller
  492. *
  493. * Given a USB controller (UCLASS_USB) dev this is dev_get_platdata(dev)
  494. */
  495. struct usb_platdata {
  496. enum usb_init_type init_type;
  497. };
  498. /**
  499. * struct usb_dev_platdata - Platform data about a USB device
  500. *
  501. * Given a USB device dev this structure is dev_get_parent_platdata(dev).
  502. * This is used by sandbox to provide emulation data also.
  503. *
  504. * @id: ID used to match this device
  505. * @speed: Stores the speed associated with a USB device
  506. * @devnum: Device address on the USB bus
  507. * @slot_id: USB3 slot ID, which is separate from the device address
  508. * @portnr: Port number of this device on its parent hub, numbered from 1
  509. * (0 mean this device is the root hub)
  510. * @strings: List of descriptor strings (for sandbox emulation purposes)
  511. * @desc_list: List of descriptors (for sandbox emulation purposes)
  512. */
  513. struct usb_dev_platdata {
  514. struct usb_device_id id;
  515. enum usb_device_speed speed;
  516. int devnum;
  517. int slot_id;
  518. int portnr; /* Hub port number, 1..n */
  519. #ifdef CONFIG_SANDBOX
  520. struct usb_string *strings;
  521. /* NULL-terminated list of descriptor pointers */
  522. struct usb_generic_descriptor **desc_list;
  523. #endif
  524. int configno;
  525. };
  526. /**
  527. * struct usb_bus_priv - information about the USB controller
  528. *
  529. * Given a USB controller (UCLASS_USB) 'dev', this is
  530. * dev_get_uclass_priv(dev).
  531. *
  532. * @next_addr: Next device address to allocate minus 1. Incremented by 1
  533. * each time a new device address is set, so this holds the
  534. * number of devices on the bus
  535. * @desc_before_addr: true if we can read a device descriptor before it
  536. * has been assigned an address. For XHCI this is not possible
  537. * so this will be false.
  538. */
  539. struct usb_bus_priv {
  540. int next_addr;
  541. bool desc_before_addr;
  542. };
  543. /**
  544. * struct dm_usb_ops - USB controller operations
  545. *
  546. * This defines the operations supoorted on a USB controller. Common
  547. * arguments are:
  548. *
  549. * @bus: USB bus (i.e. controller), which is in UCLASS_USB.
  550. * @udev: USB device parent data. Controllers are not expected to need
  551. * this, since the device address on the bus is encoded in @pipe.
  552. * It is used for sandbox, and can be handy for debugging and
  553. * logging.
  554. * @pipe: An assortment of bitfields which provide address and packet
  555. * type information. See create_pipe() above for encoding
  556. * details
  557. * @buffer: A buffer to use for sending/receiving. This should be
  558. * DMA-aligned.
  559. * @length: Buffer length in bytes
  560. */
  561. struct dm_usb_ops {
  562. /**
  563. * control() - Send a control message
  564. *
  565. * Most parameters are as above.
  566. *
  567. * @setup: Additional setup information required by the message
  568. */
  569. int (*control)(struct udevice *bus, struct usb_device *udev,
  570. unsigned long pipe, void *buffer, int length,
  571. struct devrequest *setup);
  572. /**
  573. * bulk() - Send a bulk message
  574. *
  575. * Parameters are as above.
  576. */
  577. int (*bulk)(struct udevice *bus, struct usb_device *udev,
  578. unsigned long pipe, void *buffer, int length);
  579. /**
  580. * interrupt() - Send an interrupt message
  581. *
  582. * Most parameters are as above.
  583. *
  584. * @interval: Interrupt interval
  585. */
  586. int (*interrupt)(struct udevice *bus, struct usb_device *udev,
  587. unsigned long pipe, void *buffer, int length,
  588. int interval);
  589. /**
  590. * alloc_device() - Allocate a new device context (XHCI)
  591. *
  592. * Before sending packets to a new device on an XHCI bus, a device
  593. * context must be created. If this method is not NULL it will be
  594. * called before the device is enumerated (even before its descriptor
  595. * is read). This should be NULL for EHCI, which does not need this.
  596. */
  597. int (*alloc_device)(struct udevice *bus, struct usb_device *udev);
  598. };
  599. #define usb_get_ops(dev) ((struct dm_usb_ops *)(dev)->driver->ops)
  600. #define usb_get_emul_ops(dev) ((struct dm_usb_ops *)(dev)->driver->ops)
  601. #ifdef CONFIG_MUSB_HOST
  602. int usb_reset_root_port(void);
  603. #endif
  604. /**
  605. * usb_get_dev_index() - look up a device index number
  606. *
  607. * Look up devices using their index number (starting at 0). This works since
  608. * in U-Boot device addresses are allocated starting at 1 with no gaps.
  609. *
  610. * TODO(sjg@chromium.org): Remove this function when usb_ether.c is modified
  611. * to work better with driver model.
  612. *
  613. * @bus: USB bus to check
  614. * @index: Index number of device to find (0=first). This is just the
  615. * device address less 1.
  616. */
  617. struct usb_device *usb_get_dev_index(struct udevice *bus, int index);
  618. /**
  619. * usb_legacy_port_reset() - Legacy function to reset a hub port
  620. *
  621. * @hub: Hub device
  622. * @portnr: Port number (1=first)
  623. */
  624. int usb_legacy_port_reset(struct usb_device *hub, int portnr);
  625. /**
  626. * usb_setup_device() - set up a device ready for use
  627. *
  628. * @dev: USB device pointer. This need not be a real device - it is
  629. * common for it to just be a local variable with its ->dev
  630. * member (i.e. @dev->dev) set to the parent device
  631. * @do_read: true to read the device descriptor before an address is set
  632. * (should be false for XHCI buses, true otherwise)
  633. * @parent: Parent device (either UCLASS_USB or UCLASS_USB_HUB)
  634. * @portnr: Port number on hub (1=first) or 0 for none
  635. * @return 0 if OK, -ve on error */
  636. int usb_setup_device(struct usb_device *dev, bool do_read,
  637. struct usb_device *parent, int portnr);
  638. /**
  639. * usb_hub_scan() - Scan a hub and find its devices
  640. *
  641. * @hub: Hub device to scan
  642. */
  643. int usb_hub_scan(struct udevice *hub);
  644. /**
  645. * usb_scan_device() - Scan a device on a bus
  646. *
  647. * Scan a device on a bus. It has already been detected and is ready to
  648. * be enumerated. This may be either the root hub (@parent is a bus) or a
  649. * normal device (@parent is a hub)
  650. *
  651. * @parent: Parent device
  652. * @port: Hub port number (numbered from 1)
  653. * @speed: USB speed to use for this device
  654. * @devp: Returns pointer to device if all is well
  655. * @return 0 if OK, -ve on error
  656. */
  657. int usb_scan_device(struct udevice *parent, int port,
  658. enum usb_device_speed speed, struct udevice **devp);
  659. /**
  660. * usb_get_bus() - Find the bus for a device
  661. *
  662. * Search up through parents to find the bus this device is connected to. This
  663. * will be a device with uclass UCLASS_USB.
  664. *
  665. * @dev: Device to check
  666. * @busp: Returns bus, or NULL if not found
  667. * @return 0 if OK, -EXDEV is somehow this bus does not have a controller (this
  668. * indicates a critical error in the USB stack
  669. */
  670. int usb_get_bus(struct udevice *dev, struct udevice **busp);
  671. /**
  672. * usb_select_config() - Set up a device ready for use
  673. *
  674. * This function assumes that the device already has an address and a driver
  675. * bound, and is ready to be set up.
  676. *
  677. * This re-reads the device and configuration descriptors and sets the
  678. * configuration
  679. *
  680. * @dev: Device to set up
  681. */
  682. int usb_select_config(struct usb_device *dev);
  683. /**
  684. * usb_child_pre_probe() - Pre-probe function for USB devices
  685. *
  686. * This is called on all children of hubs and USB controllers (i.e. UCLASS_USB
  687. * and UCLASS_USB_HUB) when a new device is about to be probed. It sets up the
  688. * device from the saved platform data and calls usb_select_config() to
  689. * finish set up.
  690. *
  691. * Once this is done, the device's normal driver can take over, knowing the
  692. * device is accessible on the USB bus.
  693. *
  694. * This function is for use only by the internal USB stack.
  695. *
  696. * @dev: Device to set up
  697. */
  698. int usb_child_pre_probe(struct udevice *dev);
  699. struct ehci_ctrl;
  700. /**
  701. * usb_setup_ehci_gadget() - Set up a USB device as a gadget
  702. *
  703. * TODO(sjg@chromium.org): Tidy this up when USB gadgets can use driver model
  704. *
  705. * This provides a way to tell a controller to start up as a USB device
  706. * instead of as a host. It is untested.
  707. */
  708. int usb_setup_ehci_gadget(struct ehci_ctrl **ctlrp);
  709. /**
  710. * usb_stor_reset() - Prepare to scan USB storage devices
  711. *
  712. * Empty the list of USB storage devices in preparation for scanning them.
  713. * This must be called before a USB scan.
  714. */
  715. void usb_stor_reset(void);
  716. #else /* !CONFIG_DM_USB */
  717. struct usb_device *usb_get_dev_index(int index);
  718. #endif
  719. bool usb_device_has_child_on_port(struct usb_device *parent, int port);
  720. int usb_hub_probe(struct usb_device *dev, int ifnum);
  721. void usb_hub_reset(void);
  722. /**
  723. * legacy_hub_port_reset() - reset a port given its usb_device pointer
  724. *
  725. * Reset a hub port and see if a device is present on that port, providing
  726. * sufficient time for it to show itself. The port status is returned.
  727. *
  728. * With driver model this moves to hub_port_reset() and is passed a struct
  729. * udevice.
  730. *
  731. * @dev: USB device to reset
  732. * @port: Port number to reset (note ports are numbered from 0 here)
  733. * @portstat: Returns port status
  734. */
  735. int legacy_hub_port_reset(struct usb_device *dev, int port,
  736. unsigned short *portstat);
  737. int hub_port_reset(struct udevice *dev, int port, unsigned short *portstat);
  738. /**
  739. * usb_alloc_new_device() - Allocate a new device
  740. *
  741. * @devp: returns a pointer of a new device structure. With driver model this
  742. * is a device pointer, but with legacy USB this pointer is
  743. * driver-specific.
  744. * @return 0 if OK, -ENOSPC if we have found out of room for new devices
  745. */
  746. int usb_alloc_new_device(struct udevice *controller, struct usb_device **devp);
  747. /**
  748. * usb_free_device() - Free a partially-inited device
  749. *
  750. * This is an internal function. It is used to reverse the action of
  751. * usb_alloc_new_device() when we hit a problem during init.
  752. */
  753. void usb_free_device(struct udevice *controller);
  754. int usb_new_device(struct usb_device *dev);
  755. int usb_alloc_device(struct usb_device *dev);
  756. /**
  757. * usb_emul_setup_device() - Set up a new USB device emulation
  758. *
  759. * This is normally called when a new emulation device is bound. It tells
  760. * the USB emulation uclass about the features of the emulator.
  761. *
  762. * @dev: Emulation device
  763. * @maxpacketsize: Maximum packet size (e.g. PACKET_SIZE_64)
  764. * @strings: List of USB string descriptors, terminated by a NULL
  765. * entry
  766. * @desc_list: List of points or USB descriptors, terminated by NULL.
  767. * The first entry must be struct usb_device_descriptor,
  768. * and others follow on after that.
  769. * @return 0 if OK, -ve on error
  770. */
  771. int usb_emul_setup_device(struct udevice *dev, int maxpacketsize,
  772. struct usb_string *strings, void **desc_list);
  773. /**
  774. * usb_emul_control() - Send a control packet to an emulator
  775. *
  776. * @emul: Emulator device
  777. * @udev: USB device (which the emulator is causing to appear)
  778. * See struct dm_usb_ops for details on other parameters
  779. * @return 0 if OK, -ve on error
  780. */
  781. int usb_emul_control(struct udevice *emul, struct usb_device *udev,
  782. unsigned long pipe, void *buffer, int length,
  783. struct devrequest *setup);
  784. /**
  785. * usb_emul_bulk() - Send a bulk packet to an emulator
  786. *
  787. * @emul: Emulator device
  788. * @udev: USB device (which the emulator is causing to appear)
  789. * See struct dm_usb_ops for details on other parameters
  790. * @return 0 if OK, -ve on error
  791. */
  792. int usb_emul_bulk(struct udevice *emul, struct usb_device *udev,
  793. unsigned long pipe, void *buffer, int length);
  794. /**
  795. * usb_emul_find() - Find an emulator for a particular device
  796. *
  797. * Check @pipe to find a device number on bus @bus and return it.
  798. *
  799. * @bus: USB bus (controller)
  800. * @pipe: Describes pipe being used, and includes the device number
  801. * @emulp: Returns pointer to emulator, or NULL if not found
  802. * @return 0 if found, -ve on error
  803. */
  804. int usb_emul_find(struct udevice *bus, ulong pipe, struct udevice **emulp);
  805. /**
  806. * usb_emul_reset() - Reset all emulators ready for use
  807. *
  808. * Clear out any address information in the emulators and make then ready for
  809. * a new USB scan
  810. */
  811. void usb_emul_reset(struct udevice *dev);
  812. #endif /*_USB_H_ */