4 * Copyright (c) 2005 Fabrice Bellard
6 * 2008 Generic packet handler rewrite by Max Krasnyansky
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 #include "qemu-common.h"
29 void usb_attach(USBPort *port, USBDevice *dev)
31 port->attach(port, dev);
34 /**********************/
36 /* generic USB device helpers (you are not forced to use them when
37 writing your USB device driver, but they help handling the
41 #define SETUP_STATE_IDLE 0
42 #define SETUP_STATE_DATA 1
43 #define SETUP_STATE_ACK 2
45 static int do_token_setup(USBDevice *s, USBPacket *p)
47 int request, value, index;
53 memcpy(s->setup_buf, p->data, 8);
54 s->setup_len = (s->setup_buf[7] << 8) | s->setup_buf[6];
57 request = (s->setup_buf[0] << 8) | s->setup_buf[1];
58 value = (s->setup_buf[3] << 8) | s->setup_buf[2];
59 index = (s->setup_buf[5] << 8) | s->setup_buf[4];
61 if (s->setup_buf[0] & USB_DIR_IN) {
62 ret = s->info->handle_control(s, request, value, index,
63 s->setup_len, s->data_buf);
67 if (ret < s->setup_len)
69 s->setup_state = SETUP_STATE_DATA;
71 if (s->setup_len == 0)
72 s->setup_state = SETUP_STATE_ACK;
74 s->setup_state = SETUP_STATE_DATA;
80 static int do_token_in(USBDevice *s, USBPacket *p)
82 int request, value, index;
86 return s->info->handle_data(s, p);
88 request = (s->setup_buf[0] << 8) | s->setup_buf[1];
89 value = (s->setup_buf[3] << 8) | s->setup_buf[2];
90 index = (s->setup_buf[5] << 8) | s->setup_buf[4];
92 switch(s->setup_state) {
94 if (!(s->setup_buf[0] & USB_DIR_IN)) {
95 s->setup_state = SETUP_STATE_IDLE;
96 ret = s->info->handle_control(s, request, value, index,
97 s->setup_len, s->data_buf);
106 case SETUP_STATE_DATA:
107 if (s->setup_buf[0] & USB_DIR_IN) {
108 int len = s->setup_len - s->setup_index;
111 memcpy(p->data, s->data_buf + s->setup_index, len);
112 s->setup_index += len;
113 if (s->setup_index >= s->setup_len)
114 s->setup_state = SETUP_STATE_ACK;
118 s->setup_state = SETUP_STATE_IDLE;
119 return USB_RET_STALL;
122 return USB_RET_STALL;
126 static int do_token_out(USBDevice *s, USBPacket *p)
129 return s->info->handle_data(s, p);
131 switch(s->setup_state) {
132 case SETUP_STATE_ACK:
133 if (s->setup_buf[0] & USB_DIR_IN) {
134 s->setup_state = SETUP_STATE_IDLE;
137 /* ignore additional output */
141 case SETUP_STATE_DATA:
142 if (!(s->setup_buf[0] & USB_DIR_IN)) {
143 int len = s->setup_len - s->setup_index;
146 memcpy(s->data_buf + s->setup_index, p->data, len);
147 s->setup_index += len;
148 if (s->setup_index >= s->setup_len)
149 s->setup_state = SETUP_STATE_ACK;
153 s->setup_state = SETUP_STATE_IDLE;
154 return USB_RET_STALL;
157 return USB_RET_STALL;
162 * Generic packet handler.
163 * Called by the HC (host controller).
165 * Returns length of the transaction or one of the USB_RET_XXX codes.
167 int usb_generic_handle_packet(USBDevice *s, USBPacket *p)
171 s->state = USB_STATE_ATTACHED;
175 s->state = USB_STATE_NOTATTACHED;
179 s->remote_wakeup = 0;
181 s->state = USB_STATE_DEFAULT;
182 s->info->handle_reset(s);
186 /* Rest of the PIDs must match our address */
187 if (s->state < USB_STATE_DEFAULT || p->devaddr != s->addr)
188 return USB_RET_NODEV;
191 case USB_TOKEN_SETUP:
192 return do_token_setup(s, p);
195 return do_token_in(s, p);
198 return do_token_out(s, p);
201 return USB_RET_STALL;
205 /* XXX: fix overflow */
206 int set_usb_string(uint8_t *buf, const char *str)
215 for(i = 0; i < len; i++) {
222 /* Send an internal message to a USB device. */
223 void usb_send_msg(USBDevice *dev, int msg)
226 memset(&p, 0, sizeof(p));
228 dev->info->handle_packet(dev, &p);
230 /* This _must_ be synchronous */