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. */
41 static const uint8_t qemu_msd_dev_descriptor[] = {
42 0x12, /* u8 bLength; */
43 0x01, /* u8 bDescriptorType; Device */
44 0x10, 0x00, /* u16 bcdUSB; v1.0 */
46 0x00, /* u8 bDeviceClass; */
47 0x00, /* u8 bDeviceSubClass; */
48 0x00, /* u8 bDeviceProtocol; [ low/full speeds only ] */
49 0x08, /* u8 bMaxPacketSize0; 8 Bytes */
51 /* Vendor and product id are arbitrary. */
52 0x00, 0x00, /* u16 idVendor; */
53 0x00, 0x00, /* u16 idProduct; */
54 0x00, 0x00, /* u16 bcdDevice */
56 0x01, /* u8 iManufacturer; */
57 0x02, /* u8 iProduct; */
58 0x03, /* u8 iSerialNumber; */
59 0x01 /* u8 bNumConfigurations; */
62 static const uint8_t qemu_msd_config_descriptor[] = {
64 /* one configuration */
65 0x09, /* u8 bLength; */
66 0x02, /* u8 bDescriptorType; Configuration */
67 0x20, 0x00, /* u16 wTotalLength; */
68 0x01, /* u8 bNumInterfaces; (1) */
69 0x01, /* u8 bConfigurationValue; */
70 0x00, /* u8 iConfiguration; */
71 0xc0, /* u8 bmAttributes;
76 0x00, /* u8 MaxPower; */
79 0x09, /* u8 if_bLength; */
80 0x04, /* u8 if_bDescriptorType; Interface */
81 0x00, /* u8 if_bInterfaceNumber; */
82 0x00, /* u8 if_bAlternateSetting; */
83 0x02, /* u8 if_bNumEndpoints; */
84 0x08, /* u8 if_bInterfaceClass; MASS STORAGE */
85 0x06, /* u8 if_bInterfaceSubClass; SCSI */
86 0x50, /* u8 if_bInterfaceProtocol; Bulk Only */
87 0x00, /* u8 if_iInterface; */
89 /* Bulk-In endpoint */
90 0x07, /* u8 ep_bLength; */
91 0x05, /* u8 ep_bDescriptorType; Endpoint */
92 0x81, /* u8 ep_bEndpointAddress; IN Endpoint 1 */
93 0x02, /* u8 ep_bmAttributes; Bulk */
94 0x40, 0x00, /* u16 ep_wMaxPacketSize; */
95 0x00, /* u8 ep_bInterval; */
97 /* Bulk-Out endpoint */
98 0x07, /* u8 ep_bLength; */
99 0x05, /* u8 ep_bDescriptorType; Endpoint */
100 0x02, /* u8 ep_bEndpointAddress; OUT Endpoint 2 */
101 0x02, /* u8 ep_bmAttributes; Bulk */
102 0x40, 0x00, /* u16 ep_wMaxPacketSize; */
103 0x00 /* u8 ep_bInterval; */
106 static void usb_msd_command_complete(void *opaque, uint32_t tag, int fail)
108 MSDState *s = (MSDState *)opaque;
110 DPRINTF("Command complete\n");
112 s->mode = USB_MSDM_CSW;
115 static void usb_msd_handle_reset(USBDevice *dev)
117 MSDState *s = (MSDState *)dev;
120 s->mode = USB_MSDM_CBW;
123 static int usb_msd_handle_control(USBDevice *dev, int request, int value,
124 int index, int length, uint8_t *data)
126 MSDState *s = (MSDState *)dev;
130 case DeviceRequest | USB_REQ_GET_STATUS:
131 data[0] = (1 << USB_DEVICE_SELF_POWERED) |
132 (dev->remote_wakeup << USB_DEVICE_REMOTE_WAKEUP);
136 case DeviceOutRequest | USB_REQ_CLEAR_FEATURE:
137 if (value == USB_DEVICE_REMOTE_WAKEUP) {
138 dev->remote_wakeup = 0;
144 case DeviceOutRequest | USB_REQ_SET_FEATURE:
145 if (value == USB_DEVICE_REMOTE_WAKEUP) {
146 dev->remote_wakeup = 1;
152 case DeviceOutRequest | USB_REQ_SET_ADDRESS:
156 case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
159 memcpy(data, qemu_msd_dev_descriptor,
160 sizeof(qemu_msd_dev_descriptor));
161 ret = sizeof(qemu_msd_dev_descriptor);
164 memcpy(data, qemu_msd_config_descriptor,
165 sizeof(qemu_msd_config_descriptor));
166 ret = sizeof(qemu_msd_config_descriptor);
169 switch(value & 0xff) {
179 /* vendor description */
180 ret = set_usb_string(data, "QEMU " QEMU_VERSION);
183 /* product description */
184 ret = set_usb_string(data, "QEMU USB HARDDRIVE");
188 ret = set_usb_string(data, "1");
198 case DeviceRequest | USB_REQ_GET_CONFIGURATION:
202 case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
205 case DeviceRequest | USB_REQ_GET_INTERFACE:
209 case DeviceOutRequest | USB_REQ_SET_INTERFACE:
212 case EndpointOutRequest | USB_REQ_CLEAR_FEATURE:
213 if (value == 0 && index != 0x81) { /* clear ep halt */
218 /* Class specific requests. */
219 case MassStorageReset:
220 /* Reset state ready for the next CBW. */
221 s->mode = USB_MSDM_CBW;
253 static int usb_msd_handle_data(USBDevice *dev, int pid, uint8_t devep,
254 uint8_t *data, int len)
256 MSDState *s = (MSDState *)dev;
258 struct usb_msd_cbw cbw;
259 struct usb_msd_csw csw;
269 fprintf(stderr, "usb-msd: Bad CBW size");
272 memcpy(&cbw, data, 31);
273 if (le32_to_cpu(cbw.sig) != 0x43425355) {
274 fprintf(stderr, "usb-msd: Bad signature %08x\n",
275 le32_to_cpu(cbw.sig));
278 DPRINTF("Command on LUN %d\n", cbw.lun);
280 fprintf(stderr, "usb-msd: Bad LUN %d\n", cbw.lun);
283 s->tag = le32_to_cpu(cbw.tag);
284 s->data_len = le32_to_cpu(cbw.data_len);
285 if (s->data_len == 0) {
286 s->mode = USB_MSDM_CSW;
287 } else if (cbw.flags & 0x80) {
288 s->mode = USB_MSDM_DATAIN;
290 s->mode = USB_MSDM_DATAOUT;
292 DPRINTF("Command tag 0x%x flags %08x len %d data %d\n",
293 s->tag, cbw.flags, cbw.cmd_len, s->data_len);
294 scsi_send_command(s->scsi_dev, s->tag, cbw.cmd, 0);
298 case USB_MSDM_DATAOUT:
299 DPRINTF("Data out %d/%d\n", len, s->data_len);
300 if (len > s->data_len)
303 if (scsi_write_data(s->scsi_dev, data, len))
307 if (s->data_len == 0)
308 s->mode = USB_MSDM_CSW;
313 DPRINTF("Unexpected write (len %d)\n", len);
324 DPRINTF("Command status %d tag 0x%x, len %d\n",
325 s->result, s->tag, len);
329 csw.sig = cpu_to_le32(0x53425355);
330 csw.tag = cpu_to_le32(s->tag);
332 csw.status = s->result;
333 memcpy(data, &csw, 13);
335 s->mode = USB_MSDM_CBW;
338 case USB_MSDM_DATAIN:
339 DPRINTF("Data in %d/%d\n", len, s->data_len);
340 if (len > s->data_len)
343 if (scsi_read_data(s->scsi_dev, data, len))
347 if (s->data_len == 0)
348 s->mode = USB_MSDM_CSW;
353 DPRINTF("Unexpected read (len %d)\n", len);
359 DPRINTF("Bad token\n");
368 static void usb_msd_handle_destroy(USBDevice *dev)
370 MSDState *s = (MSDState *)dev;
372 scsi_disk_destroy(s->scsi_dev);
376 USBDevice *usb_msd_init(const char *filename)
379 BlockDriverState *bdrv;
381 s = qemu_mallocz(sizeof(MSDState));
385 bdrv = bdrv_new("usb");
386 bdrv_open(bdrv, filename, 0);
388 s->dev.speed = USB_SPEED_FULL;
389 s->dev.handle_packet = usb_generic_handle_packet;
391 s->dev.handle_reset = usb_msd_handle_reset;
392 s->dev.handle_control = usb_msd_handle_control;
393 s->dev.handle_data = usb_msd_handle_data;
394 s->dev.handle_destroy = usb_msd_handle_destroy;
396 snprintf(s->dev.devname, sizeof(s->dev.devname), "QEMU USB MSD(%.16s)",
399 s->scsi_dev = scsi_disk_init(bdrv, usb_msd_command_complete, s);
400 usb_msd_handle_reset((USBDevice *)s);
401 return (USBDevice *)s;