1/* 2 * Prototypes, structure definitions and macros. 3 * 4 * Copyright (c) 2000-2003 Johannes Erdfelt <johannes@erdfelt.com> 5 * 6 * This library is covered by the LGPL, read LICENSE for details. 7 * 8 * This file (and only this file) may alternatively be licensed under the 9 * BSD license as well, read LICENSE for details. 10 */ 11#ifndef __USB_H__ 12#define __USB_H__ 13 14#include <unistd.h> 15#include <stdlib.h> 16#include <limits.h> 17 18#include <dirent.h> 19 20/* 21 * USB spec information 22 * 23 * This is all stuff grabbed from various USB specs and is pretty much 24 * not subject to change 25 */ 26 27/* 28 * Device and/or Interface Class codes 29 */ 30#define USB_CLASS_PER_INTERFACE 0 /* for DeviceClass */ 31#define USB_CLASS_AUDIO 1 32#define USB_CLASS_COMM 2 33#define USB_CLASS_HID 3 34#define USB_CLASS_PRINTER 7 35#define USB_CLASS_PTP 6 36#define USB_CLASS_MASS_STORAGE 8 37#define USB_CLASS_HUB 9 38#define USB_CLASS_DATA 10 39#define USB_CLASS_VENDOR_SPEC 0xff 40 41/* 42 * Descriptor types 43 */ 44#define USB_DT_DEVICE 0x01 45#define USB_DT_CONFIG 0x02 46#define USB_DT_STRING 0x03 47#define USB_DT_INTERFACE 0x04 48#define USB_DT_ENDPOINT 0x05 49 50#define USB_DT_HID 0x21 51#define USB_DT_REPORT 0x22 52#define USB_DT_PHYSICAL 0x23 53#define USB_DT_HUB 0x29 54 55/* 56 * Descriptor sizes per descriptor type 57 */ 58#define USB_DT_DEVICE_SIZE 18 59#define USB_DT_CONFIG_SIZE 9 60#define USB_DT_INTERFACE_SIZE 9 61#define USB_DT_ENDPOINT_SIZE 7 62#define USB_DT_ENDPOINT_AUDIO_SIZE 9 /* Audio extension */ 63#define USB_DT_HUB_NONVAR_SIZE 7 64 65/* All standard descriptors have these 2 fields in common */ 66struct usb_descriptor_header { 67 u_int8_t bLength; 68 u_int8_t bDescriptorType; 69}; 70 71/* String descriptor */ 72struct usb_string_descriptor { 73 u_int8_t bLength; 74 u_int8_t bDescriptorType; 75 u_int16_t wData[1]; 76}; 77 78/* HID descriptor */ 79struct usb_hid_descriptor { 80 u_int8_t bLength; 81 u_int8_t bDescriptorType; 82 u_int16_t bcdHID; 83 u_int8_t bCountryCode; 84 u_int8_t bNumDescriptors; 85 /* u_int8_t bReportDescriptorType; */ 86 /* u_int16_t wDescriptorLength; */ 87 /* ... */ 88}; 89 90/* Endpoint descriptor */ 91#define USB_MAXENDPOINTS 32 92struct usb_endpoint_descriptor { 93 u_int8_t bLength; 94 u_int8_t bDescriptorType; 95 u_int8_t bEndpointAddress; 96 u_int8_t bmAttributes; 97 u_int16_t wMaxPacketSize; 98 u_int8_t bInterval; 99 u_int8_t bRefresh; 100 u_int8_t bSynchAddress; 101 102 unsigned char *extra; /* Extra descriptors */ 103 int extralen; 104}; 105 106#define USB_ENDPOINT_ADDRESS_MASK 0x0f /* in bEndpointAddress */ 107#define USB_ENDPOINT_DIR_MASK 0x80 108 109#define USB_ENDPOINT_TYPE_MASK 0x03 /* in bmAttributes */ 110#define USB_ENDPOINT_TYPE_CONTROL 0 111#define USB_ENDPOINT_TYPE_ISOCHRONOUS 1 112#define USB_ENDPOINT_TYPE_BULK 2 113#define USB_ENDPOINT_TYPE_INTERRUPT 3 114 115/* Interface descriptor */ 116#define USB_MAXINTERFACES 32 117struct usb_interface_descriptor { 118 u_int8_t bLength; 119 u_int8_t bDescriptorType; 120 u_int8_t bInterfaceNumber; 121 u_int8_t bAlternateSetting; 122 u_int8_t bNumEndpoints; 123 u_int8_t bInterfaceClass; 124 u_int8_t bInterfaceSubClass; 125 u_int8_t bInterfaceProtocol; 126 u_int8_t iInterface; 127 128 struct usb_endpoint_descriptor *endpoint; 129 130 unsigned char *extra; /* Extra descriptors */ 131 int extralen; 132}; 133 134#define USB_MAXALTSETTING 128 /* Hard limit */ 135struct usb_interface { 136 struct usb_interface_descriptor *altsetting; 137 138 int num_altsetting; 139}; 140 141/* Configuration descriptor information.. */ 142#define USB_MAXCONFIG 8 143struct usb_config_descriptor { 144 u_int8_t bLength; 145 u_int8_t bDescriptorType; 146 u_int16_t wTotalLength; 147 u_int8_t bNumInterfaces; 148 u_int8_t bConfigurationValue; 149 u_int8_t iConfiguration; 150 u_int8_t bmAttributes; 151 u_int8_t MaxPower; 152 153 struct usb_interface *interface; 154 155 unsigned char *extra; /* Extra descriptors */ 156 int extralen; 157}; 158 159/* Device descriptor */ 160struct usb_device_descriptor { 161 u_int8_t bLength; 162 u_int8_t bDescriptorType; 163 u_int16_t bcdUSB; 164 u_int8_t bDeviceClass; 165 u_int8_t bDeviceSubClass; 166 u_int8_t bDeviceProtocol; 167 u_int8_t bMaxPacketSize0; 168 u_int16_t idVendor; 169 u_int16_t idProduct; 170 u_int16_t bcdDevice; 171 u_int8_t iManufacturer; 172 u_int8_t iProduct; 173 u_int8_t iSerialNumber; 174 u_int8_t bNumConfigurations; 175}; 176 177struct usb_ctrl_setup { 178 u_int8_t bRequestType; 179 u_int8_t bRequest; 180 u_int16_t wValue; 181 u_int16_t wIndex; 182 u_int16_t wLength; 183}; 184 185/* 186 * Standard requests 187 */ 188#define USB_REQ_GET_STATUS 0x00 189#define USB_REQ_CLEAR_FEATURE 0x01 190/* 0x02 is reserved */ 191#define USB_REQ_SET_FEATURE 0x03 192/* 0x04 is reserved */ 193#define USB_REQ_SET_ADDRESS 0x05 194#define USB_REQ_GET_DESCRIPTOR 0x06 195#define USB_REQ_SET_DESCRIPTOR 0x07 196#define USB_REQ_GET_CONFIGURATION 0x08 197#define USB_REQ_SET_CONFIGURATION 0x09 198#define USB_REQ_GET_INTERFACE 0x0A 199#define USB_REQ_SET_INTERFACE 0x0B 200#define USB_REQ_SYNCH_FRAME 0x0C 201 202#define USB_TYPE_STANDARD (0x00 << 5) 203#define USB_TYPE_CLASS (0x01 << 5) 204#define USB_TYPE_VENDOR (0x02 << 5) 205#define USB_TYPE_RESERVED (0x03 << 5) 206 207#define USB_RECIP_DEVICE 0x00 208#define USB_RECIP_INTERFACE 0x01 209#define USB_RECIP_ENDPOINT 0x02 210#define USB_RECIP_OTHER 0x03 211 212/* 213 * Various libusb API related stuff 214 */ 215 216#define USB_ENDPOINT_IN 0x80 217#define USB_ENDPOINT_OUT 0x00 218 219/* Error codes */ 220#define USB_ERROR_BEGIN 500000 221 222/* 223 * This is supposed to look weird. This file is generated from autoconf 224 * and I didn't want to make this too complicated. 225 */ 226#if 0 227#define USB_LE16_TO_CPU(x) do { x = ((x & 0xff) << 8) | ((x & 0xff00) >> 8); } while(0) 228#else 229#define USB_LE16_TO_CPU(x) 230#endif 231 232/* Data types */ 233struct usb_device; 234struct usb_bus; 235 236/* 237 * To maintain compatibility with applications already built with libusb, 238 * we must only add entries to the end of this structure. NEVER delete or 239 * move members and only change types if you really know what you're doing. 240 */ 241struct usb_device { 242 struct usb_device *next, *prev; 243 244 char filename[PATH_MAX + 1]; 245 246 struct usb_bus *bus; 247 248 struct usb_device_descriptor descriptor; 249 struct usb_config_descriptor *config; 250 251 void *dev; /* Darwin support */ 252 253 u_int8_t devnum; 254 255 unsigned char num_children; 256 struct usb_device **children; 257}; 258 259struct usb_bus { 260 struct usb_bus *next, *prev; 261 262 char dirname[PATH_MAX + 1]; 263 264 struct usb_device *devices; 265 u_int32_t location; 266 267 struct usb_device *root_dev; 268}; 269 270struct usb_dev_handle; 271typedef struct usb_dev_handle usb_dev_handle; 272 273/* Variables */ 274extern struct usb_bus *usb_busses; 275 276#ifdef __cplusplus 277extern "C" { 278#endif 279 280/* Function prototypes */ 281 282/* usb.c */ 283usb_dev_handle *usb_open(struct usb_device *dev); 284int usb_close(usb_dev_handle *dev); 285int usb_get_string(usb_dev_handle *dev, int index, int langid, char *buf, 286 size_t buflen); 287int usb_get_string_simple(usb_dev_handle *dev, int index, char *buf, 288 size_t buflen); 289 290/* descriptors.c */ 291int usb_get_descriptor_by_endpoint(usb_dev_handle *udev, int ep, 292 unsigned char type, unsigned char index, void *buf, int size); 293int usb_get_descriptor(usb_dev_handle *udev, unsigned char type, 294 unsigned char index, void *buf, int size); 295 296/* <arch>.c */ 297int usb_bulk_write(usb_dev_handle *dev, int ep, char *bytes, int size, 298 int timeout); 299int usb_bulk_write_sp(usb_dev_handle *dev, int ep, char *bytes, int size, 300 int timeout, int *actual_length, int max_rw); 301int usb_bulk_read(usb_dev_handle *dev, int ep, char *bytes, int size, 302 int timeout); 303int usb_interrupt_write(usb_dev_handle *dev, int ep, char *bytes, int size, 304 int timeout); 305int usb_interrupt_read(usb_dev_handle *dev, int ep, char *bytes, int size, 306 int timeout); 307int usb_control_msg(usb_dev_handle *dev, int requesttype, int request, 308 int value, int index, char *bytes, int size, int timeout); 309int usb_set_configuration(usb_dev_handle *dev, int configuration); 310int usb_claim_interface(usb_dev_handle *dev, int interface); 311int usb_release_interface(usb_dev_handle *dev, int interface); 312int usb_set_altinterface(usb_dev_handle *dev, int alternate); 313int usb_resetep(usb_dev_handle *dev, unsigned int ep); 314int usb_clear_halt(usb_dev_handle *dev, unsigned int ep); 315int usb_reset(usb_dev_handle *dev); 316 317#if 1 318#define LIBUSB_HAS_GET_DRIVER_NP 1 319int usb_get_driver_np(usb_dev_handle *dev, int interface, char *name, 320 unsigned int namelen); 321#define LIBUSB_HAS_DETACH_KERNEL_DRIVER_NP 1 322int usb_detach_kernel_driver_np(usb_dev_handle *dev, int interface); 323#endif 324 325char *usb_strerror(void); 326 327void usb_init(void); 328void usb_set_debug(int level); 329int usb_find_busses(void); 330int usb_find_devices(void); 331struct usb_device *usb_device(usb_dev_handle *dev); 332struct usb_bus *usb_get_busses(void); 333 334#ifdef __cplusplus 335} 336#endif 337 338#endif /* __USB_H__ */ 339 340