USB: pl2303: reduce number of prototypes
[linux-2.6.git] / drivers / usb / serial / pl2303.c
1 /*
2  * Prolific PL2303 USB to serial adaptor driver
3  *
4  * Copyright (C) 2001-2004 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 modify
10  *      it under the terms of the GNU General Public License as published by
11  *      the Free Software Foundation; either version 2 of the License.
12  *
13  * See Documentation/usb/usb-serial.txt for more information on using this driver
14  *
15  */
16
17 #include <linux/kernel.h>
18 #include <linux/errno.h>
19 #include <linux/init.h>
20 #include <linux/slab.h>
21 #include <linux/tty.h>
22 #include <linux/tty_driver.h>
23 #include <linux/tty_flip.h>
24 #include <linux/serial.h>
25 #include <linux/module.h>
26 #include <linux/moduleparam.h>
27 #include <linux/spinlock.h>
28 #include <asm/uaccess.h>
29 #include <linux/usb.h>
30 #include <linux/usb/serial.h>
31 #include "pl2303.h"
32
33 /*
34  * Version Information
35  */
36 #define DRIVER_DESC "Prolific PL2303 USB to serial adaptor driver"
37
38 static int debug;
39
40 #define PL2303_CLOSING_WAIT     (30*HZ)
41
42 #define PL2303_BUF_SIZE         1024
43 #define PL2303_TMP_BUF_SIZE     1024
44
45 struct pl2303_buf {
46         unsigned int    buf_size;
47         char            *buf_buf;
48         char            *buf_get;
49         char            *buf_put;
50 };
51
52 static struct usb_device_id id_table [] = {
53         { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID) },
54         { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_RSAQ2) },
55         { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_DCU11) },
56         { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_RSAQ3) },
57         { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_PHAROS) },
58         { USB_DEVICE(IODATA_VENDOR_ID, IODATA_PRODUCT_ID) },
59         { USB_DEVICE(ATEN_VENDOR_ID, ATEN_PRODUCT_ID) },
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(SAMSUNG_VENDOR_ID, SAMSUNG_PRODUCT_ID) },
73         { USB_DEVICE(SIEMENS_VENDOR_ID, SIEMENS_PRODUCT_ID_SX1) },
74         { USB_DEVICE(SIEMENS_VENDOR_ID, SIEMENS_PRODUCT_ID_X65) },
75         { USB_DEVICE(SIEMENS_VENDOR_ID, SIEMENS_PRODUCT_ID_X75) },
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         { }                                     /* Terminating entry */
85 };
86
87 MODULE_DEVICE_TABLE(usb, id_table);
88
89 static struct usb_driver pl2303_driver = {
90         .name =         "pl2303",
91         .probe =        usb_serial_probe,
92         .disconnect =   usb_serial_disconnect,
93         .id_table =     id_table,
94         .no_dynamic_id =        1,
95 };
96
97 #define SET_LINE_REQUEST_TYPE           0x21
98 #define SET_LINE_REQUEST                0x20
99
100 #define SET_CONTROL_REQUEST_TYPE        0x21
101 #define SET_CONTROL_REQUEST             0x22
102 #define CONTROL_DTR                     0x01
103 #define CONTROL_RTS                     0x02
104
105 #define BREAK_REQUEST_TYPE              0x21
106 #define BREAK_REQUEST                   0x23    
107 #define BREAK_ON                        0xffff
108 #define BREAK_OFF                       0x0000
109
110 #define GET_LINE_REQUEST_TYPE           0xa1
111 #define GET_LINE_REQUEST                0x21
112
113 #define VENDOR_WRITE_REQUEST_TYPE       0x40
114 #define VENDOR_WRITE_REQUEST            0x01
115
116 #define VENDOR_READ_REQUEST_TYPE        0xc0
117 #define VENDOR_READ_REQUEST             0x01
118
119 #define UART_STATE                      0x08
120 #define UART_STATE_TRANSIENT_MASK       0x74
121 #define UART_DCD                        0x01
122 #define UART_DSR                        0x02
123 #define UART_BREAK_ERROR                0x04
124 #define UART_RING                       0x08
125 #define UART_FRAME_ERROR                0x10
126 #define UART_PARITY_ERROR               0x20
127 #define UART_OVERRUN_ERROR              0x40
128 #define UART_CTS                        0x80
129
130
131 enum pl2303_type {
132         type_0,         /* don't know the difference between type 0 and */
133         type_1,         /* type 1, until someone from prolific tells us... */
134         HX,             /* HX version of the pl2303 chip */
135 };
136
137 struct pl2303_private {
138         spinlock_t lock;
139         struct pl2303_buf *buf;
140         int write_urb_in_use;
141         wait_queue_head_t delta_msr_wait;
142         u8 line_control;
143         u8 line_status;
144         u8 termios_initialized;
145         enum pl2303_type type;
146 };
147
148 /*
149  * pl2303_buf_alloc
150  *
151  * Allocate a circular buffer and all associated memory.
152  */
153 static struct pl2303_buf *pl2303_buf_alloc(unsigned int size)
154 {
155         struct pl2303_buf *pb;
156
157         if (size == 0)
158                 return NULL;
159
160         pb = (struct pl2303_buf *)kmalloc(sizeof(struct pl2303_buf), GFP_KERNEL);
161         if (pb == NULL)
162                 return NULL;
163
164         pb->buf_buf = kmalloc(size, GFP_KERNEL);
165         if (pb->buf_buf == NULL) {
166                 kfree(pb);
167                 return NULL;
168         }
169
170         pb->buf_size = size;
171         pb->buf_get = pb->buf_put = pb->buf_buf;
172
173         return pb;
174 }
175
176 /*
177  * pl2303_buf_free
178  *
179  * Free the buffer and all associated memory.
180  */
181 static void pl2303_buf_free(struct pl2303_buf *pb)
182 {
183         if (pb) {
184                 kfree(pb->buf_buf);
185                 kfree(pb);
186         }
187 }
188
189 /*
190  * pl2303_buf_clear
191  *
192  * Clear out all data in the circular buffer.
193  */
194 static void pl2303_buf_clear(struct pl2303_buf *pb)
195 {
196         if (pb != NULL)
197                 pb->buf_get = pb->buf_put;
198                 /* equivalent to a get of all data available */
199 }
200
201 /*
202  * pl2303_buf_data_avail
203  *
204  * Return the number of bytes of data available in the circular
205  * buffer.
206  */
207 static unsigned int pl2303_buf_data_avail(struct pl2303_buf *pb)
208 {
209         if (pb == NULL)
210                 return 0;
211
212         return ((pb->buf_size + pb->buf_put - pb->buf_get) % pb->buf_size);
213 }
214
215 /*
216  * pl2303_buf_space_avail
217  *
218  * Return the number of bytes of space available in the circular
219  * buffer.
220  */
221 static unsigned int pl2303_buf_space_avail(struct pl2303_buf *pb)
222 {
223         if (pb == NULL)
224                 return 0;
225
226         return ((pb->buf_size + pb->buf_get - pb->buf_put - 1) % pb->buf_size);
227 }
228
229 /*
230  * pl2303_buf_put
231  *
232  * Copy data data from a user buffer and put it into the circular buffer.
233  * Restrict to the amount of space available.
234  *
235  * Return the number of bytes copied.
236  */
237 static unsigned int pl2303_buf_put(struct pl2303_buf *pb, const char *buf,
238                                    unsigned int count)
239 {
240         unsigned int len;
241
242         if (pb == NULL)
243                 return 0;
244
245         len  = pl2303_buf_space_avail(pb);
246         if (count > len)
247                 count = len;
248
249         if (count == 0)
250                 return 0;
251
252         len = pb->buf_buf + pb->buf_size - pb->buf_put;
253         if (count > len) {
254                 memcpy(pb->buf_put, buf, len);
255                 memcpy(pb->buf_buf, buf+len, count - len);
256                 pb->buf_put = pb->buf_buf + count - len;
257         } else {
258                 memcpy(pb->buf_put, buf, count);
259                 if (count < len)
260                         pb->buf_put += count;
261                 else /* count == len */
262                         pb->buf_put = pb->buf_buf;
263         }
264
265         return count;
266 }
267
268 /*
269  * pl2303_buf_get
270  *
271  * Get data from the circular buffer and copy to the given buffer.
272  * Restrict to the amount of data available.
273  *
274  * Return the number of bytes copied.
275  */
276 static unsigned int pl2303_buf_get(struct pl2303_buf *pb, char *buf,
277                                    unsigned int count)
278 {
279         unsigned int len;
280
281         if (pb == NULL)
282                 return 0;
283
284         len = pl2303_buf_data_avail(pb);
285         if (count > len)
286                 count = len;
287
288         if (count == 0)
289                 return 0;
290
291         len = pb->buf_buf + pb->buf_size - pb->buf_get;
292         if (count > len) {
293                 memcpy(buf, pb->buf_get, len);
294                 memcpy(buf+len, pb->buf_buf, count - len);
295                 pb->buf_get = pb->buf_buf + count - len;
296         } else {
297                 memcpy(buf, pb->buf_get, count);
298                 if (count < len)
299                         pb->buf_get += count;
300                 else /* count == len */
301                         pb->buf_get = pb->buf_buf;
302         }
303
304         return count;
305 }
306
307 static int pl2303_startup(struct usb_serial *serial)
308 {
309         struct pl2303_private *priv;
310         enum pl2303_type type = type_0;
311         int i;
312
313         if (serial->dev->descriptor.bDeviceClass == 0x02)
314                 type = type_0;
315         else if (serial->dev->descriptor.bMaxPacketSize0 == 0x40)
316                 type = HX;
317         else if (serial->dev->descriptor.bDeviceClass == 0x00)
318                 type = type_1;
319         else if (serial->dev->descriptor.bDeviceClass == 0xFF)
320                 type = type_1;
321         dbg("device type: %d", type);
322
323         for (i = 0; i < serial->num_ports; ++i) {
324                 priv = kzalloc(sizeof(struct pl2303_private), GFP_KERNEL);
325                 if (!priv)
326                         goto cleanup;
327                 spin_lock_init(&priv->lock);
328                 priv->buf = pl2303_buf_alloc(PL2303_BUF_SIZE);
329                 if (priv->buf == NULL) {
330                         kfree(priv);
331                         goto cleanup;
332                 }
333                 init_waitqueue_head(&priv->delta_msr_wait);
334                 priv->type = type;
335                 usb_set_serial_port_data(serial->port[i], priv);
336         }
337         return 0;
338
339 cleanup:
340         for (--i; i>=0; --i) {
341                 priv = usb_get_serial_port_data(serial->port[i]);
342                 pl2303_buf_free(priv->buf);
343                 kfree(priv);
344                 usb_set_serial_port_data(serial->port[i], NULL);
345         }
346         return -ENOMEM;
347 }
348
349 static int set_control_lines(struct usb_device *dev, u8 value)
350 {
351         int retval;
352         
353         retval = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
354                                  SET_CONTROL_REQUEST, SET_CONTROL_REQUEST_TYPE,
355                                  value, 0, NULL, 0, 100);
356         dbg("%s - value = %d, retval = %d", __FUNCTION__, value, retval);
357         return retval;
358 }
359
360 static void pl2303_send(struct usb_serial_port *port)
361 {
362         int count, result;
363         struct pl2303_private *priv = usb_get_serial_port_data(port);
364         unsigned long flags;
365
366         dbg("%s - port %d", __FUNCTION__, port->number);
367
368         spin_lock_irqsave(&priv->lock, flags);
369
370         if (priv->write_urb_in_use) {
371                 spin_unlock_irqrestore(&priv->lock, flags);
372                 return;
373         }
374
375         count = pl2303_buf_get(priv->buf, port->write_urb->transfer_buffer,
376                                port->bulk_out_size);
377
378         if (count == 0) {
379                 spin_unlock_irqrestore(&priv->lock, flags);
380                 return;
381         }
382
383         priv->write_urb_in_use = 1;
384
385         spin_unlock_irqrestore(&priv->lock, flags);
386
387         usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count,
388                               port->write_urb->transfer_buffer);
389
390         port->write_urb->transfer_buffer_length = count;
391         port->write_urb->dev = port->serial->dev;
392         result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
393         if (result) {
394                 dev_err(&port->dev, "%s - failed submitting write urb,"
395                         " error %d\n", __FUNCTION__, result);
396                 priv->write_urb_in_use = 0;
397                 // TODO: reschedule pl2303_send
398         }
399
400         usb_serial_port_softint(port);
401 }
402
403 static int pl2303_write(struct usb_serial_port *port, const unsigned char *buf,
404                         int count)
405 {
406         struct pl2303_private *priv = usb_get_serial_port_data(port);
407         unsigned long flags;
408
409         dbg("%s - port %d, %d bytes", __FUNCTION__, port->number, count);
410
411         if (!count)
412                 return count;
413
414         spin_lock_irqsave(&priv->lock, flags);
415         count = pl2303_buf_put(priv->buf, buf, count);
416         spin_unlock_irqrestore(&priv->lock, flags);
417
418         pl2303_send(port);
419
420         return count;
421 }
422
423 static int pl2303_write_room(struct usb_serial_port *port)
424 {
425         struct pl2303_private *priv = usb_get_serial_port_data(port);
426         int room = 0;
427         unsigned long flags;
428
429         dbg("%s - port %d", __FUNCTION__, port->number);
430
431         spin_lock_irqsave(&priv->lock, flags);
432         room = pl2303_buf_space_avail(priv->buf);
433         spin_unlock_irqrestore(&priv->lock, flags);
434
435         dbg("%s - returns %d", __FUNCTION__, room);
436         return room;
437 }
438
439 static int pl2303_chars_in_buffer(struct usb_serial_port *port)
440 {
441         struct pl2303_private *priv = usb_get_serial_port_data(port);
442         int chars = 0;
443         unsigned long flags;
444
445         dbg("%s - port %d", __FUNCTION__, port->number);
446
447         spin_lock_irqsave(&priv->lock, flags);
448         chars = pl2303_buf_data_avail(priv->buf);
449         spin_unlock_irqrestore(&priv->lock, flags);
450
451         dbg("%s - returns %d", __FUNCTION__, chars);
452         return chars;
453 }
454
455 static void pl2303_set_termios(struct usb_serial_port *port,
456                                struct termios *old_termios)
457 {
458         struct usb_serial *serial = port->serial;
459         struct pl2303_private *priv = usb_get_serial_port_data(port);
460         unsigned long flags;
461         unsigned int cflag;
462         unsigned char *buf;
463         int baud;
464         int i;
465         u8 control;
466
467         dbg("%s -  port %d", __FUNCTION__, port->number);
468
469         if ((!port->tty) || (!port->tty->termios)) {
470                 dbg("%s - no tty structures", __FUNCTION__);
471                 return;
472         }
473
474         spin_lock_irqsave(&priv->lock, flags);
475         if (!priv->termios_initialized) {
476                 *(port->tty->termios) = tty_std_termios;
477                 port->tty->termios->c_cflag = B9600 | CS8 | CREAD |
478                                               HUPCL | CLOCAL;
479                 priv->termios_initialized = 1;
480         }
481         spin_unlock_irqrestore(&priv->lock, flags);
482
483         cflag = port->tty->termios->c_cflag;
484         /* check that they really want us to change something */
485         if (old_termios) {
486                 if ((cflag == old_termios->c_cflag) &&
487                     (RELEVANT_IFLAG(port->tty->termios->c_iflag) ==
488                      RELEVANT_IFLAG(old_termios->c_iflag))) {
489                         dbg("%s - nothing to change...", __FUNCTION__);
490                         return;
491                 }
492         }
493
494         buf = kzalloc(7, GFP_KERNEL);
495         if (!buf) {
496                 dev_err(&port->dev, "%s - out of memory.\n", __FUNCTION__);
497                 return;
498         }
499
500         i = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
501                             GET_LINE_REQUEST, GET_LINE_REQUEST_TYPE,
502                             0, 0, buf, 7, 100);
503         dbg("0xa1:0x21:0:0  %d - %x %x %x %x %x %x %x", i,
504             buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6]);
505
506         if (cflag & CSIZE) {
507                 switch (cflag & CSIZE) {
508                         case CS5:       buf[6] = 5;     break;
509                         case CS6:       buf[6] = 6;     break;
510                         case CS7:       buf[6] = 7;     break;
511                         default:
512                         case CS8:       buf[6] = 8;     break;
513                 }
514                 dbg("%s - data bits = %d", __FUNCTION__, buf[6]);
515         }
516
517         baud = 0;
518         switch (cflag & CBAUD) {
519                 case B0:        baud = 0;       break;
520                 case B75:       baud = 75;      break;
521                 case B150:      baud = 150;     break;
522                 case B300:      baud = 300;     break;
523                 case B600:      baud = 600;     break;
524                 case B1200:     baud = 1200;    break;
525                 case B1800:     baud = 1800;    break;
526                 case B2400:     baud = 2400;    break;
527                 case B4800:     baud = 4800;    break;
528                 case B9600:     baud = 9600;    break;
529                 case B19200:    baud = 19200;   break;
530                 case B38400:    baud = 38400;   break;
531                 case B57600:    baud = 57600;   break;
532                 case B115200:   baud = 115200;  break;
533                 case B230400:   baud = 230400;  break;
534                 case B460800:   baud = 460800;  break;
535                 default:
536                         dev_err(&port->dev, "pl2303 driver does not support"
537                                 " the baudrate requested (fix it)\n");
538                         break;
539         }
540         dbg("%s - baud = %d", __FUNCTION__, baud);
541         if (baud) {
542                 buf[0] = baud & 0xff;
543                 buf[1] = (baud >> 8) & 0xff;
544                 buf[2] = (baud >> 16) & 0xff;
545                 buf[3] = (baud >> 24) & 0xff;
546         }
547
548         /* For reference buf[4]=0 is 1 stop bits */
549         /* For reference buf[4]=1 is 1.5 stop bits */
550         /* For reference buf[4]=2 is 2 stop bits */
551         if (cflag & CSTOPB) {
552                 buf[4] = 2;
553                 dbg("%s - stop bits = 2", __FUNCTION__);
554         } else {
555                 buf[4] = 0;
556                 dbg("%s - stop bits = 1", __FUNCTION__);
557         }
558
559         if (cflag & PARENB) {
560                 /* For reference buf[5]=0 is none parity */
561                 /* For reference buf[5]=1 is odd parity */
562                 /* For reference buf[5]=2 is even parity */
563                 /* For reference buf[5]=3 is mark parity */
564                 /* For reference buf[5]=4 is space parity */
565                 if (cflag & PARODD) {
566                         buf[5] = 1;
567                         dbg("%s - parity = odd", __FUNCTION__);
568                 } else {
569                         buf[5] = 2;
570                         dbg("%s - parity = even", __FUNCTION__);
571                 }
572         } else {
573                 buf[5] = 0;
574                 dbg("%s - parity = none", __FUNCTION__);
575         }
576
577         i = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
578                             SET_LINE_REQUEST, SET_LINE_REQUEST_TYPE,
579                             0, 0, buf, 7, 100);
580         dbg("0x21:0x20:0:0  %d", i);
581
582         /* change control lines if we are switching to or from B0 */
583         spin_lock_irqsave(&priv->lock, flags);
584         control = priv->line_control;
585         if ((cflag & CBAUD) == B0)
586                 priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
587         else
588                 priv->line_control |= (CONTROL_DTR | CONTROL_RTS);
589         if (control != priv->line_control) {
590                 control = priv->line_control;
591                 spin_unlock_irqrestore(&priv->lock, flags);
592                 set_control_lines(serial->dev, control);
593         } else {
594                 spin_unlock_irqrestore(&priv->lock, flags);
595         }
596
597         buf[0] = buf[1] = buf[2] = buf[3] = buf[4] = buf[5] = buf[6] = 0;
598
599         i = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
600                             GET_LINE_REQUEST, GET_LINE_REQUEST_TYPE,
601                             0, 0, buf, 7, 100);
602         dbg("0xa1:0x21:0:0  %d - %x %x %x %x %x %x %x", i,
603              buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6]);
604
605         if (cflag & CRTSCTS) {
606                 __u16 index;
607                 if (priv->type == HX)
608                         index = 0x61;
609                 else
610                         index = 0x41;
611                 i = usb_control_msg(serial->dev,
612                                     usb_sndctrlpipe(serial->dev, 0),
613                                     VENDOR_WRITE_REQUEST,
614                                     VENDOR_WRITE_REQUEST_TYPE,
615                                     0x0, index, NULL, 0, 100);
616                 dbg("0x40:0x1:0x0:0x%x  %d", index, i);
617         }
618
619         kfree(buf);
620 }
621
622 static void pl2303_close(struct usb_serial_port *port, struct file *filp)
623 {
624         struct pl2303_private *priv = usb_get_serial_port_data(port);
625         unsigned long flags;
626         unsigned int c_cflag;
627         int bps;
628         long timeout;
629         wait_queue_t wait;
630
631         dbg("%s - port %d", __FUNCTION__, port->number);
632
633         /* wait for data to drain from the buffer */
634         spin_lock_irqsave(&priv->lock, flags);
635         timeout = PL2303_CLOSING_WAIT;
636         init_waitqueue_entry(&wait, current);
637         add_wait_queue(&port->tty->write_wait, &wait);
638         for (;;) {
639                 set_current_state(TASK_INTERRUPTIBLE);
640                 if (pl2303_buf_data_avail(priv->buf) == 0 ||
641                     timeout == 0 || signal_pending(current) ||
642                     !usb_get_intfdata(port->serial->interface)) /* disconnect */
643                         break;
644                 spin_unlock_irqrestore(&priv->lock, flags);
645                 timeout = schedule_timeout(timeout);
646                 spin_lock_irqsave(&priv->lock, flags);
647         }
648         set_current_state(TASK_RUNNING);
649         remove_wait_queue(&port->tty->write_wait, &wait);
650         /* clear out any remaining data in the buffer */
651         pl2303_buf_clear(priv->buf);
652         spin_unlock_irqrestore(&priv->lock, flags);
653
654         /* wait for characters to drain from the device */
655         /* (this is long enough for the entire 256 byte */
656         /* pl2303 hardware buffer to drain with no flow */
657         /* control for data rates of 1200 bps or more, */
658         /* for lower rates we should really know how much */
659         /* data is in the buffer to compute a delay */
660         /* that is not unnecessarily long) */
661         bps = tty_get_baud_rate(port->tty);
662         if (bps > 1200)
663                 timeout = max((HZ*2560)/bps,HZ/10);
664         else
665                 timeout = 2*HZ;
666         schedule_timeout_interruptible(timeout);
667
668         /* shutdown our urbs */
669         dbg("%s - shutting down urbs", __FUNCTION__);
670         usb_kill_urb(port->write_urb);
671         usb_kill_urb(port->read_urb);
672         usb_kill_urb(port->interrupt_in_urb);
673
674         if (port->tty) {
675                 c_cflag = port->tty->termios->c_cflag;
676                 if (c_cflag & HUPCL) {
677                         /* drop DTR and RTS */
678                         spin_lock_irqsave(&priv->lock, flags);
679                         priv->line_control = 0;
680                         spin_unlock_irqrestore(&priv->lock, flags);
681                         set_control_lines(port->serial->dev, 0);
682                 }
683         }
684 }
685
686 static int pl2303_open(struct usb_serial_port *port, struct file *filp)
687 {
688         struct termios tmp_termios;
689         struct usb_serial *serial = port->serial;
690         struct pl2303_private *priv = usb_get_serial_port_data(port);
691         unsigned char *buf;
692         int result;
693
694         dbg("%s -  port %d", __FUNCTION__, port->number);
695
696         if (priv->type != HX) {
697                 usb_clear_halt(serial->dev, port->write_urb->pipe);
698                 usb_clear_halt(serial->dev, port->read_urb->pipe);
699         }
700
701         buf = kmalloc(10, GFP_KERNEL);
702         if (buf==NULL)
703                 return -ENOMEM;
704
705 #define FISH(a,b,c,d)                                                           \
706         result=usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev,0),     \
707                                b, a, c, d, buf, 1, 100);                        \
708         dbg("0x%x:0x%x:0x%x:0x%x  %d - %x",a,b,c,d,result,buf[0]);
709
710 #define SOUP(a,b,c,d)                                                           \
711         result=usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev,0),     \
712                                b, a, c, d, NULL, 0, 100);                       \
713         dbg("0x%x:0x%x:0x%x:0x%x  %d",a,b,c,d,result);
714
715         FISH (VENDOR_READ_REQUEST_TYPE, VENDOR_READ_REQUEST, 0x8484, 0);
716         SOUP (VENDOR_WRITE_REQUEST_TYPE, VENDOR_WRITE_REQUEST, 0x0404, 0);
717         FISH (VENDOR_READ_REQUEST_TYPE, VENDOR_READ_REQUEST, 0x8484, 0);
718         FISH (VENDOR_READ_REQUEST_TYPE, VENDOR_READ_REQUEST, 0x8383, 0);
719         FISH (VENDOR_READ_REQUEST_TYPE, VENDOR_READ_REQUEST, 0x8484, 0);
720         SOUP (VENDOR_WRITE_REQUEST_TYPE, VENDOR_WRITE_REQUEST, 0x0404, 1);
721         FISH (VENDOR_READ_REQUEST_TYPE, VENDOR_READ_REQUEST, 0x8484, 0);
722         FISH (VENDOR_READ_REQUEST_TYPE, VENDOR_READ_REQUEST, 0x8383, 0);
723         SOUP (VENDOR_WRITE_REQUEST_TYPE, VENDOR_WRITE_REQUEST, 0, 1);
724         SOUP (VENDOR_WRITE_REQUEST_TYPE, VENDOR_WRITE_REQUEST, 1, 0);
725
726         if (priv->type == HX) {
727                 /* HX chip */
728                 SOUP (VENDOR_WRITE_REQUEST_TYPE, VENDOR_WRITE_REQUEST, 2, 0x44);
729                 /* reset upstream data pipes */
730                 SOUP (VENDOR_WRITE_REQUEST_TYPE, VENDOR_WRITE_REQUEST, 8, 0);
731                 SOUP (VENDOR_WRITE_REQUEST_TYPE, VENDOR_WRITE_REQUEST, 9, 0);
732         } else {
733                 SOUP (VENDOR_WRITE_REQUEST_TYPE, VENDOR_WRITE_REQUEST, 2, 0x24);
734         }
735
736         kfree(buf);
737
738         /* Setup termios */
739         if (port->tty) {
740                 pl2303_set_termios(port, &tmp_termios);
741         }
742
743         //FIXME: need to assert RTS and DTR if CRTSCTS off
744
745         dbg("%s - submitting read urb", __FUNCTION__);
746         port->read_urb->dev = serial->dev;
747         result = usb_submit_urb(port->read_urb, GFP_KERNEL);
748         if (result) {
749                 dev_err(&port->dev, "%s - failed submitting read urb,"
750                         " error %d\n", __FUNCTION__, result);
751                 pl2303_close(port, NULL);
752                 return -EPROTO;
753         }
754
755         dbg("%s - submitting interrupt urb", __FUNCTION__);
756         port->interrupt_in_urb->dev = serial->dev;
757         result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
758         if (result) {
759                 dev_err(&port->dev, "%s - failed submitting interrupt urb,"
760                         " error %d\n", __FUNCTION__, result);
761                 pl2303_close(port, NULL);
762                 return -EPROTO;
763         }
764         return 0;
765 }
766
767 static int pl2303_tiocmset(struct usb_serial_port *port, struct file *file,
768                            unsigned int set, unsigned int clear)
769 {
770         struct pl2303_private *priv = usb_get_serial_port_data(port);
771         unsigned long flags;
772         u8 control;
773
774         if (!usb_get_intfdata(port->serial->interface))
775                 return -ENODEV;
776
777         spin_lock_irqsave(&priv->lock, flags);
778         if (set & TIOCM_RTS)
779                 priv->line_control |= CONTROL_RTS;
780         if (set & TIOCM_DTR)
781                 priv->line_control |= CONTROL_DTR;
782         if (clear & TIOCM_RTS)
783                 priv->line_control &= ~CONTROL_RTS;
784         if (clear & TIOCM_DTR)
785                 priv->line_control &= ~CONTROL_DTR;
786         control = priv->line_control;
787         spin_unlock_irqrestore(&priv->lock, flags);
788
789         return set_control_lines(port->serial->dev, control);
790 }
791
792 static int pl2303_tiocmget(struct usb_serial_port *port, struct file *file)
793 {
794         struct pl2303_private *priv = usb_get_serial_port_data(port);
795         unsigned long flags;
796         unsigned int mcr;
797         unsigned int status;
798         unsigned int result;
799
800         dbg("%s (%d)", __FUNCTION__, port->number);
801
802         if (!usb_get_intfdata(port->serial->interface))
803                 return -ENODEV;
804
805         spin_lock_irqsave(&priv->lock, flags);
806         mcr = priv->line_control;
807         status = priv->line_status;
808         spin_unlock_irqrestore(&priv->lock, flags);
809
810         result = ((mcr & CONTROL_DTR)           ? TIOCM_DTR : 0)
811                   | ((mcr & CONTROL_RTS)        ? TIOCM_RTS : 0)
812                   | ((status & UART_CTS)        ? TIOCM_CTS : 0)
813                   | ((status & UART_DSR)        ? TIOCM_DSR : 0)
814                   | ((status & UART_RING)       ? TIOCM_RI  : 0)
815                   | ((status & UART_DCD)        ? TIOCM_CD  : 0);
816
817         dbg("%s - result = %x", __FUNCTION__, result);
818
819         return result;
820 }
821
822 static int wait_modem_info(struct usb_serial_port *port, unsigned int arg)
823 {
824         struct pl2303_private *priv = usb_get_serial_port_data(port);
825         unsigned long flags;
826         unsigned int prevstatus;
827         unsigned int status;
828         unsigned int changed;
829
830         spin_lock_irqsave(&priv->lock, flags);
831         prevstatus = priv->line_status;
832         spin_unlock_irqrestore(&priv->lock, flags);
833
834         while (1) {
835                 interruptible_sleep_on(&priv->delta_msr_wait);
836                 /* see if a signal did it */
837                 if (signal_pending(current))
838                         return -ERESTARTSYS;
839
840                 spin_lock_irqsave(&priv->lock, flags);
841                 status = priv->line_status;
842                 spin_unlock_irqrestore(&priv->lock, flags);
843
844                 changed=prevstatus^status;
845
846                 if (((arg & TIOCM_RNG) && (changed & UART_RING)) ||
847                     ((arg & TIOCM_DSR) && (changed & UART_DSR)) ||
848                     ((arg & TIOCM_CD)  && (changed & UART_DCD)) ||
849                     ((arg & TIOCM_CTS) && (changed & UART_CTS)) ) {
850                         return 0;
851                 }
852                 prevstatus = status;
853         }
854         /* NOTREACHED */
855         return 0;
856 }
857
858 static int pl2303_ioctl(struct usb_serial_port *port, struct file *file,
859                         unsigned int cmd, unsigned long arg)
860 {
861         dbg("%s (%d) cmd = 0x%04x", __FUNCTION__, port->number, cmd);
862
863         switch (cmd) {
864                 case TIOCMIWAIT:
865                         dbg("%s (%d) TIOCMIWAIT", __FUNCTION__,  port->number);
866                         return wait_modem_info(port, arg);
867
868                 default:
869                         dbg("%s not supported = 0x%04x", __FUNCTION__, cmd);
870                         break;
871         }
872
873         return -ENOIOCTLCMD;
874 }
875
876 static void pl2303_break_ctl(struct usb_serial_port *port, int break_state)
877 {
878         struct usb_serial *serial = port->serial;
879         u16 state;
880         int result;
881
882         dbg("%s - port %d", __FUNCTION__, port->number);
883
884         if (break_state == 0)
885                 state = BREAK_OFF;
886         else
887                 state = BREAK_ON;
888         dbg("%s - turning break %s", __FUNCTION__, state==BREAK_OFF ? "off" : "on");
889
890         result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
891                                  BREAK_REQUEST, BREAK_REQUEST_TYPE, state,
892                                  0, NULL, 0, 100);
893         if (result)
894                 dbg("%s - error sending break = %d", __FUNCTION__, result);
895 }
896
897 static void pl2303_shutdown(struct usb_serial *serial)
898 {
899         int i;
900         struct pl2303_private *priv;
901
902         dbg("%s", __FUNCTION__);
903
904         for (i = 0; i < serial->num_ports; ++i) {
905                 priv = usb_get_serial_port_data(serial->port[i]);
906                 if (priv) {
907                         pl2303_buf_free(priv->buf);
908                         kfree(priv);
909                         usb_set_serial_port_data(serial->port[i], NULL);
910                 }
911         }
912 }
913
914 static void pl2303_update_line_status(struct usb_serial_port *port,
915                                       unsigned char *data,
916                                       unsigned int actual_length)
917 {
918
919         struct pl2303_private *priv = usb_get_serial_port_data(port);
920         unsigned long flags;
921         u8 status_idx = UART_STATE;
922         u8 length = UART_STATE + 1;
923
924         if ((le16_to_cpu(port->serial->dev->descriptor.idVendor) == SIEMENS_VENDOR_ID) &&
925             (le16_to_cpu(port->serial->dev->descriptor.idProduct) == SIEMENS_PRODUCT_ID_X65 ||
926              le16_to_cpu(port->serial->dev->descriptor.idProduct) == SIEMENS_PRODUCT_ID_SX1 ||
927              le16_to_cpu(port->serial->dev->descriptor.idProduct) == SIEMENS_PRODUCT_ID_X75)) {
928                 length = 1;
929                 status_idx = 0;
930         }
931
932         if (actual_length < length)
933                 return;
934
935         /* Save off the uart status for others to look at */
936         spin_lock_irqsave(&priv->lock, flags);
937         priv->line_status = data[status_idx];
938         spin_unlock_irqrestore(&priv->lock, flags);
939         wake_up_interruptible(&priv->delta_msr_wait);
940 }
941
942 static void pl2303_read_int_callback(struct urb *urb, struct pt_regs *regs)
943 {
944         struct usb_serial_port *port = (struct usb_serial_port *) urb->context;
945         unsigned char *data = urb->transfer_buffer;
946         unsigned int actual_length = urb->actual_length;
947         int status;
948
949         dbg("%s (%d)", __FUNCTION__, port->number);
950
951         switch (urb->status) {
952         case 0:
953                 /* success */
954                 break;
955         case -ECONNRESET:
956         case -ENOENT:
957         case -ESHUTDOWN:
958                 /* this urb is terminated, clean up */
959                 dbg("%s - urb shutting down with status: %d", __FUNCTION__,
960                     urb->status);
961                 return;
962         default:
963                 dbg("%s - nonzero urb status received: %d", __FUNCTION__,
964                     urb->status);
965                 goto exit;
966         }
967
968         usb_serial_debug_data(debug, &port->dev, __FUNCTION__,
969                               urb->actual_length, urb->transfer_buffer);
970
971         pl2303_update_line_status(port, data, actual_length);
972
973 exit:
974         status = usb_submit_urb(urb, GFP_ATOMIC);
975         if (status)
976                 dev_err(&urb->dev->dev,
977                         "%s - usb_submit_urb failed with result %d\n",
978                         __FUNCTION__, status);
979 }
980
981 static void pl2303_read_bulk_callback(struct urb *urb, struct pt_regs *regs)
982 {
983         struct usb_serial_port *port = (struct usb_serial_port *) urb->context;
984         struct pl2303_private *priv = usb_get_serial_port_data(port);
985         struct tty_struct *tty;
986         unsigned char *data = urb->transfer_buffer;
987         unsigned long flags;
988         int i;
989         int result;
990         u8 status;
991         char tty_flag;
992
993         dbg("%s - port %d", __FUNCTION__, port->number);
994
995         if (urb->status) {
996                 dbg("%s - urb->status = %d", __FUNCTION__, urb->status);
997                 if (!port->open_count) {
998                         dbg("%s - port is closed, exiting.", __FUNCTION__);
999                         return;
1000                 }
1001                 if (urb->status == -EPROTO) {
1002                         /* PL2303 mysteriously fails with -EPROTO reschedule
1003                          * the read */
1004                         dbg("%s - caught -EPROTO, resubmitting the urb",
1005                             __FUNCTION__);
1006                         urb->status = 0;
1007                         urb->dev = port->serial->dev;
1008                         result = usb_submit_urb(urb, GFP_ATOMIC);
1009                         if (result)
1010                                 dev_err(&urb->dev->dev, "%s - failed"
1011                                         " resubmitting read urb, error %d\n",
1012                                         __FUNCTION__, result);
1013                         return;
1014                 }
1015                 dbg("%s - unable to handle the error, exiting.", __FUNCTION__);
1016                 return;
1017         }
1018
1019         usb_serial_debug_data(debug, &port->dev, __FUNCTION__,
1020                               urb->actual_length, data);
1021
1022         /* get tty_flag from status */
1023         tty_flag = TTY_NORMAL;
1024
1025         spin_lock_irqsave(&priv->lock, flags);
1026         status = priv->line_status;
1027         priv->line_status &= ~UART_STATE_TRANSIENT_MASK;
1028         spin_unlock_irqrestore(&priv->lock, flags);
1029         wake_up_interruptible(&priv->delta_msr_wait);
1030
1031         /* break takes precedence over parity, */
1032         /* which takes precedence over framing errors */
1033         if (status & UART_BREAK_ERROR )
1034                 tty_flag = TTY_BREAK;
1035         else if (status & UART_PARITY_ERROR)
1036                 tty_flag = TTY_PARITY;
1037         else if (status & UART_FRAME_ERROR)
1038                 tty_flag = TTY_FRAME;
1039         dbg("%s - tty_flag = %d", __FUNCTION__, tty_flag);
1040
1041         tty = port->tty;
1042         if (tty && urb->actual_length) {
1043                 tty_buffer_request_room(tty, urb->actual_length + 1);
1044                 /* overrun is special, not associated with a char */
1045                 if (status & UART_OVERRUN_ERROR)
1046                         tty_insert_flip_char(tty, 0, TTY_OVERRUN);
1047                 for (i = 0; i < urb->actual_length; ++i)
1048                         tty_insert_flip_char(tty, data[i], tty_flag);
1049                 tty_flip_buffer_push(tty);
1050         }
1051
1052         /* Schedule the next read _if_ we are still open */
1053         if (port->open_count) {
1054                 urb->dev = port->serial->dev;
1055                 result = usb_submit_urb(urb, GFP_ATOMIC);
1056                 if (result)
1057                         dev_err(&urb->dev->dev, "%s - failed resubmitting"
1058                                 " read urb, error %d\n", __FUNCTION__, result);
1059         }
1060
1061         return;
1062 }
1063
1064 static void pl2303_write_bulk_callback(struct urb *urb, struct pt_regs *regs)
1065 {
1066         struct usb_serial_port *port = (struct usb_serial_port *) urb->context;
1067         struct pl2303_private *priv = usb_get_serial_port_data(port);
1068         int result;
1069
1070         dbg("%s - port %d", __FUNCTION__, port->number);
1071
1072         switch (urb->status) {
1073         case 0:
1074                 /* success */
1075                 break;
1076         case -ECONNRESET:
1077         case -ENOENT:
1078         case -ESHUTDOWN:
1079                 /* this urb is terminated, clean up */
1080                 dbg("%s - urb shutting down with status: %d", __FUNCTION__,
1081                     urb->status);
1082                 priv->write_urb_in_use = 0;
1083                 return;
1084         default:
1085                 /* error in the urb, so we have to resubmit it */
1086                 dbg("%s - Overflow in write", __FUNCTION__);
1087                 dbg("%s - nonzero write bulk status received: %d", __FUNCTION__,
1088                     urb->status);
1089                 port->write_urb->transfer_buffer_length = 1;
1090                 port->write_urb->dev = port->serial->dev;
1091                 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
1092                 if (result)
1093                         dev_err(&urb->dev->dev, "%s - failed resubmitting write"
1094                                 " urb, error %d\n", __FUNCTION__, result);
1095                 else
1096                         return;
1097         }
1098
1099         priv->write_urb_in_use = 0;
1100
1101         /* send any buffered data */
1102         pl2303_send(port);
1103 }
1104
1105 /* All of the device info needed for the PL2303 SIO serial converter */
1106 static struct usb_serial_driver pl2303_device = {
1107         .driver = {
1108                 .owner =        THIS_MODULE,
1109                 .name =         "pl2303",
1110         },
1111         .id_table =             id_table,
1112         .num_interrupt_in =     NUM_DONT_CARE,
1113         .num_bulk_in =          1,
1114         .num_bulk_out =         1,
1115         .num_ports =            1,
1116         .open =                 pl2303_open,
1117         .close =                pl2303_close,
1118         .write =                pl2303_write,
1119         .ioctl =                pl2303_ioctl,
1120         .break_ctl =            pl2303_break_ctl,
1121         .set_termios =          pl2303_set_termios,
1122         .tiocmget =             pl2303_tiocmget,
1123         .tiocmset =             pl2303_tiocmset,
1124         .read_bulk_callback =   pl2303_read_bulk_callback,
1125         .read_int_callback =    pl2303_read_int_callback,
1126         .write_bulk_callback =  pl2303_write_bulk_callback,
1127         .write_room =           pl2303_write_room,
1128         .chars_in_buffer =      pl2303_chars_in_buffer,
1129         .attach =               pl2303_startup,
1130         .shutdown =             pl2303_shutdown,
1131 };
1132
1133 static int __init pl2303_init(void)
1134 {
1135         int retval;
1136
1137         retval = usb_serial_register(&pl2303_device);
1138         if (retval)
1139                 goto failed_usb_serial_register;
1140         retval = usb_register(&pl2303_driver);
1141         if (retval)
1142                 goto failed_usb_register;
1143         info(DRIVER_DESC);
1144         return 0;
1145 failed_usb_register:
1146         usb_serial_deregister(&pl2303_device);
1147 failed_usb_serial_register:
1148         return retval;
1149 }
1150
1151 static void __exit pl2303_exit(void)
1152 {
1153         usb_deregister(&pl2303_driver);
1154         usb_serial_deregister(&pl2303_device);
1155 }
1156
1157 module_init(pl2303_init);
1158 module_exit(pl2303_exit);
1159
1160 MODULE_DESCRIPTION(DRIVER_DESC);
1161 MODULE_LICENSE("GPL");
1162
1163 module_param(debug, bool, S_IRUGO | S_IWUSR);
1164 MODULE_PARM_DESC(debug, "Debug enabled or not");
1165