1df4b8c2aSAndrew Thompson /*- 2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause 35e53a4f9SPedro F. Giffuni * 4df4b8c2aSAndrew Thompson * Copyright (c) 2008 Hans Petter Selasky. All rights reserved. 5df4b8c2aSAndrew Thompson * 6df4b8c2aSAndrew Thompson * Redistribution and use in source and binary forms, with or without 7df4b8c2aSAndrew Thompson * modification, are permitted provided that the following conditions 8df4b8c2aSAndrew Thompson * are met: 9df4b8c2aSAndrew Thompson * 1. Redistributions of source code must retain the above copyright 10df4b8c2aSAndrew Thompson * notice, this list of conditions and the following disclaimer. 11df4b8c2aSAndrew Thompson * 2. Redistributions in binary form must reproduce the above copyright 12df4b8c2aSAndrew Thompson * notice, this list of conditions and the following disclaimer in the 13df4b8c2aSAndrew Thompson * documentation and/or other materials provided with the distribution. 14df4b8c2aSAndrew Thompson * 15df4b8c2aSAndrew Thompson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16df4b8c2aSAndrew Thompson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17df4b8c2aSAndrew Thompson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18df4b8c2aSAndrew Thompson * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19df4b8c2aSAndrew Thompson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20df4b8c2aSAndrew Thompson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21df4b8c2aSAndrew Thompson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22df4b8c2aSAndrew Thompson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23df4b8c2aSAndrew Thompson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24df4b8c2aSAndrew Thompson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25df4b8c2aSAndrew Thompson * SUCH DAMAGE. 26df4b8c2aSAndrew Thompson */ 27df4b8c2aSAndrew Thompson 28df4b8c2aSAndrew Thompson #ifndef _LIBUSB20_COMPAT_01_H_ 29df4b8c2aSAndrew Thompson #define _LIBUSB20_COMPAT_01_H_ 30df4b8c2aSAndrew Thompson 3166194130SHans Petter Selasky #ifndef LIBUSB_GLOBAL_INCLUDE_FILE 3266194130SHans Petter Selasky #include <stdint.h> 33df4b8c2aSAndrew Thompson #include <sys/param.h> 34f3cba95cSWojciech A. Koszek #include <sys/endian.h> 3566194130SHans Petter Selasky #endif 36df4b8c2aSAndrew Thompson 37df4b8c2aSAndrew Thompson /* USB interface class codes */ 38df4b8c2aSAndrew Thompson 39df4b8c2aSAndrew Thompson #define USB_CLASS_PER_INTERFACE 0 40df4b8c2aSAndrew Thompson #define USB_CLASS_AUDIO 1 41df4b8c2aSAndrew Thompson #define USB_CLASS_COMM 2 42df4b8c2aSAndrew Thompson #define USB_CLASS_HID 3 43df4b8c2aSAndrew Thompson #define USB_CLASS_PRINTER 7 44df4b8c2aSAndrew Thompson #define USB_CLASS_PTP 6 45df4b8c2aSAndrew Thompson #define USB_CLASS_MASS_STORAGE 8 46df4b8c2aSAndrew Thompson #define USB_CLASS_HUB 9 47df4b8c2aSAndrew Thompson #define USB_CLASS_DATA 10 48df4b8c2aSAndrew Thompson #define USB_CLASS_VENDOR_SPEC 0xff 49df4b8c2aSAndrew Thompson 50df4b8c2aSAndrew Thompson /* USB descriptor types */ 51df4b8c2aSAndrew Thompson 52df4b8c2aSAndrew Thompson #define USB_DT_DEVICE 0x01 53df4b8c2aSAndrew Thompson #define USB_DT_CONFIG 0x02 54df4b8c2aSAndrew Thompson #define USB_DT_STRING 0x03 55df4b8c2aSAndrew Thompson #define USB_DT_INTERFACE 0x04 56df4b8c2aSAndrew Thompson #define USB_DT_ENDPOINT 0x05 57df4b8c2aSAndrew Thompson 58df4b8c2aSAndrew Thompson #define USB_DT_HID 0x21 59df4b8c2aSAndrew Thompson #define USB_DT_REPORT 0x22 60df4b8c2aSAndrew Thompson #define USB_DT_PHYSICAL 0x23 61df4b8c2aSAndrew Thompson #define USB_DT_HUB 0x29 62df4b8c2aSAndrew Thompson 63df4b8c2aSAndrew Thompson /* USB descriptor type sizes */ 64df4b8c2aSAndrew Thompson 65df4b8c2aSAndrew Thompson #define USB_DT_DEVICE_SIZE 18 66df4b8c2aSAndrew Thompson #define USB_DT_CONFIG_SIZE 9 67df4b8c2aSAndrew Thompson #define USB_DT_INTERFACE_SIZE 9 68df4b8c2aSAndrew Thompson #define USB_DT_ENDPOINT_SIZE 7 69df4b8c2aSAndrew Thompson #define USB_DT_ENDPOINT_AUDIO_SIZE 9 70df4b8c2aSAndrew Thompson #define USB_DT_HUB_NONVAR_SIZE 7 71df4b8c2aSAndrew Thompson 72df4b8c2aSAndrew Thompson /* USB descriptor header */ 73df4b8c2aSAndrew Thompson struct usb_descriptor_header { 74df4b8c2aSAndrew Thompson uint8_t bLength; 75df4b8c2aSAndrew Thompson uint8_t bDescriptorType; 76df4b8c2aSAndrew Thompson }; 77df4b8c2aSAndrew Thompson 78df4b8c2aSAndrew Thompson /* USB string descriptor */ 79df4b8c2aSAndrew Thompson struct usb_string_descriptor { 80df4b8c2aSAndrew Thompson uint8_t bLength; 81df4b8c2aSAndrew Thompson uint8_t bDescriptorType; 82df4b8c2aSAndrew Thompson uint16_t wData[1]; 83df4b8c2aSAndrew Thompson }; 84df4b8c2aSAndrew Thompson 85df4b8c2aSAndrew Thompson /* USB HID descriptor */ 86df4b8c2aSAndrew Thompson struct usb_hid_descriptor { 87df4b8c2aSAndrew Thompson uint8_t bLength; 88df4b8c2aSAndrew Thompson uint8_t bDescriptorType; 89df4b8c2aSAndrew Thompson uint16_t bcdHID; 90df4b8c2aSAndrew Thompson uint8_t bCountryCode; 91df4b8c2aSAndrew Thompson uint8_t bNumDescriptors; 92df4b8c2aSAndrew Thompson /* uint8_t bReportDescriptorType; */ 93df4b8c2aSAndrew Thompson /* uint16_t wDescriptorLength; */ 94df4b8c2aSAndrew Thompson /* ... */ 95df4b8c2aSAndrew Thompson }; 96df4b8c2aSAndrew Thompson 97df4b8c2aSAndrew Thompson /* USB endpoint descriptor */ 98df4b8c2aSAndrew Thompson #define USB_MAXENDPOINTS 32 99df4b8c2aSAndrew Thompson struct usb_endpoint_descriptor { 100df4b8c2aSAndrew Thompson uint8_t bLength; 101df4b8c2aSAndrew Thompson uint8_t bDescriptorType; 102df4b8c2aSAndrew Thompson uint8_t bEndpointAddress; 103df4b8c2aSAndrew Thompson #define USB_ENDPOINT_ADDRESS_MASK 0x0f 104df4b8c2aSAndrew Thompson #define USB_ENDPOINT_DIR_MASK 0x80 105df4b8c2aSAndrew Thompson uint8_t bmAttributes; 106df4b8c2aSAndrew Thompson #define USB_ENDPOINT_TYPE_MASK 0x03 107df4b8c2aSAndrew Thompson #define USB_ENDPOINT_TYPE_CONTROL 0 108df4b8c2aSAndrew Thompson #define USB_ENDPOINT_TYPE_ISOCHRONOUS 1 109df4b8c2aSAndrew Thompson #define USB_ENDPOINT_TYPE_BULK 2 110df4b8c2aSAndrew Thompson #define USB_ENDPOINT_TYPE_INTERRUPT 3 111df4b8c2aSAndrew Thompson uint16_t wMaxPacketSize; 112df4b8c2aSAndrew Thompson uint8_t bInterval; 113df4b8c2aSAndrew Thompson uint8_t bRefresh; 114df4b8c2aSAndrew Thompson uint8_t bSynchAddress; 115df4b8c2aSAndrew Thompson 116df4b8c2aSAndrew Thompson uint8_t *extra; /* Extra descriptors */ 117df4b8c2aSAndrew Thompson int extralen; 118df4b8c2aSAndrew Thompson }; 119df4b8c2aSAndrew Thompson 120df4b8c2aSAndrew Thompson /* USB interface descriptor */ 121df4b8c2aSAndrew Thompson #define USB_MAXINTERFACES 32 122df4b8c2aSAndrew Thompson struct usb_interface_descriptor { 123df4b8c2aSAndrew Thompson uint8_t bLength; 124df4b8c2aSAndrew Thompson uint8_t bDescriptorType; 125df4b8c2aSAndrew Thompson uint8_t bInterfaceNumber; 126df4b8c2aSAndrew Thompson uint8_t bAlternateSetting; 127df4b8c2aSAndrew Thompson uint8_t bNumEndpoints; 128df4b8c2aSAndrew Thompson uint8_t bInterfaceClass; 129df4b8c2aSAndrew Thompson uint8_t bInterfaceSubClass; 130df4b8c2aSAndrew Thompson uint8_t bInterfaceProtocol; 131df4b8c2aSAndrew Thompson uint8_t iInterface; 132df4b8c2aSAndrew Thompson 133df4b8c2aSAndrew Thompson struct usb_endpoint_descriptor *endpoint; 134df4b8c2aSAndrew Thompson 135df4b8c2aSAndrew Thompson uint8_t *extra; /* Extra descriptors */ 136df4b8c2aSAndrew Thompson int extralen; 137df4b8c2aSAndrew Thompson }; 138df4b8c2aSAndrew Thompson 139df4b8c2aSAndrew Thompson #define USB_MAXALTSETTING 128 /* Hard limit */ 140df4b8c2aSAndrew Thompson struct usb_interface { 141df4b8c2aSAndrew Thompson struct usb_interface_descriptor *altsetting; 142df4b8c2aSAndrew Thompson 143df4b8c2aSAndrew Thompson int num_altsetting; 144df4b8c2aSAndrew Thompson }; 145df4b8c2aSAndrew Thompson 146df4b8c2aSAndrew Thompson /* USB configuration descriptor */ 147df4b8c2aSAndrew Thompson #define USB_MAXCONFIG 8 148df4b8c2aSAndrew Thompson struct usb_config_descriptor { 149df4b8c2aSAndrew Thompson uint8_t bLength; 150df4b8c2aSAndrew Thompson uint8_t bDescriptorType; 151df4b8c2aSAndrew Thompson uint16_t wTotalLength; 152df4b8c2aSAndrew Thompson uint8_t bNumInterfaces; 153df4b8c2aSAndrew Thompson uint8_t bConfigurationValue; 154df4b8c2aSAndrew Thompson uint8_t iConfiguration; 155df4b8c2aSAndrew Thompson uint8_t bmAttributes; 156df4b8c2aSAndrew Thompson uint8_t MaxPower; 157df4b8c2aSAndrew Thompson 158df4b8c2aSAndrew Thompson struct usb_interface *interface; 159df4b8c2aSAndrew Thompson 160df4b8c2aSAndrew Thompson uint8_t *extra; /* Extra descriptors */ 161df4b8c2aSAndrew Thompson int extralen; 162df4b8c2aSAndrew Thompson }; 163df4b8c2aSAndrew Thompson 164df4b8c2aSAndrew Thompson /* USB device descriptor */ 165df4b8c2aSAndrew Thompson struct usb_device_descriptor { 166df4b8c2aSAndrew Thompson uint8_t bLength; 167df4b8c2aSAndrew Thompson uint8_t bDescriptorType; 168df4b8c2aSAndrew Thompson uint16_t bcdUSB; 169df4b8c2aSAndrew Thompson uint8_t bDeviceClass; 170df4b8c2aSAndrew Thompson uint8_t bDeviceSubClass; 171df4b8c2aSAndrew Thompson uint8_t bDeviceProtocol; 172df4b8c2aSAndrew Thompson uint8_t bMaxPacketSize0; 173df4b8c2aSAndrew Thompson uint16_t idVendor; 174df4b8c2aSAndrew Thompson uint16_t idProduct; 175df4b8c2aSAndrew Thompson uint16_t bcdDevice; 176df4b8c2aSAndrew Thompson uint8_t iManufacturer; 177df4b8c2aSAndrew Thompson uint8_t iProduct; 178df4b8c2aSAndrew Thompson uint8_t iSerialNumber; 179df4b8c2aSAndrew Thompson uint8_t bNumConfigurations; 180df4b8c2aSAndrew Thompson }; 181df4b8c2aSAndrew Thompson 182df4b8c2aSAndrew Thompson /* USB setup packet */ 183df4b8c2aSAndrew Thompson struct usb_ctrl_setup { 184df4b8c2aSAndrew Thompson uint8_t bRequestType; 185df4b8c2aSAndrew Thompson #define USB_RECIP_DEVICE 0x00 186df4b8c2aSAndrew Thompson #define USB_RECIP_INTERFACE 0x01 187df4b8c2aSAndrew Thompson #define USB_RECIP_ENDPOINT 0x02 188df4b8c2aSAndrew Thompson #define USB_RECIP_OTHER 0x03 189df4b8c2aSAndrew Thompson #define USB_TYPE_STANDARD (0x00 << 5) 190df4b8c2aSAndrew Thompson #define USB_TYPE_CLASS (0x01 << 5) 191df4b8c2aSAndrew Thompson #define USB_TYPE_VENDOR (0x02 << 5) 192df4b8c2aSAndrew Thompson #define USB_TYPE_RESERVED (0x03 << 5) 193df4b8c2aSAndrew Thompson #define USB_ENDPOINT_IN 0x80 194df4b8c2aSAndrew Thompson #define USB_ENDPOINT_OUT 0x00 195df4b8c2aSAndrew Thompson uint8_t bRequest; 196df4b8c2aSAndrew Thompson #define USB_REQ_GET_STATUS 0x00 197df4b8c2aSAndrew Thompson #define USB_REQ_CLEAR_FEATURE 0x01 198df4b8c2aSAndrew Thompson #define USB_REQ_SET_FEATURE 0x03 199df4b8c2aSAndrew Thompson #define USB_REQ_SET_ADDRESS 0x05 200df4b8c2aSAndrew Thompson #define USB_REQ_GET_DESCRIPTOR 0x06 201df4b8c2aSAndrew Thompson #define USB_REQ_SET_DESCRIPTOR 0x07 202df4b8c2aSAndrew Thompson #define USB_REQ_GET_CONFIGURATION 0x08 203df4b8c2aSAndrew Thompson #define USB_REQ_SET_CONFIGURATION 0x09 204df4b8c2aSAndrew Thompson #define USB_REQ_GET_INTERFACE 0x0A 205df4b8c2aSAndrew Thompson #define USB_REQ_SET_INTERFACE 0x0B 206df4b8c2aSAndrew Thompson #define USB_REQ_SYNCH_FRAME 0x0C 207df4b8c2aSAndrew Thompson uint16_t wValue; 208df4b8c2aSAndrew Thompson uint16_t wIndex; 209df4b8c2aSAndrew Thompson uint16_t wLength; 210df4b8c2aSAndrew Thompson }; 211df4b8c2aSAndrew Thompson 212df4b8c2aSAndrew Thompson /* Error codes */ 213df4b8c2aSAndrew Thompson #define USB_ERROR_BEGIN 500000 214df4b8c2aSAndrew Thompson 215df4b8c2aSAndrew Thompson /* Byte swapping */ 216df4b8c2aSAndrew Thompson #define USB_LE16_TO_CPU(x) le16toh(x) 217df4b8c2aSAndrew Thompson 218df4b8c2aSAndrew Thompson /* Data types */ 219df4b8c2aSAndrew Thompson struct usb_device; 220df4b8c2aSAndrew Thompson struct usb_bus; 221df4b8c2aSAndrew Thompson 222df4b8c2aSAndrew Thompson /* 223df4b8c2aSAndrew Thompson * To maintain compatibility with applications already built with libusb, 224df4b8c2aSAndrew Thompson * we must only add entries to the end of this structure. NEVER delete or 225df4b8c2aSAndrew Thompson * move members and only change types if you really know what you're doing. 226df4b8c2aSAndrew Thompson */ 227df4b8c2aSAndrew Thompson struct usb_device { 228df4b8c2aSAndrew Thompson struct usb_device *next; 229df4b8c2aSAndrew Thompson struct usb_device *prev; 230df4b8c2aSAndrew Thompson 231df4b8c2aSAndrew Thompson char filename[PATH_MAX + 1]; 232df4b8c2aSAndrew Thompson 233df4b8c2aSAndrew Thompson struct usb_bus *bus; 234df4b8c2aSAndrew Thompson 235df4b8c2aSAndrew Thompson struct usb_device_descriptor descriptor; 236df4b8c2aSAndrew Thompson struct usb_config_descriptor *config; 237df4b8c2aSAndrew Thompson 238df4b8c2aSAndrew Thompson void *dev; 239df4b8c2aSAndrew Thompson 240df4b8c2aSAndrew Thompson uint8_t devnum; 241df4b8c2aSAndrew Thompson 242df4b8c2aSAndrew Thompson uint8_t num_children; 243df4b8c2aSAndrew Thompson struct usb_device **children; 244df4b8c2aSAndrew Thompson }; 245df4b8c2aSAndrew Thompson 246df4b8c2aSAndrew Thompson struct usb_bus { 247df4b8c2aSAndrew Thompson struct usb_bus *next; 248df4b8c2aSAndrew Thompson struct usb_bus *prev; 249df4b8c2aSAndrew Thompson 250df4b8c2aSAndrew Thompson char dirname[PATH_MAX + 1]; 251df4b8c2aSAndrew Thompson 252df4b8c2aSAndrew Thompson struct usb_device *devices; 253df4b8c2aSAndrew Thompson uint32_t location; 254df4b8c2aSAndrew Thompson 255df4b8c2aSAndrew Thompson struct usb_device *root_dev; 256df4b8c2aSAndrew Thompson }; 257df4b8c2aSAndrew Thompson 258df4b8c2aSAndrew Thompson struct usb_dev_handle; 259df4b8c2aSAndrew Thompson typedef struct usb_dev_handle usb_dev_handle; 260df4b8c2aSAndrew Thompson 261df4b8c2aSAndrew Thompson /* Variables */ 262df4b8c2aSAndrew Thompson extern struct usb_bus *usb_busses; 263df4b8c2aSAndrew Thompson 264df4b8c2aSAndrew Thompson #ifdef __cplusplus 265df4b8c2aSAndrew Thompson extern "C" { 266df4b8c2aSAndrew Thompson #endif 267df4b8c2aSAndrew Thompson #if 0 268df4b8c2aSAndrew Thompson } /* style */ 269df4b8c2aSAndrew Thompson 270df4b8c2aSAndrew Thompson #endif 271df4b8c2aSAndrew Thompson 272df4b8c2aSAndrew Thompson /* Function prototypes from "libusb20_compat01.c" */ 273df4b8c2aSAndrew Thompson 274df4b8c2aSAndrew Thompson usb_dev_handle *usb_open(struct usb_device *dev); 275df4b8c2aSAndrew Thompson int usb_close(usb_dev_handle * dev); 276df4b8c2aSAndrew Thompson int usb_get_string(usb_dev_handle * dev, int index, int langid, char *buf, size_t buflen); 277df4b8c2aSAndrew Thompson int usb_get_string_simple(usb_dev_handle * dev, int index, char *buf, size_t buflen); 278df4b8c2aSAndrew Thompson int usb_get_descriptor_by_endpoint(usb_dev_handle * udev, int ep, uint8_t type, uint8_t index, void *buf, int size); 279df4b8c2aSAndrew Thompson int usb_get_descriptor(usb_dev_handle * udev, uint8_t type, uint8_t index, void *buf, int size); 280df4b8c2aSAndrew Thompson int usb_parse_descriptor(uint8_t *source, char *description, void *dest); 281df4b8c2aSAndrew Thompson int usb_parse_configuration(struct usb_config_descriptor *config, uint8_t *buffer); 282df4b8c2aSAndrew Thompson void usb_destroy_configuration(struct usb_device *dev); 283df4b8c2aSAndrew Thompson void usb_fetch_and_parse_descriptors(usb_dev_handle * udev); 284df4b8c2aSAndrew Thompson int usb_bulk_write(usb_dev_handle * dev, int ep, char *bytes, int size, int timeout); 285df4b8c2aSAndrew Thompson int usb_bulk_read(usb_dev_handle * dev, int ep, char *bytes, int size, int timeout); 286df4b8c2aSAndrew Thompson int usb_interrupt_write(usb_dev_handle * dev, int ep, char *bytes, int size, int timeout); 287df4b8c2aSAndrew Thompson int usb_interrupt_read(usb_dev_handle * dev, int ep, char *bytes, int size, int timeout); 288df4b8c2aSAndrew Thompson int usb_control_msg(usb_dev_handle * dev, int requesttype, int request, int value, int index, char *bytes, int size, int timeout); 289df4b8c2aSAndrew Thompson int usb_set_configuration(usb_dev_handle * dev, int configuration); 290df4b8c2aSAndrew Thompson int usb_claim_interface(usb_dev_handle * dev, int interface); 291df4b8c2aSAndrew Thompson int usb_release_interface(usb_dev_handle * dev, int interface); 292df4b8c2aSAndrew Thompson int usb_set_altinterface(usb_dev_handle * dev, int alternate); 293df4b8c2aSAndrew Thompson int usb_resetep(usb_dev_handle * dev, unsigned int ep); 294df4b8c2aSAndrew Thompson int usb_clear_halt(usb_dev_handle * dev, unsigned int ep); 295df4b8c2aSAndrew Thompson int usb_reset(usb_dev_handle * dev); 296f1b5fa6eSHans Petter Selasky int usb_check_connected(usb_dev_handle * dev); 297df4b8c2aSAndrew Thompson const char *usb_strerror(void); 298df4b8c2aSAndrew Thompson void usb_init(void); 299df4b8c2aSAndrew Thompson void usb_set_debug(int level); 300df4b8c2aSAndrew Thompson int usb_find_busses(void); 301df4b8c2aSAndrew Thompson int usb_find_devices(void); 302df4b8c2aSAndrew Thompson struct usb_device *usb_device(usb_dev_handle * dev); 303df4b8c2aSAndrew Thompson struct usb_bus *usb_get_busses(void); 3044eb5923dSHans Petter Selasky int usb_get_driver_np(usb_dev_handle * dev, int interface, char *name, int namelen); 3054eb5923dSHans Petter Selasky int usb_detach_kernel_driver_np(usb_dev_handle * dev, int interface); 306df4b8c2aSAndrew Thompson 307df4b8c2aSAndrew Thompson #if 0 308df4b8c2aSAndrew Thompson { /* style */ 309df4b8c2aSAndrew Thompson #endif 310df4b8c2aSAndrew Thompson #ifdef __cplusplus 311df4b8c2aSAndrew Thompson } 312df4b8c2aSAndrew Thompson 313df4b8c2aSAndrew Thompson #endif 314df4b8c2aSAndrew Thompson 315df4b8c2aSAndrew Thompson #endif /* _LIBUSB20_COMPAT01_H_ */ 316