fix spelling in linux-user sub directory
[qemu.git] / hw / usb-msd.c
blob4c069501253ddcb9c35f0b50a9903843da51ac6c
1 /*
2 * USB Mass Storage Device emulation
4 * Copyright (c) 2006 CodeSourcery.
5 * Written by Paul Brook
7 * This code is licensed under the LGPL.
8 */
10 #include "qemu-common.h"
11 #include "qemu-option.h"
12 #include "qemu-config.h"
13 #include "usb.h"
14 #include "usb-desc.h"
15 #include "scsi.h"
16 #include "console.h"
17 #include "monitor.h"
18 #include "sysemu.h"
19 #include "blockdev.h"
21 //#define DEBUG_MSD
23 #ifdef DEBUG_MSD
24 #define DPRINTF(fmt, ...) \
25 do { printf("usb-msd: " fmt , ## __VA_ARGS__); } while (0)
26 #else
27 #define DPRINTF(fmt, ...) do {} while(0)
28 #endif
30 /* USB requests. */
31 #define MassStorageReset 0xff
32 #define GetMaxLun 0xfe
34 enum USBMSDMode {
35 USB_MSDM_CBW, /* Command Block. */
36 USB_MSDM_DATAOUT, /* Transfer data to device. */
37 USB_MSDM_DATAIN, /* Transfer data from device. */
38 USB_MSDM_CSW /* Command Status. */
41 struct usb_msd_csw {
42 uint32_t sig;
43 uint32_t tag;
44 uint32_t residue;
45 uint8_t status;
48 typedef struct {
49 USBDevice dev;
50 enum USBMSDMode mode;
51 uint32_t scsi_len;
52 uint8_t *scsi_buf;
53 uint32_t data_len;
54 uint32_t residue;
55 struct usb_msd_csw csw;
56 SCSIRequest *req;
57 SCSIBus bus;
58 BlockConf conf;
59 char *serial;
60 SCSIDevice *scsi_dev;
61 uint32_t removable;
62 /* For async completion. */
63 USBPacket *packet;
64 } MSDState;
66 struct usb_msd_cbw {
67 uint32_t sig;
68 uint32_t tag;
69 uint32_t data_len;
70 uint8_t flags;
71 uint8_t lun;
72 uint8_t cmd_len;
73 uint8_t cmd[16];
76 enum {
77 STR_MANUFACTURER = 1,
78 STR_PRODUCT,
79 STR_SERIALNUMBER,
80 STR_CONFIG_FULL,
81 STR_CONFIG_HIGH,
84 static const USBDescStrings desc_strings = {
85 [STR_MANUFACTURER] = "QEMU " QEMU_VERSION,
86 [STR_PRODUCT] = "QEMU USB HARDDRIVE",
87 [STR_SERIALNUMBER] = "1",
88 [STR_CONFIG_FULL] = "Full speed config (usb 1.1)",
89 [STR_CONFIG_HIGH] = "High speed config (usb 2.0)",
92 static const USBDescIface desc_iface_full = {
93 .bInterfaceNumber = 0,
94 .bNumEndpoints = 2,
95 .bInterfaceClass = USB_CLASS_MASS_STORAGE,
96 .bInterfaceSubClass = 0x06, /* SCSI */
97 .bInterfaceProtocol = 0x50, /* Bulk */
98 .eps = (USBDescEndpoint[]) {
100 .bEndpointAddress = USB_DIR_IN | 0x01,
101 .bmAttributes = USB_ENDPOINT_XFER_BULK,
102 .wMaxPacketSize = 64,
104 .bEndpointAddress = USB_DIR_OUT | 0x02,
105 .bmAttributes = USB_ENDPOINT_XFER_BULK,
106 .wMaxPacketSize = 64,
111 static const USBDescDevice desc_device_full = {
112 .bcdUSB = 0x0200,
113 .bMaxPacketSize0 = 8,
114 .bNumConfigurations = 1,
115 .confs = (USBDescConfig[]) {
117 .bNumInterfaces = 1,
118 .bConfigurationValue = 1,
119 .iConfiguration = STR_CONFIG_FULL,
120 .bmAttributes = 0xc0,
121 .nif = 1,
122 .ifs = &desc_iface_full,
127 static const USBDescIface desc_iface_high = {
128 .bInterfaceNumber = 0,
129 .bNumEndpoints = 2,
130 .bInterfaceClass = USB_CLASS_MASS_STORAGE,
131 .bInterfaceSubClass = 0x06, /* SCSI */
132 .bInterfaceProtocol = 0x50, /* Bulk */
133 .eps = (USBDescEndpoint[]) {
135 .bEndpointAddress = USB_DIR_IN | 0x01,
136 .bmAttributes = USB_ENDPOINT_XFER_BULK,
137 .wMaxPacketSize = 512,
139 .bEndpointAddress = USB_DIR_OUT | 0x02,
140 .bmAttributes = USB_ENDPOINT_XFER_BULK,
141 .wMaxPacketSize = 512,
146 static const USBDescDevice desc_device_high = {
147 .bcdUSB = 0x0200,
148 .bMaxPacketSize0 = 64,
149 .bNumConfigurations = 1,
150 .confs = (USBDescConfig[]) {
152 .bNumInterfaces = 1,
153 .bConfigurationValue = 1,
154 .iConfiguration = STR_CONFIG_HIGH,
155 .bmAttributes = 0xc0,
156 .nif = 1,
157 .ifs = &desc_iface_high,
162 static const USBDesc desc = {
163 .id = {
164 .idVendor = 0x46f4, /* CRC16() of "QEMU" */
165 .idProduct = 0x0001,
166 .bcdDevice = 0,
167 .iManufacturer = STR_MANUFACTURER,
168 .iProduct = STR_PRODUCT,
169 .iSerialNumber = STR_SERIALNUMBER,
171 .full = &desc_device_full,
172 .high = &desc_device_high,
173 .str = desc_strings,
176 static void usb_msd_copy_data(MSDState *s, USBPacket *p)
178 uint32_t len;
179 len = p->iov.size - p->result;
180 if (len > s->scsi_len)
181 len = s->scsi_len;
182 usb_packet_copy(p, s->scsi_buf, len);
183 s->scsi_len -= len;
184 s->scsi_buf += len;
185 s->data_len -= len;
186 if (s->scsi_len == 0 || s->data_len == 0) {
187 scsi_req_continue(s->req);
191 static void usb_msd_send_status(MSDState *s, USBPacket *p)
193 int len;
195 DPRINTF("Command status %d tag 0x%x, len %zd\n",
196 s->csw.status, s->csw.tag, p->iov.size);
198 assert(s->csw.sig == 0x53425355);
199 len = MIN(sizeof(s->csw), p->iov.size);
200 usb_packet_copy(p, &s->csw, len);
201 memset(&s->csw, 0, sizeof(s->csw));
204 static void usb_msd_transfer_data(SCSIRequest *req, uint32_t len)
206 MSDState *s = DO_UPCAST(MSDState, dev.qdev, req->bus->qbus.parent);
207 USBPacket *p = s->packet;
209 assert((s->mode == USB_MSDM_DATAOUT) == (req->cmd.mode == SCSI_XFER_TO_DEV));
210 s->scsi_len = len;
211 s->scsi_buf = scsi_req_get_buf(req);
212 if (p) {
213 usb_msd_copy_data(s, p);
214 p = s->packet;
215 if (p && p->result == p->iov.size) {
216 /* Set s->packet to NULL before calling usb_packet_complete
217 because another request may be issued before
218 usb_packet_complete returns. */
219 DPRINTF("Packet complete %p\n", p);
220 s->packet = NULL;
221 usb_packet_complete(&s->dev, p);
226 static void usb_msd_command_complete(SCSIRequest *req, uint32_t status)
228 MSDState *s = DO_UPCAST(MSDState, dev.qdev, req->bus->qbus.parent);
229 USBPacket *p = s->packet;
231 DPRINTF("Command complete %d tag 0x%x\n", status, req->tag);
232 s->residue = s->data_len;
234 s->csw.sig = cpu_to_le32(0x53425355);
235 s->csw.tag = cpu_to_le32(req->tag);
236 s->csw.residue = s->residue;
237 s->csw.status = status != 0;
239 if (s->packet) {
240 if (s->data_len == 0 && s->mode == USB_MSDM_DATAOUT) {
241 /* A deferred packet with no write data remaining must be
242 the status read packet. */
243 usb_msd_send_status(s, p);
244 s->mode = USB_MSDM_CBW;
245 } else {
246 if (s->data_len) {
247 int len = (p->iov.size - p->result);
248 usb_packet_skip(p, len);
249 s->data_len -= len;
251 if (s->data_len == 0) {
252 s->mode = USB_MSDM_CSW;
255 s->packet = NULL;
256 usb_packet_complete(&s->dev, p);
257 } else if (s->data_len == 0) {
258 s->mode = USB_MSDM_CSW;
260 scsi_req_unref(req);
261 s->req = NULL;
264 static void usb_msd_request_cancelled(SCSIRequest *req)
266 MSDState *s = DO_UPCAST(MSDState, dev.qdev, req->bus->qbus.parent);
268 if (req == s->req) {
269 scsi_req_unref(s->req);
270 s->req = NULL;
271 s->packet = NULL;
272 s->scsi_len = 0;
276 static void usb_msd_handle_reset(USBDevice *dev)
278 MSDState *s = (MSDState *)dev;
280 DPRINTF("Reset\n");
281 s->mode = USB_MSDM_CBW;
284 static int usb_msd_handle_control(USBDevice *dev, USBPacket *p,
285 int request, int value, int index, int length, uint8_t *data)
287 MSDState *s = (MSDState *)dev;
288 int ret;
290 ret = usb_desc_handle_control(dev, p, request, value, index, length, data);
291 if (ret >= 0) {
292 return ret;
295 ret = 0;
296 switch (request) {
297 case DeviceRequest | USB_REQ_GET_INTERFACE:
298 data[0] = 0;
299 ret = 1;
300 break;
301 case DeviceOutRequest | USB_REQ_SET_INTERFACE:
302 ret = 0;
303 break;
304 case EndpointOutRequest | USB_REQ_CLEAR_FEATURE:
305 ret = 0;
306 break;
307 case InterfaceOutRequest | USB_REQ_SET_INTERFACE:
308 ret = 0;
309 break;
310 /* Class specific requests. */
311 case ClassInterfaceOutRequest | MassStorageReset:
312 /* Reset state ready for the next CBW. */
313 s->mode = USB_MSDM_CBW;
314 ret = 0;
315 break;
316 case ClassInterfaceRequest | GetMaxLun:
317 data[0] = 0;
318 ret = 1;
319 break;
320 default:
321 ret = USB_RET_STALL;
322 break;
324 return ret;
327 static void usb_msd_cancel_io(USBDevice *dev, USBPacket *p)
329 MSDState *s = DO_UPCAST(MSDState, dev, dev);
331 if (s->req) {
332 scsi_req_cancel(s->req);
336 static int usb_msd_handle_data(USBDevice *dev, USBPacket *p)
338 MSDState *s = (MSDState *)dev;
339 uint32_t tag;
340 int ret = 0;
341 struct usb_msd_cbw cbw;
342 uint8_t devep = p->devep;
344 switch (p->pid) {
345 case USB_TOKEN_OUT:
346 if (devep != 2)
347 goto fail;
349 switch (s->mode) {
350 case USB_MSDM_CBW:
351 if (p->iov.size != 31) {
352 fprintf(stderr, "usb-msd: Bad CBW size");
353 goto fail;
355 usb_packet_copy(p, &cbw, 31);
356 if (le32_to_cpu(cbw.sig) != 0x43425355) {
357 fprintf(stderr, "usb-msd: Bad signature %08x\n",
358 le32_to_cpu(cbw.sig));
359 goto fail;
361 DPRINTF("Command on LUN %d\n", cbw.lun);
362 if (cbw.lun != 0) {
363 fprintf(stderr, "usb-msd: Bad LUN %d\n", cbw.lun);
364 goto fail;
366 tag = le32_to_cpu(cbw.tag);
367 s->data_len = le32_to_cpu(cbw.data_len);
368 if (s->data_len == 0) {
369 s->mode = USB_MSDM_CSW;
370 } else if (cbw.flags & 0x80) {
371 s->mode = USB_MSDM_DATAIN;
372 } else {
373 s->mode = USB_MSDM_DATAOUT;
375 DPRINTF("Command tag 0x%x flags %08x len %d data %d\n",
376 tag, cbw.flags, cbw.cmd_len, s->data_len);
377 s->residue = 0;
378 s->scsi_len = 0;
379 s->req = scsi_req_new(s->scsi_dev, tag, 0, cbw.cmd, NULL);
380 scsi_req_enqueue(s->req);
381 if (s->req && s->req->cmd.xfer != SCSI_XFER_NONE) {
382 scsi_req_continue(s->req);
384 ret = p->result;
385 break;
387 case USB_MSDM_DATAOUT:
388 DPRINTF("Data out %zd/%d\n", p->iov.size, s->data_len);
389 if (p->iov.size > s->data_len) {
390 goto fail;
393 if (s->scsi_len) {
394 usb_msd_copy_data(s, p);
396 if (s->residue) {
397 int len = p->iov.size - p->result;
398 if (len) {
399 usb_packet_skip(p, len);
400 s->data_len -= len;
401 if (s->data_len == 0) {
402 s->mode = USB_MSDM_CSW;
406 if (p->result < p->iov.size) {
407 DPRINTF("Deferring packet %p\n", p);
408 s->packet = p;
409 ret = USB_RET_ASYNC;
410 } else {
411 ret = p->result;
413 break;
415 default:
416 DPRINTF("Unexpected write (len %zd)\n", p->iov.size);
417 goto fail;
419 break;
421 case USB_TOKEN_IN:
422 if (devep != 1)
423 goto fail;
425 switch (s->mode) {
426 case USB_MSDM_DATAOUT:
427 if (s->data_len != 0 || p->iov.size < 13) {
428 goto fail;
430 /* Waiting for SCSI write to complete. */
431 s->packet = p;
432 ret = USB_RET_ASYNC;
433 break;
435 case USB_MSDM_CSW:
436 if (p->iov.size < 13) {
437 goto fail;
440 if (s->req) {
441 /* still in flight */
442 s->packet = p;
443 ret = USB_RET_ASYNC;
444 } else {
445 usb_msd_send_status(s, p);
446 s->mode = USB_MSDM_CBW;
447 ret = 13;
449 break;
451 case USB_MSDM_DATAIN:
452 DPRINTF("Data in %zd/%d, scsi_len %d\n",
453 p->iov.size, s->data_len, s->scsi_len);
454 if (s->scsi_len) {
455 usb_msd_copy_data(s, p);
457 if (s->residue) {
458 int len = p->iov.size - p->result;
459 if (len) {
460 usb_packet_skip(p, len);
461 s->data_len -= len;
462 if (s->data_len == 0) {
463 s->mode = USB_MSDM_CSW;
467 if (p->result < p->iov.size) {
468 DPRINTF("Deferring packet %p\n", p);
469 s->packet = p;
470 ret = USB_RET_ASYNC;
471 } else {
472 ret = p->result;
474 break;
476 default:
477 DPRINTF("Unexpected read (len %zd)\n", p->iov.size);
478 goto fail;
480 break;
482 default:
483 DPRINTF("Bad token\n");
484 fail:
485 ret = USB_RET_STALL;
486 break;
489 return ret;
492 static void usb_msd_password_cb(void *opaque, int err)
494 MSDState *s = opaque;
496 if (!err)
497 err = usb_device_attach(&s->dev);
499 if (err)
500 qdev_unplug(&s->dev.qdev);
503 static const struct SCSIBusInfo usb_msd_scsi_info = {
504 .tcq = false,
505 .max_target = 0,
506 .max_lun = 0,
508 .transfer_data = usb_msd_transfer_data,
509 .complete = usb_msd_command_complete,
510 .cancel = usb_msd_request_cancelled
513 static int usb_msd_initfn(USBDevice *dev)
515 MSDState *s = DO_UPCAST(MSDState, dev, dev);
516 BlockDriverState *bs = s->conf.bs;
517 DriveInfo *dinfo;
519 if (!bs) {
520 error_report("usb-msd: drive property not set");
521 return -1;
525 * Hack alert: this pretends to be a block device, but it's really
526 * a SCSI bus that can serve only a single device, which it
527 * creates automatically. But first it needs to detach from its
528 * blockdev, or else scsi_bus_legacy_add_drive() dies when it
529 * attaches again.
531 * The hack is probably a bad idea.
533 bdrv_detach_dev(bs, &s->dev.qdev);
534 s->conf.bs = NULL;
536 if (!s->serial) {
537 /* try to fall back to value set with legacy -drive serial=... */
538 dinfo = drive_get_by_blockdev(bs);
539 if (*dinfo->serial) {
540 s->serial = strdup(dinfo->serial);
543 if (s->serial) {
544 usb_desc_set_string(dev, STR_SERIALNUMBER, s->serial);
547 usb_desc_init(dev);
548 scsi_bus_new(&s->bus, &s->dev.qdev, &usb_msd_scsi_info);
549 s->scsi_dev = scsi_bus_legacy_add_drive(&s->bus, bs, 0, !!s->removable,
550 s->conf.bootindex);
551 if (!s->scsi_dev) {
552 return -1;
554 s->bus.qbus.allow_hotplug = 0;
555 usb_msd_handle_reset(dev);
557 if (bdrv_key_required(bs)) {
558 if (cur_mon) {
559 monitor_read_bdrv_key_start(cur_mon, bs, usb_msd_password_cb, s);
560 s->dev.auto_attach = 0;
561 } else {
562 autostart = 0;
566 return 0;
569 static USBDevice *usb_msd_init(const char *filename)
571 static int nr=0;
572 char id[8];
573 QemuOpts *opts;
574 DriveInfo *dinfo;
575 USBDevice *dev;
576 const char *p1;
577 char fmt[32];
579 /* parse -usbdevice disk: syntax into drive opts */
580 snprintf(id, sizeof(id), "usb%d", nr++);
581 opts = qemu_opts_create(qemu_find_opts("drive"), id, 0);
583 p1 = strchr(filename, ':');
584 if (p1++) {
585 const char *p2;
587 if (strstart(filename, "format=", &p2)) {
588 int len = MIN(p1 - p2, sizeof(fmt));
589 pstrcpy(fmt, len, p2);
590 qemu_opt_set(opts, "format", fmt);
591 } else if (*filename != ':') {
592 printf("unrecognized USB mass-storage option %s\n", filename);
593 return NULL;
595 filename = p1;
597 if (!*filename) {
598 printf("block device specification needed\n");
599 return NULL;
601 qemu_opt_set(opts, "file", filename);
602 qemu_opt_set(opts, "if", "none");
604 /* create host drive */
605 dinfo = drive_init(opts, 0);
606 if (!dinfo) {
607 qemu_opts_del(opts);
608 return NULL;
611 /* create guest device */
612 dev = usb_create(NULL /* FIXME */, "usb-storage");
613 if (!dev) {
614 return NULL;
616 if (qdev_prop_set_drive(&dev->qdev, "drive", dinfo->bdrv) < 0) {
617 qdev_free(&dev->qdev);
618 return NULL;
620 if (qdev_init(&dev->qdev) < 0)
621 return NULL;
623 return dev;
626 static const VMStateDescription vmstate_usb_msd = {
627 .name = "usb-storage",
628 .unmigratable = 1, /* FIXME: handle transactions which are in flight */
629 .version_id = 1,
630 .minimum_version_id = 1,
631 .fields = (VMStateField []) {
632 VMSTATE_USB_DEVICE(dev, MSDState),
633 VMSTATE_END_OF_LIST()
637 static struct USBDeviceInfo msd_info = {
638 .product_desc = "QEMU USB MSD",
639 .qdev.name = "usb-storage",
640 .qdev.fw_name = "storage",
641 .qdev.size = sizeof(MSDState),
642 .qdev.vmsd = &vmstate_usb_msd,
643 .usb_desc = &desc,
644 .init = usb_msd_initfn,
645 .handle_packet = usb_generic_handle_packet,
646 .cancel_packet = usb_msd_cancel_io,
647 .handle_attach = usb_desc_attach,
648 .handle_reset = usb_msd_handle_reset,
649 .handle_control = usb_msd_handle_control,
650 .handle_data = usb_msd_handle_data,
651 .usbdevice_name = "disk",
652 .usbdevice_init = usb_msd_init,
653 .qdev.props = (Property[]) {
654 DEFINE_BLOCK_PROPERTIES(MSDState, conf),
655 DEFINE_PROP_STRING("serial", MSDState, serial),
656 DEFINE_PROP_BIT("removable", MSDState, removable, 0, false),
657 DEFINE_PROP_END_OF_LIST(),
661 static void usb_msd_register_devices(void)
663 usb_qdev_register(&msd_info);
665 device_init(usb_msd_register_devices)