Statistics
| Branch: | Revision:

root / hw / usb / dev-storage.c @ 9c17d615

History | View | Annotate | Download (20.2 kB)

1
/*
2
 * USB Mass Storage Device emulation
3
 *
4
 * Copyright (c) 2006 CodeSourcery.
5
 * Written by Paul Brook
6
 *
7
 * This code is licensed under the LGPL.
8
 */
9

    
10
#include "qemu-common.h"
11
#include "qemu/option.h"
12
#include "qemu/config-file.h"
13
#include "hw/usb.h"
14
#include "hw/usb/desc.h"
15
#include "hw/scsi.h"
16
#include "ui/console.h"
17
#include "monitor/monitor.h"
18
#include "sysemu/sysemu.h"
19
#include "sysemu/blockdev.h"
20

    
21
//#define DEBUG_MSD
22

    
23
#ifdef DEBUG_MSD
24
#define DPRINTF(fmt, ...) \
25
do { printf("usb-msd: " fmt , ## __VA_ARGS__); } while (0)
26
#else
27
#define DPRINTF(fmt, ...) do {} while(0)
28
#endif
29

    
30
/* USB requests.  */
31
#define MassStorageReset  0xff
32
#define GetMaxLun         0xfe
33

    
34
enum USBMSDMode {
35
    USB_MSDM_CBW, /* Command Block.  */
36
    USB_MSDM_DATAOUT, /* Transfer data to device.  */
37
    USB_MSDM_DATAIN, /* Transfer data from device.  */
38
    USB_MSDM_CSW /* Command Status.  */
39
};
40

    
41
struct usb_msd_csw {
42
    uint32_t sig;
43
    uint32_t tag;
44
    uint32_t residue;
45
    uint8_t status;
46
};
47

    
48
typedef struct {
49
    USBDevice dev;
50
    enum USBMSDMode mode;
51
    uint32_t scsi_off;
52
    uint32_t scsi_len;
53
    uint32_t data_len;
54
    struct usb_msd_csw csw;
55
    SCSIRequest *req;
56
    SCSIBus bus;
57
    BlockConf conf;
58
    char *serial;
59
    SCSIDevice *scsi_dev;
60
    uint32_t removable;
61
    /* For async completion.  */
62
    USBPacket *packet;
63
} MSDState;
64

    
65
struct usb_msd_cbw {
66
    uint32_t sig;
67
    uint32_t tag;
68
    uint32_t data_len;
69
    uint8_t flags;
70
    uint8_t lun;
71
    uint8_t cmd_len;
72
    uint8_t cmd[16];
73
};
74

    
75
enum {
76
    STR_MANUFACTURER = 1,
77
    STR_PRODUCT,
78
    STR_SERIALNUMBER,
79
    STR_CONFIG_FULL,
80
    STR_CONFIG_HIGH,
81
    STR_CONFIG_SUPER,
82
};
83

    
84
static const USBDescStrings desc_strings = {
85
    [STR_MANUFACTURER] = "QEMU",
86
    [STR_PRODUCT]      = "QEMU USB HARDDRIVE",
87
    [STR_SERIALNUMBER] = "1",
88
    [STR_CONFIG_FULL]  = "Full speed config (usb 1.1)",
89
    [STR_CONFIG_HIGH]  = "High speed config (usb 2.0)",
90
    [STR_CONFIG_SUPER] = "Super speed config (usb 3.0)",
91
};
92

    
93
static const USBDescIface desc_iface_full = {
94
    .bInterfaceNumber              = 0,
95
    .bNumEndpoints                 = 2,
96
    .bInterfaceClass               = USB_CLASS_MASS_STORAGE,
97
    .bInterfaceSubClass            = 0x06, /* SCSI */
98
    .bInterfaceProtocol            = 0x50, /* Bulk */
99
    .eps = (USBDescEndpoint[]) {
100
        {
101
            .bEndpointAddress      = USB_DIR_IN | 0x01,
102
            .bmAttributes          = USB_ENDPOINT_XFER_BULK,
103
            .wMaxPacketSize        = 64,
104
        },{
105
            .bEndpointAddress      = USB_DIR_OUT | 0x02,
106
            .bmAttributes          = USB_ENDPOINT_XFER_BULK,
107
            .wMaxPacketSize        = 64,
108
        },
109
    }
110
};
111

    
112
static const USBDescDevice desc_device_full = {
113
    .bcdUSB                        = 0x0200,
114
    .bMaxPacketSize0               = 8,
115
    .bNumConfigurations            = 1,
116
    .confs = (USBDescConfig[]) {
117
        {
118
            .bNumInterfaces        = 1,
119
            .bConfigurationValue   = 1,
120
            .iConfiguration        = STR_CONFIG_FULL,
121
            .bmAttributes          = 0xc0,
122
            .nif = 1,
123
            .ifs = &desc_iface_full,
124
        },
125
    },
126
};
127

    
128
static const USBDescIface desc_iface_high = {
129
    .bInterfaceNumber              = 0,
130
    .bNumEndpoints                 = 2,
131
    .bInterfaceClass               = USB_CLASS_MASS_STORAGE,
132
    .bInterfaceSubClass            = 0x06, /* SCSI */
133
    .bInterfaceProtocol            = 0x50, /* Bulk */
134
    .eps = (USBDescEndpoint[]) {
135
        {
136
            .bEndpointAddress      = USB_DIR_IN | 0x01,
137
            .bmAttributes          = USB_ENDPOINT_XFER_BULK,
138
            .wMaxPacketSize        = 512,
139
        },{
140
            .bEndpointAddress      = USB_DIR_OUT | 0x02,
141
            .bmAttributes          = USB_ENDPOINT_XFER_BULK,
142
            .wMaxPacketSize        = 512,
143
        },
144
    }
145
};
146

    
147
static const USBDescDevice desc_device_high = {
148
    .bcdUSB                        = 0x0200,
149
    .bMaxPacketSize0               = 64,
150
    .bNumConfigurations            = 1,
151
    .confs = (USBDescConfig[]) {
152
        {
153
            .bNumInterfaces        = 1,
154
            .bConfigurationValue   = 1,
155
            .iConfiguration        = STR_CONFIG_HIGH,
156
            .bmAttributes          = 0xc0,
157
            .nif = 1,
158
            .ifs = &desc_iface_high,
159
        },
160
    },
161
};
162

    
163
static const USBDescIface desc_iface_super = {
164
    .bInterfaceNumber              = 0,
165
    .bNumEndpoints                 = 2,
166
    .bInterfaceClass               = USB_CLASS_MASS_STORAGE,
167
    .bInterfaceSubClass            = 0x06, /* SCSI */
168
    .bInterfaceProtocol            = 0x50, /* Bulk */
169
    .eps = (USBDescEndpoint[]) {
170
        {
171
            .bEndpointAddress      = USB_DIR_IN | 0x01,
172
            .bmAttributes          = USB_ENDPOINT_XFER_BULK,
173
            .wMaxPacketSize        = 1024,
174
            .bMaxBurst             = 15,
175
        },{
176
            .bEndpointAddress      = USB_DIR_OUT | 0x02,
177
            .bmAttributes          = USB_ENDPOINT_XFER_BULK,
178
            .wMaxPacketSize        = 1024,
179
            .bMaxBurst             = 15,
180
        },
181
    }
182
};
183

    
184
static const USBDescDevice desc_device_super = {
185
    .bcdUSB                        = 0x0300,
186
    .bMaxPacketSize0               = 9,
187
    .bNumConfigurations            = 1,
188
    .confs = (USBDescConfig[]) {
189
        {
190
            .bNumInterfaces        = 1,
191
            .bConfigurationValue   = 1,
192
            .iConfiguration        = STR_CONFIG_SUPER,
193
            .bmAttributes          = 0xc0,
194
            .nif = 1,
195
            .ifs = &desc_iface_super,
196
        },
197
    },
198
};
199

    
200
static const USBDesc desc = {
201
    .id = {
202
        .idVendor          = 0x46f4, /* CRC16() of "QEMU" */
203
        .idProduct         = 0x0001,
204
        .bcdDevice         = 0,
205
        .iManufacturer     = STR_MANUFACTURER,
206
        .iProduct          = STR_PRODUCT,
207
        .iSerialNumber     = STR_SERIALNUMBER,
208
    },
209
    .full  = &desc_device_full,
210
    .high  = &desc_device_high,
211
    .super = &desc_device_super,
212
    .str   = desc_strings,
213
};
214

    
215
static void usb_msd_copy_data(MSDState *s, USBPacket *p)
216
{
217
    uint32_t len;
218
    len = p->iov.size - p->actual_length;
219
    if (len > s->scsi_len)
220
        len = s->scsi_len;
221
    usb_packet_copy(p, scsi_req_get_buf(s->req) + s->scsi_off, len);
222
    s->scsi_len -= len;
223
    s->scsi_off += len;
224
    s->data_len -= len;
225
    if (s->scsi_len == 0 || s->data_len == 0) {
226
        scsi_req_continue(s->req);
227
    }
228
}
229

    
230
static void usb_msd_send_status(MSDState *s, USBPacket *p)
231
{
232
    int len;
233

    
234
    DPRINTF("Command status %d tag 0x%x, len %zd\n",
235
            s->csw.status, le32_to_cpu(s->csw.tag), p->iov.size);
236

    
237
    assert(s->csw.sig == cpu_to_le32(0x53425355));
238
    len = MIN(sizeof(s->csw), p->iov.size);
239
    usb_packet_copy(p, &s->csw, len);
240
    memset(&s->csw, 0, sizeof(s->csw));
241
}
242

    
243
static void usb_msd_packet_complete(MSDState *s)
244
{
245
    USBPacket *p = s->packet;
246

    
247
    /* Set s->packet to NULL before calling usb_packet_complete
248
       because another request may be issued before
249
       usb_packet_complete returns.  */
250
    DPRINTF("Packet complete %p\n", p);
251
    s->packet = NULL;
252
    usb_packet_complete(&s->dev, p);
253
}
254

    
255
static void usb_msd_transfer_data(SCSIRequest *req, uint32_t len)
256
{
257
    MSDState *s = DO_UPCAST(MSDState, dev.qdev, req->bus->qbus.parent);
258
    USBPacket *p = s->packet;
259

    
260
    assert((s->mode == USB_MSDM_DATAOUT) == (req->cmd.mode == SCSI_XFER_TO_DEV));
261
    s->scsi_len = len;
262
    s->scsi_off = 0;
263
    if (p) {
264
        usb_msd_copy_data(s, p);
265
        p = s->packet;
266
        if (p && p->actual_length == p->iov.size) {
267
            p->status = USB_RET_SUCCESS; /* Clear previous ASYNC status */
268
            usb_msd_packet_complete(s);
269
        }
270
    }
271
}
272

    
273
static void usb_msd_command_complete(SCSIRequest *req, uint32_t status, size_t resid)
274
{
275
    MSDState *s = DO_UPCAST(MSDState, dev.qdev, req->bus->qbus.parent);
276
    USBPacket *p = s->packet;
277

    
278
    DPRINTF("Command complete %d tag 0x%x\n", status, req->tag);
279

    
280
    s->csw.sig = cpu_to_le32(0x53425355);
281
    s->csw.tag = cpu_to_le32(req->tag);
282
    s->csw.residue = cpu_to_le32(s->data_len);
283
    s->csw.status = status != 0;
284

    
285
    if (s->packet) {
286
        if (s->data_len == 0 && s->mode == USB_MSDM_DATAOUT) {
287
            /* A deferred packet with no write data remaining must be
288
               the status read packet.  */
289
            usb_msd_send_status(s, p);
290
            s->mode = USB_MSDM_CBW;
291
        } else if (s->mode == USB_MSDM_CSW) {
292
            usb_msd_send_status(s, p);
293
            s->mode = USB_MSDM_CBW;
294
        } else {
295
            if (s->data_len) {
296
                int len = (p->iov.size - p->actual_length);
297
                usb_packet_skip(p, len);
298
                s->data_len -= len;
299
            }
300
            if (s->data_len == 0) {
301
                s->mode = USB_MSDM_CSW;
302
            }
303
        }
304
        p->status = USB_RET_SUCCESS; /* Clear previous ASYNC status */
305
        usb_msd_packet_complete(s);
306
    } else if (s->data_len == 0) {
307
        s->mode = USB_MSDM_CSW;
308
    }
309
    scsi_req_unref(req);
310
    s->req = NULL;
311
}
312

    
313
static void usb_msd_request_cancelled(SCSIRequest *req)
314
{
315
    MSDState *s = DO_UPCAST(MSDState, dev.qdev, req->bus->qbus.parent);
316

    
317
    if (req == s->req) {
318
        scsi_req_unref(s->req);
319
        s->req = NULL;
320
        s->scsi_len = 0;
321
    }
322
}
323

    
324
static void usb_msd_handle_reset(USBDevice *dev)
325
{
326
    MSDState *s = (MSDState *)dev;
327

    
328
    DPRINTF("Reset\n");
329
    if (s->req) {
330
        scsi_req_cancel(s->req);
331
    }
332
    assert(s->req == NULL);
333

    
334
    if (s->packet) {
335
        s->packet->status = USB_RET_STALL;
336
        usb_msd_packet_complete(s);
337
    }
338

    
339
    s->mode = USB_MSDM_CBW;
340
}
341

    
342
static void usb_msd_handle_control(USBDevice *dev, USBPacket *p,
343
               int request, int value, int index, int length, uint8_t *data)
344
{
345
    MSDState *s = (MSDState *)dev;
346
    int ret;
347

    
348
    ret = usb_desc_handle_control(dev, p, request, value, index, length, data);
349
    if (ret >= 0) {
350
        return;
351
    }
352

    
353
    switch (request) {
354
    case EndpointOutRequest | USB_REQ_CLEAR_FEATURE:
355
        break;
356
        /* Class specific requests.  */
357
    case ClassInterfaceOutRequest | MassStorageReset:
358
        /* Reset state ready for the next CBW.  */
359
        s->mode = USB_MSDM_CBW;
360
        break;
361
    case ClassInterfaceRequest | GetMaxLun:
362
        data[0] = 0;
363
        p->actual_length = 1;
364
        break;
365
    default:
366
        p->status = USB_RET_STALL;
367
        break;
368
    }
369
}
370

    
371
static void usb_msd_cancel_io(USBDevice *dev, USBPacket *p)
372
{
373
    MSDState *s = DO_UPCAST(MSDState, dev, dev);
374

    
375
    assert(s->packet == p);
376
    s->packet = NULL;
377

    
378
    if (s->req) {
379
        scsi_req_cancel(s->req);
380
    }
381
}
382

    
383
static void usb_msd_handle_data(USBDevice *dev, USBPacket *p)
384
{
385
    MSDState *s = (MSDState *)dev;
386
    uint32_t tag;
387
    struct usb_msd_cbw cbw;
388
    uint8_t devep = p->ep->nr;
389

    
390
    switch (p->pid) {
391
    case USB_TOKEN_OUT:
392
        if (devep != 2)
393
            goto fail;
394

    
395
        switch (s->mode) {
396
        case USB_MSDM_CBW:
397
            if (p->iov.size != 31) {
398
                fprintf(stderr, "usb-msd: Bad CBW size");
399
                goto fail;
400
            }
401
            usb_packet_copy(p, &cbw, 31);
402
            if (le32_to_cpu(cbw.sig) != 0x43425355) {
403
                fprintf(stderr, "usb-msd: Bad signature %08x\n",
404
                        le32_to_cpu(cbw.sig));
405
                goto fail;
406
            }
407
            DPRINTF("Command on LUN %d\n", cbw.lun);
408
            if (cbw.lun != 0) {
409
                fprintf(stderr, "usb-msd: Bad LUN %d\n", cbw.lun);
410
                goto fail;
411
            }
412
            tag = le32_to_cpu(cbw.tag);
413
            s->data_len = le32_to_cpu(cbw.data_len);
414
            if (s->data_len == 0) {
415
                s->mode = USB_MSDM_CSW;
416
            } else if (cbw.flags & 0x80) {
417
                s->mode = USB_MSDM_DATAIN;
418
            } else {
419
                s->mode = USB_MSDM_DATAOUT;
420
            }
421
            DPRINTF("Command tag 0x%x flags %08x len %d data %d\n",
422
                    tag, cbw.flags, cbw.cmd_len, s->data_len);
423
            assert(le32_to_cpu(s->csw.residue) == 0);
424
            s->scsi_len = 0;
425
            s->req = scsi_req_new(s->scsi_dev, tag, 0, cbw.cmd, NULL);
426
#ifdef DEBUG_MSD
427
            scsi_req_print(s->req);
428
#endif
429
            scsi_req_enqueue(s->req);
430
            if (s->req && s->req->cmd.xfer != SCSI_XFER_NONE) {
431
                scsi_req_continue(s->req);
432
            }
433
            break;
434

    
435
        case USB_MSDM_DATAOUT:
436
            DPRINTF("Data out %zd/%d\n", p->iov.size, s->data_len);
437
            if (p->iov.size > s->data_len) {
438
                goto fail;
439
            }
440

    
441
            if (s->scsi_len) {
442
                usb_msd_copy_data(s, p);
443
            }
444
            if (le32_to_cpu(s->csw.residue)) {
445
                int len = p->iov.size - p->actual_length;
446
                if (len) {
447
                    usb_packet_skip(p, len);
448
                    s->data_len -= len;
449
                    if (s->data_len == 0) {
450
                        s->mode = USB_MSDM_CSW;
451
                    }
452
                }
453
            }
454
            if (p->actual_length < p->iov.size) {
455
                DPRINTF("Deferring packet %p [wait data-out]\n", p);
456
                s->packet = p;
457
                p->status = USB_RET_ASYNC;
458
            }
459
            break;
460

    
461
        default:
462
            DPRINTF("Unexpected write (len %zd)\n", p->iov.size);
463
            goto fail;
464
        }
465
        break;
466

    
467
    case USB_TOKEN_IN:
468
        if (devep != 1)
469
            goto fail;
470

    
471
        switch (s->mode) {
472
        case USB_MSDM_DATAOUT:
473
            if (s->data_len != 0 || p->iov.size < 13) {
474
                goto fail;
475
            }
476
            /* Waiting for SCSI write to complete.  */
477
            s->packet = p;
478
            p->status = USB_RET_ASYNC;
479
            break;
480

    
481
        case USB_MSDM_CSW:
482
            if (p->iov.size < 13) {
483
                goto fail;
484
            }
485

    
486
            if (s->req) {
487
                /* still in flight */
488
                DPRINTF("Deferring packet %p [wait status]\n", p);
489
                s->packet = p;
490
                p->status = USB_RET_ASYNC;
491
            } else {
492
                usb_msd_send_status(s, p);
493
                s->mode = USB_MSDM_CBW;
494
            }
495
            break;
496

    
497
        case USB_MSDM_DATAIN:
498
            DPRINTF("Data in %zd/%d, scsi_len %d\n",
499
                    p->iov.size, s->data_len, s->scsi_len);
500
            if (s->scsi_len) {
501
                usb_msd_copy_data(s, p);
502
            }
503
            if (le32_to_cpu(s->csw.residue)) {
504
                int len = p->iov.size - p->actual_length;
505
                if (len) {
506
                    usb_packet_skip(p, len);
507
                    s->data_len -= len;
508
                    if (s->data_len == 0) {
509
                        s->mode = USB_MSDM_CSW;
510
                    }
511
                }
512
            }
513
            if (p->actual_length < p->iov.size) {
514
                DPRINTF("Deferring packet %p [wait data-in]\n", p);
515
                s->packet = p;
516
                p->status = USB_RET_ASYNC;
517
            }
518
            break;
519

    
520
        default:
521
            DPRINTF("Unexpected read (len %zd)\n", p->iov.size);
522
            goto fail;
523
        }
524
        break;
525

    
526
    default:
527
        DPRINTF("Bad token\n");
528
    fail:
529
        p->status = USB_RET_STALL;
530
        break;
531
    }
532
}
533

    
534
static void usb_msd_password_cb(void *opaque, int err)
535
{
536
    MSDState *s = opaque;
537

    
538
    if (!err)
539
        err = usb_device_attach(&s->dev);
540

    
541
    if (err)
542
        qdev_unplug(&s->dev.qdev, NULL);
543
}
544

    
545
static void *usb_msd_load_request(QEMUFile *f, SCSIRequest *req)
546
{
547
    MSDState *s = DO_UPCAST(MSDState, dev.qdev, req->bus->qbus.parent);
548

    
549
    /* nothing to load, just store req in our state struct */
550
    assert(s->req == NULL);
551
    scsi_req_ref(req);
552
    s->req = req;
553
    return NULL;
554
}
555

    
556
static const struct SCSIBusInfo usb_msd_scsi_info = {
557
    .tcq = false,
558
    .max_target = 0,
559
    .max_lun = 0,
560

    
561
    .transfer_data = usb_msd_transfer_data,
562
    .complete = usb_msd_command_complete,
563
    .cancel = usb_msd_request_cancelled,
564
    .load_request = usb_msd_load_request,
565
};
566

    
567
static int usb_msd_initfn(USBDevice *dev)
568
{
569
    MSDState *s = DO_UPCAST(MSDState, dev, dev);
570
    BlockDriverState *bs = s->conf.bs;
571

    
572
    if (!bs) {
573
        error_report("drive property not set");
574
        return -1;
575
    }
576

    
577
    blkconf_serial(&s->conf, &s->serial);
578

    
579
    /*
580
     * Hack alert: this pretends to be a block device, but it's really
581
     * a SCSI bus that can serve only a single device, which it
582
     * creates automatically.  But first it needs to detach from its
583
     * blockdev, or else scsi_bus_legacy_add_drive() dies when it
584
     * attaches again.
585
     *
586
     * The hack is probably a bad idea.
587
     */
588
    bdrv_detach_dev(bs, &s->dev.qdev);
589
    s->conf.bs = NULL;
590

    
591
    if (s->serial) {
592
        usb_desc_set_string(dev, STR_SERIALNUMBER, s->serial);
593
    } else {
594
        usb_desc_create_serial(dev);
595
    }
596

    
597
    usb_desc_init(dev);
598
    scsi_bus_new(&s->bus, &s->dev.qdev, &usb_msd_scsi_info);
599
    s->scsi_dev = scsi_bus_legacy_add_drive(&s->bus, bs, 0, !!s->removable,
600
                                            s->conf.bootindex);
601
    if (!s->scsi_dev) {
602
        return -1;
603
    }
604
    s->bus.qbus.allow_hotplug = 0;
605
    usb_msd_handle_reset(dev);
606

    
607
    if (bdrv_key_required(bs)) {
608
        if (cur_mon) {
609
            monitor_read_bdrv_key_start(cur_mon, bs, usb_msd_password_cb, s);
610
            s->dev.auto_attach = 0;
611
        } else {
612
            autostart = 0;
613
        }
614
    }
615

    
616
    return 0;
617
}
618

    
619
static USBDevice *usb_msd_init(USBBus *bus, const char *filename)
620
{
621
    static int nr=0;
622
    char id[8];
623
    QemuOpts *opts;
624
    DriveInfo *dinfo;
625
    USBDevice *dev;
626
    const char *p1;
627
    char fmt[32];
628

    
629
    /* parse -usbdevice disk: syntax into drive opts */
630
    snprintf(id, sizeof(id), "usb%d", nr++);
631
    opts = qemu_opts_create(qemu_find_opts("drive"), id, 0, NULL);
632

    
633
    p1 = strchr(filename, ':');
634
    if (p1++) {
635
        const char *p2;
636

    
637
        if (strstart(filename, "format=", &p2)) {
638
            int len = MIN(p1 - p2, sizeof(fmt));
639
            pstrcpy(fmt, len, p2);
640
            qemu_opt_set(opts, "format", fmt);
641
        } else if (*filename != ':') {
642
            printf("unrecognized USB mass-storage option %s\n", filename);
643
            return NULL;
644
        }
645
        filename = p1;
646
    }
647
    if (!*filename) {
648
        printf("block device specification needed\n");
649
        return NULL;
650
    }
651
    qemu_opt_set(opts, "file", filename);
652
    qemu_opt_set(opts, "if", "none");
653

    
654
    /* create host drive */
655
    dinfo = drive_init(opts, 0);
656
    if (!dinfo) {
657
        qemu_opts_del(opts);
658
        return NULL;
659
    }
660

    
661
    /* create guest device */
662
    dev = usb_create(bus, "usb-storage");
663
    if (!dev) {
664
        return NULL;
665
    }
666
    if (qdev_prop_set_drive(&dev->qdev, "drive", dinfo->bdrv) < 0) {
667
        qdev_free(&dev->qdev);
668
        return NULL;
669
    }
670
    if (qdev_init(&dev->qdev) < 0)
671
        return NULL;
672

    
673
    return dev;
674
}
675

    
676
static const VMStateDescription vmstate_usb_msd = {
677
    .name = "usb-storage",
678
    .version_id = 1,
679
    .minimum_version_id = 1,
680
    .fields = (VMStateField []) {
681
        VMSTATE_USB_DEVICE(dev, MSDState),
682
        VMSTATE_UINT32(mode, MSDState),
683
        VMSTATE_UINT32(scsi_len, MSDState),
684
        VMSTATE_UINT32(scsi_off, MSDState),
685
        VMSTATE_UINT32(data_len, MSDState),
686
        VMSTATE_UINT32(csw.sig, MSDState),
687
        VMSTATE_UINT32(csw.tag, MSDState),
688
        VMSTATE_UINT32(csw.residue, MSDState),
689
        VMSTATE_UINT8(csw.status, MSDState),
690
        VMSTATE_END_OF_LIST()
691
    }
692
};
693

    
694
static Property msd_properties[] = {
695
    DEFINE_BLOCK_PROPERTIES(MSDState, conf),
696
    DEFINE_PROP_STRING("serial", MSDState, serial),
697
    DEFINE_PROP_BIT("removable", MSDState, removable, 0, false),
698
    DEFINE_PROP_END_OF_LIST(),
699
};
700

    
701
static void usb_msd_class_initfn(ObjectClass *klass, void *data)
702
{
703
    DeviceClass *dc = DEVICE_CLASS(klass);
704
    USBDeviceClass *uc = USB_DEVICE_CLASS(klass);
705

    
706
    uc->init           = usb_msd_initfn;
707
    uc->product_desc   = "QEMU USB MSD";
708
    uc->usb_desc       = &desc;
709
    uc->cancel_packet  = usb_msd_cancel_io;
710
    uc->handle_attach  = usb_desc_attach;
711
    uc->handle_reset   = usb_msd_handle_reset;
712
    uc->handle_control = usb_msd_handle_control;
713
    uc->handle_data    = usb_msd_handle_data;
714
    dc->fw_name = "storage";
715
    dc->vmsd = &vmstate_usb_msd;
716
    dc->props = msd_properties;
717
}
718

    
719
static TypeInfo msd_info = {
720
    .name          = "usb-storage",
721
    .parent        = TYPE_USB_DEVICE,
722
    .instance_size = sizeof(MSDState),
723
    .class_init    = usb_msd_class_initfn,
724
};
725

    
726
static void usb_msd_register_types(void)
727
{
728
    type_register_static(&msd_info);
729
    usb_legacy_register("usb-storage", "disk", usb_msd_init);
730
}
731

    
732
type_init(usb_msd_register_types)