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