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 licenced under the LGPL.
11 #include "qemu-common.h"
12 #include "qemu-error.h"
15 #include "qemu-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 " QEMU_VERSION
,
115 [STR_PRODUCT_SERIAL
] = "QEMU USB SERIAL",
116 [STR_PRODUCT_BRAILLE
] = "QEMU USB 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,
154 static const USBDesc desc_serial
= {
159 .iManufacturer
= STR_MANUFACTURER
,
160 .iProduct
= STR_PRODUCT_SERIAL
,
161 .iSerialNumber
= STR_SERIALNUMBER
,
163 .full
= &desc_device
,
167 static const USBDesc desc_braille
= {
172 .iManufacturer
= STR_MANUFACTURER
,
173 .iProduct
= STR_PRODUCT_BRAILLE
,
174 .iSerialNumber
= STR_SERIALNUMBER
,
176 .full
= &desc_device
,
180 static void usb_serial_reset(USBSerialState
*s
)
182 /* TODO: Set flow control to none */
184 s
->event_trigger
= 0;
187 /* TODO: purge in char driver */
190 static void usb_serial_handle_reset(USBDevice
*dev
)
192 USBSerialState
*s
= (USBSerialState
*)dev
;
197 /* TODO: Reset char device, send BREAK? */
200 static uint8_t usb_get_modem_lines(USBSerialState
*s
)
205 if (qemu_chr_ioctl(s
->cs
, CHR_IOCTL_SERIAL_GET_TIOCM
, &flags
) == -ENOTSUP
)
206 return FTDI_CTS
|FTDI_DSR
|FTDI_RLSD
;
209 if (flags
& CHR_TIOCM_CTS
)
211 if (flags
& CHR_TIOCM_DSR
)
213 if (flags
& CHR_TIOCM_RI
)
215 if (flags
& CHR_TIOCM_CAR
)
221 static int usb_serial_handle_control(USBDevice
*dev
, int request
, int value
,
222 int index
, int length
, uint8_t *data
)
224 USBSerialState
*s
= (USBSerialState
*)dev
;
227 DPRINTF("got control %x, value %x\n",request
, value
);
228 ret
= usb_desc_handle_control(dev
, request
, value
, index
, length
, data
);
235 case DeviceRequest
| USB_REQ_GET_INTERFACE
:
239 case InterfaceOutRequest
| USB_REQ_SET_INTERFACE
:
242 case EndpointOutRequest
| USB_REQ_CLEAR_FEATURE
:
246 /* Class specific requests. */
247 case DeviceOutVendor
| FTDI_RESET
:
255 /* TODO: purge from char device */
258 /* TODO: purge from char device */
262 case DeviceOutVendor
| FTDI_SET_MDM_CTRL
:
265 qemu_chr_ioctl(s
->cs
,CHR_IOCTL_SERIAL_GET_TIOCM
, &flags
);
266 if (value
& FTDI_SET_RTS
) {
267 if (value
& FTDI_RTS
)
268 flags
|= CHR_TIOCM_RTS
;
270 flags
&= ~CHR_TIOCM_RTS
;
272 if (value
& FTDI_SET_DTR
) {
273 if (value
& FTDI_DTR
)
274 flags
|= CHR_TIOCM_DTR
;
276 flags
&= ~CHR_TIOCM_DTR
;
278 qemu_chr_ioctl(s
->cs
,CHR_IOCTL_SERIAL_SET_TIOCM
, &flags
);
281 case DeviceOutVendor
| FTDI_SET_FLOW_CTRL
:
284 case DeviceOutVendor
| FTDI_SET_BAUD
: {
285 static const int subdivisors8
[8] = { 0, 4, 2, 1, 3, 5, 6, 7 };
286 int subdivisor8
= subdivisors8
[((value
& 0xc000) >> 14)
287 | ((index
& 1) << 2)];
288 int divisor
= value
& 0x3fff;
290 /* chip special cases */
291 if (divisor
== 1 && subdivisor8
== 0)
293 if (divisor
== 0 && subdivisor8
== 0)
296 s
->params
.speed
= (48000000 / 2) / (8 * divisor
+ subdivisor8
);
297 qemu_chr_ioctl(s
->cs
, CHR_IOCTL_SERIAL_SET_PARAMS
, &s
->params
);
300 case DeviceOutVendor
| FTDI_SET_DATA
:
301 switch (value
& FTDI_PARITY
) {
303 s
->params
.parity
= 'N';
306 s
->params
.parity
= 'O';
309 s
->params
.parity
= 'E';
312 DPRINTF("unsupported parity %d\n", value
& FTDI_PARITY
);
315 switch (value
& FTDI_STOP
) {
317 s
->params
.stop_bits
= 1;
320 s
->params
.stop_bits
= 2;
323 DPRINTF("unsupported stop bits %d\n", value
& FTDI_STOP
);
326 qemu_chr_ioctl(s
->cs
, CHR_IOCTL_SERIAL_SET_PARAMS
, &s
->params
);
327 /* TODO: TX ON/OFF */
329 case DeviceInVendor
| FTDI_GET_MDM_ST
:
330 data
[0] = usb_get_modem_lines(s
) | 1;
334 case DeviceOutVendor
| FTDI_SET_EVENT_CHR
:
335 /* TODO: handle it */
336 s
->event_chr
= value
;
338 case DeviceOutVendor
| FTDI_SET_ERROR_CHR
:
339 /* TODO: handle it */
340 s
->error_chr
= value
;
342 case DeviceOutVendor
| FTDI_SET_LATENCY
:
345 case DeviceInVendor
| FTDI_GET_LATENCY
:
346 data
[0] = s
->latency
;
351 DPRINTF("got unsupported/bogus control %x, value %x\n", request
, value
);
358 static int usb_serial_handle_data(USBDevice
*dev
, USBPacket
*p
)
360 USBSerialState
*s
= (USBSerialState
*)dev
;
362 uint8_t devep
= p
->devep
;
363 uint8_t *data
= p
->data
;
371 qemu_chr_write(s
->cs
, data
, len
);
377 first_len
= RECV_BUF
- s
->recv_ptr
;
382 *data
++ = 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
;
394 if (len
> s
->recv_used
)
402 memcpy(data
, s
->recv_buf
+ s
->recv_ptr
, first_len
);
404 memcpy(data
+ first_len
, s
->recv_buf
, len
- first_len
);
406 s
->recv_ptr
= (s
->recv_ptr
+ len
) % RECV_BUF
;
411 DPRINTF("Bad token\n");
420 static void usb_serial_handle_destroy(USBDevice
*dev
)
422 USBSerialState
*s
= (USBSerialState
*)dev
;
424 qemu_chr_close(s
->cs
);
427 static int usb_serial_can_read(void *opaque
)
429 USBSerialState
*s
= opaque
;
430 return RECV_BUF
- s
->recv_used
;
433 static void usb_serial_read(void *opaque
, const uint8_t *buf
, int size
)
435 USBSerialState
*s
= opaque
;
436 int first_size
, start
;
438 /* room in the buffer? */
439 if (size
> (RECV_BUF
- s
->recv_used
))
440 size
= RECV_BUF
- s
->recv_used
;
442 start
= s
->recv_ptr
+ s
->recv_used
;
443 if (start
< RECV_BUF
) {
444 /* copy data to end of buffer */
445 first_size
= RECV_BUF
- start
;
446 if (first_size
> size
)
449 memcpy(s
->recv_buf
+ start
, buf
, first_size
);
451 /* wrap around to front if needed */
452 if (size
> first_size
)
453 memcpy(s
->recv_buf
, buf
+ first_size
, size
- first_size
);
456 memcpy(s
->recv_buf
+ start
, buf
, size
);
458 s
->recv_used
+= size
;
461 static void usb_serial_event(void *opaque
, int event
)
463 USBSerialState
*s
= opaque
;
466 case CHR_EVENT_BREAK
:
467 s
->event_trigger
|= FTDI_BI
;
469 case CHR_EVENT_FOCUS
:
471 case CHR_EVENT_OPENED
:
473 /* TODO: Reset USB port */
478 static int usb_serial_initfn(USBDevice
*dev
)
480 USBSerialState
*s
= DO_UPCAST(USBSerialState
, dev
, dev
);
485 error_report("Property chardev is required");
489 qemu_chr_add_handlers(s
->cs
, usb_serial_can_read
, usb_serial_read
,
490 usb_serial_event
, s
);
491 usb_serial_handle_reset(dev
);
495 static USBDevice
*usb_serial_init(const char *filename
)
498 CharDriverState
*cdrv
;
499 uint32_t vendorid
= 0, productid
= 0;
503 while (*filename
&& *filename
!= ':') {
506 if (strstart(filename
, "vendorid=", &p
)) {
507 vendorid
= strtol(p
, &e
, 16);
508 if (e
== p
|| (*e
&& *e
!= ',' && *e
!= ':')) {
509 error_report("bogus vendor ID %s", p
);
513 } else if (strstart(filename
, "productid=", &p
)) {
514 productid
= strtol(p
, &e
, 16);
515 if (e
== p
|| (*e
&& *e
!= ',' && *e
!= ':')) {
516 error_report("bogus product ID %s", p
);
521 error_report("unrecognized serial USB option %s", filename
);
524 while(*filename
== ',')
528 error_report("character device specification needed");
533 snprintf(label
, sizeof(label
), "usbserial%d", index
++);
534 cdrv
= qemu_chr_open(label
, filename
, NULL
);
538 dev
= usb_create(NULL
/* FIXME */, "usb-serial");
542 qdev_prop_set_chr(&dev
->qdev
, "chardev", cdrv
);
544 qdev_prop_set_uint16(&dev
->qdev
, "vendorid", vendorid
);
546 qdev_prop_set_uint16(&dev
->qdev
, "productid", productid
);
547 qdev_init_nofail(&dev
->qdev
);
552 static USBDevice
*usb_braille_init(const char *unused
)
555 CharDriverState
*cdrv
;
557 cdrv
= qemu_chr_open("braille", "braille", NULL
);
561 dev
= usb_create(NULL
/* FIXME */, "usb-braille");
562 qdev_prop_set_chr(&dev
->qdev
, "chardev", cdrv
);
563 qdev_init_nofail(&dev
->qdev
);
568 static struct USBDeviceInfo serial_info
= {
569 .product_desc
= "QEMU USB Serial",
570 .qdev
.name
= "usb-serial",
571 .qdev
.size
= sizeof(USBSerialState
),
572 .usb_desc
= &desc_serial
,
573 .init
= usb_serial_initfn
,
574 .handle_packet
= usb_generic_handle_packet
,
575 .handle_reset
= usb_serial_handle_reset
,
576 .handle_control
= usb_serial_handle_control
,
577 .handle_data
= usb_serial_handle_data
,
578 .handle_destroy
= usb_serial_handle_destroy
,
579 .usbdevice_name
= "serial",
580 .usbdevice_init
= usb_serial_init
,
581 .qdev
.props
= (Property
[]) {
582 DEFINE_PROP_CHR("chardev", USBSerialState
, cs
),
583 DEFINE_PROP_END_OF_LIST(),
587 static struct USBDeviceInfo braille_info
= {
588 .product_desc
= "QEMU USB Braille",
589 .qdev
.name
= "usb-braille",
590 .qdev
.size
= sizeof(USBSerialState
),
591 .usb_desc
= &desc_braille
,
592 .init
= usb_serial_initfn
,
593 .handle_packet
= usb_generic_handle_packet
,
594 .handle_reset
= usb_serial_handle_reset
,
595 .handle_control
= usb_serial_handle_control
,
596 .handle_data
= usb_serial_handle_data
,
597 .handle_destroy
= usb_serial_handle_destroy
,
598 .usbdevice_name
= "braille",
599 .usbdevice_init
= usb_braille_init
,
600 .qdev
.props
= (Property
[]) {
601 DEFINE_PROP_CHR("chardev", USBSerialState
, cs
),
602 DEFINE_PROP_END_OF_LIST(),
606 static void usb_serial_register_devices(void)
608 usb_qdev_register(&serial_info
);
609 usb_qdev_register(&braille_info
);
611 device_init(usb_serial_register_devices
)