usb.h 12 KB

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