usb.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. /*
  2. * (C) Copyright 2001
  3. * Denis Peter, MPL AG Switzerland
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. * Note: Part of this code has been derived from linux
  7. *
  8. */
  9. #ifndef _USB_H_
  10. #define _USB_H_
  11. #include <usb_defs.h>
  12. #include <linux/usb/ch9.h>
  13. #include <asm/cache.h>
  14. #include <part.h>
  15. /*
  16. * The EHCI spec says that we must align to at least 32 bytes. However,
  17. * some platforms require larger alignment.
  18. */
  19. #if ARCH_DMA_MINALIGN > 32
  20. #define USB_DMA_MINALIGN ARCH_DMA_MINALIGN
  21. #else
  22. #define USB_DMA_MINALIGN 32
  23. #endif
  24. /* Everything is aribtrary */
  25. #define USB_ALTSETTINGALLOC 4
  26. #define USB_MAXALTSETTING 128 /* Hard limit */
  27. #define USB_MAX_DEVICE 32
  28. #define USB_MAXCONFIG 8
  29. #define USB_MAXINTERFACES 8
  30. #define USB_MAXENDPOINTS 16
  31. #define USB_MAXCHILDREN 8 /* This is arbitrary */
  32. #define USB_MAX_HUB 16
  33. #define USB_CNTL_TIMEOUT 100 /* 100ms timeout */
  34. /*
  35. * This is the timeout to allow for submitting an urb in ms. We allow more
  36. * time for a BULK device to react - some are slow.
  37. */
  38. #define USB_TIMEOUT_MS(pipe) (usb_pipebulk(pipe) ? 5000 : 1000)
  39. /* device request (setup) */
  40. struct devrequest {
  41. unsigned char requesttype;
  42. unsigned char request;
  43. unsigned short value;
  44. unsigned short index;
  45. unsigned short length;
  46. } __attribute__ ((packed));
  47. /* Interface */
  48. struct usb_interface {
  49. struct usb_interface_descriptor desc;
  50. unsigned char no_of_ep;
  51. unsigned char num_altsetting;
  52. unsigned char act_altsetting;
  53. struct usb_endpoint_descriptor ep_desc[USB_MAXENDPOINTS];
  54. /*
  55. * Super Speed Device will have Super Speed Endpoint
  56. * Companion Descriptor (section 9.6.7 of usb 3.0 spec)
  57. * Revision 1.0 June 6th 2011
  58. */
  59. struct usb_ss_ep_comp_descriptor ss_ep_comp_desc[USB_MAXENDPOINTS];
  60. } __attribute__ ((packed));
  61. /* Configuration information.. */
  62. struct usb_config {
  63. struct usb_config_descriptor desc;
  64. unsigned char no_of_if; /* number of interfaces */
  65. struct usb_interface if_desc[USB_MAXINTERFACES];
  66. } __attribute__ ((packed));
  67. enum {
  68. /* Maximum packet size; encoded as 0,1,2,3 = 8,16,32,64 */
  69. PACKET_SIZE_8 = 0,
  70. PACKET_SIZE_16 = 1,
  71. PACKET_SIZE_32 = 2,
  72. PACKET_SIZE_64 = 3,
  73. };
  74. struct usb_device {
  75. int devnum; /* Device number on USB bus */
  76. int speed; /* full/low/high */
  77. char mf[32]; /* manufacturer */
  78. char prod[32]; /* product */
  79. char serial[32]; /* serial number */
  80. /* Maximum packet size; one of: PACKET_SIZE_* */
  81. int maxpacketsize;
  82. /* one bit for each endpoint ([0] = IN, [1] = OUT) */
  83. unsigned int toggle[2];
  84. /* endpoint halts; one bit per endpoint # & direction;
  85. * [0] = IN, [1] = OUT
  86. */
  87. unsigned int halted[2];
  88. int epmaxpacketin[16]; /* INput endpoint specific maximums */
  89. int epmaxpacketout[16]; /* OUTput endpoint specific maximums */
  90. int configno; /* selected config number */
  91. /* Device Descriptor */
  92. struct usb_device_descriptor descriptor
  93. __attribute__((aligned(ARCH_DMA_MINALIGN)));
  94. struct usb_config config; /* config descriptor */
  95. int have_langid; /* whether string_langid is valid yet */
  96. int string_langid; /* language ID for strings */
  97. int (*irq_handle)(struct usb_device *dev);
  98. unsigned long irq_status;
  99. int irq_act_len; /* transfered bytes */
  100. void *privptr;
  101. /*
  102. * Child devices - if this is a hub device
  103. * Each instance needs its own set of data structures.
  104. */
  105. unsigned long status;
  106. int act_len; /* transfered bytes */
  107. int maxchild; /* Number of ports if hub */
  108. int portnr;
  109. struct usb_device *parent;
  110. struct usb_device *children[USB_MAXCHILDREN];
  111. void *controller; /* hardware controller private data */
  112. /* slot_id - for xHCI enabled devices */
  113. unsigned int slot_id;
  114. };
  115. struct int_queue;
  116. /*
  117. * You can initialize platform's USB host or device
  118. * ports by passing this enum as an argument to
  119. * board_usb_init().
  120. */
  121. enum usb_init_type {
  122. USB_INIT_HOST,
  123. USB_INIT_DEVICE
  124. };
  125. /**********************************************************************
  126. * this is how the lowlevel part communicate with the outer world
  127. */
  128. #if defined(CONFIG_USB_UHCI) || defined(CONFIG_USB_OHCI) || \
  129. defined(CONFIG_USB_EHCI) || defined(CONFIG_USB_OHCI_NEW) || \
  130. defined(CONFIG_USB_SL811HS) || defined(CONFIG_USB_ISP116X_HCD) || \
  131. defined(CONFIG_USB_R8A66597_HCD) || defined(CONFIG_USB_DAVINCI) || \
  132. defined(CONFIG_USB_OMAP3) || defined(CONFIG_USB_DA8XX) || \
  133. defined(CONFIG_USB_BLACKFIN) || defined(CONFIG_USB_AM35X) || \
  134. defined(CONFIG_USB_MUSB_DSPS) || defined(CONFIG_USB_MUSB_AM35X) || \
  135. defined(CONFIG_USB_MUSB_OMAP2PLUS) || defined(CONFIG_USB_XHCI) || \
  136. defined(CONFIG_USB_DWC2)
  137. int usb_lowlevel_init(int index, enum usb_init_type init, void **controller);
  138. int usb_lowlevel_stop(int index);
  139. int submit_bulk_msg(struct usb_device *dev, unsigned long pipe,
  140. void *buffer, int transfer_len);
  141. int submit_control_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
  142. int transfer_len, struct devrequest *setup);
  143. int submit_int_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
  144. int transfer_len, int interval);
  145. #ifdef CONFIG_USB_EHCI /* Only the ehci code has pollable int support */
  146. struct int_queue *create_int_queue(struct usb_device *dev, unsigned long pipe,
  147. int queuesize, int elementsize, void *buffer, int interval);
  148. int destroy_int_queue(struct usb_device *dev, struct int_queue *queue);
  149. void *poll_int_queue(struct usb_device *dev, struct int_queue *queue);
  150. #endif
  151. /* Defines */
  152. #define USB_UHCI_VEND_ID 0x8086
  153. #define USB_UHCI_DEV_ID 0x7112
  154. /*
  155. * PXA25x can only act as USB device. There are drivers
  156. * which works with USB CDC gadgets implementations.
  157. * Some of them have common routines which can be used
  158. * in boards init functions e.g. udc_disconnect() used for
  159. * forced device disconnection from host.
  160. */
  161. #elif defined(CONFIG_USB_GADGET_PXA2XX)
  162. extern void udc_disconnect(void);
  163. #endif
  164. /*
  165. * board-specific hardware initialization, called by
  166. * usb drivers and u-boot commands
  167. *
  168. * @param index USB controller number
  169. * @param init initializes controller as USB host or device
  170. */
  171. int board_usb_init(int index, enum usb_init_type init);
  172. /*
  173. * can be used to clean up after failed USB initialization attempt
  174. * vide: board_usb_init()
  175. *
  176. * @param index USB controller number for selective cleanup
  177. * @param init usb_init_type passed to board_usb_init()
  178. */
  179. int board_usb_cleanup(int index, enum usb_init_type init);
  180. #ifdef CONFIG_USB_STORAGE
  181. #define USB_MAX_STOR_DEV 5
  182. block_dev_desc_t *usb_stor_get_dev(int index);
  183. int usb_stor_scan(int mode);
  184. int usb_stor_info(void);
  185. #endif
  186. #ifdef CONFIG_USB_HOST_ETHER
  187. #define USB_MAX_ETH_DEV 5
  188. int usb_host_eth_scan(int mode);
  189. #endif
  190. #ifdef CONFIG_USB_KEYBOARD
  191. int drv_usb_kbd_init(void);
  192. int usb_kbd_deregister(int force);
  193. #endif
  194. /* routines */
  195. int usb_init(void); /* initialize the USB Controller */
  196. int usb_stop(void); /* stop the USB Controller */
  197. int usb_set_protocol(struct usb_device *dev, int ifnum, int protocol);
  198. int usb_set_idle(struct usb_device *dev, int ifnum, int duration,
  199. int report_id);
  200. struct usb_device *usb_get_dev_index(int index);
  201. int usb_control_msg(struct usb_device *dev, unsigned int pipe,
  202. unsigned char request, unsigned char requesttype,
  203. unsigned short value, unsigned short index,
  204. void *data, unsigned short size, int timeout);
  205. int usb_bulk_msg(struct usb_device *dev, unsigned int pipe,
  206. void *data, int len, int *actual_length, int timeout);
  207. int usb_submit_int_msg(struct usb_device *dev, unsigned long pipe,
  208. void *buffer, int transfer_len, int interval);
  209. int usb_disable_asynch(int disable);
  210. int usb_maxpacket(struct usb_device *dev, unsigned long pipe);
  211. int usb_get_configuration_no(struct usb_device *dev, unsigned char *buffer,
  212. int cfgno);
  213. int usb_get_report(struct usb_device *dev, int ifnum, unsigned char type,
  214. unsigned char id, void *buf, int size);
  215. int usb_get_class_descriptor(struct usb_device *dev, int ifnum,
  216. unsigned char type, unsigned char id, void *buf,
  217. int size);
  218. int usb_clear_halt(struct usb_device *dev, int pipe);
  219. int usb_string(struct usb_device *dev, int index, char *buf, size_t size);
  220. int usb_set_interface(struct usb_device *dev, int interface, int alternate);
  221. /* big endian -> little endian conversion */
  222. /* some CPUs are already little endian e.g. the ARM920T */
  223. #define __swap_16(x) \
  224. ({ unsigned short x_ = (unsigned short)x; \
  225. (unsigned short)( \
  226. ((x_ & 0x00FFU) << 8) | ((x_ & 0xFF00U) >> 8)); \
  227. })
  228. #define __swap_32(x) \
  229. ({ unsigned long x_ = (unsigned long)x; \
  230. (unsigned long)( \
  231. ((x_ & 0x000000FFUL) << 24) | \
  232. ((x_ & 0x0000FF00UL) << 8) | \
  233. ((x_ & 0x00FF0000UL) >> 8) | \
  234. ((x_ & 0xFF000000UL) >> 24)); \
  235. })
  236. #ifdef __LITTLE_ENDIAN
  237. # define swap_16(x) (x)
  238. # define swap_32(x) (x)
  239. #else
  240. # define swap_16(x) __swap_16(x)
  241. # define swap_32(x) __swap_32(x)
  242. #endif
  243. /*
  244. * Calling this entity a "pipe" is glorifying it. A USB pipe
  245. * is something embarrassingly simple: it basically consists
  246. * of the following information:
  247. * - device number (7 bits)
  248. * - endpoint number (4 bits)
  249. * - current Data0/1 state (1 bit)
  250. * - direction (1 bit)
  251. * - speed (2 bits)
  252. * - max packet size (2 bits: 8, 16, 32 or 64)
  253. * - pipe type (2 bits: control, interrupt, bulk, isochronous)
  254. *
  255. * That's 18 bits. Really. Nothing more. And the USB people have
  256. * documented these eighteen bits as some kind of glorious
  257. * virtual data structure.
  258. *
  259. * Let's not fall in that trap. We'll just encode it as a simple
  260. * unsigned int. The encoding is:
  261. *
  262. * - max size: bits 0-1 (00 = 8, 01 = 16, 10 = 32, 11 = 64)
  263. * - direction: bit 7 (0 = Host-to-Device [Out],
  264. * (1 = Device-to-Host [In])
  265. * - device: bits 8-14
  266. * - endpoint: bits 15-18
  267. * - Data0/1: bit 19
  268. * - pipe type: bits 30-31 (00 = isochronous, 01 = interrupt,
  269. * 10 = control, 11 = bulk)
  270. *
  271. * Why? Because it's arbitrary, and whatever encoding we select is really
  272. * up to us. This one happens to share a lot of bit positions with the UHCI
  273. * specification, so that much of the uhci driver can just mask the bits
  274. * appropriately.
  275. */
  276. /* Create various pipes... */
  277. #define create_pipe(dev,endpoint) \
  278. (((dev)->devnum << 8) | ((endpoint) << 15) | \
  279. (dev)->maxpacketsize)
  280. #define default_pipe(dev) ((dev)->speed << 26)
  281. #define usb_sndctrlpipe(dev, endpoint) ((PIPE_CONTROL << 30) | \
  282. create_pipe(dev, endpoint))
  283. #define usb_rcvctrlpipe(dev, endpoint) ((PIPE_CONTROL << 30) | \
  284. create_pipe(dev, endpoint) | \
  285. USB_DIR_IN)
  286. #define usb_sndisocpipe(dev, endpoint) ((PIPE_ISOCHRONOUS << 30) | \
  287. create_pipe(dev, endpoint))
  288. #define usb_rcvisocpipe(dev, endpoint) ((PIPE_ISOCHRONOUS << 30) | \
  289. create_pipe(dev, endpoint) | \
  290. USB_DIR_IN)
  291. #define usb_sndbulkpipe(dev, endpoint) ((PIPE_BULK << 30) | \
  292. create_pipe(dev, endpoint))
  293. #define usb_rcvbulkpipe(dev, endpoint) ((PIPE_BULK << 30) | \
  294. create_pipe(dev, endpoint) | \
  295. USB_DIR_IN)
  296. #define usb_sndintpipe(dev, endpoint) ((PIPE_INTERRUPT << 30) | \
  297. create_pipe(dev, endpoint))
  298. #define usb_rcvintpipe(dev, endpoint) ((PIPE_INTERRUPT << 30) | \
  299. create_pipe(dev, endpoint) | \
  300. USB_DIR_IN)
  301. #define usb_snddefctrl(dev) ((PIPE_CONTROL << 30) | \
  302. default_pipe(dev))
  303. #define usb_rcvdefctrl(dev) ((PIPE_CONTROL << 30) | \
  304. default_pipe(dev) | \
  305. USB_DIR_IN)
  306. /* The D0/D1 toggle bits */
  307. #define usb_gettoggle(dev, ep, out) (((dev)->toggle[out] >> ep) & 1)
  308. #define usb_dotoggle(dev, ep, out) ((dev)->toggle[out] ^= (1 << ep))
  309. #define usb_settoggle(dev, ep, out, bit) ((dev)->toggle[out] = \
  310. ((dev)->toggle[out] & \
  311. ~(1 << ep)) | ((bit) << ep))
  312. /* Endpoint halt control/status */
  313. #define usb_endpoint_out(ep_dir) (((ep_dir >> 7) & 1) ^ 1)
  314. #define usb_endpoint_halt(dev, ep, out) ((dev)->halted[out] |= (1 << (ep)))
  315. #define usb_endpoint_running(dev, ep, out) ((dev)->halted[out] &= ~(1 << (ep)))
  316. #define usb_endpoint_halted(dev, ep, out) ((dev)->halted[out] & (1 << (ep)))
  317. #define usb_packetid(pipe) (((pipe) & USB_DIR_IN) ? USB_PID_IN : \
  318. USB_PID_OUT)
  319. #define usb_pipeout(pipe) ((((pipe) >> 7) & 1) ^ 1)
  320. #define usb_pipein(pipe) (((pipe) >> 7) & 1)
  321. #define usb_pipedevice(pipe) (((pipe) >> 8) & 0x7f)
  322. #define usb_pipe_endpdev(pipe) (((pipe) >> 8) & 0x7ff)
  323. #define usb_pipeendpoint(pipe) (((pipe) >> 15) & 0xf)
  324. #define usb_pipedata(pipe) (((pipe) >> 19) & 1)
  325. #define usb_pipetype(pipe) (((pipe) >> 30) & 3)
  326. #define usb_pipeisoc(pipe) (usb_pipetype((pipe)) == PIPE_ISOCHRONOUS)
  327. #define usb_pipeint(pipe) (usb_pipetype((pipe)) == PIPE_INTERRUPT)
  328. #define usb_pipecontrol(pipe) (usb_pipetype((pipe)) == PIPE_CONTROL)
  329. #define usb_pipebulk(pipe) (usb_pipetype((pipe)) == PIPE_BULK)
  330. #define usb_pipe_ep_index(pipe) \
  331. usb_pipecontrol(pipe) ? (usb_pipeendpoint(pipe) * 2) : \
  332. ((usb_pipeendpoint(pipe) * 2) - \
  333. (usb_pipein(pipe) ? 0 : 1))
  334. /*************************************************************************
  335. * Hub Stuff
  336. */
  337. struct usb_port_status {
  338. unsigned short wPortStatus;
  339. unsigned short wPortChange;
  340. } __attribute__ ((packed));
  341. struct usb_hub_status {
  342. unsigned short wHubStatus;
  343. unsigned short wHubChange;
  344. } __attribute__ ((packed));
  345. /* Hub descriptor */
  346. struct usb_hub_descriptor {
  347. unsigned char bLength;
  348. unsigned char bDescriptorType;
  349. unsigned char bNbrPorts;
  350. unsigned short wHubCharacteristics;
  351. unsigned char bPwrOn2PwrGood;
  352. unsigned char bHubContrCurrent;
  353. unsigned char DeviceRemovable[(USB_MAXCHILDREN+1+7)/8];
  354. unsigned char PortPowerCtrlMask[(USB_MAXCHILDREN+1+7)/8];
  355. /* DeviceRemovable and PortPwrCtrlMask want to be variable-length
  356. bitmaps that hold max 255 entries. (bit0 is ignored) */
  357. } __attribute__ ((packed));
  358. struct usb_hub_device {
  359. struct usb_device *pusb_dev;
  360. struct usb_hub_descriptor desc;
  361. };
  362. int usb_hub_probe(struct usb_device *dev, int ifnum);
  363. void usb_hub_reset(void);
  364. int hub_port_reset(struct usb_device *dev, int port,
  365. unsigned short *portstat);
  366. struct usb_device *usb_alloc_new_device(void *controller);
  367. int usb_new_device(struct usb_device *dev);
  368. void usb_free_device(void);
  369. int usb_alloc_device(struct usb_device *dev);
  370. #endif /*_USB_H_ */