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 */
202 typedef struct USBHubPort {
204 uint16_t wPortStatus;
205 uint16_t wPortChange;
208 typedef struct USBHubState {
211 USBHubPort ports[MAX_PORTS];
214 #define ClearHubFeature (0x2000 | USB_REQ_CLEAR_FEATURE)
215 #define ClearPortFeature (0x2300 | USB_REQ_CLEAR_FEATURE)
216 #define GetHubDescriptor (0xa000 | USB_REQ_GET_DESCRIPTOR)
217 #define GetHubStatus (0xa000 | USB_REQ_GET_STATUS)
218 #define GetPortStatus (0xa300 | USB_REQ_GET_STATUS)
219 #define SetHubFeature (0x2000 | USB_REQ_SET_FEATURE)
220 #define SetPortFeature (0x2300 | USB_REQ_SET_FEATURE)
222 #define PORT_STAT_CONNECTION 0x0001
223 #define PORT_STAT_ENABLE 0x0002
224 #define PORT_STAT_SUSPEND 0x0004
225 #define PORT_STAT_OVERCURRENT 0x0008
226 #define PORT_STAT_RESET 0x0010
227 #define PORT_STAT_POWER 0x0100
228 #define PORT_STAT_LOW_SPEED 0x0200
229 #define PORT_STAT_HIGH_SPEED 0x0400
230 #define PORT_STAT_TEST 0x0800
231 #define PORT_STAT_INDICATOR 0x1000
233 #define PORT_STAT_C_CONNECTION 0x0001
234 #define PORT_STAT_C_ENABLE 0x0002
235 #define PORT_STAT_C_SUSPEND 0x0004
236 #define PORT_STAT_C_OVERCURRENT 0x0008
237 #define PORT_STAT_C_RESET 0x0010
239 #define PORT_CONNECTION 0
240 #define PORT_ENABLE 1
241 #define PORT_SUSPEND 2
242 #define PORT_OVERCURRENT 3
245 #define PORT_LOWSPEED 9
246 #define PORT_HIGHSPEED 10
247 #define PORT_C_CONNECTION 16
248 #define PORT_C_ENABLE 17
249 #define PORT_C_SUSPEND 18
250 #define PORT_C_OVERCURRENT 19
251 #define PORT_C_RESET 20
253 #define PORT_INDICATOR 22
255 /* same as Linux kernel root hubs */
257 static const uint8_t qemu_hub_dev_descriptor[] = {
258 0x12, /* u8 bLength; */
259 0x01, /* u8 bDescriptorType; Device */
260 0x10, 0x01, /* u16 bcdUSB; v1.1 */
262 0x09, /* u8 bDeviceClass; HUB_CLASSCODE */
263 0x00, /* u8 bDeviceSubClass; */
264 0x00, /* u8 bDeviceProtocol; [ low/full speeds only ] */
265 0x08, /* u8 bMaxPacketSize0; 8 Bytes */
267 0x00, 0x00, /* u16 idVendor; */
268 0x00, 0x00, /* u16 idProduct; */
269 0x01, 0x01, /* u16 bcdDevice */
271 0x03, /* u8 iManufacturer; */
272 0x02, /* u8 iProduct; */
273 0x01, /* u8 iSerialNumber; */
274 0x01 /* u8 bNumConfigurations; */
277 /* XXX: patch interrupt size */
278 static const uint8_t qemu_hub_config_descriptor[] = {
280 /* one configuration */
281 0x09, /* u8 bLength; */
282 0x02, /* u8 bDescriptorType; Configuration */
283 0x19, 0x00, /* u16 wTotalLength; */
284 0x01, /* u8 bNumInterfaces; (1) */
285 0x01, /* u8 bConfigurationValue; */
286 0x00, /* u8 iConfiguration; */
287 0xc0, /* u8 bmAttributes;
292 0x00, /* u8 MaxPower; */
295 * USB 2.0, single TT organization (mandatory):
296 * one interface, protocol 0
298 * USB 2.0, multiple TT organization (optional):
299 * two interfaces, protocols 1 (like single TT)
300 * and 2 (multiple TT mode) ... config is
306 0x09, /* u8 if_bLength; */
307 0x04, /* u8 if_bDescriptorType; Interface */
308 0x00, /* u8 if_bInterfaceNumber; */
309 0x00, /* u8 if_bAlternateSetting; */
310 0x01, /* u8 if_bNumEndpoints; */
311 0x09, /* u8 if_bInterfaceClass; HUB_CLASSCODE */
312 0x00, /* u8 if_bInterfaceSubClass; */
313 0x00, /* u8 if_bInterfaceProtocol; [usb1.1 or single tt] */
314 0x00, /* u8 if_iInterface; */
316 /* one endpoint (status change endpoint) */
317 0x07, /* u8 ep_bLength; */
318 0x05, /* u8 ep_bDescriptorType; Endpoint */
319 0x81, /* u8 ep_bEndpointAddress; IN Endpoint 1 */
320 0x03, /* u8 ep_bmAttributes; Interrupt */
321 0x02, 0x00, /* u16 ep_wMaxPacketSize; 1 + (MAX_ROOT_PORTS / 8) */
322 0xff /* u8 ep_bInterval; (255ms -- usb 2.0 spec) */
325 static const uint8_t qemu_hub_hub_descriptor[] =
327 0x09, /* u8 bLength; */
328 0x29, /* u8 bDescriptorType; Hub-descriptor */
329 0x00, /* u8 bNbrPorts; (patched later) */
330 0x0a, /* u16 wHubCharacteristics; */
331 0x00, /* (per-port OC, no power switching) */
332 0x01, /* u8 bPwrOn2pwrGood; 2ms */
333 0x00, /* u8 bHubContrCurrent; 0 mA */
334 0x00, /* u8 DeviceRemovable; *** 7 Ports max *** */
335 0xff /* u8 PortPwrCtrlMask; *** 7 ports max *** */
338 static void usb_hub_attach(USBPort *port1, USBDevice *dev)
340 USBHubState *s = port1->opaque;
341 USBHubPort *port = &s->ports[port1->index];
345 usb_attach(port1, NULL);
347 port->wPortStatus |= PORT_STAT_CONNECTION;
348 port->wPortChange |= PORT_STAT_C_CONNECTION;
349 if (dev->speed == USB_SPEED_LOW)
350 port->wPortStatus |= PORT_STAT_LOW_SPEED;
352 port->wPortStatus &= ~PORT_STAT_LOW_SPEED;
353 port->port.dev = dev;
355 dev = port->port.dev;
357 port->wPortStatus &= ~PORT_STAT_CONNECTION;
358 port->wPortChange |= PORT_STAT_C_CONNECTION;
359 if (port->wPortStatus & PORT_STAT_ENABLE) {
360 port->wPortStatus &= ~PORT_STAT_ENABLE;
361 port->wPortChange |= PORT_STAT_C_ENABLE;
363 port->port.dev = NULL;
368 static void usb_hub_handle_reset(USBDevice *dev)
373 static int usb_hub_handle_control(USBDevice *dev, int request, int value,
374 int index, int length, uint8_t *data)
376 USBHubState *s = (USBHubState *)dev;
380 case DeviceRequest | USB_REQ_GET_STATUS:
381 data[0] = (1 << USB_DEVICE_SELF_POWERED) |
382 (dev->remote_wakeup << USB_DEVICE_REMOTE_WAKEUP);
386 case DeviceOutRequest | USB_REQ_CLEAR_FEATURE:
387 if (value == USB_DEVICE_REMOTE_WAKEUP) {
388 dev->remote_wakeup = 0;
394 case DeviceOutRequest | USB_REQ_SET_FEATURE:
395 if (value == USB_DEVICE_REMOTE_WAKEUP) {
396 dev->remote_wakeup = 1;
402 case DeviceOutRequest | USB_REQ_SET_ADDRESS:
406 case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
409 memcpy(data, qemu_hub_dev_descriptor,
410 sizeof(qemu_hub_dev_descriptor));
411 ret = sizeof(qemu_hub_dev_descriptor);
414 memcpy(data, qemu_hub_config_descriptor,
415 sizeof(qemu_hub_config_descriptor));
416 ret = sizeof(qemu_hub_config_descriptor);
419 switch(value & 0xff) {
430 ret = set_usb_string(data, "314159");
433 /* product description */
434 ret = set_usb_string(data, "QEMU USB Hub");
437 /* vendor description */
438 ret = set_usb_string(data, "QEMU " QEMU_VERSION);
448 case DeviceRequest | USB_REQ_GET_CONFIGURATION:
452 case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
455 case DeviceRequest | USB_REQ_GET_INTERFACE:
459 case DeviceOutRequest | USB_REQ_SET_INTERFACE:
462 /* usb specific requests */
472 unsigned int n = index - 1;
474 if (n >= s->nb_ports)
477 data[0] = port->wPortStatus;
478 data[1] = port->wPortStatus >> 8;
479 data[2] = port->wPortChange;
480 data[3] = port->wPortChange >> 8;
485 case ClearHubFeature:
486 if (value == 0 || value == 1) {
494 unsigned int n = index - 1;
497 if (n >= s->nb_ports)
500 dev = port->port.dev;
503 port->wPortStatus |= PORT_STAT_SUSPEND;
507 dev->handle_packet(dev,
508 USB_MSG_RESET, 0, 0, NULL, 0);
509 port->wPortChange |= PORT_STAT_C_RESET;
511 port->wPortChange |= PORT_STAT_C_ENABLE;
512 port->wPortStatus |= PORT_STAT_ENABLE;
523 case ClearPortFeature:
525 unsigned int n = index - 1;
528 if (n >= s->nb_ports)
531 dev = port->port.dev;
534 port->wPortStatus &= ~PORT_STAT_ENABLE;
537 port->wPortChange &= ~PORT_STAT_C_ENABLE;
540 port->wPortStatus &= ~PORT_STAT_SUSPEND;
543 port->wPortChange &= ~PORT_STAT_C_SUSPEND;
545 case PORT_C_CONNECTION:
546 port->wPortChange &= ~PORT_STAT_C_CONNECTION;
548 case PORT_C_OVERCURRENT:
549 port->wPortChange &= ~PORT_STAT_C_OVERCURRENT;
552 port->wPortChange &= ~PORT_STAT_C_RESET;
560 case GetHubDescriptor:
561 memcpy(data, qemu_hub_hub_descriptor,
562 sizeof(qemu_hub_hub_descriptor));
563 data[2] = s->nb_ports;
564 ret = sizeof(qemu_hub_hub_descriptor);
574 static int usb_hub_handle_data(USBDevice *dev, int pid,
575 uint8_t devep, uint8_t *data, int len)
577 USBHubState *s = (USBHubState *)dev;
586 n = (s->nb_ports + 1 + 7) / 8;
588 return USB_RET_BABBLE;
590 for(i = 0; i < s->nb_ports; i++) {
592 if (port->wPortChange)
593 status |= (1 << (i + 1));
596 for(i = 0; i < n; i++) {
597 data[i] = status >> (8 * i);
616 static int usb_hub_broadcast_packet(USBHubState *s, int pid,
617 uint8_t devaddr, uint8_t devep,
618 uint8_t *data, int len)
624 for(i = 0; i < s->nb_ports; i++) {
626 dev = port->port.dev;
627 if (dev && (port->wPortStatus & PORT_STAT_ENABLE)) {
628 ret = dev->handle_packet(dev, pid,
631 if (ret != USB_RET_NODEV) {
636 return USB_RET_NODEV;
639 static int usb_hub_handle_packet(USBDevice *dev, int pid,
640 uint8_t devaddr, uint8_t devep,
641 uint8_t *data, int len)
643 USBHubState *s = (USBHubState *)dev;
645 #if defined(DEBUG) && 0
646 printf("usb_hub: pid=0x%x\n", pid);
648 if (dev->state == USB_STATE_DEFAULT &&
650 devaddr != dev->addr &&
651 (pid == USB_TOKEN_SETUP ||
652 pid == USB_TOKEN_OUT ||
653 pid == USB_TOKEN_IN)) {
654 /* broadcast the packet to the devices */
655 return usb_hub_broadcast_packet(s, pid, devaddr, devep, data, len);
657 return usb_generic_handle_packet(dev, pid, devaddr, devep, data, len);
660 USBDevice *usb_hub_init(USBPort **usb_ports, int nb_ports)
666 if (nb_ports > MAX_PORTS)
668 s = qemu_mallocz(sizeof(USBHubState));
671 s->dev.speed = USB_SPEED_FULL;
672 s->dev.handle_packet = usb_hub_handle_packet;
674 /* generic USB device init */
675 s->dev.handle_reset = usb_hub_handle_reset;
676 s->dev.handle_control = usb_hub_handle_control;
677 s->dev.handle_data = usb_hub_handle_data;
679 s->nb_ports = nb_ports;
680 for(i = 0; i < s->nb_ports; i++) {
682 port->wPortStatus = PORT_STAT_POWER;
683 port->wPortChange = 0;
684 port->port.attach = usb_hub_attach;
685 port->port.opaque = s;
686 port->port.index = i;
687 usb_ports[i] = &port->port;
689 return (USBDevice *)s;