1/*
2 * QEMU USB HUB emulation
3 *
4 * Copyright (c) 2005 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
25#include "qemu/osdep.h"
26#include "qapi/error.h"
27#include "qemu/timer.h"
28#include "trace.h"
29#include "hw/qdev-properties.h"
30#include "hw/usb.h"
31#include "migration/vmstate.h"
32#include "desc.h"
33#include "qemu/error-report.h"
34#include "qemu/module.h"
35
36#define MAX_PORTS 8
37
38typedef struct USBHubPort {
39 USBPort port;
40 uint16_t wPortStatus;
41 uint16_t wPortChange;
42} USBHubPort;
43
44typedef struct USBHubState {
45 USBDevice dev;
46 USBEndpoint *intr;
47 uint32_t num_ports;
48 bool port_power;
49 QEMUTimer *port_timer;
50 USBHubPort ports[MAX_PORTS];
51} USBHubState;
52
53#define TYPE_USB_HUB "usb-hub"
54#define USB_HUB(obj) OBJECT_CHECK(USBHubState, (obj), TYPE_USB_HUB)
55
56#define ClearHubFeature (0x2000 | USB_REQ_CLEAR_FEATURE)
57#define ClearPortFeature (0x2300 | USB_REQ_CLEAR_FEATURE)
58#define GetHubDescriptor (0xa000 | USB_REQ_GET_DESCRIPTOR)
59#define GetHubStatus (0xa000 | USB_REQ_GET_STATUS)
60#define GetPortStatus (0xa300 | USB_REQ_GET_STATUS)
61#define SetHubFeature (0x2000 | USB_REQ_SET_FEATURE)
62#define SetPortFeature (0x2300 | USB_REQ_SET_FEATURE)
63
64#define PORT_STAT_CONNECTION 0x0001
65#define PORT_STAT_ENABLE 0x0002
66#define PORT_STAT_SUSPEND 0x0004
67#define PORT_STAT_OVERCURRENT 0x0008
68#define PORT_STAT_RESET 0x0010
69#define PORT_STAT_POWER 0x0100
70#define PORT_STAT_LOW_SPEED 0x0200
71#define PORT_STAT_HIGH_SPEED 0x0400
72#define PORT_STAT_TEST 0x0800
73#define PORT_STAT_INDICATOR 0x1000
74
75#define PORT_STAT_C_CONNECTION 0x0001
76#define PORT_STAT_C_ENABLE 0x0002
77#define PORT_STAT_C_SUSPEND 0x0004
78#define PORT_STAT_C_OVERCURRENT 0x0008
79#define PORT_STAT_C_RESET 0x0010
80
81#define PORT_CONNECTION 0
82#define PORT_ENABLE 1
83#define PORT_SUSPEND 2
84#define PORT_OVERCURRENT 3
85#define PORT_RESET 4
86#define PORT_POWER 8
87#define PORT_LOWSPEED 9
88#define PORT_HIGHSPEED 10
89#define PORT_C_CONNECTION 16
90#define PORT_C_ENABLE 17
91#define PORT_C_SUSPEND 18
92#define PORT_C_OVERCURRENT 19
93#define PORT_C_RESET 20
94#define PORT_TEST 21
95#define PORT_INDICATOR 22
96
97/* same as Linux kernel root hubs */
98
99enum {
100 STR_MANUFACTURER = 1,
101 STR_PRODUCT,
102 STR_SERIALNUMBER,
103};
104
105static const USBDescStrings desc_strings = {
106 [STR_MANUFACTURER] = "QEMU",
107 [STR_PRODUCT] = "QEMU USB Hub",
108 [STR_SERIALNUMBER] = "314159",
109};
110
111static const USBDescIface desc_iface_hub = {
112 .bInterfaceNumber = 0,
113 .bNumEndpoints = 1,
114 .bInterfaceClass = USB_CLASS_HUB,
115 .eps = (USBDescEndpoint[]) {
116 {
117 .bEndpointAddress = USB_DIR_IN | 0x01,
118 .bmAttributes = USB_ENDPOINT_XFER_INT,
119 .wMaxPacketSize = 1 + DIV_ROUND_UP(MAX_PORTS, 8),
120 .bInterval = 0xff,
121 },
122 }
123};
124
125static const USBDescDevice desc_device_hub = {
126 .bcdUSB = 0x0110,
127 .bDeviceClass = USB_CLASS_HUB,
128 .bMaxPacketSize0 = 8,
129 .bNumConfigurations = 1,
130 .confs = (USBDescConfig[]) {
131 {
132 .bNumInterfaces = 1,
133 .bConfigurationValue = 1,
134 .bmAttributes = USB_CFG_ATT_ONE | USB_CFG_ATT_SELFPOWER |
135 USB_CFG_ATT_WAKEUP,
136 .nif = 1,
137 .ifs = &desc_iface_hub,
138 },
139 },
140};
141
142static const USBDesc desc_hub = {
143 .id = {
144 .idVendor = 0x0409,
145 .idProduct = 0x55aa,
146 .bcdDevice = 0x0101,
147 .iManufacturer = STR_MANUFACTURER,
148 .iProduct = STR_PRODUCT,
149 .iSerialNumber = STR_SERIALNUMBER,
150 },
151 .full = &desc_device_hub,
152 .str = desc_strings,
153};
154
155static const uint8_t qemu_hub_hub_descriptor[] =
156{
157 0x00, /* u8 bLength; patched in later */
158 0x29, /* u8 bDescriptorType; Hub-descriptor */
159 0x00, /* u8 bNbrPorts; (patched later) */
160 0x0a, /* u16 wHubCharacteristics; */
161 0x00, /* (per-port OC, no power switching) */
162 0x01, /* u8 bPwrOn2pwrGood; 2ms */
163 0x00 /* u8 bHubContrCurrent; 0 mA */
164
165 /* DeviceRemovable and PortPwrCtrlMask patched in later */
166};
167
168static bool usb_hub_port_change(USBHubPort *port, uint16_t status)
169{
170 bool notify = false;
171
172 if (status & 0x1f) {
173 port->wPortChange |= status;
174 notify = true;
175 }
176 return notify;
177}
178
179static bool usb_hub_port_set(USBHubPort *port, uint16_t status)
180{
181 if (port->wPortStatus & status) {
182 return false;
183 }
184 port->wPortStatus |= status;
185 return usb_hub_port_change(port, status);
186}
187
188static bool usb_hub_port_clear(USBHubPort *port, uint16_t status)
189{
190 if (!(port->wPortStatus & status)) {
191 return false;
192 }
193 port->wPortStatus &= ~status;
194 return usb_hub_port_change(port, status);
195}
196
197static bool usb_hub_port_update(USBHubPort *port)
198{
199 bool notify = false;
200
201 if (port->port.dev && port->port.dev->attached) {
202 notify = usb_hub_port_set(port, PORT_STAT_CONNECTION);
203 if (port->port.dev->speed == USB_SPEED_LOW) {
204 usb_hub_port_set(port, PORT_STAT_LOW_SPEED);
205 } else {
206 usb_hub_port_clear(port, PORT_STAT_LOW_SPEED);
207 }
208 }
209 return notify;
210}
211
212static void usb_hub_port_update_timer(void *opaque)
213{
214 USBHubState *s = opaque;
215 bool notify = false;
216 int i;
217
218 for (i = 0; i < s->num_ports; i++) {
219 notify |= usb_hub_port_update(&s->ports[i]);
220 }
221 if (notify) {
222 usb_wakeup(s->intr, 0);
223 }
224}
225
226static void usb_hub_attach(USBPort *port1)
227{
228 USBHubState *s = port1->opaque;
229 USBHubPort *port = &s->ports[port1->index];
230
231 trace_usb_hub_attach(s->dev.addr, port1->index + 1);
232 usb_hub_port_update(port);
233 usb_wakeup(s->intr, 0);
234}
235
236static void usb_hub_detach(USBPort *port1)
237{
238 USBHubState *s = port1->opaque;
239 USBHubPort *port = &s->ports[port1->index];
240
241 trace_usb_hub_detach(s->dev.addr, port1->index + 1);
242 usb_wakeup(s->intr, 0);
243
244 /* Let upstream know the device on this port is gone */
245 s->dev.port->ops->child_detach(s->dev.port, port1->dev);
246
247 usb_hub_port_clear(port, PORT_STAT_CONNECTION);
248 usb_hub_port_clear(port, PORT_STAT_ENABLE);
249 usb_hub_port_clear(port, PORT_STAT_SUSPEND);
250 usb_wakeup(s->intr, 0);
251}
252
253static void usb_hub_child_detach(USBPort *port1, USBDevice *child)
254{
255 USBHubState *s = port1->opaque;
256
257 /* Pass along upstream */
258 s->dev.port->ops->child_detach(s->dev.port, child);
259}
260
261static void usb_hub_wakeup(USBPort *port1)
262{
263 USBHubState *s = port1->opaque;
264 USBHubPort *port = &s->ports[port1->index];
265
266 if (usb_hub_port_clear(port, PORT_STAT_SUSPEND)) {
267 usb_wakeup(s->intr, 0);
268 }
269}
270
271static void usb_hub_complete(USBPort *port, USBPacket *packet)
272{
273 USBHubState *s = port->opaque;
274
275 /*
276 * Just pass it along upstream for now.
277 *
278 * If we ever implement usb 2.0 split transactions this will
279 * become a little more complicated ...
280 *
281 * Can't use usb_packet_complete() here because packet->owner is
282 * cleared already, go call the ->complete() callback directly
283 * instead.
284 */
285 s->dev.port->ops->complete(s->dev.port, packet);
286}
287
288static USBDevice *usb_hub_find_device(USBDevice *dev, uint8_t addr)
289{
290 USBHubState *s = USB_HUB(dev);
291 USBHubPort *port;
292 USBDevice *downstream;
293 int i;
294
295 for (i = 0; i < s->num_ports; i++) {
296 port = &s->ports[i];
297 if (!(port->wPortStatus & PORT_STAT_ENABLE)) {
298 continue;
299 }
300 downstream = usb_find_device(&port->port, addr);
301 if (downstream != NULL) {
302 return downstream;
303 }
304 }
305 return NULL;
306}
307
308static void usb_hub_handle_reset(USBDevice *dev)
309{
310 USBHubState *s = USB_HUB(dev);
311 USBHubPort *port;
312 int i;
313
314 trace_usb_hub_reset(s->dev.addr);
315 for (i = 0; i < s->num_ports; i++) {
316 port = s->ports + i;
317 port->wPortStatus = 0;
318 port->wPortChange = 0;
319 usb_hub_port_set(port, PORT_STAT_POWER);
320 usb_hub_port_update(port);
321 }
322}
323
324static const char *feature_name(int feature)
325{
326 static const char *name[] = {
327 [PORT_CONNECTION] = "connection",
328 [PORT_ENABLE] = "enable",
329 [PORT_SUSPEND] = "suspend",
330 [PORT_OVERCURRENT] = "overcurrent",
331 [PORT_RESET] = "reset",
332 [PORT_POWER] = "power",
333 [PORT_LOWSPEED] = "lowspeed",
334 [PORT_HIGHSPEED] = "highspeed",
335 [PORT_C_CONNECTION] = "change-connection",
336 [PORT_C_ENABLE] = "change-enable",
337 [PORT_C_SUSPEND] = "change-suspend",
338 [PORT_C_OVERCURRENT] = "change-overcurrent",
339 [PORT_C_RESET] = "change-reset",
340 [PORT_TEST] = "test",
341 [PORT_INDICATOR] = "indicator",
342 };
343 if (feature < 0 || feature >= ARRAY_SIZE(name)) {
344 return "?";
345 }
346 return name[feature] ?: "?";
347}
348
349static void usb_hub_handle_control(USBDevice *dev, USBPacket *p,
350 int request, int value, int index, int length, uint8_t *data)
351{
352 USBHubState *s = (USBHubState *)dev;
353 int ret;
354
355 trace_usb_hub_control(s->dev.addr, request, value, index, length);
356
357 ret = usb_desc_handle_control(dev, p, request, value, index, length, data);
358 if (ret >= 0) {
359 return;
360 }
361
362 switch(request) {
363 case EndpointOutRequest | USB_REQ_CLEAR_FEATURE:
364 if (value == 0 && index != 0x81) { /* clear ep halt */
365 goto fail;
366 }
367 break;
368 /* usb specific requests */
369 case GetHubStatus:
370 data[0] = 0;
371 data[1] = 0;
372 data[2] = 0;
373 data[3] = 0;
374 p->actual_length = 4;
375 break;
376 case GetPortStatus:
377 {
378 unsigned int n = index - 1;
379 USBHubPort *port;
380 if (n >= s->num_ports) {
381 goto fail;
382 }
383 port = &s->ports[n];
384 trace_usb_hub_get_port_status(s->dev.addr, index,
385 port->wPortStatus,
386 port->wPortChange);
387 data[0] = port->wPortStatus;
388 data[1] = port->wPortStatus >> 8;
389 data[2] = port->wPortChange;
390 data[3] = port->wPortChange >> 8;
391 p->actual_length = 4;
392 }
393 break;
394 case SetHubFeature:
395 case ClearHubFeature:
396 if (value != 0 && value != 1) {
397 goto fail;
398 }
399 break;
400 case SetPortFeature:
401 {
402 unsigned int n = index - 1;
403 USBHubPort *port;
404 USBDevice *dev;
405
406 trace_usb_hub_set_port_feature(s->dev.addr, index,
407 feature_name(value));
408
409 if (n >= s->num_ports) {
410 goto fail;
411 }
412 port = &s->ports[n];
413 dev = port->port.dev;
414 switch(value) {
415 case PORT_SUSPEND:
416 port->wPortStatus |= PORT_STAT_SUSPEND;
417 break;
418 case PORT_RESET:
419 usb_hub_port_set(port, PORT_STAT_RESET);
420 usb_hub_port_clear(port, PORT_STAT_RESET);
421 if (dev && dev->attached) {
422 usb_device_reset(dev);
423 usb_hub_port_set(port, PORT_STAT_ENABLE);
424 }
425 usb_wakeup(s->intr, 0);
426 break;
427 case PORT_POWER:
428 if (s->port_power) {
429 int64_t now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
430 usb_hub_port_set(port, PORT_STAT_POWER);
431 timer_mod(s->port_timer, now + 5000000); /* 5 ms */
432 }
433 break;
434 default:
435 goto fail;
436 }
437 }
438 break;
439 case ClearPortFeature:
440 {
441 unsigned int n = index - 1;
442 USBHubPort *port;
443
444 trace_usb_hub_clear_port_feature(s->dev.addr, index,
445 feature_name(value));
446
447 if (n >= s->num_ports) {
448 goto fail;
449 }
450 port = &s->ports[n];
451 switch(value) {
452 case PORT_ENABLE:
453 port->wPortStatus &= ~PORT_STAT_ENABLE;
454 break;
455 case PORT_C_ENABLE:
456 port->wPortChange &= ~PORT_STAT_C_ENABLE;
457 break;
458 case PORT_SUSPEND:
459 usb_hub_port_clear(port, PORT_STAT_SUSPEND);
460 break;
461 case PORT_C_SUSPEND:
462 port->wPortChange &= ~PORT_STAT_C_SUSPEND;
463 break;
464 case PORT_C_CONNECTION:
465 port->wPortChange &= ~PORT_STAT_C_CONNECTION;
466 break;
467 case PORT_C_OVERCURRENT:
468 port->wPortChange &= ~PORT_STAT_C_OVERCURRENT;
469 break;
470 case PORT_C_RESET:
471 port->wPortChange &= ~PORT_STAT_C_RESET;
472 break;
473 case PORT_POWER:
474 if (s->port_power) {
475 usb_hub_port_clear(port, PORT_STAT_POWER);
476 usb_hub_port_clear(port, PORT_STAT_CONNECTION);
477 usb_hub_port_clear(port, PORT_STAT_ENABLE);
478 usb_hub_port_clear(port, PORT_STAT_SUSPEND);
479 port->wPortChange = 0;
480 }
481 default:
482 goto fail;
483 }
484 }
485 break;
486 case GetHubDescriptor:
487 {
488 unsigned int n, limit, var_hub_size = 0;
489 memcpy(data, qemu_hub_hub_descriptor,
490 sizeof(qemu_hub_hub_descriptor));
491 data[2] = s->num_ports;
492
493 if (s->port_power) {
494 data[3] &= ~0x03;
495 data[3] |= 0x01;
496 }
497
498 /* fill DeviceRemovable bits */
499 limit = DIV_ROUND_UP(s->num_ports + 1, 8) + 7;
500 for (n = 7; n < limit; n++) {
501 data[n] = 0x00;
502 var_hub_size++;
503 }
504
505 /* fill PortPwrCtrlMask bits */
506 limit = limit + DIV_ROUND_UP(s->num_ports, 8);
507 for (;n < limit; n++) {
508 data[n] = 0xff;
509 var_hub_size++;
510 }
511
512 p->actual_length = sizeof(qemu_hub_hub_descriptor) + var_hub_size;
513 data[0] = p->actual_length;
514 break;
515 }
516 default:
517 fail:
518 p->status = USB_RET_STALL;
519 break;
520 }
521}
522
523static void usb_hub_handle_data(USBDevice *dev, USBPacket *p)
524{
525 USBHubState *s = (USBHubState *)dev;
526
527 switch(p->pid) {
528 case USB_TOKEN_IN:
529 if (p->ep->nr == 1) {
530 USBHubPort *port;
531 unsigned int status;
532 uint8_t buf[4];
533 int i, n;
534 n = DIV_ROUND_UP(s->num_ports + 1, 8);
535 if (p->iov.size == 1) { /* FreeBSD workaround */
536 n = 1;
537 } else if (n > p->iov.size) {
538 p->status = USB_RET_BABBLE;
539 return;
540 }
541 status = 0;
542 for (i = 0; i < s->num_ports; i++) {
543 port = &s->ports[i];
544 if (port->wPortChange)
545 status |= (1 << (i + 1));
546 }
547 if (status != 0) {
548 trace_usb_hub_status_report(s->dev.addr, status);
549 for(i = 0; i < n; i++) {
550 buf[i] = status >> (8 * i);
551 }
552 usb_packet_copy(p, buf, n);
553 } else {
554 p->status = USB_RET_NAK; /* usb11 11.13.1 */
555 }
556 } else {
557 goto fail;
558 }
559 break;
560 case USB_TOKEN_OUT:
561 default:
562 fail:
563 p->status = USB_RET_STALL;
564 break;
565 }
566}
567
568static void usb_hub_unrealize(USBDevice *dev, Error **errp)
569{
570 USBHubState *s = (USBHubState *)dev;
571 int i;
572
573 for (i = 0; i < s->num_ports; i++) {
574 usb_unregister_port(usb_bus_from_device(dev),
575 &s->ports[i].port);
576 }
577
578 timer_del(s->port_timer);
579 timer_free(s->port_timer);
580}
581
582static USBPortOps usb_hub_port_ops = {
583 .attach = usb_hub_attach,
584 .detach = usb_hub_detach,
585 .child_detach = usb_hub_child_detach,
586 .wakeup = usb_hub_wakeup,
587 .complete = usb_hub_complete,
588};
589
590static void usb_hub_realize(USBDevice *dev, Error **errp)
591{
592 USBHubState *s = USB_HUB(dev);
593 USBHubPort *port;
594 int i;
595
596 if (s->num_ports < 1 || s->num_ports > MAX_PORTS) {
597 error_setg(errp, "num_ports (%d) out of range (1..%d)",
598 s->num_ports, MAX_PORTS);
599 return;
600 }
601
602 if (dev->port->hubcount == 5) {
603 error_setg(errp, "usb hub chain too deep");
604 return;
605 }
606
607 usb_desc_create_serial(dev);
608 usb_desc_init(dev);
609 s->port_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
610 usb_hub_port_update_timer, s);
611 s->intr = usb_ep_get(dev, USB_TOKEN_IN, 1);
612 for (i = 0; i < s->num_ports; i++) {
613 port = &s->ports[i];
614 usb_register_port(usb_bus_from_device(dev),
615 &port->port, s, i, &usb_hub_port_ops,
616 USB_SPEED_MASK_LOW | USB_SPEED_MASK_FULL);
617 usb_port_location(&port->port, dev->port, i+1);
618 }
619 usb_hub_handle_reset(dev);
620}
621
622static const VMStateDescription vmstate_usb_hub_port = {
623 .name = "usb-hub-port",
624 .version_id = 1,
625 .minimum_version_id = 1,
626 .fields = (VMStateField[]) {
627 VMSTATE_UINT16(wPortStatus, USBHubPort),
628 VMSTATE_UINT16(wPortChange, USBHubPort),
629 VMSTATE_END_OF_LIST()
630 }
631};
632
633static bool usb_hub_port_timer_needed(void *opaque)
634{
635 USBHubState *s = opaque;
636
637 return s->port_power;
638}
639
640static const VMStateDescription vmstate_usb_hub_port_timer = {
641 .name = "usb-hub/port-timer",
642 .version_id = 1,
643 .minimum_version_id = 1,
644 .needed = usb_hub_port_timer_needed,
645 .fields = (VMStateField[]) {
646 VMSTATE_TIMER_PTR(port_timer, USBHubState),
647 VMSTATE_END_OF_LIST()
648 },
649};
650
651static const VMStateDescription vmstate_usb_hub = {
652 .name = "usb-hub",
653 .version_id = 1,
654 .minimum_version_id = 1,
655 .fields = (VMStateField[]) {
656 VMSTATE_USB_DEVICE(dev, USBHubState),
657 VMSTATE_STRUCT_ARRAY(ports, USBHubState, MAX_PORTS, 0,
658 vmstate_usb_hub_port, USBHubPort),
659 VMSTATE_END_OF_LIST()
660 },
661 .subsections = (const VMStateDescription * []) {
662 &vmstate_usb_hub_port_timer,
663 NULL
664 }
665};
666
667static Property usb_hub_properties[] = {
668 DEFINE_PROP_UINT32("ports", USBHubState, num_ports, 8),
669 DEFINE_PROP_BOOL("port-power", USBHubState, port_power, false),
670 DEFINE_PROP_END_OF_LIST(),
671};
672
673static void usb_hub_class_initfn(ObjectClass *klass, void *data)
674{
675 DeviceClass *dc = DEVICE_CLASS(klass);
676 USBDeviceClass *uc = USB_DEVICE_CLASS(klass);
677
678 uc->realize = usb_hub_realize;
679 uc->product_desc = "QEMU USB Hub";
680 uc->usb_desc = &desc_hub;
681 uc->find_device = usb_hub_find_device;
682 uc->handle_reset = usb_hub_handle_reset;
683 uc->handle_control = usb_hub_handle_control;
684 uc->handle_data = usb_hub_handle_data;
685 uc->unrealize = usb_hub_unrealize;
686 set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories);
687 dc->fw_name = "hub";
688 dc->vmsd = &vmstate_usb_hub;
689 dc->props = usb_hub_properties;
690}
691
692static const TypeInfo hub_info = {
693 .name = TYPE_USB_HUB,
694 .parent = TYPE_USB_DEVICE,
695 .instance_size = sizeof(USBHubState),
696 .class_init = usb_hub_class_initfn,
697};
698
699static void usb_hub_register_types(void)
700{
701 type_register_static(&hub_info);
702}
703
704type_init(usb_hub_register_types)
705