Main Page | Class List | File List | Class Members

usb.h

00001 /*
00002  * Prototypes, structure definitions and macros.
00003  *
00004  * Copyright (c) 2000-2003 Johannes Erdfelt <johannes@erdfelt.com>
00005  *
00006  * This library is covered by the LGPL, read LICENSE for details.
00007  *
00008  * This file (and only this file) may alternatively be licensed under the
00009  * BSD license as well, read LICENSE for details.
00010  */
00011 #ifndef __USB_H__
00012 #define __USB_H__
00013 
00014 #include <unistd.h>
00015 #include <stdlib.h>
00016 #include <limits.h>
00017 
00018 #include <dirent.h>
00019 
00020 /*
00021  * USB spec information
00022  *
00023  * This is all stuff grabbed from various USB specs and is pretty much
00024  * not subject to change
00025  */
00026 
00027 /*
00028  * Device and/or Interface Class codes
00029  */
00030 #define USB_CLASS_PER_INTERFACE         0       /* for DeviceClass */
00031 #define USB_CLASS_AUDIO                 1
00032 #define USB_CLASS_COMM                  2
00033 #define USB_CLASS_HID                   3
00034 #define USB_CLASS_PRINTER               7
00035 #define USB_CLASS_MASS_STORAGE          8
00036 #define USB_CLASS_HUB                   9
00037 #define USB_CLASS_DATA                  10
00038 #define USB_CLASS_VENDOR_SPEC           0xff
00039 
00040 /*
00041  * Descriptor types
00042  */
00043 #define USB_DT_DEVICE                   0x01
00044 #define USB_DT_CONFIG                   0x02
00045 #define USB_DT_STRING                   0x03
00046 #define USB_DT_INTERFACE                0x04
00047 #define USB_DT_ENDPOINT                 0x05
00048 
00049 #define USB_DT_HID                      0x21
00050 #define USB_DT_REPORT                   0x22
00051 #define USB_DT_PHYSICAL                 0x23
00052 #define USB_DT_HUB                      0x29
00053 
00054 /*
00055  * Descriptor sizes per descriptor type
00056  */
00057 #define USB_DT_DEVICE_SIZE              18
00058 #define USB_DT_CONFIG_SIZE              9
00059 #define USB_DT_INTERFACE_SIZE           9
00060 #define USB_DT_ENDPOINT_SIZE            7
00061 #define USB_DT_ENDPOINT_AUDIO_SIZE      9       /* Audio extension */
00062 #define USB_DT_HUB_NONVAR_SIZE          7
00063 
00064 /* All standard descriptors have these 2 fields in common */
00065 struct usb_descriptor_header {
00066         u_int8_t  bLength;
00067         u_int8_t  bDescriptorType;
00068 };
00069 
00070 /* String descriptor */
00071 struct usb_string_descriptor {
00072         u_int8_t  bLength;
00073         u_int8_t  bDescriptorType;
00074         u_int16_t wData[1];
00075 };
00076 
00077 /* HID descriptor */
00078 struct usb_hid_descriptor {
00079         u_int8_t  bLength;
00080         u_int8_t  bDescriptorType;
00081         u_int16_t bcdHID;
00082         u_int8_t  bCountryCode;
00083         u_int8_t  bNumDescriptors;
00084         /* u_int8_t  bReportDescriptorType; */
00085         /* u_int16_t wDescriptorLength; */
00086         /* ... */
00087 };
00088 
00089 /* Endpoint descriptor */
00090 #define USB_MAXENDPOINTS        32
00091 struct usb_endpoint_descriptor {
00092         u_int8_t  bLength;
00093         u_int8_t  bDescriptorType;
00094         u_int8_t  bEndpointAddress;
00095         u_int8_t  bmAttributes;
00096         u_int16_t wMaxPacketSize;
00097         u_int8_t  bInterval;
00098         u_int8_t  bRefresh;
00099         u_int8_t  bSynchAddress;
00100 
00101         unsigned char *extra;   /* Extra descriptors */
00102         int extralen;
00103 };
00104 
00105 #define USB_ENDPOINT_ADDRESS_MASK       0x0f    /* in bEndpointAddress */
00106 #define USB_ENDPOINT_DIR_MASK           0x80
00107 
00108 #define USB_ENDPOINT_TYPE_MASK          0x03    /* in bmAttributes */
00109 #define USB_ENDPOINT_TYPE_CONTROL       0
00110 #define USB_ENDPOINT_TYPE_ISOCHRONOUS   1
00111 #define USB_ENDPOINT_TYPE_BULK          2
00112 #define USB_ENDPOINT_TYPE_INTERRUPT     3
00113 
00114 /* Interface descriptor */
00115 #define USB_MAXINTERFACES       32
00116 struct usb_interface_descriptor {
00117         u_int8_t  bLength;
00118         u_int8_t  bDescriptorType;
00119         u_int8_t  bInterfaceNumber;
00120         u_int8_t  bAlternateSetting;
00121         u_int8_t  bNumEndpoints;
00122         u_int8_t  bInterfaceClass;
00123         u_int8_t  bInterfaceSubClass;
00124         u_int8_t  bInterfaceProtocol;
00125         u_int8_t  iInterface;
00126 
00127         struct usb_endpoint_descriptor *endpoint;
00128 
00129         unsigned char *extra;   /* Extra descriptors */
00130         int extralen;
00131 };
00132 
00133 #define USB_MAXALTSETTING       128     /* Hard limit */
00134 struct usb_interface {
00135         struct usb_interface_descriptor *altsetting;
00136 
00137         int num_altsetting;
00138 };
00139 
00140 /* Configuration descriptor information.. */
00141 #define USB_MAXCONFIG           8
00142 struct usb_config_descriptor {
00143         u_int8_t  bLength;
00144         u_int8_t  bDescriptorType;
00145         u_int16_t wTotalLength;
00146         u_int8_t  bNumInterfaces;
00147         u_int8_t  bConfigurationValue;
00148         u_int8_t  iConfiguration;
00149         u_int8_t  bmAttributes;
00150         u_int8_t  MaxPower;
00151 
00152         struct usb_interface *interface;
00153 
00154         unsigned char *extra;   /* Extra descriptors */
00155         int extralen;
00156 };
00157 
00158 /* Device descriptor */
00159 struct usb_device_descriptor {
00160         u_int8_t  bLength;
00161         u_int8_t  bDescriptorType;
00162         u_int16_t bcdUSB;
00163         u_int8_t  bDeviceClass;
00164         u_int8_t  bDeviceSubClass;
00165         u_int8_t  bDeviceProtocol;
00166         u_int8_t  bMaxPacketSize0;
00167         u_int16_t idVendor;
00168         u_int16_t idProduct;
00169         u_int16_t bcdDevice;
00170         u_int8_t  iManufacturer;
00171         u_int8_t  iProduct;
00172         u_int8_t  iSerialNumber;
00173         u_int8_t  bNumConfigurations;
00174 };
00175 
00176 struct usb_ctrl_setup {
00177         u_int8_t  bRequestType;
00178         u_int8_t  bRequest;
00179         u_int16_t wValue;
00180         u_int16_t wIndex;
00181         u_int16_t wLength;
00182 };
00183 
00184 /*
00185  * Standard requests
00186  */
00187 #define USB_REQ_GET_STATUS              0x00
00188 #define USB_REQ_CLEAR_FEATURE           0x01
00189 /* 0x02 is reserved */
00190 #define USB_REQ_SET_FEATURE             0x03
00191 /* 0x04 is reserved */
00192 #define USB_REQ_SET_ADDRESS             0x05
00193 #define USB_REQ_GET_DESCRIPTOR          0x06
00194 #define USB_REQ_SET_DESCRIPTOR          0x07
00195 #define USB_REQ_GET_CONFIGURATION       0x08
00196 #define USB_REQ_SET_CONFIGURATION       0x09
00197 #define USB_REQ_GET_INTERFACE           0x0A
00198 #define USB_REQ_SET_INTERFACE           0x0B
00199 #define USB_REQ_SYNCH_FRAME             0x0C
00200 
00201 #define USB_TYPE_STANDARD               (0x00 << 5)
00202 #define USB_TYPE_CLASS                  (0x01 << 5)
00203 #define USB_TYPE_VENDOR                 (0x02 << 5)
00204 #define USB_TYPE_RESERVED               (0x03 << 5)
00205 
00206 #define USB_RECIP_DEVICE                0x00
00207 #define USB_RECIP_INTERFACE             0x01
00208 #define USB_RECIP_ENDPOINT              0x02
00209 #define USB_RECIP_OTHER                 0x03
00210 
00211 /*
00212  * Various libusb API related stuff
00213  */
00214 
00215 #define USB_ENDPOINT_IN                 0x80
00216 #define USB_ENDPOINT_OUT                0x00
00217 
00218 /* Error codes */
00219 #define USB_ERROR_BEGIN                 500000
00220 
00221 /*
00222  * This is supposed to look weird. This file is generated from autoconf
00223  * and I didn't want to make this too complicated.
00224  */
00225 #if 0
00226 #define USB_LE16_TO_CPU(x) do { x = ((x & 0xff) << 8) | ((x & 0xff00) >> 8); } while(0)
00227 #else
00228 #define USB_LE16_TO_CPU(x)
00229 #endif
00230 
00231 /* Data types */
00232 struct usb_device;
00233 struct usb_bus;
00234 
00235 struct usb_device {
00236   struct usb_device *next, *prev;
00237 
00238   char filename[PATH_MAX + 1];
00239 
00240   struct usb_bus *bus;
00241 
00242   struct usb_device_descriptor descriptor;
00243   struct usb_config_descriptor *config;
00244 
00245   void *dev;            /* Darwin support */
00246 };
00247 
00248 struct usb_bus {
00249   struct usb_bus *next, *prev;
00250 
00251   char dirname[PATH_MAX + 1];
00252 
00253   struct usb_device *devices;
00254   u_int32_t location;
00255 };
00256 
00257 struct usb_dev_handle;
00258 typedef struct usb_dev_handle usb_dev_handle;
00259 
00260 /* Variables */
00261 extern struct usb_bus *usb_busses;
00262 
00263 #ifdef __cplusplus
00264 extern "C" {
00265 #endif
00266 
00267 /* Function prototypes */
00268 
00269 /* usb.c */
00270 usb_dev_handle *usb_open(struct usb_device *dev);
00271 int usb_close(usb_dev_handle *dev);
00272 int usb_get_string(usb_dev_handle *dev, int index, int langid, char *buf,
00273         size_t buflen);
00274 int usb_get_string_simple(usb_dev_handle *dev, int index, char *buf,
00275         size_t buflen);
00276 
00277 /* descriptors.c */
00278 int usb_get_descriptor_by_endpoint(usb_dev_handle *udev, int ep,
00279         unsigned char type, unsigned char index, void *buf, int size);
00280 int usb_get_descriptor(usb_dev_handle *udev, unsigned char type,
00281         unsigned char index, void *buf, int size);
00282 
00283 /* <arch>.c */
00284 int usb_bulk_write(usb_dev_handle *dev, int ep, char *bytes, int size,
00285         int timeout);
00286 int usb_bulk_read(usb_dev_handle *dev, int ep, char *bytes, int size,
00287         int timeout);
00288 int usb_interrupt_write(usb_dev_handle *dev, int ep, char *bytes, int size,
00289         int timeout);
00290 int usb_interrupt_read(usb_dev_handle *dev, int ep, char *bytes, int size,
00291         int timeout);
00292 int usb_control_msg(usb_dev_handle *dev, int requesttype, int request,
00293         int value, int index, char *bytes, int size, int timeout);
00294 int usb_set_configuration(usb_dev_handle *dev, int configuration);
00295 int usb_claim_interface(usb_dev_handle *dev, int interface);
00296 int usb_release_interface(usb_dev_handle *dev, int interface);
00297 int usb_set_altinterface(usb_dev_handle *dev, int alternate);
00298 int usb_resetep(usb_dev_handle *dev, unsigned int ep);
00299 int usb_clear_halt(usb_dev_handle *dev, unsigned int ep);
00300 int usb_reset(usb_dev_handle *dev);
00301 
00302 #if 1
00303 #define LIBUSB_HAS_GET_DRIVER_NP 1
00304 int usb_get_driver_np(usb_dev_handle *dev, int interface, char *name,
00305         unsigned int namelen);
00306 #define LIBUSB_HAS_DETACH_KERNEL_DRIVER_NP 1
00307 int usb_detach_kernel_driver_np(usb_dev_handle *dev, int interface);
00308 #endif
00309 
00310 char *usb_strerror(void);
00311 
00312 void usb_init(void);
00313 void usb_set_debug(int level);
00314 int usb_find_busses(void);
00315 int usb_find_devices(void);
00316 struct usb_device *usb_device(usb_dev_handle *dev);
00317 struct usb_bus *usb_get_busses(void);
00318 
00319 #ifdef __cplusplus
00320 }
00321 #endif
00322 
00323 #endif /* __USB_H__ */
00324 

Generated on Sun Oct 31 09:07:02 2004 for LegoRobo by doxygen 1.3.4