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, int destroy)
117 MSDState *s = (MSDState *)dev;
120 s->mode = USB_MSDM_CBW;
122 scsi_disk_destroy(s->scsi_dev);
127 static int usb_msd_handle_control(USBDevice *dev, int request, int value,
128 int index, int length, uint8_t *data)
130 MSDState *s = (MSDState *)dev;
134 case DeviceRequest | USB_REQ_GET_STATUS:
135 data[0] = (1 << USB_DEVICE_SELF_POWERED) |
136 (dev->remote_wakeup << USB_DEVICE_REMOTE_WAKEUP);
140 case DeviceOutRequest | USB_REQ_CLEAR_FEATURE:
141 if (value == USB_DEVICE_REMOTE_WAKEUP) {
142 dev->remote_wakeup = 0;
148 case DeviceOutRequest | USB_REQ_SET_FEATURE:
149 if (value == USB_DEVICE_REMOTE_WAKEUP) {
150 dev->remote_wakeup = 1;
156 case DeviceOutRequest | USB_REQ_SET_ADDRESS:
160 case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
163 memcpy(data, qemu_msd_dev_descriptor,
164 sizeof(qemu_msd_dev_descriptor));
165 ret = sizeof(qemu_msd_dev_descriptor);
168 memcpy(data, qemu_msd_config_descriptor,
169 sizeof(qemu_msd_config_descriptor));
170 ret = sizeof(qemu_msd_config_descriptor);
173 switch(value & 0xff) {
183 /* vendor description */
184 ret = set_usb_string(data, "QEMU " QEMU_VERSION);
187 /* product description */
188 ret = set_usb_string(data, "QEMU USB HARDDRIVE");
192 ret = set_usb_string(data, "1");
202 case DeviceRequest | USB_REQ_GET_CONFIGURATION:
206 case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
209 case DeviceRequest | USB_REQ_GET_INTERFACE:
213 case DeviceOutRequest | USB_REQ_SET_INTERFACE:
216 case EndpointOutRequest | USB_REQ_CLEAR_FEATURE:
217 if (value == 0 && index != 0x81) { /* clear ep halt */
222 /* Class specific requests. */
223 case MassStorageReset:
224 /* Reset state ready for the next CBW. */
225 s->mode = USB_MSDM_CBW;
257 static int usb_msd_handle_data(USBDevice *dev, int pid, uint8_t devep,
258 uint8_t *data, int len)
260 MSDState *s = (MSDState *)dev;
262 struct usb_msd_cbw cbw;
263 struct usb_msd_csw csw;
273 fprintf(stderr, "usb-msd: Bad CBW size");
276 memcpy(&cbw, data, 31);
277 if (le32_to_cpu(cbw.sig) != 0x43425355) {
278 fprintf(stderr, "usb-msd: Bad signature %08x\n",
279 le32_to_cpu(cbw.sig));
282 DPRINTF("Command on LUN %d\n", cbw.lun);
284 fprintf(stderr, "usb-msd: Bad LUN %d\n", cbw.lun);
287 s->tag = le32_to_cpu(cbw.tag);
288 s->data_len = le32_to_cpu(cbw.data_len);
289 if (s->data_len == 0) {
290 s->mode = USB_MSDM_CSW;
291 } else if (cbw.flags & 0x80) {
292 s->mode = USB_MSDM_DATAIN;
294 s->mode = USB_MSDM_DATAOUT;
296 DPRINTF("Command tag 0x%x flags %08x len %d data %d\n",
297 s->tag, cbw.flags, cbw.cmd_len, s->data_len);
298 scsi_send_command(s->scsi_dev, s->tag, cbw.cmd, 0);
302 case USB_MSDM_DATAOUT:
303 DPRINTF("Data out %d/%d\n", len, s->data_len);
304 if (len > s->data_len)
307 if (scsi_write_data(s->scsi_dev, data, len))
311 if (s->data_len == 0)
312 s->mode = USB_MSDM_CSW;
317 DPRINTF("Unexpected write (len %d)\n", len);
328 DPRINTF("Command status %d tag 0x%x, len %d\n",
329 s->result, s->tag, len);
333 csw.sig = cpu_to_le32(0x53425355);
334 csw.tag = cpu_to_le32(s->tag);
336 csw.status = s->result;
337 memcpy(data, &csw, 13);
339 s->mode = USB_MSDM_CBW;
342 case USB_MSDM_DATAIN:
343 DPRINTF("Data in %d/%d\n", len, s->data_len);
344 if (len > s->data_len)
347 if (scsi_read_data(s->scsi_dev, data, len))
351 if (s->data_len == 0)
352 s->mode = USB_MSDM_CSW;
357 DPRINTF("Unexpected read (len %d)\n", len);
363 DPRINTF("Bad token\n");
373 USBDevice *usb_msd_init(const char *filename)
376 BlockDriverState *bdrv;
378 s = qemu_mallocz(sizeof(MSDState));
382 bdrv = bdrv_new("usb");
383 bdrv_open(bdrv, filename, 0);
385 s->dev.speed = USB_SPEED_FULL;
386 s->dev.handle_packet = usb_generic_handle_packet;
388 s->dev.handle_reset = usb_msd_handle_reset;
389 s->dev.handle_control = usb_msd_handle_control;
390 s->dev.handle_data = usb_msd_handle_data;
392 s->scsi_dev = scsi_disk_init(bdrv, usb_msd_command_complete, s);
393 usb_msd_handle_reset((USBDevice *)s, 0);
394 return (USBDevice *)s;