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