summaryrefslogtreecommitdiff
path: root/usb/driver/libusb_driver.h
blob: e0a5d3c461bda5735c12d49b3fa368e2000b1acb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/* libusb-win32, Generic Windows USB Library
 * Copyright (c) 2002-2005 Stephan Meyer <ste_meyer@web.de>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */


#ifndef __LIBUSB_DRIVER_H__
#define __LIBUSB_DRIVER_H__

//#define SKIP_CONFIGURE_NORMAL_DEVICES
//#define SKIP_DEVICES_WINUSB
//#define SKIP_DEVICES_PICOPP

#ifdef __GNUC__
#include <ddk/usb100.h>
#include <ddk/usbdi.h>
#include <ddk/winddk.h>
#include "usbdlib_gcc.h"
#else
#include <ntifs.h>
#include <wdm.h>
#include "usbdi.h"
#include "usbdlib.h"
#endif

#include <wchar.h>
#include <initguid.h>

#undef interface

#include "driver_debug.h"
#include "error.h"
#include "driver_api.h"

/* some missing defines */
#ifdef __GNUC__

#define USBD_TRANSFER_DIRECTION_OUT       0
#define USBD_TRANSFER_DIRECTION_BIT       0
#define USBD_TRANSFER_DIRECTION_IN        (1 << USBD_TRANSFER_DIRECTION_BIT)
#define USBD_SHORT_TRANSFER_OK_BIT        1
#define USBD_SHORT_TRANSFER_OK            (1 << USBD_SHORT_TRANSFER_OK_BIT)
#define USBD_START_ISO_TRANSFER_ASAP_BIT  2
#define USBD_START_ISO_TRANSFER_ASAP   (1 << USBD_START_ISO_TRANSFER_ASAP_BIT)

#endif

#define SET_CONFIG_ACTIVE_CONFIG -258

#define USB_RECIP_DEVICE    0x00
#define USB_RECIP_INTERFACE 0x01
#define USB_RECIP_ENDPOINT  0x02
#define USB_RECIP_OTHER     0x03

#define USB_TYPE_STANDARD   0x00
#define USB_TYPE_CLASS      0x01
#define USB_TYPE_VENDOR	    0x02


#define LIBUSB_NT_DEVICE_NAME L"\\Device\\libusb0"
#define LIBUSB_SYMBOLIC_LINK_NAME L"\\DosDevices\\libusb0-"

#define LIBUSB_MAX_NUMBER_OF_ENDPOINTS  32
#define LIBUSB_MAX_NUMBER_OF_INTERFACES 32


#define LIBUSB_DEFAULT_TIMEOUT 5000
#define LIBUSB_MAX_CONTROL_TRANSFER_TIMEOUT 5000


#ifndef __GNUC__
#define DDKAPI
#endif

#ifndef FALSE
#define FALSE 0
#endif

#ifndef TRUE
#define TRUE (!(FALSE))
#endif

typedef int bool_t;

#define POOL_TAG (ULONG) '0BSU'
#undef ExAllocatePool
#define ExAllocatePool(type, size) ExAllocatePoolWithTag(type, size, POOL_TAG)

#define IS_PIPE_TYPE(pipeInfo, pipeType) ((((pipeInfo->pipe_type & 3)==pipeType))?TRUE:FALSE)

#define IS_CTRL_PIPE(pipeInfo) IS_PIPE_TYPE(pipeInfo,UsbdPipeTypeControl)
#define IS_ISOC_PIPE(pipeInfo) IS_PIPE_TYPE(pipeInfo,UsbdPipeTypeIsochronous)
#define IS_BULK_PIPE(pipeInfo) IS_PIPE_TYPE(pipeInfo,UsbdPipeTypeBulk)
#define IS_INTR_PIPE(pipeInfo) IS_PIPE_TYPE(pipeInfo,UsbdPipeTypeInterrupt)

#define GetMaxTransferSize(pipeInfo, reqMaxTransferSize) ((reqMaxTransferSize) ? reqMaxTransferSize : pipeInfo->maximum_transfer_size)

