]>
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 | ||
e532b2e0 | 10 | #include "qemu/osdep.h" |
da34e65c | 11 | #include "qapi/error.h" |
87ecb68b | 12 | #include "qemu-common.h" |
d49b6836 | 13 | #include "qemu/error-report.h" |
1de7afc9 PB |
14 | #include "qemu/option.h" |
15 | #include "qemu/config-file.h" | |
f1ae32a1 GH |
16 | #include "hw/usb.h" |
17 | #include "hw/usb/desc.h" | |
0d09e41a | 18 | #include "hw/scsi/scsi.h" |
28ecbaee | 19 | #include "ui/console.h" |
83c9089e | 20 | #include "monitor/monitor.h" |
9c17d615 | 21 | #include "sysemu/sysemu.h" |
fa1d36df | 22 | #include "sysemu/block-backend.h" |
9c17d615 | 23 | #include "sysemu/blockdev.h" |
89f0762d | 24 | #include "qapi/visitor.h" |
f348b6d1 | 25 | #include "qemu/cutils.h" |
2e5d83bb PB |
26 | |
27 | //#define DEBUG_MSD | |
28 | ||
29 | #ifdef DEBUG_MSD | |
001faf32 BS |
30 | #define DPRINTF(fmt, ...) \ |
31 | do { printf("usb-msd: " fmt , ## __VA_ARGS__); } while (0) | |
2e5d83bb | 32 | #else |
001faf32 | 33 | #define DPRINTF(fmt, ...) do {} while(0) |
2e5d83bb PB |
34 | #endif |
35 | ||
36 | /* USB requests. */ | |
37 | #define MassStorageReset 0xff | |
38 | #define GetMaxLun 0xfe | |
39 | ||
40 | enum USBMSDMode { | |
41 | USB_MSDM_CBW, /* Command Block. */ | |
94843f66 | 42 | USB_MSDM_DATAOUT, /* Transfer data to device. */ |
2e5d83bb PB |
43 | USB_MSDM_DATAIN, /* Transfer data from device. */ |
44 | USB_MSDM_CSW /* Command Status. */ | |
45 | }; | |
46 | ||
92a114f6 GH |
47 | struct usb_msd_csw { |
48 | uint32_t sig; | |
49 | uint32_t tag; | |
50 | uint32_t residue; | |
51 | uint8_t status; | |
52 | }; | |
53 | ||
2e5d83bb PB |
54 | typedef struct { |
55 | USBDevice dev; | |
56 | enum USBMSDMode mode; | |
1dc90367 | 57 | uint32_t scsi_off; |
a917d384 | 58 | uint32_t scsi_len; |
2e5d83bb | 59 | uint32_t data_len; |
92a114f6 | 60 | struct usb_msd_csw csw; |
5c6c0e51 | 61 | SCSIRequest *req; |
ca9c39fa | 62 | SCSIBus bus; |
34707333 GH |
63 | /* For async completion. */ |
64 | USBPacket *packet; | |
65 | /* usb-storage only */ | |
428c149b | 66 | BlockConf conf; |
6bb7b867 | 67 | uint32_t removable; |
89f0762d | 68 | SCSIDevice *scsi_dev; |
2e5d83bb PB |
69 | } MSDState; |
70 | ||
79e2590c GA |
71 | #define TYPE_USB_STORAGE "usb-storage-dev" |
72 | #define USB_STORAGE_DEV(obj) OBJECT_CHECK(MSDState, (obj), TYPE_USB_STORAGE) | |
73 | ||
a917d384 PB |
74 | struct usb_msd_cbw { |
75 | uint32_t sig; | |
76 | uint32_t tag; | |
77 | uint32_t data_len; | |
78 | uint8_t flags; | |
79 | uint8_t lun; | |
80 | uint8_t cmd_len; | |
81 | uint8_t cmd[16]; | |
82 | }; | |
83 | ||
81bfd2f2 GH |
84 | enum { |
85 | STR_MANUFACTURER = 1, | |
86 | STR_PRODUCT, | |
87 | STR_SERIALNUMBER, | |
ca0c730d GH |
88 | STR_CONFIG_FULL, |
89 | STR_CONFIG_HIGH, | |
79b40459 | 90 | STR_CONFIG_SUPER, |
2e5d83bb PB |
91 | }; |
92 | ||
81bfd2f2 | 93 | static const USBDescStrings desc_strings = { |
93bfef4c | 94 | [STR_MANUFACTURER] = "QEMU", |
81bfd2f2 GH |
95 | [STR_PRODUCT] = "QEMU USB HARDDRIVE", |
96 | [STR_SERIALNUMBER] = "1", | |
ca0c730d GH |
97 | [STR_CONFIG_FULL] = "Full speed config (usb 1.1)", |
98 | [STR_CONFIG_HIGH] = "High speed config (usb 2.0)", | |
79b40459 | 99 | [STR_CONFIG_SUPER] = "Super speed config (usb 3.0)", |
81bfd2f2 GH |
100 | }; |
101 | ||
ca0c730d | 102 | static const USBDescIface desc_iface_full = { |
81bfd2f2 GH |
103 | .bInterfaceNumber = 0, |
104 | .bNumEndpoints = 2, | |
105 | .bInterfaceClass = USB_CLASS_MASS_STORAGE, | |
106 | .bInterfaceSubClass = 0x06, /* SCSI */ | |
107 | .bInterfaceProtocol = 0x50, /* Bulk */ | |
108 | .eps = (USBDescEndpoint[]) { | |
109 | { | |
110 | .bEndpointAddress = USB_DIR_IN | 0x01, | |
111 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | |
112 | .wMaxPacketSize = 64, | |
113 | },{ | |
114 | .bEndpointAddress = USB_DIR_OUT | 0x02, | |
115 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | |
116 | .wMaxPacketSize = 64, | |
117 | }, | |
118 | } | |
119 | }; | |
120 | ||
ca0c730d GH |
121 | static const USBDescDevice desc_device_full = { |
122 | .bcdUSB = 0x0200, | |
81bfd2f2 GH |
123 | .bMaxPacketSize0 = 8, |
124 | .bNumConfigurations = 1, | |
125 | .confs = (USBDescConfig[]) { | |
126 | { | |
127 | .bNumInterfaces = 1, | |
128 | .bConfigurationValue = 1, | |
ca0c730d | 129 | .iConfiguration = STR_CONFIG_FULL, |
bd93976a | 130 | .bmAttributes = USB_CFG_ATT_ONE | USB_CFG_ATT_SELFPOWER, |
add75088 | 131 | .nif = 1, |
ca0c730d GH |
132 | .ifs = &desc_iface_full, |
133 | }, | |
134 | }, | |
135 | }; | |
136 | ||
137 | static const USBDescIface desc_iface_high = { | |
138 | .bInterfaceNumber = 0, | |
139 | .bNumEndpoints = 2, | |
140 | .bInterfaceClass = USB_CLASS_MASS_STORAGE, | |
141 | .bInterfaceSubClass = 0x06, /* SCSI */ | |
142 | .bInterfaceProtocol = 0x50, /* Bulk */ | |
143 | .eps = (USBDescEndpoint[]) { | |
144 | { | |
145 | .bEndpointAddress = USB_DIR_IN | 0x01, | |
146 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | |
147 | .wMaxPacketSize = 512, | |
148 | },{ | |
149 | .bEndpointAddress = USB_DIR_OUT | 0x02, | |
150 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | |
151 | .wMaxPacketSize = 512, | |
152 | }, | |
153 | } | |
154 | }; | |
155 | ||
156 | static const USBDescDevice desc_device_high = { | |
157 | .bcdUSB = 0x0200, | |
158 | .bMaxPacketSize0 = 64, | |
159 | .bNumConfigurations = 1, | |
160 | .confs = (USBDescConfig[]) { | |
161 | { | |
162 | .bNumInterfaces = 1, | |
163 | .bConfigurationValue = 1, | |
164 | .iConfiguration = STR_CONFIG_HIGH, | |
bd93976a | 165 | .bmAttributes = USB_CFG_ATT_ONE | USB_CFG_ATT_SELFPOWER, |
add75088 | 166 | .nif = 1, |
ca0c730d | 167 | .ifs = &desc_iface_high, |
81bfd2f2 GH |
168 | }, |
169 | }, | |
170 | }; | |
171 | ||
79b40459 GH |
172 | static const USBDescIface desc_iface_super = { |
173 | .bInterfaceNumber = 0, | |
174 | .bNumEndpoints = 2, | |
175 | .bInterfaceClass = USB_CLASS_MASS_STORAGE, | |
176 | .bInterfaceSubClass = 0x06, /* SCSI */ | |
177 | .bInterfaceProtocol = 0x50, /* Bulk */ | |
178 | .eps = (USBDescEndpoint[]) { | |
179 | { | |
180 | .bEndpointAddress = USB_DIR_IN | 0x01, | |
181 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | |
182 | .wMaxPacketSize = 1024, | |
183 | .bMaxBurst = 15, | |
184 | },{ | |
185 | .bEndpointAddress = USB_DIR_OUT | 0x02, | |
186 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | |
187 | .wMaxPacketSize = 1024, | |
188 | .bMaxBurst = 15, | |
189 | }, | |
190 | } | |
191 | }; | |
192 | ||
193 | static const USBDescDevice desc_device_super = { | |
194 | .bcdUSB = 0x0300, | |
195 | .bMaxPacketSize0 = 9, | |
196 | .bNumConfigurations = 1, | |
197 | .confs = (USBDescConfig[]) { | |
198 | { | |
199 | .bNumInterfaces = 1, | |
200 | .bConfigurationValue = 1, | |
201 | .iConfiguration = STR_CONFIG_SUPER, | |
bd93976a | 202 | .bmAttributes = USB_CFG_ATT_ONE | USB_CFG_ATT_SELFPOWER, |
79b40459 GH |
203 | .nif = 1, |
204 | .ifs = &desc_iface_super, | |
205 | }, | |
206 | }, | |
207 | }; | |
208 | ||
81bfd2f2 GH |
209 | static const USBDesc desc = { |
210 | .id = { | |
db80358a RT |
211 | .idVendor = 0x46f4, /* CRC16() of "QEMU" */ |
212 | .idProduct = 0x0001, | |
81bfd2f2 GH |
213 | .bcdDevice = 0, |
214 | .iManufacturer = STR_MANUFACTURER, | |
215 | .iProduct = STR_PRODUCT, | |
216 | .iSerialNumber = STR_SERIALNUMBER, | |
217 | }, | |
79b40459 GH |
218 | .full = &desc_device_full, |
219 | .high = &desc_device_high, | |
220 | .super = &desc_device_super, | |
221 | .str = desc_strings, | |
2e5d83bb PB |
222 | }; |
223 | ||
29c74f76 | 224 | static void usb_msd_copy_data(MSDState *s, USBPacket *p) |
a917d384 PB |
225 | { |
226 | uint32_t len; | |
9a77a0f5 | 227 | len = p->iov.size - p->actual_length; |
a917d384 PB |
228 | if (len > s->scsi_len) |
229 | len = s->scsi_len; | |
1dc90367 | 230 | usb_packet_copy(p, scsi_req_get_buf(s->req) + s->scsi_off, len); |
a917d384 | 231 | s->scsi_len -= len; |
1dc90367 | 232 | s->scsi_off += len; |
a917d384 | 233 | s->data_len -= len; |
fa7935c1 | 234 | if (s->scsi_len == 0 || s->data_len == 0) { |
ad3376cc | 235 | scsi_req_continue(s->req); |
a917d384 PB |
236 | } |
237 | } | |
238 | ||
ab4797ad | 239 | static void usb_msd_send_status(MSDState *s, USBPacket *p) |
a917d384 | 240 | { |
ab4797ad | 241 | int len; |
a917d384 | 242 | |
e04da7c3 | 243 | DPRINTF("Command status %d tag 0x%x, len %zd\n", |
e2854bf3 | 244 | s->csw.status, le32_to_cpu(s->csw.tag), p->iov.size); |
92a114f6 | 245 | |
e2854bf3 | 246 | assert(s->csw.sig == cpu_to_le32(0x53425355)); |
92a114f6 GH |
247 | len = MIN(sizeof(s->csw), p->iov.size); |
248 | usb_packet_copy(p, &s->csw, len); | |
249 | memset(&s->csw, 0, sizeof(s->csw)); | |
a917d384 PB |
250 | } |
251 | ||
1e6ed80b GH |
252 | static void usb_msd_packet_complete(MSDState *s) |
253 | { | |
254 | USBPacket *p = s->packet; | |
255 | ||
256 | /* Set s->packet to NULL before calling usb_packet_complete | |
257 | because another request may be issued before | |
258 | usb_packet_complete returns. */ | |
259 | DPRINTF("Packet complete %p\n", p); | |
260 | s->packet = NULL; | |
261 | usb_packet_complete(&s->dev, p); | |
262 | } | |
263 | ||
aba1f023 | 264 | static void usb_msd_transfer_data(SCSIRequest *req, uint32_t len) |
2e5d83bb | 265 | { |
5c6c0e51 | 266 | MSDState *s = DO_UPCAST(MSDState, dev.qdev, req->bus->qbus.parent); |
a917d384 | 267 | USBPacket *p = s->packet; |
4d611c9a | 268 | |
ad3376cc | 269 | assert((s->mode == USB_MSDM_DATAOUT) == (req->cmd.mode == SCSI_XFER_TO_DEV)); |
aba1f023 | 270 | s->scsi_len = len; |
1dc90367 | 271 | s->scsi_off = 0; |
a917d384 | 272 | if (p) { |
29c74f76 GH |
273 | usb_msd_copy_data(s, p); |
274 | p = s->packet; | |
9a77a0f5 HG |
275 | if (p && p->actual_length == p->iov.size) { |
276 | p->status = USB_RET_SUCCESS; /* Clear previous ASYNC status */ | |
1e6ed80b | 277 | usb_msd_packet_complete(s); |
a917d384 | 278 | } |
4d611c9a | 279 | } |
2e5d83bb PB |
280 | } |
281 | ||
01e95455 | 282 | static void usb_msd_command_complete(SCSIRequest *req, uint32_t status, size_t resid) |
c6df7102 PB |
283 | { |
284 | MSDState *s = DO_UPCAST(MSDState, dev.qdev, req->bus->qbus.parent); | |
285 | USBPacket *p = s->packet; | |
286 | ||
7b863f41 | 287 | DPRINTF("Command complete %d tag 0x%x\n", status, req->tag); |
92a114f6 GH |
288 | |
289 | s->csw.sig = cpu_to_le32(0x53425355); | |
7b863f41 | 290 | s->csw.tag = cpu_to_le32(req->tag); |
0659879e | 291 | s->csw.residue = cpu_to_le32(s->data_len); |
414c4604 | 292 | s->csw.status = status != 0; |
92a114f6 | 293 | |
c6df7102 PB |
294 | if (s->packet) { |
295 | if (s->data_len == 0 && s->mode == USB_MSDM_DATAOUT) { | |
296 | /* A deferred packet with no write data remaining must be | |
297 | the status read packet. */ | |
298 | usb_msd_send_status(s, p); | |
299 | s->mode = USB_MSDM_CBW; | |
54414218 GH |
300 | } else if (s->mode == USB_MSDM_CSW) { |
301 | usb_msd_send_status(s, p); | |
302 | s->mode = USB_MSDM_CBW; | |
c6df7102 PB |
303 | } else { |
304 | if (s->data_len) { | |
9a77a0f5 | 305 | int len = (p->iov.size - p->actual_length); |
29c74f76 GH |
306 | usb_packet_skip(p, len); |
307 | s->data_len -= len; | |
c6df7102 PB |
308 | } |
309 | if (s->data_len == 0) { | |
310 | s->mode = USB_MSDM_CSW; | |
311 | } | |
312 | } | |
9a77a0f5 | 313 | p->status = USB_RET_SUCCESS; /* Clear previous ASYNC status */ |
1e6ed80b | 314 | usb_msd_packet_complete(s); |
c6df7102 PB |
315 | } else if (s->data_len == 0) { |
316 | s->mode = USB_MSDM_CSW; | |
317 | } | |
318 | scsi_req_unref(req); | |
319 | s->req = NULL; | |
320 | } | |
321 | ||
94d3f98a PB |
322 | static void usb_msd_request_cancelled(SCSIRequest *req) |
323 | { | |
324 | MSDState *s = DO_UPCAST(MSDState, dev.qdev, req->bus->qbus.parent); | |
325 | ||
326 | if (req == s->req) { | |
327 | scsi_req_unref(s->req); | |
328 | s->req = NULL; | |
94d3f98a PB |
329 | s->scsi_len = 0; |
330 | } | |
331 | } | |
332 | ||
059809e4 | 333 | static void usb_msd_handle_reset(USBDevice *dev) |
2e5d83bb PB |
334 | { |
335 | MSDState *s = (MSDState *)dev; | |
336 | ||
337 | DPRINTF("Reset\n"); | |
24a5bbe1 GH |
338 | if (s->req) { |
339 | scsi_req_cancel(s->req); | |
340 | } | |
341 | assert(s->req == NULL); | |
342 | ||
343 | if (s->packet) { | |
9a77a0f5 | 344 | s->packet->status = USB_RET_STALL; |
1e6ed80b | 345 | usb_msd_packet_complete(s); |
24a5bbe1 GH |
346 | } |
347 | ||
2e5d83bb | 348 | s->mode = USB_MSDM_CBW; |
2e5d83bb PB |
349 | } |
350 | ||
9a77a0f5 | 351 | static void usb_msd_handle_control(USBDevice *dev, USBPacket *p, |
007fd62f | 352 | int request, int value, int index, int length, uint8_t *data) |
2e5d83bb PB |
353 | { |
354 | MSDState *s = (MSDState *)dev; | |
34707333 GH |
355 | SCSIDevice *scsi_dev; |
356 | int ret, maxlun; | |
2e5d83bb | 357 | |
007fd62f | 358 | ret = usb_desc_handle_control(dev, p, request, value, index, length, data); |
81bfd2f2 | 359 | if (ret >= 0) { |
9a77a0f5 | 360 | return; |
81bfd2f2 GH |
361 | } |
362 | ||
2e5d83bb | 363 | switch (request) { |
2e5d83bb | 364 | case EndpointOutRequest | USB_REQ_CLEAR_FEATURE: |
e5322f76 | 365 | break; |
2e5d83bb | 366 | /* Class specific requests. */ |
f3571b1a | 367 | case ClassInterfaceOutRequest | MassStorageReset: |
2e5d83bb PB |
368 | /* Reset state ready for the next CBW. */ |
369 | s->mode = USB_MSDM_CBW; | |
2e5d83bb | 370 | break; |
f3571b1a | 371 | case ClassInterfaceRequest | GetMaxLun: |
34707333 GH |
372 | maxlun = 0; |
373 | for (;;) { | |
374 | scsi_dev = scsi_device_find(&s->bus, 0, 0, maxlun+1); | |
375 | if (scsi_dev == NULL) { | |
376 | break; | |
377 | } | |
378 | if (scsi_dev->lun != maxlun+1) { | |
379 | break; | |
380 | } | |
381 | maxlun++; | |
382 | } | |
383 | DPRINTF("MaxLun %d\n", maxlun); | |
384 | data[0] = maxlun; | |
9a77a0f5 | 385 | p->actual_length = 1; |
2e5d83bb PB |
386 | break; |
387 | default: | |
9a77a0f5 | 388 | p->status = USB_RET_STALL; |
2e5d83bb PB |
389 | break; |
390 | } | |
2e5d83bb PB |
391 | } |
392 | ||
eb5e680a | 393 | static void usb_msd_cancel_io(USBDevice *dev, USBPacket *p) |
4d611c9a | 394 | { |
79e2590c | 395 | MSDState *s = USB_STORAGE_DEV(dev); |
d3ac1a87 | 396 | |
6d7aeeeb GH |
397 | assert(s->packet == p); |
398 | s->packet = NULL; | |
399 | ||
d3ac1a87 GH |
400 | if (s->req) { |
401 | scsi_req_cancel(s->req); | |
402 | } | |
4d611c9a PB |
403 | } |
404 | ||
9a77a0f5 | 405 | static void usb_msd_handle_data(USBDevice *dev, USBPacket *p) |
2e5d83bb PB |
406 | { |
407 | MSDState *s = (MSDState *)dev; | |
7b863f41 | 408 | uint32_t tag; |
2e5d83bb | 409 | struct usb_msd_cbw cbw; |
079d0b7f | 410 | uint8_t devep = p->ep->nr; |
34707333 | 411 | SCSIDevice *scsi_dev; |
9db7c414 | 412 | uint32_t len; |
2e5d83bb | 413 | |
4d611c9a | 414 | switch (p->pid) { |
2e5d83bb PB |
415 | case USB_TOKEN_OUT: |
416 | if (devep != 2) | |
417 | goto fail; | |
418 | ||
419 | switch (s->mode) { | |
420 | case USB_MSDM_CBW: | |
29c74f76 | 421 | if (p->iov.size != 31) { |
f5dc5978 | 422 | error_report("usb-msd: Bad CBW size"); |
2e5d83bb PB |
423 | goto fail; |
424 | } | |
29c74f76 | 425 | usb_packet_copy(p, &cbw, 31); |
2e5d83bb | 426 | if (le32_to_cpu(cbw.sig) != 0x43425355) { |
f5dc5978 GA |
427 | error_report("usb-msd: Bad signature %08x", |
428 | le32_to_cpu(cbw.sig)); | |
2e5d83bb PB |
429 | goto fail; |
430 | } | |
431 | DPRINTF("Command on LUN %d\n", cbw.lun); | |
34707333 GH |
432 | scsi_dev = scsi_device_find(&s->bus, 0, 0, cbw.lun); |
433 | if (scsi_dev == NULL) { | |
f5dc5978 | 434 | error_report("usb-msd: Bad LUN %d", cbw.lun); |
2e5d83bb PB |
435 | goto fail; |
436 | } | |
7b863f41 | 437 | tag = le32_to_cpu(cbw.tag); |
2e5d83bb PB |
438 | s->data_len = le32_to_cpu(cbw.data_len); |
439 | if (s->data_len == 0) { | |
440 | s->mode = USB_MSDM_CSW; | |
441 | } else if (cbw.flags & 0x80) { | |
442 | s->mode = USB_MSDM_DATAIN; | |
443 | } else { | |
444 | s->mode = USB_MSDM_DATAOUT; | |
445 | } | |
446 | DPRINTF("Command tag 0x%x flags %08x len %d data %d\n", | |
7b863f41 | 447 | tag, cbw.flags, cbw.cmd_len, s->data_len); |
0659879e | 448 | assert(le32_to_cpu(s->csw.residue) == 0); |
ef0bdf77 | 449 | s->scsi_len = 0; |
34707333 | 450 | s->req = scsi_req_new(scsi_dev, tag, cbw.lun, cbw.cmd, NULL); |
06f9847d GH |
451 | #ifdef DEBUG_MSD |
452 | scsi_req_print(s->req); | |
453 | #endif | |
9db7c414 GH |
454 | len = scsi_req_enqueue(s->req); |
455 | if (len) { | |
ad3376cc | 456 | scsi_req_continue(s->req); |
a917d384 | 457 | } |
2e5d83bb PB |
458 | break; |
459 | ||
460 | case USB_MSDM_DATAOUT: | |
29c74f76 GH |
461 | DPRINTF("Data out %zd/%d\n", p->iov.size, s->data_len); |
462 | if (p->iov.size > s->data_len) { | |
2e5d83bb | 463 | goto fail; |
29c74f76 | 464 | } |
2e5d83bb | 465 | |
a917d384 | 466 | if (s->scsi_len) { |
29c74f76 | 467 | usb_msd_copy_data(s, p); |
a917d384 | 468 | } |
0659879e | 469 | if (le32_to_cpu(s->csw.residue)) { |
9a77a0f5 | 470 | int len = p->iov.size - p->actual_length; |
29c74f76 GH |
471 | if (len) { |
472 | usb_packet_skip(p, len); | |
473 | s->data_len -= len; | |
474 | if (s->data_len == 0) { | |
475 | s->mode = USB_MSDM_CSW; | |
476 | } | |
477 | } | |
a917d384 | 478 | } |
9a77a0f5 | 479 | if (p->actual_length < p->iov.size) { |
06f9847d | 480 | DPRINTF("Deferring packet %p [wait data-out]\n", p); |
4d611c9a | 481 | s->packet = p; |
9a77a0f5 | 482 | p->status = USB_RET_ASYNC; |
4d611c9a | 483 | } |
2e5d83bb PB |
484 | break; |
485 | ||
486 | default: | |
29c74f76 | 487 | DPRINTF("Unexpected write (len %zd)\n", p->iov.size); |
2e5d83bb PB |
488 | goto fail; |
489 | } | |
490 | break; | |
491 | ||
492 | case USB_TOKEN_IN: | |
493 | if (devep != 1) | |
494 | goto fail; | |
495 | ||
496 | switch (s->mode) { | |
a917d384 | 497 | case USB_MSDM_DATAOUT: |
29c74f76 | 498 | if (s->data_len != 0 || p->iov.size < 13) { |
a917d384 | 499 | goto fail; |
29c74f76 | 500 | } |
a917d384 | 501 | /* Waiting for SCSI write to complete. */ |
a917d384 | 502 | s->packet = p; |
9a77a0f5 | 503 | p->status = USB_RET_ASYNC; |
a917d384 PB |
504 | break; |
505 | ||
2e5d83bb | 506 | case USB_MSDM_CSW: |
29c74f76 | 507 | if (p->iov.size < 13) { |
2e5d83bb | 508 | goto fail; |
29c74f76 | 509 | } |
2e5d83bb | 510 | |
59310659 GH |
511 | if (s->req) { |
512 | /* still in flight */ | |
06f9847d | 513 | DPRINTF("Deferring packet %p [wait status]\n", p); |
59310659 | 514 | s->packet = p; |
9a77a0f5 | 515 | p->status = USB_RET_ASYNC; |
59310659 GH |
516 | } else { |
517 | usb_msd_send_status(s, p); | |
518 | s->mode = USB_MSDM_CBW; | |
59310659 | 519 | } |
2e5d83bb PB |
520 | break; |
521 | ||
522 | case USB_MSDM_DATAIN: | |
29c74f76 GH |
523 | DPRINTF("Data in %zd/%d, scsi_len %d\n", |
524 | p->iov.size, s->data_len, s->scsi_len); | |
a917d384 | 525 | if (s->scsi_len) { |
29c74f76 | 526 | usb_msd_copy_data(s, p); |
a917d384 | 527 | } |
0659879e | 528 | if (le32_to_cpu(s->csw.residue)) { |
9a77a0f5 | 529 | int len = p->iov.size - p->actual_length; |
29c74f76 GH |
530 | if (len) { |
531 | usb_packet_skip(p, len); | |
532 | s->data_len -= len; | |
533 | if (s->data_len == 0) { | |
534 | s->mode = USB_MSDM_CSW; | |
535 | } | |
536 | } | |
a917d384 | 537 | } |
9a77a0f5 | 538 | if (p->actual_length < p->iov.size) { |
06f9847d | 539 | DPRINTF("Deferring packet %p [wait data-in]\n", p); |
4d611c9a | 540 | s->packet = p; |
9a77a0f5 | 541 | p->status = USB_RET_ASYNC; |
4d611c9a | 542 | } |
2e5d83bb PB |
543 | break; |
544 | ||
545 | default: | |
29c74f76 | 546 | DPRINTF("Unexpected read (len %zd)\n", p->iov.size); |
2e5d83bb PB |
547 | goto fail; |
548 | } | |
549 | break; | |
550 | ||
551 | default: | |
552 | DPRINTF("Bad token\n"); | |
553 | fail: | |
9a77a0f5 | 554 | p->status = USB_RET_STALL; |
2e5d83bb PB |
555 | break; |
556 | } | |
2e5d83bb PB |
557 | } |
558 | ||
b3e461d3 GH |
559 | static void usb_msd_password_cb(void *opaque, int err) |
560 | { | |
561 | MSDState *s = opaque; | |
7d553f27 | 562 | Error *local_err = NULL; |
b3e461d3 | 563 | |
7d553f27 GA |
564 | if (!err) { |
565 | usb_device_attach(&s->dev, &local_err); | |
566 | } | |
fa19bf83 | 567 | |
7d553f27 | 568 | if (local_err) { |
c326529b | 569 | error_report_err(local_err); |
56f9107e | 570 | qdev_unplug(&s->dev.qdev, NULL); |
7d553f27 | 571 | } |
b3e461d3 GH |
572 | } |
573 | ||
5de88b1d GH |
574 | static void *usb_msd_load_request(QEMUFile *f, SCSIRequest *req) |
575 | { | |
576 | MSDState *s = DO_UPCAST(MSDState, dev.qdev, req->bus->qbus.parent); | |
577 | ||
578 | /* nothing to load, just store req in our state struct */ | |
579 | assert(s->req == NULL); | |
580 | scsi_req_ref(req); | |
581 | s->req = req; | |
582 | return NULL; | |
583 | } | |
584 | ||
34707333 | 585 | static const struct SCSIBusInfo usb_msd_scsi_info_storage = { |
afd4030c | 586 | .tcq = false, |
7e0380b9 PB |
587 | .max_target = 0, |
588 | .max_lun = 0, | |
afd4030c | 589 | |
c6df7102 | 590 | .transfer_data = usb_msd_transfer_data, |
94d3f98a | 591 | .complete = usb_msd_command_complete, |
5de88b1d GH |
592 | .cancel = usb_msd_request_cancelled, |
593 | .load_request = usb_msd_load_request, | |
cfdc1bb0 PB |
594 | }; |
595 | ||
34707333 GH |
596 | static const struct SCSIBusInfo usb_msd_scsi_info_bot = { |
597 | .tcq = false, | |
598 | .max_target = 0, | |
599 | .max_lun = 15, | |
600 | ||
601 | .transfer_data = usb_msd_transfer_data, | |
602 | .complete = usb_msd_command_complete, | |
603 | .cancel = usb_msd_request_cancelled, | |
604 | .load_request = usb_msd_load_request, | |
605 | }; | |
606 | ||
5a882e40 | 607 | static void usb_msd_realize_storage(USBDevice *dev, Error **errp) |
806b6024 | 608 | { |
79e2590c | 609 | MSDState *s = USB_STORAGE_DEV(dev); |
4be74634 | 610 | BlockBackend *blk = s->conf.blk; |
34707333 | 611 | SCSIDevice *scsi_dev; |
caad4eb3 | 612 | Error *err = NULL; |
806b6024 | 613 | |
4be74634 | 614 | if (!blk) { |
5a882e40 GA |
615 | error_setg(errp, "drive property not set"); |
616 | return; | |
7fc2f2c0 GH |
617 | } |
618 | ||
7d3467d9 HR |
619 | if (blk_bs(blk)) { |
620 | bdrv_add_key(blk_bs(blk), NULL, &err); | |
621 | if (err) { | |
622 | if (monitor_cur_is_qmp()) { | |
623 | error_propagate(errp, err); | |
624 | return; | |
625 | } | |
626 | error_free(err); | |
627 | err = NULL; | |
628 | if (cur_mon) { | |
629 | monitor_read_bdrv_key_start(cur_mon, blk_bs(blk), | |
630 | usb_msd_password_cb, s); | |
631 | s->dev.auto_attach = 0; | |
632 | } else { | |
633 | autostart = 0; | |
634 | } | |
7afcc1f9 MA |
635 | } |
636 | } | |
637 | ||
71938a09 | 638 | blkconf_serial(&s->conf, &dev->serial); |
0eb28a42 | 639 | blkconf_blocksizes(&s->conf); |
911525db | 640 | |
14bafc54 MA |
641 | /* |
642 | * Hack alert: this pretends to be a block device, but it's really | |
643 | * a SCSI bus that can serve only a single device, which it | |
18846dee MA |
644 | * creates automatically. But first it needs to detach from its |
645 | * blockdev, or else scsi_bus_legacy_add_drive() dies when it | |
646 | * attaches again. | |
14bafc54 MA |
647 | * |
648 | * The hack is probably a bad idea. | |
649 | */ | |
4be74634 MA |
650 | blk_detach_dev(blk, &s->dev.qdev); |
651 | s->conf.blk = NULL; | |
14bafc54 | 652 | |
71938a09 | 653 | usb_desc_create_serial(dev); |
a980a065 | 654 | usb_desc_init(dev); |
b1187b51 AF |
655 | scsi_bus_new(&s->bus, sizeof(s->bus), DEVICE(dev), |
656 | &usb_msd_scsi_info_storage, NULL); | |
4be74634 | 657 | scsi_dev = scsi_bus_legacy_add_drive(&s->bus, blk, 0, !!s->removable, |
caad4eb3 AF |
658 | s->conf.bootindex, dev->serial, |
659 | &err); | |
34707333 | 660 | if (!scsi_dev) { |
5a882e40 GA |
661 | error_propagate(errp, err); |
662 | return; | |
fa66b909 | 663 | } |
7fc2f2c0 | 664 | usb_msd_handle_reset(dev); |
89f0762d | 665 | s->scsi_dev = scsi_dev; |
806b6024 GH |
666 | } |
667 | ||
5a882e40 | 668 | static void usb_msd_realize_bot(USBDevice *dev, Error **errp) |
34707333 | 669 | { |
79e2590c | 670 | MSDState *s = USB_STORAGE_DEV(dev); |
34707333 GH |
671 | |
672 | usb_desc_create_serial(dev); | |
673 | usb_desc_init(dev); | |
b1187b51 AF |
674 | scsi_bus_new(&s->bus, sizeof(s->bus), DEVICE(dev), |
675 | &usb_msd_scsi_info_bot, NULL); | |
34707333 | 676 | usb_msd_handle_reset(dev); |
34707333 GH |
677 | } |
678 | ||
3741715c | 679 | static USBDevice *usb_msd_init(USBBus *bus, const char *filename) |
2e5d83bb | 680 | { |
7fc2f2c0 | 681 | static int nr=0; |
9b3d111a | 682 | Error *err = NULL; |
7fc2f2c0 GH |
683 | char id[8]; |
684 | QemuOpts *opts; | |
685 | DriveInfo *dinfo; | |
806b6024 | 686 | USBDevice *dev; |
334c0241 AJ |
687 | const char *p1; |
688 | char fmt[32]; | |
689 | ||
7fc2f2c0 | 690 | /* parse -usbdevice disk: syntax into drive opts */ |
31376776 MA |
691 | do { |
692 | snprintf(id, sizeof(id), "usb%d", nr++); | |
693 | opts = qemu_opts_create(qemu_find_opts("drive"), id, 1, NULL); | |
694 | } while (!opts); | |
7fc2f2c0 | 695 | |
334c0241 AJ |
696 | p1 = strchr(filename, ':'); |
697 | if (p1++) { | |
698 | const char *p2; | |
699 | ||
700 | if (strstart(filename, "format=", &p2)) { | |
701 | int len = MIN(p1 - p2, sizeof(fmt)); | |
702 | pstrcpy(fmt, len, p2); | |
f43e47db | 703 | qemu_opt_set(opts, "format", fmt, &error_abort); |
334c0241 | 704 | } else if (*filename != ':') { |
f5dc5978 | 705 | error_report("unrecognized USB mass-storage option %s", filename); |
334c0241 AJ |
706 | return NULL; |
707 | } | |
334c0241 AJ |
708 | filename = p1; |
709 | } | |
334c0241 | 710 | if (!*filename) { |
f5dc5978 | 711 | error_report("block device specification needed"); |
334c0241 AJ |
712 | return NULL; |
713 | } | |
f43e47db MA |
714 | qemu_opt_set(opts, "file", filename, &error_abort); |
715 | qemu_opt_set(opts, "if", "none", &error_abort); | |
2e5d83bb | 716 | |
7fc2f2c0 | 717 | /* create host drive */ |
60e19e06 | 718 | dinfo = drive_new(opts, 0); |
7fc2f2c0 GH |
719 | if (!dinfo) { |
720 | qemu_opts_del(opts); | |
806b6024 | 721 | return NULL; |
7fc2f2c0 | 722 | } |
2e5d83bb | 723 | |
7fc2f2c0 | 724 | /* create guest device */ |
3741715c | 725 | dev = usb_create(bus, "usb-storage"); |
9b3d111a MA |
726 | qdev_prop_set_drive(&dev->qdev, "drive", blk_by_legacy_dinfo(dinfo), |
727 | &err); | |
728 | if (err) { | |
729 | error_report_err(err); | |
02a5c4c9 | 730 | object_unparent(OBJECT(dev)); |
18846dee MA |
731 | return NULL; |
732 | } | |
7fc2f2c0 | 733 | return dev; |
2e5d83bb | 734 | } |
bb5fc20f | 735 | |
f54b6563 GH |
736 | static const VMStateDescription vmstate_usb_msd = { |
737 | .name = "usb-storage", | |
f54b6563 GH |
738 | .version_id = 1, |
739 | .minimum_version_id = 1, | |
6e3d652a | 740 | .fields = (VMStateField[]) { |
f54b6563 | 741 | VMSTATE_USB_DEVICE(dev, MSDState), |
5de88b1d GH |
742 | VMSTATE_UINT32(mode, MSDState), |
743 | VMSTATE_UINT32(scsi_len, MSDState), | |
744 | VMSTATE_UINT32(scsi_off, MSDState), | |
745 | VMSTATE_UINT32(data_len, MSDState), | |
746 | VMSTATE_UINT32(csw.sig, MSDState), | |
747 | VMSTATE_UINT32(csw.tag, MSDState), | |
748 | VMSTATE_UINT32(csw.residue, MSDState), | |
749 | VMSTATE_UINT8(csw.status, MSDState), | |
f54b6563 GH |
750 | VMSTATE_END_OF_LIST() |
751 | } | |
752 | }; | |
753 | ||
39bffca2 AL |
754 | static Property msd_properties[] = { |
755 | DEFINE_BLOCK_PROPERTIES(MSDState, conf), | |
39bffca2 AL |
756 | DEFINE_PROP_BIT("removable", MSDState, removable, 0, false), |
757 | DEFINE_PROP_END_OF_LIST(), | |
758 | }; | |
759 | ||
79e2590c | 760 | static void usb_msd_class_initfn_common(ObjectClass *klass, void *data) |
62aed765 | 761 | { |
39bffca2 | 762 | DeviceClass *dc = DEVICE_CLASS(klass); |
62aed765 AL |
763 | USBDeviceClass *uc = USB_DEVICE_CLASS(klass); |
764 | ||
62aed765 AL |
765 | uc->product_desc = "QEMU USB MSD"; |
766 | uc->usb_desc = &desc; | |
62aed765 AL |
767 | uc->cancel_packet = usb_msd_cancel_io; |
768 | uc->handle_attach = usb_desc_attach; | |
769 | uc->handle_reset = usb_msd_handle_reset; | |
770 | uc->handle_control = usb_msd_handle_control; | |
771 | uc->handle_data = usb_msd_handle_data; | |
125ee0ed | 772 | set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); |
39bffca2 AL |
773 | dc->fw_name = "storage"; |
774 | dc->vmsd = &vmstate_usb_msd; | |
34707333 GH |
775 | } |
776 | ||
777 | static void usb_msd_class_initfn_storage(ObjectClass *klass, void *data) | |
778 | { | |
779 | DeviceClass *dc = DEVICE_CLASS(klass); | |
780 | USBDeviceClass *uc = USB_DEVICE_CLASS(klass); | |
781 | ||
5a882e40 | 782 | uc->realize = usb_msd_realize_storage; |
39bffca2 | 783 | dc->props = msd_properties; |
34707333 GH |
784 | } |
785 | ||
d7bce999 EB |
786 | static void usb_msd_get_bootindex(Object *obj, Visitor *v, const char *name, |
787 | void *opaque, Error **errp) | |
89f0762d GA |
788 | { |
789 | USBDevice *dev = USB_DEVICE(obj); | |
79e2590c | 790 | MSDState *s = USB_STORAGE_DEV(dev); |
89f0762d | 791 | |
51e72bc1 | 792 | visit_type_int32(v, name, &s->conf.bootindex, errp); |
89f0762d GA |
793 | } |
794 | ||
d7bce999 EB |
795 | static void usb_msd_set_bootindex(Object *obj, Visitor *v, const char *name, |
796 | void *opaque, Error **errp) | |
89f0762d GA |
797 | { |
798 | USBDevice *dev = USB_DEVICE(obj); | |
79e2590c | 799 | MSDState *s = USB_STORAGE_DEV(dev); |
89f0762d GA |
800 | int32_t boot_index; |
801 | Error *local_err = NULL; | |
802 | ||
51e72bc1 | 803 | visit_type_int32(v, name, &boot_index, &local_err); |
89f0762d GA |
804 | if (local_err) { |
805 | goto out; | |
806 | } | |
807 | /* check whether bootindex is present in fw_boot_order list */ | |
808 | check_boot_index(boot_index, &local_err); | |
809 | if (local_err) { | |
810 | goto out; | |
811 | } | |
812 | /* change bootindex to a new one */ | |
813 | s->conf.bootindex = boot_index; | |
814 | ||
815 | if (s->scsi_dev) { | |
816 | object_property_set_int(OBJECT(s->scsi_dev), boot_index, "bootindex", | |
817 | &error_abort); | |
818 | } | |
819 | ||
820 | out: | |
821 | if (local_err) { | |
822 | error_propagate(errp, local_err); | |
823 | } | |
824 | } | |
825 | ||
79e2590c GA |
826 | static const TypeInfo usb_storage_dev_type_info = { |
827 | .name = TYPE_USB_STORAGE, | |
828 | .parent = TYPE_USB_DEVICE, | |
829 | .instance_size = sizeof(MSDState), | |
830 | .abstract = true, | |
831 | .class_init = usb_msd_class_initfn_common, | |
832 | }; | |
833 | ||
89f0762d GA |
834 | static void usb_msd_instance_init(Object *obj) |
835 | { | |
836 | object_property_add(obj, "bootindex", "int32", | |
837 | usb_msd_get_bootindex, | |
838 | usb_msd_set_bootindex, NULL, NULL, NULL); | |
839 | object_property_set_int(obj, -1, "bootindex", NULL); | |
840 | } | |
841 | ||
34707333 GH |
842 | static void usb_msd_class_initfn_bot(ObjectClass *klass, void *data) |
843 | { | |
844 | USBDeviceClass *uc = USB_DEVICE_CLASS(klass); | |
e9fd12aa | 845 | DeviceClass *dc = DEVICE_CLASS(klass); |
34707333 | 846 | |
5a882e40 | 847 | uc->realize = usb_msd_realize_bot; |
e9fd12aa | 848 | dc->hotpluggable = false; |
62aed765 AL |
849 | } |
850 | ||
8c43a6f0 | 851 | static const TypeInfo msd_info = { |
39bffca2 | 852 | .name = "usb-storage", |
79e2590c | 853 | .parent = TYPE_USB_STORAGE, |
34707333 | 854 | .class_init = usb_msd_class_initfn_storage, |
89f0762d | 855 | .instance_init = usb_msd_instance_init, |
34707333 GH |
856 | }; |
857 | ||
858 | static const TypeInfo bot_info = { | |
859 | .name = "usb-bot", | |
79e2590c | 860 | .parent = TYPE_USB_STORAGE, |
34707333 | 861 | .class_init = usb_msd_class_initfn_bot, |
806b6024 GH |
862 | }; |
863 | ||
83f7d43a | 864 | static void usb_msd_register_types(void) |
806b6024 | 865 | { |
79e2590c | 866 | type_register_static(&usb_storage_dev_type_info); |
39bffca2 | 867 | type_register_static(&msd_info); |
34707333 | 868 | type_register_static(&bot_info); |
ba02430f | 869 | usb_legacy_register("usb-storage", "disk", usb_msd_init); |
806b6024 | 870 | } |
83f7d43a AF |
871 | |
872 | type_init(usb_msd_register_types) |