2012-04-25 22:26:35 +04:00
|
|
|
/**
|
2012-10-09 07:02:04 +04:00
|
|
|
* FreeRDP: A Remote Desktop Protocol Implementation
|
2012-04-25 22:26:35 +04:00
|
|
|
* RemoteFX USB Redirection
|
|
|
|
*
|
|
|
|
* Copyright 2012 Atrust corp.
|
|
|
|
* Copyright 2012 Alfred Liu <alfred.liu@atruscorp.com>
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
2015-03-28 03:08:36 +03:00
|
|
|
#if defined(__linux__)
|
2012-04-25 22:26:35 +04:00
|
|
|
#include <libudev.h>
|
2015-03-28 03:08:36 +03:00
|
|
|
#endif
|
2012-04-25 22:26:35 +04:00
|
|
|
|
2017-11-14 18:10:52 +03:00
|
|
|
#include <errno.h>
|
2018-08-24 14:49:19 +03:00
|
|
|
#include <winpr/crt.h>
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
#include "libusb_udevice.h"
|
|
|
|
|
|
|
|
#define BASIC_STATE_FUNC_DEFINED(_arg, _type) \
|
2017-11-14 18:10:52 +03:00
|
|
|
static _type udev_get_##_arg (IUDEVICE* idev) \
|
|
|
|
{ \
|
|
|
|
UDEVICE* pdev = (UDEVICE*) idev; \
|
|
|
|
return pdev->_arg; \
|
|
|
|
} \
|
|
|
|
static void udev_set_##_arg (IUDEVICE* idev, _type _t) \
|
|
|
|
{ \
|
|
|
|
UDEVICE* pdev = (UDEVICE*) idev; \
|
|
|
|
pdev->_arg = _t; \
|
|
|
|
}
|
2012-04-25 22:26:35 +04:00
|
|
|
|
|
|
|
#define BASIC_POINT_FUNC_DEFINED(_arg, _type) \
|
2017-11-14 18:10:52 +03:00
|
|
|
static _type udev_get_p_##_arg (IUDEVICE* idev) \
|
|
|
|
{ \
|
|
|
|
UDEVICE* pdev = (UDEVICE*) idev; \
|
|
|
|
return pdev->_arg; \
|
|
|
|
} \
|
|
|
|
static void udev_set_p_##_arg (IUDEVICE* idev, _type _t) \
|
|
|
|
{ \
|
|
|
|
UDEVICE* pdev = (UDEVICE*) idev; \
|
|
|
|
pdev->_arg = _t; \
|
|
|
|
}
|
2012-04-25 22:26:35 +04:00
|
|
|
|
|
|
|
#define BASIC_STATE_FUNC_REGISTER(_arg, _dev) \
|
|
|
|
_dev->iface.get_##_arg = udev_get_##_arg; \
|
|
|
|
_dev->iface.set_##_arg = udev_set_##_arg
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct _ISO_USER_DATA ISO_USER_DATA;
|
2012-11-21 04:34:52 +04:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
struct _ISO_USER_DATA
|
|
|
|
{
|
2012-11-21 04:34:52 +04:00
|
|
|
BYTE* IsoPacket;
|
|
|
|
BYTE* output_data;
|
|
|
|
int iso_status;
|
|
|
|
int completed;
|
|
|
|
UINT32 error_count;
|
|
|
|
int noack;
|
|
|
|
UINT32 start_frame;
|
2012-04-25 22:26:35 +04:00
|
|
|
};
|
|
|
|
|
2017-11-14 18:10:52 +03:00
|
|
|
static int get_next_timeout(libusb_context* ctx, struct timeval* tv, struct timeval* out)
|
2012-04-25 22:26:35 +04:00
|
|
|
{
|
2012-11-21 04:34:52 +04:00
|
|
|
int r;
|
2012-04-25 22:26:35 +04:00
|
|
|
struct timeval timeout;
|
2012-11-21 04:34:52 +04:00
|
|
|
r = libusb_get_next_timeout(ctx, &timeout);
|
|
|
|
|
|
|
|
if (r)
|
|
|
|
{
|
2012-04-25 22:26:35 +04:00
|
|
|
/* timeout already expired? */
|
|
|
|
if (!timerisset(&timeout))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
/* choose the smallest of next URB timeout or user specified timeout */
|
|
|
|
if (timercmp(&timeout, tv, <))
|
|
|
|
*out = timeout;
|
|
|
|
else
|
|
|
|
*out = *tv;
|
2012-11-21 04:34:52 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-04-25 22:26:35 +04:00
|
|
|
*out = *tv;
|
|
|
|
}
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-11-14 18:10:52 +03:00
|
|
|
/*
|
2012-04-25 22:26:35 +04:00
|
|
|
* a simple wrapper to implement libusb_handle_events_timeout_completed
|
|
|
|
* function in libusb library git tree (1.0.9 later) */
|
2017-11-14 18:10:52 +03:00
|
|
|
static int handle_events_completed(libusb_context* ctx, int* completed)
|
2012-04-25 22:26:35 +04:00
|
|
|
{
|
|
|
|
struct timeval tv;
|
|
|
|
tv.tv_sec = 60;
|
|
|
|
tv.tv_usec = 0;
|
|
|
|
#ifdef HAVE_NEW_LIBUSB
|
|
|
|
return libusb_handle_events_timeout_completed(ctx, &tv, completed);
|
|
|
|
#else
|
|
|
|
int r;
|
|
|
|
struct timeval poll_timeout;
|
|
|
|
r = get_next_timeout(ctx, &tv, &poll_timeout);
|
|
|
|
retry:
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2012-11-21 04:34:52 +04:00
|
|
|
if (libusb_try_lock_events(ctx) == 0)
|
|
|
|
{
|
|
|
|
if (completed == NULL || !*completed)
|
|
|
|
{
|
2012-04-25 22:26:35 +04:00
|
|
|
/* we obtained the event lock: do our own event handling */
|
2017-11-14 18:10:52 +03:00
|
|
|
WLog_DBG(TAG, "doing our own event handling");
|
2012-04-25 22:26:35 +04:00
|
|
|
r = libusb_handle_events_locked(ctx, &tv);
|
|
|
|
}
|
2012-11-21 04:34:52 +04:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
libusb_unlock_events(ctx);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* another thread is doing event handling. wait for thread events that
|
|
|
|
* notify event completion. */
|
|
|
|
libusb_lock_event_waiters(ctx);
|
|
|
|
|
|
|
|
if (completed && *completed)
|
|
|
|
goto already_done;
|
|
|
|
|
2012-11-21 04:34:52 +04:00
|
|
|
if (!libusb_event_handler_active(ctx))
|
|
|
|
{
|
2012-04-25 22:26:35 +04:00
|
|
|
/* we hit a race: whoever was event handling earlier finished in the
|
|
|
|
* time it took us to reach this point. try the cycle again. */
|
|
|
|
libusb_unlock_event_waiters(ctx);
|
2017-11-14 18:10:52 +03:00
|
|
|
WLog_DBG(TAG, "event handler was active but went away, retrying");
|
2012-04-25 22:26:35 +04:00
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
|
2017-11-14 18:10:52 +03:00
|
|
|
WLog_DBG(TAG, "another thread is doing event handling");
|
2012-04-25 22:26:35 +04:00
|
|
|
r = libusb_wait_for_event(ctx, &poll_timeout);
|
|
|
|
already_done:
|
|
|
|
libusb_unlock_event_waiters(ctx);
|
|
|
|
|
2012-11-21 04:34:52 +04:00
|
|
|
if (r < 0)
|
|
|
|
{
|
2012-04-25 22:26:35 +04:00
|
|
|
return r;
|
|
|
|
}
|
2012-11-21 04:34:52 +04:00
|
|
|
else if (r == 1)
|
|
|
|
{
|
2012-04-25 22:26:35 +04:00
|
|
|
return libusb_handle_events_timeout(ctx, &tv);
|
|
|
|
}
|
|
|
|
else
|
2012-11-21 04:34:52 +04:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
#endif /* HAVE_NEW_LIBUSE */
|
|
|
|
}
|
|
|
|
|
2017-11-14 18:10:52 +03:00
|
|
|
static void func_iso_callback(struct libusb_transfer* transfer)
|
2012-04-25 22:26:35 +04:00
|
|
|
{
|
2012-11-21 04:34:52 +04:00
|
|
|
ISO_USER_DATA* iso_user_data = (ISO_USER_DATA*) transfer->user_data;
|
|
|
|
BYTE* data = iso_user_data->IsoPacket;
|
|
|
|
int* completed = &iso_user_data->completed;
|
|
|
|
UINT32 offset = 0;
|
2019-02-07 16:32:38 +03:00
|
|
|
INT32 index = 0;
|
|
|
|
INT32 i, act_len;
|
2012-11-21 04:34:52 +04:00
|
|
|
BYTE* b;
|
2012-04-25 22:26:35 +04:00
|
|
|
*completed = 1;
|
2017-11-14 18:10:52 +03:00
|
|
|
|
|
|
|
/* Fixme: currently fill the dummy frame number, tt needs to be
|
2012-04-25 22:26:35 +04:00
|
|
|
* filled a real frame number */
|
|
|
|
// urbdrc_get_mstime(iso_user_data->start_frame);
|
2012-11-21 04:34:52 +04:00
|
|
|
if ((transfer->status == LIBUSB_TRANSFER_COMPLETED) && (!iso_user_data->noack))
|
2012-04-25 22:26:35 +04:00
|
|
|
{
|
2017-11-14 18:10:52 +03:00
|
|
|
for (i = 0; i < transfer->num_iso_packets; i++)
|
2012-04-25 22:26:35 +04:00
|
|
|
{
|
|
|
|
act_len = transfer->iso_packet_desc[i].actual_length;
|
2012-10-09 11:26:39 +04:00
|
|
|
data_write_UINT32(data + offset, index);
|
|
|
|
data_write_UINT32(data + offset + 4, act_len);
|
2017-11-14 18:10:52 +03:00
|
|
|
data_write_UINT32(data + offset + 8,
|
|
|
|
transfer->iso_packet_desc[i].status);
|
2012-04-25 22:26:35 +04:00
|
|
|
offset += 12;
|
2012-11-21 04:34:52 +04:00
|
|
|
|
2017-11-14 18:10:52 +03:00
|
|
|
if (transfer->iso_packet_desc[i].status == USBD_STATUS_SUCCESS)
|
2012-04-25 22:26:35 +04:00
|
|
|
{
|
|
|
|
b = libusb_get_iso_packet_buffer_simple(transfer, i);
|
2012-11-21 04:34:52 +04:00
|
|
|
|
2017-11-14 18:10:52 +03:00
|
|
|
if (act_len > 0)
|
2012-04-25 22:26:35 +04:00
|
|
|
{
|
|
|
|
if (iso_user_data->output_data + index != b)
|
|
|
|
memcpy(iso_user_data->output_data + index, b, act_len);
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
index += act_len;
|
|
|
|
}
|
2012-11-21 04:34:52 +04:00
|
|
|
else
|
|
|
|
{
|
2016-12-14 00:47:08 +03:00
|
|
|
//WLog_ERR(TAG, "actual length %"PRIu32"", act_len);
|
2012-04-25 22:26:35 +04:00
|
|
|
//exit(EXIT_FAILURE);
|
|
|
|
}
|
2017-11-14 18:10:52 +03:00
|
|
|
}
|
|
|
|
else
|
2012-04-25 22:26:35 +04:00
|
|
|
{
|
|
|
|
iso_user_data->error_count++;
|
|
|
|
//print_transfer_status(transfer->iso_packet_desc[i].status);
|
|
|
|
}
|
|
|
|
}
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
transfer->actual_length = index;
|
|
|
|
iso_user_data->iso_status = 1;
|
|
|
|
}
|
2012-11-21 04:34:52 +04:00
|
|
|
else if ((transfer->status == LIBUSB_TRANSFER_COMPLETED) && (iso_user_data->noack))
|
2012-04-25 22:26:35 +04:00
|
|
|
{
|
2017-11-14 18:10:52 +03:00
|
|
|
/* This situation occurs when we do not need to
|
2012-04-25 22:26:35 +04:00
|
|
|
* return any packet */
|
|
|
|
iso_user_data->iso_status = 1;
|
|
|
|
}
|
2012-11-21 04:34:52 +04:00
|
|
|
else
|
|
|
|
{
|
2012-04-25 22:26:35 +04:00
|
|
|
//print_status(transfer->status);
|
|
|
|
iso_user_data->iso_status = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-21 04:34:52 +04:00
|
|
|
static const LIBUSB_ENDPOINT_DESCEIPTOR* func_get_ep_desc(LIBUSB_CONFIG_DESCRIPTOR* LibusbConfig,
|
2017-11-14 18:10:52 +03:00
|
|
|
MSUSB_CONFIG_DESCRIPTOR* MsConfig, UINT32 EndpointAddress)
|
2012-04-25 22:26:35 +04:00
|
|
|
{
|
2012-10-09 11:01:37 +04:00
|
|
|
BYTE alt;
|
2019-02-07 16:32:38 +03:00
|
|
|
UINT32 inum, pnum;
|
2012-11-21 04:34:52 +04:00
|
|
|
MSUSB_INTERFACE_DESCRIPTOR** MsInterfaces;
|
|
|
|
const LIBUSB_INTERFACE* interface;
|
|
|
|
const LIBUSB_ENDPOINT_DESCEIPTOR* endpoint;
|
2012-04-25 22:26:35 +04:00
|
|
|
MsInterfaces = MsConfig->MsInterfaces;
|
|
|
|
interface = LibusbConfig->interface;
|
2012-11-21 04:34:52 +04:00
|
|
|
|
|
|
|
for (inum = 0; inum < MsConfig->NumInterfaces; inum++)
|
2012-04-25 22:26:35 +04:00
|
|
|
{
|
|
|
|
alt = MsInterfaces[inum]->AlternateSetting;
|
|
|
|
endpoint = interface[inum].altsetting[alt].endpoint;
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2012-11-21 04:34:52 +04:00
|
|
|
for (pnum = 0; pnum < MsInterfaces[inum]->NumberOfPipes; pnum++)
|
2012-04-25 22:26:35 +04:00
|
|
|
{
|
|
|
|
if (endpoint[pnum].bEndpointAddress == EndpointAddress)
|
|
|
|
{
|
|
|
|
return &endpoint[pnum];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-11-21 04:34:52 +04:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-11-14 18:10:52 +03:00
|
|
|
static void func_bulk_transfer_cb(struct libusb_transfer* transfer)
|
2012-04-25 22:26:35 +04:00
|
|
|
{
|
2017-11-14 18:10:52 +03:00
|
|
|
int* completed = transfer->user_data;
|
2012-04-25 22:26:35 +04:00
|
|
|
*completed = 1;
|
|
|
|
/* caller interprets results and frees transfer */
|
|
|
|
}
|
|
|
|
|
2012-11-21 04:34:52 +04:00
|
|
|
static int func_set_usbd_status(UDEVICE* pdev, UINT32* status, int err_result)
|
2012-04-25 22:26:35 +04:00
|
|
|
{
|
|
|
|
switch (err_result)
|
|
|
|
{
|
|
|
|
case LIBUSB_SUCCESS:
|
|
|
|
*status = USBD_STATUS_SUCCESS;
|
|
|
|
break;
|
2012-11-21 04:34:52 +04:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
case LIBUSB_ERROR_IO:
|
|
|
|
*status = USBD_STATUS_STALL_PID;
|
2017-11-14 18:10:52 +03:00
|
|
|
WLog_ERR(TAG, "LIBUSB_ERROR_IO!!");
|
2012-04-25 22:26:35 +04:00
|
|
|
break;
|
2012-11-21 04:34:52 +04:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
case LIBUSB_ERROR_INVALID_PARAM:
|
|
|
|
*status = USBD_STATUS_INVALID_PARAMETER;
|
|
|
|
break;
|
2012-11-21 04:34:52 +04:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
case LIBUSB_ERROR_ACCESS:
|
|
|
|
*status = USBD_STATUS_NOT_ACCESSED;
|
|
|
|
break;
|
2012-11-21 04:34:52 +04:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
case LIBUSB_ERROR_NO_DEVICE:
|
|
|
|
*status = USBD_STATUS_DEVICE_GONE;
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2012-11-21 04:34:52 +04:00
|
|
|
if (pdev)
|
|
|
|
{
|
|
|
|
if (!(pdev->status & URBDRC_DEVICE_NOT_FOUND))
|
2012-04-25 22:26:35 +04:00
|
|
|
{
|
|
|
|
pdev->status |= URBDRC_DEVICE_NOT_FOUND;
|
2017-11-14 18:10:52 +03:00
|
|
|
WLog_WARN(TAG, "LIBUSB_ERROR_NO_DEVICE!!");
|
2012-04-25 22:26:35 +04:00
|
|
|
}
|
|
|
|
}
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
break;
|
2012-11-21 04:34:52 +04:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
case LIBUSB_ERROR_NOT_FOUND:
|
|
|
|
*status = USBD_STATUS_STALL_PID;
|
|
|
|
break;
|
2012-11-21 04:34:52 +04:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
case LIBUSB_ERROR_BUSY:
|
|
|
|
*status = USBD_STATUS_STALL_PID;
|
|
|
|
break;
|
2012-11-21 04:34:52 +04:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
case LIBUSB_ERROR_TIMEOUT:
|
|
|
|
*status = USBD_STATUS_TIMEOUT;
|
|
|
|
break;
|
2012-11-21 04:34:52 +04:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
case LIBUSB_ERROR_OVERFLOW:
|
|
|
|
*status = USBD_STATUS_STALL_PID;
|
|
|
|
break;
|
2012-11-21 04:34:52 +04:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
case LIBUSB_ERROR_PIPE:
|
|
|
|
*status = USBD_STATUS_STALL_PID;
|
|
|
|
break;
|
2012-11-21 04:34:52 +04:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
case LIBUSB_ERROR_INTERRUPTED:
|
|
|
|
*status = USBD_STATUS_STALL_PID;
|
|
|
|
break;
|
2012-11-21 04:34:52 +04:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
case LIBUSB_ERROR_NO_MEM:
|
|
|
|
*status = USBD_STATUS_NO_MEMORY;
|
|
|
|
break;
|
2012-11-21 04:34:52 +04:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
case LIBUSB_ERROR_NOT_SUPPORTED:
|
|
|
|
*status = USBD_STATUS_NOT_SUPPORTED;
|
|
|
|
break;
|
2012-11-21 04:34:52 +04:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
case LIBUSB_ERROR_OTHER:
|
|
|
|
*status = USBD_STATUS_STALL_PID;
|
|
|
|
break;
|
2012-11-21 04:34:52 +04:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
default:
|
|
|
|
*status = USBD_STATUS_SUCCESS;
|
|
|
|
break;
|
|
|
|
}
|
2012-11-21 04:34:52 +04:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-11-21 04:34:52 +04:00
|
|
|
static void func_iso_data_init(ISO_USER_DATA* iso_user_data, UINT32 numPacket, UINT32 buffsize,
|
2017-11-14 18:10:52 +03:00
|
|
|
UINT32 noAck, BYTE* isoPacket, BYTE* buffer)
|
2012-04-25 22:26:35 +04:00
|
|
|
{
|
|
|
|
/* init struct iso_user_data */
|
|
|
|
iso_user_data->IsoPacket = isoPacket;
|
|
|
|
iso_user_data->output_data = buffer;
|
|
|
|
iso_user_data->error_count = 0;
|
|
|
|
iso_user_data->completed = 0;
|
|
|
|
iso_user_data->noack = noAck;
|
|
|
|
urbdrc_get_mstime(iso_user_data->start_frame);
|
|
|
|
}
|
|
|
|
|
2017-11-14 18:10:52 +03:00
|
|
|
static int func_config_release_all_interface(LIBUSB_DEVICE_HANDLE* libusb_handle,
|
|
|
|
UINT32 NumInterfaces)
|
2012-04-25 22:26:35 +04:00
|
|
|
{
|
2019-02-07 16:32:38 +03:00
|
|
|
UINT32 i;
|
|
|
|
int ret;
|
2012-11-21 04:34:52 +04:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
for (i = 0; i < NumInterfaces; i++)
|
|
|
|
{
|
2017-11-14 18:10:52 +03:00
|
|
|
ret = libusb_release_interface(libusb_handle, i);
|
2012-11-21 04:34:52 +04:00
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2014-09-12 18:19:32 +04:00
|
|
|
WLog_ERR(TAG, "config_release_all_interface: error num %d", ret);
|
2012-04-25 22:26:35 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2012-11-21 04:34:52 +04:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-11-21 04:34:52 +04:00
|
|
|
static int func_claim_all_interface(LIBUSB_DEVICE_HANDLE* libusb_handle, int NumInterfaces)
|
2012-04-25 22:26:35 +04:00
|
|
|
{
|
|
|
|
int i, ret;
|
2012-11-21 04:34:52 +04:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
for (i = 0; i < NumInterfaces; i++)
|
|
|
|
{
|
2012-11-21 04:34:52 +04:00
|
|
|
ret = libusb_claim_interface(libusb_handle, i);
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2014-09-12 18:19:32 +04:00
|
|
|
WLog_ERR(TAG, "claim_all_interface: error num %d", ret);
|
2012-04-25 22:26:35 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2012-11-21 04:34:52 +04:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2012-11-21 04:34:52 +04:00
|
|
|
static void* print_transfer_status(enum libusb_transfer_status status)
|
2012-04-25 22:26:35 +04:00
|
|
|
{
|
2012-11-21 04:34:52 +04:00
|
|
|
switch (status)
|
2012-04-25 22:26:35 +04:00
|
|
|
{
|
|
|
|
case LIBUSB_TRANSFER_COMPLETED:
|
2014-09-12 18:19:32 +04:00
|
|
|
//WLog_ERR(TAG, "Transfer Status: LIBUSB_TRANSFER_COMPLETED");
|
2012-04-25 22:26:35 +04:00
|
|
|
break;
|
|
|
|
case LIBUSB_TRANSFER_ERROR:
|
2014-09-12 18:19:32 +04:00
|
|
|
WLog_ERR(TAG, "Transfer Status: LIBUSB_TRANSFER_ERROR");
|
2012-04-25 22:26:35 +04:00
|
|
|
break;
|
|
|
|
case LIBUSB_TRANSFER_TIMED_OUT:
|
2014-09-12 18:19:32 +04:00
|
|
|
WLog_ERR(TAG, "Transfer Status: LIBUSB_TRANSFER_TIMED_OUT");
|
2012-04-25 22:26:35 +04:00
|
|
|
break;
|
|
|
|
case LIBUSB_TRANSFER_CANCELLED:
|
2014-09-12 18:19:32 +04:00
|
|
|
WLog_ERR(TAG, "Transfer Status: LIBUSB_TRANSFER_CANCELLED");
|
2012-04-25 22:26:35 +04:00
|
|
|
break;
|
|
|
|
case LIBUSB_TRANSFER_STALL:
|
2014-09-12 18:19:32 +04:00
|
|
|
WLog_ERR(TAG, "Transfer Status: LIBUSB_TRANSFER_STALL");
|
2012-04-25 22:26:35 +04:00
|
|
|
break;
|
|
|
|
case LIBUSB_TRANSFER_NO_DEVICE:
|
2014-09-12 18:19:32 +04:00
|
|
|
WLog_ERR(TAG, "Transfer Status: LIBUSB_TRANSFER_NO_DEVICE");
|
2012-04-25 22:26:35 +04:00
|
|
|
break;
|
|
|
|
case LIBUSB_TRANSFER_OVERFLOW:
|
2014-09-12 18:19:32 +04:00
|
|
|
WLog_ERR(TAG, "Transfer Status: LIBUSB_TRANSFER_OVERFLOW");
|
2012-04-25 22:26:35 +04:00
|
|
|
break;
|
|
|
|
default:
|
2014-09-12 18:19:32 +04:00
|
|
|
WLog_ERR(TAG, "Transfer Status: Get unknow error num %d (0x%x)",
|
2012-04-25 22:26:35 +04:00
|
|
|
status, status);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-11-21 04:34:52 +04:00
|
|
|
static void print_status(enum libusb_transfer_status status)
|
2012-04-25 22:26:35 +04:00
|
|
|
{
|
2012-11-21 04:34:52 +04:00
|
|
|
switch (status)
|
2012-04-25 22:26:35 +04:00
|
|
|
{
|
|
|
|
case LIBUSB_TRANSFER_COMPLETED:
|
2014-09-12 18:19:32 +04:00
|
|
|
WLog_ERR(TAG, "Transfer status: LIBUSB_TRANSFER_COMPLETED");
|
2012-04-25 22:26:35 +04:00
|
|
|
break;
|
|
|
|
case LIBUSB_TRANSFER_ERROR:
|
2014-09-12 18:19:32 +04:00
|
|
|
WLog_ERR(TAG, "Transfer status: LIBUSB_TRANSFER_ERROR");
|
2012-04-25 22:26:35 +04:00
|
|
|
break;
|
|
|
|
case LIBUSB_TRANSFER_TIMED_OUT:
|
2014-09-12 18:19:32 +04:00
|
|
|
WLog_ERR(TAG, "Transfer status: LIBUSB_TRANSFER_TIMED_OUT");
|
2012-04-25 22:26:35 +04:00
|
|
|
break;
|
|
|
|
case LIBUSB_TRANSFER_CANCELLED:
|
2014-09-12 18:19:32 +04:00
|
|
|
WLog_ERR(TAG, "Transfer status: LIBUSB_TRANSFER_CANCELLED");
|
2012-04-25 22:26:35 +04:00
|
|
|
break;
|
|
|
|
case LIBUSB_TRANSFER_STALL:
|
2014-09-12 18:19:32 +04:00
|
|
|
WLog_ERR(TAG, "Transfer status: LIBUSB_TRANSFER_STALL");
|
2012-04-25 22:26:35 +04:00
|
|
|
break;
|
|
|
|
case LIBUSB_TRANSFER_NO_DEVICE:
|
2014-09-12 18:19:32 +04:00
|
|
|
WLog_ERR(TAG, "Transfer status: LIBUSB_TRANSFER_NO_DEVICE");
|
2012-04-25 22:26:35 +04:00
|
|
|
break;
|
2017-11-14 18:10:52 +03:00
|
|
|
case LIBUSB_TRANSFER_OVERFLOW:
|
2014-09-12 18:19:32 +04:00
|
|
|
WLog_ERR(TAG, "Transfer status: LIBUSB_TRANSFER_OVERFLOW");
|
2012-04-25 22:26:35 +04:00
|
|
|
break;
|
|
|
|
default:
|
2014-09-12 18:19:32 +04:00
|
|
|
WLog_ERR(TAG, "Transfer status: unknow status %d(0x%x)", status, status);
|
2012-04-25 22:26:35 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2012-11-21 04:34:52 +04:00
|
|
|
static LIBUSB_DEVICE* udev_get_libusb_dev(int bus_number, int dev_number)
|
2012-04-25 22:26:35 +04:00
|
|
|
{
|
2015-03-28 03:08:36 +03:00
|
|
|
ssize_t i, total_device;
|
2012-11-21 04:34:52 +04:00
|
|
|
LIBUSB_DEVICE** libusb_list;
|
2017-11-14 18:10:52 +03:00
|
|
|
total_device = libusb_get_device_list(NULL, &libusb_list);
|
2012-11-21 04:34:52 +04:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
for (i = 0; i < total_device; i++)
|
|
|
|
{
|
2012-11-21 04:34:52 +04:00
|
|
|
if ((bus_number == libusb_get_bus_number(libusb_list[i])) &&
|
2017-11-14 18:10:52 +03:00
|
|
|
(dev_number == libusb_get_device_address(libusb_list[i])))
|
2012-04-25 22:26:35 +04:00
|
|
|
return libusb_list[i];
|
|
|
|
}
|
2012-11-21 04:34:52 +04:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
libusb_free_device_list(libusb_list, 1);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-11-21 04:34:52 +04:00
|
|
|
static LIBUSB_DEVICE_DESCRIPTOR* udev_new_descript(LIBUSB_DEVICE* libusb_dev)
|
2012-04-25 22:26:35 +04:00
|
|
|
{
|
|
|
|
int ret;
|
2012-11-21 04:34:52 +04:00
|
|
|
LIBUSB_DEVICE_DESCRIPTOR* descriptor;
|
|
|
|
descriptor = (LIBUSB_DEVICE_DESCRIPTOR*) malloc(sizeof(LIBUSB_DEVICE_DESCRIPTOR));
|
2012-04-25 22:26:35 +04:00
|
|
|
ret = libusb_get_device_descriptor(libusb_dev, descriptor);
|
2012-11-21 04:34:52 +04:00
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2014-09-12 18:19:32 +04:00
|
|
|
WLog_ERR(TAG, "libusb_get_device_descriptor: ERROR!!");
|
2013-08-29 17:30:22 +04:00
|
|
|
free(descriptor);
|
2012-04-25 22:26:35 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
return descriptor;
|
|
|
|
}
|
|
|
|
|
2015-03-28 03:08:36 +03:00
|
|
|
/* Get HUB handle */
|
|
|
|
#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
|
2017-11-14 18:10:52 +03:00
|
|
|
static int udev_get_hub_handle(UDEVICE* pdev, UINT16 bus_number, UINT16 dev_number)
|
|
|
|
{
|
2015-03-28 03:08:36 +03:00
|
|
|
int error;
|
|
|
|
ssize_t i, total_device, ports_cnt;
|
|
|
|
uint8_t port_numbers[16];
|
2017-11-14 18:10:52 +03:00
|
|
|
LIBUSB_DEVICE** libusb_list;
|
2015-03-28 03:08:36 +03:00
|
|
|
total_device = libusb_get_device_list(NULL, &libusb_list);
|
|
|
|
/* Look for device. */
|
|
|
|
error = -1;
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2015-05-02 06:26:08 +03:00
|
|
|
for (i = 0; i < total_device; i ++)
|
|
|
|
{
|
2015-03-28 03:08:36 +03:00
|
|
|
if ((bus_number != libusb_get_bus_number(libusb_list[i])) ||
|
|
|
|
(dev_number != libusb_get_device_address(libusb_list[i])))
|
|
|
|
continue;
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2015-03-28 03:08:36 +03:00
|
|
|
error = libusb_open(libusb_list[i], &pdev->hub_handle);
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2015-05-02 06:26:08 +03:00
|
|
|
if (error < 0)
|
|
|
|
{
|
2017-11-14 18:10:52 +03:00
|
|
|
WLog_ERR(TAG, "libusb_open error: %i - %s", error, libusb_strerror(error));
|
2015-03-28 03:08:36 +03:00
|
|
|
break;
|
|
|
|
}
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2015-03-28 03:08:36 +03:00
|
|
|
/* get port number */
|
|
|
|
error = libusb_get_port_numbers(libusb_list[i], port_numbers, sizeof(port_numbers));
|
|
|
|
libusb_close(pdev->hub_handle);
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2015-05-02 06:26:08 +03:00
|
|
|
if (error < 1)
|
2017-11-14 18:10:52 +03:00
|
|
|
{
|
|
|
|
/* Prevent open hub, treat as error. */
|
|
|
|
WLog_ERR(TAG, "libusb_get_port_numbers error: %i - %s", error, libusb_strerror(error));
|
2015-03-28 03:08:36 +03:00
|
|
|
break;
|
|
|
|
}
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2015-03-28 03:08:36 +03:00
|
|
|
pdev->port_number = port_numbers[(error - 1)];
|
|
|
|
error = 0;
|
|
|
|
WLog_DBG(TAG, " Port: %d", pdev->port_number);
|
|
|
|
/* gen device path */
|
2018-08-24 11:39:48 +03:00
|
|
|
sprintf_s(pdev->path, ARRAYSIZE(pdev->path), "ugen%"PRIu16".%"PRIu16"", bus_number, dev_number);
|
2015-03-28 03:08:36 +03:00
|
|
|
WLog_DBG(TAG, " DevPath: %s", pdev->path);
|
|
|
|
break;
|
|
|
|
}
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2015-03-28 03:08:36 +03:00
|
|
|
/* Look for device hub. */
|
2015-05-02 06:26:08 +03:00
|
|
|
if (error == 0)
|
|
|
|
{
|
2015-03-28 03:08:36 +03:00
|
|
|
error = -1;
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2015-05-02 06:26:08 +03:00
|
|
|
for (i = 0; i < total_device; i ++)
|
|
|
|
{
|
2015-03-28 03:08:36 +03:00
|
|
|
if ((bus_number != libusb_get_bus_number(libusb_list[i])) ||
|
|
|
|
(1 != libusb_get_device_address(libusb_list[i]))) /* Root hub allways first on bus. */
|
|
|
|
continue;
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2016-12-14 00:47:08 +03:00
|
|
|
WLog_DBG(TAG, " Open hub: %"PRIu16"", bus_number);
|
2015-03-28 03:08:36 +03:00
|
|
|
error = libusb_open(libusb_list[i], &pdev->hub_handle);
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2015-03-28 03:08:36 +03:00
|
|
|
if (error < 0)
|
2017-11-14 18:10:52 +03:00
|
|
|
WLog_ERR(TAG, "libusb_open error: %i - %s", error, libusb_strerror(error));
|
|
|
|
|
2015-03-28 03:08:36 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2015-03-28 03:08:36 +03:00
|
|
|
libusb_free_device_list(libusb_list, 1);
|
|
|
|
|
|
|
|
if (error < 0)
|
|
|
|
return -1;
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2015-03-28 03:08:36 +03:00
|
|
|
WLog_DBG(TAG, "libusb_open success!");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#if defined(__linux__)
|
2012-11-21 04:34:52 +04:00
|
|
|
static int udev_get_hub_handle(UDEVICE* pdev, UINT16 bus_number, UINT16 dev_number)
|
2012-04-25 22:26:35 +04:00
|
|
|
{
|
2012-11-21 04:34:52 +04:00
|
|
|
struct udev* udev;
|
|
|
|
struct udev_enumerate* enumerate;
|
|
|
|
struct udev_list_entry* devices;
|
|
|
|
struct udev_list_entry* dev_list_entry;
|
|
|
|
struct udev_device* dev;
|
2017-11-14 18:10:52 +03:00
|
|
|
LIBUSB_DEVICE* libusb_dev;
|
2012-04-25 22:26:35 +04:00
|
|
|
int hub_found = 0;
|
2017-11-14 18:10:52 +03:00
|
|
|
unsigned long hub_bus = 0;
|
|
|
|
unsigned long hub_dev = 0;
|
2012-04-25 22:26:35 +04:00
|
|
|
int error = 0;
|
|
|
|
udev = udev_new();
|
2012-11-21 04:34:52 +04:00
|
|
|
|
|
|
|
if (!udev)
|
|
|
|
{
|
2014-09-16 12:16:05 +04:00
|
|
|
WLog_ERR(TAG, "Can't create udev");
|
2012-04-25 22:26:35 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
enumerate = udev_enumerate_new(udev);
|
|
|
|
udev_enumerate_add_match_subsystem(enumerate, "usb");
|
|
|
|
udev_enumerate_add_match_property(enumerate, "DEVTYPE", "usb_device");
|
|
|
|
udev_enumerate_scan_devices(enumerate);
|
|
|
|
devices = udev_enumerate_get_list_entry(enumerate);
|
2017-11-14 18:10:52 +03:00
|
|
|
udev_list_entry_foreach(dev_list_entry, devices)
|
2012-04-25 22:26:35 +04:00
|
|
|
{
|
2017-11-14 18:10:52 +03:00
|
|
|
const char* path;
|
|
|
|
errno = 0;
|
2012-04-25 22:26:35 +04:00
|
|
|
path = udev_list_entry_get_name(dev_list_entry);
|
|
|
|
dev = udev_device_new_from_syspath(udev, path);
|
2012-11-21 04:34:52 +04:00
|
|
|
|
|
|
|
if (!dev)
|
|
|
|
continue;
|
|
|
|
|
2017-11-14 18:10:52 +03:00
|
|
|
unsigned long tmp_b, tmp_d;
|
2019-10-18 12:44:24 +03:00
|
|
|
tmp_b = strtoul(udev_device_get_property_value(dev, "BUSNUM"), NULL, 10);
|
2017-11-14 18:10:52 +03:00
|
|
|
|
|
|
|
if (errno != 0)
|
|
|
|
continue;
|
|
|
|
|
2019-10-18 12:44:24 +03:00
|
|
|
tmp_d = strtoul(udev_device_get_property_value(dev, "DEVNUM"), NULL, 10);
|
2017-11-14 18:10:52 +03:00
|
|
|
|
|
|
|
if (errno != 0)
|
|
|
|
continue;
|
2012-11-21 04:34:52 +04:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
if (bus_number == tmp_b && dev_number == tmp_d)
|
|
|
|
{
|
|
|
|
/* get port number */
|
2017-11-14 18:10:52 +03:00
|
|
|
char* p1, *p2;
|
2012-11-21 04:34:52 +04:00
|
|
|
const char* sysfs_path =
|
2017-11-14 18:10:52 +03:00
|
|
|
udev_device_get_property_value(dev, "DEVPATH");
|
2012-11-21 04:34:52 +04:00
|
|
|
p1 = (char*) sysfs_path;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2012-04-25 22:26:35 +04:00
|
|
|
p2 = p1 + 1;
|
|
|
|
p1 = strchr(p2, '.');
|
2012-11-21 04:34:52 +04:00
|
|
|
}
|
|
|
|
while (p1 != NULL);
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2019-02-07 17:09:47 +03:00
|
|
|
if ((size_t)((p2 - sysfs_path) + 2U) < strlen(sysfs_path))
|
2012-04-25 22:26:35 +04:00
|
|
|
{
|
2012-11-21 04:34:52 +04:00
|
|
|
p1 = (char*) sysfs_path;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2012-04-25 22:26:35 +04:00
|
|
|
p2 = p1 + 1;
|
|
|
|
p1 = strchr(p2, '-');
|
2012-11-21 04:34:52 +04:00
|
|
|
}
|
|
|
|
while (p1 != NULL);
|
2012-04-25 22:26:35 +04:00
|
|
|
}
|
2012-11-21 04:34:52 +04:00
|
|
|
|
2017-11-14 18:10:52 +03:00
|
|
|
errno = 0;
|
|
|
|
{
|
|
|
|
unsigned long val = strtoul(p2, NULL, 0);
|
|
|
|
|
|
|
|
if ((errno != 0) || (val == 0) || (val > UINT16_MAX))
|
|
|
|
continue;
|
2012-04-25 22:26:35 +04:00
|
|
|
|
2017-11-14 18:10:52 +03:00
|
|
|
pdev->port_number = val;
|
|
|
|
}
|
|
|
|
WLog_DBG(TAG, " Port: %d", pdev->port_number);
|
2012-04-25 22:26:35 +04:00
|
|
|
/* get device path */
|
2012-11-21 04:34:52 +04:00
|
|
|
p1 = (char*) sysfs_path;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2012-04-25 22:26:35 +04:00
|
|
|
p2 = p1 + 1;
|
|
|
|
p1 = strchr(p2, '/');
|
2012-11-21 04:34:52 +04:00
|
|
|
}
|
|
|
|
while (p1 != NULL);
|
|
|
|
|
2018-08-24 11:39:48 +03:00
|
|
|
sprintf_s(pdev->path, ARRAYSIZE(pdev->path), "%s", p2);
|
2017-11-14 18:10:52 +03:00
|
|
|
WLog_DBG(TAG, " DevPath: %s", pdev->path);
|
2012-04-25 22:26:35 +04:00
|
|
|
/* query parent hub info */
|
|
|
|
dev = udev_device_get_parent(dev);
|
2012-11-21 04:34:52 +04:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
if (dev != NULL)
|
|
|
|
{
|
|
|
|
hub_found = 1;
|
2019-10-18 12:44:24 +03:00
|
|
|
hub_bus = strtoul(udev_device_get_property_value(dev, "BUSNUM"), NULL, 10);
|
|
|
|
hub_dev = strtoul(udev_device_get_property_value(dev, "DEVNUM"), NULL, 10);
|
2014-09-12 18:19:32 +04:00
|
|
|
WLog_DBG(TAG, " Hub BUS/DEV: %d %d", hub_bus, hub_dev);
|
2012-04-25 22:26:35 +04:00
|
|
|
}
|
2012-11-21 04:34:52 +04:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
udev_device_unref(dev);
|
|
|
|
break;
|
|
|
|
}
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
udev_device_unref(dev);
|
|
|
|
}
|
|
|
|
udev_enumerate_unref(enumerate);
|
|
|
|
udev_unref(udev);
|
|
|
|
|
|
|
|
if (!hub_found)
|
|
|
|
{
|
2017-11-14 18:10:52 +03:00
|
|
|
WLog_WARN(TAG, "hub was not found!");
|
2012-04-25 22:26:35 +04:00
|
|
|
return -1;
|
|
|
|
}
|
2012-11-21 04:34:52 +04:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
/* Get libusb hub handle */
|
|
|
|
libusb_dev = udev_get_libusb_dev(hub_bus, hub_dev);
|
2012-11-21 04:34:52 +04:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
if (libusb_dev == NULL)
|
|
|
|
{
|
2017-11-14 18:10:52 +03:00
|
|
|
WLog_DBG(TAG, "get hub libusb_dev fail!");
|
2012-04-25 22:26:35 +04:00
|
|
|
return -1;
|
|
|
|
}
|
2012-11-21 04:34:52 +04:00
|
|
|
|
2017-11-14 18:10:52 +03:00
|
|
|
error = libusb_open(libusb_dev, &pdev->hub_handle);
|
2012-11-21 04:34:52 +04:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
if (error < 0)
|
|
|
|
{
|
2017-11-14 18:10:52 +03:00
|
|
|
WLog_DBG(TAG, "libusb_open error!");
|
2012-04-25 22:26:35 +04:00
|
|
|
return -1;
|
|
|
|
}
|
2012-11-21 04:34:52 +04:00
|
|
|
|
2017-11-14 18:10:52 +03:00
|
|
|
WLog_DBG(TAG, "libusb_open success!");
|
2012-04-25 22:26:35 +04:00
|
|
|
/* Success! */
|
|
|
|
return 0;
|
|
|
|
}
|
2015-03-28 03:08:36 +03:00
|
|
|
#endif
|
2012-04-25 22:26:35 +04:00
|
|
|
|
2012-11-21 04:34:52 +04:00
|
|
|
static int libusb_udev_select_interface(IUDEVICE* idev, BYTE InterfaceNumber, BYTE AlternateSetting)
|
2012-04-25 22:26:35 +04:00
|
|
|
{
|
|
|
|
int error = 0, diff = 1;
|
2012-11-21 04:34:52 +04:00
|
|
|
UDEVICE* pdev = (UDEVICE*) idev;
|
|
|
|
MSUSB_CONFIG_DESCRIPTOR* MsConfig;
|
|
|
|
MSUSB_INTERFACE_DESCRIPTOR** MsInterfaces;
|
2012-04-25 22:26:35 +04:00
|
|
|
MsConfig = pdev->MsConfig;
|
2012-11-21 04:34:52 +04:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
if (MsConfig)
|
|
|
|
{
|
|
|
|
MsInterfaces = MsConfig->MsInterfaces;
|
2012-11-21 04:34:52 +04:00
|
|
|
|
|
|
|
if ((MsInterfaces) && (MsInterfaces[InterfaceNumber]->AlternateSetting == AlternateSetting))
|
2012-04-25 22:26:35 +04:00
|
|
|
{
|
|
|
|
diff = 0;
|
|
|
|
}
|
|
|
|
}
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
if (diff)
|
|
|
|
{
|
2012-11-21 04:34:52 +04:00
|
|
|
error = libusb_set_interface_alt_setting(pdev->libusb_handle,
|
2017-11-14 18:10:52 +03:00
|
|
|
InterfaceNumber, AlternateSetting);
|
2012-11-21 04:34:52 +04:00
|
|
|
|
|
|
|
if (error < 0)
|
|
|
|
{
|
2014-09-12 18:19:32 +04:00
|
|
|
WLog_ERR(TAG, "Set interface altsetting get error num %d",
|
2017-11-14 18:10:52 +03:00
|
|
|
error);
|
|
|
|
}
|
2012-04-25 22:26:35 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2017-11-14 18:10:52 +03:00
|
|
|
static MSUSB_CONFIG_DESCRIPTOR* libusb_udev_complete_msconfig_setup(IUDEVICE* idev,
|
|
|
|
MSUSB_CONFIG_DESCRIPTOR* MsConfig)
|
2012-04-25 22:26:35 +04:00
|
|
|
{
|
2012-11-21 04:34:52 +04:00
|
|
|
UDEVICE* pdev = (UDEVICE*) idev;
|
|
|
|
MSUSB_INTERFACE_DESCRIPTOR** MsInterfaces;
|
|
|
|
MSUSB_INTERFACE_DESCRIPTOR* MsInterface;
|
|
|
|
MSUSB_PIPE_DESCRIPTOR** MsPipes;
|
|
|
|
MSUSB_PIPE_DESCRIPTOR* MsPipe;
|
|
|
|
MSUSB_PIPE_DESCRIPTOR** t_MsPipes;
|
|
|
|
MSUSB_PIPE_DESCRIPTOR* t_MsPipe;
|
|
|
|
LIBUSB_CONFIG_DESCRIPTOR* LibusbConfig;
|
|
|
|
const LIBUSB_INTERFACE* LibusbInterface;
|
|
|
|
const LIBUSB_INTERFACE_DESCRIPTOR* LibusbAltsetting;
|
|
|
|
const LIBUSB_ENDPOINT_DESCEIPTOR* LibusbEndpoint;
|
2012-10-09 11:01:37 +04:00
|
|
|
BYTE LibusbNumEndpoint;
|
2019-02-07 16:32:38 +03:00
|
|
|
UINT32 inum = 0, pnum = 0, MsOutSize = 0;
|
2012-04-25 22:26:35 +04:00
|
|
|
LibusbConfig = pdev->LibusbConfig;
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
if (LibusbConfig->bNumInterfaces != MsConfig->NumInterfaces)
|
|
|
|
{
|
2016-12-14 00:47:08 +03:00
|
|
|
WLog_ERR(TAG, "Select Configuration: Libusb NumberInterfaces(%"PRIu8") is different "
|
2017-11-14 18:10:52 +03:00
|
|
|
"with MsConfig NumberInterfaces(%"PRIu32")",
|
|
|
|
LibusbConfig->bNumInterfaces, MsConfig->NumInterfaces);
|
2012-04-25 22:26:35 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* replace MsPipes for libusb */
|
|
|
|
MsInterfaces = MsConfig->MsInterfaces;
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
for (inum = 0; inum < MsConfig->NumInterfaces; inum++)
|
|
|
|
{
|
|
|
|
MsInterface = MsInterfaces[inum];
|
|
|
|
/* get libusb's number of endpoints */
|
|
|
|
LibusbInterface = &LibusbConfig->interface[MsInterface->InterfaceNumber];
|
|
|
|
LibusbAltsetting = &LibusbInterface->altsetting[MsInterface->AlternateSetting];
|
|
|
|
LibusbNumEndpoint = LibusbAltsetting->bNumEndpoints;
|
2017-05-30 11:46:43 +03:00
|
|
|
t_MsPipes = (MSUSB_PIPE_DESCRIPTOR**) calloc(LibusbNumEndpoint, sizeof(MSUSB_PIPE_DESCRIPTOR*));
|
2012-04-25 22:26:35 +04:00
|
|
|
|
|
|
|
for (pnum = 0; pnum < LibusbNumEndpoint; pnum++)
|
|
|
|
{
|
2017-11-14 18:10:52 +03:00
|
|
|
t_MsPipe = (MSUSB_PIPE_DESCRIPTOR*) malloc(sizeof(MSUSB_PIPE_DESCRIPTOR));
|
2012-04-25 22:26:35 +04:00
|
|
|
memset(t_MsPipe, 0, sizeof(MSUSB_PIPE_DESCRIPTOR));
|
|
|
|
|
|
|
|
if (pnum < MsInterface->NumberOfPipes && MsInterface->MsPipes)
|
|
|
|
{
|
|
|
|
MsPipe = MsInterface->MsPipes[pnum];
|
|
|
|
t_MsPipe->MaximumPacketSize = MsPipe->MaximumPacketSize;
|
|
|
|
t_MsPipe->MaximumTransferSize = MsPipe->MaximumTransferSize;
|
|
|
|
t_MsPipe->PipeFlags = MsPipe->PipeFlags;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-11-21 04:34:52 +04:00
|
|
|
t_MsPipe->MaximumPacketSize = 0;
|
|
|
|
t_MsPipe->MaximumTransferSize = 0xffffffff;
|
|
|
|
t_MsPipe->PipeFlags = 0;
|
2012-04-25 22:26:35 +04:00
|
|
|
}
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2012-11-21 04:34:52 +04:00
|
|
|
t_MsPipe->PipeHandle = 0;
|
|
|
|
t_MsPipe->bEndpointAddress = 0;
|
|
|
|
t_MsPipe->bInterval = 0;
|
|
|
|
t_MsPipe->PipeType = 0;
|
|
|
|
t_MsPipe->InitCompleted = 0;
|
2012-04-25 22:26:35 +04:00
|
|
|
t_MsPipes[pnum] = t_MsPipe;
|
|
|
|
}
|
|
|
|
|
|
|
|
msusb_mspipes_replace(MsInterface, t_MsPipes, LibusbNumEndpoint);
|
|
|
|
}
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
/* setup configuration */
|
|
|
|
MsOutSize = 8;
|
|
|
|
/* ConfigurationHandle: 4 bytes
|
|
|
|
* ---------------------------------------------------------------
|
|
|
|
* ||<<< 1 byte >>>|<<< 1 byte >>>|<<<<<<<<<< 2 byte >>>>>>>>>>>||
|
|
|
|
* || bus_number | dev_number | bConfigurationValue ||
|
|
|
|
* ---------------------------------------------------------------
|
|
|
|
* ***********************/
|
2017-11-14 18:10:52 +03:00
|
|
|
MsConfig->ConfigurationHandle = MsConfig->bConfigurationValue |
|
|
|
|
(pdev->bus_number << 24) |
|
|
|
|
(pdev->dev_number << 16);
|
2012-04-25 22:26:35 +04:00
|
|
|
MsInterfaces = MsConfig->MsInterfaces;
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
for (inum = 0; inum < MsConfig->NumInterfaces; inum++)
|
|
|
|
{
|
|
|
|
MsOutSize += 16;
|
|
|
|
MsInterface = MsInterfaces[inum];
|
|
|
|
/* get libusb's interface */
|
|
|
|
LibusbInterface = &LibusbConfig->interface[MsInterface->InterfaceNumber];
|
2017-11-14 18:10:52 +03:00
|
|
|
LibusbAltsetting = &LibusbInterface->altsetting[MsInterface->AlternateSetting];
|
2012-04-25 22:26:35 +04:00
|
|
|
/* InterfaceHandle: 4 bytes
|
|
|
|
* ---------------------------------------------------------------
|
|
|
|
* ||<<< 1 byte >>>|<<< 1 byte >>>|<<< 1 byte >>>|<<< 1 byte >>>||
|
|
|
|
* || bus_number | dev_number | altsetting | interfaceNum ||
|
|
|
|
* ---------------------------------------------------------------
|
|
|
|
* ***********************/
|
2017-11-14 18:10:52 +03:00
|
|
|
MsInterface->InterfaceHandle = LibusbAltsetting->bInterfaceNumber
|
|
|
|
| (LibusbAltsetting->bAlternateSetting << 8)
|
|
|
|
| (pdev->dev_number << 16)
|
|
|
|
| (pdev->bus_number << 24);
|
2012-04-25 22:26:35 +04:00
|
|
|
MsInterface->Length = 16 + (MsInterface->NumberOfPipes * 20);
|
|
|
|
MsInterface->bInterfaceClass = LibusbAltsetting->bInterfaceClass;
|
|
|
|
MsInterface->bInterfaceSubClass = LibusbAltsetting->bInterfaceSubClass;
|
|
|
|
MsInterface->bInterfaceProtocol = LibusbAltsetting->bInterfaceProtocol;
|
|
|
|
MsInterface->InitCompleted = 1;
|
|
|
|
MsPipes = MsInterface->MsPipes;
|
|
|
|
LibusbNumEndpoint = LibusbAltsetting->bNumEndpoints;
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
for (pnum = 0; pnum < LibusbNumEndpoint; pnum++)
|
|
|
|
{
|
|
|
|
MsOutSize += 20;
|
|
|
|
MsPipe = MsPipes[pnum];
|
|
|
|
/* get libusb's endpoint */
|
|
|
|
LibusbEndpoint = &LibusbAltsetting->endpoint[pnum];
|
|
|
|
/* PipeHandle: 4 bytes
|
|
|
|
* ---------------------------------------------------------------
|
|
|
|
* ||<<< 1 byte >>>|<<< 1 byte >>>|<<<<<<<<<< 2 byte >>>>>>>>>>>||
|
|
|
|
* || bus_number | dev_number | bEndpointAddress ||
|
|
|
|
* ---------------------------------------------------------------
|
|
|
|
* ***********************/
|
2017-11-14 18:10:52 +03:00
|
|
|
MsPipe->PipeHandle = LibusbEndpoint->bEndpointAddress
|
|
|
|
| (pdev->dev_number << 16)
|
|
|
|
| (pdev->bus_number << 24);
|
2012-04-25 22:26:35 +04:00
|
|
|
/* count endpoint max packet size */
|
|
|
|
int max = LibusbEndpoint->wMaxPacketSize & 0x07ff;
|
2012-10-09 11:01:37 +04:00
|
|
|
BYTE attr = LibusbEndpoint->bmAttributes;
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
if ((attr & 0x3) == 1 || (attr & 0x3) == 3)
|
|
|
|
{
|
|
|
|
max *= (1 + ((LibusbEndpoint->wMaxPacketSize >> 11) & 3));
|
|
|
|
}
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
MsPipe->MaximumPacketSize = max;
|
|
|
|
MsPipe->bEndpointAddress = LibusbEndpoint->bEndpointAddress;
|
|
|
|
MsPipe->bInterval = LibusbEndpoint->bInterval;
|
|
|
|
MsPipe->PipeType = attr & 0x3;
|
|
|
|
MsPipe->InitCompleted = 1;
|
|
|
|
}
|
|
|
|
}
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
MsConfig->MsOutSize = MsOutSize;
|
|
|
|
MsConfig->InitCompleted = 1;
|
|
|
|
|
|
|
|
/* replace device's MsConfig */
|
|
|
|
if (!(MsConfig == pdev->MsConfig))
|
|
|
|
{
|
|
|
|
msusb_msconfig_free(pdev->MsConfig);
|
|
|
|
pdev->MsConfig = MsConfig;
|
|
|
|
}
|
|
|
|
|
|
|
|
return MsConfig;
|
|
|
|
}
|
|
|
|
|
2012-11-21 04:34:52 +04:00
|
|
|
static int libusb_udev_select_configuration(IUDEVICE* idev, UINT32 bConfigurationValue)
|
2012-04-25 22:26:35 +04:00
|
|
|
{
|
2012-11-21 04:34:52 +04:00
|
|
|
UDEVICE* pdev = (UDEVICE*) idev;
|
2017-11-14 18:10:52 +03:00
|
|
|
MSUSB_CONFIG_DESCRIPTOR* MsConfig = pdev->MsConfig;
|
|
|
|
LIBUSB_DEVICE_HANDLE* libusb_handle = pdev->libusb_handle;
|
|
|
|
LIBUSB_DEVICE* libusb_dev = pdev->libusb_dev;
|
|
|
|
LIBUSB_CONFIG_DESCRIPTOR** LibusbConfig = &pdev->LibusbConfig;
|
2012-04-25 22:26:35 +04:00
|
|
|
int ret = 0;
|
|
|
|
|
2017-11-14 18:10:52 +03:00
|
|
|
if (MsConfig->InitCompleted)
|
|
|
|
{
|
2012-04-25 22:26:35 +04:00
|
|
|
func_config_release_all_interface(libusb_handle, (*LibusbConfig)->bNumInterfaces);
|
|
|
|
}
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
/* The configuration value -1 is mean to put the device in unconfigured state. */
|
|
|
|
if (bConfigurationValue == 0)
|
2017-11-14 18:10:52 +03:00
|
|
|
ret = libusb_set_configuration(libusb_handle, -1);
|
2012-04-25 22:26:35 +04:00
|
|
|
else
|
2017-11-14 18:10:52 +03:00
|
|
|
ret = libusb_set_configuration(libusb_handle, bConfigurationValue);
|
2012-04-25 22:26:35 +04:00
|
|
|
|
2017-11-14 18:10:52 +03:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2014-09-12 18:19:32 +04:00
|
|
|
WLog_ERR(TAG, "libusb_set_configuration: ERROR number %d!!", ret);
|
2012-04-25 22:26:35 +04:00
|
|
|
func_claim_all_interface(libusb_handle, (*LibusbConfig)->bNumInterfaces);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-11-14 18:10:52 +03:00
|
|
|
ret = libusb_get_active_config_descriptor(libusb_dev, LibusbConfig);
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2014-09-12 18:19:32 +04:00
|
|
|
WLog_ERR(TAG, "libusb_get_config_descriptor_by_value: ERROR number %d!!", ret);
|
2012-04-25 22:26:35 +04:00
|
|
|
func_claim_all_interface(libusb_handle, (*LibusbConfig)->bNumInterfaces);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func_claim_all_interface(libusb_handle, (*LibusbConfig)->bNumInterfaces);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-11-21 04:34:52 +04:00
|
|
|
static int libusb_udev_control_pipe_request(IUDEVICE* idev, UINT32 RequestId,
|
2017-11-14 18:10:52 +03:00
|
|
|
UINT32 EndpointAddress, UINT32* UsbdStatus, int command)
|
2012-04-25 22:26:35 +04:00
|
|
|
{
|
|
|
|
int error = 0;
|
2012-11-21 04:34:52 +04:00
|
|
|
UDEVICE* pdev = (UDEVICE*) idev;
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
/*
|
|
|
|
pdev->request_queue->register_request(pdev->request_queue, RequestId, NULL, 0);
|
|
|
|
*/
|
2017-11-14 18:10:52 +03:00
|
|
|
switch (command)
|
|
|
|
{
|
2012-04-25 22:26:35 +04:00
|
|
|
case PIPE_CANCEL:
|
|
|
|
/** cancel bulk or int transfer */
|
|
|
|
idev->cancel_all_transfer_request(idev);
|
|
|
|
//dummy_wait_s_obj(1);
|
|
|
|
/** set feature to ep (set halt)*/
|
2017-11-14 18:10:52 +03:00
|
|
|
error = libusb_control_transfer(pdev->libusb_handle,
|
|
|
|
LIBUSB_ENDPOINT_OUT | LIBUSB_RECIPIENT_ENDPOINT,
|
|
|
|
LIBUSB_REQUEST_SET_FEATURE,
|
|
|
|
ENDPOINT_HALT,
|
|
|
|
EndpointAddress,
|
|
|
|
NULL,
|
|
|
|
0,
|
|
|
|
1000);
|
2012-04-25 22:26:35 +04:00
|
|
|
break;
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
case PIPE_RESET:
|
|
|
|
idev->cancel_all_transfer_request(idev);
|
|
|
|
error = libusb_clear_halt(pdev->libusb_handle, EndpointAddress);
|
|
|
|
//func_set_usbd_status(pdev, UsbdStatus, error);
|
|
|
|
break;
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
default:
|
|
|
|
error = -0xff;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
*UsbdStatus = 0;
|
|
|
|
/*
|
|
|
|
if(pdev->request_queue->unregister_request(pdev->request_queue, RequestId))
|
2014-09-12 18:19:32 +04:00
|
|
|
WLog_ERR(TAG, "request_queue_unregister_request: not fount request 0x%x", RequestId);
|
2012-04-25 22:26:35 +04:00
|
|
|
*/
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2012-11-21 04:34:52 +04:00
|
|
|
static int libusb_udev_control_query_device_text(IUDEVICE* idev, UINT32 TextType,
|
2017-11-14 18:10:52 +03:00
|
|
|
UINT32 LocaleId,
|
|
|
|
UINT32* BufferSize,
|
|
|
|
BYTE* Buffer)
|
2012-04-25 22:26:35 +04:00
|
|
|
{
|
2012-11-21 04:34:52 +04:00
|
|
|
UDEVICE* pdev = (UDEVICE*) idev;
|
2017-11-14 18:10:52 +03:00
|
|
|
LIBUSB_DEVICE_DESCRIPTOR* devDescriptor = pdev->devDescriptor;
|
|
|
|
char* strDesc = "Generic Usb String";
|
2012-04-25 22:26:35 +04:00
|
|
|
char deviceLocation[25];
|
2012-10-09 11:01:37 +04:00
|
|
|
BYTE bus_number;
|
|
|
|
BYTE device_address;
|
2019-02-07 16:32:38 +03:00
|
|
|
int ret = 0;
|
|
|
|
size_t i = 0;
|
2012-04-25 22:26:35 +04:00
|
|
|
|
2017-11-14 18:10:52 +03:00
|
|
|
switch (TextType)
|
|
|
|
{
|
2012-04-25 22:26:35 +04:00
|
|
|
case DeviceTextDescription:
|
2017-11-14 18:10:52 +03:00
|
|
|
ret = libusb_get_string_descriptor(pdev->libusb_handle,
|
|
|
|
devDescriptor->iProduct,
|
|
|
|
LocaleId,
|
|
|
|
Buffer,
|
|
|
|
*BufferSize);
|
2012-04-25 22:26:35 +04:00
|
|
|
|
2019-02-07 16:32:38 +03:00
|
|
|
for (i = 0; (ret > 0) && (i < (size_t)ret); i++)
|
2012-04-25 22:26:35 +04:00
|
|
|
{
|
2017-11-14 18:10:52 +03:00
|
|
|
Buffer[i] = Buffer[i + 2];
|
2012-04-25 22:26:35 +04:00
|
|
|
}
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
ret -= 2;
|
2017-11-14 18:10:52 +03:00
|
|
|
|
|
|
|
if (ret <= 0 || ret < 4)
|
|
|
|
{
|
|
|
|
WLog_DBG(TAG, "libusb_get_string_descriptor: "
|
|
|
|
"ERROR num %d, iProduct: %"PRIu8"!", ret, devDescriptor->iProduct);
|
2012-04-25 22:26:35 +04:00
|
|
|
memcpy(Buffer, strDesc, strlen(strDesc));
|
|
|
|
Buffer[strlen(strDesc)] = '\0';
|
2017-11-14 18:10:52 +03:00
|
|
|
*BufferSize = (strlen((char*)Buffer)) * 2;
|
|
|
|
|
|
|
|
for (i = strlen((char*)Buffer); i > 0; i--)
|
2012-04-25 22:26:35 +04:00
|
|
|
{
|
2017-11-14 18:10:52 +03:00
|
|
|
Buffer[i * 2] = Buffer[i];
|
|
|
|
Buffer[(i * 2) - 1] = 0;
|
2012-04-25 22:26:35 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*BufferSize = ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
case DeviceTextLocationInformation:
|
|
|
|
bus_number = libusb_get_bus_number(pdev->libusb_dev);
|
|
|
|
device_address = libusb_get_device_address(pdev->libusb_dev);
|
2018-08-24 11:39:48 +03:00
|
|
|
sprintf_s(deviceLocation, ARRAYSIZE(deviceLocation), "Port_#%04"PRIu8".Hub_#%04"PRIu8"",
|
|
|
|
device_address, bus_number);
|
2012-04-25 22:26:35 +04:00
|
|
|
|
2017-11-14 18:10:52 +03:00
|
|
|
for (i = 0; i < strlen(deviceLocation); i++)
|
|
|
|
{
|
|
|
|
Buffer[i * 2] = (BYTE)deviceLocation[i];
|
|
|
|
Buffer[(i * 2) + 1] = 0;
|
2012-04-25 22:26:35 +04:00
|
|
|
}
|
2017-11-14 18:10:52 +03:00
|
|
|
|
|
|
|
*BufferSize = (i * 2);
|
2012-04-25 22:26:35 +04:00
|
|
|
break;
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
default:
|
2017-11-14 18:10:52 +03:00
|
|
|
WLog_DBG(TAG, "Query Text: unknown TextType %"PRIu32"", TextType);
|
|
|
|
break;
|
2012-04-25 22:26:35 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-11-21 04:34:52 +04:00
|
|
|
static int libusb_udev_os_feature_descriptor_request(IUDEVICE* idev, UINT32 RequestId,
|
2017-11-14 18:10:52 +03:00
|
|
|
BYTE Recipient,
|
|
|
|
BYTE InterfaceNumber,
|
|
|
|
BYTE Ms_PageIndex,
|
|
|
|
UINT16 Ms_featureDescIndex,
|
|
|
|
UINT32* UsbdStatus,
|
|
|
|
UINT32* BufferSize,
|
|
|
|
BYTE* Buffer,
|
|
|
|
int Timeout)
|
2012-04-25 22:26:35 +04:00
|
|
|
{
|
2012-11-21 04:34:52 +04:00
|
|
|
UDEVICE* pdev = (UDEVICE*) idev;
|
2012-10-09 11:01:37 +04:00
|
|
|
BYTE ms_string_desc[0x13];
|
2012-04-25 22:26:35 +04:00
|
|
|
int error = 0;
|
|
|
|
/*
|
|
|
|
pdev->request_queue->register_request(pdev->request_queue, RequestId, NULL, 0);
|
|
|
|
*/
|
|
|
|
memset(ms_string_desc, 0, 0x13);
|
2017-11-14 18:10:52 +03:00
|
|
|
error = libusb_control_transfer(pdev->libusb_handle,
|
|
|
|
LIBUSB_ENDPOINT_IN | Recipient,
|
|
|
|
LIBUSB_REQUEST_GET_DESCRIPTOR,
|
|
|
|
0x03ee,
|
|
|
|
0,
|
|
|
|
ms_string_desc,
|
|
|
|
0x12,
|
|
|
|
Timeout);
|
|
|
|
|
2014-09-12 18:19:32 +04:00
|
|
|
//WLog_ERR(TAG, "Get ms string: result number %d", error);
|
2012-04-25 22:26:35 +04:00
|
|
|
if (error > 0)
|
|
|
|
{
|
2012-10-09 11:01:37 +04:00
|
|
|
BYTE bMS_Vendorcode;
|
|
|
|
data_read_BYTE(ms_string_desc + 16, bMS_Vendorcode);
|
2014-09-12 18:19:32 +04:00
|
|
|
//WLog_ERR(TAG, "bMS_Vendorcode:0x%x", bMS_Vendorcode);
|
2012-04-25 22:26:35 +04:00
|
|
|
/** get os descriptor */
|
2017-11-14 18:10:52 +03:00
|
|
|
error = libusb_control_transfer(pdev->libusb_handle,
|
|
|
|
LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | Recipient,
|
|
|
|
bMS_Vendorcode,
|
|
|
|
(InterfaceNumber << 8) | Ms_PageIndex,
|
|
|
|
Ms_featureDescIndex,
|
|
|
|
Buffer,
|
|
|
|
*BufferSize,
|
|
|
|
Timeout);
|
2012-04-25 22:26:35 +04:00
|
|
|
*BufferSize = error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (error < 0)
|
|
|
|
*UsbdStatus = USBD_STATUS_STALL_PID;
|
|
|
|
else
|
|
|
|
*UsbdStatus = USBD_STATUS_SUCCESS;
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
/*
|
|
|
|
if(pdev->request_queue->unregister_request(pdev->request_queue, RequestId))
|
2014-09-12 18:19:32 +04:00
|
|
|
WLog_ERR(TAG, "request_queue_unregister_request: not fount request 0x%x", RequestId);
|
2012-04-25 22:26:35 +04:00
|
|
|
*/
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2012-11-21 04:34:52 +04:00
|
|
|
static int libusb_udev_query_device_descriptor(IUDEVICE* idev, int offset)
|
2012-04-25 22:26:35 +04:00
|
|
|
{
|
2012-11-21 04:34:52 +04:00
|
|
|
UDEVICE* pdev = (UDEVICE*) idev;
|
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
switch (offset)
|
|
|
|
{
|
|
|
|
case B_LENGTH:
|
|
|
|
return pdev->devDescriptor->bLength;
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
case B_DESCRIPTOR_TYPE:
|
|
|
|
return pdev->devDescriptor->bDescriptorType;
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
case BCD_USB:
|
|
|
|
return pdev->devDescriptor->bcdUSB;
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
case B_DEVICE_CLASS:
|
|
|
|
return pdev->devDescriptor->bDeviceClass;
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
case B_DEVICE_SUBCLASS:
|
|
|
|
return pdev->devDescriptor->bDeviceSubClass;
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
case B_DEVICE_PROTOCOL:
|
|
|
|
return pdev->devDescriptor->bDeviceProtocol;
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
case B_MAX_PACKET_SIZE0:
|
|
|
|
return pdev->devDescriptor->bMaxPacketSize0;
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
case ID_VENDOR:
|
|
|
|
return pdev->devDescriptor->idVendor;
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
case ID_PRODUCT:
|
|
|
|
return pdev->devDescriptor->idProduct;
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
case BCD_DEVICE:
|
|
|
|
return pdev->devDescriptor->bcdDevice;
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
case I_MANUFACTURER:
|
|
|
|
return pdev->devDescriptor->iManufacturer;
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
case I_PRODUCT:
|
|
|
|
return pdev->devDescriptor->iProduct;
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
case I_SERIAL_NUMBER:
|
|
|
|
return pdev->devDescriptor->iSerialNumber;
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
case B_NUM_CONFIGURATIONS:
|
|
|
|
return pdev->devDescriptor->bNumConfigurations;
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-11-21 04:34:52 +04:00
|
|
|
static void libusb_udev_detach_kernel_driver(IUDEVICE* idev)
|
2012-04-25 22:26:35 +04:00
|
|
|
{
|
|
|
|
int i, err = 0;
|
2012-11-21 04:34:52 +04:00
|
|
|
UDEVICE* pdev = (UDEVICE*) idev;
|
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
if ((pdev->status & URBDRC_DEVICE_DETACH_KERNEL) == 0)
|
2017-11-14 18:10:52 +03:00
|
|
|
{
|
2012-04-25 22:26:35 +04:00
|
|
|
for (i = 0; i < pdev->LibusbConfig->bNumInterfaces; i++)
|
|
|
|
{
|
2017-11-14 18:10:52 +03:00
|
|
|
err = libusb_kernel_driver_active(pdev->libusb_handle, i);
|
|
|
|
WLog_DBG(TAG, "libusb_kernel_driver_active = %d", err);
|
|
|
|
|
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
err = libusb_detach_kernel_driver(pdev->libusb_handle, i);
|
|
|
|
WLog_DBG(TAG, "libusb_detach_kernel_driver = %d", err);
|
2012-04-25 22:26:35 +04:00
|
|
|
}
|
|
|
|
}
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
pdev->status |= URBDRC_DEVICE_DETACH_KERNEL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-21 04:34:52 +04:00
|
|
|
static void libusb_udev_attach_kernel_driver(IUDEVICE* idev)
|
2012-04-25 22:26:35 +04:00
|
|
|
{
|
|
|
|
int i, err = 0;
|
2012-11-21 04:34:52 +04:00
|
|
|
UDEVICE* pdev = (UDEVICE*) idev;
|
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
for (i = 0; i < pdev->LibusbConfig->bNumInterfaces && err != LIBUSB_ERROR_NO_DEVICE; i++)
|
|
|
|
{
|
2017-11-14 18:10:52 +03:00
|
|
|
err = libusb_release_interface(pdev->libusb_handle, i);
|
|
|
|
|
|
|
|
if (err < 0)
|
|
|
|
{
|
|
|
|
WLog_DBG(TAG, "libusb_release_interface: error num %d = %d", i, err);
|
2012-04-25 22:26:35 +04:00
|
|
|
}
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
if (err != LIBUSB_ERROR_NO_DEVICE)
|
|
|
|
{
|
2017-11-14 18:10:52 +03:00
|
|
|
err = libusb_attach_kernel_driver(pdev->libusb_handle, i);
|
|
|
|
WLog_DBG(TAG, "libusb_attach_kernel_driver if%d = %d", i, err);
|
|
|
|
}
|
2012-04-25 22:26:35 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-21 04:34:52 +04:00
|
|
|
static int libusb_udev_is_composite_device(IUDEVICE* idev)
|
2012-04-25 22:26:35 +04:00
|
|
|
{
|
2012-11-21 04:34:52 +04:00
|
|
|
UDEVICE* pdev = (UDEVICE*) idev;
|
2012-04-25 22:26:35 +04:00
|
|
|
return pdev->isCompositeDevice;
|
|
|
|
}
|
|
|
|
|
2012-11-21 04:34:52 +04:00
|
|
|
static int libusb_udev_is_signal_end(IUDEVICE* idev)
|
2012-04-25 22:26:35 +04:00
|
|
|
{
|
2012-11-21 04:34:52 +04:00
|
|
|
UDEVICE* pdev = (UDEVICE*) idev;
|
2012-04-25 22:26:35 +04:00
|
|
|
return (pdev->status & URBDRC_DEVICE_SIGNAL_END) ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
2012-11-21 04:34:52 +04:00
|
|
|
static int libusb_udev_is_exist(IUDEVICE* idev)
|
2012-04-25 22:26:35 +04:00
|
|
|
{
|
2012-11-21 04:34:52 +04:00
|
|
|
UDEVICE* pdev = (UDEVICE*) idev;
|
2012-04-25 22:26:35 +04:00
|
|
|
return (pdev->status & URBDRC_DEVICE_NOT_FOUND) ? 0 : 1;
|
|
|
|
}
|
|
|
|
|
2012-11-21 04:34:52 +04:00
|
|
|
static int libusb_udev_is_channel_closed(IUDEVICE* idev)
|
2012-04-25 22:26:35 +04:00
|
|
|
{
|
2012-11-21 04:34:52 +04:00
|
|
|
UDEVICE* pdev = (UDEVICE*) idev;
|
2012-04-25 22:26:35 +04:00
|
|
|
return (pdev->status & URBDRC_DEVICE_CHANNEL_CLOSED) ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
2012-11-21 04:34:52 +04:00
|
|
|
static int libusb_udev_is_already_send(IUDEVICE* idev)
|
2012-04-25 22:26:35 +04:00
|
|
|
{
|
2012-11-21 04:34:52 +04:00
|
|
|
UDEVICE* pdev = (UDEVICE*) idev;
|
2012-04-25 22:26:35 +04:00
|
|
|
return (pdev->status & URBDRC_DEVICE_ALREADY_SEND) ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
2012-11-21 04:34:52 +04:00
|
|
|
static void libusb_udev_signal_end(IUDEVICE* idev)
|
2012-04-25 22:26:35 +04:00
|
|
|
{
|
2012-11-21 04:34:52 +04:00
|
|
|
UDEVICE* pdev = (UDEVICE*) idev;
|
2012-04-25 22:26:35 +04:00
|
|
|
pdev->status |= URBDRC_DEVICE_SIGNAL_END;
|
|
|
|
}
|
|
|
|
|
2012-11-21 04:34:52 +04:00
|
|
|
static void libusb_udev_channel_closed(IUDEVICE* idev)
|
2012-04-25 22:26:35 +04:00
|
|
|
{
|
2012-11-21 04:34:52 +04:00
|
|
|
UDEVICE* pdev = (UDEVICE*) idev;
|
2012-04-25 22:26:35 +04:00
|
|
|
pdev->status |= URBDRC_DEVICE_CHANNEL_CLOSED;
|
|
|
|
}
|
|
|
|
|
2012-11-21 04:34:52 +04:00
|
|
|
static void libusb_udev_set_already_send(IUDEVICE* idev)
|
2012-04-25 22:26:35 +04:00
|
|
|
{
|
2012-11-21 04:34:52 +04:00
|
|
|
UDEVICE* pdev = (UDEVICE*) idev;
|
2012-04-25 22:26:35 +04:00
|
|
|
pdev->status |= URBDRC_DEVICE_ALREADY_SEND;
|
|
|
|
}
|
|
|
|
|
2012-11-21 04:34:52 +04:00
|
|
|
static char* libusb_udev_get_path(IUDEVICE* idev)
|
2012-04-25 22:26:35 +04:00
|
|
|
{
|
2012-11-21 04:34:52 +04:00
|
|
|
UDEVICE* pdev = (UDEVICE*) idev;
|
2012-04-25 22:26:35 +04:00
|
|
|
return pdev->path;
|
|
|
|
}
|
|
|
|
|
2012-11-21 04:34:52 +04:00
|
|
|
static int libusb_udev_wait_action_completion(IUDEVICE* idev)
|
2012-04-25 22:26:35 +04:00
|
|
|
{
|
|
|
|
int error, sval;
|
2012-11-21 04:34:52 +04:00
|
|
|
UDEVICE* pdev = (UDEVICE*) idev;
|
|
|
|
|
2017-11-14 18:10:52 +03:00
|
|
|
while (1)
|
2012-04-25 22:26:35 +04:00
|
|
|
{
|
|
|
|
usleep(500000);
|
|
|
|
error = sem_getvalue(&pdev->sem_id, &sval);
|
2012-11-21 04:34:52 +04:00
|
|
|
|
|
|
|
if (sval == 0)
|
2012-04-25 22:26:35 +04:00
|
|
|
break;
|
|
|
|
}
|
2012-11-21 04:34:52 +04:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2012-11-21 04:34:52 +04:00
|
|
|
static void libusb_udev_push_action(IUDEVICE* idev)
|
2012-04-25 22:26:35 +04:00
|
|
|
{
|
2012-11-21 04:34:52 +04:00
|
|
|
UDEVICE* pdev = (UDEVICE*) idev;
|
2012-04-25 22:26:35 +04:00
|
|
|
sem_post(&pdev->sem_id);
|
|
|
|
}
|
|
|
|
|
2012-11-21 04:34:52 +04:00
|
|
|
static void libusb_udev_complete_action(IUDEVICE* idev)
|
2012-04-25 22:26:35 +04:00
|
|
|
{
|
2012-11-21 04:34:52 +04:00
|
|
|
UDEVICE* pdev = (UDEVICE*) idev;
|
2017-11-14 18:10:52 +03:00
|
|
|
sem_trywait(&pdev->sem_id);
|
2012-04-25 22:26:35 +04:00
|
|
|
}
|
|
|
|
|
2012-11-21 04:34:52 +04:00
|
|
|
static int libusb_udev_wait_for_detach(IUDEVICE* idev)
|
2012-04-25 22:26:35 +04:00
|
|
|
{
|
|
|
|
int error = 0;
|
|
|
|
int times = 0;
|
2012-11-21 04:34:52 +04:00
|
|
|
UDEVICE* pdev = (UDEVICE*) idev;
|
2012-04-25 22:26:35 +04:00
|
|
|
|
2012-11-21 04:34:52 +04:00
|
|
|
while (times < 25)
|
2012-04-25 22:26:35 +04:00
|
|
|
{
|
2012-11-21 04:34:52 +04:00
|
|
|
if (pdev->status & URBDRC_DEVICE_SIGNAL_END)
|
2012-04-25 22:26:35 +04:00
|
|
|
{
|
|
|
|
error = -1;
|
|
|
|
break;
|
|
|
|
}
|
2012-11-21 04:34:52 +04:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
usleep(200000);
|
|
|
|
times++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2012-11-21 04:34:52 +04:00
|
|
|
static void libusb_udev_lock_fifo_isoch(IUDEVICE* idev)
|
2012-04-25 22:26:35 +04:00
|
|
|
{
|
2012-11-21 04:34:52 +04:00
|
|
|
UDEVICE* pdev = (UDEVICE*) idev;
|
2017-11-14 18:10:52 +03:00
|
|
|
pthread_mutex_lock(&pdev->mutex_isoch);
|
2012-04-25 22:26:35 +04:00
|
|
|
}
|
|
|
|
|
2012-11-21 04:34:52 +04:00
|
|
|
static void libusb_udev_unlock_fifo_isoch(IUDEVICE* idev)
|
2012-04-25 22:26:35 +04:00
|
|
|
{
|
2012-11-21 04:34:52 +04:00
|
|
|
UDEVICE* pdev = (UDEVICE*) idev;
|
2012-04-25 22:26:35 +04:00
|
|
|
pthread_mutex_unlock(&pdev->mutex_isoch);
|
|
|
|
}
|
|
|
|
|
2017-11-14 18:10:52 +03:00
|
|
|
static int libusb_udev_query_device_port_status(IUDEVICE* idev, UINT32* UsbdStatus,
|
|
|
|
UINT32* BufferSize, BYTE* Buffer)
|
2012-04-25 22:26:35 +04:00
|
|
|
{
|
2012-11-21 04:34:52 +04:00
|
|
|
UDEVICE* pdev = (UDEVICE*) idev;
|
2012-04-25 22:26:35 +04:00
|
|
|
int success = 0, ret;
|
2017-11-14 18:10:52 +03:00
|
|
|
WLog_DBG(TAG, "...");
|
2012-04-25 22:26:35 +04:00
|
|
|
|
|
|
|
if (pdev->hub_handle != NULL)
|
|
|
|
{
|
2017-11-14 18:10:52 +03:00
|
|
|
ret = idev->control_transfer(idev, 0xffff, 0, 0,
|
|
|
|
LIBUSB_ENDPOINT_IN
|
|
|
|
| LIBUSB_REQUEST_TYPE_CLASS
|
|
|
|
| LIBUSB_RECIPIENT_OTHER,
|
|
|
|
LIBUSB_REQUEST_GET_STATUS,
|
|
|
|
0, pdev->port_number, UsbdStatus, BufferSize, Buffer, 1000);
|
2012-04-25 22:26:35 +04:00
|
|
|
|
2012-11-21 12:32:15 +04:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2017-11-14 18:10:52 +03:00
|
|
|
WLog_DBG(TAG, "libusb_control_transfer: error num %d", ret);
|
2012-04-25 22:26:35 +04:00
|
|
|
*BufferSize = 0;
|
|
|
|
}
|
2012-11-21 12:32:15 +04:00
|
|
|
else
|
|
|
|
{
|
2017-11-14 18:10:52 +03:00
|
|
|
WLog_DBG(TAG, "PORT STATUS:0x%02"PRIx8"%02"PRIx8"%02"PRIx8"%02"PRIx8"",
|
|
|
|
Buffer[3], Buffer[2], Buffer[1], Buffer[0]);
|
2012-04-25 22:26:35 +04:00
|
|
|
success = 1;
|
|
|
|
}
|
|
|
|
}
|
2012-11-21 12:32:15 +04:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2012-11-21 04:34:52 +04:00
|
|
|
static int libusb_udev_request_queue_is_none(IUDEVICE* idev)
|
2012-04-25 22:26:35 +04:00
|
|
|
{
|
2012-11-21 04:34:52 +04:00
|
|
|
UDEVICE* pdev = (UDEVICE*) idev;
|
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
if (pdev->request_queue->request_num == 0)
|
|
|
|
return 1;
|
2012-11-21 04:34:52 +04:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-11-21 04:34:52 +04:00
|
|
|
static int libusb_udev_isoch_transfer(IUDEVICE* idev, UINT32 RequestId, UINT32 EndpointAddress,
|
2017-11-14 18:10:52 +03:00
|
|
|
UINT32 TransferFlags, int NoAck, UINT32* ErrorCount,
|
|
|
|
UINT32* UrbdStatus, UINT32* StartFrame, UINT32 NumberOfPackets,
|
|
|
|
BYTE* IsoPacket, UINT32* BufferSize, BYTE* Buffer, int Timeout)
|
2012-04-25 22:26:35 +04:00
|
|
|
{
|
2012-11-21 12:32:15 +04:00
|
|
|
UINT32 iso_packet_size;
|
2012-11-21 04:34:52 +04:00
|
|
|
UDEVICE* pdev = (UDEVICE*) idev;
|
2012-04-25 22:26:35 +04:00
|
|
|
ISO_USER_DATA iso_user_data;
|
2012-11-21 12:32:15 +04:00
|
|
|
struct libusb_transfer* iso_transfer = NULL;
|
|
|
|
int status = 0, ret = 0, submit = 0;
|
2012-04-25 22:26:35 +04:00
|
|
|
iso_packet_size = *BufferSize / NumberOfPackets;
|
|
|
|
iso_transfer = libusb_alloc_transfer(NumberOfPackets);
|
2012-11-21 12:32:15 +04:00
|
|
|
|
|
|
|
if (iso_transfer == NULL)
|
|
|
|
{
|
2014-09-12 18:19:32 +04:00
|
|
|
WLog_ERR(TAG, "Error: libusb_alloc_transfer.");
|
2019-01-28 12:56:23 +03:00
|
|
|
return -1;
|
2012-04-25 22:26:35 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/** process URB_FUNCTION_IOSCH_TRANSFER */
|
2012-11-21 12:32:15 +04:00
|
|
|
func_iso_data_init(&iso_user_data, NumberOfPackets, *BufferSize, NoAck, IsoPacket, Buffer);
|
2012-04-25 22:26:35 +04:00
|
|
|
/** fill setting */
|
2017-11-14 18:10:52 +03:00
|
|
|
libusb_fill_iso_transfer(iso_transfer,
|
|
|
|
pdev->libusb_handle, EndpointAddress, Buffer, *BufferSize,
|
|
|
|
NumberOfPackets, func_iso_callback, &iso_user_data, 2000);
|
2012-04-25 22:26:35 +04:00
|
|
|
libusb_set_iso_packet_lengths(iso_transfer, iso_packet_size);
|
|
|
|
|
2012-11-21 12:32:15 +04:00
|
|
|
if (pdev->status & (URBDRC_DEVICE_SIGNAL_END | URBDRC_DEVICE_NOT_FOUND))
|
|
|
|
status = -1;
|
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
iso_user_data.iso_status = 0;
|
2012-11-21 12:32:15 +04:00
|
|
|
|
|
|
|
if (!(status < 0))
|
2012-04-25 22:26:35 +04:00
|
|
|
{
|
|
|
|
submit = libusb_submit_transfer(iso_transfer);
|
2012-11-21 12:32:15 +04:00
|
|
|
|
|
|
|
if (submit < 0)
|
|
|
|
{
|
2017-11-14 18:10:52 +03:00
|
|
|
WLog_DBG(TAG, "Error: Failed to submit transfer (ret = %d).", submit);
|
2012-11-21 12:32:15 +04:00
|
|
|
status = -1;
|
2017-11-14 18:10:52 +03:00
|
|
|
func_set_usbd_status(pdev, UrbdStatus, ret);
|
2012-04-25 22:26:35 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#if ISOCH_FIFO
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2012-11-21 12:32:15 +04:00
|
|
|
if (!NoAck)
|
|
|
|
{
|
2012-04-25 22:26:35 +04:00
|
|
|
idev->unlock_fifo_isoch(idev);
|
|
|
|
}
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
#endif
|
|
|
|
|
2017-11-14 18:10:52 +03:00
|
|
|
while (pdev && iso_user_data.iso_status == 0 && status >= 0 && submit >= 0)
|
2012-04-25 22:26:35 +04:00
|
|
|
{
|
2017-11-14 18:10:52 +03:00
|
|
|
if (pdev->status & URBDRC_DEVICE_NOT_FOUND)
|
|
|
|
{
|
2012-11-21 12:32:15 +04:00
|
|
|
status = -1;
|
2012-04-25 22:26:35 +04:00
|
|
|
break;
|
|
|
|
}
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
ret = handle_events_completed(NULL, &iso_user_data.completed);
|
2017-11-14 18:10:52 +03:00
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
WLog_DBG(TAG, "Error: libusb_handle_events (ret = %d).", ret);
|
2012-11-21 12:32:15 +04:00
|
|
|
status = -1;
|
2012-04-25 22:26:35 +04:00
|
|
|
break;
|
|
|
|
}
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
#if WAIT_COMPLETE_SLEEP
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
if (iso_user_data.iso_status == 0)
|
|
|
|
{
|
|
|
|
usleep(WAIT_COMPLETE_SLEEP);
|
|
|
|
}
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
#endif
|
2017-11-14 18:10:52 +03:00
|
|
|
}
|
2012-04-25 22:26:35 +04:00
|
|
|
|
|
|
|
if (iso_user_data.iso_status < 0)
|
2012-11-21 12:32:15 +04:00
|
|
|
status = -1;
|
2012-04-25 22:26:35 +04:00
|
|
|
|
|
|
|
*ErrorCount = iso_user_data.error_count;
|
|
|
|
*StartFrame = iso_user_data.start_frame;
|
|
|
|
*BufferSize = iso_transfer->actual_length;
|
|
|
|
libusb_free_transfer(iso_transfer);
|
2012-11-21 12:32:15 +04:00
|
|
|
return status;
|
2012-04-25 22:26:35 +04:00
|
|
|
}
|
|
|
|
|
2012-11-21 04:34:52 +04:00
|
|
|
static int libusb_udev_control_transfer(IUDEVICE* idev, UINT32 RequestId, UINT32 EndpointAddress,
|
2017-11-14 18:10:52 +03:00
|
|
|
UINT32 TransferFlags, BYTE bmRequestType, BYTE Request, UINT16 Value, UINT16 Index,
|
|
|
|
UINT32* UrbdStatus, UINT32* BufferSize, BYTE* Buffer, UINT32 Timeout)
|
2012-04-25 22:26:35 +04:00
|
|
|
{
|
2012-11-21 12:32:15 +04:00
|
|
|
int status = 0;
|
2012-11-21 04:34:52 +04:00
|
|
|
UDEVICE* pdev = (UDEVICE*) idev;
|
2017-11-14 18:10:52 +03:00
|
|
|
status = libusb_control_transfer(pdev->libusb_handle,
|
|
|
|
bmRequestType, Request, Value, Index, Buffer, *BufferSize, Timeout);
|
2012-11-21 04:34:52 +04:00
|
|
|
|
2012-11-21 12:32:15 +04:00
|
|
|
if (!(status < 0))
|
|
|
|
*BufferSize = status;
|
2012-11-21 04:34:52 +04:00
|
|
|
|
2017-11-14 18:10:52 +03:00
|
|
|
func_set_usbd_status(pdev, UrbdStatus, status);
|
2012-11-21 12:32:15 +04:00
|
|
|
return status;
|
2012-04-25 22:26:35 +04:00
|
|
|
}
|
|
|
|
|
2012-11-21 04:34:52 +04:00
|
|
|
static int libusb_udev_bulk_or_interrupt_transfer(IUDEVICE* idev, UINT32 RequestId,
|
2017-11-14 18:10:52 +03:00
|
|
|
UINT32 EndpointAddress, UINT32 TransferFlags, UINT32* UsbdStatus, UINT32* BufferSize, BYTE* Buffer,
|
|
|
|
UINT32 Timeout)
|
2012-04-25 22:26:35 +04:00
|
|
|
{
|
2012-10-09 11:26:39 +04:00
|
|
|
UINT32 transfer_type;
|
2012-11-21 04:34:52 +04:00
|
|
|
UDEVICE* pdev = (UDEVICE*) idev;
|
|
|
|
const LIBUSB_ENDPOINT_DESCEIPTOR* ep_desc;
|
|
|
|
struct libusb_transfer* transfer = NULL;
|
|
|
|
TRANSFER_REQUEST* request = NULL;
|
2012-11-21 12:32:15 +04:00
|
|
|
int completed = 0, status = 0, submit = 0;
|
2012-04-25 22:26:35 +04:00
|
|
|
int transferDir = EndpointAddress & 0x80;
|
2012-11-21 12:32:15 +04:00
|
|
|
/* alloc memory for urb transfer */
|
2017-11-14 18:10:52 +03:00
|
|
|
transfer = libusb_alloc_transfer(0);
|
2012-04-25 22:26:35 +04:00
|
|
|
ep_desc = func_get_ep_desc(pdev->LibusbConfig, pdev->MsConfig, EndpointAddress);
|
|
|
|
|
2012-11-21 04:34:52 +04:00
|
|
|
if (!ep_desc)
|
|
|
|
{
|
2016-12-14 00:47:08 +03:00
|
|
|
WLog_ERR(TAG, "func_get_ep_desc: endpoint 0x%"PRIx32" is not found!!", EndpointAddress);
|
2012-04-25 22:26:35 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-11-14 18:10:52 +03:00
|
|
|
transfer_type = (ep_desc->bmAttributes) & 0x3;
|
|
|
|
WLog_DBG(TAG, "urb_bulk_or_interrupt_transfer: ep:0x%"PRIx32" "
|
|
|
|
"transfer_type %"PRIu32" flag:%"PRIu32" OutputBufferSize:0x%"PRIx32"",
|
|
|
|
EndpointAddress, transfer_type, TransferFlags, *BufferSize);
|
2012-04-25 22:26:35 +04:00
|
|
|
|
2012-11-21 04:34:52 +04:00
|
|
|
switch (transfer_type)
|
|
|
|
{
|
2012-04-25 22:26:35 +04:00
|
|
|
case BULK_TRANSFER:
|
|
|
|
/** Bulk Transfer */
|
|
|
|
//Timeout = 10000;
|
|
|
|
break;
|
2012-11-21 04:34:52 +04:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
case INTERRUPT_TRANSFER:
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
/** Interrupt Transfer */
|
2017-11-14 18:10:52 +03:00
|
|
|
/** Sometime, we may have receive a oversized transfer request,
|
|
|
|
* it make submit urb return error, so we set the length of
|
|
|
|
* request to wMaxPacketSize */
|
2012-04-25 22:26:35 +04:00
|
|
|
if (*BufferSize != (ep_desc->wMaxPacketSize))
|
|
|
|
{
|
2017-11-14 18:10:52 +03:00
|
|
|
WLog_DBG(TAG, "Interrupt Transfer(%s): "
|
|
|
|
"BufferSize is different than maxPacketsize(0x%x)",
|
|
|
|
((transferDir) ? "IN" : "OUT"), ep_desc->wMaxPacketSize);
|
|
|
|
|
|
|
|
if ((*BufferSize) > (ep_desc->wMaxPacketSize) &&
|
|
|
|
transferDir == USBD_TRANSFER_DIRECTION_IN)
|
2012-04-25 22:26:35 +04:00
|
|
|
(*BufferSize) = ep_desc->wMaxPacketSize;
|
|
|
|
}
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
Timeout = 0;
|
|
|
|
break;
|
2012-11-21 04:34:52 +04:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
default:
|
2017-11-14 18:10:52 +03:00
|
|
|
WLog_DBG(TAG, "urb_bulk_or_interrupt_transfer:"
|
2016-12-14 00:47:08 +03:00
|
|
|
" other transfer type 0x%"PRIX32"", transfer_type);
|
2012-04-25 22:26:35 +04:00
|
|
|
return -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-11-21 04:34:52 +04:00
|
|
|
libusb_fill_bulk_transfer(transfer, pdev->libusb_handle, EndpointAddress,
|
2017-11-14 18:10:52 +03:00
|
|
|
Buffer, *BufferSize, func_bulk_transfer_cb, &completed, Timeout);
|
2012-11-21 04:34:52 +04:00
|
|
|
transfer->type = (unsigned char) transfer_type;
|
2012-04-25 22:26:35 +04:00
|
|
|
/** Bug fixed in libusb-1.0-8 later: issue of memory crash */
|
|
|
|
submit = libusb_submit_transfer(transfer);
|
2012-11-21 04:34:52 +04:00
|
|
|
|
|
|
|
if (submit < 0)
|
|
|
|
{
|
2017-11-14 18:10:52 +03:00
|
|
|
WLog_DBG(TAG, "libusb_bulk_transfer: error num %d", status);
|
2012-11-21 12:32:15 +04:00
|
|
|
func_set_usbd_status(pdev, UsbdStatus, status);
|
2012-04-25 22:26:35 +04:00
|
|
|
*BufferSize = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
request = pdev->request_queue->register_request(
|
2017-11-14 18:10:52 +03:00
|
|
|
pdev->request_queue, RequestId, transfer, EndpointAddress);
|
|
|
|
request->submit = 1;
|
2012-04-25 22:26:35 +04:00
|
|
|
}
|
|
|
|
|
2012-11-21 04:34:52 +04:00
|
|
|
if ((pdev && *UsbdStatus == 0) && (submit >= 0) &&
|
2017-11-14 18:10:52 +03:00
|
|
|
(pdev->iface.isSigToEnd((IUDEVICE*) pdev) == 0))
|
2012-04-25 22:26:35 +04:00
|
|
|
{
|
2012-11-21 04:34:52 +04:00
|
|
|
while (!completed)
|
|
|
|
{
|
2012-11-21 12:32:15 +04:00
|
|
|
status = handle_events_completed(NULL, &completed);
|
2012-11-21 04:34:52 +04:00
|
|
|
|
2012-11-21 12:32:15 +04:00
|
|
|
if (status < 0)
|
2012-11-21 04:34:52 +04:00
|
|
|
{
|
2012-11-21 12:32:15 +04:00
|
|
|
if (status == LIBUSB_ERROR_INTERRUPTED)
|
2012-04-25 22:26:35 +04:00
|
|
|
continue;
|
2012-11-21 04:34:52 +04:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
libusb_cancel_transfer(transfer);
|
2012-11-21 04:34:52 +04:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
while (!completed)
|
|
|
|
{
|
|
|
|
if (handle_events_completed(NULL, &completed) < 0)
|
|
|
|
break;
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
#if WAIT_COMPLETE_SLEEP
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
if (!completed)
|
|
|
|
usleep(WAIT_COMPLETE_SLEEP);
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
#endif
|
|
|
|
}
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
break;
|
|
|
|
}
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
#if WAIT_COMPLETE_SLEEP
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
if (!completed)
|
|
|
|
usleep(WAIT_COMPLETE_SLEEP);
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
#endif
|
|
|
|
}
|
2012-11-21 04:34:52 +04:00
|
|
|
|
|
|
|
switch (transfer->status)
|
|
|
|
{
|
|
|
|
case LIBUSB_TRANSFER_COMPLETED:
|
|
|
|
func_set_usbd_status(pdev, UsbdStatus, 0);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LIBUSB_TRANSFER_TIMED_OUT:
|
|
|
|
func_set_usbd_status(pdev, UsbdStatus, LIBUSB_ERROR_TIMEOUT);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LIBUSB_TRANSFER_STALL:
|
|
|
|
func_set_usbd_status(pdev, UsbdStatus, LIBUSB_ERROR_PIPE);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LIBUSB_TRANSFER_OVERFLOW:
|
|
|
|
func_set_usbd_status(pdev, UsbdStatus, LIBUSB_ERROR_OVERFLOW);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LIBUSB_TRANSFER_NO_DEVICE:
|
|
|
|
func_set_usbd_status(pdev, UsbdStatus, LIBUSB_ERROR_NO_DEVICE);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
func_set_usbd_status(pdev, UsbdStatus, LIBUSB_ERROR_OTHER);
|
|
|
|
break;
|
2012-04-25 22:26:35 +04:00
|
|
|
}
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
*BufferSize = transfer->actual_length;
|
|
|
|
}
|
2017-11-14 18:10:52 +03:00
|
|
|
|
|
|
|
WLog_DBG(TAG, "bulk or interrupt Transfer data size : 0x%"PRIx32"", *BufferSize);
|
2012-04-25 22:26:35 +04:00
|
|
|
|
|
|
|
if (request)
|
|
|
|
{
|
2017-11-14 18:10:52 +03:00
|
|
|
if (pdev->request_queue->unregister_request(pdev->request_queue, RequestId))
|
2016-12-14 00:47:08 +03:00
|
|
|
WLog_ERR(TAG, "request_queue_unregister_request: not fount request 0x%"PRIx32"", RequestId);
|
2012-04-25 22:26:35 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
libusb_free_transfer(transfer);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-11-21 04:34:52 +04:00
|
|
|
static void libusb_udev_cancel_all_transfer_request(IUDEVICE* idev)
|
2012-04-25 22:26:35 +04:00
|
|
|
{
|
2012-11-21 12:32:15 +04:00
|
|
|
int status;
|
2012-11-21 04:34:52 +04:00
|
|
|
UDEVICE* pdev = (UDEVICE*) idev;
|
|
|
|
REQUEST_QUEUE* request_queue = pdev->request_queue;
|
|
|
|
TRANSFER_REQUEST* request = NULL;
|
2012-04-25 22:26:35 +04:00
|
|
|
pthread_mutex_lock(&request_queue->request_loading);
|
2012-11-21 04:34:52 +04:00
|
|
|
request_queue->rewind(request_queue);
|
|
|
|
|
|
|
|
while (request_queue->has_next(request_queue))
|
2012-04-25 22:26:35 +04:00
|
|
|
{
|
2012-11-21 04:34:52 +04:00
|
|
|
request = request_queue->get_next(request_queue);
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2012-11-21 04:34:52 +04:00
|
|
|
if ((!request) || (!request->transfer) ||
|
2017-11-14 18:10:52 +03:00
|
|
|
(request->endpoint != request->transfer->endpoint) ||
|
|
|
|
(request->transfer->endpoint == 0) || (request->submit != 1))
|
2012-04-25 22:26:35 +04:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-11-21 12:32:15 +04:00
|
|
|
status = libusb_cancel_transfer(request->transfer);
|
2012-11-21 04:34:52 +04:00
|
|
|
|
2012-11-21 12:32:15 +04:00
|
|
|
if (status < 0)
|
2012-11-21 04:34:52 +04:00
|
|
|
{
|
2017-11-14 18:10:52 +03:00
|
|
|
WLog_DBG(TAG, "libusb_cancel_transfer: error num %d!!", status);
|
2012-04-25 22:26:35 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
request->submit = -1;
|
|
|
|
}
|
|
|
|
}
|
2012-11-21 04:34:52 +04:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
pthread_mutex_unlock(&request_queue->request_loading);
|
|
|
|
}
|
|
|
|
|
2017-11-14 18:10:52 +03:00
|
|
|
static int func_cancel_xact_request(TRANSFER_REQUEST* request)
|
2012-04-25 22:26:35 +04:00
|
|
|
{
|
2012-11-21 12:32:15 +04:00
|
|
|
int status;
|
2012-04-25 22:26:35 +04:00
|
|
|
|
2013-08-29 17:30:22 +04:00
|
|
|
if (!request)
|
|
|
|
return -1;
|
|
|
|
|
2012-11-21 04:34:52 +04:00
|
|
|
if ((!request->transfer) || (request->endpoint != request->transfer->endpoint) ||
|
2017-11-14 18:10:52 +03:00
|
|
|
(request->transfer->endpoint == 0) || (request->submit != 1))
|
2012-04-25 22:26:35 +04:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-11-21 12:32:15 +04:00
|
|
|
status = libusb_cancel_transfer(request->transfer);
|
2012-11-21 04:34:52 +04:00
|
|
|
|
2012-11-21 12:32:15 +04:00
|
|
|
if (status < 0)
|
2012-11-21 04:34:52 +04:00
|
|
|
{
|
2017-11-14 18:10:52 +03:00
|
|
|
WLog_DBG(TAG, "libusb_cancel_transfer: error num %d!!", status);
|
2012-11-21 04:34:52 +04:00
|
|
|
|
2012-11-21 12:32:15 +04:00
|
|
|
if (status == LIBUSB_ERROR_NOT_FOUND)
|
2017-11-14 18:10:52 +03:00
|
|
|
return -1;
|
2012-04-25 22:26:35 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-11-14 18:10:52 +03:00
|
|
|
WLog_DBG(TAG, "libusb_cancel_transfer: Success num:0x%x!!", request->RequestId);
|
2012-04-25 22:26:35 +04:00
|
|
|
request->submit = -1;
|
|
|
|
return 1;
|
|
|
|
}
|
2012-11-21 04:34:52 +04:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-11-21 04:34:52 +04:00
|
|
|
static int libusb_udev_cancel_transfer_request(IUDEVICE* idev, UINT32 RequestId)
|
2012-04-25 22:26:35 +04:00
|
|
|
{
|
2012-11-21 04:34:52 +04:00
|
|
|
UDEVICE* pdev = (UDEVICE*) idev;
|
2012-11-21 12:32:15 +04:00
|
|
|
REQUEST_QUEUE* request_queue = pdev->request_queue;
|
|
|
|
TRANSFER_REQUEST* request = NULL;
|
|
|
|
int status = 0, retry_times = 0;
|
2012-04-25 22:26:35 +04:00
|
|
|
cancel_retry:
|
|
|
|
pthread_mutex_lock(&request_queue->request_loading);
|
2012-11-21 12:32:15 +04:00
|
|
|
request_queue->rewind(request_queue);
|
2012-11-21 04:34:52 +04:00
|
|
|
|
2012-11-21 12:32:15 +04:00
|
|
|
while (request_queue->has_next(request_queue))
|
2012-04-25 22:26:35 +04:00
|
|
|
{
|
2012-11-21 12:32:15 +04:00
|
|
|
request = request_queue->get_next(request_queue);
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2013-08-29 17:30:22 +04:00
|
|
|
if (!request)
|
|
|
|
continue;
|
2012-11-21 04:34:52 +04:00
|
|
|
|
2017-11-14 18:10:52 +03:00
|
|
|
WLog_DBG(TAG, "CancelId:0x%"PRIx32" RequestId:0x%x endpoint 0x%x!!",
|
|
|
|
RequestId, request->RequestId, request->endpoint);
|
2012-11-21 04:34:52 +04:00
|
|
|
|
2017-05-05 22:05:59 +03:00
|
|
|
if (request->RequestId == (RequestId && retry_times <= 10))
|
2012-04-25 22:26:35 +04:00
|
|
|
{
|
2012-11-21 12:32:15 +04:00
|
|
|
status = func_cancel_xact_request(request);
|
2012-04-25 22:26:35 +04:00
|
|
|
break;
|
|
|
|
}
|
2012-11-21 12:32:15 +04:00
|
|
|
else if ((request->transfer) && (retry_times > 10))
|
2012-11-21 04:34:52 +04:00
|
|
|
{
|
2012-11-21 12:32:15 +04:00
|
|
|
status = -1;
|
2012-04-25 22:26:35 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-11-21 04:34:52 +04:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
pthread_mutex_unlock(&request_queue->request_loading);
|
2012-11-21 04:34:52 +04:00
|
|
|
|
2012-11-21 12:32:15 +04:00
|
|
|
if ((status == 0) && (retry_times < 10))
|
2012-11-21 04:34:52 +04:00
|
|
|
{
|
2012-04-25 22:26:35 +04:00
|
|
|
retry_times++;
|
|
|
|
usleep(100000);
|
2017-11-14 18:10:52 +03:00
|
|
|
WLog_DBG(TAG, "urbdrc_process_cancel_request: go retry!!");
|
2012-04-25 22:26:35 +04:00
|
|
|
goto cancel_retry;
|
|
|
|
}
|
2012-11-21 12:32:15 +04:00
|
|
|
else if ((status < 0) || (retry_times >= 10))
|
2012-11-21 04:34:52 +04:00
|
|
|
{
|
2012-04-25 22:26:35 +04:00
|
|
|
/** END */
|
2017-11-14 18:10:52 +03:00
|
|
|
WLog_DBG(TAG, "urbdrc_process_cancel_request: error go exit!!");
|
2012-04-25 22:26:35 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-11-14 18:10:52 +03:00
|
|
|
WLog_DBG(TAG, "urbdrc_process_cancel_request: success!!");
|
2012-04-25 22:26:35 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-10-09 11:26:39 +04:00
|
|
|
BASIC_STATE_FUNC_DEFINED(channel_id, UINT32)
|
|
|
|
BASIC_STATE_FUNC_DEFINED(UsbDevice, UINT32)
|
|
|
|
BASIC_STATE_FUNC_DEFINED(ReqCompletion, UINT32)
|
2012-10-09 11:01:37 +04:00
|
|
|
BASIC_STATE_FUNC_DEFINED(bus_number, UINT16)
|
|
|
|
BASIC_STATE_FUNC_DEFINED(dev_number, UINT16)
|
2012-04-25 22:26:35 +04:00
|
|
|
BASIC_STATE_FUNC_DEFINED(port_number, int)
|
2017-11-14 18:10:52 +03:00
|
|
|
BASIC_STATE_FUNC_DEFINED(isoch_queue, void*)
|
|
|
|
BASIC_STATE_FUNC_DEFINED(MsConfig, MSUSB_CONFIG_DESCRIPTOR*)
|
2012-04-25 22:26:35 +04:00
|
|
|
|
2017-11-14 18:10:52 +03:00
|
|
|
BASIC_POINT_FUNC_DEFINED(udev, void*)
|
|
|
|
BASIC_POINT_FUNC_DEFINED(prev, void*)
|
|
|
|
BASIC_POINT_FUNC_DEFINED(next, void*)
|
2012-04-25 22:26:35 +04:00
|
|
|
|
2012-11-21 04:34:52 +04:00
|
|
|
static void udev_load_interface(UDEVICE* pdev)
|
2012-04-25 22:26:35 +04:00
|
|
|
{
|
|
|
|
/* load interface */
|
|
|
|
/* Basic */
|
|
|
|
BASIC_STATE_FUNC_REGISTER(channel_id, pdev);
|
|
|
|
BASIC_STATE_FUNC_REGISTER(UsbDevice, pdev);
|
|
|
|
BASIC_STATE_FUNC_REGISTER(ReqCompletion, pdev);
|
|
|
|
BASIC_STATE_FUNC_REGISTER(bus_number, pdev);
|
|
|
|
BASIC_STATE_FUNC_REGISTER(dev_number, pdev);
|
|
|
|
BASIC_STATE_FUNC_REGISTER(port_number, pdev);
|
|
|
|
BASIC_STATE_FUNC_REGISTER(isoch_queue, pdev);
|
|
|
|
BASIC_STATE_FUNC_REGISTER(MsConfig, pdev);
|
|
|
|
BASIC_STATE_FUNC_REGISTER(p_udev, pdev);
|
|
|
|
BASIC_STATE_FUNC_REGISTER(p_prev, pdev);
|
|
|
|
BASIC_STATE_FUNC_REGISTER(p_next, pdev);
|
|
|
|
pdev->iface.isCompositeDevice = libusb_udev_is_composite_device;
|
|
|
|
pdev->iface.isSigToEnd = libusb_udev_is_signal_end;
|
|
|
|
pdev->iface.isExist = libusb_udev_is_exist;
|
|
|
|
pdev->iface.isAlreadySend = libusb_udev_is_already_send;
|
|
|
|
pdev->iface.isChannelClosed = libusb_udev_is_channel_closed;
|
|
|
|
pdev->iface.SigToEnd = libusb_udev_signal_end;
|
|
|
|
pdev->iface.setAlreadySend = libusb_udev_set_already_send;
|
|
|
|
pdev->iface.setChannelClosed = libusb_udev_channel_closed;
|
|
|
|
pdev->iface.getPath = libusb_udev_get_path;
|
|
|
|
/* Transfer */
|
|
|
|
pdev->iface.isoch_transfer = libusb_udev_isoch_transfer;
|
|
|
|
pdev->iface.control_transfer = libusb_udev_control_transfer;
|
|
|
|
pdev->iface.bulk_or_interrupt_transfer = libusb_udev_bulk_or_interrupt_transfer;
|
|
|
|
pdev->iface.select_interface = libusb_udev_select_interface;
|
|
|
|
pdev->iface.select_configuration = libusb_udev_select_configuration;
|
|
|
|
pdev->iface.complete_msconfig_setup = libusb_udev_complete_msconfig_setup;
|
|
|
|
pdev->iface.control_pipe_request = libusb_udev_control_pipe_request;
|
|
|
|
pdev->iface.control_query_device_text = libusb_udev_control_query_device_text;
|
|
|
|
pdev->iface.os_feature_descriptor_request = libusb_udev_os_feature_descriptor_request;
|
|
|
|
pdev->iface.cancel_all_transfer_request = libusb_udev_cancel_all_transfer_request;
|
|
|
|
pdev->iface.cancel_transfer_request = libusb_udev_cancel_transfer_request;
|
|
|
|
pdev->iface.query_device_descriptor = libusb_udev_query_device_descriptor;
|
|
|
|
pdev->iface.detach_kernel_driver = libusb_udev_detach_kernel_driver;
|
|
|
|
pdev->iface.attach_kernel_driver = libusb_udev_attach_kernel_driver;
|
|
|
|
pdev->iface.wait_action_completion = libusb_udev_wait_action_completion;
|
|
|
|
pdev->iface.push_action = libusb_udev_push_action;
|
|
|
|
pdev->iface.complete_action = libusb_udev_complete_action;
|
|
|
|
pdev->iface.lock_fifo_isoch = libusb_udev_lock_fifo_isoch;
|
|
|
|
pdev->iface.unlock_fifo_isoch = libusb_udev_unlock_fifo_isoch;
|
|
|
|
pdev->iface.query_device_port_status = libusb_udev_query_device_port_status;
|
|
|
|
pdev->iface.request_queue_is_none = libusb_udev_request_queue_is_none;
|
|
|
|
pdev->iface.wait_for_detach = libusb_udev_wait_for_detach;
|
|
|
|
}
|
|
|
|
|
2012-11-21 04:34:52 +04:00
|
|
|
static IUDEVICE* udev_init(UDEVICE* pdev, UINT16 bus_number, UINT16 dev_number)
|
2012-04-25 22:26:35 +04:00
|
|
|
{
|
2012-11-21 12:32:15 +04:00
|
|
|
int status, num;
|
2012-04-25 22:26:35 +04:00
|
|
|
LIBUSB_DEVICE_DESCRIPTOR* devDescriptor;
|
|
|
|
LIBUSB_CONFIG_DESCRIPTOR* config_temp;
|
|
|
|
LIBUSB_INTERFACE_DESCRIPTOR interface_temp;
|
|
|
|
/* Get HUB handle */
|
2012-11-21 12:32:15 +04:00
|
|
|
status = udev_get_hub_handle(pdev, bus_number, dev_number);
|
2012-11-21 04:34:52 +04:00
|
|
|
|
2012-11-21 12:32:15 +04:00
|
|
|
if (status < 0)
|
2012-04-25 22:26:35 +04:00
|
|
|
{
|
2014-09-12 18:19:32 +04:00
|
|
|
WLog_ERR(TAG, "USB init: Error to get HUB handle!!");
|
2012-04-25 22:26:35 +04:00
|
|
|
pdev->hub_handle = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
pdev->devDescriptor = udev_new_descript(pdev->libusb_dev);
|
2012-11-21 04:34:52 +04:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
if (!pdev->devDescriptor)
|
|
|
|
{
|
2014-09-12 18:19:32 +04:00
|
|
|
WLog_ERR(TAG, "USB init: Error to get device descriptor!!");
|
2012-04-25 22:26:35 +04:00
|
|
|
zfree(pdev);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
num = pdev->devDescriptor->bNumConfigurations;
|
2017-11-14 18:10:52 +03:00
|
|
|
status = libusb_get_active_config_descriptor(pdev->libusb_dev, &pdev->LibusbConfig);
|
2012-11-21 04:34:52 +04:00
|
|
|
|
2012-11-21 12:32:15 +04:00
|
|
|
if (status < 0)
|
2012-11-21 04:34:52 +04:00
|
|
|
{
|
2014-09-12 18:19:32 +04:00
|
|
|
WLog_ERR(TAG, "libusb_get_descriptor: ERROR!!ret:%d", status);
|
2012-04-25 22:26:35 +04:00
|
|
|
zfree(pdev);
|
|
|
|
return NULL;
|
|
|
|
}
|
2012-11-21 04:34:52 +04:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
config_temp = pdev->LibusbConfig;
|
|
|
|
/* get the first interface and first altsetting */
|
|
|
|
interface_temp = config_temp->interface[0].altsetting[0];
|
2017-11-14 18:10:52 +03:00
|
|
|
WLog_DBG(TAG, "Registered Device: Vid: 0x%04"PRIX16" Pid: 0x%04"PRIX16""
|
|
|
|
" InterfaceClass = 0x%02"PRIX8"",
|
|
|
|
pdev->devDescriptor->idVendor,
|
|
|
|
pdev->devDescriptor->idProduct,
|
|
|
|
interface_temp.bInterfaceClass);
|
2012-11-21 04:34:52 +04:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
/* Denied list */
|
2017-11-14 18:10:52 +03:00
|
|
|
switch (interface_temp.bInterfaceClass)
|
2012-04-25 22:26:35 +04:00
|
|
|
{
|
|
|
|
case CLASS_RESERVE:
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
//case CLASS_COMMUNICATION_IF:
|
|
|
|
//case CLASS_HID:
|
|
|
|
//case CLASS_PHYSICAL:
|
|
|
|
case CLASS_MASS_STORAGE:
|
|
|
|
case CLASS_HUB:
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
//case CLASS_COMMUNICATION_DATA_IF:
|
|
|
|
case CLASS_SMART_CARD:
|
|
|
|
case CLASS_CONTENT_SECURITY:
|
2017-11-14 18:10:52 +03:00
|
|
|
//case CLASS_WIRELESS_CONTROLLER:
|
|
|
|
//case CLASS_ELSE_DEVICE:
|
2014-09-12 18:19:32 +04:00
|
|
|
WLog_ERR(TAG, " Device is not supported!!");
|
2012-04-25 22:26:35 +04:00
|
|
|
zfree(pdev);
|
|
|
|
return NULL;
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check composite device */
|
|
|
|
devDescriptor = pdev->devDescriptor;
|
2012-11-21 04:34:52 +04:00
|
|
|
|
|
|
|
if ((devDescriptor->bNumConfigurations == 1) &&
|
2017-11-14 18:10:52 +03:00
|
|
|
(config_temp->bNumInterfaces > 1) &&
|
|
|
|
(devDescriptor->bDeviceClass == 0x0))
|
2012-04-25 22:26:35 +04:00
|
|
|
{
|
|
|
|
pdev->isCompositeDevice = 1;
|
|
|
|
}
|
2012-11-21 04:34:52 +04:00
|
|
|
else if ((devDescriptor->bDeviceClass == 0xef) &&
|
2017-11-14 18:10:52 +03:00
|
|
|
(devDescriptor->bDeviceSubClass == 0x02) &&
|
|
|
|
(devDescriptor->bDeviceProtocol == 0x01))
|
2012-04-25 22:26:35 +04:00
|
|
|
{
|
|
|
|
pdev->isCompositeDevice = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pdev->isCompositeDevice = 0;
|
|
|
|
}
|
|
|
|
|
2017-11-14 18:10:52 +03:00
|
|
|
/* set device class to first interface class */
|
2012-04-25 22:26:35 +04:00
|
|
|
devDescriptor->bDeviceClass = interface_temp.bInterfaceClass;
|
|
|
|
devDescriptor->bDeviceSubClass = interface_temp.bInterfaceSubClass;
|
|
|
|
devDescriptor->bDeviceProtocol = interface_temp.bInterfaceProtocol;
|
|
|
|
/* initialize pdev */
|
|
|
|
pdev->prev = NULL;
|
|
|
|
pdev->next = NULL;
|
|
|
|
pdev->bus_number = bus_number;
|
|
|
|
pdev->dev_number = dev_number;
|
|
|
|
pdev->status = 0;
|
|
|
|
pdev->ReqCompletion = 0;
|
|
|
|
pdev->channel_id = 0xffff;
|
|
|
|
pdev->request_queue = request_queue_new();
|
|
|
|
pdev->isoch_queue = NULL;
|
|
|
|
sem_init(&pdev->sem_id, 0, 0);
|
|
|
|
/* set config of windows */
|
|
|
|
pdev->MsConfig = msusb_msconfig_new();
|
|
|
|
pthread_mutex_init(&pdev->mutex_isoch, NULL);
|
2017-11-14 18:10:52 +03:00
|
|
|
//deb_config_msg(pdev->libusb_dev, config_temp, devDescriptor->bNumConfigurations);
|
2012-04-25 22:26:35 +04:00
|
|
|
udev_load_interface(pdev);
|
2012-11-21 04:34:52 +04:00
|
|
|
return (IUDEVICE*) pdev;
|
2012-04-25 22:26:35 +04:00
|
|
|
}
|
|
|
|
|
2017-11-14 18:10:52 +03:00
|
|
|
int udev_new_by_id(UINT16 idVendor, UINT16 idProduct, IUDEVICE** * devArray)
|
2012-04-25 22:26:35 +04:00
|
|
|
{
|
2012-11-21 04:34:52 +04:00
|
|
|
LIBUSB_DEVICE_DESCRIPTOR* descriptor;
|
|
|
|
LIBUSB_DEVICE** libusb_list;
|
|
|
|
UDEVICE** array;
|
2012-10-09 11:01:37 +04:00
|
|
|
UINT16 bus_number;
|
|
|
|
UINT16 dev_number;
|
2015-03-28 03:08:36 +03:00
|
|
|
ssize_t i, total_device;
|
|
|
|
int status, num = 0;
|
2016-12-14 00:47:08 +03:00
|
|
|
WLog_INFO(TAG, "VID: 0x%04"PRIX16", PID: 0x%04"PRIX16"", idVendor, idProduct);
|
2012-11-21 12:32:15 +04:00
|
|
|
array = (UDEVICE**) malloc(16 * sizeof(UDEVICE*));
|
2017-11-14 18:10:52 +03:00
|
|
|
total_device = libusb_get_device_list(NULL, &libusb_list);
|
2012-11-21 04:34:52 +04:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
for (i = 0; i < total_device; i++)
|
|
|
|
{
|
|
|
|
descriptor = udev_new_descript(libusb_list[i]);
|
2012-11-21 04:34:52 +04:00
|
|
|
|
|
|
|
if ((descriptor->idVendor == idVendor) && (descriptor->idProduct == idProduct))
|
2012-04-25 22:26:35 +04:00
|
|
|
{
|
|
|
|
bus_number = 0;
|
|
|
|
dev_number = 0;
|
2012-11-21 04:34:52 +04:00
|
|
|
array[num] = (PUDEVICE) malloc(sizeof(UDEVICE));
|
2012-04-25 22:26:35 +04:00
|
|
|
array[num]->libusb_dev = libusb_list[i];
|
2012-11-21 12:32:15 +04:00
|
|
|
status = libusb_open(libusb_list[i], &array[num]->libusb_handle);
|
2012-11-21 04:34:52 +04:00
|
|
|
|
2012-11-21 12:32:15 +04:00
|
|
|
if (status < 0)
|
2012-11-21 04:34:52 +04:00
|
|
|
{
|
2015-03-28 03:08:36 +03:00
|
|
|
WLog_ERR(TAG, "libusb_open: (by id) error: 0x%08X (%d)", status, status);
|
2012-04-25 22:26:35 +04:00
|
|
|
zfree(descriptor);
|
|
|
|
zfree(array[num]);
|
|
|
|
continue;
|
|
|
|
}
|
2012-11-21 04:34:52 +04:00
|
|
|
|
|
|
|
bus_number = libusb_get_bus_number(libusb_list[i]);
|
|
|
|
dev_number = libusb_get_device_address(libusb_list[i]);
|
|
|
|
array[num] = (PUDEVICE) udev_init(array[num], bus_number, dev_number);
|
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
if (array[num] != NULL)
|
|
|
|
num++;
|
|
|
|
}
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
zfree(descriptor);
|
|
|
|
}
|
2012-11-21 04:34:52 +04:00
|
|
|
|
2012-04-25 22:26:35 +04:00
|
|
|
libusb_free_device_list(libusb_list, 1);
|
2012-11-21 04:34:52 +04:00
|
|
|
*devArray = (IUDEVICE**) array;
|
2012-04-25 22:26:35 +04:00
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
2012-11-21 04:34:52 +04:00
|
|
|
IUDEVICE* udev_new_by_addr(int bus_number, int dev_number)
|
2012-04-25 22:26:35 +04:00
|
|
|
{
|
2012-11-21 04:34:52 +04:00
|
|
|
int status;
|
|
|
|
UDEVICE* pDev;
|
2017-11-14 18:10:52 +03:00
|
|
|
WLog_DBG(TAG, "bus:%d dev:%d", bus_number, dev_number);
|
2012-11-21 04:34:52 +04:00
|
|
|
pDev = (PUDEVICE) malloc(sizeof(UDEVICE));
|
2012-04-25 22:26:35 +04:00
|
|
|
pDev->libusb_dev = udev_get_libusb_dev(bus_number, dev_number);
|
2012-11-21 04:34:52 +04:00
|
|
|
|
|
|
|
if (pDev->libusb_dev == NULL)
|
|
|
|
{
|
2014-09-12 18:19:32 +04:00
|
|
|
WLog_ERR(TAG, "libusb_device_new: ERROR!!");
|
2012-04-25 22:26:35 +04:00
|
|
|
zfree(pDev);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-11-21 04:34:52 +04:00
|
|
|
status = libusb_open(pDev->libusb_dev, &pDev->libusb_handle);
|
|
|
|
|
|
|
|
if (status < 0)
|
|
|
|
{
|
2014-09-12 18:19:32 +04:00
|
|
|
WLog_ERR(TAG, "libusb_open: (by addr) ERROR!!");
|
2012-04-25 22:26:35 +04:00
|
|
|
zfree(pDev);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return udev_init(pDev, bus_number, dev_number);
|
|
|
|
}
|