]>
Commit | Line | Data |
---|---|---|
5fafdf24 | 1 | /* |
2e5d83bb PB |
2 | * USB Mass Storage Device emulation |
3 | * | |
4 | * Copyright (c) 2006 CodeSourcery. | |
5 | * Written by Paul Brook | |
6 | * | |
8e31bf38 | 7 | * This code is licensed under the LGPL. |
2e5d83bb PB |
8 | */ |
9 | ||
87ecb68b | 10 | #include "qemu-common.h" |
7fc2f2c0 GH |
11 | #include "qemu-option.h" |
12 | #include "qemu-config.h" | |
87ecb68b | 13 | #include "usb.h" |
81bfd2f2 | 14 | #include "usb-desc.h" |
43b443b6 | 15 | #include "scsi.h" |
c0f4ce77 | 16 | #include "console.h" |
b3e461d3 | 17 | #include "monitor.h" |
666daa68 | 18 | #include "sysemu.h" |
2446333c | 19 | #include "blockdev.h" |
2e5d83bb PB |
20 | |
21 | //#define DEBUG_MSD | |
22 | ||
23 | #ifdef DEBUG_MSD | |
001faf32 BS |
24 | #define DPRINTF(fmt, ...) \ |
25 | do { printf("usb-msd: " fmt , ## __VA_ARGS__); } while (0) | |
2e5d83bb | 26 | #else |
001faf32 | 27 | #define DPRINTF(fmt, ...) do {} while(0) |
2e5d83bb PB |
28 | #endif |
29 | ||
30 | /* USB requests. */ | |
31 | #define MassStorageReset 0xff | |
32 | #define GetMaxLun 0xfe | |
33 | ||
34 | enum USBMSDMode { | |
35 | USB_MSDM_CBW, /* Command Block. */ | |
94843f66 | 36 | USB_MSDM_DATAOUT, /* Transfer data to device. */ |
2e5d83bb PB |
37 | USB_MSDM_DATAIN, /* Transfer data from device. */ |
38 | USB_MSDM_CSW /* Command Status. */ | |
39 | }; | |
40 | ||
92a114f6 GH |
41 | struct usb_msd_csw { |
42 | uint32_t sig; | |
43 | uint32_t tag; | |
44 | uint32_t residue; | |
45 | uint8_t status; | |
46 | }; | |
47 | ||
2e5d83bb PB |
48 | typedef struct { |
49 | USBDevice dev; | |
50 | enum USBMSDMode mode; | |
a917d384 PB |
51 | uint32_t scsi_len; |
52 | uint8_t *scsi_buf; | |
2e5d83bb | 53 | uint32_t data_len; |
a917d384 | 54 | uint32_t residue; |
92a114f6 | 55 | struct usb_msd_csw csw; |
5c6c0e51 | 56 | SCSIRequest *req; |
ca9c39fa | 57 | SCSIBus bus; |
428c149b | 58 | BlockConf conf; |
c3a90cb1 | 59 | char *serial; |
2e5d83bb | 60 | SCSIDevice *scsi_dev; |
6bb7b867 | 61 | uint32_t removable; |
4d611c9a PB |
62 | /* For async completion. */ |
63 | USBPacket *packet; | |
2e5d83bb PB |
64 | } MSDState; |
65 | ||
a917d384 PB |
66 | struct usb_msd_cbw { |
67 | uint32_t sig; | |
68 | uint32_t tag; | |
69 | uint32_t data_len; | |
70 | uint8_t flags; | |
71 | uint8_t lun; | |
72 | uint8_t cmd_len; | |
73 | uint8_t cmd[16]; | |
74 | }; | |
75 | ||
81bfd2f2 GH |
76 | enum { |
77 | STR_MANUFACTURER = 1, | |
78 | STR_PRODUCT, | |
79 | STR_SERIALNUMBER, | |
ca0c730d GH |
80 | STR_CONFIG_FULL, |
81 | STR_CONFIG_HIGH, | |
2e5d83bb PB |
82 | }; |
83 | ||
81bfd2f2 GH |
84 | static const USBDescStrings desc_strings = { |
85 | [STR_MANUFACTURER] = "QEMU " QEMU_VERSION, | |
86 | [STR_PRODUCT] = "QEMU USB HARDDRIVE", | |
87 | [STR_SERIALNUMBER] = "1", | |
ca0c730d GH |
88 | [STR_CONFIG_FULL] = "Full speed config (usb 1.1)", |
89 | [STR_CONFIG_HIGH] = "High speed config (usb 2.0)", | |
81bfd2f2 GH |
90 | }; |
91 | ||
ca0c730d | 92 | static const USBDescIface desc_iface_full = { |
81bfd2f2 GH |
93 | .bInterfaceNumber = 0, |
94 | .bNumEndpoints = 2, | |
95 | .bInterfaceClass = USB_CLASS_MASS_STORAGE, | |
96 | .bInterfaceSubClass = 0x06, /* SCSI */ | |
97 | .bInterfaceProtocol = 0x50, /* Bulk */ | |
98 | .eps = (USBDescEndpoint[]) { | |
99 | { | |
100 | .bEndpointAddress = USB_DIR_IN | 0x01, | |
101 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | |
102 | .wMaxPacketSize = 64, | |
103 | },{ | |
104 | .bEndpointAddress = USB_DIR_OUT | 0x02, | |
105 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | |
106 | .wMaxPacketSize = 64, | |
107 | }, | |
108 | } | |
109 | }; | |
110 | ||
ca0c730d GH |
111 | static const USBDescDevice desc_device_full = { |
112 | .bcdUSB = 0x0200, | |
81bfd2f2 GH |
113 | .bMaxPacketSize0 = 8, |
114 | .bNumConfigurations = 1, | |
115 | .confs = (USBDescConfig[]) { | |
116 | { | |
117 | .bNumInterfaces = 1, | |
118 | .bConfigurationValue = 1, | |
ca0c730d | 119 | .iConfiguration = STR_CONFIG_FULL, |
81bfd2f2 | 120 | .bmAttributes = 0xc0, |
add75088 | 121 | .nif = 1, |
ca0c730d GH |
122 | .ifs = &desc_iface_full, |
123 | }, | |
124 | }, | |
125 | }; | |
126 | ||
127 | static const USBDescIface desc_iface_high = { | |
128 | .bInterfaceNumber = 0, | |
129 | .bNumEndpoints = 2, | |
130 | .bInterfaceClass = USB_CLASS_MASS_STORAGE, | |
131 | .bInterfaceSubClass = 0x06, /* SCSI */ | |
132 | .bInterfaceProtocol = 0x50, /* Bulk */ | |
133 | .eps = (USBDescEndpoint[]) { | |
134 | { | |
135 | .bEndpointAddress = USB_DIR_IN | 0x01, | |
136 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | |
137 | .wMaxPacketSize = 512, | |
138 | },{ | |
139 | .bEndpointAddress = USB_DIR_OUT | 0x02, | |
140 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | |
141 | .wMaxPacketSize = 512, | |
142 | }, | |
143 | } | |
144 | }; | |
145 | ||
146 | static const USBDescDevice desc_device_high = { | |
147 | .bcdUSB = 0x0200, | |
148 | .bMaxPacketSize0 = 64, | |
149 | .bNumConfigurations = 1, | |
150 | .confs = (USBDescConfig[]) { | |
151 | { | |
152 | .bNumInterfaces = 1, | |
153 | .bConfigurationValue = 1, | |
154 | .iConfiguration = STR_CONFIG_HIGH, | |
155 | .bmAttributes = 0xc0, | |
add75088 | 156 | .nif = 1, |
ca0c730d | 157 | .ifs = &desc_iface_high, |
81bfd2f2 GH |
158 | }, |
159 | }, | |
160 | }; | |
161 | ||
162 | static const USBDesc desc = { | |
163 | .id = { | |
db80358a RT |
164 | .idVendor = 0x46f4, /* CRC16() of "QEMU" */ |
165 | .idProduct = 0x0001, | |
81bfd2f2 GH |
166 | .bcdDevice = 0, |
167 | .iManufacturer = STR_MANUFACTURER, | |
168 | .iProduct = STR_PRODUCT, | |
169 | .iSerialNumber = STR_SERIALNUMBER, | |
170 | }, | |
ca0c730d GH |
171 | .full = &desc_device_full, |
172 | .high = &desc_device_high, | |
81bfd2f2 | 173 | .str = desc_strings, |
2e5d83bb PB |
174 | }; |
175 | ||
29c74f76 | 176 | static void usb_msd_copy_data(MSDState *s, USBPacket *p) |
a917d384 PB |
177 | { |
178 | uint32_t len; | |
29c74f76 | 179 | len = p->iov.size - p->result; |
a917d384 PB |
180 | if (len > s->scsi_len) |
181 | len = s->scsi_len; | |
29c74f76 | 182 | usb_packet_copy(p, s->scsi_buf, len); |
a917d384 | 183 | s->scsi_len -= len; |
a917d384 PB |
184 | s->scsi_buf += len; |
185 | s->data_len -= len; | |
fa7935c1 | 186 | if (s->scsi_len == 0 || s->data_len == 0) { |
ad3376cc | 187 | scsi_req_continue(s->req); |
a917d384 PB |
188 | } |
189 | } | |
190 | ||
ab4797ad | 191 | static void usb_msd_send_status(MSDState *s, USBPacket *p) |
a917d384 | 192 | { |
ab4797ad | 193 | int len; |
a917d384 | 194 | |
e04da7c3 | 195 | DPRINTF("Command status %d tag 0x%x, len %zd\n", |
92a114f6 GH |
196 | s->csw.status, s->csw.tag, p->iov.size); |
197 | ||
198 | assert(s->csw.sig == 0x53425355); | |
199 | len = MIN(sizeof(s->csw), p->iov.size); | |
200 | usb_packet_copy(p, &s->csw, len); | |
201 | memset(&s->csw, 0, sizeof(s->csw)); | |
a917d384 PB |
202 | } |
203 | ||
aba1f023 | 204 | static void usb_msd_transfer_data(SCSIRequest *req, uint32_t len) |
2e5d83bb | 205 | { |
5c6c0e51 | 206 | MSDState *s = DO_UPCAST(MSDState, dev.qdev, req->bus->qbus.parent); |
a917d384 | 207 | USBPacket *p = s->packet; |
4d611c9a | 208 | |
ad3376cc | 209 | assert((s->mode == USB_MSDM_DATAOUT) == (req->cmd.mode == SCSI_XFER_TO_DEV)); |
aba1f023 | 210 | s->scsi_len = len; |
0c34459b | 211 | s->scsi_buf = scsi_req_get_buf(req); |
a917d384 | 212 | if (p) { |
29c74f76 GH |
213 | usb_msd_copy_data(s, p); |
214 | p = s->packet; | |
215 | if (p && p->result == p->iov.size) { | |
a917d384 | 216 | /* Set s->packet to NULL before calling usb_packet_complete |
94843f66 | 217 | because another request may be issued before |
a917d384 PB |
218 | usb_packet_complete returns. */ |
219 | DPRINTF("Packet complete %p\n", p); | |
220 | s->packet = NULL; | |
13a9a0d3 | 221 | usb_packet_complete(&s->dev, p); |
a917d384 | 222 | } |
4d611c9a | 223 | } |
2e5d83bb PB |
224 | } |
225 | ||
aba1f023 | 226 | static void usb_msd_command_complete(SCSIRequest *req, uint32_t status) |
c6df7102 PB |
227 | { |
228 | MSDState *s = DO_UPCAST(MSDState, dev.qdev, req->bus->qbus.parent); | |
229 | USBPacket *p = s->packet; | |
230 | ||
7b863f41 | 231 | DPRINTF("Command complete %d tag 0x%x\n", status, req->tag); |
c6df7102 | 232 | s->residue = s->data_len; |
92a114f6 GH |
233 | |
234 | s->csw.sig = cpu_to_le32(0x53425355); | |
7b863f41 | 235 | s->csw.tag = cpu_to_le32(req->tag); |
92a114f6 | 236 | s->csw.residue = s->residue; |
414c4604 | 237 | s->csw.status = status != 0; |
92a114f6 | 238 | |
c6df7102 PB |
239 | if (s->packet) { |
240 | if (s->data_len == 0 && s->mode == USB_MSDM_DATAOUT) { | |
241 | /* A deferred packet with no write data remaining must be | |
242 | the status read packet. */ | |
243 | usb_msd_send_status(s, p); | |
244 | s->mode = USB_MSDM_CBW; | |
245 | } else { | |
246 | if (s->data_len) { | |
29c74f76 GH |
247 | int len = (p->iov.size - p->result); |
248 | usb_packet_skip(p, len); | |
249 | s->data_len -= len; | |
c6df7102 PB |
250 | } |
251 | if (s->data_len == 0) { | |
252 | s->mode = USB_MSDM_CSW; | |
253 | } | |
254 | } | |
255 | s->packet = NULL; | |
256 | usb_packet_complete(&s->dev, p); | |
257 | } else if (s->data_len == 0) { | |
258 | s->mode = USB_MSDM_CSW; | |
259 | } | |
260 | scsi_req_unref(req); | |
261 | s->req = NULL; | |
262 | } | |
263 | ||
94d3f98a PB |
264 | static void usb_msd_request_cancelled(SCSIRequest *req) |
265 | { | |
266 | MSDState *s = DO_UPCAST(MSDState, dev.qdev, req->bus->qbus.parent); | |
267 | ||
268 | if (req == s->req) { | |
269 | scsi_req_unref(s->req); | |
270 | s->req = NULL; | |
271 | s->packet = NULL; | |
272 | s->scsi_len = 0; | |
273 | } | |
274 | } | |
275 | ||
059809e4 | 276 | static void usb_msd_handle_reset(USBDevice *dev) |
2e5d83bb PB |
277 | { |
278 | MSDState *s = (MSDState *)dev; | |
279 | ||
280 | DPRINTF("Reset\n"); | |
281 | s->mode = USB_MSDM_CBW; | |
2e5d83bb PB |
282 | } |
283 | ||
007fd62f HG |
284 | static int usb_msd_handle_control(USBDevice *dev, USBPacket *p, |
285 | int request, int value, int index, int length, uint8_t *data) | |
2e5d83bb PB |
286 | { |
287 | MSDState *s = (MSDState *)dev; | |
81bfd2f2 | 288 | int ret; |
2e5d83bb | 289 | |
007fd62f | 290 | ret = usb_desc_handle_control(dev, p, request, value, index, length, data); |
81bfd2f2 GH |
291 | if (ret >= 0) { |
292 | return ret; | |
293 | } | |
294 | ||
295 | ret = 0; | |
2e5d83bb | 296 | switch (request) { |
2e5d83bb PB |
297 | case DeviceRequest | USB_REQ_GET_INTERFACE: |
298 | data[0] = 0; | |
299 | ret = 1; | |
300 | break; | |
301 | case DeviceOutRequest | USB_REQ_SET_INTERFACE: | |
302 | ret = 0; | |
303 | break; | |
304 | case EndpointOutRequest | USB_REQ_CLEAR_FEATURE: | |
e5322f76 APR |
305 | ret = 0; |
306 | break; | |
307 | case InterfaceOutRequest | USB_REQ_SET_INTERFACE: | |
2e5d83bb PB |
308 | ret = 0; |
309 | break; | |
310 | /* Class specific requests. */ | |
f3571b1a | 311 | case ClassInterfaceOutRequest | MassStorageReset: |
2e5d83bb PB |
312 | /* Reset state ready for the next CBW. */ |
313 | s->mode = USB_MSDM_CBW; | |
314 | ret = 0; | |
315 | break; | |
f3571b1a | 316 | case ClassInterfaceRequest | GetMaxLun: |
2e5d83bb PB |
317 | data[0] = 0; |
318 | ret = 1; | |
319 | break; | |
320 | default: | |
2e5d83bb PB |
321 | ret = USB_RET_STALL; |
322 | break; | |
323 | } | |
324 | return ret; | |
325 | } | |
326 | ||
eb5e680a | 327 | static void usb_msd_cancel_io(USBDevice *dev, USBPacket *p) |
4d611c9a | 328 | { |
eb5e680a | 329 | MSDState *s = DO_UPCAST(MSDState, dev, dev); |
d3ac1a87 GH |
330 | |
331 | if (s->req) { | |
332 | scsi_req_cancel(s->req); | |
333 | } | |
4d611c9a PB |
334 | } |
335 | ||
336 | static int usb_msd_handle_data(USBDevice *dev, USBPacket *p) | |
2e5d83bb PB |
337 | { |
338 | MSDState *s = (MSDState *)dev; | |
7b863f41 | 339 | uint32_t tag; |
2e5d83bb PB |
340 | int ret = 0; |
341 | struct usb_msd_cbw cbw; | |
4d611c9a | 342 | uint8_t devep = p->devep; |
2e5d83bb | 343 | |
4d611c9a | 344 | switch (p->pid) { |
2e5d83bb PB |
345 | case USB_TOKEN_OUT: |
346 | if (devep != 2) | |
347 | goto fail; | |
348 | ||
349 | switch (s->mode) { | |
350 | case USB_MSDM_CBW: | |
29c74f76 | 351 | if (p->iov.size != 31) { |
2e5d83bb PB |
352 | fprintf(stderr, "usb-msd: Bad CBW size"); |
353 | goto fail; | |
354 | } | |
29c74f76 | 355 | usb_packet_copy(p, &cbw, 31); |
2e5d83bb PB |
356 | if (le32_to_cpu(cbw.sig) != 0x43425355) { |
357 | fprintf(stderr, "usb-msd: Bad signature %08x\n", | |
358 | le32_to_cpu(cbw.sig)); | |
359 | goto fail; | |
360 | } | |
361 | DPRINTF("Command on LUN %d\n", cbw.lun); | |
362 | if (cbw.lun != 0) { | |
363 | fprintf(stderr, "usb-msd: Bad LUN %d\n", cbw.lun); | |
364 | goto fail; | |
365 | } | |
7b863f41 | 366 | tag = le32_to_cpu(cbw.tag); |
2e5d83bb PB |
367 | s->data_len = le32_to_cpu(cbw.data_len); |
368 | if (s->data_len == 0) { | |
369 | s->mode = USB_MSDM_CSW; | |
370 | } else if (cbw.flags & 0x80) { | |
371 | s->mode = USB_MSDM_DATAIN; | |
372 | } else { | |
373 | s->mode = USB_MSDM_DATAOUT; | |
374 | } | |
375 | DPRINTF("Command tag 0x%x flags %08x len %d data %d\n", | |
7b863f41 | 376 | tag, cbw.flags, cbw.cmd_len, s->data_len); |
a917d384 | 377 | s->residue = 0; |
ef0bdf77 | 378 | s->scsi_len = 0; |
7b863f41 | 379 | s->req = scsi_req_new(s->scsi_dev, tag, 0, cbw.cmd, NULL); |
c39ce112 | 380 | scsi_req_enqueue(s->req); |
59310659 | 381 | if (s->req && s->req->cmd.xfer != SCSI_XFER_NONE) { |
ad3376cc | 382 | scsi_req_continue(s->req); |
a917d384 | 383 | } |
29c74f76 | 384 | ret = p->result; |
2e5d83bb PB |
385 | break; |
386 | ||
387 | case USB_MSDM_DATAOUT: | |
29c74f76 GH |
388 | DPRINTF("Data out %zd/%d\n", p->iov.size, s->data_len); |
389 | if (p->iov.size > s->data_len) { | |
2e5d83bb | 390 | goto fail; |
29c74f76 | 391 | } |
2e5d83bb | 392 | |
a917d384 | 393 | if (s->scsi_len) { |
29c74f76 | 394 | usb_msd_copy_data(s, p); |
a917d384 | 395 | } |
29c74f76 GH |
396 | if (s->residue) { |
397 | int len = p->iov.size - p->result; | |
398 | if (len) { | |
399 | usb_packet_skip(p, len); | |
400 | s->data_len -= len; | |
401 | if (s->data_len == 0) { | |
402 | s->mode = USB_MSDM_CSW; | |
403 | } | |
404 | } | |
a917d384 | 405 | } |
29c74f76 | 406 | if (p->result < p->iov.size) { |
4d611c9a | 407 | DPRINTF("Deferring packet %p\n", p); |
4d611c9a PB |
408 | s->packet = p; |
409 | ret = USB_RET_ASYNC; | |
a917d384 | 410 | } else { |
29c74f76 | 411 | ret = p->result; |
4d611c9a | 412 | } |
2e5d83bb PB |
413 | break; |
414 | ||
415 | default: | |
29c74f76 | 416 | DPRINTF("Unexpected write (len %zd)\n", p->iov.size); |
2e5d83bb PB |
417 | goto fail; |
418 | } | |
419 | break; | |
420 | ||
421 | case USB_TOKEN_IN: | |
422 | if (devep != 1) | |
423 | goto fail; | |
424 | ||
425 | switch (s->mode) { | |
a917d384 | 426 | case USB_MSDM_DATAOUT: |
29c74f76 | 427 | if (s->data_len != 0 || p->iov.size < 13) { |
a917d384 | 428 | goto fail; |
29c74f76 | 429 | } |
a917d384 | 430 | /* Waiting for SCSI write to complete. */ |
a917d384 PB |
431 | s->packet = p; |
432 | ret = USB_RET_ASYNC; | |
433 | break; | |
434 | ||
2e5d83bb | 435 | case USB_MSDM_CSW: |
29c74f76 | 436 | if (p->iov.size < 13) { |
2e5d83bb | 437 | goto fail; |
29c74f76 | 438 | } |
2e5d83bb | 439 | |
59310659 GH |
440 | if (s->req) { |
441 | /* still in flight */ | |
442 | s->packet = p; | |
443 | ret = USB_RET_ASYNC; | |
444 | } else { | |
445 | usb_msd_send_status(s, p); | |
446 | s->mode = USB_MSDM_CBW; | |
447 | ret = 13; | |
448 | } | |
2e5d83bb PB |
449 | break; |
450 | ||
451 | case USB_MSDM_DATAIN: | |
29c74f76 GH |
452 | DPRINTF("Data in %zd/%d, scsi_len %d\n", |
453 | p->iov.size, s->data_len, s->scsi_len); | |
a917d384 | 454 | if (s->scsi_len) { |
29c74f76 | 455 | usb_msd_copy_data(s, p); |
a917d384 | 456 | } |
29c74f76 GH |
457 | if (s->residue) { |
458 | int len = p->iov.size - p->result; | |
459 | if (len) { | |
460 | usb_packet_skip(p, len); | |
461 | s->data_len -= len; | |
462 | if (s->data_len == 0) { | |
463 | s->mode = USB_MSDM_CSW; | |
464 | } | |
465 | } | |
a917d384 | 466 | } |
29c74f76 | 467 | if (p->result < p->iov.size) { |
4d611c9a | 468 | DPRINTF("Deferring packet %p\n", p); |
4d611c9a PB |
469 | s->packet = p; |
470 | ret = USB_RET_ASYNC; | |
a917d384 | 471 | } else { |
29c74f76 | 472 | ret = p->result; |
4d611c9a | 473 | } |
2e5d83bb PB |
474 | break; |
475 | ||
476 | default: | |
29c74f76 | 477 | DPRINTF("Unexpected read (len %zd)\n", p->iov.size); |
2e5d83bb PB |
478 | goto fail; |
479 | } | |
480 | break; | |
481 | ||
482 | default: | |
483 | DPRINTF("Bad token\n"); | |
484 | fail: | |
485 | ret = USB_RET_STALL; | |
486 | break; | |
487 | } | |
488 | ||
489 | return ret; | |
490 | } | |
491 | ||
b3e461d3 GH |
492 | static void usb_msd_password_cb(void *opaque, int err) |
493 | { | |
494 | MSDState *s = opaque; | |
495 | ||
496 | if (!err) | |
fa19bf83 HG |
497 | err = usb_device_attach(&s->dev); |
498 | ||
499 | if (err) | |
b3e461d3 GH |
500 | qdev_unplug(&s->dev.qdev); |
501 | } | |
502 | ||
afd4030c PB |
503 | static const struct SCSIBusInfo usb_msd_scsi_info = { |
504 | .tcq = false, | |
7e0380b9 PB |
505 | .max_target = 0, |
506 | .max_lun = 0, | |
afd4030c | 507 | |
c6df7102 | 508 | .transfer_data = usb_msd_transfer_data, |
94d3f98a PB |
509 | .complete = usb_msd_command_complete, |
510 | .cancel = usb_msd_request_cancelled | |
cfdc1bb0 PB |
511 | }; |
512 | ||
806b6024 GH |
513 | static int usb_msd_initfn(USBDevice *dev) |
514 | { | |
515 | MSDState *s = DO_UPCAST(MSDState, dev, dev); | |
f8b6cc00 | 516 | BlockDriverState *bs = s->conf.bs; |
4a1e1bc4 | 517 | DriveInfo *dinfo; |
806b6024 | 518 | |
f8b6cc00 | 519 | if (!bs) { |
1ecda02b | 520 | error_report("usb-msd: drive property not set"); |
7fc2f2c0 GH |
521 | return -1; |
522 | } | |
523 | ||
14bafc54 MA |
524 | /* |
525 | * Hack alert: this pretends to be a block device, but it's really | |
526 | * a SCSI bus that can serve only a single device, which it | |
18846dee MA |
527 | * creates automatically. But first it needs to detach from its |
528 | * blockdev, or else scsi_bus_legacy_add_drive() dies when it | |
529 | * attaches again. | |
14bafc54 MA |
530 | * |
531 | * The hack is probably a bad idea. | |
532 | */ | |
fa879d62 | 533 | bdrv_detach_dev(bs, &s->dev.qdev); |
f8b6cc00 | 534 | s->conf.bs = NULL; |
14bafc54 | 535 | |
c3a90cb1 MA |
536 | if (!s->serial) { |
537 | /* try to fall back to value set with legacy -drive serial=... */ | |
538 | dinfo = drive_get_by_blockdev(bs); | |
539 | if (*dinfo->serial) { | |
540 | s->serial = strdup(dinfo->serial); | |
541 | } | |
542 | } | |
543 | if (s->serial) { | |
544 | usb_desc_set_string(dev, STR_SERIALNUMBER, s->serial); | |
4a1e1bc4 GH |
545 | } |
546 | ||
a980a065 | 547 | usb_desc_init(dev); |
afd4030c | 548 | scsi_bus_new(&s->bus, &s->dev.qdev, &usb_msd_scsi_info); |
6bb7b867 | 549 | s->scsi_dev = scsi_bus_legacy_add_drive(&s->bus, bs, 0, !!s->removable); |
fa66b909 MA |
550 | if (!s->scsi_dev) { |
551 | return -1; | |
552 | } | |
cb23117b | 553 | s->bus.qbus.allow_hotplug = 0; |
7fc2f2c0 | 554 | usb_msd_handle_reset(dev); |
b3e461d3 | 555 | |
f8b6cc00 | 556 | if (bdrv_key_required(bs)) { |
a4426488 | 557 | if (cur_mon) { |
f8b6cc00 | 558 | monitor_read_bdrv_key_start(cur_mon, bs, usb_msd_password_cb, s); |
b3e461d3 GH |
559 | s->dev.auto_attach = 0; |
560 | } else { | |
561 | autostart = 0; | |
562 | } | |
563 | } | |
564 | ||
cf8ce30d | 565 | add_boot_device_path(s->conf.bootindex, &dev->qdev, "/disk@0,0"); |
806b6024 GH |
566 | return 0; |
567 | } | |
568 | ||
b3e461d3 | 569 | static USBDevice *usb_msd_init(const char *filename) |
2e5d83bb | 570 | { |
7fc2f2c0 GH |
571 | static int nr=0; |
572 | char id[8]; | |
573 | QemuOpts *opts; | |
574 | DriveInfo *dinfo; | |
806b6024 | 575 | USBDevice *dev; |
334c0241 AJ |
576 | const char *p1; |
577 | char fmt[32]; | |
578 | ||
7fc2f2c0 GH |
579 | /* parse -usbdevice disk: syntax into drive opts */ |
580 | snprintf(id, sizeof(id), "usb%d", nr++); | |
3329f07b | 581 | opts = qemu_opts_create(qemu_find_opts("drive"), id, 0); |
7fc2f2c0 | 582 | |
334c0241 AJ |
583 | p1 = strchr(filename, ':'); |
584 | if (p1++) { | |
585 | const char *p2; | |
586 | ||
587 | if (strstart(filename, "format=", &p2)) { | |
588 | int len = MIN(p1 - p2, sizeof(fmt)); | |
589 | pstrcpy(fmt, len, p2); | |
7fc2f2c0 | 590 | qemu_opt_set(opts, "format", fmt); |
334c0241 AJ |
591 | } else if (*filename != ':') { |
592 | printf("unrecognized USB mass-storage option %s\n", filename); | |
593 | return NULL; | |
594 | } | |
334c0241 AJ |
595 | filename = p1; |
596 | } | |
334c0241 AJ |
597 | if (!*filename) { |
598 | printf("block device specification needed\n"); | |
599 | return NULL; | |
600 | } | |
7fc2f2c0 GH |
601 | qemu_opt_set(opts, "file", filename); |
602 | qemu_opt_set(opts, "if", "none"); | |
2e5d83bb | 603 | |
7fc2f2c0 | 604 | /* create host drive */ |
319ae529 | 605 | dinfo = drive_init(opts, 0); |
7fc2f2c0 GH |
606 | if (!dinfo) { |
607 | qemu_opts_del(opts); | |
806b6024 | 608 | return NULL; |
7fc2f2c0 | 609 | } |
2e5d83bb | 610 | |
7fc2f2c0 | 611 | /* create guest device */ |
556cd098 | 612 | dev = usb_create(NULL /* FIXME */, "usb-storage"); |
d44168ff PB |
613 | if (!dev) { |
614 | return NULL; | |
615 | } | |
18846dee MA |
616 | if (qdev_prop_set_drive(&dev->qdev, "drive", dinfo->bdrv) < 0) { |
617 | qdev_free(&dev->qdev); | |
618 | return NULL; | |
619 | } | |
33e66b86 MA |
620 | if (qdev_init(&dev->qdev) < 0) |
621 | return NULL; | |
1f6e24e7 | 622 | |
7fc2f2c0 | 623 | return dev; |
2e5d83bb | 624 | } |
bb5fc20f | 625 | |
f54b6563 GH |
626 | static const VMStateDescription vmstate_usb_msd = { |
627 | .name = "usb-storage", | |
628 | .unmigratable = 1, /* FIXME: handle transactions which are in flight */ | |
629 | .version_id = 1, | |
630 | .minimum_version_id = 1, | |
631 | .fields = (VMStateField []) { | |
632 | VMSTATE_USB_DEVICE(dev, MSDState), | |
633 | VMSTATE_END_OF_LIST() | |
634 | } | |
635 | }; | |
636 | ||
806b6024 | 637 | static struct USBDeviceInfo msd_info = { |
06384698 | 638 | .product_desc = "QEMU USB MSD", |
556cd098 | 639 | .qdev.name = "usb-storage", |
cf8ce30d | 640 | .qdev.fw_name = "storage", |
806b6024 | 641 | .qdev.size = sizeof(MSDState), |
f54b6563 | 642 | .qdev.vmsd = &vmstate_usb_msd, |
81bfd2f2 | 643 | .usb_desc = &desc, |
806b6024 GH |
644 | .init = usb_msd_initfn, |
645 | .handle_packet = usb_generic_handle_packet, | |
eb5e680a | 646 | .cancel_packet = usb_msd_cancel_io, |
ca0c730d | 647 | .handle_attach = usb_desc_attach, |
806b6024 GH |
648 | .handle_reset = usb_msd_handle_reset, |
649 | .handle_control = usb_msd_handle_control, | |
650 | .handle_data = usb_msd_handle_data, | |
b3e461d3 GH |
651 | .usbdevice_name = "disk", |
652 | .usbdevice_init = usb_msd_init, | |
7fc2f2c0 | 653 | .qdev.props = (Property[]) { |
428c149b | 654 | DEFINE_BLOCK_PROPERTIES(MSDState, conf), |
c3a90cb1 | 655 | DEFINE_PROP_STRING("serial", MSDState, serial), |
6bb7b867 | 656 | DEFINE_PROP_BIT("removable", MSDState, removable, 0, false), |
7fc2f2c0 GH |
657 | DEFINE_PROP_END_OF_LIST(), |
658 | }, | |
806b6024 GH |
659 | }; |
660 | ||
661 | static void usb_msd_register_devices(void) | |
662 | { | |
663 | usb_qdev_register(&msd_info); | |
664 | } | |
665 | device_init(usb_msd_register_devices) |