1 | /* $Id: UsbMsd.cpp 69500 2017-10-28 15:14:05Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * UsbMSD - USB Mass Storage Device Emulation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2007-2017 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 |
|
---|
19 | /*********************************************************************************************************************************
|
---|
20 | * Header Files *
|
---|
21 | *********************************************************************************************************************************/
|
---|
22 | #define LOG_GROUP LOG_GROUP_USB_MSD
|
---|
23 | #include <VBox/vmm/pdmusb.h>
|
---|
24 | #include <VBox/vmm/pdmstorageifs.h>
|
---|
25 | #include <VBox/log.h>
|
---|
26 | #include <VBox/err.h>
|
---|
27 | #include <VBox/scsi.h>
|
---|
28 | #include <iprt/assert.h>
|
---|
29 | #include <iprt/critsect.h>
|
---|
30 | #include <iprt/mem.h>
|
---|
31 | #include <iprt/semaphore.h>
|
---|
32 | #include <iprt/string.h>
|
---|
33 | #include <iprt/uuid.h>
|
---|
34 | #include "VBoxDD.h"
|
---|
35 |
|
---|
36 |
|
---|
37 | /*********************************************************************************************************************************
|
---|
38 | * Defined Constants And Macros *
|
---|
39 | *********************************************************************************************************************************/
|
---|
40 | /** @name USB MSD string IDs
|
---|
41 | * @{ */
|
---|
42 | #define USBMSD_STR_ID_MANUFACTURER 1
|
---|
43 | #define USBMSD_STR_ID_PRODUCT_HD 2
|
---|
44 | #define USBMSD_STR_ID_PRODUCT_CDROM 3
|
---|
45 | /** @} */
|
---|
46 |
|
---|
47 | /** @name USB MSD vendor and product IDs
|
---|
48 | * @{ */
|
---|
49 | #define VBOX_USB_VENDOR 0x80EE
|
---|
50 | #define USBMSD_PID_HD 0x0030
|
---|
51 | #define USBMSD_PID_CD 0x0031
|
---|
52 | /** @} */
|
---|
53 |
|
---|
54 | /** Saved state version. */
|
---|
55 | #define USB_MSD_SAVED_STATE_VERSION 2
|
---|
56 | /** Saved state vesion before the cleanup. */
|
---|
57 | #define USB_MSD_SAVED_STATE_VERSION_PRE_CLEANUP 1
|
---|
58 |
|
---|
59 |
|
---|
60 | /*********************************************************************************************************************************
|
---|
61 | * Structures and Typedefs *
|
---|
62 | *********************************************************************************************************************************/
|
---|
63 |
|
---|
64 | /**
|
---|
65 | * USB MSD Command Block Wrapper or CBW. The command block
|
---|
66 | * itself (CBWCB) contains protocol-specific data (here SCSI).
|
---|
67 | */
|
---|
68 | #pragma pack(1)
|
---|
69 | typedef struct USBCBW
|
---|
70 | {
|
---|
71 | uint32_t dCBWSignature;
|
---|
72 | #define USBCBW_SIGNATURE UINT32_C(0x43425355)
|
---|
73 | uint32_t dCBWTag;
|
---|
74 | uint32_t dCBWDataTransferLength;
|
---|
75 | uint8_t bmCBWFlags;
|
---|
76 | #define USBCBW_DIR_MASK RT_BIT(7)
|
---|
77 | #define USBCBW_DIR_OUT 0
|
---|
78 | #define USBCBW_DIR_IN RT_BIT(7)
|
---|
79 | uint8_t bCBWLun;
|
---|
80 | uint8_t bCBWCBLength;
|
---|
81 | uint8_t CBWCB[16];
|
---|
82 | } USBCBW;
|
---|
83 | #pragma pack()
|
---|
84 | AssertCompileSize(USBCBW, 31);
|
---|
85 | /** Pointer to a Command Block Wrapper. */
|
---|
86 | typedef USBCBW *PUSBCBW;
|
---|
87 | /** Pointer to a const Command Block Wrapper. */
|
---|
88 | typedef const USBCBW *PCUSBCBW;
|
---|
89 |
|
---|
90 | /**
|
---|
91 | * USB MSD Command Status Wrapper or CSW.
|
---|
92 | */
|
---|
93 | #pragma pack(1)
|
---|
94 | typedef struct USBCSW
|
---|
95 | {
|
---|
96 | uint32_t dCSWSignature;
|
---|
97 | #define USBCSW_SIGNATURE UINT32_C(0x53425355)
|
---|
98 | uint32_t dCSWTag;
|
---|
99 | uint32_t dCSWDataResidue;
|
---|
100 | #define USBCSW_STATUS_OK UINT8_C(0)
|
---|
101 | #define USBCSW_STATUS_FAILED UINT8_C(1)
|
---|
102 | #define USBCSW_STATUS_PHASE_ERROR UINT8_C(2)
|
---|
103 | uint8_t bCSWStatus;
|
---|
104 | } USBCSW;
|
---|
105 | #pragma pack()
|
---|
106 | AssertCompileSize(USBCSW, 13);
|
---|
107 | /** Pointer to a Command Status Wrapper. */
|
---|
108 | typedef USBCSW *PUSBCSW;
|
---|
109 | /** Pointer to a const Command Status Wrapper. */
|
---|
110 | typedef const USBCSW *PCUSBCSW;
|
---|
111 |
|
---|
112 |
|
---|
113 | /**
|
---|
114 | * The USB MSD request state.
|
---|
115 | */
|
---|
116 | typedef enum USBMSDREQSTATE
|
---|
117 | {
|
---|
118 | /** Invalid status. */
|
---|
119 | USBMSDREQSTATE_INVALID = 0,
|
---|
120 | /** Ready to receive a new SCSI command. */
|
---|
121 | USBMSDREQSTATE_READY,
|
---|
122 | /** Waiting for the host to supply data. */
|
---|
123 | USBMSDREQSTATE_DATA_FROM_HOST,
|
---|
124 | /** The SCSI request is being executed by the driver. */
|
---|
125 | USBMSDREQSTATE_EXECUTING,
|
---|
126 | /** Have (more) data for the host. */
|
---|
127 | USBMSDREQSTATE_DATA_TO_HOST,
|
---|
128 | /** Waiting to supply status information to the host. */
|
---|
129 | USBMSDREQSTATE_STATUS,
|
---|
130 | /** Destroy the request upon completion.
|
---|
131 | * This is set when the SCSI request doesn't complete before for the device or
|
---|
132 | * mass storage reset operation times out. USBMSD::pReq will be set to NULL
|
---|
133 | * and the only reference to this request will be with DrvSCSI. */
|
---|
134 | USBMSDREQSTATE_DESTROY_ON_COMPLETION,
|
---|
135 | /** The end of the valid states. */
|
---|
136 | USBMSDREQSTATE_END,
|
---|
137 | /** 32bit blow up hack. */
|
---|
138 | USBMSDREQSTATE_32BIT_HACK = 0x7fffffff
|
---|
139 | } USBMSDREQSTATE;
|
---|
140 |
|
---|
141 |
|
---|
142 | /**
|
---|
143 | * A pending USB MSD request.
|
---|
144 | */
|
---|
145 | typedef struct USBMSDREQ
|
---|
146 | {
|
---|
147 | /** The state of the request. */
|
---|
148 | USBMSDREQSTATE enmState;
|
---|
149 | /** The I/O requesthandle .*/
|
---|
150 | PDMMEDIAEXIOREQ hIoReq;
|
---|
151 | /** The size of the data buffer. */
|
---|
152 | uint32_t cbBuf;
|
---|
153 | /** Pointer to the data buffer. */
|
---|
154 | uint8_t *pbBuf;
|
---|
155 | /** Current buffer offset. */
|
---|
156 | uint32_t offBuf;
|
---|
157 | /** The current Cbw when we're in the pending state. */
|
---|
158 | USBCBW Cbw;
|
---|
159 | /** The status of a completed SCSI request. */
|
---|
160 | uint8_t iScsiReqStatus;
|
---|
161 | } USBMSDREQ;
|
---|
162 | /** Pointer to a USB MSD request. */
|
---|
163 | typedef USBMSDREQ *PUSBMSDREQ;
|
---|
164 |
|
---|
165 |
|
---|
166 | /**
|
---|
167 | * Endpoint status data.
|
---|
168 | */
|
---|
169 | typedef struct USBMSDEP
|
---|
170 | {
|
---|
171 | bool fHalted;
|
---|
172 | } USBMSDEP;
|
---|
173 | /** Pointer to the endpoint status. */
|
---|
174 | typedef USBMSDEP *PUSBMSDEP;
|
---|
175 |
|
---|
176 |
|
---|
177 | /**
|
---|
178 | * A URB queue.
|
---|
179 | */
|
---|
180 | typedef struct USBMSDURBQUEUE
|
---|
181 | {
|
---|
182 | /** The head pointer. */
|
---|
183 | PVUSBURB pHead;
|
---|
184 | /** Where to insert the next entry. */
|
---|
185 | PVUSBURB *ppTail;
|
---|
186 | } USBMSDURBQUEUE;
|
---|
187 | /** Pointer to a URB queue. */
|
---|
188 | typedef USBMSDURBQUEUE *PUSBMSDURBQUEUE;
|
---|
189 | /** Pointer to a const URB queue. */
|
---|
190 | typedef USBMSDURBQUEUE const *PCUSBMSDURBQUEUE;
|
---|
191 |
|
---|
192 |
|
---|
193 | /**
|
---|
194 | * The USB MSD instance data.
|
---|
195 | */
|
---|
196 | typedef struct USBMSD
|
---|
197 | {
|
---|
198 | /** Pointer back to the PDM USB Device instance structure. */
|
---|
199 | PPDMUSBINS pUsbIns;
|
---|
200 | /** Critical section protecting the device state. */
|
---|
201 | RTCRITSECT CritSect;
|
---|
202 |
|
---|
203 | /** The current configuration.
|
---|
204 | * (0 - default, 1 - the only, i.e configured.) */
|
---|
205 | uint8_t bConfigurationValue;
|
---|
206 | /** Endpoint 0 is the default control pipe, 1 is the host->dev bulk pipe and 2
|
---|
207 | * is the dev->host one. */
|
---|
208 | USBMSDEP aEps[3];
|
---|
209 | /** The current request. */
|
---|
210 | PUSBMSDREQ pReq;
|
---|
211 |
|
---|
212 | /** Pending to-host queue.
|
---|
213 | * The URBs waiting here are pending the completion of the current request and
|
---|
214 | * data or status to become available.
|
---|
215 | */
|
---|
216 | USBMSDURBQUEUE ToHostQueue;
|
---|
217 |
|
---|
218 | /** Done queue
|
---|
219 | * The URBs stashed here are waiting to be reaped. */
|
---|
220 | USBMSDURBQUEUE DoneQueue;
|
---|
221 | /** Signalled when adding an URB to the done queue and fHaveDoneQueueWaiter
|
---|
222 | * is set. */
|
---|
223 | RTSEMEVENT hEvtDoneQueue;
|
---|
224 | /** Someone is waiting on the done queue. */
|
---|
225 | bool fHaveDoneQueueWaiter;
|
---|
226 |
|
---|
227 | /** Whether to signal the reset semaphore when the current request completes. */
|
---|
228 | bool fSignalResetSem;
|
---|
229 | /** Semaphore usbMsdUsbReset waits on when a request is executing at reset
|
---|
230 | * time. Only signalled when fSignalResetSem is set. */
|
---|
231 | RTSEMEVENTMULTI hEvtReset;
|
---|
232 | /** The reset URB.
|
---|
233 | * This is waiting for SCSI request completion before finishing the reset. */
|
---|
234 | PVUSBURB pResetUrb;
|
---|
235 | /** Indicates that PDMUsbHlpAsyncNotificationCompleted should be called when
|
---|
236 | * the MSD is entering the idle state. */
|
---|
237 | volatile bool fSignalIdle;
|
---|
238 |
|
---|
239 | /** Indicates that this device is a CD-ROM. */
|
---|
240 | bool fIsCdrom;
|
---|
241 |
|
---|
242 | /**
|
---|
243 | * LUN\#0 data.
|
---|
244 | */
|
---|
245 | struct
|
---|
246 | {
|
---|
247 | /** The base interface for LUN\#0. */
|
---|
248 | PDMIBASE IBase;
|
---|
249 | /** The media port interface fo LUN\#0. */
|
---|
250 | PDMIMEDIAPORT IMediaPort;
|
---|
251 | /** The extended media port interface for LUN\#0 */
|
---|
252 | PDMIMEDIAEXPORT IMediaExPort;
|
---|
253 |
|
---|
254 | /** The base interface for the SCSI driver connected to LUN\#0. */
|
---|
255 | PPDMIBASE pIBase;
|
---|
256 | /** The media interface for th SCSI drver conected to LUN\#0. */
|
---|
257 | PPDMIMEDIA pIMedia;
|
---|
258 | /** The extended media inerface for the SCSI driver connected to LUN\#0. */
|
---|
259 | PPDMIMEDIAEX pIMediaEx;
|
---|
260 | } Lun0;
|
---|
261 |
|
---|
262 | } USBMSD;
|
---|
263 | /** Pointer to the USB MSD instance data. */
|
---|
264 | typedef USBMSD *PUSBMSD;
|
---|
265 |
|
---|
266 |
|
---|
267 | /*********************************************************************************************************************************
|
---|
268 | * Global Variables *
|
---|
269 | *********************************************************************************************************************************/
|
---|
270 | static const PDMUSBDESCCACHESTRING g_aUsbMsdStrings_en_US[] =
|
---|
271 | {
|
---|
272 | { USBMSD_STR_ID_MANUFACTURER, "VirtualBox" },
|
---|
273 | { USBMSD_STR_ID_PRODUCT_HD, "USB Harddisk" },
|
---|
274 | { USBMSD_STR_ID_PRODUCT_CDROM, "USB CD-ROM" }
|
---|
275 | };
|
---|
276 |
|
---|
277 | static const PDMUSBDESCCACHELANG g_aUsbMsdLanguages[] =
|
---|
278 | {
|
---|
279 | { 0x0409, RT_ELEMENTS(g_aUsbMsdStrings_en_US), g_aUsbMsdStrings_en_US }
|
---|
280 | };
|
---|
281 |
|
---|
282 | static const VUSBDESCENDPOINTEX g_aUsbMsdEndpointDescsFS[2] =
|
---|
283 | {
|
---|
284 | {
|
---|
285 | {
|
---|
286 | /* .bLength = */ sizeof(VUSBDESCENDPOINT),
|
---|
287 | /* .bDescriptorType = */ VUSB_DT_ENDPOINT,
|
---|
288 | /* .bEndpointAddress = */ 0x81 /* ep=1, in */,
|
---|
289 | /* .bmAttributes = */ 2 /* bulk */,
|
---|
290 | /* .wMaxPacketSize = */ 64 /* maximum possible */,
|
---|
291 | /* .bInterval = */ 0 /* not applicable for bulk EP */
|
---|
292 | },
|
---|
293 | /* .pvMore = */ NULL,
|
---|
294 | /* .pvClass = */ NULL,
|
---|
295 | /* .cbClass = */ 0,
|
---|
296 | /* .pvSsepc = */ NULL,
|
---|
297 | /* .cbSsepc = */ 0
|
---|
298 | },
|
---|
299 | {
|
---|
300 | {
|
---|
301 | /* .bLength = */ sizeof(VUSBDESCENDPOINT),
|
---|
302 | /* .bDescriptorType = */ VUSB_DT_ENDPOINT,
|
---|
303 | /* .bEndpointAddress = */ 0x02 /* ep=2, out */,
|
---|
304 | /* .bmAttributes = */ 2 /* bulk */,
|
---|
305 | /* .wMaxPacketSize = */ 64 /* maximum possible */,
|
---|
306 | /* .bInterval = */ 0 /* not applicable for bulk EP */
|
---|
307 | },
|
---|
308 | /* .pvMore = */ NULL,
|
---|
309 | /* .pvClass = */ NULL,
|
---|
310 | /* .cbClass = */ 0,
|
---|
311 | /* .pvSsepc = */ NULL,
|
---|
312 | /* .cbSsepc = */ 0
|
---|
313 | }
|
---|
314 | };
|
---|
315 |
|
---|
316 | static const VUSBDESCENDPOINTEX g_aUsbMsdEndpointDescsHS[2] =
|
---|
317 | {
|
---|
318 | {
|
---|
319 | {
|
---|
320 | /* .bLength = */ sizeof(VUSBDESCENDPOINT),
|
---|
321 | /* .bDescriptorType = */ VUSB_DT_ENDPOINT,
|
---|
322 | /* .bEndpointAddress = */ 0x81 /* ep=1, in */,
|
---|
323 | /* .bmAttributes = */ 2 /* bulk */,
|
---|
324 | /* .wMaxPacketSize = */ 512 /* HS bulk packet size */,
|
---|
325 | /* .bInterval = */ 0 /* no NAKs */
|
---|
326 | },
|
---|
327 | /* .pvMore = */ NULL,
|
---|
328 | /* .pvClass = */ NULL,
|
---|
329 | /* .cbClass = */ 0,
|
---|
330 | /* .pvSsepc = */ NULL,
|
---|
331 | /* .cbSsepc = */ 0
|
---|
332 | },
|
---|
333 | {
|
---|
334 | {
|
---|
335 | /* .bLength = */ sizeof(VUSBDESCENDPOINT),
|
---|
336 | /* .bDescriptorType = */ VUSB_DT_ENDPOINT,
|
---|
337 | /* .bEndpointAddress = */ 0x02 /* ep=2, out */,
|
---|
338 | /* .bmAttributes = */ 2 /* bulk */,
|
---|
339 | /* .wMaxPacketSize = */ 512 /* HS bulk packet size */,
|
---|
340 | /* .bInterval = */ 0 /* no NAKs */
|
---|
341 | },
|
---|
342 | /* .pvMore = */ NULL,
|
---|
343 | /* .pvClass = */ NULL,
|
---|
344 | /* .cbClass = */ 0,
|
---|
345 | /* .pvSsepc = */ NULL,
|
---|
346 | /* .cbSsepc = */ 0
|
---|
347 | }
|
---|
348 | };
|
---|
349 |
|
---|
350 | static const VUSBDESCSSEPCOMPANION g_aUsbMsdEpCompanionSS =
|
---|
351 | {
|
---|
352 | /* .bLength = */ sizeof(VUSBDESCSSEPCOMPANION),
|
---|
353 | /* .bDescriptorType = */ VUSB_DT_SS_ENDPOINT_COMPANION,
|
---|
354 | /* .bMaxBurst = */ 15 /* we can burst all the way */,
|
---|
355 | /* .bmAttributes = */ 0 /* no streams */,
|
---|
356 | /* .wBytesPerInterval = */ 0 /* not a periodic endpoint */
|
---|
357 | };
|
---|
358 |
|
---|
359 | static const VUSBDESCENDPOINTEX g_aUsbMsdEndpointDescsSS[2] =
|
---|
360 | {
|
---|
361 | {
|
---|
362 | {
|
---|
363 | /* .bLength = */ sizeof(VUSBDESCENDPOINT),
|
---|
364 | /* .bDescriptorType = */ VUSB_DT_ENDPOINT,
|
---|
365 | /* .bEndpointAddress = */ 0x81 /* ep=1, in */,
|
---|
366 | /* .bmAttributes = */ 2 /* bulk */,
|
---|
367 | /* .wMaxPacketSize = */ 1024 /* SS bulk packet size */,
|
---|
368 | /* .bInterval = */ 0 /* no NAKs */
|
---|
369 | },
|
---|
370 | /* .pvMore = */ NULL,
|
---|
371 | /* .pvClass = */ NULL,
|
---|
372 | /* .cbClass = */ 0,
|
---|
373 | /* .pvSsepc = */ &g_aUsbMsdEpCompanionSS,
|
---|
374 | /* .cbSsepc = */ sizeof(g_aUsbMsdEpCompanionSS)
|
---|
375 | },
|
---|
376 | {
|
---|
377 | {
|
---|
378 | /* .bLength = */ sizeof(VUSBDESCENDPOINT),
|
---|
379 | /* .bDescriptorType = */ VUSB_DT_ENDPOINT,
|
---|
380 | /* .bEndpointAddress = */ 0x02 /* ep=2, out */,
|
---|
381 | /* .bmAttributes = */ 2 /* bulk */,
|
---|
382 | /* .wMaxPacketSize = */ 1024 /* SS bulk packet size */,
|
---|
383 | /* .bInterval = */ 0 /* no NAKs */
|
---|
384 | },
|
---|
385 | /* .pvMore = */ NULL,
|
---|
386 | /* .pvClass = */ NULL,
|
---|
387 | /* .cbClass = */ 0,
|
---|
388 | /* .pvSsepc = */ &g_aUsbMsdEpCompanionSS,
|
---|
389 | /* .cbSsepc = */ sizeof(g_aUsbMsdEpCompanionSS)
|
---|
390 | }
|
---|
391 | };
|
---|
392 |
|
---|
393 | static const VUSBDESCINTERFACEEX g_UsbMsdInterfaceDescFS =
|
---|
394 | {
|
---|
395 | {
|
---|
396 | /* .bLength = */ sizeof(VUSBDESCINTERFACE),
|
---|
397 | /* .bDescriptorType = */ VUSB_DT_INTERFACE,
|
---|
398 | /* .bInterfaceNumber = */ 0,
|
---|
399 | /* .bAlternateSetting = */ 0,
|
---|
400 | /* .bNumEndpoints = */ 2,
|
---|
401 | /* .bInterfaceClass = */ 8 /* Mass Storage */,
|
---|
402 | /* .bInterfaceSubClass = */ 6 /* SCSI transparent command set */,
|
---|
403 | /* .bInterfaceProtocol = */ 0x50 /* Bulk-Only Transport */,
|
---|
404 | /* .iInterface = */ 0
|
---|
405 | },
|
---|
406 | /* .pvMore = */ NULL,
|
---|
407 | /* .pvClass = */ NULL,
|
---|
408 | /* .cbClass = */ 0,
|
---|
409 | &g_aUsbMsdEndpointDescsFS[0],
|
---|
410 | /* .pIAD = */ NULL,
|
---|
411 | /* .cbIAD = */ 0
|
---|
412 | };
|
---|
413 |
|
---|
414 | static const VUSBDESCINTERFACEEX g_UsbMsdInterfaceDescHS =
|
---|
415 | {
|
---|
416 | {
|
---|
417 | /* .bLength = */ sizeof(VUSBDESCINTERFACE),
|
---|
418 | /* .bDescriptorType = */ VUSB_DT_INTERFACE,
|
---|
419 | /* .bInterfaceNumber = */ 0,
|
---|
420 | /* .bAlternateSetting = */ 0,
|
---|
421 | /* .bNumEndpoints = */ 2,
|
---|
422 | /* .bInterfaceClass = */ 8 /* Mass Storage */,
|
---|
423 | /* .bInterfaceSubClass = */ 6 /* SCSI transparent command set */,
|
---|
424 | /* .bInterfaceProtocol = */ 0x50 /* Bulk-Only Transport */,
|
---|
425 | /* .iInterface = */ 0
|
---|
426 | },
|
---|
427 | /* .pvMore = */ NULL,
|
---|
428 | /* .pvClass = */ NULL,
|
---|
429 | /* .cbClass = */ 0,
|
---|
430 | &g_aUsbMsdEndpointDescsHS[0],
|
---|
431 | /* .pIAD = */ NULL,
|
---|
432 | /* .cbIAD = */ 0
|
---|
433 | };
|
---|
434 |
|
---|
435 | static const VUSBDESCINTERFACEEX g_UsbMsdInterfaceDescSS =
|
---|
436 | {
|
---|
437 | {
|
---|
438 | /* .bLength = */ sizeof(VUSBDESCINTERFACE),
|
---|
439 | /* .bDescriptorType = */ VUSB_DT_INTERFACE,
|
---|
440 | /* .bInterfaceNumber = */ 0,
|
---|
441 | /* .bAlternateSetting = */ 0,
|
---|
442 | /* .bNumEndpoints = */ 2,
|
---|
443 | /* .bInterfaceClass = */ 8 /* Mass Storage */,
|
---|
444 | /* .bInterfaceSubClass = */ 6 /* SCSI transparent command set */,
|
---|
445 | /* .bInterfaceProtocol = */ 0x50 /* Bulk-Only Transport */,
|
---|
446 | /* .iInterface = */ 0
|
---|
447 | },
|
---|
448 | /* .pvMore = */ NULL,
|
---|
449 | /* .pvClass = */ NULL,
|
---|
450 | /* .cbClass = */ 0,
|
---|
451 | &g_aUsbMsdEndpointDescsSS[0],
|
---|
452 | /* .pIAD = */ NULL,
|
---|
453 | /* .cbIAD = */ 0
|
---|
454 | };
|
---|
455 |
|
---|
456 | static const VUSBINTERFACE g_aUsbMsdInterfacesFS[] =
|
---|
457 | {
|
---|
458 | { &g_UsbMsdInterfaceDescFS, /* .cSettings = */ 1 },
|
---|
459 | };
|
---|
460 |
|
---|
461 | static const VUSBINTERFACE g_aUsbMsdInterfacesHS[] =
|
---|
462 | {
|
---|
463 | { &g_UsbMsdInterfaceDescHS, /* .cSettings = */ 1 },
|
---|
464 | };
|
---|
465 |
|
---|
466 | static const VUSBINTERFACE g_aUsbMsdInterfacesSS[] =
|
---|
467 | {
|
---|
468 | { &g_UsbMsdInterfaceDescSS, /* .cSettings = */ 1 },
|
---|
469 | };
|
---|
470 |
|
---|
471 | static const VUSBDESCCONFIGEX g_UsbMsdConfigDescFS =
|
---|
472 | {
|
---|
473 | {
|
---|
474 | /* .bLength = */ sizeof(VUSBDESCCONFIG),
|
---|
475 | /* .bDescriptorType = */ VUSB_DT_CONFIG,
|
---|
476 | /* .wTotalLength = */ 0 /* recalculated on read */,
|
---|
477 | /* .bNumInterfaces = */ RT_ELEMENTS(g_aUsbMsdInterfacesFS),
|
---|
478 | /* .bConfigurationValue =*/ 1,
|
---|
479 | /* .iConfiguration = */ 0,
|
---|
480 | /* .bmAttributes = */ RT_BIT(7),
|
---|
481 | /* .MaxPower = */ 50 /* 100mA */
|
---|
482 | },
|
---|
483 | NULL, /* pvMore */
|
---|
484 | NULL, /* pvClass */
|
---|
485 | 0, /* cbClass */
|
---|
486 | &g_aUsbMsdInterfacesFS[0],
|
---|
487 | NULL /* pvOriginal */
|
---|
488 | };
|
---|
489 |
|
---|
490 | static const VUSBDESCCONFIGEX g_UsbMsdConfigDescHS =
|
---|
491 | {
|
---|
492 | {
|
---|
493 | /* .bLength = */ sizeof(VUSBDESCCONFIG),
|
---|
494 | /* .bDescriptorType = */ VUSB_DT_CONFIG,
|
---|
495 | /* .wTotalLength = */ 0 /* recalculated on read */,
|
---|
496 | /* .bNumInterfaces = */ RT_ELEMENTS(g_aUsbMsdInterfacesHS),
|
---|
497 | /* .bConfigurationValue =*/ 1,
|
---|
498 | /* .iConfiguration = */ 0,
|
---|
499 | /* .bmAttributes = */ RT_BIT(7),
|
---|
500 | /* .MaxPower = */ 50 /* 100mA */
|
---|
501 | },
|
---|
502 | NULL, /* pvMore */
|
---|
503 | NULL, /* pvClass */
|
---|
504 | 0, /* cbClass */
|
---|
505 | &g_aUsbMsdInterfacesHS[0],
|
---|
506 | NULL /* pvOriginal */
|
---|
507 | };
|
---|
508 |
|
---|
509 | static const VUSBDESCCONFIGEX g_UsbMsdConfigDescSS =
|
---|
510 | {
|
---|
511 | {
|
---|
512 | /* .bLength = */ sizeof(VUSBDESCCONFIG),
|
---|
513 | /* .bDescriptorType = */ VUSB_DT_CONFIG,
|
---|
514 | /* .wTotalLength = */ 0 /* recalculated on read */,
|
---|
515 | /* .bNumInterfaces = */ RT_ELEMENTS(g_aUsbMsdInterfacesSS),
|
---|
516 | /* .bConfigurationValue =*/ 1,
|
---|
517 | /* .iConfiguration = */ 0,
|
---|
518 | /* .bmAttributes = */ RT_BIT(7),
|
---|
519 | /* .MaxPower = */ 50 /* 100mA */
|
---|
520 | },
|
---|
521 | NULL, /* pvMore */
|
---|
522 | NULL, /* pvClass */
|
---|
523 | 0, /* cbClass */
|
---|
524 | &g_aUsbMsdInterfacesSS[0],
|
---|
525 | NULL /* pvOriginal */
|
---|
526 | };
|
---|
527 |
|
---|
528 | static const VUSBDESCDEVICE g_UsbMsdDeviceDesc20 =
|
---|
529 | {
|
---|
530 | /* .bLength = */ sizeof(g_UsbMsdDeviceDesc20),
|
---|
531 | /* .bDescriptorType = */ VUSB_DT_DEVICE,
|
---|
532 | /* .bcdUsb = */ 0x200, /* USB 2.0 */
|
---|
533 | /* .bDeviceClass = */ 0 /* Class specified in the interface desc. */,
|
---|
534 | /* .bDeviceSubClass = */ 0 /* Subclass specified in the interface desc. */,
|
---|
535 | /* .bDeviceProtocol = */ 0 /* Protocol specified in the interface desc. */,
|
---|
536 | /* .bMaxPacketSize0 = */ 64,
|
---|
537 | /* .idVendor = */ VBOX_USB_VENDOR,
|
---|
538 | /* .idProduct = */ USBMSD_PID_HD,
|
---|
539 | /* .bcdDevice = */ 0x0100, /* 1.0 */
|
---|
540 | /* .iManufacturer = */ USBMSD_STR_ID_MANUFACTURER,
|
---|
541 | /* .iProduct = */ USBMSD_STR_ID_PRODUCT_HD,
|
---|
542 | /* .iSerialNumber = */ 0,
|
---|
543 | /* .bNumConfigurations = */ 1
|
---|
544 | };
|
---|
545 |
|
---|
546 | static const VUSBDESCDEVICE g_UsbCdDeviceDesc20 =
|
---|
547 | {
|
---|
548 | /* .bLength = */ sizeof(g_UsbCdDeviceDesc20),
|
---|
549 | /* .bDescriptorType = */ VUSB_DT_DEVICE,
|
---|
550 | /* .bcdUsb = */ 0x200, /* USB 2.0 */
|
---|
551 | /* .bDeviceClass = */ 0 /* Class specified in the interface desc. */,
|
---|
552 | /* .bDeviceSubClass = */ 0 /* Subclass specified in the interface desc. */,
|
---|
553 | /* .bDeviceProtocol = */ 0 /* Protocol specified in the interface desc. */,
|
---|
554 | /* .bMaxPacketSize0 = */ 64,
|
---|
555 | /* .idVendor = */ VBOX_USB_VENDOR,
|
---|
556 | /* .idProduct = */ USBMSD_PID_CD,
|
---|
557 | /* .bcdDevice = */ 0x0100, /* 1.0 */
|
---|
558 | /* .iManufacturer = */ USBMSD_STR_ID_MANUFACTURER,
|
---|
559 | /* .iProduct = */ USBMSD_STR_ID_PRODUCT_CDROM,
|
---|
560 | /* .iSerialNumber = */ 0,
|
---|
561 | /* .bNumConfigurations = */ 1
|
---|
562 | };
|
---|
563 |
|
---|
564 | static const VUSBDESCDEVICE g_UsbMsdDeviceDesc30 =
|
---|
565 | {
|
---|
566 | /* .bLength = */ sizeof(g_UsbMsdDeviceDesc30),
|
---|
567 | /* .bDescriptorType = */ VUSB_DT_DEVICE,
|
---|
568 | /* .bcdUsb = */ 0x300, /* USB 2.0 */
|
---|
569 | /* .bDeviceClass = */ 0 /* Class specified in the interface desc. */,
|
---|
570 | /* .bDeviceSubClass = */ 0 /* Subclass specified in the interface desc. */,
|
---|
571 | /* .bDeviceProtocol = */ 0 /* Protocol specified in the interface desc. */,
|
---|
572 | /* .bMaxPacketSize0 = */ 9 /* 512, the only option for USB3. */,
|
---|
573 | /* .idVendor = */ VBOX_USB_VENDOR,
|
---|
574 | /* .idProduct = */ USBMSD_PID_HD,
|
---|
575 | /* .bcdDevice = */ 0x0110, /* 1.10 */
|
---|
576 | /* .iManufacturer = */ USBMSD_STR_ID_MANUFACTURER,
|
---|
577 | /* .iProduct = */ USBMSD_STR_ID_PRODUCT_HD,
|
---|
578 | /* .iSerialNumber = */ 0,
|
---|
579 | /* .bNumConfigurations = */ 1
|
---|
580 | };
|
---|
581 |
|
---|
582 | static const VUSBDESCDEVICE g_UsbCdDeviceDesc30 =
|
---|
583 | {
|
---|
584 | /* .bLength = */ sizeof(g_UsbCdDeviceDesc30),
|
---|
585 | /* .bDescriptorType = */ VUSB_DT_DEVICE,
|
---|
586 | /* .bcdUsb = */ 0x300, /* USB 2.0 */
|
---|
587 | /* .bDeviceClass = */ 0 /* Class specified in the interface desc. */,
|
---|
588 | /* .bDeviceSubClass = */ 0 /* Subclass specified in the interface desc. */,
|
---|
589 | /* .bDeviceProtocol = */ 0 /* Protocol specified in the interface desc. */,
|
---|
590 | /* .bMaxPacketSize0 = */ 9 /* 512, the only option for USB3. */,
|
---|
591 | /* .idVendor = */ VBOX_USB_VENDOR,
|
---|
592 | /* .idProduct = */ USBMSD_PID_CD,
|
---|
593 | /* .bcdDevice = */ 0x0110, /* 1.10 */
|
---|
594 | /* .iManufacturer = */ USBMSD_STR_ID_MANUFACTURER,
|
---|
595 | /* .iProduct = */ USBMSD_STR_ID_PRODUCT_CDROM,
|
---|
596 | /* .iSerialNumber = */ 0,
|
---|
597 | /* .bNumConfigurations = */ 1
|
---|
598 | };
|
---|
599 |
|
---|
600 | static const VUSBDEVICEQUALIFIER g_UsbMsdDeviceQualifier =
|
---|
601 | {
|
---|
602 | /* .bLength = */ sizeof(g_UsbMsdDeviceQualifier),
|
---|
603 | /* .bDescriptorType = */ VUSB_DT_DEVICE_QUALIFIER,
|
---|
604 | /* .bcdUsb = */ 0x200, /* USB 2.0 */
|
---|
605 | /* .bDeviceClass = */ 0 /* Class specified in the interface desc. */,
|
---|
606 | /* .bDeviceSubClass = */ 0 /* Subclass specified in the interface desc. */,
|
---|
607 | /* .bDeviceProtocol = */ 0 /* Protocol specified in the interface desc. */,
|
---|
608 | /* .bMaxPacketSize0 = */ 64,
|
---|
609 | /* .bNumConfigurations = */ 1,
|
---|
610 | /* .bReserved = */ 0
|
---|
611 | };
|
---|
612 |
|
---|
613 | static const struct {
|
---|
614 | VUSBDESCBOS bos;
|
---|
615 | VUSBDESCSSDEVCAP sscap;
|
---|
616 | } g_UsbMsdBOS =
|
---|
617 | {
|
---|
618 | {
|
---|
619 | /* .bLength = */ sizeof(g_UsbMsdBOS.bos),
|
---|
620 | /* .bDescriptorType = */ VUSB_DT_BOS,
|
---|
621 | /* .wTotalLength = */ sizeof(g_UsbMsdBOS),
|
---|
622 | /* .bNumDeviceCaps = */ 1
|
---|
623 | },
|
---|
624 | {
|
---|
625 | /* .bLength = */ sizeof(VUSBDESCSSDEVCAP),
|
---|
626 | /* .bDescriptorType = */ VUSB_DT_DEVICE_CAPABILITY,
|
---|
627 | /* .bDevCapabilityType = */ VUSB_DCT_SUPERSPEED_USB,
|
---|
628 | /* .bmAttributes = */ 0 /* No LTM. */,
|
---|
629 | /* .wSpeedsSupported = */ 0xe /* Any speed is good. */,
|
---|
630 | /* .bFunctionalitySupport = */ 2 /* Want HS at least. */,
|
---|
631 | /* .bU1DevExitLat = */ 0, /* We are blazingly fast. */
|
---|
632 | /* .wU2DevExitLat = */ 0
|
---|
633 | }
|
---|
634 | };
|
---|
635 |
|
---|
636 | static const PDMUSBDESCCACHE g_UsbMsdDescCacheFS =
|
---|
637 | {
|
---|
638 | /* .pDevice = */ &g_UsbMsdDeviceDesc20,
|
---|
639 | /* .paConfigs = */ &g_UsbMsdConfigDescFS,
|
---|
640 | /* .paLanguages = */ g_aUsbMsdLanguages,
|
---|
641 | /* .cLanguages = */ RT_ELEMENTS(g_aUsbMsdLanguages),
|
---|
642 | /* .fUseCachedDescriptors = */ true,
|
---|
643 | /* .fUseCachedStringsDescriptors = */ true
|
---|
644 | };
|
---|
645 |
|
---|
646 | static const PDMUSBDESCCACHE g_UsbCdDescCacheFS =
|
---|
647 | {
|
---|
648 | /* .pDevice = */ &g_UsbCdDeviceDesc20,
|
---|
649 | /* .paConfigs = */ &g_UsbMsdConfigDescFS,
|
---|
650 | /* .paLanguages = */ g_aUsbMsdLanguages,
|
---|
651 | /* .cLanguages = */ RT_ELEMENTS(g_aUsbMsdLanguages),
|
---|
652 | /* .fUseCachedDescriptors = */ true,
|
---|
653 | /* .fUseCachedStringsDescriptors = */ true
|
---|
654 | };
|
---|
655 |
|
---|
656 | static const PDMUSBDESCCACHE g_UsbMsdDescCacheHS =
|
---|
657 | {
|
---|
658 | /* .pDevice = */ &g_UsbMsdDeviceDesc20,
|
---|
659 | /* .paConfigs = */ &g_UsbMsdConfigDescHS,
|
---|
660 | /* .paLanguages = */ g_aUsbMsdLanguages,
|
---|
661 | /* .cLanguages = */ RT_ELEMENTS(g_aUsbMsdLanguages),
|
---|
662 | /* .fUseCachedDescriptors = */ true,
|
---|
663 | /* .fUseCachedStringsDescriptors = */ true
|
---|
664 | };
|
---|
665 |
|
---|
666 | static const PDMUSBDESCCACHE g_UsbCdDescCacheHS =
|
---|
667 | {
|
---|
668 | /* .pDevice = */ &g_UsbCdDeviceDesc20,
|
---|
669 | /* .paConfigs = */ &g_UsbMsdConfigDescHS,
|
---|
670 | /* .paLanguages = */ g_aUsbMsdLanguages,
|
---|
671 | /* .cLanguages = */ RT_ELEMENTS(g_aUsbMsdLanguages),
|
---|
672 | /* .fUseCachedDescriptors = */ true,
|
---|
673 | /* .fUseCachedStringsDescriptors = */ true
|
---|
674 | };
|
---|
675 |
|
---|
676 | static const PDMUSBDESCCACHE g_UsbMsdDescCacheSS =
|
---|
677 | {
|
---|
678 | /* .pDevice = */ &g_UsbMsdDeviceDesc30,
|
---|
679 | /* .paConfigs = */ &g_UsbMsdConfigDescSS,
|
---|
680 | /* .paLanguages = */ g_aUsbMsdLanguages,
|
---|
681 | /* .cLanguages = */ RT_ELEMENTS(g_aUsbMsdLanguages),
|
---|
682 | /* .fUseCachedDescriptors = */ true,
|
---|
683 | /* .fUseCachedStringsDescriptors = */ true
|
---|
684 | };
|
---|
685 |
|
---|
686 | static const PDMUSBDESCCACHE g_UsbCdDescCacheSS =
|
---|
687 | {
|
---|
688 | /* .pDevice = */ &g_UsbCdDeviceDesc30,
|
---|
689 | /* .paConfigs = */ &g_UsbMsdConfigDescSS,
|
---|
690 | /* .paLanguages = */ g_aUsbMsdLanguages,
|
---|
691 | /* .cLanguages = */ RT_ELEMENTS(g_aUsbMsdLanguages),
|
---|
692 | /* .fUseCachedDescriptors = */ true,
|
---|
693 | /* .fUseCachedStringsDescriptors = */ true
|
---|
694 | };
|
---|
695 |
|
---|
696 |
|
---|
697 | /*********************************************************************************************************************************
|
---|
698 | * Internal Functions *
|
---|
699 | *********************************************************************************************************************************/
|
---|
700 | static int usbMsdHandleBulkDevToHost(PUSBMSD pThis, PUSBMSDEP pEp, PVUSBURB pUrb);
|
---|
701 |
|
---|
702 |
|
---|
703 | /**
|
---|
704 | * Initializes an URB queue.
|
---|
705 | *
|
---|
706 | * @param pQueue The URB queue.
|
---|
707 | */
|
---|
708 | static void usbMsdQueueInit(PUSBMSDURBQUEUE pQueue)
|
---|
709 | {
|
---|
710 | pQueue->pHead = NULL;
|
---|
711 | pQueue->ppTail = &pQueue->pHead;
|
---|
712 | }
|
---|
713 |
|
---|
714 |
|
---|
715 |
|
---|
716 | /**
|
---|
717 | * Inserts an URB at the end of the queue.
|
---|
718 | *
|
---|
719 | * @param pQueue The URB queue.
|
---|
720 | * @param pUrb The URB to insert.
|
---|
721 | */
|
---|
722 | DECLINLINE(void) usbMsdQueueAddTail(PUSBMSDURBQUEUE pQueue, PVUSBURB pUrb)
|
---|
723 | {
|
---|
724 | pUrb->Dev.pNext = NULL;
|
---|
725 | *pQueue->ppTail = pUrb;
|
---|
726 | pQueue->ppTail = &pUrb->Dev.pNext;
|
---|
727 | }
|
---|
728 |
|
---|
729 |
|
---|
730 | /**
|
---|
731 | * Unlinks the head of the queue and returns it.
|
---|
732 | *
|
---|
733 | * @returns The head entry.
|
---|
734 | * @param pQueue The URB queue.
|
---|
735 | */
|
---|
736 | DECLINLINE(PVUSBURB) usbMsdQueueRemoveHead(PUSBMSDURBQUEUE pQueue)
|
---|
737 | {
|
---|
738 | PVUSBURB pUrb = pQueue->pHead;
|
---|
739 | if (pUrb)
|
---|
740 | {
|
---|
741 | PVUSBURB pNext = pUrb->Dev.pNext;
|
---|
742 | pQueue->pHead = pNext;
|
---|
743 | if (!pNext)
|
---|
744 | pQueue->ppTail = &pQueue->pHead;
|
---|
745 | else
|
---|
746 | pUrb->Dev.pNext = NULL;
|
---|
747 | }
|
---|
748 | return pUrb;
|
---|
749 | }
|
---|
750 |
|
---|
751 |
|
---|
752 | /**
|
---|
753 | * Removes an URB from anywhere in the queue.
|
---|
754 | *
|
---|
755 | * @returns true if found, false if not.
|
---|
756 | * @param pQueue The URB queue.
|
---|
757 | * @param pUrb The URB to remove.
|
---|
758 | */
|
---|
759 | DECLINLINE(bool) usbMsdQueueRemove(PUSBMSDURBQUEUE pQueue, PVUSBURB pUrb)
|
---|
760 | {
|
---|
761 | PVUSBURB pCur = pQueue->pHead;
|
---|
762 | if (pCur == pUrb)
|
---|
763 | pQueue->pHead = pUrb->Dev.pNext;
|
---|
764 | else
|
---|
765 | {
|
---|
766 | while (pCur)
|
---|
767 | {
|
---|
768 | if (pCur->Dev.pNext == pUrb)
|
---|
769 | {
|
---|
770 | pCur->Dev.pNext = pUrb->Dev.pNext;
|
---|
771 | break;
|
---|
772 | }
|
---|
773 | pCur = pCur->Dev.pNext;
|
---|
774 | }
|
---|
775 | if (!pCur)
|
---|
776 | return false;
|
---|
777 | }
|
---|
778 | if (!pUrb->Dev.pNext)
|
---|
779 | pQueue->ppTail = &pQueue->pHead;
|
---|
780 | return true;
|
---|
781 | }
|
---|
782 |
|
---|
783 |
|
---|
784 | #ifdef VBOX_STRICT
|
---|
785 | /**
|
---|
786 | * Checks if the queue is empty or not.
|
---|
787 | *
|
---|
788 | * @returns true if it is, false if it isn't.
|
---|
789 | * @param pQueue The URB queue.
|
---|
790 | */
|
---|
791 | DECLINLINE(bool) usbMsdQueueIsEmpty(PCUSBMSDURBQUEUE pQueue)
|
---|
792 | {
|
---|
793 | return pQueue->pHead == NULL;
|
---|
794 | }
|
---|
795 | #endif /* VBOX_STRICT */
|
---|
796 |
|
---|
797 |
|
---|
798 | /**
|
---|
799 | * Links an URB into the done queue.
|
---|
800 | *
|
---|
801 | * @param pThis The MSD instance.
|
---|
802 | * @param pUrb The URB.
|
---|
803 | */
|
---|
804 | static void usbMsdLinkDone(PUSBMSD pThis, PVUSBURB pUrb)
|
---|
805 | {
|
---|
806 | usbMsdQueueAddTail(&pThis->DoneQueue, pUrb);
|
---|
807 |
|
---|
808 | if (pThis->fHaveDoneQueueWaiter)
|
---|
809 | {
|
---|
810 | int rc = RTSemEventSignal(pThis->hEvtDoneQueue);
|
---|
811 | AssertRC(rc);
|
---|
812 | }
|
---|
813 | }
|
---|
814 |
|
---|
815 |
|
---|
816 |
|
---|
817 |
|
---|
818 | /**
|
---|
819 | * Allocates a new request and does basic init.
|
---|
820 | *
|
---|
821 | * @returns Pointer to the new request. NULL if we're out of memory.
|
---|
822 | * @param pThis The MSD instance.
|
---|
823 | */
|
---|
824 | static PUSBMSDREQ usbMsdReqAlloc(PUSBMSD pThis)
|
---|
825 | {
|
---|
826 | PUSBMSDREQ pReq = NULL;
|
---|
827 | PDMMEDIAEXIOREQ hIoReq = NULL;
|
---|
828 |
|
---|
829 | int rc = pThis->Lun0.pIMediaEx->pfnIoReqAlloc(pThis->Lun0.pIMediaEx, &hIoReq, (void **)&pReq,
|
---|
830 | 0 /* uTag */, PDMIMEDIAEX_F_DEFAULT);
|
---|
831 | if (RT_SUCCESS(rc))
|
---|
832 | {
|
---|
833 | pReq->hIoReq = hIoReq;
|
---|
834 | pReq->enmState = USBMSDREQSTATE_READY;
|
---|
835 | pReq->iScsiReqStatus = 0xff;
|
---|
836 | }
|
---|
837 | else
|
---|
838 | LogRel(("usbMsdReqAlloc: Out of memory (%Rrc)\n", rc));
|
---|
839 |
|
---|
840 | return pReq;
|
---|
841 | }
|
---|
842 |
|
---|
843 |
|
---|
844 | /**
|
---|
845 | * Frees a request.
|
---|
846 | *
|
---|
847 | * @param pThis The MSD instance.
|
---|
848 | * @param pReq The request.
|
---|
849 | */
|
---|
850 | static void usbMsdReqFree(PUSBMSD pThis, PUSBMSDREQ pReq)
|
---|
851 | {
|
---|
852 | /*
|
---|
853 | * Check the input.
|
---|
854 | */
|
---|
855 | AssertReturnVoid( pReq->enmState > USBMSDREQSTATE_INVALID
|
---|
856 | && pReq->enmState != USBMSDREQSTATE_EXECUTING
|
---|
857 | && pReq->enmState < USBMSDREQSTATE_END);
|
---|
858 | PPDMUSBINS pUsbIns = pThis->pUsbIns;
|
---|
859 | AssertPtrReturnVoid(pUsbIns);
|
---|
860 | AssertReturnVoid(PDM_VERSION_ARE_COMPATIBLE(pUsbIns->u32Version, PDM_USBINS_VERSION));
|
---|
861 |
|
---|
862 | /*
|
---|
863 | * Invalidate it and free the associated resources.
|
---|
864 | */
|
---|
865 | pReq->enmState = USBMSDREQSTATE_INVALID;
|
---|
866 | pReq->cbBuf = 0;
|
---|
867 | pReq->offBuf = 0;
|
---|
868 |
|
---|
869 | if (pReq->pbBuf)
|
---|
870 | {
|
---|
871 | PDMUsbHlpMMHeapFree(pUsbIns, pReq->pbBuf);
|
---|
872 | pReq->pbBuf = NULL;
|
---|
873 | }
|
---|
874 |
|
---|
875 | int rc = pThis->Lun0.pIMediaEx->pfnIoReqFree(pThis->Lun0.pIMediaEx, pReq->hIoReq);
|
---|
876 | AssertRC(rc);
|
---|
877 | }
|
---|
878 |
|
---|
879 |
|
---|
880 | /**
|
---|
881 | * Prepares a request for execution or data buffering.
|
---|
882 | *
|
---|
883 | * @param pReq The request.
|
---|
884 | * @param pCbw The SCSI command block wrapper.
|
---|
885 | */
|
---|
886 | static void usbMsdReqPrepare(PUSBMSDREQ pReq, PCUSBCBW pCbw)
|
---|
887 | {
|
---|
888 | /* Copy the CBW */
|
---|
889 | size_t cbCopy = RT_OFFSETOF(USBCBW, CBWCB[pCbw->bCBWCBLength]);
|
---|
890 | memcpy(&pReq->Cbw, pCbw, cbCopy);
|
---|
891 | memset((uint8_t *)&pReq->Cbw + cbCopy, 0, sizeof(pReq->Cbw) - cbCopy);
|
---|
892 |
|
---|
893 | /* Setup the SCSI request. */
|
---|
894 | pReq->offBuf = 0;
|
---|
895 | pReq->iScsiReqStatus = 0xff;
|
---|
896 | }
|
---|
897 |
|
---|
898 |
|
---|
899 | /**
|
---|
900 | * Makes sure that there is sufficient buffer space available.
|
---|
901 | *
|
---|
902 | * @returns Success indicator (true/false)
|
---|
903 | * @param pThis The MSD instance.
|
---|
904 | * @param pReq The request.
|
---|
905 | * @param cbBuf The required buffer space.
|
---|
906 | */
|
---|
907 | static int usbMsdReqEnsureBuffer(PUSBMSD pThis, PUSBMSDREQ pReq, uint32_t cbBuf)
|
---|
908 | {
|
---|
909 | if (RT_LIKELY(pReq->cbBuf >= cbBuf))
|
---|
910 | RT_BZERO(pReq->pbBuf, cbBuf);
|
---|
911 | else
|
---|
912 | {
|
---|
913 | PDMUsbHlpMMHeapFree(pThis->pUsbIns, pReq->pbBuf);
|
---|
914 | pReq->cbBuf = 0;
|
---|
915 |
|
---|
916 | cbBuf = RT_ALIGN_Z(cbBuf, 0x1000);
|
---|
917 | pReq->pbBuf = (uint8_t *)PDMUsbHlpMMHeapAllocZ(pThis->pUsbIns, cbBuf);
|
---|
918 | if (!pReq->pbBuf)
|
---|
919 | return false;
|
---|
920 |
|
---|
921 | pReq->cbBuf = cbBuf;
|
---|
922 | }
|
---|
923 | return true;
|
---|
924 | }
|
---|
925 |
|
---|
926 |
|
---|
927 | /**
|
---|
928 | * Completes the URB with a stalled state, halting the pipe.
|
---|
929 | */
|
---|
930 | static int usbMsdCompleteStall(PUSBMSD pThis, PUSBMSDEP pEp, PVUSBURB pUrb, const char *pszWhy)
|
---|
931 | {
|
---|
932 | RT_NOREF(pszWhy);
|
---|
933 | Log(("usbMsdCompleteStall/#%u: pUrb=%p:%s: %s\n", pThis->pUsbIns->iInstance, pUrb, pUrb->pszDesc, pszWhy));
|
---|
934 |
|
---|
935 | pUrb->enmStatus = VUSBSTATUS_STALL;
|
---|
936 |
|
---|
937 | /** @todo figure out if the stall is global or pipe-specific or both. */
|
---|
938 | if (pEp)
|
---|
939 | pEp->fHalted = true;
|
---|
940 | else
|
---|
941 | {
|
---|
942 | pThis->aEps[1].fHalted = true;
|
---|
943 | pThis->aEps[2].fHalted = true;
|
---|
944 | }
|
---|
945 |
|
---|
946 | usbMsdLinkDone(pThis, pUrb);
|
---|
947 | return VINF_SUCCESS;
|
---|
948 | }
|
---|
949 |
|
---|
950 |
|
---|
951 | /**
|
---|
952 | * Completes the URB with a OK state.
|
---|
953 | */
|
---|
954 | static int usbMsdCompleteOk(PUSBMSD pThis, PVUSBURB pUrb, size_t cbData)
|
---|
955 | {
|
---|
956 | Log(("usbMsdCompleteOk/#%u: pUrb=%p:%s cbData=%#zx\n", pThis->pUsbIns->iInstance, pUrb, pUrb->pszDesc, cbData));
|
---|
957 |
|
---|
958 | pUrb->enmStatus = VUSBSTATUS_OK;
|
---|
959 | pUrb->cbData = (uint32_t)cbData;
|
---|
960 |
|
---|
961 | usbMsdLinkDone(pThis, pUrb);
|
---|
962 | return VINF_SUCCESS;
|
---|
963 | }
|
---|
964 |
|
---|
965 |
|
---|
966 | /**
|
---|
967 | * Reset worker for usbMsdUsbReset, usbMsdUsbSetConfiguration and
|
---|
968 | * usbMsdUrbHandleDefaultPipe.
|
---|
969 | *
|
---|
970 | * @returns VBox status code.
|
---|
971 | * @param pThis The MSD instance.
|
---|
972 | * @param pUrb Set when usbMsdUrbHandleDefaultPipe is the
|
---|
973 | * caller.
|
---|
974 | * @param fSetConfig Set when usbMsdUsbSetConfiguration is the
|
---|
975 | * caller.
|
---|
976 | */
|
---|
977 | static int usbMsdResetWorker(PUSBMSD pThis, PVUSBURB pUrb, bool fSetConfig)
|
---|
978 | {
|
---|
979 | /*
|
---|
980 | * Wait for the any command currently executing to complete before
|
---|
981 | * resetting. (We cannot cancel its execution.) How we do this depends
|
---|
982 | * on the reset method.
|
---|
983 | */
|
---|
984 | PUSBMSDREQ pReq = pThis->pReq;
|
---|
985 | if ( pReq
|
---|
986 | && pReq->enmState == USBMSDREQSTATE_EXECUTING)
|
---|
987 | {
|
---|
988 | /* Don't try to deal with the set config variant nor multiple build-only
|
---|
989 | mass storage resets. */
|
---|
990 | if (pThis->pResetUrb && (pUrb || fSetConfig))
|
---|
991 | {
|
---|
992 | Log(("usbMsdResetWorker: pResetUrb is already %p:%s - stalling\n", pThis->pResetUrb, pThis->pResetUrb->pszDesc));
|
---|
993 | return usbMsdCompleteStall(pThis, NULL, pUrb, "pResetUrb");
|
---|
994 | }
|
---|
995 |
|
---|
996 | /* Bulk-Only Mass Storage Reset: Complete the reset on request completion. */
|
---|
997 | if (pUrb)
|
---|
998 | {
|
---|
999 | pThis->pResetUrb = pUrb;
|
---|
1000 | Log(("usbMsdResetWorker: Setting pResetUrb to %p:%s\n", pThis->pResetUrb, pThis->pResetUrb->pszDesc));
|
---|
1001 | return VINF_SUCCESS;
|
---|
1002 | }
|
---|
1003 |
|
---|
1004 | /* Device reset: Wait for up to 10 ms. If it doesn't work, ditch
|
---|
1005 | whole the request structure. We'll allocate a new one when needed. */
|
---|
1006 | Log(("usbMsdResetWorker: Waiting for completion...\n"));
|
---|
1007 | Assert(!pThis->fSignalResetSem);
|
---|
1008 | pThis->fSignalResetSem = true;
|
---|
1009 | RTSemEventMultiReset(pThis->hEvtReset);
|
---|
1010 | RTCritSectLeave(&pThis->CritSect);
|
---|
1011 |
|
---|
1012 | int rc = RTSemEventMultiWait(pThis->hEvtReset, 10 /*ms*/);
|
---|
1013 |
|
---|
1014 | RTCritSectEnter(&pThis->CritSect);
|
---|
1015 | pThis->fSignalResetSem = false;
|
---|
1016 | if ( RT_FAILURE(rc)
|
---|
1017 | || pReq->enmState == USBMSDREQSTATE_EXECUTING)
|
---|
1018 | {
|
---|
1019 | Log(("usbMsdResetWorker: Didn't complete, ditching the current request (%p)!\n", pReq));
|
---|
1020 | Assert(pReq == pThis->pReq);
|
---|
1021 | pReq->enmState = USBMSDREQSTATE_DESTROY_ON_COMPLETION;
|
---|
1022 | pThis->pReq = NULL;
|
---|
1023 | pReq = NULL;
|
---|
1024 | }
|
---|
1025 | }
|
---|
1026 |
|
---|
1027 | /*
|
---|
1028 | * Reset the request and device state.
|
---|
1029 | */
|
---|
1030 | if (pReq)
|
---|
1031 | {
|
---|
1032 | pReq->enmState = USBMSDREQSTATE_READY;
|
---|
1033 | pReq->iScsiReqStatus = 0xff;
|
---|
1034 | }
|
---|
1035 |
|
---|
1036 | for (unsigned i = 0; i < RT_ELEMENTS(pThis->aEps); i++)
|
---|
1037 | pThis->aEps[i].fHalted = false;
|
---|
1038 |
|
---|
1039 | if (!pUrb && !fSetConfig) /* (only device reset) */
|
---|
1040 | pThis->bConfigurationValue = 0; /* default */
|
---|
1041 |
|
---|
1042 | /*
|
---|
1043 | * Ditch all pending URBs.
|
---|
1044 | */
|
---|
1045 | PVUSBURB pCurUrb;
|
---|
1046 | while ((pCurUrb = usbMsdQueueRemoveHead(&pThis->ToHostQueue)) != NULL)
|
---|
1047 | {
|
---|
1048 | pCurUrb->enmStatus = VUSBSTATUS_CRC;
|
---|
1049 | usbMsdLinkDone(pThis, pCurUrb);
|
---|
1050 | }
|
---|
1051 |
|
---|
1052 | pCurUrb = pThis->pResetUrb;
|
---|
1053 | if (pCurUrb)
|
---|
1054 | {
|
---|
1055 | pThis->pResetUrb = NULL;
|
---|
1056 | pCurUrb->enmStatus = VUSBSTATUS_CRC;
|
---|
1057 | usbMsdLinkDone(pThis, pCurUrb);
|
---|
1058 | }
|
---|
1059 |
|
---|
1060 | if (pUrb)
|
---|
1061 | return usbMsdCompleteOk(pThis, pUrb, 0);
|
---|
1062 | return VINF_SUCCESS;
|
---|
1063 | }
|
---|
1064 |
|
---|
1065 |
|
---|
1066 | /**
|
---|
1067 | * Process a completed request.
|
---|
1068 | *
|
---|
1069 | * @returns nothing.
|
---|
1070 | * @param pThis The MSD instance.
|
---|
1071 | * @param pReq The request.
|
---|
1072 | * @param rcReq The completion status.
|
---|
1073 | */
|
---|
1074 | static void usbMsdReqComplete(PUSBMSD pThis, PUSBMSDREQ pReq, int rcReq)
|
---|
1075 | {
|
---|
1076 | RT_NOREF1(rcReq);
|
---|
1077 |
|
---|
1078 | Log(("usbMsdLun0IoReqCompleteNotify: pReq=%p dCBWTag=%#x iScsiReqStatus=%u \n", pReq, pReq->Cbw.dCBWTag, pReq->iScsiReqStatus));
|
---|
1079 | RTCritSectEnter(&pThis->CritSect);
|
---|
1080 |
|
---|
1081 | if (pReq->enmState != USBMSDREQSTATE_DESTROY_ON_COMPLETION)
|
---|
1082 | {
|
---|
1083 | Assert(pReq->enmState == USBMSDREQSTATE_EXECUTING);
|
---|
1084 | Assert(pThis->pReq == pReq);
|
---|
1085 |
|
---|
1086 | /*
|
---|
1087 | * Advance the state machine. The state machine is not affected by
|
---|
1088 | * SCSI errors.
|
---|
1089 | */
|
---|
1090 | if ((pReq->Cbw.bmCBWFlags & USBCBW_DIR_MASK) == USBCBW_DIR_OUT)
|
---|
1091 | {
|
---|
1092 | pReq->enmState = USBMSDREQSTATE_STATUS;
|
---|
1093 | Log(("usbMsdLun0IoReqCompleteNotify: Entering STATUS\n"));
|
---|
1094 | }
|
---|
1095 | else
|
---|
1096 | {
|
---|
1097 | pReq->enmState = USBMSDREQSTATE_DATA_TO_HOST;
|
---|
1098 | Log(("usbMsdLun0IoReqCompleteNotify: Entering DATA_TO_HOST\n"));
|
---|
1099 | }
|
---|
1100 |
|
---|
1101 | /*
|
---|
1102 | * Deal with pending to-host URBs.
|
---|
1103 | */
|
---|
1104 | for (;;)
|
---|
1105 | {
|
---|
1106 | PVUSBURB pUrb = usbMsdQueueRemoveHead(&pThis->ToHostQueue);
|
---|
1107 | if (!pUrb)
|
---|
1108 | break;
|
---|
1109 |
|
---|
1110 | /* Process it the normal way. */
|
---|
1111 | usbMsdHandleBulkDevToHost(pThis, &pThis->aEps[1], pUrb);
|
---|
1112 | }
|
---|
1113 | }
|
---|
1114 | else
|
---|
1115 | {
|
---|
1116 | Log(("usbMsdLun0IoReqCompleteNotify: freeing %p\n", pReq));
|
---|
1117 | usbMsdReqFree(pThis, pReq);
|
---|
1118 | }
|
---|
1119 |
|
---|
1120 | if (pThis->fSignalResetSem)
|
---|
1121 | RTSemEventMultiSignal(pThis->hEvtReset);
|
---|
1122 |
|
---|
1123 | if (pThis->pResetUrb)
|
---|
1124 | {
|
---|
1125 | pThis->pResetUrb = NULL;
|
---|
1126 | usbMsdResetWorker(pThis, pThis->pResetUrb, false /*fSetConfig*/);
|
---|
1127 | }
|
---|
1128 |
|
---|
1129 | RTCritSectLeave(&pThis->CritSect);
|
---|
1130 | }
|
---|
1131 |
|
---|
1132 |
|
---|
1133 | /**
|
---|
1134 | * @interface_method_impl{PDMIMEDIAEXPORT,pfnIoReqCopyFromBuf}
|
---|
1135 | */
|
---|
1136 | static DECLCALLBACK(int) usbMsdLun0IoReqCopyFromBuf(PPDMIMEDIAEXPORT pInterface, PDMMEDIAEXIOREQ hIoReq,
|
---|
1137 | void *pvIoReqAlloc, uint32_t offDst, PRTSGBUF pSgBuf,
|
---|
1138 | size_t cbCopy)
|
---|
1139 | {
|
---|
1140 | RT_NOREF2(pInterface, hIoReq);
|
---|
1141 | int rc = VINF_SUCCESS;
|
---|
1142 | PUSBMSDREQ pReq = (PUSBMSDREQ)pvIoReqAlloc;
|
---|
1143 |
|
---|
1144 | if (RT_UNLIKELY(offDst + cbCopy > pReq->cbBuf))
|
---|
1145 | rc = VERR_PDM_MEDIAEX_IOBUF_OVERFLOW;
|
---|
1146 | else
|
---|
1147 | {
|
---|
1148 | size_t cbCopied = RTSgBufCopyToBuf(pSgBuf, pReq->pbBuf + offDst, cbCopy);
|
---|
1149 | Assert(cbCopied == cbCopy); RT_NOREF(cbCopied);
|
---|
1150 | }
|
---|
1151 |
|
---|
1152 | return rc;
|
---|
1153 | }
|
---|
1154 |
|
---|
1155 |
|
---|
1156 | /**
|
---|
1157 | * @interface_method_impl{PDMIMEDIAEXPORT,pfnIoReqCopyToBuf}
|
---|
1158 | */
|
---|
1159 | static DECLCALLBACK(int) usbMsdLun0IoReqCopyToBuf(PPDMIMEDIAEXPORT pInterface, PDMMEDIAEXIOREQ hIoReq,
|
---|
1160 | void *pvIoReqAlloc, uint32_t offSrc, PRTSGBUF pSgBuf,
|
---|
1161 | size_t cbCopy)
|
---|
1162 | {
|
---|
1163 | RT_NOREF2(pInterface, hIoReq);
|
---|
1164 | int rc = VINF_SUCCESS;
|
---|
1165 | PUSBMSDREQ pReq = (PUSBMSDREQ)pvIoReqAlloc;
|
---|
1166 |
|
---|
1167 | if (RT_UNLIKELY(offSrc + cbCopy > pReq->cbBuf))
|
---|
1168 | rc = VERR_PDM_MEDIAEX_IOBUF_UNDERRUN;
|
---|
1169 | else
|
---|
1170 | {
|
---|
1171 | size_t cbCopied = RTSgBufCopyFromBuf(pSgBuf, pReq->pbBuf + offSrc, cbCopy);
|
---|
1172 | Assert(cbCopied == cbCopy); RT_NOREF(cbCopied);
|
---|
1173 | }
|
---|
1174 |
|
---|
1175 | return rc;
|
---|
1176 | }
|
---|
1177 |
|
---|
1178 |
|
---|
1179 | /**
|
---|
1180 | * @interface_method_impl{PDMIMEDIAEXPORT,pfnIoReqCompleteNotify}
|
---|
1181 | */
|
---|
1182 | static DECLCALLBACK(int) usbMsdLun0IoReqCompleteNotify(PPDMIMEDIAEXPORT pInterface, PDMMEDIAEXIOREQ hIoReq,
|
---|
1183 | void *pvIoReqAlloc, int rcReq)
|
---|
1184 | {
|
---|
1185 | RT_NOREF1(hIoReq);
|
---|
1186 | PUSBMSD pThis = RT_FROM_MEMBER(pInterface, USBMSD, Lun0.IMediaExPort);
|
---|
1187 | PUSBMSDREQ pReq = (PUSBMSDREQ)pvIoReqAlloc;
|
---|
1188 |
|
---|
1189 | usbMsdReqComplete(pThis, pReq, rcReq);
|
---|
1190 | return VINF_SUCCESS;
|
---|
1191 | }
|
---|
1192 |
|
---|
1193 |
|
---|
1194 | /**
|
---|
1195 | * @interface_method_impl{PDMIMEDIAEXPORT,pfnIoReqStateChanged}
|
---|
1196 | */
|
---|
1197 | static DECLCALLBACK(void) usbMsdLun0IoReqStateChanged(PPDMIMEDIAEXPORT pInterface, PDMMEDIAEXIOREQ hIoReq,
|
---|
1198 | void *pvIoReqAlloc, PDMMEDIAEXIOREQSTATE enmState)
|
---|
1199 | {
|
---|
1200 | RT_NOREF4(pInterface, hIoReq, pvIoReqAlloc, enmState);
|
---|
1201 | AssertLogRelMsgFailed(("This should not be hit because I/O requests should not be suspended\n"));
|
---|
1202 | }
|
---|
1203 |
|
---|
1204 |
|
---|
1205 | /**
|
---|
1206 | * @interface_method_impl{PDMIMEDIAEXPORT,pfnMediumEjected}
|
---|
1207 | */
|
---|
1208 | static DECLCALLBACK(void) usbMsdLun0MediumEjected(PPDMIMEDIAEXPORT pInterface)
|
---|
1209 | {
|
---|
1210 | RT_NOREF1(pInterface); /** @todo */
|
---|
1211 | }
|
---|
1212 |
|
---|
1213 |
|
---|
1214 | /**
|
---|
1215 | * @interface_method_impl{PDMIMEDIAPORT,pfnQueryDeviceLocation}
|
---|
1216 | */
|
---|
1217 | static DECLCALLBACK(int) usbMsdLun0QueryDeviceLocation(PPDMIMEDIAPORT pInterface, const char **ppcszController,
|
---|
1218 | uint32_t *piInstance, uint32_t *piLUN)
|
---|
1219 | {
|
---|
1220 | PUSBMSD pThis = RT_FROM_MEMBER(pInterface, USBMSD, Lun0.IMediaPort);
|
---|
1221 | PPDMUSBINS pUsbIns = pThis->pUsbIns;
|
---|
1222 |
|
---|
1223 | AssertPtrReturn(ppcszController, VERR_INVALID_POINTER);
|
---|
1224 | AssertPtrReturn(piInstance, VERR_INVALID_POINTER);
|
---|
1225 | AssertPtrReturn(piLUN, VERR_INVALID_POINTER);
|
---|
1226 |
|
---|
1227 | *ppcszController = pUsbIns->pReg->szName;
|
---|
1228 | *piInstance = pUsbIns->iInstance;
|
---|
1229 | *piLUN = 0;
|
---|
1230 |
|
---|
1231 | return VINF_SUCCESS;
|
---|
1232 | }
|
---|
1233 |
|
---|
1234 |
|
---|
1235 | /**
|
---|
1236 | * @interface_method_impl{PDMIBASE,pfnQueryInterface}
|
---|
1237 | */
|
---|
1238 | static DECLCALLBACK(void *) usbMsdLun0QueryInterface(PPDMIBASE pInterface, const char *pszIID)
|
---|
1239 | {
|
---|
1240 | PUSBMSD pThis = RT_FROM_MEMBER(pInterface, USBMSD, Lun0.IBase);
|
---|
1241 | PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pThis->Lun0.IBase);
|
---|
1242 | PDMIBASE_RETURN_INTERFACE(pszIID, PDMIMEDIAPORT, &pThis->Lun0.IMediaPort);
|
---|
1243 | PDMIBASE_RETURN_INTERFACE(pszIID, PDMIMEDIAEXPORT, &pThis->Lun0.IMediaExPort);
|
---|
1244 | return NULL;
|
---|
1245 | }
|
---|
1246 |
|
---|
1247 |
|
---|
1248 | /**
|
---|
1249 | * Checks if all asynchronous I/O is finished.
|
---|
1250 | *
|
---|
1251 | * Used by usbMsdVMReset, usbMsdVMSuspend and usbMsdVMPowerOff.
|
---|
1252 | *
|
---|
1253 | * @returns true if quiesced, false if busy.
|
---|
1254 | * @param pUsbIns The USB device instance.
|
---|
1255 | */
|
---|
1256 | static bool usbMsdAllAsyncIOIsFinished(PPDMUSBINS pUsbIns)
|
---|
1257 | {
|
---|
1258 | PUSBMSD pThis = PDMINS_2_DATA(pUsbIns, PUSBMSD);
|
---|
1259 |
|
---|
1260 | if ( VALID_PTR(pThis->pReq)
|
---|
1261 | && pThis->pReq->enmState == USBMSDREQSTATE_EXECUTING)
|
---|
1262 | return false;
|
---|
1263 |
|
---|
1264 | return true;
|
---|
1265 | }
|
---|
1266 |
|
---|
1267 | /**
|
---|
1268 | * @callback_method_impl{FNPDMDEVASYNCNOTIFY,
|
---|
1269 | * Callback employed by usbMsdVMSuspend and usbMsdVMPowerOff.}
|
---|
1270 | */
|
---|
1271 | static DECLCALLBACK(bool) usbMsdIsAsyncSuspendOrPowerOffDone(PPDMUSBINS pUsbIns)
|
---|
1272 | {
|
---|
1273 | if (!usbMsdAllAsyncIOIsFinished(pUsbIns))
|
---|
1274 | return false;
|
---|
1275 |
|
---|
1276 | PUSBMSD pThis = PDMINS_2_DATA(pUsbIns, PUSBMSD);
|
---|
1277 | ASMAtomicWriteBool(&pThis->fSignalIdle, false);
|
---|
1278 | return true;
|
---|
1279 | }
|
---|
1280 |
|
---|
1281 | /**
|
---|
1282 | * Common worker for usbMsdVMSuspend and usbMsdVMPowerOff.
|
---|
1283 | */
|
---|
1284 | static void usbMsdSuspendOrPowerOff(PPDMUSBINS pUsbIns)
|
---|
1285 | {
|
---|
1286 | PUSBMSD pThis = PDMINS_2_DATA(pUsbIns, PUSBMSD);
|
---|
1287 |
|
---|
1288 | ASMAtomicWriteBool(&pThis->fSignalIdle, true);
|
---|
1289 | if (!usbMsdAllAsyncIOIsFinished(pUsbIns))
|
---|
1290 | PDMUsbHlpSetAsyncNotification(pUsbIns, usbMsdIsAsyncSuspendOrPowerOffDone);
|
---|
1291 | else
|
---|
1292 | {
|
---|
1293 | ASMAtomicWriteBool(&pThis->fSignalIdle, false);
|
---|
1294 |
|
---|
1295 | if (pThis->pReq)
|
---|
1296 | {
|
---|
1297 | usbMsdReqFree(pThis, pThis->pReq);
|
---|
1298 | pThis->pReq = NULL;
|
---|
1299 | }
|
---|
1300 | }
|
---|
1301 | }
|
---|
1302 |
|
---|
1303 |
|
---|
1304 | /* -=-=-=-=- Saved State -=-=-=-=- */
|
---|
1305 |
|
---|
1306 | /**
|
---|
1307 | * @callback_method_impl{FNSSMUSBSAVEPREP}
|
---|
1308 | */
|
---|
1309 | static DECLCALLBACK(int) usbMsdSavePrep(PPDMUSBINS pUsbIns, PSSMHANDLE pSSM)
|
---|
1310 | {
|
---|
1311 | RT_NOREF(pSSM);
|
---|
1312 | #ifdef VBOX_STRICT
|
---|
1313 | PUSBMSD pThis = PDMINS_2_DATA(pUsbIns, PUSBMSD);
|
---|
1314 | Assert(usbMsdAllAsyncIOIsFinished(pUsbIns));
|
---|
1315 | Assert(usbMsdQueueIsEmpty(&pThis->ToHostQueue));
|
---|
1316 | Assert(usbMsdQueueIsEmpty(&pThis->DoneQueue));
|
---|
1317 | #else
|
---|
1318 | RT_NOREF(pUsbIns);
|
---|
1319 | #endif
|
---|
1320 | return VINF_SUCCESS;
|
---|
1321 | }
|
---|
1322 |
|
---|
1323 | /**
|
---|
1324 | * @callback_method_impl{FNSSMUSBLOADPREP}
|
---|
1325 | */
|
---|
1326 | static DECLCALLBACK(int) usbMsdLoadPrep(PPDMUSBINS pUsbIns, PSSMHANDLE pSSM)
|
---|
1327 | {
|
---|
1328 | RT_NOREF(pSSM);
|
---|
1329 | #ifdef VBOX_STRICT
|
---|
1330 | PUSBMSD pThis = PDMINS_2_DATA(pUsbIns, PUSBMSD);
|
---|
1331 | Assert(usbMsdAllAsyncIOIsFinished(pUsbIns));
|
---|
1332 | Assert(usbMsdQueueIsEmpty(&pThis->ToHostQueue));
|
---|
1333 | Assert(usbMsdQueueIsEmpty(&pThis->DoneQueue));
|
---|
1334 | #else
|
---|
1335 | RT_NOREF(pUsbIns);
|
---|
1336 | #endif
|
---|
1337 | return VINF_SUCCESS;
|
---|
1338 | }
|
---|
1339 |
|
---|
1340 | /**
|
---|
1341 | * @callback_method_impl{FNSSMUSBLIVEEXEC}
|
---|
1342 | */
|
---|
1343 | static DECLCALLBACK(int) usbMsdLiveExec(PPDMUSBINS pUsbIns, PSSMHANDLE pSSM, uint32_t uPass)
|
---|
1344 | {
|
---|
1345 | RT_NOREF(uPass);
|
---|
1346 | PUSBMSD pThis = PDMINS_2_DATA(pUsbIns, PUSBMSD);
|
---|
1347 |
|
---|
1348 | /* config. */
|
---|
1349 | SSMR3PutBool(pSSM, pThis->Lun0.pIBase != NULL);
|
---|
1350 | return VINF_SSM_DONT_CALL_AGAIN;
|
---|
1351 | }
|
---|
1352 |
|
---|
1353 | /**
|
---|
1354 | * @callback_method_impl{FNSSMUSBSAVEEXEC}
|
---|
1355 | */
|
---|
1356 | static DECLCALLBACK(int) usbMsdSaveExec(PPDMUSBINS pUsbIns, PSSMHANDLE pSSM)
|
---|
1357 | {
|
---|
1358 | PUSBMSD pThis = PDMINS_2_DATA(pUsbIns, PUSBMSD);
|
---|
1359 | int rc;
|
---|
1360 |
|
---|
1361 | /* The config */
|
---|
1362 | rc = usbMsdLiveExec(pUsbIns, pSSM, SSM_PASS_FINAL);
|
---|
1363 | AssertRCReturn(rc, rc);
|
---|
1364 |
|
---|
1365 | SSMR3PutU8(pSSM, pThis->bConfigurationValue);
|
---|
1366 | SSMR3PutBool(pSSM, pThis->aEps[0].fHalted);
|
---|
1367 | SSMR3PutBool(pSSM, pThis->aEps[1].fHalted);
|
---|
1368 | SSMR3PutBool(pSSM, pThis->aEps[2].fHalted);
|
---|
1369 | SSMR3PutBool(pSSM, pThis->pReq != NULL);
|
---|
1370 |
|
---|
1371 | if (pThis->pReq)
|
---|
1372 | {
|
---|
1373 | PUSBMSDREQ pReq = pThis->pReq;
|
---|
1374 |
|
---|
1375 | SSMR3PutU32(pSSM, pReq->enmState);
|
---|
1376 | SSMR3PutU32(pSSM, pReq->cbBuf);
|
---|
1377 | if (pReq->cbBuf)
|
---|
1378 | {
|
---|
1379 | AssertPtr(pReq->pbBuf);
|
---|
1380 | SSMR3PutMem(pSSM, pReq->pbBuf, pReq->cbBuf);
|
---|
1381 | }
|
---|
1382 |
|
---|
1383 | SSMR3PutU32(pSSM, pReq->offBuf);
|
---|
1384 | SSMR3PutMem(pSSM, &pReq->Cbw, sizeof(pReq->Cbw));
|
---|
1385 | SSMR3PutU8(pSSM, pReq->iScsiReqStatus);
|
---|
1386 | }
|
---|
1387 |
|
---|
1388 | return SSMR3PutU32(pSSM, UINT32_MAX); /* sanity/terminator */
|
---|
1389 | }
|
---|
1390 |
|
---|
1391 | /**
|
---|
1392 | * @callback_method_impl{FNSSMUSBLOADEXEC}
|
---|
1393 | */
|
---|
1394 | static DECLCALLBACK(int) usbMsdLoadExec(PPDMUSBINS pUsbIns, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass)
|
---|
1395 | {
|
---|
1396 | PUSBMSD pThis = PDMINS_2_DATA(pUsbIns, PUSBMSD);
|
---|
1397 | uint32_t u32;
|
---|
1398 | int rc;
|
---|
1399 |
|
---|
1400 | if (uVersion > USB_MSD_SAVED_STATE_VERSION)
|
---|
1401 | return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
|
---|
1402 |
|
---|
1403 | /* Verify config. */
|
---|
1404 | bool fInUse;
|
---|
1405 | rc = SSMR3GetBool(pSSM, &fInUse);
|
---|
1406 | AssertRCReturn(rc, rc);
|
---|
1407 | if (fInUse != (pThis->Lun0.pIBase != NULL))
|
---|
1408 | return SSMR3SetCfgError(pSSM, RT_SRC_POS,
|
---|
1409 | N_("The %s VM is missing a USB mass storage device. Please make sure the source and target VMs have compatible storage configurations"),
|
---|
1410 | fInUse ? "target" : "source");
|
---|
1411 |
|
---|
1412 | if (uPass == SSM_PASS_FINAL)
|
---|
1413 | {
|
---|
1414 | /* Restore data. */
|
---|
1415 | Assert(!pThis->pReq);
|
---|
1416 |
|
---|
1417 | SSMR3GetU8(pSSM, &pThis->bConfigurationValue);
|
---|
1418 | SSMR3GetBool(pSSM, &pThis->aEps[0].fHalted);
|
---|
1419 | SSMR3GetBool(pSSM, &pThis->aEps[1].fHalted);
|
---|
1420 | SSMR3GetBool(pSSM, &pThis->aEps[2].fHalted);
|
---|
1421 | bool fReqAlloc = false;
|
---|
1422 | rc = SSMR3GetBool(pSSM, &fReqAlloc);
|
---|
1423 | AssertRCReturn(rc, rc);
|
---|
1424 | if (fReqAlloc)
|
---|
1425 | {
|
---|
1426 | PUSBMSDREQ pReq = usbMsdReqAlloc(pThis);
|
---|
1427 | AssertReturn(pReq, VERR_NO_MEMORY);
|
---|
1428 | pThis->pReq = pReq;
|
---|
1429 |
|
---|
1430 | SSMR3GetU32(pSSM, (uint32_t *)&pReq->enmState);
|
---|
1431 | uint32_t cbBuf = 0;
|
---|
1432 | rc = SSMR3GetU32(pSSM, &cbBuf);
|
---|
1433 | AssertRCReturn(rc, rc);
|
---|
1434 | if (cbBuf)
|
---|
1435 | {
|
---|
1436 | if (usbMsdReqEnsureBuffer(pThis, pReq, cbBuf))
|
---|
1437 | {
|
---|
1438 | AssertPtr(pReq->pbBuf);
|
---|
1439 | Assert(cbBuf == pReq->cbBuf);
|
---|
1440 | SSMR3GetMem(pSSM, pReq->pbBuf, pReq->cbBuf);
|
---|
1441 | }
|
---|
1442 | else
|
---|
1443 | return VERR_NO_MEMORY;
|
---|
1444 | }
|
---|
1445 |
|
---|
1446 | SSMR3GetU32(pSSM, &pReq->offBuf);
|
---|
1447 | SSMR3GetMem(pSSM, &pReq->Cbw, sizeof(pReq->Cbw));
|
---|
1448 |
|
---|
1449 | if (uVersion > USB_MSD_SAVED_STATE_VERSION_PRE_CLEANUP)
|
---|
1450 | rc = SSMR3GetU8(pSSM, &pReq->iScsiReqStatus);
|
---|
1451 | else
|
---|
1452 | {
|
---|
1453 | int32_t iScsiReqStatus;
|
---|
1454 |
|
---|
1455 | /* Skip old fields which are unused now or can be determined from the CBW. */
|
---|
1456 | SSMR3Skip(pSSM, 4 * 4 + 64);
|
---|
1457 | rc = SSMR3GetS32(pSSM, &iScsiReqStatus);
|
---|
1458 | pReq->iScsiReqStatus = (uint8_t)iScsiReqStatus;
|
---|
1459 | }
|
---|
1460 | AssertRCReturn(rc, rc);
|
---|
1461 | }
|
---|
1462 |
|
---|
1463 | rc = SSMR3GetU32(pSSM, &u32);
|
---|
1464 | AssertRCReturn(rc, rc);
|
---|
1465 | AssertMsgReturn(u32 == UINT32_MAX, ("%#x\n", u32), VERR_SSM_DATA_UNIT_FORMAT_CHANGED);
|
---|
1466 | }
|
---|
1467 |
|
---|
1468 | return VINF_SUCCESS;
|
---|
1469 | }
|
---|
1470 |
|
---|
1471 |
|
---|
1472 | /**
|
---|
1473 | * @interface_method_impl{PDMUSBREG,pfnUrbReap}
|
---|
1474 | */
|
---|
1475 | static DECLCALLBACK(PVUSBURB) usbMsdUrbReap(PPDMUSBINS pUsbIns, RTMSINTERVAL cMillies)
|
---|
1476 | {
|
---|
1477 | PUSBMSD pThis = PDMINS_2_DATA(pUsbIns, PUSBMSD);
|
---|
1478 | LogFlow(("usbMsdUrbReap/#%u: cMillies=%u\n", pUsbIns->iInstance, cMillies));
|
---|
1479 |
|
---|
1480 | RTCritSectEnter(&pThis->CritSect);
|
---|
1481 |
|
---|
1482 | PVUSBURB pUrb = usbMsdQueueRemoveHead(&pThis->DoneQueue);
|
---|
1483 | if (!pUrb && cMillies)
|
---|
1484 | {
|
---|
1485 | /* Wait */
|
---|
1486 | pThis->fHaveDoneQueueWaiter = true;
|
---|
1487 | RTCritSectLeave(&pThis->CritSect);
|
---|
1488 |
|
---|
1489 | RTSemEventWait(pThis->hEvtDoneQueue, cMillies);
|
---|
1490 |
|
---|
1491 | RTCritSectEnter(&pThis->CritSect);
|
---|
1492 | pThis->fHaveDoneQueueWaiter = false;
|
---|
1493 |
|
---|
1494 | pUrb = usbMsdQueueRemoveHead(&pThis->DoneQueue);
|
---|
1495 | }
|
---|
1496 |
|
---|
1497 | RTCritSectLeave(&pThis->CritSect);
|
---|
1498 |
|
---|
1499 | if (pUrb)
|
---|
1500 | Log(("usbMsdUrbReap/#%u: pUrb=%p:%s\n", pUsbIns->iInstance, pUrb, pUrb->pszDesc));
|
---|
1501 | return pUrb;
|
---|
1502 | }
|
---|
1503 |
|
---|
1504 |
|
---|
1505 | /**
|
---|
1506 | * @interface_method_impl{PDMUSBREG,pfnWakeup}
|
---|
1507 | */
|
---|
1508 | static DECLCALLBACK(int) usbMsdWakeup(PPDMUSBINS pUsbIns)
|
---|
1509 | {
|
---|
1510 | PUSBMSD pThis = PDMINS_2_DATA(pUsbIns, PUSBMSD);
|
---|
1511 | LogFlow(("usbMsdUrbReap/#%u:\n", pUsbIns->iInstance));
|
---|
1512 |
|
---|
1513 | return RTSemEventSignal(pThis->hEvtDoneQueue);
|
---|
1514 | }
|
---|
1515 |
|
---|
1516 |
|
---|
1517 | /**
|
---|
1518 | * @interface_method_impl{PDMUSBREG,pfnUrbCancel}
|
---|
1519 | */
|
---|
1520 | static DECLCALLBACK(int) usbMsdUrbCancel(PPDMUSBINS pUsbIns, PVUSBURB pUrb)
|
---|
1521 | {
|
---|
1522 | PUSBMSD pThis = PDMINS_2_DATA(pUsbIns, PUSBMSD);
|
---|
1523 | LogFlow(("usbMsdUrbCancel/#%u: pUrb=%p:%s\n", pUsbIns->iInstance, pUrb, pUrb->pszDesc));
|
---|
1524 | RTCritSectEnter(&pThis->CritSect);
|
---|
1525 |
|
---|
1526 | /*
|
---|
1527 | * Remove the URB from the to-host queue and move it onto the done queue.
|
---|
1528 | */
|
---|
1529 | if (usbMsdQueueRemove(&pThis->ToHostQueue, pUrb))
|
---|
1530 | usbMsdLinkDone(pThis, pUrb);
|
---|
1531 |
|
---|
1532 | RTCritSectLeave(&pThis->CritSect);
|
---|
1533 | return VINF_SUCCESS;
|
---|
1534 | }
|
---|
1535 |
|
---|
1536 |
|
---|
1537 | /**
|
---|
1538 | * Wrapper around PDMISCSICONNECTOR::pfnSCSIRequestSend that deals with
|
---|
1539 | * SCSI_REQUEST_SENSE.
|
---|
1540 | *
|
---|
1541 | * @returns VBox status code.
|
---|
1542 | * @param pThis The MSD instance data.
|
---|
1543 | * @param pReq The MSD request.
|
---|
1544 | * @param pszCaller Where we're called from.
|
---|
1545 | */
|
---|
1546 | static int usbMsdSubmitScsiCommand(PUSBMSD pThis, PUSBMSDREQ pReq, const char *pszCaller)
|
---|
1547 | {
|
---|
1548 | RT_NOREF(pszCaller);
|
---|
1549 | Log(("%s: Entering EXECUTING (dCBWTag=%#x).\n", pszCaller, pReq->Cbw.dCBWTag));
|
---|
1550 | Assert(pReq == pThis->pReq);
|
---|
1551 | pReq->enmState = USBMSDREQSTATE_EXECUTING;
|
---|
1552 |
|
---|
1553 | PDMMEDIAEXIOREQSCSITXDIR enmTxDir = pReq->Cbw.dCBWDataTransferLength == 0
|
---|
1554 | ? PDMMEDIAEXIOREQSCSITXDIR_NONE
|
---|
1555 | : (pReq->Cbw.bmCBWFlags & USBCBW_DIR_MASK) == USBCBW_DIR_OUT
|
---|
1556 | ? PDMMEDIAEXIOREQSCSITXDIR_TO_DEVICE
|
---|
1557 | : PDMMEDIAEXIOREQSCSITXDIR_FROM_DEVICE;
|
---|
1558 |
|
---|
1559 | return pThis->Lun0.pIMediaEx->pfnIoReqSendScsiCmd(pThis->Lun0.pIMediaEx, pReq->hIoReq, pReq->Cbw.bCBWLun,
|
---|
1560 | &pReq->Cbw.CBWCB[0], pReq->Cbw.bCBWCBLength, enmTxDir,
|
---|
1561 | pReq->Cbw.dCBWDataTransferLength, NULL, 0,
|
---|
1562 | &pReq->iScsiReqStatus, 20 * RT_MS_1SEC);
|
---|
1563 | }
|
---|
1564 |
|
---|
1565 |
|
---|
1566 | /**
|
---|
1567 | * Handle requests sent to the outbound (to device) bulk pipe.
|
---|
1568 | */
|
---|
1569 | static int usbMsdHandleBulkHostToDev(PUSBMSD pThis, PUSBMSDEP pEp, PVUSBURB pUrb)
|
---|
1570 | {
|
---|
1571 | /*
|
---|
1572 | * Stall the request if the pipe is halted.
|
---|
1573 | */
|
---|
1574 | if (RT_UNLIKELY(pEp->fHalted))
|
---|
1575 | return usbMsdCompleteStall(pThis, NULL, pUrb, "Halted pipe");
|
---|
1576 |
|
---|
1577 | /*
|
---|
1578 | * Deal with the URB according to the current state.
|
---|
1579 | */
|
---|
1580 | PUSBMSDREQ pReq = pThis->pReq;
|
---|
1581 | USBMSDREQSTATE enmState = pReq ? pReq->enmState : USBMSDREQSTATE_READY;
|
---|
1582 | switch (enmState)
|
---|
1583 | {
|
---|
1584 | case USBMSDREQSTATE_STATUS:
|
---|
1585 | LogFlow(("usbMsdHandleBulkHostToDev: Skipping pending status.\n"));
|
---|
1586 | pReq->enmState = USBMSDREQSTATE_READY;
|
---|
1587 | RT_FALL_THRU();
|
---|
1588 |
|
---|
1589 | /*
|
---|
1590 | * We're ready to receive a command. Start off by validating the
|
---|
1591 | * incoming request.
|
---|
1592 | */
|
---|
1593 | case USBMSDREQSTATE_READY:
|
---|
1594 | {
|
---|
1595 | PCUSBCBW pCbw = (PUSBCBW)&pUrb->abData[0];
|
---|
1596 | if (pUrb->cbData < RT_UOFFSETOF(USBCBW, CBWCB[1]))
|
---|
1597 | {
|
---|
1598 | Log(("usbMsd: Bad CBW: cbData=%#x < min=%#x\n", pUrb->cbData, RT_UOFFSETOF(USBCBW, CBWCB[1]) ));
|
---|
1599 | return usbMsdCompleteStall(pThis, NULL, pUrb, "BAD CBW");
|
---|
1600 | }
|
---|
1601 | if (pCbw->dCBWSignature != USBCBW_SIGNATURE)
|
---|
1602 | {
|
---|
1603 | Log(("usbMsd: CBW: Invalid dCBWSignature value: %#x\n", pCbw->dCBWSignature));
|
---|
1604 | return usbMsdCompleteStall(pThis, NULL, pUrb, "Bad CBW");
|
---|
1605 | }
|
---|
1606 | Log(("usbMsd: CBW: dCBWTag=%#x dCBWDataTransferLength=%#x bmCBWFlags=%#x bCBWLun=%#x bCBWCBLength=%#x cbData=%#x fShortNotOk=%RTbool\n",
|
---|
1607 | pCbw->dCBWTag, pCbw->dCBWDataTransferLength, pCbw->bmCBWFlags, pCbw->bCBWLun, pCbw->bCBWCBLength, pUrb->cbData, pUrb->fShortNotOk));
|
---|
1608 | if (pCbw->bmCBWFlags & ~USBCBW_DIR_MASK)
|
---|
1609 | {
|
---|
1610 | Log(("usbMsd: CBW: Bad bmCBWFlags value: %#x\n", pCbw->bmCBWFlags));
|
---|
1611 | return usbMsdCompleteStall(pThis, NULL, pUrb, "Bad CBW");
|
---|
1612 |
|
---|
1613 | }
|
---|
1614 | if (pCbw->bCBWLun != 0)
|
---|
1615 | {
|
---|
1616 | Log(("usbMsd: CBW: Bad bCBWLun value: %#x\n", pCbw->bCBWLun));
|
---|
1617 | return usbMsdCompleteStall(pThis, NULL, pUrb, "Bad CBW");
|
---|
1618 | }
|
---|
1619 | if (pCbw->bCBWCBLength == 0)
|
---|
1620 | {
|
---|
1621 | Log(("usbMsd: CBW: Bad bCBWCBLength value: %#x\n", pCbw->bCBWCBLength));
|
---|
1622 | return usbMsdCompleteStall(pThis, NULL, pUrb, "Bad CBW");
|
---|
1623 | }
|
---|
1624 | if (pUrb->cbData < RT_UOFFSETOF(USBCBW, CBWCB[pCbw->bCBWCBLength]))
|
---|
1625 | {
|
---|
1626 | Log(("usbMsd: CBW: Mismatching cbData and bCBWCBLength values: %#x vs. %#x (%#x)\n",
|
---|
1627 | pUrb->cbData, RT_UOFFSETOF(USBCBW, CBWCB[pCbw->bCBWCBLength]), pCbw->bCBWCBLength));
|
---|
1628 | return usbMsdCompleteStall(pThis, NULL, pUrb, "Bad CBW");
|
---|
1629 | }
|
---|
1630 | if (pCbw->dCBWDataTransferLength > _1M)
|
---|
1631 | {
|
---|
1632 | Log(("usbMsd: CBW: dCBWDataTransferLength is too large: %#x (%u)\n",
|
---|
1633 | pCbw->dCBWDataTransferLength, pCbw->dCBWDataTransferLength));
|
---|
1634 | return usbMsdCompleteStall(pThis, NULL, pUrb, "Too big transfer");
|
---|
1635 | }
|
---|
1636 |
|
---|
1637 | /*
|
---|
1638 | * Make sure we've got a request and a sufficient buffer space.
|
---|
1639 | *
|
---|
1640 | * Note! This will make sure the buffer is ZERO as well, thus
|
---|
1641 | * saving us the trouble of clearing the output buffer on
|
---|
1642 | * failure later.
|
---|
1643 | */
|
---|
1644 | if (!pReq)
|
---|
1645 | {
|
---|
1646 | pReq = usbMsdReqAlloc(pThis);
|
---|
1647 | if (!pReq)
|
---|
1648 | return usbMsdCompleteStall(pThis, NULL, pUrb, "Request allocation failure");
|
---|
1649 | pThis->pReq = pReq;
|
---|
1650 | }
|
---|
1651 | if (!usbMsdReqEnsureBuffer(pThis, pReq, pCbw->dCBWDataTransferLength))
|
---|
1652 | return usbMsdCompleteStall(pThis, NULL, pUrb, "Buffer allocation failure");
|
---|
1653 |
|
---|
1654 | /*
|
---|
1655 | * Prepare the request. Kick it off right away if possible.
|
---|
1656 | */
|
---|
1657 | usbMsdReqPrepare(pReq, pCbw);
|
---|
1658 |
|
---|
1659 | if ( pReq->Cbw.dCBWDataTransferLength == 0
|
---|
1660 | || (pReq->Cbw.bmCBWFlags & USBCBW_DIR_MASK) == USBCBW_DIR_IN)
|
---|
1661 | {
|
---|
1662 | int rc = usbMsdSubmitScsiCommand(pThis, pReq, "usbMsdHandleBulkHostToDev");
|
---|
1663 | if (RT_SUCCESS(rc) && rc != VINF_PDM_MEDIAEX_IOREQ_IN_PROGRESS)
|
---|
1664 | usbMsdReqComplete(pThis, pReq, rc);
|
---|
1665 | else if (RT_FAILURE(rc))
|
---|
1666 | {
|
---|
1667 | Log(("usbMsd: Failed sending SCSI request to driver: %Rrc\n", rc));
|
---|
1668 | return usbMsdCompleteStall(pThis, NULL, pUrb, "SCSI Submit #1");
|
---|
1669 | }
|
---|
1670 | }
|
---|
1671 | else
|
---|
1672 | {
|
---|
1673 | Log(("usbMsdHandleBulkHostToDev: Entering DATA_FROM_HOST.\n"));
|
---|
1674 | pReq->enmState = USBMSDREQSTATE_DATA_FROM_HOST;
|
---|
1675 | }
|
---|
1676 |
|
---|
1677 | return usbMsdCompleteOk(pThis, pUrb, pUrb->cbData);
|
---|
1678 | }
|
---|
1679 |
|
---|
1680 | /*
|
---|
1681 | * Stuff the data into the buffer.
|
---|
1682 | */
|
---|
1683 | case USBMSDREQSTATE_DATA_FROM_HOST:
|
---|
1684 | {
|
---|
1685 | uint32_t cbData = pUrb->cbData;
|
---|
1686 | uint32_t cbLeft = pReq->Cbw.dCBWDataTransferLength - pReq->offBuf;
|
---|
1687 | if (cbData > cbLeft)
|
---|
1688 | {
|
---|
1689 | Log(("usbMsd: Too much data: cbData=%#x offBuf=%#x dCBWDataTransferLength=%#x cbLeft=%#x\n",
|
---|
1690 | cbData, pReq->offBuf, pReq->Cbw.dCBWDataTransferLength, cbLeft));
|
---|
1691 | return usbMsdCompleteStall(pThis, NULL, pUrb, "Too much data");
|
---|
1692 | }
|
---|
1693 | memcpy(&pReq->pbBuf[pReq->offBuf], &pUrb->abData[0], cbData);
|
---|
1694 | pReq->offBuf += cbData;
|
---|
1695 |
|
---|
1696 | if (pReq->offBuf == pReq->Cbw.dCBWDataTransferLength)
|
---|
1697 | {
|
---|
1698 | int rc = usbMsdSubmitScsiCommand(pThis, pReq, "usbMsdHandleBulkHostToDev");
|
---|
1699 | if (RT_SUCCESS(rc) && rc != VINF_PDM_MEDIAEX_IOREQ_IN_PROGRESS)
|
---|
1700 | usbMsdReqComplete(pThis, pReq, rc);
|
---|
1701 | else if (RT_FAILURE(rc))
|
---|
1702 | {
|
---|
1703 | Log(("usbMsd: Failed sending SCSI request to driver: %Rrc\n", rc));
|
---|
1704 | return usbMsdCompleteStall(pThis, NULL, pUrb, "SCSI Submit #2");
|
---|
1705 | }
|
---|
1706 | }
|
---|
1707 | return usbMsdCompleteOk(pThis, pUrb, cbData);
|
---|
1708 | }
|
---|
1709 |
|
---|
1710 | /*
|
---|
1711 | * Bad state, stall.
|
---|
1712 | */
|
---|
1713 | case USBMSDREQSTATE_DATA_TO_HOST:
|
---|
1714 | return usbMsdCompleteStall(pThis, NULL, pUrb, "Bad state H2D: DATA_TO_HOST");
|
---|
1715 |
|
---|
1716 | case USBMSDREQSTATE_EXECUTING:
|
---|
1717 | return usbMsdCompleteStall(pThis, NULL, pUrb, "Bad state H2D: EXECUTING");
|
---|
1718 |
|
---|
1719 | default:
|
---|
1720 | AssertMsgFailed(("enmState=%d\n", enmState));
|
---|
1721 | return usbMsdCompleteStall(pThis, NULL, pUrb, "Bad state (H2D)");
|
---|
1722 | }
|
---|
1723 | }
|
---|
1724 |
|
---|
1725 |
|
---|
1726 | /**
|
---|
1727 | * Handle requests sent to the inbound (to host) bulk pipe.
|
---|
1728 | */
|
---|
1729 | static int usbMsdHandleBulkDevToHost(PUSBMSD pThis, PUSBMSDEP pEp, PVUSBURB pUrb)
|
---|
1730 | {
|
---|
1731 | /*
|
---|
1732 | * Stall the request if the pipe is halted OR if there is no
|
---|
1733 | * pending request yet.
|
---|
1734 | */
|
---|
1735 | PUSBMSDREQ pReq = pThis->pReq;
|
---|
1736 | if (RT_UNLIKELY(pEp->fHalted || !pReq))
|
---|
1737 | return usbMsdCompleteStall(pThis, NULL, pUrb, pEp->fHalted ? "Halted pipe" : "No request");
|
---|
1738 |
|
---|
1739 | /*
|
---|
1740 | * Deal with the URB according to the state.
|
---|
1741 | */
|
---|
1742 | switch (pReq->enmState)
|
---|
1743 | {
|
---|
1744 | /*
|
---|
1745 | * We've data left to transfer to the host.
|
---|
1746 | */
|
---|
1747 | case USBMSDREQSTATE_DATA_TO_HOST:
|
---|
1748 | {
|
---|
1749 | uint32_t cbData = pUrb->cbData;
|
---|
1750 | uint32_t cbCopy = pReq->Cbw.dCBWDataTransferLength - pReq->offBuf;
|
---|
1751 | if (cbData <= cbCopy)
|
---|
1752 | cbCopy = cbData;
|
---|
1753 | else if (pUrb->fShortNotOk)
|
---|
1754 | {
|
---|
1755 | Log(("usbMsd: Requested more data that we've got; cbData=%#x offBuf=%#x dCBWDataTransferLength=%#x cbLeft=%#x\n",
|
---|
1756 | cbData, pReq->offBuf, pReq->Cbw.dCBWDataTransferLength, cbCopy));
|
---|
1757 | return usbMsdCompleteStall(pThis, NULL, pUrb, "Data underrun");
|
---|
1758 | }
|
---|
1759 | memcpy(&pUrb->abData[0], &pReq->pbBuf[pReq->offBuf], cbCopy);
|
---|
1760 | pReq->offBuf += cbCopy;
|
---|
1761 |
|
---|
1762 | if (pReq->offBuf == pReq->Cbw.dCBWDataTransferLength)
|
---|
1763 | {
|
---|
1764 | Log(("usbMsdHandleBulkDevToHost: Entering STATUS\n"));
|
---|
1765 | pReq->enmState = USBMSDREQSTATE_STATUS;
|
---|
1766 | }
|
---|
1767 | return usbMsdCompleteOk(pThis, pUrb, cbCopy);
|
---|
1768 | }
|
---|
1769 |
|
---|
1770 | /*
|
---|
1771 | * Status transfer.
|
---|
1772 | */
|
---|
1773 | case USBMSDREQSTATE_STATUS:
|
---|
1774 | {
|
---|
1775 | if ((pUrb->cbData < sizeof(USBCSW)) || (pUrb->cbData > sizeof(USBCSW) && pUrb->fShortNotOk))
|
---|
1776 | {
|
---|
1777 | Log(("usbMsd: Unexpected status request size: %#x (expected %#x), fShortNotOK=%RTbool\n", pUrb->cbData, sizeof(USBCSW), pUrb->fShortNotOk));
|
---|
1778 | return usbMsdCompleteStall(pThis, NULL, pUrb, "Invalid CSW size");
|
---|
1779 | }
|
---|
1780 |
|
---|
1781 | /* Enter a CSW into the URB data buffer. */
|
---|
1782 | PUSBCSW pCsw = (PUSBCSW)&pUrb->abData[0];
|
---|
1783 | pCsw->dCSWSignature = USBCSW_SIGNATURE;
|
---|
1784 | pCsw->dCSWTag = pReq->Cbw.dCBWTag;
|
---|
1785 | pCsw->bCSWStatus = pReq->iScsiReqStatus == SCSI_STATUS_OK
|
---|
1786 | ? USBCSW_STATUS_OK
|
---|
1787 | : pReq->iScsiReqStatus < 0xff
|
---|
1788 | ? USBCSW_STATUS_FAILED
|
---|
1789 | : USBCSW_STATUS_PHASE_ERROR;
|
---|
1790 | /** @todo the following is not always accurate; VSCSI needs
|
---|
1791 | * to implement residual counts properly! */
|
---|
1792 | if ((pReq->Cbw.bmCBWFlags & USBCBW_DIR_MASK) == USBCBW_DIR_OUT)
|
---|
1793 | pCsw->dCSWDataResidue = pCsw->bCSWStatus == USBCSW_STATUS_OK
|
---|
1794 | ? 0
|
---|
1795 | : pReq->Cbw.dCBWDataTransferLength;
|
---|
1796 | else
|
---|
1797 | pCsw->dCSWDataResidue = pCsw->bCSWStatus == USBCSW_STATUS_OK
|
---|
1798 | ? 0
|
---|
1799 | : pReq->Cbw.dCBWDataTransferLength;
|
---|
1800 | Log(("usbMsd: CSW: dCSWTag=%#x bCSWStatus=%d dCSWDataResidue=%#x\n",
|
---|
1801 | pCsw->dCSWTag, pCsw->bCSWStatus, pCsw->dCSWDataResidue));
|
---|
1802 |
|
---|
1803 | Log(("usbMsdHandleBulkDevToHost: Entering READY\n"));
|
---|
1804 | pReq->enmState = USBMSDREQSTATE_READY;
|
---|
1805 | return usbMsdCompleteOk(pThis, pUrb, sizeof(*pCsw));
|
---|
1806 | }
|
---|
1807 |
|
---|
1808 | /*
|
---|
1809 | * Status request before we've received all (or even any) data.
|
---|
1810 | * Linux 2.4.31 does this sometimes. The recommended behavior is to
|
---|
1811 | * to accept the current data amount and execute the request. (The
|
---|
1812 | * alternative behavior is to stall.)
|
---|
1813 | */
|
---|
1814 | case USBMSDREQSTATE_DATA_FROM_HOST:
|
---|
1815 | {
|
---|
1816 | if (pUrb->cbData != sizeof(USBCSW))
|
---|
1817 | {
|
---|
1818 | Log(("usbMsdHandleBulkDevToHost: DATA_FROM_HOST; cbData=%#x -> stall\n", pUrb->cbData));
|
---|
1819 | return usbMsdCompleteStall(pThis, NULL, pUrb, "Invalid CSW size");
|
---|
1820 | }
|
---|
1821 |
|
---|
1822 | int rc = usbMsdSubmitScsiCommand(pThis, pReq, "usbMsdHandleBulkDevToHost");
|
---|
1823 | if (RT_SUCCESS(rc) && rc != VINF_PDM_MEDIAEX_IOREQ_IN_PROGRESS)
|
---|
1824 | usbMsdReqComplete(pThis, pReq, rc);
|
---|
1825 | else if (RT_FAILURE(rc))
|
---|
1826 | {
|
---|
1827 | Log(("usbMsd: Failed sending SCSI request to driver: %Rrc\n", rc));
|
---|
1828 | return usbMsdCompleteStall(pThis, NULL, pUrb, "SCSI Submit #3");
|
---|
1829 | }
|
---|
1830 | }
|
---|
1831 | RT_FALL_THRU();
|
---|
1832 |
|
---|
1833 | /*
|
---|
1834 | * The SCSI command is still pending, queue the URB awaiting its
|
---|
1835 | * completion.
|
---|
1836 | */
|
---|
1837 | case USBMSDREQSTATE_EXECUTING:
|
---|
1838 | usbMsdQueueAddTail(&pThis->ToHostQueue, pUrb);
|
---|
1839 | LogFlow(("usbMsdHandleBulkDevToHost: Added %p:%s to the to-host queue\n", pUrb, pUrb->pszDesc));
|
---|
1840 | return VINF_SUCCESS;
|
---|
1841 |
|
---|
1842 | /*
|
---|
1843 | * Bad states, stall.
|
---|
1844 | */
|
---|
1845 | case USBMSDREQSTATE_READY:
|
---|
1846 | Log(("usbMsdHandleBulkDevToHost: enmState=READ(%d) (cbData=%#x)\n", pReq->enmState, pUrb->cbData));
|
---|
1847 | return usbMsdCompleteStall(pThis, NULL, pUrb, "Bad state D2H: READY");
|
---|
1848 |
|
---|
1849 | default:
|
---|
1850 | Log(("usbMsdHandleBulkDevToHost: enmState=%d cbData=%#x\n", pReq->enmState, pUrb->cbData));
|
---|
1851 | return usbMsdCompleteStall(pThis, NULL, pUrb, "Really bad state (D2H)!");
|
---|
1852 | }
|
---|
1853 | }
|
---|
1854 |
|
---|
1855 |
|
---|
1856 | /**
|
---|
1857 | * Handles request send to the default control pipe.
|
---|
1858 | */
|
---|
1859 | static int usbMsdHandleDefaultPipe(PUSBMSD pThis, PUSBMSDEP pEp, PVUSBURB pUrb)
|
---|
1860 | {
|
---|
1861 | PVUSBSETUP pSetup = (PVUSBSETUP)&pUrb->abData[0];
|
---|
1862 | AssertReturn(pUrb->cbData >= sizeof(*pSetup), VERR_VUSB_FAILED_TO_QUEUE_URB);
|
---|
1863 |
|
---|
1864 | if ((pSetup->bmRequestType & VUSB_REQ_MASK) == VUSB_REQ_STANDARD)
|
---|
1865 | {
|
---|
1866 | switch (pSetup->bRequest)
|
---|
1867 | {
|
---|
1868 | case VUSB_REQ_GET_DESCRIPTOR:
|
---|
1869 | {
|
---|
1870 | if (pSetup->bmRequestType != (VUSB_TO_DEVICE | VUSB_REQ_STANDARD | VUSB_DIR_TO_HOST))
|
---|
1871 | {
|
---|
1872 | Log(("usbMsd: Bad GET_DESCRIPTOR req: bmRequestType=%#x\n", pSetup->bmRequestType));
|
---|
1873 | return usbMsdCompleteStall(pThis, pEp, pUrb, "Bad GET_DESCRIPTOR");
|
---|
1874 | }
|
---|
1875 |
|
---|
1876 | switch (pSetup->wValue >> 8)
|
---|
1877 | {
|
---|
1878 | uint32_t cbCopy;
|
---|
1879 |
|
---|
1880 | case VUSB_DT_STRING:
|
---|
1881 | Log(("usbMsd: GET_DESCRIPTOR DT_STRING wValue=%#x wIndex=%#x\n", pSetup->wValue, pSetup->wIndex));
|
---|
1882 | break;
|
---|
1883 | case VUSB_DT_DEVICE_QUALIFIER:
|
---|
1884 | Log(("usbMsd: GET_DESCRIPTOR DT_DEVICE_QUALIFIER wValue=%#x wIndex=%#x\n", pSetup->wValue, pSetup->wIndex));
|
---|
1885 | /* Returned data is written after the setup message. */
|
---|
1886 | cbCopy = pUrb->cbData - sizeof(*pSetup);
|
---|
1887 | cbCopy = RT_MIN(cbCopy, sizeof(g_UsbMsdDeviceQualifier));
|
---|
1888 | memcpy(&pUrb->abData[sizeof(*pSetup)], &g_UsbMsdDeviceQualifier, cbCopy);
|
---|
1889 | return usbMsdCompleteOk(pThis, pUrb, cbCopy + sizeof(*pSetup));
|
---|
1890 | case VUSB_DT_BOS:
|
---|
1891 | Log(("usbMsd: GET_DESCRIPTOR DT_BOS wValue=%#x wIndex=%#x\n", pSetup->wValue, pSetup->wIndex));
|
---|
1892 | /* Returned data is written after the setup message. */
|
---|
1893 | cbCopy = pUrb->cbData - sizeof(*pSetup);
|
---|
1894 | cbCopy = RT_MIN(cbCopy, sizeof(g_UsbMsdBOS));
|
---|
1895 | memcpy(&pUrb->abData[sizeof(*pSetup)], &g_UsbMsdBOS, cbCopy);
|
---|
1896 | return usbMsdCompleteOk(pThis, pUrb, cbCopy + sizeof(*pSetup));
|
---|
1897 | default:
|
---|
1898 | Log(("usbMsd: GET_DESCRIPTOR, huh? wValue=%#x wIndex=%#x\n", pSetup->wValue, pSetup->wIndex));
|
---|
1899 | break;
|
---|
1900 | }
|
---|
1901 | break;
|
---|
1902 | }
|
---|
1903 |
|
---|
1904 | case VUSB_REQ_CLEAR_FEATURE:
|
---|
1905 | break;
|
---|
1906 | }
|
---|
1907 |
|
---|
1908 | /** @todo implement this. */
|
---|
1909 | Log(("usbMsd: Implement standard request: bmRequestType=%#x bRequest=%#x wValue=%#x wIndex=%#x wLength=%#x\n",
|
---|
1910 | pSetup->bmRequestType, pSetup->bRequest, pSetup->wValue, pSetup->wIndex, pSetup->wLength));
|
---|
1911 |
|
---|
1912 | usbMsdCompleteStall(pThis, pEp, pUrb, "TODO: standard request stuff");
|
---|
1913 | }
|
---|
1914 | /* 3.1 Bulk-Only Mass Storage Reset */
|
---|
1915 | else if ( pSetup->bmRequestType == (VUSB_REQ_CLASS | VUSB_TO_INTERFACE)
|
---|
1916 | && pSetup->bRequest == 0xff
|
---|
1917 | && !pSetup->wValue
|
---|
1918 | && !pSetup->wLength
|
---|
1919 | && pSetup->wIndex == 0)
|
---|
1920 | {
|
---|
1921 | Log(("usbMsdHandleDefaultPipe: Bulk-Only Mass Storage Reset\n"));
|
---|
1922 | return usbMsdResetWorker(pThis, pUrb, false /*fSetConfig*/);
|
---|
1923 | }
|
---|
1924 | /* 3.2 Get Max LUN, may stall if we like (but we don't). */
|
---|
1925 | else if ( pSetup->bmRequestType == (VUSB_REQ_CLASS | VUSB_TO_INTERFACE | VUSB_DIR_TO_HOST)
|
---|
1926 | && pSetup->bRequest == 0xfe
|
---|
1927 | && !pSetup->wValue
|
---|
1928 | && pSetup->wLength == 1
|
---|
1929 | && pSetup->wIndex == 0)
|
---|
1930 | {
|
---|
1931 | *(uint8_t *)(pSetup + 1) = 0; /* max lun is 0 */
|
---|
1932 | usbMsdCompleteOk(pThis, pUrb, 1);
|
---|
1933 | }
|
---|
1934 | else
|
---|
1935 | {
|
---|
1936 | Log(("usbMsd: Unknown control msg: bmRequestType=%#x bRequest=%#x wValue=%#x wIndex=%#x wLength=%#x\n",
|
---|
1937 | pSetup->bmRequestType, pSetup->bRequest, pSetup->wValue, pSetup->wIndex, pSetup->wLength));
|
---|
1938 | return usbMsdCompleteStall(pThis, pEp, pUrb, "Unknown control msg");
|
---|
1939 | }
|
---|
1940 |
|
---|
1941 | return VINF_SUCCESS;
|
---|
1942 | }
|
---|
1943 |
|
---|
1944 |
|
---|
1945 | /**
|
---|
1946 | * @interface_method_impl{PDMUSBREG,pfnUrbQueue}
|
---|
1947 | */
|
---|
1948 | static DECLCALLBACK(int) usbMsdQueue(PPDMUSBINS pUsbIns, PVUSBURB pUrb)
|
---|
1949 | {
|
---|
1950 | PUSBMSD pThis = PDMINS_2_DATA(pUsbIns, PUSBMSD);
|
---|
1951 | LogFlow(("usbMsdQueue/#%u: pUrb=%p:%s EndPt=%#x\n", pUsbIns->iInstance, pUrb, pUrb->pszDesc, pUrb->EndPt));
|
---|
1952 | RTCritSectEnter(&pThis->CritSect);
|
---|
1953 |
|
---|
1954 | /*
|
---|
1955 | * Parse on a per end-point basis.
|
---|
1956 | */
|
---|
1957 | int rc;
|
---|
1958 | switch (pUrb->EndPt)
|
---|
1959 | {
|
---|
1960 | case 0:
|
---|
1961 | rc = usbMsdHandleDefaultPipe(pThis, &pThis->aEps[0], pUrb);
|
---|
1962 | break;
|
---|
1963 |
|
---|
1964 | case 0x81:
|
---|
1965 | AssertFailed();
|
---|
1966 | RT_FALL_THRU();
|
---|
1967 | case 0x01:
|
---|
1968 | rc = usbMsdHandleBulkDevToHost(pThis, &pThis->aEps[1], pUrb);
|
---|
1969 | break;
|
---|
1970 |
|
---|
1971 | case 0x02:
|
---|
1972 | rc = usbMsdHandleBulkHostToDev(pThis, &pThis->aEps[2], pUrb);
|
---|
1973 | break;
|
---|
1974 |
|
---|
1975 | default:
|
---|
1976 | AssertMsgFailed(("EndPt=%d\n", pUrb->EndPt));
|
---|
1977 | rc = VERR_VUSB_FAILED_TO_QUEUE_URB;
|
---|
1978 | break;
|
---|
1979 | }
|
---|
1980 |
|
---|
1981 | RTCritSectLeave(&pThis->CritSect);
|
---|
1982 | return rc;
|
---|
1983 | }
|
---|
1984 |
|
---|
1985 |
|
---|
1986 | /**
|
---|
1987 | * @interface_method_impl{PDMUSBREG,pfnUsbClearHaltedEndpoint}
|
---|
1988 | */
|
---|
1989 | static DECLCALLBACK(int) usbMsdUsbClearHaltedEndpoint(PPDMUSBINS pUsbIns, unsigned uEndpoint)
|
---|
1990 | {
|
---|
1991 | PUSBMSD pThis = PDMINS_2_DATA(pUsbIns, PUSBMSD);
|
---|
1992 | LogFlow(("usbMsdUsbClearHaltedEndpoint/#%u: uEndpoint=%#x\n", pUsbIns->iInstance, uEndpoint));
|
---|
1993 |
|
---|
1994 | if ((uEndpoint & ~0x80) < RT_ELEMENTS(pThis->aEps))
|
---|
1995 | {
|
---|
1996 | RTCritSectEnter(&pThis->CritSect);
|
---|
1997 | pThis->aEps[(uEndpoint & ~0x80)].fHalted = false;
|
---|
1998 | RTCritSectLeave(&pThis->CritSect);
|
---|
1999 | }
|
---|
2000 |
|
---|
2001 | return VINF_SUCCESS;
|
---|
2002 | }
|
---|
2003 |
|
---|
2004 |
|
---|
2005 | /**
|
---|
2006 | * @interface_method_impl{PDMUSBREG,pfnUsbSetInterface}
|
---|
2007 | */
|
---|
2008 | static DECLCALLBACK(int) usbMsdUsbSetInterface(PPDMUSBINS pUsbIns, uint8_t bInterfaceNumber, uint8_t bAlternateSetting)
|
---|
2009 | {
|
---|
2010 | RT_NOREF(pUsbIns, bInterfaceNumber, bAlternateSetting);
|
---|
2011 | LogFlow(("usbMsdUsbSetInterface/#%u: bInterfaceNumber=%u bAlternateSetting=%u\n", pUsbIns->iInstance, bInterfaceNumber, bAlternateSetting));
|
---|
2012 | Assert(bAlternateSetting == 0);
|
---|
2013 | return VINF_SUCCESS;
|
---|
2014 | }
|
---|
2015 |
|
---|
2016 |
|
---|
2017 | /**
|
---|
2018 | * @interface_method_impl{PDMUSBREG,pfnUsbSetConfiguration}
|
---|
2019 | */
|
---|
2020 | static DECLCALLBACK(int) usbMsdUsbSetConfiguration(PPDMUSBINS pUsbIns, uint8_t bConfigurationValue,
|
---|
2021 | const void *pvOldCfgDesc, const void *pvOldIfState, const void *pvNewCfgDesc)
|
---|
2022 | {
|
---|
2023 | RT_NOREF(pvOldCfgDesc, pvOldIfState, pvNewCfgDesc);
|
---|
2024 | PUSBMSD pThis = PDMINS_2_DATA(pUsbIns, PUSBMSD);
|
---|
2025 | LogFlow(("usbMsdUsbSetConfiguration/#%u: bConfigurationValue=%u\n", pUsbIns->iInstance, bConfigurationValue));
|
---|
2026 | Assert(bConfigurationValue == 1);
|
---|
2027 | RTCritSectEnter(&pThis->CritSect);
|
---|
2028 |
|
---|
2029 | /*
|
---|
2030 | * If the same config is applied more than once, it's a kind of reset.
|
---|
2031 | */
|
---|
2032 | if (pThis->bConfigurationValue == bConfigurationValue)
|
---|
2033 | usbMsdResetWorker(pThis, NULL, true /*fSetConfig*/); /** @todo figure out the exact difference */
|
---|
2034 | pThis->bConfigurationValue = bConfigurationValue;
|
---|
2035 |
|
---|
2036 | RTCritSectLeave(&pThis->CritSect);
|
---|
2037 | return VINF_SUCCESS;
|
---|
2038 | }
|
---|
2039 |
|
---|
2040 |
|
---|
2041 | /**
|
---|
2042 | * @interface_method_impl{PDMUSBREG,pfnUsbGetDescriptorCache}
|
---|
2043 | */
|
---|
2044 | static DECLCALLBACK(PCPDMUSBDESCCACHE) usbMsdUsbGetDescriptorCache(PPDMUSBINS pUsbIns)
|
---|
2045 | {
|
---|
2046 | PUSBMSD pThis = PDMINS_2_DATA(pUsbIns, PUSBMSD);
|
---|
2047 | LogFlow(("usbMsdUsbGetDescriptorCache/#%u:\n", pUsbIns->iInstance));
|
---|
2048 | if (pThis->pUsbIns->enmSpeed == VUSB_SPEED_SUPER)
|
---|
2049 | return pThis->fIsCdrom ? &g_UsbCdDescCacheSS : &g_UsbMsdDescCacheSS;
|
---|
2050 | else if (pThis->pUsbIns->enmSpeed == VUSB_SPEED_HIGH)
|
---|
2051 | return pThis->fIsCdrom ? &g_UsbCdDescCacheHS : &g_UsbMsdDescCacheHS;
|
---|
2052 | else
|
---|
2053 | return pThis->fIsCdrom ? &g_UsbCdDescCacheFS : &g_UsbMsdDescCacheFS;
|
---|
2054 | }
|
---|
2055 |
|
---|
2056 |
|
---|
2057 | /**
|
---|
2058 | * @interface_method_impl{PDMUSBREG,pfnUsbReset}
|
---|
2059 | */
|
---|
2060 | static DECLCALLBACK(int) usbMsdUsbReset(PPDMUSBINS pUsbIns, bool fResetOnLinux)
|
---|
2061 | {
|
---|
2062 | RT_NOREF(fResetOnLinux);
|
---|
2063 | PUSBMSD pThis = PDMINS_2_DATA(pUsbIns, PUSBMSD);
|
---|
2064 | LogFlow(("usbMsdUsbReset/#%u:\n", pUsbIns->iInstance));
|
---|
2065 | RTCritSectEnter(&pThis->CritSect);
|
---|
2066 |
|
---|
2067 | int rc = usbMsdResetWorker(pThis, NULL, false /*fSetConfig*/);
|
---|
2068 |
|
---|
2069 | RTCritSectLeave(&pThis->CritSect);
|
---|
2070 | return rc;
|
---|
2071 | }
|
---|
2072 |
|
---|
2073 |
|
---|
2074 | /**
|
---|
2075 | * @interface_method_impl{PDMUSBREG,pfnVMSuspend}
|
---|
2076 | */
|
---|
2077 | static DECLCALLBACK(void) usbMsdVMSuspend(PPDMUSBINS pUsbIns)
|
---|
2078 | {
|
---|
2079 | LogFlow(("usbMsdVMSuspend/#%u:\n", pUsbIns->iInstance));
|
---|
2080 | usbMsdSuspendOrPowerOff(pUsbIns);
|
---|
2081 | }
|
---|
2082 |
|
---|
2083 |
|
---|
2084 | /**
|
---|
2085 | * @interface_method_impl{PDMUSBREG,pfnVMSuspend}
|
---|
2086 | */
|
---|
2087 | static DECLCALLBACK(void) usbMsdVMPowerOff(PPDMUSBINS pUsbIns)
|
---|
2088 | {
|
---|
2089 | LogFlow(("usbMsdVMPowerOff/#%u:\n", pUsbIns->iInstance));
|
---|
2090 | usbMsdSuspendOrPowerOff(pUsbIns);
|
---|
2091 | }
|
---|
2092 |
|
---|
2093 |
|
---|
2094 | /**
|
---|
2095 | * @interface_method_impl{PDMUSBREG,pfnDriverAttach}
|
---|
2096 | */
|
---|
2097 | static DECLCALLBACK(int) usbMsdDriverAttach(PPDMUSBINS pUsbIns, unsigned iLUN, uint32_t fFlags)
|
---|
2098 | {
|
---|
2099 | RT_NOREF(fFlags);
|
---|
2100 | PUSBMSD pThis = PDMINS_2_DATA(pUsbIns, PUSBMSD);
|
---|
2101 |
|
---|
2102 | LogFlow(("usbMsdDriverAttach/#%u:\n", pUsbIns->iInstance));
|
---|
2103 |
|
---|
2104 | AssertMsg(iLUN == 0, ("UsbMsd: No other LUN than 0 is supported\n"));
|
---|
2105 | AssertMsg(fFlags & PDM_TACH_FLAGS_NOT_HOT_PLUG,
|
---|
2106 | ("UsbMsd: Device does not support hotplugging\n"));
|
---|
2107 |
|
---|
2108 | /* the usual paranoia */
|
---|
2109 | AssertRelease(!pThis->Lun0.pIBase);
|
---|
2110 | AssertRelease(!pThis->Lun0.pIMedia);
|
---|
2111 | AssertRelease(!pThis->Lun0.pIMediaEx);
|
---|
2112 |
|
---|
2113 | /*
|
---|
2114 | * Try attach the block device and get the interfaces,
|
---|
2115 | * required as well as optional.
|
---|
2116 | */
|
---|
2117 | int rc = PDMUsbHlpDriverAttach(pUsbIns, iLUN, &pThis->Lun0.IBase, &pThis->Lun0.pIBase, NULL);
|
---|
2118 | if (RT_SUCCESS(rc))
|
---|
2119 | {
|
---|
2120 | /* Get media and extended media interface. */
|
---|
2121 | pThis->Lun0.pIMedia = PDMIBASE_QUERY_INTERFACE(pThis->Lun0.pIBase, PDMIMEDIA);
|
---|
2122 | AssertMsgReturn(pThis->Lun0.pIMedia, ("Missing media interface below\n"), VERR_PDM_MISSING_INTERFACE);
|
---|
2123 | pThis->Lun0.pIMediaEx = PDMIBASE_QUERY_INTERFACE(pThis->Lun0.pIBase, PDMIMEDIAEX);
|
---|
2124 | AssertMsgReturn(pThis->Lun0.pIMediaEx, ("Missing extended media interface below\n"), VERR_PDM_MISSING_INTERFACE);
|
---|
2125 |
|
---|
2126 | rc = pThis->Lun0.pIMediaEx->pfnIoReqAllocSizeSet(pThis->Lun0.pIMediaEx, sizeof(USBMSDREQ));
|
---|
2127 | AssertMsgRCReturn(rc, ("MSD failed to set I/O request size!\n"), VERR_PDM_MISSING_INTERFACE);
|
---|
2128 | }
|
---|
2129 | else
|
---|
2130 | AssertMsgFailed(("Failed to attach LUN#%d. rc=%Rrc\n", iLUN, rc));
|
---|
2131 |
|
---|
2132 | if (RT_FAILURE(rc))
|
---|
2133 | {
|
---|
2134 | pThis->Lun0.pIBase = NULL;
|
---|
2135 | pThis->Lun0.pIMedia = NULL;
|
---|
2136 | pThis->Lun0.pIMediaEx = NULL;
|
---|
2137 | }
|
---|
2138 |
|
---|
2139 | pThis->fIsCdrom = false;
|
---|
2140 | PDMMEDIATYPE enmType = pThis->Lun0.pIMedia->pfnGetType(pThis->Lun0.pIMedia);
|
---|
2141 | /* Anything else will be reported as a hard disk. */
|
---|
2142 | if (enmType == PDMMEDIATYPE_CDROM || enmType == PDMMEDIATYPE_DVD)
|
---|
2143 | pThis->fIsCdrom = true;
|
---|
2144 |
|
---|
2145 | return rc;
|
---|
2146 | }
|
---|
2147 |
|
---|
2148 |
|
---|
2149 | /**
|
---|
2150 | * @interface_method_impl{PDMUSBREG,pfnDriverDetach}
|
---|
2151 | */
|
---|
2152 | static DECLCALLBACK(void) usbMsdDriverDetach(PPDMUSBINS pUsbIns, unsigned iLUN, uint32_t fFlags)
|
---|
2153 | {
|
---|
2154 | RT_NOREF(iLUN, fFlags);
|
---|
2155 | PUSBMSD pThis = PDMINS_2_DATA(pUsbIns, PUSBMSD);
|
---|
2156 |
|
---|
2157 | LogFlow(("usbMsdDriverDetach/#%u:\n", pUsbIns->iInstance));
|
---|
2158 |
|
---|
2159 | AssertMsg(iLUN == 0, ("UsbMsd: No other LUN than 0 is supported\n"));
|
---|
2160 | AssertMsg(fFlags & PDM_TACH_FLAGS_NOT_HOT_PLUG,
|
---|
2161 | ("UsbMsd: Device does not support hotplugging\n"));
|
---|
2162 |
|
---|
2163 | if (pThis->pReq)
|
---|
2164 | {
|
---|
2165 | usbMsdReqFree(pThis, pThis->pReq);
|
---|
2166 | pThis->pReq = NULL;
|
---|
2167 | }
|
---|
2168 |
|
---|
2169 | /*
|
---|
2170 | * Zero some important members.
|
---|
2171 | */
|
---|
2172 | pThis->Lun0.pIBase = NULL;
|
---|
2173 | pThis->Lun0.pIMedia = NULL;
|
---|
2174 | pThis->Lun0.pIMediaEx = NULL;
|
---|
2175 | }
|
---|
2176 |
|
---|
2177 |
|
---|
2178 | /**
|
---|
2179 | * @callback_method_impl{FNPDMDEVASYNCNOTIFY,
|
---|
2180 | * Callback employed by usbMsdVMReset.}
|
---|
2181 | */
|
---|
2182 | static DECLCALLBACK(bool) usbMsdIsAsyncResetDone(PPDMUSBINS pUsbIns)
|
---|
2183 | {
|
---|
2184 | PUSBMSD pThis = PDMINS_2_DATA(pUsbIns, PUSBMSD);
|
---|
2185 |
|
---|
2186 | if (!usbMsdAllAsyncIOIsFinished(pUsbIns))
|
---|
2187 | return false;
|
---|
2188 | ASMAtomicWriteBool(&pThis->fSignalIdle, false);
|
---|
2189 |
|
---|
2190 | int rc = usbMsdResetWorker(pThis, NULL, false /*fSetConfig*/);
|
---|
2191 | AssertRC(rc);
|
---|
2192 | return true;
|
---|
2193 | }
|
---|
2194 |
|
---|
2195 | /**
|
---|
2196 | * @interface_method_impl{PDMDEVREG,pfnReset}
|
---|
2197 | */
|
---|
2198 | static DECLCALLBACK(void) usbMsdVMReset(PPDMUSBINS pUsbIns)
|
---|
2199 | {
|
---|
2200 | PUSBMSD pThis = PDMINS_2_DATA(pUsbIns, PUSBMSD);
|
---|
2201 |
|
---|
2202 | ASMAtomicWriteBool(&pThis->fSignalIdle, true);
|
---|
2203 | if (!usbMsdAllAsyncIOIsFinished(pUsbIns))
|
---|
2204 | PDMUsbHlpSetAsyncNotification(pUsbIns, usbMsdIsAsyncResetDone);
|
---|
2205 | else
|
---|
2206 | {
|
---|
2207 | ASMAtomicWriteBool(&pThis->fSignalIdle, false);
|
---|
2208 | int rc = usbMsdResetWorker(pThis, NULL, false /*fSetConfig*/);
|
---|
2209 | AssertRC(rc);
|
---|
2210 | }
|
---|
2211 | }
|
---|
2212 |
|
---|
2213 |
|
---|
2214 | /**
|
---|
2215 | * @interface_method_impl{PDMUSBREG,pfnDestruct}
|
---|
2216 | */
|
---|
2217 | static DECLCALLBACK(void) usbMsdDestruct(PPDMUSBINS pUsbIns)
|
---|
2218 | {
|
---|
2219 | PDMUSB_CHECK_VERSIONS_RETURN_VOID(pUsbIns);
|
---|
2220 | PUSBMSD pThis = PDMINS_2_DATA(pUsbIns, PUSBMSD);
|
---|
2221 | LogFlow(("usbMsdDestruct/#%u:\n", pUsbIns->iInstance));
|
---|
2222 |
|
---|
2223 | if (RTCritSectIsInitialized(&pThis->CritSect))
|
---|
2224 | {
|
---|
2225 | RTCritSectEnter(&pThis->CritSect);
|
---|
2226 | RTCritSectLeave(&pThis->CritSect);
|
---|
2227 | RTCritSectDelete(&pThis->CritSect);
|
---|
2228 | }
|
---|
2229 |
|
---|
2230 | if (pThis->hEvtDoneQueue != NIL_RTSEMEVENT)
|
---|
2231 | {
|
---|
2232 | RTSemEventDestroy(pThis->hEvtDoneQueue);
|
---|
2233 | pThis->hEvtDoneQueue = NIL_RTSEMEVENT;
|
---|
2234 | }
|
---|
2235 |
|
---|
2236 | if (pThis->hEvtReset != NIL_RTSEMEVENTMULTI)
|
---|
2237 | {
|
---|
2238 | RTSemEventMultiDestroy(pThis->hEvtReset);
|
---|
2239 | pThis->hEvtReset = NIL_RTSEMEVENTMULTI;
|
---|
2240 | }
|
---|
2241 | }
|
---|
2242 |
|
---|
2243 |
|
---|
2244 | /**
|
---|
2245 | * @interface_method_impl{PDMUSBREG,pfnConstruct}
|
---|
2246 | */
|
---|
2247 | static DECLCALLBACK(int) usbMsdConstruct(PPDMUSBINS pUsbIns, int iInstance, PCFGMNODE pCfg, PCFGMNODE pCfgGlobal)
|
---|
2248 | {
|
---|
2249 | RT_NOREF(pCfgGlobal);
|
---|
2250 | PDMUSB_CHECK_VERSIONS_RETURN(pUsbIns);
|
---|
2251 | PUSBMSD pThis = PDMINS_2_DATA(pUsbIns, PUSBMSD);
|
---|
2252 | Log(("usbMsdConstruct/#%u:\n", iInstance));
|
---|
2253 |
|
---|
2254 | /*
|
---|
2255 | * Perform the basic structure initialization first so the destructor
|
---|
2256 | * will not misbehave.
|
---|
2257 | */
|
---|
2258 | pThis->pUsbIns = pUsbIns;
|
---|
2259 | pThis->hEvtDoneQueue = NIL_RTSEMEVENT;
|
---|
2260 | pThis->hEvtReset = NIL_RTSEMEVENTMULTI;
|
---|
2261 | pThis->Lun0.IBase.pfnQueryInterface = usbMsdLun0QueryInterface;
|
---|
2262 | pThis->Lun0.IMediaPort.pfnQueryDeviceLocation = usbMsdLun0QueryDeviceLocation;
|
---|
2263 | pThis->Lun0.IMediaExPort.pfnIoReqCompleteNotify = usbMsdLun0IoReqCompleteNotify;
|
---|
2264 | pThis->Lun0.IMediaExPort.pfnIoReqCopyFromBuf = usbMsdLun0IoReqCopyFromBuf;
|
---|
2265 | pThis->Lun0.IMediaExPort.pfnIoReqCopyToBuf = usbMsdLun0IoReqCopyToBuf;
|
---|
2266 | pThis->Lun0.IMediaExPort.pfnIoReqQueryDiscardRanges = NULL;
|
---|
2267 | pThis->Lun0.IMediaExPort.pfnIoReqStateChanged = usbMsdLun0IoReqStateChanged;
|
---|
2268 | pThis->Lun0.IMediaExPort.pfnMediumEjected = usbMsdLun0MediumEjected;
|
---|
2269 | usbMsdQueueInit(&pThis->ToHostQueue);
|
---|
2270 | usbMsdQueueInit(&pThis->DoneQueue);
|
---|
2271 |
|
---|
2272 | int rc = RTCritSectInit(&pThis->CritSect);
|
---|
2273 | AssertRCReturn(rc, rc);
|
---|
2274 |
|
---|
2275 | rc = RTSemEventCreate(&pThis->hEvtDoneQueue);
|
---|
2276 | AssertRCReturn(rc, rc);
|
---|
2277 |
|
---|
2278 | rc = RTSemEventMultiCreate(&pThis->hEvtReset);
|
---|
2279 | AssertRCReturn(rc, rc);
|
---|
2280 |
|
---|
2281 | /*
|
---|
2282 | * Validate and read the configuration.
|
---|
2283 | */
|
---|
2284 | rc = CFGMR3ValidateConfig(pCfg, "/", "", "", "UsbMsd", iInstance);
|
---|
2285 | if (RT_FAILURE(rc))
|
---|
2286 | return rc;
|
---|
2287 |
|
---|
2288 | /*
|
---|
2289 | * Attach the SCSI driver.
|
---|
2290 | */
|
---|
2291 | rc = PDMUsbHlpDriverAttach(pUsbIns, 0 /*iLun*/, &pThis->Lun0.IBase, &pThis->Lun0.pIBase, "SCSI Port");
|
---|
2292 | if (RT_FAILURE(rc))
|
---|
2293 | return PDMUsbHlpVMSetError(pUsbIns, rc, RT_SRC_POS, N_("MSD failed to attach SCSI driver"));
|
---|
2294 | pThis->Lun0.pIMedia = PDMIBASE_QUERY_INTERFACE(pThis->Lun0.pIBase, PDMIMEDIA);
|
---|
2295 | if (!pThis->Lun0.pIMedia)
|
---|
2296 | return PDMUsbHlpVMSetError(pUsbIns, VERR_PDM_MISSING_INTERFACE_BELOW, RT_SRC_POS,
|
---|
2297 | N_("MSD failed to query the PDMIMEDIA from the driver below it"));
|
---|
2298 | pThis->Lun0.pIMediaEx = PDMIBASE_QUERY_INTERFACE(pThis->Lun0.pIBase, PDMIMEDIAEX);
|
---|
2299 | if (!pThis->Lun0.pIMediaEx)
|
---|
2300 | return PDMUsbHlpVMSetError(pUsbIns, VERR_PDM_MISSING_INTERFACE_BELOW, RT_SRC_POS,
|
---|
2301 | N_("MSD failed to query the PDMIMEDIAEX from the driver below it"));
|
---|
2302 |
|
---|
2303 | /*
|
---|
2304 | * Find out what kind of device we are.
|
---|
2305 | */
|
---|
2306 | pThis->fIsCdrom = false;
|
---|
2307 | PDMMEDIATYPE enmType = pThis->Lun0.pIMedia->pfnGetType(pThis->Lun0.pIMedia);
|
---|
2308 | /* Anything else will be reported as a hard disk. */
|
---|
2309 | if (enmType == PDMMEDIATYPE_CDROM || enmType == PDMMEDIATYPE_DVD)
|
---|
2310 | pThis->fIsCdrom = true;
|
---|
2311 |
|
---|
2312 | rc = pThis->Lun0.pIMediaEx->pfnIoReqAllocSizeSet(pThis->Lun0.pIMediaEx, sizeof(USBMSDREQ));
|
---|
2313 | if (RT_FAILURE(rc))
|
---|
2314 | return PDMUsbHlpVMSetError(pUsbIns, rc, RT_SRC_POS, N_("MSD failed to set I/O request size!"));
|
---|
2315 |
|
---|
2316 | /*
|
---|
2317 | * Register the saved state data unit.
|
---|
2318 | */
|
---|
2319 | rc = PDMUsbHlpSSMRegister(pUsbIns, USB_MSD_SAVED_STATE_VERSION, sizeof(*pThis),
|
---|
2320 | NULL, usbMsdLiveExec, NULL,
|
---|
2321 | usbMsdSavePrep, usbMsdSaveExec, NULL,
|
---|
2322 | usbMsdLoadPrep, usbMsdLoadExec, NULL);
|
---|
2323 | if (RT_FAILURE(rc))
|
---|
2324 | return PDMUsbHlpVMSetError(pUsbIns, rc, RT_SRC_POS,
|
---|
2325 | N_("MSD failed to register SSM save state handlers"));
|
---|
2326 |
|
---|
2327 | return VINF_SUCCESS;
|
---|
2328 | }
|
---|
2329 |
|
---|
2330 |
|
---|
2331 | /**
|
---|
2332 | * The USB Mass Storage Device (MSD) registration record.
|
---|
2333 | */
|
---|
2334 | const PDMUSBREG g_UsbMsd =
|
---|
2335 | {
|
---|
2336 | /* u32Version */
|
---|
2337 | PDM_USBREG_VERSION,
|
---|
2338 | /* szName */
|
---|
2339 | "Msd",
|
---|
2340 | /* pszDescription */
|
---|
2341 | "USB Mass Storage Device, one LUN.",
|
---|
2342 | /* fFlags */
|
---|
2343 | PDM_USBREG_HIGHSPEED_CAPABLE | PDM_USBREG_SUPERSPEED_CAPABLE
|
---|
2344 | | PDM_USBREG_SAVED_STATE_SUPPORTED,
|
---|
2345 | /* cMaxInstances */
|
---|
2346 | ~0U,
|
---|
2347 | /* cbInstance */
|
---|
2348 | sizeof(USBMSD),
|
---|
2349 | /* pfnConstruct */
|
---|
2350 | usbMsdConstruct,
|
---|
2351 | /* pfnDestruct */
|
---|
2352 | usbMsdDestruct,
|
---|
2353 | /* pfnVMInitComplete */
|
---|
2354 | NULL,
|
---|
2355 | /* pfnVMPowerOn */
|
---|
2356 | NULL,
|
---|
2357 | /* pfnVMReset */
|
---|
2358 | usbMsdVMReset,
|
---|
2359 | /* pfnVMSuspend */
|
---|
2360 | usbMsdVMSuspend,
|
---|
2361 | /* pfnVMResume */
|
---|
2362 | NULL,
|
---|
2363 | /* pfnVMPowerOff */
|
---|
2364 | usbMsdVMPowerOff,
|
---|
2365 | /* pfnHotPlugged */
|
---|
2366 | NULL,
|
---|
2367 | /* pfnHotUnplugged */
|
---|
2368 | NULL,
|
---|
2369 | /* pfnDriverAttach */
|
---|
2370 | usbMsdDriverAttach,
|
---|
2371 | /* pfnDriverDetach */
|
---|
2372 | usbMsdDriverDetach,
|
---|
2373 | /* pfnQueryInterface */
|
---|
2374 | NULL,
|
---|
2375 | /* pfnUsbReset */
|
---|
2376 | usbMsdUsbReset,
|
---|
2377 | /* pfnUsbGetCachedDescriptors */
|
---|
2378 | usbMsdUsbGetDescriptorCache,
|
---|
2379 | /* pfnUsbSetConfiguration */
|
---|
2380 | usbMsdUsbSetConfiguration,
|
---|
2381 | /* pfnUsbSetInterface */
|
---|
2382 | usbMsdUsbSetInterface,
|
---|
2383 | /* pfnUsbClearHaltedEndpoint */
|
---|
2384 | usbMsdUsbClearHaltedEndpoint,
|
---|
2385 | /* pfnUrbNew */
|
---|
2386 | NULL/*usbMsdUrbNew*/,
|
---|
2387 | /* pfnQueue */
|
---|
2388 | usbMsdQueue,
|
---|
2389 | /* pfnUrbCancel */
|
---|
2390 | usbMsdUrbCancel,
|
---|
2391 | /* pfnUrbReap */
|
---|
2392 | usbMsdUrbReap,
|
---|
2393 | /* pfnWakeup */
|
---|
2394 | usbMsdWakeup,
|
---|
2395 | /* u32TheEnd */
|
---|
2396 | PDM_USBREG_VERSION
|
---|
2397 | };
|
---|
2398 |
|
---|