libusb.h 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999
  1. /*
  2. * Public libusb header file
  3. * Copyright © 2001 Johannes Erdfelt <johannes@erdfelt.com>
  4. * Copyright © 2007-2008 Daniel Drake <dsd@gentoo.org>
  5. * Copyright © 2012 Pete Batard <pete@akeo.ie>
  6. * Copyright © 2012 Nathan Hjelm <hjelmn@cs.unm.edu>
  7. * For more information, please visit: http://libusb.info
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2.1 of the License, or (at your option) any later version.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. #ifndef LIBUSB_H
  24. #define LIBUSB_H
  25. #ifdef _MSC_VER
  26. /* on MS environments, the inline keyword is available in C++ only */
  27. #if !defined(__cplusplus)
  28. #define inline __inline
  29. #endif
  30. /* ssize_t is also not available (copy/paste from MinGW) */
  31. #ifndef _SSIZE_T_DEFINED
  32. #define _SSIZE_T_DEFINED
  33. #undef ssize_t
  34. #ifdef _WIN64
  35. typedef __int64 ssize_t;
  36. #else
  37. typedef int ssize_t;
  38. #endif /* _WIN64 */
  39. #endif /* _SSIZE_T_DEFINED */
  40. #endif /* _MSC_VER */
  41. /* stdint.h is not available on older MSVC */
  42. #if defined(_MSC_VER) && (_MSC_VER < 1600) && (!defined(_STDINT)) && (!defined(_STDINT_H))
  43. typedef unsigned __int8 uint8_t;
  44. typedef unsigned __int16 uint16_t;
  45. typedef unsigned __int32 uint32_t;
  46. #else
  47. #include <stdint.h>
  48. #endif
  49. #if !defined(_WIN32_WCE)
  50. #include <sys/types.h>
  51. #endif
  52. #if defined(__linux) || defined(__APPLE__) || defined(__CYGWIN__) || defined(__HAIKU__)
  53. #include <sys/time.h>
  54. #endif
  55. #include <time.h>
  56. #include <limits.h>
  57. /* 'interface' might be defined as a macro on Windows, so we need to
  58. * undefine it so as not to break the current libusb API, because
  59. * libusb_config_descriptor has an 'interface' member
  60. * As this can be problematic if you include windows.h after libusb.h
  61. * in your sources, we force windows.h to be included first. */
  62. #if defined(_WIN32) || defined(__CYGWIN__) || defined(_WIN32_WCE)
  63. #include <windows.h>
  64. #if defined(interface)
  65. #undef interface
  66. #endif
  67. #if !defined(__CYGWIN__)
  68. #include <winsock.h>
  69. #endif
  70. #endif
  71. #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5)
  72. #define LIBUSB_DEPRECATED_FOR(f) \
  73. __attribute__((deprecated("Use " #f " instead")))
  74. #else
  75. #define LIBUSB_DEPRECATED_FOR(f)
  76. #endif /* __GNUC__ */
  77. /** \def LIBUSB_CALL
  78. * \ingroup misc
  79. * libusb's Windows calling convention.
  80. *
  81. * Under Windows, the selection of available compilers and configurations
  82. * means that, unlike other platforms, there is not <em>one true calling
  83. * convention</em> (calling convention: the manner in which parameters are
  84. * passed to functions in the generated assembly code).
  85. *
  86. * Matching the Windows API itself, libusb uses the WINAPI convention (which
  87. * translates to the <tt>stdcall</tt> convention) and guarantees that the
  88. * library is compiled in this way. The public header file also includes
  89. * appropriate annotations so that your own software will use the right
  90. * convention, even if another convention is being used by default within
  91. * your codebase.
  92. *
  93. * The one consideration that you must apply in your software is to mark
  94. * all functions which you use as libusb callbacks with this LIBUSB_CALL
  95. * annotation, so that they too get compiled for the correct calling
  96. * convention.
  97. *
  98. * On non-Windows operating systems, this macro is defined as nothing. This
  99. * means that you can apply it to your code without worrying about
  100. * cross-platform compatibility.
  101. */
  102. /* LIBUSB_CALL must be defined on both definition and declaration of libusb
  103. * functions. You'd think that declaration would be enough, but cygwin will
  104. * complain about conflicting types unless both are marked this way.
  105. * The placement of this macro is important too; it must appear after the
  106. * return type, before the function name. See internal documentation for
  107. * API_EXPORTED.
  108. */
  109. #if defined(_WIN32) || defined(__CYGWIN__) || defined(_WIN32_WCE)
  110. #define LIBUSB_CALL WINAPI
  111. #else
  112. #define LIBUSB_CALL
  113. #endif
  114. /** \def LIBUSB_API_VERSION
  115. * \ingroup misc
  116. * libusb's API version.
  117. *
  118. * Since version 1.0.13, to help with feature detection, libusb defines
  119. * a LIBUSB_API_VERSION macro that gets increased every time there is a
  120. * significant change to the API, such as the introduction of a new call,
  121. * the definition of a new macro/enum member, or any other element that
  122. * libusb applications may want to detect at compilation time.
  123. *
  124. * The macro is typically used in an application as follows:
  125. * \code
  126. * #if defined(LIBUSB_API_VERSION) && (LIBUSB_API_VERSION >= 0x01001234)
  127. * // Use one of the newer features from the libusb API
  128. * #endif
  129. * \endcode
  130. *
  131. * Internally, LIBUSB_API_VERSION is defined as follows:
  132. * (libusb major << 24) | (libusb minor << 16) | (16 bit incremental)
  133. */
  134. #define LIBUSB_API_VERSION 0x01000104
  135. /* The following is kept for compatibility, but will be deprecated in the future */
  136. #define LIBUSBX_API_VERSION LIBUSB_API_VERSION
  137. #ifdef __cplusplus
  138. extern "C" {
  139. #endif
  140. /**
  141. * \ingroup misc
  142. * Convert a 16-bit value from host-endian to little-endian format. On
  143. * little endian systems, this function does nothing. On big endian systems,
  144. * the bytes are swapped.
  145. * \param x the host-endian value to convert
  146. * \returns the value in little-endian byte order
  147. */
  148. static inline uint16_t libusb_cpu_to_le16(const uint16_t x)
  149. {
  150. union {
  151. uint8_t b8[2];
  152. uint16_t b16;
  153. } _tmp;
  154. _tmp.b8[1] = (uint8_t) (x >> 8);
  155. _tmp.b8[0] = (uint8_t) (x & 0xff);
  156. return _tmp.b16;
  157. }
  158. /** \def libusb_le16_to_cpu
  159. * \ingroup misc
  160. * Convert a 16-bit value from little-endian to host-endian format. On
  161. * little endian systems, this function does nothing. On big endian systems,
  162. * the bytes are swapped.
  163. * \param x the little-endian value to convert
  164. * \returns the value in host-endian byte order
  165. */
  166. #define libusb_le16_to_cpu libusb_cpu_to_le16
  167. /* standard USB stuff */
  168. /** \ingroup desc
  169. * Device and/or Interface Class codes */
  170. enum libusb_class_code {
  171. /** In the context of a \ref libusb_device_descriptor "device descriptor",
  172. * this bDeviceClass value indicates that each interface specifies its
  173. * own class information and all interfaces operate independently.
  174. */
  175. LIBUSB_CLASS_PER_INTERFACE = 0,
  176. /** Audio class */
  177. LIBUSB_CLASS_AUDIO = 1,
  178. /** Communications class */
  179. LIBUSB_CLASS_COMM = 2,
  180. /** Human Interface Device class */
  181. LIBUSB_CLASS_HID = 3,
  182. /** Physical */
  183. LIBUSB_CLASS_PHYSICAL = 5,
  184. /** Printer class */
  185. LIBUSB_CLASS_PRINTER = 7,
  186. /** Image class */
  187. LIBUSB_CLASS_PTP = 6, /* legacy name from libusb-0.1 usb.h */
  188. LIBUSB_CLASS_IMAGE = 6,
  189. /** Mass storage class */
  190. LIBUSB_CLASS_MASS_STORAGE = 8,
  191. /** Hub class */
  192. LIBUSB_CLASS_HUB = 9,
  193. /** Data class */
  194. LIBUSB_CLASS_DATA = 10,
  195. /** Smart Card */
  196. LIBUSB_CLASS_SMART_CARD = 0x0b,
  197. /** Content Security */
  198. LIBUSB_CLASS_CONTENT_SECURITY = 0x0d,
  199. /** Video */
  200. LIBUSB_CLASS_VIDEO = 0x0e,
  201. /** Personal Healthcare */
  202. LIBUSB_CLASS_PERSONAL_HEALTHCARE = 0x0f,
  203. /** Diagnostic Device */
  204. LIBUSB_CLASS_DIAGNOSTIC_DEVICE = 0xdc,
  205. /** Wireless class */
  206. LIBUSB_CLASS_WIRELESS = 0xe0,
  207. /** Application class */
  208. LIBUSB_CLASS_APPLICATION = 0xfe,
  209. /** Class is vendor-specific */
  210. LIBUSB_CLASS_VENDOR_SPEC = 0xff
  211. };
  212. /** \ingroup desc
  213. * Descriptor types as defined by the USB specification. */
  214. enum libusb_descriptor_type {
  215. /** Device descriptor. See libusb_device_descriptor. */
  216. LIBUSB_DT_DEVICE = 0x01,
  217. /** Configuration descriptor. See libusb_config_descriptor. */
  218. LIBUSB_DT_CONFIG = 0x02,
  219. /** String descriptor */
  220. LIBUSB_DT_STRING = 0x03,
  221. /** Interface descriptor. See libusb_interface_descriptor. */
  222. LIBUSB_DT_INTERFACE = 0x04,
  223. /** Endpoint descriptor. See libusb_endpoint_descriptor. */
  224. LIBUSB_DT_ENDPOINT = 0x05,
  225. /** BOS descriptor */
  226. LIBUSB_DT_BOS = 0x0f,
  227. /** Device Capability descriptor */
  228. LIBUSB_DT_DEVICE_CAPABILITY = 0x10,
  229. /** HID descriptor */
  230. LIBUSB_DT_HID = 0x21,
  231. /** HID report descriptor */
  232. LIBUSB_DT_REPORT = 0x22,
  233. /** Physical descriptor */
  234. LIBUSB_DT_PHYSICAL = 0x23,
  235. /** Hub descriptor */
  236. LIBUSB_DT_HUB = 0x29,
  237. /** SuperSpeed Hub descriptor */
  238. LIBUSB_DT_SUPERSPEED_HUB = 0x2a,
  239. /** SuperSpeed Endpoint Companion descriptor */
  240. LIBUSB_DT_SS_ENDPOINT_COMPANION = 0x30
  241. };
  242. /* Descriptor sizes per descriptor type */
  243. #define LIBUSB_DT_DEVICE_SIZE 18
  244. #define LIBUSB_DT_CONFIG_SIZE 9
  245. #define LIBUSB_DT_INTERFACE_SIZE 9
  246. #define LIBUSB_DT_ENDPOINT_SIZE 7
  247. #define LIBUSB_DT_ENDPOINT_AUDIO_SIZE 9 /* Audio extension */
  248. #define LIBUSB_DT_HUB_NONVAR_SIZE 7
  249. #define LIBUSB_DT_SS_ENDPOINT_COMPANION_SIZE 6
  250. #define LIBUSB_DT_BOS_SIZE 5
  251. #define LIBUSB_DT_DEVICE_CAPABILITY_SIZE 3
  252. /* BOS descriptor sizes */
  253. #define LIBUSB_BT_USB_2_0_EXTENSION_SIZE 7
  254. #define LIBUSB_BT_SS_USB_DEVICE_CAPABILITY_SIZE 10
  255. #define LIBUSB_BT_CONTAINER_ID_SIZE 20
  256. /* We unwrap the BOS => define its max size */
  257. #define LIBUSB_DT_BOS_MAX_SIZE ((LIBUSB_DT_BOS_SIZE) +\
  258. (LIBUSB_BT_USB_2_0_EXTENSION_SIZE) +\
  259. (LIBUSB_BT_SS_USB_DEVICE_CAPABILITY_SIZE) +\
  260. (LIBUSB_BT_CONTAINER_ID_SIZE))
  261. #define LIBUSB_ENDPOINT_ADDRESS_MASK 0x0f /* in bEndpointAddress */
  262. #define LIBUSB_ENDPOINT_DIR_MASK 0x80
  263. /** \ingroup desc
  264. * Endpoint direction. Values for bit 7 of the
  265. * \ref libusb_endpoint_descriptor::bEndpointAddress "endpoint address" scheme.
  266. */
  267. enum libusb_endpoint_direction {
  268. /** In: device-to-host */
  269. LIBUSB_ENDPOINT_IN = 0x80,
  270. /** Out: host-to-device */
  271. LIBUSB_ENDPOINT_OUT = 0x00
  272. };
  273. #define LIBUSB_TRANSFER_TYPE_MASK 0x03 /* in bmAttributes */
  274. /** \ingroup desc
  275. * Endpoint transfer type. Values for bits 0:1 of the
  276. * \ref libusb_endpoint_descriptor::bmAttributes "endpoint attributes" field.
  277. */
  278. enum libusb_transfer_type {
  279. /** Control endpoint */
  280. LIBUSB_TRANSFER_TYPE_CONTROL = 0,
  281. /** Isochronous endpoint */
  282. LIBUSB_TRANSFER_TYPE_ISOCHRONOUS = 1,
  283. /** Bulk endpoint */
  284. LIBUSB_TRANSFER_TYPE_BULK = 2,
  285. /** Interrupt endpoint */
  286. LIBUSB_TRANSFER_TYPE_INTERRUPT = 3,
  287. /** Stream endpoint */
  288. LIBUSB_TRANSFER_TYPE_BULK_STREAM = 4,
  289. };
  290. /** \ingroup misc
  291. * Standard requests, as defined in table 9-5 of the USB 3.0 specifications */
  292. enum libusb_standard_request {
  293. /** Request status of the specific recipient */
  294. LIBUSB_REQUEST_GET_STATUS = 0x00,
  295. /** Clear or disable a specific feature */
  296. LIBUSB_REQUEST_CLEAR_FEATURE = 0x01,
  297. /* 0x02 is reserved */
  298. /** Set or enable a specific feature */
  299. LIBUSB_REQUEST_SET_FEATURE = 0x03,
  300. /* 0x04 is reserved */
  301. /** Set device address for all future accesses */
  302. LIBUSB_REQUEST_SET_ADDRESS = 0x05,
  303. /** Get the specified descriptor */
  304. LIBUSB_REQUEST_GET_DESCRIPTOR = 0x06,
  305. /** Used to update existing descriptors or add new descriptors */
  306. LIBUSB_REQUEST_SET_DESCRIPTOR = 0x07,
  307. /** Get the current device configuration value */
  308. LIBUSB_REQUEST_GET_CONFIGURATION = 0x08,
  309. /** Set device configuration */
  310. LIBUSB_REQUEST_SET_CONFIGURATION = 0x09,
  311. /** Return the selected alternate setting for the specified interface */
  312. LIBUSB_REQUEST_GET_INTERFACE = 0x0A,
  313. /** Select an alternate interface for the specified interface */
  314. LIBUSB_REQUEST_SET_INTERFACE = 0x0B,
  315. /** Set then report an endpoint's synchronization frame */
  316. LIBUSB_REQUEST_SYNCH_FRAME = 0x0C,
  317. /** Sets both the U1 and U2 Exit Latency */
  318. LIBUSB_REQUEST_SET_SEL = 0x30,
  319. /** Delay from the time a host transmits a packet to the time it is
  320. * received by the device. */
  321. LIBUSB_SET_ISOCH_DELAY = 0x31,
  322. };
  323. /** \ingroup misc
  324. * Request type bits of the
  325. * \ref libusb_control_setup::bmRequestType "bmRequestType" field in control
  326. * transfers. */
  327. enum libusb_request_type {
  328. /** Standard */
  329. LIBUSB_REQUEST_TYPE_STANDARD = (0x00 << 5),
  330. /** Class */
  331. LIBUSB_REQUEST_TYPE_CLASS = (0x01 << 5),
  332. /** Vendor */
  333. LIBUSB_REQUEST_TYPE_VENDOR = (0x02 << 5),
  334. /** Reserved */
  335. LIBUSB_REQUEST_TYPE_RESERVED = (0x03 << 5)
  336. };
  337. /** \ingroup misc
  338. * Recipient bits of the
  339. * \ref libusb_control_setup::bmRequestType "bmRequestType" field in control
  340. * transfers. Values 4 through 31 are reserved. */
  341. enum libusb_request_recipient {
  342. /** Device */
  343. LIBUSB_RECIPIENT_DEVICE = 0x00,
  344. /** Interface */
  345. LIBUSB_RECIPIENT_INTERFACE = 0x01,
  346. /** Endpoint */
  347. LIBUSB_RECIPIENT_ENDPOINT = 0x02,
  348. /** Other */
  349. LIBUSB_RECIPIENT_OTHER = 0x03,
  350. };
  351. #define LIBUSB_ISO_SYNC_TYPE_MASK 0x0C
  352. /** \ingroup desc
  353. * Synchronization type for isochronous endpoints. Values for bits 2:3 of the
  354. * \ref libusb_endpoint_descriptor::bmAttributes "bmAttributes" field in
  355. * libusb_endpoint_descriptor.
  356. */
  357. enum libusb_iso_sync_type {
  358. /** No synchronization */
  359. LIBUSB_ISO_SYNC_TYPE_NONE = 0,
  360. /** Asynchronous */
  361. LIBUSB_ISO_SYNC_TYPE_ASYNC = 1,
  362. /** Adaptive */
  363. LIBUSB_ISO_SYNC_TYPE_ADAPTIVE = 2,
  364. /** Synchronous */
  365. LIBUSB_ISO_SYNC_TYPE_SYNC = 3
  366. };
  367. #define LIBUSB_ISO_USAGE_TYPE_MASK 0x30
  368. /** \ingroup desc
  369. * Usage type for isochronous endpoints. Values for bits 4:5 of the
  370. * \ref libusb_endpoint_descriptor::bmAttributes "bmAttributes" field in
  371. * libusb_endpoint_descriptor.
  372. */
  373. enum libusb_iso_usage_type {
  374. /** Data endpoint */
  375. LIBUSB_ISO_USAGE_TYPE_DATA = 0,
  376. /** Feedback endpoint */
  377. LIBUSB_ISO_USAGE_TYPE_FEEDBACK = 1,
  378. /** Implicit feedback Data endpoint */
  379. LIBUSB_ISO_USAGE_TYPE_IMPLICIT = 2,
  380. };
  381. /** \ingroup desc
  382. * A structure representing the standard USB device descriptor. This
  383. * descriptor is documented in section 9.6.1 of the USB 3.0 specification.
  384. * All multiple-byte fields are represented in host-endian format.
  385. */
  386. struct libusb_device_descriptor {
  387. /** Size of this descriptor (in bytes) */
  388. uint8_t bLength;
  389. /** Descriptor type. Will have value
  390. * \ref libusb_descriptor_type::LIBUSB_DT_DEVICE LIBUSB_DT_DEVICE in this
  391. * context. */
  392. uint8_t bDescriptorType;
  393. /** USB specification release number in binary-coded decimal. A value of
  394. * 0x0200 indicates USB 2.0, 0x0110 indicates USB 1.1, etc. */
  395. uint16_t bcdUSB;
  396. /** USB-IF class code for the device. See \ref libusb_class_code. */
  397. uint8_t bDeviceClass;
  398. /** USB-IF subclass code for the device, qualified by the bDeviceClass
  399. * value */
  400. uint8_t bDeviceSubClass;
  401. /** USB-IF protocol code for the device, qualified by the bDeviceClass and
  402. * bDeviceSubClass values */
  403. uint8_t bDeviceProtocol;
  404. /** Maximum packet size for endpoint 0 */
  405. uint8_t bMaxPacketSize0;
  406. /** USB-IF vendor ID */
  407. uint16_t idVendor;
  408. /** USB-IF product ID */
  409. uint16_t idProduct;
  410. /** Device release number in binary-coded decimal */
  411. uint16_t bcdDevice;
  412. /** Index of string descriptor describing manufacturer */
  413. uint8_t iManufacturer;
  414. /** Index of string descriptor describing product */
  415. uint8_t iProduct;
  416. /** Index of string descriptor containing device serial number */
  417. uint8_t iSerialNumber;
  418. /** Number of possible configurations */
  419. uint8_t bNumConfigurations;
  420. };
  421. /** \ingroup desc
  422. * A structure representing the standard USB endpoint descriptor. This
  423. * descriptor is documented in section 9.6.6 of the USB 3.0 specification.
  424. * All multiple-byte fields are represented in host-endian format.
  425. */
  426. struct libusb_endpoint_descriptor {
  427. /** Size of this descriptor (in bytes) */
  428. uint8_t bLength;
  429. /** Descriptor type. Will have value
  430. * \ref libusb_descriptor_type::LIBUSB_DT_ENDPOINT LIBUSB_DT_ENDPOINT in
  431. * this context. */
  432. uint8_t bDescriptorType;
  433. /** The address of the endpoint described by this descriptor. Bits 0:3 are
  434. * the endpoint number. Bits 4:6 are reserved. Bit 7 indicates direction,
  435. * see \ref libusb_endpoint_direction.
  436. */
  437. uint8_t bEndpointAddress;
  438. /** Attributes which apply to the endpoint when it is configured using
  439. * the bConfigurationValue. Bits 0:1 determine the transfer type and
  440. * correspond to \ref libusb_transfer_type. Bits 2:3 are only used for
  441. * isochronous endpoints and correspond to \ref libusb_iso_sync_type.
  442. * Bits 4:5 are also only used for isochronous endpoints and correspond to
  443. * \ref libusb_iso_usage_type. Bits 6:7 are reserved.
  444. */
  445. uint8_t bmAttributes;
  446. /** Maximum packet size this endpoint is capable of sending/receiving. */
  447. uint16_t wMaxPacketSize;
  448. /** Interval for polling endpoint for data transfers. */
  449. uint8_t bInterval;
  450. /** For audio devices only: the rate at which synchronization feedback
  451. * is provided. */
  452. uint8_t bRefresh;
  453. /** For audio devices only: the address if the synch endpoint */
  454. uint8_t bSynchAddress;
  455. /** Extra descriptors. If libusb encounters unknown endpoint descriptors,
  456. * it will store them here, should you wish to parse them. */
  457. const unsigned char *extra;
  458. /** Length of the extra descriptors, in bytes. */
  459. int extra_length;
  460. };
  461. /** \ingroup desc
  462. * A structure representing the standard USB interface descriptor. This
  463. * descriptor is documented in section 9.6.5 of the USB 3.0 specification.
  464. * All multiple-byte fields are represented in host-endian format.
  465. */
  466. struct libusb_interface_descriptor {
  467. /** Size of this descriptor (in bytes) */
  468. uint8_t bLength;
  469. /** Descriptor type. Will have value
  470. * \ref libusb_descriptor_type::LIBUSB_DT_INTERFACE LIBUSB_DT_INTERFACE
  471. * in this context. */
  472. uint8_t bDescriptorType;
  473. /** Number of this interface */
  474. uint8_t bInterfaceNumber;
  475. /** Value used to select this alternate setting for this interface */
  476. uint8_t bAlternateSetting;
  477. /** Number of endpoints used by this interface (excluding the control
  478. * endpoint). */
  479. uint8_t bNumEndpoints;
  480. /** USB-IF class code for this interface. See \ref libusb_class_code. */
  481. uint8_t bInterfaceClass;
  482. /** USB-IF subclass code for this interface, qualified by the
  483. * bInterfaceClass value */
  484. uint8_t bInterfaceSubClass;
  485. /** USB-IF protocol code for this interface, qualified by the
  486. * bInterfaceClass and bInterfaceSubClass values */
  487. uint8_t bInterfaceProtocol;
  488. /** Index of string descriptor describing this interface */
  489. uint8_t iInterface;
  490. /** Array of endpoint descriptors. This length of this array is determined
  491. * by the bNumEndpoints field. */
  492. const struct libusb_endpoint_descriptor *endpoint;
  493. /** Extra descriptors. If libusb encounters unknown interface descriptors,
  494. * it will store them here, should you wish to parse them. */
  495. const unsigned char *extra;
  496. /** Length of the extra descriptors, in bytes. */
  497. int extra_length;
  498. };
  499. /** \ingroup desc
  500. * A collection of alternate settings for a particular USB interface.
  501. */
  502. struct libusb_interface {
  503. /** Array of interface descriptors. The length of this array is determined
  504. * by the num_altsetting field. */
  505. const struct libusb_interface_descriptor *altsetting;
  506. /** The number of alternate settings that belong to this interface */
  507. int num_altsetting;
  508. };
  509. /** \ingroup desc
  510. * A structure representing the standard USB configuration descriptor. This
  511. * descriptor is documented in section 9.6.3 of the USB 3.0 specification.
  512. * All multiple-byte fields are represented in host-endian format.
  513. */
  514. struct libusb_config_descriptor {
  515. /** Size of this descriptor (in bytes) */
  516. uint8_t bLength;
  517. /** Descriptor type. Will have value
  518. * \ref libusb_descriptor_type::LIBUSB_DT_CONFIG LIBUSB_DT_CONFIG
  519. * in this context. */
  520. uint8_t bDescriptorType;
  521. /** Total length of data returned for this configuration */
  522. uint16_t wTotalLength;
  523. /** Number of interfaces supported by this configuration */
  524. uint8_t bNumInterfaces;
  525. /** Identifier value for this configuration */
  526. uint8_t bConfigurationValue;
  527. /** Index of string descriptor describing this configuration */
  528. uint8_t iConfiguration;
  529. /** Configuration characteristics */
  530. uint8_t bmAttributes;
  531. /** Maximum power consumption of the USB device from this bus in this
  532. * configuration when the device is fully operation. Expressed in units
  533. * of 2 mA when the device is operating in high-speed mode and in units
  534. * of 8 mA when the device is operating in super-speed mode. */
  535. uint8_t MaxPower;
  536. /** Array of interfaces supported by this configuration. The length of
  537. * this array is determined by the bNumInterfaces field. */
  538. const struct libusb_interface *interface;
  539. /** Extra descriptors. If libusb encounters unknown configuration
  540. * descriptors, it will store them here, should you wish to parse them. */
  541. const unsigned char *extra;
  542. /** Length of the extra descriptors, in bytes. */
  543. int extra_length;
  544. };
  545. /** \ingroup desc
  546. * A structure representing the superspeed endpoint companion
  547. * descriptor. This descriptor is documented in section 9.6.7 of
  548. * the USB 3.0 specification. All multiple-byte fields are represented in
  549. * host-endian format.
  550. */
  551. struct libusb_ss_endpoint_companion_descriptor {
  552. /** Size of this descriptor (in bytes) */
  553. uint8_t bLength;
  554. /** Descriptor type. Will have value
  555. * \ref libusb_descriptor_type::LIBUSB_DT_SS_ENDPOINT_COMPANION in
  556. * this context. */
  557. uint8_t bDescriptorType;
  558. /** The maximum number of packets the endpoint can send or
  559. * recieve as part of a burst. */
  560. uint8_t bMaxBurst;
  561. /** In bulk EP: bits 4:0 represents the maximum number of
  562. * streams the EP supports. In isochronous EP: bits 1:0
  563. * represents the Mult - a zero based value that determines
  564. * the maximum number of packets within a service interval */
  565. uint8_t bmAttributes;
  566. /** The total number of bytes this EP will transfer every
  567. * service interval. valid only for periodic EPs. */
  568. uint16_t wBytesPerInterval;
  569. };
  570. /** \ingroup desc
  571. * A generic representation of a BOS Device Capability descriptor. It is
  572. * advised to check bDevCapabilityType and call the matching
  573. * libusb_get_*_descriptor function to get a structure fully matching the type.
  574. */
  575. struct libusb_bos_dev_capability_descriptor {
  576. /** Size of this descriptor (in bytes) */
  577. uint8_t bLength;
  578. /** Descriptor type. Will have value
  579. * \ref libusb_descriptor_type::LIBUSB_DT_DEVICE_CAPABILITY
  580. * LIBUSB_DT_DEVICE_CAPABILITY in this context. */
  581. uint8_t bDescriptorType;
  582. /** Device Capability type */
  583. uint8_t bDevCapabilityType;
  584. /** Device Capability data (bLength - 3 bytes) */
  585. uint8_t dev_capability_data
  586. #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
  587. [] /* valid C99 code */
  588. #else
  589. [0] /* non-standard, but usually working code */
  590. #endif
  591. ;
  592. };
  593. /** \ingroup desc
  594. * A structure representing the Binary Device Object Store (BOS) descriptor.
  595. * This descriptor is documented in section 9.6.2 of the USB 3.0 specification.
  596. * All multiple-byte fields are represented in host-endian format.
  597. */
  598. struct libusb_bos_descriptor {
  599. /** Size of this descriptor (in bytes) */
  600. uint8_t bLength;
  601. /** Descriptor type. Will have value
  602. * \ref libusb_descriptor_type::LIBUSB_DT_BOS LIBUSB_DT_BOS
  603. * in this context. */
  604. uint8_t bDescriptorType;
  605. /** Length of this descriptor and all of its sub descriptors */
  606. uint16_t wTotalLength;
  607. /** The number of separate device capability descriptors in
  608. * the BOS */
  609. uint8_t bNumDeviceCaps;
  610. /** bNumDeviceCap Device Capability Descriptors */
  611. struct libusb_bos_dev_capability_descriptor *dev_capability
  612. #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
  613. [] /* valid C99 code */
  614. #else
  615. [0] /* non-standard, but usually working code */
  616. #endif
  617. ;
  618. };
  619. /** \ingroup desc
  620. * A structure representing the USB 2.0 Extension descriptor
  621. * This descriptor is documented in section 9.6.2.1 of the USB 3.0 specification.
  622. * All multiple-byte fields are represented in host-endian format.
  623. */
  624. struct libusb_usb_2_0_extension_descriptor {
  625. /** Size of this descriptor (in bytes) */
  626. uint8_t bLength;
  627. /** Descriptor type. Will have value
  628. * \ref libusb_descriptor_type::LIBUSB_DT_DEVICE_CAPABILITY
  629. * LIBUSB_DT_DEVICE_CAPABILITY in this context. */
  630. uint8_t bDescriptorType;
  631. /** Capability type. Will have value
  632. * \ref libusb_capability_type::LIBUSB_BT_USB_2_0_EXTENSION
  633. * LIBUSB_BT_USB_2_0_EXTENSION in this context. */
  634. uint8_t bDevCapabilityType;
  635. /** Bitmap encoding of supported device level features.
  636. * A value of one in a bit location indicates a feature is
  637. * supported; a value of zero indicates it is not supported.
  638. * See \ref libusb_usb_2_0_extension_attributes. */
  639. uint32_t bmAttributes;
  640. };
  641. /** \ingroup desc
  642. * A structure representing the SuperSpeed USB Device Capability descriptor
  643. * This descriptor is documented in section 9.6.2.2 of the USB 3.0 specification.
  644. * All multiple-byte fields are represented in host-endian format.
  645. */
  646. struct libusb_ss_usb_device_capability_descriptor {
  647. /** Size of this descriptor (in bytes) */
  648. uint8_t bLength;
  649. /** Descriptor type. Will have value
  650. * \ref libusb_descriptor_type::LIBUSB_DT_DEVICE_CAPABILITY
  651. * LIBUSB_DT_DEVICE_CAPABILITY in this context. */
  652. uint8_t bDescriptorType;
  653. /** Capability type. Will have value
  654. * \ref libusb_capability_type::LIBUSB_BT_SS_USB_DEVICE_CAPABILITY
  655. * LIBUSB_BT_SS_USB_DEVICE_CAPABILITY in this context. */
  656. uint8_t bDevCapabilityType;
  657. /** Bitmap encoding of supported device level features.
  658. * A value of one in a bit location indicates a feature is
  659. * supported; a value of zero indicates it is not supported.
  660. * See \ref libusb_ss_usb_device_capability_attributes. */
  661. uint8_t bmAttributes;
  662. /** Bitmap encoding of the speed supported by this device when
  663. * operating in SuperSpeed mode. See \ref libusb_supported_speed. */
  664. uint16_t wSpeedSupported;
  665. /** The lowest speed at which all the functionality supported
  666. * by the device is available to the user. For example if the
  667. * device supports all its functionality when connected at
  668. * full speed and above then it sets this value to 1. */
  669. uint8_t bFunctionalitySupport;
  670. /** U1 Device Exit Latency. */
  671. uint8_t bU1DevExitLat;
  672. /** U2 Device Exit Latency. */
  673. uint16_t bU2DevExitLat;
  674. };
  675. /** \ingroup desc
  676. * A structure representing the Container ID descriptor.
  677. * This descriptor is documented in section 9.6.2.3 of the USB 3.0 specification.
  678. * All multiple-byte fields, except UUIDs, are represented in host-endian format.
  679. */
  680. struct libusb_container_id_descriptor {
  681. /** Size of this descriptor (in bytes) */
  682. uint8_t bLength;
  683. /** Descriptor type. Will have value
  684. * \ref libusb_descriptor_type::LIBUSB_DT_DEVICE_CAPABILITY
  685. * LIBUSB_DT_DEVICE_CAPABILITY in this context. */
  686. uint8_t bDescriptorType;
  687. /** Capability type. Will have value
  688. * \ref libusb_capability_type::LIBUSB_BT_CONTAINER_ID
  689. * LIBUSB_BT_CONTAINER_ID in this context. */
  690. uint8_t bDevCapabilityType;
  691. /** Reserved field */
  692. uint8_t bReserved;
  693. /** 128 bit UUID */
  694. uint8_t ContainerID[16];
  695. };
  696. /** \ingroup asyncio
  697. * Setup packet for control transfers. */
  698. struct libusb_control_setup {
  699. /** Request type. Bits 0:4 determine recipient, see
  700. * \ref libusb_request_recipient. Bits 5:6 determine type, see
  701. * \ref libusb_request_type. Bit 7 determines data transfer direction, see
  702. * \ref libusb_endpoint_direction.
  703. */
  704. uint8_t bmRequestType;
  705. /** Request. If the type bits of bmRequestType are equal to
  706. * \ref libusb_request_type::LIBUSB_REQUEST_TYPE_STANDARD
  707. * "LIBUSB_REQUEST_TYPE_STANDARD" then this field refers to
  708. * \ref libusb_standard_request. For other cases, use of this field is
  709. * application-specific. */
  710. uint8_t bRequest;
  711. /** Value. Varies according to request */
  712. uint16_t wValue;
  713. /** Index. Varies according to request, typically used to pass an index
  714. * or offset */
  715. uint16_t wIndex;
  716. /** Number of bytes to transfer */
  717. uint16_t wLength;
  718. };
  719. #define LIBUSB_CONTROL_SETUP_SIZE (sizeof(struct libusb_control_setup))
  720. /* libusb */
  721. struct libusb_context;
  722. struct libusb_device;
  723. struct libusb_device_handle;
  724. /** \ingroup lib
  725. * Structure providing the version of the libusb runtime
  726. */
  727. struct libusb_version {
  728. /** Library major version. */
  729. const uint16_t major;
  730. /** Library minor version. */
  731. const uint16_t minor;
  732. /** Library micro version. */
  733. const uint16_t micro;
  734. /** Library nano version. */
  735. const uint16_t nano;
  736. /** Library release candidate suffix string, e.g. "-rc4". */
  737. const char *rc;
  738. /** For ABI compatibility only. */
  739. const char* describe;
  740. };
  741. /** \ingroup lib
  742. * Structure representing a libusb session. The concept of individual libusb
  743. * sessions allows for your program to use two libraries (or dynamically
  744. * load two modules) which both independently use libusb. This will prevent
  745. * interference between the individual libusb users - for example
  746. * libusb_set_debug() will not affect the other user of the library, and
  747. * libusb_exit() will not destroy resources that the other user is still
  748. * using.
  749. *
  750. * Sessions are created by libusb_init() and destroyed through libusb_exit().
  751. * If your application is guaranteed to only ever include a single libusb
  752. * user (i.e. you), you do not have to worry about contexts: pass NULL in
  753. * every function call where a context is required. The default context
  754. * will be used.
  755. *
  756. * For more information, see \ref contexts.
  757. */
  758. typedef struct libusb_context libusb_context;
  759. /** \ingroup dev
  760. * Structure representing a USB device detected on the system. This is an
  761. * opaque type for which you are only ever provided with a pointer, usually
  762. * originating from libusb_get_device_list().
  763. *
  764. * Certain operations can be performed on a device, but in order to do any
  765. * I/O you will have to first obtain a device handle using libusb_open().
  766. *
  767. * Devices are reference counted with libusb_ref_device() and
  768. * libusb_unref_device(), and are freed when the reference count reaches 0.
  769. * New devices presented by libusb_get_device_list() have a reference count of
  770. * 1, and libusb_free_device_list() can optionally decrease the reference count
  771. * on all devices in the list. libusb_open() adds another reference which is
  772. * later destroyed by libusb_close().
  773. */
  774. typedef struct libusb_device libusb_device;
  775. /** \ingroup dev
  776. * Structure representing a handle on a USB device. This is an opaque type for
  777. * which you are only ever provided with a pointer, usually originating from
  778. * libusb_open().
  779. *
  780. * A device handle is used to perform I/O and other operations. When finished
  781. * with a device handle, you should call libusb_close().
  782. */
  783. typedef struct libusb_device_handle libusb_device_handle;
  784. /** \ingroup dev
  785. * Speed codes. Indicates the speed at which the device is operating.
  786. */
  787. enum libusb_speed {
  788. /** The OS doesn't report or know the device speed. */
  789. LIBUSB_SPEED_UNKNOWN = 0,
  790. /** The device is operating at low speed (1.5MBit/s). */
  791. LIBUSB_SPEED_LOW = 1,
  792. /** The device is operating at full speed (12MBit/s). */
  793. LIBUSB_SPEED_FULL = 2,
  794. /** The device is operating at high speed (480MBit/s). */
  795. LIBUSB_SPEED_HIGH = 3,
  796. /** The device is operating at super speed (5000MBit/s). */
  797. LIBUSB_SPEED_SUPER = 4,
  798. };
  799. /** \ingroup dev
  800. * Supported speeds (wSpeedSupported) bitfield. Indicates what
  801. * speeds the device supports.
  802. */
  803. enum libusb_supported_speed {
  804. /** Low speed operation supported (1.5MBit/s). */
  805. LIBUSB_LOW_SPEED_OPERATION = 1,
  806. /** Full speed operation supported (12MBit/s). */
  807. LIBUSB_FULL_SPEED_OPERATION = 2,
  808. /** High speed operation supported (480MBit/s). */
  809. LIBUSB_HIGH_SPEED_OPERATION = 4,
  810. /** Superspeed operation supported (5000MBit/s). */
  811. LIBUSB_SUPER_SPEED_OPERATION = 8,
  812. };
  813. /** \ingroup dev
  814. * Masks for the bits of the
  815. * \ref libusb_usb_2_0_extension_descriptor::bmAttributes "bmAttributes" field
  816. * of the USB 2.0 Extension descriptor.
  817. */
  818. enum libusb_usb_2_0_extension_attributes {
  819. /** Supports Link Power Management (LPM) */
  820. LIBUSB_BM_LPM_SUPPORT = 2,
  821. };
  822. /** \ingroup dev
  823. * Masks for the bits of the
  824. * \ref libusb_ss_usb_device_capability_descriptor::bmAttributes "bmAttributes" field
  825. * field of the SuperSpeed USB Device Capability descriptor.
  826. */
  827. enum libusb_ss_usb_device_capability_attributes {
  828. /** Supports Latency Tolerance Messages (LTM) */
  829. LIBUSB_BM_LTM_SUPPORT = 2,
  830. };
  831. /** \ingroup dev
  832. * USB capability types
  833. */
  834. enum libusb_bos_type {
  835. /** Wireless USB device capability */
  836. LIBUSB_BT_WIRELESS_USB_DEVICE_CAPABILITY = 1,
  837. /** USB 2.0 extensions */
  838. LIBUSB_BT_USB_2_0_EXTENSION = 2,
  839. /** SuperSpeed USB device capability */
  840. LIBUSB_BT_SS_USB_DEVICE_CAPABILITY = 3,
  841. /** Container ID type */
  842. LIBUSB_BT_CONTAINER_ID = 4,
  843. };
  844. /** \ingroup misc
  845. * Error codes. Most libusb functions return 0 on success or one of these
  846. * codes on failure.
  847. * You can call libusb_error_name() to retrieve a string representation of an
  848. * error code or libusb_strerror() to get an end-user suitable description of
  849. * an error code.
  850. */
  851. enum libusb_error {
  852. /** Success (no error) */
  853. LIBUSB_SUCCESS = 0,
  854. /** Input/output error */
  855. LIBUSB_ERROR_IO = -1,
  856. /** Invalid parameter */
  857. LIBUSB_ERROR_INVALID_PARAM = -2,
  858. /** Access denied (insufficient permissions) */
  859. LIBUSB_ERROR_ACCESS = -3,
  860. /** No such device (it may have been disconnected) */
  861. LIBUSB_ERROR_NO_DEVICE = -4,
  862. /** Entity not found */
  863. LIBUSB_ERROR_NOT_FOUND = -5,
  864. /** Resource busy */
  865. LIBUSB_ERROR_BUSY = -6,
  866. /** Operation timed out */
  867. LIBUSB_ERROR_TIMEOUT = -7,
  868. /** Overflow */
  869. LIBUSB_ERROR_OVERFLOW = -8,
  870. /** Pipe error */
  871. LIBUSB_ERROR_PIPE = -9,
  872. /** System call interrupted (perhaps due to signal) */
  873. LIBUSB_ERROR_INTERRUPTED = -10,
  874. /** Insufficient memory */
  875. LIBUSB_ERROR_NO_MEM = -11,
  876. /** Operation not supported or unimplemented on this platform */
  877. LIBUSB_ERROR_NOT_SUPPORTED = -12,
  878. /* NB: Remember to update LIBUSB_ERROR_COUNT below as well as the
  879. message strings in strerror.c when adding new error codes here. */
  880. /** Other error */
  881. LIBUSB_ERROR_OTHER = -99,
  882. };
  883. /* Total number of error codes in enum libusb_error */
  884. #define LIBUSB_ERROR_COUNT 14
  885. /** \ingroup asyncio
  886. * Transfer status codes */
  887. enum libusb_transfer_status {
  888. /** Transfer completed without error. Note that this does not indicate
  889. * that the entire amount of requested data was transferred. */
  890. LIBUSB_TRANSFER_COMPLETED,
  891. /** Transfer failed */
  892. LIBUSB_TRANSFER_ERROR,
  893. /** Transfer timed out */
  894. LIBUSB_TRANSFER_TIMED_OUT,
  895. /** Transfer was cancelled */
  896. LIBUSB_TRANSFER_CANCELLED,
  897. /** For bulk/interrupt endpoints: halt condition detected (endpoint
  898. * stalled). For control endpoints: control request not supported. */
  899. LIBUSB_TRANSFER_STALL,
  900. /** Device was disconnected */
  901. LIBUSB_TRANSFER_NO_DEVICE,
  902. /** Device sent more data than requested */
  903. LIBUSB_TRANSFER_OVERFLOW,
  904. /* NB! Remember to update libusb_error_name()
  905. when adding new status codes here. */
  906. };
  907. /** \ingroup asyncio
  908. * libusb_transfer.flags values */
  909. enum libusb_transfer_flags {
  910. /** Report short frames as errors */
  911. LIBUSB_TRANSFER_SHORT_NOT_OK = 1<<0,
  912. /** Automatically free() transfer buffer during libusb_free_transfer() */
  913. LIBUSB_TRANSFER_FREE_BUFFER = 1<<1,
  914. /** Automatically call libusb_free_transfer() after callback returns.
  915. * If this flag is set, it is illegal to call libusb_free_transfer()
  916. * from your transfer callback, as this will result in a double-free
  917. * when this flag is acted upon. */
  918. LIBUSB_TRANSFER_FREE_TRANSFER = 1<<2,
  919. /** Terminate transfers that are a multiple of the endpoint's
  920. * wMaxPacketSize with an extra zero length packet. This is useful
  921. * when a device protocol mandates that each logical request is
  922. * terminated by an incomplete packet (i.e. the logical requests are
  923. * not separated by other means).
  924. *
  925. * This flag only affects host-to-device transfers to bulk and interrupt
  926. * endpoints. In other situations, it is ignored.
  927. *
  928. * This flag only affects transfers with a length that is a multiple of
  929. * the endpoint's wMaxPacketSize. On transfers of other lengths, this
  930. * flag has no effect. Therefore, if you are working with a device that
  931. * needs a ZLP whenever the end of the logical request falls on a packet
  932. * boundary, then it is sensible to set this flag on <em>every</em>
  933. * transfer (you do not have to worry about only setting it on transfers
  934. * that end on the boundary).
  935. *
  936. * This flag is currently only supported on Linux.
  937. * On other systems, libusb_submit_transfer() will return
  938. * LIBUSB_ERROR_NOT_SUPPORTED for every transfer where this flag is set.
  939. *
  940. * Available since libusb-1.0.9.
  941. */
  942. LIBUSB_TRANSFER_ADD_ZERO_PACKET = 1 << 3,
  943. };
  944. /** \ingroup asyncio
  945. * Isochronous packet descriptor. */
  946. struct libusb_iso_packet_descriptor {
  947. /** Length of data to request in this packet */
  948. unsigned int length;
  949. /** Amount of data that was actually transferred */
  950. unsigned int actual_length;
  951. /** Status code for this packet */
  952. enum libusb_transfer_status status;
  953. };
  954. struct libusb_transfer;
  955. /** \ingroup asyncio
  956. * Asynchronous transfer callback function type. When submitting asynchronous
  957. * transfers, you pass a pointer to a callback function of this type via the
  958. * \ref libusb_transfer::callback "callback" member of the libusb_transfer
  959. * structure. libusb will call this function later, when the transfer has
  960. * completed or failed. See \ref asyncio for more information.
  961. * \param transfer The libusb_transfer struct the callback function is being
  962. * notified about.
  963. */
  964. typedef void (LIBUSB_CALL *libusb_transfer_cb_fn)(struct libusb_transfer *transfer);
  965. /** \ingroup asyncio
  966. * The generic USB transfer structure. The user populates this structure and
  967. * then submits it in order to request a transfer. After the transfer has
  968. * completed, the library populates the transfer with the results and passes
  969. * it back to the user.
  970. */
  971. struct libusb_transfer {
  972. /** Handle of the device that this transfer will be submitted to */
  973. libusb_device_handle *dev_handle;
  974. /** A bitwise OR combination of \ref libusb_transfer_flags. */
  975. uint8_t flags;
  976. /** Address of the endpoint where this transfer will be sent. */
  977. unsigned char endpoint;
  978. /** Type of the endpoint from \ref libusb_transfer_type */
  979. unsigned char type;
  980. /** Timeout for this transfer in millseconds. A value of 0 indicates no
  981. * timeout. */
  982. unsigned int timeout;
  983. /** The status of the transfer. Read-only, and only for use within
  984. * transfer callback function.
  985. *
  986. * If this is an isochronous transfer, this field may read COMPLETED even
  987. * if there were errors in the frames. Use the
  988. * \ref libusb_iso_packet_descriptor::status "status" field in each packet
  989. * to determine if errors occurred. */
  990. enum libusb_transfer_status status;
  991. /** Length of the data buffer */
  992. int length;
  993. /** Actual length of data that was transferred. Read-only, and only for
  994. * use within transfer callback function. Not valid for isochronous
  995. * endpoint transfers. */
  996. int actual_length;
  997. /** Callback function. This will be invoked when the transfer completes,
  998. * fails, or is cancelled. */
  999. libusb_transfer_cb_fn callback;
  1000. /** User context data to pass to the callback function. */
  1001. void *user_data;
  1002. /** Data buffer */
  1003. unsigned char *buffer;
  1004. /** Number of isochronous packets. Only used for I/O with isochronous
  1005. * endpoints. */
  1006. int num_iso_packets;
  1007. /** Isochronous packet descriptors, for isochronous transfers only. */
  1008. struct libusb_iso_packet_descriptor iso_packet_desc
  1009. #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
  1010. [] /* valid C99 code */
  1011. #else
  1012. [0] /* non-standard, but usually working code */
  1013. #endif
  1014. ;
  1015. };
  1016. /** \ingroup misc
  1017. * Capabilities supported by an instance of libusb on the current running
  1018. * platform. Test if the loaded library supports a given capability by calling
  1019. * \ref libusb_has_capability().
  1020. */
  1021. enum libusb_capability {
  1022. /** The libusb_has_capability() API is available. */
  1023. LIBUSB_CAP_HAS_CAPABILITY = 0x0000,
  1024. /** Hotplug support is available on this platform. */
  1025. LIBUSB_CAP_HAS_HOTPLUG = 0x0001,
  1026. /** The library can access HID devices without requiring user intervention.
  1027. * Note that before being able to actually access an HID device, you may
  1028. * still have to call additional libusb functions such as
  1029. * \ref libusb_detach_kernel_driver(). */
  1030. LIBUSB_CAP_HAS_HID_ACCESS = 0x0100,
  1031. /** The library supports detaching of the default USB driver, using
  1032. * \ref libusb_detach_kernel_driver(), if one is set by the OS kernel */
  1033. LIBUSB_CAP_SUPPORTS_DETACH_KERNEL_DRIVER = 0x0101
  1034. };
  1035. /** \ingroup lib
  1036. * Log message levels.
  1037. * - LIBUSB_LOG_LEVEL_NONE (0) : no messages ever printed by the library (default)
  1038. * - LIBUSB_LOG_LEVEL_ERROR (1) : error messages are printed to stderr
  1039. * - LIBUSB_LOG_LEVEL_WARNING (2) : warning and error messages are printed to stderr
  1040. * - LIBUSB_LOG_LEVEL_INFO (3) : informational messages are printed to stdout, warning
  1041. * and error messages are printed to stderr
  1042. * - LIBUSB_LOG_LEVEL_DEBUG (4) : debug and informational messages are printed to stdout,
  1043. * warnings and errors to stderr
  1044. */
  1045. enum libusb_log_level {
  1046. LIBUSB_LOG_LEVEL_NONE = 0,
  1047. LIBUSB_LOG_LEVEL_ERROR,
  1048. LIBUSB_LOG_LEVEL_WARNING,
  1049. LIBUSB_LOG_LEVEL_INFO,
  1050. LIBUSB_LOG_LEVEL_DEBUG,
  1051. };
  1052. int LIBUSB_CALL libusb_init(libusb_context **ctx);
  1053. void LIBUSB_CALL libusb_exit(libusb_context *ctx);
  1054. void LIBUSB_CALL libusb_set_debug(libusb_context *ctx, int level);
  1055. const struct libusb_version * LIBUSB_CALL libusb_get_version(void);
  1056. int LIBUSB_CALL libusb_has_capability(uint32_t capability);
  1057. const char * LIBUSB_CALL libusb_error_name(int errcode);
  1058. int LIBUSB_CALL libusb_setlocale(const char *locale);
  1059. const char * LIBUSB_CALL libusb_strerror(enum libusb_error errcode);
  1060. ssize_t LIBUSB_CALL libusb_get_device_list(libusb_context *ctx,
  1061. libusb_device ***list);
  1062. void LIBUSB_CALL libusb_free_device_list(libusb_device **list,
  1063. int unref_devices);
  1064. libusb_device * LIBUSB_CALL libusb_ref_device(libusb_device *dev);
  1065. void LIBUSB_CALL libusb_unref_device(libusb_device *dev);
  1066. int LIBUSB_CALL libusb_get_configuration(libusb_device_handle *dev,
  1067. int *config);
  1068. int LIBUSB_CALL libusb_get_device_descriptor(libusb_device *dev,
  1069. struct libusb_device_descriptor *desc);
  1070. int LIBUSB_CALL libusb_get_active_config_descriptor(libusb_device *dev,
  1071. struct libusb_config_descriptor **config);
  1072. int LIBUSB_CALL libusb_get_config_descriptor(libusb_device *dev,
  1073. uint8_t config_index, struct libusb_config_descriptor **config);
  1074. int LIBUSB_CALL libusb_get_config_descriptor_by_value(libusb_device *dev,
  1075. uint8_t bConfigurationValue, struct libusb_config_descriptor **config);
  1076. void LIBUSB_CALL libusb_free_config_descriptor(
  1077. struct libusb_config_descriptor *config);
  1078. int LIBUSB_CALL libusb_get_ss_endpoint_companion_descriptor(
  1079. struct libusb_context *ctx,
  1080. const struct libusb_endpoint_descriptor *endpoint,
  1081. struct libusb_ss_endpoint_companion_descriptor **ep_comp);
  1082. void LIBUSB_CALL libusb_free_ss_endpoint_companion_descriptor(
  1083. struct libusb_ss_endpoint_companion_descriptor *ep_comp);
  1084. int LIBUSB_CALL libusb_get_bos_descriptor(libusb_device_handle *handle,
  1085. struct libusb_bos_descriptor **bos);
  1086. void LIBUSB_CALL libusb_free_bos_descriptor(struct libusb_bos_descriptor *bos);
  1087. int LIBUSB_CALL libusb_get_usb_2_0_extension_descriptor(
  1088. struct libusb_context *ctx,
  1089. struct libusb_bos_dev_capability_descriptor *dev_cap,
  1090. struct libusb_usb_2_0_extension_descriptor **usb_2_0_extension);
  1091. void LIBUSB_CALL libusb_free_usb_2_0_extension_descriptor(
  1092. struct libusb_usb_2_0_extension_descriptor *usb_2_0_extension);
  1093. int LIBUSB_CALL libusb_get_ss_usb_device_capability_descriptor(
  1094. struct libusb_context *ctx,
  1095. struct libusb_bos_dev_capability_descriptor *dev_cap,
  1096. struct libusb_ss_usb_device_capability_descriptor **ss_usb_device_cap);
  1097. void LIBUSB_CALL libusb_free_ss_usb_device_capability_descriptor(
  1098. struct libusb_ss_usb_device_capability_descriptor *ss_usb_device_cap);
  1099. int LIBUSB_CALL libusb_get_container_id_descriptor(struct libusb_context *ctx,
  1100. struct libusb_bos_dev_capability_descriptor *dev_cap,
  1101. struct libusb_container_id_descriptor **container_id);
  1102. void LIBUSB_CALL libusb_free_container_id_descriptor(
  1103. struct libusb_container_id_descriptor *container_id);
  1104. uint8_t LIBUSB_CALL libusb_get_bus_number(libusb_device *dev);
  1105. uint8_t LIBUSB_CALL libusb_get_port_number(libusb_device *dev);
  1106. int LIBUSB_CALL libusb_get_port_numbers(libusb_device *dev, uint8_t* port_numbers, int port_numbers_len);
  1107. LIBUSB_DEPRECATED_FOR(libusb_get_port_numbers)
  1108. int LIBUSB_CALL libusb_get_port_path(libusb_context *ctx, libusb_device *dev, uint8_t* path, uint8_t path_length);
  1109. libusb_device * LIBUSB_CALL libusb_get_parent(libusb_device *dev);
  1110. uint8_t LIBUSB_CALL libusb_get_device_address(libusb_device *dev);
  1111. int LIBUSB_CALL libusb_get_device_speed(libusb_device *dev);
  1112. int LIBUSB_CALL libusb_get_max_packet_size(libusb_device *dev,
  1113. unsigned char endpoint);
  1114. int LIBUSB_CALL libusb_get_max_iso_packet_size(libusb_device *dev,
  1115. unsigned char endpoint);
  1116. int LIBUSB_CALL libusb_open(libusb_device *dev, libusb_device_handle **handle);
  1117. void LIBUSB_CALL libusb_close(libusb_device_handle *dev_handle);
  1118. libusb_device * LIBUSB_CALL libusb_get_device(libusb_device_handle *dev_handle);
  1119. int LIBUSB_CALL libusb_set_configuration(libusb_device_handle *dev,
  1120. int configuration);
  1121. int LIBUSB_CALL libusb_claim_interface(libusb_device_handle *dev,
  1122. int interface_number);
  1123. int LIBUSB_CALL libusb_release_interface(libusb_device_handle *dev,
  1124. int interface_number);
  1125. libusb_device_handle * LIBUSB_CALL libusb_open_device_with_vid_pid(
  1126. libusb_context *ctx, uint16_t vendor_id, uint16_t product_id);
  1127. int LIBUSB_CALL libusb_set_interface_alt_setting(libusb_device_handle *dev,
  1128. int interface_number, int alternate_setting);
  1129. int LIBUSB_CALL libusb_clear_halt(libusb_device_handle *dev,
  1130. unsigned char endpoint);
  1131. int LIBUSB_CALL libusb_reset_device(libusb_device_handle *dev);
  1132. int LIBUSB_CALL libusb_alloc_streams(libusb_device_handle *dev,
  1133. uint32_t num_streams, unsigned char *endpoints, int num_endpoints);
  1134. int LIBUSB_CALL libusb_free_streams(libusb_device_handle *dev,
  1135. unsigned char *endpoints, int num_endpoints);
  1136. int LIBUSB_CALL libusb_kernel_driver_active(libusb_device_handle *dev,
  1137. int interface_number);
  1138. int LIBUSB_CALL libusb_detach_kernel_driver(libusb_device_handle *dev,
  1139. int interface_number);
  1140. int LIBUSB_CALL libusb_attach_kernel_driver(libusb_device_handle *dev,
  1141. int interface_number);
  1142. int LIBUSB_CALL libusb_set_auto_detach_kernel_driver(
  1143. libusb_device_handle *dev, int enable);
  1144. /* async I/O */
  1145. /** \ingroup asyncio
  1146. * Get the data section of a control transfer. This convenience function is here
  1147. * to remind you that the data does not start until 8 bytes into the actual
  1148. * buffer, as the setup packet comes first.
  1149. *
  1150. * Calling this function only makes sense from a transfer callback function,
  1151. * or situations where you have already allocated a suitably sized buffer at
  1152. * transfer->buffer.
  1153. *
  1154. * \param transfer a transfer
  1155. * \returns pointer to the first byte of the data section
  1156. */
  1157. static inline unsigned char *libusb_control_transfer_get_data(
  1158. struct libusb_transfer *transfer)
  1159. {
  1160. return transfer->buffer + LIBUSB_CONTROL_SETUP_SIZE;
  1161. }
  1162. /** \ingroup asyncio
  1163. * Get the control setup packet of a control transfer. This convenience
  1164. * function is here to remind you that the control setup occupies the first
  1165. * 8 bytes of the transfer data buffer.
  1166. *
  1167. * Calling this function only makes sense from a transfer callback function,
  1168. * or situations where you have already allocated a suitably sized buffer at
  1169. * transfer->buffer.
  1170. *
  1171. * \param transfer a transfer
  1172. * \returns a casted pointer to the start of the transfer data buffer
  1173. */
  1174. static inline struct libusb_control_setup *libusb_control_transfer_get_setup(
  1175. struct libusb_transfer *transfer)
  1176. {
  1177. return (struct libusb_control_setup *)(void *) transfer->buffer;
  1178. }
  1179. /** \ingroup asyncio
  1180. * Helper function to populate the setup packet (first 8 bytes of the data
  1181. * buffer) for a control transfer. The wIndex, wValue and wLength values should
  1182. * be given in host-endian byte order.
  1183. *
  1184. * \param buffer buffer to output the setup packet into
  1185. * This pointer must be aligned to at least 2 bytes boundary.
  1186. * \param bmRequestType see the
  1187. * \ref libusb_control_setup::bmRequestType "bmRequestType" field of
  1188. * \ref libusb_control_setup
  1189. * \param bRequest see the
  1190. * \ref libusb_control_setup::bRequest "bRequest" field of
  1191. * \ref libusb_control_setup
  1192. * \param wValue see the
  1193. * \ref libusb_control_setup::wValue "wValue" field of
  1194. * \ref libusb_control_setup
  1195. * \param wIndex see the
  1196. * \ref libusb_control_setup::wIndex "wIndex" field of
  1197. * \ref libusb_control_setup
  1198. * \param wLength see the
  1199. * \ref libusb_control_setup::wLength "wLength" field of
  1200. * \ref libusb_control_setup
  1201. */
  1202. static inline void libusb_fill_control_setup(unsigned char *buffer,
  1203. uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex,
  1204. uint16_t wLength)
  1205. {
  1206. struct libusb_control_setup *setup = (struct libusb_control_setup *)(void *) buffer;
  1207. setup->bmRequestType = bmRequestType;
  1208. setup->bRequest = bRequest;
  1209. setup->wValue = libusb_cpu_to_le16(wValue);
  1210. setup->wIndex = libusb_cpu_to_le16(wIndex);
  1211. setup->wLength = libusb_cpu_to_le16(wLength);
  1212. }
  1213. struct libusb_transfer * LIBUSB_CALL libusb_alloc_transfer(int iso_packets);
  1214. int LIBUSB_CALL libusb_submit_transfer(struct libusb_transfer *transfer);
  1215. int LIBUSB_CALL libusb_cancel_transfer(struct libusb_transfer *transfer);
  1216. void LIBUSB_CALL libusb_free_transfer(struct libusb_transfer *transfer);
  1217. void LIBUSB_CALL libusb_transfer_set_stream_id(
  1218. struct libusb_transfer *transfer, uint32_t stream_id);
  1219. uint32_t LIBUSB_CALL libusb_transfer_get_stream_id(
  1220. struct libusb_transfer *transfer);
  1221. /** \ingroup asyncio
  1222. * Helper function to populate the required \ref libusb_transfer fields
  1223. * for a control transfer.
  1224. *
  1225. * If you pass a transfer buffer to this function, the first 8 bytes will
  1226. * be interpreted as a control setup packet, and the wLength field will be
  1227. * used to automatically populate the \ref libusb_transfer::length "length"
  1228. * field of the transfer. Therefore the recommended approach is:
  1229. * -# Allocate a suitably sized data buffer (including space for control setup)
  1230. * -# Call libusb_fill_control_setup()
  1231. * -# If this is a host-to-device transfer with a data stage, put the data
  1232. * in place after the setup packet
  1233. * -# Call this function
  1234. * -# Call libusb_submit_transfer()
  1235. *
  1236. * It is also legal to pass a NULL buffer to this function, in which case this
  1237. * function will not attempt to populate the length field. Remember that you
  1238. * must then populate the buffer and length fields later.
  1239. *
  1240. * \param transfer the transfer to populate
  1241. * \param dev_handle handle of the device that will handle the transfer
  1242. * \param buffer data buffer. If provided, this function will interpret the
  1243. * first 8 bytes as a setup packet and infer the transfer length from that.
  1244. * This pointer must be aligned to at least 2 bytes boundary.
  1245. * \param callback callback function to be invoked on transfer completion
  1246. * \param user_data user data to pass to callback function
  1247. * \param timeout timeout for the transfer in milliseconds
  1248. */
  1249. static inline void libusb_fill_control_transfer(
  1250. struct libusb_transfer *transfer, libusb_device_handle *dev_handle,
  1251. unsigned char *buffer, libusb_transfer_cb_fn callback, void *user_data,
  1252. unsigned int timeout)
  1253. {
  1254. struct libusb_control_setup *setup = (struct libusb_control_setup *)(void *) buffer;
  1255. transfer->dev_handle = dev_handle;
  1256. transfer->endpoint = 0;
  1257. transfer->type = LIBUSB_TRANSFER_TYPE_CONTROL;
  1258. transfer->timeout = timeout;
  1259. transfer->buffer = buffer;
  1260. if (setup)
  1261. transfer->length = (int) (LIBUSB_CONTROL_SETUP_SIZE
  1262. + libusb_le16_to_cpu(setup->wLength));
  1263. transfer->user_data = user_data;
  1264. transfer->callback = callback;
  1265. }
  1266. /** \ingroup asyncio
  1267. * Helper function to populate the required \ref libusb_transfer fields
  1268. * for a bulk transfer.
  1269. *
  1270. * \param transfer the transfer to populate
  1271. * \param dev_handle handle of the device that will handle the transfer
  1272. * \param endpoint address of the endpoint where this transfer will be sent
  1273. * \param buffer data buffer
  1274. * \param length length of data buffer
  1275. * \param callback callback function to be invoked on transfer completion
  1276. * \param user_data user data to pass to callback function
  1277. * \param timeout timeout for the transfer in milliseconds
  1278. */
  1279. static inline void libusb_fill_bulk_transfer(struct libusb_transfer *transfer,
  1280. libusb_device_handle *dev_handle, unsigned char endpoint,
  1281. unsigned char *buffer, int length, libusb_transfer_cb_fn callback,
  1282. void *user_data, unsigned int timeout)
  1283. {
  1284. transfer->dev_handle = dev_handle;
  1285. transfer->endpoint = endpoint;
  1286. transfer->type = LIBUSB_TRANSFER_TYPE_BULK;
  1287. transfer->timeout = timeout;
  1288. transfer->buffer = buffer;
  1289. transfer->length = length;
  1290. transfer->user_data = user_data;
  1291. transfer->callback = callback;
  1292. }
  1293. /** \ingroup asyncio
  1294. * Helper function to populate the required \ref libusb_transfer fields
  1295. * for a bulk transfer using bulk streams.
  1296. *
  1297. * Since version 1.0.19, \ref LIBUSB_API_VERSION >= 0x01000103
  1298. *
  1299. * \param transfer the transfer to populate
  1300. * \param dev_handle handle of the device that will handle the transfer
  1301. * \param endpoint address of the endpoint where this transfer will be sent
  1302. * \param stream_id bulk stream id for this transfer
  1303. * \param buffer data buffer
  1304. * \param length length of data buffer
  1305. * \param callback callback function to be invoked on transfer completion
  1306. * \param user_data user data to pass to callback function
  1307. * \param timeout timeout for the transfer in milliseconds
  1308. */
  1309. static inline void libusb_fill_bulk_stream_transfer(
  1310. struct libusb_transfer *transfer, libusb_device_handle *dev_handle,
  1311. unsigned char endpoint, uint32_t stream_id,
  1312. unsigned char *buffer, int length, libusb_transfer_cb_fn callback,
  1313. void *user_data, unsigned int timeout)
  1314. {
  1315. libusb_fill_bulk_transfer(transfer, dev_handle, endpoint, buffer,
  1316. length, callback, user_data, timeout);
  1317. transfer->type = LIBUSB_TRANSFER_TYPE_BULK_STREAM;
  1318. libusb_transfer_set_stream_id(transfer, stream_id);
  1319. }
  1320. /** \ingroup asyncio
  1321. * Helper function to populate the required \ref libusb_transfer fields
  1322. * for an interrupt transfer.
  1323. *
  1324. * \param transfer the transfer to populate
  1325. * \param dev_handle handle of the device that will handle the transfer
  1326. * \param endpoint address of the endpoint where this transfer will be sent
  1327. * \param buffer data buffer
  1328. * \param length length of data buffer
  1329. * \param callback callback function to be invoked on transfer completion
  1330. * \param user_data user data to pass to callback function
  1331. * \param timeout timeout for the transfer in milliseconds
  1332. */
  1333. static inline void libusb_fill_interrupt_transfer(
  1334. struct libusb_transfer *transfer, libusb_device_handle *dev_handle,
  1335. unsigned char endpoint, unsigned char *buffer, int length,
  1336. libusb_transfer_cb_fn callback, void *user_data, unsigned int timeout)
  1337. {
  1338. transfer->dev_handle = dev_handle;
  1339. transfer->endpoint = endpoint;
  1340. transfer->type = LIBUSB_TRANSFER_TYPE_INTERRUPT;
  1341. transfer->timeout = timeout;
  1342. transfer->buffer = buffer;
  1343. transfer->length = length;
  1344. transfer->user_data = user_data;
  1345. transfer->callback = callback;
  1346. }
  1347. /** \ingroup asyncio
  1348. * Helper function to populate the required \ref libusb_transfer fields
  1349. * for an isochronous transfer.
  1350. *
  1351. * \param transfer the transfer to populate
  1352. * \param dev_handle handle of the device that will handle the transfer
  1353. * \param endpoint address of the endpoint where this transfer will be sent
  1354. * \param buffer data buffer
  1355. * \param length length of data buffer
  1356. * \param num_iso_packets the number of isochronous packets
  1357. * \param callback callback function to be invoked on transfer completion
  1358. * \param user_data user data to pass to callback function
  1359. * \param timeout timeout for the transfer in milliseconds
  1360. */
  1361. static inline void libusb_fill_iso_transfer(struct libusb_transfer *transfer,
  1362. libusb_device_handle *dev_handle, unsigned char endpoint,
  1363. unsigned char *buffer, int length, int num_iso_packets,
  1364. libusb_transfer_cb_fn callback, void *user_data, unsigned int timeout)
  1365. {
  1366. transfer->dev_handle = dev_handle;
  1367. transfer->endpoint = endpoint;
  1368. transfer->type = LIBUSB_TRANSFER_TYPE_ISOCHRONOUS;
  1369. transfer->timeout = timeout;
  1370. transfer->buffer = buffer;
  1371. transfer->length = length;
  1372. transfer->num_iso_packets = num_iso_packets;
  1373. transfer->user_data = user_data;
  1374. transfer->callback = callback;
  1375. }
  1376. /** \ingroup asyncio
  1377. * Convenience function to set the length of all packets in an isochronous
  1378. * transfer, based on the num_iso_packets field in the transfer structure.
  1379. *
  1380. * \param transfer a transfer
  1381. * \param length the length to set in each isochronous packet descriptor
  1382. * \see libusb_get_max_packet_size()
  1383. */
  1384. static inline void libusb_set_iso_packet_lengths(
  1385. struct libusb_transfer *transfer, unsigned int length)
  1386. {
  1387. int i;
  1388. for (i = 0; i < transfer->num_iso_packets; i++)
  1389. transfer->iso_packet_desc[i].length = length;
  1390. }
  1391. /** \ingroup asyncio
  1392. * Convenience function to locate the position of an isochronous packet
  1393. * within the buffer of an isochronous transfer.
  1394. *
  1395. * This is a thorough function which loops through all preceding packets,
  1396. * accumulating their lengths to find the position of the specified packet.
  1397. * Typically you will assign equal lengths to each packet in the transfer,
  1398. * and hence the above method is sub-optimal. You may wish to use
  1399. * libusb_get_iso_packet_buffer_simple() instead.
  1400. *
  1401. * \param transfer a transfer
  1402. * \param packet the packet to return the address of
  1403. * \returns the base address of the packet buffer inside the transfer buffer,
  1404. * or NULL if the packet does not exist.
  1405. * \see libusb_get_iso_packet_buffer_simple()
  1406. */
  1407. static inline unsigned char *libusb_get_iso_packet_buffer(
  1408. struct libusb_transfer *transfer, unsigned int packet)
  1409. {
  1410. int i;
  1411. size_t offset = 0;
  1412. int _packet;
  1413. /* oops..slight bug in the API. packet is an unsigned int, but we use
  1414. * signed integers almost everywhere else. range-check and convert to
  1415. * signed to avoid compiler warnings. FIXME for libusb-2. */
  1416. if (packet > INT_MAX)
  1417. return NULL;
  1418. _packet = (int) packet;
  1419. if (_packet >= transfer->num_iso_packets)
  1420. return NULL;
  1421. for (i = 0; i < _packet; i++)
  1422. offset += transfer->iso_packet_desc[i].length;
  1423. return transfer->buffer + offset;
  1424. }
  1425. /** \ingroup asyncio
  1426. * Convenience function to locate the position of an isochronous packet
  1427. * within the buffer of an isochronous transfer, for transfers where each
  1428. * packet is of identical size.
  1429. *
  1430. * This function relies on the assumption that every packet within the transfer
  1431. * is of identical size to the first packet. Calculating the location of
  1432. * the packet buffer is then just a simple calculation:
  1433. * <tt>buffer + (packet_size * packet)</tt>
  1434. *
  1435. * Do not use this function on transfers other than those that have identical
  1436. * packet lengths for each packet.
  1437. *
  1438. * \param transfer a transfer
  1439. * \param packet the packet to return the address of
  1440. * \returns the base address of the packet buffer inside the transfer buffer,
  1441. * or NULL if the packet does not exist.
  1442. * \see libusb_get_iso_packet_buffer()
  1443. */
  1444. static inline unsigned char *libusb_get_iso_packet_buffer_simple(
  1445. struct libusb_transfer *transfer, unsigned int packet)
  1446. {
  1447. int _packet;
  1448. /* oops..slight bug in the API. packet is an unsigned int, but we use
  1449. * signed integers almost everywhere else. range-check and convert to
  1450. * signed to avoid compiler warnings. FIXME for libusb-2. */
  1451. if (packet > INT_MAX)
  1452. return NULL;
  1453. _packet = (int) packet;
  1454. if (_packet >= transfer->num_iso_packets)
  1455. return NULL;
  1456. return transfer->buffer + ((int) transfer->iso_packet_desc[0].length * _packet);
  1457. }
  1458. /* sync I/O */
  1459. int LIBUSB_CALL libusb_control_transfer(libusb_device_handle *dev_handle,
  1460. uint8_t request_type, uint8_t bRequest, uint16_t wValue, uint16_t wIndex,
  1461. unsigned char *data, uint16_t wLength, unsigned int timeout);
  1462. int LIBUSB_CALL libusb_bulk_transfer(libusb_device_handle *dev_handle,
  1463. unsigned char endpoint, unsigned char *data, int length,
  1464. int *actual_length, unsigned int timeout);
  1465. int LIBUSB_CALL libusb_interrupt_transfer(libusb_device_handle *dev_handle,
  1466. unsigned char endpoint, unsigned char *data, int length,
  1467. int *actual_length, unsigned int timeout);
  1468. /** \ingroup desc
  1469. * Retrieve a descriptor from the default control pipe.
  1470. * This is a convenience function which formulates the appropriate control
  1471. * message to retrieve the descriptor.
  1472. *
  1473. * \param dev a device handle
  1474. * \param desc_type the descriptor type, see \ref libusb_descriptor_type
  1475. * \param desc_index the index of the descriptor to retrieve
  1476. * \param data output buffer for descriptor
  1477. * \param length size of data buffer
  1478. * \returns number of bytes returned in data, or LIBUSB_ERROR code on failure
  1479. */
  1480. static inline int libusb_get_descriptor(libusb_device_handle *dev,
  1481. uint8_t desc_type, uint8_t desc_index, unsigned char *data, int length)
  1482. {
  1483. return libusb_control_transfer(dev, LIBUSB_ENDPOINT_IN,
  1484. LIBUSB_REQUEST_GET_DESCRIPTOR, (uint16_t) ((desc_type << 8) | desc_index),
  1485. 0, data, (uint16_t) length, 1000);
  1486. }
  1487. /** \ingroup desc
  1488. * Retrieve a descriptor from a device.
  1489. * This is a convenience function which formulates the appropriate control
  1490. * message to retrieve the descriptor. The string returned is Unicode, as
  1491. * detailed in the USB specifications.
  1492. *
  1493. * \param dev a device handle
  1494. * \param desc_index the index of the descriptor to retrieve
  1495. * \param langid the language ID for the string descriptor
  1496. * \param data output buffer for descriptor
  1497. * \param length size of data buffer
  1498. * \returns number of bytes returned in data, or LIBUSB_ERROR code on failure
  1499. * \see libusb_get_string_descriptor_ascii()
  1500. */
  1501. static inline int libusb_get_string_descriptor(libusb_device_handle *dev,
  1502. uint8_t desc_index, uint16_t langid, unsigned char *data, int length)
  1503. {
  1504. return libusb_control_transfer(dev, LIBUSB_ENDPOINT_IN,
  1505. LIBUSB_REQUEST_GET_DESCRIPTOR, (uint16_t)((LIBUSB_DT_STRING << 8) | desc_index),
  1506. langid, data, (uint16_t) length, 1000);
  1507. }
  1508. int LIBUSB_CALL libusb_get_string_descriptor_ascii(libusb_device_handle *dev,
  1509. uint8_t desc_index, unsigned char *data, int length);
  1510. /* polling and timeouts */
  1511. int LIBUSB_CALL libusb_try_lock_events(libusb_context *ctx);
  1512. void LIBUSB_CALL libusb_lock_events(libusb_context *ctx);
  1513. void LIBUSB_CALL libusb_unlock_events(libusb_context *ctx);
  1514. int LIBUSB_CALL libusb_event_handling_ok(libusb_context *ctx);
  1515. int LIBUSB_CALL libusb_event_handler_active(libusb_context *ctx);
  1516. void LIBUSB_CALL libusb_lock_event_waiters(libusb_context *ctx);
  1517. void LIBUSB_CALL libusb_unlock_event_waiters(libusb_context *ctx);
  1518. int LIBUSB_CALL libusb_wait_for_event(libusb_context *ctx, struct timeval *tv);
  1519. int LIBUSB_CALL libusb_handle_events_timeout(libusb_context *ctx,
  1520. struct timeval *tv);
  1521. int LIBUSB_CALL libusb_handle_events_timeout_completed(libusb_context *ctx,
  1522. struct timeval *tv, int *completed);
  1523. int LIBUSB_CALL libusb_handle_events(libusb_context *ctx);
  1524. int LIBUSB_CALL libusb_handle_events_completed(libusb_context *ctx, int *completed);
  1525. int LIBUSB_CALL libusb_handle_events_locked(libusb_context *ctx,
  1526. struct timeval *tv);
  1527. int LIBUSB_CALL libusb_pollfds_handle_timeouts(libusb_context *ctx);
  1528. int LIBUSB_CALL libusb_get_next_timeout(libusb_context *ctx,
  1529. struct timeval *tv);
  1530. /** \ingroup poll
  1531. * File descriptor for polling
  1532. */
  1533. struct libusb_pollfd {
  1534. /** Numeric file descriptor */
  1535. int fd;
  1536. /** Event flags to poll for from <poll.h>. POLLIN indicates that you
  1537. * should monitor this file descriptor for becoming ready to read from,
  1538. * and POLLOUT indicates that you should monitor this file descriptor for
  1539. * nonblocking write readiness. */
  1540. short events;
  1541. };
  1542. /** \ingroup poll
  1543. * Callback function, invoked when a new file descriptor should be added
  1544. * to the set of file descriptors monitored for events.
  1545. * \param fd the new file descriptor
  1546. * \param events events to monitor for, see \ref libusb_pollfd for a
  1547. * description
  1548. * \param user_data User data pointer specified in
  1549. * libusb_set_pollfd_notifiers() call
  1550. * \see libusb_set_pollfd_notifiers()
  1551. */
  1552. typedef void (LIBUSB_CALL *libusb_pollfd_added_cb)(int fd, short events,
  1553. void *user_data);
  1554. /** \ingroup poll
  1555. * Callback function, invoked when a file descriptor should be removed from
  1556. * the set of file descriptors being monitored for events. After returning
  1557. * from this callback, do not use that file descriptor again.
  1558. * \param fd the file descriptor to stop monitoring
  1559. * \param user_data User data pointer specified in
  1560. * libusb_set_pollfd_notifiers() call
  1561. * \see libusb_set_pollfd_notifiers()
  1562. */
  1563. typedef void (LIBUSB_CALL *libusb_pollfd_removed_cb)(int fd, void *user_data);
  1564. const struct libusb_pollfd ** LIBUSB_CALL libusb_get_pollfds(
  1565. libusb_context *ctx);
  1566. void LIBUSB_CALL libusb_free_pollfds(const struct libusb_pollfd **pollfds);
  1567. void LIBUSB_CALL libusb_set_pollfd_notifiers(libusb_context *ctx,
  1568. libusb_pollfd_added_cb added_cb, libusb_pollfd_removed_cb removed_cb,
  1569. void *user_data);
  1570. /** \ingroup hotplug
  1571. * Callback handle.
  1572. *
  1573. * Callbacks handles are generated by libusb_hotplug_register_callback()
  1574. * and can be used to deregister callbacks. Callback handles are unique
  1575. * per libusb_context and it is safe to call libusb_hotplug_deregister_callback()
  1576. * on an already deregisted callback.
  1577. *
  1578. * Since version 1.0.16, \ref LIBUSB_API_VERSION >= 0x01000102
  1579. *
  1580. * For more information, see \ref hotplug.
  1581. */
  1582. typedef int libusb_hotplug_callback_handle;
  1583. /** \ingroup hotplug
  1584. *
  1585. * Since version 1.0.16, \ref LIBUSB_API_VERSION >= 0x01000102
  1586. *
  1587. * Flags for hotplug events */
  1588. typedef enum {
  1589. /** Default value when not using any flags. */
  1590. LIBUSB_HOTPLUG_NO_FLAGS = 0,
  1591. /** Arm the callback and fire it for all matching currently attached devices. */
  1592. LIBUSB_HOTPLUG_ENUMERATE = 1<<0,
  1593. } libusb_hotplug_flag;
  1594. /** \ingroup hotplug
  1595. *
  1596. * Since version 1.0.16, \ref LIBUSB_API_VERSION >= 0x01000102
  1597. *
  1598. * Hotplug events */
  1599. typedef enum {
  1600. /** A device has been plugged in and is ready to use */
  1601. LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED = 0x01,
  1602. /** A device has left and is no longer available.
  1603. * It is the user's responsibility to call libusb_close on any handle associated with a disconnected device.
  1604. * It is safe to call libusb_get_device_descriptor on a device that has left */
  1605. LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT = 0x02,
  1606. } libusb_hotplug_event;
  1607. /** \ingroup hotplug
  1608. * Wildcard matching for hotplug events */
  1609. #define LIBUSB_HOTPLUG_MATCH_ANY -1
  1610. /** \ingroup hotplug
  1611. * Hotplug callback function type. When requesting hotplug event notifications,
  1612. * you pass a pointer to a callback function of this type.
  1613. *
  1614. * This callback may be called by an internal event thread and as such it is
  1615. * recommended the callback do minimal processing before returning.
  1616. *
  1617. * libusb will call this function later, when a matching event had happened on
  1618. * a matching device. See \ref hotplug for more information.
  1619. *
  1620. * It is safe to call either libusb_hotplug_register_callback() or
  1621. * libusb_hotplug_deregister_callback() from within a callback function.
  1622. *
  1623. * Since version 1.0.16, \ref LIBUSB_API_VERSION >= 0x01000102
  1624. *
  1625. * \param ctx context of this notification
  1626. * \param device libusb_device this event occurred on
  1627. * \param event event that occurred
  1628. * \param user_data user data provided when this callback was registered
  1629. * \returns bool whether this callback is finished processing events.
  1630. * returning 1 will cause this callback to be deregistered
  1631. */
  1632. typedef int (LIBUSB_CALL *libusb_hotplug_callback_fn)(libusb_context *ctx,
  1633. libusb_device *device,
  1634. libusb_hotplug_event event,
  1635. void *user_data);
  1636. /** \ingroup hotplug
  1637. * Register a hotplug callback function
  1638. *
  1639. * Register a callback with the libusb_context. The callback will fire
  1640. * when a matching event occurs on a matching device. The callback is
  1641. * armed until either it is deregistered with libusb_hotplug_deregister_callback()
  1642. * or the supplied callback returns 1 to indicate it is finished processing events.
  1643. *
  1644. * If the \ref LIBUSB_HOTPLUG_ENUMERATE is passed the callback will be
  1645. * called with a \ref LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED for all devices
  1646. * already plugged into the machine. Note that libusb modifies its internal
  1647. * device list from a separate thread, while calling hotplug callbacks from
  1648. * libusb_handle_events(), so it is possible for a device to already be present
  1649. * on, or removed from, its internal device list, while the hotplug callbacks
  1650. * still need to be dispatched. This means that when using \ref
  1651. * LIBUSB_HOTPLUG_ENUMERATE, your callback may be called twice for the arrival
  1652. * of the same device, once from libusb_hotplug_register_callback() and once
  1653. * from libusb_handle_events(); and/or your callback may be called for the
  1654. * removal of a device for which an arrived call was never made.
  1655. *
  1656. * Since version 1.0.16, \ref LIBUSB_API_VERSION >= 0x01000102
  1657. *
  1658. * \param[in] ctx context to register this callback with
  1659. * \param[in] events bitwise or of events that will trigger this callback. See \ref
  1660. * libusb_hotplug_event
  1661. * \param[in] flags hotplug callback flags. See \ref libusb_hotplug_flag
  1662. * \param[in] vendor_id the vendor id to match or \ref LIBUSB_HOTPLUG_MATCH_ANY
  1663. * \param[in] product_id the product id to match or \ref LIBUSB_HOTPLUG_MATCH_ANY
  1664. * \param[in] dev_class the device class to match or \ref LIBUSB_HOTPLUG_MATCH_ANY
  1665. * \param[in] cb_fn the function to be invoked on a matching event/device
  1666. * \param[in] user_data user data to pass to the callback function
  1667. * \param[out] handle pointer to store the handle of the allocated callback (can be NULL)
  1668. * \returns LIBUSB_SUCCESS on success LIBUSB_ERROR code on failure
  1669. */
  1670. int LIBUSB_CALL libusb_hotplug_register_callback(libusb_context *ctx,
  1671. libusb_hotplug_event events,
  1672. libusb_hotplug_flag flags,
  1673. int vendor_id, int product_id,
  1674. int dev_class,
  1675. libusb_hotplug_callback_fn cb_fn,
  1676. void *user_data,
  1677. libusb_hotplug_callback_handle *handle);
  1678. /** \ingroup hotplug
  1679. * Deregisters a hotplug callback.
  1680. *
  1681. * Deregister a callback from a libusb_context. This function is safe to call from within
  1682. * a hotplug callback.
  1683. *
  1684. * Since version 1.0.16, \ref LIBUSB_API_VERSION >= 0x01000102
  1685. *
  1686. * \param[in] ctx context this callback is registered with
  1687. * \param[in] handle the handle of the callback to deregister
  1688. */
  1689. void LIBUSB_CALL libusb_hotplug_deregister_callback(libusb_context *ctx,
  1690. libusb_hotplug_callback_handle handle);
  1691. #ifdef __cplusplus
  1692. }
  1693. #endif
  1694. #endif