#define UrbFunctionFromEndpoint(PipeInfo) ((IS_ISOC_PIPE(PipeInfo)) ? URB_FUNCTION_ISOCH_TRANSFER : URB_FUNCTION_BULK_OR_INTERRUPT_TRANSFER)
#define UsbdDirectionFromEndpoint(PipeInfo) ((PipeInfo->address & 0x80) ? USBD_TRANSFER_DIRECTION_IN : USBD_TRANSFER_DIRECTION_OUT)

#define UpdateContextConfigDescriptor(DeviceContext, Descriptor, Size, Value, Index)		\
{																							\
	if (DeviceContext->config.descriptor && DeviceContext->config.descriptor!=(Descriptor))	\
		ExFreePool(DeviceContext->config.descriptor);										\
	DeviceContext->config.descriptor=(Descriptor);											\
	DeviceContext->config.total_size=(Size);												\
	DeviceContext->config.value=(Value);													\
	DeviceContext->config.index=(Index);													\
}

#ifndef __WUSBIO_H__

// Pipe policy types

// The default value is zero. To set a time-out value, in Value pass the address of a caller-allocated ULONG variable that contains the time-out interval.
// The PIPE_TRANSFER_TIMEOUT value specifies the time-out interval, in milliseconds. The host controller cancels transfers that do not complete within the specified time-out interval.
// A value of zero (default) indicates that transfers do not time out because the host controller never cancels the transfer.
#define PIPE_TRANSFER_TIMEOUT   0x03

// Device Information types
#define DEVICE_SPEED            0x01

// Device Speeds
#define LowSpeed                0x01
#define FullSpeed               0x02
#define HighSpeed               0x03

#endif

#define USB_ENDPOINT_ADDRESS_MASK 0x0F
#define USB_ENDPOINT_DIR_MASK 0x80
#define LBYTE(w) (w & 0xFF)
#define HBYTE(w) ((w>>8) & 0xFF)


#include <pshpack1.h>

typedef struct
{
    unsigned char length;
    unsigned char type;
} usb_descriptor_header_t;

#include <poppack.h>


typedef struct
{
    long usage_count;
    int remove_pending;
    KEVENT event;
} libusb_remove_lock_t;

typedef struct
{
    int address;
    USBD_PIPE_HANDLE handle;
	int maximum_packet_size;  // Maximum packet size for this pipe
    int interval;            // Polling interval in ms if interrupt pipe
    USBD_PIPE_TYPE pipe_type;   // PipeType identifies type of transfer valid for this pipe
    
	//
    // INPUT
    // These fields are filled in by the client driver
    //
    int maximum_transfer_size; // Maximum size for a single request
                               // in bytes.
    int pipe_flags;
} libusb_endpoint_t;

typedef struct
{
    bool_t valid;
    FILE_OBJECT *file_object; /* file object this interface is bound to */
    libusb_endpoint_t endpoints[LIBUSB_MAX_NUMBER_OF_ENDPOINTS];

} libusb_interface_t;

typedef struct
{
    DEVICE_OBJECT	*self;
    DEVICE_OBJECT	*physical_device_object;
    DEVICE_OBJECT	*next_stack_device;
    DEVICE_OBJECT	*target_device;
    libusb_remove_lock_t remove_lock;
    bool_t is_filter;
    bool_t is_started;
    bool_t surprise_removal_ok;
    int id;
	USB_DEVICE_DESCRIPTOR device_descriptor; 
    struct
    {
        USBD_CONFIGURATION_HANDLE handle;
        int value;
		int index;
        libusb_interface_t interfaces[LIBUSB_MAX_NUMBER_OF_INTERFACES];
		PUSB_CONFIGURATION_DESCRIPTOR descriptor; 
		int total_size;
    } config;
    POWER_STATE power_state;
    DEVICE_POWER_STATE device_power_states[PowerSystemMaximum];
	int initial_config_value;
	char device_id[256];
	bool_t disallow_power_control;
	char objname_plugplay_registry_key[512];
	GUID device_interface_guid;
	bool_t device_interface_in_use;
	UNICODE_STRING device_interface_name;
	int control_read_timeout;
	int control_write_timeout;
} libusb_device_t, DEVICE_EXTENSION, *PDEVICE_EXTENSION;


