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