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