usb.h revision 185087
1184610Salfred/* $FreeBSD: head/lib/libusb20/libusb20_compat01.h 185087 2008-11-19 08:56:35Z alfred $ */ 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 30184610Salfred#include <sys/stdint.h> 31184610Salfred#include <sys/endian.h> 32184610Salfred#include <sys/types.h> 33184610Salfred#include <sys/param.h> 34184610Salfred 35184610Salfred/* USB interface class codes */ 36184610Salfred 37184610Salfred#define USB_CLASS_PER_INTERFACE 0 38184610Salfred#define USB_CLASS_AUDIO 1 39184610Salfred#define USB_CLASS_COMM 2 40184610Salfred#define USB_CLASS_HID 3 41184610Salfred#define USB_CLASS_PRINTER 7 42184610Salfred#define USB_CLASS_PTP 6 43184610Salfred#define USB_CLASS_MASS_STORAGE 8 44184610Salfred#define USB_CLASS_HUB 9 45184610Salfred#define USB_CLASS_DATA 10 46184610Salfred#define USB_CLASS_VENDOR_SPEC 0xff 47184610Salfred 48184610Salfred/* USB descriptor types */ 49184610Salfred 50184610Salfred#define USB_DT_DEVICE 0x01 51184610Salfred#define USB_DT_CONFIG 0x02 52184610Salfred#define USB_DT_STRING 0x03 53184610Salfred#define USB_DT_INTERFACE 0x04 54184610Salfred#define USB_DT_ENDPOINT 0x05 55184610Salfred 56184610Salfred#define USB_DT_HID 0x21 57184610Salfred#define USB_DT_REPORT 0x22 58184610Salfred#define USB_DT_PHYSICAL 0x23 59184610Salfred#define USB_DT_HUB 0x29 60184610Salfred 61184610Salfred/* USB descriptor type sizes */ 62184610Salfred 63184610Salfred#define USB_DT_DEVICE_SIZE 18 64184610Salfred#define USB_DT_CONFIG_SIZE 9 65184610Salfred#define USB_DT_INTERFACE_SIZE 9 66184610Salfred#define USB_DT_ENDPOINT_SIZE 7 67184610Salfred#define USB_DT_ENDPOINT_AUDIO_SIZE 9 68184610Salfred#define USB_DT_HUB_NONVAR_SIZE 7 69184610Salfred 70184610Salfred/* USB descriptor header */ 71184610Salfredstruct usb_descriptor_header { 72184610Salfred uint8_t bLength; 73184610Salfred uint8_t bDescriptorType; 74184610Salfred}; 75184610Salfred 76184610Salfred/* USB string descriptor */ 77184610Salfredstruct usb_string_descriptor { 78184610Salfred uint8_t bLength; 79184610Salfred uint8_t bDescriptorType; 80184610Salfred uint16_t wData[1]; 81184610Salfred}; 82184610Salfred 83184610Salfred/* USB HID descriptor */ 84184610Salfredstruct usb_hid_descriptor { 85184610Salfred uint8_t bLength; 86184610Salfred uint8_t bDescriptorType; 87184610Salfred uint16_t bcdHID; 88184610Salfred uint8_t bCountryCode; 89184610Salfred uint8_t bNumDescriptors; 90184610Salfred /* uint8_t bReportDescriptorType; */ 91184610Salfred /* uint16_t wDescriptorLength; */ 92184610Salfred /* ... */ 93184610Salfred}; 94184610Salfred 95184610Salfred/* USB endpoint descriptor */ 96184610Salfred#define USB_MAXENDPOINTS 32 97184610Salfredstruct usb_endpoint_descriptor { 98184610Salfred uint8_t bLength; 99184610Salfred uint8_t bDescriptorType; 100184610Salfred uint8_t bEndpointAddress; 101184610Salfred#define USB_ENDPOINT_ADDRESS_MASK 0x0f 102184610Salfred#define USB_ENDPOINT_DIR_MASK 0x80 103184610Salfred uint8_t bmAttributes; 104184610Salfred#define USB_ENDPOINT_TYPE_MASK 0x03 105184610Salfred#define USB_ENDPOINT_TYPE_CONTROL 0 106184610Salfred#define USB_ENDPOINT_TYPE_ISOCHRONOUS 1 107184610Salfred#define USB_ENDPOINT_TYPE_BULK 2 108184610Salfred#define USB_ENDPOINT_TYPE_INTERRUPT 3 109184610Salfred uint16_t wMaxPacketSize; 110184610Salfred uint8_t bInterval; 111184610Salfred uint8_t bRefresh; 112184610Salfred uint8_t bSynchAddress; 113184610Salfred 114184610Salfred uint8_t *extra; /* Extra descriptors */ 115184610Salfred int extralen; 116184610Salfred}; 117184610Salfred 118184610Salfred/* USB interface descriptor */ 119184610Salfred#define USB_MAXINTERFACES 32 120184610Salfredstruct usb_interface_descriptor { 121184610Salfred uint8_t bLength; 122184610Salfred uint8_t bDescriptorType; 123184610Salfred uint8_t bInterfaceNumber; 124184610Salfred uint8_t bAlternateSetting; 125184610Salfred uint8_t bNumEndpoints; 126184610Salfred uint8_t bInterfaceClass; 127184610Salfred uint8_t bInterfaceSubClass; 128184610Salfred uint8_t bInterfaceProtocol; 129184610Salfred uint8_t iInterface; 130184610Salfred 131184610Salfred struct usb_endpoint_descriptor *endpoint; 132184610Salfred 133184610Salfred uint8_t *extra; /* Extra descriptors */ 134184610Salfred int extralen; 135184610Salfred}; 136184610Salfred 137184610Salfred#define USB_MAXALTSETTING 128 /* Hard limit */ 138184610Salfredstruct usb_interface { 139184610Salfred struct usb_interface_descriptor *altsetting; 140184610Salfred 141184610Salfred int num_altsetting; 142184610Salfred}; 143184610Salfred 144184610Salfred/* USB configuration descriptor */ 145184610Salfred#define USB_MAXCONFIG 8 146184610Salfredstruct usb_config_descriptor { 147184610Salfred uint8_t bLength; 148184610Salfred uint8_t bDescriptorType; 149184610Salfred uint16_t wTotalLength; 150184610Salfred uint8_t bNumInterfaces; 151184610Salfred uint8_t bConfigurationValue; 152184610Salfred uint8_t iConfiguration; 153184610Salfred uint8_t bmAttributes; 154184610Salfred uint8_t MaxPower; 155184610Salfred 156184610Salfred struct usb_interface *interface; 157184610Salfred 158184610Salfred uint8_t *extra; /* Extra descriptors */ 159184610Salfred int extralen; 160184610Salfred}; 161184610Salfred 162184610Salfred/* USB device descriptor */ 163184610Salfredstruct usb_device_descriptor { 164184610Salfred uint8_t bLength; 165184610Salfred uint8_t bDescriptorType; 166184610Salfred uint16_t bcdUSB; 167184610Salfred uint8_t bDeviceClass; 168184610Salfred uint8_t bDeviceSubClass; 169184610Salfred uint8_t bDeviceProtocol; 170184610Salfred uint8_t bMaxPacketSize0; 171184610Salfred uint16_t idVendor; 172184610Salfred uint16_t idProduct; 173184610Salfred uint16_t bcdDevice; 174184610Salfred uint8_t iManufacturer; 175184610Salfred uint8_t iProduct; 176184610Salfred uint8_t iSerialNumber; 177184610Salfred uint8_t bNumConfigurations; 178184610Salfred}; 179184610Salfred 180184610Salfred/* USB setup packet */ 181184610Salfredstruct usb_ctrl_setup { 182184610Salfred uint8_t bRequestType; 183184610Salfred#define USB_RECIP_DEVICE 0x00 184184610Salfred#define USB_RECIP_INTERFACE 0x01 185184610Salfred#define USB_RECIP_ENDPOINT 0x02 186184610Salfred#define USB_RECIP_OTHER 0x03 187184610Salfred#define USB_TYPE_STANDARD (0x00 << 5) 188184610Salfred#define USB_TYPE_CLASS (0x01 << 5) 189184610Salfred#define USB_TYPE_VENDOR (0x02 << 5) 190184610Salfred#define USB_TYPE_RESERVED (0x03 << 5) 191184610Salfred#define USB_ENDPOINT_IN 0x80 192184610Salfred#define USB_ENDPOINT_OUT 0x00 193184610Salfred uint8_t bRequest; 194184610Salfred#define USB_REQ_GET_STATUS 0x00 195184610Salfred#define USB_REQ_CLEAR_FEATURE 0x01 196184610Salfred#define USB_REQ_SET_FEATURE 0x03 197184610Salfred#define USB_REQ_SET_ADDRESS 0x05 198184610Salfred#define USB_REQ_GET_DESCRIPTOR 0x06 199184610Salfred#define USB_REQ_SET_DESCRIPTOR 0x07 200184610Salfred#define USB_REQ_GET_CONFIGURATION 0x08 201184610Salfred#define USB_REQ_SET_CONFIGURATION 0x09 202184610Salfred#define USB_REQ_GET_INTERFACE 0x0A 203184610Salfred#define USB_REQ_SET_INTERFACE 0x0B 204184610Salfred#define USB_REQ_SYNCH_FRAME 0x0C 205184610Salfred uint16_t wValue; 206184610Salfred uint16_t wIndex; 207184610Salfred uint16_t wLength; 208184610Salfred}; 209184610Salfred 210184610Salfred/* Error codes */ 211184610Salfred#define USB_ERROR_BEGIN 500000 212184610Salfred 213184610Salfred/* Byte swapping */ 214184610Salfred#define USB_LE16_TO_CPU(x) le16toh(x) 215184610Salfred 216184610Salfred/* Data types */ 217184610Salfredstruct usb_device; 218184610Salfredstruct usb_bus; 219184610Salfred 220184610Salfred/* 221184610Salfred * To maintain compatibility with applications already built with libusb, 222184610Salfred * we must only add entries to the end of this structure. NEVER delete or 223184610Salfred * move members and only change types if you really know what you're doing. 224184610Salfred */ 225184610Salfredstruct usb_device { 226184610Salfred struct usb_device *next; 227184610Salfred struct usb_device *prev; 228184610Salfred 229184610Salfred char filename[PATH_MAX + 1]; 230184610Salfred 231184610Salfred struct usb_bus *bus; 232184610Salfred 233184610Salfred struct usb_device_descriptor descriptor; 234184610Salfred struct usb_config_descriptor *config; 235184610Salfred 236184610Salfred void *dev; 237184610Salfred 238184610Salfred uint8_t devnum; 239184610Salfred 240184610Salfred uint8_t num_children; 241184610Salfred struct usb_device **children; 242184610Salfred}; 243184610Salfred 244184610Salfredstruct usb_bus { 245184610Salfred struct usb_bus *next; 246184610Salfred struct usb_bus *prev; 247184610Salfred 248184610Salfred char dirname[PATH_MAX + 1]; 249184610Salfred 250184610Salfred struct usb_device *devices; 251184610Salfred uint32_t location; 252184610Salfred 253184610Salfred struct usb_device *root_dev; 254184610Salfred}; 255184610Salfred 256184610Salfredstruct usb_dev_handle; 257184610Salfredtypedef struct usb_dev_handle usb_dev_handle; 258184610Salfred 259184610Salfred/* Variables */ 260184610Salfredextern struct usb_bus *usb_busses; 261184610Salfred 262184610Salfred#ifdef __cplusplus 263184610Salfredextern "C" { 264184610Salfred#endif 265184610Salfred#if 0 266184610Salfred} /* style */ 267184610Salfred 268184610Salfred#endif 269184610Salfred 270184610Salfred/* Function prototypes from "libusb20_compat01.c" */ 271184610Salfred 272184610Salfredusb_dev_handle *usb_open(struct usb_device *dev); 273184610Salfredint usb_close(usb_dev_handle * dev); 274184610Salfredint usb_get_string(usb_dev_handle * dev, int index, int langid, char *buf, size_t buflen); 275184610Salfredint usb_get_string_simple(usb_dev_handle * dev, int index, char *buf, size_t buflen); 276184610Salfredint usb_get_descriptor_by_endpoint(usb_dev_handle * udev, int ep, uint8_t type, uint8_t index, void *buf, int size); 277184610Salfredint usb_get_descriptor(usb_dev_handle * udev, uint8_t type, uint8_t index, void *buf, int size); 278184610Salfredint usb_parse_descriptor(uint8_t *source, char *description, void *dest); 279184610Salfredint usb_parse_configuration(struct usb_config_descriptor *config, uint8_t *buffer); 280184610Salfredvoid usb_destroy_configuration(struct usb_device *dev); 281184610Salfredvoid usb_fetch_and_parse_descriptors(usb_dev_handle * udev); 282184610Salfredint usb_bulk_write(usb_dev_handle * dev, int ep, char *bytes, int size, int timeout); 283184610Salfredint usb_bulk_read(usb_dev_handle * dev, int ep, char *bytes, int size, int timeout); 284184610Salfredint usb_interrupt_write(usb_dev_handle * dev, int ep, char *bytes, int size, int timeout); 285184610Salfredint usb_interrupt_read(usb_dev_handle * dev, int ep, char *bytes, int size, int timeout); 286184610Salfredint usb_control_msg(usb_dev_handle * dev, int requesttype, int request, int value, int index, char *bytes, int size, int timeout); 287184610Salfredint usb_set_configuration(usb_dev_handle * dev, int configuration); 288184610Salfredint usb_claim_interface(usb_dev_handle * dev, int interface); 289184610Salfredint usb_release_interface(usb_dev_handle * dev, int interface); 290184610Salfredint usb_set_altinterface(usb_dev_handle * dev, int alternate); 291184610Salfredint usb_resetep(usb_dev_handle * dev, unsigned int ep); 292184610Salfredint usb_clear_halt(usb_dev_handle * dev, unsigned int ep); 293184610Salfredint usb_reset(usb_dev_handle * dev); 294185087Salfredconst char *usb_strerror(void); 295184610Salfredvoid usb_init(void); 296184610Salfredvoid usb_set_debug(int level); 297184610Salfredint usb_find_busses(void); 298184610Salfredint usb_find_devices(void); 299184610Salfredstruct usb_device *usb_device(usb_dev_handle * dev); 300184610Salfredstruct usb_bus *usb_get_busses(void); 301184610Salfred 302184610Salfred#if 0 303184610Salfred{ /* style */ 304184610Salfred#endif 305184610Salfred#ifdef __cplusplus 306184610Salfred} 307184610Salfred 308184610Salfred#endif 309184610Salfred 310184610Salfred#endif /* _LIBUSB20_COMPAT01_H_ */ 311