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