usb.h 13 KB

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