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