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 */ 66 struct usb_descriptor_header { 67 u_int8_t bLength; 68 u_int8_t bDescriptorType; 69 }; 70 71 /* String descriptor */ 72 struct usb_string_descriptor { 73 u_int8_t bLength; 74 u_int8_t bDescriptorType; 75 u_int16_t wData[1]; 76 }; 77 78 /* HID descriptor */ 79 struct 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 92 struct 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 117 struct 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 */ 135 struct usb_interface { 136 struct usb_interface_descriptor *altsetting; 137 138 int num_altsetting; 139 }; 140 141 /* Configuration descriptor information.. */ 142 #define USB_MAXCONFIG 8 143 struct 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 */ 160 struct 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 177 struct 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 #define USB_LE16_TO_CPU NXSwapLittleShortToHost 227 #if 0 228 #if 1 229 #define USB_LE16_TO_CPU(x) do { x = ((x & 0xff) << 8) | ((x & 0xff00) >> 8); } while(0) 230 #else 231 #define USB_LE16_TO_CPU(x) 232 #endif 233 #endif 234 235 /* Data types */ 236 struct usb_device; 237 struct usb_bus; 238 239 /* 240 * To maintain compatibility with applications already built with libusb, 241 * we must only add entries to the end of this structure. NEVER delete or 242 * move members and only change types if you really know what you're doing. 243 */ 244 struct usb_device { 245 struct usb_device *next, *prev; 246 247 char filename[PATH_MAX + 1]; 248 249 struct usb_bus *bus; 250 251 struct usb_device_descriptor descriptor; 252 struct usb_config_descriptor *config; 253 254 void *dev; /* Darwin support */ 255 256 u_int8_t devnum; 257 258 unsigned char num_children; 259 struct usb_device **children; 260 }; 261 262 struct usb_bus { 263 struct usb_bus *next, *prev; 264 265 char dirname[PATH_MAX + 1]; 266 267 struct usb_device *devices; 268 u_int32_t location; 269 270 struct usb_device *root_dev; 271 }; 272 273 struct usb_dev_handle; 274 typedef struct usb_dev_handle usb_dev_handle; 275 276 /* Variables */ 277 extern struct usb_bus *usb_busses; 278 279 #ifdef __cplusplus 280 extern "C" { 281 #endif 282 283 /* Function prototypes */ 284 285 /* usb.c */ 286 usb_dev_handle *usb_open(struct usb_device *dev); 287 int usb_close(usb_dev_handle *dev); 288 int usb_get_string(usb_dev_handle *dev, int index, int langid, char *buf, 289 size_t buflen); 290 int usb_get_string_simple(usb_dev_handle *dev, int index, char *buf, 291 size_t buflen); 292 293 /* descriptors.c */ 294 int usb_get_descriptor_by_endpoint(usb_dev_handle *udev, int ep, 295 unsigned char type, unsigned char index, void *buf, int size); 296 int usb_get_descriptor(usb_dev_handle *udev, unsigned char type, 297 unsigned char index, void *buf, int size); 298 299 /* <arch>.c */ 300 int usb_bulk_write(usb_dev_handle *dev, int ep, char *bytes, int size, 301 int timeout); 302 int usb_bulk_read(usb_dev_handle *dev, int ep, char *bytes, int size, 303 int timeout); 304 int usb_interrupt_write(usb_dev_handle *dev, int ep, char *bytes, int size, 305 int timeout); 306 int usb_interrupt_read(usb_dev_handle *dev, int ep, char *bytes, int size, 307 int timeout); 308 int usb_control_msg(usb_dev_handle *dev, int requesttype, int request, 309 int value, int index, char *bytes, int size, int timeout); 310 int usb_set_configuration(usb_dev_handle *dev, int configuration); 311 int usb_claim_interface(usb_dev_handle *dev, int interface); 312 int usb_release_interface(usb_dev_handle *dev, int interface); 313 int usb_set_altinterface(usb_dev_handle *dev, int alternate); 314 int usb_resetep(usb_dev_handle *dev, unsigned int ep); 315 int usb_clear_halt(usb_dev_handle *dev, unsigned int ep); 316 int usb_reset(usb_dev_handle *dev); 317 318 #if 0 319 #define LIBUSB_HAS_GET_DRIVER_NP 1 320 int usb_get_driver_np(usb_dev_handle *dev, int interface, char *name, 321 unsigned int namelen); 322 #define LIBUSB_HAS_DETACH_KERNEL_DRIVER_NP 1 323 int usb_detach_kernel_driver_np(usb_dev_handle *dev, int interface); 324 #endif 325 326 char *usb_strerror(void); 327 328 void usb_init(void); 329 void usb_set_debug(int level); 330 int usb_find_busses(void); 331 int usb_find_devices(void); 332 struct usb_device *usb_device(usb_dev_handle *dev); 333 struct usb_bus *usb_get_busses(void); 334 335 #ifdef __cplusplus 336 } 337 #endif 338 339 #endif /* __USB_H__ */ 340