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 ((USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_DEVICE)<<8)
48 #define DeviceInVendor ((USB_DIR_IN |USB_TYPE_VENDOR|USB_RECIP_DEVICE)<<8)
52 #define FTDI_RESET_SIO 0
53 #define FTDI_RESET_RX 1
54 #define FTDI_RESET_TX 2
59 #define FTDI_SET_DTR (FTDI_DTR << 8)
61 #define FTDI_SET_RTS (FTDI_RTS << 8)
65 #define FTDI_RTS_CTS_HS 1
66 #define FTDI_DTR_DSR_HS 2
67 #define FTDI_XON_XOFF_HS 4
71 #define FTDI_PARITY (0x7 << 8)
72 #define FTDI_ODD (0x1 << 8)
73 #define FTDI_EVEN (0x2 << 8)
74 #define FTDI_MARK (0x3 << 8)
75 #define FTDI_SPACE (0x4 << 8)
77 #define FTDI_STOP (0x3 << 11)
78 #define FTDI_STOP1 (0x0 << 11)
79 #define FTDI_STOP15 (0x1 << 11)
80 #define FTDI_STOP2 (0x2 << 11)
83 /* TODO: should be sent every 40ms */
84 #define FTDI_CTS (1<<4) // CTS line status
85 #define FTDI_DSR (1<<5) // DSR line status
86 #define FTDI_RI (1<<6) // RI line status
87 #define FTDI_RLSD (1<<7) // Receive Line Signal Detect
91 #define FTDI_DR (1<<0) // Data Ready
92 #define FTDI_OE (1<<1) // Overrun Err
93 #define FTDI_PE (1<<2) // Parity Err
94 #define FTDI_FE (1<<3) // Framing Err
95 #define FTDI_BI (1<<4) // Break Interrupt
96 #define FTDI_THRE (1<<5) // Transmitter Holding Register
97 #define FTDI_TEMT (1<<6) // Transmitter Empty
98 #define FTDI_FIFO (1<<7) // Error in FIFO
100 struct USBSerialState
{
103 uint8_t recv_buf
[RECV_BUF
];
108 uint8_t event_trigger
;
109 QEMUSerialSetParams params
;
110 int latency
; /* ms */
114 #define TYPE_USB_SERIAL "usb-serial-dev"
115 OBJECT_DECLARE_SIMPLE_TYPE(USBSerialState
, USB_SERIAL
)
118 STR_MANUFACTURER
= 1,
124 static const USBDescStrings desc_strings
= {
125 [STR_MANUFACTURER
] = "QEMU",
126 [STR_PRODUCT_SERIAL
] = "QEMU USB SERIAL",
127 [STR_PRODUCT_BRAILLE
] = "QEMU USB BAUM BRAILLE",
128 [STR_SERIALNUMBER
] = "1",
131 static const USBDescIface desc_iface0
= {
132 .bInterfaceNumber
= 0,
134 .bInterfaceClass
= 0xff,
135 .bInterfaceSubClass
= 0xff,
136 .bInterfaceProtocol
= 0xff,
137 .eps
= (USBDescEndpoint
[]) {
139 .bEndpointAddress
= USB_DIR_IN
| 0x01,
140 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
141 .wMaxPacketSize
= 64,
143 .bEndpointAddress
= USB_DIR_OUT
| 0x02,
144 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
145 .wMaxPacketSize
= 64,
150 static const USBDescDevice desc_device
= {
152 .bMaxPacketSize0
= 8,
153 .bNumConfigurations
= 1,
154 .confs
= (USBDescConfig
[]) {
157 .bConfigurationValue
= 1,
158 .bmAttributes
= USB_CFG_ATT_ONE
| USB_CFG_ATT_WAKEUP
,
166 static const USBDesc desc_serial
= {
171 .iManufacturer
= STR_MANUFACTURER
,
172 .iProduct
= STR_PRODUCT_SERIAL
,
173 .iSerialNumber
= STR_SERIALNUMBER
,
175 .full
= &desc_device
,
179 static const USBDesc desc_braille
= {
184 .iManufacturer
= STR_MANUFACTURER
,
185 .iProduct
= STR_PRODUCT_BRAILLE
,
186 .iSerialNumber
= STR_SERIALNUMBER
,
188 .full
= &desc_device
,
192 static void usb_serial_reset(USBSerialState
*s
)
194 /* TODO: Set flow control to none */
196 s
->event_trigger
= 0;
199 /* TODO: purge in char driver */
202 static void usb_serial_handle_reset(USBDevice
*dev
)
204 USBSerialState
*s
= (USBSerialState
*)dev
;
209 /* TODO: Reset char device, send BREAK? */
212 static uint8_t usb_get_modem_lines(USBSerialState
*s
)
217 if (qemu_chr_fe_ioctl(&s
->cs
,
218 CHR_IOCTL_SERIAL_GET_TIOCM
, &flags
) == -ENOTSUP
) {
219 return FTDI_CTS
|FTDI_DSR
|FTDI_RLSD
;
223 if (flags
& CHR_TIOCM_CTS
)
225 if (flags
& CHR_TIOCM_DSR
)
227 if (flags
& CHR_TIOCM_RI
)
229 if (flags
& CHR_TIOCM_CAR
)
235 static void usb_serial_handle_control(USBDevice
*dev
, USBPacket
*p
,
236 int request
, int value
, int index
, int length
, uint8_t *data
)
238 USBSerialState
*s
= (USBSerialState
*)dev
;
241 DPRINTF("got control %x, value %x\n",request
, value
);
242 ret
= usb_desc_handle_control(dev
, p
, request
, value
, index
, length
, data
);
248 case EndpointOutRequest
| USB_REQ_CLEAR_FEATURE
:
251 /* Class specific requests. */
252 case DeviceOutVendor
| FTDI_RESET
:
260 /* TODO: purge from char device */
263 /* TODO: purge from char device */
267 case DeviceOutVendor
| FTDI_SET_MDM_CTRL
:
270 qemu_chr_fe_ioctl(&s
->cs
, CHR_IOCTL_SERIAL_GET_TIOCM
, &flags
);
271 if (value
& FTDI_SET_RTS
) {
272 if (value
& FTDI_RTS
)
273 flags
|= CHR_TIOCM_RTS
;
275 flags
&= ~CHR_TIOCM_RTS
;
277 if (value
& FTDI_SET_DTR
) {
278 if (value
& FTDI_DTR
)
279 flags
|= CHR_TIOCM_DTR
;
281 flags
&= ~CHR_TIOCM_DTR
;
283 qemu_chr_fe_ioctl(&s
->cs
, CHR_IOCTL_SERIAL_SET_TIOCM
, &flags
);
286 case DeviceOutVendor
| FTDI_SET_FLOW_CTRL
:
289 case DeviceOutVendor
| FTDI_SET_BAUD
: {
290 static const int subdivisors8
[8] = { 0, 4, 2, 1, 3, 5, 6, 7 };
291 int subdivisor8
= subdivisors8
[((value
& 0xc000) >> 14)
292 | ((index
& 1) << 2)];
293 int divisor
= value
& 0x3fff;
295 /* chip special cases */
296 if (divisor
== 1 && subdivisor8
== 0)
298 if (divisor
== 0 && subdivisor8
== 0)
301 s
->params
.speed
= (48000000 / 2) / (8 * divisor
+ subdivisor8
);
302 qemu_chr_fe_ioctl(&s
->cs
, CHR_IOCTL_SERIAL_SET_PARAMS
, &s
->params
);
305 case DeviceOutVendor
| FTDI_SET_DATA
:
306 switch (value
& FTDI_PARITY
) {
308 s
->params
.parity
= 'N';
311 s
->params
.parity
= 'O';
314 s
->params
.parity
= 'E';
317 DPRINTF("unsupported parity %d\n", value
& FTDI_PARITY
);
320 switch (value
& FTDI_STOP
) {
322 s
->params
.stop_bits
= 1;
325 s
->params
.stop_bits
= 2;
328 DPRINTF("unsupported stop bits %d\n", value
& FTDI_STOP
);
331 qemu_chr_fe_ioctl(&s
->cs
, CHR_IOCTL_SERIAL_SET_PARAMS
, &s
->params
);
332 /* TODO: TX ON/OFF */
334 case DeviceInVendor
| FTDI_GET_MDM_ST
:
335 data
[0] = usb_get_modem_lines(s
) | 1;
336 data
[1] = FTDI_THRE
| FTDI_TEMT
;
337 p
->actual_length
= 2;
339 case DeviceOutVendor
| FTDI_SET_EVENT_CHR
:
340 /* TODO: handle it */
341 s
->event_chr
= value
;
343 case DeviceOutVendor
| FTDI_SET_ERROR_CHR
:
344 /* TODO: handle it */
345 s
->error_chr
= value
;
347 case DeviceOutVendor
| FTDI_SET_LATENCY
:
350 case DeviceInVendor
| FTDI_GET_LATENCY
:
351 data
[0] = s
->latency
;
352 p
->actual_length
= 1;
356 DPRINTF("got unsupported/bogus control %x, value %x\n", request
, value
);
357 p
->status
= USB_RET_STALL
;
362 static void usb_serial_token_in(USBSerialState
*s
, USBPacket
*p
)
364 const int max_packet_size
= desc_iface0
.eps
[0].wMaxPacketSize
;
368 packet_len
= p
->iov
.size
;
369 if (packet_len
<= 2) {
370 p
->status
= USB_RET_NAK
;
374 header
[0] = usb_get_modem_lines(s
) | 1;
375 /* We do not have the uart details */
376 /* handle serial break */
377 if (s
->event_trigger
&& s
->event_trigger
& FTDI_BI
) {
378 s
->event_trigger
&= ~FTDI_BI
;
380 usb_packet_copy(p
, header
, 2);
387 p
->status
= USB_RET_NAK
;
391 while (s
->recv_used
&& packet_len
> 2) {
394 len
= MIN(packet_len
, max_packet_size
);
396 if (len
> s
->recv_used
) {
400 first_len
= RECV_BUF
- s
->recv_ptr
;
401 if (first_len
> len
) {
404 usb_packet_copy(p
, header
, 2);
405 usb_packet_copy(p
, s
->recv_buf
+ s
->recv_ptr
, first_len
);
406 if (len
> first_len
) {
407 usb_packet_copy(p
, s
->recv_buf
, len
- first_len
);
410 s
->recv_ptr
= (s
->recv_ptr
+ len
) % RECV_BUF
;
411 packet_len
-= len
+ 2;
417 static void usb_serial_handle_data(USBDevice
*dev
, USBPacket
*p
)
419 USBSerialState
*s
= (USBSerialState
*)dev
;
420 uint8_t devep
= p
->ep
->nr
;
428 for (i
= 0; i
< p
->iov
.niov
; i
++) {
429 iov
= p
->iov
.iov
+ i
;
430 /* XXX this blocks entire thread. Rewrite to use
431 * qemu_chr_fe_write and background I/O callbacks */
432 qemu_chr_fe_write_all(&s
->cs
, iov
->iov_base
, iov
->iov_len
);
434 p
->actual_length
= p
->iov
.size
;
440 usb_serial_token_in(s
, p
);
444 DPRINTF("Bad token\n");
446 p
->status
= USB_RET_STALL
;
451 static int usb_serial_can_read(void *opaque
)
453 USBSerialState
*s
= opaque
;
455 if (!s
->dev
.attached
) {
458 return RECV_BUF
- s
->recv_used
;
461 static void usb_serial_read(void *opaque
, const uint8_t *buf
, int size
)
463 USBSerialState
*s
= opaque
;
464 int first_size
, start
;
466 /* room in the buffer? */
467 if (size
> (RECV_BUF
- s
->recv_used
))
468 size
= RECV_BUF
- s
->recv_used
;
470 start
= s
->recv_ptr
+ s
->recv_used
;
471 if (start
< RECV_BUF
) {
472 /* copy data to end of buffer */
473 first_size
= RECV_BUF
- start
;
474 if (first_size
> size
)
477 memcpy(s
->recv_buf
+ start
, buf
, first_size
);
479 /* wrap around to front if needed */
480 if (size
> first_size
)
481 memcpy(s
->recv_buf
, buf
+ first_size
, size
- first_size
);
484 memcpy(s
->recv_buf
+ start
, buf
, size
);
486 s
->recv_used
+= size
;
488 usb_wakeup(s
->intr
, 0);
491 static void usb_serial_event(void *opaque
, QEMUChrEvent event
)
493 USBSerialState
*s
= opaque
;
496 case CHR_EVENT_BREAK
:
497 s
->event_trigger
|= FTDI_BI
;
499 case CHR_EVENT_OPENED
:
500 if (!s
->dev
.attached
) {
501 usb_device_attach(&s
->dev
, &error_abort
);
504 case CHR_EVENT_CLOSED
:
505 if (s
->dev
.attached
) {
506 usb_device_detach(&s
->dev
);
509 case CHR_EVENT_MUX_IN
:
510 case CHR_EVENT_MUX_OUT
:
516 static void usb_serial_realize(USBDevice
*dev
, Error
**errp
)
518 USBSerialState
*s
= USB_SERIAL(dev
);
519 Error
*local_err
= NULL
;
521 usb_desc_create_serial(dev
);
523 dev
->auto_attach
= 0;
525 if (!qemu_chr_fe_backend_connected(&s
->cs
)) {
526 error_setg(errp
, "Property chardev is required");
530 usb_check_attach(dev
, &local_err
);
532 error_propagate(errp
, local_err
);
536 qemu_chr_fe_set_handlers(&s
->cs
, usb_serial_can_read
, usb_serial_read
,
537 usb_serial_event
, NULL
, s
, NULL
, true);
538 usb_serial_handle_reset(dev
);
540 if (qemu_chr_fe_backend_open(&s
->cs
) && !dev
->attached
) {
541 usb_device_attach(dev
, &error_abort
);
543 s
->intr
= usb_ep_get(dev
, USB_TOKEN_IN
, 1);
546 static USBDevice
*usb_braille_init(const char *unused
)
551 cdrv
= qemu_chr_new("braille", "braille", NULL
);
555 dev
= usb_new("usb-braille");
556 qdev_prop_set_chr(&dev
->qdev
, "chardev", cdrv
);
560 static const VMStateDescription vmstate_usb_serial
= {
561 .name
= "usb-serial",
565 static Property serial_properties
[] = {
566 DEFINE_PROP_CHR("chardev", USBSerialState
, cs
),
567 DEFINE_PROP_END_OF_LIST(),
570 static void usb_serial_dev_class_init(ObjectClass
*klass
, void *data
)
572 DeviceClass
*dc
= DEVICE_CLASS(klass
);
573 USBDeviceClass
*uc
= USB_DEVICE_CLASS(klass
);
575 uc
->realize
= usb_serial_realize
;
576 uc
->handle_reset
= usb_serial_handle_reset
;
577 uc
->handle_control
= usb_serial_handle_control
;
578 uc
->handle_data
= usb_serial_handle_data
;
579 dc
->vmsd
= &vmstate_usb_serial
;
580 set_bit(DEVICE_CATEGORY_INPUT
, dc
->categories
);
583 static const TypeInfo usb_serial_dev_type_info
= {
584 .name
= TYPE_USB_SERIAL
,
585 .parent
= TYPE_USB_DEVICE
,
586 .instance_size
= sizeof(USBSerialState
),
588 .class_init
= usb_serial_dev_class_init
,
591 static void usb_serial_class_initfn(ObjectClass
*klass
, void *data
)
593 DeviceClass
*dc
= DEVICE_CLASS(klass
);
594 USBDeviceClass
*uc
= USB_DEVICE_CLASS(klass
);
596 uc
->product_desc
= "QEMU USB Serial";
597 uc
->usb_desc
= &desc_serial
;
598 device_class_set_props(dc
, serial_properties
);
601 static const TypeInfo serial_info
= {
602 .name
= "usb-serial",
603 .parent
= TYPE_USB_SERIAL
,
604 .class_init
= usb_serial_class_initfn
,
607 static Property braille_properties
[] = {
608 DEFINE_PROP_CHR("chardev", USBSerialState
, cs
),
609 DEFINE_PROP_END_OF_LIST(),
612 static void usb_braille_class_initfn(ObjectClass
*klass
, void *data
)
614 DeviceClass
*dc
= DEVICE_CLASS(klass
);
615 USBDeviceClass
*uc
= USB_DEVICE_CLASS(klass
);
617 uc
->product_desc
= "QEMU USB Braille";
618 uc
->usb_desc
= &desc_braille
;
619 device_class_set_props(dc
, braille_properties
);
622 static const TypeInfo braille_info
= {
623 .name
= "usb-braille",
624 .parent
= TYPE_USB_SERIAL
,
625 .class_init
= usb_braille_class_initfn
,
628 static void usb_serial_register_types(void)
630 type_register_static(&usb_serial_dev_type_info
);
631 type_register_static(&serial_info
);
632 type_register_static(&braille_info
);
633 usb_legacy_register("usb-braille", "braille", usb_braille_init
);
636 type_init(usb_serial_register_types
)