usb.c 6.1 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
    port->ops->attach(port, dev);
B
bellard 已提交
32 33 34
}

/**********************/
35

B
bellard 已提交
36 37
/* generic USB device helpers (you are not forced to use them when
   writing your USB device driver, but they help handling the
38
   protocol)
B
bellard 已提交
39 40 41 42 43 44
*/

#define SETUP_STATE_IDLE 0
#define SETUP_STATE_DATA 1
#define SETUP_STATE_ACK  2

45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
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) {
62 63
        ret = s->info->handle_control(s, request, value, index, 
                                      s->setup_len, s->data_buf);
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
        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 已提交
81
{
82 83 84 85
    int request, value, index;
    int ret = 0;

    if (p->devep != 0)
86
        return s->info->handle_data(s, p);
87 88 89 90 91 92 93 94 95

    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;
96 97
            ret = s->info->handle_control(s, request, value, index,
                                          s->setup_len, s->data_buf);
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
            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)
129
        return s->info->handle_data(s, p);
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159

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

161 162 163 164 165 166 167 168
/*
 * 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 已提交
169
    switch(p->pid) {
B
bellard 已提交
170 171
    case USB_MSG_ATTACH:
        s->state = USB_STATE_ATTACHED;
172 173
        return 0;

B
bellard 已提交
174 175
    case USB_MSG_DETACH:
        s->state = USB_STATE_NOTATTACHED;
176 177
        return 0;

B
bellard 已提交
178 179 180 181
    case USB_MSG_RESET:
        s->remote_wakeup = 0;
        s->addr = 0;
        s->state = USB_STATE_DEFAULT;
182
        s->info->handle_reset(s);
183 184 185 186 187 188 189 190
        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 已提交
191
    case USB_TOKEN_SETUP:
192 193
        return do_token_setup(s, p);

B
bellard 已提交
194
    case USB_TOKEN_IN:
195 196
        return do_token_in(s, p);

B
bellard 已提交
197
    case USB_TOKEN_OUT:
198 199
        return do_token_out(s, p);
 
B
bellard 已提交
200
    default:
201
        return USB_RET_STALL;
B
bellard 已提交
202 203 204 205 206 207 208 209 210 211 212
    }
}

/* 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 已提交
213
    *q++ = 2 * len + 2;
B
bellard 已提交
214 215 216 217 218 219 220
    *q++ = 3;
    for(i = 0; i < len; i++) {
        *q++ = str[i];
        *q++ = 0;
    }
    return q - buf;
}
P
pbrook 已提交
221 222 223 224 225 226 227

/* 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;
228
    dev->info->handle_packet(dev, &p);
P
pbrook 已提交
229

230 231
    /* This _must_ be synchronous */
}