Statistics
| Branch: | Revision:

root / hw / usb.c @ 93148aa5

History | View | Annotate | Download (16.3 kB)

1
/*
2
 * QEMU USB emulation
3
 *
4
 * Copyright (c) 2005 Fabrice Bellard
5
 *
6
 * 2008 Generic packet handler rewrite by Max Krasnyansky
7
 *
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:
14
 *
15
 * The above copyright notice and this permission notice shall be included in
16
 * all copies or substantial portions of the Software.
17
 *
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.
25
 */
26
#include "qemu-common.h"
27
#include "usb.h"
28
#include "iov.h"
29
#include "trace.h"
30

    
31
void usb_attach(USBPort *port)
32
{
33
    USBDevice *dev = port->dev;
34

    
35
    assert(dev != NULL);
36
    assert(dev->attached);
37
    assert(dev->state == USB_STATE_NOTATTACHED);
38
    port->ops->attach(port);
39
    dev->state = USB_STATE_ATTACHED;
40
    usb_device_handle_attach(dev);
41
}
42

    
43
void usb_detach(USBPort *port)
44
{
45
    USBDevice *dev = port->dev;
46

    
47
    assert(dev != NULL);
48
    assert(dev->state != USB_STATE_NOTATTACHED);
49
    port->ops->detach(port);
50
    dev->state = USB_STATE_NOTATTACHED;
51
}
52

    
53
void usb_port_reset(USBPort *port)
54
{
55
    USBDevice *dev = port->dev;
56

    
57
    assert(dev != NULL);
58
    usb_detach(port);
59
    usb_attach(port);
60
    usb_device_reset(dev);
61
}
62

    
63
void usb_device_reset(USBDevice *dev)
64
{
65
    if (dev == NULL || !dev->attached) {
66
        return;
67
    }
68
    dev->remote_wakeup = 0;
69
    dev->addr = 0;
70
    dev->state = USB_STATE_DEFAULT;
71
    usb_device_handle_reset(dev);
72
}
73

    
74
void usb_wakeup(USBEndpoint *ep)
75
{
76
    USBDevice *dev = ep->dev;
77
    USBBus *bus = usb_bus_from_device(dev);
78

    
79
    if (dev->remote_wakeup && dev->port && dev->port->ops->wakeup) {
80
        dev->port->ops->wakeup(dev->port);
81
    }
82
    if (bus->ops->wakeup_endpoint) {
83
        bus->ops->wakeup_endpoint(bus, ep);
84
    }
85
}
86

    
87
/**********************/
88

    
89
/* generic USB device helpers (you are not forced to use them when
90
   writing your USB device driver, but they help handling the
91
   protocol)
92
*/
93

    
94
#define SETUP_STATE_IDLE  0
95
#define SETUP_STATE_SETUP 1
96
#define SETUP_STATE_DATA  2
97
#define SETUP_STATE_ACK   3
98

    
99
static int do_token_setup(USBDevice *s, USBPacket *p)
100
{
101
    int request, value, index;
102
    int ret = 0;
103

    
104
    if (p->iov.size != 8) {
105
        return USB_RET_STALL;
106
    }
107

    
108
    usb_packet_copy(p, s->setup_buf, p->iov.size);
109
    s->setup_len   = (s->setup_buf[7] << 8) | s->setup_buf[6];
110
    s->setup_index = 0;
111

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

    
116
    if (s->setup_buf[0] & USB_DIR_IN) {
117
        ret = usb_device_handle_control(s, p, request, value, index,
118
                                        s->setup_len, s->data_buf);
119
        if (ret == USB_RET_ASYNC) {
120
             s->setup_state = SETUP_STATE_SETUP;
121
             return USB_RET_ASYNC;
122
        }
123
        if (ret < 0)
124
            return ret;
125

    
126
        if (ret < s->setup_len)
127
            s->setup_len = ret;
128
        s->setup_state = SETUP_STATE_DATA;
129
    } else {
130
        if (s->setup_len > sizeof(s->data_buf)) {
131
            fprintf(stderr,
132
                "usb_generic_handle_packet: ctrl buffer too small (%d > %zu)\n",
133
                s->setup_len, sizeof(s->data_buf));
134
            return USB_RET_STALL;
135
        }
136
        if (s->setup_len == 0)
137
            s->setup_state = SETUP_STATE_ACK;
138
        else
139
            s->setup_state = SETUP_STATE_DATA;
140
    }
141

    
142
    return ret;
143
}
144

    
145
static int do_token_in(USBDevice *s, USBPacket *p)
146
{
147
    int request, value, index;
148
    int ret = 0;
149

    
150
    assert(p->ep->nr == 0);
151

    
152
    request = (s->setup_buf[0] << 8) | s->setup_buf[1];
153
    value   = (s->setup_buf[3] << 8) | s->setup_buf[2];
154
    index   = (s->setup_buf[5] << 8) | s->setup_buf[4];
155
 
156
    switch(s->setup_state) {
157
    case SETUP_STATE_ACK:
158
        if (!(s->setup_buf[0] & USB_DIR_IN)) {
159
            ret = usb_device_handle_control(s, p, request, value, index,
160
                                            s->setup_len, s->data_buf);
161
            if (ret == USB_RET_ASYNC) {
162
                return USB_RET_ASYNC;
163
            }
164
            s->setup_state = SETUP_STATE_IDLE;
165
            if (ret > 0)
166
                return 0;
167
            return ret;
168
        }
169

    
170
        /* return 0 byte */
171
        return 0;
172

    
173
    case SETUP_STATE_DATA:
174
        if (s->setup_buf[0] & USB_DIR_IN) {
175
            int len = s->setup_len - s->setup_index;
176
            if (len > p->iov.size) {
177
                len = p->iov.size;
178
            }
179
            usb_packet_copy(p, s->data_buf + s->setup_index, len);
180
            s->setup_index += len;
181
            if (s->setup_index >= s->setup_len)
182
                s->setup_state = SETUP_STATE_ACK;
183
            return len;
184
        }
185

    
186
        s->setup_state = SETUP_STATE_IDLE;
187
        return USB_RET_STALL;
188

    
189
    default:
190
        return USB_RET_STALL;
191
    }
192
}
193

    
194
static int do_token_out(USBDevice *s, USBPacket *p)
195
{
196
    assert(p->ep->nr == 0);
197

    
198
    switch(s->setup_state) {
199
    case SETUP_STATE_ACK:
200
        if (s->setup_buf[0] & USB_DIR_IN) {
201
            s->setup_state = SETUP_STATE_IDLE;
202
            /* transfer OK */
203
        } else {
204
            /* ignore additional output */
205
        }
206
        return 0;
207

    
208
    case SETUP_STATE_DATA:
209
        if (!(s->setup_buf[0] & USB_DIR_IN)) {
210
            int len = s->setup_len - s->setup_index;
211
            if (len > p->iov.size) {
212
                len = p->iov.size;
213
            }
214
            usb_packet_copy(p, s->data_buf + s->setup_index, len);
215
            s->setup_index += len;
216
            if (s->setup_index >= s->setup_len)
217
                s->setup_state = SETUP_STATE_ACK;
218
            return len;
219
        }
220

    
221
        s->setup_state = SETUP_STATE_IDLE;
222
        return USB_RET_STALL;
223

    
224
    default:
225
        return USB_RET_STALL;
226
    }
227
}
228

    
229
/* ctrl complete function for devices which use usb_generic_handle_packet and
230
   may return USB_RET_ASYNC from their handle_control callback. Device code
231
   which does this *must* call this function instead of the normal
232
   usb_packet_complete to complete their async control packets. */
