25 #include <winpr/wtypes.h>
26 #include <winpr/sysinfo.h>
27 #include <winpr/collections.h>
31 #include "libusb_udevice.h"
32 #include "../common/urbdrc_types.h"
34 #define BASIC_STATE_FUNC_DEFINED(_arg, _type) \
35 static _type udev_get_##_arg(IUDEVICE* idev) \
37 UDEVICE* pdev = (UDEVICE*)idev; \
40 static void udev_set_##_arg(IUDEVICE* idev, _type _t) \
42 UDEVICE* pdev = (UDEVICE*)idev; \
46 #define BASIC_POINT_FUNC_DEFINED(_arg, _type) \
47 static _type udev_get_p_##_arg(IUDEVICE* idev) \
49 UDEVICE* pdev = (UDEVICE*)idev; \
52 static void udev_set_p_##_arg(IUDEVICE* idev, _type _t) \
54 UDEVICE* pdev = (UDEVICE*)idev; \
58 #define BASIC_STATE_FUNC_REGISTER(_arg, _dev) \
59 _dev->iface.get_##_arg = udev_get_##_arg; \
60 (_dev)->iface.set_##_arg = udev_set_##_arg
62 #if LIBUSB_API_VERSION >= 0x01000103
63 #define HAVE_STREAM_ID_API 1
74 UINT32 OutputBufferSize;
76 t_isoch_transfer_cb cb;
78 #if !defined(HAVE_STREAM_ID_API)
81 } ASYNC_TRANSFER_USER_DATA;
83 static void request_free(
void* value);
85 static struct libusb_transfer* list_contains(wArrayList* list, UINT32 streamID)
90 count = ArrayList_Count(list);
91 for (
size_t x = 0; x < count; x++)
93 struct libusb_transfer* transfer = ArrayList_GetItem(list, x);
95 #if defined(HAVE_STREAM_ID_API)
96 const UINT32 currentID = libusb_transfer_get_stream_id(transfer);
98 const ASYNC_TRANSFER_USER_DATA* user_data = (ASYNC_TRANSFER_USER_DATA*)transfer->user_data;
99 const UINT32 currentID = user_data->streamID;
101 if (currentID == streamID)
107 static UINT32 stream_id_from_buffer(
struct libusb_transfer* transfer)
111 #if defined(HAVE_STREAM_ID_API)
112 return libusb_transfer_get_stream_id(transfer);
114 ASYNC_TRANSFER_USER_DATA* user_data = (ASYNC_TRANSFER_USER_DATA*)transfer->user_data;
117 return user_data->streamID;
121 static void set_stream_id_for_buffer(
struct libusb_transfer* transfer, UINT32 streamID)
123 #if defined(HAVE_STREAM_ID_API)
124 libusb_transfer_set_stream_id(transfer, streamID);
126 ASYNC_TRANSFER_USER_DATA* user_data = (ASYNC_TRANSFER_USER_DATA*)transfer->user_data;
129 user_data->streamID = streamID;
133 WINPR_ATTR_FORMAT_ARG(3, 8)
134 static BOOL log_libusb_result_(wLog* log, DWORD lvl, WINPR_FORMAT_ARG const
char* fmt,
135 const
char* fkt, const
char* file,
size_t line,
int error, ...)
141 char buffer[8192] = { 0 };
144 (void)vsnprintf(buffer,
sizeof(buffer), fmt, ap);
147 WLog_Print(log, lvl,
"[%s:%" PRIuz
"]: %s: error %s[%d]", fkt, line, buffer,
148 libusb_error_name(error), error);
154 #define log_libusb_result(log, lvl, fmt, error, ...) \
155 log_libusb_result_((log), (lvl), (fmt), __func__, __FILE__, __LINE__, error, ##__VA_ARGS__)
157 const char* usb_interface_class_to_string(uint8_t
class)
161 case LIBUSB_CLASS_PER_INTERFACE:
162 return "LIBUSB_CLASS_PER_INTERFACE";
163 case LIBUSB_CLASS_AUDIO:
164 return "LIBUSB_CLASS_AUDIO";
165 case LIBUSB_CLASS_COMM:
166 return "LIBUSB_CLASS_COMM";
167 case LIBUSB_CLASS_HID:
168 return "LIBUSB_CLASS_HID";
169 case LIBUSB_CLASS_PHYSICAL:
170 return "LIBUSB_CLASS_PHYSICAL";
171 case LIBUSB_CLASS_PRINTER:
172 return "LIBUSB_CLASS_PRINTER";
173 case LIBUSB_CLASS_IMAGE:
174 return "LIBUSB_CLASS_IMAGE";
175 case LIBUSB_CLASS_MASS_STORAGE:
176 return "LIBUSB_CLASS_MASS_STORAGE";
177 case LIBUSB_CLASS_HUB:
178 return "LIBUSB_CLASS_HUB";
179 case LIBUSB_CLASS_DATA:
180 return "LIBUSB_CLASS_DATA";
181 case LIBUSB_CLASS_SMART_CARD:
182 return "LIBUSB_CLASS_SMART_CARD";
183 case LIBUSB_CLASS_CONTENT_SECURITY:
184 return "LIBUSB_CLASS_CONTENT_SECURITY";
185 case LIBUSB_CLASS_VIDEO:
186 return "LIBUSB_CLASS_VIDEO";
187 case LIBUSB_CLASS_PERSONAL_HEALTHCARE:
188 return "LIBUSB_CLASS_PERSONAL_HEALTHCARE";
189 case LIBUSB_CLASS_DIAGNOSTIC_DEVICE:
190 return "LIBUSB_CLASS_DIAGNOSTIC_DEVICE";
191 case LIBUSB_CLASS_WIRELESS:
192 return "LIBUSB_CLASS_WIRELESS";
193 case LIBUSB_CLASS_APPLICATION:
194 return "LIBUSB_CLASS_APPLICATION";
195 case LIBUSB_CLASS_VENDOR_SPEC:
196 return "LIBUSB_CLASS_VENDOR_SPEC";
198 return "UNKNOWN_DEVICE_CLASS";
202 static ASYNC_TRANSFER_USER_DATA* async_transfer_user_data_new(IUDEVICE* idev, UINT32 MessageId,
203 size_t offset,
size_t BufferSize,
204 const BYTE* data,
size_t packetSize,
205 BOOL NoAck, t_isoch_transfer_cb cb,
208 ASYNC_TRANSFER_USER_DATA* user_data = NULL;
211 if (BufferSize > UINT32_MAX)
214 user_data = calloc(1,
sizeof(ASYNC_TRANSFER_USER_DATA));
218 user_data->data = Stream_New(NULL, offset + BufferSize + packetSize);
220 if (!user_data->data)
226 Stream_Seek(user_data->data, offset);
228 memcpy(Stream_Pointer(user_data->data), data, BufferSize);
230 user_data->OutputBufferSize = (UINT32)BufferSize;
232 user_data->noack = NoAck;
234 user_data->callback = callback;
235 user_data->idev = idev;
236 user_data->MessageId = MessageId;
238 user_data->queue = pdev->request_queue;
243 static void async_transfer_user_data_free(ASYNC_TRANSFER_USER_DATA* user_data)
247 Stream_Free(user_data->data, TRUE);
252 static void LIBUSB_CALL func_iso_callback(
struct libusb_transfer* transfer)
254 ASYNC_TRANSFER_USER_DATA* user_data = (ASYNC_TRANSFER_USER_DATA*)transfer->user_data;
255 const UINT32 streamID = stream_id_from_buffer(transfer);
256 wArrayList* list = user_data->queue;
258 ArrayList_Lock(list);
259 switch (transfer->status)
261 case LIBUSB_TRANSFER_COMPLETED:
264 BYTE* dataStart = Stream_Pointer(user_data->data);
265 Stream_SetPosition(user_data->data,
268 for (
int i = 0; i < transfer->num_iso_packets; i++)
270 const UINT32 act_len = transfer->iso_packet_desc[i].actual_length;
271 Stream_Write_UINT32(user_data->data, index);
272 Stream_Write_UINT32(user_data->data, act_len);
273 Stream_Write_UINT32(user_data->data, transfer->iso_packet_desc[i].status);
275 if (transfer->iso_packet_desc[i].status != USBD_STATUS_SUCCESS)
276 user_data->ErrorCount++;
279 const unsigned char* packetBuffer =
280 libusb_get_iso_packet_buffer_simple(transfer, i);
281 BYTE* data = dataStart + index;
283 if (data != packetBuffer)
284 memmove(data, packetBuffer, act_len);
292 case LIBUSB_TRANSFER_CANCELLED:
295 case LIBUSB_TRANSFER_TIMED_OUT:
298 case LIBUSB_TRANSFER_ERROR:
300 const UINT32 InterfaceId =
301 ((STREAM_ID_PROXY << 30) | user_data->idev->get_ReqCompletion(user_data->idev));
303 if (list_contains(list, streamID))
305 if (!user_data->noack)
307 const UINT32 RequestID = streamID & INTERFACE_ID_MASK;
308 user_data->cb(user_data->idev, user_data->callback, user_data->data,
309 InterfaceId, user_data->noack, user_data->MessageId, RequestID,
310 transfer->num_iso_packets, transfer->status,
311 user_data->StartFrame, user_data->ErrorCount,
312 user_data->OutputBufferSize);
313 user_data->data = NULL;
315 ArrayList_Remove(list, transfer);
322 ArrayList_Unlock(list);
325 static const LIBUSB_ENDPOINT_DESCEIPTOR* func_get_ep_desc(LIBUSB_CONFIG_DESCRIPTOR* LibusbConfig,
327 UINT32 EndpointAddress)
330 const LIBUSB_INTERFACE*
interface = LibusbConfig->interface;
332 for (UINT32 inum = 0; inum < MsConfig->NumInterfaces; inum++)
334 BYTE alt = MsInterfaces[inum]->AlternateSetting;
335 const LIBUSB_ENDPOINT_DESCEIPTOR* endpoint = interface[inum].altsetting[alt].endpoint;
337 for (UINT32 pnum = 0; pnum < MsInterfaces[inum]->NumberOfPipes; pnum++)
339 if (endpoint[pnum].bEndpointAddress == EndpointAddress)
341 return &endpoint[pnum];
349 static void LIBUSB_CALL func_bulk_transfer_cb(
struct libusb_transfer* transfer)
351 ASYNC_TRANSFER_USER_DATA* user_data = NULL;
352 uint32_t streamID = 0;
353 wArrayList* list = NULL;
355 user_data = (ASYNC_TRANSFER_USER_DATA*)transfer->user_data;
358 WLog_ERR(TAG,
"[%s]: Invalid transfer->user_data!");
361 list = user_data->queue;
362 ArrayList_Lock(list);
363 streamID = stream_id_from_buffer(transfer);
365 if (list_contains(list, streamID))
367 const UINT32 InterfaceId =
368 ((STREAM_ID_PROXY << 30) | user_data->idev->get_ReqCompletion(user_data->idev));
369 const UINT32 RequestID = streamID & INTERFACE_ID_MASK;
371 user_data->cb(user_data->idev, user_data->callback, user_data->data, InterfaceId,
372 user_data->noack, user_data->MessageId, RequestID, transfer->num_iso_packets,
373 transfer->status, user_data->StartFrame, user_data->ErrorCount,
374 transfer->actual_length);
375 user_data->data = NULL;
376 ArrayList_Remove(list, transfer);
378 ArrayList_Unlock(list);
384 if (!urbdrc || !status)
390 *status = USBD_STATUS_SUCCESS;
393 case LIBUSB_ERROR_IO:
394 *status = USBD_STATUS_STALL_PID;
397 case LIBUSB_ERROR_INVALID_PARAM:
398 *status = USBD_STATUS_INVALID_PARAMETER;
401 case LIBUSB_ERROR_ACCESS:
402 *status = USBD_STATUS_NOT_ACCESSED;
405 case LIBUSB_ERROR_NO_DEVICE:
406 *status = USBD_STATUS_DEVICE_GONE;
410 if (!(pdev->status & URBDRC_DEVICE_NOT_FOUND))
411 pdev->status |= URBDRC_DEVICE_NOT_FOUND;
416 case LIBUSB_ERROR_NOT_FOUND:
417 *status = USBD_STATUS_STALL_PID;
420 case LIBUSB_ERROR_BUSY:
421 *status = USBD_STATUS_STALL_PID;
424 case LIBUSB_ERROR_TIMEOUT:
425 *status = USBD_STATUS_TIMEOUT;
428 case LIBUSB_ERROR_OVERFLOW:
429 *status = USBD_STATUS_STALL_PID;
432 case LIBUSB_ERROR_PIPE:
433 *status = USBD_STATUS_STALL_PID;
436 case LIBUSB_ERROR_INTERRUPTED:
437 *status = USBD_STATUS_STALL_PID;
440 case LIBUSB_ERROR_NO_MEM:
441 *status = USBD_STATUS_NO_MEMORY;
444 case LIBUSB_ERROR_NOT_SUPPORTED:
445 *status = USBD_STATUS_NOT_SUPPORTED;
448 case LIBUSB_ERROR_OTHER:
449 *status = USBD_STATUS_STALL_PID;
453 *status = USBD_STATUS_SUCCESS;
460 static int func_config_release_all_interface(
URBDRC_PLUGIN* urbdrc,
461 LIBUSB_DEVICE_HANDLE* libusb_handle,
462 UINT32 NumInterfaces)
464 if (NumInterfaces > INT32_MAX)
466 for (INT32 i = 0; i < (INT32)NumInterfaces; i++)
468 int ret = libusb_release_interface(libusb_handle, i);
470 if (log_libusb_result(urbdrc->log, WLOG_WARN,
"libusb_release_interface", ret))
477 static int func_claim_all_interface(
URBDRC_PLUGIN* urbdrc, LIBUSB_DEVICE_HANDLE* libusb_handle,
482 for (
int i = 0; i < NumInterfaces; i++)
484 ret = libusb_claim_interface(libusb_handle, i);
486 if (log_libusb_result(urbdrc->log, WLOG_ERROR,
"libusb_claim_interface", ret))
493 static LIBUSB_DEVICE* udev_get_libusb_dev(libusb_context* context, uint8_t bus_number,
496 LIBUSB_DEVICE** libusb_list = NULL;
497 LIBUSB_DEVICE* device = NULL;
498 const ssize_t total_device = libusb_get_device_list(context, &libusb_list);
500 for (ssize_t i = 0; i < total_device; i++)
502 LIBUSB_DEVICE* dev = libusb_list[i];
503 if ((bus_number == libusb_get_bus_number(dev)) &&
504 (dev_number == libusb_get_device_address(dev)))
507 libusb_unref_device(dev);
510 libusb_free_device_list(libusb_list, 0);
514 static LIBUSB_DEVICE_DESCRIPTOR* udev_new_descript(
URBDRC_PLUGIN* urbdrc, LIBUSB_DEVICE* libusb_dev)
517 LIBUSB_DEVICE_DESCRIPTOR* descriptor =
518 (LIBUSB_DEVICE_DESCRIPTOR*)calloc(1,
sizeof(LIBUSB_DEVICE_DESCRIPTOR));
521 ret = libusb_get_device_descriptor(libusb_dev, descriptor);
523 if (log_libusb_result(urbdrc->log, WLOG_ERROR,
"libusb_get_device_descriptor", ret))
532 static int libusb_udev_select_interface(IUDEVICE* idev, BYTE InterfaceNumber, BYTE AlternateSetting)
541 if (!pdev || !pdev->urbdrc)
544 urbdrc = pdev->urbdrc;
545 MsConfig = pdev->MsConfig;
549 MsInterfaces = MsConfig->MsInterfaces;
552 WLog_Print(urbdrc->log, WLOG_INFO,
553 "select Interface(%" PRIu8
") curr AlternateSetting(%" PRIu8
554 ") new AlternateSetting(%" PRIu8
")",
555 InterfaceNumber, MsInterfaces[InterfaceNumber]->AlternateSetting,
558 if (MsInterfaces[InterfaceNumber]->AlternateSetting != AlternateSetting)
566 error = libusb_set_interface_alt_setting(pdev->libusb_handle, InterfaceNumber,
569 log_libusb_result(urbdrc->log, WLOG_ERROR,
"libusb_set_interface_alt_setting", error);
586 LIBUSB_CONFIG_DESCRIPTOR* LibusbConfig = NULL;
587 const LIBUSB_INTERFACE* LibusbInterface = NULL;
588 const LIBUSB_INTERFACE_DESCRIPTOR* LibusbAltsetting = NULL;
589 const LIBUSB_ENDPOINT_DESCEIPTOR* LibusbEndpoint = NULL;
590 BYTE LibusbNumEndpoint = 0;
592 UINT32 MsOutSize = 0;
594 if (!pdev || !pdev->LibusbConfig || !pdev->urbdrc || !MsConfig)
597 urbdrc = pdev->urbdrc;
598 LibusbConfig = pdev->LibusbConfig;
600 if (LibusbConfig->bNumInterfaces != MsConfig->NumInterfaces)
602 WLog_Print(urbdrc->log, WLOG_ERROR,
603 "Select Configuration: Libusb NumberInterfaces(%" PRIu8
") is different "
604 "with MsConfig NumberInterfaces(%" PRIu32
")",
605 LibusbConfig->bNumInterfaces, MsConfig->NumInterfaces);
609 MsInterfaces = MsConfig->MsInterfaces;
611 for (UINT32 inum = 0; inum < MsConfig->NumInterfaces; inum++)
613 MsInterface = MsInterfaces[inum];
615 LibusbInterface = &LibusbConfig->interface[MsInterface->InterfaceNumber];
616 LibusbAltsetting = &LibusbInterface->altsetting[MsInterface->AlternateSetting];
617 LibusbNumEndpoint = LibusbAltsetting->bNumEndpoints;
621 for (UINT32 pnum = 0; pnum < LibusbNumEndpoint; pnum++)
625 if (pnum < MsInterface->NumberOfPipes && MsInterface->MsPipes)
627 MsPipe = MsInterface->MsPipes[pnum];
628 t_MsPipe->MaximumPacketSize = MsPipe->MaximumPacketSize;
629 t_MsPipe->MaximumTransferSize = MsPipe->MaximumTransferSize;
630 t_MsPipe->PipeFlags = MsPipe->PipeFlags;
634 t_MsPipe->MaximumPacketSize = 0;
635 t_MsPipe->MaximumTransferSize = 0xffffffff;
636 t_MsPipe->PipeFlags = 0;
639 t_MsPipe->PipeHandle = 0;
640 t_MsPipe->bEndpointAddress = 0;
641 t_MsPipe->bInterval = 0;
642 t_MsPipe->PipeType = 0;
643 t_MsPipe->InitCompleted = 0;
644 t_MsPipes[pnum] = t_MsPipe;
647 msusb_mspipes_replace(MsInterface, t_MsPipes, LibusbNumEndpoint);
658 MsConfig->ConfigurationHandle =
659 MsConfig->bConfigurationValue | (pdev->bus_number << 24) | (pdev->dev_number << 16);
660 MsInterfaces = MsConfig->MsInterfaces;
662 for (UINT32 inum = 0; inum < MsConfig->NumInterfaces; inum++)
665 MsInterface = MsInterfaces[inum];
667 LibusbInterface = &LibusbConfig->interface[MsInterface->InterfaceNumber];
668 LibusbAltsetting = &LibusbInterface->altsetting[MsInterface->AlternateSetting];
675 MsInterface->InterfaceHandle = LibusbAltsetting->bInterfaceNumber |
676 (LibusbAltsetting->bAlternateSetting << 8) |
677 (pdev->dev_number << 16) | (pdev->bus_number << 24);
678 MsInterface->Length = 16 + (MsInterface->NumberOfPipes * 20);
679 MsInterface->bInterfaceClass = LibusbAltsetting->bInterfaceClass;
680 MsInterface->bInterfaceSubClass = LibusbAltsetting->bInterfaceSubClass;
681 MsInterface->bInterfaceProtocol = LibusbAltsetting->bInterfaceProtocol;
682 MsInterface->InitCompleted = 1;
683 MsPipes = MsInterface->MsPipes;
684 LibusbNumEndpoint = LibusbAltsetting->bNumEndpoints;
686 for (UINT32 pnum = 0; pnum < LibusbNumEndpoint; pnum++)
689 MsPipe = MsPipes[pnum];
691 LibusbEndpoint = &LibusbAltsetting->endpoint[pnum];
698 MsPipe->PipeHandle = LibusbEndpoint->bEndpointAddress | (pdev->dev_number << 16) |
699 (pdev->bus_number << 24);
701 int max = LibusbEndpoint->wMaxPacketSize & 0x07ff;
702 BYTE attr = LibusbEndpoint->bmAttributes;
704 if ((attr & 0x3) == 1 || (attr & 0x3) == 3)
706 max *= (1 + ((LibusbEndpoint->wMaxPacketSize >> 11) & 3));
709 MsPipe->MaximumPacketSize = max;
710 MsPipe->bEndpointAddress = LibusbEndpoint->bEndpointAddress;
711 MsPipe->bInterval = LibusbEndpoint->bInterval;
712 MsPipe->PipeType = attr & 0x3;
713 MsPipe->InitCompleted = 1;
717 MsConfig->MsOutSize = MsOutSize;
718 MsConfig->InitCompleted = 1;
721 if (MsConfig != pdev->MsConfig)
723 msusb_msconfig_free(pdev->MsConfig);
724 pdev->MsConfig = MsConfig;
730 static int libusb_udev_select_configuration(IUDEVICE* idev, UINT32 bConfigurationValue)
734 LIBUSB_DEVICE_HANDLE* libusb_handle = NULL;
735 LIBUSB_DEVICE* libusb_dev = NULL;
737 LIBUSB_CONFIG_DESCRIPTOR** LibusbConfig = NULL;
740 if (!pdev || !pdev->MsConfig || !pdev->LibusbConfig || !pdev->urbdrc)
743 urbdrc = pdev->urbdrc;
744 MsConfig = pdev->MsConfig;
745 libusb_handle = pdev->libusb_handle;
746 libusb_dev = pdev->libusb_dev;
747 LibusbConfig = &pdev->LibusbConfig;
749 if (MsConfig->InitCompleted)
751 func_config_release_all_interface(pdev->urbdrc, libusb_handle,
752 (*LibusbConfig)->bNumInterfaces);
756 if (bConfigurationValue == 0)
757 ret = libusb_set_configuration(libusb_handle, -1);
759 ret = libusb_set_configuration(libusb_handle, bConfigurationValue);
761 if (log_libusb_result(urbdrc->log, WLOG_ERROR,
"libusb_set_configuration", ret))
763 func_claim_all_interface(urbdrc, libusb_handle, (*LibusbConfig)->bNumInterfaces);
768 ret = libusb_get_active_config_descriptor(libusb_dev, LibusbConfig);
770 if (log_libusb_result(urbdrc->log, WLOG_ERROR,
"libusb_set_configuration", ret))
772 func_claim_all_interface(urbdrc, libusb_handle, (*LibusbConfig)->bNumInterfaces);
777 func_claim_all_interface(urbdrc, libusb_handle, (*LibusbConfig)->bNumInterfaces);
781 static int libusb_udev_control_pipe_request(IUDEVICE* idev, UINT32 RequestId,
782 UINT32 EndpointAddress, UINT32* UsbdStatus,
int command)
787 WINPR_ASSERT(EndpointAddress <= UINT8_MAX);
795 idev->cancel_all_transfer_request(idev);
801 error = libusb_control_transfer(pdev->libusb_handle,
802 LIBUSB_ENDPOINT_OUT | LIBUSB_RECIPIENT_ENDPOINT,
803 LIBUSB_REQUEST_SET_FEATURE, ENDPOINT_HALT,
804 (uint16_t)EndpointAddress, NULL, 0, 1000);
808 idev->cancel_all_transfer_request(idev);
809 error = libusb_clear_halt(pdev->libusb_handle, (uint8_t)EndpointAddress);
822 static UINT32 libusb_udev_control_query_device_text(IUDEVICE* idev, UINT32 TextType,
823 UINT16 LocaleId, UINT8* BufferSize,
827 LIBUSB_DEVICE_DESCRIPTOR* devDescriptor = NULL;
828 const char strDesc[] =
"Generic Usb String";
829 char deviceLocation[25] = { 0 };
831 BYTE device_address = 0;
835 WCHAR* text = (WCHAR*)Buffer;
838 const UINT8 inSize = *BufferSize;
841 if (!pdev || !pdev->devDescriptor || !pdev->urbdrc)
842 return ERROR_INVALID_DATA;
844 urbdrc = pdev->urbdrc;
845 devDescriptor = pdev->devDescriptor;
849 case DeviceTextDescription:
851 BYTE data[0x100] = { 0 };
852 ret = libusb_get_string_descriptor(pdev->libusb_handle, devDescriptor->iProduct,
853 LocaleId, data, 0xFF);
862 if ((ret <= 0) || (ret <= 4) || (slen <= 4) || (locale != LIBUSB_DT_STRING) ||
865 const char* msg =
"SHORT_DESCRIPTOR";
867 msg = libusb_error_name(ret);
868 WLog_Print(urbdrc->log, WLOG_DEBUG,
869 "libusb_get_string_descriptor: "
870 "%s [%d], iProduct: %" PRIu8
"!",
871 msg, ret, devDescriptor->iProduct);
873 len = MIN(
sizeof(strDesc), inSize);
874 for (
size_t i = 0; i < len; i++)
875 text[i] = (WCHAR)strDesc[i];
877 *BufferSize = (BYTE)(len * 2);
886 const size_t rchar = _wcsnlen((WCHAR*)&data[2],
sizeof(data) / 2);
887 len = MIN((BYTE)ret - 2, slen);
888 len = MIN(len, inSize);
889 len = MIN(len, rchar * 2 +
sizeof(WCHAR));
890 memcpy(Buffer, &data[2], len);
894 Buffer[len - 2] =
'\0';
895 Buffer[len - 1] =
'\0';
896 *BufferSize = (BYTE)len;
901 case DeviceTextLocationInformation:
902 bus_number = libusb_get_bus_number(pdev->libusb_dev);
903 device_address = libusb_get_device_address(pdev->libusb_dev);
904 (void)sprintf_s(deviceLocation,
sizeof(deviceLocation),
905 "Port_#%04" PRIu8
".Hub_#%04" PRIu8
"", device_address, bus_number);
907 len = strnlen(deviceLocation,
908 MIN(
sizeof(deviceLocation), (inSize > 0) ? inSize - 1U : 0));
909 for (
size_t i = 0; i < len; i++)
910 text[i] = (WCHAR)deviceLocation[i];
912 *BufferSize = (UINT8)(len *
sizeof(WCHAR));
916 WLog_Print(urbdrc->log, WLOG_DEBUG,
"Query Text: unknown TextType %" PRIu32
"",
918 return ERROR_INVALID_DATA;
924 static int libusb_udev_os_feature_descriptor_request(IUDEVICE* idev, UINT32 RequestId,
925 BYTE Recipient, BYTE InterfaceNumber,
926 BYTE Ms_PageIndex, UINT16 Ms_featureDescIndex,
927 UINT32* UsbdStatus, UINT32* BufferSize,
928 BYTE* Buffer, UINT32 Timeout)
931 BYTE ms_string_desc[0x13] = { 0 };
935 WINPR_ASSERT(UsbdStatus);
936 WINPR_ASSERT(BufferSize);
937 WINPR_ASSERT(*BufferSize <= UINT16_MAX);
942 error = libusb_control_transfer(pdev->libusb_handle, LIBUSB_ENDPOINT_IN | Recipient,
943 LIBUSB_REQUEST_GET_DESCRIPTOR, 0x03ee, 0, ms_string_desc, 0x12,
946 log_libusb_result(pdev->urbdrc->log, WLOG_DEBUG,
"libusb_control_transfer", error);
950 const BYTE bMS_Vendorcode = ms_string_desc[16];
952 error = libusb_control_transfer(
953 pdev->libusb_handle, LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | Recipient,
954 bMS_Vendorcode, (UINT16)((InterfaceNumber << 8) | Ms_PageIndex), Ms_featureDescIndex,
955 Buffer, (UINT16)*BufferSize, Timeout);
956 log_libusb_result(pdev->urbdrc->log, WLOG_DEBUG,
"libusb_control_transfer", error);
959 *BufferSize = (UINT32)error;
963 *UsbdStatus = USBD_STATUS_STALL_PID;
965 *UsbdStatus = USBD_STATUS_SUCCESS;
967 return ERROR_SUCCESS;
970 static int libusb_udev_query_device_descriptor(IUDEVICE* idev,
int offset)
977 return pdev->devDescriptor->bLength;
979 case B_DESCRIPTOR_TYPE:
980 return pdev->devDescriptor->bDescriptorType;
983 return pdev->devDescriptor->bcdUSB;
986 return pdev->devDescriptor->bDeviceClass;
988 case B_DEVICE_SUBCLASS:
989 return pdev->devDescriptor->bDeviceSubClass;
991 case B_DEVICE_PROTOCOL:
992 return pdev->devDescriptor->bDeviceProtocol;
994 case B_MAX_PACKET_SIZE0:
995 return pdev->devDescriptor->bMaxPacketSize0;
998 return pdev->devDescriptor->idVendor;
1001 return pdev->devDescriptor->idProduct;
1004 return pdev->devDescriptor->bcdDevice;
1006 case I_MANUFACTURER:
1007 return pdev->devDescriptor->iManufacturer;
1010 return pdev->devDescriptor->iProduct;
1012 case I_SERIAL_NUMBER:
1013 return pdev->devDescriptor->iSerialNumber;
1015 case B_NUM_CONFIGURATIONS:
1016 return pdev->devDescriptor->bNumConfigurations;
1023 static BOOL libusb_udev_detach_kernel_driver(IUDEVICE* idev)
1029 if (!pdev || !pdev->LibusbConfig || !pdev->libusb_handle || !pdev->urbdrc)
1035 urbdrc = pdev->urbdrc;
1037 if ((pdev->status & URBDRC_DEVICE_DETACH_KERNEL) == 0)
1039 for (
int i = 0; i < pdev->LibusbConfig->bNumInterfaces; i++)
1041 err = libusb_kernel_driver_active(pdev->libusb_handle, i);
1042 log_libusb_result(urbdrc->log, WLOG_DEBUG,
"libusb_kernel_driver_active", err);
1046 err = libusb_detach_kernel_driver(pdev->libusb_handle, i);
1047 log_libusb_result(urbdrc->log, WLOG_DEBUG,
"libusb_detach_kernel_driver", err);
1051 pdev->status |= URBDRC_DEVICE_DETACH_KERNEL;
1058 static BOOL libusb_udev_attach_kernel_driver(IUDEVICE* idev)
1063 if (!pdev || !pdev->LibusbConfig || !pdev->libusb_handle || !pdev->urbdrc)
1066 for (
int i = 0; i < pdev->LibusbConfig->bNumInterfaces && err != LIBUSB_ERROR_NO_DEVICE; i++)
1068 err = libusb_release_interface(pdev->libusb_handle, i);
1070 log_libusb_result(pdev->urbdrc->log, WLOG_DEBUG,
"libusb_release_interface", err);
1073 if (err != LIBUSB_ERROR_NO_DEVICE)
1075 err = libusb_attach_kernel_driver(pdev->libusb_handle, i);
1076 log_libusb_result(pdev->urbdrc->log, WLOG_DEBUG,
"libusb_attach_kernel_driver if=%d",
1085 static int libusb_udev_is_composite_device(IUDEVICE* idev)
1088 return pdev->isCompositeDevice;
1091 static int libusb_udev_is_exist(IUDEVICE* idev)
1094 return (pdev->status & URBDRC_DEVICE_NOT_FOUND) ? 0 : 1;
1097 static int libusb_udev_is_channel_closed(IUDEVICE* idev)
1100 IUDEVMAN* udevman = NULL;
1101 if (!pdev || !pdev->urbdrc)
1104 udevman = pdev->urbdrc->udevman;
1107 if (udevman->status & URBDRC_DEVICE_CHANNEL_CLOSED)
1111 if (pdev->status & URBDRC_DEVICE_CHANNEL_CLOSED)
1117 static int libusb_udev_is_already_send(IUDEVICE* idev)
1120 return (pdev->status & URBDRC_DEVICE_ALREADY_SEND) ? 1 : 0;
1125 static void libusb_udev_mark_channel_closed(IUDEVICE* idev)
1128 if (pdev && ((pdev->status & URBDRC_DEVICE_CHANNEL_CLOSED) == 0))
1131 const uint8_t busNr = idev->get_bus_number(idev);
1132 const uint8_t devNr = idev->get_dev_number(idev);
1134 pdev->status |= URBDRC_DEVICE_CHANNEL_CLOSED;
1135 urbdrc->udevman->unregister_udevice(urbdrc->udevman, busNr, devNr);
1141 static void libusb_udev_channel_closed(IUDEVICE* idev)
1144 if (pdev && ((pdev->status & URBDRC_DEVICE_CHANNEL_CLOSED) == 0))
1147 const uint8_t busNr = idev->get_bus_number(idev);
1148 const uint8_t devNr = idev->get_dev_number(idev);
1149 IWTSVirtualChannel* channel = NULL;
1151 if (pdev->channelManager)
1152 channel = IFCALLRESULT(NULL, pdev->channelManager->FindChannelById,
1153 pdev->channelManager, pdev->channelID);
1155 pdev->status |= URBDRC_DEVICE_CHANNEL_CLOSED;
1158 channel->Write(channel, 0, NULL, NULL);
1160 urbdrc->udevman->unregister_udevice(urbdrc->udevman, busNr, devNr);
1164 static void libusb_udev_set_already_send(IUDEVICE* idev)
1167 pdev->status |= URBDRC_DEVICE_ALREADY_SEND;
1170 static char* libusb_udev_get_path(IUDEVICE* idev)
1176 static int libusb_udev_query_device_port_status(IUDEVICE* idev, UINT32* UsbdStatus,
1177 UINT32* BufferSize, BYTE* Buffer)
1184 if (!pdev || !pdev->urbdrc)
1187 urbdrc = pdev->urbdrc;
1189 if (pdev->hub_handle != NULL)
1191 ret = idev->control_transfer(
1193 LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_RECIPIENT_OTHER,
1194 LIBUSB_REQUEST_GET_STATUS, 0, pdev->port_number, UsbdStatus, BufferSize, Buffer, 1000);
1196 if (log_libusb_result(urbdrc->log, WLOG_DEBUG,
"libusb_control_transfer", ret))
1200 WLog_Print(urbdrc->log, WLOG_DEBUG,
1201 "PORT STATUS:0x%02" PRIx8
"%02" PRIx8
"%02" PRIx8
"%02" PRIx8
"", Buffer[3],
1202 Buffer[2], Buffer[1], Buffer[0]);
1211 UINT32 MessageId, UINT32 RequestId, UINT32 EndpointAddress,
1212 UINT32 TransferFlags, UINT32 StartFrame, UINT32 ErrorCount,
1213 BOOL NoAck,
const BYTE* packetDescriptorData,
1214 UINT32 NumberOfPackets, UINT32 BufferSize,
const BYTE* Buffer,
1215 t_isoch_transfer_cb cb, UINT32 Timeout)
1218 UINT32 iso_packet_size = 0;
1220 ASYNC_TRANSFER_USER_DATA* user_data = NULL;
1221 struct libusb_transfer* iso_transfer = NULL;
1223 size_t outSize = (12ULL * NumberOfPackets);
1224 uint32_t streamID = 0x40000000 | RequestId;
1226 if (!pdev || !pdev->urbdrc)
1229 urbdrc = pdev->urbdrc;
1230 user_data = async_transfer_user_data_new(idev, MessageId, 48, BufferSize, Buffer,
1231 outSize + 1024, NoAck, cb, callback);
1236 user_data->ErrorCount = ErrorCount;
1237 user_data->StartFrame = StartFrame;
1240 Stream_Seek(user_data->data, (12ULL * NumberOfPackets));
1242 if (NumberOfPackets > 0)
1244 iso_packet_size = BufferSize / NumberOfPackets;
1245 iso_transfer = libusb_alloc_transfer((
int)NumberOfPackets);
1248 if (iso_transfer == NULL)
1250 WLog_Print(urbdrc->log, WLOG_ERROR,
1251 "Error: libusb_alloc_transfer [NumberOfPackets=%" PRIu32
", BufferSize=%" PRIu32
1253 NumberOfPackets, BufferSize);
1254 async_transfer_user_data_free(user_data);
1259 libusb_fill_iso_transfer(iso_transfer, pdev->libusb_handle, EndpointAddress,
1260 Stream_Pointer(user_data->data), BufferSize, NumberOfPackets,
1261 func_iso_callback, user_data, Timeout);
1262 set_stream_id_for_buffer(iso_transfer, streamID);
1263 libusb_set_iso_packet_lengths(iso_transfer, iso_packet_size);
1265 if (!ArrayList_Append(pdev->request_queue, iso_transfer))
1267 WLog_Print(urbdrc->log, WLOG_WARN,
1268 "Failed to queue iso transfer, streamID %08" PRIx32
" already in use!",
1270 request_free(iso_transfer);
1273 rc = libusb_submit_transfer(iso_transfer);
1274 if (log_libusb_result(urbdrc->log, WLOG_ERROR,
"libusb_submit_transfer", rc))
1279 static BOOL libusb_udev_control_transfer(IUDEVICE* idev, UINT32 RequestId, UINT32 EndpointAddress,
1280 UINT32 TransferFlags, BYTE bmRequestType, BYTE Request,
1281 UINT16 Value, UINT16 Index, UINT32* UrbdStatus,
1282 UINT32* BufferSize, BYTE* Buffer, UINT32 Timeout)
1287 WINPR_ASSERT(BufferSize);
1288 WINPR_ASSERT(*BufferSize <= UINT16_MAX);
1290 if (!pdev || !pdev->urbdrc)
1293 status = libusb_control_transfer(pdev->libusb_handle, bmRequestType, Request, Value, Index,
1294 Buffer, (UINT16)*BufferSize, Timeout);
1297 *BufferSize = (UINT32)status;
1299 log_libusb_result(pdev->urbdrc->log, WLOG_ERROR,
"libusb_control_transfer", status);
1301 if (!func_set_usbd_status(pdev->urbdrc, pdev, UrbdStatus, status))
1307 static int libusb_udev_bulk_or_interrupt_transfer(IUDEVICE* idev,
1309 UINT32 MessageId, UINT32 RequestId,
1310 UINT32 EndpointAddress, UINT32 TransferFlags,
1311 BOOL NoAck, UINT32 BufferSize,
const BYTE* data,
1312 t_isoch_transfer_cb cb, UINT32 Timeout)
1315 UINT32 transfer_type = 0;
1317 const LIBUSB_ENDPOINT_DESCEIPTOR* ep_desc = NULL;
1318 struct libusb_transfer* transfer = NULL;
1320 ASYNC_TRANSFER_USER_DATA* user_data = NULL;
1321 uint32_t streamID = 0x80000000 | RequestId;
1323 if (!pdev || !pdev->LibusbConfig || !pdev->urbdrc)
1326 urbdrc = pdev->urbdrc;
1328 async_transfer_user_data_new(idev, MessageId, 36, BufferSize, data, 0, NoAck, cb, callback);
1334 transfer = libusb_alloc_transfer(0);
1337 async_transfer_user_data_free(user_data);
1340 transfer->user_data = user_data;
1342 ep_desc = func_get_ep_desc(pdev->LibusbConfig, pdev->MsConfig, EndpointAddress);
1346 WLog_Print(urbdrc->log, WLOG_ERROR,
"func_get_ep_desc: endpoint 0x%" PRIx32
" not found",
1348 request_free(transfer);
1352 transfer_type = (ep_desc->bmAttributes) & 0x3;
1353 WLog_Print(urbdrc->log, WLOG_DEBUG,
1354 "urb_bulk_or_interrupt_transfer: ep:0x%" PRIx32
" "
1355 "transfer_type %" PRIu32
" flag:%" PRIu32
" OutputBufferSize:0x%" PRIx32
"",
1356 EndpointAddress, transfer_type, TransferFlags, BufferSize);
1358 switch (transfer_type)
1362 libusb_fill_bulk_transfer(transfer, pdev->libusb_handle, EndpointAddress,
1363 Stream_Pointer(user_data->data), BufferSize,
1364 func_bulk_transfer_cb, user_data, Timeout);
1367 case INTERRUPT_TRANSFER:
1369 libusb_fill_interrupt_transfer(transfer, pdev->libusb_handle, EndpointAddress,
1370 Stream_Pointer(user_data->data), BufferSize,
1371 func_bulk_transfer_cb, user_data, Timeout);
1375 WLog_Print(urbdrc->log, WLOG_DEBUG,
1376 "urb_bulk_or_interrupt_transfer:"
1377 " other transfer type 0x%" PRIX32
"",
1379 request_free(transfer);
1383 set_stream_id_for_buffer(transfer, streamID);
1385 if (!ArrayList_Append(pdev->request_queue, transfer))
1387 WLog_Print(urbdrc->log, WLOG_WARN,
1388 "Failed to queue transfer, streamID %08" PRIx32
" already in use!", streamID);
1389 request_free(transfer);
1392 rc = libusb_submit_transfer(transfer);
1393 if (log_libusb_result(urbdrc->log, WLOG_ERROR,
"libusb_submit_transfer", rc))
1398 static int func_cancel_xact_request(
URBDRC_PLUGIN* urbdrc,
struct libusb_transfer* transfer)
1402 if (!urbdrc || !transfer)
1405 status = libusb_cancel_transfer(transfer);
1407 if (log_libusb_result(urbdrc->log, WLOG_WARN,
"libusb_cancel_transfer", status))
1409 if (status == LIBUSB_ERROR_NOT_FOUND)
1418 static void libusb_udev_cancel_all_transfer_request(IUDEVICE* idev)
1423 if (!pdev || !pdev->request_queue || !pdev->urbdrc)
1426 ArrayList_Lock(pdev->request_queue);
1427 count = ArrayList_Count(pdev->request_queue);
1429 for (
size_t x = 0; x < count; x++)
1431 struct libusb_transfer* transfer = ArrayList_GetItem(pdev->request_queue, x);
1432 func_cancel_xact_request(pdev->urbdrc, transfer);
1435 ArrayList_Unlock(pdev->request_queue);
1438 static int libusb_udev_cancel_transfer_request(IUDEVICE* idev, UINT32 RequestId)
1442 struct libusb_transfer* transfer = NULL;
1443 uint32_t cancelID1 = 0x40000000 | RequestId;
1444 uint32_t cancelID2 = 0x80000000 | RequestId;
1446 if (!idev || !pdev->urbdrc || !pdev->request_queue)
1449 ArrayList_Lock(pdev->request_queue);
1450 transfer = list_contains(pdev->request_queue, cancelID1);
1452 transfer = list_contains(pdev->request_queue, cancelID2);
1458 rc = func_cancel_xact_request(urbdrc, transfer);
1460 ArrayList_Unlock(pdev->request_queue);
1464 BASIC_STATE_FUNC_DEFINED(channelManager, IWTSVirtualChannelManager*)
1465 BASIC_STATE_FUNC_DEFINED(channelID, UINT32)
1466 BASIC_STATE_FUNC_DEFINED(ReqCompletion, UINT32)
1467 BASIC_STATE_FUNC_DEFINED(bus_number, BYTE)
1468 BASIC_STATE_FUNC_DEFINED(dev_number, BYTE)
1469 BASIC_STATE_FUNC_DEFINED(port_number,
int)
1472 BASIC_POINT_FUNC_DEFINED(udev,
void*)
1473 BASIC_POINT_FUNC_DEFINED(prev,
void*)
1474 BASIC_POINT_FUNC_DEFINED(next,
void*)
1476 static UINT32 udev_get_UsbDevice(IUDEVICE* idev)
1483 return pdev->UsbDevice;
1486 static void udev_set_UsbDevice(IUDEVICE* idev, UINT32 val)
1493 pdev->UsbDevice = val;
1496 static void udev_free(IUDEVICE* idev)
1502 if (!idev || !udev->urbdrc)
1505 urbdrc = udev->urbdrc;
1507 libusb_udev_cancel_all_transfer_request(&udev->iface);
1508 if (udev->libusb_handle)
1510 rc = libusb_reset_device(udev->libusb_handle);
1512 log_libusb_result(urbdrc->log, WLOG_ERROR,
"libusb_reset_device", rc);
1521 udev->iface.attach_kernel_driver(idev);
1522 ArrayList_Free(udev->request_queue);
1524 msusb_msconfig_free(udev->MsConfig);
1525 libusb_unref_device(udev->libusb_dev);
1526 libusb_close(udev->libusb_handle);
1527 libusb_close(udev->hub_handle);
1528 free(udev->devDescriptor);
1532 static void udev_load_interface(
UDEVICE* pdev)
1538 BASIC_STATE_FUNC_REGISTER(channelManager, pdev);
1539 BASIC_STATE_FUNC_REGISTER(channelID, pdev);
1540 BASIC_STATE_FUNC_REGISTER(UsbDevice, pdev);
1541 BASIC_STATE_FUNC_REGISTER(ReqCompletion, pdev);
1542 BASIC_STATE_FUNC_REGISTER(bus_number, pdev);
1543 BASIC_STATE_FUNC_REGISTER(dev_number, pdev);
1544 BASIC_STATE_FUNC_REGISTER(port_number, pdev);
1545 BASIC_STATE_FUNC_REGISTER(MsConfig, pdev);
1546 BASIC_STATE_FUNC_REGISTER(p_udev, pdev);
1547 BASIC_STATE_FUNC_REGISTER(p_prev, pdev);
1548 BASIC_STATE_FUNC_REGISTER(p_next, pdev);
1549 pdev->iface.isCompositeDevice = libusb_udev_is_composite_device;
1550 pdev->iface.isExist = libusb_udev_is_exist;
1551 pdev->iface.isAlreadySend = libusb_udev_is_already_send;
1552 pdev->iface.isChannelClosed = libusb_udev_is_channel_closed;
1553 pdev->iface.setAlreadySend = libusb_udev_set_already_send;
1554 pdev->iface.setChannelClosed = libusb_udev_channel_closed;
1555 pdev->iface.markChannelClosed = libusb_udev_mark_channel_closed;
1556 pdev->iface.getPath = libusb_udev_get_path;
1558 pdev->iface.isoch_transfer = libusb_udev_isoch_transfer;
1559 pdev->iface.control_transfer = libusb_udev_control_transfer;
1560 pdev->iface.bulk_or_interrupt_transfer = libusb_udev_bulk_or_interrupt_transfer;
1561 pdev->iface.select_interface = libusb_udev_select_interface;
1562 pdev->iface.select_configuration = libusb_udev_select_configuration;
1563 pdev->iface.complete_msconfig_setup = libusb_udev_complete_msconfig_setup;
1564 pdev->iface.control_pipe_request = libusb_udev_control_pipe_request;
1565 pdev->iface.control_query_device_text = libusb_udev_control_query_device_text;
1566 pdev->iface.os_feature_descriptor_request = libusb_udev_os_feature_descriptor_request;
1567 pdev->iface.cancel_all_transfer_request = libusb_udev_cancel_all_transfer_request;
1568 pdev->iface.cancel_transfer_request = libusb_udev_cancel_transfer_request;
1569 pdev->iface.query_device_descriptor = libusb_udev_query_device_descriptor;
1570 pdev->iface.detach_kernel_driver = libusb_udev_detach_kernel_driver;
1571 pdev->iface.attach_kernel_driver = libusb_udev_attach_kernel_driver;
1572 pdev->iface.query_device_port_status = libusb_udev_query_device_port_status;
1573 pdev->iface.free = udev_free;
1577 UINT16 bus_number, UINT16 dev_number)
1580 uint8_t port_numbers[16] = { 0 };
1581 LIBUSB_DEVICE** libusb_list = NULL;
1582 const ssize_t total_device = libusb_get_device_list(ctx, &libusb_list);
1584 WINPR_ASSERT(urbdrc);
1587 for (ssize_t i = 0; i < total_device; i++)
1589 LIBUSB_DEVICE* dev = libusb_list[i];
1591 if ((bus_number != libusb_get_bus_number(dev)) ||
1592 (dev_number != libusb_get_device_address(dev)))
1593 libusb_unref_device(dev);
1596 error = libusb_open(dev, &pdev->libusb_handle);
1598 if (log_libusb_result(urbdrc->log, WLOG_ERROR,
"libusb_open", error))
1600 libusb_unref_device(dev);
1605 error = libusb_get_port_numbers(dev, port_numbers,
sizeof(port_numbers));
1609 log_libusb_result(urbdrc->log, WLOG_ERROR,
"libusb_get_port_numbers", error);
1610 libusb_unref_device(dev);
1614 pdev->port_number = port_numbers[(error - 1)];
1616 WLog_Print(urbdrc->log, WLOG_DEBUG,
" Port: %d", pdev->port_number);
1618 (void)_snprintf(pdev->path,
sizeof(pdev->path),
"%" PRIu16
"-%d", bus_number,
1621 WLog_Print(urbdrc->log, WLOG_DEBUG,
" DevPath: %s", pdev->path);
1624 libusb_free_device_list(libusb_list, 0);
1632 UINT16 bus_number, UINT16 dev_number)
1635 LIBUSB_DEVICE** libusb_list = NULL;
1636 LIBUSB_DEVICE_HANDLE* handle = NULL;
1637 const ssize_t total_device = libusb_get_device_list(ctx, &libusb_list);
1639 WINPR_ASSERT(urbdrc);
1642 for (ssize_t i = 0; i < total_device; i++)
1644 LIBUSB_DEVICE* dev = libusb_list[i];
1646 if ((bus_number != libusb_get_bus_number(dev)) ||
1647 (1 != libusb_get_device_address(dev)))
1648 libusb_unref_device(dev);
1651 WLog_Print(urbdrc->log, WLOG_DEBUG,
" Open hub: %" PRIu16
"", bus_number);
1652 error = libusb_open(dev, &handle);
1654 if (!log_libusb_result(urbdrc->log, WLOG_ERROR,
"libusb_open", error))
1655 pdev->hub_handle = handle;
1657 libusb_unref_device(dev);
1661 libusb_free_device_list(libusb_list, 0);
1669 static void request_free(
void* value)
1671 ASYNC_TRANSFER_USER_DATA* user_data = NULL;
1672 struct libusb_transfer* transfer = (
struct libusb_transfer*)value;
1676 user_data = (ASYNC_TRANSFER_USER_DATA*)transfer->user_data;
1677 async_transfer_user_data_free(user_data);
1678 transfer->user_data = NULL;
1679 libusb_free_transfer(transfer);
1682 static IUDEVICE* udev_init(
URBDRC_PLUGIN* urbdrc, libusb_context* context, LIBUSB_DEVICE* device,
1683 BYTE bus_number, BYTE dev_number)
1686 int status = LIBUSB_ERROR_OTHER;
1687 LIBUSB_DEVICE_DESCRIPTOR* devDescriptor = NULL;
1688 LIBUSB_CONFIG_DESCRIPTOR* config_temp = NULL;
1689 LIBUSB_INTERFACE_DESCRIPTOR interface_temp;
1691 WINPR_ASSERT(urbdrc);
1698 pdev->urbdrc = urbdrc;
1699 udev_load_interface(pdev);
1702 pdev->libusb_dev = device;
1704 pdev->libusb_dev = udev_get_libusb_dev(context, bus_number, dev_number);
1706 if (pdev->libusb_dev == NULL)
1709 if (urbdrc->listener_callback)
1710 udev_set_channelManager(&pdev->iface, urbdrc->listener_callback->channel_mgr);
1713 status = udev_get_device_handle(urbdrc, context, pdev, bus_number, dev_number);
1714 if (status != LIBUSB_SUCCESS)
1716 struct libusb_device_descriptor desc;
1717 const uint8_t port = libusb_get_port_number(pdev->libusb_dev);
1718 libusb_get_device_descriptor(pdev->libusb_dev, &desc);
1720 log_libusb_result(urbdrc->log, WLOG_ERROR,
1721 "libusb_open [b=0x%02X,p=0x%02X,a=0x%02X,VID=0x%04X,PID=0x%04X]", status,
1722 bus_number, port, dev_number, desc.idVendor, desc.idProduct);
1727 status = udev_get_hub_handle(urbdrc, context, pdev, bus_number, dev_number);
1730 pdev->hub_handle = NULL;
1732 pdev->devDescriptor = udev_new_descript(urbdrc, pdev->libusb_dev);
1734 if (!pdev->devDescriptor)
1737 status = libusb_get_active_config_descriptor(pdev->libusb_dev, &pdev->LibusbConfig);
1739 if (status == LIBUSB_ERROR_NOT_FOUND)
1740 status = libusb_get_config_descriptor(pdev->libusb_dev, 0, &pdev->LibusbConfig);
1745 config_temp = pdev->LibusbConfig;
1747 interface_temp = config_temp->interface[0].altsetting[0];
1748 WLog_Print(urbdrc->log, WLOG_DEBUG,
1749 "Registered Device: Vid: 0x%04" PRIX16
" Pid: 0x%04" PRIX16
""
1750 " InterfaceClass = %s",
1751 pdev->devDescriptor->idVendor, pdev->devDescriptor->idProduct,
1752 usb_interface_class_to_string(interface_temp.bInterfaceClass));
1754 devDescriptor = pdev->devDescriptor;
1756 if ((devDescriptor->bNumConfigurations == 1) && (config_temp->bNumInterfaces > 1) &&
1757 (devDescriptor->bDeviceClass == LIBUSB_CLASS_PER_INTERFACE))
1759 pdev->isCompositeDevice = 1;
1761 else if ((devDescriptor->bDeviceClass == 0xef) &&
1762 (devDescriptor->bDeviceSubClass == LIBUSB_CLASS_COMM) &&
1763 (devDescriptor->bDeviceProtocol == 0x01))
1765 pdev->isCompositeDevice = 1;
1768 pdev->isCompositeDevice = 0;
1771 devDescriptor->bDeviceClass = interface_temp.bInterfaceClass;
1772 devDescriptor->bDeviceSubClass = interface_temp.bInterfaceSubClass;
1773 devDescriptor->bDeviceProtocol = interface_temp.bInterfaceProtocol;
1775 pdev->bus_number = bus_number;
1776 pdev->dev_number = dev_number;
1777 pdev->request_queue = ArrayList_New(TRUE);
1779 if (!pdev->request_queue)
1782 ArrayList_Object(pdev->request_queue)->fnObjectFree = request_free;
1785 pdev->MsConfig = msusb_msconfig_new();
1787 if (!pdev->MsConfig)
1791 return &pdev->iface;
1793 pdev->iface.free(&pdev->iface);
1797 size_t udev_new_by_id(
URBDRC_PLUGIN* urbdrc, libusb_context* ctx, UINT16 idVendor, UINT16 idProduct,
1798 IUDEVICE*** devArray)
1800 LIBUSB_DEVICE** libusb_list = NULL;
1802 ssize_t total_device = 0;
1805 if (!urbdrc || !devArray)
1808 WLog_Print(urbdrc->log, WLOG_INFO,
"VID: 0x%04" PRIX16
", PID: 0x%04" PRIX16
"", idVendor,
1810 total_device = libusb_get_device_list(ctx, &libusb_list);
1811 if (total_device < 0)
1814 array = (
UDEVICE**)calloc((
size_t)total_device,
sizeof(
UDEVICE*));
1819 for (ssize_t i = 0; i < total_device; i++)
1821 LIBUSB_DEVICE* dev = libusb_list[i];
1822 LIBUSB_DEVICE_DESCRIPTOR* descriptor = udev_new_descript(urbdrc, dev);
1824 if ((descriptor->idVendor == idVendor) && (descriptor->idProduct == idProduct))
1826 array[num] = (
PUDEVICE)udev_init(urbdrc, ctx, dev, libusb_get_bus_number(dev),
1827 libusb_get_device_address(dev));
1829 if (array[num] != NULL)
1833 libusb_unref_device(dev);
1839 libusb_free_device_list(libusb_list, 0);
1840 *devArray = (IUDEVICE**)array;
1844 IUDEVICE* udev_new_by_addr(
URBDRC_PLUGIN* urbdrc, libusb_context* context, BYTE bus_number,
1847 WLog_Print(urbdrc->log, WLOG_DEBUG,
"bus:%d dev:%d", bus_number, dev_number);
1848 return udev_init(urbdrc, context, NULL, bus_number, dev_number);