usb.c 9.5 KB
Newer Older
B
bellard 已提交
1 2 3 4
/*
 * QEMU USB emulation
 *
 * Copyright (c) 2005 Fabrice Bellard
5
 *
6 7
 * 2008 Generic packet handler rewrite by Max Krasnyansky
 *
B
bellard 已提交
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
P
pbrook 已提交
26 27
#include "qemu-common.h"
#include "usb.h"
B
bellard 已提交
28 29 30

void usb_attach(USBPort *port, USBDevice *dev)
{
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
    if (dev != NULL) {
        /* attach */
        if (port->dev) {
            usb_attach(port, NULL);
        }
        dev->port = port;
        port->dev = dev;
        port->ops->attach(port);
        usb_send_msg(dev, USB_MSG_ATTACH);
    } else {
        /* detach */
        dev = port->dev;
        port->ops->detach(port);
        if (dev) {
            usb_send_msg(dev, USB_MSG_DETACH);
            dev->port = NULL;
            port->dev = NULL;
        }
    }
B
bellard 已提交
50 51
}

52 53 54
void usb_wakeup(USBDevice *dev)
{
    if (dev->remote_wakeup && dev->port && dev->port->ops->wakeup) {
55
        dev->port->ops->wakeup(dev->port);
56 57 58
    }
}

B
bellard 已提交
59
/**********************/
60

B
bellard 已提交
61 62
/* generic USB device helpers (you are not forced to use them when
   writing your USB device driver, but they help handling the
63
   protocol)
B
bellard 已提交
64 65
*/

66 67 68 69
#define SETUP_STATE_IDLE  0
#define SETUP_STATE_SETUP 1
#define SETUP_STATE_DATA  2
#define SETUP_STATE_ACK   3
B
bellard 已提交
70

71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
static int do_token_setup(USBDevice *s, USBPacket *p)
{
    int request, value, index;
    int ret = 0;

    if (p->len != 8)
        return USB_RET_STALL;
 
    memcpy(s->setup_buf, p->data, 8);
    s->setup_len   = (s->setup_buf[7] << 8) | s->setup_buf[6];
    s->setup_index = 0;

    request = (s->setup_buf[0] << 8) | s->setup_buf[1];
    value   = (s->setup_buf[3] << 8) | s->setup_buf[2];
    index   = (s->setup_buf[5] << 8) | s->setup_buf[4];
86

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 91 92 93
        if (ret == USB_RET_ASYNC) {
             s->setup_state = SETUP_STATE_SETUP;
             return USB_RET_ASYNC;
        }
94 95 96 97 98 99 100
        if (ret < 0)
            return ret;

        if (ret < s->setup_len)
            s->setup_len = ret;
        s->setup_state = SETUP_STATE_DATA;
    } else {
H
Hans de Goede 已提交
101 102 103 104 105 106
        if (s->setup_len > sizeof(s->data_buf)) {
            fprintf(stderr,
                "usb_generic_handle_packet: ctrl buffer too small (%d > %zu)\n",
                s->setup_len, sizeof(s->data_buf));
            return USB_RET_STALL;
        }
107 108 109 110 111 112 113 114 115 116
        if (s->setup_len == 0)
            s->setup_state = SETUP_STATE_ACK;
        else
            s->setup_state = SETUP_STATE_DATA;
    }

    return ret;
}

