Merge commit 'origin/upstream' into juha-devel
[qemu] / hw / usb-hub.c
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 #include "qemu-common.h"
25 #include "usb.h"
26 #include "hw.h"
27
28 //#define DEBUG
29
30 #define MAX_PORTS 8
31
32 typedef struct USBHubPort {
33     USBPort port;
34     uint16_t wPortStatus;
35     uint16_t wPortChange;
36 } USBHubPort;
37
38 typedef struct USBHubState {
39     USBDevice dev;
40     int nb_ports;
41     USBHubPort ports[MAX_PORTS];
42 } USBHubState;
43
44 static void usb_hub_save_state(QEMUFile *f, void *opaque)
45 {
46     USBHubState *s = (USBHubState *)opaque;
47     int i;
48     
49     qemu_put_sbe32(f, s->nb_ports);
50     for (i = 0; i < s->nb_ports; i++) {
51         qemu_put_be16(f, s->ports[i].wPortStatus);
52         qemu_put_be16(f, s->ports[i].wPortChange);
53     }
54 }
55
56 static int usb_hub_load_state(QEMUFile *f, void *opaque, int version_id)
57 {
58     USBHubState *s = (USBHubState *)opaque;
59     int i;
60     
61     if (version_id)
62         return -EINVAL;
63     
64     if (qemu_get_sbe32(f) != s->nb_ports)
65         return -EINVAL;
66     
67     for (i = 0; i < s->nb_ports; i++) {
68         s->ports[i].wPortStatus = qemu_get_be16(f);
69         s->ports[i].wPortChange = qemu_get_be16(f);
70     }
71     
72     return 0;
73 }
74
75 #define ClearHubFeature         (0x2000 | USB_REQ_CLEAR_FEATURE)
76 #define ClearPortFeature        (0x2300 | USB_REQ_CLEAR_FEATURE)
77 #define GetHubDescriptor        (0xa000 | USB_REQ_GET_DESCRIPTOR)
78 #define GetHubStatus            (0xa000 | USB_REQ_GET_STATUS)
79 #define GetPortStatus           (0xa300 | USB_REQ_GET_STATUS)
80 #define SetHubFeature           (0x2000 | USB_REQ_SET_FEATURE)
81 #define SetPortFeature          (0x2300 | USB_REQ_SET_FEATURE)
82
83 #define PORT_STAT_CONNECTION    0x0001
84 #define PORT_STAT_ENABLE        0x0002
85 #define PORT_STAT_SUSPEND       0x0004
86 #define PORT_STAT_OVERCURRENT   0x0008
87 #define PORT_STAT_RESET         0x0010
88 #define PORT_STAT_POWER         0x0100
89 #define PORT_STAT_LOW_SPEED     0x0200
90 #define PORT_STAT_HIGH_SPEED    0x0400
91 #define PORT_STAT_TEST          0x0800
92 #define PORT_STAT_INDICATOR     0x1000
93
94 #define PORT_STAT_C_CONNECTION  0x0001
95 #define PORT_STAT_C_ENABLE      0x0002
96 #define PORT_STAT_C_SUSPEND     0x0004
97 #define PORT_STAT_C_OVERCURRENT 0x0008
98 #define PORT_STAT_C_RESET       0x0010
99
100 #define PORT_CONNECTION         0
101 #define PORT_ENABLE             1
102 #define PORT_SUSPEND            2
103 #define PORT_OVERCURRENT        3
104 #define PORT_RESET              4
105 #define PORT_POWER              8
106 #define PORT_LOWSPEED           9
107 #define PORT_HIGHSPEED          10
108 #define PORT_C_CONNECTION       16
109 #define PORT_C_ENABLE           17
110 #define PORT_C_SUSPEND          18
111 #define PORT_C_OVERCURRENT      19
112 #define PORT_C_RESET            20
113 #define PORT_TEST               21
114 #define PORT_INDICATOR          22
115
116 /* same as Linux kernel root hubs */
117
118 static const uint8_t qemu_hub_dev_descriptor[] = {
119         0x12,       /*  u8 bLength; */
120         0x01,       /*  u8 bDescriptorType; Device */
121         0x10, 0x01, /*  u16 bcdUSB; v1.1 */
122
123         0x09,       /*  u8  bDeviceClass; HUB_CLASSCODE */
124         0x00,       /*  u8  bDeviceSubClass; */
125         0x00,       /*  u8  bDeviceProtocol; [ low/full speeds only ] */
126         0x08,       /*  u8  bMaxPacketSize0; 8 Bytes */
127
128         0x00, 0x00, /*  u16 idVendor; */
129         0x00, 0x00, /*  u16 idProduct; */
130         0x01, 0x01, /*  u16 bcdDevice */
131
132         0x03,       /*  u8  iManufacturer; */
133         0x02,       /*  u8  iProduct; */
134         0x01,       /*  u8  iSerialNumber; */
135         0x01        /*  u8  bNumConfigurations; */
136 };
137
138 /* XXX: patch interrupt size */
139 static const uint8_t qemu_hub_config_descriptor[] = {
140
141         /* one configuration */
142         0x09,       /*  u8  bLength; */
143         0x02,       /*  u8  bDescriptorType; Configuration */
144         0x19, 0x00, /*  u16 wTotalLength; */
145         0x01,       /*  u8  bNumInterfaces; (1) */
146         0x01,       /*  u8  bConfigurationValue; */
147         0x00,       /*  u8  iConfiguration; */
148         0xe0,       /*  u8  bmAttributes;
149                                  Bit 7: must be set,
150                                      6: Self-powered,
151                                      5: Remote wakeup,
152                                      4..0: resvd */
153         0x00,       /*  u8  MaxPower; */
154
155         /* USB 1.1:
156          * USB 2.0, single TT organization (mandatory):
157          *      one interface, protocol 0
158          *
159          * USB 2.0, multiple TT organization (optional):
160          *      two interfaces, protocols 1 (like single TT)
161          *      and 2 (multiple TT mode) ... config is
162          *      sometimes settable
163          *      NOT IMPLEMENTED
164          */
165
166         /* one interface */
167         0x09,       /*  u8  if_bLength; */
168         0x04,       /*  u8  if_bDescriptorType; Interface */
169         0x00,       /*  u8  if_bInterfaceNumber; */
170         0x00,       /*  u8  if_bAlternateSetting; */
171         0x01,       /*  u8  if_bNumEndpoints; */
172         0x09,       /*  u8  if_bInterfaceClass; HUB_CLASSCODE */
173         0x00,       /*  u8  if_bInterfaceSubClass; */
174         0x00,       /*  u8  if_bInterfaceProtocol; [usb1.1 or single tt] */
175         0x00,       /*  u8  if_iInterface; */
176
177         /* one endpoint (status change endpoint) */
178         0x07,       /*  u8  ep_bLength; */
179         0x05,       /*  u8  ep_bDescriptorType; Endpoint */
180         0x81,       /*  u8  ep_bEndpointAddress; IN Endpoint 1 */
181         0x03,       /*  u8  ep_bmAttributes; Interrupt */
182         0x02, 0x00, /*  u16 ep_wMaxPacketSize; 1 + (MAX_ROOT_PORTS / 8) */
183         0xff        /*  u8  ep_bInterval; (255ms -- usb 2.0 spec) */
184 };
185
186 static const uint8_t qemu_hub_hub_descriptor[] =
187 {
188         0x00,                   /*  u8  bLength; patched in later */
189         0x29,                   /*  u8  bDescriptorType; Hub-descriptor */
190         0x00,                   /*  u8  bNbrPorts; (patched later) */
191         0x0a,                   /* u16  wHubCharacteristics; */
192         0x00,                   /*   (per-port OC, no power switching) */
193         0x01,                   /*  u8  bPwrOn2pwrGood; 2ms */
194         0x00                    /*  u8  bHubContrCurrent; 0 mA */
195
196         /* DeviceRemovable and PortPwrCtrlMask patched in later */
197 };
198
199 static void usb_hub_attach(USBPort *port1, USBDevice *dev)
200 {
201     USBHubState *s = port1->opaque;
202     USBHubPort *port = &s->ports[port1->index];
203
204     if (dev) {
205         if (port->port.dev)
206             usb_attach(port1, NULL);
207
208         port->wPortStatus |= PORT_STAT_CONNECTION;
209         port->wPortChange |= PORT_STAT_C_CONNECTION;
210         if (dev->speed == USB_SPEED_LOW)
211             port->wPortStatus |= PORT_STAT_LOW_SPEED;
212         else
213             port->wPortStatus &= ~PORT_STAT_LOW_SPEED;
214         port->port.dev = dev;
215         /* send the attach message */
216         usb_send_msg(dev, USB_MSG_ATTACH);
217     } else {
218         dev = port->port.dev;
219         if (dev) {
220             port->wPortStatus &= ~PORT_STAT_CONNECTION;
221             port->wPortChange |= PORT_STAT_C_CONNECTION;
222             if (port->wPortStatus & PORT_STAT_ENABLE) {
223                 port->wPortStatus &= ~PORT_STAT_ENABLE;
224                 port->wPortChange |= PORT_STAT_C_ENABLE;
225             }
226             /* send the detach message */
227             usb_send_msg(dev, USB_MSG_DETACH);
228             port->port.dev = NULL;
229         }
230     }
231 }
232
233 static void usb_hub_handle_reset(USBDevice *dev)
234 {
235     /* XXX: do it */
236 }
237
238 static int usb_hub_handle_control(USBDevice *dev, int request, int value,
239                                   int index, int length, uint8_t *data)
240 {
241     USBHubState *s = (USBHubState *)dev;
242     int ret;
243
244     switch(request) {
245     case DeviceRequest | USB_REQ_GET_STATUS:
246         data[0] = (1 << USB_DEVICE_SELF_POWERED) |
247             (dev->remote_wakeup << USB_DEVICE_REMOTE_WAKEUP);
248         data[1] = 0x00;
249         ret = 2;
250         break;
251     case DeviceOutRequest | USB_REQ_CLEAR_FEATURE:
252         if (value == USB_DEVICE_REMOTE_WAKEUP) {
253             dev->remote_wakeup = 0;
254         } else {
255             goto fail;
256         }
257         ret = 0;
258         break;
259     case EndpointOutRequest | USB_REQ_CLEAR_FEATURE:
260         if (value == 0 && index != 0x81) { /* clear ep halt */
261             goto fail;
262         }
263         ret = 0;
264         break;
265     case DeviceOutRequest | USB_REQ_SET_FEATURE:
266         if (value == USB_DEVICE_REMOTE_WAKEUP) {
267             dev->remote_wakeup = 1;
268         } else {
269             goto fail;
270         }
271         ret = 0;
272         break;
273     case DeviceOutRequest | USB_REQ_SET_ADDRESS:
274         dev->addr = value;
275         ret = 0;
276         break;
277     case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
278         switch(value >> 8) {
279         case USB_DT_DEVICE:
280             memcpy(data, qemu_hub_dev_descriptor,
281                    sizeof(qemu_hub_dev_descriptor));
282             ret = sizeof(qemu_hub_dev_descriptor);
283             break;
284         case USB_DT_CONFIG:
285             memcpy(data, qemu_hub_config_descriptor,
286                    sizeof(qemu_hub_config_descriptor));
287
288             /* status change endpoint size based on number
289              * of ports */
290             data[22] = (s->nb_ports + 1 + 7) / 8;
291
292             ret = sizeof(qemu_hub_config_descriptor);
293             break;
294         case USB_DT_STRING:
295             switch(value & 0xff) {
296             case 0:
297                 /* language ids */
298                 data[0] = 4;
299                 data[1] = 3;
300                 data[2] = 0x09;
301                 data[3] = 0x04;
302                 ret = 4;
303                 break;
304             case 1:
305                 /* serial number */
306                 ret = set_usb_string(data, "314159");
307                 break;
308             case 2:
309                 /* product description */
310                 ret = set_usb_string(data, "QEMU USB Hub");
311                 break;
312             case 3:
313                 /* vendor description */
314                 ret = set_usb_string(data, "QEMU " QEMU_VERSION);
315                 break;
316             default:
317                 goto fail;
318             }
319             break;
320         default:
321             goto fail;
322         }
323         break;
324     case DeviceRequest | USB_REQ_GET_CONFIGURATION:
325         data[0] = 1;
326         ret = 1;
327         break;
328     case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
329         ret = 0;
330         break;
331     case DeviceRequest | USB_REQ_GET_INTERFACE:
332         data[0] = 0;
333         ret = 1;
334         break;
335     case DeviceOutRequest | USB_REQ_SET_INTERFACE:
336         ret = 0;
337         break;
338         /* usb specific requests */
339     case GetHubStatus:
340         data[0] = 0;
341         data[1] = 0;
342         data[2] = 0;
343         data[3] = 0;
344         ret = 4;
345         break;
346     case GetPortStatus:
347         {
348             unsigned int n = index - 1;
349             USBHubPort *port;
350             if (n >= s->nb_ports)
351                 goto fail;
352             port = &s->ports[n];
353             data[0] = port->wPortStatus;
354             data[1] = port->wPortStatus >> 8;
355             data[2] = port->wPortChange;
356             data[3] = port->wPortChange >> 8;
357             ret = 4;
358         }
359         break;
360     case SetHubFeature:
361     case ClearHubFeature:
362         if (value == 0 || value == 1) {
363         } else {
364             goto fail;
365         }
366         ret = 0;
367         break;
368     case SetPortFeature:
369         {
370             unsigned int n = index - 1;
371             USBHubPort *port;
372             USBDevice *dev;
373             if (n >= s->nb_ports)
374                 goto fail;
375             port = &s->ports[n];
376             dev = port->port.dev;
377             switch(value) {
378             case PORT_SUSPEND:
379                 port->wPortStatus |= PORT_STAT_SUSPEND;
380                 break;
381             case PORT_RESET:
382                 if (dev) {
383                     usb_send_msg(dev, USB_MSG_RESET);
384                     port->wPortChange |= PORT_STAT_C_RESET;
385                     /* set enable bit */
386                     port->wPortStatus |= PORT_STAT_ENABLE;
387                 }
388                 break;
389             case PORT_POWER:
390                 break;
391             default:
392                 goto fail;
393             }
394             ret = 0;
395         }
396         break;
397     case ClearPortFeature:
398         {
399             unsigned int n = index - 1;
400             USBHubPort *port;
401             USBDevice *dev;
402             if (n >= s->nb_ports)
403                 goto fail;
404             port = &s->ports[n];
405             dev = port->port.dev;
406             switch(value) {
407             case PORT_ENABLE:
408                 port->wPortStatus &= ~PORT_STAT_ENABLE;
409                 break;
410             case PORT_C_ENABLE:
411                 port->wPortChange &= ~PORT_STAT_C_ENABLE;
412                 break;
413             case PORT_SUSPEND:
414                 port->wPortStatus &= ~PORT_STAT_SUSPEND;
415                 break;
416             case PORT_C_SUSPEND:
417                 port->wPortChange &= ~PORT_STAT_C_SUSPEND;
418                 break;
419             case PORT_C_CONNECTION:
420                 port->wPortChange &= ~PORT_STAT_C_CONNECTION;
421                 break;
422             case PORT_C_OVERCURRENT:
423                 port->wPortChange &= ~PORT_STAT_C_OVERCURRENT;
424                 break;
425             case PORT_C_RESET:
426                 port->wPortChange &= ~PORT_STAT_C_RESET;
427                 break;
428             default:
429                 goto fail;
430             }
431             ret = 0;
432         }
433         break;
434     case GetHubDescriptor:
435         {
436             unsigned int n, limit, var_hub_size = 0;
437             memcpy(data, qemu_hub_hub_descriptor,
438                    sizeof(qemu_hub_hub_descriptor));
439             data[2] = s->nb_ports;
440
441             /* fill DeviceRemovable bits */
442             limit = ((s->nb_ports + 1 + 7) / 8) + 7;
443             for (n = 7; n < limit; n++) {
444                 data[n] = 0x00;
445                 var_hub_size++;
446             }
447
448             /* fill PortPwrCtrlMask bits */
449             limit = limit + ((s->nb_ports + 7) / 8);
450             for (;n < limit; n++) {
451                 data[n] = 0xff;
452                 var_hub_size++;
453             }
454
455             ret = sizeof(qemu_hub_hub_descriptor) + var_hub_size;
456             data[0] = ret;
457             break;
458         }
459     default:
460     fail:
461         ret = USB_RET_STALL;
462         break;
463     }
464     return ret;
465 }
466
467 static int usb_hub_handle_data(USBDevice *dev, USBPacket *p)
468 {
469     USBHubState *s = (USBHubState *)dev;
470     int ret;
471
472     switch(p->pid) {
473     case USB_TOKEN_IN:
474         if (p->devep == 1) {
475             USBHubPort *port;
476             unsigned int status;
477             int i, n;
478             n = (s->nb_ports + 1 + 7) / 8;
479             if (p->len == 1) { /* FreeBSD workaround */
480                 n = 1;
481             } else if (n > p->len) {
482                 return USB_RET_BABBLE;
483             }
484             status = 0;
485             for(i = 0; i < s->nb_ports; i++) {
486                 port = &s->ports[i];
487                 if (port->wPortChange)
488                     status |= (1 << (i + 1));
489             }
490             if (status != 0) {
491                 for(i = 0; i < n; i++) {
492                     p->data[i] = status >> (8 * i);
493                 }
494                 ret = n;
495             } else {
496                 ret = USB_RET_NAK; /* usb11 11.13.1 */
497             }
498         } else {
499             goto fail;
500         }
501         break;
502     case USB_TOKEN_OUT:
503     default:
504     fail:
505         ret = USB_RET_STALL;
506         break;
507     }
508     return ret;
509 }
510
511 static int usb_hub_broadcast_packet(USBHubState *s, USBPacket *p)
512 {
513     USBHubPort *port;
514     USBDevice *dev;
515     int i, ret;
516
517     for(i = 0; i < s->nb_ports; i++) {
518         port = &s->ports[i];
519         dev = port->port.dev;
520         if (dev && (port->wPortStatus & PORT_STAT_ENABLE)) {
521             ret = dev->handle_packet(dev, p);
522             if (ret != USB_RET_NODEV) {
523                 return ret;
524             }
525         }
526     }
527     return USB_RET_NODEV;
528 }
529
530 static int usb_hub_handle_packet(USBDevice *dev, USBPacket *p)
531 {
532     USBHubState *s = (USBHubState *)dev;
533
534 #if defined(DEBUG) && 0
535     printf("usb_hub: pid=0x%x\n", pid);
536 #endif
537     if (dev->state == USB_STATE_DEFAULT &&
538         dev->addr != 0 &&
539         p->devaddr != dev->addr &&
540         (p->pid == USB_TOKEN_SETUP ||
541          p->pid == USB_TOKEN_OUT ||
542          p->pid == USB_TOKEN_IN)) {
543         /* broadcast the packet to the devices */
544         return usb_hub_broadcast_packet(s, p);
545     }
546     return usb_generic_handle_packet(dev, p);
547 }
548
549 static void usb_hub_handle_destroy(USBDevice *dev)
550 {
551     USBHubState *s = (USBHubState *)dev;
552
553     qemu_free(s);
554 }
555
556 USBDevice *usb_hub_init(int nb_ports)
557 {
558     USBHubState *s;
559     USBHubPort *port;
560     int i;
561
562     if (nb_ports > MAX_PORTS)
563         return NULL;
564     s = qemu_mallocz(sizeof(USBHubState));
565     s->dev.speed = USB_SPEED_FULL;
566     s->dev.handle_packet = usb_hub_handle_packet;
567
568     /* generic USB device init */
569     s->dev.handle_reset = usb_hub_handle_reset;
570     s->dev.handle_control = usb_hub_handle_control;
571     s->dev.handle_data = usb_hub_handle_data;
572     s->dev.handle_destroy = usb_hub_handle_destroy;
573
574     pstrcpy(s->dev.devname, sizeof(s->dev.devname), "QEMU USB Hub");
575
576     s->nb_ports = nb_ports;
577     for(i = 0; i < s->nb_ports; i++) {
578         port = &s->ports[i];
579         qemu_register_usb_port(&port->port, s, i, usb_hub_attach);
580         port->wPortStatus = PORT_STAT_POWER;
581         port->wPortChange = 0;
582     }
583     
584     register_savevm("usb hub", -1, 0,
585                     usb_hub_save_state, usb_hub_load_state, s);
586     
587     return (USBDevice *)s;
588 }