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