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 funcs; 18 19 import consts; 20 import enums; 21 import structs; 22 import core.stdc.limits : INT_MAX; 23 import core.sys.posix.sys.time : timeval; 24 25 extern (C): 26 27 libusb_transfer* libusb_alloc_transfer(int iso_packets); 28 int libusb_attach_kernel_driver(libusb_device_handle *dev, int interface_number); 29 int libusb_bulk_transfer(libusb_device_handle *dev_handle, char endpoint, char *data, int length, int *actual_length, uint timeout); 30 int libusb_cancel_transfer(libusb_transfer *transfer); 31 int libusb_claim_interface(libusb_device_handle *dev, int interface_number); 32 int libusb_clear_halt(libusb_device_handle *dev, char endpoint); 33 void libusb_close(libusb_device_handle *dev_handle); 34 int libusb_control_transfer(libusb_device_handle *dev_handle,ubyte request_type, ubyte bRequest, ushort wValue, ushort wIndex, char *data, ushort wLength, uint timeout); 35 int libusb_detach_kernel_driver(libusb_device_handle *dev, int interface_number); 36 char* libusb_error_name(int errcode); 37 int libusb_event_handler_active(libusb_context *ctx); 38 int libusb_event_handling_ok(libusb_context *ctx); 39 void libusb_exit(libusb_context *ctx); 40 void libusb_free_bos_descriptor(libusb_bos_descriptor *bos); 41 void libusb_free_config_descriptor(libusb_config_descriptor *config); 42 void libusb_free_container_id_descriptor(libusb_container_id_descriptor *container_id); 43 void libusb_free_device_list(libusb_device **list, int unref_devices); 44 void libusb_free_ss_endpoint_companion_descriptor(libusb_ss_endpoint_companion_descriptor *ep_comp); 45 void libusb_free_ss_usb_device_capability_descriptor(libusb_ss_usb_device_capability_descriptor *ss_usb_device_cap); 46 void libusb_free_transfer(libusb_transfer *transfer); 47 void libusb_free_usb_2_0_extension_descriptor(libusb_usb_2_0_extension_descriptor *usb_2_0_extension); 48 int libusb_get_active_config_descriptor(libusb_device *dev, libusb_config_descriptor **config); 49 int libusb_get_bos_descriptor(libusb_device_handle *handle, libusb_bos_descriptor **bos); 50 ubyte libusb_get_bus_number(libusb_device *dev); 51 int libusb_get_config_descriptor(libusb_device *dev, ubyte config_index, libusb_config_descriptor **config); 52 int libusb_get_config_descriptor_by_value(libusb_device *dev, ubyte bConfigurationValue, libusb_config_descriptor **config); 53 int libusb_get_configuration(libusb_device_handle *dev, int *config); 54 int libusb_get_container_id_descriptor(libusb_context *ctx, libusb_bos_dev_capability_descriptor *dev_cap, libusb_container_id_descriptor **container_id); 55 libusb_device* libusb_get_device(libusb_device_handle *dev_handle); 56 ubyte libusb_get_device_address(libusb_device *dev); 57 int libusb_get_device_descriptor(libusb_device *dev, libusb_device_descriptor *desc); 58 ptrdiff_t libusb_get_device_list(libusb_context *ctx, libusb_device ***list); 59 int libusb_get_device_speed(libusb_device *dev); 60 int libusb_get_max_iso_packet_size(libusb_device *dev, char endpoint); 61 int libusb_get_max_packet_size(libusb_device *dev, char endpoint); 62 int libusb_get_next_timeout(libusb_context *ctx, timeval *tv); 63 libusb_device* libusb_get_parent(libusb_device *dev); 64 libusb_pollfd** libusb_get_pollfds(libusb_context *ctx); 65 ubyte libusb_get_port_number(libusb_device *dev); 66 int libusb_get_port_numbers(libusb_device *dev, ubyte *port_numbers, int port_numbers_len); 67 int libusb_get_port_path(libusb_context *ctx, libusb_device *dev, ubyte *path, ubyte path_length); 68 int libusb_get_ss_endpoint_companion_descriptor(libusb_context *ctx, const libusb_endpoint_descriptor *endpoint, libusb_ss_endpoint_companion_descriptor **ep_comp); 69 int libusb_get_ss_usb_device_capability_descriptor(libusb_context *ctx,libusb_bos_dev_capability_descriptor *dev_cap, libusb_ss_usb_device_capability_descriptor **ss_usb_device_cap); 70 int libusb_get_string_descriptor_ascii(libusb_device_handle *dev, ubyte desc_index, char *data, int length); 71 int libusb_get_usb_2_0_extension_descriptor(libusb_context *ctx, libusb_bos_dev_capability_descriptor *dev_cap, libusb_usb_2_0_extension_descriptor **usb_2_0_extension); 72 libusb_version *libusb_get_version(); 73 int libusb_handle_events(libusb_context *ctx); 74 int libusb_handle_events_completed(libusb_context *ctx, int *completed); 75 int libusb_handle_events_locked(libusb_context *ctx, timeval *tv); 76 int libusb_handle_events_timeout(libusb_context *ctx, timeval *tv); 77 int libusb_handle_events_timeout_completed(libusb_context *ctx, timeval *tv, int *completed); 78 int libusb_has_capability(uint capability); 79 void libusb_hotplug_deregister_callback(libusb_context *ctx, libusb_hotplug_callback_handle handle); 80 int libusb_hotplug_register_callback(libusb_context *ctx, libusb_hotplug_event events, libusb_hotplug_flag flags, int vendor_id, int product_id, int dev_class, libusb_hotplug_callback_fn cb_fn, void *user_data, libusb_hotplug_callback_handle *handle); 81 82 /** Initialize libusb */ 83 int libusb_init(libusb_context **ctx); 84 85 int libusb_interrupt_transfer(libusb_device_handle *dev_handle, char endpoint, char *data, int length, int *actual_length, uint timeout); 86 int libusb_kernel_driver_active(libusb_device_handle *dev, int interface_number); 87 void libusb_lock_event_waiters(libusb_context *ctx); 88 void libusb_lock_events(libusb_context *ctx); 89 int libusb_open(libusb_device *dev, libusb_device_handle **handle); 90 libusb_device_handle* libusb_open_device_with_vid_pid(libusb_context *ctx, ushort vendor_id, ushort product_id); 91 int libusb_pollfds_handle_timeouts(libusb_context *ctx); 92 libusb_device* libusb_ref_device(libusb_device *dev); 93 int libusb_release_interface(libusb_device_handle *dev, int interface_number); 94 int libusb_reset_device(libusb_device_handle *dev); 95 int libusb_set_auto_detach_kernel_driver(libusb_device_handle *dev, int enable); 96 int libusb_set_configuration(libusb_device_handle *dev, int configuration); 97 98 /** Set log message verbosity */ 99 void libusb_set_debug(libusb_context *ctx, int level); 100 101 int libusb_set_interface_alt_setting(libusb_device_handle *dev, int interface_number, int alternate_setting); 102 void libusb_set_pollfd_notifiers(libusb_context *ctx, libusb_pollfd_added_cb added_cb, libusb_pollfd_removed_cb removed_cb, void *user_data); 103 int libusb_setlocale(const char *locale); 104 char* libusb_strerror(int errcode); 105 int libusb_submit_transfer(libusb_transfer *transfer); 106 int libusb_try_lock_events(libusb_context *ctx); 107 void libusb_unlock_event_waiters(libusb_context *ctx); 108 void libusb_unlock_events(libusb_context *ctx); 109 void libusb_unref_device(libusb_device *dev); 110 int libusb_wait_for_event(libusb_context *ctx, timeval *tv); 111 112 113 /** 114 * Retrieve a descriptor from the default control pipe. 115 * This is a convenience function which formulates the appropriate control 116 * message to retrieve the descriptor. 117 * 118 * \param dev a device handle 119 * \param desc_type the descriptor type, see \ref libusb_descriptor_type 120 * \param desc_index the index of the descriptor to retrieve 121 * \param data output buffer for descriptor 122 * \param length size of data buffer 123 * \returns number of bytes returned in data, or LIBUSB_ERROR code on failure 124 */ 125 int libusb_get_descriptor(libusb_device_handle *dev, ubyte desc_type, ubyte desc_index, char *data, int length) { 126 return libusb_control_transfer(dev, libusb_endpoint_direction.LIBUSB_ENDPOINT_IN, 127 libusb_standard_request.LIBUSB_REQUEST_GET_DESCRIPTOR, 128 cast(ushort)((desc_type << 8) | desc_index), 129 0, 130 data, 131 cast(ushort)length, 1000); 132 } 133 134 /** 135 * Get the data section of a control transfer. This convenience function is here 136 * to remind you that the data does not start until 8 bytes into the actual 137 * buffer, as the setup packet comes first. 138 * 139 * Calling this function only makes sense from a transfer callback function, 140 * or situations where you have already allocated a suitably sized buffer at 141 * transfer->buffer. 142 * 143 * \param transfer a transfer 144 * \returns pointer to the first byte of the data section 145 */ 146 char *libusb_control_transfer_get_data(libusb_transfer *transfer) { 147 return transfer.buffer + LIBUSB_CONTROL_SETUP_SIZE; 148 } 149 150 /** 151 * Get the control setup packet of a control transfer. This convenience 152 * function is here to remind you that the control setup occupies the first 153 * 8 bytes of the transfer data buffer. 154 * 155 * Calling this function only makes sense from a transfer callback function, 156 * or situations where you have already allocated a suitably sized buffer at 157 * transfer->buffer. 158 * 159 * \param transfer a transfer 160 * \returns a casted pointer to the start of the transfer data buffer 161 */ 162 libusb_control_setup *libusb_control_transfer_get_setup(libusb_transfer *transfer) { 163 return cast(libusb_control_setup*)(cast(void *) transfer.buffer); 164 } 165 166 /** 167 * Helper function to populate the setup packet (first 8 bytes of the data 168 * buffer) for a control transfer. The wIndex, wValue and wLength values should 169 * be given in host-endian byte order. 170 * 171 * \param buffer buffer to output the setup packet into 172 * This pointer must be aligned to at least 2 bytes boundary. 173 * \param bmRequestType see the 174 * \ref libusb_control_setup::bmRequestType "bmRequestType" field of 175 * \ref libusb_control_setup 176 * \param bRequest see the 177 * \ref libusb_control_setup::bRequest "bRequest" field of 178 * \ref libusb_control_setup 179 * \param wValue see the 180 * \ref libusb_control_setup::wValue "wValue" field of 181 * \ref libusb_control_setup 182 * \param wIndex see the 183 * \ref libusb_control_setup::wIndex "wIndex" field of 184 * \ref libusb_control_setup 185 * \param wLength see the 186 * \ref libusb_control_setup::wLength "wLength" field of 187 * \ref libusb_control_setup 188 */ 189 void libusb_fill_control_setup(char *buffer, ubyte bmRequestType, 190 ubyte bRequest, ushort wValue, ushort wIndex, 191 ushort wLength) { 192 libusb_control_setup *setup = cast(libusb_control_setup *)(cast(void *) buffer); 193 setup.bmRequestType = bmRequestType; 194 setup.bRequest = bRequest; 195 setup.wValue = libusb_cpu_to_le16(wValue); 196 setup.wIndex = libusb_cpu_to_le16(wIndex); 197 setup.wLength = libusb_cpu_to_le16(wLength); 198 } 199 200 /** 201 * Retrieve a descriptor from a device. 202 * This is a convenience function which formulates the appropriate control 203 * message to retrieve the descriptor. The string returned is Unicode, as 204 * detailed in the USB specifications. 205 * 206 * \param dev a device handle 207 * \param desc_index the index of the descriptor to retrieve 208 * \param langid the language ID for the string descriptor 209 * \param data output buffer for descriptor 210 * \param length size of data buffer 211 * \returns number of bytes returned in data, or LIBUSB_ERROR code on failure 212 * \see libusb_get_string_descriptor_ascii() 213 */ 214 int libusb_get_string_descriptor(libusb_device_handle *dev, ubyte desc_index, ushort langid, char *data, int length) { 215 return libusb_control_transfer(dev, libusb_endpoint_direction.LIBUSB_ENDPOINT_IN, 216 libusb_standard_request.LIBUSB_REQUEST_GET_DESCRIPTOR, 217 cast(ushort)((libusb_descriptor_type.LIBUSB_DT_STRING << 8) | desc_index), 218 langid, 219 data, 220 cast(ushort)length, 221 1000); 222 } 223 224 /** 225 * Helper function to populate the required \ref libusb_transfer fields 226 * for a control transfer. 227 * 228 * If you pass a transfer buffer to this function, the first 8 bytes will 229 * be interpreted as a control setup packet, and the wLength field will be 230 * used to automatically populate the \ref libusb_transfer::length "length" 231 * field of the transfer. Therefore the recommended approach is: 232 * -# Allocate a suitably sized data buffer (including space for control setup) 233 * -# Call libusb_fill_control_setup() 234 * -# If this is a host-to-device transfer with a data stage, put the data 235 * in place after the setup packet 236 * -# Call this function 237 * -# Call libusb_submit_transfer() 238 * 239 * It is also legal to pass a NULL buffer to this function, in which case this 240 * function will not attempt to populate the length field. Remember that you 241 * must then populate the buffer and length fields later. 242 * 243 * \param transfer the transfer to populate 244 * \param dev_handle handle of the device that will handle the transfer 245 * \param buffer data buffer. If provided, this function will interpret the 246 * first 8 bytes as a setup packet and infer the transfer length from that. 247 * This pointer must be aligned to at least 2 bytes boundary. 248 * \param callback callback function to be invoked on transfer completion 249 * \param user_data user data to pass to callback function 250 * \param timeout timeout for the transfer in milliseconds 251 */ 252 void libusb_fill_control_transfer( 253 libusb_transfer *transfer, libusb_device_handle *dev_handle, 254 char *buffer, libusb_transfer_cb_fn callback, void *user_data, 255 uint timeout) { 256 libusb_control_setup *setup = cast(libusb_control_setup *)(cast(void *) buffer); 257 transfer.dev_handle = dev_handle; 258 transfer.endpoint = 0; 259 transfer.type = libusb_transfer_type.LIBUSB_TRANSFER_TYPE_CONTROL; 260 transfer.timeout = timeout; 261 transfer.buffer = buffer; 262 263 if (setup) { 264 transfer.length = cast(int)(LIBUSB_CONTROL_SETUP_SIZE+libusb_cpu_to_le16(setup.wLength)); 265 } 266 267 transfer.user_data = user_data; 268 transfer.callback = callback; 269 } 270 271 /** 272 * Helper function to populate the required \ref libusb_transfer fields 273 * for a bulk transfer. 274 * 275 * \param transfer the transfer to populate 276 * \param dev_handle handle of the device that will handle the transfer 277 * \param endpoint address of the endpoint where this transfer will be sent 278 * \param buffer data buffer 279 * \param length length of data buffer 280 * \param callback callback function to be invoked on transfer completion 281 * \param user_data user data to pass to callback function 282 * \param timeout timeout for the transfer in milliseconds 283 */ 284 void libusb_fill_bulk_transfer(libusb_transfer *transfer, 285 libusb_device_handle *dev_handle, char endpoint, 286 char *buffer, int length, libusb_transfer_cb_fn callback, 287 void *user_data, uint timeout) 288 { 289 transfer.dev_handle = dev_handle; 290 transfer.endpoint = endpoint; 291 transfer.type = libusb_transfer_type.LIBUSB_TRANSFER_TYPE_BULK; 292 transfer.timeout = timeout; 293 transfer.buffer = buffer; 294 transfer.length = length; 295 transfer.user_data = user_data; 296 transfer.callback = callback; 297 } 298 299 /** 300 * Helper function to populate the required \ref libusb_transfer fields 301 * for an interrupt transfer. 302 * 303 * \param transfer the transfer to populate 304 * \param dev_handle handle of the device that will handle the transfer 305 * \param endpoint address of the endpoint where this transfer will be sent 306 * \param buffer data buffer 307 * \param length length of data buffer 308 * \param callback callback function to be invoked on transfer completion 309 * \param user_data user data to pass to callback function 310 * \param timeout timeout for the transfer in milliseconds 311 */ 312 void libusb_fill_interrupt_transfer( 313 libusb_transfer *transfer, libusb_device_handle *dev_handle, 314 char endpoint, char *buffer, int length, 315 libusb_transfer_cb_fn callback, void *user_data, uint timeout) 316 { 317 transfer.dev_handle = dev_handle; 318 transfer.endpoint = endpoint; 319 transfer.type = libusb_transfer_type.LIBUSB_TRANSFER_TYPE_INTERRUPT; 320 transfer.timeout = timeout; 321 transfer.buffer = buffer; 322 transfer.length = length; 323 transfer.user_data = user_data; 324 transfer.callback = callback; 325 } 326 327 /** 328 * Helper function to populate the required \ref libusb_transfer fields 329 * for an isochronous transfer. 330 * 331 * \param transfer the transfer to populate 332 * \param dev_handle handle of the device that will handle the transfer 333 * \param endpoint address of the endpoint where this transfer will be sent 334 * \param buffer data buffer 335 * \param length length of data buffer 336 * \param num_iso_packets the number of isochronous packets 337 * \param callback callback function to be invoked on transfer completion 338 * \param user_data user data to pass to callback function 339 * \param timeout timeout for the transfer in milliseconds 340 */ 341 void libusb_fill_iso_transfer(libusb_transfer *transfer, 342 libusb_device_handle *dev_handle, char endpoint, 343 char *buffer, int length, int num_iso_packets, 344 libusb_transfer_cb_fn callback, void *user_data, uint timeout) 345 { 346 transfer.dev_handle = dev_handle; 347 transfer.endpoint = endpoint; 348 transfer.type = libusb_transfer_type.LIBUSB_TRANSFER_TYPE_ISOCHRONOUS; 349 transfer.timeout = timeout; 350 transfer.buffer = buffer; 351 transfer.length = length; 352 transfer.num_iso_packets = num_iso_packets; 353 transfer.user_data = user_data; 354 transfer.callback = callback; 355 } 356 357 /** 358 * Convenience function to set the length of all packets in an isochronous 359 * transfer, based on the num_iso_packets field in the transfer structure. 360 * 361 * \param transfer a transfer 362 * \param length the length to set in each isochronous packet descriptor 363 * \see libusb_get_max_packet_size() 364 */ 365 void libusb_set_iso_packet_lengths(libusb_transfer *transfer, uint length) { 366 int i; 367 for (i = 0; i < transfer.num_iso_packets; i++) { 368 transfer.iso_packet_desc[i].length = length; 369 } 370 } 371 372 /** 373 * Convenience function to locate the position of an isochronous packet 374 * within the buffer of an isochronous transfer. 375 * 376 * This is a thorough function which loops through all preceding packets, 377 * accumulating their lengths to find the position of the specified packet. 378 * Typically you will assign equal lengths to each packet in the transfer, 379 * and hence the above method is sub-optimal. You may wish to use 380 * libusb_get_iso_packet_buffer_simple() instead. 381 * 382 * \param transfer a transfer 383 * \param packet the packet to return the address of 384 * \returns the base address of the packet buffer inside the transfer buffer, 385 * or NULL if the packet does not exist. 386 * \see libusb_get_iso_packet_buffer_simple() 387 */ 388 char *libusb_get_iso_packet_buffer(libusb_transfer *transfer, uint packet) { 389 int i; 390 size_t offset = 0; 391 int _packet; 392 393 /* oops..slight bug in the API. packet is an unsigned int, but we use 394 * signed integers almost everywhere else. range-check and convert to 395 * signed to avoid compiler warnings. FIXME for libusb-2. */ 396 if (packet > INT_MAX) { return null; } 397 398 _packet = cast(int)packet; 399 400 if (_packet >= transfer.num_iso_packets) { return null; } 401 402 for (i = 0; i < _packet; i++) { 403 offset += transfer.iso_packet_desc[i].length; 404 } 405 406 return transfer.buffer + offset; 407 } 408 409 410 /** 411 * Convenience function to locate the position of an isochronous packet 412 * within the buffer of an isochronous transfer, for transfers where each 413 * packet is of identical size. 414 * 415 * This function relies on the assumption that every packet within the transfer 416 * is of identical size to the first packet. Calculating the location of 417 * the packet buffer is then just a simple calculation: 418 * <tt>buffer + (packet_size * packet)</tt> 419 * 420 * Do not use this function on transfers other than those that have identical 421 * packet lengths for each packet. 422 * 423 * \param transfer a transfer 424 * \param packet the packet to return the address of 425 * \returns the base address of the packet buffer inside the transfer buffer, 426 * or NULL if the packet does not exist. 427 * \see libusb_get_iso_packet_buffer() 428 */ 429 char *libusb_get_iso_packet_buffer_simple(libusb_transfer *transfer, uint packet) { 430 int _packet; 431 432 /* oops..slight bug in the API. packet is an unsigned int, but we use 433 * signed integers almost everywhere else. range-check and convert to 434 * signed to avoid compiler warnings. FIXME for libusb-2. */ 435 if (packet > INT_MAX) 436 { return null; } 437 438 _packet = cast(int)packet; 439 440 if (_packet >= transfer.num_iso_packets) { return null; } 441 442 return transfer.buffer + (cast(int)(transfer.iso_packet_desc[0].length * _packet)); 443 } 444 445 ushort libusb_cpu_to_le16(const ushort x) { 446 union _tmp_union { 447 ubyte[2] b8; 448 ushort b16; 449 } 450 451 _tmp_union _tmp; 452 453 _tmp.b8[1] = cast(ubyte)(x >> 8); 454 _tmp.b8[0] = cast(ubyte)(x & 0xff); 455 return _tmp.b16; 456 } 457 458 alias libusb_transfer_cb_fn = void function(libusb_transfer *transfer); 459 alias libusb_pollfd_added_cb = void function(int fd, short events, void *user_data); 460 alias libusb_pollfd_removed_cb = void function(int fd, void *user_data); 461 alias libusb_hotplug_callback_handle = int; 462 alias libusb_hotplug_callback_fn = int function(libusb_context *ctx, libusb_device *device, libusb_hotplug_event event, void *user_data);