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