usb.h 13 KB

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