]>
Commit | Line | Data |
---|---|---|
bb36d470 FB |
1 | /* |
2 | * QEMU USB emulation | |
3 | * | |
4 | * Copyright (c) 2005 Fabrice Bellard | |
5fafdf24 | 5 | * |
89b9b79f AL |
6 | * 2008 Generic packet handler rewrite by Max Krasnyansky |
7 | * | |
bb36d470 FB |
8 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
9 | * of this software and associated documentation files (the "Software"), to deal | |
10 | * in the Software without restriction, including without limitation the rights | |
11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
12 | * copies of the Software, and to permit persons to whom the Software is | |
13 | * furnished to do so, subject to the following conditions: | |
14 | * | |
15 | * The above copyright notice and this permission notice shall be included in | |
16 | * all copies or substantial portions of the Software. | |
17 | * | |
18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
21 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
24 | * THE SOFTWARE. | |
25 | */ | |
e532b2e0 | 26 | #include "qemu/osdep.h" |
f1ae32a1 | 27 | #include "hw/usb.h" |
1de7afc9 | 28 | #include "qemu/iov.h" |
808aeb98 | 29 | #include "trace.h" |
bb36d470 | 30 | |
b791c3b3 GH |
31 | void usb_pick_speed(USBPort *port) |
32 | { | |
33 | static const int speeds[] = { | |
34 | USB_SPEED_SUPER, | |
35 | USB_SPEED_HIGH, | |
36 | USB_SPEED_FULL, | |
37 | USB_SPEED_LOW, | |
38 | }; | |
39 | USBDevice *udev = port->dev; | |
40 | int i; | |
41 | ||
42 | for (i = 0; i < ARRAY_SIZE(speeds); i++) { | |
43 | if ((udev->speedmask & (1 << speeds[i])) && | |
44 | (port->speedmask & (1 << speeds[i]))) { | |
45 | udev->speed = speeds[i]; | |
46 | return; | |
47 | } | |
48 | } | |
49 | } | |
50 | ||
891fb2cd | 51 | void usb_attach(USBPort *port) |
bb36d470 | 52 | { |
891fb2cd GH |
53 | USBDevice *dev = port->dev; |
54 | ||
55 | assert(dev != NULL); | |
56 | assert(dev->attached); | |
e0b8e72d | 57 | assert(dev->state == USB_STATE_NOTATTACHED); |
b791c3b3 | 58 | usb_pick_speed(port); |
891fb2cd | 59 | port->ops->attach(port); |
d1f8b536 GH |
60 | dev->state = USB_STATE_ATTACHED; |
61 | usb_device_handle_attach(dev); | |
891fb2cd GH |
62 | } |
63 | ||
64 | void usb_detach(USBPort *port) | |
65 | { | |
66 | USBDevice *dev = port->dev; | |
67 | ||
68 | assert(dev != NULL); | |
e0b8e72d | 69 | assert(dev->state != USB_STATE_NOTATTACHED); |
891fb2cd | 70 | port->ops->detach(port); |
d1f8b536 | 71 | dev->state = USB_STATE_NOTATTACHED; |
bb36d470 FB |
72 | } |
73 | ||
d28f4e2d | 74 | void usb_port_reset(USBPort *port) |
e0b8e72d GH |
75 | { |
76 | USBDevice *dev = port->dev; | |
77 | ||
78 | assert(dev != NULL); | |
79 | usb_detach(port); | |
80 | usb_attach(port); | |
d28f4e2d GH |
81 | usb_device_reset(dev); |
82 | } | |
83 | ||
84 | void usb_device_reset(USBDevice *dev) | |
85 | { | |
86 | if (dev == NULL || !dev->attached) { | |
87 | return; | |
88 | } | |
7ed46573 | 89 | usb_device_handle_reset(dev); |
d28f4e2d GH |
90 | dev->remote_wakeup = 0; |
91 | dev->addr = 0; | |
92 | dev->state = USB_STATE_DEFAULT; | |
e0b8e72d GH |
93 | } |
94 | ||
8550a02d | 95 | void usb_wakeup(USBEndpoint *ep, unsigned int stream) |
01eacab6 | 96 | { |
7567b51f | 97 | USBDevice *dev = ep->dev; |
37f32f0f | 98 | USBBus *bus = usb_bus_from_device(dev); |
7567b51f | 99 | |
26022652 GH |
100 | if (!qdev_hotplug) { |
101 | /* | |
102 | * This is machine init cold plug. No need to wakeup anyone, | |
103 | * all devices will be reset anyway. And trying to wakeup can | |
104 | * cause problems due to hitting uninitialized devices. | |
105 | */ | |
106 | return; | |
107 | } | |
01eacab6 | 108 | if (dev->remote_wakeup && dev->port && dev->port->ops->wakeup) { |
d47e59b8 | 109 | dev->port->ops->wakeup(dev->port); |
01eacab6 | 110 | } |
37f32f0f | 111 | if (bus->ops->wakeup_endpoint) { |
8550a02d | 112 | bus->ops->wakeup_endpoint(bus, ep, stream); |
37f32f0f | 113 | } |
01eacab6 GH |
114 | } |
115 | ||
bb36d470 | 116 | /**********************/ |
89b9b79f | 117 | |
bb36d470 FB |
118 | /* generic USB device helpers (you are not forced to use them when |
119 | writing your USB device driver, but they help handling the | |
5fafdf24 | 120 | protocol) |
bb36d470 FB |
121 | */ |
122 | ||
50b7963e HG |
123 | #define SETUP_STATE_IDLE 0 |
124 | #define SETUP_STATE_SETUP 1 | |
125 | #define SETUP_STATE_DATA 2 | |
126 | #define SETUP_STATE_ACK 3 | |
1b4b29a1 | 127 | #define SETUP_STATE_PARAM 4 |
bb36d470 | 128 | |
9a77a0f5 | 129 | static void do_token_setup(USBDevice *s, USBPacket *p) |
89b9b79f AL |
130 | { |
131 | int request, value, index; | |
89b9b79f | 132 | |
4f4321c1 | 133 | if (p->iov.size != 8) { |
9a77a0f5 HG |
134 | p->status = USB_RET_STALL; |
135 | return; | |
4f4321c1 GH |
136 | } |
137 | ||
138 | usb_packet_copy(p, s->setup_buf, p->iov.size); | |
64c9bc18 | 139 | s->setup_index = 0; |
9a77a0f5 | 140 | p->actual_length = 0; |
89b9b79f | 141 | s->setup_len = (s->setup_buf[7] << 8) | s->setup_buf[6]; |
64c9bc18 PP |
142 | if (s->setup_len > sizeof(s->data_buf)) { |
143 | fprintf(stderr, | |
144 | "usb_generic_handle_packet: ctrl buffer too small (%d > %zu)\n", | |
145 | s->setup_len, sizeof(s->data_buf)); | |
146 | p->status = USB_RET_STALL; | |
147 | return; | |
148 | } | |
89b9b79f AL |
149 | |
150 | request = (s->setup_buf[0] << 8) | s->setup_buf[1]; | |
151 | value = (s->setup_buf[3] << 8) | s->setup_buf[2]; | |
152 | index = (s->setup_buf[5] << 8) | s->setup_buf[4]; | |
007fd62f | 153 | |
89b9b79f | 154 | if (s->setup_buf[0] & USB_DIR_IN) { |
9a77a0f5 HG |
155 | usb_device_handle_control(s, p, request, value, index, |
156 | s->setup_len, s->data_buf); | |
157 | if (p->status == USB_RET_ASYNC) { | |
158 | s->setup_state = SETUP_STATE_SETUP; | |
159 | } | |
160 | if (p->status != USB_RET_SUCCESS) { | |
161 | return; | |
50b7963e | 162 | } |
89b9b79f | 163 | |
9a77a0f5 HG |
164 | if (p->actual_length < s->setup_len) { |
165 | s->setup_len = p->actual_length; | |
166 | } | |
89b9b79f AL |
167 | s->setup_state = SETUP_STATE_DATA; |
168 | } else { | |
169 | if (s->setup_len == 0) | |
170 | s->setup_state = SETUP_STATE_ACK; | |
171 | else | |
172 | s->setup_state = SETUP_STATE_DATA; | |
173 | } | |
174 | ||
9a77a0f5 | 175 | p->actual_length = 8; |
89b9b79f AL |
176 | } |
177 | ||
9a77a0f5 | 178 | static void do_token_in(USBDevice *s, USBPacket *p) |
bb36d470 | 179 | { |
89b9b79f | 180 | int request, value, index; |
89b9b79f | 181 | |
079d0b7f | 182 | assert(p->ep->nr == 0); |
89b9b79f AL |
183 | |
184 | request = (s->setup_buf[0] << 8) | s->setup_buf[1]; | |
185 | value = (s->setup_buf[3] << 8) | s->setup_buf[2]; | |
186 | index = (s->setup_buf[5] << 8) | s->setup_buf[4]; | |
64c9bc18 | 187 | |
89b9b79f AL |
188 | switch(s->setup_state) { |
189 | case SETUP_STATE_ACK: | |
190 | if (!(s->setup_buf[0] & USB_DIR_IN)) { | |
9a77a0f5 HG |
191 | usb_device_handle_control(s, p, request, value, index, |
192 | s->setup_len, s->data_buf); | |
193 | if (p->status == USB_RET_ASYNC) { | |
194 | return; | |
007fd62f HG |
195 | } |
196 | s->setup_state = SETUP_STATE_IDLE; | |
9a77a0f5 | 197 | p->actual_length = 0; |
89b9b79f | 198 | } |
9a77a0f5 | 199 | break; |
89b9b79f AL |
200 | |
201 | case SETUP_STATE_DATA: | |
202 | if (s->setup_buf[0] & USB_DIR_IN) { | |
203 | int len = s->setup_len - s->setup_index; | |
4f4321c1 GH |
204 | if (len > p->iov.size) { |
205 | len = p->iov.size; | |
206 | } | |
207 | usb_packet_copy(p, s->data_buf + s->setup_index, len); | |
89b9b79f | 208 | s->setup_index += len; |
9a77a0f5 | 209 | if (s->setup_index >= s->setup_len) { |
89b9b79f | 210 | s->setup_state = SETUP_STATE_ACK; |
9a77a0f5 HG |
211 | } |
212 | return; | |
89b9b79f | 213 | } |
89b9b79f | 214 | s->setup_state = SETUP_STATE_IDLE; |
9a77a0f5 HG |
215 | p->status = USB_RET_STALL; |
216 | break; | |
89b9b79f AL |
217 | |
218 | default: | |
9a77a0f5 | 219 | p->status = USB_RET_STALL; |
89b9b79f AL |
220 | } |
221 | } | |
222 | ||
9a77a0f5 | 223 | static void do_token_out(USBDevice *s, USBPacket *p) |
89b9b79f | 224 | { |
079d0b7f | 225 | assert(p->ep->nr == 0); |
89b9b79f AL |
226 | |
227 | switch(s->setup_state) { | |
228 | case SETUP_STATE_ACK: | |
229 | if (s->setup_buf[0] & USB_DIR_IN) { | |
230 | s->setup_state = SETUP_STATE_IDLE; | |
231 | /* transfer OK */ | |
232 | } else { | |
233 | /* ignore additional output */ | |
234 | } | |
9a77a0f5 | 235 | break; |
89b9b79f AL |
236 | |
237 | case SETUP_STATE_DATA: | |
238 | if (!(s->setup_buf[0] & USB_DIR_IN)) { | |
239 | int len = s->setup_len - s->setup_index; | |
4f4321c1 GH |
240 | if (len > p->iov.size) { |
241 | len = p->iov.size; | |
242 | } | |
243 | usb_packet_copy(p, s->data_buf + s->setup_index, len); | |
89b9b79f | 244 | s->setup_index += len; |
9a77a0f5 | 245 | if (s->setup_index >= s->setup_len) { |
89b9b79f | 246 | s->setup_state = SETUP_STATE_ACK; |
9a77a0f5 HG |
247 | } |
248 | return; | |
89b9b79f | 249 | } |
89b9b79f | 250 | s->setup_state = SETUP_STATE_IDLE; |
9a77a0f5 HG |
251 | p->status = USB_RET_STALL; |
252 | break; | |
89b9b79f AL |
253 | |
254 | default: | |
9a77a0f5 | 255 | p->status = USB_RET_STALL; |
89b9b79f AL |
256 | } |
257 | } | |
bb36d470 | 258 | |
9a77a0f5 | 259 | static void do_parameter(USBDevice *s, USBPacket *p) |
1b4b29a1 | 260 | { |
9a77a0f5 | 261 | int i, request, value, index; |
1b4b29a1 GH |
262 | |
263 | for (i = 0; i < 8; i++) { | |
264 | s->setup_buf[i] = p->parameter >> (i*8); | |
265 | } | |
266 | ||
267 | s->setup_state = SETUP_STATE_PARAM; | |
268 | s->setup_len = (s->setup_buf[7] << 8) | s->setup_buf[6]; | |
269 | s->setup_index = 0; | |
270 | ||
271 | request = (s->setup_buf[0] << 8) | s->setup_buf[1]; | |
272 | value = (s->setup_buf[3] << 8) | s->setup_buf[2]; | |
273 | index = (s->setup_buf[5] << 8) | s->setup_buf[4]; | |
274 | ||
275 | if (s->setup_len > sizeof(s->data_buf)) { | |
276 | fprintf(stderr, | |
277 | "usb_generic_handle_packet: ctrl buffer too small (%d > %zu)\n", | |
278 | s->setup_len, sizeof(s->data_buf)); | |
9a77a0f5 HG |
279 | p->status = USB_RET_STALL; |
280 | return; | |
1b4b29a1 GH |
281 | } |
282 | ||
283 | if (p->pid == USB_TOKEN_OUT) { | |
284 | usb_packet_copy(p, s->data_buf, s->setup_len); | |
285 | } | |
286 | ||
9a77a0f5 HG |
287 | usb_device_handle_control(s, p, request, value, index, |
288 | s->setup_len, s->data_buf); | |
289 | if (p->status == USB_RET_ASYNC) { | |
290 | return; | |
1b4b29a1 GH |
291 | } |
292 | ||
9a77a0f5 HG |
293 | if (p->actual_length < s->setup_len) { |
294 | s->setup_len = p->actual_length; | |
1b4b29a1 GH |
295 | } |
296 | if (p->pid == USB_TOKEN_IN) { | |
9a77a0f5 | 297 | p->actual_length = 0; |
1b4b29a1 GH |
298 | usb_packet_copy(p, s->data_buf, s->setup_len); |
299 | } | |
1b4b29a1 GH |
300 | } |
301 | ||
50b7963e HG |
302 | /* ctrl complete function for devices which use usb_generic_handle_packet and |
303 | may return USB_RET_ASYNC from their handle_control callback. Device code | |
304 | which does this *must* call this function instead of the normal | |
305 | usb_packet_complete to complete their async control packets. */ | |
306 | void usb_generic_async_ctrl_complete(USBDevice *s, USBPacket *p) | |
307 | { | |
9a77a0f5 | 308 | if (p->status < 0) { |
50b7963e HG |
309 | s->setup_state = SETUP_STATE_IDLE; |
310 | } | |
311 | ||
312 | switch (s->setup_state) { | |
313 | case SETUP_STATE_SETUP: | |
9a77a0f5 HG |
314 | if (p->actual_length < s->setup_len) { |
315 | s->setup_len = p->actual_length; | |
50b7963e HG |
316 | } |
317 | s->setup_state = SETUP_STATE_DATA; | |
9a77a0f5 | 318 | p->actual_length = 8; |
50b7963e HG |
319 | break; |
320 | ||
321 | case SETUP_STATE_ACK: | |
322 | s->setup_state = SETUP_STATE_IDLE; | |
9a77a0f5 | 323 | p->actual_length = 0; |
50b7963e HG |
324 | break; |
325 | ||
1b4b29a1 | 326 | case SETUP_STATE_PARAM: |
9a77a0f5 HG |
327 | if (p->actual_length < s->setup_len) { |
328 | s->setup_len = p->actual_length; | |
1b4b29a1 GH |
329 | } |
330 | if (p->pid == USB_TOKEN_IN) { | |
9a77a0f5 | 331 | p->actual_length = 0; |
1b4b29a1 GH |
332 | usb_packet_copy(p, s->data_buf, s->setup_len); |
333 | } | |
334 | break; | |
335 | ||
50b7963e HG |
336 | default: |
337 | break; | |
338 | } | |
339 | usb_packet_complete(s, p); | |
340 | } | |
341 | ||
73796fe6 GH |
342 | USBDevice *usb_find_device(USBPort *port, uint8_t addr) |
343 | { | |
344 | USBDevice *dev = port->dev; | |
345 | ||
346 | if (dev == NULL || !dev->attached || dev->state != USB_STATE_DEFAULT) { | |
347 | return NULL; | |
348 | } | |
349 | if (dev->addr == addr) { | |
350 | return dev; | |
351 | } | |
352 | return usb_device_find_device(dev, addr); | |
353 | } | |
354 | ||
9a77a0f5 | 355 | static void usb_process_one(USBPacket *p) |
db4be873 GH |
356 | { |
357 | USBDevice *dev = p->ep->dev; | |
358 | ||
9a77a0f5 HG |
359 | /* |
360 | * Handlers expect status to be initialized to USB_RET_SUCCESS, but it | |
361 | * can be USB_RET_NAK here from a previous usb_process_one() call, | |
362 | * or USB_RET_ASYNC from going through usb_queue_one(). | |
363 | */ | |
364 | p->status = USB_RET_SUCCESS; | |
365 | ||
db4be873 GH |
366 | if (p->ep->nr == 0) { |
367 | /* control pipe */ | |
1b4b29a1 | 368 | if (p->parameter) { |
9a77a0f5 HG |
369 | do_parameter(dev, p); |
370 | return; | |
1b4b29a1 | 371 | } |
db4be873 GH |
372 | switch (p->pid) { |
373 | case USB_TOKEN_SETUP: | |
9a77a0f5 HG |
374 | do_token_setup(dev, p); |
375 | break; | |
db4be873 | 376 | case USB_TOKEN_IN: |
9a77a0f5 HG |
377 | do_token_in(dev, p); |
378 | break; | |
db4be873 | 379 | case USB_TOKEN_OUT: |
9a77a0f5 HG |
380 | do_token_out(dev, p); |
381 | break; | |
db4be873 | 382 | default: |
9a77a0f5 | 383 | p->status = USB_RET_STALL; |
db4be873 GH |
384 | } |
385 | } else { | |
386 | /* data pipe */ | |
9a77a0f5 | 387 | usb_device_handle_data(dev, p); |
db4be873 GH |
388 | } |
389 | } | |
390 | ||
9a77a0f5 HG |
391 | static void usb_queue_one(USBPacket *p) |
392 | { | |
393 | usb_packet_set_state(p, USB_PACKET_QUEUED); | |
394 | QTAILQ_INSERT_TAIL(&p->ep->queue, p, queue); | |
395 | p->status = USB_RET_ASYNC; | |
396 | } | |
397 | ||
398 | /* Hand over a packet to a device for processing. p->status == | |
53aa8c0e GH |
399 | USB_RET_ASYNC indicates the processing isn't finished yet, the |
400 | driver will call usb_packet_complete() when done processing it. */ | |
9a77a0f5 | 401 | void usb_handle_packet(USBDevice *dev, USBPacket *p) |
53aa8c0e | 402 | { |
98861f51 | 403 | if (dev == NULL) { |
9a77a0f5 HG |
404 | p->status = USB_RET_NODEV; |
405 | return; | |
98861f51 | 406 | } |
079d0b7f | 407 | assert(dev == p->ep->dev); |
1977f93d | 408 | assert(dev->state == USB_STATE_DEFAULT); |
5ac2731c | 409 | usb_packet_check_state(p, USB_PACKET_SETUP); |
db4be873 | 410 | assert(p->ep != NULL); |
1977f93d | 411 | |
0132b4b6 HG |
412 | /* Submitting a new packet clears halt */ |
413 | if (p->ep->halted) { | |
414 | assert(QTAILQ_EMPTY(&p->ep->queue)); | |
415 | p->ep->halted = false; | |
416 | } | |
417 | ||
c96c41ed | 418 | if (QTAILQ_EMPTY(&p->ep->queue) || p->ep->pipeline || p->stream) { |
9a77a0f5 HG |
419 | usb_process_one(p); |
420 | if (p->status == USB_RET_ASYNC) { | |
be41efde | 421 | /* hcd drivers cannot handle async for isoc */ |
aaac7434 | 422 | assert(p->ep->type != USB_ENDPOINT_XFER_ISOC); |
be41efde HG |
423 | /* using async for interrupt packets breaks migration */ |
424 | assert(p->ep->type != USB_ENDPOINT_XFER_INT || | |
75633529 | 425 | (dev->flags & (1 << USB_DEV_FLAG_IS_HOST))); |
db4be873 GH |
426 | usb_packet_set_state(p, USB_PACKET_ASYNC); |
427 | QTAILQ_INSERT_TAIL(&p->ep->queue, p, queue); | |
9a77a0f5 HG |
428 | } else if (p->status == USB_RET_ADD_TO_QUEUE) { |
429 | usb_queue_one(p); | |
db4be873 | 430 | } else { |
0132b4b6 HG |
431 | /* |
432 | * When pipelining is enabled usb-devices must always return async, | |
433 | * otherwise packets can complete out of order! | |
434 | */ | |
c96c41ed GH |
435 | assert(p->stream || !p->ep->pipeline || |
436 | QTAILQ_EMPTY(&p->ep->queue)); | |
9a77a0f5 | 437 | if (p->status != USB_RET_NAK) { |
cc409974 HG |
438 | usb_packet_set_state(p, USB_PACKET_COMPLETE); |
439 | } | |
1977f93d GH |
440 | } |
441 | } else { | |
9a77a0f5 | 442 | usb_queue_one(p); |
4ff658fb | 443 | } |
89b9b79f | 444 | } |
4ff658fb | 445 | |
d0ff81b8 | 446 | void usb_packet_complete_one(USBDevice *dev, USBPacket *p) |
0132b4b6 HG |
447 | { |
448 | USBEndpoint *ep = p->ep; | |
449 | ||
c96c41ed | 450 | assert(p->stream || QTAILQ_FIRST(&ep->queue) == p); |
9a77a0f5 | 451 | assert(p->status != USB_RET_ASYNC && p->status != USB_RET_NAK); |
0132b4b6 | 452 | |
9a77a0f5 HG |
453 | if (p->status != USB_RET_SUCCESS || |
454 | (p->short_not_ok && (p->actual_length < p->iov.size))) { | |
0132b4b6 HG |
455 | ep->halted = true; |
456 | } | |
457 | usb_packet_set_state(p, USB_PACKET_COMPLETE); | |
458 | QTAILQ_REMOVE(&ep->queue, p, queue); | |
459 | dev->port->ops->complete(dev->port, p); | |
460 | } | |
461 | ||
4ff658fb GH |
462 | /* Notify the controller that an async packet is complete. This should only |
463 | be called for packets previously deferred by returning USB_RET_ASYNC from | |
464 | handle_packet. */ | |
465 | void usb_packet_complete(USBDevice *dev, USBPacket *p) | |
466 | { | |
db4be873 | 467 | USBEndpoint *ep = p->ep; |
db4be873 | 468 | |
5ac2731c | 469 | usb_packet_check_state(p, USB_PACKET_ASYNC); |
d0ff81b8 | 470 | usb_packet_complete_one(dev, p); |
db4be873 | 471 | |
0cae7b1a | 472 | while (!QTAILQ_EMPTY(&ep->queue)) { |
db4be873 | 473 | p = QTAILQ_FIRST(&ep->queue); |
0cae7b1a HG |
474 | if (ep->halted) { |
475 | /* Empty the queue on a halt */ | |
9a77a0f5 | 476 | p->status = USB_RET_REMOVE_FROM_QUEUE; |
0cae7b1a HG |
477 | dev->port->ops->complete(dev->port, p); |
478 | continue; | |
479 | } | |
eb9d4673 GH |
480 | if (p->state == USB_PACKET_ASYNC) { |
481 | break; | |
482 | } | |
5ac2731c | 483 | usb_packet_check_state(p, USB_PACKET_QUEUED); |
9a77a0f5 HG |
484 | usb_process_one(p); |
485 | if (p->status == USB_RET_ASYNC) { | |
db4be873 GH |
486 | usb_packet_set_state(p, USB_PACKET_ASYNC); |
487 | break; | |
488 | } | |
d0ff81b8 | 489 | usb_packet_complete_one(ep->dev, p); |
db4be873 | 490 | } |
4ff658fb GH |
491 | } |
492 | ||
493 | /* Cancel an active packet. The packed must have been deferred by | |
494 | returning USB_RET_ASYNC from handle_packet, and not yet | |
495 | completed. */ | |
496 | void usb_cancel_packet(USBPacket * p) | |
497 | { | |
db4be873 GH |
498 | bool callback = (p->state == USB_PACKET_ASYNC); |
499 | assert(usb_packet_is_inflight(p)); | |
500 | usb_packet_set_state(p, USB_PACKET_CANCELED); | |
501 | QTAILQ_REMOVE(&p->ep->queue, p, queue); | |
502 | if (callback) { | |
503 | usb_device_cancel_packet(p->ep->dev, p); | |
504 | } | |
4ff658fb | 505 | } |
4f4321c1 GH |
506 | |
507 | ||
508 | void usb_packet_init(USBPacket *p) | |
509 | { | |
510 | qemu_iovec_init(&p->iov, 1); | |
511 | } | |
512 | ||
5ac2731c | 513 | static const char *usb_packet_state_name(USBPacketState state) |
db4be873 | 514 | { |
db4be873 GH |
515 | static const char *name[] = { |
516 | [USB_PACKET_UNDEFINED] = "undef", | |
517 | [USB_PACKET_SETUP] = "setup", | |
518 | [USB_PACKET_QUEUED] = "queued", | |
519 | [USB_PACKET_ASYNC] = "async", | |
520 | [USB_PACKET_COMPLETE] = "complete", | |
521 | [USB_PACKET_CANCELED] = "canceled", | |
522 | }; | |
5ac2731c GH |
523 | if (state < ARRAY_SIZE(name)) { |
524 | return name[state]; | |
525 | } | |
526 | return "INVALID"; | |
527 | } | |
528 | ||
529 | void usb_packet_check_state(USBPacket *p, USBPacketState expected) | |
530 | { | |
531 | USBDevice *dev; | |
532 | USBBus *bus; | |
533 | ||
534 | if (p->state == expected) { | |
535 | return; | |
536 | } | |
537 | dev = p->ep->dev; | |
538 | bus = usb_bus_from_device(dev); | |
539 | trace_usb_packet_state_fault(bus->busnr, dev->port->path, p->ep->nr, p, | |
540 | usb_packet_state_name(p->state), | |
541 | usb_packet_state_name(expected)); | |
542 | assert(!"usb packet state check failed"); | |
543 | } | |
544 | ||
545 | void usb_packet_set_state(USBPacket *p, USBPacketState state) | |
546 | { | |
f5bf14bf GH |
547 | if (p->ep) { |
548 | USBDevice *dev = p->ep->dev; | |
549 | USBBus *bus = usb_bus_from_device(dev); | |
550 | trace_usb_packet_state_change(bus->busnr, dev->port->path, p->ep->nr, p, | |
551 | usb_packet_state_name(p->state), | |
552 | usb_packet_state_name(state)); | |
553 | } else { | |
554 | trace_usb_packet_state_change(-1, "", -1, p, | |
555 | usb_packet_state_name(p->state), | |
556 | usb_packet_state_name(state)); | |
557 | } | |
db4be873 GH |
558 | p->state = state; |
559 | } | |
560 | ||
8550a02d GH |
561 | void usb_packet_setup(USBPacket *p, int pid, |
562 | USBEndpoint *ep, unsigned int stream, | |
563 | uint64_t id, bool short_not_ok, bool int_req) | |
4f4321c1 | 564 | { |
f53c398a | 565 | assert(!usb_packet_is_inflight(p)); |
0cc6a0f1 | 566 | assert(p->iov.iov != NULL); |
e983395d | 567 | p->id = id; |
4f4321c1 | 568 | p->pid = pid; |
079d0b7f | 569 | p->ep = ep; |
8550a02d | 570 | p->stream = stream; |
9a77a0f5 HG |
571 | p->status = USB_RET_SUCCESS; |
572 | p->actual_length = 0; | |
1b4b29a1 | 573 | p->parameter = 0; |
6ba43f1f | 574 | p->short_not_ok = short_not_ok; |
a6fb2ddb | 575 | p->int_req = int_req; |
a552a966 | 576 | p->combined = NULL; |
4f4321c1 | 577 | qemu_iovec_reset(&p->iov); |
db4be873 | 578 | usb_packet_set_state(p, USB_PACKET_SETUP); |
4f4321c1 GH |
579 | } |
580 | ||
581 | void usb_packet_addbuf(USBPacket *p, void *ptr, size_t len) | |
582 | { | |
583 | qemu_iovec_add(&p->iov, ptr, len); | |
584 | } | |
585 | ||
586 | void usb_packet_copy(USBPacket *p, void *ptr, size_t bytes) | |
587 | { | |
6a98d1c0 GH |
588 | QEMUIOVector *iov = p->combined ? &p->combined->iov : &p->iov; |
589 | ||
9a77a0f5 | 590 | assert(p->actual_length >= 0); |
6a98d1c0 | 591 | assert(p->actual_length + bytes <= iov->size); |
4f4321c1 GH |
592 | switch (p->pid) { |
593 | case USB_TOKEN_SETUP: | |
594 | case USB_TOKEN_OUT: | |
6a98d1c0 | 595 | iov_to_buf(iov->iov, iov->niov, p->actual_length, ptr, bytes); |
4f4321c1 GH |
596 | break; |
597 | case USB_TOKEN_IN: | |
6a98d1c0 | 598 | iov_from_buf(iov->iov, iov->niov, p->actual_length, ptr, bytes); |
4f4321c1 GH |
599 | break; |
600 | default: | |
601 | fprintf(stderr, "%s: invalid pid: %x\n", __func__, p->pid); | |
602 | abort(); | |
603 | } | |
9a77a0f5 | 604 | p->actual_length += bytes; |
4f4321c1 GH |
605 | } |
606 | ||
607 | void usb_packet_skip(USBPacket *p, size_t bytes) | |
608 | { | |
6a98d1c0 GH |
609 | QEMUIOVector *iov = p->combined ? &p->combined->iov : &p->iov; |
610 | ||
9a77a0f5 | 611 | assert(p->actual_length >= 0); |
6a98d1c0 | 612 | assert(p->actual_length + bytes <= iov->size); |
4f4321c1 | 613 | if (p->pid == USB_TOKEN_IN) { |
6a98d1c0 | 614 | iov_memset(iov->iov, iov->niov, p->actual_length, 0, bytes); |
4f4321c1 | 615 | } |
9a77a0f5 | 616 | p->actual_length += bytes; |
4f4321c1 GH |
617 | } |
618 | ||
6a98d1c0 GH |
619 | size_t usb_packet_size(USBPacket *p) |
620 | { | |
621 | return p->combined ? p->combined->iov.size : p->iov.size; | |
622 | } | |
623 | ||
4f4321c1 GH |
624 | void usb_packet_cleanup(USBPacket *p) |
625 | { | |
f53c398a | 626 | assert(!usb_packet_is_inflight(p)); |
4f4321c1 GH |
627 | qemu_iovec_destroy(&p->iov); |
628 | } | |
d8e17efd | 629 | |
19deaa08 | 630 | void usb_ep_reset(USBDevice *dev) |
d8e17efd GH |
631 | { |
632 | int ep; | |
633 | ||
63095ab5 | 634 | dev->ep_ctl.nr = 0; |
25d5de7d GH |
635 | dev->ep_ctl.type = USB_ENDPOINT_XFER_CONTROL; |
636 | dev->ep_ctl.ifnum = 0; | |
9adbaad3 | 637 | dev->ep_ctl.max_packet_size = 64; |
04b300f8 | 638 | dev->ep_ctl.max_streams = 0; |
25d5de7d | 639 | dev->ep_ctl.dev = dev; |
7936e0f0 | 640 | dev->ep_ctl.pipeline = false; |
d8e17efd | 641 | for (ep = 0; ep < USB_MAX_ENDPOINTS; ep++) { |
63095ab5 GH |
642 | dev->ep_in[ep].nr = ep + 1; |
643 | dev->ep_out[ep].nr = ep + 1; | |
644 | dev->ep_in[ep].pid = USB_TOKEN_IN; | |
645 | dev->ep_out[ep].pid = USB_TOKEN_OUT; | |
d8e17efd GH |
646 | dev->ep_in[ep].type = USB_ENDPOINT_XFER_INVALID; |
647 | dev->ep_out[ep].type = USB_ENDPOINT_XFER_INVALID; | |
7c37e6a4 GH |
648 | dev->ep_in[ep].ifnum = USB_INTERFACE_INVALID; |
649 | dev->ep_out[ep].ifnum = USB_INTERFACE_INVALID; | |
9adbaad3 HG |
650 | dev->ep_in[ep].max_packet_size = 0; |
651 | dev->ep_out[ep].max_packet_size = 0; | |
04b300f8 HG |
652 | dev->ep_in[ep].max_streams = 0; |
653 | dev->ep_out[ep].max_streams = 0; | |
25d5de7d GH |
654 | dev->ep_in[ep].dev = dev; |
655 | dev->ep_out[ep].dev = dev; | |
7936e0f0 GH |
656 | dev->ep_in[ep].pipeline = false; |
657 | dev->ep_out[ep].pipeline = false; | |
19deaa08 GH |
658 | } |
659 | } | |
660 | ||
661 | void usb_ep_init(USBDevice *dev) | |
662 | { | |
663 | int ep; | |
664 | ||
665 | usb_ep_reset(dev); | |
666 | QTAILQ_INIT(&dev->ep_ctl.queue); | |
667 | for (ep = 0; ep < USB_MAX_ENDPOINTS; ep++) { | |
db4be873 GH |
668 | QTAILQ_INIT(&dev->ep_in[ep].queue); |
669 | QTAILQ_INIT(&dev->ep_out[ep].queue); | |
d8e17efd GH |
670 | } |
671 | } | |
672 | ||
5b6780d0 GH |
673 | void usb_ep_dump(USBDevice *dev) |
674 | { | |
675 | static const char *tname[] = { | |
676 | [USB_ENDPOINT_XFER_CONTROL] = "control", | |
677 | [USB_ENDPOINT_XFER_ISOC] = "isoc", | |
678 | [USB_ENDPOINT_XFER_BULK] = "bulk", | |
679 | [USB_ENDPOINT_XFER_INT] = "int", | |
680 | }; | |
681 | int ifnum, ep, first; | |
682 | ||
683 | fprintf(stderr, "Device \"%s\", config %d\n", | |
684 | dev->product_desc, dev->configuration); | |
685 | for (ifnum = 0; ifnum < 16; ifnum++) { | |
686 | first = 1; | |
687 | for (ep = 0; ep < USB_MAX_ENDPOINTS; ep++) { | |
688 | if (dev->ep_in[ep].type != USB_ENDPOINT_XFER_INVALID && | |
689 | dev->ep_in[ep].ifnum == ifnum) { | |
690 | if (first) { | |
691 | first = 0; | |
692 | fprintf(stderr, " Interface %d, alternative %d\n", | |
693 | ifnum, dev->altsetting[ifnum]); | |
694 | } | |
f003397c GH |
695 | fprintf(stderr, " Endpoint %d, IN, %s, %d max\n", ep, |
696 | tname[dev->ep_in[ep].type], | |
697 | dev->ep_in[ep].max_packet_size); | |
5b6780d0 GH |
698 | } |
699 | if (dev->ep_out[ep].type != USB_ENDPOINT_XFER_INVALID && | |
700 | dev->ep_out[ep].ifnum == ifnum) { | |
701 | if (first) { | |
702 | first = 0; | |
703 | fprintf(stderr, " Interface %d, alternative %d\n", | |
704 | ifnum, dev->altsetting[ifnum]); | |
705 | } | |
f003397c GH |
706 | fprintf(stderr, " Endpoint %d, OUT, %s, %d max\n", ep, |
707 | tname[dev->ep_out[ep].type], | |
708 | dev->ep_out[ep].max_packet_size); | |
5b6780d0 GH |
709 | } |
710 | } | |
711 | } | |
712 | fprintf(stderr, "--\n"); | |
713 | } | |
714 | ||
d8e17efd GH |
715 | struct USBEndpoint *usb_ep_get(USBDevice *dev, int pid, int ep) |
716 | { | |
079d0b7f GH |
717 | struct USBEndpoint *eps; |
718 | ||
7011baec | 719 | assert(dev != NULL); |
25d5de7d GH |
720 | if (ep == 0) { |
721 | return &dev->ep_ctl; | |
722 | } | |
d8e17efd GH |
723 | assert(pid == USB_TOKEN_IN || pid == USB_TOKEN_OUT); |
724 | assert(ep > 0 && ep <= USB_MAX_ENDPOINTS); | |
56090d78 | 725 | eps = (pid == USB_TOKEN_IN) ? dev->ep_in : dev->ep_out; |
d8e17efd GH |
726 | return eps + ep - 1; |
727 | } | |
728 | ||
729 | uint8_t usb_ep_get_type(USBDevice *dev, int pid, int ep) | |
730 | { | |
731 | struct USBEndpoint *uep = usb_ep_get(dev, pid, ep); | |
732 | return uep->type; | |
733 | } | |
734 | ||
735 | void usb_ep_set_type(USBDevice *dev, int pid, int ep, uint8_t type) | |
736 | { | |
737 | struct USBEndpoint *uep = usb_ep_get(dev, pid, ep); | |
738 | uep->type = type; | |
739 | } | |
82f02fe9 | 740 | |
82f02fe9 GH |
741 | void usb_ep_set_ifnum(USBDevice *dev, int pid, int ep, uint8_t ifnum) |
742 | { | |
743 | struct USBEndpoint *uep = usb_ep_get(dev, pid, ep); | |
744 | uep->ifnum = ifnum; | |
745 | } | |
f003397c GH |
746 | |
747 | void usb_ep_set_max_packet_size(USBDevice *dev, int pid, int ep, | |
748 | uint16_t raw) | |
749 | { | |
750 | struct USBEndpoint *uep = usb_ep_get(dev, pid, ep); | |
751 | int size, microframes; | |
752 | ||
753 | size = raw & 0x7ff; | |
754 | switch ((raw >> 11) & 3) { | |
755 | case 1: | |
756 | microframes = 2; | |
757 | break; | |
758 | case 2: | |
759 | microframes = 3; | |
760 | break; | |
761 | default: | |
762 | microframes = 1; | |
763 | break; | |
764 | } | |
765 | uep->max_packet_size = size * microframes; | |
766 | } | |
767 | ||
04b300f8 HG |
768 | void usb_ep_set_max_streams(USBDevice *dev, int pid, int ep, uint8_t raw) |
769 | { | |
770 | struct USBEndpoint *uep = usb_ep_get(dev, pid, ep); | |
771 | int MaxStreams; | |
772 | ||
773 | MaxStreams = raw & 0x1f; | |
774 | if (MaxStreams) { | |
775 | uep->max_streams = 1 << MaxStreams; | |
776 | } else { | |
777 | uep->max_streams = 0; | |
778 | } | |
779 | } | |
780 | ||
e382d966 GH |
781 | void usb_ep_set_halted(USBDevice *dev, int pid, int ep, bool halted) |
782 | { | |
783 | struct USBEndpoint *uep = usb_ep_get(dev, pid, ep); | |
784 | uep->halted = halted; | |
785 | } | |
786 | ||
c13a9e61 HG |
787 | USBPacket *usb_ep_find_packet_by_id(USBDevice *dev, int pid, int ep, |
788 | uint64_t id) | |
789 | { | |
790 | struct USBEndpoint *uep = usb_ep_get(dev, pid, ep); | |
791 | USBPacket *p; | |
792 | ||
6735d433 | 793 | QTAILQ_FOREACH(p, &uep->queue, queue) { |
c13a9e61 HG |
794 | if (p->id == id) { |
795 | return p; | |
796 | } | |
797 | } | |
798 | ||
799 | return NULL; | |
800 | } |