usb.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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. /* Everything is aribtrary */
  31. #define USB_ALTSETTINGALLOC 4
  32. #define USB_MAXALTSETTING 128 /* Hard limit */
  33. #define USB_MAX_DEVICE 32
  34. #define USB_MAXCONFIG 8
  35. #define USB_MAXINTERFACES 8
  36. #define USB_MAXENDPOINTS 16
  37. #define USB_MAXCHILDREN 8 /* This is arbitrary */
  38. #define USB_MAX_HUB 16
  39. #define USB_CNTL_TIMEOUT 100 /* 100ms timeout */
  40. /* device request (setup) */
  41. struct devrequest {
  42. unsigned char requesttype;
  43. unsigned char request;
  44. unsigned short value;
  45. unsigned short index;
  46. unsigned short length;
  47. } __attribute__ ((packed));
  48. /* All standard descriptors have these 2 fields in common */
  49. struct usb_descriptor_header {
  50. unsigned char bLength;
  51. unsigned char bDescriptorType;
  52. } __attribute__ ((packed));
  53. /* Interface */
  54. struct usb_interface {
  55. struct usb_interface_descriptor desc;
  56. unsigned char no_of_ep;
  57. unsigned char num_altsetting;
  58. unsigned char act_altsetting;
  59. struct usb_endpoint_descriptor ep_desc[USB_MAXENDPOINTS];
  60. } __attribute__ ((packed));
  61. /* Configuration information.. */
  62. struct usb_config {
  63. struct usb_configuration_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. struct usb_device_descriptor descriptor; /* Device Descriptor */
  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. };
  110. /**********************************************************************
  111. * this is how the lowlevel part communicate with the outer world
  112. */
  113. #if defined(CONFIG_USB_UHCI) || defined(CONFIG_USB_OHCI) || \
  114. defined(CONFIG_USB_EHCI) || defined(CONFIG_USB_OHCI_NEW) || \
  115. defined(CONFIG_USB_SL811HS) || defined(CONFIG_USB_ISP116X_HCD) || \
  116. defined(CONFIG_USB_R8A66597_HCD) || defined(CONFIG_USB_DAVINCI) || \
  117. defined(CONFIG_USB_OMAP3) || defined(CONFIG_USB_DA8XX) || \
  118. defined(CONFIG_USB_BLACKFIN)
  119. int usb_lowlevel_init(void);
  120. int usb_lowlevel_stop(void);
  121. int submit_bulk_msg(struct usb_device *dev, unsigned long pipe,
  122. void *buffer, int transfer_len);
  123. int submit_control_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
  124. int transfer_len, struct devrequest *setup);
  125. int submit_int_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
  126. int transfer_len, int interval);
  127. void usb_event_poll(void);
  128. /* Defines */
  129. #define USB_UHCI_VEND_ID 0x8086
  130. #define USB_UHCI_DEV_ID 0x7112
  131. #else
  132. #error USB Lowlevel not defined
  133. #endif
  134. #ifdef CONFIG_USB_STORAGE
  135. #define USB_MAX_STOR_DEV 5
  136. block_dev_desc_t *usb_stor_get_dev(int index);
  137. int usb_stor_scan(int mode);
  138. int usb_stor_info(void);
  139. #endif
  140. #ifdef CONFIG_USB_KEYBOARD
  141. int drv_usb_kbd_init(void);
  142. int usb_kbd_deregister(void);
  143. #endif
  144. /* routines */
  145. int usb_init(void); /* initialize the USB Controller */
  146. int usb_stop(void); /* stop the USB Controller */
  147. int usb_set_protocol(struct usb_device *dev, int ifnum, int protocol);
  148. int usb_set_idle(struct usb_device *dev, int ifnum, int duration,
  149. int report_id);
  150. struct usb_device *usb_get_dev_index(int index);
  151. int usb_control_msg(struct usb_device *dev, unsigned int pipe,
  152. unsigned char request, unsigned char requesttype,
  153. unsigned short value, unsigned short index,
  154. void *data, unsigned short size, int timeout);
  155. int usb_bulk_msg(struct usb_device *dev, unsigned int pipe,
  156. void *data, int len, int *actual_length, int timeout);
  157. int usb_submit_int_msg(struct usb_device *dev, unsigned long pipe,
  158. void *buffer, int transfer_len, int interval);
  159. void usb_disable_asynch(int disable);
  160. int usb_maxpacket(struct usb_device *dev, unsigned long pipe);
  161. inline void wait_ms(unsigned long ms);
  162. int usb_get_configuration_no(struct usb_device *dev, unsigned char *buffer,
  163. int cfgno);
  164. int usb_get_report(struct usb_device *dev, int ifnum, unsigned char type,
  165. unsigned char id, void *buf, int size);
  166. int usb_get_class_descriptor(struct usb_device *dev, int ifnum,
  167. unsigned char type, unsigned char id, void *buf,
  168. int size);
  169. int usb_clear_halt(struct usb_device *dev, int pipe);
  170. int usb_string(struct usb_device *dev, int index, char *buf, size_t size);
  171. int usb_set_interface(struct usb_device *dev, int interface, int alternate);
  172. /* big endian -> little endian conversion */
  173. /* some CPUs are already little endian e.g. the ARM920T */
  174. #define __swap_16(x) \
  175. ({ unsigned short x_ = (unsigned short)x; \
  176. (unsigned short)( \
  177. ((x_ & 0x00FFU) << 8) | ((x_ & 0xFF00U) >> 8)); \
  178. })
  179. #define __swap_32(x) \
  180. ({ unsigned long x_ = (unsigned long)x; \
  181. (unsigned long)( \
  182. ((x_ & 0x000000FFUL) << 24) | \
  183. ((x_ & 0x0000FF00UL) << 8) | \
  184. ((x_ & 0x00FF0000UL) >> 8) | \
  185. ((x_ & 0xFF000000UL) >> 24)); \
  186. })
  187. #ifdef __LITTLE_ENDIAN
  188. # define swap_16(x) (x)
  189. # define swap_32(x) (x)
  190. #else
  191. # define swap_16(x) __swap_16(x)
  192. # define swap_32(x) __swap_32(x)
  193. #endif
  194. /*
  195. * Calling this entity a "pipe" is glorifying it. A USB pipe
  196. * is something embarrassingly simple: it basically consists
  197. * of the following information:
  198. * - device number (7 bits)
  199. * - endpoint number (4 bits)
  200. * - current Data0/1 state (1 bit)
  201. * - direction (1 bit)
  202. * - speed (2 bits)
  203. * - max packet size (2 bits: 8, 16, 32 or 64)
  204. * - pipe type (2 bits: control, interrupt, bulk, isochronous)
  205. *
  206. * That's 18 bits. Really. Nothing more. And the USB people have
  207. * documented these eighteen bits as some kind of glorious
  208. * virtual data structure.
  209. *
  210. * Let's not fall in that trap. We'll just encode it as a simple
  211. * unsigned int. The encoding is:
  212. *
  213. * - max size: bits 0-1 (00 = 8, 01 = 16, 10 = 32, 11 = 64)
  214. * - direction: bit 7 (0 = Host-to-Device [Out],
  215. * (1 = Device-to-Host [In])
  216. * - device: bits 8-14
  217. * - endpoint: bits 15-18
  218. * - Data0/1: bit 19
  219. * - speed: bit 26 (0 = Full, 1 = Low Speed, 2 = High)
  220. * - pipe type: bits 30-31 (00 = isochronous, 01 = interrupt,
  221. * 10 = control, 11 = bulk)
  222. *
  223. * Why? Because it's arbitrary, and whatever encoding we select is really
  224. * up to us. This one happens to share a lot of bit positions with the UHCI
  225. * specification, so that much of the uhci driver can just mask the bits
  226. * appropriately.
  227. */
  228. /* Create various pipes... */
  229. #define create_pipe(dev,endpoint) \
  230. (((dev)->devnum << 8) | (endpoint << 15) | \
  231. ((dev)->speed << 26) | (dev)->maxpacketsize)
  232. #define default_pipe(dev) ((dev)->speed << 26)
  233. #define usb_sndctrlpipe(dev, endpoint) ((PIPE_CONTROL << 30) | \
  234. create_pipe(dev, endpoint))
  235. #define usb_rcvctrlpipe(dev, endpoint) ((PIPE_CONTROL << 30) | \
  236. create_pipe(dev, endpoint) | \
  237. USB_DIR_IN)
  238. #define usb_sndisocpipe(dev, endpoint) ((PIPE_ISOCHRONOUS << 30) | \
  239. create_pipe(dev, endpoint))
  240. #define usb_rcvisocpipe(dev, endpoint) ((PIPE_ISOCHRONOUS << 30) | \
  241. create_pipe(dev, endpoint) | \
  242. USB_DIR_IN)
  243. #define usb_sndbulkpipe(dev, endpoint) ((PIPE_BULK << 30) | \
  244. create_pipe(dev, endpoint))
  245. #define usb_rcvbulkpipe(dev, endpoint) ((PIPE_BULK << 30) | \
  246. create_pipe(dev, endpoint) | \
  247. USB_DIR_IN)
  248. #define usb_sndintpipe(dev, endpoint) ((PIPE_INTERRUPT << 30) | \
  249. create_pipe(dev, endpoint))
  250. #define usb_rcvintpipe(dev, endpoint) ((PIPE_INTERRUPT << 30) | \
  251. create_pipe(dev, endpoint) | \
  252. USB_DIR_IN)
  253. #define usb_snddefctrl(dev) ((PIPE_CONTROL << 30) | \
  254. default_pipe(dev))
  255. #define usb_rcvdefctrl(dev) ((PIPE_CONTROL << 30) | \
  256. default_pipe(dev) | \
  257. USB_DIR_IN)
  258. /* The D0/D1 toggle bits */
  259. #define usb_gettoggle(dev, ep, out) (((dev)->toggle[out] >> ep) & 1)
  260. #define usb_dotoggle(dev, ep, out) ((dev)->toggle[out] ^= (1 << ep))
  261. #define usb_settoggle(dev, ep, out, bit) ((dev)->toggle[out] = \
  262. ((dev)->toggle[out] & \
  263. ~(1 << ep)) | ((bit) << ep))
  264. /* Endpoint halt control/status */
  265. #define usb_endpoint_out(ep_dir) (((ep_dir >> 7) & 1) ^ 1)
  266. #define usb_endpoint_halt(dev, ep, out) ((dev)->halted[out] |= (1 << (ep)))
  267. #define usb_endpoint_running(dev, ep, out) ((dev)->halted[out] &= ~(1 << (ep)))
  268. #define usb_endpoint_halted(dev, ep, out) ((dev)->halted[out] & (1 << (ep)))
  269. #define usb_packetid(pipe) (((pipe) & USB_DIR_IN) ? USB_PID_IN : \
  270. USB_PID_OUT)
  271. #define usb_pipeout(pipe) ((((pipe) >> 7) & 1) ^ 1)
  272. #define usb_pipein(pipe) (((pipe) >> 7) & 1)
  273. #define usb_pipedevice(pipe) (((pipe) >> 8) & 0x7f)
  274. #define usb_pipe_endpdev(pipe) (((pipe) >> 8) & 0x7ff)
  275. #define usb_pipeendpoint(pipe) (((pipe) >> 15) & 0xf)
  276. #define usb_pipedata(pipe) (((pipe) >> 19) & 1)
  277. #define usb_pipespeed(pipe) (((pipe) >> 26) & 3)
  278. #define usb_pipeslow(pipe) (usb_pipespeed(pipe) == USB_SPEED_LOW)
  279. #define usb_pipetype(pipe) (((pipe) >> 30) & 3)
  280. #define usb_pipeisoc(pipe) (usb_pipetype((pipe)) == PIPE_ISOCHRONOUS)
  281. #define usb_pipeint(pipe) (usb_pipetype((pipe)) == PIPE_INTERRUPT)
  282. #define usb_pipecontrol(pipe) (usb_pipetype((pipe)) == PIPE_CONTROL)
  283. #define usb_pipebulk(pipe) (usb_pipetype((pipe)) == PIPE_BULK)
  284. /*************************************************************************
  285. * Hub Stuff
  286. */
  287. struct usb_port_status {
  288. unsigned short wPortStatus;
  289. unsigned short wPortChange;
  290. } __attribute__ ((packed));
  291. struct usb_hub_status {
  292. unsigned short wHubStatus;
  293. unsigned short wHubChange;
  294. } __attribute__ ((packed));
  295. /* Hub descriptor */
  296. struct usb_hub_descriptor {
  297. unsigned char bLength;
  298. unsigned char bDescriptorType;
  299. unsigned char bNbrPorts;
  300. unsigned short wHubCharacteristics;
  301. unsigned char bPwrOn2PwrGood;
  302. unsigned char bHubContrCurrent;
  303. unsigned char DeviceRemovable[(USB_MAXCHILDREN+1+7)/8];
  304. unsigned char PortPowerCtrlMask[(USB_MAXCHILDREN+1+7)/8];
  305. /* DeviceRemovable and PortPwrCtrlMask want to be variable-length
  306. bitmaps that hold max 255 entries. (bit0 is ignored) */
  307. } __attribute__ ((packed));
  308. struct usb_hub_device {
  309. struct usb_device *pusb_dev;
  310. struct usb_hub_descriptor desc;
  311. };
  312. #endif /*_USB_H_ */