usb.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. #ifndef __USB_H__
  2. #define __USB_H__
  3. #include <stdlib.h>
  4. #include <windows.h>
  5. /*
  6. * 'interface' is defined somewhere in the Windows header files. This macro
  7. * is deleted here to avoid conflicts and compile errors.
  8. */
  9. #ifdef interface
  10. #undef interface
  11. #endif
  12. /*
  13. * PATH_MAX from limits.h can't be used on Windows if the dll and
  14. * import libraries are build/used by different compilers
  15. */
  16. #define LIBUSB_PATH_MAX 512
  17. /*
  18. * USB spec information
  19. *
  20. * This is all stuff grabbed from various USB specs and is pretty much
  21. * not subject to change
  22. */
  23. /*
  24. * Device and/or Interface Class codes
  25. */
  26. #define USB_CLASS_PER_INTERFACE 0 /* for DeviceClass */
  27. #define USB_CLASS_AUDIO 1
  28. #define USB_CLASS_COMM 2
  29. #define USB_CLASS_HID 3
  30. #define USB_CLASS_PRINTER 7
  31. #define USB_CLASS_MASS_STORAGE 8
  32. #define USB_CLASS_HUB 9
  33. #define USB_CLASS_DATA 10
  34. #define USB_CLASS_VENDOR_SPEC 0xff
  35. /*
  36. * Descriptor types
  37. */
  38. #define USB_DT_DEVICE 0x01
  39. #define USB_DT_CONFIG 0x02
  40. #define USB_DT_STRING 0x03
  41. #define USB_DT_INTERFACE 0x04
  42. #define USB_DT_ENDPOINT 0x05
  43. #define USB_DT_HID 0x21
  44. #define USB_DT_REPORT 0x22
  45. #define USB_DT_PHYSICAL 0x23
  46. #define USB_DT_HUB 0x29
  47. /*
  48. * Descriptor sizes per descriptor type
  49. */
  50. #define USB_DT_DEVICE_SIZE 18
  51. #define USB_DT_CONFIG_SIZE 9
  52. #define USB_DT_INTERFACE_SIZE 9
  53. #define USB_DT_ENDPOINT_SIZE 7
  54. #define USB_DT_ENDPOINT_AUDIO_SIZE 9 /* Audio extension */
  55. #define USB_DT_HUB_NONVAR_SIZE 7
  56. /* ensure byte-packed structures */
  57. #include <pshpack1.h>
  58. /* All standard descriptors have these 2 fields in common */
  59. struct usb_descriptor_header {
  60. unsigned char bLength;
  61. unsigned char bDescriptorType;
  62. };
  63. /* String descriptor */
  64. struct usb_string_descriptor {
  65. unsigned char bLength;
  66. unsigned char bDescriptorType;
  67. unsigned short wData[1];
  68. };
  69. /* HID descriptor */
  70. struct usb_hid_descriptor {
  71. unsigned char bLength;
  72. unsigned char bDescriptorType;
  73. unsigned short bcdHID;
  74. unsigned char bCountryCode;
  75. unsigned char bNumDescriptors;
  76. };
  77. /* Endpoint descriptor */
  78. #define USB_MAXENDPOINTS 32
  79. struct usb_endpoint_descriptor {
  80. unsigned char bLength;
  81. unsigned char bDescriptorType;
  82. unsigned char bEndpointAddress;
  83. unsigned char bmAttributes;
  84. unsigned short wMaxPacketSize;
  85. unsigned char bInterval;
  86. unsigned char bRefresh;
  87. unsigned char bSynchAddress;
  88. unsigned char *extra; /* Extra descriptors */
  89. int extralen;
  90. };
  91. #define USB_ENDPOINT_ADDRESS_MASK 0x0f /* in bEndpointAddress */
  92. #define USB_ENDPOINT_DIR_MASK 0x80
  93. #define USB_ENDPOINT_TYPE_MASK 0x03 /* in bmAttributes */
  94. #define USB_ENDPOINT_TYPE_CONTROL 0
  95. #define USB_ENDPOINT_TYPE_ISOCHRONOUS 1
  96. #define USB_ENDPOINT_TYPE_BULK 2
  97. #define USB_ENDPOINT_TYPE_INTERRUPT 3
  98. /* Interface descriptor */
  99. #define USB_MAXINTERFACES 32
  100. struct usb_interface_descriptor {
  101. unsigned char bLength;
  102. unsigned char bDescriptorType;
  103. unsigned char bInterfaceNumber;
  104. unsigned char bAlternateSetting;
  105. unsigned char bNumEndpoints;
  106. unsigned char bInterfaceClass;
  107. unsigned char bInterfaceSubClass;
  108. unsigned char bInterfaceProtocol;
  109. unsigned char iInterface;
  110. struct usb_endpoint_descriptor *endpoint;
  111. unsigned char *extra; /* Extra descriptors */
  112. int extralen;
  113. };
  114. #define USB_MAXALTSETTING 128 /* Hard limit */
  115. struct usb_interface {
  116. struct usb_interface_descriptor *altsetting;
  117. int num_altsetting;
  118. };
  119. /* Configuration descriptor information.. */
  120. #define USB_MAXCONFIG 8
  121. struct usb_config_descriptor {
  122. unsigned char bLength;
  123. unsigned char bDescriptorType;
  124. unsigned short wTotalLength;
  125. unsigned char bNumInterfaces;
  126. unsigned char bConfigurationValue;
  127. unsigned char iConfiguration;
  128. unsigned char bmAttributes;
  129. unsigned char MaxPower;
  130. struct usb_interface *interface;
  131. unsigned char *extra; /* Extra descriptors */
  132. int extralen;
  133. };
  134. /* Device descriptor */
  135. struct usb_device_descriptor {
  136. unsigned char bLength;
  137. unsigned char bDescriptorType;
  138. unsigned short bcdUSB;
  139. unsigned char bDeviceClass;
  140. unsigned char bDeviceSubClass;
  141. unsigned char bDeviceProtocol;
  142. unsigned char bMaxPacketSize0;
  143. unsigned short idVendor;
  144. unsigned short idProduct;
  145. unsigned short bcdDevice;
  146. unsigned char iManufacturer;
  147. unsigned char iProduct;
  148. unsigned char iSerialNumber;
  149. unsigned char bNumConfigurations;
  150. };
  151. struct usb_ctrl_setup {
  152. unsigned char bRequestType;
  153. unsigned char bRequest;
  154. unsigned short wValue;
  155. unsigned short wIndex;
  156. unsigned short wLength;
  157. };
  158. /*
  159. * Standard requests
  160. */
  161. #define USB_REQ_GET_STATUS 0x00
  162. #define USB_REQ_CLEAR_FEATURE 0x01
  163. /* 0x02 is reserved */
  164. #define USB_REQ_SET_FEATURE 0x03
  165. /* 0x04 is reserved */
  166. #define USB_REQ_SET_ADDRESS 0x05
  167. #define USB_REQ_GET_DESCRIPTOR 0x06
  168. #define USB_REQ_SET_DESCRIPTOR 0x07
  169. #define USB_REQ_GET_CONFIGURATION 0x08
  170. #define USB_REQ_SET_CONFIGURATION 0x09
  171. #define USB_REQ_GET_INTERFACE 0x0A
  172. #define USB_REQ_SET_INTERFACE 0x0B
  173. #define USB_REQ_SYNCH_FRAME 0x0C
  174. #define USB_TYPE_STANDARD (0x00 << 5)
  175. #define USB_TYPE_CLASS (0x01 << 5)
  176. #define USB_TYPE_VENDOR (0x02 << 5)
  177. #define USB_TYPE_RESERVED (0x03 << 5)
  178. #define USB_RECIP_DEVICE 0x00
  179. #define USB_RECIP_INTERFACE 0x01
  180. #define USB_RECIP_ENDPOINT 0x02
  181. #define USB_RECIP_OTHER 0x03
  182. /*
  183. * Various libusb API related stuff
  184. */
  185. #define USB_ENDPOINT_IN 0x80
  186. #define USB_ENDPOINT_OUT 0x00
  187. /* Error codes */
  188. #define USB_ERROR_BEGIN 500000
  189. /*
  190. * This is supposed to look weird. This file is generated from autoconf
  191. * and I didn't want to make this too complicated.
  192. */
  193. #define USB_LE16_TO_CPU(x)
  194. /* Data types */
  195. /* struct usb_device; */
  196. /* struct usb_bus; */
  197. struct usb_device {
  198. struct usb_device *next, *prev;
  199. char filename[LIBUSB_PATH_MAX];
  200. struct usb_bus *bus;
  201. struct usb_device_descriptor descriptor;
  202. struct usb_config_descriptor *config;
  203. void *dev; /* Darwin support */
  204. unsigned char devnum;
  205. unsigned char num_children;
  206. struct usb_device **children;
  207. };
  208. struct usb_bus {
  209. struct usb_bus *next, *prev;
  210. char dirname[LIBUSB_PATH_MAX];
  211. struct usb_device *devices;
  212. unsigned long location;
  213. struct usb_device *root_dev;
  214. };
  215. /* Version information, Windows specific */
  216. struct usb_version {
  217. struct {
  218. int major;
  219. int minor;
  220. int micro;
  221. int nano;
  222. } dll;
  223. struct {
  224. int major;
  225. int minor;
  226. int micro;
  227. int nano;
  228. } driver;
  229. };
  230. struct usb_dev_handle;
  231. typedef struct usb_dev_handle usb_dev_handle;
  232. /* Variables */
  233. #ifndef __USB_C__
  234. #define usb_busses usb_get_busses()
  235. #endif
  236. #include <poppack.h>
  237. #ifdef __cplusplus
  238. extern "C" {
  239. #endif
  240. /* Function prototypes */
  241. /* usb.c */
  242. usb_dev_handle *usb_open(struct usb_device *dev);
  243. int usb_close(usb_dev_handle *dev);
  244. int usb_get_string(usb_dev_handle *dev, int index, int langid, char *buf,
  245. size_t buflen);
  246. int usb_get_string_simple(usb_dev_handle *dev, int index, char *buf,
  247. size_t buflen);
  248. /* descriptors.c */
  249. int usb_get_descriptor_by_endpoint(usb_dev_handle *udev, int ep,
  250. unsigned char type, unsigned char index,
  251. void *buf, int size);
  252. int usb_get_descriptor(usb_dev_handle *udev, unsigned char type,
  253. unsigned char index, void *buf, int size);
  254. /* <arch>.c */
  255. int usb_bulk_write(usb_dev_handle *dev, int ep, char *bytes, int size,
  256. int timeout);
  257. int usb_bulk_read(usb_dev_handle *dev, int ep, char *bytes, int size,
  258. int timeout);
  259. int usb_interrupt_write(usb_dev_handle *dev, int ep, char *bytes, int size,
  260. int timeout);
  261. int usb_interrupt_read(usb_dev_handle *dev, int ep, char *bytes, int size,
  262. int timeout);
  263. int usb_control_msg(usb_dev_handle *dev, int requesttype, int request,
  264. int value, int index, char *bytes, int size,
  265. int timeout);
  266. int usb_set_configuration(usb_dev_handle *dev, int configuration);
  267. int usb_claim_interface(usb_dev_handle *dev, int interface);
  268. int usb_release_interface(usb_dev_handle *dev, int interface);
  269. int usb_set_altinterface(usb_dev_handle *dev, int alternate);
  270. int usb_resetep(usb_dev_handle *dev, unsigned int ep);
  271. int usb_clear_halt(usb_dev_handle *dev, unsigned int ep);
  272. int usb_reset(usb_dev_handle *dev);
  273. char *usb_strerror(void);
  274. void usb_init(void);
  275. void usb_set_debug(int level);
  276. int usb_find_busses(void);
  277. int usb_find_devices(void);
  278. struct usb_device *usb_device(usb_dev_handle *dev);
  279. struct usb_bus *usb_get_busses(void);
  280. /* Windows specific functions */
  281. #define LIBUSB_HAS_INSTALL_SERVICE_NP 1
  282. int usb_install_service_np(void);
  283. void CALLBACK usb_install_service_np_rundll(HWND wnd, HINSTANCE instance,
  284. LPSTR cmd_line, int cmd_show);
  285. #define LIBUSB_HAS_UNINSTALL_SERVICE_NP 1
  286. int usb_uninstall_service_np(void);
  287. void CALLBACK usb_uninstall_service_np_rundll(HWND wnd, HINSTANCE instance,
  288. LPSTR cmd_line, int cmd_show);
  289. #define LIBUSB_HAS_INSTALL_DRIVER_NP 1
  290. int usb_install_driver_np(const char *inf_file);
  291. void CALLBACK usb_install_driver_np_rundll(HWND wnd, HINSTANCE instance,
  292. LPSTR cmd_line, int cmd_show);
  293. #define LIBUSB_HAS_TOUCH_INF_FILE_NP 1
  294. int usb_touch_inf_file_np(const char *inf_file);
  295. void CALLBACK usb_touch_inf_file_np_rundll(HWND wnd, HINSTANCE instance,
  296. LPSTR cmd_line, int cmd_show);
  297. #define LIBUSB_HAS_INSTALL_NEEDS_RESTART_NP 1
  298. int usb_install_needs_restart_np(void);
  299. const struct usb_version *usb_get_version(void);
  300. int usb_isochronous_setup_async(usb_dev_handle *dev, void **context,
  301. unsigned char ep, int pktsize);
  302. int usb_bulk_setup_async(usb_dev_handle *dev, void **context,
  303. unsigned char ep);
  304. int usb_interrupt_setup_async(usb_dev_handle *dev, void **context,
  305. unsigned char ep);
  306. int usb_submit_async(void *context, char *bytes, int size);
  307. int usb_reap_async(void *context, int timeout);
  308. int usb_reap_async_nocancel(void *context, int timeout);
  309. int usb_cancel_async(void *context);
  310. int usb_free_async(void **context);
  311. #ifdef __cplusplus
  312. }
  313. #endif
  314. #endif /* __USB_H__ */