dev-serial: style changes to improve readability and checkpatch fixes
[qemu/kevin.git] / hw / usb / dev-serial.c
blob7a5fa3770e7fafea06199424598319973836b7a4
1 /*
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.
9 */
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"
17 #include "hw/usb.h"
18 #include "migration/vmstate.h"
19 #include "desc.h"
20 #include "chardev/char-serial.h"
21 #include "chardev/char-fe.h"
22 #include "qom/object.h"
24 //#define DEBUG_Serial
26 #ifdef DEBUG_Serial
27 #define DPRINTF(fmt, ...) \
28 do { printf("usb-serial: " fmt , ## __VA_ARGS__); } while (0)
29 #else
30 #define DPRINTF(fmt, ...) do {} while(0)
31 #endif
33 #define RECV_BUF (512 - (2 * 8))
35 /* Commands */
36 #define FTDI_RESET 0
37 #define FTDI_SET_MDM_CTRL 1
38 #define FTDI_SET_FLOW_CTRL 2
39 #define FTDI_SET_BAUD 3
40 #define FTDI_SET_DATA 4
41 #define FTDI_GET_MDM_ST 5
42 #define FTDI_SET_EVENT_CHR 6
43 #define FTDI_SET_ERROR_CHR 7
44 #define FTDI_SET_LATENCY 9
45 #define FTDI_GET_LATENCY 10
47 #define DeviceOutVendor \
48 ((USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE) << 8)
49 #define DeviceInVendor \
50 ((USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE) << 8)
52 /* RESET */
54 #define FTDI_RESET_SIO 0
55 #define FTDI_RESET_RX 1
56 #define FTDI_RESET_TX 2
58 /* SET_MDM_CTRL */
60 #define FTDI_DTR 1
61 #define FTDI_SET_DTR (FTDI_DTR << 8)
62 #define FTDI_RTS 2
63 #define FTDI_SET_RTS (FTDI_RTS << 8)
65 /* SET_FLOW_CTRL */
67 #define FTDI_RTS_CTS_HS 1
68 #define FTDI_DTR_DSR_HS 2
69 #define FTDI_XON_XOFF_HS 4
71 /* SET_DATA */
73 #define FTDI_PARITY (0x7 << 8)
74 #define FTDI_ODD (0x1 << 8)
75 #define FTDI_EVEN (0x2 << 8)
76 #define FTDI_MARK (0x3 << 8)
77 #define FTDI_SPACE (0x4 << 8)
79 #define FTDI_STOP (0x3 << 11)
80 #define FTDI_STOP1 (0x0 << 11)
81 #define FTDI_STOP15 (0x1 << 11)
82 #define FTDI_STOP2 (0x2 << 11)
84 /* GET_MDM_ST */
85 /* TODO: should be sent every 40ms */
86 #define FTDI_CTS (1 << 4) /* CTS line status */
87 #define FTDI_DSR (1 << 5) /* DSR line status */
88 #define FTDI_RI (1 << 6) /* RI line status */
89 #define FTDI_RLSD (1 << 7) /* Receive Line Signal Detect */
91 /* Status */
93 #define FTDI_DR (1 << 0) /* Data Ready */
94 #define FTDI_OE (1 << 1) /* Overrun Err */
95 #define FTDI_PE (1 << 2) /* Parity Err */
96 #define FTDI_FE (1 << 3) /* Framing Err */
97 #define FTDI_BI (1 << 4) /* Break Interrupt */
98 #define FTDI_THRE (1 << 5) /* Transmitter Holding Register */
99 #define FTDI_TEMT (1 << 6) /* Transmitter Empty */
100 #define FTDI_FIFO (1 << 7) /* Error in FIFO */
102 struct USBSerialState {
103 USBDevice dev;
105 USBEndpoint *intr;
106 uint8_t recv_buf[RECV_BUF];
107 uint16_t recv_ptr;
108 uint16_t recv_used;
109 uint8_t event_chr;
110 uint8_t error_chr;
111 uint8_t event_trigger;
112 QEMUSerialSetParams params;
113 int latency; /* ms */
114 CharBackend cs;
117 #define TYPE_USB_SERIAL "usb-serial-dev"
118 OBJECT_DECLARE_SIMPLE_TYPE(USBSerialState, USB_SERIAL)
120 enum {
121 STR_MANUFACTURER = 1,
122 STR_PRODUCT_SERIAL,
123 STR_PRODUCT_BRAILLE,
124 STR_SERIALNUMBER,
127 static const USBDescStrings desc_strings = {
128 [STR_MANUFACTURER] = "QEMU",
129 [STR_PRODUCT_SERIAL] = "QEMU USB SERIAL",
130 [STR_PRODUCT_BRAILLE] = "QEMU USB BAUM BRAILLE",
131 [STR_SERIALNUMBER] = "1",
134 static const USBDescIface desc_iface0 = {
135 .bInterfaceNumber = 0,
136 .bNumEndpoints = 2,
137 .bInterfaceClass = 0xff,
138 .bInterfaceSubClass = 0xff,
139 .bInterfaceProtocol = 0xff,
140 .eps = (USBDescEndpoint[]) {
142 .bEndpointAddress = USB_DIR_IN | 0x01,
143 .bmAttributes = USB_ENDPOINT_XFER_BULK,
144 .wMaxPacketSize = 64,
146 .bEndpointAddress = USB_DIR_OUT | 0x02,
147 .bmAttributes = USB_ENDPOINT_XFER_BULK,
148 .wMaxPacketSize = 64,
153 static const USBDescDevice desc_device = {
154 .bcdUSB = 0x0200,
155 .bMaxPacketSize0 = 8,
156 .bNumConfigurations = 1,
157 .confs = (USBDescConfig[]) {
159 .bNumInterfaces = 1,
160 .bConfigurationValue = 1,
161 .bmAttributes = USB_CFG_ATT_ONE | USB_CFG_ATT_WAKEUP,
162 .bMaxPower = 50,
163 .nif = 1,
164 .ifs = &desc_iface0,
169 static const USBDesc desc_serial = {
170 .id = {
171 .idVendor = 0x0403,
172 .idProduct = 0x6001,
173 .bcdDevice = 0x0400,
174 .iManufacturer = STR_MANUFACTURER,
175 .iProduct = STR_PRODUCT_SERIAL,
176 .iSerialNumber = STR_SERIALNUMBER,
178 .full = &desc_device,
179 .str = desc_strings,
182 static const USBDesc desc_braille = {
183 .id = {
184 .idVendor = 0x0403,
185 .idProduct = 0xfe72,
186 .bcdDevice = 0x0400,
187 .iManufacturer = STR_MANUFACTURER,
188 .iProduct = STR_PRODUCT_BRAILLE,
189 .iSerialNumber = STR_SERIALNUMBER,
191 .full = &desc_device,
192 .str = desc_strings,
195 static void usb_serial_reset(USBSerialState *s)
197 /* TODO: Set flow control to none */
198 s->event_chr = 0x0d;
199 s->event_trigger = 0;
200 s->recv_ptr = 0;
201 s->recv_used = 0;
202 /* TODO: purge in char driver */
205 static void usb_serial_handle_reset(USBDevice *dev)
207 USBSerialState *s = (USBSerialState *)dev;
209 DPRINTF("Reset\n");
211 usb_serial_reset(s);
212 /* TODO: Reset char device, send BREAK? */
215 static uint8_t usb_get_modem_lines(USBSerialState *s)
217 int flags;
218 uint8_t ret;
220 if (qemu_chr_fe_ioctl(&s->cs,
221 CHR_IOCTL_SERIAL_GET_TIOCM, &flags) == -ENOTSUP) {
222 return FTDI_CTS | FTDI_DSR | FTDI_RLSD;
225 ret = 0;
226 if (flags & CHR_TIOCM_CTS) {
227 ret |= FTDI_CTS;
229 if (flags & CHR_TIOCM_DSR) {
230 ret |= FTDI_DSR;
232 if (flags & CHR_TIOCM_RI) {
233 ret |= FTDI_RI;
235 if (flags & CHR_TIOCM_CAR) {
236 ret |= FTDI_RLSD;
239 return ret;
242 static void usb_serial_handle_control(USBDevice *dev, USBPacket *p,
243 int request, int value, int index,
244 int length, uint8_t *data)
246 USBSerialState *s = (USBSerialState *)dev;
247 int ret;
249 DPRINTF("got control %x, value %x\n", request, value);
250 ret = usb_desc_handle_control(dev, p, request, value, index, length, data);
251 if (ret >= 0) {
252 return;
255 switch (request) {
256 case EndpointOutRequest | USB_REQ_CLEAR_FEATURE:
257 break;
259 /* Class specific requests. */
260 case DeviceOutVendor | FTDI_RESET:
261 switch (value) {
262 case FTDI_RESET_SIO:
263 usb_serial_reset(s);
264 break;
265 case FTDI_RESET_RX:
266 s->recv_ptr = 0;
267 s->recv_used = 0;
268 /* TODO: purge from char device */
269 break;
270 case FTDI_RESET_TX:
271 /* TODO: purge from char device */
272 break;
274 break;
275 case DeviceOutVendor | FTDI_SET_MDM_CTRL:
277 static int flags;
278 qemu_chr_fe_ioctl(&s->cs, CHR_IOCTL_SERIAL_GET_TIOCM, &flags);
279 if (value & FTDI_SET_RTS) {
280 if (value & FTDI_RTS) {
281 flags |= CHR_TIOCM_RTS;
282 } else {
283 flags &= ~CHR_TIOCM_RTS;
286 if (value & FTDI_SET_DTR) {
287 if (value & FTDI_DTR) {
288 flags |= CHR_TIOCM_DTR;
289 } else {
290 flags &= ~CHR_TIOCM_DTR;
293 qemu_chr_fe_ioctl(&s->cs, CHR_IOCTL_SERIAL_SET_TIOCM, &flags);
294 break;
296 case DeviceOutVendor | FTDI_SET_FLOW_CTRL:
297 /* TODO: ioctl */
298 break;
299 case DeviceOutVendor | FTDI_SET_BAUD: {
300 static const int subdivisors8[8] = { 0, 4, 2, 1, 3, 5, 6, 7 };
301 int subdivisor8 = subdivisors8[((value & 0xc000) >> 14)
302 | ((index & 1) << 2)];
303 int divisor = value & 0x3fff;
305 /* chip special cases */
306 if (divisor == 1 && subdivisor8 == 0) {
307 subdivisor8 = 4;
309 if (divisor == 0 && subdivisor8 == 0) {
310 divisor = 1;
313 s->params.speed = (48000000 / 2) / (8 * divisor + subdivisor8);
314 qemu_chr_fe_ioctl(&s->cs, CHR_IOCTL_SERIAL_SET_PARAMS, &s->params);
315 break;
317 case DeviceOutVendor | FTDI_SET_DATA:
318 switch (value & FTDI_PARITY) {
319 case 0:
320 s->params.parity = 'N';
321 break;
322 case FTDI_ODD:
323 s->params.parity = 'O';
324 break;
325 case FTDI_EVEN:
326 s->params.parity = 'E';
327 break;
328 default:
329 DPRINTF("unsupported parity %d\n", value & FTDI_PARITY);
330 goto fail;
333 switch (value & FTDI_STOP) {
334 case FTDI_STOP1:
335 s->params.stop_bits = 1;
336 break;
337 case FTDI_STOP2:
338 s->params.stop_bits = 2;
339 break;
340 default:
341 DPRINTF("unsupported stop bits %d\n", value & FTDI_STOP);
342 goto fail;
345 qemu_chr_fe_ioctl(&s->cs, CHR_IOCTL_SERIAL_SET_PARAMS, &s->params);
346 /* TODO: TX ON/OFF */
347 break;
348 case DeviceInVendor | FTDI_GET_MDM_ST:
349 data[0] = usb_get_modem_lines(s) | 1;
350 data[1] = FTDI_THRE | FTDI_TEMT;
351 p->actual_length = 2;
352 break;
353 case DeviceOutVendor | FTDI_SET_EVENT_CHR:
354 /* TODO: handle it */
355 s->event_chr = value;
356 break;
357 case DeviceOutVendor | FTDI_SET_ERROR_CHR:
358 /* TODO: handle it */
359 s->error_chr = value;
360 break;
361 case DeviceOutVendor | FTDI_SET_LATENCY:
362 s->latency = value;
363 break;
364 case DeviceInVendor | FTDI_GET_LATENCY:
365 data[0] = s->latency;
366 p->actual_length = 1;
367 break;
368 default:
369 fail:
370 DPRINTF("got unsupported/bogus control %x, value %x\n", request, value);
371 p->status = USB_RET_STALL;
372 break;
376 static void usb_serial_token_in(USBSerialState *s, USBPacket *p)
378 const int max_packet_size = desc_iface0.eps[0].wMaxPacketSize;
379 int packet_len;
380 uint8_t header[2];
382 packet_len = p->iov.size;
383 if (packet_len <= 2) {
384 p->status = USB_RET_NAK;
385 return;
388 header[0] = usb_get_modem_lines(s) | 1;
389 /* We do not have the uart details */
390 /* handle serial break */
391 if (s->event_trigger && s->event_trigger & FTDI_BI) {
392 s->event_trigger &= ~FTDI_BI;
393 header[1] = FTDI_BI;
394 usb_packet_copy(p, header, 2);
395 return;
396 } else {
397 header[1] = 0;
400 if (!s->recv_used) {
401 p->status = USB_RET_NAK;
402 return;
405 while (s->recv_used && packet_len > 2) {
406 int first_len, len;
408 len = MIN(packet_len, max_packet_size);
409 len -= 2;
410 if (len > s->recv_used) {
411 len = s->recv_used;
414 first_len = RECV_BUF - s->recv_ptr;
415 if (first_len > len) {
416 first_len = len;
418 usb_packet_copy(p, header, 2);
419 usb_packet_copy(p, s->recv_buf + s->recv_ptr, first_len);
420 if (len > first_len) {
421 usb_packet_copy(p, s->recv_buf, len - first_len);
423 s->recv_used -= len;
424 s->recv_ptr = (s->recv_ptr + len) % RECV_BUF;
425 packet_len -= len + 2;
428 return;
431 static void usb_serial_handle_data(USBDevice *dev, USBPacket *p)
433 USBSerialState *s = (USBSerialState *)dev;
434 uint8_t devep = p->ep->nr;
435 struct iovec *iov;
436 int i;
438 switch (p->pid) {
439 case USB_TOKEN_OUT:
440 if (devep != 2) {
441 goto fail;
443 for (i = 0; i < p->iov.niov; i++) {
444 iov = p->iov.iov + i;
446 * XXX this blocks entire thread. Rewrite to use
447 * qemu_chr_fe_write and background I/O callbacks
449 qemu_chr_fe_write_all(&s->cs, iov->iov_base, iov->iov_len);
451 p->actual_length = p->iov.size;
452 break;
454 case USB_TOKEN_IN:
455 if (devep != 1) {
456 goto fail;
458 usb_serial_token_in(s, p);
459 break;
461 default:
462 DPRINTF("Bad token\n");
463 fail:
464 p->status = USB_RET_STALL;
465 break;
469 static int usb_serial_can_read(void *opaque)
471 USBSerialState *s = opaque;
473 if (!s->dev.attached) {
474 return 0;
476 return RECV_BUF - s->recv_used;
479 static void usb_serial_read(void *opaque, const uint8_t *buf, int size)
481 USBSerialState *s = opaque;
482 int first_size, start;
484 /* room in the buffer? */
485 if (size > (RECV_BUF - s->recv_used)) {
486 size = RECV_BUF - s->recv_used;
489 start = s->recv_ptr + s->recv_used;
490 if (start < RECV_BUF) {
491 /* copy data to end of buffer */
492 first_size = RECV_BUF - start;
493 if (first_size > size) {
494 first_size = size;
497 memcpy(s->recv_buf + start, buf, first_size);
499 /* wrap around to front if needed */
500 if (size > first_size) {
501 memcpy(s->recv_buf, buf + first_size, size - first_size);
503 } else {
504 start -= RECV_BUF;
505 memcpy(s->recv_buf + start, buf, size);
507 s->recv_used += size;
509 usb_wakeup(s->intr, 0);
512 static void usb_serial_event(void *opaque, QEMUChrEvent event)
514 USBSerialState *s = opaque;
516 switch (event) {
517 case CHR_EVENT_BREAK:
518 s->event_trigger |= FTDI_BI;
519 break;
520 case CHR_EVENT_OPENED:
521 if (!s->dev.attached) {
522 usb_device_attach(&s->dev, &error_abort);
524 break;
525 case CHR_EVENT_CLOSED:
526 if (s->dev.attached) {
527 usb_device_detach(&s->dev);
529 break;
530 case CHR_EVENT_MUX_IN:
531 case CHR_EVENT_MUX_OUT:
532 /* Ignore */
533 break;
537 static void usb_serial_realize(USBDevice *dev, Error **errp)
539 USBSerialState *s = USB_SERIAL(dev);
540 Error *local_err = NULL;
542 usb_desc_create_serial(dev);
543 usb_desc_init(dev);
544 dev->auto_attach = 0;
546 if (!qemu_chr_fe_backend_connected(&s->cs)) {
547 error_setg(errp, "Property chardev is required");
548 return;
551 usb_check_attach(dev, &local_err);
552 if (local_err) {
553 error_propagate(errp, local_err);
554 return;
557 qemu_chr_fe_set_handlers(&s->cs, usb_serial_can_read, usb_serial_read,
558 usb_serial_event, NULL, s, NULL, true);
559 usb_serial_handle_reset(dev);
561 if (qemu_chr_fe_backend_open(&s->cs) && !dev->attached) {
562 usb_device_attach(dev, &error_abort);
564 s->intr = usb_ep_get(dev, USB_TOKEN_IN, 1);
567 static USBDevice *usb_braille_init(const char *unused)
569 USBDevice *dev;
570 Chardev *cdrv;
572 cdrv = qemu_chr_new("braille", "braille", NULL);
573 if (!cdrv) {
574 return NULL;
577 dev = usb_new("usb-braille");
578 qdev_prop_set_chr(&dev->qdev, "chardev", cdrv);
579 return dev;
582 static const VMStateDescription vmstate_usb_serial = {
583 .name = "usb-serial",
584 .unmigratable = 1,
587 static Property serial_properties[] = {
588 DEFINE_PROP_CHR("chardev", USBSerialState, cs),
589 DEFINE_PROP_END_OF_LIST(),
592 static void usb_serial_dev_class_init(ObjectClass *klass, void *data)
594 DeviceClass *dc = DEVICE_CLASS(klass);
595 USBDeviceClass *uc = USB_DEVICE_CLASS(klass);
597 uc->realize = usb_serial_realize;
598 uc->handle_reset = usb_serial_handle_reset;
599 uc->handle_control = usb_serial_handle_control;
600 uc->handle_data = usb_serial_handle_data;
601 dc->vmsd = &vmstate_usb_serial;
602 set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
605 static const TypeInfo usb_serial_dev_type_info = {
606 .name = TYPE_USB_SERIAL,
607 .parent = TYPE_USB_DEVICE,
608 .instance_size = sizeof(USBSerialState),
609 .abstract = true,
610 .class_init = usb_serial_dev_class_init,
613 static void usb_serial_class_initfn(ObjectClass *klass, void *data)
615 DeviceClass *dc = DEVICE_CLASS(klass);
616 USBDeviceClass *uc = USB_DEVICE_CLASS(klass);
618 uc->product_desc = "QEMU USB Serial";
619 uc->usb_desc = &desc_serial;
620 device_class_set_props(dc, serial_properties);
623 static const TypeInfo serial_info = {
624 .name = "usb-serial",
625 .parent = TYPE_USB_SERIAL,
626 .class_init = usb_serial_class_initfn,
629 static Property braille_properties[] = {
630 DEFINE_PROP_CHR("chardev", USBSerialState, cs),
631 DEFINE_PROP_END_OF_LIST(),
634 static void usb_braille_class_initfn(ObjectClass *klass, void *data)
636 DeviceClass *dc = DEVICE_CLASS(klass);
637 USBDeviceClass *uc = USB_DEVICE_CLASS(klass);
639 uc->product_desc = "QEMU USB Braille";
640 uc->usb_desc = &desc_braille;
641 device_class_set_props(dc, braille_properties);
644 static const TypeInfo braille_info = {
645 .name = "usb-braille",
646 .parent = TYPE_USB_SERIAL,
647 .class_init = usb_braille_class_initfn,
650 static void usb_serial_register_types(void)
652 type_register_static(&usb_serial_dev_type_info);
653 type_register_static(&serial_info);
654 type_register_static(&braille_info);
655 usb_legacy_register("usb-braille", "braille", usb_braille_init);
658 type_init(usb_serial_register_types)