2 * FTDI FT232BM Device emulation
4 * Copyright (c) 2006 CodeSourcery.
5 * Copyright (c) 2008 Samuel Thibault <samuel.thibault@ens-lyon.org>
6 * Written by Paul Brook, reused for FTDI by Samuel Thibault
8 * This code is licenced under the LGPL.
11 #include "qemu-common.h"
12 #include "qemu-error.h"
14 #include "qemu-char.h"
16 //#define DEBUG_Serial
19 #define DPRINTF(fmt, ...) \
20 do { printf("usb-serial: " fmt , ## __VA_ARGS__); } while (0)
22 #define DPRINTF(fmt, ...) do {} while(0)
29 #define FTDI_SET_MDM_CTRL 1
30 #define FTDI_SET_FLOW_CTRL 2
31 #define FTDI_SET_BAUD 3
32 #define FTDI_SET_DATA 4
33 #define FTDI_GET_MDM_ST 5
34 #define FTDI_SET_EVENT_CHR 6
35 #define FTDI_SET_ERROR_CHR 7
36 #define FTDI_SET_LATENCY 9
37 #define FTDI_GET_LATENCY 10
39 #define DeviceOutVendor ((USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_DEVICE)<<8)
40 #define DeviceInVendor ((USB_DIR_IN |USB_TYPE_VENDOR|USB_RECIP_DEVICE)<<8)
44 #define FTDI_RESET_SIO 0
45 #define FTDI_RESET_RX 1
46 #define FTDI_RESET_TX 2
51 #define FTDI_SET_DTR (FTDI_DTR << 8)
53 #define FTDI_SET_RTS (FTDI_RTS << 8)
57 #define FTDI_RTS_CTS_HS 1
58 #define FTDI_DTR_DSR_HS 2
59 #define FTDI_XON_XOFF_HS 4
63 #define FTDI_PARITY (0x7 << 8)
64 #define FTDI_ODD (0x1 << 8)
65 #define FTDI_EVEN (0x2 << 8)
66 #define FTDI_MARK (0x3 << 8)
67 #define FTDI_SPACE (0x4 << 8)
69 #define FTDI_STOP (0x3 << 11)
70 #define FTDI_STOP1 (0x0 << 11)
71 #define FTDI_STOP15 (0x1 << 11)
72 #define FTDI_STOP2 (0x2 << 11)
75 /* TODO: should be sent every 40ms */
76 #define FTDI_CTS (1<<4) // CTS line status
77 #define FTDI_DSR (1<<5) // DSR line status
78 #define FTDI_RI (1<<6) // RI line status
79 #define FTDI_RLSD (1<<7) // Receive Line Signal Detect
83 #define FTDI_DR (1<<0) // Data Ready
84 #define FTDI_OE (1<<1) // Overrun Err
85 #define FTDI_PE (1<<2) // Parity Err
86 #define FTDI_FE (1<<3) // Framing Err
87 #define FTDI_BI (1<<4) // Break Interrupt
88 #define FTDI_THRE (1<<5) // Transmitter Holding Register
89 #define FTDI_TEMT (1<<6) // Transmitter Empty
90 #define FTDI_FIFO (1<<7) // Error in FIFO
96 uint8_t recv_buf
[RECV_BUF
];
101 uint8_t event_trigger
;
102 QEMUSerialSetParams params
;
103 int latency
; /* ms */
107 static const uint8_t qemu_serial_dev_descriptor
[] = {
108 0x12, /* u8 bLength; */
109 0x01, /* u8 bDescriptorType; Device */
110 0x00, 0x02, /* u16 bcdUSB; v2.0 */
112 0x00, /* u8 bDeviceClass; */
113 0x00, /* u8 bDeviceSubClass; */
114 0x00, /* u8 bDeviceProtocol; [ low/full speeds only ] */
115 0x08, /* u8 bMaxPacketSize0; 8 Bytes */
117 /* Vendor and product id are arbitrary. */
118 0x03, 0x04, /* u16 idVendor; */
119 0x00, 0xFF, /* u16 idProduct; */
120 0x00, 0x04, /* u16 bcdDevice */
122 0x01, /* u8 iManufacturer; */
123 0x02, /* u8 iProduct; */
124 0x03, /* u8 iSerialNumber; */
125 0x01 /* u8 bNumConfigurations; */
128 static const uint8_t qemu_serial_config_descriptor
[] = {
130 /* one configuration */
131 0x09, /* u8 bLength; */
132 0x02, /* u8 bDescriptorType; Configuration */
133 0x20, 0x00, /* u16 wTotalLength; */
134 0x01, /* u8 bNumInterfaces; (1) */
135 0x01, /* u8 bConfigurationValue; */
136 0x00, /* u8 iConfiguration; */
137 0x80, /* u8 bmAttributes;
142 100/2, /* u8 MaxPower; */
145 0x09, /* u8 if_bLength; */
146 0x04, /* u8 if_bDescriptorType; Interface */
147 0x00, /* u8 if_bInterfaceNumber; */
148 0x00, /* u8 if_bAlternateSetting; */
149 0x02, /* u8 if_bNumEndpoints; */
150 0xff, /* u8 if_bInterfaceClass; Vendor Specific */
151 0xff, /* u8 if_bInterfaceSubClass; Vendor Specific */
152 0xff, /* u8 if_bInterfaceProtocol; Vendor Specific */
153 0x02, /* u8 if_iInterface; */
155 /* Bulk-In endpoint */
156 0x07, /* u8 ep_bLength; */
157 0x05, /* u8 ep_bDescriptorType; Endpoint */
158 0x81, /* u8 ep_bEndpointAddress; IN Endpoint 1 */
159 0x02, /* u8 ep_bmAttributes; Bulk */
160 0x40, 0x00, /* u16 ep_wMaxPacketSize; */
161 0x00, /* u8 ep_bInterval; */
163 /* Bulk-Out endpoint */
164 0x07, /* u8 ep_bLength; */
165 0x05, /* u8 ep_bDescriptorType; Endpoint */
166 0x02, /* u8 ep_bEndpointAddress; OUT Endpoint 2 */
167 0x02, /* u8 ep_bmAttributes; Bulk */
168 0x40, 0x00, /* u16 ep_wMaxPacketSize; */
169 0x00 /* u8 ep_bInterval; */
172 static void usb_serial_reset(USBSerialState
*s
)
174 /* TODO: Set flow control to none */
176 s
->event_trigger
= 0;
179 /* TODO: purge in char driver */
182 static void usb_serial_handle_reset(USBDevice
*dev
)
184 USBSerialState
*s
= (USBSerialState
*)dev
;
189 /* TODO: Reset char device, send BREAK? */
192 static uint8_t usb_get_modem_lines(USBSerialState
*s
)
197 if (qemu_chr_ioctl(s
->cs
, CHR_IOCTL_SERIAL_GET_TIOCM
, &flags
) == -ENOTSUP
)
198 return FTDI_CTS
|FTDI_DSR
|FTDI_RLSD
;
201 if (flags
& CHR_TIOCM_CTS
)
203 if (flags
& CHR_TIOCM_DSR
)
205 if (flags
& CHR_TIOCM_RI
)
207 if (flags
& CHR_TIOCM_CAR
)
213 static int usb_serial_handle_control(USBDevice
*dev
, int request
, int value
,
214 int index
, int length
, uint8_t *data
)
216 USBSerialState
*s
= (USBSerialState
*)dev
;
219 //DPRINTF("got control %x, value %x\n",request, value);
221 case DeviceRequest
| USB_REQ_GET_STATUS
:
222 data
[0] = (0 << USB_DEVICE_SELF_POWERED
) |
223 (dev
->remote_wakeup
<< USB_DEVICE_REMOTE_WAKEUP
);
227 case DeviceOutRequest
| USB_REQ_CLEAR_FEATURE
:
228 if (value
== USB_DEVICE_REMOTE_WAKEUP
) {
229 dev
->remote_wakeup
= 0;
235 case DeviceOutRequest
| USB_REQ_SET_FEATURE
:
236 if (value
== USB_DEVICE_REMOTE_WAKEUP
) {
237 dev
->remote_wakeup
= 1;
243 case DeviceOutRequest
| USB_REQ_SET_ADDRESS
:
247 case DeviceRequest
| USB_REQ_GET_DESCRIPTOR
:
250 memcpy(data
, qemu_serial_dev_descriptor
,
251 sizeof(qemu_serial_dev_descriptor
));
252 data
[8] = s
->vendorid
& 0xff;
253 data
[9] = ((s
->vendorid
) >> 8) & 0xff;
254 data
[10] = s
->productid
& 0xff;
255 data
[11] = ((s
->productid
) >> 8) & 0xff;
256 ret
= sizeof(qemu_serial_dev_descriptor
);
259 memcpy(data
, qemu_serial_config_descriptor
,
260 sizeof(qemu_serial_config_descriptor
));
261 ret
= sizeof(qemu_serial_config_descriptor
);
264 switch(value
& 0xff) {
274 /* vendor description */
275 ret
= set_usb_string(data
, "QEMU " QEMU_VERSION
);
278 /* product description */
279 ret
= set_usb_string(data
, "QEMU USB SERIAL");
283 ret
= set_usb_string(data
, "1");
293 case DeviceRequest
| USB_REQ_GET_CONFIGURATION
:
297 case DeviceOutRequest
| USB_REQ_SET_CONFIGURATION
:
300 case DeviceRequest
| USB_REQ_GET_INTERFACE
:
304 case InterfaceOutRequest
| USB_REQ_SET_INTERFACE
:
307 case EndpointOutRequest
| USB_REQ_CLEAR_FEATURE
:
311 /* Class specific requests. */
312 case DeviceOutVendor
| FTDI_RESET
:
320 /* TODO: purge from char device */
323 /* TODO: purge from char device */
327 case DeviceOutVendor
| FTDI_SET_MDM_CTRL
:
330 qemu_chr_ioctl(s
->cs
,CHR_IOCTL_SERIAL_GET_TIOCM
, &flags
);
331 if (value
& FTDI_SET_RTS
) {
332 if (value
& FTDI_RTS
)
333 flags
|= CHR_TIOCM_RTS
;
335 flags
&= ~CHR_TIOCM_RTS
;
337 if (value
& FTDI_SET_DTR
) {
338 if (value
& FTDI_DTR
)
339 flags
|= CHR_TIOCM_DTR
;
341 flags
&= ~CHR_TIOCM_DTR
;
343 qemu_chr_ioctl(s
->cs
,CHR_IOCTL_SERIAL_SET_TIOCM
, &flags
);
346 case DeviceOutVendor
| FTDI_SET_FLOW_CTRL
:
349 case DeviceOutVendor
| FTDI_SET_BAUD
: {
350 static const int subdivisors8
[8] = { 0, 4, 2, 1, 3, 5, 6, 7 };
351 int subdivisor8
= subdivisors8
[((value
& 0xc000) >> 14)
352 | ((index
& 1) << 2)];
353 int divisor
= value
& 0x3fff;
355 /* chip special cases */
356 if (divisor
== 1 && subdivisor8
== 0)
358 if (divisor
== 0 && subdivisor8
== 0)
361 s
->params
.speed
= (48000000 / 2) / (8 * divisor
+ subdivisor8
);
362 qemu_chr_ioctl(s
->cs
, CHR_IOCTL_SERIAL_SET_PARAMS
, &s
->params
);
365 case DeviceOutVendor
| FTDI_SET_DATA
:
366 switch (value
& FTDI_PARITY
) {
368 s
->params
.parity
= 'N';
371 s
->params
.parity
= 'O';
374 s
->params
.parity
= 'E';
377 DPRINTF("unsupported parity %d\n", value
& FTDI_PARITY
);
380 switch (value
& FTDI_STOP
) {
382 s
->params
.stop_bits
= 1;
385 s
->params
.stop_bits
= 2;
388 DPRINTF("unsupported stop bits %d\n", value
& FTDI_STOP
);
391 qemu_chr_ioctl(s
->cs
, CHR_IOCTL_SERIAL_SET_PARAMS
, &s
->params
);
392 /* TODO: TX ON/OFF */
394 case DeviceInVendor
| FTDI_GET_MDM_ST
:
395 data
[0] = usb_get_modem_lines(s
) | 1;
399 case DeviceOutVendor
| FTDI_SET_EVENT_CHR
:
400 /* TODO: handle it */
401 s
->event_chr
= value
;
403 case DeviceOutVendor
| FTDI_SET_ERROR_CHR
:
404 /* TODO: handle it */
405 s
->error_chr
= value
;
407 case DeviceOutVendor
| FTDI_SET_LATENCY
:
410 case DeviceInVendor
| FTDI_GET_LATENCY
:
411 data
[0] = s
->latency
;
416 DPRINTF("got unsupported/bogus control %x, value %x\n", request
, value
);
423 static int usb_serial_handle_data(USBDevice
*dev
, USBPacket
*p
)
425 USBSerialState
*s
= (USBSerialState
*)dev
;
427 uint8_t devep
= p
->devep
;
428 uint8_t *data
= p
->data
;
436 qemu_chr_write(s
->cs
, data
, len
);
442 first_len
= RECV_BUF
- s
->recv_ptr
;
447 *data
++ = usb_get_modem_lines(s
) | 1;
448 /* We do not have the uart details */
449 /* handle serial break */
450 if (s
->event_trigger
&& s
->event_trigger
& FTDI_BI
) {
451 s
->event_trigger
&= ~FTDI_BI
;
459 if (len
> s
->recv_used
)
467 memcpy(data
, s
->recv_buf
+ s
->recv_ptr
, first_len
);
469 memcpy(data
+ first_len
, s
->recv_buf
, len
- first_len
);
471 s
->recv_ptr
= (s
->recv_ptr
+ len
) % RECV_BUF
;
476 DPRINTF("Bad token\n");
485 static void usb_serial_handle_destroy(USBDevice
*dev
)
487 USBSerialState
*s
= (USBSerialState
*)dev
;
489 qemu_chr_close(s
->cs
);
492 static int usb_serial_can_read(void *opaque
)
494 USBSerialState
*s
= opaque
;
495 return RECV_BUF
- s
->recv_used
;
498 static void usb_serial_read(void *opaque
, const uint8_t *buf
, int size
)
500 USBSerialState
*s
= opaque
;
501 int first_size
, start
;
503 /* room in the buffer? */
504 if (size
> (RECV_BUF
- s
->recv_used
))
505 size
= RECV_BUF
- s
->recv_used
;
507 start
= s
->recv_ptr
+ s
->recv_used
;
508 if (start
< RECV_BUF
) {
509 /* copy data to end of buffer */
510 first_size
= RECV_BUF
- start
;
511 if (first_size
> size
)
514 memcpy(s
->recv_buf
+ start
, buf
, first_size
);
516 /* wrap around to front if needed */
517 if (size
> first_size
)
518 memcpy(s
->recv_buf
, buf
+ first_size
, size
- first_size
);
521 memcpy(s
->recv_buf
+ start
, buf
, size
);
523 s
->recv_used
+= size
;
526 static void usb_serial_event(void *opaque
, int event
)
528 USBSerialState
*s
= opaque
;
531 case CHR_EVENT_BREAK
:
532 s
->event_trigger
|= FTDI_BI
;
534 case CHR_EVENT_FOCUS
:
536 case CHR_EVENT_OPENED
:
538 /* TODO: Reset USB port */
543 static int usb_serial_initfn(USBDevice
*dev
)
545 USBSerialState
*s
= DO_UPCAST(USBSerialState
, dev
, dev
);
546 s
->dev
.speed
= USB_SPEED_FULL
;
548 qemu_chr_add_handlers(s
->cs
, usb_serial_can_read
, usb_serial_read
,
549 usb_serial_event
, s
);
550 usb_serial_handle_reset(dev
);
554 static USBDevice
*usb_serial_init(const char *filename
)
557 CharDriverState
*cdrv
;
558 uint32_t vendorid
= 0, productid
= 0;
562 while (*filename
&& *filename
!= ':') {
565 if (strstart(filename
, "vendorid=", &p
)) {
566 vendorid
= strtol(p
, &e
, 16);
567 if (e
== p
|| (*e
&& *e
!= ',' && *e
!= ':')) {
568 error_report("bogus vendor ID %s", p
);
572 } else if (strstart(filename
, "productid=", &p
)) {
573 productid
= strtol(p
, &e
, 16);
574 if (e
== p
|| (*e
&& *e
!= ',' && *e
!= ':')) {
575 error_report("bogus product ID %s", p
);
580 error_report("unrecognized serial USB option %s", filename
);
583 while(*filename
== ',')
587 error_report("character device specification needed");
592 snprintf(label
, sizeof(label
), "usbserial%d", index
++);
593 cdrv
= qemu_chr_open(label
, filename
, NULL
);
597 dev
= usb_create(NULL
/* FIXME */, "usb-serial");
601 qdev_prop_set_chr(&dev
->qdev
, "chardev", cdrv
);
603 qdev_prop_set_uint16(&dev
->qdev
, "vendorid", vendorid
);
605 qdev_prop_set_uint16(&dev
->qdev
, "productid", productid
);
606 qdev_init_nofail(&dev
->qdev
);
611 static USBDevice
*usb_braille_init(const char *unused
)
614 CharDriverState
*cdrv
;
616 cdrv
= qemu_chr_open("braille", "braille", NULL
);
620 dev
= usb_create(NULL
/* FIXME */, "usb-braille");
621 qdev_prop_set_chr(&dev
->qdev
, "chardev", cdrv
);
622 qdev_init_nofail(&dev
->qdev
);
627 static struct USBDeviceInfo serial_info
= {
628 .product_desc
= "QEMU USB Serial",
629 .qdev
.name
= "usb-serial",
630 .qdev
.size
= sizeof(USBSerialState
),
631 .init
= usb_serial_initfn
,
632 .handle_packet
= usb_generic_handle_packet
,
633 .handle_reset
= usb_serial_handle_reset
,
634 .handle_control
= usb_serial_handle_control
,
635 .handle_data
= usb_serial_handle_data
,
636 .handle_destroy
= usb_serial_handle_destroy
,
637 .usbdevice_name
= "serial",
638 .usbdevice_init
= usb_serial_init
,
639 .qdev
.props
= (Property
[]) {
640 DEFINE_PROP_CHR("chardev", USBSerialState
, cs
),
641 DEFINE_PROP_HEX32("vendorid", USBSerialState
, vendorid
, 0x0403),
642 DEFINE_PROP_HEX32("productid", USBSerialState
, productid
, 0x6001),
643 DEFINE_PROP_END_OF_LIST(),
647 static struct USBDeviceInfo braille_info
= {
648 .product_desc
= "QEMU USB Braille",
649 .qdev
.name
= "usb-braille",
650 .qdev
.size
= sizeof(USBSerialState
),
651 .init
= usb_serial_initfn
,
652 .handle_packet
= usb_generic_handle_packet
,
653 .handle_reset
= usb_serial_handle_reset
,
654 .handle_control
= usb_serial_handle_control
,
655 .handle_data
= usb_serial_handle_data
,
656 .handle_destroy
= usb_serial_handle_destroy
,
657 .usbdevice_name
= "braille",
658 .usbdevice_init
= usb_braille_init
,
659 .qdev
.props
= (Property
[]) {
660 DEFINE_PROP_CHR("chardev", USBSerialState
, cs
),
661 DEFINE_PROP_HEX32("vendorid", USBSerialState
, vendorid
, 0x0403),
662 DEFINE_PROP_HEX32("productid", USBSerialState
, productid
, 0xfe72),
663 DEFINE_PROP_END_OF_LIST(),
667 static void usb_serial_register_devices(void)
669 usb_qdev_register(&serial_info
);
670 usb_qdev_register(&braille_info
);
672 device_init(usb_serial_register_devices
)