2005-11-05 15:22:28 +01:00
|
|
|
/*
|
|
|
|
* QEMU USB emulation
|
|
|
|
*
|
|
|
|
* Copyright (c) 2005 Fabrice Bellard
|
2007-09-16 23:08:06 +02:00
|
|
|
*
|
2008-08-21 21:29:38 +02:00
|
|
|
* 2008 Generic packet handler rewrite by Max Krasnyansky
|
|
|
|
*
|
2005-11-05 15:22:28 +01:00
|
|
|
* 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.
|
|
|
|
*/
|
2007-11-17 18:14:51 +01:00
|
|
|
#include "qemu-common.h"
|
2012-03-07 14:55:18 +01:00
|
|
|
#include "hw/usb.h"
|
2011-07-12 15:22:25 +02:00
|
|
|
#include "iov.h"
|
2012-02-24 11:03:27 +01:00
|
|
|
#include "trace.h"
|
2005-11-05 15:22:28 +01:00
|
|
|
|
2011-09-01 13:56:37 +02:00
|
|
|
void usb_attach(USBPort *port)
|
2005-11-05 15:22:28 +01:00
|
|
|
{
|
2011-09-01 13:56:37 +02:00
|
|
|
USBDevice *dev = port->dev;
|
|
|
|
|
|
|
|
assert(dev != NULL);
|
|
|
|
assert(dev->attached);
|
2011-09-15 12:10:21 +02:00
|
|
|
assert(dev->state == USB_STATE_NOTATTACHED);
|
2011-09-01 13:56:37 +02:00
|
|
|
port->ops->attach(port);
|
2012-01-06 15:13:34 +01:00
|
|
|
dev->state = USB_STATE_ATTACHED;
|
|
|
|
usb_device_handle_attach(dev);
|
2011-09-01 13:56:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void usb_detach(USBPort *port)
|
|
|
|
{
|
|
|
|
USBDevice *dev = port->dev;
|
|
|
|
|
|
|
|
assert(dev != NULL);
|
2011-09-15 12:10:21 +02:00
|
|
|
assert(dev->state != USB_STATE_NOTATTACHED);
|
2011-09-01 13:56:37 +02:00
|
|
|
port->ops->detach(port);
|
2012-01-06 15:13:34 +01:00
|
|
|
dev->state = USB_STATE_NOTATTACHED;
|
2005-11-05 15:22:28 +01:00
|
|
|
}
|
|
|
|
|
2012-01-06 15:23:10 +01:00
|
|
|
void usb_port_reset(USBPort *port)
|
2011-09-15 12:10:21 +02:00
|
|
|
{
|
|
|
|
USBDevice *dev = port->dev;
|
|
|
|
|
|
|
|
assert(dev != NULL);
|
|
|
|
usb_detach(port);
|
|
|
|
usb_attach(port);
|
2012-01-06 15:23:10 +01:00
|
|
|
usb_device_reset(dev);
|
|
|
|
}
|
|
|
|
|
|
|
|
void usb_device_reset(USBDevice *dev)
|
|
|
|
{
|
|
|
|
if (dev == NULL || !dev->attached) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
dev->remote_wakeup = 0;
|
|
|
|
dev->addr = 0;
|
|
|
|
dev->state = USB_STATE_DEFAULT;
|
|
|
|
usb_device_handle_reset(dev);
|
2011-09-15 12:10:21 +02:00
|
|
|
}
|
|
|
|
|
2012-01-17 13:25:46 +01:00
|
|
|
void usb_wakeup(USBEndpoint *ep)
|
2010-12-01 11:32:45 +01:00
|
|
|
{
|
2012-01-17 13:25:46 +01:00
|
|
|
USBDevice *dev = ep->dev;
|
2012-01-20 13:29:53 +01:00
|
|
|
USBBus *bus = usb_bus_from_device(dev);
|
2012-01-17 13:25:46 +01:00
|
|
|
|
2010-12-01 11:32:45 +01:00
|
|
|
if (dev->remote_wakeup && dev->port && dev->port->ops->wakeup) {
|
2011-06-21 11:52:28 +02:00
|
|
|
dev->port->ops->wakeup(dev->port);
|
2010-12-01 11:32:45 +01:00
|
|
|
}
|
2012-01-20 13:29:53 +01:00
|
|
|
if (bus->ops->wakeup_endpoint) {
|
|
|
|
bus->ops->wakeup_endpoint(bus, ep);
|
|
|
|
}
|
2010-12-01 11:32:45 +01:00
|
|
|
}
|
|
|
|
|
2005-11-05 15:22:28 +01:00
|
|
|
/**********************/
|
2008-08-21 21:29:38 +02:00
|
|
|
|
2005-11-05 15:22:28 +01:00
|
|
|
/* generic USB device helpers (you are not forced to use them when
|
|
|
|
writing your USB device driver, but they help handling the
|
2007-09-16 23:08:06 +02:00
|
|
|
protocol)
|
2005-11-05 15:22:28 +01:00
|
|
|
*/
|
|
|
|
|
2011-02-02 17:36:29 +01:00
|
|
|
#define SETUP_STATE_IDLE 0
|
|
|
|
#define SETUP_STATE_SETUP 1
|
|
|
|
#define SETUP_STATE_DATA 2
|
|
|
|
#define SETUP_STATE_ACK 3
|
2012-03-02 13:22:29 +01:00
|
|
|
#define SETUP_STATE_PARAM 4
|
2005-11-05 15:22:28 +01:00
|
|
|
|
2008-08-21 21:29:38 +02:00
|
|
|
static int do_token_setup(USBDevice *s, USBPacket *p)
|
|
|
|
{
|
|
|
|
int request, value, index;
|
|
|
|
int ret = 0;
|
|
|
|
|
2011-07-12 15:22:25 +02:00
|
|
|
if (p->iov.size != 8) {
|
2008-08-21 21:29:38 +02:00
|
|
|
return USB_RET_STALL;
|
2011-07-12 15:22:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
usb_packet_copy(p, s->setup_buf, p->iov.size);
|
2012-08-09 10:57:32 +02:00
|
|
|
p->result = 0;
|
2008-08-21 21:29:38 +02:00
|
|
|
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];
|
2011-02-02 16:33:13 +01:00
|
|
|
|
2008-08-21 21:29:38 +02:00
|
|
|
if (s->setup_buf[0] & USB_DIR_IN) {
|
2011-12-15 21:53:10 +01:00
|
|
|
ret = usb_device_handle_control(s, p, request, value, index,
|
|
|
|
s->setup_len, s->data_buf);
|
2011-02-02 17:36:29 +01:00
|
|
|
if (ret == USB_RET_ASYNC) {
|
|
|
|
s->setup_state = SETUP_STATE_SETUP;
|
|
|
|
return USB_RET_ASYNC;
|
|
|
|
}
|
2008-08-21 21:29:38 +02:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (ret < s->setup_len)
|
|
|
|
s->setup_len = ret;
|
|
|
|
s->setup_state = SETUP_STATE_DATA;
|
|
|
|
} else {
|
2011-02-02 17:46:00 +01:00
|
|
|
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;
|
|
|
|
}
|
2008-08-21 21:29:38 +02:00
|
|
|
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)
|
2005-11-05 15:22:28 +01:00
|
|
|
{
|
2008-08-21 21:29:38 +02:00
|
|
|
int request, value, index;
|
|
|
|
int ret = 0;
|
|
|
|
|
2012-01-12 13:23:01 +01:00
|
|
|
assert(p->ep->nr == 0);
|
2008-08-21 21:29:38 +02:00
|
|
|
|
|
|
|
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)) {
|
2011-12-15 21:53:10 +01:00
|
|
|
ret = usb_device_handle_control(s, p, request, value, index,
|
|
|
|
s->setup_len, s->data_buf);
|
2011-02-02 16:33:13 +01:00
|
|
|
if (ret == USB_RET_ASYNC) {
|
|
|
|
return USB_RET_ASYNC;
|
|
|
|
}
|
|
|
|
s->setup_state = SETUP_STATE_IDLE;
|
2008-08-21 21:29:38 +02:00
|
|
|
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;
|
2011-07-12 15:22:25 +02:00
|
|
|
if (len > p->iov.size) {
|
|
|
|
len = p->iov.size;
|
|
|
|
}
|
|
|
|
usb_packet_copy(p, s->data_buf + s->setup_index, len);
|
2008-08-21 21:29:38 +02:00
|
|
|
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)
|
|
|
|
{
|
2012-01-12 13:23:01 +01:00
|
|
|
assert(p->ep->nr == 0);
|
2008-08-21 21:29:38 +02:00
|
|
|
|
|
|
|
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;
|
2011-07-12 15:22:25 +02:00
|
|
|
if (len > p->iov.size) {
|
|
|
|
len = p->iov.size;
|
|
|
|
}
|
|
|
|
usb_packet_copy(p, s->data_buf + s->setup_index, len);
|
2008-08-21 21:29:38 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2005-11-05 15:22:28 +01:00
|
|
|
|
2012-03-02 13:22:29 +01:00
|
|
|
static int do_parameter(USBDevice *s, USBPacket *p)
|
|
|
|
{
|
|
|
|
int request, value, index;
|
|
|
|
int i, ret = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < 8; i++) {
|
|
|
|
s->setup_buf[i] = p->parameter >> (i*8);
|
|
|
|
}
|
|
|
|
|
|
|
|
s->setup_state = SETUP_STATE_PARAM;
|
|
|
|
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_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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p->pid == USB_TOKEN_OUT) {
|
|
|
|
usb_packet_copy(p, s->data_buf, s->setup_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = usb_device_handle_control(s, p, request, value, index,
|
|
|
|
s->setup_len, s->data_buf);
|
|
|
|
if (ret < 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret < s->setup_len) {
|
|
|
|
s->setup_len = ret;
|
|
|
|
}
|
|
|
|
if (p->pid == USB_TOKEN_IN) {
|
|
|
|
usb_packet_copy(p, s->data_buf, s->setup_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-02-02 17:36:29 +01:00
|
|
|
/* 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)
|
|
|
|
{
|
2011-07-12 15:22:25 +02:00
|
|
|
if (p->result < 0) {
|
2011-02-02 17:36:29 +01:00
|
|
|
s->setup_state = SETUP_STATE_IDLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (s->setup_state) {
|
|
|
|
case SETUP_STATE_SETUP:
|
2011-07-12 15:22:25 +02:00
|
|
|
if (p->result < s->setup_len) {
|
|
|
|
s->setup_len = p->result;
|
2011-02-02 17:36:29 +01:00
|
|
|
}
|
|
|
|
s->setup_state = SETUP_STATE_DATA;
|
2011-07-12 15:22:25 +02:00
|
|
|
p->result = 8;
|
2011-02-02 17:36:29 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SETUP_STATE_ACK:
|
|
|
|
s->setup_state = SETUP_STATE_IDLE;
|
2011-07-12 15:22:25 +02:00
|
|
|
p->result = 0;
|
2011-02-02 17:36:29 +01:00
|
|
|
break;
|
|
|
|
|
2012-03-02 13:22:29 +01:00
|
|
|
case SETUP_STATE_PARAM:
|
|
|
|
if (p->result < s->setup_len) {
|
|
|
|
s->setup_len = p->result;
|
|
|
|
}
|
|
|
|
if (p->pid == USB_TOKEN_IN) {
|
|
|
|
p->result = 0;
|
|
|
|
usb_packet_copy(p, s->data_buf, s->setup_len);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2011-02-02 17:36:29 +01:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
usb_packet_complete(s, p);
|
|
|
|
}
|
|
|
|
|
2005-11-05 15:22:28 +01:00
|
|
|
/* XXX: fix overflow */
|
|
|
|
int set_usb_string(uint8_t *buf, const char *str)
|
|
|
|
{
|
|
|
|
int len, i;
|
|
|
|
uint8_t *q;
|
|
|
|
|
|
|
|
q = buf;
|
|
|
|
len = strlen(str);
|
2006-03-11 21:37:58 +01:00
|
|
|
*q++ = 2 * len + 2;
|
2005-11-05 15:22:28 +01:00
|
|
|
*q++ = 3;
|
|
|
|
for(i = 0; i < len; i++) {
|
|
|
|
*q++ = str[i];
|
|
|
|
*q++ = 0;
|
|
|
|
}
|
|
|
|
return q - buf;
|
|
|
|
}
|
2006-08-12 03:04:27 +02:00
|
|
|
|
2012-01-10 16:59:28 +01:00
|
|
|
USBDevice *usb_find_device(USBPort *port, uint8_t addr)
|
|
|
|
{
|
|
|
|
USBDevice *dev = port->dev;
|
|
|
|
|
|
|
|
if (dev == NULL || !dev->attached || dev->state != USB_STATE_DEFAULT) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (dev->addr == addr) {
|
|
|
|
return dev;
|
|
|
|
}
|
|
|
|
return usb_device_find_device(dev, addr);
|
|
|
|
}
|
|
|
|
|
2012-01-12 14:26:13 +01:00
|
|
|
static int usb_process_one(USBPacket *p)
|
|
|
|
{
|
|
|
|
USBDevice *dev = p->ep->dev;
|
|
|
|
|
|
|
|
if (p->ep->nr == 0) {
|
|
|
|
/* control pipe */
|
2012-03-02 13:22:29 +01:00
|
|
|
if (p->parameter) {
|
|
|
|
return do_parameter(dev, p);
|
|
|
|
}
|
2012-01-12 14:26:13 +01:00
|
|
|
switch (p->pid) {
|
|
|
|
case USB_TOKEN_SETUP:
|
|
|
|
return do_token_setup(dev, p);
|
|
|
|
case USB_TOKEN_IN:
|
|
|
|
return do_token_in(dev, p);
|
|
|
|
case USB_TOKEN_OUT:
|
|
|
|
return do_token_out(dev, p);
|
|
|
|
default:
|
|
|
|
return USB_RET_STALL;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* data pipe */
|
|
|
|
return usb_device_handle_data(dev, p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-12 13:20:39 +02:00
|
|
|
/* 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;
|
|
|
|
|
2012-01-10 17:33:02 +01:00
|
|
|
if (dev == NULL) {
|
|
|
|
return USB_RET_NODEV;
|
|
|
|
}
|
2012-01-12 13:23:01 +01:00
|
|
|
assert(dev == p->ep->dev);
|
2012-01-11 12:14:02 +01:00
|
|
|
assert(dev->state == USB_STATE_DEFAULT);
|
2012-03-08 12:27:47 +01:00
|
|
|
usb_packet_check_state(p, USB_PACKET_SETUP);
|
2012-01-12 14:26:13 +01:00
|
|
|
assert(p->ep != NULL);
|
2012-01-11 12:14:02 +01:00
|
|
|
|
2012-03-01 14:39:28 +01:00
|
|
|
if (QTAILQ_EMPTY(&p->ep->queue) || p->ep->pipeline) {
|
2012-01-12 14:26:13 +01:00
|
|
|
ret = usb_process_one(p);
|
|
|
|
if (ret == USB_RET_ASYNC) {
|
|
|
|
usb_packet_set_state(p, USB_PACKET_ASYNC);
|
|
|
|
QTAILQ_INSERT_TAIL(&p->ep->queue, p, queue);
|
|
|
|
} else {
|
|
|
|
p->result = ret;
|
|
|
|
usb_packet_set_state(p, USB_PACKET_COMPLETE);
|
2012-01-11 12:14:02 +01:00
|
|
|
}
|
|
|
|
} else {
|
2012-01-12 14:26:13 +01:00
|
|
|
ret = USB_RET_ASYNC;
|
|
|
|
usb_packet_set_state(p, USB_PACKET_QUEUED);
|
|
|
|
QTAILQ_INSERT_TAIL(&p->ep->queue, p, queue);
|
2011-05-12 13:48:13 +02:00
|
|
|
}
|
2011-05-12 13:20:39 +02:00
|
|
|
return ret;
|
2008-08-21 21:29:38 +02:00
|
|
|
}
|
2011-05-12 13:48:13 +02:00
|
|
|
|
|
|
|
/* 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)
|
|
|
|
{
|
2012-01-12 14:26:13 +01:00
|
|
|
USBEndpoint *ep = p->ep;
|
|
|
|
int ret;
|
|
|
|
|
2012-03-08 12:27:47 +01:00
|
|
|
usb_packet_check_state(p, USB_PACKET_ASYNC);
|
2012-01-12 14:26:13 +01:00
|
|
|
assert(QTAILQ_FIRST(&ep->queue) == p);
|
|
|
|
usb_packet_set_state(p, USB_PACKET_COMPLETE);
|
|
|
|
QTAILQ_REMOVE(&ep->queue, p, queue);
|
2011-08-25 16:43:15 +02:00
|
|
|
dev->port->ops->complete(dev->port, p);
|
2012-01-12 14:26:13 +01:00
|
|
|
|
|
|
|
while (!QTAILQ_EMPTY(&ep->queue)) {
|
|
|
|
p = QTAILQ_FIRST(&ep->queue);
|
2012-02-28 15:36:06 +01:00
|
|
|
if (p->state == USB_PACKET_ASYNC) {
|
|
|
|
break;
|
|
|
|
}
|
2012-03-08 12:27:47 +01:00
|
|
|
usb_packet_check_state(p, USB_PACKET_QUEUED);
|
2012-01-12 14:26:13 +01:00
|
|
|
ret = usb_process_one(p);
|
|
|
|
if (ret == USB_RET_ASYNC) {
|
|
|
|
usb_packet_set_state(p, USB_PACKET_ASYNC);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
p->result = ret;
|
|
|
|
usb_packet_set_state(p, USB_PACKET_COMPLETE);
|
|
|
|
QTAILQ_REMOVE(&ep->queue, p, queue);
|
|
|
|
dev->port->ops->complete(dev->port, p);
|
|
|
|
}
|
2011-05-12 13:48:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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)
|
|
|
|
{
|
2012-01-12 14:26:13 +01:00
|
|
|
bool callback = (p->state == USB_PACKET_ASYNC);
|
|
|
|
assert(usb_packet_is_inflight(p));
|
|
|
|
usb_packet_set_state(p, USB_PACKET_CANCELED);
|
|
|
|
QTAILQ_REMOVE(&p->ep->queue, p, queue);
|
|
|
|
if (callback) {
|
|
|
|
usb_device_cancel_packet(p->ep->dev, p);
|
|
|
|
}
|
2011-05-12 13:48:13 +02:00
|
|
|
}
|
2011-07-12 15:22:25 +02:00
|
|
|
|
|
|
|
|
|
|
|
void usb_packet_init(USBPacket *p)
|
|
|
|
{
|
|
|
|
qemu_iovec_init(&p->iov, 1);
|
|
|
|
}
|
|
|
|
|
2012-03-08 12:27:47 +01:00
|
|
|
static const char *usb_packet_state_name(USBPacketState state)
|
2012-01-12 14:26:13 +01:00
|
|
|
{
|
|
|
|
static const char *name[] = {
|
|
|
|
[USB_PACKET_UNDEFINED] = "undef",
|
|
|
|
[USB_PACKET_SETUP] = "setup",
|
|
|
|
[USB_PACKET_QUEUED] = "queued",
|
|
|
|
[USB_PACKET_ASYNC] = "async",
|
|
|
|
[USB_PACKET_COMPLETE] = "complete",
|
|
|
|
[USB_PACKET_CANCELED] = "canceled",
|
|
|
|
};
|
2012-03-08 12:27:47 +01:00
|
|
|
if (state < ARRAY_SIZE(name)) {
|
|
|
|
return name[state];
|
|
|
|
}
|
|
|
|
return "INVALID";
|
|
|
|
}
|
|
|
|
|
|
|
|
void usb_packet_check_state(USBPacket *p, USBPacketState expected)
|
|
|
|
{
|
|
|
|
USBDevice *dev;
|
|
|
|
USBBus *bus;
|
|
|
|
|
|
|
|
if (p->state == expected) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
dev = p->ep->dev;
|
|
|
|
bus = usb_bus_from_device(dev);
|
|
|
|
trace_usb_packet_state_fault(bus->busnr, dev->port->path, p->ep->nr, p,
|
|
|
|
usb_packet_state_name(p->state),
|
|
|
|
usb_packet_state_name(expected));
|
|
|
|
assert(!"usb packet state check failed");
|
|
|
|
}
|
|
|
|
|
|
|
|
void usb_packet_set_state(USBPacket *p, USBPacketState state)
|
|
|
|
{
|
2012-03-23 13:34:50 +01:00
|
|
|
if (p->ep) {
|
|
|
|
USBDevice *dev = p->ep->dev;
|
|
|
|
USBBus *bus = usb_bus_from_device(dev);
|
|
|
|
trace_usb_packet_state_change(bus->busnr, dev->port->path, p->ep->nr, p,
|
|
|
|
usb_packet_state_name(p->state),
|
|
|
|
usb_packet_state_name(state));
|
|
|
|
} else {
|
|
|
|
trace_usb_packet_state_change(-1, "", -1, p,
|
|
|
|
usb_packet_state_name(p->state),
|
|
|
|
usb_packet_state_name(state));
|
|
|
|
}
|
2012-01-12 14:26:13 +01:00
|
|
|
p->state = state;
|
|
|
|
}
|
|
|
|
|
2012-01-12 13:23:01 +01:00
|
|
|
void usb_packet_setup(USBPacket *p, int pid, USBEndpoint *ep)
|
2011-07-12 15:22:25 +02:00
|
|
|
{
|
2012-01-12 12:51:48 +01:00
|
|
|
assert(!usb_packet_is_inflight(p));
|
2012-04-19 13:07:54 +02:00
|
|
|
assert(p->iov.iov != NULL);
|
2011-07-12 15:22:25 +02:00
|
|
|
p->pid = pid;
|
2012-01-12 13:23:01 +01:00
|
|
|
p->ep = ep;
|
2011-07-12 15:22:25 +02:00
|
|
|
p->result = 0;
|
2012-03-02 13:22:29 +01:00
|
|
|
p->parameter = 0;
|
2011-07-12 15:22:25 +02:00
|
|
|
qemu_iovec_reset(&p->iov);
|
2012-01-12 14:26:13 +01:00
|
|
|
usb_packet_set_state(p, USB_PACKET_SETUP);
|
2011-07-12 15:22:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void usb_packet_addbuf(USBPacket *p, void *ptr, size_t len)
|
|
|
|
{
|
|
|
|
qemu_iovec_add(&p->iov, ptr, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
void usb_packet_copy(USBPacket *p, void *ptr, size_t bytes)
|
|
|
|
{
|
|
|
|
assert(p->result >= 0);
|
|
|
|
assert(p->result + bytes <= p->iov.size);
|
|
|
|
switch (p->pid) {
|
|
|
|
case USB_TOKEN_SETUP:
|
|
|
|
case USB_TOKEN_OUT:
|
change iov_* function prototypes to be more appropriate
Reorder arguments to be more natural, readable and
consistent with other iov_* functions, and change
argument names, from:
iov_from_buf(iov, iov_cnt, buf, iov_off, size)
to
iov_from_buf(iov, iov_cnt, offset, buf, bytes)
The result becomes natural English:
copy data to this `iov' vector with `iov_cnt'
elements starting at byte offset `offset'
from memory buffer `buf', processing `bytes'
bytes max.
(Try to read the original prototype this way).
Also change iov_clear() to more general iov_memset()
(it uses memset() internally anyway).
While at it, add comments to the header file
describing what the routines actually does.
The patch only renames argumens in the header, but
keeps old names in the implementation. The next
patch will touch actual code to match.
Now, it might look wrong to pay so much attention
to so small things. But we've so many badly designed
interfaces already so the whole thing becomes rather
confusing or error prone. One example of this is
previous commit and small discussion which emerged
from it, with an outcome that the utility functions
like these aren't well-understdandable, leading to
strange usage cases. That's why I paid quite some
attention to this set of functions and a few
others in subsequent patches.
Signed-off-by: Michael Tokarev <mjt@tls.msk.ru>
2012-03-11 15:05:12 +01:00
|
|
|
iov_to_buf(p->iov.iov, p->iov.niov, p->result, ptr, bytes);
|
2011-07-12 15:22:25 +02:00
|
|
|
break;
|
|
|
|
case USB_TOKEN_IN:
|
change iov_* function prototypes to be more appropriate
Reorder arguments to be more natural, readable and
consistent with other iov_* functions, and change
argument names, from:
iov_from_buf(iov, iov_cnt, buf, iov_off, size)
to
iov_from_buf(iov, iov_cnt, offset, buf, bytes)
The result becomes natural English:
copy data to this `iov' vector with `iov_cnt'
elements starting at byte offset `offset'
from memory buffer `buf', processing `bytes'
bytes max.
(Try to read the original prototype this way).
Also change iov_clear() to more general iov_memset()
(it uses memset() internally anyway).
While at it, add comments to the header file
describing what the routines actually does.
The patch only renames argumens in the header, but
keeps old names in the implementation. The next
patch will touch actual code to match.
Now, it might look wrong to pay so much attention
to so small things. But we've so many badly designed
interfaces already so the whole thing becomes rather
confusing or error prone. One example of this is
previous commit and small discussion which emerged
from it, with an outcome that the utility functions
like these aren't well-understdandable, leading to
strange usage cases. That's why I paid quite some
attention to this set of functions and a few
others in subsequent patches.
Signed-off-by: Michael Tokarev <mjt@tls.msk.ru>
2012-03-11 15:05:12 +01:00
|
|
|
iov_from_buf(p->iov.iov, p->iov.niov, p->result, ptr, bytes);
|
2011-07-12 15:22:25 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "%s: invalid pid: %x\n", __func__, p->pid);
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
p->result += bytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
void usb_packet_skip(USBPacket *p, size_t bytes)
|
|
|
|
{
|
|
|
|
assert(p->result >= 0);
|
|
|
|
assert(p->result + bytes <= p->iov.size);
|
|
|
|
if (p->pid == USB_TOKEN_IN) {
|
change iov_* function prototypes to be more appropriate
Reorder arguments to be more natural, readable and
consistent with other iov_* functions, and change
argument names, from:
iov_from_buf(iov, iov_cnt, buf, iov_off, size)
to
iov_from_buf(iov, iov_cnt, offset, buf, bytes)
The result becomes natural English:
copy data to this `iov' vector with `iov_cnt'
elements starting at byte offset `offset'
from memory buffer `buf', processing `bytes'
bytes max.
(Try to read the original prototype this way).
Also change iov_clear() to more general iov_memset()
(it uses memset() internally anyway).
While at it, add comments to the header file
describing what the routines actually does.
The patch only renames argumens in the header, but
keeps old names in the implementation. The next
patch will touch actual code to match.
Now, it might look wrong to pay so much attention
to so small things. But we've so many badly designed
interfaces already so the whole thing becomes rather
confusing or error prone. One example of this is
previous commit and small discussion which emerged
from it, with an outcome that the utility functions
like these aren't well-understdandable, leading to
strange usage cases. That's why I paid quite some
attention to this set of functions and a few
others in subsequent patches.
Signed-off-by: Michael Tokarev <mjt@tls.msk.ru>
2012-03-11 15:05:12 +01:00
|
|
|
iov_memset(p->iov.iov, p->iov.niov, p->result, 0, bytes);
|
2011-07-12 15:22:25 +02:00
|
|
|
}
|
|
|
|
p->result += bytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
void usb_packet_cleanup(USBPacket *p)
|
|
|
|
{
|
2012-01-12 12:51:48 +01:00
|
|
|
assert(!usb_packet_is_inflight(p));
|
2011-07-12 15:22:25 +02:00
|
|
|
qemu_iovec_destroy(&p->iov);
|
|
|
|
}
|
2011-08-29 12:49:46 +02:00
|
|
|
|
2012-07-03 10:11:21 +02:00
|
|
|
void usb_ep_reset(USBDevice *dev)
|
2011-08-29 12:49:46 +02:00
|
|
|
{
|
|
|
|
int ep;
|
|
|
|
|
2012-01-12 13:24:22 +01:00
|
|
|
dev->ep_ctl.nr = 0;
|
2011-12-13 15:58:19 +01:00
|
|
|
dev->ep_ctl.type = USB_ENDPOINT_XFER_CONTROL;
|
|
|
|
dev->ep_ctl.ifnum = 0;
|
|
|
|
dev->ep_ctl.dev = dev;
|
2012-03-01 14:39:28 +01:00
|
|
|
dev->ep_ctl.pipeline = false;
|
2011-08-29 12:49:46 +02:00
|
|
|
for (ep = 0; ep < USB_MAX_ENDPOINTS; ep++) {
|
2012-01-12 13:24:22 +01:00
|
|
|
dev->ep_in[ep].nr = ep + 1;
|
|
|
|
dev->ep_out[ep].nr = ep + 1;
|
|
|
|
dev->ep_in[ep].pid = USB_TOKEN_IN;
|
|
|
|
dev->ep_out[ep].pid = USB_TOKEN_OUT;
|
2011-08-29 12:49:46 +02:00
|
|
|
dev->ep_in[ep].type = USB_ENDPOINT_XFER_INVALID;
|
|
|
|
dev->ep_out[ep].type = USB_ENDPOINT_XFER_INVALID;
|
2012-07-03 10:15:08 +02:00
|
|
|
dev->ep_in[ep].ifnum = USB_INTERFACE_INVALID;
|
|
|
|
dev->ep_out[ep].ifnum = USB_INTERFACE_INVALID;
|
2011-12-13 15:58:19 +01:00
|
|
|
dev->ep_in[ep].dev = dev;
|
|
|
|
dev->ep_out[ep].dev = dev;
|
2012-03-01 14:39:28 +01:00
|
|
|
dev->ep_in[ep].pipeline = false;
|
|
|
|
dev->ep_out[ep].pipeline = false;
|
2012-07-03 10:11:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void usb_ep_init(USBDevice *dev)
|
|
|
|
{
|
|
|
|
int ep;
|
|
|
|
|
|
|
|
usb_ep_reset(dev);
|
|
|
|
QTAILQ_INIT(&dev->ep_ctl.queue);
|
|
|
|
for (ep = 0; ep < USB_MAX_ENDPOINTS; ep++) {
|
2012-01-12 14:26:13 +01:00
|
|
|
QTAILQ_INIT(&dev->ep_in[ep].queue);
|
|
|
|
QTAILQ_INIT(&dev->ep_out[ep].queue);
|
2011-08-29 12:49:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-29 13:45:25 +02:00
|
|
|
void usb_ep_dump(USBDevice *dev)
|
|
|
|
{
|
|
|
|
static const char *tname[] = {
|
|
|
|
[USB_ENDPOINT_XFER_CONTROL] = "control",
|
|
|
|
[USB_ENDPOINT_XFER_ISOC] = "isoc",
|
|
|
|
[USB_ENDPOINT_XFER_BULK] = "bulk",
|
|
|
|
[USB_ENDPOINT_XFER_INT] = "int",
|
|
|
|
};
|
|
|
|
int ifnum, ep, first;
|
|
|
|
|
|
|
|
fprintf(stderr, "Device \"%s\", config %d\n",
|
|
|
|
dev->product_desc, dev->configuration);
|
|
|
|
for (ifnum = 0; ifnum < 16; ifnum++) {
|
|
|
|
first = 1;
|
|
|
|
for (ep = 0; ep < USB_MAX_ENDPOINTS; ep++) {
|
|
|
|
if (dev->ep_in[ep].type != USB_ENDPOINT_XFER_INVALID &&
|
|
|
|
dev->ep_in[ep].ifnum == ifnum) {
|
|
|
|
if (first) {
|
|
|
|
first = 0;
|
|
|
|
fprintf(stderr, " Interface %d, alternative %d\n",
|
|
|
|
ifnum, dev->altsetting[ifnum]);
|
|
|
|
}
|
2011-08-31 16:09:27 +02:00
|
|
|
fprintf(stderr, " Endpoint %d, IN, %s, %d max\n", ep,
|
|
|
|
tname[dev->ep_in[ep].type],
|
|
|
|
dev->ep_in[ep].max_packet_size);
|
2011-08-29 13:45:25 +02:00
|
|
|
}
|
|
|
|
if (dev->ep_out[ep].type != USB_ENDPOINT_XFER_INVALID &&
|
|
|
|
dev->ep_out[ep].ifnum == ifnum) {
|
|
|
|
if (first) {
|
|
|
|
first = 0;
|
|
|
|
fprintf(stderr, " Interface %d, alternative %d\n",
|
|
|
|
ifnum, dev->altsetting[ifnum]);
|
|
|
|
}
|
2011-08-31 16:09:27 +02:00
|
|
|
fprintf(stderr, " Endpoint %d, OUT, %s, %d max\n", ep,
|
|
|
|
tname[dev->ep_out[ep].type],
|
|
|
|
dev->ep_out[ep].max_packet_size);
|
2011-08-29 13:45:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fprintf(stderr, "--\n");
|
|
|
|
}
|
|
|
|
|
2011-08-29 12:49:46 +02:00
|
|
|
struct USBEndpoint *usb_ep_get(USBDevice *dev, int pid, int ep)
|
|
|
|
{
|
2012-01-12 13:23:01 +01:00
|
|
|
struct USBEndpoint *eps;
|
|
|
|
|
|
|
|
if (dev == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
eps = (pid == USB_TOKEN_IN) ? dev->ep_in : dev->ep_out;
|
2011-12-13 15:58:19 +01:00
|
|
|
if (ep == 0) {
|
|
|
|
return &dev->ep_ctl;
|
|
|
|
}
|
2011-08-29 12:49:46 +02:00
|
|
|
assert(pid == USB_TOKEN_IN || pid == USB_TOKEN_OUT);
|
|
|
|
assert(ep > 0 && ep <= USB_MAX_ENDPOINTS);
|
|
|
|
return eps + ep - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t usb_ep_get_type(USBDevice *dev, int pid, int ep)
|
|
|
|
{
|
|
|
|
struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
|
|
|
|
return uep->type;
|
|
|
|
}
|
|
|
|
|
|
|
|
void usb_ep_set_type(USBDevice *dev, int pid, int ep, uint8_t type)
|
|
|
|
{
|
|
|
|
struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
|
|
|
|
uep->type = type;
|
|
|
|
}
|
2011-08-29 12:57:48 +02:00
|
|
|
|
|
|
|
uint8_t usb_ep_get_ifnum(USBDevice *dev, int pid, int ep)
|
|
|
|
{
|
|
|
|
struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
|
|
|
|
return uep->ifnum;
|
|
|
|
}
|
|
|
|
|
|
|
|
void usb_ep_set_ifnum(USBDevice *dev, int pid, int ep, uint8_t ifnum)
|
|
|
|
{
|
|
|
|
struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
|
|
|
|
uep->ifnum = ifnum;
|
|
|
|
}
|
2011-08-31 16:09:27 +02:00
|
|
|
|
|
|
|
void usb_ep_set_max_packet_size(USBDevice *dev, int pid, int ep,
|
|
|
|
uint16_t raw)
|
|
|
|
{
|
|
|
|
struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
|
|
|
|
int size, microframes;
|
|
|
|
|
|
|
|
size = raw & 0x7ff;
|
|
|
|
switch ((raw >> 11) & 3) {
|
|
|
|
case 1:
|
|
|
|
microframes = 2;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
microframes = 3;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
microframes = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
uep->max_packet_size = size * microframes;
|
|
|
|
}
|
|
|
|
|
|
|
|
int usb_ep_get_max_packet_size(USBDevice *dev, int pid, int ep)
|
|
|
|
{
|
|
|
|
struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
|
|
|
|
return uep->max_packet_size;
|
|
|
|
}
|
2012-03-01 14:39:28 +01:00
|
|
|
|
|
|
|
void usb_ep_set_pipeline(USBDevice *dev, int pid, int ep, bool enabled)
|
|
|
|
{
|
|
|
|
struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
|
|
|
|
uep->pipeline = enabled;
|
|
|
|
}
|