1 // Copyright (c) 2004, Antony C. Roberts
2 
3 // Use of this file is subject to the terms
4 // described in the LICENSE.TXT file that
5 // accompanies this file.
6 //
7 // Your use of this file indicates your
8 // acceptance of the terms described in
9 // LICENSE.TXT.
10 //
11 // http://www.freebt.net
12 
13 #ifndef _FREEBT_H
14 #define _FREEBT_H
15 
16 #include <initguid.h>
17 #include <wdm.h>
18 #include <wmilib.h>
19 #include <wmistr.h>
20 #include <windef.h>
21 #include "usbdi.h"
22 #include "usbdlib.h"
23 
24 // Pull in all the command, event and structure definitions
25 #include "fbtHciDefs.h"
26 
27 // Standard USB Wireless/Bluetooth class, etc
28 #define FREEBT_USB_STDCLASS		0xE0		// Wireless Controller
29 #define FREEBT_USB_STDSUBCLASS	0x01		// RF Controller
30 #define FREEBT_USB_STDPROTOCOL	0x01		// Bluetooth Programming
31 
32 // Recommended Bluetooth Endpoints
33 #define FREEBT_STDENDPOINT_HCICMD	0x00	// HCI Command
34 #define FREEBT_STDENDPOINT_HCIEVENT	0x81	// HCI Event
35 #define FREEBT_STDENDPOINT_ACLIN	0x82	// HCI Data In
36 #define FREEBT_STDENDPOINT_ACLOUT	0x02	// HCI Data Out
37 #define FREEBT_STDENDPOINT_AUDIOIN	0x83	// SCO In
38 #define FREEBT_STDENDPOINT_AUDIOOUT	0x03	// SCO Out
39 
40 
41 #define OBTTAG (ULONG) 'OBTU'
42 
43 #undef ExAllocatePool
44 #define ExAllocatePool(type, size) ExAllocatePoolWithTag(type, size, OBTTAG);
45 
46 #if DBG
47 
48 #define FreeBT_DbgPrint(level, _x_) \
49             if((level) <= DebugLevel) { \
50                 DbgPrint _x_; \
51             }
52 
53 #else
54 
55 #define FreeBT_DbgPrint(level, _x_)
56 
57 #endif
58 
59 typedef struct _GLOBALS
60 {
61     UNICODE_STRING	FreeBT_RegistryPath;
62 
63 } GLOBALS;
64 
65 #define IDLE_INTERVAL 5000
66 
67 typedef enum _PIPETYPE
68 {
69 	HciCommandPipe,
70 	HciEventPipe,
71 	AclDataIn,
72 	AclDataOut,
73 	SCODataIn,
74 	SCODataOut
75 
76 } FREEBT_PIPETYPE;
77 
78 typedef enum _DEVSTATE
79 {
80     NotStarted,         // not started
81     Stopped,            // device stopped
82     Working,            // started and working
83     PendingStop,        // stop pending
84     PendingRemove,      // remove pending
85     SurpriseRemoved,    // removed by surprise
86     Removed             // removed
87 
88 } DEVSTATE;
89 
90 typedef enum _QUEUE_STATE
91 {
92     HoldRequests,       // device is not started yet
93     AllowRequests,      // device is ready to process
94     FailRequests        // fail both existing and queued up requests
95 
96 } QUEUE_STATE;
97 
98 typedef enum _WDM_VERSION
99 {
100     WinXpOrBetter,
101     Win2kOrBetter,
102     WinMeOrBetter,
103     Win98OrBetter
104 
105 } WDM_VERSION;
106 
107 #define INITIALIZE_PNP_STATE(_Data_)    \
108         (_Data_)->DeviceState =  NotStarted;\
109         (_Data_)->PrevDevState = NotStarted;
110 
111 #define SET_NEW_PNP_STATE(_Data_, _state_) \
112         (_Data_)->PrevDevState =  (_Data_)->DeviceState;\
113         (_Data_)->DeviceState = (_state_);
114 
115 #define RESTORE_PREVIOUS_PNP_STATE(_Data_)   \
116         (_Data_)->DeviceState =   (_Data_)->PrevDevState;
117 
118 
119 // registry path used for parameters
120 // global to all instances of the driver
121 #define FREEBT_REGISTRY_PARAMETERS_PATH  L"\\REGISTRY\\Machine\\System\\CurrentControlSet\\SERVICES\\BULKUSB\\Parameters"
122 
123 typedef struct _FREEBT_PIPE_CONTEXT
124 {
125     BOOLEAN			PipeOpen;
126 	FREEBT_PIPETYPE	PipeType;
127 
128 } FREEBT_PIPE_CONTEXT, *PFREEBT_PIPE_CONTEXT;
129 
130 // A structure representing the instance information associated with
131 // this particular device.
132 typedef struct _DEVICE_EXTENSION
133 {
134     // Functional Device Object
135     PDEVICE_OBJECT FunctionalDeviceObject;
136 
137     // Device object we call when submitting Urbs
138     PDEVICE_OBJECT TopOfStackDeviceObject;
139 
140     // The bus driver object
141     PDEVICE_OBJECT PhysicalDeviceObject;
142 
143     // Name buffer for our named Functional device object link
144     // The name is generated based on the driver's class GUID
145     UNICODE_STRING InterfaceName;
146 
147     // Bus drivers set the appropriate values in this structure in response
148     // to an IRP_MN_QUERY_CAPABILITIES IRP. Function and filter drivers might
149     // alter the capabilities set by the bus driver.
150     DEVICE_CAPABILITIES DeviceCapabilities;
151 
152     // Configuration Descriptor
153     PUSB_CONFIGURATION_DESCRIPTOR UsbConfigurationDescriptor;
154 
155     // Interface Information structure
156     PUSBD_INTERFACE_INFORMATION UsbInterface;
157 
158     // Pipe context for the driver
159     PFREEBT_PIPE_CONTEXT PipeContext;
160 
161     // current state of device
162     DEVSTATE DeviceState;
163 
164     // state prior to removal query
165     DEVSTATE PrevDevState;
166 
167     // obtain and hold this lock while changing the device state,
168     // the queue state and while processing the queue.
169     KSPIN_LOCK DevStateLock;
170 
171     // current system power state
172     SYSTEM_POWER_STATE SysPower;
173 
174     // current device power state
175     DEVICE_POWER_STATE DevPower;
176 
177     // Pending I/O queue state
178     QUEUE_STATE QueueState;
179 
180     // Pending I/O queue
181     LIST_ENTRY NewRequestsQueue;
182 
183     // I/O Queue Lock
184     KSPIN_LOCK QueueLock;
185 
186     KEVENT RemoveEvent;
187 
188     KEVENT StopEvent;
189 
190     ULONG OutStandingIO;
191 
192     KSPIN_LOCK IOCountLock;
193 
194     // Selective Suspend variables
195     LONG SSEnable;
196     LONG SSRegistryEnable;
197     PUSB_IDLE_CALLBACK_INFO IdleCallbackInfo;
198     PIRP PendingIdleIrp;
199     LONG IdleReqPend;
200     LONG FreeIdleIrpCount;
201     KSPIN_LOCK IdleReqStateLock;
202     KEVENT NoIdleReqPendEvent;
203 
204     // Default power state to power down to on self-susped
205     ULONG PowerDownLevel;
206 
207     // remote wakeup variables
208     PIRP WaitWakeIrp;
209     LONG FlagWWCancel;
210     LONG FlagWWOutstanding;
211     LONG WaitWakeEnable;
212 
213     // Open handle count
214     LONG OpenHandleCount;
215 
216     // Selective suspend model uses timers, dpcs and work item.
217     KTIMER Timer;
218 
219     KDPC DeferredProcCall;
220 
221     // This event is cleared when a DPC/Work Item is queued.
222     // and signaled when the work-item completes.
223     // This is essential to prevent the driver from unloading
224     // while we have DPC or work-item queued up.
225     KEVENT NoDpcWorkItemPendingEvent;
226 
227     // WMI information
228     WMILIB_CONTEXT WmiLibInfo;
229 
230     // WDM version
231     WDM_VERSION WdmVersion;
232 
233     // Pipe type
234     FREEBT_PIPETYPE	PipeType;
235 
236     // User accessible object name
237 	WCHAR wszDosDeviceName[50];
238 
239 	// A never triggered event used for delaying execution
240 	KEVENT DelayEvent;
241 
242 	// Significant pipes
243 	USBD_PIPE_INFORMATION EventPipe;
244 	USBD_PIPE_INFORMATION DataInPipe;
245 	USBD_PIPE_INFORMATION DataOutPipe;
246 	USBD_PIPE_INFORMATION AudioInPipe;
247 	USBD_PIPE_INFORMATION AudioOutPipe;
248 
249 } DEVICE_EXTENSION, *PDEVICE_EXTENSION;
250 
251 
252 typedef struct _IRP_COMPLETION_CONTEXT
253 {
254     PDEVICE_EXTENSION DeviceExtension;
255     PKEVENT Event;
256 
257 } IRP_COMPLETION_CONTEXT, *PIRP_COMPLETION_CONTEXT;
258 
259 extern GLOBALS Globals;
260 extern ULONG DebugLevel;
261 
262 #endif
263