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