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