usb.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  1. /*
  2. * (C) Copyright 2001
  3. * Denis Peter, MPL AG Switzerland
  4. *
  5. * Adapted for U-Boot driver model
  6. * (C) Copyright 2015 Google, Inc
  7. *
  8. * SPDX-License-Identifier: GPL-2.0+
  9. * Note: Part of this code has been derived from linux
  10. *
  11. */
  12. #ifndef _USB_H_
  13. #define _USB_H_
  14. #include <fdtdec.h>
  15. #include <usb_defs.h>
  16. #include <linux/usb/ch9.h>
  17. #include <asm/cache.h>
  18. #include <part.h>
  19. /*
  20. * The EHCI spec says that we must align to at least 32 bytes. However,
  21. * some platforms require larger alignment.
  22. */
  23. #if ARCH_DMA_MINALIGN > 32
  24. #define USB_DMA_MINALIGN ARCH_DMA_MINALIGN
  25. #else
  26. #define USB_DMA_MINALIGN 32
  27. #endif
  28. /* Everything is aribtrary */
  29. #define USB_ALTSETTINGALLOC 4
  30. #define USB_MAXALTSETTING 128 /* Hard limit */
  31. #define USB_MAX_DEVICE 32
  32. #define USB_MAXCONFIG 8
  33. #define USB_MAXINTERFACES 8
  34. #define USB_MAXENDPOINTS 16
  35. #define USB_MAXCHILDREN 8 /* This is arbitrary */
  36. #define USB_MAX_HUB 16
  37. #define USB_CNTL_TIMEOUT 100 /* 100ms timeout */
  38. /*
  39. * This is the timeout to allow for submitting an urb in ms. We allow more
  40. * time for a BULK device to react - some are slow.
  41. */
  42. #define USB_TIMEOUT_MS(pipe) (usb_pipebulk(pipe) ? 5000 : 1000)
  43. /* device request (setup) */
  44. struct devrequest {
  45. __u8 requesttype;
  46. __u8 request;
  47. __le16 value;
  48. __le16 index;
  49. __le16 length;
  50. } __attribute__ ((packed));
  51. /* Interface */
  52. struct usb_interface {
  53. struct usb_interface_descriptor desc;
  54. __u8 no_of_ep;
  55. __u8 num_altsetting;
  56. __u8 act_altsetting;
  57. struct usb_endpoint_descriptor ep_desc[USB_MAXENDPOINTS];
  58. /*
  59. * Super Speed Device will have Super Speed Endpoint
  60. * Companion Descriptor (section 9.6.7 of usb 3.0 spec)
  61. * Revision 1.0 June 6th 2011
  62. */
  63. struct usb_ss_ep_comp_descriptor ss_ep_comp_desc[USB_MAXENDPOINTS];
  64. } __attribute__ ((packed));
  65. /* Configuration information.. */
  66. struct usb_config {
  67. struct usb_config_descriptor desc;
  68. __u8 no_of_if; /* number of interfaces */
  69. struct usb_interface if_desc[USB_MAXINTERFACES];
  70. } __attribute__ ((packed));
  71. enum {
  72. /* Maximum packet size; encoded as 0,1,2,3 = 8,16,32,64 */
  73. PACKET_SIZE_8 = 0,
  74. PACKET_SIZE_16 = 1,
  75. PACKET_SIZE_32 = 2,
  76. PACKET_SIZE_64 = 3,
  77. };
  78. /**
  79. * struct usb_device - information about a USB device
  80. *
  81. * With driver model both UCLASS_USB (the USB controllers) and UCLASS_USB_HUB
  82. * (the hubs) have this as parent data. Hubs are children of controllers or
  83. * other hubs and there is always a single root hub for each controller.
  84. * Therefore struct usb_device can always be accessed with
  85. * dev_get_parentdata(dev), where dev is a USB device.
  86. *
  87. * Pointers exist for obtaining both the device (could be any uclass) and
  88. * controller (UCLASS_USB) from this structure. The controller does not have
  89. * a struct usb_device since it is not a device.
  90. */
  91. struct usb_device {
  92. int devnum; /* Device number on USB bus */
  93. int speed; /* full/low/high */
  94. char mf[32]; /* manufacturer */
  95. char prod[32]; /* product */
  96. char serial[32]; /* serial number */
  97. /* Maximum packet size; one of: PACKET_SIZE_* */
  98. int maxpacketsize;
  99. /* one bit for each endpoint ([0] = IN, [1] = OUT) */
  100. unsigned int toggle[2];
  101. /* endpoint halts; one bit per endpoint # & direction;
  102. * [0] = IN, [1] = OUT
  103. */
  104. unsigned int halted[2];
  105. int epmaxpacketin[16]; /* INput endpoint specific maximums */
  106. int epmaxpacketout[16]; /* OUTput endpoint specific maximums */
  107. int configno; /* selected config number */
  108. /* Device Descriptor */
  109. struct usb_device_descriptor descriptor
  110. __attribute__((aligned(ARCH_DMA_MINALIGN)));
  111. struct usb_config config; /* config descriptor */
  112. int have_langid; /* whether string_langid is valid yet */
  113. int string_langid; /* language ID for strings */
  114. int (*irq_handle)(struct usb_device *dev);
  115. unsigned long irq_status;
  116. int irq_act_len; /* transfered 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; /* transfered 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. #if defined(CONFIG_USB_UHCI) || defined(CONFIG_USB_OHCI) || \
  154. defined(CONFIG_USB_EHCI) || defined(CONFIG_USB_OHCI_NEW) || \
  155. defined(CONFIG_USB_SL811HS) || defined(CONFIG_USB_ISP116X_HCD) || \
  156. defined(CONFIG_USB_R8A66597_HCD) || defined(CONFIG_USB_DAVINCI) || \
  157. defined(CONFIG_USB_OMAP3) || defined(CONFIG_USB_DA8XX) || \
  158. defined(CONFIG_USB_BLACKFIN) || defined(CONFIG_USB_AM35X) || \
  159. defined(CONFIG_USB_MUSB_DSPS) || defined(CONFIG_USB_MUSB_AM35X) || \
  160. defined(CONFIG_USB_MUSB_OMAP2PLUS) || defined(CONFIG_USB_MUSB_SUNXI) || \
  161. defined(CONFIG_USB_XHCI) || defined(CONFIG_USB_DWC2)
  162. int usb_lowlevel_init(int index, enum usb_init_type init, void **controller);
  163. int usb_lowlevel_stop(int index);
  164. #if defined(CONFIG_MUSB_HOST) || defined(CONFIG_DM_USB)
  165. int usb_reset_root_port(void);
  166. #else
  167. #define usb_reset_root_port()
  168. #endif
  169. int submit_bulk_msg(struct usb_device *dev, unsigned long pipe,
  170. void *buffer, int transfer_len);
  171. int submit_control_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
  172. int transfer_len, struct devrequest *setup);
  173. int submit_int_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
  174. int transfer_len, int interval);
  175. #if defined CONFIG_USB_EHCI || defined CONFIG_MUSB_HOST
  176. struct int_queue *create_int_queue(struct usb_device *dev, unsigned long pipe,
  177. int queuesize, int elementsize, void *buffer, int interval);
  178. int destroy_int_queue(struct usb_device *dev, struct int_queue *queue);
  179. void *poll_int_queue(struct usb_device *dev, struct int_queue *queue);
  180. #endif
  181. /* Defines */
  182. #define USB_UHCI_VEND_ID 0x8086
  183. #define USB_UHCI_DEV_ID 0x7112
  184. /*
  185. * PXA25x can only act as USB device. There are drivers
  186. * which works with USB CDC gadgets implementations.
  187. * Some of them have common routines which can be used
  188. * in boards init functions e.g. udc_disconnect() used for
  189. * forced device disconnection from host.
  190. */
  191. #elif defined(CONFIG_USB_GADGET_PXA2XX)
  192. extern void udc_disconnect(void);
  193. #endif
  194. /*
  195. * board-specific hardware initialization, called by
  196. * usb drivers and u-boot commands
  197. *
  198. * @param index USB controller number
  199. * @param init initializes controller as USB host or device
  200. */
  201. int board_usb_init(int index, enum usb_init_type init);
  202. /*
  203. * can be used to clean up after failed USB initialization attempt
  204. * vide: board_usb_init()
  205. *
  206. * @param index USB controller number for selective cleanup
  207. * @param init usb_init_type passed to board_usb_init()
  208. */
  209. int board_usb_cleanup(int index, enum usb_init_type init);
  210. #ifdef CONFIG_USB_STORAGE
  211. #define USB_MAX_STOR_DEV 5
  212. block_dev_desc_t *usb_stor_get_dev(int index);
  213. int usb_stor_scan(int mode);
  214. int usb_stor_info(void);
  215. #endif
  216. #ifdef CONFIG_USB_HOST_ETHER
  217. #define USB_MAX_ETH_DEV 5
  218. int usb_host_eth_scan(int mode);
  219. #endif
  220. #ifdef CONFIG_USB_KEYBOARD
  221. int drv_usb_kbd_init(void);
  222. int usb_kbd_deregister(int force);
  223. #endif
  224. /* routines */
  225. int usb_init(void); /* initialize the USB Controller */
  226. int usb_stop(void); /* stop the USB Controller */
  227. int usb_set_protocol(struct usb_device *dev, int ifnum, int protocol);
  228. int usb_set_idle(struct usb_device *dev, int ifnum, int duration,
  229. int report_id);
  230. int usb_control_msg(struct usb_device *dev, unsigned int pipe,
  231. unsigned char request, unsigned char requesttype,
  232. unsigned short value, unsigned short index,
  233. void *data, unsigned short size, int timeout);
  234. int usb_bulk_msg(struct usb_device *dev, unsigned int pipe,
  235. void *data, int len, int *actual_length, int timeout);
  236. int usb_submit_int_msg(struct usb_device *dev, unsigned long pipe,
  237. void *buffer, int transfer_len, int interval);
  238. int usb_disable_asynch(int disable);
  239. int usb_maxpacket(struct usb_device *dev, unsigned long pipe);
  240. int usb_get_configuration_no(struct usb_device *dev, unsigned char *buffer,
  241. int cfgno);
  242. int usb_get_report(struct usb_device *dev, int ifnum, unsigned char type,
  243. unsigned char id, void *buf, int size);
  244. int usb_get_class_descriptor(struct usb_device *dev, int ifnum,
  245. unsigned char type, unsigned char id, void *buf,
  246. int size);
  247. int usb_clear_halt(struct usb_device *dev, int pipe);
  248. int usb_string(struct usb_device *dev, int index, char *buf, size_t size);
  249. int usb_set_interface(struct usb_device *dev, int interface, int alternate);
  250. /* big endian -> little endian conversion */
  251. /* some CPUs are already little endian e.g. the ARM920T */
  252. #define __swap_16(x) \
  253. ({ unsigned short x_ = (unsigned short)x; \
  254. (unsigned short)( \
  255. ((x_ & 0x00FFU) << 8) | ((x_ & 0xFF00U) >> 8)); \
  256. })
  257. #define __swap_32(x) \
  258. ({ unsigned long x_ = (unsigned long)x; \
  259. (unsigned long)( \
  260. ((x_ & 0x000000FFUL) << 24) | \
  261. ((x_ & 0x0000FF00UL) << 8) | \
  262. ((x_ & 0x00FF0000UL) >> 8) | \
  263. ((x_ & 0xFF000000UL) >> 24)); \
  264. })
  265. #ifdef __LITTLE_ENDIAN
  266. # define swap_16(x) (x)
  267. # define swap_32(x) (x)
  268. #else
  269. # define swap_16(x) __swap_16(x)
  270. # define swap_32(x) __swap_32(x)
  271. #endif
  272. /*
  273. * Calling this entity a "pipe" is glorifying it. A USB pipe
  274. * is something embarrassingly simple: it basically consists
  275. * of the following information:
  276. * - device number (7 bits)
  277. * - endpoint number (4 bits)
  278. * - current Data0/1 state (1 bit)
  279. * - direction (1 bit)
  280. * - speed (2 bits)
  281. * - max packet size (2 bits: 8, 16, 32 or 64)
  282. * - pipe type (2 bits: control, interrupt, bulk, isochronous)
  283. *
  284. * That's 18 bits. Really. Nothing more. And the USB people have
  285. * documented these eighteen bits as some kind of glorious
  286. * virtual data structure.
  287. *
  288. * Let's not fall in that trap. We'll just encode it as a simple
  289. * unsigned int. The encoding is:
  290. *
  291. * - max size: bits 0-1 (00 = 8, 01 = 16, 10 = 32, 11 = 64)
  292. * - direction: bit 7 (0 = Host-to-Device [Out],
  293. * (1 = Device-to-Host [In])
  294. * - device: bits 8-14
  295. * - endpoint: bits 15-18
  296. * - Data0/1: bit 19
  297. * - pipe type: bits 30-31 (00 = isochronous, 01 = interrupt,
  298. * 10 = control, 11 = bulk)
  299. *
  300. * Why? Because it's arbitrary, and whatever encoding we select is really
  301. * up to us. This one happens to share a lot of bit positions with the UHCI
  302. * specification, so that much of the uhci driver can just mask the bits
  303. * appropriately.
  304. */
  305. /* Create various pipes... */
  306. #define create_pipe(dev,endpoint) \
  307. (((dev)->devnum << 8) | ((endpoint) << 15) | \
  308. (dev)->maxpacketsize)
  309. #define default_pipe(dev) ((dev)->speed << 26)
  310. #define usb_sndctrlpipe(dev, endpoint) ((PIPE_CONTROL << 30) | \
  311. create_pipe(dev, endpoint))
  312. #define usb_rcvctrlpipe(dev, endpoint) ((PIPE_CONTROL << 30) | \
  313. create_pipe(dev, endpoint) | \
  314. USB_DIR_IN)
  315. #define usb_sndisocpipe(dev, endpoint) ((PIPE_ISOCHRONOUS << 30) | \
  316. create_pipe(dev, endpoint))
  317. #define usb_rcvisocpipe(dev, endpoint) ((PIPE_ISOCHRONOUS << 30) | \
  318. create_pipe(dev, endpoint) | \
  319. USB_DIR_IN)
  320. #define usb_sndbulkpipe(dev, endpoint) ((PIPE_BULK << 30) | \
  321. create_pipe(dev, endpoint))
  322. #define usb_rcvbulkpipe(dev, endpoint) ((PIPE_BULK << 30) | \
  323. create_pipe(dev, endpoint) | \
  324. USB_DIR_IN)
  325. #define usb_sndintpipe(dev, endpoint) ((PIPE_INTERRUPT << 30) | \
  326. create_pipe(dev, endpoint))
  327. #define usb_rcvintpipe(dev, endpoint) ((PIPE_INTERRUPT << 30) | \
  328. create_pipe(dev, endpoint) | \
  329. USB_DIR_IN)
  330. #define usb_snddefctrl(dev) ((PIPE_CONTROL << 30) | \
  331. default_pipe(dev))
  332. #define usb_rcvdefctrl(dev) ((PIPE_CONTROL << 30) | \
  333. default_pipe(dev) | \
  334. USB_DIR_IN)
  335. /* The D0/D1 toggle bits */
  336. #define usb_gettoggle(dev, ep, out) (((dev)->toggle[out] >> ep) & 1)
  337. #define usb_dotoggle(dev, ep, out) ((dev)->toggle[out] ^= (1 << ep))
  338. #define usb_settoggle(dev, ep, out, bit) ((dev)->toggle[out] = \
  339. ((dev)->toggle[out] & \
  340. ~(1 << ep)) | ((bit) << ep))
  341. /* Endpoint halt control/status */
  342. #define usb_endpoint_out(ep_dir) (((ep_dir >> 7) & 1) ^ 1)
  343. #define usb_endpoint_halt(dev, ep, out) ((dev)->halted[out] |= (1 << (ep)))
  344. #define usb_endpoint_running(dev, ep, out) ((dev)->halted[out] &= ~(1 << (ep)))
  345. #define usb_endpoint_halted(dev, ep, out) ((dev)->halted[out] & (1 << (ep)))
  346. #define usb_packetid(pipe) (((pipe) & USB_DIR_IN) ? USB_PID_IN : \
  347. USB_PID_OUT)
  348. #define usb_pipeout(pipe) ((((pipe) >> 7) & 1) ^ 1)
  349. #define usb_pipein(pipe) (((pipe) >> 7) & 1)
  350. #define usb_pipedevice(pipe) (((pipe) >> 8) & 0x7f)
  351. #define usb_pipe_endpdev(pipe) (((pipe) >> 8) & 0x7ff)
  352. #define usb_pipeendpoint(pipe) (((pipe) >> 15) & 0xf)
  353. #define usb_pipedata(pipe) (((pipe) >> 19) & 1)
  354. #define usb_pipetype(pipe) (((pipe) >> 30) & 3)
  355. #define usb_pipeisoc(pipe) (usb_pipetype((pipe)) == PIPE_ISOCHRONOUS)
  356. #define usb_pipeint(pipe) (usb_pipetype((pipe)) == PIPE_INTERRUPT)
  357. #define usb_pipecontrol(pipe) (usb_pipetype((pipe)) == PIPE_CONTROL)
  358. #define usb_pipebulk(pipe) (usb_pipetype((pipe)) == PIPE_BULK)
  359. #define usb_pipe_ep_index(pipe) \
  360. usb_pipecontrol(pipe) ? (usb_pipeendpoint(pipe) * 2) : \
  361. ((usb_pipeendpoint(pipe) * 2) - \
  362. (usb_pipein(pipe) ? 0 : 1))
  363. /*************************************************************************
  364. * Hub Stuff
  365. */
  366. struct usb_port_status {
  367. unsigned short wPortStatus;
  368. unsigned short wPortChange;
  369. } __attribute__ ((packed));
  370. struct usb_hub_status {
  371. unsigned short wHubStatus;
  372. unsigned short wHubChange;
  373. } __attribute__ ((packed));
  374. /* Hub descriptor */
  375. struct usb_hub_descriptor {
  376. unsigned char bLength;
  377. unsigned char bDescriptorType;
  378. unsigned char bNbrPorts;
  379. unsigned short wHubCharacteristics;
  380. unsigned char bPwrOn2PwrGood;
  381. unsigned char bHubContrCurrent;
  382. unsigned char DeviceRemovable[(USB_MAXCHILDREN+1+7)/8];
  383. unsigned char PortPowerCtrlMask[(USB_MAXCHILDREN+1+7)/8];
  384. /* DeviceRemovable and PortPwrCtrlMask want to be variable-length
  385. bitmaps that hold max 255 entries. (bit0 is ignored) */
  386. } __attribute__ ((packed));
  387. struct usb_hub_device {
  388. struct usb_device *pusb_dev;
  389. struct usb_hub_descriptor desc;
  390. };
  391. #ifdef CONFIG_DM_USB
  392. /**
  393. * struct usb_platdata - Platform data about a USB controller
  394. *
  395. * Given a USB controller (UCLASS_USB) dev this is dev_get_platdata(dev)
  396. */
  397. struct usb_platdata {
  398. enum usb_init_type init_type;
  399. };
  400. /**
  401. * struct usb_dev_platdata - Platform data about a USB device
  402. *
  403. * Given a USB device dev this structure is dev_get_parent_platdata(dev).
  404. * This is used by sandbox to provide emulation data also.
  405. *
  406. * @id: ID used to match this device
  407. * @speed: Stores the speed associated with a USB device
  408. * @devnum: Device address on the USB bus
  409. * @slot_id: USB3 slot ID, which is separate from the device address
  410. * @portnr: Port number of this device on its parent hub, numbered from 1
  411. * (0 mean this device is the root hub)
  412. * @strings: List of descriptor strings (for sandbox emulation purposes)
  413. * @desc_list: List of descriptors (for sandbox emulation purposes)
  414. */
  415. struct usb_dev_platdata {
  416. struct usb_device_id id;
  417. enum usb_device_speed speed;
  418. int devnum;
  419. int slot_id;
  420. int portnr; /* Hub port number, 1..n */
  421. #ifdef CONFIG_SANDBOX
  422. struct usb_string *strings;
  423. /* NULL-terminated list of descriptor pointers */
  424. struct usb_generic_descriptor **desc_list;
  425. #endif
  426. int configno;
  427. };
  428. /**
  429. * struct usb_bus_priv - information about the USB controller
  430. *
  431. * Given a USB controller (UCLASS_USB) 'dev', this is
  432. * dev_get_uclass_priv(dev).
  433. *
  434. * @next_addr: Next device address to allocate minus 1. Incremented by 1
  435. * each time a new device address is set, so this holds the
  436. * number of devices on the bus
  437. * @desc_before_addr: true if we can read a device descriptor before it
  438. * has been assigned an address. For XHCI this is not possible
  439. * so this will be false.
  440. */
  441. struct usb_bus_priv {
  442. int next_addr;
  443. bool desc_before_addr;
  444. };
  445. /**
  446. * struct dm_usb_ops - USB controller operations
  447. *
  448. * This defines the operations supoorted on a USB controller. Common
  449. * arguments are:
  450. *
  451. * @bus: USB bus (i.e. controller), which is in UCLASS_USB.
  452. * @udev: USB device parent data. Controllers are not expected to need
  453. * this, since the device address on the bus is encoded in @pipe.
  454. * It is used for sandbox, and can be handy for debugging and
  455. * logging.
  456. * @pipe: An assortment of bitfields which provide address and packet
  457. * type information. See create_pipe() above for encoding
  458. * details
  459. * @buffer: A buffer to use for sending/receiving. This should be
  460. * DMA-aligned.
  461. * @length: Buffer length in bytes
  462. */
  463. struct dm_usb_ops {
  464. /**
  465. * control() - Send a control message
  466. *
  467. * Most parameters are as above.
  468. *
  469. * @setup: Additional setup information required by the message
  470. */
  471. int (*control)(struct udevice *bus, struct usb_device *udev,
  472. unsigned long pipe, void *buffer, int length,
  473. struct devrequest *setup);
  474. /**
  475. * bulk() - Send a bulk message
  476. *
  477. * Parameters are as above.
  478. */
  479. int (*bulk)(struct udevice *bus, struct usb_device *udev,
  480. unsigned long pipe, void *buffer, int length);
  481. /**
  482. * interrupt() - Send an interrupt message
  483. *
  484. * Most parameters are as above.
  485. *
  486. * @interval: Interrupt interval
  487. */
  488. int (*interrupt)(struct udevice *bus, struct usb_device *udev,
  489. unsigned long pipe, void *buffer, int length,
  490. int interval);
  491. /**
  492. * alloc_device() - Allocate a new device context (XHCI)
  493. *
  494. * Before sending packets to a new device on an XHCI bus, a device
  495. * context must be created. If this method is not NULL it will be
  496. * called before the device is enumerated (even before its descriptor
  497. * is read). This should be NULL for EHCI, which does not need this.
  498. */
  499. int (*alloc_device)(struct udevice *bus, struct usb_device *udev);
  500. };
  501. #define usb_get_ops(dev) ((struct dm_usb_ops *)(dev)->driver->ops)
  502. #define usb_get_emul_ops(dev) ((struct dm_usb_ops *)(dev)->driver->ops)
  503. #ifdef CONFIG_MUSB_HOST
  504. int usb_reset_root_port(void);
  505. #endif
  506. /**
  507. * usb_get_dev_index() - look up a device index number
  508. *
  509. * Look up devices using their index number (starting at 0). This works since
  510. * in U-Boot device addresses are allocated starting at 1 with no gaps.
  511. *
  512. * TODO(sjg@chromium.org): Remove this function when usb_ether.c is modified
  513. * to work better with driver model.
  514. *
  515. * @bus: USB bus to check
  516. * @index: Index number of device to find (0=first). This is just the
  517. * device address less 1.
  518. */
  519. struct usb_device *usb_get_dev_index(struct udevice *bus, int index);
  520. /**
  521. * usb_legacy_port_reset() - Legacy function to reset a hub port
  522. *
  523. * @hub: Hub device
  524. * @portnr: Port number (1=first)
  525. */
  526. int usb_legacy_port_reset(struct usb_device *hub, int portnr);
  527. /**
  528. * usb_setup_device() - set up a device ready for use
  529. *
  530. * @dev: USB device pointer. This need not be a real device - it is
  531. * common for it to just be a local variable with its ->dev
  532. * member (i.e. @dev->dev) set to the parent device
  533. * @do_read: true to read the device descriptor before an address is set
  534. * (should be false for XHCI buses, true otherwise)
  535. * @parent: Parent device (either UCLASS_USB or UCLASS_USB_HUB)
  536. * @portnr: Port number on hub (1=first) or 0 for none
  537. * @return 0 if OK, -ve on error */
  538. int usb_setup_device(struct usb_device *dev, bool do_read,
  539. struct usb_device *parent, int portnr);
  540. /**
  541. * usb_hub_scan() - Scan a hub and find its devices
  542. *
  543. * @hub: Hub device to scan
  544. */
  545. int usb_hub_scan(struct udevice *hub);
  546. /**
  547. * usb_scan_device() - Scan a device on a bus
  548. *
  549. * Scan a device on a bus. It has already been detected and is ready to
  550. * be enumerated. This may be either the root hub (@parent is a bus) or a
  551. * normal device (@parent is a hub)
  552. *
  553. * @parent: Parent device
  554. * @port: Hub port number (numbered from 1)
  555. * @speed: USB speed to use for this device
  556. * @devp: Returns pointer to device if all is well
  557. * @return 0 if OK, -ve on error
  558. */
  559. int usb_scan_device(struct udevice *parent, int port,
  560. enum usb_device_speed speed, struct udevice **devp);
  561. /**
  562. * usb_get_bus() - Find the bus for a device
  563. *
  564. * Search up through parents to find the bus this device is connected to. This
  565. * will be a device with uclass UCLASS_USB.
  566. *
  567. * @dev: Device to check
  568. * @busp: Returns bus, or NULL if not found
  569. * @return 0 if OK, -EXDEV is somehow this bus does not have a controller (this
  570. * indicates a critical error in the USB stack
  571. */
  572. int usb_get_bus(struct udevice *dev, struct udevice **busp);
  573. /**
  574. * usb_select_config() - Set up a device ready for use
  575. *
  576. * This function assumes that the device already has an address and a driver
  577. * bound, and is ready to be set up.
  578. *
  579. * This re-reads the device and configuration descriptors and sets the
  580. * configuration
  581. *
  582. * @dev: Device to set up
  583. */
  584. int usb_select_config(struct usb_device *dev);
  585. /**
  586. * usb_child_pre_probe() - Pre-probe function for USB devices
  587. *
  588. * This is called on all children of hubs and USB controllers (i.e. UCLASS_USB
  589. * and UCLASS_USB_HUB) when a new device is about to be probed. It sets up the
  590. * device from the saved platform data and calls usb_select_config() to
  591. * finish set up.
  592. *
  593. * Once this is done, the device's normal driver can take over, knowing the
  594. * device is accessible on the USB bus.
  595. *
  596. * This function is for use only by the internal USB stack.
  597. *
  598. * @dev: Device to set up
  599. */
  600. int usb_child_pre_probe(struct udevice *dev);
  601. struct ehci_ctrl;
  602. /**
  603. * usb_setup_ehci_gadget() - Set up a USB device as a gadget
  604. *
  605. * TODO(sjg@chromium.org): Tidy this up when USB gadgets can use driver model
  606. *
  607. * This provides a way to tell a controller to start up as a USB device
  608. * instead of as a host. It is untested.
  609. */
  610. int usb_setup_ehci_gadget(struct ehci_ctrl **ctlrp);
  611. /**
  612. * usb_stor_reset() - Prepare to scan USB storage devices
  613. *
  614. * Empty the list of USB storage devices in preparation for scanning them.
  615. * This must be called before a USB scan.
  616. */
  617. void usb_stor_reset(void);
  618. #else /* !CONFIG_DM_USB */
  619. struct usb_device *usb_get_dev_index(int index);
  620. #endif
  621. bool usb_device_has_child_on_port(struct usb_device *parent, int port);
  622. int usb_hub_probe(struct usb_device *dev, int ifnum);
  623. void usb_hub_reset(void);
  624. int hub_port_reset(struct usb_device *dev, int port,
  625. unsigned short *portstat);
  626. /**
  627. * usb_alloc_new_device() - Allocate a new device
  628. *
  629. * @devp: returns a pointer of a new device structure. With driver model this
  630. * is a device pointer, but with legacy USB this pointer is
  631. * driver-specific.
  632. * @return 0 if OK, -ENOSPC if we have found out of room for new devices
  633. */
  634. int usb_alloc_new_device(struct udevice *controller, struct usb_device **devp);
  635. /**
  636. * usb_free_device() - Free a partially-inited device
  637. *
  638. * This is an internal function. It is used to reverse the action of
  639. * usb_alloc_new_device() when we hit a problem during init.
  640. */
  641. void usb_free_device(struct udevice *controller);
  642. int usb_new_device(struct usb_device *dev);
  643. int usb_alloc_device(struct usb_device *dev);
  644. #endif /*_USB_H_ */