1/*
2 * Windows CE backend for libusb 1.0
3 * Copyright © 2011-2013 RealVNC Ltd.
4 * Portions taken from Windows backend, which is
5 * Copyright © 2009-2010 Pete Batard <pbatard@gmail.com>
6 * With contributions from Michael Plante, Orin Eman et al.
7 * Parts of this code adapted from libusb-win32-v1 by Stephan Meyer
8 * Major code testing contribution by Xiaofan Chen
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 */
24#pragma once
25
26#include "windows_common.h"
27
28#include <windows.h>
29#include "poll_windows.h"
30
31#define MAX_DEVICE_COUNT            256
32
33// This is a modified dump of the types in the ceusbkwrapper.h library header
34// with functions transformed into extern pointers.
35//
36// This backend dynamically loads ceusbkwrapper.dll and doesn't include
37// ceusbkwrapper.h directly to simplify the build process. The kernel
38// side wrapper driver is built using the platform image build tools,
39// which makes it difficult to reference directly from the libusb build
40// system.
41struct UKW_DEVICE_PRIV;
42typedef struct UKW_DEVICE_PRIV *UKW_DEVICE;
43typedef UKW_DEVICE *PUKW_DEVICE, *LPUKW_DEVICE;
44
45typedef struct {
46	UINT8 bLength;
47	UINT8 bDescriptorType;
48	UINT16 bcdUSB;
49	UINT8 bDeviceClass;
50	UINT8 bDeviceSubClass;
51	UINT8 bDeviceProtocol;
52	UINT8 bMaxPacketSize0;
53	UINT16 idVendor;
54	UINT16 idProduct;
55	UINT16 bcdDevice;
56	UINT8 iManufacturer;
57	UINT8 iProduct;
58	UINT8 iSerialNumber;
59	UINT8 bNumConfigurations;
60} UKW_DEVICE_DESCRIPTOR, *PUKW_DEVICE_DESCRIPTOR, *LPUKW_DEVICE_DESCRIPTOR;
61
62typedef struct {
63	UINT8 bmRequestType;
64	UINT8 bRequest;
65	UINT16 wValue;
66	UINT16 wIndex;
67	UINT16 wLength;
68} UKW_CONTROL_HEADER, *PUKW_CONTROL_HEADER, *LPUKW_CONTROL_HEADER;
69
70// Collection of flags which can be used when issuing transfer requests
71/* Indicates that the transfer direction is 'in' */
72#define UKW_TF_IN_TRANSFER        0x00000001
73/* Indicates that the transfer direction is 'out' */
74#define UKW_TF_OUT_TRANSFER       0x00000000
75/* Specifies that the transfer should complete as soon as possible,
76 * even if no OVERLAPPED structure has been provided. */
77#define UKW_TF_NO_WAIT            0x00000100
78/* Indicates that transfers shorter than the buffer are ok */
79#define UKW_TF_SHORT_TRANSFER_OK  0x00000200
80#define UKW_TF_SEND_TO_DEVICE     0x00010000
81#define UKW_TF_SEND_TO_INTERFACE  0x00020000
82#define UKW_TF_SEND_TO_ENDPOINT   0x00040000
83/* Don't block when waiting for memory allocations */
84#define UKW_TF_DONT_BLOCK_FOR_MEM 0x00080000
85
86/* Value to use when dealing with configuration values, such as UkwGetConfigDescriptor,
87 * to specify the currently active configuration for the device. */
88#define UKW_ACTIVE_CONFIGURATION -1
89
90DLL_DECLARE_HANDLE(ceusbkwrapper);
91DLL_DECLARE_FUNC(WINAPI, HANDLE, UkwOpenDriver, ());
92DLL_DECLARE_FUNC(WINAPI, BOOL, UkwGetDeviceList, (HANDLE, LPUKW_DEVICE, DWORD, LPDWORD));
93DLL_DECLARE_FUNC(WINAPI, void, UkwReleaseDeviceList, (HANDLE, LPUKW_DEVICE, DWORD));
94DLL_DECLARE_FUNC(WINAPI, BOOL, UkwGetDeviceAddress, (UKW_DEVICE, unsigned char*, unsigned char*, unsigned long*));
95DLL_DECLARE_FUNC(WINAPI, BOOL, UkwGetDeviceDescriptor, (UKW_DEVICE, LPUKW_DEVICE_DESCRIPTOR));
96DLL_DECLARE_FUNC(WINAPI, BOOL, UkwGetConfigDescriptor, (UKW_DEVICE, DWORD, LPVOID, DWORD, LPDWORD));
97DLL_DECLARE_FUNC(WINAPI, void, UkwCloseDriver, (HANDLE));
98DLL_DECLARE_FUNC(WINAPI, BOOL, UkwCancelTransfer, (UKW_DEVICE, LPOVERLAPPED, DWORD));
99DLL_DECLARE_FUNC(WINAPI, BOOL, UkwIssueControlTransfer, (UKW_DEVICE, DWORD, LPUKW_CONTROL_HEADER, LPVOID, DWORD, LPDWORD, LPOVERLAPPED));
100DLL_DECLARE_FUNC(WINAPI, BOOL, UkwClaimInterface, (UKW_DEVICE, DWORD));
101DLL_DECLARE_FUNC(WINAPI, BOOL, UkwReleaseInterface, (UKW_DEVICE, DWORD));
102DLL_DECLARE_FUNC(WINAPI, BOOL, UkwSetInterfaceAlternateSetting, (UKW_DEVICE, DWORD, DWORD));
103DLL_DECLARE_FUNC(WINAPI, BOOL, UkwClearHaltHost, (UKW_DEVICE, UCHAR));
104DLL_DECLARE_FUNC(WINAPI, BOOL, UkwClearHaltDevice, (UKW_DEVICE, UCHAR));
105DLL_DECLARE_FUNC(WINAPI, BOOL, UkwGetConfig, (UKW_DEVICE, PUCHAR));
106DLL_DECLARE_FUNC(WINAPI, BOOL, UkwSetConfig, (UKW_DEVICE, UCHAR));
107DLL_DECLARE_FUNC(WINAPI, BOOL, UkwResetDevice, (UKW_DEVICE));
108DLL_DECLARE_FUNC(WINAPI, BOOL, UkwKernelDriverActive, (UKW_DEVICE, DWORD, PBOOL));
109DLL_DECLARE_FUNC(WINAPI, BOOL, UkwAttachKernelDriver, (UKW_DEVICE, DWORD));
110DLL_DECLARE_FUNC(WINAPI, BOOL, UkwDetachKernelDriver, (UKW_DEVICE, DWORD));
111DLL_DECLARE_FUNC(WINAPI, BOOL, UkwIssueBulkTransfer, (UKW_DEVICE, DWORD, UCHAR, LPVOID, DWORD, LPDWORD, LPOVERLAPPED));
112DLL_DECLARE_FUNC(WINAPI, BOOL, UkwIsPipeHalted, (UKW_DEVICE, UCHAR, LPBOOL));
113
114// Used to determine if an endpoint status really is halted on a failed transfer.
115#define STATUS_HALT_FLAG 0x1
116
117struct wince_device_priv {
118	UKW_DEVICE dev;
119	UKW_DEVICE_DESCRIPTOR desc;
120};
121
122struct wince_transfer_priv {
123	struct winfd pollable_fd;
124	uint8_t interface_number;
125};
126
127