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)
34 usb_attach(port, NULL);
38 port->ops->attach(port);
39 usb_send_msg(dev, USB_MSG_ATTACH);
43 port->ops->detach(port);
45 usb_send_msg(dev, USB_MSG_DETACH);
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
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;
79 memcpy(s->setup_buf, p->data, 8);
80 s->setup_len = (s->setup_buf[7] << 8) | s->setup_buf[6];
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;
97 if (ret < s->setup_len)
99 s->setup_state = SETUP_STATE_DATA;
101 if (s->setup_len > sizeof(s->data_buf)) {
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;
110 s->setup_state = SETUP_STATE_DATA;
116 static int do_token_in(USBDevice *s, USBPacket *p)
118 int request, value, index;
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;
145 case SETUP_STATE_DATA:
146 if (s->setup_buf[0] & USB_DIR_IN) {
147 int len = s->setup_len - s->setup_index;
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;
157 s->setup_state = SETUP_STATE_IDLE;
158 return USB_RET_STALL;
161 return USB_RET_STALL;
165 static int do_token_out(USBDevice *s, USBPacket *p)
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;
176 /* ignore additional output */
180 case SETUP_STATE_DATA:
181 if (!(s->setup_buf[0] & USB_DIR_IN)) {
182 int len = s->setup_len - s->setup_index;
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;
192 s->setup_state = SETUP_STATE_IDLE;
193 return USB_RET_STALL;
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)
210 s->state = USB_STATE_ATTACHED;
211 if (s->info->handle_attach) {
212 s->info->handle_attach(s);
217 s->state = USB_STATE_NOTATTACHED;
221 s->remote_wakeup = 0;
223 s->state = USB_STATE_DEFAULT;
224 if (s->info->handle_reset) {
225 s->info->handle_reset(s);
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;
235 case USB_TOKEN_SETUP:
236 return do_token_setup(s, p);
239 return do_token_in(s, p);
242 return do_token_out(s, p);
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)
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;
268 case SETUP_STATE_ACK:
269 s->setup_state = SETUP_STATE_IDLE;
276 usb_packet_complete(s, p);
279 /* XXX: fix overflow */
280 int set_usb_string(uint8_t *buf, const char *str)
289 for(i = 0; i < len; i++) {
296 /* Send an internal message to a USB device. */
297 void usb_send_msg(USBDevice *dev, int msg)
302 memset(&p, 0, sizeof(p));
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)
316 assert(p->owner == NULL);
317 ret = dev->info->handle_packet(dev, p);
318 if (ret == USB_RET_ASYNC) {
319 if (p->owner == NULL) {
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
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
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);
342 /* Cancel an active packet. The packed must have been deferred by
343 returning USB_RET_ASYNC from handle_packet, and not yet
345 void usb_cancel_packet(USBPacket * p)
347 assert(p->owner != NULL);
348 p->owner->info->cancel_packet(p->owner, p);