1
0

usb.h 11 KB

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