233
void usb_generic_async_ctrl_complete(USBDevice *s, USBPacket *p)
234
{
235
    if (p->result < 0) {
236
        s->setup_state = SETUP_STATE_IDLE;
237
    }
238

    
239
    switch (s->setup_state) {
240
    case SETUP_STATE_SETUP:
241
        if (p->result < s->setup_len) {
242
            s->setup_len = p->result;
243
        }
244
        s->setup_state = SETUP_STATE_DATA;
245
        p->result = 8;
246
        break;
247

    
248
    case SETUP_STATE_ACK:
249
        s->setup_state = SETUP_STATE_IDLE;
250
        p->result = 0;
251
        break;
252

    
253
    default:
254
        break;
255
    }
256
    usb_packet_complete(s, p);
257
}
258

    
259
/* XXX: fix overflow */
260
int set_usb_string(uint8_t *buf, const char *str)
261
{
262
    int len, i;
263
    uint8_t *q;
264

    
265
    q = buf;
266
    len = strlen(str);
267
    *q++ = 2 * len + 2;
268
    *q++ = 3;
269
    for(i = 0; i < len; i++) {
270
        *q++ = str[i];
271
        *q++ = 0;
272
    }
273
    return q - buf;
274
}
275

    
276
USBDevice *usb_find_device(USBPort *port, uint8_t addr)
277
{
278
    USBDevice *dev = port->dev;
279

    
280
    if (dev == NULL || !dev->attached || dev->state != USB_STATE_DEFAULT) {
281
        return NULL;
282
    }
283
    if (dev->addr == addr) {
284
        return dev;
285
    }
286
    return usb_device_find_device(dev, addr);
287
}
288

    
289
static int usb_process_one(USBPacket *p)
290
{
291
    USBDevice *dev = p->ep->dev;
292

    
293
    if (p->ep->nr == 0) {
294
        /* control pipe */
295
        switch (p->pid) {
296
        case USB_TOKEN_SETUP:
297
            return do_token_setup(dev, p);
298
        case USB_TOKEN_IN:
299
            return do_token_in(dev, p);
300
        case USB_TOKEN_OUT:
301
            return do_token_out(dev, p);
302
        default:
303
            return USB_RET_STALL;
304
        }
305
    } else {
306
        /* data pipe */
307
        return usb_device_handle_data(dev, p);
308
    }
309
}
310

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

    
318
    if (dev == NULL) {
319
        return USB_RET_NODEV;
320
    }