NTSTATUS DDKAPI add_device(DRIVER_OBJECT *driver_object,
                           DEVICE_OBJECT *physical_device_object);

NTSTATUS DDKAPI dispatch(DEVICE_OBJECT *device_object, IRP *irp);
NTSTATUS dispatch_pnp(libusb_device_t *dev, IRP *irp);
NTSTATUS dispatch_power(libusb_device_t *dev, IRP *irp);
NTSTATUS dispatch_ioctl(libusb_device_t *dev, IRP *irp);

NTSTATUS complete_irp(IRP *irp, NTSTATUS status, ULONG info);

#define call_usbd(dev, urb, control_code, timeout) \
	call_usbd_ex(dev, urb, control_code, timeout, LIBUSB_MAX_CONTROL_TRANSFER_TIMEOUT)

NTSTATUS call_usbd_ex(libusb_device_t *dev, 
					  void *urb, 
					  ULONG control_code, 
					  int timeout,
					  int max_timeout);

NTSTATUS pass_irp_down(libusb_device_t *dev, IRP *irp,
                       PIO_COMPLETION_ROUTINE completion_routine,
                       void *context);

bool_t accept_irp(libusb_device_t *dev, IRP *irp);

bool_t get_pipe_handle(libusb_device_t *dev, int endpoint_address,
                       USBD_PIPE_HANDLE *pipe_handle);

bool_t get_pipe_info(libusb_device_t *dev, int endpoint_address,
                       libusb_endpoint_t** pipe_info);

void clear_pipe_info(libusb_device_t *dev);
bool_t update_pipe_info(libusb_device_t *dev,
                        USBD_INTERFACE_INFORMATION *interface_info);

void remove_lock_initialize(libusb_device_t *dev);
NTSTATUS remove_lock_acquire(libusb_device_t *dev);
void remove_lock_release(libusb_device_t *dev);
void remove_lock_release_and_wait(libusb_device_t *dev);

NTSTATUS set_configuration(libusb_device_t *dev,
                           int configuration, int timeout);
NTSTATUS auto_configure(libusb_device_t *dev);

NTSTATUS get_configuration(libusb_device_t *dev,
                           unsigned char *configuration, int *ret,
                           int timeout);

NTSTATUS set_feature(libusb_device_t *dev,
                     int recipient, int index, int feature, int timeout);
NTSTATUS clear_feature(libusb_device_t *dev,
                       int recipient, int index, int feature, int timeout);
NTSTATUS get_status(libusb_device_t *dev, int recipient,
                    int index, char *status, int *ret, int timeout);
NTSTATUS set_descriptor(libusb_device_t *dev,
                        void *buffer, int size,
                        int type, int recipient, int index, int language_id,
                        int *sent, int timeout);
NTSTATUS get_descriptor(libusb_device_t *dev, void *buffer, int size,
                        int type, int recipient, int index, int language_id,
                        int *received, int timeout);

PUSB_CONFIGURATION_DESCRIPTOR get_config_descriptor(
	libusb_device_t *dev,
	int value,
	int *size,
	int* index);

NTSTATUS vendor_class_request(libusb_device_t *dev,
                              int type, int recipient,
                              int request, int value, int index,
                              void *buffer, int size, int direction,
                              int *ret, int timeout);

NTSTATUS abort_endpoint(libusb_device_t *dev, int endpoint, int timeout);
NTSTATUS reset_endpoint(libusb_device_t *dev, int endpoint, int timeout);
NTSTATUS reset_device(libusb_device_t *dev, int timeout);

#define USB_RESET_TYPE_RESET_PORT (1 << 0)
#define USB_RESET_TYPE_CYCLE_PORT (1 << 1)
#define USB_RESET_TYPE_FULL_RESET (USB_RESET_TYPE_CYCLE_PORT | USB_RESET_TYPE_RESET_PORT)
NTSTATUS reset_device_ex(libusb_device_t *dev, int timeout, unsigned int reset_type);

