usb-linux: Teach about super speed
[qemu/ar7.git] / hw / usb.c
blob4a39cbcc7d2e609b2844b64eeefb69dab400c11c
1 /*
2 * QEMU USB emulation
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
24 * THE SOFTWARE.
26 #include "qemu-common.h"
27 #include "usb.h"
29 void usb_attach(USBPort *port, USBDevice *dev)
31 if (dev != NULL) {
32 /* attach */
33 if (port->dev) {
34 usb_attach(port, NULL);
36 dev->port = port;
37 port->dev = dev;
38 port->ops->attach(port);
39 usb_send_msg(dev, USB_MSG_ATTACH);
40 } else {
41 /* detach */
42 dev = port->dev;
43 port->ops->detach(port);
44 if (dev) {
45 usb_send_msg(dev, USB_MSG_DETACH);
46 dev->port = NULL;
47 port->dev = NULL;
52 void usb_wakeup(USBDevice *dev)
54 if (dev->remote_wakeup && dev->port && dev->port->ops->wakeup) {
55 dev->port->ops->wakeup(dev);
59 /**********************/
61 /* generic USB device helpers (you are not forced to use them when
62 writing your USB device driver, but they help handling the
63 protocol)
66 #define SETUP_STATE_IDLE 0
67 #define SETUP_STATE_SETUP 1
68 #define SETUP_STATE_DATA 2
69 #define SETUP_STATE_ACK 3
71 static int do_token_setup(USBDevice *s, USBPacket *p)
73 int request, value, index;
74 int ret = 0;
76 if (p->len != 8)
77 return USB_RET_STALL;
79 memcpy(s->setup_buf, p->data, 8);
80 s->setup_len = (s->setup_buf[7] << 8) | s->setup_buf[6];
81 s->setup_index = 0;
83 request = (s->setup_buf[0] << 8) | s->setup_buf[1];
84 value = (s->setup_buf[3] << 8) | s->setup_buf[2];
85 index = (s->setup_buf[5] << 8) | s->setup_buf[4];
87 if (s->setup_buf[0] & USB_DIR_IN) {
88 ret = s->info->handle_control(s, p, request, value, index,
89 s->setup_len, s->data_buf);
90 if (ret == USB_RET_ASYNC) {
91 s->setup_state = SETUP_STATE_SETUP;
92 return USB_RET_ASYNC;
94 if (ret < 0)
95 return ret;
97 if (ret < s->setup_len)
98 s->setup_len = ret;
99 s->setup_state = SETUP_STATE_DATA;
100 } else {
101 if (s->setup_len > sizeof(s->data_buf)) {
102 fprintf(stderr,
103 "usb_generic_handle_packet: ctrl buffer too small (%d > %zu)\n",
104 s->setup_len, sizeof(s->data_buf));
105 return USB_RET_STALL;
107 if (s->setup_len == 0)
108 s->setup_state = SETUP_STATE_ACK;
109 else
110 s->setup_state = SETUP_STATE_DATA;
113 return ret;
116 static int do_token_in(USBDevice *s, USBPacket *p)
118 int request, value, index;
119 int ret = 0;
121 if (p->devep != 0)
122 return s->info->handle_data(s, p);
124 request = (s->setup_buf[0] << 8) | s->setup_buf[1];
125 value = (s->setup_buf[3] << 8) | s->setup_buf[2];
126 index = (s->setup_buf[5] << 8) | s->setup_buf[4];
128 switch(s->setup_state) {
129 case SETUP_STATE_ACK:
130 if (!(s->setup_buf[0] & USB_DIR_IN)) {
131 ret = s->info->handle_control(s, p, request, value, index,
132 s->setup_len, s->data_buf);
133 if (ret == USB_RET_ASYNC) {
134 return USB_RET_ASYNC;
136 s->setup_state = SETUP_STATE_IDLE;
137 if (ret > 0)
138 return 0;
139 return ret;
142 /* return 0 byte */
143 return 0;
145 case SETUP_STATE_DATA:
146 if (s->setup_buf[0] & USB_DIR_IN) {
147 int len = s->setup_len - s->setup_index;
148 if (len > p->len)
149 len = p->len;
150 memcpy(p->data, s->data_buf + s->setup_index, len);
151 s->setup_index += len;
152 if (s->setup_index >= s->setup_len)
153 s->setup_state = SETUP_STATE_ACK;
154 return len;
157 s->setup_state = SETUP_STATE_IDLE;
158 return USB_RET_STALL;
160 default:
161 return USB_RET_STALL;
165 static int do_token_out(USBDevice *s, USBPacket *p)
167 if (p->devep != 0)
168 return s->info->handle_data(s, p);
170 switch(s->setup_state) {
171 case SETUP_STATE_ACK:
172 if (s->setup_buf[0] & USB_DIR_IN) {
173 s->setup_state = SETUP_STATE_IDLE;
174 /* transfer OK */
175 } else {
176 /* ignore additional output */
178 return 0;
180 case SETUP_STATE_DATA:
181 if (!(s->setup_buf[0] & USB_DIR_IN)) {
182 int len = s->setup_len - s->setup_index;
183 if (len > p->len)
184 len = p->len;
185 memcpy(s->data_buf + s->setup_index, p->data, len);
186 s->setup_index += len;
187 if (s->setup_index >= s->setup_len)
188 s->setup_state = SETUP_STATE_ACK;
189 return len;
192 s->setup_state = SETUP_STATE_IDLE;
193 return USB_RET_STALL;
195 default:
196 return USB_RET_STALL;
201 * Generic packet handler.
202 * Called by the HC (host controller).
204 * Returns length of the transaction or one of the USB_RET_XXX codes.
206 int usb_generic_handle_packet(USBDevice *s, USBPacket *p)
208 switch(p->pid) {
209 case USB_MSG_ATTACH:
210 s->state = USB_STATE_ATTACHED;
211 if (s->info->handle_attach) {
212 s->info->handle_attach(s);
214 return 0;
216 case USB_MSG_DETACH:
217 s->state = USB_STATE_NOTATTACHED;
218 return 0;
220 case USB_MSG_RESET:
221 s->remote_wakeup = 0;
222 s->addr = 0;
223 s->state = USB_STATE_DEFAULT;
224 if (s->info->handle_reset) {
225 s->info->handle_reset(s);
227 return 0;
230 /* Rest of the PIDs must match our address */
231 if (s->state < USB_STATE_DEFAULT || p->devaddr != s->addr)
232 return USB_RET_NODEV;
234 switch (p->pid) {
235 case USB_TOKEN_SETUP:
236 return do_token_setup(s, p);
238 case USB_TOKEN_IN:
239 return do_token_in(s, p);
241 case USB_TOKEN_OUT:
242 return do_token_out(s, p);
244 default:
245 return USB_RET_STALL;
249 /* ctrl complete function for devices which use usb_generic_handle_packet and
250 may return USB_RET_ASYNC from their handle_control callback. Device code
251 which does this *must* call this function instead of the normal
252 usb_packet_complete to complete their async control packets. */
253 void usb_generic_async_ctrl_complete(USBDevice *s, USBPacket *p)
255 if (p->len < 0) {
256 s->setup_state = SETUP_STATE_IDLE;
259 switch (s->setup_state) {
260 case SETUP_STATE_SETUP:
261 if (p->len < s->setup_len) {
262 s->setup_len = p->len;
264 s->setup_state = SETUP_STATE_DATA;
265 p->len = 8;
266 break;
268 case SETUP_STATE_ACK:
269 s->setup_state = SETUP_STATE_IDLE;
270 p->len = 0;
271 break;
273 default:
274 break;
276 usb_packet_complete(s, p);
279 /* XXX: fix overflow */
280 int set_usb_string(uint8_t *buf, const char *str)
282 int len, i;
283 uint8_t *q;
285 q = buf;
286 len = strlen(str);
287 *q++ = 2 * len + 2;
288 *q++ = 3;
289 for(i = 0; i < len; i++) {
290 *q++ = str[i];
291 *q++ = 0;
293 return q - buf;
296 /* Send an internal message to a USB device. */
297 void usb_send_msg(USBDevice *dev, int msg)
299 USBPacket p;
300 int ret;
302 memset(&p, 0, sizeof(p));
303 p.pid = msg;
304 ret = usb_handle_packet(dev, &p);
305 /* This _must_ be synchronous */
306 assert(ret != USB_RET_ASYNC);
309 /* Hand over a packet to a device for processing. Return value
310 USB_RET_ASYNC indicates the processing isn't finished yet, the
311 driver will call usb_packet_complete() when done processing it. */
312 int usb_handle_packet(USBDevice *dev, USBPacket *p)
314 int ret;
316 assert(p->owner == NULL);
317 ret = dev->info->handle_packet(dev, p);
318 if (ret == USB_RET_ASYNC) {
319 if (p->owner == NULL) {
320 p->owner = dev;
321 } else {
322 /* We'll end up here when usb_handle_packet is called
323 * recursively due to a hub being in the chain. Nothing
324 * to do. Leave p->owner pointing to the device, not the
325 * hub. */;
328 return ret;
331 /* Notify the controller that an async packet is complete. This should only
332 be called for packets previously deferred by returning USB_RET_ASYNC from
333 handle_packet. */
334 void usb_packet_complete(USBDevice *dev, USBPacket *p)
336 /* Note: p->owner != dev is possible in case dev is a hub */
337 assert(p->owner != NULL);
338 dev->port->ops->complete(dev, p);
339 p->owner = NULL;
342 /* Cancel an active packet. The packed must have been deferred by
343 returning USB_RET_ASYNC from handle_packet, and not yet
344 completed. */
345 void usb_cancel_packet(USBPacket * p)
347 assert(p->owner != NULL);
348 p->owner->info->cancel_packet(p->owner, p);
349 p->owner = NULL;