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