usb.c 6.8 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 55 56 57 58
void usb_wakeup(USBDevice *dev)
{
    if (dev->remote_wakeup && dev->port && dev->port->ops->wakeup) {
        dev->port->ops->wakeup(dev);
    }
}

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_DATA 1
#define SETUP_STATE_ACK  2

70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
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];
 
    if (s->setup_buf[0] & USB_DIR_IN) {
87 88
        ret = s->info->handle_control(s, request, value, index, 
                                      s->setup_len, s->data_buf);
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
        if (ret < 0)
            return ret;

        if (ret < s->setup_len)
            s->setup_len = ret;
        s->setup_state = SETUP_STATE_DATA;
    } else {
        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 已提交
106
{
107 108 109 110
    int request, value, index;
    int ret = 0;

    if (p->devep != 0)
111
        return s->info->handle_data(s, p);
112 113 114 115 116 117 118 119 120

    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)) {
            s->setup_state = SETUP_STATE_IDLE;
121 122
            ret = s->info->handle_control(s, request, value, index,
                                          s->setup_len, s->data_buf);
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
            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)
154
        return s->info->handle_data(s, p);
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184

    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 已提交
185

186 187 188 189 190 191 192 193
/*
 * 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 已提交
194
    switch(p->pid) {
B
bellard 已提交
195 196
    case USB_MSG_ATTACH:
        s->state = USB_STATE_ATTACHED;
G
Gerd Hoffmann 已提交
197 198 199
        if (s->info->handle_attach) {
            s->info->handle_attach(s);
        }
200 201
        return 0;

B
bellard 已提交
202 203
    case USB_MSG_DETACH:
        s->state = USB_STATE_NOTATTACHED;
204 205
        return 0;

B
bellard 已提交
206 207 208 209
    case USB_MSG_RESET:
        s->remote_wakeup = 0;
        s->addr = 0;
        s->state = USB_STATE_DEFAULT;
G
Gerd Hoffmann 已提交
210 211 212
        if (s->info->handle_reset) {
            s->info->handle_reset(s);
        }
213 214 215 216 217 218 219 220
        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 已提交
221
    case USB_TOKEN_SETUP:
222 223
        return do_token_setup(s, p);

B
bellard 已提交
224
    case USB_TOKEN_IN:
225 226
        return do_token_in(s, p);

B
bellard 已提交
227
    case USB_TOKEN_OUT:
228 229
        return do_token_out(s, p);
 
B
bellard 已提交
230
    default:
231
        return USB_RET_STALL;
B
bellard 已提交
232 233 234 235 236 237 238 239 240 241 242
    }
}

/* 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 已提交
243
    *q++ = 2 * len + 2;
B
bellard 已提交
244 245 246 247 248 249 250
    *q++ = 3;
    for(i = 0; i < len; i++) {
        *q++ = str[i];
        *q++ = 0;
    }
    return q - buf;
}
P
pbrook 已提交
251 252 253 254 255 256 257

/* Send an internal message to a USB device.  */
void usb_send_msg(USBDevice *dev, int msg)
{
    USBPacket p;
    memset(&p, 0, sizeof(p));
    p.pid = msg;
258
    dev->info->handle_packet(dev, &p);
P
pbrook 已提交
259

260 261
    /* This _must_ be synchronous */
}