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