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