bool_t reg_get_hardware_id(DEVICE_OBJECT *physical_device_object,
                           char *data, int size);
bool_t reg_get_compatible_id(DEVICE_OBJECT *physical_device_object,
                           char *data, int size);

bool_t reg_get_properties(libusb_device_t *dev);


void power_set_device_state(libusb_device_t *dev,
                            DEVICE_POWER_STATE device_state, bool_t block);

USB_INTERFACE_DESCRIPTOR *
find_interface_desc(USB_CONFIGURATION_DESCRIPTOR *config_desc,
                    unsigned int size, int interface_number, int altsetting);

#define FIND_INTERFACE_INDEX_ANY		(-1)
USB_INTERFACE_DESCRIPTOR* find_interface_desc_ex(USB_CONFIGURATION_DESCRIPTOR *config_desc,
												 unsigned int size,
												 interface_request_t* intf,
												 unsigned int* size_left);

USB_ENDPOINT_DESCRIPTOR *
find_endpoint_desc_by_index(USB_INTERFACE_DESCRIPTOR *interface_desc,
                    unsigned int size, int pipe_index);

/*
Gets a device property for the device_object.

Returns: NTSTATUS code from IoGetDeviceProperty 
         STATUS_INVALID_PARAMETER
*/
NTSTATUS reg_get_device_property(PDEVICE_OBJECT device_object,
							   int property, 
							   char* data_buffer,
							   int data_length,
							   int* actual_length);

NTSTATUS reg_get_custom_property(PDEVICE_OBJECT device_object,
								 char *data_buffer, 
								 unsigned int data_length, 
								 unsigned int name_offset, 
								 int* actual_length);


NTSTATUS transfer(libusb_device_t* dev,
				  IN PIRP irp,
				  IN int direction,
				  IN int urbFunction,
				  IN libusb_endpoint_t* endpoint,
				  IN int packetSize,
				  IN int transferFlags,
				  IN int isoLatency,
				  IN PMDL mdlAddress,
				  IN int totalLength);

NTSTATUS large_transfer(IN libusb_device_t* dev,
						IN PIRP irp,
						IN int direction,
						IN int urbFunction,
						IN libusb_endpoint_t* endpoint,
						IN int packetSize,
						IN int maxTransferSize,
						IN int transferFlags,
						IN int isoLatency,
						IN PMDL mdlAddress,
						IN int totalLength);

ULONG get_current_frame(IN PDEVICE_EXTENSION dev, IN PIRP Irp);


NTSTATUS control_transfer(libusb_device_t* dev, 
						 PIRP irp,
						 PMDL mdl,
						 int size,
						 int usbd_direction,
						 int *ret,
						 int timeout,
						 UCHAR request_type,
						 UCHAR request,
						 USHORT value,
						 USHORT index,
						 USHORT length);

NTSTATUS claim_interface(libusb_device_t *dev, FILE_OBJECT *file_object,
                         int interface);

NTSTATUS claim_interface_ex(libusb_device_t *dev, 
							  FILE_OBJECT *file_object, 
							  interface_request_t* interface_request);

NTSTATUS release_all_interfaces(libusb_device_t *dev,
                                FILE_OBJECT *file_object);

NTSTATUS release_interface(libusb_device_t *dev, FILE_OBJECT *file_object,
                           int interface);

NTSTATUS release_interface_ex(libusb_device_t *dev, 
							  FILE_OBJECT *file_object, 
							  interface_request_t* interface_request);

NTSTATUS set_interface(libusb_device_t *dev,
					   int interface_number, 
					   int alt_interface_number,
                       int timeout);

NTSTATUS set_interface_ex(libusb_device_t *dev, 
					   interface_request_t* interface_request, 
                       int timeout);

NTSTATUS get_interface(libusb_device_t *dev,
					   int interface_number, 
					   unsigned char *altsetting,
					   int timeout);

NTSTATUS get_interface_ex(libusb_device_t *dev, 
					   interface_request_t* interface_request, 
                       int timeout);

VOID set_filter_interface_key(libusb_device_t *dev, ULONG id);
#endif