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 "qemu-common.h"
13 #include "qemu/error-report.h"
15 #include "hw/usb/desc.h"
16 #include "sysemu/char.h"
18 //#define DEBUG_Serial
21 #define DPRINTF(fmt, ...) \
22 do { printf("usb-serial: " fmt , ## __VA_ARGS__); } while (0)
24 #define DPRINTF(fmt, ...) do {} while(0)
31 #define FTDI_SET_MDM_CTRL 1
32 #define FTDI_SET_FLOW_CTRL 2
33 #define FTDI_SET_BAUD 3
34 #define FTDI_SET_DATA 4
35 #define FTDI_GET_MDM_ST 5
36 #define FTDI_SET_EVENT_CHR 6
37 #define FTDI_SET_ERROR_CHR 7
38 #define FTDI_SET_LATENCY 9
39 #define FTDI_GET_LATENCY 10
41 #define DeviceOutVendor ((USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_DEVICE)<<8)
42 #define DeviceInVendor ((USB_DIR_IN |USB_TYPE_VENDOR|USB_RECIP_DEVICE)<<8)
46 #define FTDI_RESET_SIO 0
47 #define FTDI_RESET_RX 1
48 #define FTDI_RESET_TX 2
53 #define FTDI_SET_DTR (FTDI_DTR << 8)
55 #define FTDI_SET_RTS (FTDI_RTS << 8)
59 #define FTDI_RTS_CTS_HS 1
60 #define FTDI_DTR_DSR_HS 2
61 #define FTDI_XON_XOFF_HS 4
65 #define FTDI_PARITY (0x7 << 8)
66 #define FTDI_ODD (0x1 << 8)
67 #define FTDI_EVEN (0x2 << 8)
68 #define FTDI_MARK (0x3 << 8)
69 #define FTDI_SPACE (0x4 << 8)
71 #define FTDI_STOP (0x3 << 11)
72 #define FTDI_STOP1 (0x0 << 11)
73 #define FTDI_STOP15 (0x1 << 11)
74 #define FTDI_STOP2 (0x2 << 11)
77 /* TODO: should be sent every 40ms */
78 #define FTDI_CTS (1<<4) // CTS line status
79 #define FTDI_DSR (1<<5) // DSR line status
80 #define FTDI_RI (1<<6) // RI line status
81 #define FTDI_RLSD (1<<7) // Receive Line Signal Detect
85 #define FTDI_DR (1<<0) // Data Ready
86 #define FTDI_OE (1<<1) // Overrun Err
87 #define FTDI_PE (1<<2) // Parity Err
88 #define FTDI_FE (1<<3) // Framing Err
89 #define FTDI_BI (1<<4) // Break Interrupt
90 #define FTDI_THRE (1<<5) // Transmitter Holding Register
91 #define FTDI_TEMT (1<<6) // Transmitter Empty
92 #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 #define TYPE_USB_SERIAL "usb-serial-dev"
108 #define USB_SERIAL_DEV(obj) OBJECT_CHECK(USBSerialState, (obj), TYPE_USB_SERIAL)
111 STR_MANUFACTURER
= 1,
117 static const USBDescStrings desc_strings
= {
118 [STR_MANUFACTURER
] = "QEMU",
119 [STR_PRODUCT_SERIAL
] = "QEMU USB SERIAL",
120 [STR_PRODUCT_BRAILLE
] = "QEMU USB BAUM BRAILLE",
121 [STR_SERIALNUMBER
] = "1",
124 static const USBDescIface desc_iface0
= {
125 .bInterfaceNumber
= 0,
127 .bInterfaceClass
= 0xff,
128 .bInterfaceSubClass
= 0xff,
129 .bInterfaceProtocol
= 0xff,
130 .eps
= (USBDescEndpoint
[]) {
132 .bEndpointAddress
= USB_DIR_IN
| 0x01,
133 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
134 .wMaxPacketSize
= 64,
136 .bEndpointAddress
= USB_DIR_OUT
| 0x02,
137 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
138 .wMaxPacketSize
= 64,
143 static const USBDescDevice desc_device
= {
145 .bMaxPacketSize0
= 8,
146 .bNumConfigurations
= 1,
147 .confs
= (USBDescConfig
[]) {
150 .bConfigurationValue
= 1,
151 .bmAttributes
= USB_CFG_ATT_ONE
,
159 static const USBDesc desc_serial
= {
164 .iManufacturer
= STR_MANUFACTURER
,
165 .iProduct
= STR_PRODUCT_SERIAL
,
166 .iSerialNumber
= STR_SERIALNUMBER
,
168 .full
= &desc_device
,
172 static const USBDesc desc_braille
= {
177 .iManufacturer
= STR_MANUFACTURER
,
178 .iProduct
= STR_PRODUCT_BRAILLE
,
179 .iSerialNumber
= STR_SERIALNUMBER
,
181 .full
= &desc_device
,
185 static void usb_serial_reset(USBSerialState
*s
)
187 /* TODO: Set flow control to none */
189 s
->event_trigger
= 0;
192 /* TODO: purge in char driver */
195 static void usb_serial_handle_reset(USBDevice
*dev
)
197 USBSerialState
*s
= (USBSerialState
*)dev
;
202 /* TODO: Reset char device, send BREAK? */
205 static uint8_t usb_get_modem_lines(USBSerialState
*s
)
210 if (qemu_chr_fe_ioctl(s
->cs
, CHR_IOCTL_SERIAL_GET_TIOCM
, &flags
) == -ENOTSUP
)
211 return FTDI_CTS
|FTDI_DSR
|FTDI_RLSD
;
214 if (flags
& CHR_TIOCM_CTS
)
216 if (flags
& CHR_TIOCM_DSR
)
218 if (flags
& CHR_TIOCM_RI
)
220 if (flags
& CHR_TIOCM_CAR
)
226 static void usb_serial_handle_control(USBDevice
*dev
, USBPacket
*p
,
227 int request
, int value
, int index
, int length
, uint8_t *data
)
229 USBSerialState
*s
= (USBSerialState
*)dev
;
232 DPRINTF("got control %x, value %x\n",request
, value
);
233 ret
= usb_desc_handle_control(dev
, p
, request
, value
, index
, length
, data
);
239 case EndpointOutRequest
| USB_REQ_CLEAR_FEATURE
:
242 /* Class specific requests. */
243 case DeviceOutVendor
| FTDI_RESET
:
251 /* TODO: purge from char device */
254 /* TODO: purge from char device */
258 case DeviceOutVendor
| FTDI_SET_MDM_CTRL
:
261 qemu_chr_fe_ioctl(s
->cs
,CHR_IOCTL_SERIAL_GET_TIOCM
, &flags
);
262 if (value
& FTDI_SET_RTS
) {
263 if (value
& FTDI_RTS
)
264 flags
|= CHR_TIOCM_RTS
;
266 flags
&= ~CHR_TIOCM_RTS
;
268 if (value
& FTDI_SET_DTR
) {
269 if (value
& FTDI_DTR
)
270 flags
|= CHR_TIOCM_DTR
;
272 flags
&= ~CHR_TIOCM_DTR
;
274 qemu_chr_fe_ioctl(s
->cs
,CHR_IOCTL_SERIAL_SET_TIOCM
, &flags
);
277 case DeviceOutVendor
| FTDI_SET_FLOW_CTRL
:
280 case DeviceOutVendor
| FTDI_SET_BAUD
: {
281 static const int subdivisors8
[8] = { 0, 4, 2, 1, 3, 5, 6, 7 };
282 int subdivisor8
= subdivisors8
[((value
& 0xc000) >> 14)
283 | ((index
& 1) << 2)];
284 int divisor
= value
& 0x3fff;
286 /* chip special cases */
287 if (divisor
== 1 && subdivisor8
== 0)
289 if (divisor
== 0 && subdivisor8
== 0)
292 s
->params
.speed
= (48000000 / 2) / (8 * divisor
+ subdivisor8
);
293 qemu_chr_fe_ioctl(s
->cs
, CHR_IOCTL_SERIAL_SET_PARAMS
, &s
->params
);
296 case DeviceOutVendor
| FTDI_SET_DATA
:
297 switch (value
& FTDI_PARITY
) {
299 s
->params
.parity
= 'N';
302 s
->params
.parity
= 'O';
305 s
->params
.parity
= 'E';
308 DPRINTF("unsupported parity %d\n", value
& FTDI_PARITY
);
311 switch (value
& FTDI_STOP
) {
313 s
->params
.stop_bits
= 1;
316 s
->params
.stop_bits
= 2;
319 DPRINTF("unsupported stop bits %d\n", value
& FTDI_STOP
);
322 qemu_chr_fe_ioctl(s
->cs
, CHR_IOCTL_SERIAL_SET_PARAMS
, &s
->params
);
323 /* TODO: TX ON/OFF */
325 case DeviceInVendor
| FTDI_GET_MDM_ST
:
326 data
[0] = usb_get_modem_lines(s
) | 1;
328 p
->actual_length
= 2;
330 case DeviceOutVendor
| FTDI_SET_EVENT_CHR
:
331 /* TODO: handle it */
332 s
->event_chr
= value
;
334 case DeviceOutVendor
| FTDI_SET_ERROR_CHR
:
335 /* TODO: handle it */
336 s
->error_chr
= value
;
338 case DeviceOutVendor
| FTDI_SET_LATENCY
:
341 case DeviceInVendor
| FTDI_GET_LATENCY
:
342 data
[0] = s
->latency
;
343 p
->actual_length
= 1;
347 DPRINTF("got unsupported/bogus control %x, value %x\n", request
, value
);
348 p
->status
= USB_RET_STALL
;
353 static void usb_serial_handle_data(USBDevice
*dev
, USBPacket
*p
)
355 USBSerialState
*s
= (USBSerialState
*)dev
;
356 uint8_t devep
= p
->ep
->nr
;
359 int i
, first_len
, len
;
365 for (i
= 0; i
< p
->iov
.niov
; i
++) {
366 iov
= p
->iov
.iov
+ i
;
367 qemu_chr_fe_write(s
->cs
, iov
->iov_base
, iov
->iov_len
);
369 p
->actual_length
= p
->iov
.size
;
375 first_len
= RECV_BUF
- s
->recv_ptr
;
378 p
->status
= USB_RET_NAK
;
381 header
[0] = usb_get_modem_lines(s
) | 1;
382 /* We do not have the uart details */
383 /* handle serial break */
384 if (s
->event_trigger
&& s
->event_trigger
& FTDI_BI
) {
385 s
->event_trigger
&= ~FTDI_BI
;
387 usb_packet_copy(p
, header
, 2);
393 if (len
> s
->recv_used
)
396 p
->status
= USB_RET_NAK
;
401 usb_packet_copy(p
, header
, 2);
402 usb_packet_copy(p
, s
->recv_buf
+ s
->recv_ptr
, first_len
);
404 usb_packet_copy(p
, s
->recv_buf
, len
- first_len
);
406 s
->recv_ptr
= (s
->recv_ptr
+ len
) % RECV_BUF
;
410 DPRINTF("Bad token\n");
412 p
->status
= USB_RET_STALL
;
417 static int usb_serial_can_read(void *opaque
)
419 USBSerialState
*s
= opaque
;
421 if (!s
->dev
.attached
) {
424 return RECV_BUF
- s
->recv_used
;
427 static void usb_serial_read(void *opaque
, const uint8_t *buf
, int size
)
429 USBSerialState
*s
= opaque
;
430 int first_size
, start
;
432 /* room in the buffer? */
433 if (size
> (RECV_BUF
- s
->recv_used
))
434 size
= RECV_BUF
- s
->recv_used
;
436 start
= s
->recv_ptr
+ s
->recv_used
;
437 if (start
< RECV_BUF
) {
438 /* copy data to end of buffer */
439 first_size
= RECV_BUF
- start
;
440 if (first_size
> size
)
443 memcpy(s
->recv_buf
+ start
, buf
, first_size
);
445 /* wrap around to front if needed */
446 if (size
> first_size
)
447 memcpy(s
->recv_buf
, buf
+ first_size
, size
- first_size
);
450 memcpy(s
->recv_buf
+ start
, buf
, size
);
452 s
->recv_used
+= size
;
455 static void usb_serial_event(void *opaque
, int event
)
457 USBSerialState
*s
= opaque
;
460 case CHR_EVENT_BREAK
:
461 s
->event_trigger
|= FTDI_BI
;
463 case CHR_EVENT_FOCUS
:
465 case CHR_EVENT_OPENED
:
466 if (!s
->dev
.attached
) {
467 usb_device_attach(&s
->dev
, &error_abort
);
470 case CHR_EVENT_CLOSED
:
471 if (s
->dev
.attached
) {
472 usb_device_detach(&s
->dev
);
478 static void usb_serial_realize(USBDevice
*dev
, Error
**errp
)
480 USBSerialState
*s
= USB_SERIAL_DEV(dev
);
481 Error
*local_err
= NULL
;
483 usb_desc_create_serial(dev
);
485 dev
->auto_attach
= 0;
488 error_setg(errp
, "Property chardev is required");
492 usb_check_attach(dev
, &local_err
);
494 error_propagate(errp
, local_err
);
498 qemu_chr_add_handlers(s
->cs
, usb_serial_can_read
, usb_serial_read
,
499 usb_serial_event
, s
);
500 usb_serial_handle_reset(dev
);
502 if (s
->cs
->be_open
&& !dev
->attached
) {
503 usb_device_attach(dev
, &error_abort
);
507 static USBDevice
*usb_serial_init(USBBus
*bus
, const char *filename
)
510 CharDriverState
*cdrv
;
511 uint32_t vendorid
= 0, productid
= 0;
515 while (*filename
&& *filename
!= ':') {
518 if (strstart(filename
, "vendorid=", &p
)) {
519 vendorid
= strtol(p
, &e
, 16);
520 if (e
== p
|| (*e
&& *e
!= ',' && *e
!= ':')) {
521 error_report("bogus vendor ID %s", p
);
525 } else if (strstart(filename
, "productid=", &p
)) {
526 productid
= strtol(p
, &e
, 16);
527 if (e
== p
|| (*e
&& *e
!= ',' && *e
!= ':')) {
528 error_report("bogus product ID %s", p
);
533 error_report("unrecognized serial USB option %s", filename
);
536 while(*filename
== ',')
540 error_report("character device specification needed");
545 snprintf(label
, sizeof(label
), "usbserial%d", index
++);
546 cdrv
= qemu_chr_new(label
, filename
, NULL
);
550 dev
= usb_create(bus
, "usb-serial");
551 qdev_prop_set_chr(&dev
->qdev
, "chardev", cdrv
);
553 qdev_prop_set_uint16(&dev
->qdev
, "vendorid", vendorid
);
555 qdev_prop_set_uint16(&dev
->qdev
, "productid", productid
);
559 static USBDevice
*usb_braille_init(USBBus
*bus
, const char *unused
)
562 CharDriverState
*cdrv
;
564 cdrv
= qemu_chr_new("braille", "braille", NULL
);
568 dev
= usb_create(bus
, "usb-braille");
569 qdev_prop_set_chr(&dev
->qdev
, "chardev", cdrv
);
573 static const VMStateDescription vmstate_usb_serial
= {
574 .name
= "usb-serial",
578 static Property serial_properties
[] = {
579 DEFINE_PROP_CHR("chardev", USBSerialState
, cs
),
580 DEFINE_PROP_END_OF_LIST(),
583 static void usb_serial_dev_class_init(ObjectClass
*klass
, void *data
)
585 DeviceClass
*dc
= DEVICE_CLASS(klass
);
586 USBDeviceClass
*uc
= USB_DEVICE_CLASS(klass
);
588 uc
->realize
= usb_serial_realize
;
589 uc
->handle_reset
= usb_serial_handle_reset
;
590 uc
->handle_control
= usb_serial_handle_control
;
591 uc
->handle_data
= usb_serial_handle_data
;
592 dc
->vmsd
= &vmstate_usb_serial
;
593 set_bit(DEVICE_CATEGORY_INPUT
, dc
->categories
);
596 static const TypeInfo usb_serial_dev_type_info
= {
597 .name
= TYPE_USB_SERIAL
,
598 .parent
= TYPE_USB_DEVICE
,
599 .instance_size
= sizeof(USBSerialState
),
601 .class_init
= usb_serial_dev_class_init
,
604 static void usb_serial_class_initfn(ObjectClass
*klass
, void *data
)
606 DeviceClass
*dc
= DEVICE_CLASS(klass
);
607 USBDeviceClass
*uc
= USB_DEVICE_CLASS(klass
);
609 uc
->product_desc
= "QEMU USB Serial";
610 uc
->usb_desc
= &desc_serial
;
611 dc
->props
= serial_properties
;
614 static const TypeInfo serial_info
= {
615 .name
= "usb-serial",
616 .parent
= TYPE_USB_SERIAL
,
617 .class_init
= usb_serial_class_initfn
,
620 static Property braille_properties
[] = {
621 DEFINE_PROP_CHR("chardev", USBSerialState
, cs
),
622 DEFINE_PROP_END_OF_LIST(),
625 static void usb_braille_class_initfn(ObjectClass
*klass
, void *data
)
627 DeviceClass
*dc
= DEVICE_CLASS(klass
);
628 USBDeviceClass
*uc
= USB_DEVICE_CLASS(klass
);
630 uc
->product_desc
= "QEMU USB Braille";
631 uc
->usb_desc
= &desc_braille
;
632 dc
->props
= braille_properties
;
635 static const TypeInfo braille_info
= {
636 .name
= "usb-braille",
637 .parent
= TYPE_USB_SERIAL
,
638 .class_init
= usb_braille_class_initfn
,
641 static void usb_serial_register_types(void)
643 type_register_static(&usb_serial_dev_type_info
);
644 type_register_static(&serial_info
);
645 usb_legacy_register("usb-serial", "serial", usb_serial_init
);
646 type_register_static(&braille_info
);
647 usb_legacy_register("usb-braille", "braille", usb_braille_init
);
650 type_init(usb_serial_register_types
)