usb.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * (C) Copyright 2001
  4. * Denis Peter, MPL AG Switzerland
  5. *
  6. * Adapted for U-Boot driver model
  7. * (C) Copyright 2015 Google, Inc
  8. * Note: Part of this code has been derived from linux
  9. *
  10. */
  11. #ifndef _USB_H_
  12. #define _USB_H_
  13. //#include <fdtdec.h>
  14. #include <usb_defs.h>
  15. #include <linux/usb/ch9.h>
  16. //#include <asm/cache.h>
  17. //#include <part.h>
  18. /*
  19. * The EHCI spec says that we must align to at least 32 bytes. However,
  20. * some platforms require larger alignment.
  21. */
  22. #if ARCH_DMA_MINALIGN > 32
  23. #define USB_DMA_MINALIGN ARCH_DMA_MINALIGN
  24. #else
  25. #define USB_DMA_MINALIGN 32
  26. #endif
  27. /* Everything is aribtrary */
  28. #define USB_ALTSETTINGALLOC 4
  29. #define USB_MAXALTSETTING 128 /* Hard limit */
  30. #define USB_MAX_DEVICE 32
  31. #define USB_MAXCONFIG 8
  32. #define USB_MAXINTERFACES 8
  33. #define USB_MAXENDPOINTS 16
  34. #define USB_MAXCHILDREN 8 /* This is arbitrary */
  35. #define USB_MAX_HUB 16
  36. #define USB_CNTL_TIMEOUT 10000 /* 100ms timeout */
  37. /*
  38. * This is the timeout to allow for submitting an urb in ms. We allow more
  39. * time for a BULK device to react - some are slow.
  40. */
  41. #define USB_TIMEOUT_MS(pipe) (usb_pipebulk(pipe) ? 5000 : 1000)
  42. /* device request (setup) */
  43. struct devrequest {
  44. __u8 requesttype;
  45. __u8 request;
  46. __le16 value;
  47. __le16 index;
  48. __le16 length;
  49. };
  50. /* Interface */
  51. struct usb_interface {
  52. struct usb_interface_descriptor desc;
  53. __u8 no_of_ep;
  54. __u8 num_altsetting;
  55. __u8 act_altsetting;
  56. struct usb_endpoint_descriptor ep_desc[USB_MAXENDPOINTS];
  57. /*
  58. * Super Speed Device will have Super Speed Endpoint
  59. * Companion Descriptor (section 9.6.7 of usb 3.0 spec)
  60. * Revision 1.0 June 6th 2011
  61. */
  62. struct usb_ss_ep_comp_descriptor ss_ep_comp_desc[USB_MAXENDPOINTS];
  63. };
  64. /* Configuration information.. */
  65. struct usb_config {
  66. struct usb_config_descriptor desc;
  67. __u8 no_of_if; /* number of interfaces */
  68. struct usb_interface if_desc[USB_MAXINTERFACES];
  69. };
  70. enum {
  71. /* Maximum packet size; encoded as 0,1,2,3 = 8,16,32,64 */
  72. PACKET_SIZE_8 = 0,
  73. PACKET_SIZE_16 = 1,
  74. PACKET_SIZE_32 = 2,
  75. PACKET_SIZE_64 = 3,
  76. };
  77. /**
  78. * struct usb_device - information about a USB device
  79. *
  80. * With driver model both UCLASS_USB (the USB controllers) and UCLASS_USB_HUB
  81. * (the hubs) have this as parent data. Hubs are children of controllers or
  82. * other hubs and there is always a single root hub for each controller.
  83. * Therefore struct usb_device can always be accessed with
  84. * dev_get_parent_priv(dev), where dev is a USB device.
  85. *
  86. * Pointers exist for obtaining both the device (could be any uclass) and
  87. * controller (UCLASS_USB) from this structure. The controller does not have
  88. * a struct usb_device since it is not a device.
  89. */
  90. struct usb_device {
  91. int devnum; /* Device number on USB bus */
  92. int speed; /* full/low/high */
  93. char mf[32]; /* manufacturer */
  94. char prod[32]; /* product */
  95. char serial[32]; /* serial number */
  96. struct usb_tt *tt;
  97. int ttport;
  98. /* Maximum packet size; one of: PACKET_SIZE_* */
  99. int maxpacketsize;
  100. /* one bit for each endpoint ([0] = IN, [1] = OUT) */
  101. unsigned int toggle[2];
  102. /* endpoint halts; one bit per endpoint # & direction;
  103. * [0] = IN, [1] = OUT
  104. */
  105. unsigned int halted[2];
  106. int epmaxpacketin[16]; /* INput endpoint specific maximums */
  107. int epmaxpacketout[16]; /* OUTput endpoint specific maximums */
  108. int configno; /* selected config number */
  109. /* Device Descriptor */
  110. struct usb_device_descriptor descriptor;
  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, int *actual_length, int timeout);
  162. int submit_control_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
  163. int transfer_len, struct devrequest *setup, int timeout);
  164. /* Defines */
  165. #define USB_UHCI_VEND_ID 0x8086
  166. #define USB_UHCI_DEV_ID 0x7112
  167. /*
  168. * PXA25x can only act as USB device. There are drivers
  169. * which works with USB CDC gadgets implementations.
  170. * Some of them have common routines which can be used
  171. * in boards init functions e.g. udc_disconnect() used for
  172. * forced device disconnection from host.
  173. */
  174. extern void udc_disconnect(void);
  175. /*
  176. * board-specific hardware initialization, called by
  177. * usb drivers and u-boot commands
  178. *
  179. * @param index USB controller number
  180. * @param init initializes controller as USB host or device
  181. */
  182. int board_usb_init(int index, enum usb_init_type init);
  183. /*
  184. * can be used to clean up after failed USB initialization attempt
  185. * vide: board_usb_init()
  186. *
  187. * @param index USB controller number for selective cleanup
  188. * @param init usb_init_type passed to board_usb_init()
  189. */
  190. int board_usb_cleanup(int index, enum usb_init_type init);
  191. #ifdef CONFIG_USB_STORAGE
  192. #define USB_MAX_STOR_DEV 7
  193. int usb_stor_scan(int mode);
  194. int usb_stor_info(void);
  195. #endif
  196. #ifdef CONFIG_USB_HOST_ETHER
  197. #define USB_MAX_ETH_DEV 5
  198. int usb_host_eth_scan(int mode);
  199. #endif
  200. #ifdef CONFIG_USB_KEYBOARD
  201. int drv_usb_kbd_init(void);
  202. int usb_kbd_deregister(int force);
  203. #endif
  204. /* routines */
  205. int usb_init(void); /* initialize the USB Controller */
  206. int usb_stop(void); /* stop the USB Controller */
  207. int usb_detect_change(void); /* detect if a USB device has been (un)plugged */
  208. int usb_set_protocol(struct usb_device *dev, int ifnum, int protocol);
  209. int usb_set_idle(struct usb_device *dev, int ifnum, int duration,
  210. int report_id);
  211. int usb_control_msg(struct usb_device *dev, unsigned int pipe,
  212. unsigned char request, unsigned char requesttype,
  213. unsigned short value, unsigned short index,
  214. void *data, unsigned short size, int timeout);
  215. int usb_bulk_msg(struct usb_device *dev, unsigned int pipe,
  216. void *data, int len, int *actual_length, int timeout);
  217. int usb_disable_asynch(int disable);
  218. int usb_maxpacket(struct usb_device *dev, unsigned long pipe);
  219. int usb_get_configuration_no(struct usb_device *dev, int cfgno,
  220. unsigned char *buffer, int length);
  221. int usb_get_configuration_len(struct usb_device *dev, int cfgno);
  222. int usb_get_report(struct usb_device *dev, int ifnum, unsigned char type,
  223. unsigned char id, void *buf, int size);
  224. int usb_get_class_descriptor(struct usb_device *dev, int ifnum,
  225. unsigned char type, unsigned char id, void *buf,
  226. int size);
  227. int usb_clear_halt(struct usb_device *dev, int pipe);
  228. int usb_string(struct usb_device *dev, int index, char *buf, size_t size);
  229. int usb_set_interface(struct usb_device *dev, int interface, int alternate);
  230. int usb_get_port_status(struct usb_device *dev, int port, void *data);
  231. /* big endian -> little endian conversion */
  232. /* some CPUs are already little endian e.g. the ARM920T */
  233. #define __swap_16(x) \
  234. ({ unsigned short x_ = (unsigned short)x; \
  235. (unsigned short)( \
  236. ((x_ & 0x00FFU) << 8) | ((x_ & 0xFF00U) >> 8)); \
  237. })
  238. #define __swap_32(x) \
  239. ({ unsigned long x_ = (unsigned long)x; \
  240. (unsigned long)( \
  241. ((x_ & 0x000000FFUL) << 24) | \
  242. ((x_ & 0x0000FF00UL) << 8) | \
  243. ((x_ & 0x00FF0000UL) >> 8) | \
  244. ((x_ & 0xFF000000UL) >> 24)); \
  245. })
  246. #ifdef __LITTLE_ENDIAN
  247. # define swap_16(x) (x)
  248. # define swap_32(x) (x)
  249. #else
  250. # define swap_16(x) __swap_16(x)
  251. # define swap_32(x) __swap_32(x)
  252. #endif
  253. /*
  254. * Calling this entity a "pipe" is glorifying it. A USB pipe
  255. * is something embarrassingly simple: it basically consists
  256. * of the following information:
  257. * - device number (7 bits)
  258. * - endpoint number (4 bits)
  259. * - current Data0/1 state (1 bit)
  260. * - direction (1 bit)
  261. * - speed (2 bits)
  262. * - max packet size (2 bits: 8, 16, 32 or 64)
  263. * - pipe type (2 bits: control, interrupt, bulk, isochronous)
  264. *
  265. * That's 18 bits. Really. Nothing more. And the USB people have
  266. * documented these eighteen bits as some kind of glorious
  267. * virtual data structure.
  268. *
  269. * Let's not fall in that trap. We'll just encode it as a simple
  270. * unsigned int. The encoding is:
  271. *
  272. * - max size: bits 0-1 (00 = 8, 01 = 16, 10 = 32, 11 = 64)
  273. * - direction: bit 7 (0 = Host-to-Device [Out],
  274. * (1 = Device-to-Host [In])
  275. * - device: bits 8-14
  276. * - endpoint: bits 15-18
  277. * - Data0/1: bit 19
  278. * - pipe type: bits 30-31 (00 = isochronous, 01 = interrupt,
  279. * 10 = control, 11 = bulk)
  280. *
  281. * Why? Because it's arbitrary, and whatever encoding we select is really
  282. * up to us. This one happens to share a lot of bit positions with the UHCI
  283. * specification, so that much of the uhci driver can just mask the bits
  284. * appropriately.
  285. */
  286. /* Create various pipes... */
  287. #define create_pipe(dev,endpoint) \
  288. (((dev)->devnum << 8) | ((endpoint) << 15) | \
  289. (dev)->maxpacketsize)
  290. #define default_pipe(dev) ((dev)->speed << 26)
  291. #define usb_sndctrlpipe(dev, endpoint) ((PIPE_CONTROL << 30) | \
  292. create_pipe(dev, endpoint))
  293. #define usb_rcvctrlpipe(dev, endpoint) ((PIPE_CONTROL << 30) | \
  294. create_pipe(dev, endpoint) | \
  295. USB_DIR_IN)
  296. #define usb_sndisocpipe(dev, endpoint) ((PIPE_ISOCHRONOUS << 30) | \
  297. create_pipe(dev, endpoint))
  298. #define usb_rcvisocpipe(dev, endpoint) ((PIPE_ISOCHRONOUS << 30) | \
  299. create_pipe(dev, endpoint) | \
  300. USB_DIR_IN)
  301. #define usb_sndbulkpipe(dev, endpoint) ((PIPE_BULK << 30) | \
  302. create_pipe(dev, endpoint))
  303. #define usb_rcvbulkpipe(dev, endpoint) ((PIPE_BULK << 30) | \
  304. create_pipe(dev, endpoint) | \
  305. USB_DIR_IN)
  306. #define usb_sndintpipe(dev, endpoint) ((PIPE_INTERRUPT << 30) | \
  307. create_pipe(dev, endpoint))
  308. #define usb_rcvintpipe(dev, endpoint) ((PIPE_INTERRUPT << 30) | \
  309. create_pipe(dev, endpoint) | \
  310. USB_DIR_IN)
  311. #define usb_snddefctrl(dev) ((PIPE_CONTROL << 30) | \
  312. default_pipe(dev))
  313. #define usb_rcvdefctrl(dev) ((PIPE_CONTROL << 30) | \
  314. default_pipe(dev) | \
  315. USB_DIR_IN)
  316. /* The D0/D1 toggle bits */
  317. #define usb_gettoggle(dev, ep, out) (((dev)->toggle[out] >> ep) & 1)
  318. #define usb_dotoggle(dev, ep, out) ((dev)->toggle[out] ^= (1 << ep))
  319. #define usb_settoggle(dev, ep, out, bit) ((dev)->toggle[out] = \
  320. ((dev)->toggle[out] & \
  321. ~(1 << ep)) | ((bit) << ep))
  322. /* Endpoint halt control/status */
  323. #define usb_endpoint_out(ep_dir) (((ep_dir >> 7) & 1) ^ 1)
  324. #define usb_endpoint_halt(dev, ep, out) ((dev)->halted[out] |= (1 << (ep)))
  325. #define usb_endpoint_running(dev, ep, out) ((dev)->halted[out] &= ~(1 << (ep)))
  326. #define usb_endpoint_halted(dev, ep, out) ((dev)->halted[out] & (1 << (ep)))
  327. #define usb_packetid(pipe) (((pipe) & USB_DIR_IN) ? USB_PID_IN : \
  328. USB_PID_OUT)
  329. //#define usb_pipeout(pipe) ((((pipe) >> 7) & 1) ^ 1)
  330. //#define usb_pipein(pipe) (((pipe) >> 7) & 1)
  331. #define usb_pipein(pipe) ((pipe) & USB_DIR_IN)
  332. #define usb_pipeout(pipe) (!usb_pipein(pipe))
  333. #define usb_pipedevice(pipe) (((pipe) >> 8) & 0x7f)
  334. #define usb_pipe_endpdev(pipe) (((pipe) >> 8) & 0x7ff)
  335. #define usb_pipeendpoint(pipe) (((pipe) >> 15) & 0xf)
  336. #define usb_pipedata(pipe) (((pipe) >> 19) & 1)
  337. #define usb_pipetype(pipe) (((pipe) >> 30) & 3)
  338. #define usb_pipeisoc(pipe) (usb_pipetype((pipe)) == PIPE_ISOCHRONOUS)
  339. #define usb_pipeint(pipe) (usb_pipetype((pipe)) == PIPE_INTERRUPT)
  340. #define usb_pipecontrol(pipe) (usb_pipetype((pipe)) == PIPE_CONTROL)
  341. #define usb_pipebulk(pipe) (usb_pipetype((pipe)) == PIPE_BULK)
  342. #define usb_pipe_ep_index(pipe) \
  343. usb_pipecontrol(pipe) ? (usb_pipeendpoint(pipe) * 2) : \
  344. ((usb_pipeendpoint(pipe) * 2) - \
  345. (usb_pipein(pipe) ? 0 : 1))
  346. /**
  347. * struct usb_device_id - identifies USB devices for probing and hotplugging
  348. * @match_flags: Bit mask controlling which of the other fields are used to
  349. * match against new devices. Any field except for driver_info may be
  350. * used, although some only make sense in conjunction with other fields.
  351. * This is usually set by a USB_DEVICE_*() macro, which sets all
  352. * other fields in this structure except for driver_info.
  353. * @idVendor: USB vendor ID for a device; numbers are assigned
  354. * by the USB forum to its members.
  355. * @idProduct: Vendor-assigned product ID.
  356. * @bcdDevice_lo: Low end of range of vendor-assigned product version numbers.
  357. * This is also used to identify individual product versions, for
  358. * a range consisting of a single device.
  359. * @bcdDevice_hi: High end of version number range. The range of product
  360. * versions is inclusive.
  361. * @bDeviceClass: Class of device; numbers are assigned
  362. * by the USB forum. Products may choose to implement classes,
  363. * or be vendor-specific. Device classes specify behavior of all
  364. * the interfaces on a device.
  365. * @bDeviceSubClass: Subclass of device; associated with bDeviceClass.
  366. * @bDeviceProtocol: Protocol of device; associated with bDeviceClass.
  367. * @bInterfaceClass: Class of interface; numbers are assigned
  368. * by the USB forum. Products may choose to implement classes,
  369. * or be vendor-specific. Interface classes specify behavior only
  370. * of a given interface; other interfaces may support other classes.
  371. * @bInterfaceSubClass: Subclass of interface; associated with bInterfaceClass.
  372. * @bInterfaceProtocol: Protocol of interface; associated with bInterfaceClass.
  373. * @bInterfaceNumber: Number of interface; composite devices may use
  374. * fixed interface numbers to differentiate between vendor-specific
  375. * interfaces.
  376. * @driver_info: Holds information used by the driver. Usually it holds
  377. * a pointer to a descriptor understood by the driver, or perhaps
  378. * device flags.
  379. *
  380. * In most cases, drivers will create a table of device IDs by using
  381. * USB_DEVICE(), or similar macros designed for that purpose.
  382. * They will then export it to userspace using MODULE_DEVICE_TABLE(),
  383. * and provide it to the USB core through their usb_driver structure.
  384. *
  385. * See the usb_match_id() function for information about how matches are
  386. * performed. Briefly, you will normally use one of several macros to help
  387. * construct these entries. Each entry you provide will either identify
  388. * one or more specific products, or will identify a class of products
  389. * which have agreed to behave the same. You should put the more specific
  390. * matches towards the beginning of your table, so that driver_info can
  391. * record quirks of specific products.
  392. */
  393. struct usb_device_id {
  394. /* which fields to match against? */
  395. u16 match_flags;
  396. /* Used for product specific matches; range is inclusive */
  397. u16 idVendor;
  398. u16 idProduct;
  399. u16 bcdDevice_lo;
  400. u16 bcdDevice_hi;
  401. /* Used for device class matches */
  402. u8 bDeviceClass;
  403. u8 bDeviceSubClass;
  404. u8 bDeviceProtocol;
  405. /* Used for interface class matches */
  406. u8 bInterfaceClass;
  407. u8 bInterfaceSubClass;
  408. u8 bInterfaceProtocol;
  409. /* Used for vendor-specific interface matches */
  410. u8 bInterfaceNumber;
  411. /* not matched against */
  412. ulong driver_info;
  413. };
  414. /* Some useful macros to use to create struct usb_device_id */
  415. #define USB_DEVICE_ID_MATCH_VENDOR 0x0001
  416. #define USB_DEVICE_ID_MATCH_PRODUCT 0x0002
  417. #define USB_DEVICE_ID_MATCH_DEV_LO 0x0004
  418. #define USB_DEVICE_ID_MATCH_DEV_HI 0x0008
  419. #define USB_DEVICE_ID_MATCH_DEV_CLASS 0x0010
  420. #define USB_DEVICE_ID_MATCH_DEV_SUBCLASS 0x0020
  421. #define USB_DEVICE_ID_MATCH_DEV_PROTOCOL 0x0040
  422. #define USB_DEVICE_ID_MATCH_INT_CLASS 0x0080
  423. #define USB_DEVICE_ID_MATCH_INT_SUBCLASS 0x0100
  424. #define USB_DEVICE_ID_MATCH_INT_PROTOCOL 0x0200
  425. #define USB_DEVICE_ID_MATCH_INT_NUMBER 0x0400
  426. /* Match anything, indicates this is a valid entry even if everything is 0 */
  427. #define USB_DEVICE_ID_MATCH_NONE 0x0800
  428. #define USB_DEVICE_ID_MATCH_ALL 0x07ff
  429. /**
  430. * struct usb_driver_entry - Matches a driver to its usb_device_ids
  431. * @driver: Driver to use
  432. * @match: List of match records for this driver, terminated by {}
  433. */
  434. struct usb_driver_entry {
  435. struct driver *driver;
  436. const struct usb_device_id *match;
  437. };
  438. #define USB_DEVICE_ID_MATCH_DEVICE \
  439. (USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_PRODUCT)
  440. /**
  441. * USB_DEVICE - macro used to describe a specific usb device
  442. * @vend: the 16 bit USB Vendor ID
  443. * @prod: the 16 bit USB Product ID
  444. *
  445. * This macro is used to create a struct usb_device_id that matches a
  446. * specific device.
  447. */
  448. #define USB_DEVICE(vend, prod) \
  449. .match_flags = USB_DEVICE_ID_MATCH_DEVICE, \
  450. .idVendor = (vend), \
  451. .idProduct = (prod)
  452. #define U_BOOT_USB_DEVICE(__name, __match) \
  453. ll_entry_declare(struct usb_driver_entry, __name, usb_driver_entry) = {\
  454. .driver = llsym(struct driver, __name, driver), \
  455. .match = __match, \
  456. }
  457. /*************************************************************************
  458. * Hub Stuff
  459. */
  460. struct usb_port_status {
  461. unsigned short wPortStatus;
  462. unsigned short wPortChange;
  463. };
  464. struct usb_hub_status {
  465. unsigned short wHubStatus;
  466. unsigned short wHubChange;
  467. };
  468. /*
  469. * Hub Device descriptor
  470. * USB Hub class device protocols
  471. */
  472. #define USB_HUB_PR_FS 0 /* Full speed hub */
  473. #define USB_HUB_PR_HS_NO_TT 0 /* Hi-speed hub without TT */
  474. #define USB_HUB_PR_HS_SINGLE_TT 1 /* Hi-speed hub with single TT */
  475. #define USB_HUB_PR_HS_MULTI_TT 2 /* Hi-speed hub with multiple TT */
  476. #define USB_HUB_PR_SS 3 /* Super speed hub */
  477. /* Transaction Translator Think Times, in bits */
  478. #define HUB_TTTT_8_BITS 0x00
  479. #define HUB_TTTT_16_BITS 0x20
  480. #define HUB_TTTT_24_BITS 0x40
  481. #define HUB_TTTT_32_BITS 0x60
  482. /* Hub descriptor */
  483. struct usb_hub_descriptor {
  484. unsigned char bLength;
  485. unsigned char bDescriptorType;
  486. unsigned char bNbrPorts;
  487. unsigned short wHubCharacteristics;
  488. unsigned char bPwrOn2PwrGood;
  489. unsigned char bHubContrCurrent;
  490. /* 2.0 and 3.0 hubs differ here */
  491. union {
  492. struct {
  493. /* add 1 bit for hub status change; round to bytes */
  494. __u8 DeviceRemovable[(USB_MAXCHILDREN + 1 + 7) / 8];
  495. __u8 PortPowerCtrlMask[(USB_MAXCHILDREN + 1 + 7) / 8];
  496. }hs;
  497. struct {
  498. __u8 bHubHdrDecLat;
  499. __le16 wHubDelay;
  500. __le16 DeviceRemovable;
  501. }ss;
  502. } u;
  503. };
  504. struct usb_hub_device {
  505. struct usb_device *pusb_dev;
  506. struct usb_hub_descriptor desc;
  507. ulong connect_timeout; /* Device connection timeout in ms */
  508. ulong query_delay; /* Device query delay in ms */
  509. int overcurrent_count[USB_MAXCHILDREN]; /* Over-current counter */
  510. int hub_depth; /* USB 3.0 hub depth */
  511. struct usb_tt tt; /* Transaction Translator */
  512. };
  513. #ifdef CONFIG_DM_USB
  514. /**
  515. * struct usb_platdata - Platform data about a USB controller
  516. *
  517. * Given a USB controller (UCLASS_USB) dev this is dev_get_platdata(dev)
  518. */
  519. struct usb_platdata {
  520. enum usb_init_type init_type;
  521. };
  522. /**
  523. * struct usb_dev_platdata - Platform data about a USB device
  524. *
  525. * Given a USB device dev this structure is dev_get_parent_platdata(dev).
  526. * This is used by sandbox to provide emulation data also.
  527. *
  528. * @id: ID used to match this device
  529. * @devnum: Device address on the USB bus
  530. * @udev: usb-uclass internal use only do NOT use
  531. * @strings: List of descriptor strings (for sandbox emulation purposes)
  532. * @desc_list: List of descriptors (for sandbox emulation purposes)
  533. */
  534. struct usb_dev_platdata {
  535. struct usb_device_id id;
  536. int devnum;
  537. /*
  538. * This pointer is used to pass the usb_device used in usb_scan_device,
  539. * to get the usb descriptors before the driver is known, to the
  540. * actual udevice once the driver is known and the udevice is created.
  541. * This will be NULL except during probe, do NOT use.
  542. *
  543. * This should eventually go away.
  544. */
  545. struct usb_device *udev;
  546. int configno;
  547. };
  548. /**
  549. * struct usb_bus_priv - information about the USB controller
  550. *
  551. * Given a USB controller (UCLASS_USB) 'dev', this is
  552. * dev_get_uclass_priv(dev).
  553. *
  554. * @next_addr: Next device address to allocate minus 1. Incremented by 1
  555. * each time a new device address is set, so this holds the
  556. * number of devices on the bus
  557. * @desc_before_addr: true if we can read a device descriptor before it
  558. * has been assigned an address. For XHCI this is not possible
  559. * so this will be false.
  560. * @companion: True if this is a companion controller to another USB
  561. * controller
  562. */
  563. struct usb_bus_priv {
  564. int next_addr;
  565. bool desc_before_addr;
  566. bool companion;
  567. };
  568. /**
  569. * struct usb_emul_platdata - platform data about the USB emulator
  570. *
  571. * Given a USB emulator (UCLASS_USB_EMUL) 'dev', this is
  572. * dev_get_uclass_platdata(dev).
  573. *
  574. * @port1: USB emulator device port number on the parent hub
  575. */
  576. struct usb_emul_platdata {
  577. int port1; /* Port number (numbered from 1) */
  578. };
  579. /**
  580. * struct dm_usb_ops - USB controller operations
  581. *
  582. * This defines the operations supoorted on a USB controller. Common
  583. * arguments are:
  584. *
  585. * @bus: USB bus (i.e. controller), which is in UCLASS_USB.
  586. * @udev: USB device parent data. Controllers are not expected to need
  587. * this, since the device address on the bus is encoded in @pipe.
  588. * It is used for sandbox, and can be handy for debugging and
  589. * logging.
  590. * @pipe: An assortment of bitfields which provide address and packet
  591. * type information. See create_pipe() above for encoding
  592. * details
  593. * @buffer: A buffer to use for sending/receiving. This should be
  594. * DMA-aligned.
  595. * @length: Buffer length in bytes
  596. */
  597. struct dm_usb_ops {
  598. /**
  599. * control() - Send a control message
  600. *
  601. * Most parameters are as above.
  602. *
  603. * @setup: Additional setup information required by the message
  604. */
  605. int (*control)(struct udevice *bus, struct usb_device *udev,
  606. unsigned long pipe, void *buffer, int length,
  607. struct devrequest *setup);
  608. /**
  609. * bulk() - Send a bulk message
  610. *
  611. * Parameters are as above.
  612. */
  613. int (*bulk)(struct udevice *bus, struct usb_device *udev,
  614. unsigned long pipe, void *buffer, int length);
  615. /**
  616. * interrupt() - Send an interrupt message
  617. *
  618. * Most parameters are as above.
  619. *
  620. * @interval: Interrupt interval
  621. */
  622. int (*interrupt)(struct udevice *bus, struct usb_device *udev,
  623. unsigned long pipe, void *buffer, int length,
  624. int interval);
  625. /**
  626. * alloc_device() - Allocate a new device context (XHCI)
  627. *
  628. * Before sending packets to a new device on an XHCI bus, a device
  629. * context must be created. If this method is not NULL it will be
  630. * called before the device is enumerated (even before its descriptor
  631. * is read). This should be NULL for EHCI, which does not need this.
  632. */
  633. int (*alloc_device)(struct udevice *bus, struct usb_device *udev);
  634. /**
  635. * reset_root_port() - Reset usb root port
  636. */
  637. int (*reset_root_port)(struct udevice *bus, struct usb_device *udev);
  638. /**
  639. * get_max_xfer_size() - Get HCD's maximum transfer bytes
  640. *
  641. * The HCD may have limitation on the maximum bytes to be transferred
  642. * in a USB transfer. USB class driver needs to be aware of this.
  643. */
  644. int (*get_max_xfer_size)(struct udevice *bus, size_t *size);
  645. };
  646. #define usb_get_ops(dev) ((struct dm_usb_ops *)(dev)->driver->ops)
  647. #define usb_get_emul_ops(dev) ((struct dm_usb_ops *)(dev)->driver->ops)
  648. /**
  649. * usb_get_dev_index() - look up a device index number
  650. *
  651. * Look up devices using their index number (starting at 0). This works since
  652. * in U-Boot device addresses are allocated starting at 1 with no gaps.
  653. *
  654. * TODO(sjg@chromium.org): Remove this function when usb_ether.c is modified
  655. * to work better with driver model.
  656. *
  657. * @bus: USB bus to check
  658. * @index: Index number of device to find (0=first). This is just the
  659. * device address less 1.
  660. */
  661. struct usb_device *usb_get_dev_index(struct udevice *bus, int index);
  662. /**
  663. * usb_setup_device() - set up a device ready for use
  664. *
  665. * @dev: USB device pointer. This need not be a real device - it is
  666. * common for it to just be a local variable with its ->dev
  667. * member (i.e. @dev->dev) set to the parent device and
  668. * dev->portnr set to the port number on the hub (1=first)
  669. * @do_read: true to read the device descriptor before an address is set
  670. * (should be false for XHCI buses, true otherwise)
  671. * @parent: Parent device (either UCLASS_USB or UCLASS_USB_HUB)
  672. * @return 0 if OK, -ve on error */
  673. int usb_setup_device(struct usb_device *dev, bool do_read,
  674. struct usb_device *parent);
  675. /**
  676. * usb_hub_is_root_hub() - Test whether a hub device is root hub or not
  677. *
  678. * @hub: USB hub device to test
  679. * @return: true if the hub device is root hub, false otherwise.
  680. */
  681. bool usb_hub_is_root_hub(struct udevice *hub);
  682. /**
  683. * usb_hub_scan() - Scan a hub and find its devices
  684. *
  685. * @hub: Hub device to scan
  686. */
  687. int usb_hub_scan(struct udevice *hub);
  688. /**
  689. * usb_scan_device() - Scan a device on a bus
  690. *
  691. * Scan a device on a bus. It has already been detected and is ready to
  692. * be enumerated. This may be either the root hub (@parent is a bus) or a
  693. * normal device (@parent is a hub)
  694. *
  695. * @parent: Parent device
  696. * @port: Hub port number (numbered from 1)
  697. * @speed: USB speed to use for this device
  698. * @devp: Returns pointer to device if all is well
  699. * @return 0 if OK, -ve on error
  700. */
  701. int usb_scan_device(struct udevice *parent, int port,
  702. enum usb_device_speed speed, struct udevice **devp);
  703. /**
  704. * usb_get_bus() - Find the bus for a device
  705. *
  706. * Search up through parents to find the bus this device is connected to. This
  707. * will be a device with uclass UCLASS_USB.
  708. *
  709. * @dev: Device to check
  710. * @return The bus, or NULL if not found (this indicates a critical error in
  711. * the USB stack
  712. */
  713. struct udevice *usb_get_bus(struct udevice *dev);
  714. /**
  715. * usb_select_config() - Set up a device ready for use
  716. *
  717. * This function assumes that the device already has an address and a driver
  718. * bound, and is ready to be set up.
  719. *
  720. * This re-reads the device and configuration descriptors and sets the
  721. * configuration
  722. *
  723. * @dev: Device to set up
  724. */
  725. int usb_select_config(struct usb_device *dev);
  726. /**
  727. * usb_child_pre_probe() - Pre-probe function for USB devices
  728. *
  729. * This is called on all children of hubs and USB controllers (i.e. UCLASS_USB
  730. * and UCLASS_USB_HUB) when a new device is about to be probed. It sets up the
  731. * device from the saved platform data and calls usb_select_config() to
  732. * finish set up.
  733. *
  734. * Once this is done, the device's normal driver can take over, knowing the
  735. * device is accessible on the USB bus.
  736. *
  737. * This function is for use only by the internal USB stack.
  738. *
  739. * @dev: Device to set up
  740. */
  741. int usb_child_pre_probe(struct udevice *dev);
  742. struct ehci_ctrl;
  743. /**
  744. * usb_stor_reset() - Prepare to scan USB storage devices
  745. *
  746. * Empty the list of USB storage devices in preparation for scanning them.
  747. * This must be called before a USB scan.
  748. */
  749. void usb_stor_reset(void);
  750. #else /* !CONFIG_DM_USB */
  751. struct usb_device *usb_get_dev_index(int index);
  752. #endif
  753. bool usb_device_has_child_on_port(struct usb_device *parent, int port);
  754. int usb_hub_probe(struct usb_device *dev, int ifnum);
  755. void usb_hub_reset(void);
  756. /**
  757. * usb_alloc_new_device() - Allocate a new device
  758. *
  759. * @devp: returns a pointer of a new device structure. With driver model this
  760. * is a device pointer, but with legacy USB this pointer is
  761. * driver-specific.
  762. * @return 0 if OK, -ENOSPC if we have found out of room for new devices
  763. */
  764. int usb_alloc_new_device(void *controller, struct usb_device **devp);
  765. /**
  766. * usb_free_device() - Free a partially-inited device
  767. *
  768. * This is an internal function. It is used to reverse the action of
  769. * usb_alloc_new_device() when we hit a problem during init.
  770. */
  771. void usb_free_device(void *controller);
  772. int usb_new_device(struct usb_device *dev);
  773. int usb_alloc_device(struct usb_device *dev);
  774. /**
  775. * usb_get_max_xfer_size() - Get HCD's maximum transfer bytes
  776. *
  777. * The HCD may have limitation on the maximum bytes to be transferred
  778. * in a USB transfer. USB class driver needs to be aware of this.
  779. *
  780. * @dev: USB device
  781. * @size: maximum transfer bytes
  782. * @return 0 if OK, -ve on error
  783. */
  784. int usb_get_max_xfer_size(struct usb_device *dev, size_t *size);
  785. /**
  786. * usb_show_tree() - show the USB device tree
  787. *
  788. * This shows a list of active USB devices along with basic information about
  789. * each.
  790. */
  791. void usb_show_tree(void);
  792. #define USB_DEV_PLUGED 0
  793. #define USB_DEV_UNPLUGED 1
  794. int usb_wait_dev_pluged(uint32_t timeout);
  795. #endif /*_USB_H_ */