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