Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0+ */ |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2003 |
| 4 | * Gerry Hamel, geh@ti.com, Texas Instruments |
| 5 | * |
| 6 | * Based on linux/drivers/usbd/usbd.h |
| 7 | * |
| 8 | * Copyright (c) 2000, 2001, 2002 Lineo |
| 9 | * Copyright (c) 2001 Hewlett Packard |
| 10 | * |
| 11 | * By: |
| 12 | * Stuart Lynne <sl@lineo.com>, |
| 13 | * Tom Rushworth <tbr@lineo.com>, |
| 14 | * Bruce Balden <balden@lineo.com> |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | #ifndef __USBDCORE_H__ |
| 18 | #define __USBDCORE_H__ |
| 19 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 20 | #include "usbdescriptors.h" |
| 21 | |
| 22 | |
| 23 | #define MAX_URBS_QUEUED 5 |
| 24 | |
| 25 | |
| 26 | #if 1 |
| 27 | #define usberr(fmt,args...) serial_printf("ERROR: %s(), %d: "fmt"\n",__FUNCTION__,__LINE__,##args) |
| 28 | #else |
| 29 | #define usberr(fmt,args...) do{}while(0) |
| 30 | #endif |
| 31 | |
| 32 | #if 0 |
| 33 | #define usbdbg(fmt,args...) serial_printf("debug: %s(), %d: "fmt"\n",__FUNCTION__,__LINE__,##args) |
| 34 | #else |
| 35 | #define usbdbg(fmt,args...) do{}while(0) |
| 36 | #endif |
| 37 | |
| 38 | #if 0 |
| 39 | #define usbinfo(fmt,args...) serial_printf("info: %s(), %d: "fmt"\n",__FUNCTION__,__LINE__,##args) |
| 40 | #else |
| 41 | #define usbinfo(fmt,args...) do{}while(0) |
| 42 | #endif |
| 43 | |
| 44 | #ifndef le16_to_cpu |
| 45 | #define le16_to_cpu(x) (x) |
| 46 | #endif |
| 47 | |
| 48 | #ifndef inb |
| 49 | #define inb(p) (*(volatile u8*)(p)) |
| 50 | #endif |
| 51 | |
| 52 | #ifndef outb |
| 53 | #define outb(val,p) (*(volatile u8*)(p) = (val)) |
| 54 | #endif |
| 55 | |
| 56 | #ifndef inw |
| 57 | #define inw(p) (*(volatile u16*)(p)) |
| 58 | #endif |
| 59 | |
| 60 | #ifndef outw |
| 61 | #define outw(val,p) (*(volatile u16*)(p) = (val)) |
| 62 | #endif |
| 63 | |
| 64 | #ifndef inl |
| 65 | #define inl(p) (*(volatile u32*)(p)) |
| 66 | #endif |
| 67 | |
| 68 | #ifndef outl |
| 69 | #define outl(val,p) (*(volatile u32*)(p) = (val)) |
| 70 | #endif |
| 71 | |
| 72 | #ifndef insw |
| 73 | #define insw(p,to,len) mmio_insw(p,to,len) |
| 74 | #endif |
| 75 | |
| 76 | #ifndef outsw |
| 77 | #define outsw(p,from,len) mmio_outsw(p,from,len) |
| 78 | #endif |
| 79 | |
| 80 | #ifndef insb |
| 81 | #define insb(p,to,len) mmio_insb(p,to,len) |
| 82 | #endif |
| 83 | |
| 84 | #ifndef mmio_insw |
| 85 | #define mmio_insw(r,b,l) ({ int __i ; \ |
| 86 | u16 *__b2; \ |
| 87 | __b2 = (u16 *) b; \ |
| 88 | for (__i = 0; __i < l; __i++) { \ |
| 89 | *(__b2 + __i) = inw(r); \ |
| 90 | }; \ |
| 91 | }) |
| 92 | #endif |
| 93 | |
| 94 | #ifndef mmio_outsw |
| 95 | #define mmio_outsw(r,b,l) ({ int __i; \ |
| 96 | u16 *__b2; \ |
| 97 | __b2 = (u16 *) b; \ |
| 98 | for (__i = 0; __i < l; __i++) { \ |
| 99 | outw( *(__b2 + __i), r); \ |
| 100 | } \ |
| 101 | }) |
| 102 | #endif |
| 103 | |
| 104 | #ifndef mmio_insb |
| 105 | #define mmio_insb(r,b,l) ({ int __i ; \ |
| 106 | u8 *__b2; \ |
| 107 | __b2 = (u8 *) b; \ |
| 108 | for (__i = 0; __i < l; __i++) { \ |
| 109 | *(__b2 + __i) = inb(r); \ |
| 110 | }; \ |
| 111 | }) |
| 112 | #endif |
| 113 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 114 | /* |
| 115 | * Structure member address manipulation macros. |
| 116 | * These are used by client code (code using the urb_link routines), since |
| 117 | * the urb_link structure is embedded in the client data structures. |
| 118 | * |
| 119 | * Note: a macro offsetof equivalent to member_offset is defined in stddef.h |
| 120 | * but this is kept here for the sake of portability. |
| 121 | * |
| 122 | * p2surround returns a pointer to the surrounding structure given |
| 123 | * type of the surrounding structure, the name memb of the structure |
| 124 | * member pointed at by ptr. For example, if you have: |
| 125 | * |
| 126 | * struct foo { |
| 127 | * int x; |
| 128 | * float y; |
| 129 | * char z; |
| 130 | * } thingy; |
| 131 | * |
| 132 | * char *cp = &thingy.z; |
| 133 | * |
| 134 | * then |
| 135 | * |
| 136 | * &thingy == p2surround(struct foo, z, cp) |
| 137 | * |
| 138 | * Clear? |
| 139 | */ |
| 140 | #define _cv_(ptr) ((char*)(void*)(ptr)) |
| 141 | #define member_offset(type,memb) (_cv_(&(((type*)0)->memb))-(char*)0) |
| 142 | #define p2surround(type,memb,ptr) ((type*)(void*)(_cv_(ptr)-member_offset(type,memb))) |
| 143 | |
| 144 | struct urb; |
| 145 | |
| 146 | struct usb_endpoint_instance; |
| 147 | struct usb_interface_instance; |
| 148 | struct usb_configuration_instance; |
| 149 | struct usb_device_instance; |
| 150 | struct usb_bus_instance; |
| 151 | |
| 152 | /* |
| 153 | * Device and/or Interface Class codes |
| 154 | */ |
| 155 | #define USB_CLASS_PER_INTERFACE 0 /* for DeviceClass */ |
| 156 | #define USB_CLASS_AUDIO 1 |
| 157 | #define USB_CLASS_COMM 2 |
| 158 | #define USB_CLASS_HID 3 |
| 159 | #define USB_CLASS_PHYSICAL 5 |
| 160 | #define USB_CLASS_PRINTER 7 |
| 161 | #define USB_CLASS_MASS_STORAGE 8 |
| 162 | #define USB_CLASS_HUB 9 |
| 163 | #define USB_CLASS_DATA 10 |
| 164 | #define USB_CLASS_APP_SPEC 0xfe |
| 165 | #define USB_CLASS_VENDOR_SPEC 0xff |
| 166 | |
| 167 | /* |
| 168 | * USB types |
| 169 | */ |
| 170 | #define USB_TYPE_STANDARD (0x00 << 5) |
| 171 | #define USB_TYPE_CLASS (0x01 << 5) |
| 172 | #define USB_TYPE_VENDOR (0x02 << 5) |
| 173 | #define USB_TYPE_RESERVED (0x03 << 5) |
| 174 | |
| 175 | /* |
| 176 | * USB recipients |
| 177 | */ |
| 178 | #define USB_RECIP_DEVICE 0x00 |
| 179 | #define USB_RECIP_INTERFACE 0x01 |
| 180 | #define USB_RECIP_ENDPOINT 0x02 |
| 181 | #define USB_RECIP_OTHER 0x03 |
| 182 | |
| 183 | /* |
| 184 | * USB directions |
| 185 | */ |
| 186 | #define USB_DIR_OUT 0 |
| 187 | #define USB_DIR_IN 0x80 |
| 188 | |
| 189 | /* |
| 190 | * Descriptor types |
| 191 | */ |
| 192 | #define USB_DT_DEVICE 0x01 |
| 193 | #define USB_DT_CONFIG 0x02 |
| 194 | #define USB_DT_STRING 0x03 |
| 195 | #define USB_DT_INTERFACE 0x04 |
| 196 | #define USB_DT_ENDPOINT 0x05 |
| 197 | |
| 198 | #define USB_DT_HID (USB_TYPE_CLASS | 0x01) |
| 199 | #define USB_DT_REPORT (USB_TYPE_CLASS | 0x02) |
| 200 | #define USB_DT_PHYSICAL (USB_TYPE_CLASS | 0x03) |
| 201 | #define USB_DT_HUB (USB_TYPE_CLASS | 0x09) |
| 202 | |
| 203 | /* |
| 204 | * Descriptor sizes per descriptor type |
| 205 | */ |
| 206 | #define USB_DT_DEVICE_SIZE 18 |
| 207 | #define USB_DT_CONFIG_SIZE 9 |
| 208 | #define USB_DT_INTERFACE_SIZE 9 |
| 209 | #define USB_DT_ENDPOINT_SIZE 7 |
| 210 | #define USB_DT_ENDPOINT_AUDIO_SIZE 9 /* Audio extension */ |
| 211 | #define USB_DT_HUB_NONVAR_SIZE 7 |
| 212 | #define USB_DT_HID_SIZE 9 |
| 213 | |
| 214 | /* |
| 215 | * Endpoints |
| 216 | */ |
| 217 | #define USB_ENDPOINT_NUMBER_MASK 0x0f /* in bEndpointAddress */ |
| 218 | #define USB_ENDPOINT_DIR_MASK 0x80 |
| 219 | |
| 220 | #define USB_ENDPOINT_XFERTYPE_MASK 0x03 /* in bmAttributes */ |
| 221 | #define USB_ENDPOINT_XFER_CONTROL 0 |
| 222 | #define USB_ENDPOINT_XFER_ISOC 1 |
| 223 | #define USB_ENDPOINT_XFER_BULK 2 |
| 224 | #define USB_ENDPOINT_XFER_INT 3 |
| 225 | |
| 226 | /* |
| 227 | * USB Packet IDs (PIDs) |
| 228 | */ |
| 229 | #define USB_PID_UNDEF_0 0xf0 |
| 230 | #define USB_PID_OUT 0xe1 |
| 231 | #define USB_PID_ACK 0xd2 |
| 232 | #define USB_PID_DATA0 0xc3 |
| 233 | #define USB_PID_PING 0xb4 /* USB 2.0 */ |
| 234 | #define USB_PID_SOF 0xa5 |
| 235 | #define USB_PID_NYET 0x96 /* USB 2.0 */ |
| 236 | #define USB_PID_DATA2 0x87 /* USB 2.0 */ |
| 237 | #define USB_PID_SPLIT 0x78 /* USB 2.0 */ |
| 238 | #define USB_PID_IN 0x69 |
| 239 | #define USB_PID_NAK 0x5a |
| 240 | #define USB_PID_DATA1 0x4b |
| 241 | #define USB_PID_PREAMBLE 0x3c /* Token mode */ |
| 242 | #define USB_PID_ERR 0x3c /* USB 2.0: handshake mode */ |
| 243 | #define USB_PID_SETUP 0x2d |
| 244 | #define USB_PID_STALL 0x1e |
| 245 | #define USB_PID_MDATA 0x0f /* USB 2.0 */ |
| 246 | |
| 247 | /* |
| 248 | * Standard requests |
| 249 | */ |
| 250 | #define USB_REQ_GET_STATUS 0x00 |
| 251 | #define USB_REQ_CLEAR_FEATURE 0x01 |
| 252 | #define USB_REQ_SET_FEATURE 0x03 |
| 253 | #define USB_REQ_SET_ADDRESS 0x05 |
| 254 | #define USB_REQ_GET_DESCRIPTOR 0x06 |
| 255 | #define USB_REQ_SET_DESCRIPTOR 0x07 |
| 256 | #define USB_REQ_GET_CONFIGURATION 0x08 |
| 257 | #define USB_REQ_SET_CONFIGURATION 0x09 |
| 258 | #define USB_REQ_GET_INTERFACE 0x0A |
| 259 | #define USB_REQ_SET_INTERFACE 0x0B |
| 260 | #define USB_REQ_SYNCH_FRAME 0x0C |
| 261 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 262 | /* |
| 263 | * HID requests |
| 264 | */ |
| 265 | #define USB_REQ_GET_REPORT 0x01 |
| 266 | #define USB_REQ_GET_IDLE 0x02 |
| 267 | #define USB_REQ_GET_PROTOCOL 0x03 |
| 268 | #define USB_REQ_SET_REPORT 0x09 |
| 269 | #define USB_REQ_SET_IDLE 0x0A |
| 270 | #define USB_REQ_SET_PROTOCOL 0x0B |
| 271 | |
| 272 | |
| 273 | /* |
| 274 | * USB Spec Release number |
| 275 | */ |
| 276 | |
| 277 | #define USB_BCD_VERSION 0x0110 |
| 278 | |
| 279 | |
| 280 | /* |
| 281 | * Device Requests (c.f Table 9-2) |
| 282 | */ |
| 283 | |
| 284 | #define USB_REQ_DIRECTION_MASK 0x80 |
| 285 | #define USB_REQ_TYPE_MASK 0x60 |
| 286 | #define USB_REQ_RECIPIENT_MASK 0x1f |
| 287 | |
| 288 | #define USB_REQ_DEVICE2HOST 0x80 |
| 289 | #define USB_REQ_HOST2DEVICE 0x00 |
| 290 | |
| 291 | #define USB_REQ_TYPE_STANDARD 0x00 |
| 292 | #define USB_REQ_TYPE_CLASS 0x20 |
| 293 | #define USB_REQ_TYPE_VENDOR 0x40 |
| 294 | |
| 295 | #define USB_REQ_RECIPIENT_DEVICE 0x00 |
| 296 | #define USB_REQ_RECIPIENT_INTERFACE 0x01 |
| 297 | #define USB_REQ_RECIPIENT_ENDPOINT 0x02 |
| 298 | #define USB_REQ_RECIPIENT_OTHER 0x03 |
| 299 | |
| 300 | /* |
| 301 | * get status bits |
| 302 | */ |
| 303 | |
| 304 | #define USB_STATUS_SELFPOWERED 0x01 |
| 305 | #define USB_STATUS_REMOTEWAKEUP 0x02 |
| 306 | |
| 307 | #define USB_STATUS_HALT 0x01 |
| 308 | |
| 309 | /* |
| 310 | * descriptor types |
| 311 | */ |
| 312 | |
| 313 | #define USB_DESCRIPTOR_TYPE_DEVICE 0x01 |
| 314 | #define USB_DESCRIPTOR_TYPE_CONFIGURATION 0x02 |
| 315 | #define USB_DESCRIPTOR_TYPE_STRING 0x03 |
| 316 | #define USB_DESCRIPTOR_TYPE_INTERFACE 0x04 |
| 317 | #define USB_DESCRIPTOR_TYPE_ENDPOINT 0x05 |
| 318 | #define USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER 0x06 |
| 319 | #define USB_DESCRIPTOR_TYPE_OTHER_SPEED_CONFIGURATION 0x07 |
| 320 | #define USB_DESCRIPTOR_TYPE_INTERFACE_POWER 0x08 |
| 321 | #define USB_DESCRIPTOR_TYPE_HID 0x21 |
| 322 | #define USB_DESCRIPTOR_TYPE_REPORT 0x22 |
| 323 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 324 | /* |
| 325 | * standard feature selectors |
| 326 | */ |
| 327 | #define USB_ENDPOINT_HALT 0x00 |
| 328 | #define USB_DEVICE_REMOTE_WAKEUP 0x01 |
| 329 | #define USB_TEST_MODE 0x02 |
| 330 | |
| 331 | |
| 332 | /* USB Requests |
| 333 | * |
| 334 | */ |
| 335 | |
| 336 | struct usb_device_request { |
| 337 | u8 bmRequestType; |
| 338 | u8 bRequest; |
| 339 | u16 wValue; |
| 340 | u16 wIndex; |
| 341 | u16 wLength; |
| 342 | } __attribute__ ((packed)); |
| 343 | |
| 344 | |
| 345 | /* USB Status |
| 346 | * |
| 347 | */ |
| 348 | typedef enum urb_send_status { |
| 349 | SEND_IN_PROGRESS, |
| 350 | SEND_FINISHED_OK, |
| 351 | SEND_FINISHED_ERROR, |
| 352 | RECV_READY, |
| 353 | RECV_OK, |
| 354 | RECV_ERROR |
| 355 | } urb_send_status_t; |
| 356 | |
| 357 | /* |
| 358 | * Device State (c.f USB Spec 2.0 Figure 9-1) |
| 359 | * |
| 360 | * What state the usb device is in. |
| 361 | * |
| 362 | * Note the state does not change if the device is suspended, we simply set a |
| 363 | * flag to show that it is suspended. |
| 364 | * |
| 365 | */ |
| 366 | typedef enum usb_device_state { |
| 367 | STATE_INIT, /* just initialized */ |
| 368 | STATE_CREATED, /* just created */ |
| 369 | STATE_ATTACHED, /* we are attached */ |
| 370 | STATE_POWERED, /* we have seen power indication (electrical bus signal) */ |
| 371 | STATE_DEFAULT, /* we been reset */ |
| 372 | STATE_ADDRESSED, /* we have been addressed (in default configuration) */ |
| 373 | STATE_CONFIGURED, /* we have seen a set configuration device command */ |
| 374 | STATE_UNKNOWN, /* destroyed */ |
| 375 | } usb_device_state_t; |
| 376 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 377 | /* |
| 378 | * Device status |
| 379 | * |
| 380 | * Overall state |
| 381 | */ |
| 382 | typedef enum usb_device_status { |
| 383 | USBD_OPENING, /* we are currently opening */ |
| 384 | USBD_OK, /* ok to use */ |
| 385 | USBD_SUSPENDED, /* we are currently suspended */ |
| 386 | USBD_CLOSING, /* we are currently closing */ |
| 387 | } usb_device_status_t; |
| 388 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 389 | /* |
| 390 | * Device Events |
| 391 | * |
| 392 | * These are defined in the USB Spec (c.f USB Spec 2.0 Figure 9-1). |
| 393 | * |
| 394 | * There are additional events defined to handle some extra actions we need |
| 395 | * to have handled. |
| 396 | * |
| 397 | */ |
| 398 | typedef enum usb_device_event { |
| 399 | |
| 400 | DEVICE_UNKNOWN, /* bi - unknown event */ |
| 401 | DEVICE_INIT, /* bi - initialize */ |
| 402 | DEVICE_CREATE, /* bi - */ |
| 403 | DEVICE_HUB_CONFIGURED, /* bi - bus has been plugged int */ |
| 404 | DEVICE_RESET, /* bi - hub has powered our port */ |
| 405 | |
| 406 | DEVICE_ADDRESS_ASSIGNED, /* ep0 - set address setup received */ |
| 407 | DEVICE_CONFIGURED, /* ep0 - set configure setup received */ |
| 408 | DEVICE_SET_INTERFACE, /* ep0 - set interface setup received */ |
| 409 | |
| 410 | DEVICE_SET_FEATURE, /* ep0 - set feature setup received */ |
| 411 | DEVICE_CLEAR_FEATURE, /* ep0 - clear feature setup received */ |
| 412 | |
| 413 | DEVICE_DE_CONFIGURED, /* ep0 - set configure setup received for ?? */ |
| 414 | |
| 415 | DEVICE_BUS_INACTIVE, /* bi - bus in inactive (no SOF packets) */ |
| 416 | DEVICE_BUS_ACTIVITY, /* bi - bus is active again */ |
| 417 | |
| 418 | DEVICE_POWER_INTERRUPTION, /* bi - hub has depowered our port */ |
| 419 | DEVICE_HUB_RESET, /* bi - bus has been unplugged */ |
| 420 | DEVICE_DESTROY, /* bi - device instance should be destroyed */ |
| 421 | |
Vagrant Cascadian | edfdb99 | 2016-04-30 19:18:00 -0700 | [diff] [blame] | 422 | DEVICE_HOTPLUG, /* bi - a hotplug event has occurred */ |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 423 | |
| 424 | DEVICE_FUNCTION_PRIVATE, /* function - private */ |
| 425 | |
| 426 | } usb_device_event_t; |
| 427 | |
| 428 | |
| 429 | typedef struct urb_link { |
| 430 | struct urb_link *next; |
| 431 | struct urb_link *prev; |
| 432 | } urb_link; |
| 433 | |
| 434 | /* USB Data structure - for passing data around. |
| 435 | * |
| 436 | * This is used for both sending and receiving data. |
| 437 | * |
| 438 | * The callback function is used to let the function driver know when |
| 439 | * transmitted data has been sent. |
| 440 | * |
| 441 | * The callback function is set by the alloc_recv function when an urb is |
| 442 | * allocated for receiving data for an endpoint and used to call the |
| 443 | * function driver to inform it that data has arrived. |
| 444 | */ |
| 445 | |
Shiraz Hashim | 96decf6 | 2012-12-17 14:19:37 +0530 | [diff] [blame] | 446 | /* in linux we'd malloc this, but in u-boot we prefer static data */ |
| 447 | #define URB_BUF_SIZE 512 |
| 448 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 449 | struct urb { |
| 450 | |
| 451 | struct usb_endpoint_instance *endpoint; |
| 452 | struct usb_device_instance *device; |
| 453 | |
| 454 | struct usb_device_request device_request; /* contents of received SETUP packet */ |
| 455 | |
| 456 | struct urb_link link; /* embedded struct for circular doubly linked list of urbs */ |
| 457 | |
| 458 | u8* buffer; |
| 459 | unsigned int buffer_length; |
| 460 | unsigned int actual_length; |
| 461 | |
| 462 | urb_send_status_t status; |
| 463 | int data; |
| 464 | |
| 465 | u16 buffer_data[URB_BUF_SIZE]; /* data received (OUT) or being sent (IN) */ |
| 466 | }; |
| 467 | |
| 468 | /* Endpoint configuration |
| 469 | * |
| 470 | * Per endpoint configuration data. Used to track which function driver owns |
| 471 | * an endpoint. |
| 472 | * |
| 473 | */ |
| 474 | struct usb_endpoint_instance { |
| 475 | int endpoint_address; /* logical endpoint address */ |
| 476 | |
| 477 | /* control */ |
| 478 | int status; /* halted */ |
| 479 | int state; /* available for use by bus interface driver */ |
| 480 | |
| 481 | /* receive side */ |
| 482 | struct urb_link rcv; /* received urbs */ |
| 483 | struct urb_link rdy; /* empty urbs ready to receive */ |
| 484 | struct urb *rcv_urb; /* active urb */ |
| 485 | int rcv_attributes; /* copy of bmAttributes from endpoint descriptor */ |
| 486 | int rcv_packetSize; /* maximum packet size from endpoint descriptor */ |
| 487 | int rcv_transferSize; /* maximum transfer size from function driver */ |
| 488 | int rcv_queue; |
| 489 | |
| 490 | /* transmit side */ |
| 491 | struct urb_link tx; /* urbs ready to transmit */ |
| 492 | struct urb_link done; /* transmitted urbs */ |
| 493 | struct urb *tx_urb; /* active urb */ |
| 494 | int tx_attributes; /* copy of bmAttributes from endpoint descriptor */ |
| 495 | int tx_packetSize; /* maximum packet size from endpoint descriptor */ |
| 496 | int tx_transferSize; /* maximum transfer size from function driver */ |
| 497 | int tx_queue; |
| 498 | |
| 499 | int sent; /* data already sent */ |
| 500 | int last; /* data sent in last packet XXX do we need this */ |
| 501 | }; |
| 502 | |
| 503 | struct usb_alternate_instance { |
| 504 | struct usb_interface_descriptor *interface_descriptor; |
| 505 | |
| 506 | int endpoints; |
| 507 | int *endpoint_transfersize_array; |
| 508 | struct usb_endpoint_descriptor **endpoints_descriptor_array; |
| 509 | }; |
| 510 | |
| 511 | struct usb_interface_instance { |
| 512 | int alternates; |
| 513 | struct usb_alternate_instance *alternates_instance_array; |
| 514 | }; |
| 515 | |
| 516 | struct usb_configuration_instance { |
| 517 | int interfaces; |
| 518 | struct usb_configuration_descriptor *configuration_descriptor; |
| 519 | struct usb_interface_instance *interface_instance_array; |
| 520 | }; |
| 521 | |
| 522 | |
| 523 | /* USB Device Instance |
| 524 | * |
| 525 | * For each physical bus interface we create a logical device structure. This |
| 526 | * tracks all of the required state to track the USB HOST's view of the device. |
| 527 | * |
| 528 | * Keep track of the device configuration for a real physical bus interface, |
| 529 | * this includes the bus interface, multiple function drivers, the current |
| 530 | * configuration and the current state. |
| 531 | * |
| 532 | * This will show: |
| 533 | * the specific bus interface driver |
| 534 | * the default endpoint 0 driver |
| 535 | * the configured function driver |
| 536 | * device state |
| 537 | * device status |
| 538 | * endpoint list |
| 539 | */ |
| 540 | |
| 541 | struct usb_device_instance { |
| 542 | |
| 543 | /* generic */ |
| 544 | char *name; |
| 545 | struct usb_device_descriptor *device_descriptor; /* per device descriptor */ |
| 546 | |
| 547 | void (*event) (struct usb_device_instance *device, usb_device_event_t event, int data); |
| 548 | |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 549 | /* Do cdc device specific control requests */ |
| 550 | int (*cdc_recv_setup)(struct usb_device_request *request, struct urb *urb); |
| 551 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 552 | /* bus interface */ |
| 553 | struct usb_bus_instance *bus; /* which bus interface driver */ |
| 554 | |
| 555 | /* configuration descriptors */ |
| 556 | int configurations; |
| 557 | struct usb_configuration_instance *configuration_instance_array; |
| 558 | |
| 559 | /* device state */ |
| 560 | usb_device_state_t device_state; /* current USB Device state */ |
| 561 | usb_device_state_t device_previous_state; /* current USB Device state */ |
| 562 | |
| 563 | u8 address; /* current address (zero is default) */ |
| 564 | u8 configuration; /* current show configuration (zero is default) */ |
| 565 | u8 interface; /* current interface (zero is default) */ |
| 566 | u8 alternate; /* alternate flag */ |
| 567 | |
| 568 | usb_device_status_t status; /* device status */ |
| 569 | |
| 570 | int urbs_queued; /* number of submitted urbs */ |
| 571 | |
| 572 | /* Shouldn't need to make this atomic, all we need is a change indicator */ |
| 573 | unsigned long usbd_rxtx_timestamp; |
| 574 | unsigned long usbd_last_rxtx_timestamp; |
| 575 | |
| 576 | }; |
| 577 | |
| 578 | /* Bus Interface configuration structure |
| 579 | * |
| 580 | * This is allocated for each configured instance of a bus interface driver. |
| 581 | * |
| 582 | * The privdata pointer may be used by the bus interface driver to store private |
| 583 | * per instance state information. |
| 584 | */ |
| 585 | struct usb_bus_instance { |
| 586 | |
| 587 | struct usb_device_instance *device; |
| 588 | struct usb_endpoint_instance *endpoint_array; /* array of available configured endpoints */ |
| 589 | |
| 590 | int max_endpoints; /* maximimum number of rx enpoints */ |
| 591 | unsigned char maxpacketsize; |
| 592 | |
| 593 | unsigned int serial_number; |
| 594 | char *serial_number_str; |
| 595 | void *privdata; /* private data for the bus interface */ |
| 596 | |
| 597 | }; |
| 598 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 599 | void urb_link_init (urb_link * ul); |
| 600 | void urb_detach (struct urb *urb); |
| 601 | urb_link *first_urb_link (urb_link * hd); |
| 602 | struct urb *first_urb (urb_link * hd); |
| 603 | struct urb *first_urb_detached (urb_link * hd); |
| 604 | void urb_append (urb_link * hd, struct urb *urb); |
| 605 | |
| 606 | struct urb *usbd_alloc_urb (struct usb_device_instance *device, struct usb_endpoint_instance *endpoint); |
| 607 | void usbd_dealloc_urb (struct urb *urb); |
| 608 | |
| 609 | /* |
| 610 | * usbd_device_event is used by bus interface drivers to tell the higher layers that |
| 611 | * certain events have taken place. |
| 612 | */ |
| 613 | void usbd_device_event_irq (struct usb_device_instance *conf, usb_device_event_t, int); |
| 614 | void usbd_device_event (struct usb_device_instance *conf, usb_device_event_t, int); |
| 615 | |
| 616 | /* descriptors |
| 617 | * |
| 618 | * Various ways of finding descriptors based on the current device and any |
| 619 | * possible configuration / interface / endpoint for it. |
| 620 | */ |
| 621 | struct usb_configuration_descriptor *usbd_device_configuration_descriptor (struct usb_device_instance *, int, int); |
| 622 | struct usb_function_instance *usbd_device_function_instance (struct usb_device_instance *, unsigned int); |
| 623 | struct usb_interface_instance *usbd_device_interface_instance (struct usb_device_instance *, int, int, int); |
| 624 | struct usb_alternate_instance *usbd_device_alternate_instance (struct usb_device_instance *, int, int, int, int); |
| 625 | struct usb_interface_descriptor *usbd_device_interface_descriptor (struct usb_device_instance *, int, int, int, int); |
| 626 | struct usb_endpoint_descriptor *usbd_device_endpoint_descriptor_index (struct usb_device_instance *, int, int, int, int, int); |
| 627 | struct usb_class_descriptor *usbd_device_class_descriptor_index (struct usb_device_instance *, int, int, int, int, int); |
| 628 | struct usb_class_report_descriptor *usbd_device_class_report_descriptor_index( struct usb_device_instance *, int , int , int , int , int ); |
| 629 | struct usb_endpoint_descriptor *usbd_device_endpoint_descriptor (struct usb_device_instance *, int, int, int, int, int); |
| 630 | int usbd_device_endpoint_transfersize (struct usb_device_instance *, int, int, int, int, int); |
| 631 | struct usb_string_descriptor *usbd_get_string (u8); |
Vipin KUMAR | bdb1770 | 2012-03-26 15:38:06 +0530 | [diff] [blame] | 632 | struct usb_device_descriptor *usbd_device_device_descriptor(struct |
| 633 | usb_device_instance *, int); |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 634 | |
| 635 | int usbd_endpoint_halted (struct usb_device_instance *device, int endpoint); |
| 636 | void usbd_rcv_complete(struct usb_endpoint_instance *endpoint, int len, int urb_bad); |
| 637 | void usbd_tx_complete (struct usb_endpoint_instance *endpoint); |
| 638 | |
Tom Rix | ba7829e | 2009-10-31 12:37:39 -0500 | [diff] [blame] | 639 | /* These are macros used in debugging */ |
| 640 | #ifdef DEBUG |
| 641 | static inline void print_urb(struct urb *u) |
| 642 | { |
| 643 | serial_printf("urb %p\n", (u)); |
| 644 | serial_printf("\tendpoint %p\n", u->endpoint); |
| 645 | serial_printf("\tdevice %p\n", u->device); |
| 646 | serial_printf("\tbuffer %p\n", u->buffer); |
| 647 | serial_printf("\tbuffer_length %d\n", u->buffer_length); |
| 648 | serial_printf("\tactual_length %d\n", u->actual_length); |
| 649 | serial_printf("\tstatus %d\n", u->status); |
| 650 | serial_printf("\tdata %d\n", u->data); |
| 651 | } |
| 652 | |
| 653 | static inline void print_usb_device_request(struct usb_device_request *r) |
| 654 | { |
| 655 | serial_printf("usb request\n"); |
| 656 | serial_printf("\tbmRequestType 0x%2.2x\n", r->bmRequestType); |
| 657 | if ((r->bmRequestType & USB_REQ_DIRECTION_MASK) == 0) |
| 658 | serial_printf("\t\tDirection : To device\n"); |
| 659 | else |
| 660 | serial_printf("\t\tDirection : To host\n"); |
| 661 | if ((r->bmRequestType & USB_TYPE_STANDARD) == USB_TYPE_STANDARD) |
| 662 | serial_printf("\t\tType : Standard\n"); |
| 663 | if ((r->bmRequestType & USB_TYPE_CLASS) == USB_TYPE_CLASS) |
| 664 | serial_printf("\t\tType : Standard\n"); |
| 665 | if ((r->bmRequestType & USB_TYPE_VENDOR) == USB_TYPE_VENDOR) |
| 666 | serial_printf("\t\tType : Standard\n"); |
| 667 | if ((r->bmRequestType & USB_TYPE_RESERVED) == USB_TYPE_RESERVED) |
| 668 | serial_printf("\t\tType : Standard\n"); |
| 669 | if ((r->bmRequestType & USB_REQ_RECIPIENT_MASK) == |
| 670 | USB_REQ_RECIPIENT_DEVICE) |
| 671 | serial_printf("\t\tRecipient : Device\n"); |
| 672 | if ((r->bmRequestType & USB_REQ_RECIPIENT_MASK) == |
| 673 | USB_REQ_RECIPIENT_INTERFACE) |
| 674 | serial_printf("\t\tRecipient : Interface\n"); |
| 675 | if ((r->bmRequestType & USB_REQ_RECIPIENT_MASK) == |
| 676 | USB_REQ_RECIPIENT_ENDPOINT) |
| 677 | serial_printf("\t\tRecipient : Endpoint\n"); |
| 678 | if ((r->bmRequestType & USB_REQ_RECIPIENT_MASK) == |
| 679 | USB_REQ_RECIPIENT_OTHER) |
| 680 | serial_printf("\t\tRecipient : Other\n"); |
| 681 | serial_printf("\tbRequest 0x%2.2x\n", r->bRequest); |
| 682 | if (r->bRequest == USB_REQ_GET_STATUS) |
| 683 | serial_printf("\t\tGET_STATUS\n"); |
| 684 | else if (r->bRequest == USB_REQ_SET_ADDRESS) |
| 685 | serial_printf("\t\tSET_ADDRESS\n"); |
| 686 | else if (r->bRequest == USB_REQ_SET_FEATURE) |
| 687 | serial_printf("\t\tSET_FEATURE\n"); |
| 688 | else if (r->bRequest == USB_REQ_GET_DESCRIPTOR) |
| 689 | serial_printf("\t\tGET_DESCRIPTOR\n"); |
| 690 | else if (r->bRequest == USB_REQ_SET_CONFIGURATION) |
| 691 | serial_printf("\t\tSET_CONFIGURATION\n"); |
| 692 | else if (r->bRequest == USB_REQ_SET_INTERFACE) |
| 693 | serial_printf("\t\tUSB_REQ_SET_INTERFACE\n"); |
| 694 | else |
| 695 | serial_printf("\tUNKNOWN %d\n", r->bRequest); |
| 696 | serial_printf("\twValue 0x%4.4x\n", r->wValue); |
| 697 | if (r->bRequest == USB_REQ_GET_DESCRIPTOR) { |
| 698 | switch (r->wValue >> 8) { |
| 699 | case USB_DESCRIPTOR_TYPE_DEVICE: |
| 700 | serial_printf("\tDEVICE\n"); |
| 701 | break; |
| 702 | case USB_DESCRIPTOR_TYPE_CONFIGURATION: |
| 703 | serial_printf("\tCONFIGURATION\n"); |
| 704 | break; |
| 705 | case USB_DESCRIPTOR_TYPE_STRING: |
| 706 | serial_printf("\tSTRING\n"); |
| 707 | break; |
| 708 | case USB_DESCRIPTOR_TYPE_INTERFACE: |
| 709 | serial_printf("\tINTERFACE\n"); |
| 710 | break; |
| 711 | case USB_DESCRIPTOR_TYPE_ENDPOINT: |
| 712 | serial_printf("\tENDPOINT\n"); |
| 713 | break; |
| 714 | case USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER: |
| 715 | serial_printf("\tDEVICE_QUALIFIER\n"); |
| 716 | break; |
| 717 | case USB_DESCRIPTOR_TYPE_OTHER_SPEED_CONFIGURATION: |
| 718 | serial_printf("\tOTHER_SPEED_CONFIGURATION\n"); |
| 719 | break; |
| 720 | case USB_DESCRIPTOR_TYPE_INTERFACE_POWER: |
| 721 | serial_printf("\tINTERFACE_POWER\n"); |
| 722 | break; |
| 723 | case USB_DESCRIPTOR_TYPE_HID: |
| 724 | serial_printf("\tHID\n"); |
| 725 | break; |
| 726 | case USB_DESCRIPTOR_TYPE_REPORT: |
| 727 | serial_printf("\tREPORT\n"); |
| 728 | break; |
| 729 | default: |
| 730 | serial_printf("\tUNKNOWN TYPE\n"); |
| 731 | break; |
| 732 | } |
| 733 | } |
| 734 | serial_printf("\twIndex 0x%4.4x\n", r->wIndex); |
| 735 | serial_printf("\twLength 0x%4.4x\n", r->wLength); |
| 736 | } |
| 737 | #else |
| 738 | /* stubs */ |
| 739 | #define print_urb(u) |
| 740 | #define print_usb_device_request(r) |
| 741 | #endif /* DEBUG */ |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 742 | #endif |