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