1184610Salfred/* $FreeBSD$ */ 2184610Salfred/*- 3184610Salfred * Copyright (c) 2008 Hans Petter Selasky. All rights reserved. 4184610Salfred * 5184610Salfred * Redistribution and use in source and binary forms, with or without 6184610Salfred * modification, are permitted provided that the following conditions 7184610Salfred * are met: 8184610Salfred * 1. Redistributions of source code must retain the above copyright 9184610Salfred * notice, this list of conditions and the following disclaimer. 10184610Salfred * 2. Redistributions in binary form must reproduce the above copyright 11184610Salfred * notice, this list of conditions and the following disclaimer in the 12184610Salfred * documentation and/or other materials provided with the distribution. 13184610Salfred * 14184610Salfred * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15184610Salfred * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16184610Salfred * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17184610Salfred * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18184610Salfred * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19184610Salfred * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20184610Salfred * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21184610Salfred * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22184610Salfred * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23184610Salfred * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24184610Salfred * SUCH DAMAGE. 25184610Salfred */ 26184610Salfred 27184610Salfred#ifndef _LIBUSB20_COMPAT_01_H_ 28184610Salfred#define _LIBUSB20_COMPAT_01_H_ 29184610Salfred 30248236Shselasky#ifndef LIBUSB_GLOBAL_INCLUDE_FILE 31248236Shselasky#include <stdint.h> 32184610Salfred#include <sys/param.h> 33203815Swkoszek#include <sys/endian.h> 34248236Shselasky#endif 35184610Salfred 36184610Salfred/* USB interface class codes */ 37184610Salfred 38184610Salfred#define USB_CLASS_PER_INTERFACE 0 39184610Salfred#define USB_CLASS_AUDIO 1 40184610Salfred#define USB_CLASS_COMM 2 41184610Salfred#define USB_CLASS_HID 3 42184610Salfred#define USB_CLASS_PRINTER 7 43184610Salfred#define USB_CLASS_PTP 6 44184610Salfred#define USB_CLASS_MASS_STORAGE 8 45184610Salfred#define USB_CLASS_HUB 9 46184610Salfred#define USB_CLASS_DATA 10 47184610Salfred#define USB_CLASS_VENDOR_SPEC 0xff 48184610Salfred 49184610Salfred/* USB descriptor types */ 50184610Salfred 51184610Salfred#define USB_DT_DEVICE 0x01 52184610Salfred#define USB_DT_CONFIG 0x02 53184610Salfred#define USB_DT_STRING 0x03 54184610Salfred#define USB_DT_INTERFACE 0x04 55184610Salfred#define USB_DT_ENDPOINT 0x05 56184610Salfred 57184610Salfred#define USB_DT_HID 0x21 58184610Salfred#define USB_DT_REPORT 0x22 59184610Salfred#define USB_DT_PHYSICAL 0x23 60184610Salfred#define USB_DT_HUB 0x29 61184610Salfred 62184610Salfred/* USB descriptor type sizes */ 63184610Salfred 64184610Salfred#define USB_DT_DEVICE_SIZE 18 65184610Salfred#define USB_DT_CONFIG_SIZE 9 66184610Salfred#define USB_DT_INTERFACE_SIZE 9 67184610Salfred#define USB_DT_ENDPOINT_SIZE 7 68184610Salfred#define USB_DT_ENDPOINT_AUDIO_SIZE 9 69184610Salfred#define USB_DT_HUB_NONVAR_SIZE 7 70184610Salfred 71184610Salfred/* USB descriptor header */ 72184610Salfredstruct usb_descriptor_header { 73184610Salfred uint8_t bLength; 74184610Salfred uint8_t bDescriptorType; 75184610Salfred}; 76184610Salfred 77184610Salfred/* USB string descriptor */ 78184610Salfredstruct usb_string_descriptor { 79184610Salfred uint8_t bLength; 80184610Salfred uint8_t bDescriptorType; 81184610Salfred uint16_t wData[1]; 82184610Salfred}; 83184610Salfred 84184610Salfred/* USB HID descriptor */ 85184610Salfredstruct usb_hid_descriptor { 86184610Salfred uint8_t bLength; 87184610Salfred uint8_t bDescriptorType; 88184610Salfred uint16_t bcdHID; 89184610Salfred uint8_t bCountryCode; 90184610Salfred uint8_t bNumDescriptors; 91184610Salfred /* uint8_t bReportDescriptorType; */ 92184610Salfred /* uint16_t wDescriptorLength; */ 93184610Salfred /* ... */ 94184610Salfred}; 95184610Salfred 96184610Salfred/* USB endpoint descriptor */ 97184610Salfred#define USB_MAXENDPOINTS 32 98184610Salfredstruct usb_endpoint_descriptor { 99184610Salfred uint8_t bLength; 100184610Salfred uint8_t bDescriptorType; 101184610Salfred uint8_t bEndpointAddress; 102184610Salfred#define USB_ENDPOINT_ADDRESS_MASK 0x0f 103184610Salfred#define USB_ENDPOINT_DIR_MASK 0x80 104184610Salfred uint8_t bmAttributes; 105184610Salfred#define USB_ENDPOINT_TYPE_MASK 0x03 106184610Salfred#define USB_ENDPOINT_TYPE_CONTROL 0 107184610Salfred#define USB_ENDPOINT_TYPE_ISOCHRONOUS 1 108184610Salfred#define USB_ENDPOINT_TYPE_BULK 2 109184610Salfred#define USB_ENDPOINT_TYPE_INTERRUPT 3 110184610Salfred uint16_t wMaxPacketSize; 111184610Salfred uint8_t bInterval; 112184610Salfred uint8_t bRefresh; 113184610Salfred uint8_t bSynchAddress; 114184610Salfred 115184610Salfred uint8_t *extra; /* Extra descriptors */ 116184610Salfred int extralen; 117184610Salfred}; 118184610Salfred 119184610Salfred/* USB interface descriptor */ 120184610Salfred#define USB_MAXINTERFACES 32 121184610Salfredstruct usb_interface_descriptor { 122184610Salfred uint8_t bLength; 123184610Salfred uint8_t bDescriptorType; 124184610Salfred uint8_t bInterfaceNumber; 125184610Salfred uint8_t bAlternateSetting; 126184610Salfred uint8_t bNumEndpoints; 127184610Salfred uint8_t bInterfaceClass; 128184610Salfred uint8_t bInterfaceSubClass; 129184610Salfred uint8_t bInterfaceProtocol; 130184610Salfred uint8_t iInterface; 131184610Salfred 132184610Salfred struct usb_endpoint_descriptor *endpoint; 133184610Salfred 134184610Salfred uint8_t *extra; /* Extra descriptors */ 135184610Salfred int extralen; 136184610Salfred}; 137184610Salfred 138184610Salfred#define USB_MAXALTSETTING 128 /* Hard limit */ 139184610Salfredstruct usb_interface { 140184610Salfred struct usb_interface_descriptor *altsetting; 141184610Salfred 142184610Salfred int num_altsetting; 143184610Salfred}; 144184610Salfred 145184610Salfred/* USB configuration descriptor */ 146184610Salfred#define USB_MAXCONFIG 8 147184610Salfredstruct usb_config_descriptor { 148184610Salfred uint8_t bLength; 149184610Salfred uint8_t bDescriptorType; 150184610Salfred uint16_t wTotalLength; 151184610Salfred uint8_t bNumInterfaces; 152184610Salfred uint8_t bConfigurationValue; 153184610Salfred uint8_t iConfiguration; 154184610Salfred uint8_t bmAttributes; 155184610Salfred uint8_t MaxPower; 156184610Salfred 157184610Salfred struct usb_interface *interface; 158184610Salfred 159184610Salfred uint8_t *extra; /* Extra descriptors */ 160184610Salfred int extralen; 161184610Salfred}; 162184610Salfred 163184610Salfred/* USB device descriptor */ 164184610Salfredstruct usb_device_descriptor { 165184610Salfred uint8_t bLength; 166184610Salfred uint8_t bDescriptorType; 167184610Salfred uint16_t bcdUSB; 168184610Salfred uint8_t bDeviceClass; 169184610Salfred uint8_t bDeviceSubClass; 170184610Salfred uint8_t bDeviceProtocol; 171184610Salfred uint8_t bMaxPacketSize0; 172184610Salfred uint16_t idVendor; 173184610Salfred uint16_t idProduct; 174184610Salfred uint16_t bcdDevice; 175184610Salfred uint8_t iManufacturer; 176184610Salfred uint8_t iProduct; 177184610Salfred uint8_t iSerialNumber; 178184610Salfred uint8_t bNumConfigurations; 179184610Salfred}; 180184610Salfred 181184610Salfred/* USB setup packet */ 182184610Salfredstruct usb_ctrl_setup { 183184610Salfred uint8_t bRequestType; 184184610Salfred#define USB_RECIP_DEVICE 0x00 185184610Salfred#define USB_RECIP_INTERFACE 0x01 186184610Salfred#define USB_RECIP_ENDPOINT 0x02 187184610Salfred#define USB_RECIP_OTHER 0x03 188184610Salfred#define USB_TYPE_STANDARD (0x00 << 5) 189184610Salfred#define USB_TYPE_CLASS (0x01 << 5) 190184610Salfred#define USB_TYPE_VENDOR (0x02 << 5) 191184610Salfred#define USB_TYPE_RESERVED (0x03 << 5) 192184610Salfred#define USB_ENDPOINT_IN 0x80 193184610Salfred#define USB_ENDPOINT_OUT 0x00 194184610Salfred uint8_t bRequest; 195184610Salfred#define USB_REQ_GET_STATUS 0x00 196184610Salfred#define USB_REQ_CLEAR_FEATURE 0x01 197184610Salfred#define USB_REQ_SET_FEATURE 0x03 198184610Salfred#define USB_REQ_SET_ADDRESS 0x05 199184610Salfred#define USB_REQ_GET_DESCRIPTOR 0x06 200184610Salfred#define USB_REQ_SET_DESCRIPTOR 0x07 201184610Salfred#define USB_REQ_GET_CONFIGURATION 0x08 202184610Salfred#define USB_REQ_SET_CONFIGURATION 0x09 203184610Salfred#define USB_REQ_GET_INTERFACE 0x0A 204184610Salfred#define USB_REQ_SET_INTERFACE 0x0B 205184610Salfred#define USB_REQ_SYNCH_FRAME 0x0C 206184610Salfred uint16_t wValue; 207184610Salfred uint16_t wIndex; 208184610Salfred uint16_t wLength; 209184610Salfred}; 210184610Salfred 211184610Salfred/* Error codes */ 212184610Salfred#define USB_ERROR_BEGIN 500000 213184610Salfred 214184610Salfred/* Byte swapping */ 215184610Salfred#define USB_LE16_TO_CPU(x) le16toh(x) 216184610Salfred 217184610Salfred/* Data types */ 218184610Salfredstruct usb_device; 219184610Salfredstruct usb_bus; 220184610Salfred 221184610Salfred/* 222184610Salfred * To maintain compatibility with applications already built with libusb, 223184610Salfred * we must only add entries to the end of this structure. NEVER delete or 224184610Salfred * move members and only change types if you really know what you're doing. 225184610Salfred */ 226184610Salfredstruct usb_device { 227184610Salfred struct usb_device *next; 228184610Salfred struct usb_device *prev; 229184610Salfred 230184610Salfred char filename[PATH_MAX + 1]; 231184610Salfred 232184610Salfred struct usb_bus *bus; 233184610Salfred 234184610Salfred struct usb_device_descriptor descriptor; 235184610Salfred struct usb_config_descriptor *config; 236184610Salfred 237184610Salfred void *dev; 238184610Salfred 239184610Salfred uint8_t devnum; 240184610Salfred 241184610Salfred uint8_t num_children; 242184610Salfred struct usb_device **children; 243184610Salfred}; 244184610Salfred 245184610Salfredstruct usb_bus { 246184610Salfred struct usb_bus *next; 247184610Salfred struct usb_bus *prev; 248184610Salfred 249184610Salfred char dirname[PATH_MAX + 1]; 250184610Salfred 251184610Salfred struct usb_device *devices; 252184610Salfred uint32_t location; 253184610Salfred 254184610Salfred struct usb_device *root_dev; 255184610Salfred}; 256184610Salfred 257184610Salfredstruct usb_dev_handle; 258184610Salfredtypedef struct usb_dev_handle usb_dev_handle; 259184610Salfred 260184610Salfred/* Variables */ 261184610Salfredextern struct usb_bus *usb_busses; 262184610Salfred 263184610Salfred#ifdef __cplusplus 264184610Salfredextern "C" { 265184610Salfred#endif 266184610Salfred#if 0 267184610Salfred} /* style */ 268184610Salfred 269184610Salfred#endif 270184610Salfred 271184610Salfred/* Function prototypes from "libusb20_compat01.c" */ 272184610Salfred 273184610Salfredusb_dev_handle *usb_open(struct usb_device *dev); 274184610Salfredint usb_close(usb_dev_handle * dev); 275184610Salfredint usb_get_string(usb_dev_handle * dev, int index, int langid, char *buf, size_t buflen); 276184610Salfredint usb_get_string_simple(usb_dev_handle * dev, int index, char *buf, size_t buflen); 277184610Salfredint usb_get_descriptor_by_endpoint(usb_dev_handle * udev, int ep, uint8_t type, uint8_t index, void *buf, int size); 278184610Salfredint usb_get_descriptor(usb_dev_handle * udev, uint8_t type, uint8_t index, void *buf, int size); 279184610Salfredint usb_parse_descriptor(uint8_t *source, char *description, void *dest); 280184610Salfredint usb_parse_configuration(struct usb_config_descriptor *config, uint8_t *buffer); 281184610Salfredvoid usb_destroy_configuration(struct usb_device *dev); 282184610Salfredvoid usb_fetch_and_parse_descriptors(usb_dev_handle * udev); 283184610Salfredint usb_bulk_write(usb_dev_handle * dev, int ep, char *bytes, int size, int timeout); 284184610Salfredint usb_bulk_read(usb_dev_handle * dev, int ep, char *bytes, int size, int timeout); 285184610Salfredint usb_interrupt_write(usb_dev_handle * dev, int ep, char *bytes, int size, int timeout); 286184610Salfredint usb_interrupt_read(usb_dev_handle * dev, int ep, char *bytes, int size, int timeout); 287184610Salfredint usb_control_msg(usb_dev_handle * dev, int requesttype, int request, int value, int index, char *bytes, int size, int timeout); 288184610Salfredint usb_set_configuration(usb_dev_handle * dev, int configuration); 289184610Salfredint usb_claim_interface(usb_dev_handle * dev, int interface); 290184610Salfredint usb_release_interface(usb_dev_handle * dev, int interface); 291184610Salfredint usb_set_altinterface(usb_dev_handle * dev, int alternate); 292184610Salfredint usb_resetep(usb_dev_handle * dev, unsigned int ep); 293184610Salfredint usb_clear_halt(usb_dev_handle * dev, unsigned int ep); 294184610Salfredint usb_reset(usb_dev_handle * dev); 295213848Shselaskyint usb_check_connected(usb_dev_handle * dev); 296185087Salfredconst char *usb_strerror(void); 297184610Salfredvoid usb_init(void); 298184610Salfredvoid usb_set_debug(int level); 299184610Salfredint usb_find_busses(void); 300184610Salfredint usb_find_devices(void); 301184610Salfredstruct usb_device *usb_device(usb_dev_handle * dev); 302184610Salfredstruct usb_bus *usb_get_busses(void); 303224085Shselaskyint usb_get_driver_np(usb_dev_handle * dev, int interface, char *name, int namelen); 304224085Shselaskyint usb_detach_kernel_driver_np(usb_dev_handle * dev, int interface); 305184610Salfred 306184610Salfred#if 0 307184610Salfred{ /* style */ 308184610Salfred#endif 309184610Salfred#ifdef __cplusplus 310184610Salfred} 311184610Salfred 312184610Salfred#endif 313184610Salfred 314184610Salfred#endif /* _LIBUSB20_COMPAT01_H_ */ 315