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