win-pvdrivers

view xenusb/xenusb.h @ 991:e9903455ba9d

Fix low gref resource handling. again.
Tidy up.
author James Harper <james.harper@bendigoit.com.au>
date Fri Sep 21 23:25:36 2012 +1000 (2012-09-21)
parents ea3c61839ff5
children 329b9b9d47ec
line source
1 /*
2 PV Drivers for Windows Xen HVM Domains
3 Copyright (C) 2007 James Harper
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
20 #pragma warning(disable: 4127)
22 #pragma warning(disable : 4200) // zero-sized array
24 #if !defined(_XENUSB_H_)
25 #define _XENUSB_H_
27 #define __attribute__(arg) /* empty */
29 #define ECONNRESET 104
30 #define ESHUTDOWN 108
31 #define EINPROGRESS 115
32 #define EISCONN 127
34 #include <ntifs.h>
35 #include <ntddk.h>
37 #define DDKAPI
38 //#include <wdm.h>
39 #include <wdf.h>
40 #include <initguid.h>
41 #include <wdmguid.h>
42 #include <errno.h>
43 #define NTSTRSAFE_LIB
44 #include <ntstrsafe.h>
45 #include <liblfds.h>
47 #define __DRIVER_NAME "XenUSB"
49 #include <xen_windows.h>
50 #include <io/ring.h>
51 #include <io/usbif.h>
52 #include <io/xenbus.h>
53 #include <usb.h>
54 #include <usbioctl.h>
55 #include <usbdlib.h>
56 #include <hubbusif.h>
57 #include <usbbusif.h>
59 #define C_HUB_LOCAL_POWER 0
60 #define C_HUB_OVER_CURRENT 1
61 #define PORT_CONNECTION 0
62 #define PORT_ENABLE 1
63 #define PORT_SUSPEND 2
64 #define PORT_OVER_CURRENT 3
65 #define PORT_RESET 4
66 #define PORT_POWER 8
67 #define PORT_LOW_SPEED 9
68 #define PORT_HIGH_SPEED 10
69 #define C_PORT_CONNECTION 16
70 #define C_PORT_ENABLE 17
71 #define C_PORT_SUSPEND 18
72 #define C_PORT_OVER_CURRENT 19
73 #define C_PORT_RESET 20
74 #define PORT_TEST 21
75 #define PORT_INDICATOR 22
77 #define XENUSB_POOL_TAG (ULONG)'XenU'
79 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
81 #define CHILD_STATE_EMPTY 0
82 #define CHILD_STATE_DELETED 1
83 #define CHILD_STATE_ADDED 2
85 #define LINUX_PIPE_DIRECTION_OUT 0x00000000
86 #define LINUX_PIPE_DIRECTION_IN 0x00000080
88 /* these are linux definitions - different to usb standard */
89 #define LINUX_PIPE_TYPE_ISOC 0x00000000
90 #define LINUX_PIPE_TYPE_INTR 0x40000000
91 #define LINUX_PIPE_TYPE_CTRL 0x80000000
92 #define LINUX_PIPE_TYPE_BULK 0xC0000000
94 /*
95 * urb->transfer_flags:
96 */
97 #define LINUX_URB_SHORT_NOT_OK 0x0001 /* report short reads as errors */
98 #define LINUX_URB_ISO_ASAP 0x0002 /* iso-only, urb->start_frame ignored */
99 #define LINUX_URB_NO_TRANSFER_DMA_MAP 0x0004 /* urb->transfer_dma valid on submit */
100 #define LINUX_URB_NO_SETUP_DMA_MAP 0x0008 /* urb->setup_dma valid on submit */
101 #define LINUX_URB_NO_FSBR 0x0020 /* UHCI-specific */
102 #define LINUX_URB_ZERO_PACKET 0x0040 /* Finish bulk OUT with short packet */
103 #define LINUX_URB_NO_INTERRUPT 0x0080 /* HINT: no non-error interrupt needed */
105 struct _pvurb;
106 struct _partial_pvurb;
107 typedef struct _pvurb pvurb_t;
108 typedef struct _partial_pvurb partial_pvurb_t;
110 /* needs to be at least USB_URB_RING_SIZE number of requests available */
111 #define MAX_REQ_ID_COUNT 64
112 #define REQ_ID_COUNT min(MAX_REQ_ID_COUNT, USB_URB_RING_SIZE)
114 /*
115 for IOCTL_PVUSB_SUBMIT_URB, the pvusb_urb_t struct is passed as Parameters.Others.Arg1
116 req must have pipe, transfer_flags, buffer_length, and u fields filled in
117 */
119 #define IOCTL_INTERNAL_PVUSB_SUBMIT_URB CTL_CODE(FILE_DEVICE_UNKNOWN, 0x800, METHOD_NEITHER, FILE_READ_DATA | FILE_WRITE_DATA)
121 struct _partial_pvurb {
122 LIST_ENTRY entry;
123 pvurb_t *pvurb;
124 partial_pvurb_t *other_partial_pvurb; /* link to the cancelled or cancelling partial_pvurb */
125 usbif_urb_request_t req;
126 usbif_urb_response_t rsp;
127 PMDL mdl;
128 BOOLEAN on_ring; /* is (or has been) on the hardware ring */
129 };
131 struct _pvurb {
132 /* set by xenusb_devurb.c */
133 usbif_urb_request_t req; /* only pipe, transfer_flags, isoc/intr filled out by submitter */
134 PMDL mdl; /* can be null for requests with no data */
135 /* set by xenusb_fdo.c */
136 usbif_urb_response_t rsp; /* only status, actual_length, error_count valid */
137 NTSTATUS status;
138 WDFREQUEST request;
139 ULONG ref; /* reference counting */
140 ULONG total_length;
141 pvurb_t *next; /* collect then process responses as they come off the ring */
142 };
144 struct _xenusb_endpoint_t;
145 struct _xenusb_interface_t;
146 struct _xenusb_config_t;
147 struct _xenusb_device_t;
148 typedef struct _xenusb_endpoint_t xenusb_endpoint_t, *pxenusb_endpoint_t;
149 typedef struct _xenusb_interface_t xenusb_interface_t;
150 typedef struct _xenusb_config_t xenusb_config_t;
151 typedef struct _xenusb_device_t xenusb_device_t;
153 typedef struct _xenusb_endpoint_t {
154 xenusb_interface_t *interface;
155 ULONG pipe_value;
156 WDFQUEUE queue;
157 WDFSPINLOCK lock;
158 USB_ENDPOINT_DESCRIPTOR endpoint_descriptor;
159 };
160 //WDF_DECLARE_CONTEXT_TYPE_WITH_NAME(pxenusb_endpoint_t, GetEndpoint)
162 typedef struct _xenusb_interface_t {
163 xenusb_config_t *config;
164 USB_INTERFACE_DESCRIPTOR interface_descriptor;
165 xenusb_endpoint_t *endpoints[0];
166 } xenusb_interface_t;
168 typedef struct _xenusb_config_t {
169 xenusb_device_t *device;
170 USB_CONFIGURATION_DESCRIPTOR config_descriptor;
171 PUCHAR config_descriptor_all;
172 xenusb_interface_t *interfaces[0];
173 } xenusb_config_t;
175 typedef struct _xenusb_device_t {
176 WDFDEVICE pdo_device;
177 UCHAR address;
178 USB_DEVICE_DESCRIPTOR device_descriptor;
179 ULONG port_number;
180 WDFQUEUE urb_queue;
181 USB_DEVICE_SPEED device_speed;
182 USB_DEVICE_TYPE device_type;
183 xenusb_config_t *active_config;
184 xenusb_interface_t *active_interface;
185 xenusb_config_t **configs; /* pointer to an array of configs */
186 } xenusb_device_t;
188 #define USB_PORT_TYPE_NOT_CONNECTED 0
189 #define USB_PORT_TYPE_LOW_SPEED 1
190 #define USB_PORT_TYPE_FULL_SPEED 2
191 #define USB_PORT_TYPE_HIGH_SPEED 3
193 typedef struct
194 {
195 ULONG port_number;
196 ULONG port_type;
197 USHORT port_status;
198 USHORT port_change;
199 ULONG reset_counter;
200 } xenusb_port_t;
202 /*
203 TODO: this driver crashes under checked build of windows (or probably just checked usbhub.sys)
204 Needs a magic number of (ULONG)'HUBx' at the start of BusContext
205 Other magic numbers (ULONG)'BStx' at offset 0x4C of some structure
206 and (ULONG)'HUB ' somewhere in an FDO extension(?)
207 */
209 typedef struct {
210 ULONG magic; /* (ULONG)'HUBx' */
211 /* other magic numbers are (ULONG)'BStx' at offset 0x4C and (ULONG)'HUB ' somewhere in an FDO extension(?) */
212 BOOLEAN XenBus_ShuttingDown;
213 WDFQUEUE io_queue;
214 WDFQUEUE pvurb_queue;
215 WDFCHILDLIST child_list;
217 WDFDEVICE root_hub_device;
219 struct stack_state *req_id_ss;
220 partial_pvurb_t *partial_pvurbs[MAX_REQ_ID_COUNT];
222 PUCHAR config_page;
224 /* protected by conn_ring_lock */
225 ULONG num_ports;
226 xenusb_port_t ports[32];
228 KSPIN_LOCK urb_ring_lock;
229 usbif_urb_sring_t *urb_sring;
230 usbif_urb_front_ring_t urb_ring;
231 LIST_ENTRY partial_pvurb_queue;
232 LIST_ENTRY partial_pvurb_ring;
234 KSPIN_LOCK conn_ring_lock;
235 usbif_conn_sring_t *conn_sring;
236 usbif_conn_front_ring_t conn_ring;
238 domid_t backend_id;
239 evtchn_port_t event_channel;
241 XENPCI_VECTORS vectors;
242 PXENPCI_DEVICE_STATE device_state;
244 } XENUSB_DEVICE_DATA, *PXENUSB_DEVICE_DATA;
246 WDF_DECLARE_CONTEXT_TYPE_WITH_NAME(XENUSB_DEVICE_DATA, GetXudd)
248 #define DEV_ID_COUNT 64
250 typedef struct {
251 WDFDEVICE wdf_device;
252 WDFDEVICE wdf_device_bus_fdo;
253 WDFIOTARGET bus_fdo_target;
254 WDFQUEUE io_queue;
255 WDFQUEUE urb_queue;
256 ULONG device_number;
257 struct stack_state *dev_id_ss;
258 xenusb_device_t *usb_device;
259 PVOID BusCallbackContext;
260 PRH_INIT_CALLBACK BusCallbackFunction;
261 } XENUSB_PDO_DEVICE_DATA, *PXENUSB_PDO_DEVICE_DATA;
263 WDF_DECLARE_CONTEXT_TYPE_WITH_NAME(XENUSB_PDO_DEVICE_DATA, GetXupdd)
265 #if 0
266 typedef struct {
267 UCHAR usb_class;
268 UCHAR usb_subclass;
269 UCHAR usb_protocol;
270 } xenusb_compatible_id_details_t;
271 #endif
273 typedef struct {
274 WDF_CHILD_IDENTIFICATION_DESCRIPTION_HEADER header;
275 ULONG device_number;
276 //ULONG port_number;
277 //ULONG port_type;
278 //USHORT vendor_id;
279 //USHORT product_id;
280 //xenusb_compatible_id_details_t xucid[1];
281 } XENUSB_PDO_IDENTIFICATION_DESCRIPTION, *PXENUSB_PDO_IDENTIFICATION_DESCRIPTION;
284 static uint16_t
285 get_id_from_freelist(struct stack_state *ss) {
286 ULONG_PTR _id;
287 if (!stack_pop(ss, (VOID *)&_id)) {
288 KdPrint((__DRIVER_NAME " No more id's\n"));
289 return (uint16_t)-1;
290 }
291 return (uint16_t)_id;
292 }
294 static VOID
295 put_id_on_freelist(struct stack_state *ss, uint16_t id) {
296 ULONG_PTR _id = id;
297 stack_push(ss, (VOID *)_id);
298 }
300 static
301 ULONGLONG parse_numeric_string(PCHAR string)
302 {
303 ULONGLONG val = 0;
304 while (*string != 0)
305 {
306 val = val * 10 + (*string - '0');
307 string++;
308 }
309 return val;
310 }
312 EVT_WDF_DRIVER_DEVICE_ADD XenUsb_EvtDriverDeviceAdd;
314 EVT_WDF_CHILD_LIST_CREATE_DEVICE XenUsb_EvtChildListCreateDevice;
315 EVT_WDF_CHILD_LIST_SCAN_FOR_CHILDREN XenUsb_EvtChildListScanForChildren;
317 VOID
318 XenUsb_EnumeratePorts(WDFDEVICE device);
320 EVT_WDF_IO_QUEUE_IO_INTERNAL_DEVICE_CONTROL XenUsb_EvtIoInternalDeviceControl_DEVICE_SUBMIT_URB;
321 EVT_WDF_IO_QUEUE_IO_INTERNAL_DEVICE_CONTROL XenUsb_EvtIoInternalDeviceControl_ROOTHUB_SUBMIT_URB;
323 #define URB_DECODE_UNKNOWN 0 /* URB is unknown */
324 #define URB_DECODE_COMPLETE 1 /* URB is decoded and no further work should be required */
325 #define URB_DECODE_INCOMPLETE 2 /* URB is decoded but further work is required */
326 #define URB_DECODE_NOT_CONTROL 3 /* URB is known but not a control packet */
328 typedef struct {
329 char *urb_function_name;
330 PULONG length;
331 PVOID buffer;
332 PVOID mdl;
333 ULONG transfer_flags;
334 union {
335 USB_DEFAULT_PIPE_SETUP_PACKET default_pipe_setup_packet;
336 UCHAR raw[8];
337 } setup_packet;
338 } urb_decode_t;
340 ULONG
341 XenUsb_DecodeControlUrb(PURB urb, urb_decode_t *decode_data);
343 VOID
344 XenUsbHub_ProcessHubInterruptEvent(xenusb_endpoint_t *endpoint);
346 #endif