]>
Commit | Line | Data |
---|---|---|
5fafdf24 | 1 | /* |
2e5d83bb PB |
2 | * USB Mass Storage Device emulation |
3 | * | |
4 | * Copyright (c) 2006 CodeSourcery. | |
5 | * Written by Paul Brook | |
6 | * | |
8e31bf38 | 7 | * This code is licensed under the LGPL. |
2e5d83bb PB |
8 | */ |
9 | ||
e532b2e0 | 10 | #include "qemu/osdep.h" |
87ecb68b | 11 | #include "qemu-common.h" |
d49b6836 | 12 | #include "qemu/error-report.h" |
1de7afc9 PB |
13 | #include "qemu/option.h" |
14 | #include "qemu/config-file.h" | |
f1ae32a1 GH |
15 | #include "hw/usb.h" |
16 | #include "hw/usb/desc.h" | |
0d09e41a | 17 | #include "hw/scsi/scsi.h" |
28ecbaee | 18 | #include "ui/console.h" |
83c9089e | 19 | #include "monitor/monitor.h" |
9c17d615 | 20 | #include "sysemu/sysemu.h" |
fa1d36df | 21 | #include "sysemu/block-backend.h" |
9c17d615 | 22 | #include "sysemu/blockdev.h" |
89f0762d | 23 | #include "qapi/visitor.h" |
2e5d83bb PB |
24 | |
25 | //#define DEBUG_MSD | |
26 | ||
27 | #ifdef DEBUG_MSD | |
001faf32 BS |
28 | #define DPRINTF(fmt, ...) \ |
29 | do { printf("usb-msd: " fmt , ## __VA_ARGS__); } while (0) | |
2e5d83bb | 30 | #else |
001faf32 | 31 | #define DPRINTF(fmt, ...) do {} while(0) |
2e5d83bb PB |
32 | #endif |
33 | ||
34 | /* USB requests. */ | |
35 | #define MassStorageReset 0xff | |
36 | #define GetMaxLun 0xfe | |
37 | ||
38 | enum USBMSDMode { | |
39 | USB_MSDM_CBW, /* Command Block. */ | |
94843f66 | 40 | USB_MSDM_DATAOUT, /* Transfer data to device. */ |
2e5d83bb PB |
41 | USB_MSDM_DATAIN, /* Transfer data from device. */ |
42 | USB_MSDM_CSW /* Command Status. */ | |
43 | }; | |
44 | ||
92a114f6 GH |
45 | struct usb_msd_csw { |
46 | uint32_t sig; | |
47 | uint32_t tag; | |
48 | uint32_t residue; | |
49 | uint8_t status; | |
50 | }; | |
51 | ||
2e5d83bb PB |
52 | typedef struct { |
53 | USBDevice dev; | |
54 | enum USBMSDMode mode; | |
1dc90367 | 55 | uint32_t scsi_off; |
a917d384 | 56 | uint32_t scsi_len; |
2e5d83bb | 57 | uint32_t data_len; |
92a114f6 | 58 | struct usb_msd_csw csw; |
5c6c0e51 | 59 | SCSIRequest *req; |
ca9c39fa | 60 | SCSIBus bus; |
34707333 GH |
61 | /* For async completion. */ |
62 | USBPacket *packet; | |
63 | /* usb-storage only */ | |
428c149b | 64 | BlockConf conf; |
6bb7b867 | 65 | uint32_t removable; |
89f0762d | 66 | SCSIDevice *scsi_dev; |
2e5d83bb PB |
67 | } MSDState; |
68 | ||
79e2590c GA |
69 | #define TYPE_USB_STORAGE "usb-storage-dev" |
70 | #define USB_STORAGE_DEV(obj) OBJECT_CHECK(MSDState, (obj), TYPE_USB_STORAGE) | |
71 | ||
a917d384 PB |
72 | struct usb_msd_cbw { |
73 | uint32_t sig; | |
74 | uint32_t tag; | |
75 | uint32_t data_len; | |
76 | uint8_t flags; | |
77 | uint8_t lun; | |
78 | uint8_t cmd_len; | |
79 | uint8_t cmd[16]; | |
80 | }; | |
81 | ||
81bfd2f2 GH |
82 | enum { |
83 | STR_MANUFACTURER = 1, | |
84 | STR_PRODUCT, | |
85 | STR_SERIALNUMBER, | |
ca0c730d GH |
86 | STR_CONFIG_FULL, |
87 | STR_CONFIG_HIGH, | |
79b40459 | 88 | STR_CONFIG_SUPER, |
2e5d83bb PB |
89 | }; |
90 | ||
81bfd2f2 | 91 | static const USBDescStrings desc_strings = { |
93bfef4c | 92 | [STR_MANUFACTURER] = "QEMU", |
81bfd2f2 GH |
93 | [STR_PRODUCT] = "QEMU USB HARDDRIVE", |
94 | [STR_SERIALNUMBER] = "1", | |
ca0c730d GH |
95 | [STR_CONFIG_FULL] = "Full speed config (usb 1.1)", |
96 | [STR_CONFIG_HIGH] = "High speed config (usb 2.0)", | |
79b40459 | 97 | [STR_CONFIG_SUPER] = "Super speed config (usb 3.0)", |
81bfd2f2 GH |
98 | }; |
99 | ||
ca0c730d | 100 | static const USBDescIface desc_iface_full = { |
81bfd2f2 GH |
101 | .bInterfaceNumber = 0, |
102 | .bNumEndpoints = 2, | |
103 | .bInterfaceClass = USB_CLASS_MASS_STORAGE, | |
104 | .bInterfaceSubClass = 0x06, /* SCSI */ | |
105 | .bInterfaceProtocol = 0x50, /* Bulk */ | |
106 | .eps = (USBDescEndpoint[]) { | |
107 | { | |
108 | .bEndpointAddress = USB_DIR_IN | 0x01, | |
109 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | |
110 | .wMaxPacketSize = 64, | |
111 | },{ | |
112 | .bEndpointAddress = USB_DIR_OUT | 0x02, | |
113 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | |
114 | .wMaxPacketSize = 64, | |
115 | }, | |
116 | } | |
117 | }; | |
118 | ||
ca0c730d GH |
119 | static const USBDescDevice desc_device_full = { |
120 | .bcdUSB = 0x0200, | |
81bfd2f2 GH |
121 | .bMaxPacketSize0 = 8, |
122 | .bNumConfigurations = 1, | |
123 | .confs = (USBDescConfig[]) { | |
124 | { | |
125 | .bNumInterfaces = 1, | |
126 | .bConfigurationValue = 1, | |
ca0c730d | 127 | .iConfiguration = STR_CONFIG_FULL, |
bd93976a | 128 | .bmAttributes = USB_CFG_ATT_ONE | USB_CFG_ATT_SELFPOWER, |
add75088 | 129 | .nif = 1, |
ca0c730d GH |
130 | .ifs = &desc_iface_full, |
131 | }, | |
132 | }, | |
133 | }; | |
134 | ||
135 | static const USBDescIface desc_iface_high = { | |
136 | .bInterfaceNumber = 0, | |
137 | .bNumEndpoints = 2, | |
138 | .bInterfaceClass = USB_CLASS_MASS_STORAGE, | |
139 | .bInterfaceSubClass = 0x06, /* SCSI */ | |
140 | .bInterfaceProtocol = 0x50, /* Bulk */ | |
141 | .eps = (USBDescEndpoint[]) { | |
142 | { | |
143 | .bEndpointAddress = USB_DIR_IN | 0x01, | |
144 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | |
145 | .wMaxPacketSize = 512, | |
146 | },{ | |
147 | .bEndpointAddress = USB_DIR_OUT | 0x02, | |
148 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | |
149 | .wMaxPacketSize = 512, | |
150 | }, | |
151 | } | |
152 | }; | |
153 | ||
154 | static const USBDescDevice desc_device_high = { | |
155 | .bcdUSB = 0x0200, | |
156 | .bMaxPacketSize0 = 64, | |
157 | .bNumConfigurations = 1, | |
158 | .confs = (USBDescConfig[]) { | |
159 | { | |
160 | .bNumInterfaces = 1, | |
161 | .bConfigurationValue = 1, | |
162 | .iConfiguration = STR_CONFIG_HIGH, | |
bd93976a | 163 | .bmAttributes = USB_CFG_ATT_ONE | USB_CFG_ATT_SELFPOWER, |
add75088 | 164 | .nif = 1, |
ca0c730d | 165 | .ifs = &desc_iface_high, |
81bfd2f2 GH |
166 | }, |
167 | }, | |
168 | }; | |
169 | ||
79b40459 GH |
170 | static const USBDescIface desc_iface_super = { |
171 | .bInterfaceNumber = 0, | |
172 | .bNumEndpoints = 2, | |
173 | .bInterfaceClass = USB_CLASS_MASS_STORAGE, | |
174 | .bInterfaceSubClass = 0x06, /* SCSI */ | |
175 | .bInterfaceProtocol = 0x50, /* Bulk */ | |
176 | .eps = (USBDescEndpoint[]) { | |
177 | { | |
178 | .bEndpointAddress = USB_DIR_IN | 0x01, | |
179 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | |
180 | .wMaxPacketSize = 1024, | |
181 | .bMaxBurst = 15, | |
182 | },{ | |
183 | .bEndpointAddress = USB_DIR_OUT | 0x02, | |
184 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | |
185 | .wMaxPacketSize = 1024, | |
186 | .bMaxBurst = 15, | |
187 | }, | |
188 | } | |
189 | }; | |
190 | ||
191 | static const USBDescDevice desc_device_super = { | |
192 | .bcdUSB = 0x0300, | |
193 | .bMaxPacketSize0 = 9, | |
194 | .bNumConfigurations = 1, | |
195 | .confs = (USBDescConfig[]) { | |
196 | { | |
197 | .bNumInterfaces = 1, | |
198 | .bConfigurationValue = 1, | |
199 | .iConfiguration = STR_CONFIG_SUPER, | |
bd93976a | 200 | .bmAttributes = USB_CFG_ATT_ONE | USB_CFG_ATT_SELFPOWER, |
79b40459 GH |
201 | .nif = 1, |
202 | .ifs = &desc_iface_super, | |
203 | }, | |
204 | }, | |
205 | }; | |
206 | ||
81bfd2f2 GH |
207 | static const USBDesc desc = { |
208 | .id = { | |
db80358a RT |
209 | .idVendor = 0x46f4, /* CRC16() of "QEMU" */ |
210 | .idProduct = 0x0001, | |
81bfd2f2 GH |
211 | .bcdDevice = 0, |
212 | .iManufacturer = STR_MANUFACTURER, | |
213 | .iProduct = STR_PRODUCT, | |
214 | .iSerialNumber = STR_SERIALNUMBER, | |
215 | }, | |
79b40459 GH |
216 | .full = &desc_device_full, |
217 | .high = &desc_device_high, | |
218 | .super = &desc_device_super, | |
219 | .str = desc_strings, | |
2e5d83bb PB |
220 | }; |
221 | ||
29c74f76 | 222 | static void usb_msd_copy_data(MSDState *s, USBPacket *p) |
a917d384 PB |
223 | { |
224 | uint32_t len; | |
9a77a0f5 | 225 | len = p->iov.size - p->actual_length; |
a917d384 PB |
226 | if (len > s->scsi_len) |
227 | len = s->scsi_len; | |
1dc90367 | 228 | usb_packet_copy(p, scsi_req_get_buf(s->req) + s->scsi_off, len); |
a917d384 | 229 | s->scsi_len -= len; |
1dc90367 | 230 | s->scsi_off += len; |
a917d384 | 231 | s->data_len -= len; |
fa7935c1 | 232 | if (s->scsi_len == 0 || s->data_len == 0) { |
ad3376cc | 233 | scsi_req_continue(s->req); |
a917d384 PB |
234 | } |
235 | } | |
236 | ||
ab4797ad | 237 | static void usb_msd_send_status(MSDState *s, USBPacket *p) |
a917d384 | 238 | { |
ab4797ad | 239 | int len; |
a917d384 | 240 | |
e04da7c3 | 241 | DPRINTF("Command status %d tag 0x%x, len %zd\n", |
e2854bf3 | 242 | s->csw.status, le32_to_cpu(s->csw.tag), p->iov.size); |
92a114f6 | 243 | |
e2854bf3 | 244 | assert(s->csw.sig == cpu_to_le32(0x53425355)); |
92a114f6 GH |
245 | len = MIN(sizeof(s->csw), p->iov.size); |
246 | usb_packet_copy(p, &s->csw, len); | |
247 | memset(&s->csw, 0, sizeof(s->csw)); | |
a917d384 PB |
248 | } |
249 | ||
1e6ed80b GH |
250 | static void usb_msd_packet_complete(MSDState *s) |
251 | { | |
252 | USBPacket *p = s->packet; | |
253 | ||
254 | /* Set s->packet to NULL before calling usb_packet_complete | |
255 | because another request may be issued before | |
256 | usb_packet_complete returns. */ | |
257 | DPRINTF("Packet complete %p\n", p); | |
258 | s->packet = NULL; | |
259 | usb_packet_complete(&s->dev, p); | |
260 | } | |
261 | ||
aba1f023 | 262 | static void usb_msd_transfer_data(SCSIRequest *req, uint32_t len) |
2e5d83bb | 263 | { |
5c6c0e51 | 264 | MSDState *s = DO_UPCAST(MSDState, dev.qdev, req->bus->qbus.parent); |
a917d384 | 265 | USBPacket *p = s->packet; |
4d611c9a | 266 | |
ad3376cc | 267 | assert((s->mode == USB_MSDM_DATAOUT) == (req->cmd.mode == SCSI_XFER_TO_DEV)); |
aba1f023 | 268 | s->scsi_len = len; |
1dc90367 | 269 | s->scsi_off = 0; |
a917d384 | 270 | if (p) { |
29c74f76 GH |
271 | usb_msd_copy_data(s, p); |
272 | p = s->packet; | |
9a77a0f5 HG |
273 | if (p && p->actual_length == p->iov.size) { |
274 | p->status = USB_RET_SUCCESS; /* Clear previous ASYNC status */ | |
1e6ed80b | 275 | usb_msd_packet_complete(s); |
a917d384 | 276 | } |
4d611c9a | 277 | } |
2e5d83bb PB |
278 | } |
279 | ||
01e95455 | 280 | static void usb_msd_command_complete(SCSIRequest *req, uint32_t status, size_t resid) |
c6df7102 PB |
281 | { |
282 | MSDState *s = DO_UPCAST(MSDState, dev.qdev, req->bus->qbus.parent); | |
283 | USBPacket *p = s->packet; | |
284 | ||
7b863f41 | 285 | DPRINTF("Command complete %d tag 0x%x\n", status, req->tag); |
92a114f6 GH |
286 | |
287 | s->csw.sig = cpu_to_le32(0x53425355); | |
7b863f41 | 288 | s->csw.tag = cpu_to_le32(req->tag); |
0659879e | 289 | s->csw.residue = cpu_to_le32(s->data_len); |
414c4604 | 290 | s->csw.status = status != 0; |
92a114f6 | 291 | |
c6df7102 PB |
292 | if (s->packet) { |
293 | if (s->data_len == 0 && s->mode == USB_MSDM_DATAOUT) { | |
294 | /* A deferred packet with no write data remaining must be | |
295 | the status read packet. */ | |
296 | usb_msd_send_status(s, p); | |
297 | s->mode = USB_MSDM_CBW; | |
54414218 GH |
298 | } else if (s->mode == USB_MSDM_CSW) { |
299 | usb_msd_send_status(s, p); | |
300 | s->mode = USB_MSDM_CBW; | |
c6df7102 PB |
301 | } else { |
302 | if (s->data_len) { | |
9a77a0f5 | 303 | int len = (p->iov.size - p->actual_length); |
29c74f76 GH |
304 | usb_packet_skip(p, len); |
305 | s->data_len -= len; | |
c6df7102 PB |
306 | } |
307 | if (s->data_len == 0) { | |
308 | s->mode = USB_MSDM_CSW; | |
309 | } | |
310 | } | |
9a77a0f5 | 311 | p->status = USB_RET_SUCCESS; /* Clear previous ASYNC status */ |
1e6ed80b | 312 | usb_msd_packet_complete(s); |
c6df7102 PB |
313 | } else if (s->data_len == 0) { |
314 | s->mode = USB_MSDM_CSW; | |
315 | } | |
316 | scsi_req_unref(req); | |
317 | s->req = NULL; | |
318 | } | |
319 | ||
94d3f98a PB |
320 | static void usb_msd_request_cancelled(SCSIRequest *req) |
321 | { | |
322 | MSDState *s = DO_UPCAST(MSDState, dev.qdev, req->bus->qbus.parent); | |
323 | ||
324 | if (req == s->req) { | |
325 | scsi_req_unref(s->req); | |
326 | s->req = NULL; | |
94d3f98a PB |
327 | s->scsi_len = 0; |
328 | } | |
329 | } | |
330 | ||
059809e4 | 331 | static void usb_msd_handle_reset(USBDevice *dev) |
2e5d83bb PB |
332 | { |
333 | MSDState *s = (MSDState *)dev; | |
334 | ||
335 | DPRINTF("Reset\n"); | |
24a5bbe1 GH |
336 | if (s->req) { |
337 | scsi_req_cancel(s->req); | |
338 | } | |
339 | assert(s->req == NULL); | |
340 | ||
341 | if (s->packet) { | |
9a77a0f5 | 342 | s->packet->status = USB_RET_STALL; |
1e6ed80b | 343 | usb_msd_packet_complete(s); |
24a5bbe1 GH |
344 | } |
345 | ||
2e5d83bb | 346 | s->mode = USB_MSDM_CBW; |
2e5d83bb PB |
347 | } |
348 | ||
9a77a0f5 | 349 | static void usb_msd_handle_control(USBDevice *dev, USBPacket *p, |
007fd62f | 350 | int request, int value, int index, int length, uint8_t *data) |
2e5d83bb PB |
351 | { |
352 | MSDState *s = (MSDState *)dev; | |
34707333 GH |
353 | SCSIDevice *scsi_dev; |
354 | int ret, maxlun; | |
2e5d83bb | 355 | |
007fd62f | 356 | ret = usb_desc_handle_control(dev, p, request, value, index, length, data); |
81bfd2f2 | 357 | if (ret >= 0) { |
9a77a0f5 | 358 | return; |
81bfd2f2 GH |
359 | } |
360 | ||
2e5d83bb | 361 | switch (request) { |
2e5d83bb | 362 | case EndpointOutRequest | USB_REQ_CLEAR_FEATURE: |
e5322f76 | 363 | break; |
2e5d83bb | 364 | /* Class specific requests. */ |
f3571b1a | 365 | case ClassInterfaceOutRequest | MassStorageReset: |
2e5d83bb PB |
366 | /* Reset state ready for the next CBW. */ |
367 | s->mode = USB_MSDM_CBW; | |
2e5d83bb | 368 | break; |
f3571b1a | 369 | case ClassInterfaceRequest | GetMaxLun: |
34707333 GH |
370 | maxlun = 0; |
371 | for (;;) { | |
372 | scsi_dev = scsi_device_find(&s->bus, 0, 0, maxlun+1); | |
373 | if (scsi_dev == NULL) { | |
374 | break; | |
375 | } | |
376 | if (scsi_dev->lun != maxlun+1) { | |
377 | break; | |
378 | } | |
379 | maxlun++; | |
380 | } | |
381 | DPRINTF("MaxLun %d\n", maxlun); | |
382 | data[0] = maxlun; | |
9a77a0f5 | 383 | p->actual_length = 1; |
2e5d83bb PB |
384 | break; |
385 | default: | |
9a77a0f5 | 386 | p->status = USB_RET_STALL; |
2e5d83bb PB |
387 | break; |
388 | } | |
2e5d83bb PB |
389 | } |
390 | ||
eb5e680a | 391 | static void usb_msd_cancel_io(USBDevice *dev, USBPacket *p) |
4d611c9a | 392 | { |
79e2590c | 393 | MSDState *s = USB_STORAGE_DEV(dev); |
d3ac1a87 | 394 | |
6d7aeeeb GH |
395 | assert(s->packet == p); |
396 | s->packet = NULL; | |
397 | ||
d3ac1a87 GH |
398 | if (s->req) { |
399 | scsi_req_cancel(s->req); | |
400 | } | |
4d611c9a PB |
401 | } |
402 | ||
9a77a0f5 | 403 | static void usb_msd_handle_data(USBDevice *dev, USBPacket *p) |
2e5d83bb PB |
404 | { |
405 | MSDState *s = (MSDState *)dev; | |
7b863f41 | 406 | uint32_t tag; |
2e5d83bb | 407 | struct usb_msd_cbw cbw; |
079d0b7f | 408 | uint8_t devep = p->ep->nr; |
34707333 | 409 | SCSIDevice *scsi_dev; |
9db7c414 | 410 | uint32_t len; |
2e5d83bb | 411 | |
4d611c9a | 412 | switch (p->pid) { |
2e5d83bb PB |
413 | case USB_TOKEN_OUT: |
414 | if (devep != 2) | |
415 | goto fail; | |
416 | ||
417 | switch (s->mode) { | |
418 | case USB_MSDM_CBW: | |
29c74f76 | 419 | if (p->iov.size != 31) { |
f5dc5978 | 420 | error_report("usb-msd: Bad CBW size"); |
2e5d83bb PB |
421 | goto fail; |
422 | } | |
29c74f76 | 423 | usb_packet_copy(p, &cbw, 31); |
2e5d83bb | 424 | if (le32_to_cpu(cbw.sig) != 0x43425355) { |
f5dc5978 GA |
425 | error_report("usb-msd: Bad signature %08x", |
426 | le32_to_cpu(cbw.sig)); | |
2e5d83bb PB |
427 | goto fail; |
428 | } | |
429 | DPRINTF("Command on LUN %d\n", cbw.lun); | |
34707333 GH |
430 | scsi_dev = scsi_device_find(&s->bus, 0, 0, cbw.lun); |
431 | if (scsi_dev == NULL) { | |
f5dc5978 | 432 | error_report("usb-msd: Bad LUN %d", cbw.lun); |
2e5d83bb PB |
433 | goto fail; |
434 | } | |
7b863f41 | 435 | tag = le32_to_cpu(cbw.tag); |
2e5d83bb PB |
436 | s->data_len = le32_to_cpu(cbw.data_len); |
437 | if (s->data_len == 0) { | |
438 | s->mode = USB_MSDM_CSW; | |
439 | } else if (cbw.flags & 0x80) { | |
440 | s->mode = USB_MSDM_DATAIN; | |
441 | } else { | |
442 | s->mode = USB_MSDM_DATAOUT; | |
443 | } | |
444 | DPRINTF("Command tag 0x%x flags %08x len %d data %d\n", | |
7b863f41 | 445 | tag, cbw.flags, cbw.cmd_len, s->data_len); |
0659879e | 446 | assert(le32_to_cpu(s->csw.residue) == 0); |
ef0bdf77 | 447 | s->scsi_len = 0; |
34707333 | 448 | s->req = scsi_req_new(scsi_dev, tag, cbw.lun, cbw.cmd, NULL); |
06f9847d GH |
449 | #ifdef DEBUG_MSD |
450 | scsi_req_print(s->req); | |
451 | #endif | |
9db7c414 GH |
452 | len = scsi_req_enqueue(s->req); |
453 | if (len) { | |
ad3376cc | 454 | scsi_req_continue(s->req); |
a917d384 | 455 | } |
2e5d83bb PB |
456 | break; |
457 | ||
458 | case USB_MSDM_DATAOUT: | |
29c74f76 GH |
459 | DPRINTF("Data out %zd/%d\n", p->iov.size, s->data_len); |
460 | if (p->iov.size > s->data_len) { | |
2e5d83bb | 461 | goto fail; |
29c74f76 | 462 | } |
2e5d83bb | 463 | |
a917d384 | 464 | if (s->scsi_len) { |
29c74f76 | 465 | usb_msd_copy_data(s, p); |
a917d384 | 466 | } |
0659879e | 467 | if (le32_to_cpu(s->csw.residue)) { |
9a77a0f5 | 468 | int len = p->iov.size - p->actual_length; |
29c74f76 GH |
469 | if (len) { |
470 | usb_packet_skip(p, len); | |
471 | s->data_len -= len; | |
472 | if (s->data_len == 0) { | |
473 | s->mode = USB_MSDM_CSW; | |
474 | } | |
475 | } | |
a917d384 | 476 | } |
9a77a0f5 | 477 | if (p->actual_length < p->iov.size) { |
06f9847d | 478 | DPRINTF("Deferring packet %p [wait data-out]\n", p); |
4d611c9a | 479 | s->packet = p; |
9a77a0f5 | 480 | p->status = USB_RET_ASYNC; |
4d611c9a | 481 | } |
2e5d83bb PB |
482 | break; |
483 | ||
484 | default: | |
29c74f76 | 485 | DPRINTF("Unexpected write (len %zd)\n", p->iov.size); |
2e5d83bb PB |
486 | goto fail; |
487 | } | |
488 | break; | |
489 | ||
490 | case USB_TOKEN_IN: | |
491 | if (devep != 1) | |
492 | goto fail; | |
493 | ||
494 | switch (s->mode) { | |
a917d384 | 495 | case USB_MSDM_DATAOUT: |
29c74f76 | 496 | if (s->data_len != 0 || p->iov.size < 13) { |
a917d384 | 497 | goto fail; |
29c74f76 | 498 | } |
a917d384 | 499 | /* Waiting for SCSI write to complete. */ |
a917d384 | 500 | s->packet = p; |
9a77a0f5 | 501 | p->status = USB_RET_ASYNC; |
a917d384 PB |
502 | break; |
503 | ||
2e5d83bb | 504 | case USB_MSDM_CSW: |
29c74f76 | 505 | if (p->iov.size < 13) { |
2e5d83bb | 506 | goto fail; |
29c74f76 | 507 | } |
2e5d83bb | 508 | |
59310659 GH |
509 | if (s->req) { |
510 | /* still in flight */ | |
06f9847d | 511 | DPRINTF("Deferring packet %p [wait status]\n", p); |
59310659 | 512 | s->packet = p; |
9a77a0f5 | 513 | p->status = USB_RET_ASYNC; |
59310659 GH |
514 | } else { |
515 | usb_msd_send_status(s, p); | |
516 | s->mode = USB_MSDM_CBW; | |
59310659 | 517 | } |
2e5d83bb PB |
518 | break; |
519 | ||
520 | case USB_MSDM_DATAIN: | |
29c74f76 GH |
521 | DPRINTF("Data in %zd/%d, scsi_len %d\n", |
522 | p->iov.size, s->data_len, s->scsi_len); | |
a917d384 | 523 | if (s->scsi_len) { |
29c74f76 | 524 | usb_msd_copy_data(s, p); |
a917d384 | 525 | } |
0659879e | 526 | if (le32_to_cpu(s->csw.residue)) { |
9a77a0f5 | 527 | int len = p->iov.size - p->actual_length; |
29c74f76 GH |
528 | if (len) { |
529 | usb_packet_skip(p, len); | |
530 | s->data_len -= len; | |
531 | if (s->data_len == 0) { | |
532 | s->mode = USB_MSDM_CSW; | |
533 | } | |
534 | } | |
a917d384 | 535 | } |
9a77a0f5 | 536 | if (p->actual_length < p->iov.size) { |
06f9847d | 537 | DPRINTF("Deferring packet %p [wait data-in]\n", p); |
4d611c9a | 538 | s->packet = p; |
9a77a0f5 | 539 | p->status = USB_RET_ASYNC; |
4d611c9a | 540 | } |
2e5d83bb PB |
541 | break; |
542 | ||
543 | default: | |
29c74f76 | 544 | DPRINTF("Unexpected read (len %zd)\n", p->iov.size); |
2e5d83bb PB |
545 | goto fail; |
546 | } | |
547 | break; | |
548 | ||
549 | default: | |
550 | DPRINTF("Bad token\n"); | |
551 | fail: | |
9a77a0f5 | 552 | p->status = USB_RET_STALL; |
2e5d83bb PB |
553 | break; |
554 | } | |
2e5d83bb PB |
555 | } |
556 | ||
b3e461d3 GH |
557 | static void usb_msd_password_cb(void *opaque, int err) |
558 | { | |
559 | MSDState *s = opaque; | |
7d553f27 | 560 | Error *local_err = NULL; |
b3e461d3 | 561 | |
7d553f27 GA |
562 | if (!err) { |
563 | usb_device_attach(&s->dev, &local_err); | |
564 | } | |
fa19bf83 | 565 | |
7d553f27 | 566 | if (local_err) { |
c326529b | 567 | error_report_err(local_err); |
56f9107e | 568 | qdev_unplug(&s->dev.qdev, NULL); |
7d553f27 | 569 | } |
b3e461d3 GH |
570 | } |
571 | ||
5de88b1d GH |
572 | static void *usb_msd_load_request(QEMUFile *f, SCSIRequest *req) |
573 | { | |
574 | MSDState *s = DO_UPCAST(MSDState, dev.qdev, req->bus->qbus.parent); | |
575 | ||
576 | /* nothing to load, just store req in our state struct */ | |
577 | assert(s->req == NULL); | |
578 | scsi_req_ref(req); | |
579 | s->req = req; | |
580 | return NULL; | |
581 | } | |
582 | ||
34707333 | 583 | static const struct SCSIBusInfo usb_msd_scsi_info_storage = { |
afd4030c | 584 | .tcq = false, |
7e0380b9 PB |
585 | .max_target = 0, |
586 | .max_lun = 0, | |
afd4030c | 587 | |
c6df7102 | 588 | .transfer_data = usb_msd_transfer_data, |
94d3f98a | 589 | .complete = usb_msd_command_complete, |
5de88b1d GH |
590 | .cancel = usb_msd_request_cancelled, |
591 | .load_request = usb_msd_load_request, | |
cfdc1bb0 PB |
592 | }; |
593 | ||
34707333 GH |
594 | static const struct SCSIBusInfo usb_msd_scsi_info_bot = { |
595 | .tcq = false, | |
596 | .max_target = 0, | |
597 | .max_lun = 15, | |
598 | ||
599 | .transfer_data = usb_msd_transfer_data, | |
600 | .complete = usb_msd_command_complete, | |
601 | .cancel = usb_msd_request_cancelled, | |
602 | .load_request = usb_msd_load_request, | |
603 | }; | |
604 | ||
5a882e40 | 605 | static void usb_msd_realize_storage(USBDevice *dev, Error **errp) |
806b6024 | 606 | { |
79e2590c | 607 | MSDState *s = USB_STORAGE_DEV(dev); |
4be74634 | 608 | BlockBackend *blk = s->conf.blk; |
34707333 | 609 | SCSIDevice *scsi_dev; |
caad4eb3 | 610 | Error *err = NULL; |
806b6024 | 611 | |
4be74634 | 612 | if (!blk) { |
5a882e40 GA |
613 | error_setg(errp, "drive property not set"); |
614 | return; | |
7fc2f2c0 GH |
615 | } |
616 | ||
7d3467d9 HR |
617 | if (blk_bs(blk)) { |
618 | bdrv_add_key(blk_bs(blk), NULL, &err); | |
619 | if (err) { | |
620 | if (monitor_cur_is_qmp()) { | |
621 | error_propagate(errp, err); | |
622 | return; | |
623 | } | |
624 | error_free(err); | |
625 | err = NULL; | |
626 | if (cur_mon) { | |
627 | monitor_read_bdrv_key_start(cur_mon, blk_bs(blk), | |
628 | usb_msd_password_cb, s); | |
629 | s->dev.auto_attach = 0; | |
630 | } else { | |
631 | autostart = 0; | |
632 | } | |
7afcc1f9 MA |
633 | } |
634 | } | |
635 | ||
71938a09 | 636 | blkconf_serial(&s->conf, &dev->serial); |
0eb28a42 | 637 | blkconf_blocksizes(&s->conf); |
911525db | 638 | |
14bafc54 MA |
639 | /* |
640 | * Hack alert: this pretends to be a block device, but it's really | |
641 | * a SCSI bus that can serve only a single device, which it | |
18846dee MA |
642 | * creates automatically. But first it needs to detach from its |
643 | * blockdev, or else scsi_bus_legacy_add_drive() dies when it | |
644 | * attaches again. | |
14bafc54 MA |
645 | * |
646 | * The hack is probably a bad idea. | |
647 | */ | |
4be74634 MA |
648 | blk_detach_dev(blk, &s->dev.qdev); |
649 | s->conf.blk = NULL; | |
14bafc54 | 650 | |
71938a09 | 651 | usb_desc_create_serial(dev); |
a980a065 | 652 | usb_desc_init(dev); |
b1187b51 AF |
653 | scsi_bus_new(&s->bus, sizeof(s->bus), DEVICE(dev), |
654 | &usb_msd_scsi_info_storage, NULL); | |
4be74634 | 655 | scsi_dev = scsi_bus_legacy_add_drive(&s->bus, blk, 0, !!s->removable, |
caad4eb3 AF |
656 | s->conf.bootindex, dev->serial, |
657 | &err); | |
34707333 | 658 | if (!scsi_dev) { |
5a882e40 GA |
659 | error_propagate(errp, err); |
660 | return; | |
fa66b909 | 661 | } |
7fc2f2c0 | 662 | usb_msd_handle_reset(dev); |
89f0762d | 663 | s->scsi_dev = scsi_dev; |
806b6024 GH |
664 | } |
665 | ||
5a882e40 | 666 | static void usb_msd_realize_bot(USBDevice *dev, Error **errp) |
34707333 | 667 | { |
79e2590c | 668 | MSDState *s = USB_STORAGE_DEV(dev); |
34707333 GH |
669 | |
670 | usb_desc_create_serial(dev); | |
671 | usb_desc_init(dev); | |
b1187b51 AF |
672 | scsi_bus_new(&s->bus, sizeof(s->bus), DEVICE(dev), |
673 | &usb_msd_scsi_info_bot, NULL); | |
34707333 | 674 | usb_msd_handle_reset(dev); |
34707333 GH |
675 | } |
676 | ||
3741715c | 677 | static USBDevice *usb_msd_init(USBBus *bus, const char *filename) |
2e5d83bb | 678 | { |
7fc2f2c0 | 679 | static int nr=0; |
9b3d111a | 680 | Error *err = NULL; |
7fc2f2c0 GH |
681 | char id[8]; |
682 | QemuOpts *opts; | |
683 | DriveInfo *dinfo; | |
806b6024 | 684 | USBDevice *dev; |
334c0241 AJ |
685 | const char *p1; |
686 | char fmt[32]; | |
687 | ||
7fc2f2c0 | 688 | /* parse -usbdevice disk: syntax into drive opts */ |
31376776 MA |
689 | do { |
690 | snprintf(id, sizeof(id), "usb%d", nr++); | |
691 | opts = qemu_opts_create(qemu_find_opts("drive"), id, 1, NULL); | |
692 | } while (!opts); | |
7fc2f2c0 | 693 | |
334c0241 AJ |
694 | p1 = strchr(filename, ':'); |
695 | if (p1++) { | |
696 | const char *p2; | |
697 | ||
698 | if (strstart(filename, "format=", &p2)) { | |
699 | int len = MIN(p1 - p2, sizeof(fmt)); | |
700 | pstrcpy(fmt, len, p2); | |
f43e47db | 701 | qemu_opt_set(opts, "format", fmt, &error_abort); |
334c0241 | 702 | } else if (*filename != ':') { |
f5dc5978 | 703 | error_report("unrecognized USB mass-storage option %s", filename); |
334c0241 AJ |
704 | return NULL; |
705 | } | |
334c0241 AJ |
706 | filename = p1; |
707 | } | |
334c0241 | 708 | if (!*filename) { |
f5dc5978 | 709 | error_report("block device specification needed"); |
334c0241 AJ |
710 | return NULL; |
711 | } | |
f43e47db MA |
712 | qemu_opt_set(opts, "file", filename, &error_abort); |
713 | qemu_opt_set(opts, "if", "none", &error_abort); | |
2e5d83bb | 714 | |
7fc2f2c0 | 715 | /* create host drive */ |
60e19e06 | 716 | dinfo = drive_new(opts, 0); |
7fc2f2c0 GH |
717 | if (!dinfo) { |
718 | qemu_opts_del(opts); | |
806b6024 | 719 | return NULL; |
7fc2f2c0 | 720 | } |
2e5d83bb | 721 | |
7fc2f2c0 | 722 | /* create guest device */ |
3741715c | 723 | dev = usb_create(bus, "usb-storage"); |
9b3d111a MA |
724 | qdev_prop_set_drive(&dev->qdev, "drive", blk_by_legacy_dinfo(dinfo), |
725 | &err); | |
726 | if (err) { | |
727 | error_report_err(err); | |
02a5c4c9 | 728 | object_unparent(OBJECT(dev)); |
18846dee MA |
729 | return NULL; |
730 | } | |
7fc2f2c0 | 731 | return dev; |
2e5d83bb | 732 | } |
bb5fc20f | 733 | |
f54b6563 GH |
734 | static const VMStateDescription vmstate_usb_msd = { |
735 | .name = "usb-storage", | |
f54b6563 GH |
736 | .version_id = 1, |
737 | .minimum_version_id = 1, | |
6e3d652a | 738 | .fields = (VMStateField[]) { |
f54b6563 | 739 | VMSTATE_USB_DEVICE(dev, MSDState), |
5de88b1d GH |
740 | VMSTATE_UINT32(mode, MSDState), |
741 | VMSTATE_UINT32(scsi_len, MSDState), | |
742 | VMSTATE_UINT32(scsi_off, MSDState), | |
743 | VMSTATE_UINT32(data_len, MSDState), | |
744 | VMSTATE_UINT32(csw.sig, MSDState), | |
745 | VMSTATE_UINT32(csw.tag, MSDState), | |
746 | VMSTATE_UINT32(csw.residue, MSDState), | |
747 | VMSTATE_UINT8(csw.status, MSDState), | |
f54b6563 GH |
748 | VMSTATE_END_OF_LIST() |
749 | } | |
750 | }; | |
751 | ||
39bffca2 AL |
752 | static Property msd_properties[] = { |
753 | DEFINE_BLOCK_PROPERTIES(MSDState, conf), | |
39bffca2 AL |
754 | DEFINE_PROP_BIT("removable", MSDState, removable, 0, false), |
755 | DEFINE_PROP_END_OF_LIST(), | |
756 | }; | |
757 | ||
79e2590c | 758 | static void usb_msd_class_initfn_common(ObjectClass *klass, void *data) |
62aed765 | 759 | { |
39bffca2 | 760 | DeviceClass *dc = DEVICE_CLASS(klass); |
62aed765 AL |
761 | USBDeviceClass *uc = USB_DEVICE_CLASS(klass); |
762 | ||
62aed765 AL |
763 | uc->product_desc = "QEMU USB MSD"; |
764 | uc->usb_desc = &desc; | |
62aed765 AL |
765 | uc->cancel_packet = usb_msd_cancel_io; |
766 | uc->handle_attach = usb_desc_attach; | |
767 | uc->handle_reset = usb_msd_handle_reset; | |
768 | uc->handle_control = usb_msd_handle_control; | |
769 | uc->handle_data = usb_msd_handle_data; | |
125ee0ed | 770 | set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); |
39bffca2 AL |
771 | dc->fw_name = "storage"; |
772 | dc->vmsd = &vmstate_usb_msd; | |
34707333 GH |
773 | } |
774 | ||
775 | static void usb_msd_class_initfn_storage(ObjectClass *klass, void *data) | |
776 | { | |
777 | DeviceClass *dc = DEVICE_CLASS(klass); | |
778 | USBDeviceClass *uc = USB_DEVICE_CLASS(klass); | |
779 | ||
5a882e40 | 780 | uc->realize = usb_msd_realize_storage; |
39bffca2 | 781 | dc->props = msd_properties; |
34707333 GH |
782 | } |
783 | ||
89f0762d GA |
784 | static void usb_msd_get_bootindex(Object *obj, Visitor *v, void *opaque, |
785 | const char *name, Error **errp) | |
786 | { | |
787 | USBDevice *dev = USB_DEVICE(obj); | |
79e2590c | 788 | MSDState *s = USB_STORAGE_DEV(dev); |
89f0762d GA |
789 | |
790 | visit_type_int32(v, &s->conf.bootindex, name, errp); | |
791 | } | |
792 | ||
793 | static void usb_msd_set_bootindex(Object *obj, Visitor *v, void *opaque, | |
794 | const char *name, Error **errp) | |
795 | { | |
796 | USBDevice *dev = USB_DEVICE(obj); | |
79e2590c | 797 | MSDState *s = USB_STORAGE_DEV(dev); |
89f0762d GA |
798 | int32_t boot_index; |
799 | Error *local_err = NULL; | |
800 | ||
801 | visit_type_int32(v, &boot_index, name, &local_err); | |
802 | if (local_err) { | |
803 | goto out; | |
804 | } | |
805 | /* check whether bootindex is present in fw_boot_order list */ | |
806 | check_boot_index(boot_index, &local_err); | |
807 | if (local_err) { | |
808 | goto out; | |
809 | } | |
810 | /* change bootindex to a new one */ | |
811 | s->conf.bootindex = boot_index; | |
812 | ||
813 | if (s->scsi_dev) { | |
814 | object_property_set_int(OBJECT(s->scsi_dev), boot_index, "bootindex", | |
815 | &error_abort); | |
816 | } | |
817 | ||
818 | out: | |
819 | if (local_err) { | |
820 | error_propagate(errp, local_err); | |
821 | } | |
822 | } | |
823 | ||
79e2590c GA |
824 | static const TypeInfo usb_storage_dev_type_info = { |
825 | .name = TYPE_USB_STORAGE, | |
826 | .parent = TYPE_USB_DEVICE, | |
827 | .instance_size = sizeof(MSDState), | |
828 | .abstract = true, | |
829 | .class_init = usb_msd_class_initfn_common, | |
830 | }; | |
831 | ||
89f0762d GA |
832 | static void usb_msd_instance_init(Object *obj) |
833 | { | |
834 | object_property_add(obj, "bootindex", "int32", | |
835 | usb_msd_get_bootindex, | |
836 | usb_msd_set_bootindex, NULL, NULL, NULL); | |
837 | object_property_set_int(obj, -1, "bootindex", NULL); | |
838 | } | |
839 | ||
34707333 GH |
840 | static void usb_msd_class_initfn_bot(ObjectClass *klass, void *data) |
841 | { | |
842 | USBDeviceClass *uc = USB_DEVICE_CLASS(klass); | |
e9fd12aa | 843 | DeviceClass *dc = DEVICE_CLASS(klass); |
34707333 | 844 | |
5a882e40 | 845 | uc->realize = usb_msd_realize_bot; |
e9fd12aa | 846 | dc->hotpluggable = false; |
62aed765 AL |
847 | } |
848 | ||
8c43a6f0 | 849 | static const TypeInfo msd_info = { |
39bffca2 | 850 | .name = "usb-storage", |
79e2590c | 851 | .parent = TYPE_USB_STORAGE, |
34707333 | 852 | .class_init = usb_msd_class_initfn_storage, |
89f0762d | 853 | .instance_init = usb_msd_instance_init, |
34707333 GH |
854 | }; |
855 | ||
856 | static const TypeInfo bot_info = { | |
857 | .name = "usb-bot", | |
79e2590c | 858 | .parent = TYPE_USB_STORAGE, |
34707333 | 859 | .class_init = usb_msd_class_initfn_bot, |
806b6024 GH |
860 | }; |
861 | ||
83f7d43a | 862 | static void usb_msd_register_types(void) |
806b6024 | 863 | { |
79e2590c | 864 | type_register_static(&usb_storage_dev_type_info); |
39bffca2 | 865 | type_register_static(&msd_info); |
34707333 | 866 | type_register_static(&bot_info); |
ba02430f | 867 | usb_legacy_register("usb-storage", "disk", usb_msd_init); |
806b6024 | 868 | } |
83f7d43a AF |
869 | |
870 | type_init(usb_msd_register_types) |