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"
23 //#define DEBUG_Serial
26 #define DPRINTF(fmt, ...) \
27 do { printf("usb-serial: " fmt , ## __VA_ARGS__); } while (0)
29 #define DPRINTF(fmt, ...) do {} while(0)
36 #define FTDI_SET_MDM_CTRL 1
37 #define FTDI_SET_FLOW_CTRL 2
38 #define FTDI_SET_BAUD 3
39 #define FTDI_SET_DATA 4
40 #define FTDI_GET_MDM_ST 5
41 #define FTDI_SET_EVENT_CHR 6
42 #define FTDI_SET_ERROR_CHR 7
43 #define FTDI_SET_LATENCY 9
44 #define FTDI_GET_LATENCY 10
46 #define DeviceOutVendor ((USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_DEVICE)<<8)
47 #define DeviceInVendor ((USB_DIR_IN |USB_TYPE_VENDOR|USB_RECIP_DEVICE)<<8)
51 #define FTDI_RESET_SIO 0
52 #define FTDI_RESET_RX 1
53 #define FTDI_RESET_TX 2
58 #define FTDI_SET_DTR (FTDI_DTR << 8)
60 #define FTDI_SET_RTS (FTDI_RTS << 8)
64 #define FTDI_RTS_CTS_HS 1
65 #define FTDI_DTR_DSR_HS 2
66 #define FTDI_XON_XOFF_HS 4
70 #define FTDI_PARITY (0x7 << 8)
71 #define FTDI_ODD (0x1 << 8)
72 #define FTDI_EVEN (0x2 << 8)
73 #define FTDI_MARK (0x3 << 8)
74 #define FTDI_SPACE (0x4 << 8)
76 #define FTDI_STOP (0x3 << 11)
77 #define FTDI_STOP1 (0x0 << 11)
78 #define FTDI_STOP15 (0x1 << 11)
79 #define FTDI_STOP2 (0x2 << 11)
82 /* TODO: should be sent every 40ms */
83 #define FTDI_CTS (1<<4) // CTS line status
84 #define FTDI_DSR (1<<5) // DSR line status
85 #define FTDI_RI (1<<6) // RI line status
86 #define FTDI_RLSD (1<<7) // Receive Line Signal Detect
90 #define FTDI_DR (1<<0) // Data Ready
91 #define FTDI_OE (1<<1) // Overrun Err
92 #define FTDI_PE (1<<2) // Parity Err
93 #define FTDI_FE (1<<3) // Framing Err
94 #define FTDI_BI (1<<4) // Break Interrupt
95 #define FTDI_THRE (1<<5) // Transmitter Holding Register
96 #define FTDI_TEMT (1<<6) // Transmitter Empty
97 #define FTDI_FIFO (1<<7) // Error in FIFO
101 uint8_t recv_buf
[RECV_BUF
];
106 uint8_t event_trigger
;
107 QEMUSerialSetParams params
;
108 int latency
; /* ms */
112 #define TYPE_USB_SERIAL "usb-serial-dev"
113 #define USB_SERIAL_DEV(obj) OBJECT_CHECK(USBSerialState, (obj), TYPE_USB_SERIAL)
116 STR_MANUFACTURER
= 1,
122 static const USBDescStrings desc_strings
= {
123 [STR_MANUFACTURER
] = "QEMU",
124 [STR_PRODUCT_SERIAL
] = "QEMU USB SERIAL",
125 [STR_PRODUCT_BRAILLE
] = "QEMU USB BAUM BRAILLE",
126 [STR_SERIALNUMBER
] = "1",
129 static const USBDescIface desc_iface0
= {
130 .bInterfaceNumber
= 0,
132 .bInterfaceClass
= 0xff,
133 .bInterfaceSubClass
= 0xff,
134 .bInterfaceProtocol
= 0xff,
135 .eps
= (USBDescEndpoint
[]) {
137 .bEndpointAddress
= USB_DIR_IN
| 0x01,
138 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
139 .wMaxPacketSize
= 64,
141 .bEndpointAddress
= USB_DIR_OUT
| 0x02,
142 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
143 .wMaxPacketSize
= 64,
148 static const USBDescDevice desc_device
= {
150 .bMaxPacketSize0
= 8,
151 .bNumConfigurations
= 1,
152 .confs
= (USBDescConfig
[]) {
155 .bConfigurationValue
= 1,
156 .bmAttributes
= USB_CFG_ATT_ONE
,
164 static const USBDesc desc_serial
= {
169 .iManufacturer
= STR_MANUFACTURER
,
170 .iProduct
= STR_PRODUCT_SERIAL
,
171 .iSerialNumber
= STR_SERIALNUMBER
,
173 .full
= &desc_device
,
177 static const USBDesc desc_braille
= {
182 .iManufacturer
= STR_MANUFACTURER
,
183 .iProduct
= STR_PRODUCT_BRAILLE
,
184 .iSerialNumber
= STR_SERIALNUMBER
,
186 .full
= &desc_device
,
190 static void usb_serial_reset(USBSerialState
*s
)
192 /* TODO: Set flow control to none */
194 s
->event_trigger
= 0;
197 /* TODO: purge in char driver */
200 static void usb_serial_handle_reset(USBDevice
*dev
)
202 USBSerialState
*s
= (USBSerialState
*)dev
;
207 /* TODO: Reset char device, send BREAK? */
210 static uint8_t usb_get_modem_lines(USBSerialState
*s
)
215 if (qemu_chr_fe_ioctl(&s
->cs
,
216 CHR_IOCTL_SERIAL_GET_TIOCM
, &flags
) == -ENOTSUP
) {
217 return FTDI_CTS
|FTDI_DSR
|FTDI_RLSD
;
221 if (flags
& CHR_TIOCM_CTS
)
223 if (flags
& CHR_TIOCM_DSR
)
225 if (flags
& CHR_TIOCM_RI
)
227 if (flags
& CHR_TIOCM_CAR
)
233 static void usb_serial_handle_control(USBDevice
*dev
, USBPacket
*p
,
234 int request
, int value
, int index
, int length
, uint8_t *data
)
236 USBSerialState
*s
= (USBSerialState
*)dev
;
239 DPRINTF("got control %x, value %x\n",request
, value
);
240 ret
= usb_desc_handle_control(dev
, p
, request
, value
, index
, length
, data
);
246 case EndpointOutRequest
| USB_REQ_CLEAR_FEATURE
:
249 /* Class specific requests. */
250 case DeviceOutVendor
| FTDI_RESET
:
258 /* TODO: purge from char device */
261 /* TODO: purge from char device */
265 case DeviceOutVendor
| FTDI_SET_MDM_CTRL
:
268 qemu_chr_fe_ioctl(&s
->cs
, CHR_IOCTL_SERIAL_GET_TIOCM
, &flags
);
269 if (value
& FTDI_SET_RTS
) {
270 if (value
& FTDI_RTS
)
271 flags
|= CHR_TIOCM_RTS
;
273 flags
&= ~CHR_TIOCM_RTS
;
275 if (value
& FTDI_SET_DTR
) {
276 if (value
& FTDI_DTR
)
277 flags
|= CHR_TIOCM_DTR
;
279 flags
&= ~CHR_TIOCM_DTR
;
281 qemu_chr_fe_ioctl(&s
->cs
, CHR_IOCTL_SERIAL_SET_TIOCM
, &flags
);
284 case DeviceOutVendor
| FTDI_SET_FLOW_CTRL
:
287 case DeviceOutVendor
| FTDI_SET_BAUD
: {
288 static const int subdivisors8
[8] = { 0, 4, 2, 1, 3, 5, 6, 7 };
289 int subdivisor8
= subdivisors8
[((value
& 0xc000) >> 14)
290 | ((index
& 1) << 2)];
291 int divisor
= value
& 0x3fff;
293 /* chip special cases */
294 if (divisor
== 1 && subdivisor8
== 0)
296 if (divisor
== 0 && subdivisor8
== 0)
299 s
->params
.speed
= (48000000 / 2) / (8 * divisor
+ subdivisor8
);
300 qemu_chr_fe_ioctl(&s
->cs
, CHR_IOCTL_SERIAL_SET_PARAMS
, &s
->params
);
303 case DeviceOutVendor
| FTDI_SET_DATA
:
304 switch (value
& FTDI_PARITY
) {
306 s
->params
.parity
= 'N';
309 s
->params
.parity
= 'O';
312 s
->params
.parity
= 'E';
315 DPRINTF("unsupported parity %d\n", value
& FTDI_PARITY
);
318 switch (value
& FTDI_STOP
) {
320 s
->params
.stop_bits
= 1;
323 s
->params
.stop_bits
= 2;
326 DPRINTF("unsupported stop bits %d\n", value
& FTDI_STOP
);
329 qemu_chr_fe_ioctl(&s
->cs
, CHR_IOCTL_SERIAL_SET_PARAMS
, &s
->params
);
330 /* TODO: TX ON/OFF */
332 case DeviceInVendor
| FTDI_GET_MDM_ST
:
333 data
[0] = usb_get_modem_lines(s
) | 1;
335 p
->actual_length
= 2;
337 case DeviceOutVendor
| FTDI_SET_EVENT_CHR
:
338 /* TODO: handle it */
339 s
->event_chr
= value
;
341 case DeviceOutVendor
| FTDI_SET_ERROR_CHR
:
342 /* TODO: handle it */
343 s
->error_chr
= value
;
345 case DeviceOutVendor
| FTDI_SET_LATENCY
:
348 case DeviceInVendor
| FTDI_GET_LATENCY
:
349 data
[0] = s
->latency
;
350 p
->actual_length
= 1;
354 DPRINTF("got unsupported/bogus control %x, value %x\n", request
, value
);
355 p
->status
= USB_RET_STALL
;
360 static void usb_serial_handle_data(USBDevice
*dev
, USBPacket
*p
)
362 USBSerialState
*s
= (USBSerialState
*)dev
;
363 uint8_t devep
= p
->ep
->nr
;
366 int i
, first_len
, len
;
372 for (i
= 0; i
< p
->iov
.niov
; i
++) {
373 iov
= p
->iov
.iov
+ i
;
374 /* XXX this blocks entire thread. Rewrite to use
375 * qemu_chr_fe_write and background I/O callbacks */
376 qemu_chr_fe_write_all(&s
->cs
, iov
->iov_base
, iov
->iov_len
);
378 p
->actual_length
= p
->iov
.size
;
384 first_len
= RECV_BUF
- s
->recv_ptr
;
387 p
->status
= USB_RET_NAK
;
390 header
[0] = usb_get_modem_lines(s
) | 1;
391 /* We do not have the uart details */
392 /* handle serial break */
393 if (s
->event_trigger
&& s
->event_trigger
& FTDI_BI
) {
394 s
->event_trigger
&= ~FTDI_BI
;
396 usb_packet_copy(p
, header
, 2);
402 if (len
> s
->recv_used
)
405 p
->status
= USB_RET_NAK
;
410 usb_packet_copy(p
, header
, 2);
411 usb_packet_copy(p
, s
->recv_buf
+ s
->recv_ptr
, first_len
);
413 usb_packet_copy(p
, s
->recv_buf
, len
- first_len
);
415 s
->recv_ptr
= (s
->recv_ptr
+ len
) % RECV_BUF
;
419 DPRINTF("Bad token\n");
421 p
->status
= USB_RET_STALL
;
426 static int usb_serial_can_read(void *opaque
)
428 USBSerialState
*s
= opaque
;
430 if (!s
->dev
.attached
) {
433 return RECV_BUF
- s
->recv_used
;
436 static void usb_serial_read(void *opaque
, const uint8_t *buf
, int size
)
438 USBSerialState
*s
= opaque
;
439 int first_size
, start
;
441 /* room in the buffer? */
442 if (size
> (RECV_BUF
- s
->recv_used
))
443 size
= RECV_BUF
- s
->recv_used
;
445 start
= s
->recv_ptr
+ s
->recv_used
;
446 if (start
< RECV_BUF
) {
447 /* copy data to end of buffer */
448 first_size
= RECV_BUF
- start
;
449 if (first_size
> size
)
452 memcpy(s
->recv_buf
+ start
, buf
, first_size
);
454 /* wrap around to front if needed */
455 if (size
> first_size
)
456 memcpy(s
->recv_buf
, buf
+ first_size
, size
- first_size
);
459 memcpy(s
->recv_buf
+ start
, buf
, size
);
461 s
->recv_used
+= size
;
464 static void usb_serial_event(void *opaque
, int event
)
466 USBSerialState
*s
= opaque
;
469 case CHR_EVENT_BREAK
:
470 s
->event_trigger
|= FTDI_BI
;
472 case CHR_EVENT_OPENED
:
473 if (!s
->dev
.attached
) {
474 usb_device_attach(&s
->dev
, &error_abort
);
477 case CHR_EVENT_CLOSED
:
478 if (s
->dev
.attached
) {
479 usb_device_detach(&s
->dev
);
485 static void usb_serial_realize(USBDevice
*dev
, Error
**errp
)
487 USBSerialState
*s
= USB_SERIAL_DEV(dev
);
488 Error
*local_err
= NULL
;
490 usb_desc_create_serial(dev
);
492 dev
->auto_attach
= 0;
494 if (!qemu_chr_fe_backend_connected(&s
->cs
)) {
495 error_setg(errp
, "Property chardev is required");
499 usb_check_attach(dev
, &local_err
);
501 error_propagate(errp
, local_err
);
505 qemu_chr_fe_set_handlers(&s
->cs
, usb_serial_can_read
, usb_serial_read
,
506 usb_serial_event
, NULL
, s
, NULL
, true);
507 usb_serial_handle_reset(dev
);
509 if (qemu_chr_fe_backend_open(&s
->cs
) && !dev
->attached
) {
510 usb_device_attach(dev
, &error_abort
);
514 static USBDevice
*usb_braille_init(USBBus
*bus
, const char *unused
)
519 cdrv
= qemu_chr_new("braille", "braille", NULL
);
523 dev
= usb_create(bus
, "usb-braille");
524 qdev_prop_set_chr(&dev
->qdev
, "chardev", cdrv
);
528 static const VMStateDescription vmstate_usb_serial
= {
529 .name
= "usb-serial",
533 static Property serial_properties
[] = {
534 DEFINE_PROP_CHR("chardev", USBSerialState
, cs
),
535 DEFINE_PROP_END_OF_LIST(),
538 static void usb_serial_dev_class_init(ObjectClass
*klass
, void *data
)
540 DeviceClass
*dc
= DEVICE_CLASS(klass
);
541 USBDeviceClass
*uc
= USB_DEVICE_CLASS(klass
);
543 uc
->realize
= usb_serial_realize
;
544 uc
->handle_reset
= usb_serial_handle_reset
;
545 uc
->handle_control
= usb_serial_handle_control
;
546 uc
->handle_data
= usb_serial_handle_data
;
547 dc
->vmsd
= &vmstate_usb_serial
;
548 set_bit(DEVICE_CATEGORY_INPUT
, dc
->categories
);
551 static const TypeInfo usb_serial_dev_type_info
= {
552 .name
= TYPE_USB_SERIAL
,
553 .parent
= TYPE_USB_DEVICE
,
554 .instance_size
= sizeof(USBSerialState
),
556 .class_init
= usb_serial_dev_class_init
,
559 static void usb_serial_class_initfn(ObjectClass
*klass
, void *data
)
561 DeviceClass
*dc
= DEVICE_CLASS(klass
);
562 USBDeviceClass
*uc
= USB_DEVICE_CLASS(klass
);
564 uc
->product_desc
= "QEMU USB Serial";
565 uc
->usb_desc
= &desc_serial
;
566 dc
->props
= serial_properties
;
569 static const TypeInfo serial_info
= {
570 .name
= "usb-serial",
571 .parent
= TYPE_USB_SERIAL
,
572 .class_init
= usb_serial_class_initfn
,
575 static Property braille_properties
[] = {
576 DEFINE_PROP_CHR("chardev", USBSerialState
, cs
),
577 DEFINE_PROP_END_OF_LIST(),
580 static void usb_braille_class_initfn(ObjectClass
*klass
, void *data
)
582 DeviceClass
*dc
= DEVICE_CLASS(klass
);
583 USBDeviceClass
*uc
= USB_DEVICE_CLASS(klass
);
585 uc
->product_desc
= "QEMU USB Braille";
586 uc
->usb_desc
= &desc_braille
;
587 dc
->props
= braille_properties
;
590 static const TypeInfo braille_info
= {
591 .name
= "usb-braille",
592 .parent
= TYPE_USB_SERIAL
,
593 .class_init
= usb_braille_class_initfn
,
596 static void usb_serial_register_types(void)
598 type_register_static(&usb_serial_dev_type_info
);
599 type_register_static(&serial_info
);
600 type_register_static(&braille_info
);
601 usb_legacy_register("usb-braille", "braille", usb_braille_init
);
604 type_init(usb_serial_register_types
)