1 /* 2 * FTDI FT232BM Device emulation 3 * 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 7 * 8 * This code is licensed under the LGPL. 9 */ 10 11 #include "qemu-common.h" 12 #include "qemu/error-report.h" 13 #include "hw/usb.h" 14 #include "hw/usb/desc.h" 15 #include "sysemu/char.h" 16 17 //#define DEBUG_Serial 18 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 25 26 #define RECV_BUF 384 27 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 39 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) 42 43 /* RESET */ 44 45 #define FTDI_RESET_SIO 0 46 #define FTDI_RESET_RX 1 47 #define FTDI_RESET_TX 2 48 49 /* SET_MDM_CTRL */ 50 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) 55 56 /* SET_FLOW_CTRL */ 57 58 #define FTDI_RTS_CTS_HS 1 59 #define FTDI_DTR_DSR_HS 2 60 #define FTDI_XON_XOFF_HS 4 61 62 /* SET_DATA */ 63 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) 69 70 #define FTDI_STOP (0x3 << 11) 71 #define FTDI_STOP1 (0x0 << 11) 72 #define FTDI_STOP15 (0x1 << 11) 73 #define FTDI_STOP2 (0x2 << 11) 74 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 81 82 /* Status */ 83 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 92 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; 105 106 #define TYPE_USB_SERIAL "usb-serial-dev" 107 #define USB_SERIAL_DEV(obj) OBJECT_CHECK(USBSerialState, (obj), TYPE_USB_SERIAL) 108 109 enum { 110 STR_MANUFACTURER = 1, 111 STR_PRODUCT_SERIAL, 112 STR_PRODUCT_BRAILLE, 113 STR_SERIALNUMBER, 114 }; 115 116 static const USBDescStrings desc_strings = { 117 [STR_MANUFACTURER] = "QEMU", 118 [STR_PRODUCT_SERIAL] = "QEMU USB SERIAL", 119 [STR_PRODUCT_BRAILLE] = "QEMU USB BAUM BRAILLE", 120 [STR_SERIALNUMBER] = "1", 121 }; 122 123 static const USBDescIface desc_iface0 = { 124 .bInterfaceNumber = 0, 125 .bNumEndpoints = 2, 126 .bInterfaceClass = 0xff, 127 .bInterfaceSubClass = 0xff, 128 .bInterfaceProtocol = 0xff, 129 .eps = (USBDescEndpoint[]) { 130 { 131 .bEndpointAddress = USB_DIR_IN | 0x01, 132 .bmAttributes = USB_ENDPOINT_XFER_BULK, 133 .wMaxPacketSize = 64, 134 },{ 135 .bEndpointAddress = USB_DIR_OUT | 0x02, 136 .bmAttributes = USB_ENDPOINT_XFER_BULK, 137 .wMaxPacketSize = 64, 138 }, 139 } 140 }; 141 142 static const USBDescDevice desc_device = { 143 .bcdUSB = 0x0200, 144 .bMaxPacketSize0 = 8, 145 .bNumConfigurations = 1, 146 .confs = (USBDescConfig[]) { 147 { 148 .bNumInterfaces = 1, 149 .bConfigurationValue = 1, 150 .bmAttributes = USB_CFG_ATT_ONE, 151 .bMaxPower = 50, 152 .nif = 1, 153 .ifs = &desc_iface0, 154 }, 155 }, 156 }; 157 158 static const USBDesc desc_serial = { 159 .id = { 160 .idVendor = 0x0403, 161 .idProduct = 0x6001, 162 .bcdDevice = 0x0400, 163 .iManufacturer = STR_MANUFACTURER, 164 .iProduct = STR_PRODUCT_SERIAL, 165 .iSerialNumber = STR_SERIALNUMBER, 166 }, 167 .full = &desc_device, 168 .str = desc_strings, 169 }; 170 171 static const USBDesc desc_braille = { 172 .id = { 173 .idVendor = 0x0403, 174 .idProduct = 0xfe72, 175 .bcdDevice = 0x0400, 176 .iManufacturer = STR_MANUFACTURER, 177 .iProduct = STR_PRODUCT_BRAILLE, 178 .iSerialNumber = STR_SERIALNUMBER, 179 }, 180 .full = &desc_device, 181 .str = desc_strings, 182 }; 183 184 static void usb_serial_reset(USBSerialState *s) 185 { 186 /* TODO: Set flow control to none */ 187 s->event_chr = 0x0d; 188 s->event_trigger = 0; 189 s->recv_ptr = 0; 190 s->recv_used = 0; 191 /* TODO: purge in char driver */ 192 } 193 194 static void usb_serial_handle_reset(USBDevice *dev) 195 { 196 USBSerialState *s = (USBSerialState *)dev; 197 198 DPRINTF("Reset\n"); 199 200 usb_serial_reset(s); 201 /* TODO: Reset char device, send BREAK? */ 202 } 203 204 static uint8_t usb_get_modem_lines(USBSerialState *s) 205 { 206 int flags; 207 uint8_t ret; 208 209 if (qemu_chr_fe_ioctl(s->cs, CHR_IOCTL_SERIAL_GET_TIOCM, &flags) == -ENOTSUP) 210 return FTDI_CTS|FTDI_DSR|FTDI_RLSD; 211 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; 221 222 return ret; 223 } 224 225 static void usb_serial_handle_control(USBDevice *dev, USBPacket *p, 226 int request, int value, int index, int length, uint8_t *data) 227 { 228 USBSerialState *s = (USBSerialState *)dev; 229 int ret; 230 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; 235 } 236 237 switch (request) { 238 case EndpointOutRequest | USB_REQ_CLEAR_FEATURE: 239 break; 240 241 /* Class specific requests. */ 242 case DeviceOutVendor | FTDI_RESET: 243 switch (value) { 244 case FTDI_RESET_SIO: 245 usb_serial_reset(s); 246 break; 247 case FTDI_RESET_RX: 248 s->recv_ptr = 0; 249 s->recv_used = 0; 250 /* TODO: purge from char device */ 251 break; 252 case FTDI_RESET_TX: 253 /* TODO: purge from char device */ 254 break; 255 } 256 break; 257 case DeviceOutVendor | FTDI_SET_MDM_CTRL: 258 { 259 static int flags; 260 qemu_chr_fe_ioctl(s->cs,CHR_IOCTL_SERIAL_GET_TIOCM, &flags); 261 if (value & FTDI_SET_RTS) { 262 if (value & FTDI_RTS) 263 flags |= CHR_TIOCM_RTS; 264 else 265 flags &= ~CHR_TIOCM_RTS; 266 } 267 if (value & FTDI_SET_DTR) { 268 if (value & FTDI_DTR) 269 flags |= CHR_TIOCM_DTR; 270 else 271 flags &= ~CHR_TIOCM_DTR; 272 } 273 qemu_chr_fe_ioctl(s->cs,CHR_IOCTL_SERIAL_SET_TIOCM, &flags); 274 break; 275 } 276 case DeviceOutVendor | FTDI_SET_FLOW_CTRL: 277 /* TODO: ioctl */ 278 break; 279 case DeviceOutVendor | FTDI_SET_BAUD: { 280 static const int subdivisors8[8] = { 0, 4, 2, 1, 3, 5, 6, 7 }; 281 int subdivisor8 = subdivisors8[((value & 0xc000) >> 14) 282 | ((index & 1) << 2)]; 283 int divisor = value & 0x3fff; 284 285 /* chip special cases */ 286 if (divisor == 1 && subdivisor8 == 0) 287 subdivisor8 = 4; 288 if (divisor == 0 && subdivisor8 == 0) 289 divisor = 1; 290 291 s->params.speed = (48000000 / 2) / (8 * divisor + subdivisor8); 292 qemu_chr_fe_ioctl(s->cs, CHR_IOCTL_SERIAL_SET_PARAMS, &s->params); 293 break; 294 } 295 case DeviceOutVendor | FTDI_SET_DATA: 296 switch (value & FTDI_PARITY) { 297 case 0: 298 s->params.parity = 'N'; 299 break; 300 case FTDI_ODD: 301 s->params.parity = 'O'; 302 break; 303 case FTDI_EVEN: 304 s->params.parity = 'E'; 305 break; 306 default: 307 DPRINTF("unsupported parity %d\n", value & FTDI_PARITY); 308 goto fail; 309 } 310 switch (value & FTDI_STOP) { 311 case FTDI_STOP1: 312 s->params.stop_bits = 1; 313 break; 314 case FTDI_STOP2: 315 s->params.stop_bits = 2; 316 break; 317 default: 318 DPRINTF("unsupported stop bits %d\n", value & FTDI_STOP); 319 goto fail; 320 } 321 qemu_chr_fe_ioctl(s->cs, CHR_IOCTL_SERIAL_SET_PARAMS, &s->params); 322 /* TODO: TX ON/OFF */ 323 break; 324 case DeviceInVendor | FTDI_GET_MDM_ST: 325 data[0] = usb_get_modem_lines(s) | 1; 326 data[1] = 0; 327 p->actual_length = 2; 328 break; 329 case DeviceOutVendor | FTDI_SET_EVENT_CHR: 330 /* TODO: handle it */ 331 s->event_chr = value; 332 break; 333 case DeviceOutVendor | FTDI_SET_ERROR_CHR: 334 /* TODO: handle it */ 335 s->error_chr = value; 336 break; 337 case DeviceOutVendor | FTDI_SET_LATENCY: 338 s->latency = value; 339 break; 340 case DeviceInVendor | FTDI_GET_LATENCY: 341 data[0] = s->latency; 342 p->actual_length = 1; 343 break; 344 default: 345 fail: 346 DPRINTF("got unsupported/bogus control %x, value %x\n", request, value); 347 p->status = USB_RET_STALL; 348 break; 349 } 350 } 351 352 static void usb_serial_handle_data(USBDevice *dev, USBPacket *p) 353 { 354 USBSerialState *s = (USBSerialState *)dev; 355 uint8_t devep = p->ep->nr; 356 struct iovec *iov; 357 uint8_t header[2]; 358 int i, first_len, len; 359 360 switch (p->pid) { 361 case USB_TOKEN_OUT: 362 if (devep != 2) 363 goto fail; 364 for (i = 0; i < p->iov.niov; i++) { 365 iov = p->iov.iov + i; 366 qemu_chr_fe_write(s->cs, iov->iov_base, iov->iov_len); 367 } 368 p->actual_length = p->iov.size; 369 break; 370 371 case USB_TOKEN_IN: 372 if (devep != 1) 373 goto fail; 374 first_len = RECV_BUF - s->recv_ptr; 375 len = p->iov.size; 376 if (len <= 2) { 377 p->status = USB_RET_NAK; 378 break; 379 } 380 header[0] = usb_get_modem_lines(s) | 1; 381 /* We do not have the uart details */ 382 /* handle serial break */ 383 if (s->event_trigger && s->event_trigger & FTDI_BI) { 384 s->event_trigger &= ~FTDI_BI; 385 header[1] = FTDI_BI; 386 usb_packet_copy(p, header, 2); 387 break; 388 } else { 389 header[1] = 0; 390 } 391 len -= 2; 392 if (len > s->recv_used) 393 len = s->recv_used; 394 if (!len) { 395 p->status = USB_RET_NAK; 396 break; 397 } 398 if (first_len > len) 399 first_len = len; 400 usb_packet_copy(p, header, 2); 401 usb_packet_copy(p, s->recv_buf + s->recv_ptr, first_len); 402 if (len > first_len) 403 usb_packet_copy(p, s->recv_buf, len - first_len); 404 s->recv_used -= len; 405 s->recv_ptr = (s->recv_ptr + len) % RECV_BUF; 406 break; 407 408 default: 409 DPRINTF("Bad token\n"); 410 fail: 411 p->status = USB_RET_STALL; 412 break; 413 } 414 } 415 416 static int usb_serial_can_read(void *opaque) 417 { 418 USBSerialState *s = opaque; 419 420 if (!s->dev.attached) { 421 return 0; 422 } 423 return RECV_BUF - s->recv_used; 424 } 425 426 static void usb_serial_read(void *opaque, const uint8_t *buf, int size) 427 { 428 USBSerialState *s = opaque; 429 int first_size, start; 430 431 /* room in the buffer? */ 432 if (size > (RECV_BUF - s->recv_used)) 433 size = RECV_BUF - s->recv_used; 434 435 start = s->recv_ptr + s->recv_used; 436 if (start < RECV_BUF) { 437 /* copy data to end of buffer */ 438 first_size = RECV_BUF - start; 439 if (first_size > size) 440 first_size = size; 441 442 memcpy(s->recv_buf + start, buf, first_size); 443 444 /* wrap around to front if needed */ 445 if (size > first_size) 446 memcpy(s->recv_buf, buf + first_size, size - first_size); 447 } else { 448 start -= RECV_BUF; 449 memcpy(s->recv_buf + start, buf, size); 450 } 451 s->recv_used += size; 452 } 453 454 static void usb_serial_event(void *opaque, int event) 455 { 456 USBSerialState *s = opaque; 457 458 switch (event) { 459 case CHR_EVENT_BREAK: 460 s->event_trigger |= FTDI_BI; 461 break; 462 case CHR_EVENT_FOCUS: 463 break; 464 case CHR_EVENT_OPENED: 465 if (!s->dev.attached) { 466 usb_device_attach(&s->dev, &error_abort); 467 } 468 break; 469 case CHR_EVENT_CLOSED: 470 if (s->dev.attached) { 471 usb_device_detach(&s->dev); 472 } 473 break; 474 } 475 } 476 477 static void usb_serial_realize(USBDevice *dev, Error **errp) 478 { 479 USBSerialState *s = USB_SERIAL_DEV(dev); 480 Error *local_err = NULL; 481 482 usb_desc_create_serial(dev); 483 usb_desc_init(dev); 484 dev->auto_attach = 0; 485 486 if (!s->cs) { 487 error_setg(errp, "Property chardev is required"); 488 return; 489 } 490 491 usb_check_attach(dev, &local_err); 492 if (local_err) { 493 error_propagate(errp, local_err); 494 return; 495 } 496 497 qemu_chr_add_handlers(s->cs, usb_serial_can_read, usb_serial_read, 498 usb_serial_event, s); 499 usb_serial_handle_reset(dev); 500 501 if (s->cs->be_open && !dev->attached) { 502 usb_device_attach(dev, &error_abort); 503 } 504 } 505 506 static USBDevice *usb_serial_init(USBBus *bus, const char *filename) 507 { 508 USBDevice *dev; 509 CharDriverState *cdrv; 510 uint32_t vendorid = 0, productid = 0; 511 char label[32]; 512 static int index; 513 514 while (*filename && *filename != ':') { 515 const char *p; 516 char *e; 517 if (strstart(filename, "vendorid=", &p)) { 518 vendorid = strtol(p, &e, 16); 519 if (e == p || (*e && *e != ',' && *e != ':')) { 520 error_report("bogus vendor ID %s", p); 521 return NULL; 522 } 523 filename = e; 524 } else if (strstart(filename, "productid=", &p)) { 525 productid = strtol(p, &e, 16); 526 if (e == p || (*e && *e != ',' && *e != ':')) { 527 error_report("bogus product ID %s", p); 528 return NULL; 529 } 530 filename = e; 531 } else { 532 error_report("unrecognized serial USB option %s", filename); 533 return NULL; 534 } 535 while(*filename == ',') 536 filename++; 537 } 538 if (!*filename) { 539 error_report("character device specification needed"); 540 return NULL; 541 } 542 filename++; 543 544 snprintf(label, sizeof(label), "usbserial%d", index++); 545 cdrv = qemu_chr_new(label, filename, NULL); 546 if (!cdrv) 547 return NULL; 548 549 dev = usb_create(bus, "usb-serial"); 550 qdev_prop_set_chr(&dev->qdev, "chardev", cdrv); 551 if (vendorid) 552 qdev_prop_set_uint16(&dev->qdev, "vendorid", vendorid); 553 if (productid) 554 qdev_prop_set_uint16(&dev->qdev, "productid", productid); 555 return dev; 556 } 557 558 static USBDevice *usb_braille_init(USBBus *bus, const char *unused) 559 { 560 USBDevice *dev; 561 CharDriverState *cdrv; 562 563 cdrv = qemu_chr_new("braille", "braille", NULL); 564 if (!cdrv) 565 return NULL; 566 567 dev = usb_create(bus, "usb-braille"); 568 qdev_prop_set_chr(&dev->qdev, "chardev", cdrv); 569 return dev; 570 } 571 572 static const VMStateDescription vmstate_usb_serial = { 573 .name = "usb-serial", 574 .unmigratable = 1, 575 }; 576 577 static Property serial_properties[] = { 578 DEFINE_PROP_CHR("chardev", USBSerialState, cs), 579 DEFINE_PROP_END_OF_LIST(), 580 }; 581 582 static void usb_serial_dev_class_init(ObjectClass *klass, void *data) 583 { 584 DeviceClass *dc = DEVICE_CLASS(klass); 585 USBDeviceClass *uc = USB_DEVICE_CLASS(klass); 586 587 uc->realize = usb_serial_realize; 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 set_bit(DEVICE_CATEGORY_INPUT, dc->categories); 593 } 594 595 static const TypeInfo usb_serial_dev_type_info = { 596 .name = TYPE_USB_SERIAL, 597 .parent = TYPE_USB_DEVICE, 598 .instance_size = sizeof(USBSerialState), 599 .abstract = true, 600 .class_init = usb_serial_dev_class_init, 601 }; 602 603 static void usb_serial_class_initfn(ObjectClass *klass, void *data) 604 { 605 DeviceClass *dc = DEVICE_CLASS(klass); 606 USBDeviceClass *uc = USB_DEVICE_CLASS(klass); 607 608 uc->product_desc = "QEMU USB Serial"; 609 uc->usb_desc = &desc_serial; 610 dc->props = serial_properties; 611 } 612 613 static const TypeInfo serial_info = { 614 .name = "usb-serial", 615 .parent = TYPE_USB_SERIAL, 616 .class_init = usb_serial_class_initfn, 617 }; 618 619 static Property braille_properties[] = { 620 DEFINE_PROP_CHR("chardev", USBSerialState, cs), 621 DEFINE_PROP_END_OF_LIST(), 622 }; 623 624 static void usb_braille_class_initfn(ObjectClass *klass, void *data) 625 { 626 DeviceClass *dc = DEVICE_CLASS(klass); 627 USBDeviceClass *uc = USB_DEVICE_CLASS(klass); 628 629 uc->product_desc = "QEMU USB Braille"; 630 uc->usb_desc = &desc_braille; 631 dc->props = braille_properties; 632 } 633 634 static const TypeInfo braille_info = { 635 .name = "usb-braille", 636 .parent = TYPE_USB_SERIAL, 637 .class_init = usb_braille_class_initfn, 638 }; 639 640 static void usb_serial_register_types(void) 641 { 642 type_register_static(&usb_serial_dev_type_info); 643 type_register_static(&serial_info); 644 usb_legacy_register("usb-serial", "serial", usb_serial_init); 645 type_register_static(&braille_info); 646 usb_legacy_register("usb-braille", "braille", usb_braille_init); 647 } 648 649 type_init(usb_serial_register_types) 650