initial commit with v2.6.9
[linux-2.6.9-moxart.git] / drivers / usb / serial / pl2303.c
blobc61e56829a960c1d7011ba7c7b75e9cdb21b6c77
1 /*
2 * Prolific PL2303 USB to serial adaptor driver
4 * Copyright (C) 2001-2004 Greg Kroah-Hartman (greg@kroah.com)
5 * Copyright (C) 2003 IBM Corp.
7 * Original driver for 2.2.x by anonymous
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, or
12 * (at your option) any later version.
14 * See Documentation/usb/usb-serial.txt for more information on using this driver
16 * 2002_Mar_26 gkh
17 * allowed driver to work properly if there is no tty assigned to a port
18 * (this happens for serial console devices.)
20 * 2001_Oct_06 gkh
21 * Added RTS and DTR line control. Thanks to joe@bndlg.de for parts of it.
23 * 2001_Sep_19 gkh
24 * Added break support.
26 * 2001_Aug_30 gkh
27 * fixed oops in write_bulk_callback.
29 * 2001_Aug_28 gkh
30 * reworked buffer logic to be like other usb-serial drivers. Hopefully
31 * removing some reported problems.
33 * 2001_Jun_06 gkh
34 * finished porting to 2.4 format.
38 #include <linux/config.h>
39 #include <linux/kernel.h>
40 #include <linux/errno.h>
41 #include <linux/init.h>
42 #include <linux/slab.h>
43 #include <linux/tty.h>
44 #include <linux/tty_driver.h>
45 #include <linux/tty_flip.h>
46 #include <linux/serial.h>
47 #include <linux/module.h>
48 #include <linux/moduleparam.h>
49 #include <linux/spinlock.h>
50 #include <asm/uaccess.h>
51 #include <linux/usb.h>
52 #include "usb-serial.h"
53 #include "pl2303.h"
56 * Version Information
58 #define DRIVER_VERSION "v0.11"
59 #define DRIVER_DESC "Prolific PL2303 USB to serial adaptor driver"
61 static int debug;
63 static struct usb_device_id id_table [] = {
64 { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID) },
65 { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_RSAQ2) },
66 { USB_DEVICE(IODATA_VENDOR_ID, IODATA_PRODUCT_ID) },
67 { USB_DEVICE(ATEN_VENDOR_ID, ATEN_PRODUCT_ID) },
68 { USB_DEVICE(ATEN_VENDOR_ID2, ATEN_PRODUCT_ID) },
69 { USB_DEVICE(ELCOM_VENDOR_ID, ELCOM_PRODUCT_ID) },
70 { USB_DEVICE(ITEGNO_VENDOR_ID, ITEGNO_PRODUCT_ID) },
71 { USB_DEVICE(MA620_VENDOR_ID, MA620_PRODUCT_ID) },
72 { USB_DEVICE(RATOC_VENDOR_ID, RATOC_PRODUCT_ID) },
73 { USB_DEVICE(TRIPP_VENDOR_ID, TRIPP_PRODUCT_ID) },
74 { USB_DEVICE(RADIOSHACK_VENDOR_ID, RADIOSHACK_PRODUCT_ID) },
75 { USB_DEVICE(DCU10_VENDOR_ID, DCU10_PRODUCT_ID) },
76 { USB_DEVICE(SITECOM_VENDOR_ID, SITECOM_PRODUCT_ID) },
77 { USB_DEVICE(ALCATEL_VENDOR_ID, ALCATEL_PRODUCT_ID) },
78 { USB_DEVICE(SAMSUNG_VENDOR_ID, SAMSUNG_PRODUCT_ID) },
79 { } /* Terminating entry */
82 MODULE_DEVICE_TABLE (usb, id_table);
84 static struct usb_driver pl2303_driver = {
85 .owner = THIS_MODULE,
86 .name = "pl2303",
87 .probe = usb_serial_probe,
88 .disconnect = usb_serial_disconnect,
89 .id_table = id_table,
92 #define SET_LINE_REQUEST_TYPE 0x21
93 #define SET_LINE_REQUEST 0x20
95 #define SET_CONTROL_REQUEST_TYPE 0x21
96 #define SET_CONTROL_REQUEST 0x22
97 #define CONTROL_DTR 0x01
98 #define CONTROL_RTS 0x02
100 #define BREAK_REQUEST_TYPE 0x21
101 #define BREAK_REQUEST 0x23
102 #define BREAK_ON 0xffff
103 #define BREAK_OFF 0x0000
105 #define GET_LINE_REQUEST_TYPE 0xa1
106 #define GET_LINE_REQUEST 0x21
108 #define VENDOR_WRITE_REQUEST_TYPE 0x40
109 #define VENDOR_WRITE_REQUEST 0x01
111 #define VENDOR_READ_REQUEST_TYPE 0xc0
112 #define VENDOR_READ_REQUEST 0x01
114 #define UART_STATE 0x08
115 #define UART_STATE_TRANSIENT_MASK 0x74
116 #define UART_DCD 0x01
117 #define UART_DSR 0x02
118 #define UART_BREAK_ERROR 0x04
119 #define UART_RING 0x08
120 #define UART_FRAME_ERROR 0x10
121 #define UART_PARITY_ERROR 0x20
122 #define UART_OVERRUN_ERROR 0x40
123 #define UART_CTS 0x80
125 /* function prototypes for a PL2303 serial converter */
126 static int pl2303_open (struct usb_serial_port *port, struct file *filp);
127 static void pl2303_close (struct usb_serial_port *port, struct file *filp);
128 static void pl2303_set_termios (struct usb_serial_port *port,
129 struct termios *old);
130 static int pl2303_ioctl (struct usb_serial_port *port, struct file *file,
131 unsigned int cmd, unsigned long arg);
132 static void pl2303_read_int_callback (struct urb *urb, struct pt_regs *regs);
133 static void pl2303_read_bulk_callback (struct urb *urb, struct pt_regs *regs);
134 static void pl2303_write_bulk_callback (struct urb *urb, struct pt_regs *regs);
135 static int pl2303_write (struct usb_serial_port *port, int from_user,
136 const unsigned char *buf, int count);
137 static void pl2303_break_ctl(struct usb_serial_port *port,int break_state);
138 static int pl2303_tiocmget (struct usb_serial_port *port, struct file *file);
139 static int pl2303_tiocmset (struct usb_serial_port *port, struct file *file,
140 unsigned int set, unsigned int clear);
141 static int pl2303_startup (struct usb_serial *serial);
142 static void pl2303_shutdown (struct usb_serial *serial);
145 /* All of the device info needed for the PL2303 SIO serial converter */
146 static struct usb_serial_device_type pl2303_device = {
147 .owner = THIS_MODULE,
148 .name = "PL-2303",
149 .id_table = id_table,
150 .num_interrupt_in = NUM_DONT_CARE,
151 .num_bulk_in = 1,
152 .num_bulk_out = 1,
153 .num_ports = 1,
154 .open = pl2303_open,
155 .close = pl2303_close,
156 .write = pl2303_write,
157 .ioctl = pl2303_ioctl,
158 .break_ctl = pl2303_break_ctl,
159 .set_termios = pl2303_set_termios,
160 .tiocmget = pl2303_tiocmget,
161 .tiocmset = pl2303_tiocmset,
162 .read_bulk_callback = pl2303_read_bulk_callback,
163 .read_int_callback = pl2303_read_int_callback,
164 .write_bulk_callback = pl2303_write_bulk_callback,
165 .attach = pl2303_startup,
166 .shutdown = pl2303_shutdown,
169 enum pl2303_type {
170 type_0, /* don't know the difference between type 0 and */
171 type_1, /* type 1, until someone from prolific tells us... */
172 HX, /* HX version of the pl2303 chip */
175 struct pl2303_private {
176 spinlock_t lock;
177 wait_queue_head_t delta_msr_wait;
178 u8 line_control;
179 u8 line_status;
180 u8 termios_initialized;
181 enum pl2303_type type;
185 static int pl2303_startup (struct usb_serial *serial)
187 struct pl2303_private *priv;
188 enum pl2303_type type = type_0;
189 int i;
191 if (serial->dev->descriptor.bDeviceClass == 0x02)
192 type = type_0;
193 else if (serial->dev->descriptor.bMaxPacketSize0 == 0x40)
194 type = HX;
195 else if (serial->dev->descriptor.bDeviceClass == 0x00)
196 type = type_1;
197 else if (serial->dev->descriptor.bDeviceClass == 0xFF)
198 type = type_1;
199 dbg("device type: %d", type);
201 for (i = 0; i < serial->num_ports; ++i) {
202 priv = kmalloc (sizeof (struct pl2303_private), GFP_KERNEL);
203 if (!priv)
204 return -ENOMEM;
205 memset (priv, 0x00, sizeof (struct pl2303_private));
206 spin_lock_init(&priv->lock);
207 init_waitqueue_head(&priv->delta_msr_wait);
208 priv->type = type;
209 usb_set_serial_port_data(serial->port[i], priv);
211 return 0;
214 static int set_control_lines (struct usb_device *dev, u8 value)
216 int retval;
218 retval = usb_control_msg (dev, usb_sndctrlpipe (dev, 0),
219 SET_CONTROL_REQUEST, SET_CONTROL_REQUEST_TYPE,
220 value, 0, NULL, 0, 100);
221 dbg("%s - value = %d, retval = %d", __FUNCTION__, value, retval);
222 return retval;
225 static int pl2303_write (struct usb_serial_port *port, int from_user, const unsigned char *buf, int count)
227 int result;
229 dbg("%s - port %d, %d bytes", __FUNCTION__, port->number, count);
231 if (!count)
232 return count;
234 if (port->write_urb->status == -EINPROGRESS) {
235 dbg("%s - already writing", __FUNCTION__);
236 return 0;
239 count = (count > port->bulk_out_size) ? port->bulk_out_size : count;
240 if (from_user) {
241 if (copy_from_user (port->write_urb->transfer_buffer, buf, count))
242 return -EFAULT;
243 } else {
244 memcpy (port->write_urb->transfer_buffer, buf, count);
247 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count, port->write_urb->transfer_buffer);
249 port->write_urb->transfer_buffer_length = count;
250 port->write_urb->dev = port->serial->dev;
251 result = usb_submit_urb (port->write_urb, GFP_ATOMIC);
252 if (result)
253 dev_err(&port->dev, "%s - failed submitting write urb, error %d\n", __FUNCTION__, result);
254 else
255 result = count;
257 return result;
262 static void pl2303_set_termios (struct usb_serial_port *port, struct termios *old_termios)
264 struct usb_serial *serial = port->serial;
265 struct pl2303_private *priv = usb_get_serial_port_data(port);
266 unsigned long flags;
267 unsigned int cflag;
268 unsigned char *buf;
269 int baud;
270 int i;
271 u8 control;
273 dbg("%s - port %d", __FUNCTION__, port->number);
275 if ((!port->tty) || (!port->tty->termios)) {
276 dbg("%s - no tty structures", __FUNCTION__);
277 return;
280 spin_lock_irqsave(&priv->lock, flags);
281 if (!priv->termios_initialized) {
282 *(port->tty->termios) = tty_std_termios;
283 port->tty->termios->c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
284 priv->termios_initialized = 1;
286 spin_unlock_irqrestore(&priv->lock, flags);
288 cflag = port->tty->termios->c_cflag;
289 /* check that they really want us to change something */
290 if (old_termios) {
291 if ((cflag == old_termios->c_cflag) &&
292 (RELEVANT_IFLAG(port->tty->termios->c_iflag) == RELEVANT_IFLAG(old_termios->c_iflag))) {
293 dbg("%s - nothing to change...", __FUNCTION__);
294 return;
298 buf = kmalloc (7, GFP_KERNEL);
299 if (!buf) {
300 dev_err(&port->dev, "%s - out of memory.\n", __FUNCTION__);
301 return;
303 memset (buf, 0x00, 0x07);
305 i = usb_control_msg (serial->dev, usb_rcvctrlpipe (serial->dev, 0),
306 GET_LINE_REQUEST, GET_LINE_REQUEST_TYPE,
307 0, 0, buf, 7, 100);
308 dbg ("0xa1:0x21:0:0 %d - %x %x %x %x %x %x %x", i,
309 buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6]);
312 if (cflag & CSIZE) {
313 switch (cflag & CSIZE) {
314 case CS5: buf[6] = 5; break;
315 case CS6: buf[6] = 6; break;
316 case CS7: buf[6] = 7; break;
317 default:
318 case CS8: buf[6] = 8; break;
320 dbg("%s - data bits = %d", __FUNCTION__, buf[6]);
323 baud = 0;
324 switch (cflag & CBAUD) {
325 case B0: baud = 0; break;
326 case B75: baud = 75; break;
327 case B150: baud = 150; break;
328 case B300: baud = 300; break;
329 case B600: baud = 600; break;
330 case B1200: baud = 1200; break;
331 case B1800: baud = 1800; break;
332 case B2400: baud = 2400; break;
333 case B4800: baud = 4800; break;
334 case B9600: baud = 9600; break;
335 case B19200: baud = 19200; break;
336 case B38400: baud = 38400; break;
337 case B57600: baud = 57600; break;
338 case B115200: baud = 115200; break;
339 case B230400: baud = 230400; break;
340 case B460800: baud = 460800; break;
341 default:
342 dev_err(&port->dev, "pl2303 driver does not support the baudrate requested (fix it)\n");
343 break;
345 dbg("%s - baud = %d", __FUNCTION__, baud);
346 if (baud) {
347 buf[0] = baud & 0xff;
348 buf[1] = (baud >> 8) & 0xff;
349 buf[2] = (baud >> 16) & 0xff;
350 buf[3] = (baud >> 24) & 0xff;
353 /* For reference buf[4]=0 is 1 stop bits */
354 /* For reference buf[4]=1 is 1.5 stop bits */
355 /* For reference buf[4]=2 is 2 stop bits */
356 if (cflag & CSTOPB) {
357 buf[4] = 2;
358 dbg("%s - stop bits = 2", __FUNCTION__);
359 } else {
360 buf[4] = 0;
361 dbg("%s - stop bits = 1", __FUNCTION__);
364 if (cflag & PARENB) {
365 /* For reference buf[5]=0 is none parity */
366 /* For reference buf[5]=1 is odd parity */
367 /* For reference buf[5]=2 is even parity */
368 /* For reference buf[5]=3 is mark parity */
369 /* For reference buf[5]=4 is space parity */
370 if (cflag & PARODD) {
371 buf[5] = 1;
372 dbg("%s - parity = odd", __FUNCTION__);
373 } else {
374 buf[5] = 2;
375 dbg("%s - parity = even", __FUNCTION__);
377 } else {
378 buf[5] = 0;
379 dbg("%s - parity = none", __FUNCTION__);
382 i = usb_control_msg (serial->dev, usb_sndctrlpipe (serial->dev, 0),
383 SET_LINE_REQUEST, SET_LINE_REQUEST_TYPE,
384 0, 0, buf, 7, 100);
385 dbg ("0x21:0x20:0:0 %d", i);
387 /* change control lines if we are switching to or from B0 */
388 spin_lock_irqsave(&priv->lock, flags);
389 control = priv->line_control;
390 if ((cflag & CBAUD) == B0)
391 priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
392 else
393 priv->line_control |= (CONTROL_DTR | CONTROL_RTS);
394 if (control != priv->line_control) {
395 control = priv->line_control;
396 spin_unlock_irqrestore(&priv->lock, flags);
397 set_control_lines(serial->dev, control);
398 } else {
399 spin_unlock_irqrestore(&priv->lock, flags);
402 buf[0] = buf[1] = buf[2] = buf[3] = buf[4] = buf[5] = buf[6] = 0;
404 i = usb_control_msg (serial->dev, usb_rcvctrlpipe (serial->dev, 0),
405 GET_LINE_REQUEST, GET_LINE_REQUEST_TYPE,
406 0, 0, buf, 7, 100);
407 dbg ("0xa1:0x21:0:0 %d - %x %x %x %x %x %x %x", i,
408 buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6]);
410 if (cflag & CRTSCTS) {
411 __u16 index;
412 if (priv->type == HX)
413 index = 0x61;
414 else
415 index = 0x41;
416 i = usb_control_msg(serial->dev,
417 usb_sndctrlpipe(serial->dev, 0),
418 VENDOR_WRITE_REQUEST,
419 VENDOR_WRITE_REQUEST_TYPE,
420 0x0, index, NULL, 0, 100);
421 dbg ("0x40:0x1:0x0:0x%x %d", index, i);
424 kfree (buf);
427 static int pl2303_open (struct usb_serial_port *port, struct file *filp)
429 struct termios tmp_termios;
430 struct usb_serial *serial = port->serial;
431 struct pl2303_private *priv = usb_get_serial_port_data(port);
432 unsigned char *buf;
433 int result;
435 dbg("%s - port %d", __FUNCTION__, port->number);
437 usb_clear_halt(serial->dev, port->write_urb->pipe);
438 usb_clear_halt(serial->dev, port->read_urb->pipe);
440 buf = kmalloc(10, GFP_KERNEL);
441 if (buf==NULL)
442 return -ENOMEM;
444 #define FISH(a,b,c,d) \
445 result=usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev,0), \
446 b, a, c, d, buf, 1, 100); \
447 dbg("0x%x:0x%x:0x%x:0x%x %d - %x",a,b,c,d,result,buf[0]);
449 #define SOUP(a,b,c,d) \
450 result=usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev,0), \
451 b, a, c, d, NULL, 0, 100); \
452 dbg("0x%x:0x%x:0x%x:0x%x %d",a,b,c,d,result);
454 FISH (VENDOR_READ_REQUEST_TYPE, VENDOR_READ_REQUEST, 0x8484, 0);
455 SOUP (VENDOR_WRITE_REQUEST_TYPE, VENDOR_WRITE_REQUEST, 0x0404, 0);
456 FISH (VENDOR_READ_REQUEST_TYPE, VENDOR_READ_REQUEST, 0x8484, 0);
457 FISH (VENDOR_READ_REQUEST_TYPE, VENDOR_READ_REQUEST, 0x8383, 0);
458 FISH (VENDOR_READ_REQUEST_TYPE, VENDOR_READ_REQUEST, 0x8484, 0);
459 SOUP (VENDOR_WRITE_REQUEST_TYPE, VENDOR_WRITE_REQUEST, 0x0404, 1);
460 FISH (VENDOR_READ_REQUEST_TYPE, VENDOR_READ_REQUEST, 0x8484, 0);
461 FISH (VENDOR_READ_REQUEST_TYPE, VENDOR_READ_REQUEST, 0x8383, 0);
462 SOUP (VENDOR_WRITE_REQUEST_TYPE, VENDOR_WRITE_REQUEST, 0, 1);
463 SOUP (VENDOR_WRITE_REQUEST_TYPE, VENDOR_WRITE_REQUEST, 1, 0);
465 if (priv->type == HX) {
466 /* HX chip */
467 SOUP (VENDOR_WRITE_REQUEST_TYPE, VENDOR_WRITE_REQUEST, 2, 0x44);
468 /* reset upstream data pipes */
469 SOUP (VENDOR_WRITE_REQUEST_TYPE, VENDOR_WRITE_REQUEST, 8, 0);
470 SOUP (VENDOR_WRITE_REQUEST_TYPE, VENDOR_WRITE_REQUEST, 9, 0);
471 } else {
472 SOUP (VENDOR_WRITE_REQUEST_TYPE, VENDOR_WRITE_REQUEST, 2, 0x24);
475 kfree(buf);
477 /* Setup termios */
478 if (port->tty) {
479 pl2303_set_termios (port, &tmp_termios);
482 //FIXME: need to assert RTS and DTR if CRTSCTS off
484 dbg("%s - submitting read urb", __FUNCTION__);
485 port->read_urb->dev = serial->dev;
486 result = usb_submit_urb (port->read_urb, GFP_KERNEL);
487 if (result) {
488 dev_err(&port->dev, "%s - failed submitting read urb, error %d\n", __FUNCTION__, result);
489 pl2303_close (port, NULL);
490 return -EPROTO;
493 dbg("%s - submitting interrupt urb", __FUNCTION__);
494 port->interrupt_in_urb->dev = serial->dev;
495 result = usb_submit_urb (port->interrupt_in_urb, GFP_KERNEL);
496 if (result) {
497 dev_err(&port->dev, "%s - failed submitting interrupt urb, error %d\n", __FUNCTION__, result);
498 pl2303_close (port, NULL);
499 return -EPROTO;
501 return 0;
505 static void pl2303_close (struct usb_serial_port *port, struct file *filp)
507 struct pl2303_private *priv;
508 unsigned long flags;
509 unsigned int c_cflag;
510 int result;
512 dbg("%s - port %d", __FUNCTION__, port->number);
514 /* shutdown our urbs */
515 dbg("%s - shutting down urbs", __FUNCTION__);
516 result = usb_unlink_urb (port->write_urb);
517 if (result)
518 dbg("%s - usb_unlink_urb (write_urb)"
519 " failed with reason: %d", __FUNCTION__,
520 result);
522 result = usb_unlink_urb (port->read_urb);
523 if (result)
524 dbg("%s - usb_unlink_urb (read_urb) "
525 "failed with reason: %d", __FUNCTION__,
526 result);
528 result = usb_unlink_urb (port->interrupt_in_urb);
529 if (result)
530 dbg("%s - usb_unlink_urb (interrupt_in_urb)"
531 " failed with reason: %d", __FUNCTION__,
532 result);
534 if (port->tty) {
535 c_cflag = port->tty->termios->c_cflag;
536 if (c_cflag & HUPCL) {
537 /* drop DTR and RTS */
538 priv = usb_get_serial_port_data(port);
539 spin_lock_irqsave(&priv->lock, flags);
540 priv->line_control = 0;
541 spin_unlock_irqrestore (&priv->lock, flags);
542 set_control_lines (port->serial->dev, 0);
548 static int pl2303_tiocmset (struct usb_serial_port *port, struct file *file,
549 unsigned int set, unsigned int clear)
551 struct pl2303_private *priv = usb_get_serial_port_data(port);
552 unsigned long flags;
553 u8 control;
555 spin_lock_irqsave (&priv->lock, flags);
556 if (set & TIOCM_RTS)
557 priv->line_control |= CONTROL_RTS;
558 if (set & TIOCM_DTR)
559 priv->line_control |= CONTROL_DTR;
560 if (clear & TIOCM_RTS)
561 priv->line_control &= ~CONTROL_RTS;
562 if (clear & TIOCM_DTR)
563 priv->line_control &= ~CONTROL_DTR;
564 control = priv->line_control;
565 spin_unlock_irqrestore (&priv->lock, flags);
567 return set_control_lines (port->serial->dev, control);
570 static int pl2303_tiocmget (struct usb_serial_port *port, struct file *file)
572 struct pl2303_private *priv = usb_get_serial_port_data(port);
573 unsigned long flags;
574 unsigned int mcr;
575 unsigned int status;
576 unsigned int result;
578 dbg("%s (%d)", __FUNCTION__, port->number);
580 spin_lock_irqsave (&priv->lock, flags);
581 mcr = priv->line_control;
582 status = priv->line_status;
583 spin_unlock_irqrestore (&priv->lock, flags);
585 result = ((mcr & CONTROL_DTR) ? TIOCM_DTR : 0)
586 | ((mcr & CONTROL_RTS) ? TIOCM_RTS : 0)
587 | ((status & UART_CTS) ? TIOCM_CTS : 0)
588 | ((status & UART_DSR) ? TIOCM_DSR : 0)
589 | ((status & UART_RING) ? TIOCM_RI : 0)
590 | ((status & UART_DCD) ? TIOCM_CD : 0);
592 dbg("%s - result = %x", __FUNCTION__, result);
594 return result;
597 static int wait_modem_info(struct usb_serial_port *port, unsigned int arg)
599 struct pl2303_private *priv = usb_get_serial_port_data(port);
600 unsigned long flags;
601 unsigned int prevstatus;
602 unsigned int status;
603 unsigned int changed;
605 spin_lock_irqsave (&priv->lock, flags);
606 prevstatus = priv->line_status;
607 spin_unlock_irqrestore (&priv->lock, flags);
609 while (1) {
610 interruptible_sleep_on(&priv->delta_msr_wait);
611 /* see if a signal did it */
612 if (signal_pending(current))
613 return -ERESTARTSYS;
615 spin_lock_irqsave (&priv->lock, flags);
616 status = priv->line_status;
617 spin_unlock_irqrestore (&priv->lock, flags);
619 changed=prevstatus^status;
621 if (((arg & TIOCM_RNG) && (changed & UART_RING)) ||
622 ((arg & TIOCM_DSR) && (changed & UART_DSR)) ||
623 ((arg & TIOCM_CD) && (changed & UART_DCD)) ||
624 ((arg & TIOCM_CTS) && (changed & UART_CTS)) ) {
625 return 0;
627 prevstatus = status;
629 /* NOTREACHED */
630 return 0;
633 static int pl2303_ioctl (struct usb_serial_port *port, struct file *file, unsigned int cmd, unsigned long arg)
635 dbg("%s (%d) cmd = 0x%04x", __FUNCTION__, port->number, cmd);
637 switch (cmd) {
638 case TIOCMIWAIT:
639 dbg("%s (%d) TIOCMIWAIT", __FUNCTION__, port->number);
640 return wait_modem_info(port, arg);
642 default:
643 dbg("%s not supported = 0x%04x", __FUNCTION__, cmd);
644 break;
647 return -ENOIOCTLCMD;
650 static void pl2303_break_ctl (struct usb_serial_port *port, int break_state)
652 struct usb_serial *serial = port->serial;
653 u16 state;
654 int result;
656 dbg("%s - port %d", __FUNCTION__, port->number);
658 if (break_state == 0)
659 state = BREAK_OFF;
660 else
661 state = BREAK_ON;
662 dbg("%s - turning break %s", __FUNCTION__, state==BREAK_OFF ? "off" : "on");
664 result = usb_control_msg (serial->dev, usb_sndctrlpipe (serial->dev, 0),
665 BREAK_REQUEST, BREAK_REQUEST_TYPE, state,
666 0, NULL, 0, 100);
667 if (result)
668 dbg("%s - error sending break = %d", __FUNCTION__, result);
672 static void pl2303_shutdown (struct usb_serial *serial)
674 int i;
676 dbg("%s", __FUNCTION__);
678 for (i = 0; i < serial->num_ports; ++i) {
679 kfree (usb_get_serial_port_data(serial->port[i]));
680 usb_set_serial_port_data(serial->port[i], NULL);
685 static void pl2303_read_int_callback (struct urb *urb, struct pt_regs *regs)
687 struct usb_serial_port *port = (struct usb_serial_port *) urb->context;
688 struct pl2303_private *priv = usb_get_serial_port_data(port);
689 unsigned char *data = urb->transfer_buffer;
690 unsigned long flags;
691 int status;
692 u8 uart_state;
694 dbg("%s (%d)", __FUNCTION__, port->number);
696 switch (urb->status) {
697 case 0:
698 /* success */
699 break;
700 case -ECONNRESET:
701 case -ENOENT:
702 case -ESHUTDOWN:
703 /* this urb is terminated, clean up */
704 dbg("%s - urb shutting down with status: %d", __FUNCTION__, urb->status);
705 return;
706 default:
707 dbg("%s - nonzero urb status received: %d", __FUNCTION__, urb->status);
708 goto exit;
712 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, urb->actual_length, urb->transfer_buffer);
714 if (urb->actual_length < UART_STATE)
715 goto exit;
717 /* Save off the uart status for others to look at */
718 uart_state = data[UART_STATE];
719 spin_lock_irqsave(&priv->lock, flags);
720 uart_state |= (priv->line_status & UART_STATE_TRANSIENT_MASK);
721 priv->line_status = uart_state;
722 spin_unlock_irqrestore(&priv->lock, flags);
724 exit:
725 status = usb_submit_urb (urb, GFP_ATOMIC);
726 if (status)
727 dev_err(&urb->dev->dev, "%s - usb_submit_urb failed with result %d\n",
728 __FUNCTION__, status);
732 static void pl2303_read_bulk_callback (struct urb *urb, struct pt_regs *regs)
734 struct usb_serial_port *port = (struct usb_serial_port *) urb->context;
735 struct pl2303_private *priv = usb_get_serial_port_data(port);
736 struct tty_struct *tty;
737 unsigned char *data = urb->transfer_buffer;
738 unsigned long flags;
739 int i;
740 int result;
741 u8 status;
742 char tty_flag;
744 dbg("%s - port %d", __FUNCTION__, port->number);
746 if (urb->status) {
747 dbg("%s - urb->status = %d", __FUNCTION__, urb->status);
748 if (!port->open_count) {
749 dbg("%s - port is closed, exiting.", __FUNCTION__);
750 return;
752 if (urb->status == -EPROTO) {
753 /* PL2303 mysteriously fails with -EPROTO reschedule the read */
754 dbg("%s - caught -EPROTO, resubmitting the urb", __FUNCTION__);
755 urb->status = 0;
756 urb->dev = port->serial->dev;
757 result = usb_submit_urb(urb, GFP_ATOMIC);
758 if (result)
759 dev_err(&urb->dev->dev, "%s - failed resubmitting read urb, error %d\n", __FUNCTION__, result);
760 return;
762 dbg("%s - unable to handle the error, exiting.", __FUNCTION__);
763 return;
766 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, urb->actual_length, data);
768 /* get tty_flag from status */
769 tty_flag = TTY_NORMAL;
771 spin_lock_irqsave(&priv->lock, flags);
772 status = priv->line_status;
773 priv->line_status &= ~UART_STATE_TRANSIENT_MASK;
774 spin_unlock_irqrestore(&priv->lock, flags);
775 wake_up_interruptible (&priv->delta_msr_wait);
777 /* break takes precedence over parity, */
778 /* which takes precedence over framing errors */
779 if (status & UART_BREAK_ERROR )
780 tty_flag = TTY_BREAK;
781 else if (status & UART_PARITY_ERROR)
782 tty_flag = TTY_PARITY;
783 else if (status & UART_FRAME_ERROR)
784 tty_flag = TTY_FRAME;
785 dbg("%s - tty_flag = %d", __FUNCTION__, tty_flag);
787 tty = port->tty;
788 if (tty && urb->actual_length) {
789 /* overrun is special, not associated with a char */
790 if (status & UART_OVERRUN_ERROR)
791 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
793 for (i = 0; i < urb->actual_length; ++i) {
794 if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
795 tty_flip_buffer_push(tty);
797 tty_insert_flip_char (tty, data[i], tty_flag);
799 tty_flip_buffer_push (tty);
802 /* Schedule the next read _if_ we are still open */
803 if (port->open_count) {
804 urb->dev = port->serial->dev;
805 result = usb_submit_urb(urb, GFP_ATOMIC);
806 if (result)
807 dev_err(&urb->dev->dev, "%s - failed resubmitting read urb, error %d\n", __FUNCTION__, result);
810 return;
815 static void pl2303_write_bulk_callback (struct urb *urb, struct pt_regs *regs)
817 struct usb_serial_port *port = (struct usb_serial_port *) urb->context;
818 int result;
820 dbg("%s - port %d", __FUNCTION__, port->number);
822 if (urb->status) {
823 /* error in the urb, so we have to resubmit it */
824 dbg("%s - Overflow in write", __FUNCTION__);
825 dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, urb->status);
826 port->write_urb->transfer_buffer_length = 1;
827 port->write_urb->dev = port->serial->dev;
828 result = usb_submit_urb (port->write_urb, GFP_ATOMIC);
829 if (result)
830 dev_err(&urb->dev->dev, "%s - failed resubmitting write urb, error %d\n", __FUNCTION__, result);
832 return;
835 schedule_work(&port->work);
839 static int __init pl2303_init (void)
841 int retval;
842 retval = usb_serial_register(&pl2303_device);
843 if (retval)
844 goto failed_usb_serial_register;
845 retval = usb_register(&pl2303_driver);
846 if (retval)
847 goto failed_usb_register;
848 info(DRIVER_DESC " " DRIVER_VERSION);
849 return 0;
850 failed_usb_register:
851 usb_serial_deregister(&pl2303_device);
852 failed_usb_serial_register:
853 return retval;
857 static void __exit pl2303_exit (void)
859 usb_deregister (&pl2303_driver);
860 usb_serial_deregister (&pl2303_device);
864 module_init(pl2303_init);
865 module_exit(pl2303_exit);
867 MODULE_DESCRIPTION(DRIVER_DESC);
868 MODULE_LICENSE("GPL");
870 module_param(debug, bool, S_IRUGO | S_IWUSR);
871 MODULE_PARM_DESC(debug, "Debug enabled or not");