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 licensed under the LGPL.
11 #include "qemu/osdep.h"
12 #include "qapi/error.h"
13 #include "qemu/cutils.h"
14 #include "qemu/error-report.h"
15 #include "qemu/module.h"
16 #include "hw/qdev-properties.h"
18 #include "migration/vmstate.h"
20 #include "chardev/char-serial.h"
21 #include "chardev/char-fe.h"
22 #include "qom/object.h"
24 //#define DEBUG_Serial
27 #define DPRINTF(fmt, ...) \
28 do { printf("usb-serial: " fmt , ## __VA_ARGS__); } while (0)
30 #define DPRINTF(fmt, ...) do {} while(0)
33 #define RECV_BUF (512 - (2 * 8))
37 #define FTDI_SET_MDM_CTRL 1
38 #define FTDI_SET_FLOW_CTRL 2
39 #define FTDI_SET_BAUD 3
40 #define FTDI_SET_DATA 4
41 #define FTDI_GET_MDM_ST 5
42 #define FTDI_SET_EVENT_CHR 6
43 #define FTDI_SET_ERROR_CHR 7
44 #define FTDI_SET_LATENCY 9
45 #define FTDI_GET_LATENCY 10
47 #define DeviceOutVendor \
48 ((USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE) << 8)
49 #define DeviceInVendor \
50 ((USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE) << 8)
54 #define FTDI_RESET_SIO 0
55 #define FTDI_RESET_RX 1
56 #define FTDI_RESET_TX 2
61 #define FTDI_SET_DTR (FTDI_DTR << 8)
63 #define FTDI_SET_RTS (FTDI_RTS << 8)
67 #define FTDI_RTS_CTS_HS 1
68 #define FTDI_DTR_DSR_HS 2
69 #define FTDI_XON_XOFF_HS 4
73 #define FTDI_PARITY (0x7 << 8)
74 #define FTDI_ODD (0x1 << 8)
75 #define FTDI_EVEN (0x2 << 8)
76 #define FTDI_MARK (0x3 << 8)
77 #define FTDI_SPACE (0x4 << 8)
79 #define FTDI_STOP (0x3 << 11)
80 #define FTDI_STOP1 (0x0 << 11)
81 #define FTDI_STOP15 (0x1 << 11)
82 #define FTDI_STOP2 (0x2 << 11)
85 /* TODO: should be sent every 40ms */
86 #define FTDI_CTS (1 << 4) /* CTS line status */
87 #define FTDI_DSR (1 << 5) /* DSR line status */
88 #define FTDI_RI (1 << 6) /* RI line status */
89 #define FTDI_RLSD (1 << 7) /* Receive Line Signal Detect */
93 #define FTDI_DR (1 << 0) /* Data Ready */
94 #define FTDI_OE (1 << 1) /* Overrun Err */
95 #define FTDI_PE (1 << 2) /* Parity Err */
96 #define FTDI_FE (1 << 3) /* Framing Err */
97 #define FTDI_BI (1 << 4) /* Break Interrupt */
98 #define FTDI_THRE (1 << 5) /* Transmitter Holding Register */
99 #define FTDI_TEMT (1 << 6) /* Transmitter Empty */
100 #define FTDI_FIFO (1 << 7) /* Error in FIFO */
102 struct USBSerialState
{
106 uint8_t recv_buf
[RECV_BUF
];
111 uint8_t event_trigger
;
112 QEMUSerialSetParams params
;
113 int latency
; /* ms */
117 #define TYPE_USB_SERIAL "usb-serial-dev"
118 OBJECT_DECLARE_SIMPLE_TYPE(USBSerialState
, USB_SERIAL
)
121 STR_MANUFACTURER
= 1,
127 static const USBDescStrings desc_strings
= {
128 [STR_MANUFACTURER
] = "QEMU",
129 [STR_PRODUCT_SERIAL
] = "QEMU USB SERIAL",
130 [STR_PRODUCT_BRAILLE
] = "QEMU USB BAUM BRAILLE",
131 [STR_SERIALNUMBER
] = "1",
134 static const USBDescIface desc_iface0
= {
135 .bInterfaceNumber
= 0,
137 .bInterfaceClass
= 0xff,
138 .bInterfaceSubClass
= 0xff,
139 .bInterfaceProtocol
= 0xff,
140 .eps
= (USBDescEndpoint
[]) {
142 .bEndpointAddress
= USB_DIR_IN
| 0x01,
143 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
144 .wMaxPacketSize
= 64,
146 .bEndpointAddress
= USB_DIR_OUT
| 0x02,
147 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
148 .wMaxPacketSize
= 64,
153 static const USBDescDevice desc_device
= {
155 .bMaxPacketSize0
= 8,
156 .bNumConfigurations
= 1,
157 .confs
= (USBDescConfig
[]) {
160 .bConfigurationValue
= 1,
161 .bmAttributes
= USB_CFG_ATT_ONE
| USB_CFG_ATT_WAKEUP
,
169 static const USBDesc desc_serial
= {
174 .iManufacturer
= STR_MANUFACTURER
,
175 .iProduct
= STR_PRODUCT_SERIAL
,
176 .iSerialNumber
= STR_SERIALNUMBER
,
178 .full
= &desc_device
,
182 static const USBDesc desc_braille
= {
187 .iManufacturer
= STR_MANUFACTURER
,
188 .iProduct
= STR_PRODUCT_BRAILLE
,
189 .iSerialNumber
= STR_SERIALNUMBER
,
191 .full
= &desc_device
,
195 static void usb_serial_reset(USBSerialState
*s
)
197 /* TODO: Set flow control to none */
199 s
->event_trigger
= 0;
202 /* TODO: purge in char driver */
205 static void usb_serial_handle_reset(USBDevice
*dev
)
207 USBSerialState
*s
= (USBSerialState
*)dev
;
212 /* TODO: Reset char device, send BREAK? */
215 static uint8_t usb_get_modem_lines(USBSerialState
*s
)
220 if (qemu_chr_fe_ioctl(&s
->cs
,
221 CHR_IOCTL_SERIAL_GET_TIOCM
, &flags
) == -ENOTSUP
) {
222 return FTDI_CTS
| FTDI_DSR
| FTDI_RLSD
;
226 if (flags
& CHR_TIOCM_CTS
) {
229 if (flags
& CHR_TIOCM_DSR
) {
232 if (flags
& CHR_TIOCM_RI
) {
235 if (flags
& CHR_TIOCM_CAR
) {
242 static void usb_serial_handle_control(USBDevice
*dev
, USBPacket
*p
,
243 int request
, int value
, int index
,
244 int length
, uint8_t *data
)
246 USBSerialState
*s
= (USBSerialState
*)dev
;
249 DPRINTF("got control %x, value %x\n", request
, value
);
250 ret
= usb_desc_handle_control(dev
, p
, request
, value
, index
, length
, data
);
256 case EndpointOutRequest
| USB_REQ_CLEAR_FEATURE
:
259 /* Class specific requests. */
260 case DeviceOutVendor
| FTDI_RESET
:
268 /* TODO: purge from char device */
271 /* TODO: purge from char device */
275 case DeviceOutVendor
| FTDI_SET_MDM_CTRL
:
278 qemu_chr_fe_ioctl(&s
->cs
, CHR_IOCTL_SERIAL_GET_TIOCM
, &flags
);
279 if (value
& FTDI_SET_RTS
) {
280 if (value
& FTDI_RTS
) {
281 flags
|= CHR_TIOCM_RTS
;
283 flags
&= ~CHR_TIOCM_RTS
;
286 if (value
& FTDI_SET_DTR
) {
287 if (value
& FTDI_DTR
) {
288 flags
|= CHR_TIOCM_DTR
;
290 flags
&= ~CHR_TIOCM_DTR
;
293 qemu_chr_fe_ioctl(&s
->cs
, CHR_IOCTL_SERIAL_SET_TIOCM
, &flags
);
296 case DeviceOutVendor
| FTDI_SET_FLOW_CTRL
:
299 case DeviceOutVendor
| FTDI_SET_BAUD
: {
300 static const int subdivisors8
[8] = { 0, 4, 2, 1, 3, 5, 6, 7 };
301 int subdivisor8
= subdivisors8
[((value
& 0xc000) >> 14)
302 | ((index
& 1) << 2)];
303 int divisor
= value
& 0x3fff;
305 /* chip special cases */
306 if (divisor
== 1 && subdivisor8
== 0) {
309 if (divisor
== 0 && subdivisor8
== 0) {
313 s
->params
.speed
= (48000000 / 2) / (8 * divisor
+ subdivisor8
);
314 qemu_chr_fe_ioctl(&s
->cs
, CHR_IOCTL_SERIAL_SET_PARAMS
, &s
->params
);
317 case DeviceOutVendor
| FTDI_SET_DATA
:
318 switch (value
& FTDI_PARITY
) {
320 s
->params
.parity
= 'N';
323 s
->params
.parity
= 'O';
326 s
->params
.parity
= 'E';
329 DPRINTF("unsupported parity %d\n", value
& FTDI_PARITY
);
333 switch (value
& FTDI_STOP
) {
335 s
->params
.stop_bits
= 1;
338 s
->params
.stop_bits
= 2;
341 DPRINTF("unsupported stop bits %d\n", value
& FTDI_STOP
);
345 qemu_chr_fe_ioctl(&s
->cs
, CHR_IOCTL_SERIAL_SET_PARAMS
, &s
->params
);
346 /* TODO: TX ON/OFF */
348 case DeviceInVendor
| FTDI_GET_MDM_ST
:
349 data
[0] = usb_get_modem_lines(s
) | 1;
350 data
[1] = FTDI_THRE
| FTDI_TEMT
;
351 p
->actual_length
= 2;
353 case DeviceOutVendor
| FTDI_SET_EVENT_CHR
:
354 /* TODO: handle it */
355 s
->event_chr
= value
;
357 case DeviceOutVendor
| FTDI_SET_ERROR_CHR
:
358 /* TODO: handle it */
359 s
->error_chr
= value
;
361 case DeviceOutVendor
| FTDI_SET_LATENCY
:
364 case DeviceInVendor
| FTDI_GET_LATENCY
:
365 data
[0] = s
->latency
;
366 p
->actual_length
= 1;
370 DPRINTF("got unsupported/bogus control %x, value %x\n", request
, value
);
371 p
->status
= USB_RET_STALL
;
376 static void usb_serial_token_in(USBSerialState
*s
, USBPacket
*p
)
378 const int max_packet_size
= desc_iface0
.eps
[0].wMaxPacketSize
;
382 packet_len
= p
->iov
.size
;
383 if (packet_len
<= 2) {
384 p
->status
= USB_RET_NAK
;
388 header
[0] = usb_get_modem_lines(s
) | 1;
389 /* We do not have the uart details */
390 /* handle serial break */
391 if (s
->event_trigger
&& s
->event_trigger
& FTDI_BI
) {
392 s
->event_trigger
&= ~FTDI_BI
;
394 usb_packet_copy(p
, header
, 2);
401 p
->status
= USB_RET_NAK
;
405 while (s
->recv_used
&& packet_len
> 2) {
408 len
= MIN(packet_len
, max_packet_size
);
410 if (len
> s
->recv_used
) {
414 first_len
= RECV_BUF
- s
->recv_ptr
;
415 if (first_len
> len
) {
418 usb_packet_copy(p
, header
, 2);
419 usb_packet_copy(p
, s
->recv_buf
+ s
->recv_ptr
, first_len
);
420 if (len
> first_len
) {
421 usb_packet_copy(p
, s
->recv_buf
, len
- first_len
);
424 s
->recv_ptr
= (s
->recv_ptr
+ len
) % RECV_BUF
;
425 packet_len
-= len
+ 2;
431 static void usb_serial_handle_data(USBDevice
*dev
, USBPacket
*p
)
433 USBSerialState
*s
= (USBSerialState
*)dev
;
434 uint8_t devep
= p
->ep
->nr
;
443 for (i
= 0; i
< p
->iov
.niov
; i
++) {
444 iov
= p
->iov
.iov
+ i
;
446 * XXX this blocks entire thread. Rewrite to use
447 * qemu_chr_fe_write and background I/O callbacks
449 qemu_chr_fe_write_all(&s
->cs
, iov
->iov_base
, iov
->iov_len
);
451 p
->actual_length
= p
->iov
.size
;
458 usb_serial_token_in(s
, p
);
462 DPRINTF("Bad token\n");
464 p
->status
= USB_RET_STALL
;
469 static int usb_serial_can_read(void *opaque
)
471 USBSerialState
*s
= opaque
;
473 if (!s
->dev
.attached
) {
476 return RECV_BUF
- s
->recv_used
;
479 static void usb_serial_read(void *opaque
, const uint8_t *buf
, int size
)
481 USBSerialState
*s
= opaque
;
482 int first_size
, start
;
484 /* room in the buffer? */
485 if (size
> (RECV_BUF
- s
->recv_used
)) {
486 size
= RECV_BUF
- s
->recv_used
;
489 start
= s
->recv_ptr
+ s
->recv_used
;
490 if (start
< RECV_BUF
) {
491 /* copy data to end of buffer */
492 first_size
= RECV_BUF
- start
;
493 if (first_size
> size
) {
497 memcpy(s
->recv_buf
+ start
, buf
, first_size
);
499 /* wrap around to front if needed */
500 if (size
> first_size
) {
501 memcpy(s
->recv_buf
, buf
+ first_size
, size
- first_size
);
505 memcpy(s
->recv_buf
+ start
, buf
, size
);
507 s
->recv_used
+= size
;
509 usb_wakeup(s
->intr
, 0);
512 static void usb_serial_event(void *opaque
, QEMUChrEvent event
)
514 USBSerialState
*s
= opaque
;
517 case CHR_EVENT_BREAK
:
518 s
->event_trigger
|= FTDI_BI
;
520 case CHR_EVENT_OPENED
:
521 if (!s
->dev
.attached
) {
522 usb_device_attach(&s
->dev
, &error_abort
);
525 case CHR_EVENT_CLOSED
:
526 if (s
->dev
.attached
) {
527 usb_device_detach(&s
->dev
);
530 case CHR_EVENT_MUX_IN
:
531 case CHR_EVENT_MUX_OUT
:
537 static void usb_serial_realize(USBDevice
*dev
, Error
**errp
)
539 USBSerialState
*s
= USB_SERIAL(dev
);
540 Error
*local_err
= NULL
;
542 usb_desc_create_serial(dev
);
544 dev
->auto_attach
= 0;
546 if (!qemu_chr_fe_backend_connected(&s
->cs
)) {
547 error_setg(errp
, "Property chardev is required");
551 usb_check_attach(dev
, &local_err
);
553 error_propagate(errp
, local_err
);
557 qemu_chr_fe_set_handlers(&s
->cs
, usb_serial_can_read
, usb_serial_read
,
558 usb_serial_event
, NULL
, s
, NULL
, true);
559 usb_serial_handle_reset(dev
);
561 if (qemu_chr_fe_backend_open(&s
->cs
) && !dev
->attached
) {
562 usb_device_attach(dev
, &error_abort
);
564 s
->intr
= usb_ep_get(dev
, USB_TOKEN_IN
, 1);
567 static USBDevice
*usb_braille_init(const char *unused
)
572 cdrv
= qemu_chr_new("braille", "braille", NULL
);
577 dev
= usb_new("usb-braille");
578 qdev_prop_set_chr(&dev
->qdev
, "chardev", cdrv
);
582 static const VMStateDescription vmstate_usb_serial
= {
583 .name
= "usb-serial",
587 static Property serial_properties
[] = {
588 DEFINE_PROP_CHR("chardev", USBSerialState
, cs
),
589 DEFINE_PROP_END_OF_LIST(),
592 static void usb_serial_dev_class_init(ObjectClass
*klass
, void *data
)
594 DeviceClass
*dc
= DEVICE_CLASS(klass
);
595 USBDeviceClass
*uc
= USB_DEVICE_CLASS(klass
);
597 uc
->realize
= usb_serial_realize
;
598 uc
->handle_reset
= usb_serial_handle_reset
;
599 uc
->handle_control
= usb_serial_handle_control
;
600 uc
->handle_data
= usb_serial_handle_data
;
601 dc
->vmsd
= &vmstate_usb_serial
;
602 set_bit(DEVICE_CATEGORY_INPUT
, dc
->categories
);
605 static const TypeInfo usb_serial_dev_type_info
= {
606 .name
= TYPE_USB_SERIAL
,
607 .parent
= TYPE_USB_DEVICE
,
608 .instance_size
= sizeof(USBSerialState
),
610 .class_init
= usb_serial_dev_class_init
,
613 static void usb_serial_class_initfn(ObjectClass
*klass
, void *data
)
615 DeviceClass
*dc
= DEVICE_CLASS(klass
);
616 USBDeviceClass
*uc
= USB_DEVICE_CLASS(klass
);
618 uc
->product_desc
= "QEMU USB Serial";
619 uc
->usb_desc
= &desc_serial
;
620 device_class_set_props(dc
, serial_properties
);
623 static const TypeInfo serial_info
= {
624 .name
= "usb-serial",
625 .parent
= TYPE_USB_SERIAL
,
626 .class_init
= usb_serial_class_initfn
,
629 static Property braille_properties
[] = {
630 DEFINE_PROP_CHR("chardev", USBSerialState
, cs
),
631 DEFINE_PROP_END_OF_LIST(),
634 static void usb_braille_class_initfn(ObjectClass
*klass
, void *data
)
636 DeviceClass
*dc
= DEVICE_CLASS(klass
);
637 USBDeviceClass
*uc
= USB_DEVICE_CLASS(klass
);
639 uc
->product_desc
= "QEMU USB Braille";
640 uc
->usb_desc
= &desc_braille
;
641 device_class_set_props(dc
, braille_properties
);
644 static const TypeInfo braille_info
= {
645 .name
= "usb-braille",
646 .parent
= TYPE_USB_SERIAL
,
647 .class_init
= usb_braille_class_initfn
,
650 static void usb_serial_register_types(void)
652 type_register_static(&usb_serial_dev_type_info
);
653 type_register_static(&serial_info
);
654 type_register_static(&braille_info
);
655 usb_legacy_register("usb-braille", "braille", usb_braille_init
);
658 type_init(usb_serial_register_types
)