2005-11-05 17:22:28 +03:00
|
|
|
/*
|
|
|
|
* QEMU USB emulation
|
|
|
|
*
|
|
|
|
* Copyright (c) 2005 Fabrice Bellard
|
2007-09-17 01:08:06 +04:00
|
|
|
*
|
2008-08-21 23:29:38 +04:00
|
|
|
* 2008 Generic packet handler rewrite by Max Krasnyansky
|
|
|
|
*
|
2005-11-05 17:22:28 +03: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 20:14:51 +03:00
|
|
|
#include "qemu-common.h"
|
|
|
|
#include "usb.h"
|
2011-07-12 17:22:25 +04:00
|
|
|
#include "iov.h"
|
2005-11-05 17:22:28 +03:00
|
|
|
|
2011-09-01 15:56:37 +04:00
|
|
|
void usb_attach(USBPort *port)
|
2005-11-05 17:22:28 +03:00
|
|
|
{
|
2011-09-01 15:56:37 +04:00
|
|
|
USBDevice *dev = port->dev;
|
|
|
|
|
|
|
|
assert(dev != NULL);
|
|
|
|
assert(dev->attached);
|
|
|
|
port->ops->attach(port);
|
|
|
|
usb_send_msg(dev, USB_MSG_ATTACH);
|
|
|
|
}
|
|
|
|
|
|
|
|
void usb_detach(USBPort *port)
|
|
|
|
{
|
|
|
|
USBDevice *dev = port->dev;
|
|
|
|
|
|
|
|
assert(dev != NULL);
|
|
|
|
port->ops->detach(port);
|
|
|
|
usb_send_msg(dev, USB_MSG_DETACH);
|
2005-11-05 17:22:28 +03:00
|
|
|
}
|
|
|
|
|
2010-12-01 13:32:45 +03:00
|
|
|
void usb_wakeup(USBDevice *dev)
|
|
|
|
{
|
|
|
|
if (dev->remote_wakeup && dev->port && dev->port->ops->wakeup) {
|
2011-06-21 13:52:28 +04:00
|
|
|
dev->port->ops->wakeup(dev->port);
|
2010-12-01 13:32:45 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-11-05 17:22:28 +03:00
|
|
|
/**********************/
|
2008-08-21 23:29:38 +04:00
|
|
|
|
2005-11-05 17:22:28 +03: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-17 01:08:06 +04:00
|
|
|
protocol)
|
2005-11-05 17:22:28 +03:00
|
|
|
*/
|
|
|
|
|
2011-02-02 19:36:29 +03:00
|
|
|
#define SETUP_STATE_IDLE 0
|
|
|
|
#define SETUP_STATE_SETUP 1
|
|
|
|
#define SETUP_STATE_DATA 2
|
|
|
|
#define SETUP_STATE_ACK 3
|
2005-11-05 17:22:28 +03:00
|
|
|
|
2008-08-21 23:29:38 +04:00
|
|
|
static int do_token_setup(USBDevice *s, USBPacket *p)
|
|
|
|
{
|
|
|
|
int request, value, index;
|
|
|
|
int ret = 0;
|
|
|
|
|
2011-07-12 17:22:25 +04:00
|
|
|
if (p->iov.size != 8) {
|
2008-08-21 23:29:38 +04:00
|
|
|
return USB_RET_STALL;
|
2011-07-12 17:22:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
usb_packet_copy(p, s->setup_buf, p->iov.size);
|
2008-08-21 23:29:38 +04: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 18:33:13 +03:00
|
|
|
|
2008-08-21 23:29:38 +04:00
|
|
|
if (s->setup_buf[0] & USB_DIR_IN) {
|
2011-02-02 18:33:13 +03:00
|
|
|
ret = s->info->handle_control(s, p, request, value, index,
|
2009-08-31 16:23:59 +04:00
|
|
|
s->setup_len, s->data_buf);
|
2011-02-02 19:36:29 +03:00
|
|
|
if (ret == USB_RET_ASYNC) {
|
|
|
|
s->setup_state = SETUP_STATE_SETUP;
|
|
|
|
return USB_RET_ASYNC;
|
|
|
|
}
|
2008-08-21 23:29:38 +04: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 19:46:00 +03: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 23:29:38 +04: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 17:22:28 +03:00
|
|
|
{
|
2008-08-21 23:29:38 +04:00
|
|
|
int request, value, index;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
if (p->devep != 0)
|
2009-08-31 16:23:59 +04:00
|
|
|
return s->info->handle_data(s, p);
|
2008-08-21 23:29:38 +04: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-02-02 18:33:13 +03:00
|
|
|
ret = s->info->handle_control(s, p, request, value, index,
|
2009-08-31 16:23:59 +04:00
|
|
|
s->setup_len, s->data_buf);
|
2011-02-02 18:33:13 +03:00
|
|
|
if (ret == USB_RET_ASYNC) {
|
|
|
|
return USB_RET_ASYNC;
|
|
|
|
}
|
|
|
|
s->setup_state = SETUP_STATE_IDLE;
|
2008-08-21 23:29:38 +04: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 17:22:25 +04:00
|
|
|
if (len > p->iov.size) {
|
|
|
|
len = p->iov.size;
|
|
|
|
}
|
|
|
|
usb_packet_copy(p, s->data_buf + s->setup_index, len);
|
2008-08-21 23:29:38 +04: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)
|
|
|
|
{
|
|
|
|
if (p->devep != 0)
|
2009-08-31 16:23:59 +04:00
|
|
|
return s->info->handle_data(s, p);
|
2008-08-21 23:29:38 +04: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 17:22:25 +04:00
|
|
|
if (len > p->iov.size) {
|
|
|
|
len = p->iov.size;
|
|
|
|
}
|
|
|
|
usb_packet_copy(p, s->data_buf + s->setup_index, len);
|
2008-08-21 23:29:38 +04: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 17:22:28 +03:00
|
|
|
|
2008-08-21 23:29:38 +04:00
|
|
|
/*
|
|
|
|
* 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)
|
|
|
|
{
|
2006-08-12 05:04:27 +04:00
|
|
|
switch(p->pid) {
|
2005-11-05 17:22:28 +03:00
|
|
|
case USB_MSG_ATTACH:
|
|
|
|
s->state = USB_STATE_ATTACHED;
|
2010-12-03 19:59:36 +03:00
|
|
|
if (s->info->handle_attach) {
|
|
|
|
s->info->handle_attach(s);
|
|
|
|
}
|
2008-08-21 23:29:38 +04:00
|
|
|
return 0;
|
|
|
|
|
2005-11-05 17:22:28 +03:00
|
|
|
case USB_MSG_DETACH:
|
|
|
|
s->state = USB_STATE_NOTATTACHED;
|
2008-08-21 23:29:38 +04:00
|
|
|
return 0;
|
|
|
|
|
2005-11-05 17:22:28 +03:00
|
|
|
case USB_MSG_RESET:
|
|
|
|
s->remote_wakeup = 0;
|
|
|
|
s->addr = 0;
|
|
|
|
s->state = USB_STATE_DEFAULT;
|
2010-12-03 19:59:36 +03:00
|
|
|
if (s->info->handle_reset) {
|
|
|
|
s->info->handle_reset(s);
|
|
|
|
}
|
2008-08-21 23:29:38 +04:00
|
|
|
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) {
|
2005-11-05 17:22:28 +03:00
|
|
|
case USB_TOKEN_SETUP:
|
2008-08-21 23:29:38 +04:00
|
|
|
return do_token_setup(s, p);
|
|
|
|
|
2005-11-05 17:22:28 +03:00
|
|
|
case USB_TOKEN_IN:
|
2008-08-21 23:29:38 +04:00
|
|
|
return do_token_in(s, p);
|
|
|
|
|
2005-11-05 17:22:28 +03:00
|
|
|
case USB_TOKEN_OUT:
|
2008-08-21 23:29:38 +04:00
|
|
|
return do_token_out(s, p);
|
|
|
|
|
2005-11-05 17:22:28 +03:00
|
|
|
default:
|
2008-08-21 23:29:38 +04:00
|
|
|
return USB_RET_STALL;
|
2005-11-05 17:22:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-02 19:36:29 +03: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 17:22:25 +04:00
|
|
|
if (p->result < 0) {
|
2011-02-02 19:36:29 +03:00
|
|
|
s->setup_state = SETUP_STATE_IDLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (s->setup_state) {
|
|
|
|
case SETUP_STATE_SETUP:
|
2011-07-12 17:22:25 +04:00
|
|
|
if (p->result < s->setup_len) {
|
|
|
|
s->setup_len = p->result;
|
2011-02-02 19:36:29 +03:00
|
|
|
}
|
|
|
|
s->setup_state = SETUP_STATE_DATA;
|
2011-07-12 17:22:25 +04:00
|
|
|
p->result = 8;
|
2011-02-02 19:36:29 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SETUP_STATE_ACK:
|
|
|
|
s->setup_state = SETUP_STATE_IDLE;
|
2011-07-12 17:22:25 +04:00
|
|
|
p->result = 0;
|
2011-02-02 19:36:29 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
usb_packet_complete(s, p);
|
|
|
|
}
|
|
|
|
|
2005-11-05 17:22:28 +03: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 23:37:58 +03:00
|
|
|
*q++ = 2 * len + 2;
|
2005-11-05 17:22:28 +03:00
|
|
|
*q++ = 3;
|
|
|
|
for(i = 0; i < len; i++) {
|
|
|
|
*q++ = str[i];
|
|
|
|
*q++ = 0;
|
|
|
|
}
|
|
|
|
return q - buf;
|
|
|
|
}
|
2006-08-12 05:04:27 +04:00
|
|
|
|
|
|
|
/* Send an internal message to a USB device. */
|
|
|
|
void usb_send_msg(USBDevice *dev, int msg)
|
|
|
|
{
|
|
|
|
USBPacket p;
|
2011-05-12 15:20:39 +04:00
|
|
|
int ret;
|
|
|
|
|
2006-08-12 05:04:27 +04:00
|
|
|
memset(&p, 0, sizeof(p));
|
|
|
|
p.pid = msg;
|
2011-05-12 15:20:39 +04:00
|
|
|
ret = usb_handle_packet(dev, &p);
|
2008-08-21 23:29:38 +04:00
|
|
|
/* This _must_ be synchronous */
|
2011-05-12 15:20:39 +04:00
|
|
|
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;
|
|
|
|
|
2011-05-12 15:48:13 +04:00
|
|
|
assert(p->owner == NULL);
|
2011-05-12 15:20:39 +04:00
|
|
|
ret = dev->info->handle_packet(dev, p);
|
2011-05-12 15:48:13 +04:00
|
|
|
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. */;
|
|
|
|
}
|
|
|
|
}
|
2011-05-12 15:20:39 +04:00
|
|
|
return ret;
|
2008-08-21 23:29:38 +04:00
|
|
|
}
|
2011-05-12 15:48:13 +04: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)
|
|
|
|
{
|
|
|
|
/* Note: p->owner != dev is possible in case dev is a hub */
|
|
|
|
assert(p->owner != NULL);
|
|
|
|
p->owner = NULL;
|
2011-08-25 18:43:15 +04:00
|
|
|
dev->port->ops->complete(dev->port, p);
|
2011-05-12 15:48:13 +04: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)
|
|
|
|
{
|
|
|
|
assert(p->owner != NULL);
|
2011-05-16 12:34:53 +04:00
|
|
|
p->owner->info->cancel_packet(p->owner, p);
|
2011-05-12 15:48:13 +04:00
|
|
|
p->owner = NULL;
|
|
|
|
}
|
2011-07-12 17:22:25 +04:00
|
|
|
|
|
|
|
|
|
|
|
void usb_packet_init(USBPacket *p)
|
|
|
|
{
|
|
|
|
qemu_iovec_init(&p->iov, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void usb_packet_setup(USBPacket *p, int pid, uint8_t addr, uint8_t ep)
|
|
|
|
{
|
|
|
|
p->pid = pid;
|
|
|
|
p->devaddr = addr;
|
|
|
|
p->devep = ep;
|
|
|
|
p->result = 0;
|
|
|
|
qemu_iovec_reset(&p->iov);
|
|
|
|
}
|
|
|
|
|
|
|
|
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:
|
|
|
|
iov_to_buf(p->iov.iov, p->iov.niov, ptr, p->result, bytes);
|
|
|
|
break;
|
|
|
|
case USB_TOKEN_IN:
|
|
|
|
iov_from_buf(p->iov.iov, p->iov.niov, ptr, p->result, bytes);
|
|
|
|
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) {
|
|
|
|
iov_clear(p->iov.iov, p->iov.niov, p->result, bytes);
|
|
|
|
}
|
|
|
|
p->result += bytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
void usb_packet_cleanup(USBPacket *p)
|
|
|
|
{
|
|
|
|
qemu_iovec_destroy(&p->iov);
|
|
|
|
}
|