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-common.h"
12 #include "qemu/error-report.h"
14 #include "hw/usb/desc.h"
15 #include "sysemu/char.h"
17 //#define DEBUG_Serial
20 #define DPRINTF(fmt, ...) \
21 do { printf("usb-serial: " fmt , ## __VA_ARGS__); } while (0)
23 #define DPRINTF(fmt, ...) do {} while(0)
30 #define FTDI_SET_MDM_CTRL 1
31 #define FTDI_SET_FLOW_CTRL 2
32 #define FTDI_SET_BAUD 3
33 #define FTDI_SET_DATA 4
34 #define FTDI_GET_MDM_ST 5
35 #define FTDI_SET_EVENT_CHR 6
36 #define FTDI_SET_ERROR_CHR 7
37 #define FTDI_SET_LATENCY 9
38 #define FTDI_GET_LATENCY 10
40 #define DeviceOutVendor ((USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_DEVICE)<<8)
41 #define DeviceInVendor ((USB_DIR_IN |USB_TYPE_VENDOR|USB_RECIP_DEVICE)<<8)
45 #define FTDI_RESET_SIO 0
46 #define FTDI_RESET_RX 1
47 #define FTDI_RESET_TX 2
52 #define FTDI_SET_DTR (FTDI_DTR << 8)
54 #define FTDI_SET_RTS (FTDI_RTS << 8)
58 #define FTDI_RTS_CTS_HS 1
59 #define FTDI_DTR_DSR_HS 2
60 #define FTDI_XON_XOFF_HS 4
64 #define FTDI_PARITY (0x7 << 8)
65 #define FTDI_ODD (0x1 << 8)
66 #define FTDI_EVEN (0x2 << 8)
67 #define FTDI_MARK (0x3 << 8)
68 #define FTDI_SPACE (0x4 << 8)
70 #define FTDI_STOP (0x3 << 11)
71 #define FTDI_STOP1 (0x0 << 11)
72 #define FTDI_STOP15 (0x1 << 11)
73 #define FTDI_STOP2 (0x2 << 11)
76 /* TODO: should be sent every 40ms */
77 #define FTDI_CTS (1<<4) // CTS line status
78 #define FTDI_DSR (1<<5) // DSR line status
79 #define FTDI_RI (1<<6) // RI line status
80 #define FTDI_RLSD (1<<7) // Receive Line Signal Detect
84 #define FTDI_DR (1<<0) // Data Ready
85 #define FTDI_OE (1<<1) // Overrun Err
86 #define FTDI_PE (1<<2) // Parity Err
87 #define FTDI_FE (1<<3) // Framing Err
88 #define FTDI_BI (1<<4) // Break Interrupt
89 #define FTDI_THRE (1<<5) // Transmitter Holding Register
90 #define FTDI_TEMT (1<<6) // Transmitter Empty
91 #define FTDI_FIFO (1<<7) // Error in FIFO
95 uint8_t recv_buf
[RECV_BUF
];
100 uint8_t event_trigger
;
101 QEMUSerialSetParams params
;
102 int latency
; /* ms */
107 STR_MANUFACTURER
= 1,
113 static const USBDescStrings desc_strings
= {
114 [STR_MANUFACTURER
] = "QEMU",
115 [STR_PRODUCT_SERIAL
] = "QEMU USB SERIAL",
116 [STR_PRODUCT_BRAILLE
] = "QEMU USB BAUM BRAILLE",
117 [STR_SERIALNUMBER
] = "1",
120 static const USBDescIface desc_iface0
= {
121 .bInterfaceNumber
= 0,
123 .bInterfaceClass
= 0xff,
124 .bInterfaceSubClass
= 0xff,
125 .bInterfaceProtocol
= 0xff,
126 .eps
= (USBDescEndpoint
[]) {
128 .bEndpointAddress
= USB_DIR_IN
| 0x01,
129 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
130 .wMaxPacketSize
= 64,
132 .bEndpointAddress
= USB_DIR_OUT
| 0x02,
133 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
134 .wMaxPacketSize
= 64,
139 static const USBDescDevice desc_device
= {
141 .bMaxPacketSize0
= 8,
142 .bNumConfigurations
= 1,
143 .confs
= (USBDescConfig
[]) {
146 .bConfigurationValue
= 1,
147 .bmAttributes
= 0x80,
155 static const USBDesc desc_serial
= {
160 .iManufacturer
= STR_MANUFACTURER
,
161 .iProduct
= STR_PRODUCT_SERIAL
,
162 .iSerialNumber
= STR_SERIALNUMBER
,
164 .full
= &desc_device
,
168 static const USBDesc desc_braille
= {
173 .iManufacturer
= STR_MANUFACTURER
,
174 .iProduct
= STR_PRODUCT_BRAILLE
,
175 .iSerialNumber
= STR_SERIALNUMBER
,
177 .full
= &desc_device
,
181 static void usb_serial_reset(USBSerialState
*s
)
183 /* TODO: Set flow control to none */
185 s
->event_trigger
= 0;
188 /* TODO: purge in char driver */
191 static void usb_serial_handle_reset(USBDevice
*dev
)
193 USBSerialState
*s
= (USBSerialState
*)dev
;
198 /* TODO: Reset char device, send BREAK? */
201 static uint8_t usb_get_modem_lines(USBSerialState
*s
)
206 if (qemu_chr_fe_ioctl(s
->cs
, CHR_IOCTL_SERIAL_GET_TIOCM
, &flags
) == -ENOTSUP
)
207 return FTDI_CTS
|FTDI_DSR
|FTDI_RLSD
;
210 if (flags
& CHR_TIOCM_CTS
)
212 if (flags
& CHR_TIOCM_DSR
)
214 if (flags
& CHR_TIOCM_RI
)
216 if (flags
& CHR_TIOCM_CAR
)
222 static void usb_serial_handle_control(USBDevice
*dev
, USBPacket
*p
,
223 int request
, int value
, int index
, int length
, uint8_t *data
)
225 USBSerialState
*s
= (USBSerialState
*)dev
;
228 DPRINTF("got control %x, value %x\n",request
, value
);
229 ret
= usb_desc_handle_control(dev
, p
, request
, value
, index
, length
, data
);
235 case EndpointOutRequest
| USB_REQ_CLEAR_FEATURE
:
238 /* Class specific requests. */
239 case DeviceOutVendor
| FTDI_RESET
:
247 /* TODO: purge from char device */
250 /* TODO: purge from char device */
254 case DeviceOutVendor
| FTDI_SET_MDM_CTRL
:
257 qemu_chr_fe_ioctl(s
->cs
,CHR_IOCTL_SERIAL_GET_TIOCM
, &flags
);
258 if (value
& FTDI_SET_RTS
) {
259 if (value
& FTDI_RTS
)
260 flags
|= CHR_TIOCM_RTS
;
262 flags
&= ~CHR_TIOCM_RTS
;
264 if (value
& FTDI_SET_DTR
) {
265 if (value
& FTDI_DTR
)
266 flags
|= CHR_TIOCM_DTR
;
268 flags
&= ~CHR_TIOCM_DTR
;
270 qemu_chr_fe_ioctl(s
->cs
,CHR_IOCTL_SERIAL_SET_TIOCM
, &flags
);
273 case DeviceOutVendor
| FTDI_SET_FLOW_CTRL
:
276 case DeviceOutVendor
| FTDI_SET_BAUD
: {
277 static const int subdivisors8
[8] = { 0, 4, 2, 1, 3, 5, 6, 7 };
278 int subdivisor8
= subdivisors8
[((value
& 0xc000) >> 14)
279 | ((index
& 1) << 2)];
280 int divisor
= value
& 0x3fff;
282 /* chip special cases */
283 if (divisor
== 1 && subdivisor8
== 0)
285 if (divisor
== 0 && subdivisor8
== 0)
288 s
->params
.speed
= (48000000 / 2) / (8 * divisor
+ subdivisor8
);
289 qemu_chr_fe_ioctl(s
->cs
, CHR_IOCTL_SERIAL_SET_PARAMS
, &s
->params
);
292 case DeviceOutVendor
| FTDI_SET_DATA
:
293 switch (value
& FTDI_PARITY
) {
295 s
->params
.parity
= 'N';
298 s
->params
.parity
= 'O';
301 s
->params
.parity
= 'E';
304 DPRINTF("unsupported parity %d\n", value
& FTDI_PARITY
);
307 switch (value
& FTDI_STOP
) {
309 s
->params
.stop_bits
= 1;
312 s
->params
.stop_bits
= 2;
315 DPRINTF("unsupported stop bits %d\n", value
& FTDI_STOP
);
318 qemu_chr_fe_ioctl(s
->cs
, CHR_IOCTL_SERIAL_SET_PARAMS
, &s
->params
);
319 /* TODO: TX ON/OFF */
321 case DeviceInVendor
| FTDI_GET_MDM_ST
:
322 data
[0] = usb_get_modem_lines(s
) | 1;
324 p
->actual_length
= 2;
326 case DeviceOutVendor
| FTDI_SET_EVENT_CHR
:
327 /* TODO: handle it */
328 s
->event_chr
= value
;
330 case DeviceOutVendor
| FTDI_SET_ERROR_CHR
:
331 /* TODO: handle it */
332 s
->error_chr
= value
;
334 case DeviceOutVendor
| FTDI_SET_LATENCY
:
337 case DeviceInVendor
| FTDI_GET_LATENCY
:
338 data
[0] = s
->latency
;
339 p
->actual_length
= 1;
343 DPRINTF("got unsupported/bogus control %x, value %x\n", request
, value
);
344 p
->status
= USB_RET_STALL
;
349 static void usb_serial_handle_data(USBDevice
*dev
, USBPacket
*p
)
351 USBSerialState
*s
= (USBSerialState
*)dev
;
352 uint8_t devep
= p
->ep
->nr
;
355 int i
, first_len
, len
;
361 for (i
= 0; i
< p
->iov
.niov
; i
++) {
362 iov
= p
->iov
.iov
+ i
;
363 qemu_chr_fe_write(s
->cs
, iov
->iov_base
, iov
->iov_len
);
365 p
->actual_length
= p
->iov
.size
;
371 first_len
= RECV_BUF
- s
->recv_ptr
;
374 p
->status
= USB_RET_NAK
;
377 header
[0] = usb_get_modem_lines(s
) | 1;
378 /* We do not have the uart details */
379 /* handle serial break */
380 if (s
->event_trigger
&& s
->event_trigger
& FTDI_BI
) {
381 s
->event_trigger
&= ~FTDI_BI
;
383 usb_packet_copy(p
, header
, 2);
389 if (len
> s
->recv_used
)
392 p
->status
= USB_RET_NAK
;
397 usb_packet_copy(p
, header
, 2);
398 usb_packet_copy(p
, s
->recv_buf
+ s
->recv_ptr
, first_len
);
400 usb_packet_copy(p
, s
->recv_buf
, len
- first_len
);
402 s
->recv_ptr
= (s
->recv_ptr
+ len
) % RECV_BUF
;
406 DPRINTF("Bad token\n");
408 p
->status
= USB_RET_STALL
;
413 static int usb_serial_can_read(void *opaque
)
415 USBSerialState
*s
= opaque
;
417 if (!s
->dev
.attached
) {
420 return RECV_BUF
- s
->recv_used
;
423 static void usb_serial_read(void *opaque
, const uint8_t *buf
, int size
)
425 USBSerialState
*s
= opaque
;
426 int first_size
, start
;
428 /* room in the buffer? */
429 if (size
> (RECV_BUF
- s
->recv_used
))
430 size
= RECV_BUF
- s
->recv_used
;
432 start
= s
->recv_ptr
+ s
->recv_used
;
433 if (start
< RECV_BUF
) {
434 /* copy data to end of buffer */
435 first_size
= RECV_BUF
- start
;
436 if (first_size
> size
)
439 memcpy(s
->recv_buf
+ start
, buf
, first_size
);
441 /* wrap around to front if needed */
442 if (size
> first_size
)
443 memcpy(s
->recv_buf
, buf
+ first_size
, size
- first_size
);
446 memcpy(s
->recv_buf
+ start
, buf
, size
);
448 s
->recv_used
+= size
;
451 static void usb_serial_event(void *opaque
, int event
)
453 USBSerialState
*s
= opaque
;
456 case CHR_EVENT_BREAK
:
457 s
->event_trigger
|= FTDI_BI
;
459 case CHR_EVENT_FOCUS
:
461 case CHR_EVENT_OPENED
:
462 if (!s
->dev
.attached
) {
463 usb_device_attach(&s
->dev
);
466 case CHR_EVENT_CLOSED
:
467 if (s
->dev
.attached
) {
468 usb_device_detach(&s
->dev
);
474 static int usb_serial_initfn(USBDevice
*dev
)
476 USBSerialState
*s
= DO_UPCAST(USBSerialState
, dev
, dev
);
478 usb_desc_create_serial(dev
);
480 dev
->auto_attach
= 0;
483 error_report("Property chardev is required");
487 qemu_chr_add_handlers(s
->cs
, usb_serial_can_read
, usb_serial_read
,
488 usb_serial_event
, s
);
489 usb_serial_handle_reset(dev
);
491 if (s
->cs
->be_open
&& !dev
->attached
) {
492 usb_device_attach(dev
);
497 static USBDevice
*usb_serial_init(USBBus
*bus
, const char *filename
)
500 CharDriverState
*cdrv
;
501 uint32_t vendorid
= 0, productid
= 0;
505 while (*filename
&& *filename
!= ':') {
508 if (strstart(filename
, "vendorid=", &p
)) {
509 vendorid
= strtol(p
, &e
, 16);
510 if (e
== p
|| (*e
&& *e
!= ',' && *e
!= ':')) {
511 error_report("bogus vendor ID %s", p
);
515 } else if (strstart(filename
, "productid=", &p
)) {
516 productid
= strtol(p
, &e
, 16);
517 if (e
== p
|| (*e
&& *e
!= ',' && *e
!= ':')) {
518 error_report("bogus product ID %s", p
);
523 error_report("unrecognized serial USB option %s", filename
);
526 while(*filename
== ',')
530 error_report("character device specification needed");
535 snprintf(label
, sizeof(label
), "usbserial%d", index
++);
536 cdrv
= qemu_chr_new(label
, filename
, NULL
);
540 dev
= usb_create(bus
, "usb-serial");
544 qdev_prop_set_chr(&dev
->qdev
, "chardev", cdrv
);
546 qdev_prop_set_uint16(&dev
->qdev
, "vendorid", vendorid
);
548 qdev_prop_set_uint16(&dev
->qdev
, "productid", productid
);
549 qdev_init_nofail(&dev
->qdev
);
554 static USBDevice
*usb_braille_init(USBBus
*bus
, const char *unused
)
557 CharDriverState
*cdrv
;
559 cdrv
= qemu_chr_new("braille", "braille", NULL
);
563 dev
= usb_create(bus
, "usb-braille");
564 qdev_prop_set_chr(&dev
->qdev
, "chardev", cdrv
);
565 qdev_init_nofail(&dev
->qdev
);
570 static const VMStateDescription vmstate_usb_serial
= {
571 .name
= "usb-serial",
575 static Property serial_properties
[] = {
576 DEFINE_PROP_CHR("chardev", USBSerialState
, cs
),
577 DEFINE_PROP_END_OF_LIST(),
580 static void usb_serial_class_initfn(ObjectClass
*klass
, void *data
)
582 DeviceClass
*dc
= DEVICE_CLASS(klass
);
583 USBDeviceClass
*uc
= USB_DEVICE_CLASS(klass
);
585 uc
->init
= usb_serial_initfn
;
586 uc
->product_desc
= "QEMU USB Serial";
587 uc
->usb_desc
= &desc_serial
;
588 uc
->handle_reset
= usb_serial_handle_reset
;
589 uc
->handle_control
= usb_serial_handle_control
;
590 uc
->handle_data
= usb_serial_handle_data
;
591 dc
->vmsd
= &vmstate_usb_serial
;
592 dc
->props
= serial_properties
;
595 static const TypeInfo serial_info
= {
596 .name
= "usb-serial",
597 .parent
= TYPE_USB_DEVICE
,
598 .instance_size
= sizeof(USBSerialState
),
599 .class_init
= usb_serial_class_initfn
,
602 static Property braille_properties
[] = {
603 DEFINE_PROP_CHR("chardev", USBSerialState
, cs
),
604 DEFINE_PROP_END_OF_LIST(),
607 static void usb_braille_class_initfn(ObjectClass
*klass
, void *data
)
609 DeviceClass
*dc
= DEVICE_CLASS(klass
);
610 USBDeviceClass
*uc
= USB_DEVICE_CLASS(klass
);
612 uc
->init
= usb_serial_initfn
;
613 uc
->product_desc
= "QEMU USB Braille";
614 uc
->usb_desc
= &desc_braille
;
615 uc
->handle_reset
= usb_serial_handle_reset
;
616 uc
->handle_control
= usb_serial_handle_control
;
617 uc
->handle_data
= usb_serial_handle_data
;
618 dc
->vmsd
= &vmstate_usb_serial
;
619 dc
->props
= braille_properties
;
622 static const TypeInfo braille_info
= {
623 .name
= "usb-braille",
624 .parent
= TYPE_USB_DEVICE
,
625 .instance_size
= sizeof(USBSerialState
),
626 .class_init
= usb_braille_class_initfn
,
629 static void usb_serial_register_types(void)
631 type_register_static(&serial_info
);
632 usb_legacy_register("usb-serial", "serial", usb_serial_init
);
633 type_register_static(&braille_info
);
634 usb_legacy_register("usb-braille", "braille", usb_braille_init
);
637 type_init(usb_serial_register_types
)