1
/* libusb-win32, Generic Windows USB Library
2
* Copyright (c) 2002-2005 Stephan Meyer <ste_meyer@web.de>
4
* This program is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License as published by
6
* the Free Software Foundation; either version 2 of the License, or
7
* (at your option) any later version.
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software
16
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
#ifndef __LIBUSB_DRIVER_H__
21
#define __LIBUSB_DRIVER_H__
23
//#define SKIP_CONFIGURE_NORMAL_DEVICES
24
//#define SKIP_DEVICES_WINUSB
25
//#define SKIP_DEVICES_PICOPP
28
#include <ddk/usb100.h>
29
#include <ddk/usbdi.h>
30
#include <ddk/winddk.h>
31
#include "usbdlib_gcc.h"
44
#include "driver_debug.h"
46
#include "driver_api.h"
48
/* some missing defines */
51
#define USBD_TRANSFER_DIRECTION_OUT 0
52
#define USBD_TRANSFER_DIRECTION_BIT 0
53
#define USBD_TRANSFER_DIRECTION_IN (1 << USBD_TRANSFER_DIRECTION_BIT)
54
#define USBD_SHORT_TRANSFER_OK_BIT 1
55
#define USBD_SHORT_TRANSFER_OK (1 << USBD_SHORT_TRANSFER_OK_BIT)
56
#define USBD_START_ISO_TRANSFER_ASAP_BIT 2
57
#define USBD_START_ISO_TRANSFER_ASAP (1 << USBD_START_ISO_TRANSFER_ASAP_BIT)
61
#define SET_CONFIG_ACTIVE_CONFIG -258
63
#define USB_RECIP_DEVICE 0x00
64
#define USB_RECIP_INTERFACE 0x01
65
#define USB_RECIP_ENDPOINT 0x02
66
#define USB_RECIP_OTHER 0x03
68
#define USB_TYPE_STANDARD 0x00
69
#define USB_TYPE_CLASS 0x01
70
#define USB_TYPE_VENDOR 0x02
73
#define LIBUSB_NT_DEVICE_NAME L"\\Device\\libusb0"
74
#define LIBUSB_SYMBOLIC_LINK_NAME L"\\DosDevices\\libusb0-"
76
#define LIBUSB_MAX_NUMBER_OF_ENDPOINTS 32
77
#define LIBUSB_MAX_NUMBER_OF_INTERFACES 32
80
#define LIBUSB_DEFAULT_TIMEOUT 5000
81
#define LIBUSB_MAX_CONTROL_TRANSFER_TIMEOUT 5000
93
#define TRUE (!(FALSE))
98
#define POOL_TAG (ULONG) '0BSU'
100
#define ExAllocatePool(type, size) ExAllocatePoolWithTag(type, size, POOL_TAG)
102
#define IS_PIPE_TYPE(pipeInfo, pipeType) ((((pipeInfo->pipe_type & 3)==pipeType))?TRUE:FALSE)
104
#define IS_CTRL_PIPE(pipeInfo) IS_PIPE_TYPE(pipeInfo,UsbdPipeTypeControl)
105
#define IS_ISOC_PIPE(pipeInfo) IS_PIPE_TYPE(pipeInfo,UsbdPipeTypeIsochronous)
106
#define IS_BULK_PIPE(pipeInfo) IS_PIPE_TYPE(pipeInfo,UsbdPipeTypeBulk)
107
#define IS_INTR_PIPE(pipeInfo) IS_PIPE_TYPE(pipeInfo,UsbdPipeTypeInterrupt)
109
#define GetMaxTransferSize(pipeInfo, reqMaxTransferSize) ((reqMaxTransferSize) ? reqMaxTransferSize : pipeInfo->maximum_transfer_size)
111
#define UrbFunctionFromEndpoint(PipeInfo) ((IS_ISOC_PIPE(PipeInfo)) ? URB_FUNCTION_ISOCH_TRANSFER : URB_FUNCTION_BULK_OR_INTERRUPT_TRANSFER)
112
#define UsbdDirectionFromEndpoint(PipeInfo) ((PipeInfo->address & 0x80) ? USBD_TRANSFER_DIRECTION_IN : USBD_TRANSFER_DIRECTION_OUT)
114
#define UpdateContextConfigDescriptor(DeviceContext, Descriptor, Size, Value, Index) \
116
if (DeviceContext->config.descriptor && DeviceContext->config.descriptor!=(Descriptor)) \
117
ExFreePool(DeviceContext->config.descriptor); \
118
DeviceContext->config.descriptor=(Descriptor); \
119
DeviceContext->config.total_size=(Size); \
120
DeviceContext->config.value=(Value); \
121
DeviceContext->config.index=(Index); \
128
// 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.
129
// 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.
130
// A value of zero (default) indicates that transfers do not time out because the host controller never cancels the transfer.
131
#define PIPE_TRANSFER_TIMEOUT 0x03
133
// Device Information types
134
#define DEVICE_SPEED 0x01
137
#define LowSpeed 0x01
138
#define FullSpeed 0x02
139
#define HighSpeed 0x03
143
#define USB_ENDPOINT_ADDRESS_MASK 0x0F
144
#define USB_ENDPOINT_DIR_MASK 0x80
145
#define LBYTE(w) (w & 0xFF)
146
#define HBYTE(w) ((w>>8) & 0xFF)
149
#include <pshpack1.h>
153
unsigned char length;
155
} usb_descriptor_header_t;
165
} libusb_remove_lock_t;
170
USBD_PIPE_HANDLE handle;
171
int maximum_packet_size; // Maximum packet size for this pipe
172
int interval; // Polling interval in ms if interrupt pipe
173
USBD_PIPE_TYPE pipe_type; // PipeType identifies type of transfer valid for this pipe
177
// These fields are filled in by the client driver
179
int maximum_transfer_size; // Maximum size for a single request
187
FILE_OBJECT *file_object; /* file object this interface is bound to */
188
libusb_endpoint_t endpoints[LIBUSB_MAX_NUMBER_OF_ENDPOINTS];
190
} libusb_interface_t;
195
DEVICE_OBJECT *physical_device_object;
196
DEVICE_OBJECT *next_stack_device;
197
DEVICE_OBJECT *target_device;
198
libusb_remove_lock_t remove_lock;
201
bool_t surprise_removal_ok;
203
USB_DEVICE_DESCRIPTOR device_descriptor;
206
USBD_CONFIGURATION_HANDLE handle;
209
libusb_interface_t interfaces[LIBUSB_MAX_NUMBER_OF_INTERFACES];
210
PUSB_CONFIGURATION_DESCRIPTOR descriptor;
213
POWER_STATE power_state;
214
DEVICE_POWER_STATE device_power_states[PowerSystemMaximum];
215
int initial_config_value;
217
bool_t disallow_power_control;
218
char objname_plugplay_registry_key[512];
219
GUID device_interface_guid;
220
bool_t device_interface_in_use;
221
UNICODE_STRING device_interface_name;
222
int control_read_timeout;
223
int control_write_timeout;
224
} libusb_device_t, DEVICE_EXTENSION, *PDEVICE_EXTENSION;
227
NTSTATUS DDKAPI add_device(DRIVER_OBJECT *driver_object,
228
DEVICE_OBJECT *physical_device_object);
230
NTSTATUS DDKAPI dispatch(DEVICE_OBJECT *device_object, IRP *irp);
231
NTSTATUS dispatch_pnp(libusb_device_t *dev, IRP *irp);
232
NTSTATUS dispatch_power(libusb_device_t *dev, IRP *irp);
233
NTSTATUS dispatch_ioctl(libusb_device_t *dev, IRP *irp);
235
NTSTATUS complete_irp(IRP *irp, NTSTATUS status, ULONG info);
237
#define call_usbd(dev, urb, control_code, timeout) \
238
call_usbd_ex(dev, urb, control_code, timeout, LIBUSB_MAX_CONTROL_TRANSFER_TIMEOUT)
240
NTSTATUS call_usbd_ex(libusb_device_t *dev,
246
NTSTATUS pass_irp_down(libusb_device_t *dev, IRP *irp,
247
PIO_COMPLETION_ROUTINE completion_routine,
250
bool_t accept_irp(libusb_device_t *dev, IRP *irp);
252
bool_t get_pipe_handle(libusb_device_t *dev, int endpoint_address,
253
USBD_PIPE_HANDLE *pipe_handle);
255
bool_t get_pipe_info(libusb_device_t *dev, int endpoint_address,
256
libusb_endpoint_t** pipe_info);
258
void clear_pipe_info(libusb_device_t *dev);
259
bool_t update_pipe_info(libusb_device_t *dev,
260
USBD_INTERFACE_INFORMATION *interface_info);
262
void remove_lock_initialize(libusb_device_t *dev);
263
NTSTATUS remove_lock_acquire(libusb_device_t *dev);
264
void remove_lock_release(libusb_device_t *dev);
265
void remove_lock_release_and_wait(libusb_device_t *dev);
267
NTSTATUS set_configuration(libusb_device_t *dev,
268
int configuration, int timeout);
269
NTSTATUS auto_configure(libusb_device_t *dev);
271
NTSTATUS get_configuration(libusb_device_t *dev,
272
unsigned char *configuration, int *ret,
275
NTSTATUS set_feature(libusb_device_t *dev,
276
int recipient, int index, int feature, int timeout);
277
NTSTATUS clear_feature(libusb_device_t *dev,
278
int recipient, int index, int feature, int timeout);
279
NTSTATUS get_status(libusb_device_t *dev, int recipient,
280
int index, char *status, int *ret, int timeout);
281
NTSTATUS set_descriptor(libusb_device_t *dev,
282
void *buffer, int size,
283
int type, int recipient, int index, int language_id,
284
int *sent, int timeout);
285
NTSTATUS get_descriptor(libusb_device_t *dev, void *buffer, int size,
286
int type, int recipient, int index, int language_id,
287
int *received, int timeout);
289
PUSB_CONFIGURATION_DESCRIPTOR get_config_descriptor(
290
libusb_device_t *dev,
295
NTSTATUS vendor_class_request(libusb_device_t *dev,
296
int type, int recipient,
297
int request, int value, int index,
298
void *buffer, int size, int direction,
299
int *ret, int timeout);
301
NTSTATUS abort_endpoint(libusb_device_t *dev, int endpoint, int timeout);
302
NTSTATUS reset_endpoint(libusb_device_t *dev, int endpoint, int timeout);
303
NTSTATUS reset_device(libusb_device_t *dev, int timeout);
305
#define USB_RESET_TYPE_RESET_PORT (1 << 0)
306
#define USB_RESET_TYPE_CYCLE_PORT (1 << 1)
307
#define USB_RESET_TYPE_FULL_RESET (USB_RESET_TYPE_CYCLE_PORT | USB_RESET_TYPE_RESET_PORT)
308
NTSTATUS reset_device_ex(libusb_device_t *dev, int timeout, unsigned int reset_type);
310
bool_t reg_get_hardware_id(DEVICE_OBJECT *physical_device_object,
311
char *data, int size);
312
bool_t reg_get_compatible_id(DEVICE_OBJECT *physical_device_object,
313
char *data, int size);
315
bool_t reg_get_properties(libusb_device_t *dev);
318
void power_set_device_state(libusb_device_t *dev,
319
DEVICE_POWER_STATE device_state, bool_t block);
321
USB_INTERFACE_DESCRIPTOR *
322
find_interface_desc(USB_CONFIGURATION_DESCRIPTOR *config_desc,
323
unsigned int size, int interface_number, int altsetting);
325
#define FIND_INTERFACE_INDEX_ANY (-1)
326
USB_INTERFACE_DESCRIPTOR* find_interface_desc_ex(USB_CONFIGURATION_DESCRIPTOR *config_desc,
328
interface_request_t* intf,
329
unsigned int* size_left);
331
USB_ENDPOINT_DESCRIPTOR *
332
find_endpoint_desc_by_index(USB_INTERFACE_DESCRIPTOR *interface_desc,
333
unsigned int size, int pipe_index);
336
Gets a device property for the device_object.
338
Returns: NTSTATUS code from IoGetDeviceProperty
339
STATUS_INVALID_PARAMETER
341
NTSTATUS reg_get_device_property(PDEVICE_OBJECT device_object,
347
NTSTATUS reg_get_custom_property(PDEVICE_OBJECT device_object,
349
unsigned int data_length,
350
unsigned int name_offset,
354
NTSTATUS transfer(libusb_device_t* dev,
358
IN libusb_endpoint_t* endpoint,
360
IN int transferFlags,
365
NTSTATUS large_transfer(IN libusb_device_t* dev,
369
IN libusb_endpoint_t* endpoint,
371
IN int maxTransferSize,
372
IN int transferFlags,
377
ULONG get_current_frame(IN PDEVICE_EXTENSION dev, IN PIRP Irp);
380
NTSTATUS control_transfer(libusb_device_t* dev,
393
NTSTATUS claim_interface(libusb_device_t *dev, FILE_OBJECT *file_object,
396
NTSTATUS claim_interface_ex(libusb_device_t *dev,
397
FILE_OBJECT *file_object,
398
interface_request_t* interface_request);
400
NTSTATUS release_all_interfaces(libusb_device_t *dev,
401
FILE_OBJECT *file_object);
403
NTSTATUS release_interface(libusb_device_t *dev, FILE_OBJECT *file_object,
406
NTSTATUS release_interface_ex(libusb_device_t *dev,
407
FILE_OBJECT *file_object,
408
interface_request_t* interface_request);
410
NTSTATUS set_interface(libusb_device_t *dev,
411
int interface_number,
412
int alt_interface_number,
415
NTSTATUS set_interface_ex(libusb_device_t *dev,
416
interface_request_t* interface_request,
419
NTSTATUS get_interface(libusb_device_t *dev,
420
int interface_number,
421
unsigned char *altsetting,
424
NTSTATUS get_interface_ex(libusb_device_t *dev,
425
interface_request_t* interface_request,
428
VOID set_filter_interface_key(libusb_device_t *dev, ULONG id);