usb.h 35 KB

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