25#include <winpr/sysinfo.h>
26#include <winpr/cast.h>
28#include <urbdrc_helpers.h>
30#include "urbdrc_types.h"
31#include "data_transfer.h"
34static void usb_process_get_port_status(IUDEVICE* pdev,
wStream* out)
36 int bcdUSB = pdev->query_device_descriptor(pdev, BCD_USB);
41 Stream_Write_UINT32(out, 0x303);
45 Stream_Write_UINT32(out, 0x103);
50 Stream_Write_UINT32(out, 0x503);
56static BOOL write_urb_result_header(
wStream* s, UINT16 Size, UINT32 status)
58 if (!Stream_EnsureRemainingCapacity(s, 8ULL + Size))
60 Stream_Write_UINT16(s, Size);
61 Stream_Seek_UINT16(s);
62 Stream_Write_UINT32(s, status);
69static wStream* create_urb_completion_message(UINT32 InterfaceId, UINT32 MessageId,
70 UINT32 RequestId, UINT32 FunctionId)
73 create_shared_message_header_with_functionid(InterfaceId, MessageId, FunctionId, 4);
77 Stream_Write_UINT32(out, RequestId);
82 HRESULT hResult, UINT32 OutputSize,
const void* data)
84 WINPR_ASSERT(callback);
85 UINT status = ERROR_OUTOFMEMORY;
87 if (!Stream_EnsureRemainingCapacity(out, 8ULL + OutputSize))
90 Stream_Write_INT32(out, hResult);
91 Stream_Write_UINT32(out, OutputSize);
92 Stream_Write(out, data, OutputSize);
93 return stream_write_and_free(callback->plugin, callback->channel, out);
96 Stream_Free(out, TRUE);
100static UINT urb_write_completion(WINPR_ATTR_UNUSED IUDEVICE* pdev,
102 UINT32 InterfaceId, UINT32 MessageId, UINT32 RequestId,
103 UINT32 usbd_status, UINT32 OutputBufferSize)
106 return ERROR_INVALID_PARAMETER;
108 if (Stream_Capacity(out) < OutputBufferSize + 36)
110 Stream_Free(out, TRUE);
111 return ERROR_INVALID_PARAMETER;
114 Stream_SetPosition(out, 0);
116 const UINT32 FunctionId = (OutputBufferSize != 0) ? URB_COMPLETION : URB_COMPLETION_NO_DATA;
117 if (!write_shared_message_header_with_functionid(out, InterfaceId, MessageId, FunctionId))
119 Stream_Free(out, TRUE);
120 return ERROR_OUTOFMEMORY;
123 Stream_Write_UINT32(out, RequestId);
124 Stream_Write_UINT32(out, 8);
126 if (!write_urb_result_header(out, 8, usbd_status))
128 Stream_Free(out, TRUE);
129 return ERROR_OUTOFMEMORY;
132 Stream_Write_UINT32(out, 0);
133 Stream_Write_UINT32(out, OutputBufferSize);
134 Stream_Seek(out, OutputBufferSize);
137 return stream_write_and_free(callback->plugin, callback->channel, out);
139 Stream_Free(out, TRUE);
141 return ERROR_SUCCESS;
144static wStream* urb_create_iocompletion(UINT32 InterfaceField, UINT32 MessageId, UINT32 RequestId,
145 UINT32 OutputBufferSize)
147 const UINT32 InterfaceId = (STREAM_ID_PROXY << 30) | (InterfaceField & 0x3FFFFFFF);
149#if UINT32_MAX >= SIZE_MAX
150 if (OutputBufferSize > UINT32_MAX - 28ull)
154 wStream* out = create_shared_message_header_with_functionid(
155 InterfaceId, MessageId, IOCONTROL_COMPLETION, OutputBufferSize + 16ull);
159 Stream_Write_UINT32(out, RequestId);
160 Stream_Write_UINT32(out, USBD_STATUS_SUCCESS);
161 Stream_Write_UINT32(out, OutputBufferSize);
162 Stream_Write_UINT32(out, OutputBufferSize);
166static UINT urbdrc_process_register_request_callback(IUDEVICE* pdev,
170 UINT32 NumRequestCompletion = 0;
171 UINT32 RequestCompletion = 0;
173 if (!callback || !s || !udevman || !pdev)
174 return ERROR_INVALID_PARAMETER;
179 return ERROR_INVALID_PARAMETER;
181 WLog_Print(urbdrc->log, WLOG_DEBUG,
"urbdrc_process_register_request_callback");
183 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4ULL))
184 return ERROR_INVALID_DATA;
186 Stream_Read_UINT32(s, NumRequestCompletion);
188 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4ULL * NumRequestCompletion))
189 return ERROR_INVALID_DATA;
190 for (uint32_t x = 0; x < NumRequestCompletion; x++)
194 Stream_Read_UINT32(s, RequestCompletion);
195 pdev->set_ReqCompletion(pdev, RequestCompletion);
198 return ERROR_SUCCESS;
201static UINT urbdrc_process_cancel_request(IUDEVICE* pdev,
wStream* s, IUDEVMAN* udevman)
206 if (!s || !udevman || !pdev)
207 return ERROR_INVALID_PARAMETER;
211 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
212 return ERROR_INVALID_DATA;
214 Stream_Read_UINT32(s, CancelId);
215 WLog_Print(urbdrc->log, WLOG_DEBUG,
"CANCEL_REQUEST: CancelId=%08" PRIx32
"", CancelId);
217 if (pdev->cancel_transfer_request(pdev, CancelId) < 0)
218 return ERROR_INTERNAL_ERROR;
220 return ERROR_SUCCESS;
223static UINT urbdrc_process_retract_device_request(WINPR_ATTR_UNUSED IUDEVICE* pdev,
wStream* s,
230 return ERROR_INVALID_PARAMETER;
235 return ERROR_INVALID_PARAMETER;
237 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
238 return ERROR_INVALID_DATA;
240 Stream_Read_UINT32(s, Reason);
244 case UsbRetractReason_BlockedByPolicy:
245 WLog_Print(urbdrc->log, WLOG_DEBUG,
246 "UsbRetractReason_BlockedByPolicy: now it is not support");
247 return ERROR_ACCESS_DENIED;
250 WLog_Print(urbdrc->log, WLOG_DEBUG,
251 "urbdrc_process_retract_device_request: Unknown Reason %" PRIu32
"", Reason);
252 return ERROR_ACCESS_DENIED;
255 return ERROR_SUCCESS;
259 wStream* s, UINT32 MessageId, IUDEVMAN* udevman)
261 UINT32 InterfaceId = 0;
262 UINT32 IoControlCode = 0;
263 UINT32 InputBufferSize = 0;
264 UINT32 OutputBufferSize = 0;
265 UINT32 RequestId = 0;
266 UINT32 usbd_status = USBD_STATUS_SUCCESS;
271 if (!callback || !s || !udevman || !pdev)
272 return ERROR_INVALID_PARAMETER;
277 return ERROR_INVALID_PARAMETER;
279 if (!Stream_CheckAndLogRequiredLength(TAG, s, 8))
280 return ERROR_INVALID_DATA;
282 Stream_Read_UINT32(s, IoControlCode);
283 Stream_Read_UINT32(s, InputBufferSize);
285 if (!Stream_SafeSeek(s, InputBufferSize))
286 return ERROR_INVALID_DATA;
287 if (!Stream_CheckAndLogRequiredLength(TAG, s, 8ULL))
288 return ERROR_INVALID_DATA;
290 Stream_Read_UINT32(s, OutputBufferSize);
291 Stream_Read_UINT32(s, RequestId);
293 if (OutputBufferSize > UINT32_MAX - 4)
294 return ERROR_INVALID_DATA;
296 InterfaceId = ((STREAM_ID_PROXY << 30) | pdev->get_ReqCompletion(pdev));
297 out = urb_create_iocompletion(InterfaceId, MessageId, RequestId, OutputBufferSize + 4);
300 return ERROR_OUTOFMEMORY;
302 switch (IoControlCode)
304 case IOCTL_INTERNAL_USB_SUBMIT_URB:
305 WLog_Print(urbdrc->log, WLOG_DEBUG,
"ioctl: IOCTL_INTERNAL_USB_SUBMIT_URB");
306 WLog_Print(urbdrc->log, WLOG_ERROR,
307 " Function IOCTL_INTERNAL_USB_SUBMIT_URB: Unchecked");
310 case IOCTL_INTERNAL_USB_RESET_PORT:
311 WLog_Print(urbdrc->log, WLOG_DEBUG,
"ioctl: IOCTL_INTERNAL_USB_RESET_PORT");
314 case IOCTL_INTERNAL_USB_GET_PORT_STATUS:
315 WLog_Print(urbdrc->log, WLOG_DEBUG,
"ioctl: IOCTL_INTERNAL_USB_GET_PORT_STATUS");
316 success = pdev->query_device_port_status(pdev, &usbd_status, &OutputBufferSize,
317 Stream_Pointer(out));
321 if (!Stream_SafeSeek(out, OutputBufferSize))
323 Stream_Free(out, TRUE);
324 return ERROR_INVALID_DATA;
327 if (pdev->isExist(pdev) == 0)
328 Stream_Write_UINT32(out, 0);
330 usb_process_get_port_status(pdev, out);
335 case IOCTL_INTERNAL_USB_CYCLE_PORT:
336 WLog_Print(urbdrc->log, WLOG_DEBUG,
"ioctl: IOCTL_INTERNAL_USB_CYCLE_PORT");
337 WLog_Print(urbdrc->log, WLOG_ERROR,
338 " Function IOCTL_INTERNAL_USB_CYCLE_PORT: Unchecked");
341 case IOCTL_INTERNAL_USB_SUBMIT_IDLE_NOTIFICATION:
342 WLog_Print(urbdrc->log, WLOG_DEBUG,
343 "ioctl: IOCTL_INTERNAL_USB_SUBMIT_IDLE_NOTIFICATION");
344 WLog_Print(urbdrc->log, WLOG_ERROR,
345 " Function IOCTL_INTERNAL_USB_SUBMIT_IDLE_NOTIFICATION: Unchecked");
349 WLog_Print(urbdrc->log, WLOG_DEBUG,
350 "urbdrc_process_io_control: unknown IoControlCode 0x%" PRIX32
"",
352 Stream_Free(out, TRUE);
353 return ERROR_INVALID_OPERATION;
356 return stream_write_and_free(callback->plugin, callback->channel, out);
360 wStream* s, UINT32 MessageId, IUDEVMAN* udevman)
362 if (!pdev || !callback || !s || !udevman)
363 return ERROR_INVALID_PARAMETER;
366 WINPR_ASSERT(urbdrc);
368 if (!Stream_CheckAndLogRequiredLength(TAG, s, 8))
369 return ERROR_INVALID_DATA;
371 const UINT32 IoControlCode = Stream_Get_UINT32(s);
372 if (IoControlCode != IOCTL_TSUSBGD_IOCTL_USBDI_QUERY_BUS_TIME)
376 "Invalid [MS-RDPEUSB] 2.2.13 USB Internal IO Control Code::IoControlCode0x%08" PRIx32
377 ", must be IOCTL_TSUSBGD_IOCTL_USBDI_QUERY_BUS_TIME [0x00224000]",
379 return ERROR_INVALID_DATA;
381 const UINT32 InputBufferSize = Stream_Get_UINT32(s);
383 if (!Stream_SafeSeek(s, InputBufferSize))
384 return ERROR_INVALID_DATA;
385 if (!Stream_CheckAndLogRequiredLength(TAG, s, 8ULL))
386 return ERROR_INVALID_DATA;
387 const UINT32 OutputBufferSize = Stream_Get_UINT32(s);
388 const UINT32 RequestId = Stream_Get_UINT32(s);
389 const UINT32 InterfaceId = ((STREAM_ID_PROXY << 30) | pdev->get_ReqCompletion(pdev));
392 const UINT32 frames = GetTickCount();
394 if (4 > OutputBufferSize)
396 WLog_Print(urbdrc->log, WLOG_DEBUG,
"out_size %" PRIu32
" > OutputBufferSize %" PRIu32, 4u,
398 return ERROR_BAD_CONFIGURATION;
400 wStream* out = urb_create_iocompletion(InterfaceId, MessageId, RequestId, 4);
403 return ERROR_OUTOFMEMORY;
405 Stream_Write_UINT32(out, frames);
406 return stream_write_and_free(callback->plugin, callback->channel, out);
411 UINT32 InterfaceId, UINT32 MessageId, HRESULT hr,
412 const BYTE* text, uint8_t bytelen)
414 WINPR_ASSERT(callback);
416 const uint8_t charlen = bytelen /
sizeof(WCHAR);
417 wStream* out = create_shared_message_header_with_functionid(InterfaceId, MessageId, charlen,
421 return ERROR_OUTOFMEMORY;
423 Stream_Write(out, text, bytelen);
424 Stream_Write_INT32(out, hr);
425 return stream_write_and_free(callback->plugin, callback->channel, out);
429 wStream* s, UINT32 MessageId, IUDEVMAN* udevman)
433 UINT8 bufferSize = 0xFF;
434 BYTE DeviceDescription[0x100] = WINPR_C_ARRAY_INIT;
436 if (!pdev || !callback || !s || !udevman)
437 return ERROR_INVALID_PARAMETER;
438 if (!Stream_CheckAndLogRequiredLength(TAG, s, 8))
439 return ERROR_INVALID_DATA;
441 Stream_Read_UINT32(s, TextType);
442 Stream_Read_UINT32(s, LocaleId);
443 if (LocaleId > UINT16_MAX)
444 return ERROR_INVALID_DATA;
446 HRESULT hr = (HRESULT)pdev->control_query_device_text(pdev, TextType, (UINT16)LocaleId,
447 &bufferSize, DeviceDescription);
448 const UINT32 InterfaceId = ((STREAM_ID_STUB << 30) | pdev->get_UsbDevice(pdev));
449 return urbdrc_send_query_device_text_response(callback, InterfaceId, MessageId, hr,
450 DeviceDescription, bufferSize);
453static void func_select_all_interface_for_msconfig(
URBDRC_PLUGIN* urbdrc, IUDEVICE* pdev,
456 WINPR_ASSERT(urbdrc);
458 WINPR_ASSERT(MsConfig);
461 UINT32 NumInterfaces = MsConfig->NumInterfaces;
463 for (UINT32 inum = 0; inum < NumInterfaces; inum++)
465 const BYTE InterfaceNumber = MsInterfaces[inum]->InterfaceNumber;
466 const BYTE AlternateSetting = MsInterfaces[inum]->AlternateSetting;
467 const int rc = pdev->select_interface(pdev, InterfaceNumber, AlternateSetting);
470 WLog_Print(urbdrc->log, WLOG_WARN,
"select_interface %" PRIu8
" [%" PRIu8
"] failed",
471 InterfaceNumber, AlternateSetting);
478 UINT32 InterfaceId, UINT32 MessageId,
479 UINT32 RequestId, UINT32 UrbStatus,
483 create_urb_completion_message(InterfaceId, MessageId, RequestId, URB_COMPLETION_NO_DATA);
485 return ERROR_OUTOFMEMORY;
487 const int size = 8 + ((MsConfig) ? MsConfig->MsOutSize : 8);
488 const uint16_t usize = WINPR_ASSERTING_INT_CAST(uint16_t, size);
490 if (!Stream_EnsureRemainingCapacity(out, 4))
492 Stream_Write_UINT32(out, usize);
494 if (!write_urb_result_header(out, usize, UrbStatus))
500 if (!msusb_msconfig_write(MsConfig, out))
505 Stream_Write_UINT32(out, 0);
506 Stream_Write_UINT32(out, 0);
509 return send_urb_completion_message(callback, out, 0, 0,
nullptr);
512 Stream_Free(out, TRUE);
513 return ERROR_OUTOFMEMORY;
517 UINT32 RequestField, UINT32 MessageId, IUDEVMAN* udevman,
521 UINT32 NumInterfaces = 0;
522 UINT32 usbd_status = 0;
523 BYTE ConfigurationDescriptorIsValid = 0;
525 const BOOL noAck = (RequestField & 0x80000000U) != 0;
526 const UINT32 RequestId = RequestField & 0x7FFFFFFF;
528 if (!callback || !s || !udevman || !pdev)
529 return ERROR_INVALID_PARAMETER;
534 return ERROR_INVALID_PARAMETER;
536 if (transferDir == 0)
538 WLog_Print(urbdrc->log, WLOG_ERROR,
"urb_select_configuration: unsupported transfer out");
539 return ERROR_INVALID_PARAMETER;
542 if (!Stream_CheckAndLogRequiredLength(TAG, s, 8))
543 return ERROR_INVALID_DATA;
545 const UINT32 InterfaceId = ((STREAM_ID_PROXY << 30) | pdev->get_ReqCompletion(pdev));
546 Stream_Read_UINT8(s, ConfigurationDescriptorIsValid);
548 Stream_Read_UINT32(s, NumInterfaces);
551 if (ConfigurationDescriptorIsValid)
554 MsConfig = msusb_msconfig_read(s, NumInterfaces);
557 return ERROR_INVALID_DATA;
560 const int lrc = pdev->select_configuration(pdev, MsConfig->bConfigurationValue);
563 msusb_msconfig_free(MsConfig);
565 return ERROR_INTERNAL_ERROR;
569 func_select_all_interface_for_msconfig(urbdrc, pdev, MsConfig);
571 if (!pdev->complete_msconfig_setup(pdev, MsConfig))
573 msusb_msconfig_free(MsConfig);
579 return CHANNEL_RC_OK;
580 return send_urb_select_configuration_result(callback, InterfaceId, MessageId, RequestId,
581 usbd_status, MsConfig);
586 UINT32 InterfaceId, UINT32 MessageId,
589 WINPR_ASSERT(callback);
590 WINPR_ASSERT(MsInterface);
592 const uint32_t interface_size = 16U + (MsInterface->NumberOfPipes * 20U);
594 create_urb_completion_message(InterfaceId, MessageId, RequestId, URB_COMPLETION_NO_DATA);
597 return ERROR_OUTOFMEMORY;
599 const uint32_t size = 8U + interface_size;
600 const uint16_t usize = WINPR_ASSERTING_INT_CAST(uint16_t, size);
602 if (!Stream_EnsureRemainingCapacity(out, 4))
604 Stream_Write_UINT32(out, usize);
606 if (!write_urb_result_header(out, usize, USBD_STATUS_SUCCESS))
609 if (!msusb_msinterface_write(MsInterface, out))
612 return send_urb_completion_message(callback, out, 0, 0,
nullptr);
615 Stream_Free(out, TRUE);
617 return ERROR_INTERNAL_ERROR;
621 UINT32 RequestField, UINT32 MessageId, IUDEVMAN* udevman,
624 const BOOL noAck = (RequestField & 0x80000000U) != 0;
625 const UINT32 RequestId = RequestField & 0x7FFFFFFF;
627 if (!callback || !s || !udevman || !pdev)
628 return ERROR_INVALID_PARAMETER;
633 return ERROR_INVALID_PARAMETER;
635 if (transferDir == 0)
637 WLog_Print(urbdrc->log, WLOG_ERROR,
"urb_select_interface: not support transfer out");
638 return ERROR_INVALID_PARAMETER;
641 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
642 return ERROR_INVALID_DATA;
644 const UINT32 InterfaceId = ((STREAM_ID_PROXY << 30) | pdev->get_ReqCompletion(pdev));
645 const UINT32 ConfigurationHandle = Stream_Get_UINT32(s);
648 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4) || !MsInterface)
650 msusb_msinterface_free(MsInterface);
651 return ERROR_INVALID_DATA;
654 const UINT32 OutputBufferSize = Stream_Get_UINT32(s);
655 if (OutputBufferSize != 0)
657 WLog_Print(urbdrc->log, WLOG_ERROR,
658 "[MS-RDPEUSB] 2.2.9.3 TS_URB_SELECT_INTERFACE::OutputBufferSize must be 0, got "
661 msusb_msinterface_free(MsInterface);
662 return ERROR_INVALID_DATA;
666 pdev->select_interface(pdev, MsInterface->InterfaceNumber, MsInterface->AlternateSetting);
669 msusb_msinterface_free(MsInterface);
670 return ERROR_INTERNAL_ERROR;
675 const uint8_t InterfaceNumber = MsInterface->InterfaceNumber;
676 if (!msusb_msinterface_replace(MsConfig, InterfaceNumber, MsInterface))
677 return ERROR_BAD_CONFIGURATION;
680 if (!pdev->complete_msconfig_setup(pdev, MsConfig))
681 return ERROR_BAD_CONFIGURATION;
684 return CHANNEL_RC_OK;
686 return urb_select_interface_result(callback, RequestId, InterfaceId, MessageId, MsInterface);
690 UINT32 RequestField, UINT32 MessageId, IUDEVMAN* udevman,
691 int transferDir,
int External)
694 UINT32 InterfaceId = 0;
695 UINT32 EndpointAddress = 0;
696 UINT32 PipeHandle = 0;
697 UINT32 TransferFlags = 0;
698 UINT32 OutputBufferSize = 0;
699 UINT32 usbd_status = 0;
701 BYTE bmRequestType = 0;
706 BYTE* buffer =
nullptr;
709 const BOOL noAck = (RequestField & 0x80000000U) != 0;
710 const UINT32 RequestId = RequestField & 0x7FFFFFFF;
712 if (!callback || !s || !udevman || !pdev)
713 return ERROR_INVALID_PARAMETER;
718 return ERROR_INVALID_PARAMETER;
720 if (!Stream_CheckAndLogRequiredLength(TAG, s, 8))
721 return ERROR_INVALID_DATA;
723 InterfaceId = ((STREAM_ID_PROXY << 30) | pdev->get_ReqCompletion(pdev));
724 Stream_Read_UINT32(s, PipeHandle);
725 Stream_Read_UINT32(s, TransferFlags);
726 EndpointAddress = (PipeHandle & 0x000000ff);
731 case URB_CONTROL_TRANSFER_EXTERNAL:
732 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
733 return ERROR_INVALID_DATA;
735 Stream_Read_UINT32(s, Timeout);
738 case URB_CONTROL_TRANSFER_NONEXTERNAL:
745 if (!Stream_CheckAndLogRequiredLength(TAG, s, 12))
746 return ERROR_INVALID_DATA;
748 Stream_Read_UINT8(s, bmRequestType);
749 Stream_Read_UINT8(s, Request);
750 Stream_Read_UINT16(s, Value);
751 Stream_Read_UINT16(s, Index);
752 Stream_Read_UINT16(s, length);
753 Stream_Read_UINT32(s, OutputBufferSize);
755 if (length != OutputBufferSize)
757 WLog_Print(urbdrc->log, WLOG_ERROR,
"urb_control_transfer ERROR: buf != length");
758 return ERROR_INVALID_DATA;
761 out_size = 36 + OutputBufferSize;
762 out = Stream_New(
nullptr, out_size);
765 return ERROR_OUTOFMEMORY;
767 Stream_Seek(out, 36);
769 buffer = Stream_Pointer(out);
771 if (transferDir == USBD_TRANSFER_DIRECTION_OUT)
773 if (!Stream_CheckAndLogRequiredLength(TAG, s, OutputBufferSize))
775 Stream_Free(out, TRUE);
776 return ERROR_INVALID_DATA;
778 Stream_Copy(s, out, OutputBufferSize);
782 if (!pdev->control_transfer(pdev, RequestId, EndpointAddress, TransferFlags, bmRequestType,
783 Request, Value, Index, &usbd_status, &OutputBufferSize, buffer,
786 WLog_Print(urbdrc->log, WLOG_ERROR,
"control_transfer failed");
787 Stream_Free(out, TRUE);
788 return ERROR_INTERNAL_ERROR;
791 return urb_write_completion(pdev, callback, noAck, out, InterfaceId, MessageId, RequestId,
792 usbd_status, OutputBufferSize);
796 UINT32 InterfaceId, BOOL noAck, UINT32 MessageId, UINT32 RequestId,
797 WINPR_ATTR_UNUSED UINT32 NumberOfPackets, UINT32 status,
798 WINPR_ATTR_UNUSED UINT32 StartFrame,
799 WINPR_ATTR_UNUSED UINT32 ErrorCount, UINT32 OutputBufferSize)
801 if (!pdev->isChannelClosed(pdev))
802 urb_write_completion(pdev, callback, noAck, out, InterfaceId, MessageId, RequestId, status,
805 Stream_Free(out, TRUE);
809 wStream* s, UINT32 RequestField, UINT32 MessageId,
810 IUDEVMAN* udevman,
int transferDir)
812 UINT32 EndpointAddress = 0;
813 UINT32 PipeHandle = 0;
814 UINT32 TransferFlags = 0;
815 UINT32 OutputBufferSize = 0;
816 const BOOL noAck = (RequestField & 0x80000000U) != 0;
817 const UINT32 RequestId = RequestField & 0x7FFFFFFF;
819 if (!pdev || !callback || !s || !udevman)
820 return ERROR_INVALID_PARAMETER;
822 if (!Stream_CheckAndLogRequiredLength(TAG, s, 12))
823 return ERROR_INVALID_DATA;
825 Stream_Read_UINT32(s, PipeHandle);
826 Stream_Read_UINT32(s, TransferFlags);
827 Stream_Read_UINT32(s, OutputBufferSize);
828 EndpointAddress = (PipeHandle & 0x000000ff);
830 if (transferDir == USBD_TRANSFER_DIRECTION_OUT)
832 if (!Stream_CheckAndLogRequiredLength(TAG, s, OutputBufferSize))
834 return ERROR_INVALID_DATA;
839 const int rc = pdev->bulk_or_interrupt_transfer(
840 pdev, callback, MessageId, RequestId, EndpointAddress, TransferFlags, noAck,
842 (transferDir == USBD_TRANSFER_DIRECTION_OUT) ? Stream_Pointer(s) : nullptr,
843 urb_bulk_transfer_cb, 10000);
848static void urb_isoch_transfer_cb(WINPR_ATTR_UNUSED IUDEVICE* pdev,
850 UINT32 InterfaceId, BOOL noAck, UINT32 MessageId,
851 UINT32 RequestId, UINT32 NumberOfPackets, UINT32 status,
852 UINT32 StartFrame, UINT32 ErrorCount, UINT32 OutputBufferSize)
856 UINT32 packetSize = (status == 0) ? NumberOfPackets * 12 : 0;
857 Stream_SetPosition(out, 0);
859 const UINT32 FunctionId = (OutputBufferSize == 0) ? URB_COMPLETION_NO_DATA : URB_COMPLETION;
860 if (!write_shared_message_header_with_functionid(out, InterfaceId, MessageId, FunctionId))
862 Stream_Free(out, TRUE);
866 Stream_Write_UINT32(out, RequestId);
867 Stream_Write_UINT32(out, 20 + packetSize);
868 if (!write_urb_result_header(out, WINPR_ASSERTING_INT_CAST(uint16_t, 20 + packetSize),
871 Stream_Free(out, TRUE);
875 Stream_Write_UINT32(out, StartFrame);
880 Stream_Write_UINT32(out, NumberOfPackets);
881 Stream_Write_UINT32(out, ErrorCount);
882 Stream_Seek(out, packetSize);
886 Stream_Write_UINT32(out, 0);
887 Stream_Write_UINT32(out, ErrorCount);
890 Stream_Write_UINT32(out, 0);
891 Stream_Write_UINT32(out, OutputBufferSize);
892 Stream_Seek(out, OutputBufferSize);
894 const UINT rc = stream_write_and_free(callback->plugin, callback->channel, out);
895 if (rc != CHANNEL_RC_OK)
896 WLog_WARN(TAG,
"stream_write_and_free failed with %" PRIu32, rc);
901 UINT32 RequestField, UINT32 MessageId, IUDEVMAN* udevman,
905 UINT32 EndpointAddress = 0;
906 UINT32 PipeHandle = 0;
907 UINT32 TransferFlags = 0;
908 UINT32 StartFrame = 0;
909 UINT32 NumberOfPackets = 0;
910 UINT32 ErrorCount = 0;
911 UINT32 OutputBufferSize = 0;
912 BYTE* packetDescriptorData =
nullptr;
913 const BOOL noAck = (RequestField & 0x80000000U) != 0;
914 const UINT32 RequestId = RequestField & 0x7FFFFFFF;
916 if (!pdev || !callback || !udevman)
917 return ERROR_INVALID_PARAMETER;
919 if (!Stream_CheckAndLogRequiredLength(TAG, s, 20))
920 return ERROR_INVALID_DATA;
922 Stream_Read_UINT32(s, PipeHandle);
923 EndpointAddress = (PipeHandle & 0x000000ff);
924 Stream_Read_UINT32(s, TransferFlags);
925 Stream_Read_UINT32(s, StartFrame);
926 Stream_Read_UINT32(s, NumberOfPackets);
927 Stream_Read_UINT32(s, ErrorCount);
929 if (!Stream_CheckAndLogRequiredLengthOfSize(TAG, s, NumberOfPackets, 12ull))
930 return ERROR_INVALID_DATA;
932 packetDescriptorData = Stream_Pointer(s);
933 Stream_Seek(s, 12ULL * NumberOfPackets);
935 if (!Stream_CheckAndLogRequiredLength(TAG, s,
sizeof(UINT32)))
936 return ERROR_INVALID_DATA;
937 Stream_Read_UINT32(s, OutputBufferSize);
939 if (transferDir == USBD_TRANSFER_DIRECTION_OUT)
941 if (!Stream_CheckAndLogRequiredLength(TAG, s, OutputBufferSize))
942 return ERROR_INVALID_DATA;
945 rc = pdev->isoch_transfer(
946 pdev, callback, MessageId, RequestId, EndpointAddress, TransferFlags, StartFrame,
947 ErrorCount, noAck, packetDescriptorData, NumberOfPackets, OutputBufferSize,
948 (transferDir == USBD_TRANSFER_DIRECTION_OUT) ? Stream_Pointer(s) : nullptr,
949 urb_isoch_transfer_cb, 2000);
952 return ERROR_INTERNAL_ERROR;
957 wStream* s, UINT32 RequestField, UINT32 MessageId,
958 IUDEVMAN* udevman, BYTE func_recipient,
int transferDir)
961 UINT32 InterfaceId = 0;
962 UINT32 OutputBufferSize = 0;
963 UINT32 usbd_status = 0;
964 BYTE bmRequestType = 0;
970 const BOOL noAck = (RequestField & 0x80000000U) != 0;
971 const UINT32 RequestId = RequestField & 0x7FFFFFFF;
973 if (!callback || !s || !udevman || !pdev)
974 return ERROR_INVALID_PARAMETER;
979 return ERROR_INVALID_PARAMETER;
981 if (!Stream_CheckAndLogRequiredLength(TAG, s, 8))
982 return ERROR_INVALID_DATA;
984 InterfaceId = ((STREAM_ID_PROXY << 30) | pdev->get_ReqCompletion(pdev));
985 Stream_Read_UINT8(s, desc_index);
986 Stream_Read_UINT8(s, desc_type);
987 Stream_Read_UINT16(s, langId);
988 Stream_Read_UINT32(s, OutputBufferSize);
989 if (OutputBufferSize > UINT32_MAX - 36)
990 return ERROR_INVALID_DATA;
991 if (transferDir == USBD_TRANSFER_DIRECTION_OUT)
993 if (!Stream_CheckAndLogRequiredLength(TAG, s, OutputBufferSize))
994 return ERROR_INVALID_DATA;
997 out_size = 36ULL + OutputBufferSize;
998 out = Stream_New(
nullptr, out_size);
1001 return ERROR_OUTOFMEMORY;
1003 Stream_Seek(out, 36);
1004 bmRequestType = func_recipient;
1006 switch (transferDir)
1008 case USBD_TRANSFER_DIRECTION_IN:
1009 bmRequestType |= 0x80;
1012 case USBD_TRANSFER_DIRECTION_OUT:
1013 bmRequestType |= 0x00;
1014 Stream_Copy(s, out, OutputBufferSize);
1015 Stream_Rewind(out, OutputBufferSize);
1019 WLog_Print(urbdrc->log, WLOG_DEBUG,
"get error transferDir");
1020 OutputBufferSize = 0;
1021 usbd_status = USBD_STATUS_STALL_PID;
1026 if (!pdev->control_transfer(pdev, RequestId, 0, 0, bmRequestType,
1028 WINPR_ASSERTING_INT_CAST(UINT16, ((desc_type << 8) | desc_index)),
1029 langId, &usbd_status, &OutputBufferSize, Stream_Pointer(out), 1000))
1031 WLog_Print(urbdrc->log, WLOG_ERROR,
"get_descriptor failed");
1032 Stream_Free(out, TRUE);
1033 return ERROR_INTERNAL_ERROR;
1036 return urb_write_completion(pdev, callback, noAck, out, InterfaceId, MessageId, RequestId,
1037 usbd_status, OutputBufferSize);
1041 wStream* s, UINT32 RequestField, UINT32 MessageId,
1042 IUDEVMAN* udevman, BYTE func_recipient,
int transferDir)
1044 size_t out_size = 0;
1045 UINT32 InterfaceId = 0;
1046 UINT32 OutputBufferSize = 0;
1047 UINT32 usbd_status = 0;
1049 BYTE bmRequestType = 0;
1052 const BOOL noAck = (RequestField & 0x80000000U) != 0;
1053 const UINT32 RequestId = RequestField & 0x7FFFFFFF;
1055 if (!callback || !s || !udevman || !pdev)
1056 return ERROR_INVALID_PARAMETER;
1061 return ERROR_INVALID_PARAMETER;
1063 if (transferDir == 0)
1065 WLog_Print(urbdrc->log, WLOG_DEBUG,
1066 "urb_control_get_status_request: transfer out not supported");
1067 return ERROR_INVALID_PARAMETER;
1070 if (!Stream_CheckAndLogRequiredLength(TAG, s, 8))
1071 return ERROR_INVALID_DATA;
1073 InterfaceId = ((STREAM_ID_PROXY << 30) | pdev->get_ReqCompletion(pdev));
1074 Stream_Read_UINT16(s, Index);
1076 Stream_Read_UINT32(s, OutputBufferSize);
1077 if (OutputBufferSize > UINT32_MAX - 36)
1078 return ERROR_INVALID_DATA;
1079 out_size = 36ULL + OutputBufferSize;
1080 out = Stream_New(
nullptr, out_size);
1083 return ERROR_OUTOFMEMORY;
1085 Stream_Seek(out, 36);
1086 bmRequestType = func_recipient | 0x80;
1088 if (!pdev->control_transfer(pdev, RequestId, 0, 0, bmRequestType, 0x00,
1089 0, Index, &usbd_status, &OutputBufferSize, Stream_Pointer(out),
1092 WLog_Print(urbdrc->log, WLOG_ERROR,
"control_transfer failed");
1093 Stream_Free(out, TRUE);
1094 return ERROR_INTERNAL_ERROR;
1097 return urb_write_completion(pdev, callback, noAck, out, InterfaceId, MessageId, RequestId,
1098 usbd_status, OutputBufferSize);
1102 wStream* s, UINT32 RequestField, UINT32 MessageId,
1103 IUDEVMAN* udevman, BYTE func_type,
1104 BYTE func_recipient,
int transferDir)
1106 UINT32 out_size = 0;
1107 UINT32 InterfaceId = 0;
1108 UINT32 TransferFlags = 0;
1109 UINT32 usbd_status = 0;
1110 UINT32 OutputBufferSize = 0;
1111 BYTE ReqTypeReservedBits = 0;
1113 BYTE bmRequestType = 0;
1118 const BOOL noAck = (RequestField & 0x80000000U) != 0;
1119 const UINT32 RequestId = RequestField & 0x7FFFFFFF;
1121 if (!callback || !s || !udevman || !pdev)
1122 return ERROR_INVALID_PARAMETER;
1127 return ERROR_INVALID_PARAMETER;
1129 if (!Stream_CheckAndLogRequiredLength(TAG, s, 16))
1130 return ERROR_INVALID_DATA;
1132 InterfaceId = ((STREAM_ID_PROXY << 30) | pdev->get_ReqCompletion(pdev));
1133 Stream_Read_UINT32(s, TransferFlags);
1134 Stream_Read_UINT8(s, ReqTypeReservedBits);
1135 Stream_Read_UINT8(s, Request);
1136 Stream_Read_UINT16(s, Value);
1137 Stream_Read_UINT16(s, Index);
1138 Stream_Seek_UINT16(s);
1139 Stream_Read_UINT32(s, OutputBufferSize);
1140 if (OutputBufferSize > UINT32_MAX - 36)
1141 return ERROR_INVALID_DATA;
1143 if (transferDir == USBD_TRANSFER_DIRECTION_OUT)
1145 if (!Stream_CheckAndLogRequiredLength(TAG, s, OutputBufferSize))
1146 return ERROR_INVALID_DATA;
1149 out_size = 36ULL + OutputBufferSize;
1150 out = Stream_New(
nullptr, out_size);
1153 return ERROR_OUTOFMEMORY;
1155 Stream_Seek(out, 36);
1158 if (transferDir == USBD_TRANSFER_DIRECTION_OUT)
1160 Stream_Copy(s, out, OutputBufferSize);
1161 Stream_Rewind(out, OutputBufferSize);
1165 bmRequestType = func_type | func_recipient;
1167 if (TransferFlags & USBD_TRANSFER_DIRECTION)
1168 bmRequestType |= 0x80;
1170 WLog_Print(urbdrc->log, WLOG_DEBUG,
1171 "RequestId 0x%" PRIx32
" TransferFlags: 0x%" PRIx32
" ReqTypeReservedBits: 0x%" PRIx8
1173 "Request:0x%" PRIx8
" Value: 0x%" PRIx16
" Index: 0x%" PRIx16
1174 " OutputBufferSize: 0x%" PRIx32
" bmRequestType: 0x%" PRIx8,
1175 RequestId, TransferFlags, ReqTypeReservedBits, Request, Value, Index,
1176 OutputBufferSize, bmRequestType);
1178 if (!pdev->control_transfer(pdev, RequestId, 0, 0, bmRequestType, Request, Value, Index,
1179 &usbd_status, &OutputBufferSize, Stream_Pointer(out), 2000))
1181 WLog_Print(urbdrc->log, WLOG_ERROR,
"control_transfer failed");
1182 Stream_Free(out, TRUE);
1183 return ERROR_INTERNAL_ERROR;
1186 return urb_write_completion(pdev, callback, noAck, out, InterfaceId, MessageId, RequestId,
1187 usbd_status, OutputBufferSize);
1191 wStream* s, UINT32 RequestField, UINT32 MessageId,
1192 IUDEVMAN* udevman,
int transferDir)
1194 size_t out_size = 0;
1195 UINT32 InterfaceId = 0;
1196 UINT32 OutputBufferSize = 0;
1197 UINT32 usbd_status = 0;
1199 BYTE InterfaceNumber = 0;
1200 BYTE Ms_PageIndex = 0;
1201 UINT16 Ms_featureDescIndex = 0;
1205 const BOOL noAck = (RequestField & 0x80000000U) != 0;
1206 const UINT32 RequestId = RequestField & 0x7FFFFFFF;
1208 if (!callback || !s || !udevman || !pdev)
1209 return ERROR_INVALID_PARAMETER;
1214 return ERROR_INVALID_PARAMETER;
1216 if (!Stream_CheckAndLogRequiredLength(TAG, s, 12))
1217 return ERROR_INVALID_DATA;
1220 Stream_Read_UINT8(s, Recipient);
1221 Recipient = (Recipient & 0x1f);
1222 Stream_Read_UINT8(s, InterfaceNumber);
1223 Stream_Read_UINT8(s, Ms_PageIndex);
1224 Stream_Read_UINT16(s, Ms_featureDescIndex);
1226 Stream_Read_UINT32(s, OutputBufferSize);
1227 if (OutputBufferSize > UINT32_MAX - 36)
1228 return ERROR_INVALID_DATA;
1230 switch (transferDir)
1232 case USBD_TRANSFER_DIRECTION_OUT:
1233 if (!Stream_CheckAndLogRequiredLength(TAG, s, OutputBufferSize))
1234 return ERROR_INVALID_DATA;
1242 InterfaceId = ((STREAM_ID_PROXY << 30) | pdev->get_ReqCompletion(pdev));
1243 out_size = 36ULL + OutputBufferSize;
1244 out = Stream_New(
nullptr, out_size);
1247 return ERROR_OUTOFMEMORY;
1249 Stream_Seek(out, 36);
1251 switch (transferDir)
1253 case USBD_TRANSFER_DIRECTION_OUT:
1254 Stream_Copy(s, out, OutputBufferSize);
1255 Stream_Rewind(out, OutputBufferSize);
1258 case USBD_TRANSFER_DIRECTION_IN:
1264 WLog_Print(urbdrc->log, WLOG_DEBUG,
1265 "Ms descriptor arg: Recipient:0x%" PRIx8
", "
1266 "InterfaceNumber:0x%" PRIx8
", Ms_PageIndex:0x%" PRIx8
", "
1267 "Ms_featureDescIndex:0x%" PRIx16
", OutputBufferSize:0x%" PRIx32
"",
1268 Recipient, InterfaceNumber, Ms_PageIndex, Ms_featureDescIndex, OutputBufferSize);
1270 ret = pdev->os_feature_descriptor_request(pdev, RequestId, Recipient, InterfaceNumber,
1271 Ms_PageIndex, Ms_featureDescIndex, &usbd_status,
1272 &OutputBufferSize, Stream_Pointer(out), 1000);
1275 WLog_Print(urbdrc->log, WLOG_DEBUG,
"os_feature_descriptor_request: error num %d", ret);
1277 return urb_write_completion(pdev, callback, noAck, out, InterfaceId, MessageId, RequestId,
1278 usbd_status, OutputBufferSize);
1282 UINT32 RequestField, UINT32 MessageId, IUDEVMAN* udevman,
1283 int transferDir,
int action)
1285 UINT32 usbd_status = 0;
1286 UINT32 ret = USBD_STATUS_REQUEST_FAILED;
1288 const BOOL noAck = (RequestField & 0x80000000U) != 0;
1289 const UINT32 RequestId = RequestField & 0x7FFFFFFF;
1291 if (!callback || !s || !udevman || !pdev)
1292 return ERROR_INVALID_PARAMETER;
1297 return ERROR_INVALID_PARAMETER;
1299 if (!Stream_CheckAndLogRequiredLength(TAG, s, 8))
1300 return ERROR_INVALID_DATA;
1302 if (transferDir == 0)
1304 WLog_Print(urbdrc->log, WLOG_DEBUG,
"urb_pipe_request: not support transfer out");
1305 return ERROR_INVALID_PARAMETER;
1308 const UINT32 InterfaceId = ((STREAM_ID_PROXY << 30) | pdev->get_ReqCompletion(pdev));
1309 const UINT32 PipeHandle = Stream_Get_UINT32(s);
1310 const UINT32 OutputBufferSize = Stream_Get_UINT32(s);
1311 const UINT32 EndpointAddress = (PipeHandle & 0x000000ff);
1313 if (OutputBufferSize != 0)
1315 WLog_Print(urbdrc->log, WLOG_DEBUG,
1316 "2.2.9.4 TS_URB_PIPE_REQUEST OutputBufferSize %" PRIu32
" != 0",
1318 return ERROR_BAD_CONFIGURATION;
1324 rc = pdev->control_pipe_request(pdev, RequestId, EndpointAddress, &usbd_status,
1328 WLog_Print(urbdrc->log, WLOG_DEBUG,
"PIPE SET HALT: error %u", ret);
1330 ret = USBD_STATUS_SUCCESS;
1335 WLog_Print(urbdrc->log, WLOG_DEBUG,
"urb_pipe_request: PIPE_RESET ep 0x%" PRIx32
"",
1337 rc = pdev->control_pipe_request(pdev, RequestId, EndpointAddress, &usbd_status,
1341 WLog_Print(urbdrc->log, WLOG_DEBUG,
"PIPE RESET: error %u", ret);
1343 ret = USBD_STATUS_SUCCESS;
1348 WLog_Print(urbdrc->log, WLOG_DEBUG,
"urb_pipe_request action: %d not supported",
1350 ret = USBD_STATUS_INVALID_URB_FUNCTION;
1356 wStream* out = Stream_New(
nullptr, 36);
1359 return ERROR_OUTOFMEMORY;
1361 return urb_write_completion(pdev, callback, noAck, out, InterfaceId, MessageId, RequestId, ret,
1366 UINT32 RequestId, UINT32 MessageId,
1367 UINT32 CompletionId, UINT32 FrameNumber)
1369 WINPR_ASSERT(callback);
1371 const UINT32 InterfaceId = ((STREAM_ID_PROXY << 30) | CompletionId);
1373 create_urb_completion_message(InterfaceId, MessageId, RequestId, URB_COMPLETION_NO_DATA);
1376 return ERROR_OUTOFMEMORY;
1378 Stream_Write_UINT32(out, 12);
1379 if (!write_urb_result_header(out, 12, USBD_STATUS_SUCCESS))
1381 Stream_Free(out, TRUE);
1382 return ERROR_OUTOFMEMORY;
1385 Stream_Write_UINT32(out, FrameNumber);
1386 return send_urb_completion_message(callback, out, 0, 0,
nullptr);
1390 wStream* s, UINT32 RequestField, UINT32 MessageId,
1391 IUDEVMAN* udevman,
int transferDir)
1393 const BOOL noAck = (RequestField & 0x80000000U) != 0;
1394 const UINT32 RequestId = RequestField & 0x7FFFFFFF;
1396 if (!callback || !s || !udevman || !pdev)
1397 return ERROR_INVALID_PARAMETER;
1402 return ERROR_INVALID_PARAMETER;
1404 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
1405 return ERROR_INVALID_DATA;
1407 if (transferDir == 0)
1409 WLog_Print(urbdrc->log, WLOG_DEBUG,
1410 "urb_get_current_frame_number: not support transfer out");
1411 return ERROR_INVALID_PARAMETER;
1414 const UINT32 OutputBufferSize = Stream_Get_UINT32(s);
1415 if (OutputBufferSize != 0)
1417 WLog_Print(urbdrc->log, WLOG_WARN,
"OutputBufferSize=%" PRIu32
", expected 0",
1421 const UINT32 dummy_frames = GetTickCount();
1422 const UINT32 CompletionId = pdev->get_ReqCompletion(pdev);
1425 return CHANNEL_RC_OK;
1427 return urb_send_current_frame_number_result(callback, RequestId, MessageId, CompletionId,
1432static UINT urb_control_get_configuration_request(IUDEVICE* pdev,
1434 UINT32 RequestField, UINT32 MessageId,
1435 IUDEVMAN* udevman,
int transferDir)
1437 size_t out_size = 0;
1438 UINT32 InterfaceId = 0;
1439 UINT32 OutputBufferSize = 0;
1440 UINT32 usbd_status = 0;
1443 const BOOL noAck = (RequestField & 0x80000000U) != 0;
1444 const UINT32 RequestId = RequestField & 0x7FFFFFFF;
1446 if (!callback || !s || !udevman || !pdev)
1447 return ERROR_INVALID_PARAMETER;
1452 return ERROR_INVALID_PARAMETER;
1454 if (transferDir == 0)
1456 WLog_Print(urbdrc->log, WLOG_DEBUG,
1457 "urb_control_get_configuration_request:"
1458 " not support transfer out");
1459 return ERROR_INVALID_PARAMETER;
1462 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
1463 return ERROR_INVALID_DATA;
1465 Stream_Read_UINT32(s, OutputBufferSize);
1466 if (OutputBufferSize > UINT32_MAX - 36)
1467 return ERROR_INVALID_DATA;
1468 out_size = 36ULL + OutputBufferSize;
1469 out = Stream_New(
nullptr, out_size);
1472 return ERROR_OUTOFMEMORY;
1474 Stream_Seek(out, 36);
1475 InterfaceId = ((STREAM_ID_PROXY << 30) | pdev->get_ReqCompletion(pdev));
1477 if (!pdev->control_transfer(pdev, RequestId, 0, 0, 0x80 | 0x00,
1479 0, 0, &usbd_status, &OutputBufferSize, Stream_Pointer(out), 1000))
1481 WLog_Print(urbdrc->log, WLOG_DEBUG,
"control_transfer failed");
1482 Stream_Free(out, TRUE);
1483 return ERROR_INTERNAL_ERROR;
1486 return urb_write_completion(pdev, callback, noAck, out, InterfaceId, MessageId, RequestId,
1487 usbd_status, OutputBufferSize);
1492 wStream* s, UINT32 RequestField, UINT32 MessageId,
1493 IUDEVMAN* udevman,
int transferDir)
1495 size_t out_size = 0;
1496 UINT32 InterfaceId = 0;
1497 UINT32 OutputBufferSize = 0;
1498 UINT32 usbd_status = 0;
1499 UINT16 InterfaceNr = 0;
1502 const BOOL noAck = (RequestField & 0x80000000U) != 0;
1503 const UINT32 RequestId = RequestField & 0x7FFFFFFF;
1505 if (!callback || !s || !udevman || !pdev)
1506 return ERROR_INVALID_PARAMETER;
1511 return ERROR_INVALID_PARAMETER;
1513 if (!Stream_CheckAndLogRequiredLength(TAG, s, 8))
1514 return ERROR_INVALID_DATA;
1516 if (transferDir == 0)
1518 WLog_Print(urbdrc->log, WLOG_DEBUG,
1519 "urb_control_get_interface_request: not support transfer out");
1520 return ERROR_INVALID_PARAMETER;
1523 InterfaceId = ((STREAM_ID_PROXY << 30) | pdev->get_ReqCompletion(pdev));
1524 Stream_Read_UINT16(s, InterfaceNr);
1526 Stream_Read_UINT32(s, OutputBufferSize);
1527 if (OutputBufferSize > UINT32_MAX - 36)
1528 return ERROR_INVALID_DATA;
1529 out_size = 36ULL + OutputBufferSize;
1530 out = Stream_New(
nullptr, out_size);
1533 return ERROR_OUTOFMEMORY;
1535 Stream_Seek(out, 36);
1537 if (!pdev->control_transfer(
1538 pdev, RequestId, 0, 0, 0x80 | 0x01, 0x0A,
1539 0, InterfaceNr, &usbd_status, &OutputBufferSize, Stream_Pointer(out), 1000))
1541 WLog_Print(urbdrc->log, WLOG_DEBUG,
"control_transfer failed");
1542 Stream_Free(out, TRUE);
1543 return ERROR_INTERNAL_ERROR;
1546 return urb_write_completion(pdev, callback, noAck, out, InterfaceId, MessageId, RequestId,
1547 usbd_status, OutputBufferSize);
1551 wStream* s, UINT32 RequestField, UINT32 MessageId,
1552 IUDEVMAN* udevman, BYTE func_recipient, BYTE command,
1555 UINT32 InterfaceId = 0;
1556 UINT32 OutputBufferSize = 0;
1557 UINT32 usbd_status = 0;
1558 UINT16 FeatureSelector = 0;
1560 BYTE bmRequestType = 0;
1564 const BOOL noAck = (RequestField & 0x80000000U) != 0;
1565 const UINT32 RequestId = RequestField & 0x7FFFFFFF;
1567 if (!callback || !s || !udevman || !pdev)
1568 return ERROR_INVALID_PARAMETER;
1573 return ERROR_INVALID_PARAMETER;
1575 if (!Stream_CheckAndLogRequiredLength(TAG, s, 8))
1576 return ERROR_INVALID_DATA;
1578 InterfaceId = ((STREAM_ID_PROXY << 30) | pdev->get_ReqCompletion(pdev));
1579 Stream_Read_UINT16(s, FeatureSelector);
1580 Stream_Read_UINT16(s, Index);
1581 Stream_Read_UINT32(s, OutputBufferSize);
1582 if (OutputBufferSize > UINT32_MAX - 36)
1583 return ERROR_INVALID_DATA;
1584 switch (transferDir)
1586 case USBD_TRANSFER_DIRECTION_OUT:
1587 if (!Stream_CheckAndLogRequiredLength(TAG, s, OutputBufferSize))
1588 return ERROR_INVALID_DATA;
1596 out = Stream_New(
nullptr, 36ULL + OutputBufferSize);
1599 return ERROR_OUTOFMEMORY;
1601 Stream_Seek(out, 36);
1602 bmRequestType = func_recipient;
1604 switch (transferDir)
1606 case USBD_TRANSFER_DIRECTION_OUT:
1607 WLog_Print(urbdrc->log, WLOG_ERROR,
1608 "Function urb_control_feature_request: OUT Unchecked");
1609 Stream_Copy(s, out, OutputBufferSize);
1610 Stream_Rewind(out, OutputBufferSize);
1611 bmRequestType |= 0x00;
1614 case USBD_TRANSFER_DIRECTION_IN:
1615 bmRequestType |= 0x80;
1623 case URB_SET_FEATURE:
1627 case URB_CLEAR_FEATURE:
1632 WLog_Print(urbdrc->log, WLOG_ERROR,
1633 "urb_control_feature_request: Error Command 0x%02" PRIx8
"", command);
1634 Stream_Free(out, TRUE);
1635 return ERROR_INTERNAL_ERROR;
1638 if (!pdev->control_transfer(pdev, RequestId, 0, 0, bmRequestType, bmRequest, FeatureSelector,
1639 Index, &usbd_status, &OutputBufferSize, Stream_Pointer(out), 1000))
1641 WLog_Print(urbdrc->log, WLOG_DEBUG,
"feature control transfer failed");
1642 Stream_Free(out, TRUE);
1643 return ERROR_INTERNAL_ERROR;
1646 return urb_write_completion(pdev, callback, noAck, out, InterfaceId, MessageId, RequestId,
1647 usbd_status, OutputBufferSize);
1651 wStream* s, UINT32 MessageId, IUDEVMAN* udevman,
1656 UINT16 URB_Function = 0;
1657 UINT32 RequestId = 0;
1658 UINT error = ERROR_INTERNAL_ERROR;
1661 if (!callback || !s || !udevman || !pdev)
1662 return ERROR_INVALID_PARAMETER;
1667 return ERROR_INVALID_PARAMETER;
1669 if (!Stream_CheckAndLogRequiredLength(TAG, s, 12))
1670 return ERROR_INVALID_DATA;
1672 Stream_Read_UINT32(s, CbTsUrb);
1673 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4ULL + CbTsUrb))
1674 return ERROR_INVALID_DATA;
1675 Stream_Read_UINT16(s, Size);
1676 if (Size != CbTsUrb)
1678 const char* section = (transferDir == USBD_TRANSFER_DIRECTION_IN)
1679 ?
"2.2.6.7 Transfer In Request (TRANSFER_IN_REQUEST)"
1680 :
"2.2.6.8 Transfer Out Request (TRANSFER_OUT_REQUEST)";
1682 "[MS-RDPEUSB] 2.2.9.1.1 TS_URB_HEADER::Size 0x%04" PRIx16
1683 " != %s::CbTsUrb 0x%08" PRIx32,
1684 Size, section, CbTsUrb);
1685 return ERROR_INVALID_DATA;
1687 Stream_Read_UINT16(s, URB_Function);
1688 Stream_Read_UINT32(s, RequestId);
1689 WLog_Print(urbdrc->log, WLOG_DEBUG,
"URB %s[%" PRIu16
"]", urb_function_string(URB_Function),
1692 switch (URB_Function)
1694 case TS_URB_SELECT_CONFIGURATION:
1695 error = urb_select_configuration(pdev, callback, s, RequestId, MessageId, udevman,
1699 case TS_URB_SELECT_INTERFACE:
1701 urb_select_interface(pdev, callback, s, RequestId, MessageId, udevman, transferDir);
1704 case TS_URB_PIPE_REQUEST:
1705 error = urb_pipe_request(pdev, callback, s, RequestId, MessageId, udevman, transferDir,
1709 case TS_URB_TAKE_FRAME_LENGTH_CONTROL:
1715 case TS_URB_RELEASE_FRAME_LENGTH_CONTROL:
1721 case TS_URB_GET_FRAME_LENGTH:
1727 case TS_URB_SET_FRAME_LENGTH:
1733 case TS_URB_GET_CURRENT_FRAME_NUMBER:
1734 error = urb_get_current_frame_number(pdev, callback, s, RequestId, MessageId, udevman,
1738 case TS_URB_CONTROL_TRANSFER:
1739 error = urb_control_transfer(pdev, callback, s, RequestId, MessageId, udevman,
1740 transferDir, URB_CONTROL_TRANSFER_NONEXTERNAL);
1743 case TS_URB_BULK_OR_INTERRUPT_TRANSFER:
1744 error = urb_bulk_or_interrupt_transfer(pdev, callback, s, RequestId, MessageId, udevman,
1748 case TS_URB_ISOCH_TRANSFER:
1750 urb_isoch_transfer(pdev, callback, s, RequestId, MessageId, udevman, transferDir);
1753 case TS_URB_GET_DESCRIPTOR_FROM_DEVICE:
1754 error = urb_control_descriptor_request(pdev, callback, s, RequestId, MessageId, udevman,
1758 case TS_URB_SET_DESCRIPTOR_TO_DEVICE:
1759 error = urb_control_descriptor_request(pdev, callback, s, RequestId, MessageId, udevman,
1763 case TS_URB_SET_FEATURE_TO_DEVICE:
1764 error = urb_control_feature_request(pdev, callback, s, RequestId, MessageId, udevman,
1765 0x00, URB_SET_FEATURE, transferDir);
1768 case TS_URB_SET_FEATURE_TO_INTERFACE:
1769 error = urb_control_feature_request(pdev, callback, s, RequestId, MessageId, udevman,
1770 0x01, URB_SET_FEATURE, transferDir);
1773 case TS_URB_SET_FEATURE_TO_ENDPOINT:
1774 error = urb_control_feature_request(pdev, callback, s, RequestId, MessageId, udevman,
1775 0x02, URB_SET_FEATURE, transferDir);
1778 case TS_URB_CLEAR_FEATURE_TO_DEVICE:
1779 error = urb_control_feature_request(pdev, callback, s, RequestId, MessageId, udevman,
1780 0x00, URB_CLEAR_FEATURE, transferDir);
1783 case TS_URB_CLEAR_FEATURE_TO_INTERFACE:
1784 error = urb_control_feature_request(pdev, callback, s, RequestId, MessageId, udevman,
1785 0x01, URB_CLEAR_FEATURE, transferDir);
1788 case TS_URB_CLEAR_FEATURE_TO_ENDPOINT:
1789 error = urb_control_feature_request(pdev, callback, s, RequestId, MessageId, udevman,
1790 0x02, URB_CLEAR_FEATURE, transferDir);
1793 case TS_URB_GET_STATUS_FROM_DEVICE:
1794 error = urb_control_get_status_request(pdev, callback, s, RequestId, MessageId, udevman,
1798 case TS_URB_GET_STATUS_FROM_INTERFACE:
1799 error = urb_control_get_status_request(pdev, callback, s, RequestId, MessageId, udevman,
1803 case TS_URB_GET_STATUS_FROM_ENDPOINT:
1804 error = urb_control_get_status_request(pdev, callback, s, RequestId, MessageId, udevman,
1808 case TS_URB_RESERVED_0X0016:
1811 case TS_URB_VENDOR_DEVICE:
1812 error = urb_control_vendor_or_class_request(pdev, callback, s, RequestId, MessageId,
1813 udevman, (0x02 << 5),
1817 case TS_URB_VENDOR_INTERFACE:
1818 error = urb_control_vendor_or_class_request(pdev, callback, s, RequestId, MessageId,
1819 udevman, (0x02 << 5),
1823 case TS_URB_VENDOR_ENDPOINT:
1824 error = urb_control_vendor_or_class_request(pdev, callback, s, RequestId, MessageId,
1825 udevman, (0x02 << 5),
1829 case TS_URB_CLASS_DEVICE:
1830 error = urb_control_vendor_or_class_request(pdev, callback, s, RequestId, MessageId,
1831 udevman, (0x01 << 5),
1835 case TS_URB_CLASS_INTERFACE:
1836 error = urb_control_vendor_or_class_request(pdev, callback, s, RequestId, MessageId,
1837 udevman, (0x01 << 5),
1841 case TS_URB_CLASS_ENDPOINT:
1842 error = urb_control_vendor_or_class_request(pdev, callback, s, RequestId, MessageId,
1843 udevman, (0x01 << 5),
1847 case TS_URB_RESERVE_0X001D:
1850 case TS_URB_SYNC_RESET_PIPE_AND_CLEAR_STALL:
1851 error = urb_pipe_request(pdev, callback, s, RequestId, MessageId, udevman, transferDir,
1855 case TS_URB_CLASS_OTHER:
1856 error = urb_control_vendor_or_class_request(pdev, callback, s, RequestId, MessageId,
1857 udevman, (0x01 << 5),
1861 case TS_URB_VENDOR_OTHER:
1862 error = urb_control_vendor_or_class_request(pdev, callback, s, RequestId, MessageId,
1863 udevman, (0x02 << 5),
1867 case TS_URB_GET_STATUS_FROM_OTHER:
1868 error = urb_control_get_status_request(pdev, callback, s, RequestId, MessageId, udevman,
1872 case TS_URB_CLEAR_FEATURE_TO_OTHER:
1873 error = urb_control_feature_request(pdev, callback, s, RequestId, MessageId, udevman,
1874 0x03, URB_CLEAR_FEATURE, transferDir);
1877 case TS_URB_SET_FEATURE_TO_OTHER:
1878 error = urb_control_feature_request(pdev, callback, s, RequestId, MessageId, udevman,
1879 0x03, URB_SET_FEATURE, transferDir);
1882 case TS_URB_GET_DESCRIPTOR_FROM_ENDPOINT:
1883 error = urb_control_descriptor_request(pdev, callback, s, RequestId, MessageId, udevman,
1887 case TS_URB_SET_DESCRIPTOR_TO_ENDPOINT:
1888 error = urb_control_descriptor_request(pdev, callback, s, RequestId, MessageId, udevman,
1892 case TS_URB_CONTROL_GET_CONFIGURATION_REQUEST:
1893 error = urb_control_get_configuration_request(pdev, callback, s, RequestId, MessageId,
1894 udevman, transferDir);
1897 case TS_URB_CONTROL_GET_INTERFACE_REQUEST:
1898 error = urb_control_get_interface_request(pdev, callback, s, RequestId, MessageId,
1899 udevman, transferDir);
1902 case TS_URB_GET_DESCRIPTOR_FROM_INTERFACE:
1903 error = urb_control_descriptor_request(pdev, callback, s, RequestId, MessageId, udevman,
1907 case TS_URB_SET_DESCRIPTOR_TO_INTERFACE:
1908 error = urb_control_descriptor_request(pdev, callback, s, RequestId, MessageId, udevman,
1912 case TS_URB_GET_OS_FEATURE_DESCRIPTOR_REQUEST:
1913 error = urb_os_feature_descriptor_request(pdev, callback, s, RequestId, MessageId,
1914 udevman, transferDir);
1917 case TS_URB_RESERVE_0X002B:
1918 case TS_URB_RESERVE_0X002C:
1919 case TS_URB_RESERVE_0X002D:
1920 case TS_URB_RESERVE_0X002E:
1921 case TS_URB_RESERVE_0X002F:
1925 case TS_URB_SYNC_RESET_PIPE:
1926 error = urb_pipe_request(pdev, callback, s, RequestId, MessageId, udevman, transferDir,
1930 case TS_URB_SYNC_CLEAR_STALL:
1931 urb_pipe_request(pdev, callback, s, RequestId, MessageId, udevman, transferDir,
1935 case TS_URB_CONTROL_TRANSFER_EX:
1936 error = urb_control_transfer(pdev, callback, s, RequestId, MessageId, udevman,
1937 transferDir, URB_CONTROL_TRANSFER_EXTERNAL);
1941 WLog_Print(urbdrc->log, WLOG_DEBUG,
"URB_Func: %" PRIx16
" is not found!",
1948 WLog_Print(urbdrc->log, WLOG_WARN,
1949 "USB transfer request URB Function '%s' [0x%08x] failed with %08" PRIx32,
1950 urb_function_string(URB_Function), URB_Function, error);
1957 IUDEVMAN* udevman,
wStream* data)
1959 UINT32 InterfaceId = 0;
1960 UINT32 MessageId = 0;
1961 UINT32 FunctionId = 0;
1962 IUDEVICE* pdev =
nullptr;
1963 UINT error = ERROR_INTERNAL_ERROR;
1965 if (!urbdrc || !data || !callback || !udevman)
1968 if (!Stream_CheckAndLogRequiredLength(TAG, data, 8))
1971 Stream_Rewind_UINT32(data);
1973 Stream_Read_UINT32(data, InterfaceId);
1974 Stream_Read_UINT32(data, MessageId);
1975 Stream_Read_UINT32(data, FunctionId);
1977 pdev = udevman->get_udevice_by_UsbDevice(udevman, InterfaceId);
1980 if (pdev ==
nullptr)
1982 error = ERROR_SUCCESS;
1987 if (pdev->isChannelClosed(pdev))
1989 error = ERROR_SUCCESS;
1994 if (!pdev->detach_kernel_driver(pdev))
1996 error = ERROR_SUCCESS;
2002 case CANCEL_REQUEST:
2003 error = urbdrc_process_cancel_request(pdev, data, udevman);
2006 case REGISTER_REQUEST_CALLBACK:
2007 error = urbdrc_process_register_request_callback(pdev, callback, data, udevman);
2011 error = urbdrc_process_io_control(pdev, callback, data, MessageId, udevman);
2014 case INTERNAL_IO_CONTROL:
2015 error = urbdrc_process_internal_io_control(pdev, callback, data, MessageId, udevman);
2018 case QUERY_DEVICE_TEXT:
2019 error = urbdrc_process_query_device_text(pdev, callback, data, MessageId, udevman);
2022 case TRANSFER_IN_REQUEST:
2023 error = urbdrc_process_transfer_request(pdev, callback, data, MessageId, udevman,
2024 USBD_TRANSFER_DIRECTION_IN);
2027 case TRANSFER_OUT_REQUEST:
2028 error = urbdrc_process_transfer_request(pdev, callback, data, MessageId, udevman,
2029 USBD_TRANSFER_DIRECTION_OUT);
2032 case RETRACT_DEVICE:
2033 error = urbdrc_process_retract_device_request(pdev, data, udevman);
2037 WLog_Print(urbdrc->log, WLOG_WARN,
2038 "urbdrc_process_udev_data_transfer:"
2039 " unknown FunctionId 0x%" PRIX32
"",
2047 WLog_WARN(TAG,
"USB request failed with %08" PRIx32, error);