321
    assert(dev == p->ep->dev);
322
    assert(dev->state == USB_STATE_DEFAULT);
323
    assert(p->state == USB_PACKET_SETUP);
324
    assert(p->ep != NULL);
325

    
326
    if (QTAILQ_EMPTY(&p->ep->queue)) {
327
        ret = usb_process_one(p);
328
        if (ret == USB_RET_ASYNC) {
329
            usb_packet_set_state(p, USB_PACKET_ASYNC);
330
            QTAILQ_INSERT_TAIL(&p->ep->queue, p, queue);
331
        } else {
332
            p->result = ret;
333
            usb_packet_set_state(p, USB_PACKET_COMPLETE);
334
        }
335
    } else {
336
        ret = USB_RET_ASYNC;
337
        usb_packet_set_state(p, USB_PACKET_QUEUED);
338
        QTAILQ_INSERT_TAIL(&p->ep->queue, p, queue);
339
    }
340
    return ret;
341
}
342

    
343
/* Notify the controller that an async packet is complete.  This should only
344
   be called for packets previously deferred by returning USB_RET_ASYNC from
345
   handle_packet. */
346
void usb_packet_complete(USBDevice *dev, USBPacket *p)
347
{
348
    USBEndpoint *ep = p->ep;
349
    int ret;
350

    
351
    assert(p->state == USB_PACKET_ASYNC);
352
    assert(QTAILQ_FIRST(&ep->queue) == p);
353
    usb_packet_set_state(p, USB_PACKET_COMPLETE);
354
    QTAILQ_REMOVE(&ep->queue, p, queue);
355
    dev->port->ops->complete(dev->port, p);
356

    
357
    while (!QTAILQ_EMPTY(&ep->queue)) {
358
        p = QTAILQ_FIRST(&ep->queue);
359
        assert(p->state == USB_PACKET_QUEUED);
360
        ret = usb_process_one(p);
361
        if (ret == USB_RET_ASYNC) {
362
            usb_packet_set_state(p, USB_PACKET_ASYNC);
363
            break;
364
        }
365
        p->result = ret;
366
        usb_packet_set_state(p, USB_PACKET_COMPLETE);
367
        QTAILQ_REMOVE(&ep->queue, p, queue);
368
        dev->port->ops->complete(dev->port, p);
369
    }
370
}
371

    
372
/* Cancel an active packet.  The packed must have been deferred by
373
   returning USB_RET_ASYNC from handle_packet, and not yet
374
   completed.  */
