fix event fallout in sh_serial.c
[qemu/aliguori.git] / hw / usb-serial.c
bloba74d813231e445de9c35c4e6faf92a740856a307
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-common.h"
12 #include "qemu-error.h"
13 #include "usb.h"
14 #include "usb-desc.h"
15 #include "qemu-char.h"
17 //#define DEBUG_Serial
19 #ifdef DEBUG_Serial
20 #define DPRINTF(fmt, ...) \
21 do { printf("usb-serial: " fmt , ## __VA_ARGS__); } while (0)
22 #else
23 #define DPRINTF(fmt, ...) do {} while(0)
24 #endif
26 #define RECV_BUF 384
28 /* Commands */
29 #define FTDI_RESET 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)
43 /* RESET */
45 #define FTDI_RESET_SIO 0
46 #define FTDI_RESET_RX 1
47 #define FTDI_RESET_TX 2
49 /* SET_MDM_CTRL */
51 #define FTDI_DTR 1
52 #define FTDI_SET_DTR (FTDI_DTR << 8)
53 #define FTDI_RTS 2
54 #define FTDI_SET_RTS (FTDI_RTS << 8)
56 /* SET_FLOW_CTRL */
58 #define FTDI_RTS_CTS_HS 1
59 #define FTDI_DTR_DSR_HS 2
60 #define FTDI_XON_XOFF_HS 4
62 /* SET_DATA */
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)
75 /* GET_MDM_ST */
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
82 /* Status */
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
93 typedef struct {
94 USBDevice dev;
95 uint8_t recv_buf[RECV_BUF];
96 uint16_t recv_ptr;
97 uint16_t recv_used;
98 uint8_t event_chr;
99 uint8_t error_chr;
100 uint8_t event_trigger;
101 QEMUSerialSetParams params;
102 int latency; /* ms */
103 CharDriverState *cs;
104 } USBSerialState;
106 enum {
107 STR_MANUFACTURER = 1,
108 STR_PRODUCT_SERIAL,
109 STR_PRODUCT_BRAILLE,
110 STR_SERIALNUMBER,
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,
122 .bNumEndpoints = 2,
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 = {
140 .bcdUSB = 0x0200,
141 .bMaxPacketSize0 = 8,
142 .bNumConfigurations = 1,
143 .confs = (USBDescConfig[]) {
145 .bNumInterfaces = 1,
146 .bConfigurationValue = 1,
147 .bmAttributes = 0x80,
148 .bMaxPower = 50,
149 .nif = 1,
150 .ifs = &desc_iface0,
155 static const USBDesc desc_serial = {
156 .id = {
157 .idVendor = 0x0403,
158 .idProduct = 0x6001,
159 .bcdDevice = 0x0400,
160 .iManufacturer = STR_MANUFACTURER,
161 .iProduct = STR_PRODUCT_SERIAL,
162 .iSerialNumber = STR_SERIALNUMBER,
164 .full = &desc_device,
165 .str = desc_strings,
168 static const USBDesc desc_braille = {
169 .id = {
170 .idVendor = 0x0403,
171 .idProduct = 0xfe72,
172 .bcdDevice = 0x0400,
173 .iManufacturer = STR_MANUFACTURER,
174 .iProduct = STR_PRODUCT_BRAILLE,
175 .iSerialNumber = STR_SERIALNUMBER,
177 .full = &desc_device,
178 .str = desc_strings,
181 static void usb_serial_update_handlers(USBSerialState *s);
183 static void usb_serial_reset(USBSerialState *s)
185 /* TODO: Set flow control to none */
186 s->event_chr = 0x0d;
187 s->event_trigger = 0;
188 s->recv_ptr = 0;
189 s->recv_used = 0;
190 usb_serial_update_handlers(s);
191 /* TODO: purge in char driver */
194 static void usb_serial_handle_reset(USBDevice *dev)
196 USBSerialState *s = (USBSerialState *)dev;
198 DPRINTF("Reset\n");
200 usb_serial_reset(s);
201 /* TODO: Reset char device, send BREAK? */
204 static uint8_t usb_get_modem_lines(USBSerialState *s)
206 int flags;
207 uint8_t ret;
209 if (qemu_chr_fe_ioctl(s->cs, CHR_IOCTL_SERIAL_GET_TIOCM, &flags) == -ENOTSUP)
210 return FTDI_CTS|FTDI_DSR|FTDI_RLSD;
212 ret = 0;
213 if (flags & CHR_TIOCM_CTS)
214 ret |= FTDI_CTS;
215 if (flags & CHR_TIOCM_DSR)
216 ret |= FTDI_DSR;
217 if (flags & CHR_TIOCM_RI)
218 ret |= FTDI_RI;
219 if (flags & CHR_TIOCM_CAR)
220 ret |= FTDI_RLSD;
222 return ret;
225 static int usb_serial_handle_control(USBDevice *dev, USBPacket *p,
226 int request, int value, int index, int length, uint8_t *data)
228 USBSerialState *s = (USBSerialState *)dev;
229 int ret;
231 DPRINTF("got control %x, value %x\n",request, value);
232 ret = usb_desc_handle_control(dev, p, request, value, index, length, data);
233 if (ret >= 0) {
234 return ret;
237 ret = 0;
238 switch (request) {
239 case DeviceRequest | USB_REQ_GET_INTERFACE:
240 data[0] = 0;
241 ret = 1;
242 break;
243 case InterfaceOutRequest | USB_REQ_SET_INTERFACE:
244 ret = 0;
245 break;
246 case EndpointOutRequest | USB_REQ_CLEAR_FEATURE:
247 ret = 0;
248 break;
250 /* Class specific requests. */
251 case DeviceOutVendor | FTDI_RESET:
252 switch (value) {
253 case FTDI_RESET_SIO:
254 usb_serial_reset(s);
255 break;
256 case FTDI_RESET_RX:
257 s->recv_ptr = 0;
258 s->recv_used = 0;
259 /* TODO: purge from char device */
260 usb_serial_update_handlers(s);
261 break;
262 case FTDI_RESET_TX:
263 /* TODO: purge from char device */
264 break;
266 break;
267 case DeviceOutVendor | FTDI_SET_MDM_CTRL:
269 static int flags;
270 qemu_chr_fe_ioctl(s->cs,CHR_IOCTL_SERIAL_GET_TIOCM, &flags);
271 if (value & FTDI_SET_RTS) {
272 if (value & FTDI_RTS)
273 flags |= CHR_TIOCM_RTS;
274 else
275 flags &= ~CHR_TIOCM_RTS;
277 if (value & FTDI_SET_DTR) {
278 if (value & FTDI_DTR)
279 flags |= CHR_TIOCM_DTR;
280 else
281 flags &= ~CHR_TIOCM_DTR;
283 qemu_chr_fe_ioctl(s->cs,CHR_IOCTL_SERIAL_SET_TIOCM, &flags);
284 break;
286 case DeviceOutVendor | FTDI_SET_FLOW_CTRL:
287 /* TODO: ioctl */
288 break;
289 case DeviceOutVendor | FTDI_SET_BAUD: {
290 static const int subdivisors8[8] = { 0, 4, 2, 1, 3, 5, 6, 7 };
291 int subdivisor8 = subdivisors8[((value & 0xc000) >> 14)
292 | ((index & 1) << 2)];
293 int divisor = value & 0x3fff;
295 /* chip special cases */
296 if (divisor == 1 && subdivisor8 == 0)
297 subdivisor8 = 4;
298 if (divisor == 0 && subdivisor8 == 0)
299 divisor = 1;
301 s->params.speed = (48000000 / 2) / (8 * divisor + subdivisor8);
302 qemu_chr_fe_ioctl(s->cs, CHR_IOCTL_SERIAL_SET_PARAMS, &s->params);
303 break;
305 case DeviceOutVendor | FTDI_SET_DATA:
306 switch (value & FTDI_PARITY) {
307 case 0:
308 s->params.parity = 'N';
309 break;
310 case FTDI_ODD:
311 s->params.parity = 'O';
312 break;
313 case FTDI_EVEN:
314 s->params.parity = 'E';
315 break;
316 default:
317 DPRINTF("unsupported parity %d\n", value & FTDI_PARITY);
318 goto fail;
320 switch (value & FTDI_STOP) {
321 case FTDI_STOP1:
322 s->params.stop_bits = 1;
323 break;
324 case FTDI_STOP2:
325 s->params.stop_bits = 2;
326 break;
327 default:
328 DPRINTF("unsupported stop bits %d\n", value & FTDI_STOP);
329 goto fail;
331 qemu_chr_fe_ioctl(s->cs, CHR_IOCTL_SERIAL_SET_PARAMS, &s->params);
332 /* TODO: TX ON/OFF */
333 break;
334 case DeviceInVendor | FTDI_GET_MDM_ST:
335 data[0] = usb_get_modem_lines(s) | 1;
336 data[1] = 0;
337 ret = 2;
338 break;
339 case DeviceOutVendor | FTDI_SET_EVENT_CHR:
340 /* TODO: handle it */
341 s->event_chr = value;
342 break;
343 case DeviceOutVendor | FTDI_SET_ERROR_CHR:
344 /* TODO: handle it */
345 s->error_chr = value;
346 break;
347 case DeviceOutVendor | FTDI_SET_LATENCY:
348 s->latency = value;
349 break;
350 case DeviceInVendor | FTDI_GET_LATENCY:
351 data[0] = s->latency;
352 ret = 1;
353 break;
354 default:
355 fail:
356 DPRINTF("got unsupported/bogus control %x, value %x\n", request, value);
357 ret = USB_RET_STALL;
358 break;
360 return ret;
363 static int usb_serial_handle_data(USBDevice *dev, USBPacket *p)
365 USBSerialState *s = (USBSerialState *)dev;
366 int i, ret = 0;
367 uint8_t devep = p->devep;
368 struct iovec *iov;
369 uint8_t header[2];
370 int first_len, len;
372 switch (p->pid) {
373 case USB_TOKEN_OUT:
374 if (devep != 2)
375 goto fail;
376 for (i = 0; i < p->iov.niov; i++) {
377 iov = p->iov.iov + i;
378 qemu_chr_fe_write(s->cs, iov->iov_base, iov->iov_len);
380 break;
382 case USB_TOKEN_IN:
383 if (devep != 1)
384 goto fail;
385 first_len = RECV_BUF - s->recv_ptr;
386 len = p->iov.size;
387 if (len <= 2) {
388 ret = USB_RET_NAK;
389 break;
391 header[0] = usb_get_modem_lines(s) | 1;
392 /* We do not have the uart details */
393 /* handle serial break */
394 if (s->event_trigger && s->event_trigger & FTDI_BI) {
395 s->event_trigger &= ~FTDI_BI;
396 header[1] = FTDI_BI;
397 usb_packet_copy(p, header, 2);
398 ret = 2;
399 break;
400 } else {
401 header[1] = 0;
403 len -= 2;
404 if (len > s->recv_used)
405 len = s->recv_used;
406 if (!len) {
407 ret = USB_RET_NAK;
408 break;
410 if (first_len > len)
411 first_len = len;
412 usb_packet_copy(p, header, 2);
413 usb_packet_copy(p, s->recv_buf + s->recv_ptr, first_len);
414 if (len > first_len)
415 usb_packet_copy(p, s->recv_buf, len - first_len);
416 s->recv_used -= len;
417 usb_serial_update_handlers(s);
418 s->recv_ptr = (s->recv_ptr + len) % RECV_BUF;
419 ret = len + 2;
420 break;
422 default:
423 DPRINTF("Bad token\n");
424 fail:
425 ret = USB_RET_STALL;
426 break;
429 return ret;
432 static void usb_serial_handle_destroy(USBDevice *dev)
434 USBSerialState *s = (USBSerialState *)dev;
436 qemu_chr_fe_delete(s->cs);
439 static int usb_serial_can_read(void *opaque)
441 USBSerialState *s = opaque;
442 return RECV_BUF - s->recv_used;
445 static void usb_serial_read(void *opaque, const uint8_t *buf, int size)
447 USBSerialState *s = opaque;
448 int first_size, start;
450 /* room in the buffer? */
451 if (size > (RECV_BUF - s->recv_used))
452 size = RECV_BUF - s->recv_used;
454 start = s->recv_ptr + s->recv_used;
455 if (start < RECV_BUF) {
456 /* copy data to end of buffer */
457 first_size = RECV_BUF - start;
458 if (first_size > size)
459 first_size = size;
461 memcpy(s->recv_buf + start, buf, first_size);
463 /* wrap around to front if needed */
464 if (size > first_size)
465 memcpy(s->recv_buf, buf + first_size, size - first_size);
466 } else {
467 start -= RECV_BUF;
468 memcpy(s->recv_buf + start, buf, size);
470 s->recv_used += size;
471 usb_serial_update_handlers(s);
474 static void usb_serial_read_handler(void *opaque)
476 USBSerialState *s = opaque;
477 uint8_t buf[32];
478 int size;
480 size = usb_serial_can_read(s);
481 size = MIN(size, sizeof(buf));
483 size = qemu_chr_fe_read(s->cs, buf, size);
484 usb_serial_read(s, buf, size);
487 static int usb_serial_event(void *opaque, int event, void *data)
489 USBSerialState *s = opaque;
491 switch (event) {
492 case CHR_EVENT_BREAK:
493 s->event_trigger |= FTDI_BI;
494 break;
495 case CHR_EVENT_FOCUS:
496 break;
497 case CHR_EVENT_OPENED:
498 usb_serial_reset(s);
499 /* TODO: Reset USB port */
500 break;
503 return 0;
506 static void usb_serial_update_handlers(USBSerialState *s)
508 if (usb_serial_can_read(s) > 0) {
509 qemu_chr_fe_set_handlers(s->cs, usb_serial_read_handler,
510 NULL, usb_serial_event, s);
511 } else {
512 qemu_chr_fe_set_handlers(s->cs, NULL, NULL, usb_serial_event, s);
516 static int usb_serial_initfn(USBDevice *dev)
518 USBSerialState *s = DO_UPCAST(USBSerialState, dev, dev);
520 usb_desc_init(dev);
522 if (!s->cs) {
523 error_report("Property chardev is required");
524 return -1;
527 qemu_chr_fe_open(s->cs);
528 usb_serial_update_handlers(s);
529 usb_serial_handle_reset(dev);
530 return 0;
533 static USBDevice *usb_serial_init(const char *filename)
535 USBDevice *dev;
536 CharDriverState *cdrv;
537 uint32_t vendorid = 0, productid = 0;
538 char label[32];
539 static int index;
541 while (*filename && *filename != ':') {
542 const char *p;
543 char *e;
544 if (strstart(filename, "vendorid=", &p)) {
545 vendorid = strtol(p, &e, 16);
546 if (e == p || (*e && *e != ',' && *e != ':')) {
547 error_report("bogus vendor ID %s", p);
548 return NULL;
550 filename = e;
551 } else if (strstart(filename, "productid=", &p)) {
552 productid = strtol(p, &e, 16);
553 if (e == p || (*e && *e != ',' && *e != ':')) {
554 error_report("bogus product ID %s", p);
555 return NULL;
557 filename = e;
558 } else {
559 error_report("unrecognized serial USB option %s", filename);
560 return NULL;
562 while(*filename == ',')
563 filename++;
565 if (!*filename) {
566 error_report("character device specification needed");
567 return NULL;
569 filename++;
571 snprintf(label, sizeof(label), "usbserial%d", index++);
572 cdrv = qemu_chr_new(label, filename, NULL);
573 if (!cdrv)
574 return NULL;
576 dev = usb_create(NULL /* FIXME */, "usb-serial");
577 if (!dev) {
578 return NULL;
580 qdev_prop_set_chr(&dev->qdev, "chardev", cdrv);
581 if (vendorid)
582 qdev_prop_set_uint16(&dev->qdev, "vendorid", vendorid);
583 if (productid)
584 qdev_prop_set_uint16(&dev->qdev, "productid", productid);
585 qdev_init_nofail(&dev->qdev);
587 return dev;
590 static USBDevice *usb_braille_init(const char *unused)
592 USBDevice *dev;
593 CharDriverState *cdrv;
595 cdrv = qemu_chr_new("braille", "braille", NULL);
596 if (!cdrv)
597 return NULL;
599 dev = usb_create(NULL /* FIXME */, "usb-braille");
600 qdev_prop_set_chr(&dev->qdev, "chardev", cdrv);
601 qdev_init_nofail(&dev->qdev);
603 return dev;
606 static const VMStateDescription vmstate_usb_serial = {
607 .name = "usb-serial",
608 .unmigratable = 1,
611 static struct USBDeviceInfo serial_info = {
612 .product_desc = "QEMU USB Serial",
613 .qdev.name = "usb-serial",
614 .qdev.size = sizeof(USBSerialState),
615 .qdev.vmsd = &vmstate_usb_serial,
616 .usb_desc = &desc_serial,
617 .init = usb_serial_initfn,
618 .handle_packet = usb_generic_handle_packet,
619 .handle_reset = usb_serial_handle_reset,
620 .handle_control = usb_serial_handle_control,
621 .handle_data = usb_serial_handle_data,
622 .handle_destroy = usb_serial_handle_destroy,
623 .usbdevice_name = "serial",
624 .usbdevice_init = usb_serial_init,
625 .qdev.props = (Property[]) {
626 DEFINE_PROP_CHR("chardev", USBSerialState, cs),
627 DEFINE_PROP_END_OF_LIST(),
631 static struct USBDeviceInfo braille_info = {
632 .product_desc = "QEMU USB Braille",
633 .qdev.name = "usb-braille",
634 .qdev.size = sizeof(USBSerialState),
635 .qdev.vmsd = &vmstate_usb_serial,
636 .usb_desc = &desc_braille,
637 .init = usb_serial_initfn,
638 .handle_packet = usb_generic_handle_packet,
639 .handle_reset = usb_serial_handle_reset,
640 .handle_control = usb_serial_handle_control,
641 .handle_data = usb_serial_handle_data,
642 .handle_destroy = usb_serial_handle_destroy,
643 .usbdevice_name = "braille",
644 .usbdevice_init = usb_braille_init,
645 .qdev.props = (Property[]) {
646 DEFINE_PROP_CHR("chardev", USBSerialState, cs),
647 DEFINE_PROP_END_OF_LIST(),
651 static void usb_serial_register_devices(void)
653 usb_qdev_register(&serial_info);
654 usb_qdev_register(&braille_info);
656 device_init(usb_serial_register_devices)