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-common.h"
14 #include "qemu/error-report.h"
16 #include "hw/usb/desc.h"
17 #include "sysemu/char.h"
19 //#define DEBUG_Serial
22 #define DPRINTF(fmt, ...) \
23 do { printf("usb-serial: " fmt , ## __VA_ARGS__); } while (0)
25 #define DPRINTF(fmt, ...) do {} while(0)
32 #define FTDI_SET_MDM_CTRL 1
33 #define FTDI_SET_FLOW_CTRL 2
34 #define FTDI_SET_BAUD 3
35 #define FTDI_SET_DATA 4
36 #define FTDI_GET_MDM_ST 5
37 #define FTDI_SET_EVENT_CHR 6
38 #define FTDI_SET_ERROR_CHR 7
39 #define FTDI_SET_LATENCY 9
40 #define FTDI_GET_LATENCY 10
42 #define DeviceOutVendor ((USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_DEVICE)<<8)
43 #define DeviceInVendor ((USB_DIR_IN |USB_TYPE_VENDOR|USB_RECIP_DEVICE)<<8)
47 #define FTDI_RESET_SIO 0
48 #define FTDI_RESET_RX 1
49 #define FTDI_RESET_TX 2
54 #define FTDI_SET_DTR (FTDI_DTR << 8)
56 #define FTDI_SET_RTS (FTDI_RTS << 8)
60 #define FTDI_RTS_CTS_HS 1
61 #define FTDI_DTR_DSR_HS 2
62 #define FTDI_XON_XOFF_HS 4
66 #define FTDI_PARITY (0x7 << 8)
67 #define FTDI_ODD (0x1 << 8)
68 #define FTDI_EVEN (0x2 << 8)
69 #define FTDI_MARK (0x3 << 8)
70 #define FTDI_SPACE (0x4 << 8)
72 #define FTDI_STOP (0x3 << 11)
73 #define FTDI_STOP1 (0x0 << 11)
74 #define FTDI_STOP15 (0x1 << 11)
75 #define FTDI_STOP2 (0x2 << 11)
78 /* TODO: should be sent every 40ms */
79 #define FTDI_CTS (1<<4) // CTS line status
80 #define FTDI_DSR (1<<5) // DSR line status
81 #define FTDI_RI (1<<6) // RI line status
82 #define FTDI_RLSD (1<<7) // Receive Line Signal Detect
86 #define FTDI_DR (1<<0) // Data Ready
87 #define FTDI_OE (1<<1) // Overrun Err
88 #define FTDI_PE (1<<2) // Parity Err
89 #define FTDI_FE (1<<3) // Framing Err
90 #define FTDI_BI (1<<4) // Break Interrupt
91 #define FTDI_THRE (1<<5) // Transmitter Holding Register
92 #define FTDI_TEMT (1<<6) // Transmitter Empty
93 #define FTDI_FIFO (1<<7) // Error in FIFO
97 uint8_t recv_buf
[RECV_BUF
];
102 uint8_t event_trigger
;
103 QEMUSerialSetParams params
;
104 int latency
; /* ms */
108 #define TYPE_USB_SERIAL "usb-serial-dev"
109 #define USB_SERIAL_DEV(obj) OBJECT_CHECK(USBSerialState, (obj), TYPE_USB_SERIAL)
112 STR_MANUFACTURER
= 1,
118 static const USBDescStrings desc_strings
= {
119 [STR_MANUFACTURER
] = "QEMU",
120 [STR_PRODUCT_SERIAL
] = "QEMU USB SERIAL",
121 [STR_PRODUCT_BRAILLE
] = "QEMU USB BAUM BRAILLE",
122 [STR_SERIALNUMBER
] = "1",
125 static const USBDescIface desc_iface0
= {
126 .bInterfaceNumber
= 0,
128 .bInterfaceClass
= 0xff,
129 .bInterfaceSubClass
= 0xff,
130 .bInterfaceProtocol
= 0xff,
131 .eps
= (USBDescEndpoint
[]) {
133 .bEndpointAddress
= USB_DIR_IN
| 0x01,
134 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
135 .wMaxPacketSize
= 64,
137 .bEndpointAddress
= USB_DIR_OUT
| 0x02,
138 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
139 .wMaxPacketSize
= 64,
144 static const USBDescDevice desc_device
= {
146 .bMaxPacketSize0
= 8,
147 .bNumConfigurations
= 1,
148 .confs
= (USBDescConfig
[]) {
151 .bConfigurationValue
= 1,
152 .bmAttributes
= USB_CFG_ATT_ONE
,
160 static const USBDesc desc_serial
= {
165 .iManufacturer
= STR_MANUFACTURER
,
166 .iProduct
= STR_PRODUCT_SERIAL
,
167 .iSerialNumber
= STR_SERIALNUMBER
,
169 .full
= &desc_device
,
173 static const USBDesc desc_braille
= {
178 .iManufacturer
= STR_MANUFACTURER
,
179 .iProduct
= STR_PRODUCT_BRAILLE
,
180 .iSerialNumber
= STR_SERIALNUMBER
,
182 .full
= &desc_device
,
186 static void usb_serial_reset(USBSerialState
*s
)
188 /* TODO: Set flow control to none */
190 s
->event_trigger
= 0;
193 /* TODO: purge in char driver */
196 static void usb_serial_handle_reset(USBDevice
*dev
)
198 USBSerialState
*s
= (USBSerialState
*)dev
;
203 /* TODO: Reset char device, send BREAK? */
206 static uint8_t usb_get_modem_lines(USBSerialState
*s
)
211 if (qemu_chr_fe_ioctl(s
->cs
, CHR_IOCTL_SERIAL_GET_TIOCM
, &flags
) == -ENOTSUP
)
212 return FTDI_CTS
|FTDI_DSR
|FTDI_RLSD
;
215 if (flags
& CHR_TIOCM_CTS
)
217 if (flags
& CHR_TIOCM_DSR
)
219 if (flags
& CHR_TIOCM_RI
)
221 if (flags
& CHR_TIOCM_CAR
)
227 static void usb_serial_handle_control(USBDevice
*dev
, USBPacket
*p
,
228 int request
, int value
, int index
, int length
, uint8_t *data
)
230 USBSerialState
*s
= (USBSerialState
*)dev
;
233 DPRINTF("got control %x, value %x\n",request
, value
);
234 ret
= usb_desc_handle_control(dev
, p
, request
, value
, index
, length
, data
);
240 case EndpointOutRequest
| USB_REQ_CLEAR_FEATURE
:
243 /* Class specific requests. */
244 case DeviceOutVendor
| FTDI_RESET
:
252 /* TODO: purge from char device */
255 /* TODO: purge from char device */
259 case DeviceOutVendor
| FTDI_SET_MDM_CTRL
:
262 qemu_chr_fe_ioctl(s
->cs
,CHR_IOCTL_SERIAL_GET_TIOCM
, &flags
);
263 if (value
& FTDI_SET_RTS
) {
264 if (value
& FTDI_RTS
)
265 flags
|= CHR_TIOCM_RTS
;
267 flags
&= ~CHR_TIOCM_RTS
;
269 if (value
& FTDI_SET_DTR
) {
270 if (value
& FTDI_DTR
)
271 flags
|= CHR_TIOCM_DTR
;
273 flags
&= ~CHR_TIOCM_DTR
;
275 qemu_chr_fe_ioctl(s
->cs
,CHR_IOCTL_SERIAL_SET_TIOCM
, &flags
);
278 case DeviceOutVendor
| FTDI_SET_FLOW_CTRL
:
281 case DeviceOutVendor
| FTDI_SET_BAUD
: {
282 static const int subdivisors8
[8] = { 0, 4, 2, 1, 3, 5, 6, 7 };
283 int subdivisor8
= subdivisors8
[((value
& 0xc000) >> 14)
284 | ((index
& 1) << 2)];
285 int divisor
= value
& 0x3fff;
287 /* chip special cases */
288 if (divisor
== 1 && subdivisor8
== 0)
290 if (divisor
== 0 && subdivisor8
== 0)
293 s
->params
.speed
= (48000000 / 2) / (8 * divisor
+ subdivisor8
);
294 qemu_chr_fe_ioctl(s
->cs
, CHR_IOCTL_SERIAL_SET_PARAMS
, &s
->params
);
297 case DeviceOutVendor
| FTDI_SET_DATA
:
298 switch (value
& FTDI_PARITY
) {
300 s
->params
.parity
= 'N';
303 s
->params
.parity
= 'O';
306 s
->params
.parity
= 'E';
309 DPRINTF("unsupported parity %d\n", value
& FTDI_PARITY
);
312 switch (value
& FTDI_STOP
) {
314 s
->params
.stop_bits
= 1;
317 s
->params
.stop_bits
= 2;
320 DPRINTF("unsupported stop bits %d\n", value
& FTDI_STOP
);
323 qemu_chr_fe_ioctl(s
->cs
, CHR_IOCTL_SERIAL_SET_PARAMS
, &s
->params
);
324 /* TODO: TX ON/OFF */
326 case DeviceInVendor
| FTDI_GET_MDM_ST
:
327 data
[0] = usb_get_modem_lines(s
) | 1;
329 p
->actual_length
= 2;
331 case DeviceOutVendor
| FTDI_SET_EVENT_CHR
:
332 /* TODO: handle it */
333 s
->event_chr
= value
;
335 case DeviceOutVendor
| FTDI_SET_ERROR_CHR
:
336 /* TODO: handle it */
337 s
->error_chr
= value
;
339 case DeviceOutVendor
| FTDI_SET_LATENCY
:
342 case DeviceInVendor
| FTDI_GET_LATENCY
:
343 data
[0] = s
->latency
;
344 p
->actual_length
= 1;
348 DPRINTF("got unsupported/bogus control %x, value %x\n", request
, value
);
349 p
->status
= USB_RET_STALL
;
354 static void usb_serial_handle_data(USBDevice
*dev
, USBPacket
*p
)
356 USBSerialState
*s
= (USBSerialState
*)dev
;
357 uint8_t devep
= p
->ep
->nr
;
360 int i
, first_len
, len
;
366 for (i
= 0; i
< p
->iov
.niov
; i
++) {
367 iov
= p
->iov
.iov
+ i
;
368 qemu_chr_fe_write(s
->cs
, iov
->iov_base
, iov
->iov_len
);
370 p
->actual_length
= p
->iov
.size
;
376 first_len
= RECV_BUF
- s
->recv_ptr
;
379 p
->status
= USB_RET_NAK
;
382 header
[0] = usb_get_modem_lines(s
) | 1;
383 /* We do not have the uart details */
384 /* handle serial break */
385 if (s
->event_trigger
&& s
->event_trigger
& FTDI_BI
) {
386 s
->event_trigger
&= ~FTDI_BI
;
388 usb_packet_copy(p
, header
, 2);
394 if (len
> s
->recv_used
)
397 p
->status
= USB_RET_NAK
;
402 usb_packet_copy(p
, header
, 2);
403 usb_packet_copy(p
, s
->recv_buf
+ s
->recv_ptr
, first_len
);
405 usb_packet_copy(p
, s
->recv_buf
, len
- first_len
);
407 s
->recv_ptr
= (s
->recv_ptr
+ len
) % RECV_BUF
;
411 DPRINTF("Bad token\n");
413 p
->status
= USB_RET_STALL
;
418 static int usb_serial_can_read(void *opaque
)
420 USBSerialState
*s
= opaque
;
422 if (!s
->dev
.attached
) {
425 return RECV_BUF
- s
->recv_used
;
428 static void usb_serial_read(void *opaque
, const uint8_t *buf
, int size
)
430 USBSerialState
*s
= opaque
;
431 int first_size
, start
;
433 /* room in the buffer? */
434 if (size
> (RECV_BUF
- s
->recv_used
))
435 size
= RECV_BUF
- s
->recv_used
;
437 start
= s
->recv_ptr
+ s
->recv_used
;
438 if (start
< RECV_BUF
) {
439 /* copy data to end of buffer */
440 first_size
= RECV_BUF
- start
;
441 if (first_size
> size
)
444 memcpy(s
->recv_buf
+ start
, buf
, first_size
);
446 /* wrap around to front if needed */
447 if (size
> first_size
)
448 memcpy(s
->recv_buf
, buf
+ first_size
, size
- first_size
);
451 memcpy(s
->recv_buf
+ start
, buf
, size
);
453 s
->recv_used
+= size
;
456 static void usb_serial_event(void *opaque
, int event
)
458 USBSerialState
*s
= opaque
;
461 case CHR_EVENT_BREAK
:
462 s
->event_trigger
|= FTDI_BI
;
464 case CHR_EVENT_FOCUS
:
466 case CHR_EVENT_OPENED
:
467 if (!s
->dev
.attached
) {
468 usb_device_attach(&s
->dev
, &error_abort
);
471 case CHR_EVENT_CLOSED
:
472 if (s
->dev
.attached
) {
473 usb_device_detach(&s
->dev
);
479 static void usb_serial_realize(USBDevice
*dev
, Error
**errp
)
481 USBSerialState
*s
= USB_SERIAL_DEV(dev
);
482 Error
*local_err
= NULL
;
484 usb_desc_create_serial(dev
);
486 dev
->auto_attach
= 0;
489 error_setg(errp
, "Property chardev is required");
493 usb_check_attach(dev
, &local_err
);
495 error_propagate(errp
, local_err
);
499 qemu_chr_add_handlers(s
->cs
, usb_serial_can_read
, usb_serial_read
,
500 usb_serial_event
, s
);
501 usb_serial_handle_reset(dev
);
503 if (s
->cs
->be_open
&& !dev
->attached
) {
504 usb_device_attach(dev
, &error_abort
);
508 static USBDevice
*usb_serial_init(USBBus
*bus
, const char *filename
)
511 CharDriverState
*cdrv
;
512 uint32_t vendorid
= 0, productid
= 0;
516 while (*filename
&& *filename
!= ':') {
519 if (strstart(filename
, "vendorid=", &p
)) {
520 vendorid
= strtol(p
, &e
, 16);
521 if (e
== p
|| (*e
&& *e
!= ',' && *e
!= ':')) {
522 error_report("bogus vendor ID %s", p
);
526 } else if (strstart(filename
, "productid=", &p
)) {
527 productid
= strtol(p
, &e
, 16);
528 if (e
== p
|| (*e
&& *e
!= ',' && *e
!= ':')) {
529 error_report("bogus product ID %s", p
);
534 error_report("unrecognized serial USB option %s", filename
);
537 while(*filename
== ',')
541 error_report("character device specification needed");
546 snprintf(label
, sizeof(label
), "usbserial%d", index
++);
547 cdrv
= qemu_chr_new(label
, filename
, NULL
);
551 dev
= usb_create(bus
, "usb-serial");
552 qdev_prop_set_chr(&dev
->qdev
, "chardev", cdrv
);
554 qdev_prop_set_uint16(&dev
->qdev
, "vendorid", vendorid
);
556 qdev_prop_set_uint16(&dev
->qdev
, "productid", productid
);
560 static USBDevice
*usb_braille_init(USBBus
*bus
, const char *unused
)
563 CharDriverState
*cdrv
;
565 cdrv
= qemu_chr_new("braille", "braille", NULL
);
569 dev
= usb_create(bus
, "usb-braille");
570 qdev_prop_set_chr(&dev
->qdev
, "chardev", cdrv
);
574 static const VMStateDescription vmstate_usb_serial
= {
575 .name
= "usb-serial",
579 static Property serial_properties
[] = {
580 DEFINE_PROP_CHR("chardev", USBSerialState
, cs
),
581 DEFINE_PROP_END_OF_LIST(),
584 static void usb_serial_dev_class_init(ObjectClass
*klass
, void *data
)
586 DeviceClass
*dc
= DEVICE_CLASS(klass
);
587 USBDeviceClass
*uc
= USB_DEVICE_CLASS(klass
);
589 uc
->realize
= usb_serial_realize
;
590 uc
->handle_reset
= usb_serial_handle_reset
;
591 uc
->handle_control
= usb_serial_handle_control
;
592 uc
->handle_data
= usb_serial_handle_data
;
593 dc
->vmsd
= &vmstate_usb_serial
;
594 set_bit(DEVICE_CATEGORY_INPUT
, dc
->categories
);
597 static const TypeInfo usb_serial_dev_type_info
= {
598 .name
= TYPE_USB_SERIAL
,
599 .parent
= TYPE_USB_DEVICE
,
600 .instance_size
= sizeof(USBSerialState
),
602 .class_init
= usb_serial_dev_class_init
,
605 static void usb_serial_class_initfn(ObjectClass
*klass
, void *data
)
607 DeviceClass
*dc
= DEVICE_CLASS(klass
);
608 USBDeviceClass
*uc
= USB_DEVICE_CLASS(klass
);
610 uc
->product_desc
= "QEMU USB Serial";
611 uc
->usb_desc
= &desc_serial
;
612 dc
->props
= serial_properties
;
615 static const TypeInfo serial_info
= {
616 .name
= "usb-serial",
617 .parent
= TYPE_USB_SERIAL
,
618 .class_init
= usb_serial_class_initfn
,
621 static Property braille_properties
[] = {
622 DEFINE_PROP_CHR("chardev", USBSerialState
, cs
),
623 DEFINE_PROP_END_OF_LIST(),
626 static void usb_braille_class_initfn(ObjectClass
*klass
, void *data
)
628 DeviceClass
*dc
= DEVICE_CLASS(klass
);
629 USBDeviceClass
*uc
= USB_DEVICE_CLASS(klass
);
631 uc
->product_desc
= "QEMU USB Braille";
632 uc
->usb_desc
= &desc_braille
;
633 dc
->props
= braille_properties
;
636 static const TypeInfo braille_info
= {
637 .name
= "usb-braille",
638 .parent
= TYPE_USB_SERIAL
,
639 .class_init
= usb_braille_class_initfn
,
642 static void usb_serial_register_types(void)
644 type_register_static(&usb_serial_dev_type_info
);
645 type_register_static(&serial_info
);
646 usb_legacy_register("usb-serial", "serial", usb_serial_init
);
647 type_register_static(&braille_info
);
648 usb_legacy_register("usb-braille", "braille", usb_braille_init
);
651 type_init(usb_serial_register_types
)