USB: serial: pl2303: new device id for Chilitag
[pandora-kernel.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
18 #include <linux/kernel.h>
19 #include <linux/errno.h>
20 #include <linux/init.h>
21 #include <linux/slab.h>
22 #include <linux/tty.h>
23 #include <linux/tty_driver.h>
24 #include <linux/tty_flip.h>
25 #include <linux/serial.h>
26 #include <linux/module.h>
27 #include <linux/moduleparam.h>
28 #include <linux/spinlock.h>
29 #include <linux/uaccess.h>
30 #include <linux/usb.h>
31 #include <linux/usb/serial.h>
32 #include "pl2303.h"
33
34 /*
35  * Version Information
36  */
37 #define DRIVER_DESC "Prolific PL2303 USB to serial adaptor driver"
38
39 static int debug;
40
41 #define PL2303_CLOSING_WAIT     (30*HZ)
42
43 static const struct usb_device_id id_table[] = {
44         { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID) },
45         { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_RSAQ2) },
46         { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_DCU11) },
47         { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_RSAQ3) },
48         { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_CHILITAG) },
49         { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_PHAROS) },
50         { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_ALDIGA) },
51         { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_MMX) },
52         { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_GPRS) },
53         { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_HCR331) },
54         { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_MOTOROLA) },
55         { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_ZTEK) },
56         { USB_DEVICE(IODATA_VENDOR_ID, IODATA_PRODUCT_ID) },
57         { USB_DEVICE(IODATA_VENDOR_ID, IODATA_PRODUCT_ID_RSAQ5) },
58         { USB_DEVICE(ATEN_VENDOR_ID, ATEN_PRODUCT_ID) },
59         { USB_DEVICE(ATEN_VENDOR_ID, ATEN_PRODUCT_ID2) },
60         { USB_DEVICE(ATEN_VENDOR_ID2, ATEN_PRODUCT_ID) },
61         { USB_DEVICE(ELCOM_VENDOR_ID, ELCOM_PRODUCT_ID) },
62         { USB_DEVICE(ELCOM_VENDOR_ID, ELCOM_PRODUCT_ID_UCSGT) },
63         { USB_DEVICE(ITEGNO_VENDOR_ID, ITEGNO_PRODUCT_ID) },
64         { USB_DEVICE(ITEGNO_VENDOR_ID, ITEGNO_PRODUCT_ID_2080) },
65         { USB_DEVICE(MA620_VENDOR_ID, MA620_PRODUCT_ID) },
66         { USB_DEVICE(RATOC_VENDOR_ID, RATOC_PRODUCT_ID) },
67         { USB_DEVICE(TRIPP_VENDOR_ID, TRIPP_PRODUCT_ID) },
68         { USB_DEVICE(RADIOSHACK_VENDOR_ID, RADIOSHACK_PRODUCT_ID) },
69         { USB_DEVICE(DCU10_VENDOR_ID, DCU10_PRODUCT_ID) },
70         { USB_DEVICE(SITECOM_VENDOR_ID, SITECOM_PRODUCT_ID) },
71         { USB_DEVICE(ALCATEL_VENDOR_ID, ALCATEL_PRODUCT_ID) },
72         { USB_DEVICE(SIEMENS_VENDOR_ID, SIEMENS_PRODUCT_ID_SX1) },
73         { USB_DEVICE(SIEMENS_VENDOR_ID, SIEMENS_PRODUCT_ID_X65) },
74         { USB_DEVICE(SIEMENS_VENDOR_ID, SIEMENS_PRODUCT_ID_X75) },
75         { USB_DEVICE(SIEMENS_VENDOR_ID, SIEMENS_PRODUCT_ID_EF81) },
76         { USB_DEVICE(BENQ_VENDOR_ID, BENQ_PRODUCT_ID_S81) }, /* Benq/Siemens S81 */
77         { USB_DEVICE(SYNTECH_VENDOR_ID, SYNTECH_PRODUCT_ID) },
78         { USB_DEVICE(NOKIA_CA42_VENDOR_ID, NOKIA_CA42_PRODUCT_ID) },
79         { USB_DEVICE(CA_42_CA42_VENDOR_ID, CA_42_CA42_PRODUCT_ID) },
80         { USB_DEVICE(SAGEM_VENDOR_ID, SAGEM_PRODUCT_ID) },
81         { USB_DEVICE(LEADTEK_VENDOR_ID, LEADTEK_9531_PRODUCT_ID) },
82         { USB_DEVICE(SPEEDDRAGON_VENDOR_ID, SPEEDDRAGON_PRODUCT_ID) },
83         { USB_DEVICE(DATAPILOT_U2_VENDOR_ID, DATAPILOT_U2_PRODUCT_ID) },
84         { USB_DEVICE(BELKIN_VENDOR_ID, BELKIN_PRODUCT_ID) },
85         { USB_DEVICE(ALCOR_VENDOR_ID, ALCOR_PRODUCT_ID) },
86         { USB_DEVICE(WS002IN_VENDOR_ID, WS002IN_PRODUCT_ID) },
87         { USB_DEVICE(COREGA_VENDOR_ID, COREGA_PRODUCT_ID) },
88         { USB_DEVICE(YCCABLE_VENDOR_ID, YCCABLE_PRODUCT_ID) },
89         { USB_DEVICE(SUPERIAL_VENDOR_ID, SUPERIAL_PRODUCT_ID) },
90         { USB_DEVICE(HP_VENDOR_ID, HP_LD220_PRODUCT_ID) },
91         { USB_DEVICE(HP_VENDOR_ID, HP_LD960_PRODUCT_ID) },
92         { USB_DEVICE(HP_VENDOR_ID, HP_LCM220_PRODUCT_ID) },
93         { USB_DEVICE(HP_VENDOR_ID, HP_LCM960_PRODUCT_ID) },
94         { USB_DEVICE(CRESSI_VENDOR_ID, CRESSI_EDY_PRODUCT_ID) },
95         { USB_DEVICE(ZEAGLE_VENDOR_ID, ZEAGLE_N2ITION3_PRODUCT_ID) },
96         { USB_DEVICE(SONY_VENDOR_ID, SONY_QN3USB_PRODUCT_ID) },
97         { USB_DEVICE(SANWA_VENDOR_ID, SANWA_PRODUCT_ID) },
98         { USB_DEVICE(ADLINK_VENDOR_ID, ADLINK_ND6530_PRODUCT_ID) },
99         { USB_DEVICE(SMART_VENDOR_ID, SMART_PRODUCT_ID) },
100         { }                                     /* Terminating entry */
101 };
102
103 MODULE_DEVICE_TABLE(usb, id_table);
104
105 static struct usb_driver pl2303_driver = {
106         .name =         "pl2303",
107         .probe =        usb_serial_probe,
108         .disconnect =   usb_serial_disconnect,
109         .id_table =     id_table,
110         .suspend =      usb_serial_suspend,
111         .resume =       usb_serial_resume,
112         .no_dynamic_id =        1,
113         .supports_autosuspend = 1,
114 };
115
116 #define SET_LINE_REQUEST_TYPE           0x21
117 #define SET_LINE_REQUEST                0x20
118
119 #define SET_CONTROL_REQUEST_TYPE        0x21
120 #define SET_CONTROL_REQUEST             0x22
121 #define CONTROL_DTR                     0x01
122 #define CONTROL_RTS                     0x02
123
124 #define BREAK_REQUEST_TYPE              0x21
125 #define BREAK_REQUEST                   0x23
126 #define BREAK_ON                        0xffff
127 #define BREAK_OFF                       0x0000
128
129 #define GET_LINE_REQUEST_TYPE           0xa1
130 #define GET_LINE_REQUEST                0x21
131
132 #define VENDOR_WRITE_REQUEST_TYPE       0x40
133 #define VENDOR_WRITE_REQUEST            0x01
134
135 #define VENDOR_READ_REQUEST_TYPE        0xc0
136 #define VENDOR_READ_REQUEST             0x01
137
138 #define UART_STATE                      0x08
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
150 enum pl2303_type {
151         type_0,         /* don't know the difference between type 0 and */
152         type_1,         /* type 1, until someone from prolific tells us... */
153         HX,             /* HX version of the pl2303 chip */
154 };
155
156 struct pl2303_private {
157         spinlock_t lock;
158         u8 line_control;
159         u8 line_status;
160         enum pl2303_type type;
161
162         u8 line_settings[7];
163 };
164
165 static int pl2303_vendor_read(__u16 value, __u16 index,
166                 struct usb_serial *serial, unsigned char *buf)
167 {
168         int res = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
169                         VENDOR_READ_REQUEST, VENDOR_READ_REQUEST_TYPE,
170                         value, index, buf, 1, 100);
171         dbg("0x%x:0x%x:0x%x:0x%x  %d - %x", VENDOR_READ_REQUEST_TYPE,
172                         VENDOR_READ_REQUEST, value, index, res, buf[0]);
173         return res;
174 }
175
176 static int pl2303_vendor_write(__u16 value, __u16 index,
177                 struct usb_serial *serial)
178 {
179         int res = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
180                         VENDOR_WRITE_REQUEST, VENDOR_WRITE_REQUEST_TYPE,
181                         value, index, NULL, 0, 100);
182         dbg("0x%x:0x%x:0x%x:0x%x  %d", VENDOR_WRITE_REQUEST_TYPE,
183                         VENDOR_WRITE_REQUEST, value, index, res);
184         return res;
185 }
186
187 static int pl2303_startup(struct usb_serial *serial)
188 {
189         struct pl2303_private *priv;
190         unsigned char num_ports = serial->num_ports;
191         enum pl2303_type type = type_0;
192         unsigned char *buf;
193         int i;
194
195         if (serial->num_bulk_in < num_ports ||
196                         serial->num_bulk_out < num_ports ||
197                         serial->num_interrupt_in < num_ports) {
198                 dev_err(&serial->interface->dev, "missing endpoints\n");
199                 return -ENODEV;
200         }
201
202         buf = kmalloc(10, GFP_KERNEL);
203         if (buf == NULL)
204                 return -ENOMEM;
205
206         if (serial->dev->descriptor.bDeviceClass == 0x02)
207                 type = type_0;
208         else if (serial->dev->descriptor.bMaxPacketSize0 == 0x40)
209                 type = HX;
210         else if (serial->dev->descriptor.bDeviceClass == 0x00)
211                 type = type_1;
212         else if (serial->dev->descriptor.bDeviceClass == 0xFF)
213                 type = type_1;
214         dbg("device type: %d", type);
215
216         for (i = 0; i < serial->num_ports; ++i) {
217                 priv = kzalloc(sizeof(struct pl2303_private), GFP_KERNEL);
218                 if (!priv)
219                         goto cleanup;
220                 spin_lock_init(&priv->lock);
221                 priv->type = type;
222                 usb_set_serial_port_data(serial->port[i], priv);
223         }
224
225         pl2303_vendor_read(0x8484, 0, serial, buf);
226         pl2303_vendor_write(0x0404, 0, serial);
227         pl2303_vendor_read(0x8484, 0, serial, buf);
228         pl2303_vendor_read(0x8383, 0, serial, buf);
229         pl2303_vendor_read(0x8484, 0, serial, buf);
230         pl2303_vendor_write(0x0404, 1, serial);
231         pl2303_vendor_read(0x8484, 0, serial, buf);
232         pl2303_vendor_read(0x8383, 0, serial, buf);
233         pl2303_vendor_write(0, 1, serial);
234         pl2303_vendor_write(1, 0, serial);
235         if (type == HX)
236                 pl2303_vendor_write(2, 0x44, serial);
237         else
238                 pl2303_vendor_write(2, 0x24, serial);
239
240         kfree(buf);
241         return 0;
242
243 cleanup:
244         kfree(buf);
245         for (--i; i >= 0; --i) {
246                 priv = usb_get_serial_port_data(serial->port[i]);
247                 kfree(priv);
248                 usb_set_serial_port_data(serial->port[i], NULL);
249         }
250         return -ENOMEM;
251 }
252
253 static int set_control_lines(struct usb_device *dev, u8 value)
254 {
255         int retval;
256
257         retval = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
258                                  SET_CONTROL_REQUEST, SET_CONTROL_REQUEST_TYPE,
259                                  value, 0, NULL, 0, 100);
260         dbg("%s - value = %d, retval = %d", __func__, value, retval);
261         return retval;
262 }
263
264 static void pl2303_set_termios(struct tty_struct *tty,
265                 struct usb_serial_port *port, struct ktermios *old_termios)
266 {
267         struct usb_serial *serial = port->serial;
268         struct pl2303_private *priv = usb_get_serial_port_data(port);
269         unsigned long flags;
270         unsigned int cflag;
271         unsigned char *buf;
272         int baud;
273         int i;
274         u8 control;
275         const int baud_sup[] = { 75, 150, 300, 600, 1200, 1800, 2400, 3600,
276                                  4800, 7200, 9600, 14400, 19200, 28800, 38400,
277                                  57600, 115200, 230400, 460800, 614400,
278                                  921600, 1228800, 2457600, 3000000, 6000000 };
279         int baud_floor, baud_ceil;
280         int k;
281
282         dbg("%s -  port %d", __func__, port->number);
283
284         if (old_termios && !tty_termios_hw_change(tty->termios, old_termios))
285                 return;
286
287         cflag = tty->termios->c_cflag;
288
289         buf = kzalloc(7, GFP_KERNEL);
290         if (!buf) {
291                 dev_err(&port->dev, "%s - out of memory.\n", __func__);
292                 /* Report back no change occurred */
293                 if (old_termios)
294                         *tty->termios = *old_termios;
295                 return;
296         }
297
298         i = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
299                             GET_LINE_REQUEST, GET_LINE_REQUEST_TYPE,
300                             0, 0, buf, 7, 100);
301         dbg("0xa1:0x21:0:0  %d - %x %x %x %x %x %x %x", i,
302             buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6]);
303
304         switch (C_CSIZE(tty)) {
305         case CS5:
306                 buf[6] = 5;
307                 break;
308         case CS6:
309                 buf[6] = 6;
310                 break;
311         case CS7:
312                 buf[6] = 7;
313                 break;
314         default:
315         case CS8:
316                 buf[6] = 8;
317         }
318         dev_dbg(&port->dev, "data bits = %d\n", buf[6]);
319
320         /* For reference buf[0]:buf[3] baud rate value */
321         /* NOTE: Only the values defined in baud_sup are supported !
322          *       => if unsupported values are set, the PL2303 seems to use
323          *          9600 baud (at least my PL2303X always does)
324          */
325         baud = tty_get_baud_rate(tty);
326         dbg("%s - baud requested = %d", __func__, baud);
327         if (baud) {
328                 /* Set baudrate to nearest supported value */
329                 for (k=0; k<ARRAY_SIZE(baud_sup); k++) {
330                         if (baud_sup[k] / baud) {
331                                 baud_ceil = baud_sup[k];
332                                 if (k==0) {
333                                         baud = baud_ceil;
334                                 } else {
335                                         baud_floor = baud_sup[k-1];
336                                         if ((baud_ceil % baud)
337                                             > (baud % baud_floor))
338                                                 baud = baud_floor;
339                                         else
340                                                 baud = baud_ceil;
341                                 }
342                                 break;
343                         }
344                 }
345                 if (baud > 1228800) {
346                         /* type_0, type_1 only support up to 1228800 baud */
347                         if (priv->type != HX)
348                                 baud = 1228800;
349                         else if (baud > 6000000)
350                                 baud = 6000000;
351                 }
352                 dbg("%s - baud set = %d", __func__, baud);
353                 if (baud <= 115200) {
354                         buf[0] = baud & 0xff;
355                         buf[1] = (baud >> 8) & 0xff;
356                         buf[2] = (baud >> 16) & 0xff;
357                         buf[3] = (baud >> 24) & 0xff;
358                 } else {
359                         /* apparently the formula for higher speeds is:
360                          * baudrate = 12M * 32 / (2^buf[1]) / buf[0]
361                          */
362                         unsigned tmp = 12*1000*1000*32 / baud;
363                         buf[3] = 0x80;
364                         buf[2] = 0;
365                         buf[1] = (tmp >= 256);
366                         while (tmp >= 256) {
367                                 tmp >>= 2;
368                                 buf[1] <<= 1;
369                         }
370                         buf[0] = tmp;
371                 }
372         }
373
374         /* For reference buf[4]=0 is 1 stop bits */
375         /* For reference buf[4]=1 is 1.5 stop bits */
376         /* For reference buf[4]=2 is 2 stop bits */
377         if (cflag & CSTOPB) {
378                 /* NOTE: Comply with "real" UARTs / RS232:
379                  *       use 1.5 instead of 2 stop bits with 5 data bits
380                  */
381                 if ((cflag & CSIZE) == CS5) {
382                         buf[4] = 1;
383                         dbg("%s - stop bits = 1.5", __func__);
384                 } else {
385                         buf[4] = 2;
386                         dbg("%s - stop bits = 2", __func__);
387                 }
388         } else {
389                 buf[4] = 0;
390                 dbg("%s - stop bits = 1", __func__);
391         }
392
393         if (cflag & PARENB) {
394                 /* For reference buf[5]=0 is none parity */
395                 /* For reference buf[5]=1 is odd parity */
396                 /* For reference buf[5]=2 is even parity */
397                 /* For reference buf[5]=3 is mark parity */
398                 /* For reference buf[5]=4 is space parity */
399                 if (cflag & PARODD) {
400                         if (cflag & CMSPAR) {
401                                 buf[5] = 3;
402                                 dbg("%s - parity = mark", __func__);
403                         } else {
404                                 buf[5] = 1;
405                                 dbg("%s - parity = odd", __func__);
406                         }
407                 } else {
408                         if (cflag & CMSPAR) {
409                                 buf[5] = 4;
410                                 dbg("%s - parity = space", __func__);
411                         } else {
412                                 buf[5] = 2;
413                                 dbg("%s - parity = even", __func__);
414                         }
415                 }
416         } else {
417                 buf[5] = 0;
418                 dbg("%s - parity = none", __func__);
419         }
420
421         /*
422          * Some PL2303 are known to lose bytes if you change serial settings
423          * even to the same values as before. Thus we actually need to filter
424          * in this specific case.
425          *
426          * Note that the tty_termios_hw_change check above is not sufficient
427          * as a previously requested baud rate may differ from the one
428          * actually used (and stored in old_termios).
429          *
430          * NOTE: No additional locking needed for line_settings as it is
431          *       only used in set_termios, which is serialised against itself.
432          */
433         if (!old_termios || memcmp(buf, priv->line_settings, 7)) {
434                 i = usb_control_msg(serial->dev,
435                                     usb_sndctrlpipe(serial->dev, 0),
436                                     SET_LINE_REQUEST, SET_LINE_REQUEST_TYPE,
437                                     0, 0, buf, 7, 100);
438
439                 dbg("0x21:0x20:0:0  %d", i);
440
441                 if (i == 7)
442                         memcpy(priv->line_settings, buf, 7);
443         }
444
445         /* change control lines if we are switching to or from B0 */
446         spin_lock_irqsave(&priv->lock, flags);
447         control = priv->line_control;
448         if ((cflag & CBAUD) == B0)
449                 priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
450         else if (old_termios && (old_termios->c_cflag & CBAUD) == B0)
451                 priv->line_control |= (CONTROL_DTR | CONTROL_RTS);
452         if (control != priv->line_control) {
453                 control = priv->line_control;
454                 spin_unlock_irqrestore(&priv->lock, flags);
455                 set_control_lines(serial->dev, control);
456         } else {
457                 spin_unlock_irqrestore(&priv->lock, flags);
458         }
459
460         buf[0] = buf[1] = buf[2] = buf[3] = buf[4] = buf[5] = buf[6] = 0;
461
462         i = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
463                             GET_LINE_REQUEST, GET_LINE_REQUEST_TYPE,
464                             0, 0, buf, 7, 100);
465         dbg("0xa1:0x21:0:0  %d - %x %x %x %x %x %x %x", i,
466              buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6]);
467
468         if (cflag & CRTSCTS) {
469                 if (priv->type == HX)
470                         pl2303_vendor_write(0x0, 0x61, serial);
471                 else
472                         pl2303_vendor_write(0x0, 0x41, serial);
473         } else {
474                 pl2303_vendor_write(0x0, 0x0, serial);
475         }
476
477         /* Save resulting baud rate */
478         if (baud)
479                 tty_encode_baud_rate(tty, baud, baud);
480
481         kfree(buf);
482 }
483
484 static void pl2303_dtr_rts(struct usb_serial_port *port, int on)
485 {
486         struct pl2303_private *priv = usb_get_serial_port_data(port);
487         unsigned long flags;
488         u8 control;
489
490         spin_lock_irqsave(&priv->lock, flags);
491         /* Change DTR and RTS */
492         if (on)
493                 priv->line_control |= (CONTROL_DTR | CONTROL_RTS);
494         else
495                 priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
496         control = priv->line_control;
497         spin_unlock_irqrestore(&priv->lock, flags);
498         set_control_lines(port->serial->dev, control);
499 }
500
501 static void pl2303_close(struct usb_serial_port *port)
502 {
503         dbg("%s - port %d", __func__, port->number);
504
505         usb_serial_generic_close(port);
506         usb_kill_urb(port->interrupt_in_urb);
507 }
508
509 static int pl2303_open(struct tty_struct *tty, struct usb_serial_port *port)
510 {
511         struct usb_serial *serial = port->serial;
512         struct pl2303_private *priv = usb_get_serial_port_data(port);
513         int result;
514
515         dbg("%s -  port %d", __func__, port->number);
516
517         if (priv->type != HX) {
518                 usb_clear_halt(serial->dev, port->write_urb->pipe);
519                 usb_clear_halt(serial->dev, port->read_urb->pipe);
520         } else {
521                 /* reset upstream data pipes */
522                 pl2303_vendor_write(8, 0, serial);
523                 pl2303_vendor_write(9, 0, serial);
524         }
525
526         /* Setup termios */
527         if (tty)
528                 pl2303_set_termios(tty, port, NULL);
529
530         dbg("%s - submitting read urb", __func__);
531         result = usb_serial_generic_submit_read_urb(port, GFP_KERNEL);
532         if (result) {
533                 pl2303_close(port);
534                 return -EPROTO;
535         }
536
537         dbg("%s - submitting interrupt urb", __func__);
538         result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
539         if (result) {
540                 dev_err(&port->dev, "%s - failed submitting interrupt urb,"
541                         " error %d\n", __func__, result);
542                 pl2303_close(port);
543                 return -EPROTO;
544         }
545         port->port.drain_delay = 256;
546         return 0;
547 }
548
549 static int pl2303_tiocmset(struct tty_struct *tty,
550                            unsigned int set, unsigned int clear)
551 {
552         struct usb_serial_port *port = tty->driver_data;
553         struct pl2303_private *priv = usb_get_serial_port_data(port);
554         unsigned long flags;
555         u8 control;
556
557         if (!usb_get_intfdata(port->serial->interface))
558                 return -ENODEV;
559
560         spin_lock_irqsave(&priv->lock, flags);
561         if (set & TIOCM_RTS)
562                 priv->line_control |= CONTROL_RTS;
563         if (set & TIOCM_DTR)
564                 priv->line_control |= CONTROL_DTR;
565         if (clear & TIOCM_RTS)
566                 priv->line_control &= ~CONTROL_RTS;
567         if (clear & TIOCM_DTR)
568                 priv->line_control &= ~CONTROL_DTR;
569         control = priv->line_control;
570         spin_unlock_irqrestore(&priv->lock, flags);
571
572         return set_control_lines(port->serial->dev, control);
573 }
574
575 static int pl2303_tiocmget(struct tty_struct *tty)
576 {
577         struct usb_serial_port *port = tty->driver_data;
578         struct pl2303_private *priv = usb_get_serial_port_data(port);
579         unsigned long flags;
580         unsigned int mcr;
581         unsigned int status;
582         unsigned int result;
583
584         dbg("%s (%d)", __func__, port->number);
585
586         if (!usb_get_intfdata(port->serial->interface))
587                 return -ENODEV;
588
589         spin_lock_irqsave(&priv->lock, flags);
590         mcr = priv->line_control;
591         status = priv->line_status;
592         spin_unlock_irqrestore(&priv->lock, flags);
593
594         result = ((mcr & CONTROL_DTR)           ? TIOCM_DTR : 0)
595                   | ((mcr & CONTROL_RTS)        ? TIOCM_RTS : 0)
596                   | ((status & UART_CTS)        ? TIOCM_CTS : 0)
597                   | ((status & UART_DSR)        ? TIOCM_DSR : 0)
598                   | ((status & UART_RING)       ? TIOCM_RI  : 0)
599                   | ((status & UART_DCD)        ? TIOCM_CD  : 0);
600
601         dbg("%s - result = %x", __func__, result);
602
603         return result;
604 }
605
606 static int pl2303_carrier_raised(struct usb_serial_port *port)
607 {
608         struct pl2303_private *priv = usb_get_serial_port_data(port);
609         if (priv->line_status & UART_DCD)
610                 return 1;
611         return 0;
612 }
613
614 static int wait_modem_info(struct usb_serial_port *port, unsigned int arg)
615 {
616         struct pl2303_private *priv = usb_get_serial_port_data(port);
617         unsigned long flags;
618         unsigned int prevstatus;
619         unsigned int status;
620         unsigned int changed;
621
622         spin_lock_irqsave(&priv->lock, flags);
623         prevstatus = priv->line_status;
624         spin_unlock_irqrestore(&priv->lock, flags);
625
626         while (1) {
627                 interruptible_sleep_on(&port->delta_msr_wait);
628                 /* see if a signal did it */
629                 if (signal_pending(current))
630                         return -ERESTARTSYS;
631
632                 if (port->serial->disconnected)
633                         return -EIO;
634
635                 spin_lock_irqsave(&priv->lock, flags);
636                 status = priv->line_status;
637                 spin_unlock_irqrestore(&priv->lock, flags);
638
639                 changed = prevstatus ^ status;
640
641                 if (((arg & TIOCM_RNG) && (changed & UART_RING)) ||
642                     ((arg & TIOCM_DSR) && (changed & UART_DSR)) ||
643                     ((arg & TIOCM_CD)  && (changed & UART_DCD)) ||
644                     ((arg & TIOCM_CTS) && (changed & UART_CTS))) {
645                         return 0;
646                 }
647                 prevstatus = status;
648         }
649         /* NOTREACHED */
650         return 0;
651 }
652
653 static int pl2303_ioctl(struct tty_struct *tty,
654                         unsigned int cmd, unsigned long arg)
655 {
656         struct serial_struct ser;
657         struct usb_serial_port *port = tty->driver_data;
658         dbg("%s (%d) cmd = 0x%04x", __func__, port->number, cmd);
659
660         switch (cmd) {
661         case TIOCGSERIAL:
662                 memset(&ser, 0, sizeof ser);
663                 ser.type = PORT_16654;
664                 ser.line = port->serial->minor;
665                 ser.port = port->number;
666                 ser.baud_base = 460800;
667
668                 if (copy_to_user((void __user *)arg, &ser, sizeof ser))
669                         return -EFAULT;
670
671                 return 0;
672
673         case TIOCMIWAIT:
674                 dbg("%s (%d) TIOCMIWAIT", __func__,  port->number);
675                 return wait_modem_info(port, arg);
676         default:
677                 dbg("%s not supported = 0x%04x", __func__, cmd);
678                 break;
679         }
680         return -ENOIOCTLCMD;
681 }
682
683 static void pl2303_break_ctl(struct tty_struct *tty, int break_state)
684 {
685         struct usb_serial_port *port = tty->driver_data;
686         struct usb_serial *serial = port->serial;
687         u16 state;
688         int result;
689
690         dbg("%s - port %d", __func__, port->number);
691
692         if (break_state == 0)
693                 state = BREAK_OFF;
694         else
695                 state = BREAK_ON;
696         dbg("%s - turning break %s", __func__,
697                         state == BREAK_OFF ? "off" : "on");
698
699         result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
700                                  BREAK_REQUEST, BREAK_REQUEST_TYPE, state,
701                                  0, NULL, 0, 100);
702         if (result)
703                 dbg("%s - error sending break = %d", __func__, result);
704 }
705
706 static void pl2303_release(struct usb_serial *serial)
707 {
708         int i;
709         struct pl2303_private *priv;
710
711         dbg("%s", __func__);
712
713         for (i = 0; i < serial->num_ports; ++i) {
714                 priv = usb_get_serial_port_data(serial->port[i]);
715                 kfree(priv);
716         }
717 }
718
719 static void pl2303_update_line_status(struct usb_serial_port *port,
720                                       unsigned char *data,
721                                       unsigned int actual_length)
722 {
723
724         struct pl2303_private *priv = usb_get_serial_port_data(port);
725         struct tty_struct *tty;
726         unsigned long flags;
727         u8 status_idx = UART_STATE;
728         u8 length = UART_STATE + 1;
729         u8 prev_line_status;
730         u16 idv, idp;
731
732         idv = le16_to_cpu(port->serial->dev->descriptor.idVendor);
733         idp = le16_to_cpu(port->serial->dev->descriptor.idProduct);
734
735
736         if (idv == SIEMENS_VENDOR_ID) {
737                 if (idp == SIEMENS_PRODUCT_ID_X65 ||
738                     idp == SIEMENS_PRODUCT_ID_SX1 ||
739                     idp == SIEMENS_PRODUCT_ID_X75) {
740
741                         length = 1;
742                         status_idx = 0;
743                 }
744         }
745
746         if (actual_length < length)
747                 return;
748
749         /* Save off the uart status for others to look at */
750         spin_lock_irqsave(&priv->lock, flags);
751         prev_line_status = priv->line_status;
752         priv->line_status = data[status_idx];
753         spin_unlock_irqrestore(&priv->lock, flags);
754         if (priv->line_status & UART_BREAK_ERROR)
755                 usb_serial_handle_break(port);
756         wake_up_interruptible(&port->delta_msr_wait);
757
758         tty = tty_port_tty_get(&port->port);
759         if (!tty)
760                 return;
761         if ((priv->line_status ^ prev_line_status) & UART_DCD)
762                 usb_serial_handle_dcd_change(port, tty,
763                                 priv->line_status & UART_DCD);
764         tty_kref_put(tty);
765 }
766
767 static void pl2303_read_int_callback(struct urb *urb)
768 {
769         struct usb_serial_port *port =  urb->context;
770         unsigned char *data = urb->transfer_buffer;
771         unsigned int actual_length = urb->actual_length;
772         int status = urb->status;
773         int retval;
774
775         dbg("%s (%d)", __func__, port->number);
776
777         switch (status) {
778         case 0:
779                 /* success */
780                 break;
781         case -ECONNRESET:
782         case -ENOENT:
783         case -ESHUTDOWN:
784                 /* this urb is terminated, clean up */
785                 dbg("%s - urb shutting down with status: %d", __func__,
786                     status);
787                 return;
788         default:
789                 dbg("%s - nonzero urb status received: %d", __func__,
790                     status);
791                 goto exit;
792         }
793
794         usb_serial_debug_data(debug, &port->dev, __func__,
795                               urb->actual_length, urb->transfer_buffer);
796
797         pl2303_update_line_status(port, data, actual_length);
798
799 exit:
800         retval = usb_submit_urb(urb, GFP_ATOMIC);
801         if (retval)
802                 dev_err(&urb->dev->dev,
803                         "%s - usb_submit_urb failed with result %d\n",
804                         __func__, retval);
805 }
806
807 static void pl2303_process_read_urb(struct urb *urb)
808 {
809         struct usb_serial_port *port = urb->context;
810         struct pl2303_private *priv = usb_get_serial_port_data(port);
811         struct tty_struct *tty;
812         unsigned char *data = urb->transfer_buffer;
813         char tty_flag = TTY_NORMAL;
814         unsigned long flags;
815         u8 line_status;
816         int i;
817
818         /* update line status */
819         spin_lock_irqsave(&priv->lock, flags);
820         line_status = priv->line_status;
821         priv->line_status &= ~UART_STATE_TRANSIENT_MASK;
822         spin_unlock_irqrestore(&priv->lock, flags);
823         wake_up_interruptible(&port->delta_msr_wait);
824
825         if (!urb->actual_length)
826                 return;
827
828         tty = tty_port_tty_get(&port->port);
829         if (!tty)
830                 return;
831
832         /* break takes precedence over parity, */
833         /* which takes precedence over framing errors */
834         if (line_status & UART_BREAK_ERROR)
835                 tty_flag = TTY_BREAK;
836         else if (line_status & UART_PARITY_ERROR)
837                 tty_flag = TTY_PARITY;
838         else if (line_status & UART_FRAME_ERROR)
839                 tty_flag = TTY_FRAME;
840         dbg("%s - tty_flag = %d", __func__, tty_flag);
841
842         /* overrun is special, not associated with a char */
843         if (line_status & UART_OVERRUN_ERROR)
844                 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
845
846         if (port->port.console && port->sysrq) {
847                 for (i = 0; i < urb->actual_length; ++i)
848                         if (!usb_serial_handle_sysrq_char(port, data[i]))
849                                 tty_insert_flip_char(tty, data[i], tty_flag);
850         } else {
851                 tty_insert_flip_string_fixed_flag(tty, data, tty_flag,
852                                                         urb->actual_length);
853         }
854
855         tty_flip_buffer_push(tty);
856         tty_kref_put(tty);
857 }
858
859 /* All of the device info needed for the PL2303 SIO serial converter */
860 static struct usb_serial_driver pl2303_device = {
861         .driver = {
862                 .owner =        THIS_MODULE,
863                 .name =         "pl2303",
864         },
865         .id_table =             id_table,
866         .usb_driver =           &pl2303_driver,
867         .num_ports =            1,
868         .bulk_in_size =         256,
869         .bulk_out_size =        256,
870         .open =                 pl2303_open,
871         .close =                pl2303_close,
872         .dtr_rts =              pl2303_dtr_rts,
873         .carrier_raised =       pl2303_carrier_raised,
874         .ioctl =                pl2303_ioctl,
875         .break_ctl =            pl2303_break_ctl,
876         .set_termios =          pl2303_set_termios,
877         .tiocmget =             pl2303_tiocmget,
878         .tiocmset =             pl2303_tiocmset,
879         .process_read_urb =     pl2303_process_read_urb,
880         .read_int_callback =    pl2303_read_int_callback,
881         .attach =               pl2303_startup,
882         .release =              pl2303_release,
883 };
884
885 static int __init pl2303_init(void)
886 {
887         int retval;
888
889         retval = usb_serial_register(&pl2303_device);
890         if (retval)
891                 goto failed_usb_serial_register;
892         retval = usb_register(&pl2303_driver);
893         if (retval)
894                 goto failed_usb_register;
895         printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_DESC "\n");
896         return 0;
897 failed_usb_register:
898         usb_serial_deregister(&pl2303_device);
899 failed_usb_serial_register:
900         return retval;
901 }
902
903 static void __exit pl2303_exit(void)
904 {
905         usb_deregister(&pl2303_driver);
906         usb_serial_deregister(&pl2303_device);
907 }
908
909 module_init(pl2303_init);
910 module_exit(pl2303_exit);
911
912 MODULE_DESCRIPTION(DRIVER_DESC);
913 MODULE_LICENSE("GPL");
914
915 module_param(debug, bool, S_IRUGO | S_IWUSR);
916 MODULE_PARM_DESC(debug, "Debug enabled or not");
917