TCG/HPPA: use stack for TCG temps
[qemu/mdroth.git] / hw / usb-msd.c
blob86582cc723b881d728cf8285a1f2b5e8ab2d8c0e
1 /*
2 * USB Mass Storage Device emulation
4 * Copyright (c) 2006 CodeSourcery.
5 * Written by Paul Brook
7 * This code is licenced 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 typedef struct {
42 USBDevice dev;
43 enum USBMSDMode mode;
44 uint32_t scsi_len;
45 uint8_t *scsi_buf;
46 uint32_t usb_len;
47 uint8_t *usb_buf;
48 uint32_t data_len;
49 uint32_t residue;
50 uint32_t tag;
51 SCSIRequest *req;
52 SCSIBus bus;
53 BlockConf conf;
54 char *serial;
55 SCSIDevice *scsi_dev;
56 uint32_t removable;
57 int result;
58 /* For async completion. */
59 USBPacket *packet;
60 } MSDState;
62 struct usb_msd_cbw {
63 uint32_t sig;
64 uint32_t tag;
65 uint32_t data_len;
66 uint8_t flags;
67 uint8_t lun;
68 uint8_t cmd_len;
69 uint8_t cmd[16];
72 struct usb_msd_csw {
73 uint32_t sig;
74 uint32_t tag;
75 uint32_t residue;
76 uint8_t status;
79 enum {
80 STR_MANUFACTURER = 1,
81 STR_PRODUCT,
82 STR_SERIALNUMBER,
83 STR_CONFIG_FULL,
84 STR_CONFIG_HIGH,
87 static const USBDescStrings desc_strings = {
88 [STR_MANUFACTURER] = "QEMU " QEMU_VERSION,
89 [STR_PRODUCT] = "QEMU USB HARDDRIVE",
90 [STR_SERIALNUMBER] = "1",
91 [STR_CONFIG_FULL] = "Full speed config (usb 1.1)",
92 [STR_CONFIG_HIGH] = "High speed config (usb 2.0)",
95 static const USBDescIface desc_iface_full = {
96 .bInterfaceNumber = 0,
97 .bNumEndpoints = 2,
98 .bInterfaceClass = USB_CLASS_MASS_STORAGE,
99 .bInterfaceSubClass = 0x06, /* SCSI */
100 .bInterfaceProtocol = 0x50, /* Bulk */
101 .eps = (USBDescEndpoint[]) {
103 .bEndpointAddress = USB_DIR_IN | 0x01,
104 .bmAttributes = USB_ENDPOINT_XFER_BULK,
105 .wMaxPacketSize = 64,
107 .bEndpointAddress = USB_DIR_OUT | 0x02,
108 .bmAttributes = USB_ENDPOINT_XFER_BULK,
109 .wMaxPacketSize = 64,
114 static const USBDescDevice desc_device_full = {
115 .bcdUSB = 0x0200,
116 .bMaxPacketSize0 = 8,
117 .bNumConfigurations = 1,
118 .confs = (USBDescConfig[]) {
120 .bNumInterfaces = 1,
121 .bConfigurationValue = 1,
122 .iConfiguration = STR_CONFIG_FULL,
123 .bmAttributes = 0xc0,
124 .nif = 1,
125 .ifs = &desc_iface_full,
130 static 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[]) {
138 .bEndpointAddress = USB_DIR_IN | 0x01,
139 .bmAttributes = USB_ENDPOINT_XFER_BULK,
140 .wMaxPacketSize = 512,
142 .bEndpointAddress = USB_DIR_OUT | 0x02,
143 .bmAttributes = USB_ENDPOINT_XFER_BULK,
144 .wMaxPacketSize = 512,
149 static const USBDescDevice desc_device_high = {
150 .bcdUSB = 0x0200,
151 .bMaxPacketSize0 = 64,
152 .bNumConfigurations = 1,
153 .confs = (USBDescConfig[]) {
155 .bNumInterfaces = 1,
156 .bConfigurationValue = 1,
157 .iConfiguration = STR_CONFIG_HIGH,
158 .bmAttributes = 0xc0,
159 .nif = 1,
160 .ifs = &desc_iface_high,
165 static const USBDesc desc = {
166 .id = {
167 .idVendor = 0,
168 .idProduct = 0,
169 .bcdDevice = 0,
170 .iManufacturer = STR_MANUFACTURER,
171 .iProduct = STR_PRODUCT,
172 .iSerialNumber = STR_SERIALNUMBER,
174 .full = &desc_device_full,
175 .high = &desc_device_high,
176 .str = desc_strings,
179 static void usb_msd_copy_data(MSDState *s)
181 uint32_t len;
182 len = s->usb_len;
183 if (len > s->scsi_len)
184 len = s->scsi_len;
185 if (s->mode == USB_MSDM_DATAIN) {
186 memcpy(s->usb_buf, s->scsi_buf, len);
187 } else {
188 memcpy(s->scsi_buf, s->usb_buf, len);
190 s->usb_len -= len;
191 s->scsi_len -= len;
192 s->usb_buf += len;
193 s->scsi_buf += len;
194 s->data_len -= len;
195 if (s->scsi_len == 0 || s->data_len == 0) {
196 scsi_req_continue(s->req);
200 static void usb_msd_send_status(MSDState *s, USBPacket *p)
202 struct usb_msd_csw csw;
203 int len;
205 csw.sig = cpu_to_le32(0x53425355);
206 csw.tag = cpu_to_le32(s->tag);
207 csw.residue = s->residue;
208 csw.status = s->result;
210 len = MIN(sizeof(csw), p->len);
211 memcpy(p->data, &csw, len);
214 static void usb_msd_transfer_data(SCSIRequest *req, uint32_t len)
216 MSDState *s = DO_UPCAST(MSDState, dev.qdev, req->bus->qbus.parent);
217 USBPacket *p = s->packet;
219 if (req->tag != s->tag) {
220 fprintf(stderr, "usb-msd: Unexpected SCSI Tag 0x%x\n", req->tag);
223 assert((s->mode == USB_MSDM_DATAOUT) == (req->cmd.mode == SCSI_XFER_TO_DEV));
224 s->scsi_len = len;
225 s->scsi_buf = scsi_req_get_buf(req);
226 if (p) {
227 usb_msd_copy_data(s);
228 if (s->packet && s->usb_len == 0) {
229 /* Set s->packet to NULL before calling usb_packet_complete
230 because another request may be issued before
231 usb_packet_complete returns. */
232 DPRINTF("Packet complete %p\n", p);
233 s->packet = NULL;
234 usb_packet_complete(&s->dev, p);
239 static void usb_msd_command_complete(SCSIRequest *req, uint32_t status)
241 MSDState *s = DO_UPCAST(MSDState, dev.qdev, req->bus->qbus.parent);
242 USBPacket *p = s->packet;
244 if (req->tag != s->tag) {
245 fprintf(stderr, "usb-msd: Unexpected SCSI Tag 0x%x\n", req->tag);
247 DPRINTF("Command complete %d\n", status);
248 s->residue = s->data_len;
249 s->result = status != 0;
250 if (s->packet) {
251 if (s->data_len == 0 && s->mode == USB_MSDM_DATAOUT) {
252 /* A deferred packet with no write data remaining must be
253 the status read packet. */
254 usb_msd_send_status(s, p);
255 s->mode = USB_MSDM_CBW;
256 } else {
257 if (s->data_len) {
258 s->data_len -= s->usb_len;
259 if (s->mode == USB_MSDM_DATAIN) {
260 memset(s->usb_buf, 0, s->usb_len);
262 s->usb_len = 0;
264 if (s->data_len == 0) {
265 s->mode = USB_MSDM_CSW;
268 s->packet = NULL;
269 usb_packet_complete(&s->dev, p);
270 } else if (s->data_len == 0) {
271 s->mode = USB_MSDM_CSW;
273 scsi_req_unref(req);
274 s->req = NULL;
277 static void usb_msd_request_cancelled(SCSIRequest *req)
279 MSDState *s = DO_UPCAST(MSDState, dev.qdev, req->bus->qbus.parent);
281 if (req == s->req) {
282 scsi_req_unref(s->req);
283 s->req = NULL;
284 s->packet = NULL;
285 s->scsi_len = 0;
289 static void usb_msd_handle_reset(USBDevice *dev)
291 MSDState *s = (MSDState *)dev;
293 DPRINTF("Reset\n");
294 s->mode = USB_MSDM_CBW;
297 static int usb_msd_handle_control(USBDevice *dev, USBPacket *p,
298 int request, int value, int index, int length, uint8_t *data)
300 MSDState *s = (MSDState *)dev;
301 int ret;
303 ret = usb_desc_handle_control(dev, p, request, value, index, length, data);
304 if (ret >= 0) {
305 return ret;
308 ret = 0;
309 switch (request) {
310 case DeviceRequest | USB_REQ_GET_INTERFACE:
311 data[0] = 0;
312 ret = 1;
313 break;
314 case DeviceOutRequest | USB_REQ_SET_INTERFACE:
315 ret = 0;
316 break;
317 case EndpointOutRequest | USB_REQ_CLEAR_FEATURE:
318 ret = 0;
319 break;
320 case InterfaceOutRequest | USB_REQ_SET_INTERFACE:
321 ret = 0;
322 break;
323 /* Class specific requests. */
324 case ClassInterfaceOutRequest | MassStorageReset:
325 /* Reset state ready for the next CBW. */
326 s->mode = USB_MSDM_CBW;
327 ret = 0;
328 break;
329 case ClassInterfaceRequest | GetMaxLun:
330 data[0] = 0;
331 ret = 1;
332 break;
333 default:
334 ret = USB_RET_STALL;
335 break;
337 return ret;
340 static void usb_msd_cancel_io(USBDevice *dev, USBPacket *p)
342 MSDState *s = DO_UPCAST(MSDState, dev, dev);
343 scsi_req_cancel(s->req);
346 static int usb_msd_handle_data(USBDevice *dev, USBPacket *p)
348 MSDState *s = (MSDState *)dev;
349 int ret = 0;
350 struct usb_msd_cbw cbw;
351 uint8_t devep = p->devep;
352 uint8_t *data = p->data;
353 int len = p->len;
355 switch (p->pid) {
356 case USB_TOKEN_OUT:
357 if (devep != 2)
358 goto fail;
360 switch (s->mode) {
361 case USB_MSDM_CBW:
362 if (len != 31) {
363 fprintf(stderr, "usb-msd: Bad CBW size");
364 goto fail;
366 memcpy(&cbw, data, 31);
367 if (le32_to_cpu(cbw.sig) != 0x43425355) {
368 fprintf(stderr, "usb-msd: Bad signature %08x\n",
369 le32_to_cpu(cbw.sig));
370 goto fail;
372 DPRINTF("Command on LUN %d\n", cbw.lun);
373 if (cbw.lun != 0) {
374 fprintf(stderr, "usb-msd: Bad LUN %d\n", cbw.lun);
375 goto fail;
377 s->tag = le32_to_cpu(cbw.tag);
378 s->data_len = le32_to_cpu(cbw.data_len);
379 if (s->data_len == 0) {
380 s->mode = USB_MSDM_CSW;
381 } else if (cbw.flags & 0x80) {
382 s->mode = USB_MSDM_DATAIN;
383 } else {
384 s->mode = USB_MSDM_DATAOUT;
386 DPRINTF("Command tag 0x%x flags %08x len %d data %d\n",
387 s->tag, cbw.flags, cbw.cmd_len, s->data_len);
388 s->residue = 0;
389 s->scsi_len = 0;
390 s->req = scsi_req_new(s->scsi_dev, s->tag, 0);
391 scsi_req_enqueue(s->req, cbw.cmd);
392 /* ??? Should check that USB and SCSI data transfer
393 directions match. */
394 if (s->mode != USB_MSDM_CSW && s->residue == 0) {
395 scsi_req_continue(s->req);
397 ret = len;
398 break;
400 case USB_MSDM_DATAOUT:
401 DPRINTF("Data out %d/%d\n", len, s->data_len);
402 if (len > s->data_len)
403 goto fail;
405 s->usb_buf = data;
406 s->usb_len = len;
407 if (s->scsi_len) {
408 usb_msd_copy_data(s);
410 if (s->residue && s->usb_len) {
411 s->data_len -= s->usb_len;
412 if (s->data_len == 0)
413 s->mode = USB_MSDM_CSW;
414 s->usb_len = 0;
416 if (s->usb_len) {
417 DPRINTF("Deferring packet %p\n", p);
418 s->packet = p;
419 ret = USB_RET_ASYNC;
420 } else {
421 ret = len;
423 break;
425 default:
426 DPRINTF("Unexpected write (len %d)\n", len);
427 goto fail;
429 break;
431 case USB_TOKEN_IN:
432 if (devep != 1)
433 goto fail;
435 switch (s->mode) {
436 case USB_MSDM_DATAOUT:
437 if (s->data_len != 0 || len < 13)
438 goto fail;
439 /* Waiting for SCSI write to complete. */
440 s->packet = p;
441 ret = USB_RET_ASYNC;
442 break;
444 case USB_MSDM_CSW:
445 DPRINTF("Command status %d tag 0x%x, len %d\n",
446 s->result, s->tag, len);
447 if (len < 13)
448 goto fail;
450 usb_msd_send_status(s, p);
451 s->mode = USB_MSDM_CBW;
452 ret = 13;
453 break;
455 case USB_MSDM_DATAIN:
456 DPRINTF("Data in %d/%d, scsi_len %d\n", len, s->data_len, s->scsi_len);
457 if (len > s->data_len)
458 len = s->data_len;
459 s->usb_buf = data;
460 s->usb_len = len;
461 if (s->scsi_len) {
462 usb_msd_copy_data(s);
464 if (s->residue && s->usb_len) {
465 s->data_len -= s->usb_len;
466 memset(s->usb_buf, 0, s->usb_len);
467 if (s->data_len == 0)
468 s->mode = USB_MSDM_CSW;
469 s->usb_len = 0;
471 if (s->usb_len) {
472 DPRINTF("Deferring packet %p\n", p);
473 s->packet = p;
474 ret = USB_RET_ASYNC;
475 } else {
476 ret = len;
478 break;
480 default:
481 DPRINTF("Unexpected read (len %d)\n", len);
482 goto fail;
484 break;
486 default:
487 DPRINTF("Bad token\n");
488 fail:
489 ret = USB_RET_STALL;
490 break;
493 return ret;
496 static void usb_msd_password_cb(void *opaque, int err)
498 MSDState *s = opaque;
500 if (!err)
501 err = usb_device_attach(&s->dev);
503 if (err)
504 qdev_unplug(&s->dev.qdev);
507 static const struct SCSIBusOps usb_msd_scsi_ops = {
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(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, 0, 1, &usb_msd_scsi_ops);
549 s->scsi_dev = scsi_bus_legacy_add_drive(&s->bus, bs, 0, !!s->removable);
550 if (!s->scsi_dev) {
551 return -1;
553 s->bus.qbus.allow_hotplug = 0;
554 usb_msd_handle_reset(dev);
556 if (bdrv_key_required(bs)) {
557 if (cur_mon) {
558 monitor_read_bdrv_key_start(cur_mon, bs, usb_msd_password_cb, s);
559 s->dev.auto_attach = 0;
560 } else {
561 autostart = 0;
565 add_boot_device_path(s->conf.bootindex, &dev->qdev, "/disk@0,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 struct USBDeviceInfo msd_info = {
627 .product_desc = "QEMU USB MSD",
628 .qdev.name = "usb-storage",
629 .qdev.fw_name = "storage",
630 .qdev.size = sizeof(MSDState),
631 .usb_desc = &desc,
632 .init = usb_msd_initfn,
633 .handle_packet = usb_generic_handle_packet,
634 .cancel_packet = usb_msd_cancel_io,
635 .handle_attach = usb_desc_attach,
636 .handle_reset = usb_msd_handle_reset,
637 .handle_control = usb_msd_handle_control,
638 .handle_data = usb_msd_handle_data,
639 .usbdevice_name = "disk",
640 .usbdevice_init = usb_msd_init,
641 .qdev.props = (Property[]) {
642 DEFINE_BLOCK_PROPERTIES(MSDState, conf),
643 DEFINE_PROP_STRING("serial", MSDState, serial),
644 DEFINE_PROP_BIT("removable", MSDState, removable, 0, false),
645 DEFINE_PROP_END_OF_LIST(),
649 static void usb_msd_register_devices(void)
651 usb_qdev_register(&msd_info);
653 device_init(usb_msd_register_devices)