2008-01-14 06:41:02 +03:00
|
|
|
/*
|
|
|
|
* FTDI FT232BM Device emulation
|
|
|
|
*
|
|
|
|
* Copyright (c) 2006 CodeSourcery.
|
|
|
|
* Copyright (c) 2008 Samuel Thibault <samuel.thibault@ens-lyon.org>
|
|
|
|
* Written by Paul Brook, reused for FTDI by Samuel Thibault
|
|
|
|
*
|
2011-06-26 06:21:35 +04:00
|
|
|
* This code is licensed under the LGPL.
|
2008-01-14 06:41:02 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "qemu-common.h"
|
2010-02-18 18:24:31 +03:00
|
|
|
#include "qemu-error.h"
|
2008-01-14 06:41:02 +03:00
|
|
|
#include "usb.h"
|
2010-11-17 13:05:32 +03:00
|
|
|
#include "usb-desc.h"
|
2008-01-14 06:41:02 +03:00
|
|
|
#include "qemu-char.h"
|
|
|
|
|
|
|
|
//#define DEBUG_Serial
|
|
|
|
|
|
|
|
#ifdef DEBUG_Serial
|
2009-05-13 21:53:17 +04:00
|
|
|
#define DPRINTF(fmt, ...) \
|
|
|
|
do { printf("usb-serial: " fmt , ## __VA_ARGS__); } while (0)
|
2008-01-14 06:41:02 +03:00
|
|
|
#else
|
2009-05-13 21:53:17 +04:00
|
|
|
#define DPRINTF(fmt, ...) do {} while(0)
|
2008-01-14 06:41:02 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#define RECV_BUF 384
|
|
|
|
|
|
|
|
/* Commands */
|
|
|
|
#define FTDI_RESET 0
|
|
|
|
#define FTDI_SET_MDM_CTRL 1
|
|
|
|
#define FTDI_SET_FLOW_CTRL 2
|
|
|
|
#define FTDI_SET_BAUD 3
|
|
|
|
#define FTDI_SET_DATA 4
|
|
|
|
#define FTDI_GET_MDM_ST 5
|
|
|
|
#define FTDI_SET_EVENT_CHR 6
|
|
|
|
#define FTDI_SET_ERROR_CHR 7
|
|
|
|
#define FTDI_SET_LATENCY 9
|
|
|
|
#define FTDI_GET_LATENCY 10
|
|
|
|
|
|
|
|
#define DeviceOutVendor ((USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_DEVICE)<<8)
|
|
|
|
#define DeviceInVendor ((USB_DIR_IN |USB_TYPE_VENDOR|USB_RECIP_DEVICE)<<8)
|
|
|
|
|
|
|
|
/* RESET */
|
|
|
|
|
|
|
|
#define FTDI_RESET_SIO 0
|
|
|
|
#define FTDI_RESET_RX 1
|
|
|
|
#define FTDI_RESET_TX 2
|
|
|
|
|
|
|
|
/* SET_MDM_CTRL */
|
|
|
|
|
|
|
|
#define FTDI_DTR 1
|
2008-08-13 08:23:17 +04:00
|
|
|
#define FTDI_SET_DTR (FTDI_DTR << 8)
|
2008-01-14 06:41:02 +03:00
|
|
|
#define FTDI_RTS 2
|
2008-08-13 08:23:17 +04:00
|
|
|
#define FTDI_SET_RTS (FTDI_RTS << 8)
|
2008-01-14 06:41:02 +03:00
|
|
|
|
|
|
|
/* SET_FLOW_CTRL */
|
|
|
|
|
|
|
|
#define FTDI_RTS_CTS_HS 1
|
|
|
|
#define FTDI_DTR_DSR_HS 2
|
|
|
|
#define FTDI_XON_XOFF_HS 4
|
|
|
|
|
|
|
|
/* SET_DATA */
|
|
|
|
|
|
|
|
#define FTDI_PARITY (0x7 << 8)
|
|
|
|
#define FTDI_ODD (0x1 << 8)
|
|
|
|
#define FTDI_EVEN (0x2 << 8)
|
|
|
|
#define FTDI_MARK (0x3 << 8)
|
|
|
|
#define FTDI_SPACE (0x4 << 8)
|
|
|
|
|
|
|
|
#define FTDI_STOP (0x3 << 11)
|
|
|
|
#define FTDI_STOP1 (0x0 << 11)
|
|
|
|
#define FTDI_STOP15 (0x1 << 11)
|
|
|
|
#define FTDI_STOP2 (0x2 << 11)
|
|
|
|
|
|
|
|
/* GET_MDM_ST */
|
|
|
|
/* TODO: should be sent every 40ms */
|
|
|
|
#define FTDI_CTS (1<<4) // CTS line status
|
|
|
|
#define FTDI_DSR (1<<5) // DSR line status
|
|
|
|
#define FTDI_RI (1<<6) // RI line status
|
|
|
|
#define FTDI_RLSD (1<<7) // Receive Line Signal Detect
|
|
|
|
|
|
|
|
/* Status */
|
|
|
|
|
|
|
|
#define FTDI_DR (1<<0) // Data Ready
|
|
|
|
#define FTDI_OE (1<<1) // Overrun Err
|
|
|
|
#define FTDI_PE (1<<2) // Parity Err
|
|
|
|
#define FTDI_FE (1<<3) // Framing Err
|
|
|
|
#define FTDI_BI (1<<4) // Break Interrupt
|
|
|
|
#define FTDI_THRE (1<<5) // Transmitter Holding Register
|
|
|
|
#define FTDI_TEMT (1<<6) // Transmitter Empty
|
|
|
|
#define FTDI_FIFO (1<<7) // Error in FIFO
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
USBDevice dev;
|
|
|
|
uint8_t recv_buf[RECV_BUF];
|
2008-09-18 02:04:21 +04:00
|
|
|
uint16_t recv_ptr;
|
|
|
|
uint16_t recv_used;
|
2008-01-14 06:41:02 +03:00
|
|
|
uint8_t event_chr;
|
|
|
|
uint8_t error_chr;
|
|
|
|
uint8_t event_trigger;
|
|
|
|
QEMUSerialSetParams params;
|
|
|
|
int latency; /* ms */
|
|
|
|
CharDriverState *cs;
|
|
|
|
} USBSerialState;
|
|
|
|
|
2010-11-17 13:05:32 +03:00
|
|
|
enum {
|
|
|
|
STR_MANUFACTURER = 1,
|
|
|
|
STR_PRODUCT_SERIAL,
|
|
|
|
STR_PRODUCT_BRAILLE,
|
|
|
|
STR_SERIALNUMBER,
|
2008-01-14 06:41:02 +03:00
|
|
|
};
|
|
|
|
|
2010-11-17 13:05:32 +03:00
|
|
|
static const USBDescStrings desc_strings = {
|
|
|
|
[STR_MANUFACTURER] = "QEMU " QEMU_VERSION,
|
|
|
|
[STR_PRODUCT_SERIAL] = "QEMU USB SERIAL",
|
|
|
|
[STR_PRODUCT_BRAILLE] = "QEMU USB BRAILLE",
|
|
|
|
[STR_SERIALNUMBER] = "1",
|
|
|
|
};
|
|
|
|
|
|
|
|
static const USBDescIface desc_iface0 = {
|
|
|
|
.bInterfaceNumber = 0,
|
|
|
|
.bNumEndpoints = 2,
|
|
|
|
.bInterfaceClass = 0xff,
|
|
|
|
.bInterfaceSubClass = 0xff,
|
|
|
|
.bInterfaceProtocol = 0xff,
|
|
|
|
.eps = (USBDescEndpoint[]) {
|
|
|
|
{
|
|
|
|
.bEndpointAddress = USB_DIR_IN | 0x01,
|
|
|
|
.bmAttributes = USB_ENDPOINT_XFER_BULK,
|
|
|
|
.wMaxPacketSize = 64,
|
|
|
|
},{
|
|
|
|
.bEndpointAddress = USB_DIR_OUT | 0x02,
|
|
|
|
.bmAttributes = USB_ENDPOINT_XFER_BULK,
|
|
|
|
.wMaxPacketSize = 64,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static const USBDescDevice desc_device = {
|
|
|
|
.bcdUSB = 0x0200,
|
|
|
|
.bMaxPacketSize0 = 8,
|
|
|
|
.bNumConfigurations = 1,
|
|
|
|
.confs = (USBDescConfig[]) {
|
|
|
|
{
|
|
|
|
.bNumInterfaces = 1,
|
|
|
|
.bConfigurationValue = 1,
|
|
|
|
.bmAttributes = 0x80,
|
|
|
|
.bMaxPower = 50,
|
2011-04-03 09:33:19 +04:00
|
|
|
.nif = 1,
|
2010-11-17 13:05:32 +03:00
|
|
|
.ifs = &desc_iface0,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
static const USBDesc desc_serial = {
|
|
|
|
.id = {
|
|
|
|
.idVendor = 0x0403,
|
|
|
|
.idProduct = 0x6001,
|
|
|
|
.bcdDevice = 0x0400,
|
|
|
|
.iManufacturer = STR_MANUFACTURER,
|
|
|
|
.iProduct = STR_PRODUCT_SERIAL,
|
|
|
|
.iSerialNumber = STR_SERIALNUMBER,
|
|
|
|
},
|
|
|
|
.full = &desc_device,
|
|
|
|
.str = desc_strings,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const USBDesc desc_braille = {
|
|
|
|
.id = {
|
|
|
|
.idVendor = 0x0403,
|
|
|
|
.idProduct = 0xfe72,
|
|
|
|
.bcdDevice = 0x0400,
|
|
|
|
.iManufacturer = STR_MANUFACTURER,
|
|
|
|
.iProduct = STR_PRODUCT_BRAILLE,
|
|
|
|
.iSerialNumber = STR_SERIALNUMBER,
|
|
|
|
},
|
|
|
|
.full = &desc_device,
|
|
|
|
.str = desc_strings,
|
2008-01-14 06:41:02 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
static void usb_serial_reset(USBSerialState *s)
|
|
|
|
{
|
|
|
|
/* TODO: Set flow control to none */
|
|
|
|
s->event_chr = 0x0d;
|
|
|
|
s->event_trigger = 0;
|
|
|
|
s->recv_ptr = 0;
|
|
|
|
s->recv_used = 0;
|
|
|
|
/* TODO: purge in char driver */
|
|
|
|
}
|
|
|
|
|
|
|
|
static void usb_serial_handle_reset(USBDevice *dev)
|
|
|
|
{
|
|
|
|
USBSerialState *s = (USBSerialState *)dev;
|
|
|
|
|
|
|
|
DPRINTF("Reset\n");
|
|
|
|
|
|
|
|
usb_serial_reset(s);
|
|
|
|
/* TODO: Reset char device, send BREAK? */
|
|
|
|
}
|
|
|
|
|
2008-08-13 08:23:17 +04:00
|
|
|
static uint8_t usb_get_modem_lines(USBSerialState *s)
|
|
|
|
{
|
|
|
|
int flags;
|
|
|
|
uint8_t ret;
|
|
|
|
|
2011-08-15 20:17:34 +04:00
|
|
|
if (qemu_chr_fe_ioctl(s->cs, CHR_IOCTL_SERIAL_GET_TIOCM, &flags) == -ENOTSUP)
|
2008-08-13 08:23:17 +04:00
|
|
|
return FTDI_CTS|FTDI_DSR|FTDI_RLSD;
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
if (flags & CHR_TIOCM_CTS)
|
|
|
|
ret |= FTDI_CTS;
|
|
|
|
if (flags & CHR_TIOCM_DSR)
|
|
|
|
ret |= FTDI_DSR;
|
|
|
|
if (flags & CHR_TIOCM_RI)
|
|
|
|
ret |= FTDI_RI;
|
|
|
|
if (flags & CHR_TIOCM_CAR)
|
|
|
|
ret |= FTDI_RLSD;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-02-02 18:33:13 +03:00
|
|
|
static int usb_serial_handle_control(USBDevice *dev, USBPacket *p,
|
|
|
|
int request, int value, int index, int length, uint8_t *data)
|
2008-01-14 06:41:02 +03:00
|
|
|
{
|
|
|
|
USBSerialState *s = (USBSerialState *)dev;
|
2010-11-17 13:05:32 +03:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
DPRINTF("got control %x, value %x\n",request, value);
|
2011-02-02 18:33:13 +03:00
|
|
|
ret = usb_desc_handle_control(dev, p, request, value, index, length, data);
|
2010-11-17 13:05:32 +03:00
|
|
|
if (ret >= 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2008-01-14 06:41:02 +03:00
|
|
|
|
2010-11-17 13:05:32 +03:00
|
|
|
ret = 0;
|
2008-01-14 06:41:02 +03:00
|
|
|
switch (request) {
|
|
|
|
case EndpointOutRequest | USB_REQ_CLEAR_FEATURE:
|
|
|
|
ret = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Class specific requests. */
|
|
|
|
case DeviceOutVendor | FTDI_RESET:
|
|
|
|
switch (value) {
|
|
|
|
case FTDI_RESET_SIO:
|
|
|
|
usb_serial_reset(s);
|
|
|
|
break;
|
|
|
|
case FTDI_RESET_RX:
|
|
|
|
s->recv_ptr = 0;
|
|
|
|
s->recv_used = 0;
|
|
|
|
/* TODO: purge from char device */
|
|
|
|
break;
|
|
|
|
case FTDI_RESET_TX:
|
|
|
|
/* TODO: purge from char device */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case DeviceOutVendor | FTDI_SET_MDM_CTRL:
|
2008-08-13 08:23:17 +04:00
|
|
|
{
|
|
|
|
static int flags;
|
2011-08-15 20:17:34 +04:00
|
|
|
qemu_chr_fe_ioctl(s->cs,CHR_IOCTL_SERIAL_GET_TIOCM, &flags);
|
2008-08-13 08:23:17 +04:00
|
|
|
if (value & FTDI_SET_RTS) {
|
|
|
|
if (value & FTDI_RTS)
|
|
|
|
flags |= CHR_TIOCM_RTS;
|
|
|
|
else
|
|
|
|
flags &= ~CHR_TIOCM_RTS;
|
|
|
|
}
|
|
|
|
if (value & FTDI_SET_DTR) {
|
|
|
|
if (value & FTDI_DTR)
|
|
|
|
flags |= CHR_TIOCM_DTR;
|
|
|
|
else
|
|
|
|
flags &= ~CHR_TIOCM_DTR;
|
|
|
|
}
|
2011-08-15 20:17:34 +04:00
|
|
|
qemu_chr_fe_ioctl(s->cs,CHR_IOCTL_SERIAL_SET_TIOCM, &flags);
|
2008-01-14 06:41:02 +03:00
|
|
|
break;
|
2008-08-13 08:23:17 +04:00
|
|
|
}
|
2008-01-14 06:41:02 +03:00
|
|
|
case DeviceOutVendor | FTDI_SET_FLOW_CTRL:
|
|
|
|
/* TODO: ioctl */
|
|
|
|
break;
|
|
|
|
case DeviceOutVendor | FTDI_SET_BAUD: {
|
|
|
|
static const int subdivisors8[8] = { 0, 4, 2, 1, 3, 5, 6, 7 };
|
|
|
|
int subdivisor8 = subdivisors8[((value & 0xc000) >> 14)
|
|
|
|
| ((index & 1) << 2)];
|
|
|
|
int divisor = value & 0x3fff;
|
|
|
|
|
|
|
|
/* chip special cases */
|
|
|
|
if (divisor == 1 && subdivisor8 == 0)
|
|
|
|
subdivisor8 = 4;
|
|
|
|
if (divisor == 0 && subdivisor8 == 0)
|
|
|
|
divisor = 1;
|
|
|
|
|
|
|
|
s->params.speed = (48000000 / 2) / (8 * divisor + subdivisor8);
|
2011-08-15 20:17:34 +04:00
|
|
|
qemu_chr_fe_ioctl(s->cs, CHR_IOCTL_SERIAL_SET_PARAMS, &s->params);
|
2008-01-14 06:41:02 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case DeviceOutVendor | FTDI_SET_DATA:
|
|
|
|
switch (value & FTDI_PARITY) {
|
|
|
|
case 0:
|
|
|
|
s->params.parity = 'N';
|
|
|
|
break;
|
|
|
|
case FTDI_ODD:
|
|
|
|
s->params.parity = 'O';
|
|
|
|
break;
|
|
|
|
case FTDI_EVEN:
|
|
|
|
s->params.parity = 'E';
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
DPRINTF("unsupported parity %d\n", value & FTDI_PARITY);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
switch (value & FTDI_STOP) {
|
|
|
|
case FTDI_STOP1:
|
|
|
|
s->params.stop_bits = 1;
|
|
|
|
break;
|
|
|
|
case FTDI_STOP2:
|
|
|
|
s->params.stop_bits = 2;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
DPRINTF("unsupported stop bits %d\n", value & FTDI_STOP);
|
|
|
|
goto fail;
|
|
|
|
}
|
2011-08-15 20:17:34 +04:00
|
|
|
qemu_chr_fe_ioctl(s->cs, CHR_IOCTL_SERIAL_SET_PARAMS, &s->params);
|
2008-01-14 06:41:02 +03:00
|
|
|
/* TODO: TX ON/OFF */
|
|
|
|
break;
|
|
|
|
case DeviceInVendor | FTDI_GET_MDM_ST:
|
2008-08-13 08:23:17 +04:00
|
|
|
data[0] = usb_get_modem_lines(s) | 1;
|
|
|
|
data[1] = 0;
|
|
|
|
ret = 2;
|
2008-01-14 06:41:02 +03:00
|
|
|
break;
|
|
|
|
case DeviceOutVendor | FTDI_SET_EVENT_CHR:
|
|
|
|
/* TODO: handle it */
|
|
|
|
s->event_chr = value;
|
|
|
|
break;
|
|
|
|
case DeviceOutVendor | FTDI_SET_ERROR_CHR:
|
|
|
|
/* TODO: handle it */
|
|
|
|
s->error_chr = value;
|
|
|
|
break;
|
|
|
|
case DeviceOutVendor | FTDI_SET_LATENCY:
|
|
|
|
s->latency = value;
|
|
|
|
break;
|
|
|
|
case DeviceInVendor | FTDI_GET_LATENCY:
|
|
|
|
data[0] = s->latency;
|
|
|
|
ret = 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fail:
|
|
|
|
DPRINTF("got unsupported/bogus control %x, value %x\n", request, value);
|
|
|
|
ret = USB_RET_STALL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int usb_serial_handle_data(USBDevice *dev, USBPacket *p)
|
|
|
|
{
|
|
|
|
USBSerialState *s = (USBSerialState *)dev;
|
2011-07-13 12:53:23 +04:00
|
|
|
int i, ret = 0;
|
2012-01-12 16:23:01 +04:00
|
|
|
uint8_t devep = p->ep->nr;
|
2011-07-13 12:53:23 +04:00
|
|
|
struct iovec *iov;
|
|
|
|
uint8_t header[2];
|
|
|
|
int first_len, len;
|
2008-01-14 06:41:02 +03:00
|
|
|
|
|
|
|
switch (p->pid) {
|
|
|
|
case USB_TOKEN_OUT:
|
|
|
|
if (devep != 2)
|
|
|
|
goto fail;
|
2011-07-13 12:53:23 +04:00
|
|
|
for (i = 0; i < p->iov.niov; i++) {
|
|
|
|
iov = p->iov.iov + i;
|
2011-08-15 20:17:28 +04:00
|
|
|
qemu_chr_fe_write(s->cs, iov->iov_base, iov->iov_len);
|
2011-07-13 12:53:23 +04:00
|
|
|
}
|
2008-01-14 06:41:02 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case USB_TOKEN_IN:
|
|
|
|
if (devep != 1)
|
|
|
|
goto fail;
|
|
|
|
first_len = RECV_BUF - s->recv_ptr;
|
2011-07-13 12:53:23 +04:00
|
|
|
len = p->iov.size;
|
2008-01-14 06:41:02 +03:00
|
|
|
if (len <= 2) {
|
|
|
|
ret = USB_RET_NAK;
|
|
|
|
break;
|
|
|
|
}
|
2011-07-13 12:53:23 +04:00
|
|
|
header[0] = usb_get_modem_lines(s) | 1;
|
2008-08-13 08:23:17 +04:00
|
|
|
/* We do not have the uart details */
|
2009-05-18 19:00:26 +04:00
|
|
|
/* handle serial break */
|
|
|
|
if (s->event_trigger && s->event_trigger & FTDI_BI) {
|
|
|
|
s->event_trigger &= ~FTDI_BI;
|
2011-07-13 12:53:23 +04:00
|
|
|
header[1] = FTDI_BI;
|
|
|
|
usb_packet_copy(p, header, 2);
|
2009-05-18 19:00:26 +04:00
|
|
|
ret = 2;
|
|
|
|
break;
|
|
|
|
} else {
|
2011-07-13 12:53:23 +04:00
|
|
|
header[1] = 0;
|
2009-05-18 19:00:26 +04:00
|
|
|
}
|
2008-01-14 06:41:02 +03:00
|
|
|
len -= 2;
|
|
|
|
if (len > s->recv_used)
|
|
|
|
len = s->recv_used;
|
|
|
|
if (!len) {
|
|
|
|
ret = USB_RET_NAK;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (first_len > len)
|
|
|
|
first_len = len;
|
2011-07-13 12:53:23 +04:00
|
|
|
usb_packet_copy(p, header, 2);
|
|
|
|
usb_packet_copy(p, s->recv_buf + s->recv_ptr, first_len);
|
2008-01-14 06:41:02 +03:00
|
|
|
if (len > first_len)
|
2011-07-13 12:53:23 +04:00
|
|
|
usb_packet_copy(p, s->recv_buf, len - first_len);
|
2008-01-14 06:41:02 +03:00
|
|
|
s->recv_used -= len;
|
|
|
|
s->recv_ptr = (s->recv_ptr + len) % RECV_BUF;
|
|
|
|
ret = len + 2;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
DPRINTF("Bad token\n");
|
|
|
|
fail:
|
|
|
|
ret = USB_RET_STALL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void usb_serial_handle_destroy(USBDevice *dev)
|
|
|
|
{
|
|
|
|
USBSerialState *s = (USBSerialState *)dev;
|
|
|
|
|
2011-08-15 20:17:38 +04:00
|
|
|
qemu_chr_delete(s->cs);
|
2008-01-14 06:41:02 +03:00
|
|
|
}
|
|
|
|
|
2008-08-18 00:26:25 +04:00
|
|
|
static int usb_serial_can_read(void *opaque)
|
2008-01-14 06:41:02 +03:00
|
|
|
{
|
|
|
|
USBSerialState *s = opaque;
|
|
|
|
return RECV_BUF - s->recv_used;
|
|
|
|
}
|
|
|
|
|
2008-08-18 00:26:25 +04:00
|
|
|
static void usb_serial_read(void *opaque, const uint8_t *buf, int size)
|
2008-01-14 06:41:02 +03:00
|
|
|
{
|
|
|
|
USBSerialState *s = opaque;
|
2010-02-03 19:00:54 +03:00
|
|
|
int first_size, start;
|
|
|
|
|
|
|
|
/* room in the buffer? */
|
|
|
|
if (size > (RECV_BUF - s->recv_used))
|
|
|
|
size = RECV_BUF - s->recv_used;
|
|
|
|
|
|
|
|
start = s->recv_ptr + s->recv_used;
|
|
|
|
if (start < RECV_BUF) {
|
|
|
|
/* copy data to end of buffer */
|
|
|
|
first_size = RECV_BUF - start;
|
|
|
|
if (first_size > size)
|
|
|
|
first_size = size;
|
|
|
|
|
|
|
|
memcpy(s->recv_buf + start, buf, first_size);
|
|
|
|
|
|
|
|
/* wrap around to front if needed */
|
|
|
|
if (size > first_size)
|
|
|
|
memcpy(s->recv_buf, buf + first_size, size - first_size);
|
|
|
|
} else {
|
|
|
|
start -= RECV_BUF;
|
|
|
|
memcpy(s->recv_buf + start, buf, size);
|
|
|
|
}
|
2008-01-14 06:41:02 +03:00
|
|
|
s->recv_used += size;
|
|
|
|
}
|
|
|
|
|
2008-08-18 00:26:25 +04:00
|
|
|
static void usb_serial_event(void *opaque, int event)
|
2008-01-14 06:41:02 +03:00
|
|
|
{
|
|
|
|
USBSerialState *s = opaque;
|
|
|
|
|
|
|
|
switch (event) {
|
|
|
|
case CHR_EVENT_BREAK:
|
2009-05-18 19:00:26 +04:00
|
|
|
s->event_trigger |= FTDI_BI;
|
2008-01-14 06:41:02 +03:00
|
|
|
break;
|
|
|
|
case CHR_EVENT_FOCUS:
|
|
|
|
break;
|
2009-10-07 17:01:16 +04:00
|
|
|
case CHR_EVENT_OPENED:
|
2008-01-14 06:41:02 +03:00
|
|
|
usb_serial_reset(s);
|
|
|
|
/* TODO: Reset USB port */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-31 16:23:59 +04:00
|
|
|
static int usb_serial_initfn(USBDevice *dev)
|
|
|
|
{
|
|
|
|
USBSerialState *s = DO_UPCAST(USBSerialState, dev, dev);
|
2010-11-26 22:20:41 +03:00
|
|
|
|
|
|
|
usb_desc_init(dev);
|
2009-10-26 17:56:47 +03:00
|
|
|
|
2010-05-28 19:03:22 +04:00
|
|
|
if (!s->cs) {
|
|
|
|
error_report("Property chardev is required");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-10-26 17:56:47 +03:00
|
|
|
qemu_chr_add_handlers(s->cs, usb_serial_can_read, usb_serial_read,
|
|
|
|
usb_serial_event, s);
|
|
|
|
usb_serial_handle_reset(dev);
|
2009-08-31 16:23:59 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-02-27 18:18:47 +04:00
|
|
|
static USBDevice *usb_serial_init(USBBus *bus, const char *filename)
|
2008-01-14 06:41:02 +03:00
|
|
|
{
|
2009-08-31 16:23:59 +04:00
|
|
|
USBDevice *dev;
|
2008-01-14 06:41:02 +03:00
|
|
|
CharDriverState *cdrv;
|
2009-10-26 17:56:47 +03:00
|
|
|
uint32_t vendorid = 0, productid = 0;
|
2008-10-31 20:31:29 +03:00
|
|
|
char label[32];
|
|
|
|
static int index;
|
2008-01-14 06:41:02 +03:00
|
|
|
|
|
|
|
while (*filename && *filename != ':') {
|
|
|
|
const char *p;
|
|
|
|
char *e;
|
|
|
|
if (strstart(filename, "vendorid=", &p)) {
|
|
|
|
vendorid = strtol(p, &e, 16);
|
|
|
|
if (e == p || (*e && *e != ',' && *e != ':')) {
|
2010-02-18 19:25:24 +03:00
|
|
|
error_report("bogus vendor ID %s", p);
|
2008-01-14 06:41:02 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
filename = e;
|
|
|
|
} else if (strstart(filename, "productid=", &p)) {
|
|
|
|
productid = strtol(p, &e, 16);
|
|
|
|
if (e == p || (*e && *e != ',' && *e != ':')) {
|
2010-02-18 19:25:24 +03:00
|
|
|
error_report("bogus product ID %s", p);
|
2008-01-14 06:41:02 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
filename = e;
|
|
|
|
} else {
|
2010-02-18 19:25:24 +03:00
|
|
|
error_report("unrecognized serial USB option %s", filename);
|
2008-01-14 06:41:02 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
while(*filename == ',')
|
|
|
|
filename++;
|
|
|
|
}
|
|
|
|
if (!*filename) {
|
2010-02-18 19:25:24 +03:00
|
|
|
error_report("character device specification needed");
|
2008-01-14 06:41:02 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
filename++;
|
|
|
|
|
2008-10-31 20:31:29 +03:00
|
|
|
snprintf(label, sizeof(label), "usbserial%d", index++);
|
2011-08-15 20:17:36 +04:00
|
|
|
cdrv = qemu_chr_new(label, filename, NULL);
|
2008-01-14 06:41:02 +03:00
|
|
|
if (!cdrv)
|
2009-08-31 16:23:59 +04:00
|
|
|
return NULL;
|
2008-01-14 06:41:02 +03:00
|
|
|
|
2012-02-27 18:18:47 +04:00
|
|
|
dev = usb_create(bus, "usb-serial");
|
2010-02-25 16:29:06 +03:00
|
|
|
if (!dev) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2009-10-26 17:56:47 +03:00
|
|
|
qdev_prop_set_chr(&dev->qdev, "chardev", cdrv);
|
|
|
|
if (vendorid)
|
|
|
|
qdev_prop_set_uint16(&dev->qdev, "vendorid", vendorid);
|
|
|
|
if (productid)
|
|
|
|
qdev_prop_set_uint16(&dev->qdev, "productid", productid);
|
2010-01-15 14:56:41 +03:00
|
|
|
qdev_init_nofail(&dev->qdev);
|
2008-01-14 06:41:02 +03:00
|
|
|
|
2009-10-26 17:56:47 +03:00
|
|
|
return dev;
|
|
|
|
}
|
|
|
|
|
2012-02-27 18:18:47 +04:00
|
|
|
static USBDevice *usb_braille_init(USBBus *bus, const char *unused)
|
2009-10-26 17:56:47 +03:00
|
|
|
{
|
|
|
|
USBDevice *dev;
|
|
|
|
CharDriverState *cdrv;
|
|
|
|
|
2011-08-15 20:17:36 +04:00
|
|
|
cdrv = qemu_chr_new("braille", "braille", NULL);
|
2009-10-26 17:56:47 +03:00
|
|
|
if (!cdrv)
|
|
|
|
return NULL;
|
2009-08-31 16:23:59 +04:00
|
|
|
|
2012-02-27 18:18:47 +04:00
|
|
|
dev = usb_create(bus, "usb-braille");
|
2009-10-26 17:56:47 +03:00
|
|
|
qdev_prop_set_chr(&dev->qdev, "chardev", cdrv);
|
2010-01-15 14:56:41 +03:00
|
|
|
qdev_init_nofail(&dev->qdev);
|
2009-10-26 17:56:47 +03:00
|
|
|
|
|
|
|
return dev;
|
2008-01-14 06:41:02 +03:00
|
|
|
}
|
2009-08-31 16:23:59 +04:00
|
|
|
|
2011-07-20 12:06:18 +04:00
|
|
|
static const VMStateDescription vmstate_usb_serial = {
|
|
|
|
.name = "usb-serial",
|
|
|
|
.unmigratable = 1,
|
|
|
|
};
|
|
|
|
|
2011-12-08 07:34:16 +04:00
|
|
|
static Property serial_properties[] = {
|
|
|
|
DEFINE_PROP_CHR("chardev", USBSerialState, cs),
|
|
|
|
DEFINE_PROP_END_OF_LIST(),
|
|
|
|
};
|
|
|
|
|
2011-12-16 00:53:10 +04:00
|
|
|
static void usb_serial_class_initfn(ObjectClass *klass, void *data)
|
|
|
|
{
|
2011-12-08 07:34:16 +04:00
|
|
|
DeviceClass *dc = DEVICE_CLASS(klass);
|
2011-12-16 00:53:10 +04:00
|
|
|
USBDeviceClass *uc = USB_DEVICE_CLASS(klass);
|
|
|
|
|
|
|
|
uc->init = usb_serial_initfn;
|
|
|
|
uc->product_desc = "QEMU USB Serial";
|
|
|
|
uc->usb_desc = &desc_serial;
|
|
|
|
uc->handle_reset = usb_serial_handle_reset;
|
|
|
|
uc->handle_control = usb_serial_handle_control;
|
|
|
|
uc->handle_data = usb_serial_handle_data;
|
|
|
|
uc->handle_destroy = usb_serial_handle_destroy;
|
2011-12-08 07:34:16 +04:00
|
|
|
dc->vmsd = &vmstate_usb_serial;
|
|
|
|
dc->props = serial_properties;
|
2011-12-16 00:53:10 +04:00
|
|
|
}
|
|
|
|
|
2011-12-08 07:34:16 +04:00
|
|
|
static TypeInfo serial_info = {
|
|
|
|
.name = "usb-serial",
|
|
|
|
.parent = TYPE_USB_DEVICE,
|
|
|
|
.instance_size = sizeof(USBSerialState),
|
|
|
|
.class_init = usb_serial_class_initfn,
|
|
|
|
};
|
|
|
|
|
|
|
|
static Property braille_properties[] = {
|
|
|
|
DEFINE_PROP_CHR("chardev", USBSerialState, cs),
|
|
|
|
DEFINE_PROP_END_OF_LIST(),
|
2009-10-26 17:56:47 +03:00
|
|
|
};
|
|
|
|
|
2011-12-16 00:53:10 +04:00
|
|
|
static void usb_braille_class_initfn(ObjectClass *klass, void *data)
|
|
|
|
{
|
2011-12-08 07:34:16 +04:00
|
|
|
DeviceClass *dc = DEVICE_CLASS(klass);
|
2011-12-16 00:53:10 +04:00
|
|
|
USBDeviceClass *uc = USB_DEVICE_CLASS(klass);
|
|
|
|
|
|
|
|
uc->init = usb_serial_initfn;
|
|
|
|
uc->product_desc = "QEMU USB Braille";
|
|
|
|
uc->usb_desc = &desc_braille;
|
|
|
|
uc->handle_reset = usb_serial_handle_reset;
|
|
|
|
uc->handle_control = usb_serial_handle_control;
|
|
|
|
uc->handle_data = usb_serial_handle_data;
|
|
|
|
uc->handle_destroy = usb_serial_handle_destroy;
|
2011-12-08 07:34:16 +04:00
|
|
|
dc->vmsd = &vmstate_usb_serial;
|
|
|
|
dc->props = braille_properties;
|
2011-12-16 00:53:10 +04:00
|
|
|
}
|
|
|
|
|
2011-12-08 07:34:16 +04:00
|
|
|
static TypeInfo braille_info = {
|
|
|
|
.name = "usb-braille",
|
|
|
|
.parent = TYPE_USB_DEVICE,
|
|
|
|
.instance_size = sizeof(USBSerialState),
|
|
|
|
.class_init = usb_braille_class_initfn,
|
2009-08-31 16:23:59 +04:00
|
|
|
};
|
|
|
|
|
2012-02-09 18:20:55 +04:00
|
|
|
static void usb_serial_register_types(void)
|
2009-08-31 16:23:59 +04:00
|
|
|
{
|
2011-12-08 07:34:16 +04:00
|
|
|
type_register_static(&serial_info);
|
2011-12-09 00:56:53 +04:00
|
|
|
usb_legacy_register("usb-serial", "serial", usb_serial_init);
|
2011-12-08 07:34:16 +04:00
|
|
|
type_register_static(&braille_info);
|
2011-12-09 00:56:53 +04:00
|
|
|
usb_legacy_register("usb-braille", "braille", usb_braille_init);
|
2009-08-31 16:23:59 +04:00
|
|
|
}
|
2012-02-09 18:20:55 +04:00
|
|
|
|
|
|
|
type_init(usb_serial_register_types)
|