static int do_token_in(USBDevice *s, USBPacket *p)
B
bellard 已提交
117
{
118 119 120 121
    int request, value, index;
    int ret = 0;

    if (p->devep != 0)
122
        return s->info->handle_data(s, p);
123 124 125 126 127 128 129 130

    request = (s->setup_buf[0] << 8) | s->setup_buf[1];
    value   = (s->setup_buf[3] << 8) | s->setup_buf[2];
    index   = (s->setup_buf[5] << 8) | s->setup_buf[4];
 
    switch(s->setup_state) {
    case SETUP_STATE_ACK:
        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 134 135 136
            if (ret == USB_RET_ASYNC) {
                return USB_RET_ASYNC;
            }
            s->setup_state = SETUP_STATE_IDLE;
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
            if (ret > 0)
                return 0;
            return ret;
        }

        /* return 0 byte */
        return 0;

    case SETUP_STATE_DATA:
        if (s->setup_buf[0] & USB_DIR_IN) {
            int len = s->setup_len - s->setup_index;
            if (len > p->len)
                len = p->len;
            memcpy(p->data, s->data_buf + s->setup_index, len);
            s->setup_index += len;
            if (s->setup_index >= s->setup_len)
                s->setup_state = SETUP_STATE_ACK;
            return len;
        }

        s->setup_state = SETUP_STATE_IDLE;
        return USB_RET_STALL;

    default:
        return USB_RET_STALL;
    }
}

static int do_token_out(USBDevice *s, USBPacket *p)
{
    if (p->devep != 0)
168
        return s->info->handle_data(s, p);
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198

    switch(s->setup_state) {
    case SETUP_STATE_ACK:
        if (s->setup_buf[0] & USB_DIR_IN) {
            s->setup_state = SETUP_STATE_IDLE;
            /* transfer OK */
        } else {
            /* ignore additional output */
        }
        return 0;

    case SETUP_STATE_DATA:
        if (!(s->setup_buf[0] & USB_DIR_IN)) {
            int len = s->setup_len - s->setup_index;
            if (len > p->len)
                len = p->len;
            memcpy(s->data_buf + s->setup_index, p->data, len);
            s->setup_index += len;
            if (s->setup_index >= s->setup_len)
                s->setup_state = SETUP_STATE_ACK;
            return len;
        }

        s->setup_state = SETUP_STATE_IDLE;
        return USB_RET_STALL;

    default:
        return USB_RET_STALL;
    }
}
B
bellard 已提交
199

200 201 202 203 204 205 206 207
/*
 * Generic packet handler.
 * Called by the HC (host controller).
 *
 * Returns length of the transaction or one of the USB_RET_XXX codes.
 */
int usb_generic_handle_packet(USBDevice *s, USBPacket *p)
{
P
pbrook 已提交
208
    switch(p->pid) {
B
bellard 已提交
209 210
    case USB_MSG_ATTACH:
        s->state = USB_STATE_ATTACHED;
G
Gerd Hoffmann 已提交
211 212 213
        if (s->info->handle_attach) {
            s->info->handle_attach(s);
        }
214 215
        return 0;

B
bellard 已提交
216 217
    case USB_MSG_DETACH:
        s->state = USB_STATE_NOTATTACHED;
218 219
        return 0;

B
bellard 已提交
220 221 222 223
    case USB_MSG_RESET:
        s->remote_wakeup = 0;
        s->addr = 0;
        s->state = USB_STATE_DEFAULT;
G
Gerd Hoffmann 已提交
224 225 226
        if (s->info->handle_reset) {
            s->info->handle_reset(s);
        }
227 228 229 230 231 232 233 234
        return 0;
    }

    /* Rest of the PIDs must match our address */
    if (s->state < USB_STATE_DEFAULT || p->devaddr != s->addr)
        return USB_RET_NODEV;

    switch (p->pid) {
B
bellard 已提交
235
    case USB_TOKEN_SETUP:
236 237
        return do_token_setup(s, p);

B
bellard 已提交
238
    case USB_TOKEN_IN:
239 240
        return do_token_in(s, p);

B
bellard 已提交
241
    case USB_TOKEN_OUT:
242 243
        return do_token_out(s, p);
 
B
bellard 已提交
244
    default:
245
        return USB_RET_STALL;
B
bellard 已提交
246 247 248
    }
}

