usb.h 36 KB

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