FreeRDP
libusb_udevice.c
1 
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 
25 #include <winpr/wtypes.h>
26 #include <winpr/sysinfo.h>
27 #include <winpr/collections.h>
28 
29 #include <errno.h>
30 
31 #include "libusb_udevice.h"
32 #include "../common/urbdrc_types.h"
33 
34 #define BASIC_STATE_FUNC_DEFINED(_arg, _type) \
35  static _type udev_get_##_arg(IUDEVICE* idev) \
36  { \
37  UDEVICE* pdev = (UDEVICE*)idev; \
38  return pdev->_arg; \
39  } \
40  static void udev_set_##_arg(IUDEVICE* idev, _type _t) \
41  { \
42  UDEVICE* pdev = (UDEVICE*)idev; \
43  pdev->_arg = _t; \
44  }
45 
46 #define BASIC_POINT_FUNC_DEFINED(_arg, _type) \
47  static _type udev_get_p_##_arg(IUDEVICE* idev) \
48  { \
49  UDEVICE* pdev = (UDEVICE*)idev; \
50  return pdev->_arg; \
51  } \
52  static void udev_set_p_##_arg(IUDEVICE* idev, _type _t) \
53  { \
54  UDEVICE* pdev = (UDEVICE*)idev; \
55  pdev->_arg = _t; \
56  }
57 
58 #define BASIC_STATE_FUNC_REGISTER(_arg, _dev) \
59  _dev->iface.get_##_arg = udev_get_##_arg; \
60  (_dev)->iface.set_##_arg = udev_set_##_arg
61 
62 #if LIBUSB_API_VERSION >= 0x01000103
63 #define HAVE_STREAM_ID_API 1
64 #endif
65 
66 typedef struct
67 {
68  wStream* data;
69  BOOL noack;
70  UINT32 MessageId;
71  UINT32 StartFrame;
72  UINT32 ErrorCount;
73  IUDEVICE* idev;
74  UINT32 OutputBufferSize;
75  GENERIC_CHANNEL_CALLBACK* callback;
76  t_isoch_transfer_cb cb;
77  wArrayList* queue;
78 #if !defined(HAVE_STREAM_ID_API)
79  UINT32 streamID;
80 #endif
81 } ASYNC_TRANSFER_USER_DATA;
82 
83 static void request_free(void* value);
84 
85 static struct libusb_transfer* list_contains(wArrayList* list, UINT32 streamID)
86 {
87  size_t count = 0;
88  if (!list)
89  return NULL;
90  count = ArrayList_Count(list);
91  for (size_t x = 0; x < count; x++)
92  {
93  struct libusb_transfer* transfer = ArrayList_GetItem(list, x);
94 
95 #if defined(HAVE_STREAM_ID_API)
96  const UINT32 currentID = libusb_transfer_get_stream_id(transfer);
97 #else
98  const ASYNC_TRANSFER_USER_DATA* user_data = (ASYNC_TRANSFER_USER_DATA*)transfer->user_data;
99  const UINT32 currentID = user_data->streamID;
100 #endif
101  if (currentID == streamID)
102  return transfer;
103  }
104  return NULL;
105 }
106 
107 static UINT32 stream_id_from_buffer(struct libusb_transfer* transfer)
108 {
109  if (!transfer)
110  return 0;
111 #if defined(HAVE_STREAM_ID_API)
112  return libusb_transfer_get_stream_id(transfer);
113 #else
114  ASYNC_TRANSFER_USER_DATA* user_data = (ASYNC_TRANSFER_USER_DATA*)transfer->user_data;
115  if (!user_data)
116  return 0;
117  return user_data->streamID;
118 #endif
119 }
120 
121 static void set_stream_id_for_buffer(struct libusb_transfer* transfer, UINT32 streamID)
122 {
123 #if defined(HAVE_STREAM_ID_API)
124  libusb_transfer_set_stream_id(transfer, streamID);
125 #else
126  ASYNC_TRANSFER_USER_DATA* user_data = (ASYNC_TRANSFER_USER_DATA*)transfer->user_data;
127  if (!user_data)
128  return;
129  user_data->streamID = streamID;
130 #endif
131 }
132 
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, ...)
136 {
137  WINPR_UNUSED(file);
138 
139  if (error < 0)
140  {
141  char buffer[8192] = { 0 };
142  va_list ap = { 0 };
143  va_start(ap, error);
144  (void)vsnprintf(buffer, sizeof(buffer), fmt, ap);
145  va_end(ap);
146 
147  WLog_Print(log, lvl, "[%s:%" PRIuz "]: %s: error %s[%d]", fkt, line, buffer,
148  libusb_error_name(error), error);
149  return TRUE;
150  }
151  return FALSE;
152 }
153 
154 #define log_libusb_result(log, lvl, fmt, error, ...) \
155  log_libusb_result_((log), (lvl), (fmt), __func__, __FILE__, __LINE__, error, ##__VA_ARGS__)
156 
157 const char* usb_interface_class_to_string(uint8_t class)
158 {
159  switch (class)
160  {
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";
197  default:
198  return "UNKNOWN_DEVICE_CLASS";
199  }
200 }
201 
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,
206  GENERIC_CHANNEL_CALLBACK* callback)
207 {
208  ASYNC_TRANSFER_USER_DATA* user_data = NULL;
209  UDEVICE* pdev = (UDEVICE*)idev;
210 
211  if (BufferSize > UINT32_MAX)
212  return NULL;
213 
214  user_data = calloc(1, sizeof(ASYNC_TRANSFER_USER_DATA));
215  if (!user_data)
216  return NULL;
217 
218  user_data->data = Stream_New(NULL, offset + BufferSize + packetSize);
219 
220  if (!user_data->data)
221  {
222  free(user_data);
223  return NULL;
224  }
225 
226  Stream_Seek(user_data->data, offset); /* Skip header offset */
227  if (data)
228  memcpy(Stream_Pointer(user_data->data), data, BufferSize);
229  else
230  user_data->OutputBufferSize = (UINT32)BufferSize;
231 
232  user_data->noack = NoAck;
233  user_data->cb = cb;
234  user_data->callback = callback;
235  user_data->idev = idev;
236  user_data->MessageId = MessageId;
237 
238  user_data->queue = pdev->request_queue;
239 
240  return user_data;
241 }
242 
243 static void async_transfer_user_data_free(ASYNC_TRANSFER_USER_DATA* user_data)
244 {
245  if (user_data)
246  {
247  Stream_Free(user_data->data, TRUE);
248  free(user_data);
249  }
250 }
251 
252 static void LIBUSB_CALL func_iso_callback(struct libusb_transfer* transfer)
253 {
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;
257 
258  ArrayList_Lock(list);
259  switch (transfer->status)
260  {
261  case LIBUSB_TRANSFER_COMPLETED:
262  {
263  UINT32 index = 0;
264  BYTE* dataStart = Stream_Pointer(user_data->data);
265  Stream_SetPosition(user_data->data,
266  40); /* TS_URB_ISOCH_TRANSFER_RESULT IsoPacket offset */
267 
268  for (int i = 0; i < transfer->num_iso_packets; i++)
269  {
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);
274 
275  if (transfer->iso_packet_desc[i].status != USBD_STATUS_SUCCESS)
276  user_data->ErrorCount++;
277  else
278  {
279  const unsigned char* packetBuffer =
280  libusb_get_iso_packet_buffer_simple(transfer, i);
281  BYTE* data = dataStart + index;
282 
283  if (data != packetBuffer)
284  memmove(data, packetBuffer, act_len);
285 
286  index += act_len;
287  }
288  }
289  }
290  /* fallthrough */
291  WINPR_FALLTHROUGH
292  case LIBUSB_TRANSFER_CANCELLED:
293  /* fallthrough */
294  WINPR_FALLTHROUGH
295  case LIBUSB_TRANSFER_TIMED_OUT:
296  /* fallthrough */
297  WINPR_FALLTHROUGH
298  case LIBUSB_TRANSFER_ERROR:
299  {
300  const UINT32 InterfaceId =
301  ((STREAM_ID_PROXY << 30) | user_data->idev->get_ReqCompletion(user_data->idev));
302 
303  if (list_contains(list, streamID))
304  {
305  if (!user_data->noack)
306  {
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;
314  }
315  ArrayList_Remove(list, transfer);
316  }
317  }
318  break;
319  default:
320  break;
321  }
322  ArrayList_Unlock(list);
323 }
324 
325 static const LIBUSB_ENDPOINT_DESCEIPTOR* func_get_ep_desc(LIBUSB_CONFIG_DESCRIPTOR* LibusbConfig,
326  MSUSB_CONFIG_DESCRIPTOR* MsConfig,
327  UINT32 EndpointAddress)
328 {
329  MSUSB_INTERFACE_DESCRIPTOR** MsInterfaces = MsConfig->MsInterfaces;
330  const LIBUSB_INTERFACE* interface = LibusbConfig->interface;
331 
332  for (UINT32 inum = 0; inum < MsConfig->NumInterfaces; inum++)
333  {
334  BYTE alt = MsInterfaces[inum]->AlternateSetting;
335  const LIBUSB_ENDPOINT_DESCEIPTOR* endpoint = interface[inum].altsetting[alt].endpoint;
336 
337  for (UINT32 pnum = 0; pnum < MsInterfaces[inum]->NumberOfPipes; pnum++)
338  {
339  if (endpoint[pnum].bEndpointAddress == EndpointAddress)
340  {
341  return &endpoint[pnum];
342  }
343  }
344  }
345 
346  return NULL;
347 }
348 
349 static void LIBUSB_CALL func_bulk_transfer_cb(struct libusb_transfer* transfer)
350 {
351  ASYNC_TRANSFER_USER_DATA* user_data = NULL;
352  uint32_t streamID = 0;
353  wArrayList* list = NULL;
354 
355  user_data = (ASYNC_TRANSFER_USER_DATA*)transfer->user_data;
356  if (!user_data)
357  {
358  WLog_ERR(TAG, "[%s]: Invalid transfer->user_data!");
359  return;
360  }
361  list = user_data->queue;
362  ArrayList_Lock(list);
363  streamID = stream_id_from_buffer(transfer);
364 
365  if (list_contains(list, streamID))
366  {
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;
370 
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);
377  }
378  ArrayList_Unlock(list);
379 }
380 
381 static BOOL func_set_usbd_status(URBDRC_PLUGIN* urbdrc, UDEVICE* pdev, UINT32* status,
382  int err_result)
383 {
384  if (!urbdrc || !status)
385  return FALSE;
386 
387  switch (err_result)
388  {
389  case LIBUSB_SUCCESS:
390  *status = USBD_STATUS_SUCCESS;
391  break;
392 
393  case LIBUSB_ERROR_IO:
394  *status = USBD_STATUS_STALL_PID;
395  break;
396 
397  case LIBUSB_ERROR_INVALID_PARAM:
398  *status = USBD_STATUS_INVALID_PARAMETER;
399  break;
400 
401  case LIBUSB_ERROR_ACCESS:
402  *status = USBD_STATUS_NOT_ACCESSED;
403  break;
404 
405  case LIBUSB_ERROR_NO_DEVICE:
406  *status = USBD_STATUS_DEVICE_GONE;
407 
408  if (pdev)
409  {
410  if (!(pdev->status & URBDRC_DEVICE_NOT_FOUND))
411  pdev->status |= URBDRC_DEVICE_NOT_FOUND;
412  }
413 
414  break;
415 
416  case LIBUSB_ERROR_NOT_FOUND:
417  *status = USBD_STATUS_STALL_PID;
418  break;
419 
420  case LIBUSB_ERROR_BUSY:
421  *status = USBD_STATUS_STALL_PID;
422  break;
423 
424  case LIBUSB_ERROR_TIMEOUT:
425  *status = USBD_STATUS_TIMEOUT;
426  break;
427 
428  case LIBUSB_ERROR_OVERFLOW:
429  *status = USBD_STATUS_STALL_PID;
430  break;
431 
432  case LIBUSB_ERROR_PIPE:
433  *status = USBD_STATUS_STALL_PID;
434  break;
435 
436  case LIBUSB_ERROR_INTERRUPTED:
437  *status = USBD_STATUS_STALL_PID;
438  break;
439 
440  case LIBUSB_ERROR_NO_MEM:
441  *status = USBD_STATUS_NO_MEMORY;
442  break;
443 
444  case LIBUSB_ERROR_NOT_SUPPORTED:
445  *status = USBD_STATUS_NOT_SUPPORTED;
446  break;
447 
448  case LIBUSB_ERROR_OTHER:
449  *status = USBD_STATUS_STALL_PID;
450  break;
451 
452  default:
453  *status = USBD_STATUS_SUCCESS;
454  break;
455  }
456 
457  return TRUE;
458 }
459 
460 static int func_config_release_all_interface(URBDRC_PLUGIN* urbdrc,
461  LIBUSB_DEVICE_HANDLE* libusb_handle,
462  UINT32 NumInterfaces)
463 {
464  if (NumInterfaces > INT32_MAX)
465  return -1;
466  for (INT32 i = 0; i < (INT32)NumInterfaces; i++)
467  {
468  int ret = libusb_release_interface(libusb_handle, i);
469 
470  if (log_libusb_result(urbdrc->log, WLOG_WARN, "libusb_release_interface", ret))
471  return -1;
472  }
473 
474  return 0;
475 }
476 
477 static int func_claim_all_interface(URBDRC_PLUGIN* urbdrc, LIBUSB_DEVICE_HANDLE* libusb_handle,
478  int NumInterfaces)
479 {
480  int ret = 0;
481 
482  for (int i = 0; i < NumInterfaces; i++)
483  {
484  ret = libusb_claim_interface(libusb_handle, i);
485 
486  if (log_libusb_result(urbdrc->log, WLOG_ERROR, "libusb_claim_interface", ret))
487  return -1;
488  }
489 
490  return 0;
491 }
492 
493 static LIBUSB_DEVICE* udev_get_libusb_dev(libusb_context* context, uint8_t bus_number,
494  uint8_t dev_number)
495 {
496  LIBUSB_DEVICE** libusb_list = NULL;
497  LIBUSB_DEVICE* device = NULL;
498  const ssize_t total_device = libusb_get_device_list(context, &libusb_list);
499 
500  for (ssize_t i = 0; i < total_device; i++)
501  {
502  LIBUSB_DEVICE* dev = libusb_list[i];
503  if ((bus_number == libusb_get_bus_number(dev)) &&
504  (dev_number == libusb_get_device_address(dev)))
505  device = dev;
506  else
507  libusb_unref_device(dev);
508  }
509 
510  libusb_free_device_list(libusb_list, 0);
511  return device;
512 }
513 
514 static LIBUSB_DEVICE_DESCRIPTOR* udev_new_descript(URBDRC_PLUGIN* urbdrc, LIBUSB_DEVICE* libusb_dev)
515 {
516  int ret = 0;
517  LIBUSB_DEVICE_DESCRIPTOR* descriptor =
518  (LIBUSB_DEVICE_DESCRIPTOR*)calloc(1, sizeof(LIBUSB_DEVICE_DESCRIPTOR));
519  if (!descriptor)
520  return NULL;
521  ret = libusb_get_device_descriptor(libusb_dev, descriptor);
522 
523  if (log_libusb_result(urbdrc->log, WLOG_ERROR, "libusb_get_device_descriptor", ret))
524  {
525  free(descriptor);
526  return NULL;
527  }
528 
529  return descriptor;
530 }
531 
532 static int libusb_udev_select_interface(IUDEVICE* idev, BYTE InterfaceNumber, BYTE AlternateSetting)
533 {
534  int error = 0;
535  int diff = 0;
536  UDEVICE* pdev = (UDEVICE*)idev;
537  URBDRC_PLUGIN* urbdrc = NULL;
538  MSUSB_CONFIG_DESCRIPTOR* MsConfig = NULL;
539  MSUSB_INTERFACE_DESCRIPTOR** MsInterfaces = NULL;
540 
541  if (!pdev || !pdev->urbdrc)
542  return -1;
543 
544  urbdrc = pdev->urbdrc;
545  MsConfig = pdev->MsConfig;
546 
547  if (MsConfig)
548  {
549  MsInterfaces = MsConfig->MsInterfaces;
550  if (MsInterfaces)
551  {
552  WLog_Print(urbdrc->log, WLOG_INFO,
553  "select Interface(%" PRIu8 ") curr AlternateSetting(%" PRIu8
554  ") new AlternateSetting(%" PRIu8 ")",
555  InterfaceNumber, MsInterfaces[InterfaceNumber]->AlternateSetting,
556  AlternateSetting);
557 
558  if (MsInterfaces[InterfaceNumber]->AlternateSetting != AlternateSetting)
559  {
560  diff = 1;
561  }
562  }
563 
564  if (diff)
565  {
566  error = libusb_set_interface_alt_setting(pdev->libusb_handle, InterfaceNumber,
567  AlternateSetting);
568 
569  log_libusb_result(urbdrc->log, WLOG_ERROR, "libusb_set_interface_alt_setting", error);
570  }
571  }
572 
573  return error;
574 }
575 
577 libusb_udev_complete_msconfig_setup(IUDEVICE* idev, MSUSB_CONFIG_DESCRIPTOR* MsConfig)
578 {
579  UDEVICE* pdev = (UDEVICE*)idev;
580  MSUSB_INTERFACE_DESCRIPTOR** MsInterfaces = NULL;
581  MSUSB_INTERFACE_DESCRIPTOR* MsInterface = NULL;
582  MSUSB_PIPE_DESCRIPTOR** MsPipes = NULL;
583  MSUSB_PIPE_DESCRIPTOR* MsPipe = NULL;
584  MSUSB_PIPE_DESCRIPTOR** t_MsPipes = NULL;
585  MSUSB_PIPE_DESCRIPTOR* t_MsPipe = NULL;
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;
591  URBDRC_PLUGIN* urbdrc = NULL;
592  UINT32 MsOutSize = 0;
593 
594  if (!pdev || !pdev->LibusbConfig || !pdev->urbdrc || !MsConfig)
595  return NULL;
596 
597  urbdrc = pdev->urbdrc;
598  LibusbConfig = pdev->LibusbConfig;
599 
600  if (LibusbConfig->bNumInterfaces != MsConfig->NumInterfaces)
601  {
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);
606  }
607 
608  /* replace MsPipes for libusb */
609  MsInterfaces = MsConfig->MsInterfaces;
610 
611  for (UINT32 inum = 0; inum < MsConfig->NumInterfaces; inum++)
612  {
613  MsInterface = MsInterfaces[inum];
614  /* get libusb's number of endpoints */
615  LibusbInterface = &LibusbConfig->interface[MsInterface->InterfaceNumber];
616  LibusbAltsetting = &LibusbInterface->altsetting[MsInterface->AlternateSetting];
617  LibusbNumEndpoint = LibusbAltsetting->bNumEndpoints;
618  t_MsPipes =
619  (MSUSB_PIPE_DESCRIPTOR**)calloc(LibusbNumEndpoint, sizeof(MSUSB_PIPE_DESCRIPTOR*));
620 
621  for (UINT32 pnum = 0; pnum < LibusbNumEndpoint; pnum++)
622  {
623  t_MsPipe = (MSUSB_PIPE_DESCRIPTOR*)calloc(1, sizeof(MSUSB_PIPE_DESCRIPTOR));
624 
625  if (pnum < MsInterface->NumberOfPipes && MsInterface->MsPipes)
626  {
627  MsPipe = MsInterface->MsPipes[pnum];
628  t_MsPipe->MaximumPacketSize = MsPipe->MaximumPacketSize;
629  t_MsPipe->MaximumTransferSize = MsPipe->MaximumTransferSize;
630  t_MsPipe->PipeFlags = MsPipe->PipeFlags;
631  }
632  else
633  {
634  t_MsPipe->MaximumPacketSize = 0;
635  t_MsPipe->MaximumTransferSize = 0xffffffff;
636  t_MsPipe->PipeFlags = 0;
637  }
638 
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;
645  }
646 
647  msusb_mspipes_replace(MsInterface, t_MsPipes, LibusbNumEndpoint);
648  }
649 
650  /* setup configuration */
651  MsOutSize = 8;
652  /* ConfigurationHandle: 4 bytes
653  * ---------------------------------------------------------------
654  * ||<<< 1 byte >>>|<<< 1 byte >>>|<<<<<<<<<< 2 byte >>>>>>>>>>>||
655  * || bus_number | dev_number | bConfigurationValue ||
656  * ---------------------------------------------------------------
657  * ***********************/
658  MsConfig->ConfigurationHandle =
659  MsConfig->bConfigurationValue | (pdev->bus_number << 24) | (pdev->dev_number << 16);
660  MsInterfaces = MsConfig->MsInterfaces;
661 
662  for (UINT32 inum = 0; inum < MsConfig->NumInterfaces; inum++)
663  {
664  MsOutSize += 16;
665  MsInterface = MsInterfaces[inum];
666  /* get libusb's interface */
667  LibusbInterface = &LibusbConfig->interface[MsInterface->InterfaceNumber];
668  LibusbAltsetting = &LibusbInterface->altsetting[MsInterface->AlternateSetting];
669  /* InterfaceHandle: 4 bytes
670  * ---------------------------------------------------------------
671  * ||<<< 1 byte >>>|<<< 1 byte >>>|<<< 1 byte >>>|<<< 1 byte >>>||
672  * || bus_number | dev_number | altsetting | interfaceNum ||
673  * ---------------------------------------------------------------
674  * ***********************/
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;
685 
686  for (UINT32 pnum = 0; pnum < LibusbNumEndpoint; pnum++)
687  {
688  MsOutSize += 20;
689  MsPipe = MsPipes[pnum];
690  /* get libusb's endpoint */
691  LibusbEndpoint = &LibusbAltsetting->endpoint[pnum];
692  /* PipeHandle: 4 bytes
693  * ---------------------------------------------------------------
694  * ||<<< 1 byte >>>|<<< 1 byte >>>|<<<<<<<<<< 2 byte >>>>>>>>>>>||
695  * || bus_number | dev_number | bEndpointAddress ||
696  * ---------------------------------------------------------------
697  * ***********************/
698  MsPipe->PipeHandle = LibusbEndpoint->bEndpointAddress | (pdev->dev_number << 16) |
699  (pdev->bus_number << 24);
700  /* count endpoint max packet size */
701  int max = LibusbEndpoint->wMaxPacketSize & 0x07ff;
702  BYTE attr = LibusbEndpoint->bmAttributes;
703 
704  if ((attr & 0x3) == 1 || (attr & 0x3) == 3)
705  {
706  max *= (1 + ((LibusbEndpoint->wMaxPacketSize >> 11) & 3));
707  }
708 
709  MsPipe->MaximumPacketSize = max;
710  MsPipe->bEndpointAddress = LibusbEndpoint->bEndpointAddress;
711  MsPipe->bInterval = LibusbEndpoint->bInterval;
712  MsPipe->PipeType = attr & 0x3;
713  MsPipe->InitCompleted = 1;
714  }
715  }
716 
717  MsConfig->MsOutSize = MsOutSize;
718  MsConfig->InitCompleted = 1;
719 
720  /* replace device's MsConfig */
721  if (MsConfig != pdev->MsConfig)
722  {
723  msusb_msconfig_free(pdev->MsConfig);
724  pdev->MsConfig = MsConfig;
725  }
726 
727  return MsConfig;
728 }
729 
730 static int libusb_udev_select_configuration(IUDEVICE* idev, UINT32 bConfigurationValue)
731 {
732  UDEVICE* pdev = (UDEVICE*)idev;
733  MSUSB_CONFIG_DESCRIPTOR* MsConfig = NULL;
734  LIBUSB_DEVICE_HANDLE* libusb_handle = NULL;
735  LIBUSB_DEVICE* libusb_dev = NULL;
736  URBDRC_PLUGIN* urbdrc = NULL;
737  LIBUSB_CONFIG_DESCRIPTOR** LibusbConfig = NULL;
738  int ret = 0;
739 
740  if (!pdev || !pdev->MsConfig || !pdev->LibusbConfig || !pdev->urbdrc)
741  return -1;
742 
743  urbdrc = pdev->urbdrc;
744  MsConfig = pdev->MsConfig;
745  libusb_handle = pdev->libusb_handle;
746  libusb_dev = pdev->libusb_dev;
747  LibusbConfig = &pdev->LibusbConfig;
748 
749  if (MsConfig->InitCompleted)
750  {
751  func_config_release_all_interface(pdev->urbdrc, libusb_handle,
752  (*LibusbConfig)->bNumInterfaces);
753  }
754 
755  /* The configuration value -1 is mean to put the device in unconfigured state. */
756  if (bConfigurationValue == 0)
757  ret = libusb_set_configuration(libusb_handle, -1);
758  else
759  ret = libusb_set_configuration(libusb_handle, bConfigurationValue);
760 
761  if (log_libusb_result(urbdrc->log, WLOG_ERROR, "libusb_set_configuration", ret))
762  {
763  func_claim_all_interface(urbdrc, libusb_handle, (*LibusbConfig)->bNumInterfaces);
764  return -1;
765  }
766  else
767  {
768  ret = libusb_get_active_config_descriptor(libusb_dev, LibusbConfig);
769 
770  if (log_libusb_result(urbdrc->log, WLOG_ERROR, "libusb_set_configuration", ret))
771  {
772  func_claim_all_interface(urbdrc, libusb_handle, (*LibusbConfig)->bNumInterfaces);
773  return -1;
774  }
775  }
776 
777  func_claim_all_interface(urbdrc, libusb_handle, (*LibusbConfig)->bNumInterfaces);
778  return 0;
779 }
780 
781 static int libusb_udev_control_pipe_request(IUDEVICE* idev, UINT32 RequestId,
782  UINT32 EndpointAddress, UINT32* UsbdStatus, int command)
783 {
784  int error = 0;
785  UDEVICE* pdev = (UDEVICE*)idev;
786 
787  WINPR_ASSERT(EndpointAddress <= UINT8_MAX);
788  /*
789  pdev->request_queue->register_request(pdev->request_queue, RequestId, NULL, 0);
790  */
791  switch (command)
792  {
793  case PIPE_CANCEL:
795  idev->cancel_all_transfer_request(idev);
796  // dummy_wait_s_obj(1);
798  /*
799  uint8_t request_type, uint8_t bRequest,
800  */
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);
805  break;
806 
807  case PIPE_RESET:
808  idev->cancel_all_transfer_request(idev);
809  error = libusb_clear_halt(pdev->libusb_handle, (uint8_t)EndpointAddress);
810  // func_set_usbd_status(pdev, UsbdStatus, error);
811  break;
812 
813  default:
814  error = -0xff;
815  break;
816  }
817 
818  *UsbdStatus = 0;
819  return error;
820 }
821 
822 static UINT32 libusb_udev_control_query_device_text(IUDEVICE* idev, UINT32 TextType,
823  UINT16 LocaleId, UINT8* BufferSize,
824  BYTE* Buffer)
825 {
826  UDEVICE* pdev = (UDEVICE*)idev;
827  LIBUSB_DEVICE_DESCRIPTOR* devDescriptor = NULL;
828  const char strDesc[] = "Generic Usb String";
829  char deviceLocation[25] = { 0 };
830  BYTE bus_number = 0;
831  BYTE device_address = 0;
832  int ret = 0;
833  size_t len = 0;
834  URBDRC_PLUGIN* urbdrc = NULL;
835  WCHAR* text = (WCHAR*)Buffer;
836  BYTE slen = 0;
837  BYTE locale = 0;
838  const UINT8 inSize = *BufferSize;
839 
840  *BufferSize = 0;
841  if (!pdev || !pdev->devDescriptor || !pdev->urbdrc)
842  return ERROR_INVALID_DATA;
843 
844  urbdrc = pdev->urbdrc;
845  devDescriptor = pdev->devDescriptor;
846 
847  switch (TextType)
848  {
849  case DeviceTextDescription:
850  {
851  BYTE data[0x100] = { 0 };
852  ret = libusb_get_string_descriptor(pdev->libusb_handle, devDescriptor->iProduct,
853  LocaleId, data, 0xFF);
854  /* The returned data in the buffer is:
855  * 1 byte length of following data
856  * 1 byte descriptor type, must be 0x03 for strings
857  * n WCHAR unicode string (of length / 2 characters) including '\0'
858  */
859  slen = data[0];
860  locale = data[1];
861 
862  if ((ret <= 0) || (ret <= 4) || (slen <= 4) || (locale != LIBUSB_DT_STRING) ||
863  (ret > UINT8_MAX))
864  {
865  const char* msg = "SHORT_DESCRIPTOR";
866  if (ret < 0)
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);
872 
873  len = MIN(sizeof(strDesc), inSize);
874  for (size_t i = 0; i < len; i++)
875  text[i] = (WCHAR)strDesc[i];
876 
877  *BufferSize = (BYTE)(len * 2);
878  }
879  else
880  {
881  /* ret and slen should be equals, but you never know creativity
882  * of device manufacturers...
883  * So also check the string length returned as server side does
884  * not honor strings with multi '\0' characters well.
885  */
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);
891 
892  /* Just as above, the returned WCHAR string should be '\0'
893  * terminated, but never trust hardware to conform to specs... */
894  Buffer[len - 2] = '\0';
895  Buffer[len - 1] = '\0';
896  *BufferSize = (BYTE)len;
897  }
898  }
899  break;
900 
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);
906 
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];
911  text[len++] = '\0';
912  *BufferSize = (UINT8)(len * sizeof(WCHAR));
913  break;
914 
915  default:
916  WLog_Print(urbdrc->log, WLOG_DEBUG, "Query Text: unknown TextType %" PRIu32 "",
917  TextType);
918  return ERROR_INVALID_DATA;
919  }
920 
921  return S_OK;
922 }
923 
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)
929 {
930  UDEVICE* pdev = (UDEVICE*)idev;
931  BYTE ms_string_desc[0x13] = { 0 };
932  int error = 0;
933 
934  WINPR_ASSERT(idev);
935  WINPR_ASSERT(UsbdStatus);
936  WINPR_ASSERT(BufferSize);
937  WINPR_ASSERT(*BufferSize <= UINT16_MAX);
938 
939  /*
940  pdev->request_queue->register_request(pdev->request_queue, RequestId, NULL, 0);
941  */
942  error = libusb_control_transfer(pdev->libusb_handle, LIBUSB_ENDPOINT_IN | Recipient,
943  LIBUSB_REQUEST_GET_DESCRIPTOR, 0x03ee, 0, ms_string_desc, 0x12,
944  Timeout);
945 
946  log_libusb_result(pdev->urbdrc->log, WLOG_DEBUG, "libusb_control_transfer", error);
947 
948  if (error > 0)
949  {
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);
957 
958  if (error >= 0)
959  *BufferSize = (UINT32)error;
960  }
961 
962  if (error < 0)
963  *UsbdStatus = USBD_STATUS_STALL_PID;
964  else
965  *UsbdStatus = USBD_STATUS_SUCCESS;
966 
967  return ERROR_SUCCESS;
968 }
969 
970 static int libusb_udev_query_device_descriptor(IUDEVICE* idev, int offset)
971 {
972  UDEVICE* pdev = (UDEVICE*)idev;
973 
974  switch (offset)
975  {
976  case B_LENGTH:
977  return pdev->devDescriptor->bLength;
978 
979  case B_DESCRIPTOR_TYPE:
980  return pdev->devDescriptor->bDescriptorType;
981 
982  case BCD_USB:
983  return pdev->devDescriptor->bcdUSB;
984 
985  case B_DEVICE_CLASS:
986  return pdev->devDescriptor->bDeviceClass;
987 
988  case B_DEVICE_SUBCLASS:
989  return pdev->devDescriptor->bDeviceSubClass;
990 
991  case B_DEVICE_PROTOCOL:
992  return pdev->devDescriptor->bDeviceProtocol;
993 
994  case B_MAX_PACKET_SIZE0:
995  return pdev->devDescriptor->bMaxPacketSize0;
996 
997  case ID_VENDOR:
998  return pdev->devDescriptor->idVendor;
999 
1000  case ID_PRODUCT:
1001  return pdev->devDescriptor->idProduct;
1002 
1003  case BCD_DEVICE:
1004  return pdev->devDescriptor->bcdDevice;
1005 
1006  case I_MANUFACTURER:
1007  return pdev->devDescriptor->iManufacturer;
1008 
1009  case I_PRODUCT:
1010  return pdev->devDescriptor->iProduct;
1011 
1012  case I_SERIAL_NUMBER:
1013  return pdev->devDescriptor->iSerialNumber;
1014 
1015  case B_NUM_CONFIGURATIONS:
1016  return pdev->devDescriptor->bNumConfigurations;
1017 
1018  default:
1019  return 0;
1020  }
1021 }
1022 
1023 static BOOL libusb_udev_detach_kernel_driver(IUDEVICE* idev)
1024 {
1025  int err = 0;
1026  UDEVICE* pdev = (UDEVICE*)idev;
1027  URBDRC_PLUGIN* urbdrc = NULL;
1028 
1029  if (!pdev || !pdev->LibusbConfig || !pdev->libusb_handle || !pdev->urbdrc)
1030  return FALSE;
1031 
1032 #ifdef _WIN32
1033  return TRUE;
1034 #else
1035  urbdrc = pdev->urbdrc;
1036 
1037  if ((pdev->status & URBDRC_DEVICE_DETACH_KERNEL) == 0)
1038  {
1039  for (int i = 0; i < pdev->LibusbConfig->bNumInterfaces; i++)
1040  {
1041  err = libusb_kernel_driver_active(pdev->libusb_handle, i);
1042  log_libusb_result(urbdrc->log, WLOG_DEBUG, "libusb_kernel_driver_active", err);
1043 
1044  if (err)
1045  {
1046  err = libusb_detach_kernel_driver(pdev->libusb_handle, i);
1047  log_libusb_result(urbdrc->log, WLOG_DEBUG, "libusb_detach_kernel_driver", err);
1048  }
1049  }
1050 
1051  pdev->status |= URBDRC_DEVICE_DETACH_KERNEL;
1052  }
1053 
1054  return TRUE;
1055 #endif
1056 }
1057 
1058 static BOOL libusb_udev_attach_kernel_driver(IUDEVICE* idev)
1059 {
1060  int err = 0;
1061  UDEVICE* pdev = (UDEVICE*)idev;
1062 
1063  if (!pdev || !pdev->LibusbConfig || !pdev->libusb_handle || !pdev->urbdrc)
1064  return FALSE;
1065 
1066  for (int i = 0; i < pdev->LibusbConfig->bNumInterfaces && err != LIBUSB_ERROR_NO_DEVICE; i++)
1067  {
1068  err = libusb_release_interface(pdev->libusb_handle, i);
1069 
1070  log_libusb_result(pdev->urbdrc->log, WLOG_DEBUG, "libusb_release_interface", err);
1071 
1072 #ifndef _WIN32
1073  if (err != LIBUSB_ERROR_NO_DEVICE)
1074  {
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",
1077  err, i);
1078  }
1079 #endif
1080  }
1081 
1082  return TRUE;
1083 }
1084 
1085 static int libusb_udev_is_composite_device(IUDEVICE* idev)
1086 {
1087  UDEVICE* pdev = (UDEVICE*)idev;
1088  return pdev->isCompositeDevice;
1089 }
1090 
1091 static int libusb_udev_is_exist(IUDEVICE* idev)
1092 {
1093  UDEVICE* pdev = (UDEVICE*)idev;
1094  return (pdev->status & URBDRC_DEVICE_NOT_FOUND) ? 0 : 1;
1095 }
1096 
1097 static int libusb_udev_is_channel_closed(IUDEVICE* idev)
1098 {
1099  UDEVICE* pdev = (UDEVICE*)idev;
1100  IUDEVMAN* udevman = NULL;
1101  if (!pdev || !pdev->urbdrc)
1102  return 1;
1103 
1104  udevman = pdev->urbdrc->udevman;
1105  if (udevman)
1106  {
1107  if (udevman->status & URBDRC_DEVICE_CHANNEL_CLOSED)
1108  return 1;
1109  }
1110 
1111  if (pdev->status & URBDRC_DEVICE_CHANNEL_CLOSED)
1112  return 1;
1113 
1114  return 0;
1115 }
1116 
1117 static int libusb_udev_is_already_send(IUDEVICE* idev)
1118 {
1119  UDEVICE* pdev = (UDEVICE*)idev;
1120  return (pdev->status & URBDRC_DEVICE_ALREADY_SEND) ? 1 : 0;
1121 }
1122 
1123 /* This is called from channel cleanup code.
1124  * Avoid double free, just remove the device and mark the channel closed. */
1125 static void libusb_udev_mark_channel_closed(IUDEVICE* idev)
1126 {
1127  UDEVICE* pdev = (UDEVICE*)idev;
1128  if (pdev && ((pdev->status & URBDRC_DEVICE_CHANNEL_CLOSED) == 0))
1129  {
1130  URBDRC_PLUGIN* urbdrc = pdev->urbdrc;
1131  const uint8_t busNr = idev->get_bus_number(idev);
1132  const uint8_t devNr = idev->get_dev_number(idev);
1133 
1134  pdev->status |= URBDRC_DEVICE_CHANNEL_CLOSED;
1135  urbdrc->udevman->unregister_udevice(urbdrc->udevman, busNr, devNr);
1136  }
1137 }
1138 
1139 /* This is called by local events where the device is removed or in an error
1140  * state. Remove the device from redirection and close the channel. */
1141 static void libusb_udev_channel_closed(IUDEVICE* idev)
1142 {
1143  UDEVICE* pdev = (UDEVICE*)idev;
1144  if (pdev && ((pdev->status & URBDRC_DEVICE_CHANNEL_CLOSED) == 0))
1145  {
1146  URBDRC_PLUGIN* urbdrc = pdev->urbdrc;
1147  const uint8_t busNr = idev->get_bus_number(idev);
1148  const uint8_t devNr = idev->get_dev_number(idev);
1149  IWTSVirtualChannel* channel = NULL;
1150 
1151  if (pdev->channelManager)
1152  channel = IFCALLRESULT(NULL, pdev->channelManager->FindChannelById,
1153  pdev->channelManager, pdev->channelID);
1154 
1155  pdev->status |= URBDRC_DEVICE_CHANNEL_CLOSED;
1156 
1157  if (channel)
1158  channel->Write(channel, 0, NULL, NULL);
1159 
1160  urbdrc->udevman->unregister_udevice(urbdrc->udevman, busNr, devNr);
1161  }
1162 }
1163 
1164 static void libusb_udev_set_already_send(IUDEVICE* idev)
1165 {
1166  UDEVICE* pdev = (UDEVICE*)idev;
1167  pdev->status |= URBDRC_DEVICE_ALREADY_SEND;
1168 }
1169 
1170 static char* libusb_udev_get_path(IUDEVICE* idev)
1171 {
1172  UDEVICE* pdev = (UDEVICE*)idev;
1173  return pdev->path;
1174 }
1175 
1176 static int libusb_udev_query_device_port_status(IUDEVICE* idev, UINT32* UsbdStatus,
1177  UINT32* BufferSize, BYTE* Buffer)
1178 {
1179  UDEVICE* pdev = (UDEVICE*)idev;
1180  int success = 0;
1181  int ret = 0;
1182  URBDRC_PLUGIN* urbdrc = NULL;
1183 
1184  if (!pdev || !pdev->urbdrc)
1185  return -1;
1186 
1187  urbdrc = pdev->urbdrc;
1188 
1189  if (pdev->hub_handle != NULL)
1190  {
1191  ret = idev->control_transfer(
1192  idev, 0xffff, 0, 0,
1193  LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_RECIPIENT_OTHER,
1194  LIBUSB_REQUEST_GET_STATUS, 0, pdev->port_number, UsbdStatus, BufferSize, Buffer, 1000);
1195 
1196  if (log_libusb_result(urbdrc->log, WLOG_DEBUG, "libusb_control_transfer", ret))
1197  *BufferSize = 0;
1198  else
1199  {
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]);
1203  success = 1;
1204  }
1205  }
1206 
1207  return success;
1208 }
1209 
1210 static int libusb_udev_isoch_transfer(IUDEVICE* idev, GENERIC_CHANNEL_CALLBACK* callback,
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)
1216 {
1217  int rc = 0;
1218  UINT32 iso_packet_size = 0;
1219  UDEVICE* pdev = (UDEVICE*)idev;
1220  ASYNC_TRANSFER_USER_DATA* user_data = NULL;
1221  struct libusb_transfer* iso_transfer = NULL;
1222  URBDRC_PLUGIN* urbdrc = NULL;
1223  size_t outSize = (12ULL * NumberOfPackets);
1224  uint32_t streamID = 0x40000000 | RequestId;
1225 
1226  if (!pdev || !pdev->urbdrc)
1227  return -1;
1228 
1229  urbdrc = pdev->urbdrc;
1230  user_data = async_transfer_user_data_new(idev, MessageId, 48, BufferSize, Buffer,
1231  outSize + 1024, NoAck, cb, callback);
1232 
1233  if (!user_data)
1234  return -1;
1235 
1236  user_data->ErrorCount = ErrorCount;
1237  user_data->StartFrame = StartFrame;
1238 
1239  if (!Buffer)
1240  Stream_Seek(user_data->data, (12ULL * NumberOfPackets));
1241 
1242  if (NumberOfPackets > 0)
1243  {
1244  iso_packet_size = BufferSize / NumberOfPackets;
1245  iso_transfer = libusb_alloc_transfer((int)NumberOfPackets);
1246  }
1247 
1248  if (iso_transfer == NULL)
1249  {
1250  WLog_Print(urbdrc->log, WLOG_ERROR,
1251  "Error: libusb_alloc_transfer [NumberOfPackets=%" PRIu32 ", BufferSize=%" PRIu32
1252  " ]",
1253  NumberOfPackets, BufferSize);
1254  async_transfer_user_data_free(user_data);
1255  return -1;
1256  }
1257 
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);
1264 
1265  if (!ArrayList_Append(pdev->request_queue, iso_transfer))
1266  {
1267  WLog_Print(urbdrc->log, WLOG_WARN,
1268  "Failed to queue iso transfer, streamID %08" PRIx32 " already in use!",
1269  streamID);
1270  request_free(iso_transfer);
1271  return -1;
1272  }
1273  rc = libusb_submit_transfer(iso_transfer);
1274  if (log_libusb_result(urbdrc->log, WLOG_ERROR, "libusb_submit_transfer", rc))
1275  return -1;
1276  return rc;
1277 }
1278 
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)
1283 {
1284  int status = 0;
1285  UDEVICE* pdev = (UDEVICE*)idev;
1286 
1287  WINPR_ASSERT(BufferSize);
1288  WINPR_ASSERT(*BufferSize <= UINT16_MAX);
1289 
1290  if (!pdev || !pdev->urbdrc)
1291  return FALSE;
1292 
1293  status = libusb_control_transfer(pdev->libusb_handle, bmRequestType, Request, Value, Index,
1294  Buffer, (UINT16)*BufferSize, Timeout);
1295 
1296  if (status >= 0)
1297  *BufferSize = (UINT32)status;
1298  else
1299  log_libusb_result(pdev->urbdrc->log, WLOG_ERROR, "libusb_control_transfer", status);
1300 
1301  if (!func_set_usbd_status(pdev->urbdrc, pdev, UrbdStatus, status))
1302  return FALSE;
1303 
1304  return TRUE;
1305 }
1306 
1307 static int libusb_udev_bulk_or_interrupt_transfer(IUDEVICE* idev,
1308  GENERIC_CHANNEL_CALLBACK* callback,
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)
1313 {
1314  int rc = 0;
1315  UINT32 transfer_type = 0;
1316  UDEVICE* pdev = (UDEVICE*)idev;
1317  const LIBUSB_ENDPOINT_DESCEIPTOR* ep_desc = NULL;
1318  struct libusb_transfer* transfer = NULL;
1319  URBDRC_PLUGIN* urbdrc = NULL;
1320  ASYNC_TRANSFER_USER_DATA* user_data = NULL;
1321  uint32_t streamID = 0x80000000 | RequestId;
1322 
1323  if (!pdev || !pdev->LibusbConfig || !pdev->urbdrc)
1324  return -1;
1325 
1326  urbdrc = pdev->urbdrc;
1327  user_data =
1328  async_transfer_user_data_new(idev, MessageId, 36, BufferSize, data, 0, NoAck, cb, callback);
1329 
1330  if (!user_data)
1331  return -1;
1332 
1333  /* alloc memory for urb transfer */
1334  transfer = libusb_alloc_transfer(0);
1335  if (!transfer)
1336  {
1337  async_transfer_user_data_free(user_data);
1338  return -1;
1339  }
1340  transfer->user_data = user_data;
1341 
1342  ep_desc = func_get_ep_desc(pdev->LibusbConfig, pdev->MsConfig, EndpointAddress);
1343 
1344  if (!ep_desc)
1345  {
1346  WLog_Print(urbdrc->log, WLOG_ERROR, "func_get_ep_desc: endpoint 0x%" PRIx32 " not found",
1347  EndpointAddress);
1348  request_free(transfer);
1349  return -1;
1350  }
1351 
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);
1357 
1358  switch (transfer_type)
1359  {
1360  case BULK_TRANSFER:
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);
1365  break;
1366 
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);
1372  break;
1373 
1374  default:
1375  WLog_Print(urbdrc->log, WLOG_DEBUG,
1376  "urb_bulk_or_interrupt_transfer:"
1377  " other transfer type 0x%" PRIX32 "",
1378  transfer_type);
1379  request_free(transfer);
1380  return -1;
1381  }
1382 
1383  set_stream_id_for_buffer(transfer, streamID);
1384 
1385  if (!ArrayList_Append(pdev->request_queue, transfer))
1386  {
1387  WLog_Print(urbdrc->log, WLOG_WARN,
1388  "Failed to queue transfer, streamID %08" PRIx32 " already in use!", streamID);
1389  request_free(transfer);
1390  return -1;
1391  }
1392  rc = libusb_submit_transfer(transfer);
1393  if (log_libusb_result(urbdrc->log, WLOG_ERROR, "libusb_submit_transfer", rc))
1394  return -1;
1395  return rc;
1396 }
1397 
1398 static int func_cancel_xact_request(URBDRC_PLUGIN* urbdrc, struct libusb_transfer* transfer)
1399 {
1400  int status = 0;
1401 
1402  if (!urbdrc || !transfer)
1403  return -1;
1404 
1405  status = libusb_cancel_transfer(transfer);
1406 
1407  if (log_libusb_result(urbdrc->log, WLOG_WARN, "libusb_cancel_transfer", status))
1408  {
1409  if (status == LIBUSB_ERROR_NOT_FOUND)
1410  return -1;
1411  }
1412  else
1413  return 1;
1414 
1415  return 0;
1416 }
1417 
1418 static void libusb_udev_cancel_all_transfer_request(IUDEVICE* idev)
1419 {
1420  UDEVICE* pdev = (UDEVICE*)idev;
1421  size_t count = 0;
1422 
1423  if (!pdev || !pdev->request_queue || !pdev->urbdrc)
1424  return;
1425 
1426  ArrayList_Lock(pdev->request_queue);
1427  count = ArrayList_Count(pdev->request_queue);
1428 
1429  for (size_t x = 0; x < count; x++)
1430  {
1431  struct libusb_transfer* transfer = ArrayList_GetItem(pdev->request_queue, x);
1432  func_cancel_xact_request(pdev->urbdrc, transfer);
1433  }
1434 
1435  ArrayList_Unlock(pdev->request_queue);
1436 }
1437 
1438 static int libusb_udev_cancel_transfer_request(IUDEVICE* idev, UINT32 RequestId)
1439 {
1440  int rc = -1;
1441  UDEVICE* pdev = (UDEVICE*)idev;
1442  struct libusb_transfer* transfer = NULL;
1443  uint32_t cancelID1 = 0x40000000 | RequestId;
1444  uint32_t cancelID2 = 0x80000000 | RequestId;
1445 
1446  if (!idev || !pdev->urbdrc || !pdev->request_queue)
1447  return -1;
1448 
1449  ArrayList_Lock(pdev->request_queue);
1450  transfer = list_contains(pdev->request_queue, cancelID1);
1451  if (!transfer)
1452  transfer = list_contains(pdev->request_queue, cancelID2);
1453 
1454  if (transfer)
1455  {
1456  URBDRC_PLUGIN* urbdrc = pdev->urbdrc;
1457 
1458  rc = func_cancel_xact_request(urbdrc, transfer);
1459  }
1460  ArrayList_Unlock(pdev->request_queue);
1461  return rc;
1462 }
1463 
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)
1470 BASIC_STATE_FUNC_DEFINED(MsConfig, MSUSB_CONFIG_DESCRIPTOR*)
1471 
1472 BASIC_POINT_FUNC_DEFINED(udev, void*)
1473 BASIC_POINT_FUNC_DEFINED(prev, void*)
1474 BASIC_POINT_FUNC_DEFINED(next, void*)
1475 
1476 static UINT32 udev_get_UsbDevice(IUDEVICE* idev)
1477 {
1478  UDEVICE* pdev = (UDEVICE*)idev;
1479 
1480  if (!pdev)
1481  return 0;
1482 
1483  return pdev->UsbDevice;
1484 }
1485 
1486 static void udev_set_UsbDevice(IUDEVICE* idev, UINT32 val)
1487 {
1488  UDEVICE* pdev = (UDEVICE*)idev;
1489 
1490  if (!pdev)
1491  return;
1492 
1493  pdev->UsbDevice = val;
1494 }
1495 
1496 static void udev_free(IUDEVICE* idev)
1497 {
1498  int rc = 0;
1499  UDEVICE* udev = (UDEVICE*)idev;
1500  URBDRC_PLUGIN* urbdrc = NULL;
1501 
1502  if (!idev || !udev->urbdrc)
1503  return;
1504 
1505  urbdrc = udev->urbdrc;
1506 
1507  libusb_udev_cancel_all_transfer_request(&udev->iface);
1508  if (udev->libusb_handle)
1509  {
1510  rc = libusb_reset_device(udev->libusb_handle);
1511 
1512  log_libusb_result(urbdrc->log, WLOG_ERROR, "libusb_reset_device", rc);
1513  }
1514 
1515  /* HACK: We need to wait until the cancel transfer has been processed by
1516  * poll_libusb_events
1517  */
1518  Sleep(100);
1519 
1520  /* release all interface and attach kernel driver */
1521  udev->iface.attach_kernel_driver(idev);
1522  ArrayList_Free(udev->request_queue);
1523  /* free the config descriptor that send from windows */
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);
1529  free(idev);
1530 }
1531 
1532 static void udev_load_interface(UDEVICE* pdev)
1533 {
1534  WINPR_ASSERT(pdev);
1535 
1536  /* load interface */
1537  /* Basic */
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;
1557  /* Transfer */
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;
1574 }
1575 
1576 static int udev_get_device_handle(URBDRC_PLUGIN* urbdrc, libusb_context* ctx, UDEVICE* pdev,
1577  UINT16 bus_number, UINT16 dev_number)
1578 {
1579  int error = -1;
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);
1583 
1584  WINPR_ASSERT(urbdrc);
1585 
1586  /* Look for device. */
1587  for (ssize_t i = 0; i < total_device; i++)
1588  {
1589  LIBUSB_DEVICE* dev = libusb_list[i];
1590 
1591  if ((bus_number != libusb_get_bus_number(dev)) ||
1592  (dev_number != libusb_get_device_address(dev)))
1593  libusb_unref_device(dev);
1594  else
1595  {
1596  error = libusb_open(dev, &pdev->libusb_handle);
1597 
1598  if (log_libusb_result(urbdrc->log, WLOG_ERROR, "libusb_open", error))
1599  {
1600  libusb_unref_device(dev);
1601  continue;
1602  }
1603 
1604  /* get port number */
1605  error = libusb_get_port_numbers(dev, port_numbers, sizeof(port_numbers));
1606  if (error < 1)
1607  {
1608  /* Prevent open hub, treat as error. */
1609  log_libusb_result(urbdrc->log, WLOG_ERROR, "libusb_get_port_numbers", error);
1610  libusb_unref_device(dev);
1611  continue;
1612  }
1613 
1614  pdev->port_number = port_numbers[(error - 1)];
1615  error = 0;
1616  WLog_Print(urbdrc->log, WLOG_DEBUG, " Port: %d", pdev->port_number);
1617  /* gen device path */
1618  (void)_snprintf(pdev->path, sizeof(pdev->path), "%" PRIu16 "-%d", bus_number,
1619  pdev->port_number);
1620 
1621  WLog_Print(urbdrc->log, WLOG_DEBUG, " DevPath: %s", pdev->path);
1622  }
1623  }
1624  libusb_free_device_list(libusb_list, 0);
1625 
1626  if (error < 0)
1627  return -1;
1628  return 0;
1629 }
1630 
1631 static int udev_get_hub_handle(URBDRC_PLUGIN* urbdrc, libusb_context* ctx, UDEVICE* pdev,
1632  UINT16 bus_number, UINT16 dev_number)
1633 {
1634  int error = -1;
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);
1638 
1639  WINPR_ASSERT(urbdrc);
1640 
1641  /* Look for device hub. */
1642  for (ssize_t i = 0; i < total_device; i++)
1643  {
1644  LIBUSB_DEVICE* dev = libusb_list[i];
1645 
1646  if ((bus_number != libusb_get_bus_number(dev)) ||
1647  (1 != libusb_get_device_address(dev))) /* Root hub allways first on bus. */
1648  libusb_unref_device(dev);
1649  else
1650  {
1651  WLog_Print(urbdrc->log, WLOG_DEBUG, " Open hub: %" PRIu16 "", bus_number);
1652  error = libusb_open(dev, &handle);
1653 
1654  if (!log_libusb_result(urbdrc->log, WLOG_ERROR, "libusb_open", error))
1655  pdev->hub_handle = handle;
1656  else
1657  libusb_unref_device(dev);
1658  }
1659  }
1660 
1661  libusb_free_device_list(libusb_list, 0);
1662 
1663  if (error < 0)
1664  return -1;
1665 
1666  return 0;
1667 }
1668 
1669 static void request_free(void* value)
1670 {
1671  ASYNC_TRANSFER_USER_DATA* user_data = NULL;
1672  struct libusb_transfer* transfer = (struct libusb_transfer*)value;
1673  if (!transfer)
1674  return;
1675 
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);
1680 }
1681 
1682 static IUDEVICE* udev_init(URBDRC_PLUGIN* urbdrc, libusb_context* context, LIBUSB_DEVICE* device,
1683  BYTE bus_number, BYTE dev_number)
1684 {
1685  UDEVICE* pdev = NULL;
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;
1690 
1691  WINPR_ASSERT(urbdrc);
1692 
1693  pdev = (PUDEVICE)calloc(1, sizeof(UDEVICE));
1694 
1695  if (!pdev)
1696  return NULL;
1697 
1698  pdev->urbdrc = urbdrc;
1699  udev_load_interface(pdev);
1700 
1701  if (device)
1702  pdev->libusb_dev = device;
1703  else
1704  pdev->libusb_dev = udev_get_libusb_dev(context, bus_number, dev_number);
1705 
1706  if (pdev->libusb_dev == NULL)
1707  goto fail;
1708 
1709  if (urbdrc->listener_callback)
1710  udev_set_channelManager(&pdev->iface, urbdrc->listener_callback->channel_mgr);
1711 
1712  /* Get DEVICE handle */
1713  status = udev_get_device_handle(urbdrc, context, pdev, bus_number, dev_number);
1714  if (status != LIBUSB_SUCCESS)
1715  {
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);
1719 
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);
1723  goto fail;
1724  }
1725 
1726  /* Get HUB handle */
1727  status = udev_get_hub_handle(urbdrc, context, pdev, bus_number, dev_number);
1728 
1729  if (status < 0)
1730  pdev->hub_handle = NULL;
1731 
1732  pdev->devDescriptor = udev_new_descript(urbdrc, pdev->libusb_dev);
1733 
1734  if (!pdev->devDescriptor)
1735  goto fail;
1736 
1737  status = libusb_get_active_config_descriptor(pdev->libusb_dev, &pdev->LibusbConfig);
1738 
1739  if (status == LIBUSB_ERROR_NOT_FOUND)
1740  status = libusb_get_config_descriptor(pdev->libusb_dev, 0, &pdev->LibusbConfig);
1741 
1742  if (status < 0)
1743  goto fail;
1744 
1745  config_temp = pdev->LibusbConfig;
1746  /* get the first interface and first altsetting */
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));
1753  /* Check composite device */
1754  devDescriptor = pdev->devDescriptor;
1755 
1756  if ((devDescriptor->bNumConfigurations == 1) && (config_temp->bNumInterfaces > 1) &&
1757  (devDescriptor->bDeviceClass == LIBUSB_CLASS_PER_INTERFACE))
1758  {
1759  pdev->isCompositeDevice = 1;
1760  }
1761  else if ((devDescriptor->bDeviceClass == 0xef) &&
1762  (devDescriptor->bDeviceSubClass == LIBUSB_CLASS_COMM) &&
1763  (devDescriptor->bDeviceProtocol == 0x01))
1764  {
1765  pdev->isCompositeDevice = 1;
1766  }
1767  else
1768  pdev->isCompositeDevice = 0;
1769 
1770  /* set device class to first interface class */
1771  devDescriptor->bDeviceClass = interface_temp.bInterfaceClass;
1772  devDescriptor->bDeviceSubClass = interface_temp.bInterfaceSubClass;
1773  devDescriptor->bDeviceProtocol = interface_temp.bInterfaceProtocol;
1774  /* initialize pdev */
1775  pdev->bus_number = bus_number;
1776  pdev->dev_number = dev_number;
1777  pdev->request_queue = ArrayList_New(TRUE);
1778 
1779  if (!pdev->request_queue)
1780  goto fail;
1781 
1782  ArrayList_Object(pdev->request_queue)->fnObjectFree = request_free;
1783 
1784  /* set config of windows */
1785  pdev->MsConfig = msusb_msconfig_new();
1786 
1787  if (!pdev->MsConfig)
1788  goto fail;
1789 
1790  // deb_config_msg(pdev->libusb_dev, config_temp, devDescriptor->bNumConfigurations);
1791  return &pdev->iface;
1792 fail:
1793  pdev->iface.free(&pdev->iface);
1794  return NULL;
1795 }
1796 
1797 size_t udev_new_by_id(URBDRC_PLUGIN* urbdrc, libusb_context* ctx, UINT16 idVendor, UINT16 idProduct,
1798  IUDEVICE*** devArray)
1799 {
1800  LIBUSB_DEVICE** libusb_list = NULL;
1801  UDEVICE** array = NULL;
1802  ssize_t total_device = 0;
1803  size_t num = 0;
1804 
1805  if (!urbdrc || !devArray)
1806  return 0;
1807 
1808  WLog_Print(urbdrc->log, WLOG_INFO, "VID: 0x%04" PRIX16 ", PID: 0x%04" PRIX16 "", idVendor,
1809  idProduct);
1810  total_device = libusb_get_device_list(ctx, &libusb_list);
1811  if (total_device < 0)
1812  return 0;
1813 
1814  array = (UDEVICE**)calloc((size_t)total_device, sizeof(UDEVICE*));
1815 
1816  if (!array)
1817  goto fail;
1818 
1819  for (ssize_t i = 0; i < total_device; i++)
1820  {
1821  LIBUSB_DEVICE* dev = libusb_list[i];
1822  LIBUSB_DEVICE_DESCRIPTOR* descriptor = udev_new_descript(urbdrc, dev);
1823 
1824  if ((descriptor->idVendor == idVendor) && (descriptor->idProduct == idProduct))
1825  {
1826  array[num] = (PUDEVICE)udev_init(urbdrc, ctx, dev, libusb_get_bus_number(dev),
1827  libusb_get_device_address(dev));
1828 
1829  if (array[num] != NULL)
1830  num++;
1831  }
1832  else
1833  libusb_unref_device(dev);
1834 
1835  free(descriptor);
1836  }
1837 
1838 fail:
1839  libusb_free_device_list(libusb_list, 0);
1840  *devArray = (IUDEVICE**)array;
1841  return num;
1842 }
1843 
1844 IUDEVICE* udev_new_by_addr(URBDRC_PLUGIN* urbdrc, libusb_context* context, BYTE bus_number,
1845  BYTE dev_number)
1846 {
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);
1849 }