qxl: remove flipped
[qemu/agraf.git] / hw / usb-msd.c
blob5fbd2d021b5dc142d813421feec3a79ae8802cd1
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, size_t resid)
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 if (s->req) {
282 scsi_req_cancel(s->req);
284 assert(s->req == NULL);
286 if (s->packet) {
287 USBPacket *p = s->packet;
288 s->packet = NULL;
289 p->result = USB_RET_STALL;
290 usb_packet_complete(dev, p);
293 s->mode = USB_MSDM_CBW;
296 static int usb_msd_handle_control(USBDevice *dev, USBPacket *p,
297 int request, int value, int index, int length, uint8_t *data)
299 MSDState *s = (MSDState *)dev;
300 int ret;
302 ret = usb_desc_handle_control(dev, p, request, value, index, length, data);
303 if (ret >= 0) {
304 return ret;
307 ret = 0;
308 switch (request) {
309 case EndpointOutRequest | USB_REQ_CLEAR_FEATURE:
310 ret = 0;
311 break;
312 /* Class specific requests. */
313 case ClassInterfaceOutRequest | MassStorageReset:
314 /* Reset state ready for the next CBW. */
315 s->mode = USB_MSDM_CBW;
316 ret = 0;
317 break;
318 case ClassInterfaceRequest | GetMaxLun:
319 data[0] = 0;
320 ret = 1;
321 break;
322 default:
323 ret = USB_RET_STALL;
324 break;
326 return ret;
329 static void usb_msd_cancel_io(USBDevice *dev, USBPacket *p)
331 MSDState *s = DO_UPCAST(MSDState, dev, dev);
333 if (s->req) {
334 scsi_req_cancel(s->req);
338 static int usb_msd_handle_data(USBDevice *dev, USBPacket *p)
340 MSDState *s = (MSDState *)dev;
341 uint32_t tag;
342 int ret = 0;
343 struct usb_msd_cbw cbw;
344 uint8_t devep = p->ep->nr;
346 switch (p->pid) {
347 case USB_TOKEN_OUT:
348 if (devep != 2)
349 goto fail;
351 switch (s->mode) {
352 case USB_MSDM_CBW:
353 if (p->iov.size != 31) {
354 fprintf(stderr, "usb-msd: Bad CBW size");
355 goto fail;
357 usb_packet_copy(p, &cbw, 31);
358 if (le32_to_cpu(cbw.sig) != 0x43425355) {
359 fprintf(stderr, "usb-msd: Bad signature %08x\n",
360 le32_to_cpu(cbw.sig));
361 goto fail;
363 DPRINTF("Command on LUN %d\n", cbw.lun);
364 if (cbw.lun != 0) {
365 fprintf(stderr, "usb-msd: Bad LUN %d\n", cbw.lun);
366 goto fail;
368 tag = le32_to_cpu(cbw.tag);
369 s->data_len = le32_to_cpu(cbw.data_len);
370 if (s->data_len == 0) {
371 s->mode = USB_MSDM_CSW;
372 } else if (cbw.flags & 0x80) {
373 s->mode = USB_MSDM_DATAIN;
374 } else {
375 s->mode = USB_MSDM_DATAOUT;
377 DPRINTF("Command tag 0x%x flags %08x len %d data %d\n",
378 tag, cbw.flags, cbw.cmd_len, s->data_len);
379 s->residue = 0;
380 s->scsi_len = 0;
381 s->req = scsi_req_new(s->scsi_dev, tag, 0, cbw.cmd, NULL);
382 scsi_req_enqueue(s->req);
383 if (s->req && s->req->cmd.xfer != SCSI_XFER_NONE) {
384 scsi_req_continue(s->req);
386 ret = p->result;
387 break;
389 case USB_MSDM_DATAOUT:
390 DPRINTF("Data out %zd/%d\n", p->iov.size, s->data_len);
391 if (p->iov.size > s->data_len) {
392 goto fail;
395 if (s->scsi_len) {
396 usb_msd_copy_data(s, p);
398 if (s->residue) {
399 int len = p->iov.size - p->result;
400 if (len) {
401 usb_packet_skip(p, len);
402 s->data_len -= len;
403 if (s->data_len == 0) {
404 s->mode = USB_MSDM_CSW;
408 if (p->result < p->iov.size) {
409 DPRINTF("Deferring packet %p\n", p);
410 s->packet = p;
411 ret = USB_RET_ASYNC;
412 } else {
413 ret = p->result;
415 break;
417 default:
418 DPRINTF("Unexpected write (len %zd)\n", p->iov.size);
419 goto fail;
421 break;
423 case USB_TOKEN_IN:
424 if (devep != 1)
425 goto fail;
427 switch (s->mode) {
428 case USB_MSDM_DATAOUT:
429 if (s->data_len != 0 || p->iov.size < 13) {
430 goto fail;
432 /* Waiting for SCSI write to complete. */
433 s->packet = p;
434 ret = USB_RET_ASYNC;
435 break;
437 case USB_MSDM_CSW:
438 if (p->iov.size < 13) {
439 goto fail;
442 if (s->req) {
443 /* still in flight */
444 s->packet = p;
445 ret = USB_RET_ASYNC;
446 } else {
447 usb_msd_send_status(s, p);
448 s->mode = USB_MSDM_CBW;
449 ret = 13;
451 break;
453 case USB_MSDM_DATAIN:
454 DPRINTF("Data in %zd/%d, scsi_len %d\n",
455 p->iov.size, s->data_len, s->scsi_len);
456 if (s->scsi_len) {
457 usb_msd_copy_data(s, p);
459 if (s->residue) {
460 int len = p->iov.size - p->result;
461 if (len) {
462 usb_packet_skip(p, len);
463 s->data_len -= len;
464 if (s->data_len == 0) {
465 s->mode = USB_MSDM_CSW;
469 if (p->result < p->iov.size) {
470 DPRINTF("Deferring packet %p\n", p);
471 s->packet = p;
472 ret = USB_RET_ASYNC;
473 } else {
474 ret = p->result;
476 break;
478 default:
479 DPRINTF("Unexpected read (len %zd)\n", p->iov.size);
480 goto fail;
482 break;
484 default:
485 DPRINTF("Bad token\n");
486 fail:
487 ret = USB_RET_STALL;
488 break;
491 return ret;
494 static void usb_msd_password_cb(void *opaque, int err)
496 MSDState *s = opaque;
498 if (!err)
499 err = usb_device_attach(&s->dev);
501 if (err)
502 qdev_unplug(&s->dev.qdev);
505 static const struct SCSIBusInfo usb_msd_scsi_info = {
506 .tcq = false,
507 .max_target = 0,
508 .max_lun = 0,
510 .transfer_data = usb_msd_transfer_data,
511 .complete = usb_msd_command_complete,
512 .cancel = usb_msd_request_cancelled
515 static int usb_msd_initfn(USBDevice *dev)
517 MSDState *s = DO_UPCAST(MSDState, dev, dev);
518 BlockDriverState *bs = s->conf.bs;
519 DriveInfo *dinfo;
521 if (!bs) {
522 error_report("drive property not set");
523 return -1;
527 * Hack alert: this pretends to be a block device, but it's really
528 * a SCSI bus that can serve only a single device, which it
529 * creates automatically. But first it needs to detach from its
530 * blockdev, or else scsi_bus_legacy_add_drive() dies when it
531 * attaches again.
533 * The hack is probably a bad idea.
535 bdrv_detach_dev(bs, &s->dev.qdev);
536 s->conf.bs = NULL;
538 if (!s->serial) {
539 /* try to fall back to value set with legacy -drive serial=... */
540 dinfo = drive_get_by_blockdev(bs);
541 if (*dinfo->serial) {
542 s->serial = strdup(dinfo->serial);
545 if (s->serial) {
546 usb_desc_set_string(dev, STR_SERIALNUMBER, s->serial);
549 usb_desc_init(dev);
550 scsi_bus_new(&s->bus, &s->dev.qdev, &usb_msd_scsi_info);
551 s->scsi_dev = scsi_bus_legacy_add_drive(&s->bus, bs, 0, !!s->removable,
552 s->conf.bootindex);
553 if (!s->scsi_dev) {
554 return -1;
556 s->bus.qbus.allow_hotplug = 0;
557 usb_msd_handle_reset(dev);
559 if (bdrv_key_required(bs)) {
560 if (cur_mon) {
561 monitor_read_bdrv_key_start(cur_mon, bs, usb_msd_password_cb, s);
562 s->dev.auto_attach = 0;
563 } else {
564 autostart = 0;
568 return 0;
571 static USBDevice *usb_msd_init(const char *filename)
573 static int nr=0;
574 char id[8];
575 QemuOpts *opts;
576 DriveInfo *dinfo;
577 USBDevice *dev;
578 const char *p1;
579 char fmt[32];
581 /* parse -usbdevice disk: syntax into drive opts */
582 snprintf(id, sizeof(id), "usb%d", nr++);
583 opts = qemu_opts_create(qemu_find_opts("drive"), id, 0);
585 p1 = strchr(filename, ':');
586 if (p1++) {
587 const char *p2;
589 if (strstart(filename, "format=", &p2)) {
590 int len = MIN(p1 - p2, sizeof(fmt));
591 pstrcpy(fmt, len, p2);
592 qemu_opt_set(opts, "format", fmt);
593 } else if (*filename != ':') {
594 printf("unrecognized USB mass-storage option %s\n", filename);
595 return NULL;
597 filename = p1;
599 if (!*filename) {
600 printf("block device specification needed\n");
601 return NULL;
603 qemu_opt_set(opts, "file", filename);
604 qemu_opt_set(opts, "if", "none");
606 /* create host drive */
607 dinfo = drive_init(opts, 0);
608 if (!dinfo) {
609 qemu_opts_del(opts);
610 return NULL;
613 /* create guest device */
614 dev = usb_create(NULL /* FIXME */, "usb-storage");
615 if (!dev) {
616 return NULL;
618 if (qdev_prop_set_drive(&dev->qdev, "drive", dinfo->bdrv) < 0) {
619 qdev_free(&dev->qdev);
620 return NULL;
622 if (qdev_init(&dev->qdev) < 0)
623 return NULL;
625 return dev;
628 static const VMStateDescription vmstate_usb_msd = {
629 .name = "usb-storage",
630 .unmigratable = 1, /* FIXME: handle transactions which are in flight */
631 .version_id = 1,
632 .minimum_version_id = 1,
633 .fields = (VMStateField []) {
634 VMSTATE_USB_DEVICE(dev, MSDState),
635 VMSTATE_END_OF_LIST()
639 static Property msd_properties[] = {
640 DEFINE_BLOCK_PROPERTIES(MSDState, conf),
641 DEFINE_PROP_STRING("serial", MSDState, serial),
642 DEFINE_PROP_BIT("removable", MSDState, removable, 0, false),
643 DEFINE_PROP_END_OF_LIST(),
646 static void usb_msd_class_initfn(ObjectClass *klass, void *data)
648 DeviceClass *dc = DEVICE_CLASS(klass);
649 USBDeviceClass *uc = USB_DEVICE_CLASS(klass);
651 uc->init = usb_msd_initfn;
652 uc->product_desc = "QEMU USB MSD";
653 uc->usb_desc = &desc;
654 uc->cancel_packet = usb_msd_cancel_io;
655 uc->handle_attach = usb_desc_attach;
656 uc->handle_reset = usb_msd_handle_reset;
657 uc->handle_control = usb_msd_handle_control;
658 uc->handle_data = usb_msd_handle_data;
659 dc->fw_name = "storage";
660 dc->vmsd = &vmstate_usb_msd;
661 dc->props = msd_properties;
664 static TypeInfo msd_info = {
665 .name = "usb-storage",
666 .parent = TYPE_USB_DEVICE,
667 .instance_size = sizeof(MSDState),
668 .class_init = usb_msd_class_initfn,
671 static void usb_msd_register_types(void)
673 type_register_static(&msd_info);
674 usb_legacy_register("usb-storage", "disk", usb_msd_init);
677 type_init(usb_msd_register_types)