]> Git Repo - qemu.git/blame - hw/usb/core.c
usb: usb-bt QOMify
[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
73796fe6
GH
334USBDevice *usb_find_device(USBPort *port, uint8_t addr)
335{
336 USBDevice *dev = port->dev;
337
338 if (dev == NULL || !dev->attached || dev->state != USB_STATE_DEFAULT) {
339 return NULL;
340 }
341 if (dev->addr == addr) {
342 return dev;
343 }
344 return usb_device_find_device(dev, addr);
345}
346
9a77a0f5 347static void usb_process_one(USBPacket *p)
db4be873
GH
348{
349 USBDevice *dev = p->ep->dev;
350
9a77a0f5
HG
351 /*
352 * Handlers expect status to be initialized to USB_RET_SUCCESS, but it
353 * can be USB_RET_NAK here from a previous usb_process_one() call,
354 * or USB_RET_ASYNC from going through usb_queue_one().
355 */
356 p->status = USB_RET_SUCCESS;
357
db4be873
GH
358 if (p->ep->nr == 0) {
359 /* control pipe */
1b4b29a1 360 if (p->parameter) {
9a77a0f5
HG
361 do_parameter(dev, p);
362 return;
1b4b29a1 363 }
db4be873
GH
364 switch (p->pid) {
365 case USB_TOKEN_SETUP:
9a77a0f5
HG
366 do_token_setup(dev, p);
367 break;
db4be873 368 case USB_TOKEN_IN:
9a77a0f5
HG
369 do_token_in(dev, p);
370 break;
db4be873 371 case USB_TOKEN_OUT:
9a77a0f5
HG
372 do_token_out(dev, p);
373 break;
db4be873 374 default:
9a77a0f5 375 p->status = USB_RET_STALL;
db4be873
GH
376 }
377 } else {
378 /* data pipe */
9a77a0f5 379 usb_device_handle_data(dev, p);
db4be873
GH
380 }
381}
382
9a77a0f5
HG
383static void usb_queue_one(USBPacket *p)
384{
385 usb_packet_set_state(p, USB_PACKET_QUEUED);
386 QTAILQ_INSERT_TAIL(&p->ep->queue, p, queue);
387 p->status = USB_RET_ASYNC;
388}
389
390/* Hand over a packet to a device for processing. p->status ==
53aa8c0e
GH
391 USB_RET_ASYNC indicates the processing isn't finished yet, the
392 driver will call usb_packet_complete() when done processing it. */
9a77a0f5 393void usb_handle_packet(USBDevice *dev, USBPacket *p)
53aa8c0e 394{
98861f51 395 if (dev == NULL) {
9a77a0f5
HG
396 p->status = USB_RET_NODEV;
397 return;
98861f51 398 }
079d0b7f 399 assert(dev == p->ep->dev);
1977f93d 400 assert(dev->state == USB_STATE_DEFAULT);
5ac2731c 401 usb_packet_check_state(p, USB_PACKET_SETUP);
db4be873 402 assert(p->ep != NULL);
1977f93d 403
0132b4b6
HG
404 /* Submitting a new packet clears halt */
405 if (p->ep->halted) {
406 assert(QTAILQ_EMPTY(&p->ep->queue));
407 p->ep->halted = false;
408 }
409
c96c41ed 410 if (QTAILQ_EMPTY(&p->ep->queue) || p->ep->pipeline || p->stream) {
9a77a0f5
HG
411 usb_process_one(p);
412 if (p->status == USB_RET_ASYNC) {
be41efde 413 /* hcd drivers cannot handle async for isoc */
aaac7434 414 assert(p->ep->type != USB_ENDPOINT_XFER_ISOC);
be41efde
HG
415 /* using async for interrupt packets breaks migration */
416 assert(p->ep->type != USB_ENDPOINT_XFER_INT ||
75633529 417 (dev->flags & (1 << USB_DEV_FLAG_IS_HOST)));
db4be873
GH
418 usb_packet_set_state(p, USB_PACKET_ASYNC);
419 QTAILQ_INSERT_TAIL(&p->ep->queue, p, queue);
9a77a0f5
HG
420 } else if (p->status == USB_RET_ADD_TO_QUEUE) {
421 usb_queue_one(p);
db4be873 422 } else {
0132b4b6
HG
423 /*
424 * When pipelining is enabled usb-devices must always return async,
425 * otherwise packets can complete out of order!
426 */
c96c41ed
GH
427 assert(p->stream || !p->ep->pipeline ||
428 QTAILQ_EMPTY(&p->ep->queue));
9a77a0f5 429 if (p->status != USB_RET_NAK) {
cc409974
HG
430 usb_packet_set_state(p, USB_PACKET_COMPLETE);
431 }
1977f93d
GH
432 }
433 } else {
9a77a0f5 434 usb_queue_one(p);
4ff658fb 435 }
89b9b79f 436}
4ff658fb 437
d0ff81b8 438void usb_packet_complete_one(USBDevice *dev, USBPacket *p)
0132b4b6
HG
439{
440 USBEndpoint *ep = p->ep;
441
c96c41ed 442 assert(p->stream || QTAILQ_FIRST(&ep->queue) == p);
9a77a0f5 443 assert(p->status != USB_RET_ASYNC && p->status != USB_RET_NAK);
0132b4b6 444
9a77a0f5
HG
445 if (p->status != USB_RET_SUCCESS ||
446 (p->short_not_ok && (p->actual_length < p->iov.size))) {
0132b4b6
HG
447 ep->halted = true;
448 }
449 usb_packet_set_state(p, USB_PACKET_COMPLETE);
450 QTAILQ_REMOVE(&ep->queue, p, queue);
451 dev->port->ops->complete(dev->port, p);
452}
453
4ff658fb
GH
454/* Notify the controller that an async packet is complete. This should only
455 be called for packets previously deferred by returning USB_RET_ASYNC from
456 handle_packet. */
457void usb_packet_complete(USBDevice *dev, USBPacket *p)
458{
db4be873 459 USBEndpoint *ep = p->ep;
db4be873 460
5ac2731c 461 usb_packet_check_state(p, USB_PACKET_ASYNC);
d0ff81b8 462 usb_packet_complete_one(dev, p);
db4be873 463
0cae7b1a 464 while (!QTAILQ_EMPTY(&ep->queue)) {
db4be873 465 p = QTAILQ_FIRST(&ep->queue);
0cae7b1a
HG
466 if (ep->halted) {
467 /* Empty the queue on a halt */
9a77a0f5 468 p->status = USB_RET_REMOVE_FROM_QUEUE;
0cae7b1a
HG
469 dev->port->ops->complete(dev->port, p);
470 continue;
471 }
eb9d4673
GH
472 if (p->state == USB_PACKET_ASYNC) {
473 break;
474 }
5ac2731c 475 usb_packet_check_state(p, USB_PACKET_QUEUED);
9a77a0f5
HG
476 usb_process_one(p);
477 if (p->status == USB_RET_ASYNC) {
db4be873
GH
478 usb_packet_set_state(p, USB_PACKET_ASYNC);
479 break;
480 }
d0ff81b8 481 usb_packet_complete_one(ep->dev, p);
db4be873 482 }
4ff658fb
GH
483}
484
485/* Cancel an active packet. The packed must have been deferred by
486 returning USB_RET_ASYNC from handle_packet, and not yet
487 completed. */
488void usb_cancel_packet(USBPacket * p)
489{
db4be873
GH
490 bool callback = (p->state == USB_PACKET_ASYNC);
491 assert(usb_packet_is_inflight(p));
492 usb_packet_set_state(p, USB_PACKET_CANCELED);
493 QTAILQ_REMOVE(&p->ep->queue, p, queue);
494 if (callback) {
495 usb_device_cancel_packet(p->ep->dev, p);
496 }
4ff658fb 497}
4f4321c1
GH
498
499
500void usb_packet_init(USBPacket *p)
501{
502 qemu_iovec_init(&p->iov, 1);
503}
504
5ac2731c 505static const char *usb_packet_state_name(USBPacketState state)
db4be873 506{
db4be873
GH
507 static const char *name[] = {
508 [USB_PACKET_UNDEFINED] = "undef",
509 [USB_PACKET_SETUP] = "setup",
510 [USB_PACKET_QUEUED] = "queued",
511 [USB_PACKET_ASYNC] = "async",
512 [USB_PACKET_COMPLETE] = "complete",
513 [USB_PACKET_CANCELED] = "canceled",
514 };
5ac2731c
GH
515 if (state < ARRAY_SIZE(name)) {
516 return name[state];
517 }
518 return "INVALID";
519}
520
521void usb_packet_check_state(USBPacket *p, USBPacketState expected)
522{
523 USBDevice *dev;
524 USBBus *bus;
525
526 if (p->state == expected) {
527 return;
528 }
529 dev = p->ep->dev;
530 bus = usb_bus_from_device(dev);
531 trace_usb_packet_state_fault(bus->busnr, dev->port->path, p->ep->nr, p,
532 usb_packet_state_name(p->state),
533 usb_packet_state_name(expected));
534 assert(!"usb packet state check failed");
535}
536
537void usb_packet_set_state(USBPacket *p, USBPacketState state)
538{
f5bf14bf
GH
539 if (p->ep) {
540 USBDevice *dev = p->ep->dev;
541 USBBus *bus = usb_bus_from_device(dev);
542 trace_usb_packet_state_change(bus->busnr, dev->port->path, p->ep->nr, p,
543 usb_packet_state_name(p->state),
544 usb_packet_state_name(state));
545 } else {
546 trace_usb_packet_state_change(-1, "", -1, p,
547 usb_packet_state_name(p->state),
548 usb_packet_state_name(state));
549 }
db4be873
GH
550 p->state = state;
551}
552
8550a02d
GH
553void usb_packet_setup(USBPacket *p, int pid,
554 USBEndpoint *ep, unsigned int stream,
555 uint64_t id, bool short_not_ok, bool int_req)
4f4321c1 556{
f53c398a 557 assert(!usb_packet_is_inflight(p));
0cc6a0f1 558 assert(p->iov.iov != NULL);
e983395d 559 p->id = id;
4f4321c1 560 p->pid = pid;
079d0b7f 561 p->ep = ep;
8550a02d 562 p->stream = stream;
9a77a0f5
HG
563 p->status = USB_RET_SUCCESS;
564 p->actual_length = 0;
1b4b29a1 565 p->parameter = 0;
6ba43f1f 566 p->short_not_ok = short_not_ok;
a6fb2ddb 567 p->int_req = int_req;
a552a966 568 p->combined = NULL;
4f4321c1 569 qemu_iovec_reset(&p->iov);
db4be873 570 usb_packet_set_state(p, USB_PACKET_SETUP);
4f4321c1
GH
571}
572
573void usb_packet_addbuf(USBPacket *p, void *ptr, size_t len)
574{
575 qemu_iovec_add(&p->iov, ptr, len);
576}
577
578void usb_packet_copy(USBPacket *p, void *ptr, size_t bytes)
579{
6a98d1c0
GH
580 QEMUIOVector *iov = p->combined ? &p->combined->iov : &p->iov;
581
9a77a0f5 582 assert(p->actual_length >= 0);
6a98d1c0 583 assert(p->actual_length + bytes <= iov->size);
4f4321c1
GH
584 switch (p->pid) {
585 case USB_TOKEN_SETUP:
586 case USB_TOKEN_OUT:
6a98d1c0 587 iov_to_buf(iov->iov, iov->niov, p->actual_length, ptr, bytes);
4f4321c1
GH
588 break;
589 case USB_TOKEN_IN:
6a98d1c0 590 iov_from_buf(iov->iov, iov->niov, p->actual_length, ptr, bytes);
4f4321c1
GH
591 break;
592 default:
593 fprintf(stderr, "%s: invalid pid: %x\n", __func__, p->pid);
594 abort();
595 }
9a77a0f5 596 p->actual_length += bytes;
4f4321c1
GH
597}
598
599void usb_packet_skip(USBPacket *p, size_t bytes)
600{
6a98d1c0
GH
601 QEMUIOVector *iov = p->combined ? &p->combined->iov : &p->iov;
602
9a77a0f5 603 assert(p->actual_length >= 0);
6a98d1c0 604 assert(p->actual_length + bytes <= iov->size);
4f4321c1 605 if (p->pid == USB_TOKEN_IN) {
6a98d1c0 606 iov_memset(iov->iov, iov->niov, p->actual_length, 0, bytes);
4f4321c1 607 }
9a77a0f5 608 p->actual_length += bytes;
4f4321c1
GH
609}
610
6a98d1c0
GH
611size_t usb_packet_size(USBPacket *p)
612{
613 return p->combined ? p->combined->iov.size : p->iov.size;
614}
615
4f4321c1
GH
616void usb_packet_cleanup(USBPacket *p)
617{
f53c398a 618 assert(!usb_packet_is_inflight(p));
4f4321c1
GH
619 qemu_iovec_destroy(&p->iov);
620}
d8e17efd 621
19deaa08 622void usb_ep_reset(USBDevice *dev)
d8e17efd
GH
623{
624 int ep;
625
63095ab5 626 dev->ep_ctl.nr = 0;
25d5de7d
GH
627 dev->ep_ctl.type = USB_ENDPOINT_XFER_CONTROL;
628 dev->ep_ctl.ifnum = 0;
9adbaad3 629 dev->ep_ctl.max_packet_size = 64;
04b300f8 630 dev->ep_ctl.max_streams = 0;
25d5de7d 631 dev->ep_ctl.dev = dev;
7936e0f0 632 dev->ep_ctl.pipeline = false;
d8e17efd 633 for (ep = 0; ep < USB_MAX_ENDPOINTS; ep++) {
63095ab5
GH
634 dev->ep_in[ep].nr = ep + 1;
635 dev->ep_out[ep].nr = ep + 1;
636 dev->ep_in[ep].pid = USB_TOKEN_IN;
637 dev->ep_out[ep].pid = USB_TOKEN_OUT;
d8e17efd
GH
638 dev->ep_in[ep].type = USB_ENDPOINT_XFER_INVALID;
639 dev->ep_out[ep].type = USB_ENDPOINT_XFER_INVALID;
7c37e6a4
GH
640 dev->ep_in[ep].ifnum = USB_INTERFACE_INVALID;
641 dev->ep_out[ep].ifnum = USB_INTERFACE_INVALID;
9adbaad3
HG
642 dev->ep_in[ep].max_packet_size = 0;
643 dev->ep_out[ep].max_packet_size = 0;
04b300f8
HG
644 dev->ep_in[ep].max_streams = 0;
645 dev->ep_out[ep].max_streams = 0;
25d5de7d
GH
646 dev->ep_in[ep].dev = dev;
647 dev->ep_out[ep].dev = dev;
7936e0f0
GH
648 dev->ep_in[ep].pipeline = false;
649 dev->ep_out[ep].pipeline = false;
19deaa08
GH
650 }
651}
652
653void usb_ep_init(USBDevice *dev)
654{
655 int ep;
656
657 usb_ep_reset(dev);
658 QTAILQ_INIT(&dev->ep_ctl.queue);
659 for (ep = 0; ep < USB_MAX_ENDPOINTS; ep++) {
db4be873
GH
660 QTAILQ_INIT(&dev->ep_in[ep].queue);
661 QTAILQ_INIT(&dev->ep_out[ep].queue);
d8e17efd
GH
662 }
663}
664
5b6780d0
GH
665void usb_ep_dump(USBDevice *dev)
666{
667 static const char *tname[] = {
668 [USB_ENDPOINT_XFER_CONTROL] = "control",
669 [USB_ENDPOINT_XFER_ISOC] = "isoc",
670 [USB_ENDPOINT_XFER_BULK] = "bulk",
671 [USB_ENDPOINT_XFER_INT] = "int",
672 };
673 int ifnum, ep, first;
674
675 fprintf(stderr, "Device \"%s\", config %d\n",
676 dev->product_desc, dev->configuration);
677 for (ifnum = 0; ifnum < 16; ifnum++) {
678 first = 1;
679 for (ep = 0; ep < USB_MAX_ENDPOINTS; ep++) {
680 if (dev->ep_in[ep].type != USB_ENDPOINT_XFER_INVALID &&
681 dev->ep_in[ep].ifnum == ifnum) {
682 if (first) {
683 first = 0;
684 fprintf(stderr, " Interface %d, alternative %d\n",
685 ifnum, dev->altsetting[ifnum]);
686 }
f003397c
GH
687 fprintf(stderr, " Endpoint %d, IN, %s, %d max\n", ep,
688 tname[dev->ep_in[ep].type],
689 dev->ep_in[ep].max_packet_size);
5b6780d0
GH
690 }
691 if (dev->ep_out[ep].type != USB_ENDPOINT_XFER_INVALID &&
692 dev->ep_out[ep].ifnum == ifnum) {
693 if (first) {
694 first = 0;
695 fprintf(stderr, " Interface %d, alternative %d\n",
696 ifnum, dev->altsetting[ifnum]);
697 }
f003397c
GH
698 fprintf(stderr, " Endpoint %d, OUT, %s, %d max\n", ep,
699 tname[dev->ep_out[ep].type],
700 dev->ep_out[ep].max_packet_size);
5b6780d0
GH
701 }
702 }
703 }
704 fprintf(stderr, "--\n");
705}
706
d8e17efd
GH
707struct USBEndpoint *usb_ep_get(USBDevice *dev, int pid, int ep)
708{
079d0b7f
GH
709 struct USBEndpoint *eps;
710
711 if (dev == NULL) {
712 return NULL;
713 }
714 eps = (pid == USB_TOKEN_IN) ? dev->ep_in : dev->ep_out;
25d5de7d
GH
715 if (ep == 0) {
716 return &dev->ep_ctl;
717 }
d8e17efd
GH
718 assert(pid == USB_TOKEN_IN || pid == USB_TOKEN_OUT);
719 assert(ep > 0 && ep <= USB_MAX_ENDPOINTS);
720 return eps + ep - 1;
721}
722
723uint8_t usb_ep_get_type(USBDevice *dev, int pid, int ep)
724{
725 struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
726 return uep->type;
727}
728
729void usb_ep_set_type(USBDevice *dev, int pid, int ep, uint8_t type)
730{
731 struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
732 uep->type = type;
733}
82f02fe9 734
82f02fe9
GH
735void usb_ep_set_ifnum(USBDevice *dev, int pid, int ep, uint8_t ifnum)
736{
737 struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
738 uep->ifnum = ifnum;
739}
f003397c
GH
740
741void usb_ep_set_max_packet_size(USBDevice *dev, int pid, int ep,
742 uint16_t raw)
743{
744 struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
745 int size, microframes;
746
747 size = raw & 0x7ff;
748 switch ((raw >> 11) & 3) {
749 case 1:
750 microframes = 2;
751 break;
752 case 2:
753 microframes = 3;
754 break;
755 default:
756 microframes = 1;
757 break;
758 }
759 uep->max_packet_size = size * microframes;
760}
761
04b300f8
HG
762void usb_ep_set_max_streams(USBDevice *dev, int pid, int ep, uint8_t raw)
763{
764 struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
765 int MaxStreams;
766
767 MaxStreams = raw & 0x1f;
768 if (MaxStreams) {
769 uep->max_streams = 1 << MaxStreams;
770 } else {
771 uep->max_streams = 0;
772 }
773}
774
e382d966
GH
775void usb_ep_set_halted(USBDevice *dev, int pid, int ep, bool halted)
776{
777 struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
778 uep->halted = halted;
779}
780
c13a9e61
HG
781USBPacket *usb_ep_find_packet_by_id(USBDevice *dev, int pid, int ep,
782 uint64_t id)
783{
784 struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
785 USBPacket *p;
786
6735d433 787 QTAILQ_FOREACH(p, &uep->queue, queue) {
c13a9e61
HG
788 if (p->id == id) {
789 return p;
790 }
791 }
792
793 return NULL;
794}
This page took 1.022123 seconds and 4 git commands to generate.