usb.h 8.6 KB

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