1 /**
2  * Converted/butchered LIBUSB header to a D-Language interface
3  *
4  * Authors: Ross Lonstein <lonstein@brightboxcharge.com>
5  * License: LGPL
6  * Copyright: 2015, Brightbox, Inc.
7  * See_Also: http://www.libusb.org
8  * Notes: Libusb itself is Copyright
9  * $(UL
10  *   $(LI 2001 Johannes Erdfelt <johannes@erdfelt.com>)
11  *   $(LI 2007-2008 Daniel Drake <dsd@gentoo.org>)
12  *   $(LI 2012 Pete Batard <pete@akeo.ie>)
13  *   $(LI 2012 Nathan Hjelm <hjelmn@cs.unm.edu>) )
14  *
15  */
16 
17 module structs;
18 
19 import enums;
20 
21 extern (C):
22 
23 alias libusb_transfer_cb_fn = void function(libusb_transfer *transfer);
24 
25 /**
26  * A structure representing the standard USB device descriptor. This
27  * descriptor is documented in section 9.6.1 of the USB 3.0 specification.
28  * All multiple-byte fields are represented in host-endian format.
29  */
30 struct libusb_device_descriptor
31 {
32     /** Size of this descriptor (in bytes) */
33     ubyte  bLength;
34 
35     /** Descriptor type. Will have value
36      * \ref libusb_descriptor_type::LIBUSB_DT_DEVICE LIBUSB_DT_DEVICE in this
37      * context. */
38     ubyte  bDescriptorType;
39 
40     /** USB specification release number in binary-coded decimal. A value of
41      * 0x0200 indicates USB 2.0, 0x0110 indicates USB 1.1, etc. */
42     ushort bcdUSB;
43 
44     /** USB-IF class code for the device. See \ref libusb_class_code. */
45     ubyte  bDeviceClass;
46 
47     /** USB-IF subclass code for the device, qualified by the bDeviceClass
48      * value */
49     ubyte  bDeviceSubClass;
50 
51     /** USB-IF protocol code for the device, qualified by the bDeviceClass and
52      * bDeviceSubClass values */
53     ubyte  bDeviceProtocol;
54 
55     /** Maximum packet size for endpoint 0 */
56     ubyte  bMaxPacketSize0;
57 
58     /** USB-IF vendor ID */
59     ushort idVendor;
60 
61     /** USB-IF product ID */
62     ushort idProduct;
63 
64     /** Device release number in binary-coded decimal */
65     ushort bcdDevice;
66 
67     /** Index of string descriptor describing manufacturer */
68     ubyte  iManufacturer;
69 
70     /** Index of string descriptor describing product */
71     ubyte  iProduct;
72 
73     /** Index of string descriptor containing device serial number */
74     ubyte  iSerialNumber;
75 
76     /** Number of possible configurations */
77     ubyte  bNumConfigurations;
78 };
79 
80 /**
81  * A structure representing the standard USB endpoint descriptor. This
82  * descriptor is documented in section 9.6.6 of the USB 3.0 specification.
83  * All multiple-byte fields are represented in host-endian format.
84  */
85 struct libusb_endpoint_descriptor
86 {
87     /** Size of this descriptor (in bytes) */
88     ubyte  bLength;
89 
90     /** Descriptor type. Will have value
91      * \ref libusb_descriptor_type::LIBUSB_DT_ENDPOINT LIBUSB_DT_ENDPOINT in
92      * this context. */
93     ubyte  bDescriptorType;
94 
95     /** The address of the endpoint described by this descriptor. Bits 0:3 are
96      * the endpoint number. Bits 4:6 are reserved. Bit 7 indicates direction,
97      * see \ref libusb_endpoint_direction.
98      */
99     ubyte  bEndpointAddress;
100 
101     /** Attributes which apply to the endpoint when it is configured using
102      * the bConfigurationValue. Bits 0:1 determine the transfer type and
103      * correspond to \ref libusb_transfer_type. Bits 2:3 are only used for
104      * isochronous endpoints and correspond to \ref libusb_iso_sync_type.
105      * Bits 4:5 are also only used for isochronous endpoints and correspond to
106      * \ref libusb_iso_usage_type. Bits 6:7 are reserved.
107      */
108     ubyte  bmAttributes;
109 
110     /** Maximum packet size this endpoint is capable of sending/receiving. */
111     ushort wMaxPacketSize;
112 
113     /** Interval for polling endpoint for data transfers. */
114     ubyte  bInterval;
115 
116     /** For audio devices only: the rate at which synchronization feedback
117      * is provided. */
118     ubyte  bRefresh;
119 
120     /** For audio devices only: the address if the synch endpoint */
121     ubyte  bSynchAddress;
122 
123     /** Extra descriptors. If libusbx encounters unknown endpoint descriptors,
124      * it will store them here, should you wish to parse them. */
125     const char *extra;
126 
127     /** Length of the extra descriptors, in bytes. */
128     int extra_length;
129 };
130 
131 /**
132  * A structure representing the standard USB interface descriptor. This
133  * descriptor is documented in section 9.6.5 of the USB 3.0 specification.
134  * All multiple-byte fields are represented in host-endian format.
135  */
136 struct libusb_interface_descriptor
137 {
138     /** Size of this descriptor (in bytes) */
139     ubyte  bLength;
140 
141     /** Descriptor type. Will have value
142      * \ref libusb_descriptor_type::LIBUSB_DT_INTERFACE LIBUSB_DT_INTERFACE
143      * in this context. */
144     ubyte  bDescriptorType;
145 
146     /** Number of this interface */
147     ubyte  bInterfaceNumber;
148 
149     /** Value used to select this alternate setting for this interface */
150     ubyte  bAlternateSetting;
151 
152     /** Number of endpoints used by this interface (excluding the control
153      * endpoint). */
154     ubyte  bNumEndpoints;
155 
156     /** USB-IF class code for this interface. See \ref libusb_class_code. */
157     ubyte  bInterfaceClass;
158 
159     /** USB-IF subclass code for this interface, qualified by the
160      * bInterfaceClass value */
161     ubyte  bInterfaceSubClass;
162 
163     /** USB-IF protocol code for this interface, qualified by the
164      * bInterfaceClass and bInterfaceSubClass values */
165     ubyte  bInterfaceProtocol;
166 
167     /** Index of string descriptor describing this interface */
168     ubyte  iInterface;
169 
170     /** Array of endpoint descriptors. This length of this array is determined
171      * by the bNumEndpoints field. */
172     const libusb_endpoint_descriptor *endpoint;
173 
174     /** Extra descriptors. If libusbx encounters unknown interface descriptors,
175      * it will store them here, should you wish to parse them. */
176     const char *extra;
177 
178     /** Length of the extra descriptors, in bytes. */
179     int extra_length;
180 };
181 
182 /**
183  * A collection of alternate settings for a particular USB interface.
184  */
185 struct libusb_interface {
186   /** Array of interface descriptors. The length of this array is determined
187    * by the num_altsetting field. */
188   libusb_interface_descriptor *altsetting;
189 
190   /** The number of alternate settings that belong to this interface */
191   int num_altsetting;
192 };
193 
194 
195 /**
196  * A structure representing the standard USB configuration descriptor. This
197  * descriptor is documented in section 9.6.3 of the USB 3.0 specification.
198  * All multiple-byte fields are represented in host-endian format.
199  */
200 struct libusb_config_descriptor {
201     /** Size of this descriptor (in bytes) */
202     ubyte  bLength;
203 
204     /** Descriptor type. Will have value
205      * \ref libusb_descriptor_type::LIBUSB_DT_CONFIG LIBUSB_DT_CONFIG
206      * in this context. */
207     ubyte  bDescriptorType;
208 
209     /** Total length of data returned for this configuration */
210     ushort wTotalLength;
211 
212     /** Number of interfaces supported by this configuration */
213     ubyte  bNumInterfaces;
214 
215     /** Identifier value for this configuration */
216     ubyte  bConfigurationValue;
217 
218     /** Index of string descriptor describing this configuration */
219     ubyte  iConfiguration;
220 
221     /** Configuration characteristics */
222     ubyte  bmAttributes;
223 
224     /** Maximum power consumption of the USB device from this bus in this
225      * configuration when the device is fully opreation. Expressed in units
226      * of 2 mA. */
227     ubyte  MaxPower;
228 
229     /** Array of interfaces supported by this configuration. The length of
230      * this array is determined by the bNumInterfaces field. */
231     const libusb_interface *iface; // REPLACE 'interface' keyword with 'iface'
232 
233     /** Extra descriptors. If libusbx encounters unknown configuration
234      * descriptors, it will store them here, should you wish to parse them. */
235     const char *extra;
236 
237     /** Length of the extra descriptors, in bytes. */
238     int extra_length;
239 };
240 
241 /**
242  * A structure representing the superspeed endpoint companion
243  * descriptor. This descriptor is documented in section 9.6.7 of
244  * the USB 3.0 specification. All multiple-byte fields are represented in
245  * host-endian format.
246  */
247 struct libusb_ss_endpoint_companion_descriptor
248 {
249 
250     /** Size of this descriptor (in bytes) */
251     ubyte  bLength;
252 
253     /** Descriptor type. Will have value
254      * \ref libusb_descriptor_type::LIBUSB_DT_SS_ENDPOINT_COMPANION in
255      * this context. */
256     ubyte  bDescriptorType;
257 
258 
259     /** The maximum number of packets the endpoint can send or
260      *  recieve as part of a burst. */
261     ubyte  bMaxBurst;
262 
263     /** In bulk EP: bits 4:0 represents the maximum number of
264      *  streams the EP supports. In isochronous EP: bits 1:0
265      *  represents the Mult - a zero based value that determines
266      *  the maximum number of packets within a service interval  */
267     ubyte  bmAttributes;
268 
269     /** The total number of bytes this EP will transfer every
270      *  service interval. valid only for periodic EPs. */
271     ushort wBytesPerInterval;
272 };
273 
274 /**
275  * A generic representation of a BOS Device Capability descriptor. It is
276  * advised to check bDevCapabilityType and call the matching
277  * libusb_get_*_descriptor function to get a structure fully matching the type.
278  */
279 struct libusb_bos_dev_capability_descriptor
280 {
281     /** Size of this descriptor (in bytes) */
282     ubyte bLength;
283     /** Descriptor type. Will have value
284      * \ref libusb_descriptor_type::LIBUSB_DT_DEVICE_CAPABILITY
285      * LIBUSB_DT_DEVICE_CAPABILITY in this context. */
286     ubyte bDescriptorType;
287     /** Device Capability type */
288     ubyte bDevCapabilityType;
289     /** Device Capability data (bLength - 3 bytes) */
290     ubyte[] dev_capability_data;
291 };
292 
293 /**
294  * A structure representing the Binary Device Object Store (BOS) descriptor.
295  * This descriptor is documented in section 9.6.2 of the USB 3.0 specification.
296  * All multiple-byte fields are represented in host-endian format.
297  */
298 struct libusb_bos_descriptor
299 {
300     /** Size of this descriptor (in bytes) */
301     ubyte  bLength;
302 
303     /** Descriptor type. Will have value
304      * \ref libusb_descriptor_type::LIBUSB_DT_BOS LIBUSB_DT_BOS
305      * in this context. */
306     ubyte  bDescriptorType;
307 
308     /** Length of this descriptor and all of its sub descriptors */
309     ushort wTotalLength;
310 
311     /** The number of separate device capability descriptors in
312      * the BOS */
313     ubyte  bNumDeviceCaps;
314 
315     /** bNumDeviceCap Device Capability Descriptors */
316     libusb_bos_dev_capability_descriptor[] *dev_capability;
317 };
318 
319 /**
320  * A structure representing the USB 2.0 Extension descriptor
321  * This descriptor is documented in section 9.6.2.1 of the USB 3.0 specification.
322  * All multiple-byte fields are represented in host-endian format.
323  */
324 struct libusb_usb_2_0_extension_descriptor
325 {
326     /** Size of this descriptor (in bytes) */
327     ubyte  bLength;
328 
329     /** Descriptor type. Will have value
330      * \ref libusb_descriptor_type::LIBUSB_DT_DEVICE_CAPABILITY
331      * LIBUSB_DT_DEVICE_CAPABILITY in this context. */
332     ubyte  bDescriptorType;
333 
334     /** Capability type. Will have value
335      * \ref libusb_capability_type::LIBUSB_BT_USB_2_0_EXTENSION
336      * LIBUSB_BT_USB_2_0_EXTENSION in this context. */
337     ubyte  bDevCapabilityType;
338 
339     /** Bitmap encoding of supported device level features.
340      * A value of one in a bit location indicates a feature is
341      * supported; a value of zero indicates it is not supported.
342      * See \ref libusb_usb_2_0_extension_attributes. */
343     uint  bmAttributes;
344 };
345 
346 /**
347  * A structure representing the SuperSpeed USB Device Capability descriptor
348  * This descriptor is documented in section 9.6.2.2 of the USB 3.0 specification.
349  * All multiple-byte fields are represented in host-endian format.
350  */
351 struct libusb_ss_usb_device_capability_descriptor
352 {
353     /** Size of this descriptor (in bytes) */
354     ubyte  bLength;
355 
356     /** Descriptor type. Will have value
357      * \ref libusb_descriptor_type::LIBUSB_DT_DEVICE_CAPABILITY
358      * LIBUSB_DT_DEVICE_CAPABILITY in this context. */
359     ubyte  bDescriptorType;
360 
361     /** Capability type. Will have value
362      * \ref libusb_capability_type::LIBUSB_BT_SS_USB_DEVICE_CAPABILITY
363      * LIBUSB_BT_SS_USB_DEVICE_CAPABILITY in this context. */
364     ubyte  bDevCapabilityType;
365 
366     /** Bitmap encoding of supported device level features.
367      * A value of one in a bit location indicates a feature is
368      * supported; a value of zero indicates it is not supported.
369      * See \ref libusb_ss_usb_device_capability_attributes. */
370     ubyte  bmAttributes;
371 
372     /** Bitmap encoding of the speed supported by this device when
373      * operating in SuperSpeed mode. See \ref libusb_supported_speed. */
374     ushort wSpeedSupported;
375 
376     /** The lowest speed at which all the functionality supported
377      * by the device is available to the user. For example if the
378      * device supports all its functionality when connected at
379      * full speed and above then it sets this value to 1. */
380     ubyte  bFunctionalitySupport;
381 
382     /** U1 Device Exit Latency. */
383     ubyte  bU1DevExitLat;
384 
385     /** U2 Device Exit Latency. */
386     ushort bU2DevExitLat;
387 };
388 
389 /**
390  * A structure representing the Container ID descriptor.
391  * This descriptor is documented in section 9.6.2.3 of the USB 3.0 specification.
392  * All multiple-byte fields, except UUIDs, are represented in host-endian format.
393  */
394 struct libusb_container_id_descriptor
395 {
396     /** Size of this descriptor (in bytes) */
397     ubyte  bLength;
398 
399     /** Descriptor type. Will have value
400      * \ref libusb_descriptor_type::LIBUSB_DT_DEVICE_CAPABILITY
401      * LIBUSB_DT_DEVICE_CAPABILITY in this context. */
402     ubyte  bDescriptorType;
403 
404     /** Capability type. Will have value
405      * \ref libusb_capability_type::LIBUSB_BT_CONTAINER_ID
406      * LIBUSB_BT_CONTAINER_ID in this context. */
407     ubyte  bDevCapabilityType;
408 
409     /** Reserved field */
410     ubyte bReserved;
411 
412     /** 128 bit UUID */
413     ubyte[16] ContainerID;
414 };
415 
416 /**
417  * Setup packet for control transfers. */
418 struct libusb_control_setup
419 {
420     /** Request type. Bits 0:4 determine recipient, see
421      * \ref libusb_request_recipient. Bits 5:6 determine type, see
422      * \ref libusb_request_type. Bit 7 determines data transfer direction, see
423      * \ref libusb_endpoint_direction.
424      */
425     ubyte  bmRequestType;
426 
427     /** Request. If the type bits of bmRequestType are equal to
428      * \ref libusb_request_type::LIBUSB_REQUEST_TYPE_STANDARD
429      * "LIBUSB_REQUEST_TYPE_STANDARD" then this field refers to
430      * \ref libusb_standard_request. For other cases, use of this field is
431      * application-specific. */
432     ubyte  bRequest;
433 
434     /** Value. Varies according to request */
435     ushort wValue;
436 
437     /** Index. Varies according to request, typically used to pass an index
438      * or offset */
439     ushort wIndex;
440 
441     /** Number of bytes to transfer */
442     ushort wLength;
443 };
444 
445 /* libusbx */
446 
447 struct libusb_context;
448 struct libusb_device;
449 struct libusb_device_handle;
450 struct libusb_hotplug_callback;
451 
452 /**
453  * Structure providing the version of the libusbx runtime
454  */
455 struct libusb_version {
456   const ushort major;   /** Library major version. */
457   const ushort minor;   /** Library minor version. */
458   const ushort micro;   /** Library micro version. */
459   const ushort nano;    /** Library nano version. */
460   const char *rc;       /** Library release candidate suffix string, e.g. "-rc4". */
461   const char *describe; /** For ABI compatibility only. */
462 };
463 
464 
465 /**
466  * Isochronous packet descriptor. */
467 struct libusb_iso_packet_descriptor
468 {
469     /** Length of data to request in this packet */
470     uint length;
471 
472     /** Amount of data that was actually transferred */
473     uint actual_length;
474 
475     /** Status code for this packet */
476     libusb_transfer_status status;
477 };
478 
479 /**
480  * The generic USB transfer structure. The user populates this structure and
481  * then submits it in order to request a transfer. After the transfer has
482  * completed, the library populates the transfer with the results and passes
483  * it back to the user.
484  */
485 struct libusb_transfer {
486     /** Handle of the device that this transfer will be submitted to */
487     libusb_device_handle *dev_handle;
488 
489     /** A bitwise OR combination of \ref libusb_transfer_flags. */
490     ubyte flags;
491 
492     /** Address of the endpoint where this transfer will be sent. */
493     char endpoint;
494 
495     /** Type of the endpoint from \ref libusb_transfer_type */
496     char type;
497 
498     /** Timeout for this transfer in millseconds. A value of 0 indicates no
499      * timeout. */
500     uint timeout;
501 
502     /** The status of the transfer. Read-only, and only for use within
503      * transfer callback function.
504      *
505      * If this is an isochronous transfer, this field may read COMPLETED even
506      * if there were errors in the frames. Use the
507      * \ref libusb_iso_packet_descriptor::status "status" field in each packet
508      * to determine if errors occurred. */
509     libusb_transfer_status status;
510 
511     /** Length of the data buffer */
512     int length;
513 
514     /** Actual length of data that was transferred. Read-only, and only for
515      * use within transfer callback function. Not valid for isochronous
516      * endpoint transfers. */
517     int actual_length;
518 
519     /** Callback function. This will be invoked when the transfer completes,
520      * fails, or is cancelled. */
521     libusb_transfer_cb_fn callback;
522 
523     /** User context data to pass to the callback function. */
524     void *user_data;
525 
526     /** Data buffer */
527     char *buffer;
528 
529     /** Number of isochronous packets. Only used for I/O with isochronous
530      * endpoints. */
531     int num_iso_packets;
532 
533     /** Isochronous packet descriptors, for isochronous transfers only. */
534     libusb_iso_packet_descriptor[] iso_packet_desc;
535 };
536 
537 
538 /**
539  * File descriptor for polling
540  */
541 struct libusb_pollfd
542 {
543     /** Numeric file descriptor */
544     int fd;
545 
546     /** Event flags to poll for from <poll.h>. POLLIN indicates that you
547      * should monitor this file descriptor for becoming ready to read from,
548      * and POLLOUT indicates that you should monitor this file descriptor for
549      * nonblocking write readiness. */
550     short events;
551 };
552