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