2 * USB Mass Storage Device emulation
4 * Copyright (c) 2006 CodeSourcery.
5 * Written by Paul Brook
7 * This code is licenced under the LGPL.
15 #define DPRINTF(fmt, args...) \
16 do { printf("usb-msd: " fmt , ##args); } while (0)
18 #define DPRINTF(fmt, args...) do {} while(0)
22 #define MassStorageReset 0xff
23 #define GetMaxLun 0xfe
26 USB_MSDM_CBW, /* Command Block. */
27 USB_MSDM_DATAOUT, /* Tranfer data to device. */
28 USB_MSDM_DATAIN, /* Transfer data from device. */
29 USB_MSDM_CSW /* Command Status. */
36 uint32_t transfer_len;
41 /* For async completion. */
45 static const uint8_t qemu_msd_dev_descriptor[] = {
46 0x12, /* u8 bLength; */
47 0x01, /* u8 bDescriptorType; Device */
48 0x10, 0x00, /* u16 bcdUSB; v1.0 */
50 0x00, /* u8 bDeviceClass; */
51 0x00, /* u8 bDeviceSubClass; */
52 0x00, /* u8 bDeviceProtocol; [ low/full speeds only ] */
53 0x08, /* u8 bMaxPacketSize0; 8 Bytes */
55 /* Vendor and product id are arbitrary. */
56 0x00, 0x00, /* u16 idVendor; */
57 0x00, 0x00, /* u16 idProduct; */
58 0x00, 0x00, /* u16 bcdDevice */
60 0x01, /* u8 iManufacturer; */
61 0x02, /* u8 iProduct; */
62 0x03, /* u8 iSerialNumber; */
63 0x01 /* u8 bNumConfigurations; */
66 static const uint8_t qemu_msd_config_descriptor[] = {
68 /* one configuration */
69 0x09, /* u8 bLength; */
70 0x02, /* u8 bDescriptorType; Configuration */
71 0x20, 0x00, /* u16 wTotalLength; */
72 0x01, /* u8 bNumInterfaces; (1) */
73 0x01, /* u8 bConfigurationValue; */
74 0x00, /* u8 iConfiguration; */
75 0xc0, /* u8 bmAttributes;
80 0x00, /* u8 MaxPower; */
83 0x09, /* u8 if_bLength; */
84 0x04, /* u8 if_bDescriptorType; Interface */
85 0x00, /* u8 if_bInterfaceNumber; */
86 0x00, /* u8 if_bAlternateSetting; */
87 0x02, /* u8 if_bNumEndpoints; */
88 0x08, /* u8 if_bInterfaceClass; MASS STORAGE */
89 0x06, /* u8 if_bInterfaceSubClass; SCSI */
90 0x50, /* u8 if_bInterfaceProtocol; Bulk Only */
91 0x00, /* u8 if_iInterface; */
93 /* Bulk-In endpoint */
94 0x07, /* u8 ep_bLength; */
95 0x05, /* u8 ep_bDescriptorType; Endpoint */
96 0x81, /* u8 ep_bEndpointAddress; IN Endpoint 1 */
97 0x02, /* u8 ep_bmAttributes; Bulk */
98 0x40, 0x00, /* u16 ep_wMaxPacketSize; */
99 0x00, /* u8 ep_bInterval; */
101 /* Bulk-Out endpoint */
102 0x07, /* u8 ep_bLength; */
103 0x05, /* u8 ep_bDescriptorType; Endpoint */
104 0x02, /* u8 ep_bEndpointAddress; OUT Endpoint 2 */
105 0x02, /* u8 ep_bmAttributes; Bulk */
106 0x40, 0x00, /* u16 ep_wMaxPacketSize; */
107 0x00 /* u8 ep_bInterval; */
110 static void usb_msd_command_complete(void *opaque, uint32_t reason, int fail)
112 MSDState *s = (MSDState *)opaque;
115 s->data_len -= s->transfer_len;
117 if (reason == SCSI_REASON_DONE) {
118 DPRINTF("Command complete %d\n", fail);
120 s->mode = USB_MSDM_CSW;
123 /* Set s->packet to NULL before calling usb_packet_complete because
124 annother request may be issues before usb_packet_complete returns.
126 DPRINTF("Packet complete %p\n", p);
129 usb_packet_complete(p);
133 static void usb_msd_handle_reset(USBDevice *dev)
135 MSDState *s = (MSDState *)dev;
138 s->mode = USB_MSDM_CBW;
141 static int usb_msd_handle_control(USBDevice *dev, int request, int value,
142 int index, int length, uint8_t *data)
144 MSDState *s = (MSDState *)dev;
148 case DeviceRequest | USB_REQ_GET_STATUS:
149 data[0] = (1 << USB_DEVICE_SELF_POWERED) |
150 (dev->remote_wakeup << USB_DEVICE_REMOTE_WAKEUP);
154 case DeviceOutRequest | USB_REQ_CLEAR_FEATURE:
155 if (value == USB_DEVICE_REMOTE_WAKEUP) {
156 dev->remote_wakeup = 0;
162 case DeviceOutRequest | USB_REQ_SET_FEATURE:
163 if (value == USB_DEVICE_REMOTE_WAKEUP) {
164 dev->remote_wakeup = 1;
170 case DeviceOutRequest | USB_REQ_SET_ADDRESS:
174 case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
177 memcpy(data, qemu_msd_dev_descriptor,
178 sizeof(qemu_msd_dev_descriptor));
179 ret = sizeof(qemu_msd_dev_descriptor);
182 memcpy(data, qemu_msd_config_descriptor,
183 sizeof(qemu_msd_config_descriptor));
184 ret = sizeof(qemu_msd_config_descriptor);
187 switch(value & 0xff) {
197 /* vendor description */
198 ret = set_usb_string(data, "QEMU " QEMU_VERSION);
201 /* product description */
202 ret = set_usb_string(data, "QEMU USB HARDDRIVE");
206 ret = set_usb_string(data, "1");
216 case DeviceRequest | USB_REQ_GET_CONFIGURATION:
220 case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
223 case DeviceRequest | USB_REQ_GET_INTERFACE:
227 case DeviceOutRequest | USB_REQ_SET_INTERFACE:
230 case EndpointOutRequest | USB_REQ_CLEAR_FEATURE:
231 if (value == 0 && index != 0x81) { /* clear ep halt */
236 /* Class specific requests. */
237 case MassStorageReset:
238 /* Reset state ready for the next CBW. */
239 s->mode = USB_MSDM_CBW;
271 static void usb_msd_cancel_io(USBPacket *p, void *opaque)
273 MSDState *s = opaque;
274 scsi_cancel_io(s->scsi_dev);
278 static int usb_msd_handle_data(USBDevice *dev, USBPacket *p)
280 MSDState *s = (MSDState *)dev;
282 struct usb_msd_cbw cbw;
283 struct usb_msd_csw csw;
284 uint8_t devep = p->devep;
285 uint8_t *data = p->data;
296 fprintf(stderr, "usb-msd: Bad CBW size");
299 memcpy(&cbw, data, 31);
300 if (le32_to_cpu(cbw.sig) != 0x43425355) {
301 fprintf(stderr, "usb-msd: Bad signature %08x\n",
302 le32_to_cpu(cbw.sig));
305 DPRINTF("Command on LUN %d\n", cbw.lun);
307 fprintf(stderr, "usb-msd: Bad LUN %d\n", cbw.lun);
310 s->tag = le32_to_cpu(cbw.tag);
311 s->data_len = le32_to_cpu(cbw.data_len);
312 if (s->data_len == 0) {
313 s->mode = USB_MSDM_CSW;
314 } else if (cbw.flags & 0x80) {
315 s->mode = USB_MSDM_DATAIN;
317 s->mode = USB_MSDM_DATAOUT;
319 DPRINTF("Command tag 0x%x flags %08x len %d data %d\n",
320 s->tag, cbw.flags, cbw.cmd_len, s->data_len);
321 scsi_send_command(s->scsi_dev, s->tag, cbw.cmd, 0);
325 case USB_MSDM_DATAOUT:
326 DPRINTF("Data out %d/%d\n", len, s->data_len);
327 if (len > s->data_len)
330 s->transfer_len = len;
331 if (scsi_write_data(s->scsi_dev, data, len))
334 if (s->transfer_len == 0) {
337 DPRINTF("Deferring packet %p\n", p);
338 usb_defer_packet(p, usb_msd_cancel_io, s);
345 DPRINTF("Unexpected write (len %d)\n", len);
356 DPRINTF("Command status %d tag 0x%x, len %d\n",
357 s->result, s->tag, len);
361 csw.sig = cpu_to_le32(0x53425355);
362 csw.tag = cpu_to_le32(s->tag);
364 csw.status = s->result;
365 memcpy(data, &csw, 13);
367 s->mode = USB_MSDM_CBW;
370 case USB_MSDM_DATAIN:
371 DPRINTF("Data in %d/%d\n", len, s->data_len);
372 if (len > s->data_len)
375 s->transfer_len = len;
376 if (scsi_read_data(s->scsi_dev, data, len))
379 if (s->transfer_len == 0) {
382 DPRINTF("Deferring packet %p\n", p);
383 usb_defer_packet(p, usb_msd_cancel_io, s);
390 DPRINTF("Unexpected read (len %d)\n", len);
396 DPRINTF("Bad token\n");
405 static void usb_msd_handle_destroy(USBDevice *dev)
407 MSDState *s = (MSDState *)dev;
409 scsi_disk_destroy(s->scsi_dev);
414 USBDevice *usb_msd_init(const char *filename)
417 BlockDriverState *bdrv;
419 s = qemu_mallocz(sizeof(MSDState));
423 bdrv = bdrv_new("usb");
424 if (bdrv_open(bdrv, filename, 0) < 0)
428 s->dev.speed = USB_SPEED_FULL;
429 s->dev.handle_packet = usb_generic_handle_packet;
431 s->dev.handle_reset = usb_msd_handle_reset;
432 s->dev.handle_control = usb_msd_handle_control;
433 s->dev.handle_data = usb_msd_handle_data;
434 s->dev.handle_destroy = usb_msd_handle_destroy;
436 snprintf(s->dev.devname, sizeof(s->dev.devname), "QEMU USB MSD(%.16s)",
439 s->scsi_dev = scsi_disk_init(bdrv, usb_msd_command_complete, s);
440 usb_msd_handle_reset((USBDevice *)s);
441 return (USBDevice *)s;