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