usb.h 33 KB

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