Merge remote-tracking branch 'remotes/armbru/tags/pull-build-2019-07-02-v2' into...
[qemu/ar7.git] / hw / usb / dev-hub.c
blob89f55dd25c9503fa294d4270513c967435134d97
1 /*
2 * QEMU USB HUB emulation
4 * Copyright (c) 2005 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
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"
34 #define MAX_PORTS 8
36 typedef struct USBHubPort {
37 USBPort port;
38 uint16_t wPortStatus;
39 uint16_t wPortChange;
40 } USBHubPort;
42 typedef 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;
51 #define TYPE_USB_HUB "usb-hub"
52 #define USB_HUB(obj) OBJECT_CHECK(USBHubState, (obj), TYPE_USB_HUB)
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)
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
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
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
95 /* same as Linux kernel root hubs */
97 enum {
98 STR_MANUFACTURER = 1,
99 STR_PRODUCT,
100 STR_SERIALNUMBER,
103 static const USBDescStrings desc_strings = {
104 [STR_MANUFACTURER] = "QEMU",
105 [STR_PRODUCT] = "QEMU USB Hub",
106 [STR_SERIALNUMBER] = "314159",
109 static const USBDescIface desc_iface_hub = {
110 .bInterfaceNumber = 0,
111 .bNumEndpoints = 1,
112 .bInterfaceClass = USB_CLASS_HUB,
113 .eps = (USBDescEndpoint[]) {
115 .bEndpointAddress = USB_DIR_IN | 0x01,
116 .bmAttributes = USB_ENDPOINT_XFER_INT,
117 .wMaxPacketSize = 1 + DIV_ROUND_UP(MAX_PORTS, 8),
118 .bInterval = 0xff,
123 static const USBDescDevice desc_device_hub = {
124 .bcdUSB = 0x0110,
125 .bDeviceClass = USB_CLASS_HUB,
126 .bMaxPacketSize0 = 8,
127 .bNumConfigurations = 1,
128 .confs = (USBDescConfig[]) {
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,
140 static 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,
149 .full = &desc_device_hub,
150 .str = desc_strings,
153 static const uint8_t qemu_hub_hub_descriptor[] =
155 0x00, /* u8 bLength; patched in later */
156 0x29, /* u8 bDescriptorType; Hub-descriptor */
157 0x00, /* u8 bNbrPorts; (patched later) */
158 0x0a, /* u16 wHubCharacteristics; */
159 0x00, /* (per-port OC, no power switching) */
160 0x01, /* u8 bPwrOn2pwrGood; 2ms */
161 0x00 /* u8 bHubContrCurrent; 0 mA */
163 /* DeviceRemovable and PortPwrCtrlMask patched in later */
166 static bool usb_hub_port_change(USBHubPort *port, uint16_t status)
168 bool notify = false;
170 if (status & 0x1f) {
171 port->wPortChange |= status;
172 notify = true;
174 return notify;
177 static bool usb_hub_port_set(USBHubPort *port, uint16_t status)
179 if (port->wPortStatus & status) {
180 return false;
182 port->wPortStatus |= status;
183 return usb_hub_port_change(port, status);
186 static bool usb_hub_port_clear(USBHubPort *port, uint16_t status)
188 if (!(port->wPortStatus & status)) {
189 return false;
191 port->wPortStatus &= ~status;
192 return usb_hub_port_change(port, status);
195 static bool usb_hub_port_update(USBHubPort *port)
197 bool notify = false;
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);
207 return notify;
210 static void usb_hub_port_update_timer(void *opaque)
212 USBHubState *s = opaque;
213 bool notify = false;
214 int i;
216 for (i = 0; i < s->num_ports; i++) {
217 notify |= usb_hub_port_update(&s->ports[i]);
219 if (notify) {
220 usb_wakeup(s->intr, 0);
224 static void usb_hub_attach(USBPort *port1)
226 USBHubState *s = port1->opaque;
227 USBHubPort *port = &s->ports[port1->index];
229 trace_usb_hub_attach(s->dev.addr, port1->index + 1);
230 usb_hub_port_update(port);
231 usb_wakeup(s->intr, 0);
234 static void usb_hub_detach(USBPort *port1)
236 USBHubState *s = port1->opaque;
237 USBHubPort *port = &s->ports[port1->index];
239 trace_usb_hub_detach(s->dev.addr, port1->index + 1);
240 usb_wakeup(s->intr, 0);
242 /* Let upstream know the device on this port is gone */
243 s->dev.port->ops->child_detach(s->dev.port, port1->dev);
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);
251 static void usb_hub_child_detach(USBPort *port1, USBDevice *child)
253 USBHubState *s = port1->opaque;
255 /* Pass along upstream */
256 s->dev.port->ops->child_detach(s->dev.port, child);
259 static void usb_hub_wakeup(USBPort *port1)
261 USBHubState *s = port1->opaque;
262 USBHubPort *port = &s->ports[port1->index];
264 if (usb_hub_port_clear(port, PORT_STAT_SUSPEND)) {
265 usb_wakeup(s->intr, 0);
269 static void usb_hub_complete(USBPort *port, USBPacket *packet)
271 USBHubState *s = port->opaque;
274 * Just pass it along upstream for now.
276 * If we ever implement usb 2.0 split transactions this will
277 * become a little more complicated ...
279 * Can't use usb_packet_complete() here because packet->owner is
280 * cleared already, go call the ->complete() callback directly
281 * instead.
283 s->dev.port->ops->complete(s->dev.port, packet);
286 static USBDevice *usb_hub_find_device(USBDevice *dev, uint8_t addr)
288 USBHubState *s = USB_HUB(dev);
289 USBHubPort *port;
290 USBDevice *downstream;
291 int i;
293 for (i = 0; i < s->num_ports; i++) {
294 port = &s->ports[i];
295 if (!(port->wPortStatus & PORT_STAT_ENABLE)) {
296 continue;
298 downstream = usb_find_device(&port->port, addr);
299 if (downstream != NULL) {
300 return downstream;
303 return NULL;
306 static void usb_hub_handle_reset(USBDevice *dev)
308 USBHubState *s = USB_HUB(dev);
309 USBHubPort *port;
310 int i;
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);
322 static const char *feature_name(int feature)
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",
341 if (feature < 0 || feature >= ARRAY_SIZE(name)) {
342 return "?";
344 return name[feature] ?: "?";
347 static void usb_hub_handle_control(USBDevice *dev, USBPacket *p,
348 int request, int value, int index, int length, uint8_t *data)
350 USBHubState *s = (USBHubState *)dev;
351 int ret;
353 trace_usb_hub_control(s->dev.addr, request, value, index, length);
355 ret = usb_desc_handle_control(dev, p, request, value, index, length, data);
356 if (ret >= 0) {
357 return;
360 switch(request) {
361 case EndpointOutRequest | USB_REQ_CLEAR_FEATURE:
362 if (value == 0 && index != 0x81) { /* clear ep halt */
363 goto fail;
365 break;
366 /* usb specific requests */
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:
376 unsigned int n = index - 1;
377 USBHubPort *port;
378 if (n >= s->num_ports) {
379 goto fail;
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;
391 break;
392 case SetHubFeature:
393 case ClearHubFeature:
394 if (value != 0 && value != 1) {
395 goto fail;
397 break;
398 case SetPortFeature:
400 unsigned int n = index - 1;
401 USBHubPort *port;
402 USBDevice *dev;
404 trace_usb_hub_set_port_feature(s->dev.addr, index,
405 feature_name(value));
407 if (n >= s->num_ports) {
408 goto fail;
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);
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); /* 5 ms */
431 break;
432 default:
433 goto fail;
436 break;
437 case ClearPortFeature:
439 unsigned int n = index - 1;
440 USBHubPort *port;
442 trace_usb_hub_clear_port_feature(s->dev.addr, index,
443 feature_name(value));
445 if (n >= s->num_ports) {
446 goto fail;
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;
479 default:
480 goto fail;
483 break;
484 case GetHubDescriptor:
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;
491 if (s->port_power) {
492 data[3] &= ~0x03;
493 data[3] |= 0x01;
496 /* fill DeviceRemovable bits */
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++;
503 /* fill PortPwrCtrlMask bits */
504 limit = limit + DIV_ROUND_UP(s->num_ports, 8);
505 for (;n < limit; n++) {
506 data[n] = 0xff;
507 var_hub_size++;
510 p->actual_length = sizeof(qemu_hub_hub_descriptor) + var_hub_size;
511 data[0] = p->actual_length;
512 break;
514 default:
515 fail:
516 p->status = USB_RET_STALL;
517 break;
521 static void usb_hub_handle_data(USBDevice *dev, USBPacket *p)
523 USBHubState *s = (USBHubState *)dev;
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) { /* FreeBSD workaround */
534 n = 1;
535 } else if (n > p->iov.size) {
536 p->status = USB_RET_BABBLE;
537 return;
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));
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);
550 usb_packet_copy(p, buf, n);
551 } else {
552 p->status = USB_RET_NAK; /* usb11 11.13.1 */
554 } else {
555 goto fail;
557 break;
558 case USB_TOKEN_OUT:
559 default:
560 fail:
561 p->status = USB_RET_STALL;
562 break;
566 static void usb_hub_unrealize(USBDevice *dev, Error **errp)
568 USBHubState *s = (USBHubState *)dev;
569 int i;
571 for (i = 0; i < s->num_ports; i++) {
572 usb_unregister_port(usb_bus_from_device(dev),
573 &s->ports[i].port);
576 timer_del(s->port_timer);
577 timer_free(s->port_timer);
580 static 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,
588 static void usb_hub_realize(USBDevice *dev, Error **errp)
590 USBHubState *s = USB_HUB(dev);
591 USBHubPort *port;
592 int i;
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;
600 if (dev->port->hubcount == 5) {
601 error_setg(errp, "usb hub chain too deep");
602 return;
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);
617 usb_hub_handle_reset(dev);
620 static 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()
631 static bool usb_hub_port_timer_needed(void *opaque)
633 USBHubState *s = opaque;
635 return s->port_power;
638 static 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()
649 static 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()
659 .subsections = (const VMStateDescription * []) {
660 &vmstate_usb_hub_port_timer,
661 NULL
665 static 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(),
671 static void usb_hub_class_initfn(ObjectClass *klass, void *data)
673 DeviceClass *dc = DEVICE_CLASS(klass);
674 USBDeviceClass *uc = USB_DEVICE_CLASS(klass);
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;
690 static 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,
697 static void usb_hub_register_types(void)
699 type_register_static(&hub_info);
702 type_init(usb_hub_register_types)