1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Prolific PL2303 USB to serial adaptor driver 4 * 5 * Copyright (C) 2001-2007 Greg Kroah-Hartman (greg@kroah.com) 6 * Copyright (C) 2003 IBM Corp. 7 * 8 * Original driver for 2.2.x by anonymous 9 * 10 * See Documentation/usb/usb-serial.rst for more information on using this 11 * driver 12 */ 13 14 #include <linux/kernel.h> 15 #include <linux/errno.h> 16 #include <linux/slab.h> 17 #include <linux/tty.h> 18 #include <linux/tty_driver.h> 19 #include <linux/tty_flip.h> 20 #include <linux/serial.h> 21 #include <linux/module.h> 22 #include <linux/moduleparam.h> 23 #include <linux/spinlock.h> 24 #include <linux/uaccess.h> 25 #include <linux/usb.h> 26 #include <linux/usb/serial.h> 27 #include <asm/unaligned.h> 28 #include "pl2303.h" 29 30 31 #define PL2303_QUIRK_UART_STATE_IDX0 BIT(0) 32 #define PL2303_QUIRK_LEGACY BIT(1) 33 #define PL2303_QUIRK_ENDPOINT_HACK BIT(2) 34 35 static const struct usb_device_id id_table[] = { 36 { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID), 37 .driver_info = PL2303_QUIRK_ENDPOINT_HACK }, 38 { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_RSAQ2) }, 39 { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_DCU11) }, 40 { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_RSAQ3) }, 41 { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_CHILITAG) }, 42 { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_PHAROS) }, 43 { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_ALDIGA) }, 44 { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_MMX) }, 45 { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_GPRS) }, 46 { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_HCR331) }, 47 { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_MOTOROLA) }, 48 { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_ZTEK) }, 49 { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_TB) }, 50 { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_GC) }, 51 { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_GB) }, 52 { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_GT) }, 53 { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_GL) }, 54 { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_GE) }, 55 { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_GS) }, 56 { USB_DEVICE(IODATA_VENDOR_ID, IODATA_PRODUCT_ID) }, 57 { USB_DEVICE(IODATA_VENDOR_ID, IODATA_PRODUCT_ID_RSAQ5) }, 58 { USB_DEVICE(ATEN_VENDOR_ID, ATEN_PRODUCT_ID), 59 .driver_info = PL2303_QUIRK_ENDPOINT_HACK }, 60 { USB_DEVICE(ATEN_VENDOR_ID, ATEN_PRODUCT_UC485), 61 .driver_info = PL2303_QUIRK_ENDPOINT_HACK }, 62 { USB_DEVICE(ATEN_VENDOR_ID, ATEN_PRODUCT_UC232B), 63 .driver_info = PL2303_QUIRK_ENDPOINT_HACK }, 64 { USB_DEVICE(ATEN_VENDOR_ID, ATEN_PRODUCT_ID2) }, 65 { USB_DEVICE(ATEN_VENDOR_ID2, ATEN_PRODUCT_ID) }, 66 { USB_DEVICE(ELCOM_VENDOR_ID, ELCOM_PRODUCT_ID) }, 67 { USB_DEVICE(ELCOM_VENDOR_ID, ELCOM_PRODUCT_ID_UCSGT) }, 68 { USB_DEVICE(ITEGNO_VENDOR_ID, ITEGNO_PRODUCT_ID) }, 69 { USB_DEVICE(ITEGNO_VENDOR_ID, ITEGNO_PRODUCT_ID_2080) }, 70 { USB_DEVICE(MA620_VENDOR_ID, MA620_PRODUCT_ID) }, 71 { USB_DEVICE(RATOC_VENDOR_ID, RATOC_PRODUCT_ID) }, 72 { USB_DEVICE(TRIPP_VENDOR_ID, TRIPP_PRODUCT_ID) }, 73 { USB_DEVICE(RADIOSHACK_VENDOR_ID, RADIOSHACK_PRODUCT_ID) }, 74 { USB_DEVICE(DCU10_VENDOR_ID, DCU10_PRODUCT_ID) }, 75 { USB_DEVICE(SITECOM_VENDOR_ID, SITECOM_PRODUCT_ID) }, 76 { USB_DEVICE(ALCATEL_VENDOR_ID, ALCATEL_PRODUCT_ID) }, 77 { USB_DEVICE(SIEMENS_VENDOR_ID, SIEMENS_PRODUCT_ID_SX1), 78 .driver_info = PL2303_QUIRK_UART_STATE_IDX0 }, 79 { USB_DEVICE(SIEMENS_VENDOR_ID, SIEMENS_PRODUCT_ID_X65), 80 .driver_info = PL2303_QUIRK_UART_STATE_IDX0 }, 81 { USB_DEVICE(SIEMENS_VENDOR_ID, SIEMENS_PRODUCT_ID_X75), 82 .driver_info = PL2303_QUIRK_UART_STATE_IDX0 }, 83 { USB_DEVICE(SIEMENS_VENDOR_ID, SIEMENS_PRODUCT_ID_EF81), 84 .driver_info = PL2303_QUIRK_ENDPOINT_HACK }, 85 { USB_DEVICE(BENQ_VENDOR_ID, BENQ_PRODUCT_ID_S81) }, /* Benq/Siemens S81 */ 86 { USB_DEVICE(SYNTECH_VENDOR_ID, SYNTECH_PRODUCT_ID) }, 87 { USB_DEVICE(NOKIA_CA42_VENDOR_ID, NOKIA_CA42_PRODUCT_ID) }, 88 { USB_DEVICE(CA_42_CA42_VENDOR_ID, CA_42_CA42_PRODUCT_ID) }, 89 { USB_DEVICE(SAGEM_VENDOR_ID, SAGEM_PRODUCT_ID) }, 90 { USB_DEVICE(LEADTEK_VENDOR_ID, LEADTEK_9531_PRODUCT_ID) }, 91 { USB_DEVICE(SPEEDDRAGON_VENDOR_ID, SPEEDDRAGON_PRODUCT_ID) }, 92 { USB_DEVICE(DATAPILOT_U2_VENDOR_ID, DATAPILOT_U2_PRODUCT_ID) }, 93 { USB_DEVICE(BELKIN_VENDOR_ID, BELKIN_PRODUCT_ID) }, 94 { USB_DEVICE(ALCOR_VENDOR_ID, ALCOR_PRODUCT_ID), 95 .driver_info = PL2303_QUIRK_ENDPOINT_HACK }, 96 { USB_DEVICE(WS002IN_VENDOR_ID, WS002IN_PRODUCT_ID) }, 97 { USB_DEVICE(COREGA_VENDOR_ID, COREGA_PRODUCT_ID) }, 98 { USB_DEVICE(YCCABLE_VENDOR_ID, YCCABLE_PRODUCT_ID) }, 99 { USB_DEVICE(SUPERIAL_VENDOR_ID, SUPERIAL_PRODUCT_ID) }, 100 { USB_DEVICE(HP_VENDOR_ID, HP_LD220_PRODUCT_ID) }, 101 { USB_DEVICE(HP_VENDOR_ID, HP_LD220TA_PRODUCT_ID) }, 102 { USB_DEVICE(HP_VENDOR_ID, HP_LD381_PRODUCT_ID) }, 103 { USB_DEVICE(HP_VENDOR_ID, HP_LD381GC_PRODUCT_ID) }, 104 { USB_DEVICE(HP_VENDOR_ID, HP_LD960_PRODUCT_ID) }, 105 { USB_DEVICE(HP_VENDOR_ID, HP_LD960TA_PRODUCT_ID) }, 106 { USB_DEVICE(HP_VENDOR_ID, HP_LCM220_PRODUCT_ID) }, 107 { USB_DEVICE(HP_VENDOR_ID, HP_LCM960_PRODUCT_ID) }, 108 { USB_DEVICE(HP_VENDOR_ID, HP_LM920_PRODUCT_ID) }, 109 { USB_DEVICE(HP_VENDOR_ID, HP_LM940_PRODUCT_ID) }, 110 { USB_DEVICE(HP_VENDOR_ID, HP_TD620_PRODUCT_ID) }, 111 { USB_DEVICE(CRESSI_VENDOR_ID, CRESSI_EDY_PRODUCT_ID) }, 112 { USB_DEVICE(ZEAGLE_VENDOR_ID, ZEAGLE_N2ITION3_PRODUCT_ID) }, 113 { USB_DEVICE(SONY_VENDOR_ID, SONY_QN3USB_PRODUCT_ID) }, 114 { USB_DEVICE(SANWA_VENDOR_ID, SANWA_PRODUCT_ID) }, 115 { USB_DEVICE(ADLINK_VENDOR_ID, ADLINK_ND6530_PRODUCT_ID) }, 116 { USB_DEVICE(SMART_VENDOR_ID, SMART_PRODUCT_ID) }, 117 { USB_DEVICE(AT_VENDOR_ID, AT_VTKIT3_PRODUCT_ID) }, 118 { } /* Terminating entry */ 119 }; 120 121 MODULE_DEVICE_TABLE(usb, id_table); 122 123 #define SET_LINE_REQUEST_TYPE 0x21 124 #define SET_LINE_REQUEST 0x20 125 126 #define SET_CONTROL_REQUEST_TYPE 0x21 127 #define SET_CONTROL_REQUEST 0x22 128 #define CONTROL_DTR 0x01 129 #define CONTROL_RTS 0x02 130 131 #define BREAK_REQUEST_TYPE 0x21 132 #define BREAK_REQUEST 0x23 133 #define BREAK_ON 0xffff 134 #define BREAK_OFF 0x0000 135 136 #define GET_LINE_REQUEST_TYPE 0xa1 137 #define GET_LINE_REQUEST 0x21 138 139 #define VENDOR_WRITE_REQUEST_TYPE 0x40 140 #define VENDOR_WRITE_REQUEST 0x01 141 #define VENDOR_WRITE_NREQUEST 0x80 142 143 #define VENDOR_READ_REQUEST_TYPE 0xc0 144 #define VENDOR_READ_REQUEST 0x01 145 #define VENDOR_READ_NREQUEST 0x81 146 147 #define UART_STATE_INDEX 8 148 #define UART_STATE_MSR_MASK 0x8b 149 #define UART_STATE_TRANSIENT_MASK 0x74 150 #define UART_DCD 0x01 151 #define UART_DSR 0x02 152 #define UART_BREAK_ERROR 0x04 153 #define UART_RING 0x08 154 #define UART_FRAME_ERROR 0x10 155 #define UART_PARITY_ERROR 0x20 156 #define UART_OVERRUN_ERROR 0x40 157 #define UART_CTS 0x80 158 159 #define PL2303_FLOWCTRL_MASK 0xf0 160 161 #define PL2303_READ_TYPE_HX_STATUS 0x8080 162 163 #define PL2303_HXN_RESET_REG 0x07 164 #define PL2303_HXN_RESET_UPSTREAM_PIPE 0x02 165 #define PL2303_HXN_RESET_DOWNSTREAM_PIPE 0x01 166 167 #define PL2303_HXN_FLOWCTRL_REG 0x0a 168 #define PL2303_HXN_FLOWCTRL_MASK 0x1c 169 #define PL2303_HXN_FLOWCTRL_NONE 0x1c 170 #define PL2303_HXN_FLOWCTRL_RTS_CTS 0x18 171 #define PL2303_HXN_FLOWCTRL_XON_XOFF 0x0c 172 173 static void pl2303_set_break(struct usb_serial_port *port, bool enable); 174 175 enum pl2303_type { 176 TYPE_01, /* Type 0 and 1 (difference unknown) */ 177 TYPE_HX, /* HX version of the pl2303 chip */ 178 TYPE_HXN, /* HXN version of the pl2303 chip */ 179 TYPE_COUNT 180 }; 181 182 struct pl2303_type_data { 183 speed_t max_baud_rate; 184 unsigned long quirks; 185 unsigned int no_autoxonxoff:1; 186 unsigned int no_divisors:1; 187 }; 188 189 struct pl2303_serial_private { 190 const struct pl2303_type_data *type; 191 unsigned long quirks; 192 }; 193 194 struct pl2303_private { 195 spinlock_t lock; 196 u8 line_control; 197 u8 line_status; 198 199 u8 line_settings[7]; 200 }; 201 202 static const struct pl2303_type_data pl2303_type_data[TYPE_COUNT] = { 203 [TYPE_01] = { 204 .max_baud_rate = 1228800, 205 .quirks = PL2303_QUIRK_LEGACY, 206 .no_autoxonxoff = true, 207 }, 208 [TYPE_HX] = { 209 .max_baud_rate = 12000000, 210 }, 211 [TYPE_HXN] = { 212 .max_baud_rate = 12000000, 213 .no_divisors = true, 214 }, 215 }; 216 217 static int pl2303_vendor_read(struct usb_serial *serial, u16 value, 218 unsigned char buf[1]) 219 { 220 struct pl2303_serial_private *spriv = usb_get_serial_data(serial); 221 struct device *dev = &serial->interface->dev; 222 u8 request; 223 int res; 224 225 if (spriv->type == &pl2303_type_data[TYPE_HXN]) 226 request = VENDOR_READ_NREQUEST; 227 else 228 request = VENDOR_READ_REQUEST; 229 230 res = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0), 231 request, VENDOR_READ_REQUEST_TYPE, 232 value, 0, buf, 1, 100); 233 if (res != 1) { 234 dev_err(dev, "%s - failed to read [%04x]: %d\n", __func__, 235 value, res); 236 if (res >= 0) 237 res = -EIO; 238 239 return res; 240 } 241 242 dev_dbg(dev, "%s - [%04x] = %02x\n", __func__, value, buf[0]); 243 244 return 0; 245 } 246 247 static int pl2303_vendor_write(struct usb_serial *serial, u16 value, u16 index) 248 { 249 struct pl2303_serial_private *spriv = usb_get_serial_data(serial); 250 struct device *dev = &serial->interface->dev; 251 u8 request; 252 int res; 253 254 dev_dbg(dev, "%s - [%04x] = %02x\n", __func__, value, index); 255 256 if (spriv->type == &pl2303_type_data[TYPE_HXN]) 257 request = VENDOR_WRITE_NREQUEST; 258 else 259 request = VENDOR_WRITE_REQUEST; 260 261 res = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), 262 request, VENDOR_WRITE_REQUEST_TYPE, 263 value, index, NULL, 0, 100); 264 if (res) { 265 dev_err(dev, "%s - failed to write [%04x]: %d\n", __func__, 266 value, res); 267 return res; 268 } 269 270 return 0; 271 } 272 273 static int pl2303_update_reg(struct usb_serial *serial, u8 reg, u8 mask, u8 val) 274 { 275 struct pl2303_serial_private *spriv = usb_get_serial_data(serial); 276 int ret = 0; 277 u8 *buf; 278 279 buf = kmalloc(1, GFP_KERNEL); 280 if (!buf) 281 return -ENOMEM; 282 283 if (spriv->type == &pl2303_type_data[TYPE_HXN]) 284 ret = pl2303_vendor_read(serial, reg, buf); 285 else 286 ret = pl2303_vendor_read(serial, reg | 0x80, buf); 287 288 if (ret) 289 goto out_free; 290 291 *buf &= ~mask; 292 *buf |= val & mask; 293 294 ret = pl2303_vendor_write(serial, reg, *buf); 295 out_free: 296 kfree(buf); 297 298 return ret; 299 } 300 301 static int pl2303_probe(struct usb_serial *serial, 302 const struct usb_device_id *id) 303 { 304 usb_set_serial_data(serial, (void *)id->driver_info); 305 306 return 0; 307 } 308 309 /* 310 * Use interrupt endpoint from first interface if available. 311 * 312 * This is needed due to the looney way its endpoints are set up. 313 */ 314 static int pl2303_endpoint_hack(struct usb_serial *serial, 315 struct usb_serial_endpoints *epds) 316 { 317 struct usb_interface *interface = serial->interface; 318 struct usb_device *dev = serial->dev; 319 struct device *ddev = &interface->dev; 320 struct usb_host_interface *iface_desc; 321 struct usb_endpoint_descriptor *endpoint; 322 unsigned int i; 323 324 if (interface == dev->actconfig->interface[0]) 325 return 0; 326 327 /* check out the endpoints of the other interface */ 328 iface_desc = dev->actconfig->interface[0]->cur_altsetting; 329 330 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) { 331 endpoint = &iface_desc->endpoint[i].desc; 332 333 if (!usb_endpoint_is_int_in(endpoint)) 334 continue; 335 336 dev_dbg(ddev, "found interrupt in on separate interface\n"); 337 if (epds->num_interrupt_in < ARRAY_SIZE(epds->interrupt_in)) 338 epds->interrupt_in[epds->num_interrupt_in++] = endpoint; 339 } 340 341 return 0; 342 } 343 344 static int pl2303_calc_num_ports(struct usb_serial *serial, 345 struct usb_serial_endpoints *epds) 346 { 347 unsigned long quirks = (unsigned long)usb_get_serial_data(serial); 348 struct device *dev = &serial->interface->dev; 349 int ret; 350 351 if (quirks & PL2303_QUIRK_ENDPOINT_HACK) { 352 ret = pl2303_endpoint_hack(serial, epds); 353 if (ret) 354 return ret; 355 } 356 357 if (epds->num_interrupt_in < 1) { 358 dev_err(dev, "required interrupt-in endpoint missing\n"); 359 return -ENODEV; 360 } 361 362 return 1; 363 } 364 365 static int pl2303_startup(struct usb_serial *serial) 366 { 367 struct pl2303_serial_private *spriv; 368 enum pl2303_type type = TYPE_01; 369 unsigned char *buf; 370 int res; 371 372 spriv = kzalloc(sizeof(*spriv), GFP_KERNEL); 373 if (!spriv) 374 return -ENOMEM; 375 376 buf = kmalloc(1, GFP_KERNEL); 377 if (!buf) { 378 kfree(spriv); 379 return -ENOMEM; 380 } 381 382 if (serial->dev->descriptor.bDeviceClass == 0x02) 383 type = TYPE_01; /* type 0 */ 384 else if (serial->dev->descriptor.bMaxPacketSize0 == 0x40) 385 type = TYPE_HX; 386 else if (serial->dev->descriptor.bDeviceClass == 0x00) 387 type = TYPE_01; /* type 1 */ 388 else if (serial->dev->descriptor.bDeviceClass == 0xFF) 389 type = TYPE_01; /* type 1 */ 390 dev_dbg(&serial->interface->dev, "device type: %d\n", type); 391 392 if (type == TYPE_HX) { 393 res = usb_control_msg(serial->dev, 394 usb_rcvctrlpipe(serial->dev, 0), 395 VENDOR_READ_REQUEST, VENDOR_READ_REQUEST_TYPE, 396 PL2303_READ_TYPE_HX_STATUS, 0, buf, 1, 100); 397 if (res != 1) 398 type = TYPE_HXN; 399 } 400 401 spriv->type = &pl2303_type_data[type]; 402 spriv->quirks = (unsigned long)usb_get_serial_data(serial); 403 spriv->quirks |= spriv->type->quirks; 404 405 usb_set_serial_data(serial, spriv); 406 407 if (type != TYPE_HXN) { 408 pl2303_vendor_read(serial, 0x8484, buf); 409 pl2303_vendor_write(serial, 0x0404, 0); 410 pl2303_vendor_read(serial, 0x8484, buf); 411 pl2303_vendor_read(serial, 0x8383, buf); 412 pl2303_vendor_read(serial, 0x8484, buf); 413 pl2303_vendor_write(serial, 0x0404, 1); 414 pl2303_vendor_read(serial, 0x8484, buf); 415 pl2303_vendor_read(serial, 0x8383, buf); 416 pl2303_vendor_write(serial, 0, 1); 417 pl2303_vendor_write(serial, 1, 0); 418 if (spriv->quirks & PL2303_QUIRK_LEGACY) 419 pl2303_vendor_write(serial, 2, 0x24); 420 else 421 pl2303_vendor_write(serial, 2, 0x44); 422 } 423 424 kfree(buf); 425 426 return 0; 427 } 428 429 static void pl2303_release(struct usb_serial *serial) 430 { 431 struct pl2303_serial_private *spriv = usb_get_serial_data(serial); 432 433 kfree(spriv); 434 } 435 436 static int pl2303_port_probe(struct usb_serial_port *port) 437 { 438 struct pl2303_private *priv; 439 440 priv = kzalloc(sizeof(*priv), GFP_KERNEL); 441 if (!priv) 442 return -ENOMEM; 443 444 spin_lock_init(&priv->lock); 445 446 usb_set_serial_port_data(port, priv); 447 448 port->port.drain_delay = 256; 449 450 return 0; 451 } 452 453 static void pl2303_port_remove(struct usb_serial_port *port) 454 { 455 struct pl2303_private *priv = usb_get_serial_port_data(port); 456 457 kfree(priv); 458 } 459 460 static int pl2303_set_control_lines(struct usb_serial_port *port, u8 value) 461 { 462 struct usb_device *dev = port->serial->dev; 463 int retval; 464 465 dev_dbg(&port->dev, "%s - %02x\n", __func__, value); 466 467 retval = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), 468 SET_CONTROL_REQUEST, SET_CONTROL_REQUEST_TYPE, 469 value, 0, NULL, 0, 100); 470 if (retval) 471 dev_err(&port->dev, "%s - failed: %d\n", __func__, retval); 472 473 return retval; 474 } 475 476 /* 477 * Returns the nearest supported baud rate that can be set directly without 478 * using divisors. 479 */ 480 static speed_t pl2303_get_supported_baud_rate(speed_t baud) 481 { 482 static const speed_t baud_sup[] = { 483 75, 150, 300, 600, 1200, 1800, 2400, 3600, 4800, 7200, 9600, 484 14400, 19200, 28800, 38400, 57600, 115200, 230400, 460800, 485 614400, 921600, 1228800, 2457600, 3000000, 6000000 486 }; 487 488 unsigned i; 489 490 for (i = 0; i < ARRAY_SIZE(baud_sup); ++i) { 491 if (baud_sup[i] > baud) 492 break; 493 } 494 495 if (i == ARRAY_SIZE(baud_sup)) 496 baud = baud_sup[i - 1]; 497 else if (i > 0 && (baud_sup[i] - baud) > (baud - baud_sup[i - 1])) 498 baud = baud_sup[i - 1]; 499 else 500 baud = baud_sup[i]; 501 502 return baud; 503 } 504 505 /* 506 * NOTE: If unsupported baud rates are set directly, the PL2303 seems to 507 * use 9600 baud. 508 */ 509 static speed_t pl2303_encode_baud_rate_direct(unsigned char buf[4], 510 speed_t baud) 511 { 512 put_unaligned_le32(baud, buf); 513 514 return baud; 515 } 516 517 static speed_t pl2303_encode_baud_rate_divisor(unsigned char buf[4], 518 speed_t baud) 519 { 520 unsigned int baseline, mantissa, exponent; 521 522 /* 523 * Apparently the formula is: 524 * baudrate = 12M * 32 / (mantissa * 4^exponent) 525 * where 526 * mantissa = buf[8:0] 527 * exponent = buf[11:9] 528 */ 529 baseline = 12000000 * 32; 530 mantissa = baseline / baud; 531 if (mantissa == 0) 532 mantissa = 1; /* Avoid dividing by zero if baud > 32*12M. */ 533 exponent = 0; 534 while (mantissa >= 512) { 535 if (exponent < 7) { 536 mantissa >>= 2; /* divide by 4 */ 537 exponent++; 538 } else { 539 /* Exponent is maxed. Trim mantissa and leave. */ 540 mantissa = 511; 541 break; 542 } 543 } 544 545 buf[3] = 0x80; 546 buf[2] = 0; 547 buf[1] = exponent << 1 | mantissa >> 8; 548 buf[0] = mantissa & 0xff; 549 550 /* Calculate and return the exact baud rate. */ 551 baud = (baseline / mantissa) >> (exponent << 1); 552 553 return baud; 554 } 555 556 static void pl2303_encode_baud_rate(struct tty_struct *tty, 557 struct usb_serial_port *port, 558 u8 buf[4]) 559 { 560 struct usb_serial *serial = port->serial; 561 struct pl2303_serial_private *spriv = usb_get_serial_data(serial); 562 speed_t baud_sup; 563 speed_t baud; 564 565 baud = tty_get_baud_rate(tty); 566 dev_dbg(&port->dev, "baud requested = %u\n", baud); 567 if (!baud) 568 return; 569 570 if (spriv->type->max_baud_rate) 571 baud = min_t(speed_t, baud, spriv->type->max_baud_rate); 572 /* 573 * Use direct method for supported baud rates, otherwise use divisors. 574 * Newer chip types do not support divisor encoding. 575 */ 576 if (spriv->type->no_divisors) 577 baud_sup = baud; 578 else 579 baud_sup = pl2303_get_supported_baud_rate(baud); 580 581 if (baud == baud_sup) 582 baud = pl2303_encode_baud_rate_direct(buf, baud); 583 else 584 baud = pl2303_encode_baud_rate_divisor(buf, baud); 585 586 /* Save resulting baud rate */ 587 tty_encode_baud_rate(tty, baud, baud); 588 dev_dbg(&port->dev, "baud set = %u\n", baud); 589 } 590 591 static int pl2303_get_line_request(struct usb_serial_port *port, 592 unsigned char buf[7]) 593 { 594 struct usb_device *udev = port->serial->dev; 595 int ret; 596 597 ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 598 GET_LINE_REQUEST, GET_LINE_REQUEST_TYPE, 599 0, 0, buf, 7, 100); 600 if (ret != 7) { 601 dev_err(&port->dev, "%s - failed: %d\n", __func__, ret); 602 603 if (ret >= 0) 604 ret = -EIO; 605 606 return ret; 607 } 608 609 dev_dbg(&port->dev, "%s - %7ph\n", __func__, buf); 610 611 return 0; 612 } 613 614 static int pl2303_set_line_request(struct usb_serial_port *port, 615 unsigned char buf[7]) 616 { 617 struct usb_device *udev = port->serial->dev; 618 int ret; 619 620 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 621 SET_LINE_REQUEST, SET_LINE_REQUEST_TYPE, 622 0, 0, buf, 7, 100); 623 if (ret < 0) { 624 dev_err(&port->dev, "%s - failed: %d\n", __func__, ret); 625 return ret; 626 } 627 628 dev_dbg(&port->dev, "%s - %7ph\n", __func__, buf); 629 630 return 0; 631 } 632 633 static bool pl2303_termios_change(const struct ktermios *a, const struct ktermios *b) 634 { 635 bool ixon_change; 636 637 ixon_change = ((a->c_iflag ^ b->c_iflag) & (IXON | IXANY)) || 638 a->c_cc[VSTART] != b->c_cc[VSTART] || 639 a->c_cc[VSTOP] != b->c_cc[VSTOP]; 640 641 return tty_termios_hw_change(a, b) || ixon_change; 642 } 643 644 static bool pl2303_enable_xonxoff(struct tty_struct *tty, const struct pl2303_type_data *type) 645 { 646 if (!I_IXON(tty) || I_IXANY(tty)) 647 return false; 648 649 if (START_CHAR(tty) != 0x11 || STOP_CHAR(tty) != 0x13) 650 return false; 651 652 if (type->no_autoxonxoff) 653 return false; 654 655 return true; 656 } 657 658 static void pl2303_set_termios(struct tty_struct *tty, 659 struct usb_serial_port *port, struct ktermios *old_termios) 660 { 661 struct usb_serial *serial = port->serial; 662 struct pl2303_serial_private *spriv = usb_get_serial_data(serial); 663 struct pl2303_private *priv = usb_get_serial_port_data(port); 664 unsigned long flags; 665 unsigned char *buf; 666 int ret; 667 u8 control; 668 669 if (old_termios && !pl2303_termios_change(&tty->termios, old_termios)) 670 return; 671 672 buf = kzalloc(7, GFP_KERNEL); 673 if (!buf) { 674 /* Report back no change occurred */ 675 if (old_termios) 676 tty->termios = *old_termios; 677 return; 678 } 679 680 pl2303_get_line_request(port, buf); 681 682 switch (C_CSIZE(tty)) { 683 case CS5: 684 buf[6] = 5; 685 break; 686 case CS6: 687 buf[6] = 6; 688 break; 689 case CS7: 690 buf[6] = 7; 691 break; 692 default: 693 case CS8: 694 buf[6] = 8; 695 } 696 dev_dbg(&port->dev, "data bits = %d\n", buf[6]); 697 698 /* For reference buf[0]:buf[3] baud rate value */ 699 pl2303_encode_baud_rate(tty, port, &buf[0]); 700 701 /* For reference buf[4]=0 is 1 stop bits */ 702 /* For reference buf[4]=1 is 1.5 stop bits */ 703 /* For reference buf[4]=2 is 2 stop bits */ 704 if (C_CSTOPB(tty)) { 705 /* 706 * NOTE: Comply with "real" UARTs / RS232: 707 * use 1.5 instead of 2 stop bits with 5 data bits 708 */ 709 if (C_CSIZE(tty) == CS5) { 710 buf[4] = 1; 711 dev_dbg(&port->dev, "stop bits = 1.5\n"); 712 } else { 713 buf[4] = 2; 714 dev_dbg(&port->dev, "stop bits = 2\n"); 715 } 716 } else { 717 buf[4] = 0; 718 dev_dbg(&port->dev, "stop bits = 1\n"); 719 } 720 721 if (C_PARENB(tty)) { 722 /* For reference buf[5]=0 is none parity */ 723 /* For reference buf[5]=1 is odd parity */ 724 /* For reference buf[5]=2 is even parity */ 725 /* For reference buf[5]=3 is mark parity */ 726 /* For reference buf[5]=4 is space parity */ 727 if (C_PARODD(tty)) { 728 if (C_CMSPAR(tty)) { 729 buf[5] = 3; 730 dev_dbg(&port->dev, "parity = mark\n"); 731 } else { 732 buf[5] = 1; 733 dev_dbg(&port->dev, "parity = odd\n"); 734 } 735 } else { 736 if (C_CMSPAR(tty)) { 737 buf[5] = 4; 738 dev_dbg(&port->dev, "parity = space\n"); 739 } else { 740 buf[5] = 2; 741 dev_dbg(&port->dev, "parity = even\n"); 742 } 743 } 744 } else { 745 buf[5] = 0; 746 dev_dbg(&port->dev, "parity = none\n"); 747 } 748 749 /* 750 * Some PL2303 are known to lose bytes if you change serial settings 751 * even to the same values as before. Thus we actually need to filter 752 * in this specific case. 753 * 754 * Note that the tty_termios_hw_change check above is not sufficient 755 * as a previously requested baud rate may differ from the one 756 * actually used (and stored in old_termios). 757 * 758 * NOTE: No additional locking needed for line_settings as it is 759 * only used in set_termios, which is serialised against itself. 760 */ 761 if (!old_termios || memcmp(buf, priv->line_settings, 7)) { 762 ret = pl2303_set_line_request(port, buf); 763 if (!ret) 764 memcpy(priv->line_settings, buf, 7); 765 } 766 767 /* change control lines if we are switching to or from B0 */ 768 spin_lock_irqsave(&priv->lock, flags); 769 control = priv->line_control; 770 if (C_BAUD(tty) == B0) 771 priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS); 772 else if (old_termios && (old_termios->c_cflag & CBAUD) == B0) 773 priv->line_control |= (CONTROL_DTR | CONTROL_RTS); 774 if (control != priv->line_control) { 775 control = priv->line_control; 776 spin_unlock_irqrestore(&priv->lock, flags); 777 pl2303_set_control_lines(port, control); 778 } else { 779 spin_unlock_irqrestore(&priv->lock, flags); 780 } 781 782 if (C_CRTSCTS(tty)) { 783 if (spriv->quirks & PL2303_QUIRK_LEGACY) { 784 pl2303_update_reg(serial, 0, PL2303_FLOWCTRL_MASK, 0x40); 785 } else if (spriv->type == &pl2303_type_data[TYPE_HXN]) { 786 pl2303_update_reg(serial, PL2303_HXN_FLOWCTRL_REG, 787 PL2303_HXN_FLOWCTRL_MASK, 788 PL2303_HXN_FLOWCTRL_RTS_CTS); 789 } else { 790 pl2303_update_reg(serial, 0, PL2303_FLOWCTRL_MASK, 0x60); 791 } 792 } else if (pl2303_enable_xonxoff(tty, spriv->type)) { 793 if (spriv->type == &pl2303_type_data[TYPE_HXN]) { 794 pl2303_update_reg(serial, PL2303_HXN_FLOWCTRL_REG, 795 PL2303_HXN_FLOWCTRL_MASK, 796 PL2303_HXN_FLOWCTRL_XON_XOFF); 797 } else { 798 pl2303_update_reg(serial, 0, PL2303_FLOWCTRL_MASK, 0xc0); 799 } 800 } else { 801 if (spriv->type == &pl2303_type_data[TYPE_HXN]) { 802 pl2303_update_reg(serial, PL2303_HXN_FLOWCTRL_REG, 803 PL2303_HXN_FLOWCTRL_MASK, 804 PL2303_HXN_FLOWCTRL_NONE); 805 } else { 806 pl2303_update_reg(serial, 0, PL2303_FLOWCTRL_MASK, 0); 807 } 808 } 809 810 kfree(buf); 811 } 812 813 static void pl2303_dtr_rts(struct usb_serial_port *port, int on) 814 { 815 struct pl2303_private *priv = usb_get_serial_port_data(port); 816 unsigned long flags; 817 u8 control; 818 819 spin_lock_irqsave(&priv->lock, flags); 820 if (on) 821 priv->line_control |= (CONTROL_DTR | CONTROL_RTS); 822 else 823 priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS); 824 control = priv->line_control; 825 spin_unlock_irqrestore(&priv->lock, flags); 826 827 pl2303_set_control_lines(port, control); 828 } 829 830 static void pl2303_close(struct usb_serial_port *port) 831 { 832 usb_serial_generic_close(port); 833 usb_kill_urb(port->interrupt_in_urb); 834 pl2303_set_break(port, false); 835 } 836 837 static int pl2303_open(struct tty_struct *tty, struct usb_serial_port *port) 838 { 839 struct usb_serial *serial = port->serial; 840 struct pl2303_serial_private *spriv = usb_get_serial_data(serial); 841 int result; 842 843 if (spriv->quirks & PL2303_QUIRK_LEGACY) { 844 usb_clear_halt(serial->dev, port->write_urb->pipe); 845 usb_clear_halt(serial->dev, port->read_urb->pipe); 846 } else { 847 /* reset upstream data pipes */ 848 if (spriv->type == &pl2303_type_data[TYPE_HXN]) { 849 pl2303_vendor_write(serial, PL2303_HXN_RESET_REG, 850 PL2303_HXN_RESET_UPSTREAM_PIPE | 851 PL2303_HXN_RESET_DOWNSTREAM_PIPE); 852 } else { 853 pl2303_vendor_write(serial, 8, 0); 854 pl2303_vendor_write(serial, 9, 0); 855 } 856 } 857 858 /* Setup termios */ 859 if (tty) 860 pl2303_set_termios(tty, port, NULL); 861 862 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL); 863 if (result) { 864 dev_err(&port->dev, "failed to submit interrupt urb: %d\n", 865 result); 866 return result; 867 } 868 869 result = usb_serial_generic_open(tty, port); 870 if (result) { 871 usb_kill_urb(port->interrupt_in_urb); 872 return result; 873 } 874 875 return 0; 876 } 877 878 static int pl2303_tiocmset(struct tty_struct *tty, 879 unsigned int set, unsigned int clear) 880 { 881 struct usb_serial_port *port = tty->driver_data; 882 struct pl2303_private *priv = usb_get_serial_port_data(port); 883 unsigned long flags; 884 u8 control; 885 int ret; 886 887 spin_lock_irqsave(&priv->lock, flags); 888 if (set & TIOCM_RTS) 889 priv->line_control |= CONTROL_RTS; 890 if (set & TIOCM_DTR) 891 priv->line_control |= CONTROL_DTR; 892 if (clear & TIOCM_RTS) 893 priv->line_control &= ~CONTROL_RTS; 894 if (clear & TIOCM_DTR) 895 priv->line_control &= ~CONTROL_DTR; 896 control = priv->line_control; 897 spin_unlock_irqrestore(&priv->lock, flags); 898 899 ret = pl2303_set_control_lines(port, control); 900 if (ret) 901 return usb_translate_errors(ret); 902 903 return 0; 904 } 905 906 static int pl2303_tiocmget(struct tty_struct *tty) 907 { 908 struct usb_serial_port *port = tty->driver_data; 909 struct pl2303_private *priv = usb_get_serial_port_data(port); 910 unsigned long flags; 911 unsigned int mcr; 912 unsigned int status; 913 unsigned int result; 914 915 spin_lock_irqsave(&priv->lock, flags); 916 mcr = priv->line_control; 917 status = priv->line_status; 918 spin_unlock_irqrestore(&priv->lock, flags); 919 920 result = ((mcr & CONTROL_DTR) ? TIOCM_DTR : 0) 921 | ((mcr & CONTROL_RTS) ? TIOCM_RTS : 0) 922 | ((status & UART_CTS) ? TIOCM_CTS : 0) 923 | ((status & UART_DSR) ? TIOCM_DSR : 0) 924 | ((status & UART_RING) ? TIOCM_RI : 0) 925 | ((status & UART_DCD) ? TIOCM_CD : 0); 926 927 dev_dbg(&port->dev, "%s - result = %x\n", __func__, result); 928 929 return result; 930 } 931 932 static int pl2303_carrier_raised(struct usb_serial_port *port) 933 { 934 struct pl2303_private *priv = usb_get_serial_port_data(port); 935 936 if (priv->line_status & UART_DCD) 937 return 1; 938 939 return 0; 940 } 941 942 static int pl2303_get_serial(struct tty_struct *tty, 943 struct serial_struct *ss) 944 { 945 struct usb_serial_port *port = tty->driver_data; 946 947 ss->type = PORT_16654; 948 ss->line = port->minor; 949 ss->port = port->port_number; 950 ss->baud_base = 460800; 951 return 0; 952 } 953 954 static void pl2303_set_break(struct usb_serial_port *port, bool enable) 955 { 956 struct usb_serial *serial = port->serial; 957 u16 state; 958 int result; 959 960 if (enable) 961 state = BREAK_ON; 962 else 963 state = BREAK_OFF; 964 965 dev_dbg(&port->dev, "%s - turning break %s\n", __func__, 966 state == BREAK_OFF ? "off" : "on"); 967 968 result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), 969 BREAK_REQUEST, BREAK_REQUEST_TYPE, state, 970 0, NULL, 0, 100); 971 if (result) 972 dev_err(&port->dev, "error sending break = %d\n", result); 973 } 974 975 static void pl2303_break_ctl(struct tty_struct *tty, int state) 976 { 977 struct usb_serial_port *port = tty->driver_data; 978 979 pl2303_set_break(port, state); 980 } 981 982 static void pl2303_update_line_status(struct usb_serial_port *port, 983 unsigned char *data, 984 unsigned int actual_length) 985 { 986 struct usb_serial *serial = port->serial; 987 struct pl2303_serial_private *spriv = usb_get_serial_data(serial); 988 struct pl2303_private *priv = usb_get_serial_port_data(port); 989 struct tty_struct *tty; 990 unsigned long flags; 991 unsigned int status_idx = UART_STATE_INDEX; 992 u8 status; 993 u8 delta; 994 995 if (spriv->quirks & PL2303_QUIRK_UART_STATE_IDX0) 996 status_idx = 0; 997 998 if (actual_length < status_idx + 1) 999 return; 1000 1001 status = data[status_idx]; 1002 1003 /* Save off the uart status for others to look at */ 1004 spin_lock_irqsave(&priv->lock, flags); 1005 delta = priv->line_status ^ status; 1006 priv->line_status = status; 1007 spin_unlock_irqrestore(&priv->lock, flags); 1008 1009 if (status & UART_BREAK_ERROR) 1010 usb_serial_handle_break(port); 1011 1012 if (delta & UART_STATE_MSR_MASK) { 1013 if (delta & UART_CTS) 1014 port->icount.cts++; 1015 if (delta & UART_DSR) 1016 port->icount.dsr++; 1017 if (delta & UART_RING) 1018 port->icount.rng++; 1019 if (delta & UART_DCD) { 1020 port->icount.dcd++; 1021 tty = tty_port_tty_get(&port->port); 1022 if (tty) { 1023 usb_serial_handle_dcd_change(port, tty, 1024 status & UART_DCD); 1025 tty_kref_put(tty); 1026 } 1027 } 1028 1029 wake_up_interruptible(&port->port.delta_msr_wait); 1030 } 1031 } 1032 1033 static void pl2303_read_int_callback(struct urb *urb) 1034 { 1035 struct usb_serial_port *port = urb->context; 1036 unsigned char *data = urb->transfer_buffer; 1037 unsigned int actual_length = urb->actual_length; 1038 int status = urb->status; 1039 int retval; 1040 1041 switch (status) { 1042 case 0: 1043 /* success */ 1044 break; 1045 case -ECONNRESET: 1046 case -ENOENT: 1047 case -ESHUTDOWN: 1048 /* this urb is terminated, clean up */ 1049 dev_dbg(&port->dev, "%s - urb shutting down with status: %d\n", 1050 __func__, status); 1051 return; 1052 default: 1053 dev_dbg(&port->dev, "%s - nonzero urb status received: %d\n", 1054 __func__, status); 1055 goto exit; 1056 } 1057 1058 usb_serial_debug_data(&port->dev, __func__, 1059 urb->actual_length, urb->transfer_buffer); 1060 1061 pl2303_update_line_status(port, data, actual_length); 1062 1063 exit: 1064 retval = usb_submit_urb(urb, GFP_ATOMIC); 1065 if (retval) { 1066 dev_err(&port->dev, 1067 "%s - usb_submit_urb failed with result %d\n", 1068 __func__, retval); 1069 } 1070 } 1071 1072 static void pl2303_process_read_urb(struct urb *urb) 1073 { 1074 struct usb_serial_port *port = urb->context; 1075 struct pl2303_private *priv = usb_get_serial_port_data(port); 1076 unsigned char *data = urb->transfer_buffer; 1077 char tty_flag = TTY_NORMAL; 1078 unsigned long flags; 1079 u8 line_status; 1080 int i; 1081 1082 /* update line status */ 1083 spin_lock_irqsave(&priv->lock, flags); 1084 line_status = priv->line_status; 1085 priv->line_status &= ~UART_STATE_TRANSIENT_MASK; 1086 spin_unlock_irqrestore(&priv->lock, flags); 1087 1088 if (!urb->actual_length) 1089 return; 1090 1091 /* 1092 * Break takes precedence over parity, which takes precedence over 1093 * framing errors. 1094 */ 1095 if (line_status & UART_BREAK_ERROR) 1096 tty_flag = TTY_BREAK; 1097 else if (line_status & UART_PARITY_ERROR) 1098 tty_flag = TTY_PARITY; 1099 else if (line_status & UART_FRAME_ERROR) 1100 tty_flag = TTY_FRAME; 1101 1102 if (tty_flag != TTY_NORMAL) 1103 dev_dbg(&port->dev, "%s - tty_flag = %d\n", __func__, 1104 tty_flag); 1105 /* overrun is special, not associated with a char */ 1106 if (line_status & UART_OVERRUN_ERROR) 1107 tty_insert_flip_char(&port->port, 0, TTY_OVERRUN); 1108 1109 if (port->sysrq) { 1110 for (i = 0; i < urb->actual_length; ++i) 1111 if (!usb_serial_handle_sysrq_char(port, data[i])) 1112 tty_insert_flip_char(&port->port, data[i], 1113 tty_flag); 1114 } else { 1115 tty_insert_flip_string_fixed_flag(&port->port, data, tty_flag, 1116 urb->actual_length); 1117 } 1118 1119 tty_flip_buffer_push(&port->port); 1120 } 1121 1122 static struct usb_serial_driver pl2303_device = { 1123 .driver = { 1124 .owner = THIS_MODULE, 1125 .name = "pl2303", 1126 }, 1127 .id_table = id_table, 1128 .num_bulk_in = 1, 1129 .num_bulk_out = 1, 1130 .num_interrupt_in = 0, /* see pl2303_calc_num_ports */ 1131 .bulk_in_size = 256, 1132 .bulk_out_size = 256, 1133 .open = pl2303_open, 1134 .close = pl2303_close, 1135 .dtr_rts = pl2303_dtr_rts, 1136 .carrier_raised = pl2303_carrier_raised, 1137 .get_serial = pl2303_get_serial, 1138 .break_ctl = pl2303_break_ctl, 1139 .set_termios = pl2303_set_termios, 1140 .tiocmget = pl2303_tiocmget, 1141 .tiocmset = pl2303_tiocmset, 1142 .tiocmiwait = usb_serial_generic_tiocmiwait, 1143 .process_read_urb = pl2303_process_read_urb, 1144 .read_int_callback = pl2303_read_int_callback, 1145 .probe = pl2303_probe, 1146 .calc_num_ports = pl2303_calc_num_ports, 1147 .attach = pl2303_startup, 1148 .release = pl2303_release, 1149 .port_probe = pl2303_port_probe, 1150 .port_remove = pl2303_port_remove, 1151 }; 1152 1153 static struct usb_serial_driver * const serial_drivers[] = { 1154 &pl2303_device, NULL 1155 }; 1156 1157 module_usb_serial_driver(serial_drivers, id_table); 1158 1159 MODULE_DESCRIPTION("Prolific PL2303 USB to serial adaptor driver"); 1160 MODULE_LICENSE("GPL v2"); 1161