root/drivers/usb/serial/pl2303.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. pl2303_vendor_read
  2. pl2303_vendor_write
  3. pl2303_update_reg
  4. pl2303_probe
  5. pl2303_endpoint_hack
  6. pl2303_calc_num_ports
  7. pl2303_startup
  8. pl2303_release
  9. pl2303_port_probe
  10. pl2303_port_remove
  11. pl2303_set_control_lines
  12. pl2303_get_supported_baud_rate
  13. pl2303_encode_baud_rate_direct
  14. pl2303_encode_baud_rate_divisor
  15. pl2303_encode_baud_rate
  16. pl2303_get_line_request
  17. pl2303_set_line_request
  18. pl2303_termios_change
  19. pl2303_enable_xonxoff
  20. pl2303_set_termios
  21. pl2303_dtr_rts
  22. pl2303_close
  23. pl2303_open
  24. pl2303_tiocmset
  25. pl2303_tiocmget
  26. pl2303_carrier_raised
  27. pl2303_get_serial
  28. pl2303_set_break
  29. pl2303_break_ctl
  30. pl2303_update_line_status
  31. pl2303_read_int_callback
  32. pl2303_process_read_urb

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

/* [<][>][^][v][top][bottom][index][help] */