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