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