Subversion Repositories f9daq

Rev

Blame | Last modification | View Log | RSS feed

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