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;
40 /* For async completion. */
44 static const uint8_t qemu_msd_dev_descriptor[] = {
45 0x12, /* u8 bLength; */
46 0x01, /* u8 bDescriptorType; Device */
47 0x10, 0x00, /* u16 bcdUSB; v1.0 */
49 0x00, /* u8 bDeviceClass; */
50 0x00, /* u8 bDeviceSubClass; */
51 0x00, /* u8 bDeviceProtocol; [ low/full speeds only ] */
52 0x08, /* u8 bMaxPacketSize0; 8 Bytes */
54 /* Vendor and product id are arbitrary. */
55 0x00, 0x00, /* u16 idVendor; */
56 0x00, 0x00, /* u16 idProduct; */
57 0x00, 0x00, /* u16 bcdDevice */
59 0x01, /* u8 iManufacturer; */
60 0x02, /* u8 iProduct; */
61 0x03, /* u8 iSerialNumber; */
62 0x01 /* u8 bNumConfigurations; */
65 static const uint8_t qemu_msd_config_descriptor[] = {
67 /* one configuration */
68 0x09, /* u8 bLength; */
69 0x02, /* u8 bDescriptorType; Configuration */
70 0x20, 0x00, /* u16 wTotalLength; */
71 0x01, /* u8 bNumInterfaces; (1) */
72 0x01, /* u8 bConfigurationValue; */
73 0x00, /* u8 iConfiguration; */
74 0xc0, /* u8 bmAttributes;
79 0x00, /* u8 MaxPower; */
82 0x09, /* u8 if_bLength; */
83 0x04, /* u8 if_bDescriptorType; Interface */
84 0x00, /* u8 if_bInterfaceNumber; */
85 0x00, /* u8 if_bAlternateSetting; */
86 0x02, /* u8 if_bNumEndpoints; */
87 0x08, /* u8 if_bInterfaceClass; MASS STORAGE */
88 0x06, /* u8 if_bInterfaceSubClass; SCSI */
89 0x50, /* u8 if_bInterfaceProtocol; Bulk Only */
90 0x00, /* u8 if_iInterface; */
92 /* Bulk-In endpoint */
93 0x07, /* u8 ep_bLength; */
94 0x05, /* u8 ep_bDescriptorType; Endpoint */
95 0x81, /* u8 ep_bEndpointAddress; IN Endpoint 1 */
96 0x02, /* u8 ep_bmAttributes; Bulk */
97 0x40, 0x00, /* u16 ep_wMaxPacketSize; */
98 0x00, /* u8 ep_bInterval; */
100 /* Bulk-Out endpoint */
101 0x07, /* u8 ep_bLength; */
102 0x05, /* u8 ep_bDescriptorType; Endpoint */
103 0x02, /* u8 ep_bEndpointAddress; OUT Endpoint 2 */
104 0x02, /* u8 ep_bmAttributes; Bulk */
105 0x40, 0x00, /* u16 ep_wMaxPacketSize; */
106 0x00 /* u8 ep_bInterval; */
109 static void usb_msd_command_complete(void *opaque, uint32_t reason, int fail)
111 MSDState *s = (MSDState *)opaque;
114 s->data_len -= s->transfer_len;
116 if (reason == SCSI_REASON_DONE) {
117 DPRINTF("Command complete %d\n", fail);
119 s->mode = USB_MSDM_CSW;
122 /* Set s->packet to NULL before calling usb_packet_complete because
123 annother request may be issues before usb_packet_complete returns.
125 DPRINTF("Packet complete %p\n", p);
128 usb_packet_complete(p);
132 static void usb_msd_handle_reset(USBDevice *dev)
134 MSDState *s = (MSDState *)dev;
137 s->mode = USB_MSDM_CBW;
140 static int usb_msd_handle_control(USBDevice *dev, int request, int value,
141 int index, int length, uint8_t *data)
143 MSDState *s = (MSDState *)dev;
147 case DeviceRequest | USB_REQ_GET_STATUS:
148 data[0] = (1 << USB_DEVICE_SELF_POWERED) |
149 (dev->remote_wakeup << USB_DEVICE_REMOTE_WAKEUP);
153 case DeviceOutRequest | USB_REQ_CLEAR_FEATURE:
154 if (value == USB_DEVICE_REMOTE_WAKEUP) {
155 dev->remote_wakeup = 0;
161 case DeviceOutRequest | USB_REQ_SET_FEATURE:
162 if (value == USB_DEVICE_REMOTE_WAKEUP) {
163 dev->remote_wakeup = 1;
169 case DeviceOutRequest | USB_REQ_SET_ADDRESS:
173 case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
176 memcpy(data, qemu_msd_dev_descriptor,
177 sizeof(qemu_msd_dev_descriptor));
178 ret = sizeof(qemu_msd_dev_descriptor);
181 memcpy(data, qemu_msd_config_descriptor,
182 sizeof(qemu_msd_config_descriptor));
183 ret = sizeof(qemu_msd_config_descriptor);
186 switch(value & 0xff) {
196 /* vendor description */
197 ret = set_usb_string(data, "QEMU " QEMU_VERSION);
200 /* product description */
201 ret = set_usb_string(data, "QEMU USB HARDDRIVE");
205 ret = set_usb_string(data, "1");
215 case DeviceRequest | USB_REQ_GET_CONFIGURATION:
219 case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
222 case DeviceRequest | USB_REQ_GET_INTERFACE:
226 case DeviceOutRequest | USB_REQ_SET_INTERFACE:
229 case EndpointOutRequest | USB_REQ_CLEAR_FEATURE:
230 if (value == 0 && index != 0x81) { /* clear ep halt */
235 /* Class specific requests. */
236 case MassStorageReset:
237 /* Reset state ready for the next CBW. */
238 s->mode = USB_MSDM_CBW;
270 static void usb_msd_cancel_io(USBPacket *p, void *opaque)
272 MSDState *s = opaque;
273 scsi_cancel_io(s->scsi_dev);
277 static int usb_msd_handle_data(USBDevice *dev, USBPacket *p)
279 MSDState *s = (MSDState *)dev;
281 struct usb_msd_cbw cbw;
282 struct usb_msd_csw csw;
283 uint8_t devep = p->devep;
284 uint8_t *data = p->data;
295 fprintf(stderr, "usb-msd: Bad CBW size");
298 memcpy(&cbw, data, 31);
299 if (le32_to_cpu(cbw.sig) != 0x43425355) {
300 fprintf(stderr, "usb-msd: Bad signature %08x\n",
301 le32_to_cpu(cbw.sig));
304 DPRINTF("Command on LUN %d\n", cbw.lun);
306 fprintf(stderr, "usb-msd: Bad LUN %d\n", cbw.lun);
309 s->tag = le32_to_cpu(cbw.tag);
310 s->data_len = le32_to_cpu(cbw.data_len);
311 if (s->data_len == 0) {
312 s->mode = USB_MSDM_CSW;
313 } else if (cbw.flags & 0x80) {
314 s->mode = USB_MSDM_DATAIN;
316 s->mode = USB_MSDM_DATAOUT;
318 DPRINTF("Command tag 0x%x flags %08x len %d data %d\n",
319 s->tag, cbw.flags, cbw.cmd_len, s->data_len);
320 scsi_send_command(s->scsi_dev, s->tag, cbw.cmd, 0);
324 case USB_MSDM_DATAOUT:
325 DPRINTF("Data out %d/%d\n", len, s->data_len);
326 if (len > s->data_len)
329 s->transfer_len = len;
330 if (scsi_write_data(s->scsi_dev, data, len))
333 if (s->transfer_len == 0) {
336 DPRINTF("Deferring packet %p\n", p);
337 usb_defer_packet(p, usb_msd_cancel_io, s);
344 DPRINTF("Unexpected write (len %d)\n", len);
355 DPRINTF("Command status %d tag 0x%x, len %d\n",
356 s->result, s->tag, len);
360 csw.sig = cpu_to_le32(0x53425355);
361 csw.tag = cpu_to_le32(s->tag);
363 csw.status = s->result;
364 memcpy(data, &csw, 13);
366 s->mode = USB_MSDM_CBW;
369 case USB_MSDM_DATAIN:
370 DPRINTF("Data in %d/%d\n", len, s->data_len);
371 if (len > s->data_len)
374 s->transfer_len = len;
375 if (scsi_read_data(s->scsi_dev, data, len))
378 if (s->transfer_len == 0) {
381 DPRINTF("Deferring packet %p\n", p);
382 usb_defer_packet(p, usb_msd_cancel_io, s);
389 DPRINTF("Unexpected read (len %d)\n", len);
395 DPRINTF("Bad token\n");
404 static void usb_msd_handle_destroy(USBDevice *dev)
406 MSDState *s = (MSDState *)dev;
408 scsi_disk_destroy(s->scsi_dev);
412 USBDevice *usb_msd_init(const char *filename)
415 BlockDriverState *bdrv;
417 s = qemu_mallocz(sizeof(MSDState));
421 bdrv = bdrv_new("usb");
422 bdrv_open(bdrv, filename, 0);
424 s->dev.speed = USB_SPEED_FULL;
425 s->dev.handle_packet = usb_generic_handle_packet;
427 s->dev.handle_reset = usb_msd_handle_reset;
428 s->dev.handle_control = usb_msd_handle_control;
429 s->dev.handle_data = usb_msd_handle_data;
430 s->dev.handle_destroy = usb_msd_handle_destroy;
432 snprintf(s->dev.devname, sizeof(s->dev.devname), "QEMU USB MSD(%.16s)",
435 s->scsi_dev = scsi_disk_init(bdrv, usb_msd_command_complete, s);
436 usb_msd_handle_reset((USBDevice *)s);
437 return (USBDevice *)s;