25 #include <winpr/assert.h>
26 #include <winpr/cast.h>
27 #include <winpr/wtypes.h>
28 #include <winpr/sysinfo.h>
29 #include <winpr/collections.h>
33 #include "libusb_udevice.h"
34 #include "../common/urbdrc_types.h"
36 #define BASIC_STATE_FUNC_DEFINED(_arg, _type) \
37 static _type udev_get_##_arg(IUDEVICE* idev) \
39 UDEVICE* pdev = (UDEVICE*)idev; \
42 static void udev_set_##_arg(IUDEVICE* idev, _type _t) \
44 UDEVICE* pdev = (UDEVICE*)idev; \
48 #define BASIC_POINT_FUNC_DEFINED(_arg, _type) \
49 static _type udev_get_p_##_arg(IUDEVICE* idev) \
51 UDEVICE* pdev = (UDEVICE*)idev; \
54 static void udev_set_p_##_arg(IUDEVICE* idev, _type _t) \
56 UDEVICE* pdev = (UDEVICE*)idev; \
60 #define BASIC_STATE_FUNC_REGISTER(_arg, _dev) \
61 _dev->iface.get_##_arg = udev_get_##_arg; \
62 (_dev)->iface.set_##_arg = udev_set_##_arg
64 #if LIBUSB_API_VERSION >= 0x01000103
65 #define HAVE_STREAM_ID_API 1
76 UINT32 OutputBufferSize;
78 t_isoch_transfer_cb cb;
80 #if !defined(HAVE_STREAM_ID_API)
83 } ASYNC_TRANSFER_USER_DATA;
85 static void request_free(
void* value);
87 static struct libusb_transfer* list_contains(wArrayList* list, UINT32 streamID)
92 count = ArrayList_Count(list);
93 for (
size_t x = 0; x < count; x++)
95 struct libusb_transfer* transfer = ArrayList_GetItem(list, x);
97 #if defined(HAVE_STREAM_ID_API)
98 const UINT32 currentID = libusb_transfer_get_stream_id(transfer);
100 const ASYNC_TRANSFER_USER_DATA* user_data = (ASYNC_TRANSFER_USER_DATA*)transfer->user_data;
101 const UINT32 currentID = user_data->streamID;
103 if (currentID == streamID)
109 static UINT32 stream_id_from_buffer(
struct libusb_transfer* transfer)
113 #if defined(HAVE_STREAM_ID_API)
114 return libusb_transfer_get_stream_id(transfer);
116 ASYNC_TRANSFER_USER_DATA* user_data = (ASYNC_TRANSFER_USER_DATA*)transfer->user_data;
119 return user_data->streamID;
123 static void set_stream_id_for_buffer(
struct libusb_transfer* transfer, UINT32 streamID)
125 #if defined(HAVE_STREAM_ID_API)
126 libusb_transfer_set_stream_id(transfer, streamID);
128 ASYNC_TRANSFER_USER_DATA* user_data = (ASYNC_TRANSFER_USER_DATA*)transfer->user_data;
131 user_data->streamID = streamID;
135 WINPR_ATTR_FORMAT_ARG(3, 8)
136 static BOOL log_libusb_result_(wLog* log, DWORD lvl, WINPR_FORMAT_ARG const
char* fmt,
137 const
char* fkt, const
char* file,
size_t line,
int error, ...)
143 char buffer[8192] = { 0 };
146 (void)vsnprintf(buffer,
sizeof(buffer), fmt, ap);
149 WLog_Print(log, lvl,
"[%s:%" PRIuz
"]: %s: error %s[%d]", fkt, line, buffer,
150 libusb_error_name(error), error);
156 #define log_libusb_result(log, lvl, fmt, error, ...) \
157 log_libusb_result_((log), (lvl), (fmt), __func__, __FILE__, __LINE__, error, ##__VA_ARGS__)
159 const char* usb_interface_class_to_string(uint8_t
class)
163 case LIBUSB_CLASS_PER_INTERFACE:
164 return "LIBUSB_CLASS_PER_INTERFACE";
165 case LIBUSB_CLASS_AUDIO:
166 return "LIBUSB_CLASS_AUDIO";
167 case LIBUSB_CLASS_COMM:
168 return "LIBUSB_CLASS_COMM";
169 case LIBUSB_CLASS_HID:
170 return "LIBUSB_CLASS_HID";
171 case LIBUSB_CLASS_PHYSICAL:
172 return "LIBUSB_CLASS_PHYSICAL";
173 case LIBUSB_CLASS_PRINTER:
174 return "LIBUSB_CLASS_PRINTER";
175 case LIBUSB_CLASS_IMAGE:
176 return "LIBUSB_CLASS_IMAGE";
177 case LIBUSB_CLASS_MASS_STORAGE:
178 return "LIBUSB_CLASS_MASS_STORAGE";
179 case LIBUSB_CLASS_HUB:
180 return "LIBUSB_CLASS_HUB";
181 case LIBUSB_CLASS_DATA:
182 return "LIBUSB_CLASS_DATA";
183 case LIBUSB_CLASS_SMART_CARD:
184 return "LIBUSB_CLASS_SMART_CARD";
185 case LIBUSB_CLASS_CONTENT_SECURITY:
186 return "LIBUSB_CLASS_CONTENT_SECURITY";
187 case LIBUSB_CLASS_VIDEO:
188 return "LIBUSB_CLASS_VIDEO";
189 case LIBUSB_CLASS_PERSONAL_HEALTHCARE:
190 return "LIBUSB_CLASS_PERSONAL_HEALTHCARE";
191 case LIBUSB_CLASS_DIAGNOSTIC_DEVICE:
192 return "LIBUSB_CLASS_DIAGNOSTIC_DEVICE";
193 case LIBUSB_CLASS_WIRELESS:
194 return "LIBUSB_CLASS_WIRELESS";
195 case LIBUSB_CLASS_APPLICATION:
196 return "LIBUSB_CLASS_APPLICATION";
197 case LIBUSB_CLASS_VENDOR_SPEC:
198 return "LIBUSB_CLASS_VENDOR_SPEC";
200 return "UNKNOWN_DEVICE_CLASS";
204 static ASYNC_TRANSFER_USER_DATA* async_transfer_user_data_new(IUDEVICE* idev, UINT32 MessageId,
205 size_t offset,
size_t BufferSize,
206 const BYTE* data,
size_t packetSize,
207 BOOL NoAck, t_isoch_transfer_cb cb,
210 ASYNC_TRANSFER_USER_DATA* user_data = NULL;
213 if (BufferSize > UINT32_MAX)
216 user_data = calloc(1,
sizeof(ASYNC_TRANSFER_USER_DATA));
220 user_data->data = Stream_New(NULL, offset + BufferSize + packetSize);
222 if (!user_data->data)
228 Stream_Seek(user_data->data, offset);
230 memcpy(Stream_Pointer(user_data->data), data, BufferSize);
232 user_data->OutputBufferSize = (UINT32)BufferSize;
234 user_data->noack = NoAck;
236 user_data->callback = callback;
237 user_data->idev = idev;
238 user_data->MessageId = MessageId;
240 user_data->queue = pdev->request_queue;
245 static void async_transfer_user_data_free(ASYNC_TRANSFER_USER_DATA* user_data)
249 Stream_Free(user_data->data, TRUE);
254 static void LIBUSB_CALL func_iso_callback(
struct libusb_transfer* transfer)
256 ASYNC_TRANSFER_USER_DATA* user_data = (ASYNC_TRANSFER_USER_DATA*)transfer->user_data;
257 const UINT32 streamID = stream_id_from_buffer(transfer);
258 wArrayList* list = user_data->queue;
260 ArrayList_Lock(list);
261 switch (transfer->status)
263 case LIBUSB_TRANSFER_COMPLETED:
266 BYTE* dataStart = Stream_Pointer(user_data->data);
267 Stream_SetPosition(user_data->data,
270 for (uint32_t i = 0; i < WINPR_ASSERTING_INT_CAST(uint32_t, transfer->num_iso_packets);
273 const UINT32 act_len = transfer->iso_packet_desc[i].actual_length;
274 Stream_Write_UINT32(user_data->data, index);
275 Stream_Write_UINT32(user_data->data, act_len);
276 Stream_Write_UINT32(user_data->data, transfer->iso_packet_desc[i].status);
278 if (transfer->iso_packet_desc[i].status != USBD_STATUS_SUCCESS)
279 user_data->ErrorCount++;
282 const unsigned char* packetBuffer =
283 libusb_get_iso_packet_buffer_simple(transfer, i);
284 BYTE* data = dataStart + index;
286 if (data != packetBuffer)
287 memmove(data, packetBuffer, act_len);
295 case LIBUSB_TRANSFER_CANCELLED:
298 case LIBUSB_TRANSFER_TIMED_OUT:
301 case LIBUSB_TRANSFER_ERROR:
303 const UINT32 InterfaceId =
304 ((STREAM_ID_PROXY << 30) | user_data->idev->get_ReqCompletion(user_data->idev));
306 if (list_contains(list, streamID))
308 if (!user_data->noack)
310 const UINT32 RequestID = streamID & INTERFACE_ID_MASK;
311 user_data->cb(user_data->idev, user_data->callback, user_data->data,
312 InterfaceId, user_data->noack, user_data->MessageId, RequestID,
313 WINPR_ASSERTING_INT_CAST(uint32_t, transfer->num_iso_packets),
314 transfer->status, user_data->StartFrame, user_data->ErrorCount,
315 user_data->OutputBufferSize);
316 user_data->data = NULL;
318 ArrayList_Remove(list, transfer);
325 ArrayList_Unlock(list);
328 static const LIBUSB_ENDPOINT_DESCEIPTOR* func_get_ep_desc(LIBUSB_CONFIG_DESCRIPTOR* LibusbConfig,
330 UINT32 EndpointAddress)
333 const LIBUSB_INTERFACE*
interface = LibusbConfig->interface;
335 for (UINT32 inum = 0; inum < MsConfig->NumInterfaces; inum++)
337 BYTE alt = MsInterfaces[inum]->AlternateSetting;
338 const LIBUSB_ENDPOINT_DESCEIPTOR* endpoint = interface[inum].altsetting[alt].endpoint;
340 for (UINT32 pnum = 0; pnum < MsInterfaces[inum]->NumberOfPipes; pnum++)
342 if (endpoint[pnum].bEndpointAddress == EndpointAddress)
344 return &endpoint[pnum];
352 static void LIBUSB_CALL func_bulk_transfer_cb(
struct libusb_transfer* transfer)
354 ASYNC_TRANSFER_USER_DATA* user_data = NULL;
355 uint32_t streamID = 0;
356 wArrayList* list = NULL;
358 user_data = (ASYNC_TRANSFER_USER_DATA*)transfer->user_data;
361 WLog_ERR(TAG,
"[%s]: Invalid transfer->user_data!");
364 list = user_data->queue;
365 ArrayList_Lock(list);
366 streamID = stream_id_from_buffer(transfer);
368 if (list_contains(list, streamID))
370 const UINT32 InterfaceId =
371 ((STREAM_ID_PROXY << 30) | user_data->idev->get_ReqCompletion(user_data->idev));
372 const UINT32 RequestID = streamID & INTERFACE_ID_MASK;
374 user_data->cb(user_data->idev, user_data->callback, user_data->data, InterfaceId,
375 user_data->noack, user_data->MessageId, RequestID,
376 WINPR_ASSERTING_INT_CAST(uint32_t, transfer->num_iso_packets),
377 transfer->status, user_data->StartFrame, user_data->ErrorCount,
378 WINPR_ASSERTING_INT_CAST(uint32_t, transfer->actual_length));
379 user_data->data = NULL;
380 ArrayList_Remove(list, transfer);
382 ArrayList_Unlock(list);
388 if (!urbdrc || !status)
394 *status = USBD_STATUS_SUCCESS;
397 case LIBUSB_ERROR_IO:
398 *status = USBD_STATUS_STALL_PID;
401 case LIBUSB_ERROR_INVALID_PARAM:
402 *status = USBD_STATUS_INVALID_PARAMETER;
405 case LIBUSB_ERROR_ACCESS:
406 *status = USBD_STATUS_NOT_ACCESSED;
409 case LIBUSB_ERROR_NO_DEVICE:
410 *status = USBD_STATUS_DEVICE_GONE;
414 if (!(pdev->status & URBDRC_DEVICE_NOT_FOUND))
415 pdev->status |= URBDRC_DEVICE_NOT_FOUND;
420 case LIBUSB_ERROR_NOT_FOUND:
421 *status = USBD_STATUS_STALL_PID;
424 case LIBUSB_ERROR_BUSY:
425 *status = USBD_STATUS_STALL_PID;
428 case LIBUSB_ERROR_TIMEOUT:
429 *status = USBD_STATUS_TIMEOUT;
432 case LIBUSB_ERROR_OVERFLOW:
433 *status = USBD_STATUS_STALL_PID;
436 case LIBUSB_ERROR_PIPE:
437 *status = USBD_STATUS_STALL_PID;
440 case LIBUSB_ERROR_INTERRUPTED:
441 *status = USBD_STATUS_STALL_PID;
444 case LIBUSB_ERROR_NO_MEM:
445 *status = USBD_STATUS_NO_MEMORY;
448 case LIBUSB_ERROR_NOT_SUPPORTED:
449 *status = USBD_STATUS_NOT_SUPPORTED;
452 case LIBUSB_ERROR_OTHER:
453 *status = USBD_STATUS_STALL_PID;
457 *status = USBD_STATUS_SUCCESS;
464 static int func_config_release_all_interface(
URBDRC_PLUGIN* urbdrc,
465 LIBUSB_DEVICE_HANDLE* libusb_handle,
466 UINT32 NumInterfaces)
468 if (NumInterfaces > INT32_MAX)
470 for (INT32 i = 0; i < (INT32)NumInterfaces; i++)
472 int ret = libusb_release_interface(libusb_handle, i);
474 if (log_libusb_result(urbdrc->log, WLOG_WARN,
"libusb_release_interface", ret))
481 static int func_claim_all_interface(
URBDRC_PLUGIN* urbdrc, LIBUSB_DEVICE_HANDLE* libusb_handle,
486 for (
int i = 0; i < NumInterfaces; i++)
488 ret = libusb_claim_interface(libusb_handle, i);
490 if (log_libusb_result(urbdrc->log, WLOG_ERROR,
"libusb_claim_interface", ret))
497 static LIBUSB_DEVICE* udev_get_libusb_dev(libusb_context* context, uint8_t bus_number,
500 LIBUSB_DEVICE** libusb_list = NULL;
501 LIBUSB_DEVICE* device = NULL;
502 const ssize_t total_device = libusb_get_device_list(context, &libusb_list);
504 for (ssize_t i = 0; i < total_device; i++)
506 LIBUSB_DEVICE* dev = libusb_list[i];
507 if ((bus_number == libusb_get_bus_number(dev)) &&
508 (dev_number == libusb_get_device_address(dev)))
511 libusb_unref_device(dev);
514 libusb_free_device_list(libusb_list, 0);
518 static LIBUSB_DEVICE_DESCRIPTOR* udev_new_descript(
URBDRC_PLUGIN* urbdrc, LIBUSB_DEVICE* libusb_dev)
521 LIBUSB_DEVICE_DESCRIPTOR* descriptor =
522 (LIBUSB_DEVICE_DESCRIPTOR*)calloc(1,
sizeof(LIBUSB_DEVICE_DESCRIPTOR));
525 ret = libusb_get_device_descriptor(libusb_dev, descriptor);
527 if (log_libusb_result(urbdrc->log, WLOG_ERROR,
"libusb_get_device_descriptor", ret))
536 static int libusb_udev_select_interface(IUDEVICE* idev, BYTE InterfaceNumber, BYTE AlternateSetting)
545 if (!pdev || !pdev->urbdrc)
548 urbdrc = pdev->urbdrc;
549 MsConfig = pdev->MsConfig;
553 MsInterfaces = MsConfig->MsInterfaces;
556 WLog_Print(urbdrc->log, WLOG_INFO,
557 "select Interface(%" PRIu8
") curr AlternateSetting(%" PRIu8
558 ") new AlternateSetting(%" PRIu8
")",
559 InterfaceNumber, MsInterfaces[InterfaceNumber]->AlternateSetting,
562 if (MsInterfaces[InterfaceNumber]->AlternateSetting != AlternateSetting)
570 error = libusb_set_interface_alt_setting(pdev->libusb_handle, InterfaceNumber,
573 log_libusb_result(urbdrc->log, WLOG_ERROR,
"libusb_set_interface_alt_setting", error);
590 LIBUSB_CONFIG_DESCRIPTOR* LibusbConfig = NULL;
591 const LIBUSB_INTERFACE* LibusbInterface = NULL;
592 const LIBUSB_INTERFACE_DESCRIPTOR* LibusbAltsetting = NULL;
593 const LIBUSB_ENDPOINT_DESCEIPTOR* LibusbEndpoint = NULL;
594 BYTE LibusbNumEndpoint = 0;
596 UINT32 MsOutSize = 0;
598 if (!pdev || !pdev->LibusbConfig || !pdev->urbdrc || !MsConfig)
601 urbdrc = pdev->urbdrc;
602 LibusbConfig = pdev->LibusbConfig;
604 if (LibusbConfig->bNumInterfaces != MsConfig->NumInterfaces)
606 WLog_Print(urbdrc->log, WLOG_ERROR,
607 "Select Configuration: Libusb NumberInterfaces(%" PRIu8
") is different "
608 "with MsConfig NumberInterfaces(%" PRIu32
")",
609 LibusbConfig->bNumInterfaces, MsConfig->NumInterfaces);
613 MsInterfaces = MsConfig->MsInterfaces;
615 for (UINT32 inum = 0; inum < MsConfig->NumInterfaces; inum++)
617 MsInterface = MsInterfaces[inum];
619 LibusbInterface = &LibusbConfig->interface[MsInterface->InterfaceNumber];
620 LibusbAltsetting = &LibusbInterface->altsetting[MsInterface->AlternateSetting];
621 LibusbNumEndpoint = LibusbAltsetting->bNumEndpoints;
625 for (UINT32 pnum = 0; pnum < LibusbNumEndpoint; pnum++)
629 if (pnum < MsInterface->NumberOfPipes && MsInterface->MsPipes)
631 MsPipe = MsInterface->MsPipes[pnum];
632 t_MsPipe->MaximumPacketSize = MsPipe->MaximumPacketSize;
633 t_MsPipe->MaximumTransferSize = MsPipe->MaximumTransferSize;
634 t_MsPipe->PipeFlags = MsPipe->PipeFlags;
638 t_MsPipe->MaximumPacketSize = 0;
639 t_MsPipe->MaximumTransferSize = 0xffffffff;
640 t_MsPipe->PipeFlags = 0;
643 t_MsPipe->PipeHandle = 0;
644 t_MsPipe->bEndpointAddress = 0;
645 t_MsPipe->bInterval = 0;
646 t_MsPipe->PipeType = 0;
647 t_MsPipe->InitCompleted = 0;
648 t_MsPipes[pnum] = t_MsPipe;
651 msusb_mspipes_replace(MsInterface, t_MsPipes, LibusbNumEndpoint);
662 MsConfig->ConfigurationHandle = (uint32_t)MsConfig->bConfigurationValue |
663 ((uint32_t)pdev->bus_number << 24) |
664 (((uint32_t)pdev->dev_number << 16) & 0xFF0000);
665 MsInterfaces = MsConfig->MsInterfaces;
667 for (UINT32 inum = 0; inum < MsConfig->NumInterfaces; inum++)
670 MsInterface = MsInterfaces[inum];
672 LibusbInterface = &LibusbConfig->interface[MsInterface->InterfaceNumber];
673 LibusbAltsetting = &LibusbInterface->altsetting[MsInterface->AlternateSetting];
680 MsInterface->InterfaceHandle =
681 WINPR_ASSERTING_INT_CAST(UINT32, (LibusbAltsetting->bInterfaceNumber |
682 (LibusbAltsetting->bAlternateSetting << 8) |
683 (pdev->dev_number << 16) | (pdev->bus_number << 24)));
684 const size_t len = 16 + (MsInterface->NumberOfPipes * 20);
685 MsInterface->Length = WINPR_ASSERTING_INT_CAST(UINT16, len);
686 MsInterface->bInterfaceClass = LibusbAltsetting->bInterfaceClass;
687 MsInterface->bInterfaceSubClass = LibusbAltsetting->bInterfaceSubClass;
688 MsInterface->bInterfaceProtocol = LibusbAltsetting->bInterfaceProtocol;
689 MsInterface->InitCompleted = 1;
690 MsPipes = MsInterface->MsPipes;
691 LibusbNumEndpoint = LibusbAltsetting->bNumEndpoints;
693 for (UINT32 pnum = 0; pnum < LibusbNumEndpoint; pnum++)
696 MsPipe = MsPipes[pnum];
698 LibusbEndpoint = &LibusbAltsetting->endpoint[pnum];
705 MsPipe->PipeHandle = LibusbEndpoint->bEndpointAddress |
706 (((uint32_t)pdev->dev_number << 16) & 0xFF0000) |
707 (((uint32_t)pdev->bus_number << 24) & 0xFF000000);
709 unsigned max = LibusbEndpoint->wMaxPacketSize & 0x07ff;
710 BYTE attr = LibusbEndpoint->bmAttributes;
712 if ((attr & 0x3) == 1 || (attr & 0x3) == 3)
714 max *= (1 + ((LibusbEndpoint->wMaxPacketSize >> 11) & 3));
717 MsPipe->MaximumPacketSize = WINPR_ASSERTING_INT_CAST(uint16_t, max);
718 MsPipe->bEndpointAddress = LibusbEndpoint->bEndpointAddress;
719 MsPipe->bInterval = LibusbEndpoint->bInterval;
720 MsPipe->PipeType = attr & 0x3;
721 MsPipe->InitCompleted = 1;
725 MsConfig->MsOutSize = WINPR_ASSERTING_INT_CAST(
int, MsOutSize);
726 MsConfig->InitCompleted = 1;
729 if (MsConfig != pdev->MsConfig)
731 msusb_msconfig_free(pdev->MsConfig);
732 pdev->MsConfig = MsConfig;
738 static int libusb_udev_select_configuration(IUDEVICE* idev, UINT32 bConfigurationValue)
742 LIBUSB_DEVICE_HANDLE* libusb_handle = NULL;
743 LIBUSB_DEVICE* libusb_dev = NULL;
745 LIBUSB_CONFIG_DESCRIPTOR** LibusbConfig = NULL;
748 if (!pdev || !pdev->MsConfig || !pdev->LibusbConfig || !pdev->urbdrc)
751 urbdrc = pdev->urbdrc;
752 MsConfig = pdev->MsConfig;
753 libusb_handle = pdev->libusb_handle;
754 libusb_dev = pdev->libusb_dev;
755 LibusbConfig = &pdev->LibusbConfig;
757 if (MsConfig->InitCompleted)
759 func_config_release_all_interface(pdev->urbdrc, libusb_handle,
760 (*LibusbConfig)->bNumInterfaces);
764 if (bConfigurationValue == 0)
765 ret = libusb_set_configuration(libusb_handle, -1);
767 ret = libusb_set_configuration(libusb_handle,
768 WINPR_ASSERTING_INT_CAST(
int, bConfigurationValue));
770 if (log_libusb_result(urbdrc->log, WLOG_ERROR,
"libusb_set_configuration", ret))
772 func_claim_all_interface(urbdrc, libusb_handle, (*LibusbConfig)->bNumInterfaces);
777 ret = libusb_get_active_config_descriptor(libusb_dev, LibusbConfig);
779 if (log_libusb_result(urbdrc->log, WLOG_ERROR,
"libusb_set_configuration", ret))
781 func_claim_all_interface(urbdrc, libusb_handle, (*LibusbConfig)->bNumInterfaces);
786 func_claim_all_interface(urbdrc, libusb_handle, (*LibusbConfig)->bNumInterfaces);
790 static int libusb_udev_control_pipe_request(IUDEVICE* idev, UINT32 RequestId,
791 UINT32 EndpointAddress, UINT32* UsbdStatus,
int command)
796 WINPR_ASSERT(EndpointAddress <= UINT8_MAX);
804 idev->cancel_all_transfer_request(idev);
810 error = libusb_control_transfer(pdev->libusb_handle,
811 (uint8_t)LIBUSB_ENDPOINT_OUT |
812 (uint8_t)LIBUSB_RECIPIENT_ENDPOINT,
813 LIBUSB_REQUEST_SET_FEATURE, ENDPOINT_HALT,
814 (uint16_t)EndpointAddress, NULL, 0, 1000);
818 idev->cancel_all_transfer_request(idev);
819 error = libusb_clear_halt(pdev->libusb_handle, (uint8_t)EndpointAddress);
832 static UINT32 libusb_udev_control_query_device_text(IUDEVICE* idev, UINT32 TextType,
833 UINT16 LocaleId, UINT8* BufferSize,
837 LIBUSB_DEVICE_DESCRIPTOR* devDescriptor = NULL;
838 const char strDesc[] =
"Generic Usb String";
839 char deviceLocation[25] = { 0 };
841 BYTE device_address = 0;
845 WCHAR* text = (WCHAR*)Buffer;
848 const UINT8 inSize = *BufferSize;
851 if (!pdev || !pdev->devDescriptor || !pdev->urbdrc)
852 return ERROR_INVALID_DATA;
854 urbdrc = pdev->urbdrc;
855 devDescriptor = pdev->devDescriptor;
859 case DeviceTextDescription:
861 BYTE data[0x100] = { 0 };
862 ret = libusb_get_string_descriptor(pdev->libusb_handle, devDescriptor->iProduct,
863 LocaleId, data, 0xFF);
872 if ((ret <= 0) || (ret <= 4) || (slen <= 4) || (locale != LIBUSB_DT_STRING) ||
875 const char* msg =
"SHORT_DESCRIPTOR";
877 msg = libusb_error_name(ret);
878 WLog_Print(urbdrc->log, WLOG_DEBUG,
879 "libusb_get_string_descriptor: "
880 "%s [%d], iProduct: %" PRIu8
"!",
881 msg, ret, devDescriptor->iProduct);
883 len = MIN(
sizeof(strDesc), inSize);
884 for (
size_t i = 0; i < len; i++)
885 text[i] = (WCHAR)strDesc[i];
887 *BufferSize = (BYTE)(len * 2);
896 const size_t rchar = _wcsnlen((WCHAR*)&data[2],
sizeof(data) / 2);
897 len = MIN((BYTE)ret - 2, slen);
898 len = MIN(len, inSize);
899 len = MIN(len, rchar * 2 +
sizeof(WCHAR));
900 memcpy(Buffer, &data[2], len);
904 Buffer[len - 2] =
'\0';
905 Buffer[len - 1] =
'\0';
906 *BufferSize = (BYTE)len;
911 case DeviceTextLocationInformation:
912 bus_number = libusb_get_bus_number(pdev->libusb_dev);
913 device_address = libusb_get_device_address(pdev->libusb_dev);
914 (void)sprintf_s(deviceLocation,
sizeof(deviceLocation),
915 "Port_#%04" PRIu8
".Hub_#%04" PRIu8
"", device_address, bus_number);
917 len = strnlen(deviceLocation,
918 MIN(
sizeof(deviceLocation), (inSize > 0) ? inSize - 1U : 0));
919 for (
size_t i = 0; i < len; i++)
920 text[i] = (WCHAR)deviceLocation[i];
922 *BufferSize = (UINT8)(len *
sizeof(WCHAR));
926 WLog_Print(urbdrc->log, WLOG_DEBUG,
"Query Text: unknown TextType %" PRIu32
"",
928 return ERROR_INVALID_DATA;
934 static int libusb_udev_os_feature_descriptor_request(IUDEVICE* idev, UINT32 RequestId,
935 BYTE Recipient, BYTE InterfaceNumber,
936 BYTE Ms_PageIndex, UINT16 Ms_featureDescIndex,
937 UINT32* UsbdStatus, UINT32* BufferSize,
938 BYTE* Buffer, UINT32 Timeout)
941 BYTE ms_string_desc[0x13] = { 0 };
945 WINPR_ASSERT(UsbdStatus);
946 WINPR_ASSERT(BufferSize);
947 WINPR_ASSERT(*BufferSize <= UINT16_MAX);
952 error = libusb_control_transfer(pdev->libusb_handle, LIBUSB_ENDPOINT_IN | Recipient,
953 LIBUSB_REQUEST_GET_DESCRIPTOR, 0x03ee, 0, ms_string_desc, 0x12,
956 log_libusb_result(pdev->urbdrc->log, WLOG_DEBUG,
"libusb_control_transfer", error);
960 const BYTE bMS_Vendorcode = ms_string_desc[16];
962 error = libusb_control_transfer(
964 (uint8_t)LIBUSB_ENDPOINT_IN | (uint8_t)LIBUSB_REQUEST_TYPE_VENDOR | Recipient,
965 bMS_Vendorcode, (UINT16)((InterfaceNumber << 8) | Ms_PageIndex), Ms_featureDescIndex,
966 Buffer, (UINT16)*BufferSize, Timeout);
967 log_libusb_result(pdev->urbdrc->log, WLOG_DEBUG,
"libusb_control_transfer", error);
970 *BufferSize = (UINT32)error;
974 *UsbdStatus = USBD_STATUS_STALL_PID;
976 *UsbdStatus = USBD_STATUS_SUCCESS;
978 return ERROR_SUCCESS;
981 static int libusb_udev_query_device_descriptor(IUDEVICE* idev,
int offset)
988 return pdev->devDescriptor->bLength;
990 case B_DESCRIPTOR_TYPE:
991 return pdev->devDescriptor->bDescriptorType;
994 return pdev->devDescriptor->bcdUSB;
997 return pdev->devDescriptor->bDeviceClass;
999 case B_DEVICE_SUBCLASS:
1000 return pdev->devDescriptor->bDeviceSubClass;
1002 case B_DEVICE_PROTOCOL:
1003 return pdev->devDescriptor->bDeviceProtocol;
1005 case B_MAX_PACKET_SIZE0:
1006 return pdev->devDescriptor->bMaxPacketSize0;
1009 return pdev->devDescriptor->idVendor;
1012 return pdev->devDescriptor->idProduct;
1015 return pdev->devDescriptor->bcdDevice;
1017 case I_MANUFACTURER:
1018 return pdev->devDescriptor->iManufacturer;
1021 return pdev->devDescriptor->iProduct;
1023 case I_SERIAL_NUMBER:
1024 return pdev->devDescriptor->iSerialNumber;
1026 case B_NUM_CONFIGURATIONS:
1027 return pdev->devDescriptor->bNumConfigurations;
1034 static BOOL libusb_udev_detach_kernel_driver(IUDEVICE* idev)
1040 if (!pdev || !pdev->LibusbConfig || !pdev->libusb_handle || !pdev->urbdrc)
1046 urbdrc = pdev->urbdrc;
1048 if ((pdev->status & URBDRC_DEVICE_DETACH_KERNEL) == 0)
1050 for (
int i = 0; i < pdev->LibusbConfig->bNumInterfaces; i++)
1052 err = libusb_kernel_driver_active(pdev->libusb_handle, i);
1053 log_libusb_result(urbdrc->log, WLOG_DEBUG,
"libusb_kernel_driver_active", err);
1057 err = libusb_detach_kernel_driver(pdev->libusb_handle, i);
1058 log_libusb_result(urbdrc->log, WLOG_DEBUG,
"libusb_detach_kernel_driver", err);
1062 pdev->status |= URBDRC_DEVICE_DETACH_KERNEL;
1069 static BOOL libusb_udev_attach_kernel_driver(IUDEVICE* idev)
1074 if (!pdev || !pdev->LibusbConfig || !pdev->libusb_handle || !pdev->urbdrc)
1077 for (
int i = 0; i < pdev->LibusbConfig->bNumInterfaces && err != LIBUSB_ERROR_NO_DEVICE; i++)
1079 err = libusb_release_interface(pdev->libusb_handle, i);
1081 log_libusb_result(pdev->urbdrc->log, WLOG_DEBUG,
"libusb_release_interface", err);
1084 if (err != LIBUSB_ERROR_NO_DEVICE)
1086 err = libusb_attach_kernel_driver(pdev->libusb_handle, i);
1087 log_libusb_result(pdev->urbdrc->log, WLOG_DEBUG,
"libusb_attach_kernel_driver if=%d",
1096 static int libusb_udev_is_composite_device(IUDEVICE* idev)
1099 return pdev->isCompositeDevice;
1102 static int libusb_udev_is_exist(IUDEVICE* idev)
1105 return (pdev->status & URBDRC_DEVICE_NOT_FOUND) ? 0 : 1;
1108 static int libusb_udev_is_channel_closed(IUDEVICE* idev)
1111 IUDEVMAN* udevman = NULL;
1112 if (!pdev || !pdev->urbdrc)
1115 udevman = pdev->urbdrc->udevman;
1118 if (udevman->status & URBDRC_DEVICE_CHANNEL_CLOSED)
1122 if (pdev->status & URBDRC_DEVICE_CHANNEL_CLOSED)
1128 static int libusb_udev_is_already_send(IUDEVICE* idev)
1131 return (pdev->status & URBDRC_DEVICE_ALREADY_SEND) ? 1 : 0;
1136 static void libusb_udev_mark_channel_closed(IUDEVICE* idev)
1139 if (pdev && ((pdev->status & URBDRC_DEVICE_CHANNEL_CLOSED) == 0))
1142 const uint8_t busNr = idev->get_bus_number(idev);
1143 const uint8_t devNr = idev->get_dev_number(idev);
1145 pdev->status |= URBDRC_DEVICE_CHANNEL_CLOSED;
1146 urbdrc->udevman->unregister_udevice(urbdrc->udevman, busNr, devNr);
1152 static void libusb_udev_channel_closed(IUDEVICE* idev)
1155 if (pdev && ((pdev->status & URBDRC_DEVICE_CHANNEL_CLOSED) == 0))
1158 const uint8_t busNr = idev->get_bus_number(idev);
1159 const uint8_t devNr = idev->get_dev_number(idev);
1160 IWTSVirtualChannel* channel = NULL;
1162 if (pdev->channelManager)
1163 channel = IFCALLRESULT(NULL, pdev->channelManager->FindChannelById,
1164 pdev->channelManager, pdev->channelID);
1166 pdev->status |= URBDRC_DEVICE_CHANNEL_CLOSED;
1169 channel->Write(channel, 0, NULL, NULL);
1171 urbdrc->udevman->unregister_udevice(urbdrc->udevman, busNr, devNr);
1175 static void libusb_udev_set_already_send(IUDEVICE* idev)
1178 pdev->status |= URBDRC_DEVICE_ALREADY_SEND;
1181 static char* libusb_udev_get_path(IUDEVICE* idev)
1187 static int libusb_udev_query_device_port_status(IUDEVICE* idev, UINT32* UsbdStatus,
1188 UINT32* BufferSize, BYTE* Buffer)
1195 if (!pdev || !pdev->urbdrc)
1198 urbdrc = pdev->urbdrc;
1200 if (pdev->hub_handle != NULL)
1202 ret = idev->control_transfer(
1204 (uint8_t)LIBUSB_ENDPOINT_IN | (uint8_t)LIBUSB_REQUEST_TYPE_CLASS |
1205 (uint8_t)LIBUSB_RECIPIENT_OTHER,
1206 LIBUSB_REQUEST_GET_STATUS, 0, pdev->port_number, UsbdStatus, BufferSize, Buffer, 1000);
1208 if (log_libusb_result(urbdrc->log, WLOG_DEBUG,
"libusb_control_transfer", ret))
1212 WLog_Print(urbdrc->log, WLOG_DEBUG,
1213 "PORT STATUS:0x%02" PRIx8
"%02" PRIx8
"%02" PRIx8
"%02" PRIx8
"", Buffer[3],
1214 Buffer[2], Buffer[1], Buffer[0]);
1223 UINT32 MessageId, UINT32 RequestId, UINT32 EndpointAddress,
1224 UINT32 TransferFlags, UINT32 StartFrame, UINT32 ErrorCount,
1225 BOOL NoAck,
const BYTE* packetDescriptorData,
1226 UINT32 NumberOfPackets, UINT32 BufferSize,
const BYTE* Buffer,
1227 t_isoch_transfer_cb cb, UINT32 Timeout)
1230 UINT32 iso_packet_size = 0;
1232 ASYNC_TRANSFER_USER_DATA* user_data = NULL;
1233 struct libusb_transfer* iso_transfer = NULL;
1235 size_t outSize = (12ULL * NumberOfPackets);
1236 uint32_t streamID = 0x40000000 | RequestId;
1238 if (!pdev || !pdev->urbdrc)
1241 urbdrc = pdev->urbdrc;
1242 user_data = async_transfer_user_data_new(idev, MessageId, 48, BufferSize, Buffer,
1243 outSize + 1024, NoAck, cb, callback);
1248 user_data->ErrorCount = ErrorCount;
1249 user_data->StartFrame = StartFrame;
1252 Stream_Seek(user_data->data, (12ULL * NumberOfPackets));
1254 if (NumberOfPackets > 0)
1256 iso_packet_size = BufferSize / NumberOfPackets;
1257 iso_transfer = libusb_alloc_transfer((
int)NumberOfPackets);
1260 if (iso_transfer == NULL)
1262 WLog_Print(urbdrc->log, WLOG_ERROR,
1263 "Error: libusb_alloc_transfer [NumberOfPackets=%" PRIu32
", BufferSize=%" PRIu32
1265 NumberOfPackets, BufferSize);
1266 async_transfer_user_data_free(user_data);
1271 libusb_fill_iso_transfer(
1272 iso_transfer, pdev->libusb_handle, WINPR_ASSERTING_INT_CAST(uint8_t, EndpointAddress),
1273 Stream_Pointer(user_data->data), WINPR_ASSERTING_INT_CAST(
int, BufferSize),
1274 WINPR_ASSERTING_INT_CAST(
int, NumberOfPackets), func_iso_callback, user_data, Timeout);
1275 set_stream_id_for_buffer(iso_transfer, streamID);
1276 libusb_set_iso_packet_lengths(iso_transfer, iso_packet_size);
1278 if (!ArrayList_Append(pdev->request_queue, iso_transfer))
1280 WLog_Print(urbdrc->log, WLOG_WARN,
1281 "Failed to queue iso transfer, streamID %08" PRIx32
" already in use!",
1283 request_free(iso_transfer);
1286 rc = libusb_submit_transfer(iso_transfer);
1287 if (log_libusb_result(urbdrc->log, WLOG_ERROR,
"libusb_submit_transfer", rc))
1292 static BOOL libusb_udev_control_transfer(IUDEVICE* idev, UINT32 RequestId, UINT32 EndpointAddress,
1293 UINT32 TransferFlags, BYTE bmRequestType, BYTE Request,
1294 UINT16 Value, UINT16 Index, UINT32* UrbdStatus,
1295 UINT32* BufferSize, BYTE* Buffer, UINT32 Timeout)
1300 WINPR_ASSERT(BufferSize);
1301 WINPR_ASSERT(*BufferSize <= UINT16_MAX);
1303 if (!pdev || !pdev->urbdrc)
1306 status = libusb_control_transfer(pdev->libusb_handle, bmRequestType, Request, Value, Index,
1307 Buffer, (UINT16)*BufferSize, Timeout);
1310 *BufferSize = (UINT32)status;
1312 log_libusb_result(pdev->urbdrc->log, WLOG_ERROR,
"libusb_control_transfer", status);
1314 if (!func_set_usbd_status(pdev->urbdrc, pdev, UrbdStatus, status))
1320 static int libusb_udev_bulk_or_interrupt_transfer(IUDEVICE* idev,
1322 UINT32 MessageId, UINT32 RequestId,
1323 UINT32 EndpointAddress, UINT32 TransferFlags,
1324 BOOL NoAck, UINT32 BufferSize,
const BYTE* data,
1325 t_isoch_transfer_cb cb, UINT32 Timeout)
1328 UINT32 transfer_type = 0;
1330 const LIBUSB_ENDPOINT_DESCEIPTOR* ep_desc = NULL;
1331 struct libusb_transfer* transfer = NULL;
1333 ASYNC_TRANSFER_USER_DATA* user_data = NULL;
1334 uint32_t streamID = 0x80000000 | RequestId;
1336 if (!pdev || !pdev->LibusbConfig || !pdev->urbdrc)
1339 urbdrc = pdev->urbdrc;
1341 async_transfer_user_data_new(idev, MessageId, 36, BufferSize, data, 0, NoAck, cb, callback);
1347 transfer = libusb_alloc_transfer(0);
1350 async_transfer_user_data_free(user_data);
1353 transfer->user_data = user_data;
1355 ep_desc = func_get_ep_desc(pdev->LibusbConfig, pdev->MsConfig, EndpointAddress);
1359 WLog_Print(urbdrc->log, WLOG_ERROR,
"func_get_ep_desc: endpoint 0x%" PRIx32
" not found",
1361 request_free(transfer);
1365 transfer_type = (ep_desc->bmAttributes) & 0x3;
1366 WLog_Print(urbdrc->log, WLOG_DEBUG,
1367 "urb_bulk_or_interrupt_transfer: ep:0x%" PRIx32
" "
1368 "transfer_type %" PRIu32
" flag:%" PRIu32
" OutputBufferSize:0x%" PRIx32
"",
1369 EndpointAddress, transfer_type, TransferFlags, BufferSize);
1371 switch (transfer_type)
1375 libusb_fill_bulk_transfer(
1376 transfer, pdev->libusb_handle, WINPR_ASSERTING_INT_CAST(uint8_t, EndpointAddress),
1377 Stream_Pointer(user_data->data), WINPR_ASSERTING_INT_CAST(
int, BufferSize),
1378 func_bulk_transfer_cb, user_data, Timeout);
1381 case INTERRUPT_TRANSFER:
1383 libusb_fill_interrupt_transfer(
1384 transfer, pdev->libusb_handle, WINPR_ASSERTING_INT_CAST(uint8_t, EndpointAddress),
1385 Stream_Pointer(user_data->data), WINPR_ASSERTING_INT_CAST(
int, BufferSize),
1386 func_bulk_transfer_cb, user_data, Timeout);
1390 WLog_Print(urbdrc->log, WLOG_DEBUG,
1391 "urb_bulk_or_interrupt_transfer:"
1392 " other transfer type 0x%" PRIX32
"",
1394 request_free(transfer);
1398 set_stream_id_for_buffer(transfer, streamID);
1400 if (!ArrayList_Append(pdev->request_queue, transfer))
1402 WLog_Print(urbdrc->log, WLOG_WARN,
1403 "Failed to queue transfer, streamID %08" PRIx32
" already in use!", streamID);
1404 request_free(transfer);
1407 rc = libusb_submit_transfer(transfer);
1408 if (log_libusb_result(urbdrc->log, WLOG_ERROR,
"libusb_submit_transfer", rc))
1413 static int func_cancel_xact_request(
URBDRC_PLUGIN* urbdrc,
struct libusb_transfer* transfer)
1417 if (!urbdrc || !transfer)
1420 status = libusb_cancel_transfer(transfer);
1422 if (log_libusb_result(urbdrc->log, WLOG_WARN,
"libusb_cancel_transfer", status))
1424 if (status == LIBUSB_ERROR_NOT_FOUND)
1433 static void libusb_udev_cancel_all_transfer_request(IUDEVICE* idev)
1438 if (!pdev || !pdev->request_queue || !pdev->urbdrc)
1441 ArrayList_Lock(pdev->request_queue);
1442 count = ArrayList_Count(pdev->request_queue);
1444 for (
size_t x = 0; x < count; x++)
1446 struct libusb_transfer* transfer = ArrayList_GetItem(pdev->request_queue, x);
1447 func_cancel_xact_request(pdev->urbdrc, transfer);
1450 ArrayList_Unlock(pdev->request_queue);
1453 static int libusb_udev_cancel_transfer_request(IUDEVICE* idev, UINT32 RequestId)
1457 struct libusb_transfer* transfer = NULL;
1458 uint32_t cancelID1 = 0x40000000 | RequestId;
1459 uint32_t cancelID2 = 0x80000000 | RequestId;
1461 if (!idev || !pdev->urbdrc || !pdev->request_queue)
1464 ArrayList_Lock(pdev->request_queue);
1465 transfer = list_contains(pdev->request_queue, cancelID1);
1467 transfer = list_contains(pdev->request_queue, cancelID2);
1473 rc = func_cancel_xact_request(urbdrc, transfer);
1475 ArrayList_Unlock(pdev->request_queue);
1479 BASIC_STATE_FUNC_DEFINED(channelManager, IWTSVirtualChannelManager*)
1480 BASIC_STATE_FUNC_DEFINED(channelID, UINT32)
1481 BASIC_STATE_FUNC_DEFINED(ReqCompletion, UINT32)
1482 BASIC_STATE_FUNC_DEFINED(bus_number, BYTE)
1483 BASIC_STATE_FUNC_DEFINED(dev_number, BYTE)
1484 BASIC_STATE_FUNC_DEFINED(port_number, UINT8)
1487 BASIC_POINT_FUNC_DEFINED(udev,
void*)
1488 BASIC_POINT_FUNC_DEFINED(prev,
void*)
1489 BASIC_POINT_FUNC_DEFINED(next,
void*)
1491 static UINT32 udev_get_UsbDevice(IUDEVICE* idev)
1498 return pdev->UsbDevice;
1501 static void udev_set_UsbDevice(IUDEVICE* idev, UINT32 val)
1508 pdev->UsbDevice = val;
1511 static void udev_free(IUDEVICE* idev)
1517 if (!idev || !udev->urbdrc)
1520 urbdrc = udev->urbdrc;
1522 libusb_udev_cancel_all_transfer_request(&udev->iface);
1523 if (udev->libusb_handle)
1525 rc = libusb_reset_device(udev->libusb_handle);
1527 log_libusb_result(urbdrc->log, WLOG_ERROR,
"libusb_reset_device", rc);
1536 udev->iface.attach_kernel_driver(idev);
1537 ArrayList_Free(udev->request_queue);
1539 msusb_msconfig_free(udev->MsConfig);
1540 libusb_unref_device(udev->libusb_dev);
1541 libusb_close(udev->libusb_handle);
1542 libusb_close(udev->hub_handle);
1543 free(udev->devDescriptor);
1547 static void udev_load_interface(
UDEVICE* pdev)
1553 BASIC_STATE_FUNC_REGISTER(channelManager, pdev);
1554 BASIC_STATE_FUNC_REGISTER(channelID, pdev);
1555 BASIC_STATE_FUNC_REGISTER(UsbDevice, pdev);
1556 BASIC_STATE_FUNC_REGISTER(ReqCompletion, pdev);
1557 BASIC_STATE_FUNC_REGISTER(bus_number, pdev);
1558 BASIC_STATE_FUNC_REGISTER(dev_number, pdev);
1559 BASIC_STATE_FUNC_REGISTER(port_number, pdev);
1560 BASIC_STATE_FUNC_REGISTER(MsConfig, pdev);
1561 BASIC_STATE_FUNC_REGISTER(p_udev, pdev);
1562 BASIC_STATE_FUNC_REGISTER(p_prev, pdev);
1563 BASIC_STATE_FUNC_REGISTER(p_next, pdev);
1564 pdev->iface.isCompositeDevice = libusb_udev_is_composite_device;
1565 pdev->iface.isExist = libusb_udev_is_exist;
1566 pdev->iface.isAlreadySend = libusb_udev_is_already_send;
1567 pdev->iface.isChannelClosed = libusb_udev_is_channel_closed;
1568 pdev->iface.setAlreadySend = libusb_udev_set_already_send;
1569 pdev->iface.setChannelClosed = libusb_udev_channel_closed;
1570 pdev->iface.markChannelClosed = libusb_udev_mark_channel_closed;
1571 pdev->iface.getPath = libusb_udev_get_path;
1573 pdev->iface.isoch_transfer = libusb_udev_isoch_transfer;
1574 pdev->iface.control_transfer = libusb_udev_control_transfer;
1575 pdev->iface.bulk_or_interrupt_transfer = libusb_udev_bulk_or_interrupt_transfer;
1576 pdev->iface.select_interface = libusb_udev_select_interface;
1577 pdev->iface.select_configuration = libusb_udev_select_configuration;
1578 pdev->iface.complete_msconfig_setup = libusb_udev_complete_msconfig_setup;
1579 pdev->iface.control_pipe_request = libusb_udev_control_pipe_request;
1580 pdev->iface.control_query_device_text = libusb_udev_control_query_device_text;
1581 pdev->iface.os_feature_descriptor_request = libusb_udev_os_feature_descriptor_request;
1582 pdev->iface.cancel_all_transfer_request = libusb_udev_cancel_all_transfer_request;
1583 pdev->iface.cancel_transfer_request = libusb_udev_cancel_transfer_request;
1584 pdev->iface.query_device_descriptor = libusb_udev_query_device_descriptor;
1585 pdev->iface.detach_kernel_driver = libusb_udev_detach_kernel_driver;
1586 pdev->iface.attach_kernel_driver = libusb_udev_attach_kernel_driver;
1587 pdev->iface.query_device_port_status = libusb_udev_query_device_port_status;
1588 pdev->iface.free = udev_free;
1592 UINT16 bus_number, UINT16 dev_number)
1595 uint8_t port_numbers[16] = { 0 };
1596 LIBUSB_DEVICE** libusb_list = NULL;
1597 const ssize_t total_device = libusb_get_device_list(ctx, &libusb_list);
1599 WINPR_ASSERT(urbdrc);
1602 for (ssize_t i = 0; i < total_device; i++)
1604 LIBUSB_DEVICE* dev = libusb_list[i];
1606 if ((bus_number != libusb_get_bus_number(dev)) ||
1607 (dev_number != libusb_get_device_address(dev)))
1608 libusb_unref_device(dev);
1611 error = libusb_open(dev, &pdev->libusb_handle);
1613 if (log_libusb_result(urbdrc->log, WLOG_ERROR,
"libusb_open", error))
1615 libusb_unref_device(dev);
1620 error = libusb_get_port_numbers(dev, port_numbers,
sizeof(port_numbers));
1624 log_libusb_result(urbdrc->log, WLOG_ERROR,
"libusb_get_port_numbers", error);
1625 libusb_unref_device(dev);
1629 pdev->port_number = port_numbers[(error - 1)];
1631 WLog_Print(urbdrc->log, WLOG_DEBUG,
" Port: %" PRIu8, pdev->port_number);
1633 (void)_snprintf(pdev->path,
sizeof(pdev->path),
"%" PRIu16
"-%d", bus_number,
1636 WLog_Print(urbdrc->log, WLOG_DEBUG,
" DevPath: %s", pdev->path);
1639 libusb_free_device_list(libusb_list, 0);
1647 UINT16 bus_number, UINT16 dev_number)
1650 LIBUSB_DEVICE** libusb_list = NULL;
1651 LIBUSB_DEVICE_HANDLE* handle = NULL;
1652 const ssize_t total_device = libusb_get_device_list(ctx, &libusb_list);
1654 WINPR_ASSERT(urbdrc);
1657 for (ssize_t i = 0; i < total_device; i++)
1659 LIBUSB_DEVICE* dev = libusb_list[i];
1661 if ((bus_number != libusb_get_bus_number(dev)) ||
1662 (1 != libusb_get_device_address(dev)))
1663 libusb_unref_device(dev);
1666 WLog_Print(urbdrc->log, WLOG_DEBUG,
" Open hub: %" PRIu16
"", bus_number);
1667 error = libusb_open(dev, &handle);
1669 if (!log_libusb_result(urbdrc->log, WLOG_ERROR,
"libusb_open", error))
1670 pdev->hub_handle = handle;
1672 libusb_unref_device(dev);
1676 libusb_free_device_list(libusb_list, 0);
1684 static void request_free(
void* value)
1686 ASYNC_TRANSFER_USER_DATA* user_data = NULL;
1687 struct libusb_transfer* transfer = (
struct libusb_transfer*)value;
1691 user_data = (ASYNC_TRANSFER_USER_DATA*)transfer->user_data;
1692 async_transfer_user_data_free(user_data);
1693 transfer->user_data = NULL;
1694 libusb_free_transfer(transfer);
1697 static IUDEVICE* udev_init(
URBDRC_PLUGIN* urbdrc, libusb_context* context, LIBUSB_DEVICE* device,
1698 BYTE bus_number, BYTE dev_number)
1701 int status = LIBUSB_ERROR_OTHER;
1702 LIBUSB_DEVICE_DESCRIPTOR* devDescriptor = NULL;
1703 LIBUSB_CONFIG_DESCRIPTOR* config_temp = NULL;
1704 LIBUSB_INTERFACE_DESCRIPTOR interface_temp;
1706 WINPR_ASSERT(urbdrc);
1713 pdev->urbdrc = urbdrc;
1714 udev_load_interface(pdev);
1717 pdev->libusb_dev = device;
1719 pdev->libusb_dev = udev_get_libusb_dev(context, bus_number, dev_number);
1721 if (pdev->libusb_dev == NULL)
1724 if (urbdrc->listener_callback)
1725 udev_set_channelManager(&pdev->iface, urbdrc->listener_callback->channel_mgr);
1728 status = udev_get_device_handle(urbdrc, context, pdev, bus_number, dev_number);
1729 if (status != LIBUSB_SUCCESS)
1731 struct libusb_device_descriptor desc;
1732 const uint8_t port = libusb_get_port_number(pdev->libusb_dev);
1733 libusb_get_device_descriptor(pdev->libusb_dev, &desc);
1735 log_libusb_result(urbdrc->log, WLOG_ERROR,
1736 "libusb_open [b=0x%02X,p=0x%02X,a=0x%02X,VID=0x%04X,PID=0x%04X]", status,
1737 bus_number, port, dev_number, desc.idVendor, desc.idProduct);
1742 status = udev_get_hub_handle(urbdrc, context, pdev, bus_number, dev_number);
1745 pdev->hub_handle = NULL;
1747 pdev->devDescriptor = udev_new_descript(urbdrc, pdev->libusb_dev);
1749 if (!pdev->devDescriptor)
1752 status = libusb_get_active_config_descriptor(pdev->libusb_dev, &pdev->LibusbConfig);
1754 if (status == LIBUSB_ERROR_NOT_FOUND)
1755 status = libusb_get_config_descriptor(pdev->libusb_dev, 0, &pdev->LibusbConfig);
1760 config_temp = pdev->LibusbConfig;
1762 interface_temp = config_temp->interface[0].altsetting[0];
1763 WLog_Print(urbdrc->log, WLOG_DEBUG,
1764 "Registered Device: Vid: 0x%04" PRIX16
" Pid: 0x%04" PRIX16
""
1765 " InterfaceClass = %s",
1766 pdev->devDescriptor->idVendor, pdev->devDescriptor->idProduct,
1767 usb_interface_class_to_string(interface_temp.bInterfaceClass));
1769 devDescriptor = pdev->devDescriptor;
1771 if ((devDescriptor->bNumConfigurations == 1) && (config_temp->bNumInterfaces > 1) &&
1772 (devDescriptor->bDeviceClass == LIBUSB_CLASS_PER_INTERFACE))
1774 pdev->isCompositeDevice = 1;
1776 else if ((devDescriptor->bDeviceClass == 0xef) &&
1777 (devDescriptor->bDeviceSubClass == LIBUSB_CLASS_COMM) &&
1778 (devDescriptor->bDeviceProtocol == 0x01))
1780 pdev->isCompositeDevice = 1;
1783 pdev->isCompositeDevice = 0;
1786 devDescriptor->bDeviceClass = interface_temp.bInterfaceClass;
1787 devDescriptor->bDeviceSubClass = interface_temp.bInterfaceSubClass;
1788 devDescriptor->bDeviceProtocol = interface_temp.bInterfaceProtocol;
1790 pdev->bus_number = bus_number;
1791 pdev->dev_number = dev_number;
1792 pdev->request_queue = ArrayList_New(TRUE);
1794 if (!pdev->request_queue)
1797 ArrayList_Object(pdev->request_queue)->fnObjectFree = request_free;
1800 pdev->MsConfig = msusb_msconfig_new();
1802 if (!pdev->MsConfig)
1806 return &pdev->iface;
1808 pdev->iface.free(&pdev->iface);
1812 size_t udev_new_by_id(
URBDRC_PLUGIN* urbdrc, libusb_context* ctx, UINT16 idVendor, UINT16 idProduct,
1813 IUDEVICE*** devArray)
1815 LIBUSB_DEVICE** libusb_list = NULL;
1817 ssize_t total_device = 0;
1820 if (!urbdrc || !devArray)
1823 WLog_Print(urbdrc->log, WLOG_INFO,
"VID: 0x%04" PRIX16
", PID: 0x%04" PRIX16
"", idVendor,
1825 total_device = libusb_get_device_list(ctx, &libusb_list);
1826 if (total_device < 0)
1829 array = (
UDEVICE**)calloc((
size_t)total_device,
sizeof(
UDEVICE*));
1834 for (ssize_t i = 0; i < total_device; i++)
1836 LIBUSB_DEVICE* dev = libusb_list[i];
1837 LIBUSB_DEVICE_DESCRIPTOR* descriptor = udev_new_descript(urbdrc, dev);
1839 if ((descriptor->idVendor == idVendor) && (descriptor->idProduct == idProduct))
1841 array[num] = (
PUDEVICE)udev_init(urbdrc, ctx, dev, libusb_get_bus_number(dev),
1842 libusb_get_device_address(dev));
1844 if (array[num] != NULL)
1848 libusb_unref_device(dev);
1854 libusb_free_device_list(libusb_list, 0);
1855 *devArray = (IUDEVICE**)array;
1859 IUDEVICE* udev_new_by_addr(
URBDRC_PLUGIN* urbdrc, libusb_context* context, BYTE bus_number,
1862 WLog_Print(urbdrc->log, WLOG_DEBUG,
"bus:%d dev:%d", bus_number, dev_number);
1863 return udev_init(urbdrc, context, NULL, bus_number, dev_number);