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