FreeRDP
Loading...
Searching...
No Matches
urbdrc_main.c
1
21#include <winpr/assert.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <errno.h>
26
27#include <winpr/pool.h>
28#include <winpr/print.h>
29
30#include <winpr/crt.h>
31#include <winpr/synch.h>
32#include <winpr/string.h>
33#include <winpr/cmdline.h>
34
35#include <freerdp/dvc.h>
36#include <freerdp/addin.h>
37#include <freerdp/channels/log.h>
38#include <freerdp/channels/urbdrc.h>
39
40#include "urbdrc_types.h"
41#include "urbdrc_main.h"
42#include "data_transfer.h"
43
44#include <urbdrc_helpers.h>
45
46static IWTSVirtualChannel* get_channel(IUDEVMAN* idevman)
47{
48 IWTSVirtualChannelManager* channel_mgr = NULL;
49 URBDRC_PLUGIN* urbdrc = NULL;
50
51 if (!idevman)
52 return NULL;
53
54 urbdrc = (URBDRC_PLUGIN*)idevman->plugin;
55
56 if (!urbdrc || !urbdrc->listener_callback)
57 return NULL;
58
59 channel_mgr = urbdrc->listener_callback->channel_mgr;
60
61 if (!channel_mgr)
62 return NULL;
63
64 return channel_mgr->FindChannelById(channel_mgr, idevman->controlChannelId);
65}
66
67static int func_container_id_generate(IUDEVICE* pdev, char* strContainerId)
68{
69 char* p = NULL;
70 char* path = NULL;
71 UINT8 containerId[17] = { 0 };
72 UINT16 idVendor = 0;
73 UINT16 idProduct = 0;
74 idVendor = (UINT16)pdev->query_device_descriptor(pdev, ID_VENDOR);
75 idProduct = (UINT16)pdev->query_device_descriptor(pdev, ID_PRODUCT);
76 path = pdev->getPath(pdev);
77
78 if (strlen(path) > 8)
79 p = (path + strlen(path)) - 8;
80 else
81 p = path;
82
83 (void)sprintf_s((char*)containerId, sizeof(containerId), "%04" PRIX16 "%04" PRIX16 "%s",
84 idVendor, idProduct, p);
85 /* format */
86 (void)sprintf_s(strContainerId, DEVICE_CONTAINER_STR_SIZE,
87 "{%02" PRIx8 "%02" PRIx8 "%02" PRIx8 "%02" PRIx8 "-%02" PRIx8 "%02" PRIx8
88 "-%02" PRIx8 "%02" PRIx8 "-%02" PRIx8 "%02" PRIx8 "-%02" PRIx8 "%02" PRIx8
89 "%02" PRIx8 "%02" PRIx8 "%02" PRIx8 "%02" PRIx8 "}",
90 containerId[0], containerId[1], containerId[2], containerId[3], containerId[4],
91 containerId[5], containerId[6], containerId[7], containerId[8], containerId[9],
92 containerId[10], containerId[11], containerId[12], containerId[13],
93 containerId[14], containerId[15]);
94 return 0;
95}
96
97static int func_instance_id_generate(IUDEVICE* pdev, char* strInstanceId, size_t len)
98{
99 char instanceId[17] = { 0 };
100 (void)sprintf_s(instanceId, sizeof(instanceId), "\\%s", pdev->getPath(pdev));
101 /* format */
102 (void)sprintf_s(strInstanceId, len,
103 "%02" PRIx8 "%02" PRIx8 "%02" PRIx8 "%02" PRIx8 "-%02" PRIx8 "%02" PRIx8
104 "-%02" PRIx8 "%02" PRIx8 "-%02" PRIx8 "%02" PRIx8 "-%02" PRIx8 "%02" PRIx8
105 "%02" PRIx8 "%02" PRIx8 "%02" PRIx8 "%02" PRIx8 "",
106 instanceId[0], instanceId[1], instanceId[2], instanceId[3], instanceId[4],
107 instanceId[5], instanceId[6], instanceId[7], instanceId[8], instanceId[9],
108 instanceId[10], instanceId[11], instanceId[12], instanceId[13], instanceId[14],
109 instanceId[15]);
110 return 0;
111}
112
113/* [MS-RDPEUSB] 2.2.3.2 Interface Manipulation Exchange Capabilities Response
114 * (RIM_EXCHANGE_CAPABILITY_RESPONSE) */
115static UINT urbdrc_send_capability_response(GENERIC_CHANNEL_CALLBACK* callback, UINT32 MessageId,
116 UINT32 Version)
117{
118 const UINT32 InterfaceId = ((STREAM_ID_NONE << 30) | CAPABILITIES_NEGOTIATOR);
119 wStream* out = create_shared_message_header_with_functionid(InterfaceId, MessageId, Version, 4);
120
121 if (!out)
122 return ERROR_OUTOFMEMORY;
123
124 Stream_Write_UINT32(out, 0x00000000); /* HRESULT */
125 return stream_write_and_free(callback->plugin, callback->channel, out);
126}
127
133static UINT urbdrc_process_capability_request(GENERIC_CHANNEL_CALLBACK* callback, wStream* s,
134 UINT32 MessageId)
135{
136 WINPR_ASSERT(callback);
137 URBDRC_PLUGIN* urbdrc = (URBDRC_PLUGIN*)callback->plugin;
138 WINPR_ASSERT(urbdrc);
139
140 if (!callback || !s)
141 return ERROR_INVALID_PARAMETER;
142
143 if (!Stream_CheckAndLogRequiredLengthWLog(urbdrc->log, s, 4))
144 return ERROR_INVALID_DATA;
145
146 UINT32 Version = Stream_Get_UINT32(s);
147
148 if (Version > RIM_CAPABILITY_VERSION_01)
149 {
150 WLog_Print(urbdrc->log, WLOG_WARN, "Unknown capability version %" PRIu32 ", expected %d",
151 Version, RIM_CAPABILITY_VERSION_01);
152 Version = RIM_CAPABILITY_VERSION_01;
153 }
154
155 return urbdrc_send_capability_response(callback, MessageId, Version);
156}
157
158/* [MS-RDPEUSB] 2.2.5.1 Channel Created Message (CHANNEL_CREATED) */
159static UINT urbdrc_send_channel_created(GENERIC_CHANNEL_CALLBACK* callback, UINT32 MessageId,
160 UINT32 MajorVersion, UINT32 MinorVersion,
161 UINT32 Capabilities)
162{
163 WINPR_ASSERT(callback);
164
165 const UINT32 InterfaceId = ((STREAM_ID_PROXY << 30) | CLIENT_CHANNEL_NOTIFICATION);
166 wStream* out =
167 create_shared_message_header_with_functionid(InterfaceId, MessageId, CHANNEL_CREATED, 12);
168
169 if (!out)
170 return ERROR_OUTOFMEMORY;
171
172 Stream_Write_UINT32(out, MajorVersion);
173 Stream_Write_UINT32(out, MinorVersion);
174 Stream_Write_UINT32(out, Capabilities); /* capabilities version */
175 return stream_write_and_free(callback->plugin, callback->channel, out);
176}
177
183static UINT urbdrc_process_channel_create(GENERIC_CHANNEL_CALLBACK* callback, wStream* s,
184 UINT32 MessageId)
185{
186 UINT32 MajorVersion = 0;
187 UINT32 MinorVersion = 0;
188 UINT32 Capabilities = 0;
189 URBDRC_PLUGIN* urbdrc = NULL;
190
191 if (!callback || !s || !callback->plugin)
192 return ERROR_INVALID_PARAMETER;
193
194 urbdrc = (URBDRC_PLUGIN*)callback->plugin;
195
196 if (!Stream_CheckAndLogRequiredLength(TAG, s, 12))
197 return ERROR_INVALID_DATA;
198
199 Stream_Read_UINT32(s, MajorVersion);
200 Stream_Read_UINT32(s, MinorVersion);
201 Stream_Read_UINT32(s, Capabilities);
202
203 /* Version check, we only support version 1.0 */
204 if ((MajorVersion != 1) || (MinorVersion != 0))
205 {
206 WLog_Print(urbdrc->log, WLOG_WARN,
207 "server supports USB channel version %" PRIu32 ".%" PRIu32, MajorVersion,
208 MinorVersion);
209 WLog_Print(urbdrc->log, WLOG_WARN, "we only support channel version 1.0");
210 MajorVersion = 1;
211 MinorVersion = 0;
212 }
213 if (Capabilities != 0)
214 {
215 WLog_Print(urbdrc->log, WLOG_WARN,
216 "[MS-RDPEUSB] 2.2.5.1 Channel Created Message (CHANNEL_CREATED) states "
217 "Capabilities must be 0, got %" PRIu32,
218 Capabilities);
219 Capabilities = 0;
220 }
221
222 return urbdrc_send_channel_created(callback, MessageId, MajorVersion, MinorVersion,
223 Capabilities);
224}
225
226static UINT urdbrc_send_virtual_channel_add(IWTSPlugin* plugin, IWTSVirtualChannel* channel,
227 UINT32 MessageId)
228{
229 const UINT32 InterfaceId = ((STREAM_ID_PROXY << 30) | CLIENT_DEVICE_SINK);
230 wStream* out = create_shared_message_header_with_functionid(InterfaceId, MessageId,
231 ADD_VIRTUAL_CHANNEL, 0);
232
233 if (!out)
234 return ERROR_OUTOFMEMORY;
235
236 return stream_write_and_free(plugin, channel, out);
237}
238
239static BOOL write_string_block(wStream* s, size_t count, const char** strings, const size_t* length,
240 BOOL isMultiSZ)
241{
242 size_t len = 0;
243 for (size_t x = 0; x < count; x++)
244 {
245 len += length[x] + 1ULL;
246 }
247
248 if (isMultiSZ)
249 len++;
250
251 if (!Stream_EnsureRemainingCapacity(s, len * sizeof(WCHAR) + sizeof(UINT32)))
252 return FALSE;
253
254 /* Write number of characters (including '\0') of all strings */
255 Stream_Write_UINT32(s, (UINT32)len); /* cchHwIds */
256 /* HardwareIds 1 */
257
258 for (size_t x = 0; x < count; x++)
259 {
260 size_t clength = length[x];
261 const char* str = strings[x];
262
263 const SSIZE_T w = Stream_Write_UTF16_String_From_UTF8(s, clength, str, clength, TRUE);
264 if ((w < 0) || ((size_t)w != clength))
265 return FALSE;
266 Stream_Write_UINT16(s, 0);
267 }
268
269 if (isMultiSZ)
270 Stream_Write_UINT16(s, 0);
271 return TRUE;
272}
273
274/* [MS-RDPEUSB] 2.2.4.2 Add Device Message (ADD_DEVICE) */
275static UINT urbdrc_send_add_device(GENERIC_CHANNEL_CALLBACK* callback, UINT32 UsbDevice,
276 UINT32 bcdUSB, const char* strInstanceId, size_t InstanceIdLen,
277 size_t nrHwIds, const char* HardwareIds[],
278 const size_t HardwareIdsLen[], size_t nrCompatIds,
279 const char* CompatibilityIds[],
280 const size_t CompatibilityIdsLen[], const char* strContainerId,
281 size_t ContainerIdLen)
282{
283 WINPR_ASSERT(callback);
284 WINPR_ASSERT(HardwareIds);
285 WINPR_ASSERT(HardwareIdsLen);
286 WINPR_ASSERT(CompatibilityIds);
287 WINPR_ASSERT(CompatibilityIdsLen);
288
289 const UINT32 InterfaceId = ((STREAM_ID_PROXY << 30) | CLIENT_DEVICE_SINK);
290 wStream* out = create_shared_message_header_with_functionid(InterfaceId, 0, ADD_DEVICE, 8);
291 if (!out)
292 return ERROR_OUTOFMEMORY;
293
294 Stream_Write_UINT32(out, 0x00000001); /* NumUsbDevice */
295 Stream_Write_UINT32(out, UsbDevice); /* UsbDevice */
296
297 if (!write_string_block(out, 1, &strInstanceId, &InstanceIdLen, FALSE))
298 goto fail;
299
300 if (!write_string_block(out, nrHwIds, HardwareIds, HardwareIdsLen, TRUE))
301 goto fail;
302
303 if (!write_string_block(out, nrCompatIds, CompatibilityIds, CompatibilityIdsLen, TRUE))
304 goto fail;
305
306 if (!write_string_block(out, 1, &strContainerId, &ContainerIdLen, FALSE))
307 goto fail;
308
309 /* USB_DEVICE_CAPABILITIES 28 bytes */
310 if (!Stream_EnsureRemainingCapacity(out, 28))
311 goto fail;
312
313 Stream_Write_UINT32(out, 0x0000001c); /* CbSize */
314 Stream_Write_UINT32(out, 2); /* UsbBusInterfaceVersion, 0 ,1 or 2 */ // TODO: Get from libusb
315 Stream_Write_UINT32(out, 0x600); /* USBDI_Version, 0x500 or 0x600 */ // TODO: Get from libusb
316 /* Supported_USB_Version, 0x110,0x110 or 0x200(usb2.0) */
317 Stream_Write_UINT32(out, bcdUSB);
318 Stream_Write_UINT32(out, 0x00000000); /* HcdCapabilities, MUST always be zero */
319
320 if (bcdUSB < 0x200)
321 Stream_Write_UINT32(out, 0x00000000); /* DeviceIsHighSpeed */
322 else
323 Stream_Write_UINT32(out, 0x00000001); /* DeviceIsHighSpeed */
324
325 Stream_Write_UINT32(out, 0x50); /* NoAckIsochWriteJitterBufferSizeInMs, >=10 or <=512 */
326 return stream_write_and_free(callback->plugin, callback->channel, out);
327
328fail:
329 Stream_Free(out, TRUE);
330 return ERROR_INTERNAL_ERROR;
331}
332
338static UINT urdbrc_send_usb_device_add(GENERIC_CHANNEL_CALLBACK* callback, IUDEVICE* pdev)
339{
340 char HardwareIds[2][DEVICE_HARDWARE_ID_SIZE] = { { 0 } };
341 const char* CHardwareIds[2] = { HardwareIds[0], HardwareIds[1] };
342 char CompatibilityIds[4][DEVICE_COMPATIBILITY_ID_SIZE] = { { 0 } };
343 const char* CCompatibilityIds[4] = { CompatibilityIds[0], CompatibilityIds[1],
344 CompatibilityIds[2], CompatibilityIds[3] };
345 char strContainerId[DEVICE_CONTAINER_STR_SIZE] = { 0 };
346 char strInstanceId[DEVICE_INSTANCE_STR_SIZE] = { 0 };
347 size_t CompatibilityIdLen[4] = { 0 };
348 size_t HardwareIdsLen[2] = { 0 };
349 const size_t nrHwIds = ARRAYSIZE(HardwareIds);
350 size_t nrCompatIds = 3;
351
352 /* USB kernel driver detach!! */
353 pdev->detach_kernel_driver(pdev);
354 {
355 const UINT16 idVendor = (UINT16)pdev->query_device_descriptor(pdev, ID_VENDOR);
356 const UINT16 idProduct = (UINT16)pdev->query_device_descriptor(pdev, ID_PRODUCT);
357 const UINT16 bcdDevice = (UINT16)pdev->query_device_descriptor(pdev, BCD_DEVICE);
358 (void)sprintf_s(HardwareIds[1], DEVICE_HARDWARE_ID_SIZE,
359 "USB\\VID_%04" PRIX16 "&PID_%04" PRIX16 "", idVendor, idProduct);
360 (void)sprintf_s(HardwareIds[0], DEVICE_HARDWARE_ID_SIZE,
361 "USB\\VID_%04" PRIX16 "&PID_%04" PRIX16 "&REV_%04" PRIX16 "", idVendor,
362 idProduct, bcdDevice);
363 }
364 {
365 const UINT8 bDeviceClass = (UINT8)pdev->query_device_descriptor(pdev, B_DEVICE_CLASS);
366 const UINT8 bDeviceSubClass = (UINT8)pdev->query_device_descriptor(pdev, B_DEVICE_SUBCLASS);
367 const UINT8 bDeviceProtocol = (UINT8)pdev->query_device_descriptor(pdev, B_DEVICE_PROTOCOL);
368
369 if (!(pdev->isCompositeDevice(pdev)))
370 {
371 (void)sprintf_s(CompatibilityIds[2], DEVICE_COMPATIBILITY_ID_SIZE,
372 "USB\\Class_%02" PRIX8 "", bDeviceClass);
373 (void)sprintf_s(CompatibilityIds[1], DEVICE_COMPATIBILITY_ID_SIZE,
374 "USB\\Class_%02" PRIX8 "&SubClass_%02" PRIX8 "", bDeviceClass,
375 bDeviceSubClass);
376 (void)sprintf_s(CompatibilityIds[0], DEVICE_COMPATIBILITY_ID_SIZE,
377 "USB\\Class_%02" PRIX8 "&SubClass_%02" PRIX8 "&Prot_%02" PRIX8 "",
378 bDeviceClass, bDeviceSubClass, bDeviceProtocol);
379 }
380 else
381 {
382 (void)sprintf_s(CompatibilityIds[3], DEVICE_COMPATIBILITY_ID_SIZE, "USB\\COMPOSITE");
383 (void)sprintf_s(CompatibilityIds[2], DEVICE_COMPATIBILITY_ID_SIZE, "USB\\DevClass_00");
384 (void)sprintf_s(CompatibilityIds[1], DEVICE_COMPATIBILITY_ID_SIZE,
385 "USB\\DevClass_00&SubClass_00");
386 (void)sprintf_s(CompatibilityIds[0], DEVICE_COMPATIBILITY_ID_SIZE,
387 "USB\\DevClass_00&SubClass_00&Prot_00");
388 nrCompatIds = 4;
389 }
390 }
391 func_instance_id_generate(pdev, strInstanceId, DEVICE_INSTANCE_STR_SIZE);
392 func_container_id_generate(pdev, strContainerId);
393
394 for (size_t x = 0; x < nrHwIds; x++)
395 {
396 HardwareIdsLen[x] = strnlen(HardwareIds[x], DEVICE_HARDWARE_ID_SIZE);
397 }
398
399 for (size_t x = 0; x < nrCompatIds; x++)
400 {
401 CompatibilityIdLen[x] = strnlen(CompatibilityIds[x], DEVICE_COMPATIBILITY_ID_SIZE);
402 }
403
404 const size_t InstanceIdLen = strnlen(strInstanceId, sizeof(strInstanceId));
405 const size_t ContainerIdLen = strnlen(strContainerId, sizeof(strContainerId));
406
407 const UINT32 UsbDevice = pdev->get_UsbDevice(pdev);
408 const UINT32 bcdUSB =
409 WINPR_ASSERTING_INT_CAST(uint32_t, pdev->query_device_descriptor(pdev, BCD_USB));
410 return urbdrc_send_add_device(callback, UsbDevice, bcdUSB, strInstanceId, InstanceIdLen,
411 nrHwIds, CHardwareIds, HardwareIdsLen, nrCompatIds,
412 CCompatibilityIds, CompatibilityIdLen, strContainerId,
413 ContainerIdLen);
414}
415
421static UINT urbdrc_exchange_capabilities(GENERIC_CHANNEL_CALLBACK* callback, wStream* data)
422{
423 UINT32 MessageId = 0;
424 UINT32 FunctionId = 0;
425 UINT32 InterfaceId = 0;
426 UINT error = CHANNEL_RC_OK;
427
428 if (!data)
429 return ERROR_INVALID_PARAMETER;
430
431 if (!Stream_CheckAndLogRequiredLength(TAG, data, 8))
432 return ERROR_INVALID_DATA;
433
434 Stream_Rewind_UINT32(data);
435 Stream_Read_UINT32(data, InterfaceId);
436 Stream_Read_UINT32(data, MessageId);
437 Stream_Read_UINT32(data, FunctionId);
438
439 switch (FunctionId)
440 {
441 case RIM_EXCHANGE_CAPABILITY_REQUEST:
442 if (InterfaceId != 0)
443 {
444 WLog_ERR(
445 TAG,
446 "[MS-RDPEUSB] 2.2.3.1 Interface Manipulation Exchange Capabilities Request "
447 "(RIM_EXCHANGE_CAPABILITY_REQUEST))::InterfaceId expected 0, got %" PRIu32,
448 InterfaceId);
449 return ERROR_INVALID_DATA;
450 }
451 error = urbdrc_process_capability_request(callback, data, MessageId);
452 break;
453
454 case RIMCALL_RELEASE:
455 break;
456
457 default:
458 error = ERROR_NOT_FOUND;
459 break;
460 }
461
462 return error;
463}
464
465static BOOL urbdrc_announce_devices(IUDEVMAN* udevman)
466{
467 UINT error = ERROR_SUCCESS;
468
469 udevman->loading_lock(udevman);
470 udevman->rewind(udevman);
471
472 while (udevman->has_next(udevman))
473 {
474 IUDEVICE* pdev = udevman->get_next(udevman);
475
476 if (!pdev->isAlreadySend(pdev))
477 {
478 const UINT32 deviceId = pdev->get_UsbDevice(pdev);
479 UINT cerror =
480 urdbrc_send_virtual_channel_add(udevman->plugin, get_channel(udevman), deviceId);
481
482 if (cerror != ERROR_SUCCESS)
483 break;
484 }
485 }
486
487 udevman->loading_unlock(udevman);
488
489 return error == ERROR_SUCCESS;
490}
491
492static UINT urbdrc_device_control_channel(GENERIC_CHANNEL_CALLBACK* callback,
493 WINPR_ATTR_UNUSED wStream* s)
494{
495 URBDRC_PLUGIN* urbdrc = (URBDRC_PLUGIN*)callback->plugin;
496 IUDEVMAN* udevman = urbdrc->udevman;
497 IWTSVirtualChannel* channel = callback->channel;
498 IUDEVICE* pdev = NULL;
499 BOOL found = FALSE;
500 UINT error = ERROR_INTERNAL_ERROR;
501 UINT32 channelId = callback->channel_mgr->GetChannelId(channel);
502
503 switch (urbdrc->vchannel_status)
504 {
505 case INIT_CHANNEL_IN:
506 /* Control channel was established */
507 error = ERROR_SUCCESS;
508 udevman->initialize(udevman, channelId);
509
510 if (!urbdrc_announce_devices(udevman))
511 goto fail;
512
513 urbdrc->vchannel_status = INIT_CHANNEL_OUT;
514 break;
515
516 case INIT_CHANNEL_OUT:
517 /* A new device channel was created, add the channel
518 * to the device */
519 udevman->loading_lock(udevman);
520 udevman->rewind(udevman);
521
522 while (udevman->has_next(udevman))
523 {
524 pdev = udevman->get_next(udevman);
525
526 if (!pdev->isAlreadySend(pdev))
527 {
528 const UINT32 channelID = callback->channel_mgr->GetChannelId(channel);
529 found = TRUE;
530 pdev->setAlreadySend(pdev);
531 pdev->set_channelManager(pdev, callback->channel_mgr);
532 pdev->set_channelID(pdev, channelID);
533 break;
534 }
535 }
536
537 udevman->loading_unlock(udevman);
538 error = ERROR_SUCCESS;
539
540 if (found && pdev->isAlreadySend(pdev))
541 error = urdbrc_send_usb_device_add(callback, pdev);
542
543 break;
544
545 default:
546 WLog_Print(urbdrc->log, WLOG_ERROR, "vchannel_status unknown value %" PRIu32 "",
547 urbdrc->vchannel_status);
548 break;
549 }
550
551fail:
552 return error;
553}
554
560static UINT urbdrc_process_channel_notification(GENERIC_CHANNEL_CALLBACK* callback, wStream* data)
561{
562 UINT32 MessageId = 0;
563 UINT32 FunctionId = 0;
564 UINT32 InterfaceId = 0;
565 UINT error = CHANNEL_RC_OK;
566 URBDRC_PLUGIN* urbdrc = NULL;
567
568 if (!callback || !data)
569 return ERROR_INVALID_PARAMETER;
570
571 urbdrc = (URBDRC_PLUGIN*)callback->plugin;
572
573 if (!urbdrc)
574 return ERROR_INVALID_PARAMETER;
575
576 if (!Stream_CheckAndLogRequiredLength(TAG, data, 8))
577 return ERROR_INVALID_DATA;
578
579 Stream_Rewind(data, 4);
580 Stream_Read_UINT32(data, InterfaceId);
581 Stream_Read_UINT32(data, MessageId);
582 Stream_Read_UINT32(data, FunctionId);
583 WLog_Print(urbdrc->log, WLOG_TRACE, "%s [%" PRIu32 "]",
584 call_to_string(FALSE, InterfaceId, FunctionId), FunctionId);
585
586 switch (FunctionId)
587 {
588 case CHANNEL_CREATED:
589 error = urbdrc_process_channel_create(callback, data, MessageId);
590 break;
591
592 case RIMCALL_RELEASE:
593 error = urbdrc_device_control_channel(callback, data);
594 break;
595
596 default:
597 WLog_Print(urbdrc->log, WLOG_TRACE, "unknown FunctionId 0x%" PRIX32 "", FunctionId);
598 error = 1;
599 break;
600 }
601
602 return error;
603}
604
610static UINT urbdrc_on_data_received(IWTSVirtualChannelCallback* pChannelCallback, wStream* data)
611{
612 GENERIC_CHANNEL_CALLBACK* callback = (GENERIC_CHANNEL_CALLBACK*)pChannelCallback;
613 URBDRC_PLUGIN* urbdrc = NULL;
614 IUDEVMAN* udevman = NULL;
615 UINT32 InterfaceId = 0;
616 UINT error = ERROR_INTERNAL_ERROR;
617
618 if (callback == NULL)
619 return ERROR_INVALID_PARAMETER;
620
621 if (callback->plugin == NULL)
622 return error;
623
624 urbdrc = (URBDRC_PLUGIN*)callback->plugin;
625
626 if (urbdrc->udevman == NULL)
627 return error;
628
629 udevman = urbdrc->udevman;
630
631 if (!Stream_CheckAndLogRequiredLength(TAG, data, 12))
632 return ERROR_INVALID_DATA;
633
634 urbdrc_dump_message(urbdrc->log, FALSE, FALSE, data);
635 Stream_Read_UINT32(data, InterfaceId);
636
637 /* Need to check InterfaceId and mask values */
638 switch (InterfaceId)
639 {
640 case CAPABILITIES_NEGOTIATOR | (STREAM_ID_NONE << 30):
641 error = urbdrc_exchange_capabilities(callback, data);
642 break;
643
644 case SERVER_CHANNEL_NOTIFICATION | (STREAM_ID_PROXY << 30):
645 error = urbdrc_process_channel_notification(callback, data);
646 break;
647
648 default:
649 error = urbdrc_process_udev_data_transfer(callback, urbdrc, udevman, data);
650 WLog_DBG(TAG, "urbdrc_process_udev_data_transfer returned 0x%08" PRIx32, error);
651 error = ERROR_SUCCESS; /* Ignore errors, the device may have been unplugged. */
652 break;
653 }
654
655 return error;
656}
657
663static UINT urbdrc_on_close(IWTSVirtualChannelCallback* pChannelCallback)
664{
665 GENERIC_CHANNEL_CALLBACK* callback = (GENERIC_CHANNEL_CALLBACK*)pChannelCallback;
666 if (callback)
667 {
668 URBDRC_PLUGIN* urbdrc = (URBDRC_PLUGIN*)callback->plugin;
669 if (urbdrc)
670 {
671 IUDEVMAN* udevman = urbdrc->udevman;
672 if (udevman && callback->channel_mgr)
673 {
674 UINT32 control = callback->channel_mgr->GetChannelId(callback->channel);
675 if (udevman->controlChannelId == control)
676 udevman->status |= URBDRC_DEVICE_CHANNEL_CLOSED;
677 else
678 { /* Need to notify the local backend the device is gone */
679 IUDEVICE* pdev = udevman->get_udevice_by_ChannelID(udevman, control);
680 if (pdev)
681 pdev->markChannelClosed(pdev);
682 }
683 }
684 }
685 }
686 free(callback);
687 return CHANNEL_RC_OK;
688}
689
695static UINT urbdrc_on_new_channel_connection(IWTSListenerCallback* pListenerCallback,
696 IWTSVirtualChannel* pChannel,
697 WINPR_ATTR_UNUSED BYTE* pData,
698 WINPR_ATTR_UNUSED BOOL* pbAccept,
699 IWTSVirtualChannelCallback** ppCallback)
700{
701 GENERIC_LISTENER_CALLBACK* listener_callback = (GENERIC_LISTENER_CALLBACK*)pListenerCallback;
702 GENERIC_CHANNEL_CALLBACK* callback = NULL;
703
704 if (!ppCallback)
705 return ERROR_INVALID_PARAMETER;
706
707 callback = (GENERIC_CHANNEL_CALLBACK*)calloc(1, sizeof(GENERIC_CHANNEL_CALLBACK));
708
709 if (!callback)
710 return ERROR_OUTOFMEMORY;
711
712 callback->iface.OnDataReceived = urbdrc_on_data_received;
713 callback->iface.OnClose = urbdrc_on_close;
714 callback->plugin = listener_callback->plugin;
715 callback->channel_mgr = listener_callback->channel_mgr;
716 callback->channel = pChannel;
717 *ppCallback = (IWTSVirtualChannelCallback*)callback;
718 return CHANNEL_RC_OK;
719}
720
726static UINT urbdrc_plugin_initialize(IWTSPlugin* pPlugin, IWTSVirtualChannelManager* pChannelMgr)
727{
728 UINT status = 0;
729 URBDRC_PLUGIN* urbdrc = (URBDRC_PLUGIN*)pPlugin;
730 IUDEVMAN* udevman = NULL;
731 char channelName[sizeof(URBDRC_CHANNEL_NAME)] = { URBDRC_CHANNEL_NAME };
732
733 if (!urbdrc || !urbdrc->udevman)
734 return ERROR_INVALID_PARAMETER;
735
736 if (urbdrc->initialized)
737 {
738 WLog_ERR(TAG, "[%s] channel initialized twice, aborting", URBDRC_CHANNEL_NAME);
739 return ERROR_INVALID_DATA;
740 }
741 udevman = urbdrc->udevman;
742 urbdrc->listener_callback =
744
745 if (!urbdrc->listener_callback)
746 return CHANNEL_RC_NO_MEMORY;
747
748 urbdrc->listener_callback->iface.OnNewChannelConnection = urbdrc_on_new_channel_connection;
749 urbdrc->listener_callback->plugin = pPlugin;
750 urbdrc->listener_callback->channel_mgr = pChannelMgr;
751
752 /* [MS-RDPEUSB] 2.1 Transport defines the channel name in uppercase letters */
753 CharUpperA(channelName);
754 status = pChannelMgr->CreateListener(pChannelMgr, channelName, 0,
755 &urbdrc->listener_callback->iface, &urbdrc->listener);
756 if (status != CHANNEL_RC_OK)
757 return status;
758
759 status = CHANNEL_RC_OK;
760 if (udevman->listener_created_callback)
761 status = udevman->listener_created_callback(udevman);
762
763 urbdrc->initialized = status == CHANNEL_RC_OK;
764 return status;
765}
766
772static UINT urbdrc_plugin_terminated(IWTSPlugin* pPlugin)
773{
774 URBDRC_PLUGIN* urbdrc = (URBDRC_PLUGIN*)pPlugin;
775 IUDEVMAN* udevman = NULL;
776
777 if (!urbdrc)
778 return ERROR_INVALID_DATA;
779 if (urbdrc->listener_callback)
780 {
781 IWTSVirtualChannelManager* mgr = urbdrc->listener_callback->channel_mgr;
782 if (mgr)
783 IFCALL(mgr->DestroyListener, mgr, urbdrc->listener);
784 }
785 udevman = urbdrc->udevman;
786
787 if (udevman)
788 {
789 udevman->free(udevman);
790 udevman = NULL;
791 }
792
793 free(urbdrc->subsystem);
794 free(urbdrc->listener_callback);
795 free(urbdrc);
796 return CHANNEL_RC_OK;
797}
798
799static BOOL urbdrc_register_udevman_addin(IWTSPlugin* pPlugin, IUDEVMAN* udevman)
800{
801 URBDRC_PLUGIN* urbdrc = (URBDRC_PLUGIN*)pPlugin;
802
803 if (urbdrc->udevman)
804 {
805 WLog_Print(urbdrc->log, WLOG_ERROR, "existing device, abort.");
806 return FALSE;
807 }
808
809 DEBUG_DVC("device registered.");
810 urbdrc->udevman = udevman;
811 return TRUE;
812}
813
819static UINT urbdrc_load_udevman_addin(IWTSPlugin* pPlugin, LPCSTR name, const ADDIN_ARGV* args)
820{
821 URBDRC_PLUGIN* urbdrc = (URBDRC_PLUGIN*)pPlugin;
822 FREERDP_URBDRC_SERVICE_ENTRY_POINTS entryPoints = { 0 };
823
824 PVIRTUALCHANNELENTRY pvce =
825 freerdp_load_channel_addin_entry(URBDRC_CHANNEL_NAME, name, NULL, 0);
826 PFREERDP_URBDRC_DEVICE_ENTRY entry = WINPR_FUNC_PTR_CAST(pvce, PFREERDP_URBDRC_DEVICE_ENTRY);
827
828 if (!entry)
829 return ERROR_INVALID_OPERATION;
830
831 entryPoints.plugin = pPlugin;
832 entryPoints.pRegisterUDEVMAN = urbdrc_register_udevman_addin;
833 entryPoints.args = args;
834
835 const UINT error = entry(&entryPoints);
836 if (error)
837 {
838 WLog_Print(urbdrc->log, WLOG_ERROR, "%s entry returns error.", name);
839 return error;
840 }
841
842 return CHANNEL_RC_OK;
843}
844
845static BOOL urbdrc_set_subsystem(URBDRC_PLUGIN* urbdrc, const char* subsystem)
846{
847 free(urbdrc->subsystem);
848 urbdrc->subsystem = _strdup(subsystem);
849 return (urbdrc->subsystem != NULL);
850}
851
857static UINT urbdrc_process_addin_args(URBDRC_PLUGIN* urbdrc, const ADDIN_ARGV* args)
858{
859 int status = 0;
860 COMMAND_LINE_ARGUMENT_A urbdrc_args[] = {
861 { "dbg", COMMAND_LINE_VALUE_FLAG, "", NULL, BoolValueFalse, -1, NULL, "debug" },
862 { "sys", COMMAND_LINE_VALUE_REQUIRED, "<subsystem>", NULL, NULL, -1, NULL, "subsystem" },
863 { "dev", COMMAND_LINE_VALUE_REQUIRED, "<device list>", NULL, NULL, -1, NULL, "devices" },
864 { "encode", COMMAND_LINE_VALUE_FLAG, "", NULL, NULL, -1, NULL, "encode" },
865 { "quality", COMMAND_LINE_VALUE_REQUIRED, "<[0-2] -> [high-medium-low]>", NULL, NULL, -1,
866 NULL, "quality" },
867 { NULL, 0, NULL, NULL, NULL, -1, NULL, NULL }
868 };
869
870 const DWORD flags =
871 COMMAND_LINE_SIGIL_NONE | COMMAND_LINE_SEPARATOR_COLON | COMMAND_LINE_IGN_UNKNOWN_KEYWORD;
872 const COMMAND_LINE_ARGUMENT_A* arg = NULL;
873 status =
874 CommandLineParseArgumentsA(args->argc, args->argv, urbdrc_args, flags, urbdrc, NULL, NULL);
875
876 if (status < 0)
877 return ERROR_INVALID_DATA;
878
879 arg = urbdrc_args;
880
881 do
882 {
883 if (!(arg->Flags & COMMAND_LINE_VALUE_PRESENT))
884 continue;
885
886 CommandLineSwitchStart(arg) CommandLineSwitchCase(arg, "dbg")
887 {
888 WLog_SetLogLevel(urbdrc->log, WLOG_TRACE);
889 }
890 CommandLineSwitchCase(arg, "sys")
891 {
892 if (!urbdrc_set_subsystem(urbdrc, arg->Value))
893 return ERROR_OUTOFMEMORY;
894 }
895 CommandLineSwitchDefault(arg)
896 {
897 }
898 CommandLineSwitchEnd(arg)
899 } while ((arg = CommandLineFindNextArgumentA(arg)) != NULL);
900
901 return CHANNEL_RC_OK;
902}
903
904BOOL add_device(IUDEVMAN* idevman, UINT32 flags, BYTE busnum, BYTE devnum, UINT16 idVendor,
905 UINT16 idProduct)
906{
907 UINT32 regflags = 0;
908
909 if (!idevman)
910 return FALSE;
911
912 URBDRC_PLUGIN* urbdrc = (URBDRC_PLUGIN*)idevman->plugin;
913
914 if (!urbdrc || !urbdrc->listener_callback)
915 return FALSE;
916
917 UINT32 mask = (DEVICE_ADD_FLAG_VENDOR | DEVICE_ADD_FLAG_PRODUCT);
918 if ((flags & mask) == mask)
919 regflags |= UDEVMAN_FLAG_ADD_BY_VID_PID;
920 mask = (DEVICE_ADD_FLAG_BUS | DEVICE_ADD_FLAG_DEV);
921 if ((flags & mask) == mask)
922 regflags |= UDEVMAN_FLAG_ADD_BY_ADDR;
923
924 const size_t success =
925 idevman->register_udevice(idevman, busnum, devnum, idVendor, idProduct, regflags);
926
927 if ((success > 0) && (flags & DEVICE_ADD_FLAG_REGISTER))
928 {
929 if (!urbdrc_announce_devices(idevman))
930 return FALSE;
931 }
932
933 return TRUE;
934}
935
936BOOL del_device(IUDEVMAN* idevman, UINT32 flags, BYTE busnum, BYTE devnum, UINT16 idVendor,
937 UINT16 idProduct)
938{
939 IUDEVICE* pdev = NULL;
940 URBDRC_PLUGIN* urbdrc = NULL;
941
942 if (!idevman)
943 return FALSE;
944
945 urbdrc = (URBDRC_PLUGIN*)idevman->plugin;
946
947 if (!urbdrc || !urbdrc->listener_callback)
948 return FALSE;
949
950 idevman->loading_lock(idevman);
951 idevman->rewind(idevman);
952
953 while (idevman->has_next(idevman))
954 {
955 BOOL match = TRUE;
956 IUDEVICE* dev = idevman->get_next(idevman);
957
958 if ((flags & (DEVICE_ADD_FLAG_BUS | DEVICE_ADD_FLAG_DEV | DEVICE_ADD_FLAG_VENDOR |
959 DEVICE_ADD_FLAG_PRODUCT)) == 0)
960 match = FALSE;
961 if (flags & DEVICE_ADD_FLAG_BUS)
962 {
963 if (dev->get_bus_number(dev) != busnum)
964 match = FALSE;
965 }
966 if (flags & DEVICE_ADD_FLAG_DEV)
967 {
968 if (dev->get_dev_number(dev) != devnum)
969 match = FALSE;
970 }
971 if (flags & DEVICE_ADD_FLAG_VENDOR)
972 {
973 int vid = dev->query_device_descriptor(dev, ID_VENDOR);
974 if (vid != idVendor)
975 match = FALSE;
976 }
977 if (flags & DEVICE_ADD_FLAG_PRODUCT)
978 {
979 int pid = dev->query_device_descriptor(dev, ID_PRODUCT);
980 if (pid != idProduct)
981 match = FALSE;
982 }
983
984 if (match)
985 {
986 pdev = dev;
987 break;
988 }
989 }
990
991 if (pdev)
992 pdev->setChannelClosed(pdev);
993
994 idevman->loading_unlock(idevman);
995 return TRUE;
996}
997
1003FREERDP_ENTRY_POINT(UINT VCAPITYPE urbdrc_DVCPluginEntry(IDRDYNVC_ENTRY_POINTS* pEntryPoints))
1004{
1005 UINT status = 0;
1006 const ADDIN_ARGV* args = NULL;
1007 URBDRC_PLUGIN* urbdrc = NULL;
1008 urbdrc = (URBDRC_PLUGIN*)pEntryPoints->GetPlugin(pEntryPoints, URBDRC_CHANNEL_NAME);
1009 args = pEntryPoints->GetPluginData(pEntryPoints);
1010
1011 if (urbdrc == NULL)
1012 {
1013 urbdrc = (URBDRC_PLUGIN*)calloc(1, sizeof(URBDRC_PLUGIN));
1014
1015 if (!urbdrc)
1016 return CHANNEL_RC_NO_MEMORY;
1017
1018 urbdrc->iface.Initialize = urbdrc_plugin_initialize;
1019 urbdrc->iface.Terminated = urbdrc_plugin_terminated;
1020 urbdrc->vchannel_status = INIT_CHANNEL_IN;
1021 status = pEntryPoints->RegisterPlugin(pEntryPoints, URBDRC_CHANNEL_NAME, &urbdrc->iface);
1022
1023 /* After we register the plugin free will be taken care of by dynamic channel */
1024 if (status != CHANNEL_RC_OK)
1025 {
1026 free(urbdrc);
1027 goto fail;
1028 }
1029
1030 urbdrc->log = WLog_Get(TAG);
1031
1032 if (!urbdrc->log)
1033 goto fail;
1034 }
1035
1036 status = urbdrc_process_addin_args(urbdrc, args);
1037
1038 if (status != CHANNEL_RC_OK)
1039 goto fail;
1040
1041 if (!urbdrc->subsystem && !urbdrc_set_subsystem(urbdrc, "libusb"))
1042 goto fail;
1043
1044 return urbdrc_load_udevman_addin(&urbdrc->iface, urbdrc->subsystem, args);
1045fail:
1046 return status;
1047}
1048
1049UINT stream_write_and_free(IWTSPlugin* plugin, IWTSVirtualChannel* channel, wStream* out)
1050{
1051 URBDRC_PLUGIN* urbdrc = (URBDRC_PLUGIN*)plugin;
1052
1053 if (!out)
1054 return ERROR_INVALID_PARAMETER;
1055
1056 if (!channel || !out || !urbdrc)
1057 {
1058 Stream_Free(out, TRUE);
1059 return ERROR_INVALID_PARAMETER;
1060 }
1061
1062 if (!channel->Write)
1063 {
1064 Stream_Free(out, TRUE);
1065 return ERROR_INTERNAL_ERROR;
1066 }
1067
1068 urbdrc_dump_message(urbdrc->log, TRUE, TRUE, out);
1069 const size_t len = Stream_GetPosition(out);
1070 UINT rc = ERROR_INTERNAL_ERROR;
1071 if (len <= UINT32_MAX)
1072 rc = channel->Write(channel, (UINT32)len, Stream_Buffer(out), NULL);
1073 Stream_Free(out, TRUE);
1074 return rc;
1075}
Definition urbdrc_main.h:73