root / hw / usb-msd.c @ dd4239d6
History | View | Annotate | Download (17.3 kB)
1 | 5fafdf24 | ths | /*
|
---|---|---|---|
2 | 2e5d83bb | pbrook | * USB Mass Storage Device emulation
|
3 | 2e5d83bb | pbrook | *
|
4 | 2e5d83bb | pbrook | * Copyright (c) 2006 CodeSourcery.
|
5 | 2e5d83bb | pbrook | * Written by Paul Brook
|
6 | 2e5d83bb | pbrook | *
|
7 | 2e5d83bb | pbrook | * This code is licenced under the LGPL.
|
8 | 2e5d83bb | pbrook | */
|
9 | 2e5d83bb | pbrook | |
10 | 87ecb68b | pbrook | #include "qemu-common.h" |
11 | 7fc2f2c0 | Gerd Hoffmann | #include "qemu-option.h" |
12 | 7fc2f2c0 | Gerd Hoffmann | #include "qemu-config.h" |
13 | 87ecb68b | pbrook | #include "usb.h" |
14 | 87ecb68b | pbrook | #include "block.h" |
15 | 43b443b6 | Gerd Hoffmann | #include "scsi.h" |
16 | c0f4ce77 | aliguori | #include "console.h" |
17 | b3e461d3 | Gerd Hoffmann | #include "monitor.h" |
18 | 2e5d83bb | pbrook | |
19 | 2e5d83bb | pbrook | //#define DEBUG_MSD
|
20 | 2e5d83bb | pbrook | |
21 | 2e5d83bb | pbrook | #ifdef DEBUG_MSD
|
22 | 001faf32 | Blue Swirl | #define DPRINTF(fmt, ...) \
|
23 | 001faf32 | Blue Swirl | do { printf("usb-msd: " fmt , ## __VA_ARGS__); } while (0) |
24 | 2e5d83bb | pbrook | #else
|
25 | 001faf32 | Blue Swirl | #define DPRINTF(fmt, ...) do {} while(0) |
26 | 2e5d83bb | pbrook | #endif
|
27 | 2e5d83bb | pbrook | |
28 | 2e5d83bb | pbrook | /* USB requests. */
|
29 | 2e5d83bb | pbrook | #define MassStorageReset 0xff |
30 | 2e5d83bb | pbrook | #define GetMaxLun 0xfe |
31 | 2e5d83bb | pbrook | |
32 | 2e5d83bb | pbrook | enum USBMSDMode {
|
33 | 2e5d83bb | pbrook | USB_MSDM_CBW, /* Command Block. */
|
34 | 2e5d83bb | pbrook | USB_MSDM_DATAOUT, /* Tranfer data to device. */
|
35 | 2e5d83bb | pbrook | USB_MSDM_DATAIN, /* Transfer data from device. */
|
36 | 2e5d83bb | pbrook | USB_MSDM_CSW /* Command Status. */
|
37 | 2e5d83bb | pbrook | }; |
38 | 2e5d83bb | pbrook | |
39 | 2e5d83bb | pbrook | typedef struct { |
40 | 2e5d83bb | pbrook | USBDevice dev; |
41 | 2e5d83bb | pbrook | enum USBMSDMode mode;
|
42 | a917d384 | pbrook | uint32_t scsi_len; |
43 | a917d384 | pbrook | uint8_t *scsi_buf; |
44 | a917d384 | pbrook | uint32_t usb_len; |
45 | a917d384 | pbrook | uint8_t *usb_buf; |
46 | 2e5d83bb | pbrook | uint32_t data_len; |
47 | a917d384 | pbrook | uint32_t residue; |
48 | 2e5d83bb | pbrook | uint32_t tag; |
49 | ca9c39fa | Gerd Hoffmann | SCSIBus bus; |
50 | 7fc2f2c0 | Gerd Hoffmann | DriveInfo *dinfo; |
51 | 2e5d83bb | pbrook | SCSIDevice *scsi_dev; |
52 | 2e5d83bb | pbrook | int result;
|
53 | 4d611c9a | pbrook | /* For async completion. */
|
54 | 4d611c9a | pbrook | USBPacket *packet; |
55 | 2e5d83bb | pbrook | } MSDState; |
56 | 2e5d83bb | pbrook | |
57 | a917d384 | pbrook | struct usb_msd_cbw {
|
58 | a917d384 | pbrook | uint32_t sig; |
59 | a917d384 | pbrook | uint32_t tag; |
60 | a917d384 | pbrook | uint32_t data_len; |
61 | a917d384 | pbrook | uint8_t flags; |
62 | a917d384 | pbrook | uint8_t lun; |
63 | a917d384 | pbrook | uint8_t cmd_len; |
64 | a917d384 | pbrook | uint8_t cmd[16];
|
65 | a917d384 | pbrook | }; |
66 | a917d384 | pbrook | |
67 | a917d384 | pbrook | struct usb_msd_csw {
|
68 | a917d384 | pbrook | uint32_t sig; |
69 | a917d384 | pbrook | uint32_t tag; |
70 | a917d384 | pbrook | uint32_t residue; |
71 | a917d384 | pbrook | uint8_t status; |
72 | a917d384 | pbrook | }; |
73 | a917d384 | pbrook | |
74 | 2e5d83bb | pbrook | static const uint8_t qemu_msd_dev_descriptor[] = { |
75 | 2e5d83bb | pbrook | 0x12, /* u8 bLength; */ |
76 | 2e5d83bb | pbrook | 0x01, /* u8 bDescriptorType; Device */ |
77 | e126cf13 | ths | 0x00, 0x01, /* u16 bcdUSB; v1.0 */ |
78 | 2e5d83bb | pbrook | |
79 | 2e5d83bb | pbrook | 0x00, /* u8 bDeviceClass; */ |
80 | 2e5d83bb | pbrook | 0x00, /* u8 bDeviceSubClass; */ |
81 | 2e5d83bb | pbrook | 0x00, /* u8 bDeviceProtocol; [ low/full speeds only ] */ |
82 | 2e5d83bb | pbrook | 0x08, /* u8 bMaxPacketSize0; 8 Bytes */ |
83 | 2e5d83bb | pbrook | |
84 | 2e5d83bb | pbrook | /* Vendor and product id are arbitrary. */
|
85 | 2e5d83bb | pbrook | 0x00, 0x00, /* u16 idVendor; */ |
86 | 2e5d83bb | pbrook | 0x00, 0x00, /* u16 idProduct; */ |
87 | 2e5d83bb | pbrook | 0x00, 0x00, /* u16 bcdDevice */ |
88 | 2e5d83bb | pbrook | |
89 | 2e5d83bb | pbrook | 0x01, /* u8 iManufacturer; */ |
90 | 2e5d83bb | pbrook | 0x02, /* u8 iProduct; */ |
91 | 2e5d83bb | pbrook | 0x03, /* u8 iSerialNumber; */ |
92 | 2e5d83bb | pbrook | 0x01 /* u8 bNumConfigurations; */ |
93 | 2e5d83bb | pbrook | }; |
94 | 2e5d83bb | pbrook | |
95 | 2e5d83bb | pbrook | static const uint8_t qemu_msd_config_descriptor[] = { |
96 | 2e5d83bb | pbrook | |
97 | 2e5d83bb | pbrook | /* one configuration */
|
98 | 2e5d83bb | pbrook | 0x09, /* u8 bLength; */ |
99 | 2e5d83bb | pbrook | 0x02, /* u8 bDescriptorType; Configuration */ |
100 | 2e5d83bb | pbrook | 0x20, 0x00, /* u16 wTotalLength; */ |
101 | 2e5d83bb | pbrook | 0x01, /* u8 bNumInterfaces; (1) */ |
102 | 2e5d83bb | pbrook | 0x01, /* u8 bConfigurationValue; */ |
103 | 2e5d83bb | pbrook | 0x00, /* u8 iConfiguration; */ |
104 | 5fafdf24 | ths | 0xc0, /* u8 bmAttributes; |
105 | 2e5d83bb | pbrook | Bit 7: must be set,
|
106 | 2e5d83bb | pbrook | 6: Self-powered,
|
107 | 2e5d83bb | pbrook | 5: Remote wakeup,
|
108 | 2e5d83bb | pbrook | 4..0: resvd */
|
109 | 2e5d83bb | pbrook | 0x00, /* u8 MaxPower; */ |
110 | 3b46e624 | ths | |
111 | 2e5d83bb | pbrook | /* one interface */
|
112 | 2e5d83bb | pbrook | 0x09, /* u8 if_bLength; */ |
113 | 2e5d83bb | pbrook | 0x04, /* u8 if_bDescriptorType; Interface */ |
114 | 2e5d83bb | pbrook | 0x00, /* u8 if_bInterfaceNumber; */ |
115 | 2e5d83bb | pbrook | 0x00, /* u8 if_bAlternateSetting; */ |
116 | 2e5d83bb | pbrook | 0x02, /* u8 if_bNumEndpoints; */ |
117 | 2e5d83bb | pbrook | 0x08, /* u8 if_bInterfaceClass; MASS STORAGE */ |
118 | 2e5d83bb | pbrook | 0x06, /* u8 if_bInterfaceSubClass; SCSI */ |
119 | 2e5d83bb | pbrook | 0x50, /* u8 if_bInterfaceProtocol; Bulk Only */ |
120 | 2e5d83bb | pbrook | 0x00, /* u8 if_iInterface; */ |
121 | 3b46e624 | ths | |
122 | 2e5d83bb | pbrook | /* Bulk-In endpoint */
|
123 | 2e5d83bb | pbrook | 0x07, /* u8 ep_bLength; */ |
124 | 2e5d83bb | pbrook | 0x05, /* u8 ep_bDescriptorType; Endpoint */ |
125 | 2e5d83bb | pbrook | 0x81, /* u8 ep_bEndpointAddress; IN Endpoint 1 */ |
126 | 2e5d83bb | pbrook | 0x02, /* u8 ep_bmAttributes; Bulk */ |
127 | 2e5d83bb | pbrook | 0x40, 0x00, /* u16 ep_wMaxPacketSize; */ |
128 | 2e5d83bb | pbrook | 0x00, /* u8 ep_bInterval; */ |
129 | 2e5d83bb | pbrook | |
130 | 2e5d83bb | pbrook | /* Bulk-Out endpoint */
|
131 | 2e5d83bb | pbrook | 0x07, /* u8 ep_bLength; */ |
132 | 2e5d83bb | pbrook | 0x05, /* u8 ep_bDescriptorType; Endpoint */ |
133 | 2e5d83bb | pbrook | 0x02, /* u8 ep_bEndpointAddress; OUT Endpoint 2 */ |
134 | 2e5d83bb | pbrook | 0x02, /* u8 ep_bmAttributes; Bulk */ |
135 | 2e5d83bb | pbrook | 0x40, 0x00, /* u16 ep_wMaxPacketSize; */ |
136 | 2e5d83bb | pbrook | 0x00 /* u8 ep_bInterval; */ |
137 | 2e5d83bb | pbrook | }; |
138 | 2e5d83bb | pbrook | |
139 | a917d384 | pbrook | static void usb_msd_copy_data(MSDState *s) |
140 | a917d384 | pbrook | { |
141 | a917d384 | pbrook | uint32_t len; |
142 | a917d384 | pbrook | len = s->usb_len; |
143 | a917d384 | pbrook | if (len > s->scsi_len)
|
144 | a917d384 | pbrook | len = s->scsi_len; |
145 | a917d384 | pbrook | if (s->mode == USB_MSDM_DATAIN) {
|
146 | a917d384 | pbrook | memcpy(s->usb_buf, s->scsi_buf, len); |
147 | a917d384 | pbrook | } else {
|
148 | a917d384 | pbrook | memcpy(s->scsi_buf, s->usb_buf, len); |
149 | a917d384 | pbrook | } |
150 | a917d384 | pbrook | s->usb_len -= len; |
151 | a917d384 | pbrook | s->scsi_len -= len; |
152 | a917d384 | pbrook | s->usb_buf += len; |
153 | a917d384 | pbrook | s->scsi_buf += len; |
154 | a917d384 | pbrook | s->data_len -= len; |
155 | a917d384 | pbrook | if (s->scsi_len == 0) { |
156 | a917d384 | pbrook | if (s->mode == USB_MSDM_DATAIN) {
|
157 | d52affa7 | Gerd Hoffmann | s->scsi_dev->info->read_data(s->scsi_dev, s->tag); |
158 | a917d384 | pbrook | } else if (s->mode == USB_MSDM_DATAOUT) { |
159 | d52affa7 | Gerd Hoffmann | s->scsi_dev->info->write_data(s->scsi_dev, s->tag); |
160 | a917d384 | pbrook | } |
161 | a917d384 | pbrook | } |
162 | a917d384 | pbrook | } |
163 | a917d384 | pbrook | |
164 | a917d384 | pbrook | static void usb_msd_send_status(MSDState *s) |
165 | a917d384 | pbrook | { |
166 | a917d384 | pbrook | struct usb_msd_csw csw;
|
167 | a917d384 | pbrook | |
168 | a917d384 | pbrook | csw.sig = cpu_to_le32(0x53425355);
|
169 | a917d384 | pbrook | csw.tag = cpu_to_le32(s->tag); |
170 | a917d384 | pbrook | csw.residue = s->residue; |
171 | a917d384 | pbrook | csw.status = s->result; |
172 | a917d384 | pbrook | memcpy(s->usb_buf, &csw, 13);
|
173 | a917d384 | pbrook | } |
174 | a917d384 | pbrook | |
175 | d52affa7 | Gerd Hoffmann | static void usb_msd_command_complete(SCSIBus *bus, int reason, uint32_t tag, |
176 | a917d384 | pbrook | uint32_t arg) |
177 | 2e5d83bb | pbrook | { |
178 | d52affa7 | Gerd Hoffmann | MSDState *s = DO_UPCAST(MSDState, dev.qdev, bus->qbus.parent); |
179 | a917d384 | pbrook | USBPacket *p = s->packet; |
180 | 4d611c9a | pbrook | |
181 | a917d384 | pbrook | if (tag != s->tag) {
|
182 | a917d384 | pbrook | fprintf(stderr, "usb-msd: Unexpected SCSI Tag 0x%x\n", tag);
|
183 | a917d384 | pbrook | } |
184 | 4d611c9a | pbrook | if (reason == SCSI_REASON_DONE) {
|
185 | a917d384 | pbrook | DPRINTF("Command complete %d\n", arg);
|
186 | a917d384 | pbrook | s->residue = s->data_len; |
187 | a917d384 | pbrook | s->result = arg != 0;
|
188 | a917d384 | pbrook | if (s->packet) {
|
189 | a917d384 | pbrook | if (s->data_len == 0 && s->mode == USB_MSDM_DATAOUT) { |
190 | a917d384 | pbrook | /* A deferred packet with no write data remaining must be
|
191 | a917d384 | pbrook | the status read packet. */
|
192 | a917d384 | pbrook | usb_msd_send_status(s); |
193 | a917d384 | pbrook | s->mode = USB_MSDM_CBW; |
194 | a917d384 | pbrook | } else {
|
195 | a917d384 | pbrook | if (s->data_len) {
|
196 | a917d384 | pbrook | s->data_len -= s->usb_len; |
197 | a917d384 | pbrook | if (s->mode == USB_MSDM_DATAIN)
|
198 | a917d384 | pbrook | memset(s->usb_buf, 0, s->usb_len);
|
199 | a917d384 | pbrook | s->usb_len = 0;
|
200 | a917d384 | pbrook | } |
201 | a917d384 | pbrook | if (s->data_len == 0) |
202 | a917d384 | pbrook | s->mode = USB_MSDM_CSW; |
203 | a917d384 | pbrook | } |
204 | a917d384 | pbrook | s->packet = NULL;
|
205 | a917d384 | pbrook | usb_packet_complete(p); |
206 | a917d384 | pbrook | } else if (s->data_len == 0) { |
207 | a917d384 | pbrook | s->mode = USB_MSDM_CSW; |
208 | a917d384 | pbrook | } |
209 | a917d384 | pbrook | return;
|
210 | 4d611c9a | pbrook | } |
211 | a917d384 | pbrook | s->scsi_len = arg; |
212 | d52affa7 | Gerd Hoffmann | s->scsi_buf = s->scsi_dev->info->get_buf(s->scsi_dev, tag); |
213 | a917d384 | pbrook | if (p) {
|
214 | a917d384 | pbrook | usb_msd_copy_data(s); |
215 | a917d384 | pbrook | if (s->usb_len == 0) { |
216 | a917d384 | pbrook | /* Set s->packet to NULL before calling usb_packet_complete
|
217 | a917d384 | pbrook | because annother request may be issued before
|
218 | a917d384 | pbrook | usb_packet_complete returns. */
|
219 | a917d384 | pbrook | DPRINTF("Packet complete %p\n", p);
|
220 | a917d384 | pbrook | s->packet = NULL;
|
221 | a917d384 | pbrook | usb_packet_complete(p); |
222 | a917d384 | pbrook | } |
223 | 4d611c9a | pbrook | } |
224 | 2e5d83bb | pbrook | } |
225 | 2e5d83bb | pbrook | |
226 | 059809e4 | bellard | static void usb_msd_handle_reset(USBDevice *dev) |
227 | 2e5d83bb | pbrook | { |
228 | 2e5d83bb | pbrook | MSDState *s = (MSDState *)dev; |
229 | 2e5d83bb | pbrook | |
230 | 2e5d83bb | pbrook | DPRINTF("Reset\n");
|
231 | 2e5d83bb | pbrook | s->mode = USB_MSDM_CBW; |
232 | 2e5d83bb | pbrook | } |
233 | 2e5d83bb | pbrook | |
234 | 2e5d83bb | pbrook | static int usb_msd_handle_control(USBDevice *dev, int request, int value, |
235 | 2e5d83bb | pbrook | int index, int length, uint8_t *data) |
236 | 2e5d83bb | pbrook | { |
237 | 2e5d83bb | pbrook | MSDState *s = (MSDState *)dev; |
238 | 2e5d83bb | pbrook | int ret = 0; |
239 | 2e5d83bb | pbrook | |
240 | 2e5d83bb | pbrook | switch (request) {
|
241 | 2e5d83bb | pbrook | case DeviceRequest | USB_REQ_GET_STATUS:
|
242 | 2e5d83bb | pbrook | data[0] = (1 << USB_DEVICE_SELF_POWERED) | |
243 | 2e5d83bb | pbrook | (dev->remote_wakeup << USB_DEVICE_REMOTE_WAKEUP); |
244 | 2e5d83bb | pbrook | data[1] = 0x00; |
245 | 2e5d83bb | pbrook | ret = 2;
|
246 | 2e5d83bb | pbrook | break;
|
247 | 2e5d83bb | pbrook | case DeviceOutRequest | USB_REQ_CLEAR_FEATURE:
|
248 | 2e5d83bb | pbrook | if (value == USB_DEVICE_REMOTE_WAKEUP) {
|
249 | 2e5d83bb | pbrook | dev->remote_wakeup = 0;
|
250 | 2e5d83bb | pbrook | } else {
|
251 | 2e5d83bb | pbrook | goto fail;
|
252 | 2e5d83bb | pbrook | } |
253 | 2e5d83bb | pbrook | ret = 0;
|
254 | 2e5d83bb | pbrook | break;
|
255 | 2e5d83bb | pbrook | case DeviceOutRequest | USB_REQ_SET_FEATURE:
|
256 | 2e5d83bb | pbrook | if (value == USB_DEVICE_REMOTE_WAKEUP) {
|
257 | 2e5d83bb | pbrook | dev->remote_wakeup = 1;
|
258 | 2e5d83bb | pbrook | } else {
|
259 | 2e5d83bb | pbrook | goto fail;
|
260 | 2e5d83bb | pbrook | } |
261 | 2e5d83bb | pbrook | ret = 0;
|
262 | 2e5d83bb | pbrook | break;
|
263 | 2e5d83bb | pbrook | case DeviceOutRequest | USB_REQ_SET_ADDRESS:
|
264 | 2e5d83bb | pbrook | dev->addr = value; |
265 | 2e5d83bb | pbrook | ret = 0;
|
266 | 2e5d83bb | pbrook | break;
|
267 | 2e5d83bb | pbrook | case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
|
268 | 2e5d83bb | pbrook | switch(value >> 8) { |
269 | 2e5d83bb | pbrook | case USB_DT_DEVICE:
|
270 | 5fafdf24 | ths | memcpy(data, qemu_msd_dev_descriptor, |
271 | 2e5d83bb | pbrook | sizeof(qemu_msd_dev_descriptor));
|
272 | 2e5d83bb | pbrook | ret = sizeof(qemu_msd_dev_descriptor);
|
273 | 2e5d83bb | pbrook | break;
|
274 | 2e5d83bb | pbrook | case USB_DT_CONFIG:
|
275 | 5fafdf24 | ths | memcpy(data, qemu_msd_config_descriptor, |
276 | 2e5d83bb | pbrook | sizeof(qemu_msd_config_descriptor));
|
277 | 2e5d83bb | pbrook | ret = sizeof(qemu_msd_config_descriptor);
|
278 | 2e5d83bb | pbrook | break;
|
279 | 2e5d83bb | pbrook | case USB_DT_STRING:
|
280 | 2e5d83bb | pbrook | switch(value & 0xff) { |
281 | 2e5d83bb | pbrook | case 0: |
282 | 2e5d83bb | pbrook | /* language ids */
|
283 | 2e5d83bb | pbrook | data[0] = 4; |
284 | 2e5d83bb | pbrook | data[1] = 3; |
285 | 2e5d83bb | pbrook | data[2] = 0x09; |
286 | 2e5d83bb | pbrook | data[3] = 0x04; |
287 | 2e5d83bb | pbrook | ret = 4;
|
288 | 2e5d83bb | pbrook | break;
|
289 | 2e5d83bb | pbrook | case 1: |
290 | 2e5d83bb | pbrook | /* vendor description */
|
291 | 2e5d83bb | pbrook | ret = set_usb_string(data, "QEMU " QEMU_VERSION);
|
292 | 2e5d83bb | pbrook | break;
|
293 | 2e5d83bb | pbrook | case 2: |
294 | 2e5d83bb | pbrook | /* product description */
|
295 | 2e5d83bb | pbrook | ret = set_usb_string(data, "QEMU USB HARDDRIVE");
|
296 | 2e5d83bb | pbrook | break;
|
297 | 2e5d83bb | pbrook | case 3: |
298 | 2e5d83bb | pbrook | /* serial number */
|
299 | 2e5d83bb | pbrook | ret = set_usb_string(data, "1");
|
300 | 2e5d83bb | pbrook | break;
|
301 | 2e5d83bb | pbrook | default:
|
302 | 2e5d83bb | pbrook | goto fail;
|
303 | 2e5d83bb | pbrook | } |
304 | 2e5d83bb | pbrook | break;
|
305 | 2e5d83bb | pbrook | default:
|
306 | 2e5d83bb | pbrook | goto fail;
|
307 | 2e5d83bb | pbrook | } |
308 | 2e5d83bb | pbrook | break;
|
309 | 2e5d83bb | pbrook | case DeviceRequest | USB_REQ_GET_CONFIGURATION:
|
310 | 2e5d83bb | pbrook | data[0] = 1; |
311 | 2e5d83bb | pbrook | ret = 1;
|
312 | 2e5d83bb | pbrook | break;
|
313 | 2e5d83bb | pbrook | case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
|
314 | 2e5d83bb | pbrook | ret = 0;
|
315 | 2e5d83bb | pbrook | break;
|
316 | 2e5d83bb | pbrook | case DeviceRequest | USB_REQ_GET_INTERFACE:
|
317 | 2e5d83bb | pbrook | data[0] = 0; |
318 | 2e5d83bb | pbrook | ret = 1;
|
319 | 2e5d83bb | pbrook | break;
|
320 | 2e5d83bb | pbrook | case DeviceOutRequest | USB_REQ_SET_INTERFACE:
|
321 | 2e5d83bb | pbrook | ret = 0;
|
322 | 2e5d83bb | pbrook | break;
|
323 | 2e5d83bb | pbrook | case EndpointOutRequest | USB_REQ_CLEAR_FEATURE:
|
324 | 2e5d83bb | pbrook | if (value == 0 && index != 0x81) { /* clear ep halt */ |
325 | 2e5d83bb | pbrook | goto fail;
|
326 | 2e5d83bb | pbrook | } |
327 | 2e5d83bb | pbrook | ret = 0;
|
328 | 2e5d83bb | pbrook | break;
|
329 | 2e5d83bb | pbrook | /* Class specific requests. */
|
330 | 2e5d83bb | pbrook | case MassStorageReset:
|
331 | 2e5d83bb | pbrook | /* Reset state ready for the next CBW. */
|
332 | 2e5d83bb | pbrook | s->mode = USB_MSDM_CBW; |
333 | 2e5d83bb | pbrook | ret = 0;
|
334 | 2e5d83bb | pbrook | break;
|
335 | 2e5d83bb | pbrook | case GetMaxLun:
|
336 | 2e5d83bb | pbrook | data[0] = 0; |
337 | 2e5d83bb | pbrook | ret = 1;
|
338 | 2e5d83bb | pbrook | break;
|
339 | 2e5d83bb | pbrook | default:
|
340 | 2e5d83bb | pbrook | fail:
|
341 | 2e5d83bb | pbrook | ret = USB_RET_STALL; |
342 | 2e5d83bb | pbrook | break;
|
343 | 2e5d83bb | pbrook | } |
344 | 2e5d83bb | pbrook | return ret;
|
345 | 2e5d83bb | pbrook | } |
346 | 2e5d83bb | pbrook | |
347 | 4d611c9a | pbrook | static void usb_msd_cancel_io(USBPacket *p, void *opaque) |
348 | 4d611c9a | pbrook | { |
349 | 4d611c9a | pbrook | MSDState *s = opaque; |
350 | d52affa7 | Gerd Hoffmann | s->scsi_dev->info->cancel_io(s->scsi_dev, s->tag); |
351 | 4d611c9a | pbrook | s->packet = NULL;
|
352 | a917d384 | pbrook | s->scsi_len = 0;
|
353 | 4d611c9a | pbrook | } |
354 | 4d611c9a | pbrook | |
355 | 4d611c9a | pbrook | static int usb_msd_handle_data(USBDevice *dev, USBPacket *p) |
356 | 2e5d83bb | pbrook | { |
357 | 2e5d83bb | pbrook | MSDState *s = (MSDState *)dev; |
358 | 2e5d83bb | pbrook | int ret = 0; |
359 | 2e5d83bb | pbrook | struct usb_msd_cbw cbw;
|
360 | 4d611c9a | pbrook | uint8_t devep = p->devep; |
361 | 4d611c9a | pbrook | uint8_t *data = p->data; |
362 | 4d611c9a | pbrook | int len = p->len;
|
363 | 2e5d83bb | pbrook | |
364 | 4d611c9a | pbrook | switch (p->pid) {
|
365 | 2e5d83bb | pbrook | case USB_TOKEN_OUT:
|
366 | 2e5d83bb | pbrook | if (devep != 2) |
367 | 2e5d83bb | pbrook | goto fail;
|
368 | 2e5d83bb | pbrook | |
369 | 2e5d83bb | pbrook | switch (s->mode) {
|
370 | 2e5d83bb | pbrook | case USB_MSDM_CBW:
|
371 | 2e5d83bb | pbrook | if (len != 31) { |
372 | 2e5d83bb | pbrook | fprintf(stderr, "usb-msd: Bad CBW size");
|
373 | 2e5d83bb | pbrook | goto fail;
|
374 | 2e5d83bb | pbrook | } |
375 | 2e5d83bb | pbrook | memcpy(&cbw, data, 31);
|
376 | 2e5d83bb | pbrook | if (le32_to_cpu(cbw.sig) != 0x43425355) { |
377 | 2e5d83bb | pbrook | fprintf(stderr, "usb-msd: Bad signature %08x\n",
|
378 | 2e5d83bb | pbrook | le32_to_cpu(cbw.sig)); |
379 | 2e5d83bb | pbrook | goto fail;
|
380 | 2e5d83bb | pbrook | } |
381 | 2e5d83bb | pbrook | DPRINTF("Command on LUN %d\n", cbw.lun);
|
382 | 2e5d83bb | pbrook | if (cbw.lun != 0) { |
383 | 2e5d83bb | pbrook | fprintf(stderr, "usb-msd: Bad LUN %d\n", cbw.lun);
|
384 | 2e5d83bb | pbrook | goto fail;
|
385 | 2e5d83bb | pbrook | } |
386 | 2e5d83bb | pbrook | s->tag = le32_to_cpu(cbw.tag); |
387 | 2e5d83bb | pbrook | s->data_len = le32_to_cpu(cbw.data_len); |
388 | 2e5d83bb | pbrook | if (s->data_len == 0) { |
389 | 2e5d83bb | pbrook | s->mode = USB_MSDM_CSW; |
390 | 2e5d83bb | pbrook | } else if (cbw.flags & 0x80) { |
391 | 2e5d83bb | pbrook | s->mode = USB_MSDM_DATAIN; |
392 | 2e5d83bb | pbrook | } else {
|
393 | 2e5d83bb | pbrook | s->mode = USB_MSDM_DATAOUT; |
394 | 2e5d83bb | pbrook | } |
395 | 2e5d83bb | pbrook | DPRINTF("Command tag 0x%x flags %08x len %d data %d\n",
|
396 | 2e5d83bb | pbrook | s->tag, cbw.flags, cbw.cmd_len, s->data_len); |
397 | a917d384 | pbrook | s->residue = 0;
|
398 | d52affa7 | Gerd Hoffmann | s->scsi_dev->info->send_command(s->scsi_dev, s->tag, cbw.cmd, 0);
|
399 | a917d384 | pbrook | /* ??? Should check that USB and SCSI data transfer
|
400 | a917d384 | pbrook | directions match. */
|
401 | a917d384 | pbrook | if (s->residue == 0) { |
402 | a917d384 | pbrook | if (s->mode == USB_MSDM_DATAIN) {
|
403 | d52affa7 | Gerd Hoffmann | s->scsi_dev->info->read_data(s->scsi_dev, s->tag); |
404 | a917d384 | pbrook | } else if (s->mode == USB_MSDM_DATAOUT) { |
405 | d52affa7 | Gerd Hoffmann | s->scsi_dev->info->write_data(s->scsi_dev, s->tag); |
406 | a917d384 | pbrook | } |
407 | a917d384 | pbrook | } |
408 | 2e5d83bb | pbrook | ret = len; |
409 | 2e5d83bb | pbrook | break;
|
410 | 2e5d83bb | pbrook | |
411 | 2e5d83bb | pbrook | case USB_MSDM_DATAOUT:
|
412 | 2e5d83bb | pbrook | DPRINTF("Data out %d/%d\n", len, s->data_len);
|
413 | 2e5d83bb | pbrook | if (len > s->data_len)
|
414 | 2e5d83bb | pbrook | goto fail;
|
415 | 2e5d83bb | pbrook | |
416 | a917d384 | pbrook | s->usb_buf = data; |
417 | a917d384 | pbrook | s->usb_len = len; |
418 | a917d384 | pbrook | if (s->scsi_len) {
|
419 | a917d384 | pbrook | usb_msd_copy_data(s); |
420 | a917d384 | pbrook | } |
421 | a917d384 | pbrook | if (s->residue && s->usb_len) {
|
422 | a917d384 | pbrook | s->data_len -= s->usb_len; |
423 | a917d384 | pbrook | if (s->data_len == 0) |
424 | a917d384 | pbrook | s->mode = USB_MSDM_CSW; |
425 | a917d384 | pbrook | s->usb_len = 0;
|
426 | a917d384 | pbrook | } |
427 | a917d384 | pbrook | if (s->usb_len) {
|
428 | 4d611c9a | pbrook | DPRINTF("Deferring packet %p\n", p);
|
429 | 4d611c9a | pbrook | usb_defer_packet(p, usb_msd_cancel_io, s); |
430 | 4d611c9a | pbrook | s->packet = p; |
431 | 4d611c9a | pbrook | ret = USB_RET_ASYNC; |
432 | a917d384 | pbrook | } else {
|
433 | a917d384 | pbrook | ret = len; |
434 | 4d611c9a | pbrook | } |
435 | 2e5d83bb | pbrook | break;
|
436 | 2e5d83bb | pbrook | |
437 | 2e5d83bb | pbrook | default:
|
438 | 2e5d83bb | pbrook | DPRINTF("Unexpected write (len %d)\n", len);
|
439 | 2e5d83bb | pbrook | goto fail;
|
440 | 2e5d83bb | pbrook | } |
441 | 2e5d83bb | pbrook | break;
|
442 | 2e5d83bb | pbrook | |
443 | 2e5d83bb | pbrook | case USB_TOKEN_IN:
|
444 | 2e5d83bb | pbrook | if (devep != 1) |
445 | 2e5d83bb | pbrook | goto fail;
|
446 | 2e5d83bb | pbrook | |
447 | 2e5d83bb | pbrook | switch (s->mode) {
|
448 | a917d384 | pbrook | case USB_MSDM_DATAOUT:
|
449 | a917d384 | pbrook | if (s->data_len != 0 || len < 13) |
450 | a917d384 | pbrook | goto fail;
|
451 | a917d384 | pbrook | /* Waiting for SCSI write to complete. */
|
452 | a917d384 | pbrook | usb_defer_packet(p, usb_msd_cancel_io, s); |
453 | a917d384 | pbrook | s->packet = p; |
454 | a917d384 | pbrook | ret = USB_RET_ASYNC; |
455 | a917d384 | pbrook | break;
|
456 | a917d384 | pbrook | |
457 | 2e5d83bb | pbrook | case USB_MSDM_CSW:
|
458 | 2e5d83bb | pbrook | DPRINTF("Command status %d tag 0x%x, len %d\n",
|
459 | 2e5d83bb | pbrook | s->result, s->tag, len); |
460 | 2e5d83bb | pbrook | if (len < 13) |
461 | 2e5d83bb | pbrook | goto fail;
|
462 | 2e5d83bb | pbrook | |
463 | a917d384 | pbrook | s->usb_len = len; |
464 | a917d384 | pbrook | s->usb_buf = data; |
465 | a917d384 | pbrook | usb_msd_send_status(s); |
466 | 2e5d83bb | pbrook | s->mode = USB_MSDM_CBW; |
467 | a917d384 | pbrook | ret = 13;
|
468 | 2e5d83bb | pbrook | break;
|
469 | 2e5d83bb | pbrook | |
470 | 2e5d83bb | pbrook | case USB_MSDM_DATAIN:
|
471 | 2e5d83bb | pbrook | DPRINTF("Data in %d/%d\n", len, s->data_len);
|
472 | 2e5d83bb | pbrook | if (len > s->data_len)
|
473 | 2e5d83bb | pbrook | len = s->data_len; |
474 | a917d384 | pbrook | s->usb_buf = data; |
475 | a917d384 | pbrook | s->usb_len = len; |
476 | a917d384 | pbrook | if (s->scsi_len) {
|
477 | a917d384 | pbrook | usb_msd_copy_data(s); |
478 | a917d384 | pbrook | } |
479 | a917d384 | pbrook | if (s->residue && s->usb_len) {
|
480 | a917d384 | pbrook | s->data_len -= s->usb_len; |
481 | a917d384 | pbrook | memset(s->usb_buf, 0, s->usb_len);
|
482 | a917d384 | pbrook | if (s->data_len == 0) |
483 | a917d384 | pbrook | s->mode = USB_MSDM_CSW; |
484 | a917d384 | pbrook | s->usb_len = 0;
|
485 | a917d384 | pbrook | } |
486 | a917d384 | pbrook | if (s->usb_len) {
|
487 | 4d611c9a | pbrook | DPRINTF("Deferring packet %p\n", p);
|
488 | 4d611c9a | pbrook | usb_defer_packet(p, usb_msd_cancel_io, s); |
489 | 4d611c9a | pbrook | s->packet = p; |
490 | 4d611c9a | pbrook | ret = USB_RET_ASYNC; |
491 | a917d384 | pbrook | } else {
|
492 | a917d384 | pbrook | ret = len; |
493 | 4d611c9a | pbrook | } |
494 | 2e5d83bb | pbrook | break;
|
495 | 2e5d83bb | pbrook | |
496 | 2e5d83bb | pbrook | default:
|
497 | 2e5d83bb | pbrook | DPRINTF("Unexpected read (len %d)\n", len);
|
498 | 2e5d83bb | pbrook | goto fail;
|
499 | 2e5d83bb | pbrook | } |
500 | 2e5d83bb | pbrook | break;
|
501 | 2e5d83bb | pbrook | |
502 | 2e5d83bb | pbrook | default:
|
503 | 2e5d83bb | pbrook | DPRINTF("Bad token\n");
|
504 | 2e5d83bb | pbrook | fail:
|
505 | 2e5d83bb | pbrook | ret = USB_RET_STALL; |
506 | 2e5d83bb | pbrook | break;
|
507 | 2e5d83bb | pbrook | } |
508 | 2e5d83bb | pbrook | |
509 | 2e5d83bb | pbrook | return ret;
|
510 | 2e5d83bb | pbrook | } |
511 | 2e5d83bb | pbrook | |
512 | b3e461d3 | Gerd Hoffmann | static void usb_msd_password_cb(void *opaque, int err) |
513 | b3e461d3 | Gerd Hoffmann | { |
514 | b3e461d3 | Gerd Hoffmann | MSDState *s = opaque; |
515 | b3e461d3 | Gerd Hoffmann | |
516 | b3e461d3 | Gerd Hoffmann | if (!err)
|
517 | b3e461d3 | Gerd Hoffmann | usb_device_attach(&s->dev); |
518 | b3e461d3 | Gerd Hoffmann | else
|
519 | b3e461d3 | Gerd Hoffmann | qdev_unplug(&s->dev.qdev); |
520 | b3e461d3 | Gerd Hoffmann | } |
521 | b3e461d3 | Gerd Hoffmann | |
522 | 806b6024 | Gerd Hoffmann | static int usb_msd_initfn(USBDevice *dev) |
523 | 806b6024 | Gerd Hoffmann | { |
524 | 806b6024 | Gerd Hoffmann | MSDState *s = DO_UPCAST(MSDState, dev, dev); |
525 | 806b6024 | Gerd Hoffmann | |
526 | 7fc2f2c0 | Gerd Hoffmann | if (!s->dinfo || !s->dinfo->bdrv) {
|
527 | 7fc2f2c0 | Gerd Hoffmann | qemu_error("usb-msd: drive property not set\n");
|
528 | 7fc2f2c0 | Gerd Hoffmann | return -1; |
529 | 7fc2f2c0 | Gerd Hoffmann | } |
530 | 7fc2f2c0 | Gerd Hoffmann | |
531 | 806b6024 | Gerd Hoffmann | s->dev.speed = USB_SPEED_FULL; |
532 | ca9c39fa | Gerd Hoffmann | scsi_bus_new(&s->bus, &s->dev.qdev, 0, 1, usb_msd_command_complete); |
533 | ca9c39fa | Gerd Hoffmann | s->scsi_dev = scsi_bus_legacy_add_drive(&s->bus, s->dinfo, 0);
|
534 | cb23117b | Gerd Hoffmann | s->bus.qbus.allow_hotplug = 0;
|
535 | 7fc2f2c0 | Gerd Hoffmann | usb_msd_handle_reset(dev); |
536 | b3e461d3 | Gerd Hoffmann | |
537 | b3e461d3 | Gerd Hoffmann | if (bdrv_key_required(s->dinfo->bdrv)) {
|
538 | b3e461d3 | Gerd Hoffmann | if (s->dev.qdev.hotplugged) {
|
539 | b3e461d3 | Gerd Hoffmann | monitor_read_bdrv_key_start(cur_mon, s->dinfo->bdrv, |
540 | b3e461d3 | Gerd Hoffmann | usb_msd_password_cb, s); |
541 | b3e461d3 | Gerd Hoffmann | s->dev.auto_attach = 0;
|
542 | b3e461d3 | Gerd Hoffmann | } else {
|
543 | b3e461d3 | Gerd Hoffmann | autostart = 0;
|
544 | b3e461d3 | Gerd Hoffmann | } |
545 | b3e461d3 | Gerd Hoffmann | } |
546 | b3e461d3 | Gerd Hoffmann | |
547 | 806b6024 | Gerd Hoffmann | return 0; |
548 | 806b6024 | Gerd Hoffmann | } |
549 | 806b6024 | Gerd Hoffmann | |
550 | b3e461d3 | Gerd Hoffmann | static USBDevice *usb_msd_init(const char *filename) |
551 | 2e5d83bb | pbrook | { |
552 | 7fc2f2c0 | Gerd Hoffmann | static int nr=0; |
553 | 7fc2f2c0 | Gerd Hoffmann | char id[8]; |
554 | 7fc2f2c0 | Gerd Hoffmann | QemuOpts *opts; |
555 | 7fc2f2c0 | Gerd Hoffmann | DriveInfo *dinfo; |
556 | 806b6024 | Gerd Hoffmann | USBDevice *dev; |
557 | 7fc2f2c0 | Gerd Hoffmann | int fatal_error;
|
558 | 334c0241 | aurel32 | const char *p1; |
559 | 334c0241 | aurel32 | char fmt[32]; |
560 | 334c0241 | aurel32 | |
561 | 7fc2f2c0 | Gerd Hoffmann | /* parse -usbdevice disk: syntax into drive opts */
|
562 | 7fc2f2c0 | Gerd Hoffmann | snprintf(id, sizeof(id), "usb%d", nr++); |
563 | 7fc2f2c0 | Gerd Hoffmann | opts = qemu_opts_create(&qemu_drive_opts, id, 0);
|
564 | 7fc2f2c0 | Gerd Hoffmann | |
565 | 334c0241 | aurel32 | p1 = strchr(filename, ':');
|
566 | 334c0241 | aurel32 | if (p1++) {
|
567 | 334c0241 | aurel32 | const char *p2; |
568 | 334c0241 | aurel32 | |
569 | 334c0241 | aurel32 | if (strstart(filename, "format=", &p2)) { |
570 | 334c0241 | aurel32 | int len = MIN(p1 - p2, sizeof(fmt)); |
571 | 334c0241 | aurel32 | pstrcpy(fmt, len, p2); |
572 | 7fc2f2c0 | Gerd Hoffmann | qemu_opt_set(opts, "format", fmt);
|
573 | 334c0241 | aurel32 | } else if (*filename != ':') { |
574 | 334c0241 | aurel32 | printf("unrecognized USB mass-storage option %s\n", filename);
|
575 | 334c0241 | aurel32 | return NULL; |
576 | 334c0241 | aurel32 | } |
577 | 334c0241 | aurel32 | filename = p1; |
578 | 334c0241 | aurel32 | } |
579 | 334c0241 | aurel32 | if (!*filename) {
|
580 | 334c0241 | aurel32 | printf("block device specification needed\n");
|
581 | 334c0241 | aurel32 | return NULL; |
582 | 334c0241 | aurel32 | } |
583 | 7fc2f2c0 | Gerd Hoffmann | qemu_opt_set(opts, "file", filename);
|
584 | 7fc2f2c0 | Gerd Hoffmann | qemu_opt_set(opts, "if", "none"); |
585 | 2e5d83bb | pbrook | |
586 | 7fc2f2c0 | Gerd Hoffmann | /* create host drive */
|
587 | 7fc2f2c0 | Gerd Hoffmann | dinfo = drive_init(opts, NULL, &fatal_error);
|
588 | 7fc2f2c0 | Gerd Hoffmann | if (!dinfo) {
|
589 | 7fc2f2c0 | Gerd Hoffmann | qemu_opts_del(opts); |
590 | 806b6024 | Gerd Hoffmann | return NULL; |
591 | 7fc2f2c0 | Gerd Hoffmann | } |
592 | 2e5d83bb | pbrook | |
593 | 7fc2f2c0 | Gerd Hoffmann | /* create guest device */
|
594 | 7fc2f2c0 | Gerd Hoffmann | dev = usb_create(NULL /* FIXME */, "QEMU USB MSD"); |
595 | 7fc2f2c0 | Gerd Hoffmann | qdev_prop_set_drive(&dev->qdev, "drive", dinfo);
|
596 | 33e66b86 | Markus Armbruster | if (qdev_init(&dev->qdev) < 0) |
597 | 33e66b86 | Markus Armbruster | return NULL; |
598 | 1f6e24e7 | bellard | |
599 | 7fc2f2c0 | Gerd Hoffmann | return dev;
|
600 | 2e5d83bb | pbrook | } |
601 | bb5fc20f | aliguori | |
602 | 806b6024 | Gerd Hoffmann | static struct USBDeviceInfo msd_info = { |
603 | 806b6024 | Gerd Hoffmann | .qdev.name = "QEMU USB MSD",
|
604 | 7fc2f2c0 | Gerd Hoffmann | .qdev.alias = "usb-storage",
|
605 | 806b6024 | Gerd Hoffmann | .qdev.size = sizeof(MSDState),
|
606 | 806b6024 | Gerd Hoffmann | .init = usb_msd_initfn, |
607 | 806b6024 | Gerd Hoffmann | .handle_packet = usb_generic_handle_packet, |
608 | 806b6024 | Gerd Hoffmann | .handle_reset = usb_msd_handle_reset, |
609 | 806b6024 | Gerd Hoffmann | .handle_control = usb_msd_handle_control, |
610 | 806b6024 | Gerd Hoffmann | .handle_data = usb_msd_handle_data, |
611 | b3e461d3 | Gerd Hoffmann | .usbdevice_name = "disk",
|
612 | b3e461d3 | Gerd Hoffmann | .usbdevice_init = usb_msd_init, |
613 | 7fc2f2c0 | Gerd Hoffmann | .qdev.props = (Property[]) { |
614 | 7fc2f2c0 | Gerd Hoffmann | DEFINE_PROP_DRIVE("drive", MSDState, dinfo),
|
615 | 7fc2f2c0 | Gerd Hoffmann | DEFINE_PROP_END_OF_LIST(), |
616 | 7fc2f2c0 | Gerd Hoffmann | }, |
617 | 806b6024 | Gerd Hoffmann | }; |
618 | 806b6024 | Gerd Hoffmann | |
619 | 806b6024 | Gerd Hoffmann | static void usb_msd_register_devices(void) |
620 | 806b6024 | Gerd Hoffmann | { |
621 | 806b6024 | Gerd Hoffmann | usb_qdev_register(&msd_info); |
622 | 806b6024 | Gerd Hoffmann | } |
623 | 806b6024 | Gerd Hoffmann | device_init(usb_msd_register_devices) |