375
void usb_cancel_packet(USBPacket * p)
376
{
377
    bool callback = (p->state == USB_PACKET_ASYNC);
378
    assert(usb_packet_is_inflight(p));
379
    usb_packet_set_state(p, USB_PACKET_CANCELED);
380
    QTAILQ_REMOVE(&p->ep->queue, p, queue);
381
    if (callback) {
382
        usb_device_cancel_packet(p->ep->dev, p);
383
    }
384
}
385

    
386

    
387
void usb_packet_init(USBPacket *p)
388
{
389
    qemu_iovec_init(&p->iov, 1);
390
}
391

    
392
void usb_packet_set_state(USBPacket *p, USBPacketState state)
393
{
394
    static const char *name[] = {
395
        [USB_PACKET_UNDEFINED] = "undef",
396
        [USB_PACKET_SETUP]     = "setup",
397
        [USB_PACKET_QUEUED]    = "queued",
398
        [USB_PACKET_ASYNC]     = "async",
399
        [USB_PACKET_COMPLETE]  = "complete",
400
        [USB_PACKET_CANCELED]  = "canceled",
401
    };
402
    USBDevice *dev = p->ep->dev;
403
    USBBus *bus = usb_bus_from_device(dev);
404

    
405
    trace_usb_packet_state_change(bus->busnr, dev->port->path, p->ep->nr,
406
                                  p, name[p->state], name[state]);
407
    p->state = state;
408
}
409

    
410
void usb_packet_setup(USBPacket *p, int pid, USBEndpoint *ep)
411
{
412
    assert(!usb_packet_is_inflight(p));
413
    p->pid = pid;
414
    p->ep = ep;
415
    p->result = 0;
416
    qemu_iovec_reset(&p->iov);
417
    usb_packet_set_state(p, USB_PACKET_SETUP);
418
}
419

    
420
void usb_packet_addbuf(USBPacket *p, void *ptr, size_t len)
421
{
422
    qemu_iovec_add(&p->iov, ptr, len);
423
}
424

    
425
void usb_packet_copy(USBPacket *p, void *ptr, size_t bytes)
426
{
427
    assert(p->result >= 0);
428
    assert(p->result + bytes <= p->iov.size);
429
    switch (p->pid) {
430
    case USB_TOKEN_SETUP:
431
    case USB_TOKEN_OUT:
432
        iov_to_buf(p->iov.iov, p->iov.niov, ptr, p->result, bytes);
433
        break;
434
    case USB_TOKEN_IN:
435
        iov_from_buf(p->iov.iov, p->iov.niov, ptr, p->result, bytes);
436
        break;
437
    default:
438
        fprintf(stderr, "%s: invalid pid: %x\n", __func__, p->pid);
439
        abort();
440
    }
441
    p->result += bytes;
442
}
443

    
444
void usb_packet_skip(USBPacket *p, size_t bytes)
445
{
446
    assert(p->result >= 0);
447
    assert(p->result + bytes <= p->iov.size);
448
    if (p->pid == USB_TOKEN_IN) {
449
        iov_clear(p->iov.iov, p->iov.niov, p->result, bytes);
450
    }
451
    p->result += bytes;
452
}
453

    
454
void usb_packet_cleanup(USBPacket *p)
455
{
456
    assert(!usb_packet_is_inflight(p));
457
    qemu_iovec_destroy(&p->iov);
458
}
459

    
460
void usb_ep_init(USBDevice *dev)
461
{
462
    int ep;
463

    
464
    dev->ep_ctl.nr = 0;
465
    dev->ep_ctl.type = USB_ENDPOINT_XFER_CONTROL;
466
    dev->ep_ctl.ifnum = 0;
467
    dev->ep_ctl.dev = dev;
468
    QTAILQ_INIT(&dev->ep_ctl.queue);
469
    for (ep = 0; ep < USB_MAX_ENDPOINTS; ep++) {
470
        dev->ep_in[ep].nr = ep + 1;
471
        dev->ep_out[ep].nr = ep + 1;
472
        dev->ep_in[ep].pid = USB_TOKEN_IN;
473
        dev->ep_out[ep].pid = USB_TOKEN_OUT;
474
        dev->ep_in[ep].type = USB_ENDPOINT_XFER_INVALID;
475
        dev->ep_out[ep].type = USB_ENDPOINT_XFER_INVALID;
476
        dev->ep_in[ep].ifnum = 0;
477
        dev->ep_out[ep].ifnum = 0;
478
        dev->ep_in[ep].dev = dev;
479
        dev->ep_out[ep].dev = dev;
480
        QTAILQ_INIT(&dev->ep_in[ep].queue);
481
        QTAILQ_INIT(&dev->ep_out[ep].queue);
482
    }
483
}
484

    
485
void usb_ep_dump(USBDevice *dev)
486
{
487
    static const char *tname[] = {
488
        [USB_ENDPOINT_XFER_CONTROL] = "control",
489
        [USB_ENDPOINT_XFER_ISOC]    = "isoc",
490
        [USB_ENDPOINT_XFER_BULK]    = "bulk",
491
        [USB_ENDPOINT_XFER_INT]     = "int",
492
    };
493
    int ifnum, ep, first;
494

    
495
    fprintf(stderr, "Device \"%s\", config %d\n",
496
            dev->product_desc, dev->configuration);
497
    for (ifnum = 0; ifnum < 16; ifnum++) {
498
        first = 1;
499
        for (ep = 0; ep < USB_MAX_ENDPOINTS; ep++) {
500
            if (dev->ep_in[ep].type != USB_ENDPOINT_XFER_INVALID &&
501
                dev->ep_in[ep].ifnum == ifnum) {
502
                if (first) {
503
                    first = 0;
504
                    fprintf(stderr, "  Interface %d, alternative %d\n",
505
                            ifnum, dev->altsetting[ifnum]);
506
                }
507
                fprintf(stderr, "    Endpoint %d, IN, %s, %d max\n", ep,
508
                        tname[dev->ep_in[ep].type],
509
                        dev->ep_in[ep].max_packet_size);
510
            }
511
            if (dev->ep_out[ep].type != USB_ENDPOINT_XFER_INVALID &&
512
                dev->ep_out[ep].ifnum == ifnum) {
513
                if (first) {
514
                    first = 0;
515
                    fprintf(stderr, "  Interface %d, alternative %d\n",
516
                            ifnum, dev->altsetting[ifnum]);
517
                }
518
                fprintf(stderr, "    Endpoint %d, OUT, %s, %d max\n", ep,
519
                        tname[dev->ep_out[ep].type],
520
                        dev->ep_out[ep].max_packet_size);
521
            }
522
        }
523
    }
524
    fprintf(stderr, "--\n");
525
}
526

    
527
struct USBEndpoint *usb_ep_get(USBDevice *dev, int pid, int ep)
528
{
529
    struct USBEndpoint *eps;
530

    
531
    if (dev == NULL) {
532
        return NULL;
533
    }
534
    eps = (pid == USB_TOKEN_IN) ? dev->ep_in : dev->ep_out;
535
    if (ep == 0) {
536
        return &dev->ep_ctl;
537
    }
538
    assert(pid == USB_TOKEN_IN || pid == USB_TOKEN_OUT);
539
    assert(ep > 0 && ep <= USB_MAX_ENDPOINTS);
540
    return eps + ep - 1;
541
}
542

    
543
uint8_t usb_ep_get_type(USBDevice *dev, int pid, int ep)
544
{
545
    struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
546
    return uep->type;
547
}
548

    
549
void usb_ep_set_type(USBDevice *dev, int pid, int ep, uint8_t type)
550
{
551
    struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
552
    uep->type = type;
553
}
554

    
555
uint8_t usb_ep_get_ifnum(USBDevice *dev, int pid, int ep)
556
{
557
    struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
558
    return uep->ifnum;
559
}
560

    
561
void usb_ep_set_ifnum(USBDevice *dev, int pid, int ep, uint8_t ifnum)
562
{
563
    struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
564
    uep->ifnum = ifnum;
565
}
566

    
567
void usb_ep_set_max_packet_size(USBDevice *dev, int pid, int ep,
568
                                uint16_t raw)
569
{
570
    struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
571
    int size, microframes;
572

    
573
    size = raw & 0x7ff;
574
    switch ((raw >> 11) & 3) {
575
    case 1:
576
        microframes = 2;
577
        break;
578
    case 2:
579
        microframes = 3;
580
        break;
581
    default:
582
        microframes = 1;
583
        break;
584
    }
585
    uep->max_packet_size = size * microframes;
586
}
587

    
588
int usb_ep_get_max_packet_size(USBDevice *dev, int pid, int ep)
589
{
590
    struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
591
    return uep->max_packet_size;
592
}