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