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
26 void usb_attach(USBPort *port, USBDevice *dev)
28 port->attach(port, dev);
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
37 #define SETUP_STATE_IDLE 0
38 #define SETUP_STATE_DATA 1
39 #define SETUP_STATE_ACK 2
41 int usb_generic_handle_packet(USBDevice *s, int pid,
42 uint8_t devaddr, uint8_t devep,
43 uint8_t *data, int len)
49 s->state = USB_STATE_ATTACHED;
52 s->state = USB_STATE_NOTATTACHED;
57 s->state = USB_STATE_DEFAULT;
61 if (s->state < USB_STATE_DEFAULT || devaddr != s->addr)
65 memcpy(s->setup_buf, data, 8);
66 s->setup_len = (s->setup_buf[7] << 8) | s->setup_buf[6];
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],
77 if (ret < s->setup_len)
79 s->setup_state = SETUP_STATE_DATA;
81 if (s->setup_len == 0)
82 s->setup_state = SETUP_STATE_ACK;
84 s->setup_state = SETUP_STATE_DATA;
88 if (s->state < USB_STATE_DEFAULT || devaddr != s->addr)
92 switch(s->setup_state) {
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],
108 case SETUP_STATE_DATA:
109 if (s->setup_buf[0] & USB_DIR_IN) {
110 l = s->setup_len - s->setup_index;
113 memcpy(data, s->data_buf + s->setup_index, l);
115 if (s->setup_index >= s->setup_len)
116 s->setup_state = SETUP_STATE_ACK;
119 s->setup_state = SETUP_STATE_IDLE;
128 ret = s->handle_data(s, pid, devep, data, len);
133 if (s->state < USB_STATE_DEFAULT || devaddr != s->addr)
134 return USB_RET_NODEV;
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) {
146 case SETUP_STATE_DATA:
147 if (!(s->setup_buf[0] & USB_DIR_IN)) {
148 l = s->setup_len - s->setup_index;
151 memcpy(s->data_buf + s->setup_index, data, l);
153 if (s->setup_index >= s->setup_len)
154 s->setup_state = SETUP_STATE_ACK;
157 s->setup_state = SETUP_STATE_IDLE;
166 ret = s->handle_data(s, pid, devep, data, len);
178 /* XXX: fix overflow */
179 int set_usb_string(uint8_t *buf, const char *str)
188 for(i = 0; i < len; i++) {
195 /**********************/
196 /* USB hub emulation */
203 #define DS_CONTROL_IN 1
204 #define DS_CONTROL_OUT 2
206 typedef struct USBHubPort {
209 uint16_t wPortStatus;
210 uint16_t wPortChange;
213 typedef struct USBHubState {
216 USBHubPort ports[MAX_PORTS];
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)
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
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
244 #define PORT_CONNECTION 0
245 #define PORT_ENABLE 1
246 #define PORT_SUSPEND 2
247 #define PORT_OVERCURRENT 3
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
258 #define PORT_INDICATOR 22
260 /* same as Linux kernel root hubs */
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 */
267 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */
268 0x00, /* __u8 bDeviceSubClass; */
269 0x00, /* __u8 bDeviceProtocol; [ low/full speeds only ] */
270 0x08, /* __u8 bMaxPacketSize0; 8 Bytes */
272 0x00, 0x00, /* __u16 idVendor; */
273 0x00, 0x00, /* __u16 idProduct; */
274 0x01, 0x01, /* __u16 bcdDevice */
276 0x03, /* __u8 iManufacturer; */
277 0x02, /* __u8 iProduct; */
278 0x01, /* __u8 iSerialNumber; */
279 0x01 /* __u8 bNumConfigurations; */
282 /* XXX: patch interrupt size */
283 static const uint8_t qemu_hub_config_descriptor[] = {
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;
297 0x00, /* __u8 MaxPower; */
300 * USB 2.0, single TT organization (mandatory):
301 * one interface, protocol 0
303 * USB 2.0, multiple TT organization (optional):
304 * two interfaces, protocols 1 (like single TT)
305 * and 2 (multiple TT mode) ... config is
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; */
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) */
330 static const uint8_t qemu_hub_hub_descriptor[] =
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 *** */
343 static void usb_hub_attach(USBPort *port1, USBDevice *dev)
345 USBHubState *s = port1->opaque;
346 USBHubPort *port = &s->ports[port1->index];
350 usb_attach(port1, NULL);
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;
357 port->wPortStatus &= ~PORT_STAT_LOW_SPEED;
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;
373 static void usb_hub_handle_reset(USBDevice *dev)
378 static int usb_hub_handle_control(USBDevice *dev, int request, int value,
379 int index, int length, uint8_t *data)
381 USBHubState *s = (USBHubState *)dev;
385 case DeviceRequest | USB_REQ_GET_STATUS:
386 data[0] = (1 << USB_DEVICE_SELF_POWERED) |
387 (dev->remote_wakeup << USB_DEVICE_REMOTE_WAKEUP);
391 case DeviceOutRequest | USB_REQ_CLEAR_FEATURE:
392 if (value == USB_DEVICE_REMOTE_WAKEUP) {
393 dev->remote_wakeup = 0;
399 case DeviceOutRequest | USB_REQ_SET_FEATURE:
400 if (value == USB_DEVICE_REMOTE_WAKEUP) {
401 dev->remote_wakeup = 1;
407 case DeviceOutRequest | USB_REQ_SET_ADDRESS:
411 case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
414 memcpy(data, qemu_hub_dev_descriptor,
415 sizeof(qemu_hub_dev_descriptor));
416 ret = sizeof(qemu_hub_dev_descriptor);
419 memcpy(data, qemu_hub_config_descriptor,
420 sizeof(qemu_hub_config_descriptor));
421 ret = sizeof(qemu_hub_config_descriptor);
424 switch(value & 0xff) {
435 ret = set_usb_string(data, "314159");
438 /* product description */
439 ret = set_usb_string(data, "QEMU USB Hub");
442 /* vendor description */
443 ret = set_usb_string(data, "QEMU " QEMU_VERSION);
453 case DeviceRequest | USB_REQ_GET_CONFIGURATION:
457 case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
460 case DeviceRequest | USB_REQ_GET_INTERFACE:
464 case DeviceOutRequest | USB_REQ_SET_INTERFACE:
467 /* usb specific requests */
477 unsigned int n = index - 1;
479 if (n >= s->nb_ports)
482 data[0] = port->wPortStatus;
483 data[1] = port->wPortStatus >> 8;
484 data[2] = port->wPortChange;
485 data[3] = port->wPortChange >> 8;
490 case ClearHubFeature:
491 if (value == 0 || value == 1) {
499 unsigned int n = index - 1;
502 if (n >= s->nb_ports)
508 port->wPortStatus |= PORT_STAT_SUSPEND;
512 dev->handle_packet(dev,
513 USB_MSG_RESET, 0, 0, NULL, 0);
514 port->wPortChange |= PORT_STAT_C_RESET;
516 port->wPortChange |= PORT_STAT_C_ENABLE;
517 port->wPortStatus |= PORT_STAT_ENABLE;
528 case ClearPortFeature:
530 unsigned int n = index - 1;
533 if (n >= s->nb_ports)
539 port->wPortStatus &= ~PORT_STAT_ENABLE;
542 port->wPortChange &= ~PORT_STAT_C_ENABLE;
545 port->wPortStatus &= ~PORT_STAT_SUSPEND;
548 port->wPortChange &= ~PORT_STAT_C_SUSPEND;
550 case PORT_C_CONNECTION:
551 port->wPortChange &= ~PORT_STAT_C_CONNECTION;
553 case PORT_C_OVERCURRENT:
554 port->wPortChange &= ~PORT_STAT_C_OVERCURRENT;
557 port->wPortChange &= ~PORT_STAT_C_RESET;
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);
578 static int usb_hub_handle_data(USBDevice *dev, int pid,
579 uint8_t devep, uint8_t *data, int len)
581 USBHubState *s = (USBHubState *)dev;
590 n = (s->nb_ports + 1 + 7) / 8;
592 return USB_RET_BABBLE;
594 for(i = 0; i < s->nb_ports; i++) {
596 if (port->wPortChange)
597 status |= (1 << (i + 1));
600 for(i = 0; i < n; i++) {
601 data[i] = status >> (8 * i);
620 static int usb_hub_broadcast_packet(USBHubState *s, int pid,
621 uint8_t devaddr, uint8_t devep,
622 uint8_t *data, int len)
628 for(i = 0; i < s->nb_ports; i++) {
631 if (dev && (port->wPortStatus & PORT_STAT_ENABLE)) {
632 ret = dev->handle_packet(dev, pid,
635 if (ret != USB_RET_NODEV) {
640 return USB_RET_NODEV;
643 static int usb_hub_handle_packet(USBDevice *dev, int pid,
644 uint8_t devaddr, uint8_t devep,
645 uint8_t *data, int len)
647 USBHubState *s = (USBHubState *)dev;
649 #if defined(DEBUG) && 0
650 printf("usb_hub: pid=0x%x\n", pid);
652 if (dev->state == USB_STATE_DEFAULT &&
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);
661 return usb_generic_handle_packet(dev, pid, devaddr, devep, data, len);
664 USBDevice *usb_hub_init(USBPort **usb_ports, int nb_ports)
670 if (nb_ports > MAX_PORTS)
672 s = qemu_mallocz(sizeof(USBHubState));
675 s->dev.speed = USB_SPEED_FULL;
676 s->dev.handle_packet = usb_hub_handle_packet;
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;
683 s->nb_ports = nb_ports;
684 for(i = 0; i < s->nb_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;
693 return (USBDevice *)s;