USB: serial: pl2303: add new ATEN device id
[karo-tx-linux.git] / drivers / usb / serial / pl2303.c
1 /*
2  * Prolific PL2303 USB to serial adaptor driver
3  *
4  * Copyright (C) 2001-2007 Greg Kroah-Hartman (greg@kroah.com)
5  * Copyright (C) 2003 IBM Corp.
6  *
7  * Original driver for 2.2.x by anonymous
8  *
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.
12  *
13  * See Documentation/usb/usb-serial.txt for more information on using this
14  * driver
15  */
16
17 #include <linux/kernel.h>
18 #include <linux/errno.h>
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>
27 #include <linux/uaccess.h>
28 #include <linux/usb.h>
29 #include <linux/usb/serial.h>
30 #include <asm/unaligned.h>
31 #include "pl2303.h"
32
33
34 #define PL2303_QUIRK_UART_STATE_IDX0            BIT(0)
35 #define PL2303_QUIRK_LEGACY                     BIT(1)
36 #define PL2303_QUIRK_ENDPOINT_HACK              BIT(2)
37
38 static const struct usb_device_id id_table[] = {
39         { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID),
40                 .driver_info = PL2303_QUIRK_ENDPOINT_HACK },
41         { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_RSAQ2) },
42         { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_DCU11) },
43         { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_RSAQ3) },
44         { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_PHAROS) },
45         { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_ALDIGA) },
46         { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_MMX) },
47         { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_GPRS) },
48         { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_HCR331) },
49         { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_MOTOROLA) },
50         { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_ZTEK) },
51         { USB_DEVICE(IODATA_VENDOR_ID, IODATA_PRODUCT_ID) },
52         { USB_DEVICE(IODATA_VENDOR_ID, IODATA_PRODUCT_ID_RSAQ5) },
53         { USB_DEVICE(ATEN_VENDOR_ID, ATEN_PRODUCT_ID),
54                 .driver_info = PL2303_QUIRK_ENDPOINT_HACK },
55         { USB_DEVICE(ATEN_VENDOR_ID, ATEN_PRODUCT_UC485),
56                 .driver_info = PL2303_QUIRK_ENDPOINT_HACK },
57         { USB_DEVICE(ATEN_VENDOR_ID, ATEN_PRODUCT_ID2) },
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) },
62         { USB_DEVICE(ITEGNO_VENDOR_ID, ITEGNO_PRODUCT_ID_2080) },
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) },
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 },
76         { USB_DEVICE(SIEMENS_VENDOR_ID, SIEMENS_PRODUCT_ID_EF81),
77                 .driver_info = PL2303_QUIRK_ENDPOINT_HACK },
78         { USB_DEVICE(BENQ_VENDOR_ID, BENQ_PRODUCT_ID_S81) }, /* Benq/Siemens S81 */
79         { USB_DEVICE(SYNTECH_VENDOR_ID, SYNTECH_PRODUCT_ID) },
80         { USB_DEVICE(NOKIA_CA42_VENDOR_ID, NOKIA_CA42_PRODUCT_ID) },
81         { USB_DEVICE(CA_42_CA42_VENDOR_ID, CA_42_CA42_PRODUCT_ID) },
82         { USB_DEVICE(SAGEM_VENDOR_ID, SAGEM_PRODUCT_ID) },
83         { USB_DEVICE(LEADTEK_VENDOR_ID, LEADTEK_9531_PRODUCT_ID) },
84         { USB_DEVICE(SPEEDDRAGON_VENDOR_ID, SPEEDDRAGON_PRODUCT_ID) },
85         { USB_DEVICE(DATAPILOT_U2_VENDOR_ID, DATAPILOT_U2_PRODUCT_ID) },
86         { USB_DEVICE(BELKIN_VENDOR_ID, BELKIN_PRODUCT_ID) },
87         { USB_DEVICE(ALCOR_VENDOR_ID, ALCOR_PRODUCT_ID),
88                 .driver_info = PL2303_QUIRK_ENDPOINT_HACK },
89         { USB_DEVICE(WS002IN_VENDOR_ID, WS002IN_PRODUCT_ID) },
90         { USB_DEVICE(COREGA_VENDOR_ID, COREGA_PRODUCT_ID) },
91         { USB_DEVICE(YCCABLE_VENDOR_ID, YCCABLE_PRODUCT_ID) },
92         { USB_DEVICE(SUPERIAL_VENDOR_ID, SUPERIAL_PRODUCT_ID) },
93         { USB_DEVICE(HP_VENDOR_ID, HP_LD220_PRODUCT_ID) },
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) },
97         { USB_DEVICE(CRESSI_VENDOR_ID, CRESSI_EDY_PRODUCT_ID) },
98         { USB_DEVICE(ZEAGLE_VENDOR_ID, ZEAGLE_N2ITION3_PRODUCT_ID) },
99         { USB_DEVICE(SONY_VENDOR_ID, SONY_QN3USB_PRODUCT_ID) },
100         { USB_DEVICE(SANWA_VENDOR_ID, SANWA_PRODUCT_ID) },
101         { USB_DEVICE(ADLINK_VENDOR_ID, ADLINK_ND6530_PRODUCT_ID) },
102         { USB_DEVICE(SMART_VENDOR_ID, SMART_PRODUCT_ID) },
103         { }                                     /* Terminating entry */
104 };
105
106 MODULE_DEVICE_TABLE(usb, id_table);
107
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
117 #define BREAK_REQUEST                   0x23
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
130 #define UART_STATE_INDEX                8
131 #define UART_STATE_MSR_MASK             0x8b
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
142 static void pl2303_set_break(struct usb_serial_port *port, bool enable);
143
144 enum pl2303_type {
145         TYPE_01,        /* Type 0 and 1 (difference unknown) */
146         TYPE_HX,        /* HX version of the pl2303 chip */
147         TYPE_COUNT
148 };
149
150 struct pl2303_type_data {
151         speed_t max_baud_rate;
152         unsigned long quirks;
153 };
154
155 struct pl2303_serial_private {
156         const struct pl2303_type_data *type;
157         unsigned long quirks;
158 };
159
160 struct pl2303_private {
161         spinlock_t lock;
162         u8 line_control;
163         u8 line_status;
164
165         u8 line_settings[7];
166 };
167
168 static const struct pl2303_type_data pl2303_type_data[TYPE_COUNT] = {
169         [TYPE_01] = {
170                 .max_baud_rate =        1228800,
171                 .quirks =               PL2303_QUIRK_LEGACY,
172         },
173         [TYPE_HX] = {
174                 .max_baud_rate =        12000000,
175         },
176 };
177
178 static int pl2303_vendor_read(struct usb_serial *serial, u16 value,
179                                                         unsigned char buf[1])
180 {
181         struct device *dev = &serial->interface->dev;
182         int res;
183
184         res = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
185                         VENDOR_READ_REQUEST, VENDOR_READ_REQUEST_TYPE,
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         }
195
196         dev_dbg(dev, "%s - [%04x] = %02x\n", __func__, value, buf[0]);
197
198         return 0;
199 }
200
201 static int pl2303_vendor_write(struct usb_serial *serial, u16 value, u16 index)
202 {
203         struct device *dev = &serial->interface->dev;
204         int res;
205
206         dev_dbg(dev, "%s - [%04x] = %02x\n", __func__, value, index);
207
208         res = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
209                         VENDOR_WRITE_REQUEST, VENDOR_WRITE_REQUEST_TYPE,
210                         value, index, NULL, 0, 100);
211         if (res) {
212                 dev_err(dev, "%s - failed to write [%04x]: %d\n", __func__,
213                                                                 value, res);
214                 return res;
215         }
216
217         return 0;
218 }
219
220 static 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
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  */
233 static int pl2303_endpoint_hack(struct usb_serial *serial,
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
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
263 static 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;
274         }
275
276         if (epds->num_interrupt_in < 1) {
277                 dev_err(dev, "required interrupt-in endpoint missing\n");
278                 return -ENODEV;
279         }
280
281         return 1;
282 }
283
284 static int pl2303_startup(struct usb_serial *serial)
285 {
286         struct pl2303_serial_private *spriv;
287         enum pl2303_type type = TYPE_01;
288         unsigned char *buf;
289
290         spriv = kzalloc(sizeof(*spriv), GFP_KERNEL);
291         if (!spriv)
292                 return -ENOMEM;
293
294         buf = kmalloc(1, GFP_KERNEL);
295         if (!buf) {
296                 kfree(spriv);
297                 return -ENOMEM;
298         }
299
300         if (serial->dev->descriptor.bDeviceClass == 0x02)
301                 type = TYPE_01;         /* type 0 */
302         else if (serial->dev->descriptor.bMaxPacketSize0 == 0x40)
303                 type = TYPE_HX;
304         else if (serial->dev->descriptor.bDeviceClass == 0x00)
305                 type = TYPE_01;         /* type 1 */
306         else if (serial->dev->descriptor.bDeviceClass == 0xFF)
307                 type = TYPE_01;         /* type 1 */
308         dev_dbg(&serial->interface->dev, "device type: %d\n", type);
309
310         spriv->type = &pl2303_type_data[type];
311         spriv->quirks = (unsigned long)usb_get_serial_data(serial);
312         spriv->quirks |= spriv->type->quirks;
313
314         usb_set_serial_data(serial, spriv);
315
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);
326         if (spriv->quirks & PL2303_QUIRK_LEGACY)
327                 pl2303_vendor_write(serial, 2, 0x24);
328         else
329                 pl2303_vendor_write(serial, 2, 0x44);
330
331         kfree(buf);
332
333         return 0;
334 }
335
336 static void pl2303_release(struct usb_serial *serial)
337 {
338         struct pl2303_serial_private *spriv = usb_get_serial_data(serial);
339
340         kfree(spriv);
341 }
342
343 static 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);
352
353         usb_set_serial_port_data(port, priv);
354
355         port->port.drain_delay = 256;
356
357         return 0;
358 }
359
360 static int pl2303_port_remove(struct usb_serial_port *port)
361 {
362         struct pl2303_private *priv = usb_get_serial_port_data(port);
363
364         kfree(priv);
365
366         return 0;
367 }
368
369 static int pl2303_set_control_lines(struct usb_serial_port *port, u8 value)
370 {
371         struct usb_device *dev = port->serial->dev;
372         int retval;
373
374         dev_dbg(&port->dev, "%s - %02x\n", __func__, value);
375
376         retval = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
377                                  SET_CONTROL_REQUEST, SET_CONTROL_REQUEST_TYPE,
378                                  value, 0, NULL, 0, 100);
379         if (retval)
380                 dev_err(&port->dev, "%s - failed: %d\n", __func__, retval);
381
382         return retval;
383 }
384
385 /*
386  * Returns the nearest supported baud rate that can be set directly without
387  * using divisors.
388  */
389 static speed_t pl2303_get_supported_baud_rate(speed_t baud)
390 {
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,
394                 614400, 921600, 1228800, 2457600, 3000000, 6000000
395         };
396
397         unsigned i;
398
399         for (i = 0; i < ARRAY_SIZE(baud_sup); ++i) {
400                 if (baud_sup[i] > baud)
401                         break;
402         }
403
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];
410
411         return baud;
412 }
413
414 /*
415  * NOTE: If unsupported baud rates are set directly, the PL2303 seems to
416  *       use 9600 baud.
417  */
418 static 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
426 static speed_t pl2303_encode_baud_rate_divisor(unsigned char buf[4],
427                                                                 speed_t baud)
428 {
429         unsigned int baseline, mantissa, exponent;
430
431         /*
432          * Apparently the formula is:
433          *   baudrate = 12M * 32 / (mantissa * 4^exponent)
434          * where
435          *   mantissa = buf[8:0]
436          *   exponent = buf[11:9]
437          */
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
454         buf[3] = 0x80;
455         buf[2] = 0;
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);
461
462         return baud;
463 }
464
465 static 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);
471         speed_t baud_sup;
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;
478
479         if (spriv->type->max_baud_rate)
480                 baud = min_t(speed_t, baud, spriv->type->max_baud_rate);
481         /*
482          * Use direct method for supported baud rates, otherwise use divisors.
483          */
484         baud_sup = pl2303_get_supported_baud_rate(baud);
485
486         if (baud == baud_sup)
487                 baud = pl2303_encode_baud_rate_direct(buf, baud);
488         else
489                 baud = pl2303_encode_baud_rate_divisor(buf, baud);
490
491         /* Save resulting baud rate */
492         tty_encode_baud_rate(tty, baud, baud);
493         dev_dbg(&port->dev, "baud set = %u\n", baud);
494 }
495
496 static 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
508                 if (ret >= 0)
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
519 static 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);
528         if (ret < 0) {
529                 dev_err(&port->dev, "%s - failed: %d\n", __func__, ret);
530                 return ret;
531         }
532
533         dev_dbg(&port->dev, "%s - %7ph\n", __func__, buf);
534
535         return 0;
536 }
537
538 static 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;
545         unsigned char *buf;
546         int ret;
547         u8 control;
548
549         if (old_termios && !tty_termios_hw_change(&tty->termios, old_termios))
550                 return;
551
552         buf = kzalloc(7, GFP_KERNEL);
553         if (!buf) {
554                 /* Report back no change occurred */
555                 if (old_termios)
556                         tty->termios = *old_termios;
557                 return;
558         }
559
560         pl2303_get_line_request(port, buf);
561
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;
575         }
576         dev_dbg(&port->dev, "data bits = %d\n", buf[6]);
577
578         /* For reference buf[0]:buf[3] baud rate value */
579         pl2303_encode_baud_rate(tty, port, &buf[0]);
580
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 */
584         if (C_CSTOPB(tty)) {
585                 /*
586                  * NOTE: Comply with "real" UARTs / RS232:
587                  *       use 1.5 instead of 2 stop bits with 5 data bits
588                  */
589                 if (C_CSIZE(tty) == CS5) {
590                         buf[4] = 1;
591                         dev_dbg(&port->dev, "stop bits = 1.5\n");
592                 } else {
593                         buf[4] = 2;
594                         dev_dbg(&port->dev, "stop bits = 2\n");
595                 }
596         } else {
597                 buf[4] = 0;
598                 dev_dbg(&port->dev, "stop bits = 1\n");
599         }
600
601         if (C_PARENB(tty)) {
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 */
607                 if (C_PARODD(tty)) {
608                         if (C_CMSPAR(tty)) {
609                                 buf[5] = 3;
610                                 dev_dbg(&port->dev, "parity = mark\n");
611                         } else {
612                                 buf[5] = 1;
613                                 dev_dbg(&port->dev, "parity = odd\n");
614                         }
615                 } else {
616                         if (C_CMSPAR(tty)) {
617                                 buf[5] = 4;
618                                 dev_dbg(&port->dev, "parity = space\n");
619                         } else {
620                                 buf[5] = 2;
621                                 dev_dbg(&port->dev, "parity = even\n");
622                         }
623                 }
624         } else {
625                 buf[5] = 0;
626                 dev_dbg(&port->dev, "parity = none\n");
627         }
628
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)) {
642                 ret = pl2303_set_line_request(port, buf);
643                 if (!ret)
644                         memcpy(priv->line_settings, buf, 7);
645         }
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;
650         if (C_BAUD(tty) == B0)
651                 priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
652         else if (old_termios && (old_termios->c_cflag & CBAUD) == B0)
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);
657                 pl2303_set_control_lines(port, control);
658         } else {
659                 spin_unlock_irqrestore(&priv->lock, flags);
660         }
661
662         if (C_CRTSCTS(tty)) {
663                 if (spriv->quirks & PL2303_QUIRK_LEGACY)
664                         pl2303_vendor_write(serial, 0x0, 0x41);
665                 else
666                         pl2303_vendor_write(serial, 0x0, 0x61);
667         } else {
668                 pl2303_vendor_write(serial, 0x0, 0x0);
669         }
670
671         kfree(buf);
672 }
673
674 static 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);
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);
687
688         pl2303_set_control_lines(port, control);
689 }
690
691 static void pl2303_close(struct usb_serial_port *port)
692 {
693         usb_serial_generic_close(port);
694         usb_kill_urb(port->interrupt_in_urb);
695         pl2303_set_break(port, false);
696 }
697
698 static int pl2303_open(struct tty_struct *tty, struct usb_serial_port *port)
699 {
700         struct usb_serial *serial = port->serial;
701         struct pl2303_serial_private *spriv = usb_get_serial_data(serial);
702         int result;
703
704         if (spriv->quirks & PL2303_QUIRK_LEGACY) {
705                 usb_clear_halt(serial->dev, port->write_urb->pipe);
706                 usb_clear_halt(serial->dev, port->read_urb->pipe);
707         } else {
708                 /* reset upstream data pipes */
709                 pl2303_vendor_write(serial, 8, 0);
710                 pl2303_vendor_write(serial, 9, 0);
711         }
712
713         /* Setup termios */
714         if (tty)
715                 pl2303_set_termios(tty, port, NULL);
716
717         result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
718         if (result) {
719                 dev_err(&port->dev, "failed to submit interrupt urb: %d\n",
720                         result);
721                 return result;
722         }
723
724         result = usb_serial_generic_open(tty, port);
725         if (result) {
726                 usb_kill_urb(port->interrupt_in_urb);
727                 return result;
728         }
729
730         return 0;
731 }
732
733 static int pl2303_tiocmset(struct tty_struct *tty,
734                            unsigned int set, unsigned int clear)
735 {
736         struct usb_serial_port *port = tty->driver_data;
737         struct pl2303_private *priv = usb_get_serial_port_data(port);
738         unsigned long flags;
739         u8 control;
740         int ret;
741
742         spin_lock_irqsave(&priv->lock, flags);
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;
752         spin_unlock_irqrestore(&priv->lock, flags);
753
754         ret = pl2303_set_control_lines(port, control);
755         if (ret)
756                 return usb_translate_errors(ret);
757
758         return 0;
759 }
760
761 static int pl2303_tiocmget(struct tty_struct *tty)
762 {
763         struct usb_serial_port *port = tty->driver_data;
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
770         spin_lock_irqsave(&priv->lock, flags);
771         mcr = priv->line_control;
772         status = priv->line_status;
773         spin_unlock_irqrestore(&priv->lock, flags);
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
782         dev_dbg(&port->dev, "%s - result = %x\n", __func__, result);
783
784         return result;
785 }
786
787 static int pl2303_carrier_raised(struct usb_serial_port *port)
788 {
789         struct pl2303_private *priv = usb_get_serial_port_data(port);
790
791         if (priv->line_status & UART_DCD)
792                 return 1;
793
794         return 0;
795 }
796
797 static int pl2303_ioctl(struct tty_struct *tty,
798                         unsigned int cmd, unsigned long arg)
799 {
800         struct serial_struct ser;
801         struct usb_serial_port *port = tty->driver_data;
802
803         switch (cmd) {
804         case TIOCGSERIAL:
805                 memset(&ser, 0, sizeof ser);
806                 ser.type = PORT_16654;
807                 ser.line = port->minor;
808                 ser.port = port->port_number;
809                 ser.baud_base = 460800;
810
811                 if (copy_to_user((void __user *)arg, &ser, sizeof ser))
812                         return -EFAULT;
813
814                 return 0;
815         default:
816                 break;
817         }
818
819         return -ENOIOCTLCMD;
820 }
821
822 static void pl2303_set_break(struct usb_serial_port *port, bool enable)
823 {
824         struct usb_serial *serial = port->serial;
825         u16 state;
826         int result;
827
828         if (enable)
829                 state = BREAK_ON;
830         else
831                 state = BREAK_OFF;
832
833         dev_dbg(&port->dev, "%s - turning break %s\n", __func__,
834                         state == BREAK_OFF ? "off" : "on");
835
836         result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
837                                  BREAK_REQUEST, BREAK_REQUEST_TYPE, state,
838                                  0, NULL, 0, 100);
839         if (result)
840                 dev_err(&port->dev, "error sending break = %d\n", result);
841 }
842
843 static 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
850 static void pl2303_update_line_status(struct usb_serial_port *port,
851                                       unsigned char *data,
852                                       unsigned int actual_length)
853 {
854         struct usb_serial *serial = port->serial;
855         struct pl2303_serial_private *spriv = usb_get_serial_data(serial);
856         struct pl2303_private *priv = usb_get_serial_port_data(port);
857         struct tty_struct *tty;
858         unsigned long flags;
859         unsigned int status_idx = UART_STATE_INDEX;
860         u8 status;
861         u8 delta;
862
863         if (spriv->quirks & PL2303_QUIRK_UART_STATE_IDX0)
864                 status_idx = 0;
865
866         if (actual_length < status_idx + 1)
867                 return;
868
869         status = data[status_idx];
870
871         /* Save off the uart status for others to look at */
872         spin_lock_irqsave(&priv->lock, flags);
873         delta = priv->line_status ^ status;
874         priv->line_status = status;
875         spin_unlock_irqrestore(&priv->lock, flags);
876
877         if (status & UART_BREAK_ERROR)
878                 usb_serial_handle_break(port);
879
880         if (delta & UART_STATE_MSR_MASK) {
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++;
887                 if (delta & UART_DCD) {
888                         port->icount.dcd++;
889                         tty = tty_port_tty_get(&port->port);
890                         if (tty) {
891                                 usb_serial_handle_dcd_change(port, tty,
892                                                         status & UART_DCD);
893                                 tty_kref_put(tty);
894                         }
895                 }
896
897                 wake_up_interruptible(&port->port.delta_msr_wait);
898         }
899 }
900
901 static void pl2303_read_int_callback(struct urb *urb)
902 {
903         struct usb_serial_port *port =  urb->context;
904         unsigned char *data = urb->transfer_buffer;
905         unsigned int actual_length = urb->actual_length;
906         int status = urb->status;
907         int retval;
908
909         switch (status) {
910         case 0:
911                 /* success */
912                 break;
913         case -ECONNRESET:
914         case -ENOENT:
915         case -ESHUTDOWN:
916                 /* this urb is terminated, clean up */
917                 dev_dbg(&port->dev, "%s - urb shutting down with status: %d\n",
918                         __func__, status);
919                 return;
920         default:
921                 dev_dbg(&port->dev, "%s - nonzero urb status received: %d\n",
922                         __func__, status);
923                 goto exit;
924         }
925
926         usb_serial_debug_data(&port->dev, __func__,
927                               urb->actual_length, urb->transfer_buffer);
928
929         pl2303_update_line_status(port, data, actual_length);
930
931 exit:
932         retval = usb_submit_urb(urb, GFP_ATOMIC);
933         if (retval) {
934                 dev_err(&port->dev,
935                         "%s - usb_submit_urb failed with result %d\n",
936                         __func__, retval);
937         }
938 }
939
940 static void pl2303_process_read_urb(struct urb *urb)
941 {
942         struct usb_serial_port *port = urb->context;
943         struct pl2303_private *priv = usb_get_serial_port_data(port);
944         unsigned char *data = urb->transfer_buffer;
945         char tty_flag = TTY_NORMAL;
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);
955
956         if (!urb->actual_length)
957                 return;
958
959         /*
960          * Break takes precedence over parity, which takes precedence over
961          * framing errors.
962          */
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;
969
970         if (tty_flag != TTY_NORMAL)
971                 dev_dbg(&port->dev, "%s - tty_flag = %d\n", __func__,
972                                                                 tty_flag);
973         /* overrun is special, not associated with a char */
974         if (line_status & UART_OVERRUN_ERROR)
975                 tty_insert_flip_char(&port->port, 0, TTY_OVERRUN);
976
977         if (port->port.console && port->sysrq) {
978                 for (i = 0; i < urb->actual_length; ++i)
979                         if (!usb_serial_handle_sysrq_char(port, data[i]))
980                                 tty_insert_flip_char(&port->port, data[i],
981                                                 tty_flag);
982         } else {
983                 tty_insert_flip_string_fixed_flag(&port->port, data, tty_flag,
984                                                         urb->actual_length);
985         }
986
987         tty_flip_buffer_push(&port->port);
988 }
989
990 static struct usb_serial_driver pl2303_device = {
991         .driver = {
992                 .owner =        THIS_MODULE,
993                 .name =         "pl2303",
994         },
995         .id_table =             id_table,
996         .num_bulk_in =          1,
997         .num_bulk_out =         1,
998         .num_interrupt_in =     0,      /* see pl2303_calc_num_ports */
999         .bulk_in_size =         256,
1000         .bulk_out_size =        256,
1001         .open =                 pl2303_open,
1002         .close =                pl2303_close,
1003         .dtr_rts =              pl2303_dtr_rts,
1004         .carrier_raised =       pl2303_carrier_raised,
1005         .ioctl =                pl2303_ioctl,
1006         .break_ctl =            pl2303_break_ctl,
1007         .set_termios =          pl2303_set_termios,
1008         .tiocmget =             pl2303_tiocmget,
1009         .tiocmset =             pl2303_tiocmset,
1010         .tiocmiwait =           usb_serial_generic_tiocmiwait,
1011         .process_read_urb =     pl2303_process_read_urb,
1012         .read_int_callback =    pl2303_read_int_callback,
1013         .probe =                pl2303_probe,
1014         .calc_num_ports =       pl2303_calc_num_ports,
1015         .attach =               pl2303_startup,
1016         .release =              pl2303_release,
1017         .port_probe =           pl2303_port_probe,
1018         .port_remove =          pl2303_port_remove,
1019 };
1020
1021 static struct usb_serial_driver * const serial_drivers[] = {
1022         &pl2303_device, NULL
1023 };
1024
1025 module_usb_serial_driver(serial_drivers, id_table);
1026
1027 MODULE_DESCRIPTION("Prolific PL2303 USB to serial adaptor driver");
1028 MODULE_LICENSE("GPL");