249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
/* ctrl complete function for devices which use usb_generic_handle_packet and
   may return USB_RET_ASYNC from their handle_control callback. Device code
   which does this *must* call this function instead of the normal
   usb_packet_complete to complete their async control packets. */
void usb_generic_async_ctrl_complete(USBDevice *s, USBPacket *p)
{
    if (p->len < 0) {
        s->setup_state = SETUP_STATE_IDLE;
    }

    switch (s->setup_state) {
    case SETUP_STATE_SETUP:
        if (p->len < s->setup_len) {
            s->setup_len = p->len;
        }
        s->setup_state = SETUP_STATE_DATA;
        p->len = 8;
        break;

    case SETUP_STATE_ACK:
        s->setup_state = SETUP_STATE_IDLE;
        p->len = 0;
        break;

    default:
        break;
    }
    usb_packet_complete(s, p);
}

B
bellard 已提交
279 280 281 282 283 284 285 286
/* XXX: fix overflow */
int set_usb_string(uint8_t *buf, const char *str)
{
    int len, i;
    uint8_t *q;

    q = buf;
    len = strlen(str);
P
pbrook 已提交
287
    *q++ = 2 * len + 2;
B
bellard 已提交
288 289 290 291 292 293 294
    *q++ = 3;
    for(i = 0; i < len; i++) {
        *q++ = str[i];
        *q++ = 0;
    }
    return q - buf;
}
P
pbrook 已提交
295 296 297 298 299

/* Send an internal message to a USB device.  */
void usb_send_msg(USBDevice *dev, int msg)
{
    USBPacket p;
G
Gerd Hoffmann 已提交
300 301
    int ret;

P
pbrook 已提交
302 303
    memset(&p, 0, sizeof(p));
    p.pid = msg;
G
Gerd Hoffmann 已提交
304
    ret = usb_handle_packet(dev, &p);
305
    /* This _must_ be synchronous */
G
Gerd Hoffmann 已提交
306 307 308 309 310 311 312 313 314 315
    assert(ret != USB_RET_ASYNC);
}

/* Hand over a packet to a device for processing.  Return value
   USB_RET_ASYNC indicates the processing isn't finished yet, the
   driver will call usb_packet_complete() when done processing it. */
int usb_handle_packet(USBDevice *dev, USBPacket *p)
{
    int ret;

G
Gerd Hoffmann 已提交
316
    assert(p->owner == NULL);
G
Gerd Hoffmann 已提交
317
    ret = dev->info->handle_packet(dev, p);
G
Gerd Hoffmann 已提交
318 319 320 321 322 323 324 325 326 327
    if (ret == USB_RET_ASYNC) {
        if (p->owner == NULL) {
            p->owner = dev;
        } else {
            /* We'll end up here when usb_handle_packet is called
             * recursively due to a hub being in the chain.  Nothing
             * to do.  Leave p->owner pointing to the device, not the
             * hub. */;
        }
    }
G
Gerd Hoffmann 已提交
328
    return ret;
329
}
G
Gerd Hoffmann 已提交
330 331 332 333 334 335 336 337

/* Notify the controller that an async packet is complete.  This should only
   be called for packets previously deferred by returning USB_RET_ASYNC from
   handle_packet. */
void usb_packet_complete(USBDevice *dev, USBPacket *p)
{
    /* Note: p->owner != dev is possible in case dev is a hub */
    assert(p->owner != NULL);
338
    dev->port->ops->complete(dev->port, p);
G
Gerd Hoffmann 已提交
339 340 341 342 343 344 345 346 347
    p->owner = NULL;
}

/* Cancel an active packet.  The packed must have been deferred by
   returning USB_RET_ASYNC from handle_packet, and not yet
   completed.  */
void usb_cancel_packet(USBPacket * p)
{
    assert(p->owner != NULL);
348
    p->owner->info->cancel_packet(p->owner, p);
G
Gerd Hoffmann 已提交
349 350
    p->owner = NULL;
}