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