2 * USB Mass Storage Device emulation
4 * Copyright (c) 2006 CodeSourcery.
5 * Written by Paul Brook
7 * This code is licenced under the LGPL.
10 #include "qemu-common.h"
13 #include "scsi-disk.h"
19 #define DPRINTF(fmt, args...) \
20 do { printf("usb-msd: " fmt , ##args); } while (0)
22 #define DPRINTF(fmt, args...) do {} while(0)
26 #define MassStorageReset 0xff
27 #define GetMaxLun 0xfe
30 USB_MSDM_CBW, /* Command Block. */
31 USB_MSDM_DATAOUT, /* Tranfer data to device. */
32 USB_MSDM_DATAIN, /* Transfer data from device. */
33 USB_MSDM_CSW /* Command Status. */
49 /* For async completion. */
70 static const uint8_t qemu_msd_dev_descriptor[] = {
71 0x12, /* u8 bLength; */
72 0x01, /* u8 bDescriptorType; Device */
73 0x00, 0x01, /* u16 bcdUSB; v1.0 */
75 0x00, /* u8 bDeviceClass; */
76 0x00, /* u8 bDeviceSubClass; */
77 0x00, /* u8 bDeviceProtocol; [ low/full speeds only ] */
78 0x08, /* u8 bMaxPacketSize0; 8 Bytes */
80 /* Vendor and product id are arbitrary. */
81 0x00, 0x00, /* u16 idVendor; */
82 0x00, 0x00, /* u16 idProduct; */
83 0x00, 0x00, /* u16 bcdDevice */
85 0x01, /* u8 iManufacturer; */
86 0x02, /* u8 iProduct; */
87 0x03, /* u8 iSerialNumber; */
88 0x01 /* u8 bNumConfigurations; */
91 static const uint8_t qemu_msd_config_descriptor[] = {
93 /* one configuration */
94 0x09, /* u8 bLength; */
95 0x02, /* u8 bDescriptorType; Configuration */
96 0x20, 0x00, /* u16 wTotalLength; */
97 0x01, /* u8 bNumInterfaces; (1) */
98 0x01, /* u8 bConfigurationValue; */
99 0x00, /* u8 iConfiguration; */
100 0xc0, /* u8 bmAttributes;
105 0x00, /* u8 MaxPower; */
108 0x09, /* u8 if_bLength; */
109 0x04, /* u8 if_bDescriptorType; Interface */
110 0x00, /* u8 if_bInterfaceNumber; */
111 0x00, /* u8 if_bAlternateSetting; */
112 0x02, /* u8 if_bNumEndpoints; */
113 0x08, /* u8 if_bInterfaceClass; MASS STORAGE */
114 0x06, /* u8 if_bInterfaceSubClass; SCSI */
115 0x50, /* u8 if_bInterfaceProtocol; Bulk Only */
116 0x00, /* u8 if_iInterface; */
118 /* Bulk-In endpoint */
119 0x07, /* u8 ep_bLength; */
120 0x05, /* u8 ep_bDescriptorType; Endpoint */
121 0x81, /* u8 ep_bEndpointAddress; IN Endpoint 1 */
122 0x02, /* u8 ep_bmAttributes; Bulk */
123 0x40, 0x00, /* u16 ep_wMaxPacketSize; */
124 0x00, /* u8 ep_bInterval; */
126 /* Bulk-Out endpoint */
127 0x07, /* u8 ep_bLength; */
128 0x05, /* u8 ep_bDescriptorType; Endpoint */
129 0x02, /* u8 ep_bEndpointAddress; OUT Endpoint 2 */
130 0x02, /* u8 ep_bmAttributes; Bulk */
131 0x40, 0x00, /* u16 ep_wMaxPacketSize; */
132 0x00 /* u8 ep_bInterval; */
135 static void usb_msd_copy_data(MSDState *s)
139 if (len > s->scsi_len)
141 if (s->mode == USB_MSDM_DATAIN) {
142 memcpy(s->usb_buf, s->scsi_buf, len);
144 memcpy(s->scsi_buf, s->usb_buf, len);
151 if (s->scsi_len == 0) {
152 if (s->mode == USB_MSDM_DATAIN) {
153 s->scsi_dev->read_data(s->scsi_dev, s->tag);
154 } else if (s->mode == USB_MSDM_DATAOUT) {
155 s->scsi_dev->write_data(s->scsi_dev, s->tag);
160 static void usb_msd_send_status(MSDState *s)
162 struct usb_msd_csw csw;
164 csw.sig = cpu_to_le32(0x53425355);
165 csw.tag = cpu_to_le32(s->tag);
166 csw.residue = s->residue;
167 csw.status = s->result;
168 memcpy(s->usb_buf, &csw, 13);
171 static void usb_msd_command_complete(void *opaque, int reason, uint32_t tag,
174 MSDState *s = (MSDState *)opaque;
175 USBPacket *p = s->packet;
178 fprintf(stderr, "usb-msd: Unexpected SCSI Tag 0x%x\n", tag);
180 if (reason == SCSI_REASON_DONE) {
181 DPRINTF("Command complete %d\n", arg);
182 s->residue = s->data_len;
183 s->result = arg != 0;
185 if (s->data_len == 0 && s->mode == USB_MSDM_DATAOUT) {
186 /* A deferred packet with no write data remaining must be
187 the status read packet. */
188 usb_msd_send_status(s);
189 s->mode = USB_MSDM_CBW;
192 s->data_len -= s->usb_len;
193 if (s->mode == USB_MSDM_DATAIN)
194 memset(s->usb_buf, 0, s->usb_len);
197 if (s->data_len == 0)
198 s->mode = USB_MSDM_CSW;
201 usb_packet_complete(p);
202 } else if (s->data_len == 0) {
203 s->mode = USB_MSDM_CSW;
208 s->scsi_buf = s->scsi_dev->get_buf(s->scsi_dev, tag);
210 usb_msd_copy_data(s);
211 if (s->usb_len == 0) {
212 /* Set s->packet to NULL before calling usb_packet_complete
213 because annother request may be issued before
214 usb_packet_complete returns. */
215 DPRINTF("Packet complete %p\n", p);
217 usb_packet_complete(p);
222 static void usb_msd_handle_reset(USBDevice *dev)
224 MSDState *s = (MSDState *)dev;
227 s->mode = USB_MSDM_CBW;
230 static int usb_msd_handle_control(USBDevice *dev, int request, int value,
231 int index, int length, uint8_t *data)
233 MSDState *s = (MSDState *)dev;
237 case DeviceRequest | USB_REQ_GET_STATUS:
238 data[0] = (1 << USB_DEVICE_SELF_POWERED) |
239 (dev->remote_wakeup << USB_DEVICE_REMOTE_WAKEUP);
243 case DeviceOutRequest | USB_REQ_CLEAR_FEATURE:
244 if (value == USB_DEVICE_REMOTE_WAKEUP) {
245 dev->remote_wakeup = 0;
251 case DeviceOutRequest | USB_REQ_SET_FEATURE:
252 if (value == USB_DEVICE_REMOTE_WAKEUP) {
253 dev->remote_wakeup = 1;
259 case DeviceOutRequest | USB_REQ_SET_ADDRESS:
263 case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
266 memcpy(data, qemu_msd_dev_descriptor,
267 sizeof(qemu_msd_dev_descriptor));
268 ret = sizeof(qemu_msd_dev_descriptor);
271 memcpy(data, qemu_msd_config_descriptor,
272 sizeof(qemu_msd_config_descriptor));
273 ret = sizeof(qemu_msd_config_descriptor);
276 switch(value & 0xff) {
286 /* vendor description */
287 ret = set_usb_string(data, "QEMU " QEMU_VERSION);
290 /* product description */
291 ret = set_usb_string(data, "QEMU USB HARDDRIVE");
295 ret = set_usb_string(data, "1");
305 case DeviceRequest | USB_REQ_GET_CONFIGURATION:
309 case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
312 case DeviceRequest | USB_REQ_GET_INTERFACE:
316 case DeviceOutRequest | USB_REQ_SET_INTERFACE:
319 case EndpointOutRequest | USB_REQ_CLEAR_FEATURE:
320 if (value == 0 && index != 0x81) { /* clear ep halt */
325 /* Class specific requests. */
326 case MassStorageReset:
327 /* Reset state ready for the next CBW. */
328 s->mode = USB_MSDM_CBW;
343 static void usb_msd_cancel_io(USBPacket *p, void *opaque)
345 MSDState *s = opaque;
346 s->scsi_dev->cancel_io(s->scsi_dev, s->tag);
351 static int usb_msd_handle_data(USBDevice *dev, USBPacket *p)
353 MSDState *s = (MSDState *)dev;
355 struct usb_msd_cbw cbw;
356 uint8_t devep = p->devep;
357 uint8_t *data = p->data;
368 fprintf(stderr, "usb-msd: Bad CBW size");
371 memcpy(&cbw, data, 31);
372 if (le32_to_cpu(cbw.sig) != 0x43425355) {
373 fprintf(stderr, "usb-msd: Bad signature %08x\n",
374 le32_to_cpu(cbw.sig));
377 DPRINTF("Command on LUN %d\n", cbw.lun);
379 fprintf(stderr, "usb-msd: Bad LUN %d\n", cbw.lun);
382 s->tag = le32_to_cpu(cbw.tag);
383 s->data_len = le32_to_cpu(cbw.data_len);
384 if (s->data_len == 0) {
385 s->mode = USB_MSDM_CSW;
386 } else if (cbw.flags & 0x80) {
387 s->mode = USB_MSDM_DATAIN;
389 s->mode = USB_MSDM_DATAOUT;
391 DPRINTF("Command tag 0x%x flags %08x len %d data %d\n",
392 s->tag, cbw.flags, cbw.cmd_len, s->data_len);
394 s->scsi_dev->send_command(s->scsi_dev, s->tag, cbw.cmd, 0);
395 /* ??? Should check that USB and SCSI data transfer
397 if (s->residue == 0) {
398 if (s->mode == USB_MSDM_DATAIN) {
399 s->scsi_dev->read_data(s->scsi_dev, s->tag);
400 } else if (s->mode == USB_MSDM_DATAOUT) {
401 s->scsi_dev->write_data(s->scsi_dev, s->tag);
407 case USB_MSDM_DATAOUT:
408 DPRINTF("Data out %d/%d\n", len, s->data_len);
409 if (len > s->data_len)
415 usb_msd_copy_data(s);
417 if (s->residue && s->usb_len) {
418 s->data_len -= s->usb_len;
419 if (s->data_len == 0)
420 s->mode = USB_MSDM_CSW;
424 DPRINTF("Deferring packet %p\n", p);
425 usb_defer_packet(p, usb_msd_cancel_io, s);
434 DPRINTF("Unexpected write (len %d)\n", len);
444 case USB_MSDM_DATAOUT:
445 if (s->data_len != 0 || len < 13)
447 /* Waiting for SCSI write to complete. */
448 usb_defer_packet(p, usb_msd_cancel_io, s);
454 DPRINTF("Command status %d tag 0x%x, len %d\n",
455 s->result, s->tag, len);
461 usb_msd_send_status(s);
462 s->mode = USB_MSDM_CBW;
466 case USB_MSDM_DATAIN:
467 DPRINTF("Data in %d/%d\n", len, s->data_len);
468 if (len > s->data_len)
473 usb_msd_copy_data(s);
475 if (s->residue && s->usb_len) {
476 s->data_len -= s->usb_len;
477 memset(s->usb_buf, 0, s->usb_len);
478 if (s->data_len == 0)
479 s->mode = USB_MSDM_CSW;
483 DPRINTF("Deferring packet %p\n", p);
484 usb_defer_packet(p, usb_msd_cancel_io, s);
493 DPRINTF("Unexpected read (len %d)\n", len);
499 DPRINTF("Bad token\n");
508 static void usb_msd_handle_destroy(USBDevice *dev)
510 MSDState *s = (MSDState *)dev;
512 s->scsi_dev->destroy(s->scsi_dev);
517 USBDevice *usb_msd_init(const char *filename)
520 BlockDriverState *bdrv;
521 BlockDriver *drv = NULL;
525 p1 = strchr(filename, ':');
529 if (strstart(filename, "format=", &p2)) {
530 int len = MIN(p1 - p2, sizeof(fmt));
531 pstrcpy(fmt, len, p2);
533 drv = bdrv_find_format(fmt);
535 printf("invalid format %s\n", fmt);
538 } else if (*filename != ':') {
539 printf("unrecognized USB mass-storage option %s\n", filename);
547 printf("block device specification needed\n");
551 s = qemu_mallocz(sizeof(MSDState));
553 bdrv = bdrv_new("usb");
554 if (bdrv_open2(bdrv, filename, 0, drv) < 0)
558 s->dev.speed = USB_SPEED_FULL;
559 s->dev.handle_packet = usb_generic_handle_packet;
561 s->dev.handle_reset = usb_msd_handle_reset;
562 s->dev.handle_control = usb_msd_handle_control;
563 s->dev.handle_data = usb_msd_handle_data;
564 s->dev.handle_destroy = usb_msd_handle_destroy;
566 snprintf(s->dev.devname, sizeof(s->dev.devname), "QEMU USB MSD(%.16s)",
569 s->scsi_dev = scsi_disk_init(bdrv, 0, usb_msd_command_complete, s);
570 usb_msd_handle_reset((USBDevice *)s);
571 return (USBDevice *)s;
577 BlockDriverState *usb_msd_get_bdrv(USBDevice *dev)
579 MSDState *s = (MSDState *)dev;