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