usb.h 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023
  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_HCD || defined CONFIG_USB_MUSB_HOST \
  167. || defined(CONFIG_DM_USB)
  168. struct int_queue *create_int_queue(struct usb_device *dev, unsigned long pipe,
  169. int queuesize, int elementsize, void *buffer, int interval);
  170. int destroy_int_queue(struct usb_device *dev, struct int_queue *queue);
  171. void *poll_int_queue(struct usb_device *dev, struct int_queue *queue);
  172. #endif
  173. /* Defines */
  174. #define USB_UHCI_VEND_ID 0x8086
  175. #define USB_UHCI_DEV_ID 0x7112
  176. /*
  177. * PXA25x can only act as USB device. There are drivers
  178. * which works with USB CDC gadgets implementations.
  179. * Some of them have common routines which can be used
  180. * in boards init functions e.g. udc_disconnect() used for
  181. * forced device disconnection from host.
  182. */
  183. extern void udc_disconnect(void);
  184. /*
  185. * board-specific hardware initialization, called by
  186. * usb drivers and u-boot commands
  187. *
  188. * @param index USB controller number
  189. * @param init initializes controller as USB host or device
  190. */
  191. int board_usb_init(int index, enum usb_init_type init);
  192. /*
  193. * can be used to clean up after failed USB initialization attempt
  194. * vide: board_usb_init()
  195. *
  196. * @param index USB controller number for selective cleanup
  197. * @param init usb_init_type passed to board_usb_init()
  198. */
  199. int board_usb_cleanup(int index, enum usb_init_type init);
  200. #ifdef CONFIG_USB_STORAGE
  201. #define USB_MAX_STOR_DEV 7
  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. /*
  478. * Hub Device descriptor
  479. * USB Hub class device protocols
  480. */
  481. #define USB_HUB_PR_FS 0 /* Full speed hub */
  482. #define USB_HUB_PR_HS_NO_TT 0 /* Hi-speed hub without TT */
  483. #define USB_HUB_PR_HS_SINGLE_TT 1 /* Hi-speed hub with single TT */
  484. #define USB_HUB_PR_HS_MULTI_TT 2 /* Hi-speed hub with multiple TT */
  485. #define USB_HUB_PR_SS 3 /* Super speed hub */
  486. /* Transaction Translator Think Times, in bits */
  487. #define HUB_TTTT_8_BITS 0x00
  488. #define HUB_TTTT_16_BITS 0x20
  489. #define HUB_TTTT_24_BITS 0x40
  490. #define HUB_TTTT_32_BITS 0x60
  491. /* Hub descriptor */
  492. struct usb_hub_descriptor {
  493. unsigned char bLength;
  494. unsigned char bDescriptorType;
  495. unsigned char bNbrPorts;
  496. unsigned short wHubCharacteristics;
  497. unsigned char bPwrOn2PwrGood;
  498. unsigned char bHubContrCurrent;
  499. /* 2.0 and 3.0 hubs differ here */
  500. union {
  501. struct {
  502. /* add 1 bit for hub status change; round to bytes */
  503. __u8 DeviceRemovable[(USB_MAXCHILDREN + 1 + 7) / 8];
  504. __u8 PortPowerCtrlMask[(USB_MAXCHILDREN + 1 + 7) / 8];
  505. } __attribute__ ((packed)) hs;
  506. struct {
  507. __u8 bHubHdrDecLat;
  508. __le16 wHubDelay;
  509. __le16 DeviceRemovable;
  510. } __attribute__ ((packed)) ss;
  511. } u;
  512. } __attribute__ ((packed));
  513. struct usb_hub_device {
  514. struct usb_device *pusb_dev;
  515. struct usb_hub_descriptor desc;
  516. ulong connect_timeout; /* Device connection timeout in ms */
  517. ulong query_delay; /* Device query delay in ms */
  518. int overcurrent_count[USB_MAXCHILDREN]; /* Over-current counter */
  519. int hub_depth; /* USB 3.0 hub depth */
  520. struct usb_tt tt; /* Transaction Translator */
  521. };
  522. #ifdef CONFIG_DM_USB
  523. /**
  524. * struct usb_platdata - Platform data about a USB controller
  525. *
  526. * Given a USB controller (UCLASS_USB) dev this is dev_get_platdata(dev)
  527. */
  528. struct usb_platdata {
  529. enum usb_init_type init_type;
  530. };
  531. /**
  532. * struct usb_dev_platdata - Platform data about a USB device
  533. *
  534. * Given a USB device dev this structure is dev_get_parent_platdata(dev).
  535. * This is used by sandbox to provide emulation data also.
  536. *
  537. * @id: ID used to match this device
  538. * @devnum: Device address on the USB bus
  539. * @udev: usb-uclass internal use only do NOT use
  540. * @strings: List of descriptor strings (for sandbox emulation purposes)
  541. * @desc_list: List of descriptors (for sandbox emulation purposes)
  542. */
  543. struct usb_dev_platdata {
  544. struct usb_device_id id;
  545. int devnum;
  546. /*
  547. * This pointer is used to pass the usb_device used in usb_scan_device,
  548. * to get the usb descriptors before the driver is known, to the
  549. * actual udevice once the driver is known and the udevice is created.
  550. * This will be NULL except during probe, do NOT use.
  551. *
  552. * This should eventually go away.
  553. */
  554. struct usb_device *udev;
  555. #ifdef CONFIG_SANDBOX
  556. struct usb_string *strings;
  557. /* NULL-terminated list of descriptor pointers */
  558. struct usb_generic_descriptor **desc_list;
  559. #endif
  560. int configno;
  561. };
  562. /**
  563. * struct usb_bus_priv - information about the USB controller
  564. *
  565. * Given a USB controller (UCLASS_USB) 'dev', this is
  566. * dev_get_uclass_priv(dev).
  567. *
  568. * @next_addr: Next device address to allocate minus 1. Incremented by 1
  569. * each time a new device address is set, so this holds the
  570. * number of devices on the bus
  571. * @desc_before_addr: true if we can read a device descriptor before it
  572. * has been assigned an address. For XHCI this is not possible
  573. * so this will be false.
  574. * @companion: True if this is a companion controller to another USB
  575. * controller
  576. */
  577. struct usb_bus_priv {
  578. int next_addr;
  579. bool desc_before_addr;
  580. bool companion;
  581. };
  582. /**
  583. * struct dm_usb_ops - USB controller operations
  584. *
  585. * This defines the operations supoorted on a USB controller. Common
  586. * arguments are:
  587. *
  588. * @bus: USB bus (i.e. controller), which is in UCLASS_USB.
  589. * @udev: USB device parent data. Controllers are not expected to need
  590. * this, since the device address on the bus is encoded in @pipe.
  591. * It is used for sandbox, and can be handy for debugging and
  592. * logging.
  593. * @pipe: An assortment of bitfields which provide address and packet
  594. * type information. See create_pipe() above for encoding
  595. * details
  596. * @buffer: A buffer to use for sending/receiving. This should be
  597. * DMA-aligned.
  598. * @length: Buffer length in bytes
  599. */
  600. struct dm_usb_ops {
  601. /**
  602. * control() - Send a control message
  603. *
  604. * Most parameters are as above.
  605. *
  606. * @setup: Additional setup information required by the message
  607. */
  608. int (*control)(struct udevice *bus, struct usb_device *udev,
  609. unsigned long pipe, void *buffer, int length,
  610. struct devrequest *setup);
  611. /**
  612. * bulk() - Send a bulk message
  613. *
  614. * Parameters are as above.
  615. */
  616. int (*bulk)(struct udevice *bus, struct usb_device *udev,
  617. unsigned long pipe, void *buffer, int length);
  618. /**
  619. * interrupt() - Send an interrupt message
  620. *
  621. * Most parameters are as above.
  622. *
  623. * @interval: Interrupt interval
  624. */
  625. int (*interrupt)(struct udevice *bus, struct usb_device *udev,
  626. unsigned long pipe, void *buffer, int length,
  627. int interval);
  628. /**
  629. * create_int_queue() - Create and queue interrupt packets
  630. *
  631. * Create and queue @queuesize number of interrupt usb packets of
  632. * @elementsize bytes each. @buffer must be atleast @queuesize *
  633. * @elementsize bytes.
  634. *
  635. * Note some controllers only support a queuesize of 1.
  636. *
  637. * @interval: Interrupt interval
  638. *
  639. * @return A pointer to the created interrupt queue or NULL on error
  640. */
  641. struct int_queue * (*create_int_queue)(struct udevice *bus,
  642. struct usb_device *udev, unsigned long pipe,
  643. int queuesize, int elementsize, void *buffer,
  644. int interval);
  645. /**
  646. * poll_int_queue() - Poll an interrupt queue for completed packets
  647. *
  648. * Poll an interrupt queue for completed packets. The return value
  649. * points to the part of the buffer passed to create_int_queue()
  650. * corresponding to the completed packet.
  651. *
  652. * @queue: queue to poll
  653. *
  654. * @return Pointer to the data of the first completed packet, or
  655. * NULL if no packets are ready
  656. */
  657. void * (*poll_int_queue)(struct udevice *bus, struct usb_device *udev,
  658. struct int_queue *queue);
  659. /**
  660. * destroy_int_queue() - Destroy an interrupt queue
  661. *
  662. * Destroy an interrupt queue created by create_int_queue().
  663. *
  664. * @queue: queue to poll
  665. *
  666. * @return 0 if OK, -ve on error
  667. */
  668. int (*destroy_int_queue)(struct udevice *bus, struct usb_device *udev,
  669. struct int_queue *queue);
  670. /**
  671. * alloc_device() - Allocate a new device context (XHCI)
  672. *
  673. * Before sending packets to a new device on an XHCI bus, a device
  674. * context must be created. If this method is not NULL it will be
  675. * called before the device is enumerated (even before its descriptor
  676. * is read). This should be NULL for EHCI, which does not need this.
  677. */
  678. int (*alloc_device)(struct udevice *bus, struct usb_device *udev);
  679. /**
  680. * reset_root_port() - Reset usb root port
  681. */
  682. int (*reset_root_port)(struct udevice *bus, struct usb_device *udev);
  683. };
  684. #define usb_get_ops(dev) ((struct dm_usb_ops *)(dev)->driver->ops)
  685. #define usb_get_emul_ops(dev) ((struct dm_usb_ops *)(dev)->driver->ops)
  686. /**
  687. * usb_get_dev_index() - look up a device index number
  688. *
  689. * Look up devices using their index number (starting at 0). This works since
  690. * in U-Boot device addresses are allocated starting at 1 with no gaps.
  691. *
  692. * TODO(sjg@chromium.org): Remove this function when usb_ether.c is modified
  693. * to work better with driver model.
  694. *
  695. * @bus: USB bus to check
  696. * @index: Index number of device to find (0=first). This is just the
  697. * device address less 1.
  698. */
  699. struct usb_device *usb_get_dev_index(struct udevice *bus, int index);
  700. /**
  701. * usb_setup_device() - set up a device ready for use
  702. *
  703. * @dev: USB device pointer. This need not be a real device - it is
  704. * common for it to just be a local variable with its ->dev
  705. * member (i.e. @dev->dev) set to the parent device and
  706. * dev->portnr set to the port number on the hub (1=first)
  707. * @do_read: true to read the device descriptor before an address is set
  708. * (should be false for XHCI buses, true otherwise)
  709. * @parent: Parent device (either UCLASS_USB or UCLASS_USB_HUB)
  710. * @return 0 if OK, -ve on error */
  711. int usb_setup_device(struct usb_device *dev, bool do_read,
  712. struct usb_device *parent);
  713. /**
  714. * usb_hub_is_root_hub() - Test whether a hub device is root hub or not
  715. *
  716. * @hub: USB hub device to test
  717. * @return: true if the hub device is root hub, false otherwise.
  718. */
  719. bool usb_hub_is_root_hub(struct udevice *hub);
  720. /**
  721. * usb_hub_scan() - Scan a hub and find its devices
  722. *
  723. * @hub: Hub device to scan
  724. */
  725. int usb_hub_scan(struct udevice *hub);
  726. /**
  727. * usb_scan_device() - Scan a device on a bus
  728. *
  729. * Scan a device on a bus. It has already been detected and is ready to
  730. * be enumerated. This may be either the root hub (@parent is a bus) or a
  731. * normal device (@parent is a hub)
  732. *
  733. * @parent: Parent device
  734. * @port: Hub port number (numbered from 1)
  735. * @speed: USB speed to use for this device
  736. * @devp: Returns pointer to device if all is well
  737. * @return 0 if OK, -ve on error
  738. */
  739. int usb_scan_device(struct udevice *parent, int port,
  740. enum usb_device_speed speed, struct udevice **devp);
  741. /**
  742. * usb_get_bus() - Find the bus for a device
  743. *
  744. * Search up through parents to find the bus this device is connected to. This
  745. * will be a device with uclass UCLASS_USB.
  746. *
  747. * @dev: Device to check
  748. * @return The bus, or NULL if not found (this indicates a critical error in
  749. * the USB stack
  750. */
  751. struct udevice *usb_get_bus(struct udevice *dev);
  752. /**
  753. * usb_select_config() - Set up a device ready for use
  754. *
  755. * This function assumes that the device already has an address and a driver
  756. * bound, and is ready to be set up.
  757. *
  758. * This re-reads the device and configuration descriptors and sets the
  759. * configuration
  760. *
  761. * @dev: Device to set up
  762. */
  763. int usb_select_config(struct usb_device *dev);
  764. /**
  765. * usb_child_pre_probe() - Pre-probe function for USB devices
  766. *
  767. * This is called on all children of hubs and USB controllers (i.e. UCLASS_USB
  768. * and UCLASS_USB_HUB) when a new device is about to be probed. It sets up the
  769. * device from the saved platform data and calls usb_select_config() to
  770. * finish set up.
  771. *
  772. * Once this is done, the device's normal driver can take over, knowing the
  773. * device is accessible on the USB bus.
  774. *
  775. * This function is for use only by the internal USB stack.
  776. *
  777. * @dev: Device to set up
  778. */
  779. int usb_child_pre_probe(struct udevice *dev);
  780. struct ehci_ctrl;
  781. /**
  782. * usb_setup_ehci_gadget() - Set up a USB device as a gadget
  783. *
  784. * TODO(sjg@chromium.org): Tidy this up when USB gadgets can use driver model
  785. *
  786. * This provides a way to tell a controller to start up as a USB device
  787. * instead of as a host. It is untested.
  788. */
  789. int usb_setup_ehci_gadget(struct ehci_ctrl **ctlrp);
  790. /**
  791. * usb_stor_reset() - Prepare to scan USB storage devices
  792. *
  793. * Empty the list of USB storage devices in preparation for scanning them.
  794. * This must be called before a USB scan.
  795. */
  796. void usb_stor_reset(void);
  797. #else /* !CONFIG_DM_USB */
  798. struct usb_device *usb_get_dev_index(int index);
  799. #endif
  800. bool usb_device_has_child_on_port(struct usb_device *parent, int port);
  801. int usb_hub_probe(struct usb_device *dev, int ifnum);
  802. void usb_hub_reset(void);
  803. /*
  804. * usb_find_usb2_hub_address_port() - Get hub address and port for TT setting
  805. *
  806. * Searches for the first HS hub above the given device. If a
  807. * HS hub is found, the hub address and the port the device is
  808. * connected to is return, as required for SPLIT transactions
  809. *
  810. * @param: udev full speed or low speed device
  811. */
  812. void usb_find_usb2_hub_address_port(struct usb_device *udev,
  813. uint8_t *hub_address, uint8_t *hub_port);
  814. /**
  815. * usb_alloc_new_device() - Allocate a new device
  816. *
  817. * @devp: returns a pointer of a new device structure. With driver model this
  818. * is a device pointer, but with legacy USB this pointer is
  819. * driver-specific.
  820. * @return 0 if OK, -ENOSPC if we have found out of room for new devices
  821. */
  822. int usb_alloc_new_device(struct udevice *controller, struct usb_device **devp);
  823. /**
  824. * usb_free_device() - Free a partially-inited device
  825. *
  826. * This is an internal function. It is used to reverse the action of
  827. * usb_alloc_new_device() when we hit a problem during init.
  828. */
  829. void usb_free_device(struct udevice *controller);
  830. int usb_new_device(struct usb_device *dev);
  831. int usb_alloc_device(struct usb_device *dev);
  832. /**
  833. * usb_emul_setup_device() - Set up a new USB device emulation
  834. *
  835. * This is normally called when a new emulation device is bound. It tells
  836. * the USB emulation uclass about the features of the emulator.
  837. *
  838. * @dev: Emulation device
  839. * @maxpacketsize: Maximum packet size (e.g. PACKET_SIZE_64)
  840. * @strings: List of USB string descriptors, terminated by a NULL
  841. * entry
  842. * @desc_list: List of points or USB descriptors, terminated by NULL.
  843. * The first entry must be struct usb_device_descriptor,
  844. * and others follow on after that.
  845. * @return 0 if OK, -ve on error
  846. */
  847. int usb_emul_setup_device(struct udevice *dev, int maxpacketsize,
  848. struct usb_string *strings, void **desc_list);
  849. /**
  850. * usb_emul_control() - Send a control packet to an emulator
  851. *
  852. * @emul: Emulator device
  853. * @udev: USB device (which the emulator is causing to appear)
  854. * See struct dm_usb_ops for details on other parameters
  855. * @return 0 if OK, -ve on error
  856. */
  857. int usb_emul_control(struct udevice *emul, struct usb_device *udev,
  858. unsigned long pipe, void *buffer, int length,
  859. struct devrequest *setup);
  860. /**
  861. * usb_emul_bulk() - Send a bulk packet to an emulator
  862. *
  863. * @emul: Emulator device
  864. * @udev: USB device (which the emulator is causing to appear)
  865. * See struct dm_usb_ops for details on other parameters
  866. * @return 0 if OK, -ve on error
  867. */
  868. int usb_emul_bulk(struct udevice *emul, struct usb_device *udev,
  869. unsigned long pipe, void *buffer, int length);
  870. /**
  871. * usb_emul_int() - Send an interrupt packet to an emulator
  872. *
  873. * @emul: Emulator device
  874. * @udev: USB device (which the emulator is causing to appear)
  875. * See struct dm_usb_ops for details on other parameters
  876. * @return 0 if OK, -ve on error
  877. */
  878. int usb_emul_int(struct udevice *emul, struct usb_device *udev,
  879. unsigned long pipe, void *buffer, int length, int interval);
  880. /**
  881. * usb_emul_find() - Find an emulator for a particular device
  882. *
  883. * Check @pipe to find a device number on bus @bus and return it.
  884. *
  885. * @bus: USB bus (controller)
  886. * @pipe: Describes pipe being used, and includes the device number
  887. * @emulp: Returns pointer to emulator, or NULL if not found
  888. * @return 0 if found, -ve on error
  889. */
  890. int usb_emul_find(struct udevice *bus, ulong pipe, struct udevice **emulp);
  891. /**
  892. * usb_emul_find_for_dev() - Find an emulator for a particular device
  893. *
  894. * @bus: USB bus (controller)
  895. * @dev: USB device to check
  896. * @emulp: Returns pointer to emulator, or NULL if not found
  897. * @return 0 if found, -ve on error
  898. */
  899. int usb_emul_find_for_dev(struct udevice *dev, struct udevice **emulp);
  900. /**
  901. * usb_emul_reset() - Reset all emulators ready for use
  902. *
  903. * Clear out any address information in the emulators and make then ready for
  904. * a new USB scan
  905. */
  906. void usb_emul_reset(struct udevice *dev);
  907. /**
  908. * usb_show_tree() - show the USB device tree
  909. *
  910. * This shows a list of active USB devices along with basic information about
  911. * each.
  912. */
  913. void usb_show_tree(void);
  914. #endif /*_USB_H_ */