1 /* libusb-win32, Generic Windows USB Library
2  * Copyright (c) 2002-2005 Stephan Meyer <ste_meyer@web.de>
3  *
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.
8  *
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.
13  *
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
17  */
18 
19 
20 #ifndef __LIBUSB_DRIVER_H__
21 #define __LIBUSB_DRIVER_H__
22 
23 //#define SKIP_CONFIGURE_NORMAL_DEVICES
24 //#define SKIP_DEVICES_WINUSB
25 //#define SKIP_DEVICES_PICOPP
26 
27 #ifdef __GNUC__
28 #include <ddk/usb100.h>
29 #include <ddk/usbdi.h>
30 #include <ddk/winddk.h>
31 #include "usbdlib_gcc.h"
32 #else
33 #include <ntifs.h>
34 #include <wdm.h>
35 #include "usbdi.h"
36 #include "usbdlib.h"
37 #endif
38 
39 #include <wchar.h>
40 #include <initguid.h>
41 
42 #undef interface
43 
44 #include "driver_debug.h"
45 #include "error.h"
46 #include "driver_api.h"
47 
48 /* some missing defines */
49 #ifdef __GNUC__
50 
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)
58 
59 #endif
60 
61 #define SET_CONFIG_ACTIVE_CONFIG -258
62 
63 #define USB_RECIP_DEVICE    0x00
64 #define USB_RECIP_INTERFACE 0x01
65 #define USB_RECIP_ENDPOINT  0x02
66 #define USB_RECIP_OTHER     0x03
67 
68 #define USB_TYPE_STANDARD   0x00
69 #define USB_TYPE_CLASS      0x01
70 #define USB_TYPE_VENDOR	    0x02
71 
72 
73 #define LIBUSB_NT_DEVICE_NAME L"\\Device\\libusb0"
74 #define LIBUSB_SYMBOLIC_LINK_NAME L"\\DosDevices\\libusb0-"
75 
76 #define LIBUSB_MAX_NUMBER_OF_ENDPOINTS  32
77 #define LIBUSB_MAX_NUMBER_OF_INTERFACES 32
78 
79 
80 #define LIBUSB_DEFAULT_TIMEOUT 5000
81 #define LIBUSB_MAX_CONTROL_TRANSFER_TIMEOUT 5000
82 
83 
84 #ifndef __GNUC__
85 #define DDKAPI
86 #endif
87 
88 #ifndef FALSE
89 #define FALSE 0
90 #endif
91 
92 #ifndef TRUE
93 #define TRUE (!(FALSE))
94 #endif
95 
96 typedef int bool_t;
97 
98 #define POOL_TAG (ULONG) '0BSU'
99 #undef ExAllocatePool
100 #define ExAllocatePool(type, size) ExAllocatePoolWithTag(type, size, POOL_TAG)
101 
102 #define IS_PIPE_TYPE(pipeInfo, pipeType) ((((pipeInfo->pipe_type & 3)==pipeType))?TRUE:FALSE)
103 
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)
108 
109 #define GetMaxTransferSize(pipeInfo, reqMaxTransferSize) ((reqMaxTransferSize) ? reqMaxTransferSize : pipeInfo->maximum_transfer_size)
110 
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)
113 
114 #define UpdateContextConfigDescriptor(DeviceContext, Descriptor, Size, Value, Index)		\
115 {																							\
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);													\
122 }
123 
124 #ifndef __WUSBIO_H__
125 
126 // Pipe policy types
127 
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
132 
133 // Device Information types
134 #define DEVICE_SPEED            0x01
135 
136 // Device Speeds
137 #define LowSpeed                0x01
138 #define FullSpeed               0x02
139 #define HighSpeed               0x03
140 
141 #endif
142 
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)
147 
148 
149 #include <pshpack1.h>
150 
151 typedef struct
152 {
153     unsigned char length;
154     unsigned char type;
155 } usb_descriptor_header_t;
156 
157 #include <poppack.h>
158 
159 
160 typedef struct
161 {
162     long usage_count;
163     int remove_pending;
164     KEVENT event;
165 } libusb_remove_lock_t;
166 
167 typedef struct
168 {
169     int address;
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
174 
175 	//
176     // INPUT
177     // These fields are filled in by the client driver
178     //
179     int maximum_transfer_size; // Maximum size for a single request
180                                // in bytes.
181     int pipe_flags;
182 } libusb_endpoint_t;
183 
184 typedef struct
185 {
186     bool_t valid;
187     FILE_OBJECT *file_object; /* file object this interface is bound to */
188     libusb_endpoint_t endpoints[LIBUSB_MAX_NUMBER_OF_ENDPOINTS];
189 
190 } libusb_interface_t;
191 
192 typedef struct
193 {
194     DEVICE_OBJECT	*self;
195     DEVICE_OBJECT	*physical_device_object;
196     DEVICE_OBJECT	*next_stack_device;
197     DEVICE_OBJECT	*target_device;
198     libusb_remove_lock_t remove_lock;
199     bool_t is_filter;
200     bool_t is_started;
201     bool_t surprise_removal_ok;
202     int id;
203 	USB_DEVICE_DESCRIPTOR device_descriptor;
204     struct
205     {
206         USBD_CONFIGURATION_HANDLE handle;
207         int value;
208 		int index;
209         libusb_interface_t interfaces[LIBUSB_MAX_NUMBER_OF_INTERFACES];
210 		PUSB_CONFIGURATION_DESCRIPTOR descriptor;
211 		int total_size;
212     } config;
213     POWER_STATE power_state;
214     DEVICE_POWER_STATE device_power_states[PowerSystemMaximum];
215 	int initial_config_value;
216 	char device_id[256];
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;
225 
226 
227 NTSTATUS DDKAPI add_device(DRIVER_OBJECT *driver_object,
228                            DEVICE_OBJECT *physical_device_object);
229 
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);
234 
235 NTSTATUS complete_irp(IRP *irp, NTSTATUS status, ULONG info);
236 
237 #define call_usbd(dev, urb, control_code, timeout) \
238 	call_usbd_ex(dev, urb, control_code, timeout, LIBUSB_MAX_CONTROL_TRANSFER_TIMEOUT)
239 
240 NTSTATUS call_usbd_ex(libusb_device_t *dev,
241 					  void *urb,
242 					  ULONG control_code,
243 					  int timeout,
244 					  int max_timeout);
245 
246 NTSTATUS pass_irp_down(libusb_device_t *dev, IRP *irp,
247                        PIO_COMPLETION_ROUTINE completion_routine,
248                        void *context);
249 
250 bool_t accept_irp(libusb_device_t *dev, IRP *irp);
251 
252 bool_t get_pipe_handle(libusb_device_t *dev, int endpoint_address,
253                        USBD_PIPE_HANDLE *pipe_handle);
254 
255 bool_t get_pipe_info(libusb_device_t *dev, int endpoint_address,
256                        libusb_endpoint_t** pipe_info);
257 
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);
261 
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);
266 
267 NTSTATUS set_configuration(libusb_device_t *dev,
268                            int configuration, int timeout);
269 NTSTATUS auto_configure(libusb_device_t *dev);
270 
271 NTSTATUS get_configuration(libusb_device_t *dev,
272                            unsigned char *configuration, int *ret,
273                            int timeout);
274 
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);
288 
289 PUSB_CONFIGURATION_DESCRIPTOR get_config_descriptor(
290 	libusb_device_t *dev,
291 	int value,
292 	int *size,
293 	int* index);
294 
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);
300 
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);
304 
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);
309 
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);
314 
315 bool_t reg_get_properties(libusb_device_t *dev);
316 
317 
318 void power_set_device_state(libusb_device_t *dev,
319                             DEVICE_POWER_STATE device_state, bool_t block);
320 
321 USB_INTERFACE_DESCRIPTOR *
322 find_interface_desc(USB_CONFIGURATION_DESCRIPTOR *config_desc,
323                     unsigned int size, int interface_number, int altsetting);
324 
325 #define FIND_INTERFACE_INDEX_ANY		(-1)
326 USB_INTERFACE_DESCRIPTOR* find_interface_desc_ex(USB_CONFIGURATION_DESCRIPTOR *config_desc,
327 												 unsigned int size,
328 												 interface_request_t* intf,
329 												 unsigned int* size_left);
330 
331 USB_ENDPOINT_DESCRIPTOR *
332 find_endpoint_desc_by_index(USB_INTERFACE_DESCRIPTOR *interface_desc,
333                     unsigned int size, int pipe_index);
334 
335 /*
336 Gets a device property for the device_object.
337 
338 Returns: NTSTATUS code from IoGetDeviceProperty
339          STATUS_INVALID_PARAMETER
340 */
341 NTSTATUS reg_get_device_property(PDEVICE_OBJECT device_object,
342 							   int property,
343 							   char* data_buffer,
344 							   int data_length,
345 							   int* actual_length);
346 
347 NTSTATUS reg_get_custom_property(PDEVICE_OBJECT device_object,
348 								 char *data_buffer,
349 								 unsigned int data_length,
350 								 unsigned int name_offset,
351 								 int* actual_length);
352 
353 
354 NTSTATUS transfer(libusb_device_t* dev,
355 				  IN PIRP irp,
356 				  IN int direction,
357 				  IN int urbFunction,
358 				  IN libusb_endpoint_t* endpoint,
359 				  IN int packetSize,
360 				  IN int transferFlags,
361 				  IN int isoLatency,
362 				  IN PMDL mdlAddress,
363 				  IN int totalLength);
364 
365 NTSTATUS large_transfer(IN libusb_device_t* dev,
366 						IN PIRP irp,
367 						IN int direction,
368 						IN int urbFunction,
369 						IN libusb_endpoint_t* endpoint,
370 						IN int packetSize,
371 						IN int maxTransferSize,
372 						IN int transferFlags,
373 						IN int isoLatency,
374 						IN PMDL mdlAddress,
375 						IN int totalLength);
376 
377 ULONG get_current_frame(IN PDEVICE_EXTENSION dev, IN PIRP Irp);
378 
379 
380 NTSTATUS control_transfer(libusb_device_t* dev,
381 						 PIRP irp,
382 						 PMDL mdl,
383 						 int size,
384 						 int usbd_direction,
385 						 int *ret,
386 						 int timeout,
387 						 UCHAR request_type,
388 						 UCHAR request,
389 						 USHORT value,
390 						 USHORT index,
391 						 USHORT length);
392 
393 NTSTATUS claim_interface(libusb_device_t *dev, FILE_OBJECT *file_object,
394                          int interface);
395 
396 NTSTATUS claim_interface_ex(libusb_device_t *dev,
397 							  FILE_OBJECT *file_object,
398 							  interface_request_t* interface_request);
399 
400 NTSTATUS release_all_interfaces(libusb_device_t *dev,
401                                 FILE_OBJECT *file_object);
402 
403 NTSTATUS release_interface(libusb_device_t *dev, FILE_OBJECT *file_object,
404                            int interface);
405 
406 NTSTATUS release_interface_ex(libusb_device_t *dev,
407 							  FILE_OBJECT *file_object,
408 							  interface_request_t* interface_request);
409 
410 NTSTATUS set_interface(libusb_device_t *dev,
411 					   int interface_number,
412 					   int alt_interface_number,
413                        int timeout);
414 
415 NTSTATUS set_interface_ex(libusb_device_t *dev,
416 					   interface_request_t* interface_request,
417                        int timeout);
418 
419 NTSTATUS get_interface(libusb_device_t *dev,
420 					   int interface_number,
421 					   unsigned char *altsetting,
422 					   int timeout);
423 
424 NTSTATUS get_interface_ex(libusb_device_t *dev,
425 					   interface_request_t* interface_request,
426                        int timeout);
427 
428 VOID set_filter_interface_key(libusb_device_t *dev, ULONG id);
429 #endif
430