1 /* $FreeBSD: src/sys/dev/usb/usb_compat_linux.h,v 1.3 2009/05/21 01:05:21 thompsa Exp $ */ 2 /*- 3 * Copyright (c) 2007 Luigi Rizzo - Universita` di Pisa. All rights reserved. 4 * Copyright (c) 2007-2021 Hans Petter Selasky. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 /* 29 * Many definitions in this header file derive 30 * from the Linux Kernel's usb.h and ch9.h 31 */ 32 33 #ifndef _LINUX_USB_H_ 34 #define _LINUX_USB_H_ 35 36 #include <linux/pm.h> 37 38 #ifndef __packed 39 #define __packed __attribute__((__packed__)) 40 #endif 41 42 struct usb_device; 43 struct usb_interface; 44 struct usb_driver; 45 struct usb_linux_softc; 46 struct usb_device_id; 47 struct input_id; 48 struct urb; 49 50 typedef void (*usb_complete_t)(struct urb *); 51 52 #define USB_LINUX_IFACE_MAX 32 53 #define USB_MAXIADS (USB_LINUX_IFACE_MAX / 2) 54 55 #define USB_SPEED_UNKNOWN 255 /* XXX */ 56 #define USB_SPEED_LOW LIBUSB20_SPEED_LOW 57 #define USB_SPEED_FULL LIBUSB20_SPEED_FULL 58 #define USB_SPEED_HIGH LIBUSB20_SPEED_HIGH 59 #define USB_SPEED_VARIABLE LIBUSB20_SPEED_VARIABLE 60 #define USB_SPEED_WIRELESS LIBUSB20_SPEED_VARIABLE 61 #define USB_SPEED_SUPER LIBUSB20_SPEED_SUPER 62 #define USB_SPEED_SUPER_PLUS 254 /* XXX */ 63 64 #define USB_CTRL_GET_TIMEOUT 5000 /* ms */ 65 #define USB_CTRL_SET_TIMEOUT 5000 /* ms */ 66 67 #if 0 68 /* 69 * Linux compatible USB device drivers put their device information 70 * into the "usb_device_id" structure using the "USB_DEVICE()" macro. 71 * The "MODULE_DEVICE_TABLE()" macro can be used to export this 72 * information to userland. 73 */ 74 struct usb_device_id { 75 /* which fields to match against */ 76 uint16_t match_flags; 77 #define USB_DEVICE_ID_MATCH_VENDOR 0x0001 78 #define USB_DEVICE_ID_MATCH_PRODUCT 0x0002 79 #define USB_DEVICE_ID_MATCH_DEV_LO 0x0004 80 #define USB_DEVICE_ID_MATCH_DEV_HI 0x0008 81 #define USB_DEVICE_ID_MATCH_DEV_CLASS 0x0010 82 #define USB_DEVICE_ID_MATCH_DEV_SUBCLASS 0x0020 83 #define USB_DEVICE_ID_MATCH_DEV_PROTOCOL 0x0040 84 #define USB_DEVICE_ID_MATCH_INT_CLASS 0x0080 85 #define USB_DEVICE_ID_MATCH_INT_SUBCLASS 0x0100 86 #define USB_DEVICE_ID_MATCH_INT_PROTOCOL 0x0200 87 #define USB_DEVICE_ID_MATCH_INT_INFO \ 88 (USB_DEVICE_ID_MATCH_INT_CLASS | \ 89 USB_DEVICE_ID_MATCH_INT_SUBCLASS | \ 90 USB_DEVICE_ID_MATCH_INT_PROTOCOL) 91 #define USB_DEVICE_ID_MATCH_DEV_RANGE \ 92 (USB_DEVICE_ID_MATCH_DEV_LO | \ 93 USB_DEVICE_ID_MATCH_DEV_HI) 94 #define USB_DEVICE_ID_MATCH_DEVICE_AND_VERSION \ 95 (USB_DEVICE_ID_MATCH_DEVICE | \ 96 USB_DEVICE_ID_MATCH_DEV_LO | \ 97 USB_DEVICE_ID_MATCH_DEV_HI) 98 99 /* Used for product specific matches; the BCD range is inclusive */ 100 uint16_t idVendor; 101 uint16_t idProduct; 102 uint16_t bcdDevice_lo; 103 uint16_t bcdDevice_hi; 104 105 /* Used for device class matches */ 106 uint8_t bDeviceClass; 107 uint8_t bDeviceSubClass; 108 uint8_t bDeviceProtocol; 109 110 /* Used for interface class matches */ 111 uint8_t bInterfaceClass; 112 uint8_t bInterfaceSubClass; 113 uint8_t bInterfaceProtocol; 114 115 /* Hook for driver specific information */ 116 unsigned long driver_info; 117 }; 118 119 #else 120 #define USB_DEVICE_ID_MATCH_INT_INFO \ 121 (USB_DEVICE_ID_MATCH_INT_CLASS | \ 122 USB_DEVICE_ID_MATCH_INT_SUBCLASS | \ 123 USB_DEVICE_ID_MATCH_INT_PROTOCOL) 124 #define USB_DEVICE_ID_MATCH_DEV_RANGE \ 125 (USB_DEVICE_ID_MATCH_DEV_LO | \ 126 USB_DEVICE_ID_MATCH_DEV_HI) 127 #define USB_DEVICE_ID_MATCH_DEVICE_AND_VERSION \ 128 (USB_DEVICE_ID_MATCH_DEVICE | \ 129 USB_DEVICE_ID_MATCH_DEV_LO | \ 130 USB_DEVICE_ID_MATCH_DEV_HI) 131 #endif 132 133 #define USB_DEVICE_ID_MATCH_DEVICE \ 134 (USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_PRODUCT) 135 136 #define USB_DEVICE(vend,prod) \ 137 .match_flags = USB_DEVICE_ID_MATCH_VENDOR | \ 138 USB_DEVICE_ID_MATCH_PRODUCT, \ 139 .idVendor = (vend), \ 140 .idProduct = (prod) 141 142 #define USB_DEVICE_VER(vend,prod,lo_ver,hi_ver) \ 143 .match_flags = USB_DEVICE_ID_MATCH_VENDOR | \ 144 USB_DEVICE_ID_MATCH_PRODUCT | \ 145 USB_DEVICE_ID_MATCH_DEV_LO | \ 146 USB_DEVICE_ID_MATCH_DEV_HI, \ 147 .idVendor = (vend), \ 148 .idProduct = (prod), \ 149 .bcdDevice_lo = (lo_ver), \ 150 .bcdDevice_hi = (hi_ver) 151 152 #define USB_INTERFACE_INFO(a,b,c)\ 153 .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS | \ 154 USB_DEVICE_ID_MATCH_INT_SUBCLASS | \ 155 USB_DEVICE_ID_MATCH_INT_PROTOCOL, \ 156 .bInterfaceClass = (a), \ 157 .bInterfaceSubClass = (b), \ 158 .bInterfaceProtocol = (c) 159 160 #define USB_DEVICE_AND_INTERFACE_INFO(vend, prod, a, b, c)\ 161 .match_flags = USB_DEVICE_ID_MATCH_VENDOR | \ 162 USB_DEVICE_ID_MATCH_PRODUCT | \ 163 USB_DEVICE_ID_MATCH_INT_CLASS | \ 164 USB_DEVICE_ID_MATCH_INT_SUBCLASS | \ 165 USB_DEVICE_ID_MATCH_INT_PROTOCOL, \ 166 .idVendor = (vend), \ 167 .idProduct = (prod), \ 168 .bInterfaceClass = (a), \ 169 .bInterfaceSubClass = (b), \ 170 .bInterfaceProtocol = (c) 171 172 /* The "usb_driver" structure holds the Linux USB device driver 173 * callbacks, and a pointer to device ID's which this entry should 174 * match against. Usually this entry is exposed to the USB emulation 175 * layer using the "USB_DRIVER_EXPORT()" macro, which is defined 176 * below. 177 */ 178 struct usb_driver { 179 const char *name; 180 181 int (*probe) (struct usb_interface *intf, 182 const struct usb_device_id *id); 183 184 void (*disconnect) (struct usb_interface *intf); 185 186 int (*ioctl) (struct usb_interface *intf, unsigned int code, 187 void *buf); 188 189 int (*suspend) (struct usb_interface *intf, pm_message_t message); 190 int (*resume) (struct usb_interface *intf); 191 192 int (*reset_resume) (struct usb_interface *intf); 193 194 const struct usb_device_id *id_table; 195 196 void (*shutdown) (struct usb_interface *intf); 197 198 int (*pre_reset) (struct usb_interface *); 199 int (*post_reset) (struct usb_interface *); 200 201 LIST_ENTRY(usb_driver) linux_driver_list; 202 203 uint8_t supports_autosuspend; 204 uint8_t soft_unbind; 205 uint8_t no_dynamic_id; 206 }; 207 208 /* 209 * Generic USB descriptor header. 210 */ 211 struct usb_descriptor_header { 212 uint8_t bLength; 213 uint8_t bDescriptorType; 214 } __packed; 215 216 /* 217 * The following structure is the same as "usb_device_descriptor_t" 218 * except that 16-bit values are "uint16_t" and not an array of "uint8_t". 219 * It is used by Linux USB device drivers. 220 */ 221 struct usb_device_descriptor { 222 uint8_t bLength; 223 uint8_t bDescriptorType; 224 225 uint16_t bcdUSB; 226 uint8_t bDeviceClass; 227 uint8_t bDeviceSubClass; 228 uint8_t bDeviceProtocol; 229 uint8_t bMaxPacketSize0; 230 uint16_t idVendor; 231 uint16_t idProduct; 232 uint16_t bcdDevice; 233 uint8_t iManufacturer; 234 uint8_t iProduct; 235 uint8_t iSerialNumber; 236 uint8_t bNumConfigurations; 237 } __packed; 238 239 /* 240 * The following structure is the same as "usb_config_descriptor_t" 241 * except that 16-bit values are "uint16_t" and not an array of "uint8_t". 242 * It is used by Linux USB device drivers. 243 */ 244 struct usb_config_descriptor { 245 uint8_t bLength; 246 uint8_t bDescriptorType; 247 248 uint16_t wTotalLength; 249 uint8_t bNumInterfaces; 250 uint8_t bConfigurationValue; 251 uint8_t iConfiguration; 252 uint8_t bmAttributes; 253 #define USB_CONFIG_ATT_ONE (1 << 7) /* must be set */ 254 #define USB_CONFIG_ATT_SELFPOWER (1 << 6) /* self powered */ 255 #define USB_CONFIG_ATT_WAKEUP (1 << 5) /* can wakeup */ 256 #define USB_CONFIG_ATT_BATTERY (1 << 4) /* battery powered */ 257 uint8_t bMaxPower; /* max current in 2 mA units */ 258 } __packed; 259 260 /* 261 * The following structure is the same as 262 * "usb_interface_descriptor_t". It is used by 263 * Linux USB device drivers. 264 */ 265 struct usb_interface_descriptor { 266 uint8_t bLength; 267 uint8_t bDescriptorType; 268 269 uint8_t bInterfaceNumber; 270 uint8_t bAlternateSetting; 271 uint8_t bNumEndpoints; 272 uint8_t bInterfaceClass; 273 uint8_t bInterfaceSubClass; 274 uint8_t bInterfaceProtocol; 275 uint8_t iInterface; 276 } __packed; 277 278 struct usb_interface_assoc_descriptor { 279 uint8_t bLength; 280 uint8_t bDescriptorType; 281 282 uint8_t bFirstInterface; 283 uint8_t bInterfaceCount; 284 uint8_t bFunctionClass; 285 uint8_t bFunctionSubClass; 286 uint8_t bFunctionProtocol; 287 uint8_t iFunction; 288 } __packed; 289 290 /* 291 * The following structure is the same as "usb_endpoint_descriptor_t" 292 * except that 16-bit values are "uint16_t" and not an array of "uint8_t". 293 * It is used by Linux USB device drivers. 294 */ 295 struct usb_endpoint_descriptor { 296 uint8_t bLength; 297 uint8_t bDescriptorType; 298 299 uint8_t bEndpointAddress; 300 uint8_t bmAttributes; 301 uint16_t wMaxPacketSize; 302 uint8_t bInterval; 303 304 /* extension for audio endpoints only: */ 305 uint8_t bRefresh; 306 uint8_t bSynchAddress; 307 } __packed; 308 309 struct usb_ss_ep_comp_descriptor { 310 uint8_t bLength; 311 uint8_t bDescriptorType; 312 uint8_t bMaxBurst; 313 uint8_t bmAttributes; 314 uint16_t wBytesPerInterval; 315 } __packed; 316 317 #define USB_DT_SS_EP_COMP_SIZE 6 318 #define USB_SS_MAX_STREAMS(x) (1 << ((x) & 0x1f)) 319 #define USB_SS_MULT(x) (1 + ((x) & 0x3)) 320 321 #define USB_DT_ENDPOINT_SIZE 7 322 #define USB_DT_ENDPOINT_AUDIO_SIZE 9 323 324 /* 325 * Endpoints 326 */ 327 #define USB_ENDPOINT_NUMBER_MASK 0x0f /* in bEndpointAddress */ 328 #define USB_ENDPOINT_DIR_MASK 0x80 329 330 #define USB_ENDPOINT_XFERTYPE_MASK 0x03 /* in bmAttributes */ 331 #define USB_ENDPOINT_XFER_CONTROL 0 332 #define USB_ENDPOINT_XFER_ISOC 1 333 #define USB_ENDPOINT_XFER_BULK 2 334 #define USB_ENDPOINT_XFER_INT 3 335 #define USB_ENDPOINT_MAX_ADJUSTABLE 0x80 336 337 /* CONTROL REQUEST SUPPORT */ 338 339 /* 340 * Definition of direction mask for 341 * "bEndpointAddress" and "bmRequestType": 342 */ 343 #define USB_DIR_MASK 0x80 344 #define USB_DIR_OUT 0x00 /* write to USB device */ 345 #define USB_DIR_IN 0x80 /* read from USB device */ 346 347 /* 348 * Definition of type mask for 349 * "bmRequestType": 350 */ 351 #define USB_TYPE_MASK (0x03 << 5) 352 #define USB_TYPE_STANDARD (0x00 << 5) 353 #define USB_TYPE_CLASS (0x01 << 5) 354 #define USB_TYPE_VENDOR (0x02 << 5) 355 #define USB_TYPE_RESERVED (0x03 << 5) 356 357 /* 358 * Definition of receiver mask for 359 * "bmRequestType": 360 */ 361 #define USB_RECIP_MASK 0x1f 362 #define USB_RECIP_DEVICE 0x00 363 #define USB_RECIP_INTERFACE 0x01 364 #define USB_RECIP_ENDPOINT 0x02 365 #define USB_RECIP_OTHER 0x03 366 367 /* 368 * Definition of standard request values for 369 * "bRequest": 370 */ 371 #define USB_REQ_GET_STATUS 0x00 372 #define USB_REQ_CLEAR_FEATURE 0x01 373 #define USB_REQ_SET_FEATURE 0x03 374 #define USB_REQ_SET_ADDRESS 0x05 375 #define USB_REQ_GET_DESCRIPTOR 0x06 376 #define USB_REQ_SET_DESCRIPTOR 0x07 377 #define USB_REQ_GET_CONFIGURATION 0x08 378 #define USB_REQ_SET_CONFIGURATION 0x09 379 #define USB_REQ_GET_INTERFACE 0x0A 380 #define USB_REQ_SET_INTERFACE 0x0B 381 #define USB_REQ_SYNCH_FRAME 0x0C 382 383 #define USB_REQ_SET_ENCRYPTION 0x0D /* Wireless USB */ 384 #define USB_REQ_GET_ENCRYPTION 0x0E 385 #define USB_REQ_SET_HANDSHAKE 0x0F 386 #define USB_REQ_GET_HANDSHAKE 0x10 387 #define USB_REQ_SET_CONNECTION 0x11 388 #define USB_REQ_SET_SECURITY_DATA 0x12 389 #define USB_REQ_GET_SECURITY_DATA 0x13 390 #define USB_REQ_SET_WUSB_DATA 0x14 391 #define USB_REQ_LOOPBACK_DATA_WRITE 0x15 392 #define USB_REQ_LOOPBACK_DATA_READ 0x16 393 #define USB_REQ_SET_INTERFACE_DS 0x17 394 395 /* 396 * USB feature flags are written using USB_REQ_{CLEAR,SET}_FEATURE, and 397 * are read as a bit array returned by USB_REQ_GET_STATUS. (So there 398 * are at most sixteen features of each type.) 399 */ 400 #define USB_DEVICE_SELF_POWERED 0 /* (read only) */ 401 #define USB_DEVICE_REMOTE_WAKEUP 1 /* dev may initiate wakeup */ 402 #define USB_DEVICE_TEST_MODE 2 /* (wired high speed only) */ 403 #define USB_DEVICE_BATTERY 2 /* (wireless) */ 404 #define USB_DEVICE_B_HNP_ENABLE 3 /* (otg) dev may initiate HNP */ 405 #define USB_DEVICE_WUSB_DEVICE 3 /* (wireless) */ 406 #define USB_DEVICE_A_HNP_SUPPORT 4 /* (otg) RH port supports HNP */ 407 #define USB_DEVICE_A_ALT_HNP_SUPPORT 5 /* (otg) other RH port does */ 408 #define USB_DEVICE_DEBUG_MODE 6 /* (special devices only) */ 409 410 #define USB_ENDPOINT_HALT 0 /* IN/OUT will STALL */ 411 412 #define PIPE_ISOCHRONOUS 0x01 /* UE_ISOCHRONOUS */ 413 #define PIPE_INTERRUPT 0x03 /* UE_INTERRUPT */ 414 #define PIPE_CONTROL 0x00 /* UE_CONTROL */ 415 #define PIPE_BULK 0x02 /* UE_BULK */ 416 417 /* Whenever Linux references an USB endpoint: 418 * a) to initialize "urb->pipe" 419 * b) second argument passed to "usb_control_msg()" 420 * 421 * Then it uses one of the following macros. The "endpoint" argument 422 * is the physical endpoint value masked by 0xF. The "dev" argument 423 * is a pointer to "struct usb_device". 424 */ 425 #define usb_sndctrlpipe(dev,endpoint) \ 426 usb_create_host_endpoint(dev, PIPE_CONTROL, (endpoint) & ~USB_DIR_IN) 427 428 #define usb_rcvctrlpipe(dev,endpoint) \ 429 usb_create_host_endpoint(dev, PIPE_CONTROL, (endpoint) | USB_DIR_IN) 430 431 #define usb_sndisocpipe(dev,endpoint) \ 432 usb_create_host_endpoint(dev, PIPE_ISOCHRONOUS, (endpoint) & ~USB_DIR_IN) 433 434 #define usb_rcvisocpipe(dev,endpoint) \ 435 usb_create_host_endpoint(dev, PIPE_ISOCHRONOUS, (endpoint) | USB_DIR_IN) 436 437 #define usb_sndbulkpipe(dev,endpoint) \ 438 usb_create_host_endpoint(dev, PIPE_BULK, (endpoint) & ~USB_DIR_IN) 439 440 #define usb_rcvbulkpipe(dev,endpoint) \ 441 usb_create_host_endpoint(dev, PIPE_BULK, (endpoint) | USB_DIR_IN) 442 443 #define usb_sndintpipe(dev,endpoint) \ 444 usb_create_host_endpoint(dev, PIPE_INTERRUPT, (endpoint) & ~USB_DIR_IN) 445 446 #define usb_rcvintpipe(dev,endpoint) \ 447 usb_create_host_endpoint(dev, PIPE_INTERRUPT, (endpoint) | USB_DIR_IN) 448 449 #define usb_pipein(endpoint) ((endpoint) & USB_DIR_IN) 450 #define usb_pipeout(endpoint) ((~(endpoint)) & USB_DIR_IN) 451 #define usb_pipeendpoint(endpoint) (((endpoint) >> 15) & 0xf) 452 453 /* The following four structures makes up a tree, where we have the 454 * leaf structure, "usb_host_endpoint", first, and the root structure, 455 * "usb_device", last. The four structures below mirror the structure 456 * of the USB descriptors belonging to an USB configuration. Please 457 * refer to the USB specification for a definition of "endpoints" and 458 * "interfaces". 459 */ 460 struct usb_host_endpoint { 461 struct usb_endpoint_descriptor desc; 462 struct usb_ss_ep_comp_descriptor ss_ep_comp; 463 464 TAILQ_HEAD(, urb) bsd_urb_list; 465 466 struct libusb20_transfer *bsd_xfer[2]; 467 468 uint8_t *extra; /* Extra descriptors */ 469 470 uint16_t extralen; 471 472 uint8_t bsd_iface_index; 473 474 void *align[0]; 475 }; 476 477 struct usb_host_interface { 478 struct usb_interface_descriptor desc; 479 480 /* the following array has size "desc.bNumEndpoint" */ 481 struct usb_host_endpoint *endpoint; 482 483 const char *string; /* iInterface string, if present */ 484 uint8_t *extra; /* Extra descriptors */ 485 486 uint16_t extralen; 487 488 uint8_t bsd_iface_index; 489 490 void *align[0]; 491 }; 492 493 struct usb_interface { 494 struct device dev; 495 496 /* array of alternate settings for this interface */ 497 struct usb_host_interface *altsetting; 498 struct usb_host_interface *cur_altsetting; 499 struct usb_interface_assoc_descriptor *intf_assoc; 500 struct usb_device *usb_dev; 501 void *bsd_priv_sc; /* device specific information */ 502 503 uint8_t num_altsetting; /* number of alternate settings */ 504 uint8_t bsd_iface_index; 505 uint8_t needs_remote_wakeup; 506 507 void *align[0]; 508 }; 509 510 #define usb_host_config usb_config 511 512 struct usb_config { 513 struct usb_config_descriptor desc; 514 struct usb_interface *interface[USB_LINUX_IFACE_MAX]; 515 struct usb_interface_assoc_descriptor *intf_assoc[USB_MAXIADS]; 516 }; 517 518 struct usb_device { 519 struct device dev; 520 struct device *bus; 521 void *parent; 522 struct usb_config *config; 523 struct usb_config *actconfig; 524 struct usb_host_endpoint *ep_in[16]; 525 struct usb_host_endpoint *ep_out[16]; 526 527 struct usb_device_descriptor descriptor; 528 struct usb_config bsd_config; 529 struct usb_host_endpoint ep0; 530 531 struct libusb20_device *bsd_udev; 532 struct usb_interface *bsd_iface_start; 533 struct usb_interface *bsd_iface_end; 534 struct usb_host_endpoint *bsd_endpoint_start; 535 struct usb_host_endpoint *bsd_endpoint_end; 536 537 /* static strings from the device */ 538 char *product; /* iProduct string, if present */ 539 char *manufacturer; /* iManufacturer string, if present */ 540 char *serial; /* iSerialNumber string, if present */ 541 542 uint32_t quirks; /* quirks, not implemented */ 543 544 uint16_t devnum; 545 uint16_t bsd_last_ms; /* completion time of last ISOC 546 * transfer */ 547 548 uint8_t speed; /* LIBUSB20_SPEED_XXX */ 549 550 char devpath[1]; 551 552 void *align[0]; 553 }; 554 555 /* 556 * The following structure is used to extend "struct urb" when we are 557 * dealing with an isochronous endpoint. It contains information about 558 * the data offset and data length of an isochronous packet. 559 * The "actual_length" field is updated before the "complete" 560 * callback in the "urb" structure is called. 561 */ 562 struct usb_iso_packet_descriptor { 563 uint32_t offset; /* depreciated buffer offset (the 564 * packets are usually back to back) */ 565 uint16_t length; /* expected length */ 566 uint16_t actual_length; 567 int16_t status; /* status */ 568 }; 569 570 /* 571 * The following structure holds various information about an USB 572 * transfer. This structure is used for all kinds of USB transfers. 573 * 574 * URB is short for USB Request Block. 575 */ 576 struct urb { 577 TAILQ_ENTRY(urb) bsd_urb_list; 578 579 struct usb_device *dev; /* (in) pointer to associated device */ 580 unsigned int pipe; /* (in) pipe */ 581 uint8_t *setup_packet; /* (in) setup packet (control only) */ 582 void *transfer_buffer; /* (in) associated data buffer */ 583 void *context; /* (in) context for completion */ 584 void *hcpriv; /* non-zero when URB is queued */ 585 #define USB_HCPRIV_QUEUED ((void *)1) 586 usb_complete_t complete; /* (in) completion routine */ 587 588 uint32_t transfer_buffer_length;/* (in) data buffer length */ 589 uint32_t actual_length; /* (return) actual transfer length */ 590 uint32_t timeout; /* (in) FreeBSD specific */ 591 uint32_t reject; /* (internal) reject URB */ 592 593 struct kref kref; /* refcount of the URB */ 594 struct list_head anchor_list; /* the URB may be anchored */ 595 struct usb_anchor *anchor; 596 597 uint16_t transfer_flags; /* (in) */ 598 #define URB_SHORT_NOT_OK 0x0001 /* report short transfers like errors */ 599 #define URB_ISO_ASAP 0x0002 /* ignore "start_frame" field */ 600 #define URB_ZERO_PACKET 0x0004 /* the USB transfer ends with a short 601 * packet */ 602 #define URB_NO_TRANSFER_DMA_MAP 0x0008 /* "transfer_dma" is valid on submit */ 603 #define URB_WAIT_WAKEUP 0x0010 /* custom flags */ 604 #define URB_IS_SLEEPING 0x0020 /* custom flags */ 605 #define URB_FREE_BUFFER 0x0040 /* free transfer buffer with the URB */ 606 607 uint16_t start_frame; /* (modify) start frame (ISO) */ 608 uint16_t number_of_packets; /* (in) number of ISO packets */ 609 uint16_t interval; /* (modify) transfer interval 610 * (INT/ISO) */ 611 uint16_t error_count; /* (return) number of ISO errors */ 612 int16_t status; /* (return) status */ 613 614 uint8_t setup_dma; /* (in) not used on FreeBSD */ 615 dma_addr_t transfer_dma; /* (in) not used on FreeBSD */ 616 uint8_t bsd_no_resubmit; /* (internal) FreeBSD specific */ 617 618 struct usb_iso_packet_descriptor iso_frame_desc[]; /* (in) ISO ONLY */ 619 }; 620 621 /* various prototypes */ 622 623 uint16_t usb_get_current_frame_number(struct usb_device *dev); 624 625 int usb_submit_urb(struct urb *urb, uint16_t mem_flags); 626 int usb_unlink_urb(struct urb *urb); 627 int usb_clear_halt(struct usb_device *dev, unsigned int); 628 int usb_control_msg(struct usb_device *dev, unsigned int, uint8_t request, uint8_t requesttype, uint16_t value, uint16_t index, void *data, uint16_t size, uint32_t timeout); 629 int usb_set_interface(struct usb_device *dev, uint8_t ifnum, uint8_t alternate); 630 631 unsigned int usb_create_host_endpoint(struct usb_device *dev, uint8_t type, uint8_t ep); 632 struct urb *usb_alloc_urb(uint16_t iso_packets, uint16_t mem_flags); 633 struct usb_host_interface *usb_altnum_to_altsetting(const struct usb_interface *intf, uint8_t alt_index); 634 struct usb_interface *usb_ifnum_to_if(struct usb_device *dev, uint8_t iface_no); 635 636 void *usb_buffer_alloc(struct usb_device *dev, uint32_t size, uint16_t mem_flags, dma_addr_t *dma_addr); 637 void *usb_get_intfdata(struct usb_interface *intf); 638 639 void usb_buffer_free(struct usb_device *dev, uint32_t size, void *addr, dma_addr_t dma_addr); 640 void usb_free_urb(struct urb *urb); 641 #define usb_put_urb(urb) usb_free_urb(urb) 642 struct urb *usb_get_urb(struct urb *); 643 void usb_init_urb(struct urb *urb); 644 void usb_kill_urb(struct urb *urb); 645 void usb_set_intfdata(struct usb_interface *intf, void *data); 646 int usb_register(struct usb_driver *drv); 647 int usb_deregister(struct usb_driver *drv); 648 649 struct usb_linux_softc *usb_linux2usb(int fd); 650 int usb_linux_probe_p(int *p_bus, int *p_addr, int *p_index, const char **ppdesc); 651 int usb_linux_detach(int fd); 652 int usb_linux_suspend(int fd); 653 int usb_linux_resume(int fd); 654 655 #define interface_to_usbdev(intf) (intf)->usb_dev 656 #define interface_to_bsddev(intf) (intf)->usb_dev->bsd_udev 657 658 /* chapter 9 stuff, taken from "include/linux/usb/ch9.h" */ 659 660 #define USB_DT_DEVICE 0x01 661 #define USB_DT_CONFIG 0x02 662 #define USB_DT_STRING 0x03 663 #define USB_DT_INTERFACE 0x04 664 #define USB_DT_ENDPOINT 0x05 665 #define USB_DT_DEVICE_QUALIFIER 0x06 666 #define USB_DT_OTHER_SPEED_CONFIG 0x07 667 #define USB_DT_INTERFACE_POWER 0x08 668 #define USB_DT_OTG 0x09 669 #define USB_DT_DEBUG 0x0a 670 #define USB_DT_INTERFACE_ASSOCIATION 0x0b 671 #define USB_DT_SECURITY 0x0c 672 #define USB_DT_KEY 0x0d 673 #define USB_DT_ENCRYPTION_TYPE 0x0e 674 #define USB_DT_BOS 0x0f 675 #define USB_DT_DEVICE_CAPABILITY 0x10 676 #define USB_DT_WIRELESS_ENDPOINT_COMP 0x11 677 #define USB_DT_WIRE_ADAPTER 0x21 678 #define USB_DT_RPIPE 0x22 679 #define USB_DT_CS_RADIO_CONTROL 0x23 680 #define USB_DT_SS_ENDPOINT_COMP 0x30 681 682 #define USB_DT_CS_DEVICE (USB_TYPE_CLASS | USB_DT_DEVICE) 683 #define USB_DT_CS_CONFIG (USB_TYPE_CLASS | USB_DT_CONFIG) 684 #define USB_DT_CS_STRING (USB_TYPE_CLASS | USB_DT_STRING) 685 #define USB_DT_CS_INTERFACE (USB_TYPE_CLASS | USB_DT_INTERFACE) 686 #define USB_DT_CS_ENDPOINT (USB_TYPE_CLASS | USB_DT_ENDPOINT) 687 688 #define USB_CLASS_PER_INTERFACE 0x00 689 #define USB_CLASS_AUDIO 0x01 690 #define USB_CLASS_COMM 0x02 691 #define USB_CLASS_HID 0x03 692 #define USB_CLASS_PHYSICAL 0x05 693 #define USB_CLASS_STILL_IMAGE 0x06 694 #define USB_CLASS_PRINTER 0x07 695 #define USB_CLASS_MASS_STORAGE 0x08 696 #define USB_CLASS_HUB 0x09 697 #define USB_CLASS_CDC_DATA 0x0a 698 #define USB_CLASS_CSCID 0x0b 699 #define USB_CLASS_CONTENT_SEC 0x0d 700 #define USB_CLASS_VIDEO 0x0e 701 #define USB_CLASS_WIRELESS_CONTROLLER 0xe0 702 #define USB_CLASS_MISC 0xef 703 #define USB_CLASS_APP_SPEC 0xfe 704 #define USB_CLASS_VENDOR_SPEC 0xff 705 706 struct usb_ctrlrequest { 707 __u8 bRequestType; 708 __u8 bRequest; 709 __le16 wValue; 710 __le16 wIndex; 711 __le16 wLength; 712 } __packed; 713 714 int usb_endpoint_dir_in(const struct usb_endpoint_descriptor *epd); 715 int usb_endpoint_dir_out(const struct usb_endpoint_descriptor *epd); 716 int usb_endpoint_xfer_bulk(const struct usb_endpoint_descriptor *epd); 717 int usb_endpoint_xfer_control(const struct usb_endpoint_descriptor *epd); 718 int usb_endpoint_xfer_int(const struct usb_endpoint_descriptor *epd); 719 int usb_endpoint_xfer_isoc(const struct usb_endpoint_descriptor *epd); 720 void usb_fill_control_urb(struct urb *, struct usb_device *, unsigned int, unsigned char *setup_packet, void *transfer_buffer, int buffer_length, usb_complete_t complete_fn, void *context); 721 void usb_fill_bulk_urb(struct urb *, struct usb_device *, unsigned int, void *transfer_buffer, int buffer_length, usb_complete_t complete_fn, void *context); 722 void usb_fill_int_urb(struct urb *, struct usb_device *, unsigned int, void *transfer_buffer, int buffer_length, usb_complete_t complete_fn, void *context, int interval); 723 struct usb_interface *usb_get_intf(struct usb_interface *intf); 724 void usb_put_intf(struct usb_interface *intf); 725 struct usb_device *usb_get_dev(struct usb_device *intf); 726 void usb_put_dev(struct usb_device *intf); 727 int usb_string(struct usb_device *dev, int index, char *buf, size_t size); 728 int usb_make_path(struct usb_device *dev, char *buf, size_t size); 729 int usb_autopm_get_interface(struct usb_interface *); 730 int usb_autopm_set_interface(struct usb_interface *); 731 int usb_driver_claim_interface(struct usb_driver *, struct usb_interface *, void *); 732 733 #define usb_autopm_put_interface(...) __nop 734 #define usb_autopm_enable(...) __nop 735 #define usb_autopm_disable(...) __nop 736 #define usb_mark_last_busy(...) __nop 737 #define usb_driver_release_interface(...) __nop 738 739 #define usb_endpoint_type(epd) \ 740 ((epd)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) 741 #define usb_endpoint_num(epd) \ 742 ((epd)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK) 743 #define usb_endpoint_is_bulk_in(epd) \ 744 (usb_endpoint_xfer_bulk(epd) && usb_endpoint_dir_in(epd)) 745 #define usb_endpoint_is_bulk_out(epd) \ 746 (usb_endpoint_xfer_bulk(epd) && usb_endpoint_dir_out(epd)) 747 #define usb_endpoint_is_int_in(epd) \ 748 (usb_endpoint_xfer_int(epd) && usb_endpoint_dir_in(epd)) 749 #define usb_endpoint_is_int_out(epd) \ 750 (usb_endpoint_xfer_int(epd) && usb_endpoint_dir_out(epd)) 751 #define usb_endpoint_is_isoc_in(epd) \ 752 (usb_endpoint_xfer_isoc(epd) && usb_endpoint_dir_in(epd)) 753 #define usb_endpoint_is_isoc_out(epd) \ 754 (usb_endpoint_xfer_isoc(epd) && usb_endpoint_dir_out(epd)) 755 756 int usb_bulk_msg(struct usb_device *, unsigned int, void *, int, int *, int); 757 int usb_match_device(struct usb_device *, const struct usb_device_id *); 758 int usb_match_one_id(struct usb_interface *, const struct usb_device_id *); 759 const struct usb_device_id *usb_match_id(struct usb_interface *, const struct usb_device_id *); 760 int usb_reset_configuration(struct usb_device *dev); 761 int usb_lock_device_for_reset(struct usb_device *udev, const struct usb_interface *iface); 762 void usb_unlock_device(struct usb_device *udev); 763 int usb_reset_device(struct usb_device *dev); 764 void usb_reset_endpoint(struct usb_device *, unsigned int); 765 uint8_t usb_pipetype(unsigned int); 766 uint16_t usb_maxpacket(struct usb_device *dev, int endpoint, int is_out); 767 void usb_enable_autosuspend(struct usb_device *udev); 768 int __usb_get_extra_descriptor(char *, unsigned, unsigned char, void **); 769 int usb_translate_errors(int); 770 771 #define usb_pipeisoc(pipe) (usb_pipetype((pipe)) == PIPE_ISOCHRONOUS) 772 #define usb_pipeint(pipe) (usb_pipetype((pipe)) == PIPE_INTERRUPT) 773 #define usb_pipecontrol(pipe) (usb_pipetype((pipe)) == PIPE_CONTROL) 774 #define usb_pipebulk(pipe) (usb_pipetype((pipe)) == PIPE_BULK) 775 776 #define usb_interrupt_msg(dev, pipe, data, len, actlen, timo) \ 777 usb_bulk_msg(dev, pipe, data, len, actlen, timo) 778 779 #define usb_get_extra_descriptor(desc, type, ptr) \ 780 __usb_get_extra_descriptor((desc)->extra, \ 781 (desc)->extralen, type, (void *)(ptr)) 782 783 #define usb_hub_for_each_child(a,b,c) \ 784 for ((b) = 0, (c) = NULL; 0;) 785 786 int usb_endpoint_maxp(const struct usb_endpoint_descriptor *); 787 int usb_endpoint_maxp_mult(const struct usb_endpoint_descriptor *); 788 789 #define usb_alloc_coherent(...) usb_buffer_alloc(__VA_ARGS__) 790 #define usb_free_coherent(...) usb_buffer_free(__VA_ARGS__) 791 #define usb_debug_root NULL 792 793 #define to_usb_interface(d) container_of(d, struct usb_interface, dev) 794 #define to_usb_device(d) container_of(d, struct usb_device, dev) 795 796 #define USB_STATE_URB_BUF 0x01 797 798 int usb_autopm_get_interface_async(struct usb_interface *); 799 int usb_autopm_get_interface_no_resume(struct usb_interface *); 800 int usb_autopm_get_interface_no_suspend(struct usb_interface *); 801 802 void usb_autopm_put_interface_async(struct usb_interface *); 803 void usb_autopm_put_interface_no_suspend(struct usb_interface *); 804 void usb_autopm_put_interface_no_resume(struct usb_interface *); 805 806 void usb_block_urb(struct urb *); 807 void usb_unblock_urb(struct urb *); 808 809 void usb_queue_reset_device(struct usb_interface *); 810 811 struct usb_host_endpoint *usb_pipe_endpoint(struct usb_device *, unsigned int); 812 int usb_urb_ep_type_check(const struct urb *); 813 814 struct usb_anchor { 815 struct list_head urb_list; 816 wait_queue_head_t wait; 817 spinlock_t lock; 818 atomic_t suspend_wakeups; 819 }; 820 821 void usb_anchor_urb(struct urb *, struct usb_anchor *); 822 void usb_unanchor_urb(struct urb *); 823 void init_usb_anchor(struct usb_anchor *); 824 int usb_wait_anchor_empty_timeout(struct usb_anchor *, unsigned int timeout); 825 void usb_kill_anchored_urbs(struct usb_anchor *); 826 void usb_poison_urb(struct urb *); 827 void usb_unpoison_urb(struct urb *); 828 829 struct usb_sg_request { 830 int status; 831 size_t bytes; 832 833 spinlock_t lock; 834 835 struct usb_device *dev; 836 int pipe; 837 838 int entries; 839 struct urb **urbs; 840 841 int count; 842 struct completion complete; 843 }; 844 845 int usb_sg_init( 846 struct usb_sg_request *, 847 struct usb_device *, 848 unsigned pipe, 849 unsigned period, 850 struct scatterlist *, 851 int nents, 852 size_t length, 853 gfp_t mem_flags 854 ); 855 void usb_sg_cancel(struct usb_sg_request *); 856 void usb_sg_wait(struct usb_sg_request *); 857 858 #endif /* _LINUX_USB_H_ */ 859