Merge with Linux 2.5.74.
[linux-2.6/linux-mips.git] / drivers / usb / serial / pl2303.c
blobe400cba84cd9b0959b28765ffca0f842f9dd704b
1 /*
2 * Prolific PL2303 USB to serial adaptor driver
4 * Copyright (C) 2001-2003 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/spinlock.h>
49 #include <asm/uaccess.h>
50 #include <linux/usb.h>
52 #ifdef CONFIG_USB_SERIAL_DEBUG
53 static int debug = 1;
54 #else
55 static int debug;
56 #endif
58 #include "usb-serial.h"
59 #include "pl2303.h"
62 * Version Information
64 #define DRIVER_VERSION "v0.9"
65 #define DRIVER_DESC "Prolific PL2303 USB to serial adaptor driver"
69 static struct usb_device_id id_table [] = {
70 { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID) },
71 { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_RSAQ2) },
72 { USB_DEVICE(IODATA_VENDOR_ID, IODATA_PRODUCT_ID) },
73 { USB_DEVICE(ATEN_VENDOR_ID, ATEN_PRODUCT_ID) },
74 { USB_DEVICE(ELCOM_VENDOR_ID, ELCOM_PRODUCT_ID) },
75 { USB_DEVICE(ITEGNO_VENDOR_ID, ITEGNO_PRODUCT_ID) },
76 { USB_DEVICE(MA620_VENDOR_ID, MA620_PRODUCT_ID) },
77 { USB_DEVICE(RATOC_VENDOR_ID, RATOC_PRODUCT_ID) },
78 { USB_DEVICE(TRIPP_VENDOR_ID, TRIPP_PRODUCT_ID) },
79 { USB_DEVICE(RADIOSHACK_VENDOR_ID, RADIOSHACK_PRODUCT_ID) },
80 { USB_DEVICE(DCU10_VENDOR_ID, DCU10_PRODUCT_ID) },
81 { } /* Terminating entry */
84 MODULE_DEVICE_TABLE (usb, id_table);
86 static struct usb_driver pl2303_driver = {
87 .owner = THIS_MODULE,
88 .name = "pl2303",
89 .probe = usb_serial_probe,
90 .disconnect = usb_serial_disconnect,
91 .id_table = id_table,
94 #define SET_LINE_REQUEST_TYPE 0x21
95 #define SET_LINE_REQUEST 0x20
97 #define SET_CONTROL_REQUEST_TYPE 0x21
98 #define SET_CONTROL_REQUEST 0x22
99 #define CONTROL_DTR 0x01
100 #define CONTROL_RTS 0x02
102 #define BREAK_REQUEST_TYPE 0x21
103 #define BREAK_REQUEST 0x23
104 #define BREAK_ON 0xffff
105 #define BREAK_OFF 0x0000
107 #define GET_LINE_REQUEST_TYPE 0xa1
108 #define GET_LINE_REQUEST 0x21
110 #define VENDOR_WRITE_REQUEST_TYPE 0x40
111 #define VENDOR_WRITE_REQUEST 0x01
113 #define VENDOR_READ_REQUEST_TYPE 0xc0
114 #define VENDOR_READ_REQUEST 0x01
116 #define UART_STATE 0x08
117 #define UART_DCD 0x01
118 #define UART_DSR 0x02
119 #define UART_BREAK_ERROR 0x04
120 #define UART_RING 0x08
121 #define UART_FRAME_ERROR 0x10
122 #define UART_PARITY_ERROR 0x20
123 #define UART_OVERRUN_ERROR 0x40
124 #define UART_CTS 0x80
126 /* function prototypes for a PL2303 serial converter */
127 static int pl2303_open (struct usb_serial_port *port, struct file *filp);
128 static void pl2303_close (struct usb_serial_port *port, struct file *filp);
129 static void pl2303_set_termios (struct usb_serial_port *port,
130 struct termios *old);
131 static int pl2303_ioctl (struct usb_serial_port *port, struct file *file,
132 unsigned int cmd, unsigned long arg);
133 static void pl2303_read_int_callback (struct urb *urb, struct pt_regs *regs);
134 static void pl2303_read_bulk_callback (struct urb *urb, struct pt_regs *regs);
135 static void pl2303_write_bulk_callback (struct urb *urb, struct pt_regs *regs);
136 static int pl2303_write (struct usb_serial_port *port, int from_user,
137 const unsigned char *buf, int count);
138 static void pl2303_break_ctl(struct usb_serial_port *port,int break_state);
139 static int pl2303_tiocmget (struct usb_serial_port *port, struct file *file);
140 static int pl2303_tiocmset (struct usb_serial_port *port, struct file *file,
141 unsigned int set, unsigned int clear);
142 static int pl2303_startup (struct usb_serial *serial);
143 static void pl2303_shutdown (struct usb_serial *serial);
146 /* All of the device info needed for the PL2303 SIO serial converter */
147 static struct usb_serial_device_type pl2303_device = {
148 .owner = THIS_MODULE,
149 .name = "PL-2303",
150 .id_table = id_table,
151 .num_interrupt_in = NUM_DONT_CARE,
152 .num_bulk_in = 1,
153 .num_bulk_out = 1,
154 .num_ports = 1,
155 .open = pl2303_open,
156 .close = pl2303_close,
157 .write = pl2303_write,
158 .ioctl = pl2303_ioctl,
159 .break_ctl = pl2303_break_ctl,
160 .set_termios = pl2303_set_termios,
161 .tiocmget = pl2303_tiocmget,
162 .tiocmset = pl2303_tiocmset,
163 .read_bulk_callback = pl2303_read_bulk_callback,
164 .read_int_callback = pl2303_read_int_callback,
165 .write_bulk_callback = pl2303_write_bulk_callback,
166 .attach = pl2303_startup,
167 .shutdown = pl2303_shutdown,
170 struct pl2303_private {
171 spinlock_t lock;
172 u8 line_control;
173 u8 line_status;
174 u8 termios_initialized;
178 static int pl2303_startup (struct usb_serial *serial)
180 struct pl2303_private *priv;
181 int i;
183 for (i = 0; i < serial->num_ports; ++i) {
184 priv = kmalloc (sizeof (struct pl2303_private), GFP_KERNEL);
185 if (!priv)
186 return -ENOMEM;
187 memset (priv, 0x00, sizeof (struct pl2303_private));
188 spin_lock_init(&priv->lock);
189 usb_set_serial_port_data(&serial->port[i], priv);
191 return 0;
194 static int set_control_lines (struct usb_device *dev, u8 value)
196 int retval;
198 retval = usb_control_msg (dev, usb_sndctrlpipe (dev, 0),
199 SET_CONTROL_REQUEST, SET_CONTROL_REQUEST_TYPE,
200 value, 0, NULL, 0, 100);
201 dbg("%s - value = %d, retval = %d", __FUNCTION__, value, retval);
202 return retval;
205 static int pl2303_write (struct usb_serial_port *port, int from_user, const unsigned char *buf, int count)
207 int result;
209 dbg("%s - port %d, %d bytes", __FUNCTION__, port->number, count);
211 if (port->write_urb->status == -EINPROGRESS) {
212 dbg("%s - already writing", __FUNCTION__);
213 return 0;
216 count = (count > port->bulk_out_size) ? port->bulk_out_size : count;
217 if (from_user) {
218 if (copy_from_user (port->write_urb->transfer_buffer, buf, count))
219 return -EFAULT;
220 } else {
221 memcpy (port->write_urb->transfer_buffer, buf, count);
224 usb_serial_debug_data (__FILE__, __FUNCTION__, count, port->write_urb->transfer_buffer);
226 port->write_urb->transfer_buffer_length = count;
227 port->write_urb->dev = port->serial->dev;
228 result = usb_submit_urb (port->write_urb, GFP_ATOMIC);
229 if (result)
230 dev_err(&port->dev, "%s - failed submitting write urb, error %d\n", __FUNCTION__, result);
231 else
232 result = count;
234 return result;
239 static void pl2303_set_termios (struct usb_serial_port *port, struct termios *old_termios)
241 struct usb_serial *serial = port->serial;
242 struct pl2303_private *priv = usb_get_serial_port_data(port);
243 unsigned long flags;
244 unsigned int cflag;
245 unsigned char *buf;
246 int baud;
247 int i;
249 dbg("%s - port %d", __FUNCTION__, port->number);
251 if ((!port->tty) || (!port->tty->termios)) {
252 dbg("%s - no tty structures", __FUNCTION__);
253 return;
256 spin_lock_irqsave(&priv->lock, flags);
257 if (!priv->termios_initialized) {
258 *(port->tty->termios) = tty_std_termios;
259 port->tty->termios->c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
260 priv->termios_initialized = 1;
262 spin_unlock_irqrestore(&priv->lock, flags);
264 cflag = port->tty->termios->c_cflag;
265 /* check that they really want us to change something */
266 if (old_termios) {
267 if ((cflag == old_termios->c_cflag) &&
268 (RELEVANT_IFLAG(port->tty->termios->c_iflag) == RELEVANT_IFLAG(old_termios->c_iflag))) {
269 dbg("%s - nothing to change...", __FUNCTION__);
270 return;
274 buf = kmalloc (7, GFP_KERNEL);
275 if (!buf) {
276 dev_err(&port->dev, "%s - out of memory.\n", __FUNCTION__);
277 return;
279 memset (buf, 0x00, 0x07);
281 i = usb_control_msg (serial->dev, usb_rcvctrlpipe (serial->dev, 0),
282 GET_LINE_REQUEST, GET_LINE_REQUEST_TYPE,
283 0, 0, buf, 7, 100);
284 dbg ("0xa1:0x21:0:0 %d - %x %x %x %x %x %x %x", i,
285 buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6]);
288 i = usb_control_msg (serial->dev, usb_sndctrlpipe (serial->dev, 0),
289 VENDOR_WRITE_REQUEST, VENDOR_WRITE_REQUEST_TYPE,
290 0, 1, NULL, 0, 100);
292 dbg ("0x40:1:0:1 %d", i);
294 if (cflag & CSIZE) {
295 switch (cflag & CSIZE) {
296 case CS5: buf[6] = 5; break;
297 case CS6: buf[6] = 6; break;
298 case CS7: buf[6] = 7; break;
299 default:
300 case CS8: buf[6] = 8; break;
302 dbg("%s - data bits = %d", __FUNCTION__, buf[6]);
305 baud = 0;
306 switch (cflag & CBAUD) {
307 case B0: baud = 0; break;
308 case B75: baud = 75; break;
309 case B150: baud = 150; break;
310 case B300: baud = 300; break;
311 case B600: baud = 600; break;
312 case B1200: baud = 1200; break;
313 case B1800: baud = 1800; break;
314 case B2400: baud = 2400; break;
315 case B4800: baud = 4800; break;
316 case B9600: baud = 9600; break;
317 case B19200: baud = 19200; break;
318 case B38400: baud = 38400; break;
319 case B57600: baud = 57600; break;
320 case B115200: baud = 115200; break;
321 case B230400: baud = 230400; break;
322 case B460800: baud = 460800; break;
323 default:
324 dev_err(&port->dev, "pl2303 driver does not support the baudrate requested (fix it)\n");
325 break;
327 dbg("%s - baud = %d", __FUNCTION__, baud);
328 if (baud) {
329 buf[0] = baud & 0xff;
330 buf[1] = (baud >> 8) & 0xff;
331 buf[2] = (baud >> 16) & 0xff;
332 buf[3] = (baud >> 24) & 0xff;
335 /* For reference buf[4]=0 is 1 stop bits */
336 /* For reference buf[4]=1 is 1.5 stop bits */
337 /* For reference buf[4]=2 is 2 stop bits */
338 if (cflag & CSTOPB) {
339 buf[4] = 2;
340 dbg("%s - stop bits = 2", __FUNCTION__);
341 } else {
342 buf[4] = 0;
343 dbg("%s - stop bits = 1", __FUNCTION__);
346 if (cflag & PARENB) {
347 /* For reference buf[5]=0 is none parity */
348 /* For reference buf[5]=1 is odd parity */
349 /* For reference buf[5]=2 is even parity */
350 /* For reference buf[5]=3 is mark parity */
351 /* For reference buf[5]=4 is space parity */
352 if (cflag & PARODD) {
353 buf[5] = 1;
354 dbg("%s - parity = odd", __FUNCTION__);
355 } else {
356 buf[5] = 2;
357 dbg("%s - parity = even", __FUNCTION__);
359 } else {
360 buf[5] = 0;
361 dbg("%s - parity = none", __FUNCTION__);
364 i = usb_control_msg (serial->dev, usb_sndctrlpipe (serial->dev, 0),
365 SET_LINE_REQUEST, SET_LINE_REQUEST_TYPE,
366 0, 0, buf, 7, 100);
367 dbg ("0x21:0x20:0:0 %d", i);
369 if (cflag && CBAUD) {
370 u8 control;
372 spin_lock_irqsave(&priv->lock, flags);
373 if ((cflag && CBAUD) == B0)
374 priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
375 else
376 priv->line_control |= (CONTROL_DTR | CONTROL_RTS);
377 control = priv->line_control;
378 spin_unlock_irqrestore(&priv->lock, flags);
379 set_control_lines (serial->dev, control);
382 buf[0] = buf[1] = buf[2] = buf[3] = buf[4] = buf[5] = buf[6] = 0;
384 i = usb_control_msg (serial->dev, usb_rcvctrlpipe (serial->dev, 0),
385 GET_LINE_REQUEST, GET_LINE_REQUEST_TYPE,
386 0, 0, buf, 7, 100);
387 dbg ("0xa1:0x21:0:0 %d - %x %x %x %x %x %x %x", i,
388 buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6]);
390 if (cflag & CRTSCTS) {
391 i = usb_control_msg (serial->dev, usb_sndctrlpipe (serial->dev, 0),
392 VENDOR_WRITE_REQUEST, VENDOR_WRITE_REQUEST_TYPE,
393 0x0, 0x41, NULL, 0, 100);
394 dbg ("0x40:0x1:0x0:0x41 %d", i);
397 kfree (buf);
401 static int pl2303_open (struct usb_serial_port *port, struct file *filp)
403 struct termios tmp_termios;
404 struct usb_serial *serial = port->serial;
405 unsigned char buf[10];
406 int result;
408 if (port_paranoia_check (port, __FUNCTION__))
409 return -ENODEV;
411 dbg("%s - port %d", __FUNCTION__, port->number);
413 #define FISH(a,b,c,d) \
414 result=usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev,0), \
415 b, a, c, d, buf, 1, 100); \
416 dbg("0x%x:0x%x:0x%x:0x%x %d - %x",a,b,c,d,result,buf[0]);
418 #define SOUP(a,b,c,d) \
419 result=usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev,0), \
420 b, a, c, d, NULL, 0, 100); \
421 dbg("0x%x:0x%x:0x%x:0x%x %d",a,b,c,d,result);
423 FISH (VENDOR_READ_REQUEST_TYPE, VENDOR_READ_REQUEST, 0x8484, 0);
424 SOUP (VENDOR_WRITE_REQUEST_TYPE, VENDOR_WRITE_REQUEST, 0x0404, 0);
425 FISH (VENDOR_READ_REQUEST_TYPE, VENDOR_READ_REQUEST, 0x8484, 0);
426 FISH (VENDOR_READ_REQUEST_TYPE, VENDOR_READ_REQUEST, 0x8383, 0);
427 FISH (VENDOR_READ_REQUEST_TYPE, VENDOR_READ_REQUEST, 0x8484, 0);
428 SOUP (VENDOR_WRITE_REQUEST_TYPE, VENDOR_WRITE_REQUEST, 0x0404, 1);
429 FISH (VENDOR_READ_REQUEST_TYPE, VENDOR_READ_REQUEST, 0x8484, 0);
430 FISH (VENDOR_READ_REQUEST_TYPE, VENDOR_READ_REQUEST, 0x8383, 0);
431 SOUP (VENDOR_WRITE_REQUEST_TYPE, VENDOR_WRITE_REQUEST, 0, 1);
432 SOUP (VENDOR_WRITE_REQUEST_TYPE, VENDOR_WRITE_REQUEST, 1, 0xc0);
433 SOUP (VENDOR_WRITE_REQUEST_TYPE, VENDOR_WRITE_REQUEST, 2, 4);
435 /* Setup termios */
436 if (port->tty) {
437 pl2303_set_termios (port, &tmp_termios);
440 //FIXME: need to assert RTS and DTR if CRTSCTS off
442 dbg("%s - submitting read urb", __FUNCTION__);
443 port->read_urb->dev = serial->dev;
444 result = usb_submit_urb (port->read_urb, GFP_KERNEL);
445 if (result) {
446 dev_err(&port->dev, "%s - failed submitting read urb, error %d\n", __FUNCTION__, result);
447 pl2303_close (port, NULL);
448 return -EPROTO;
451 dbg("%s - submitting interrupt urb", __FUNCTION__);
452 port->interrupt_in_urb->dev = serial->dev;
453 result = usb_submit_urb (port->interrupt_in_urb, GFP_KERNEL);
454 if (result) {
455 dev_err(&port->dev, "%s - failed submitting interrupt urb, error %d\n", __FUNCTION__, result);
456 pl2303_close (port, NULL);
457 return -EPROTO;
459 return 0;
463 static void pl2303_close (struct usb_serial_port *port, struct file *filp)
465 struct usb_serial *serial;
466 struct pl2303_private *priv;
467 unsigned long flags;
468 unsigned int c_cflag;
469 int result;
471 if (port_paranoia_check (port, __FUNCTION__))
472 return;
473 serial = get_usb_serial (port, __FUNCTION__);
474 if (!serial)
475 return;
477 dbg("%s - port %d", __FUNCTION__, port->number);
479 /* shutdown our urbs */
480 dbg("%s - shutting down urbs", __FUNCTION__);
481 result = usb_unlink_urb (port->write_urb);
482 if (result)
483 dbg("%s - usb_unlink_urb (write_urb)"
484 " failed with reason: %d", __FUNCTION__,
485 result);
487 result = usb_unlink_urb (port->read_urb);
488 if (result)
489 dbg("%s - usb_unlink_urb (read_urb) "
490 "failed with reason: %d", __FUNCTION__,
491 result);
493 result = usb_unlink_urb (port->interrupt_in_urb);
494 if (result)
495 dbg("%s - usb_unlink_urb (interrupt_in_urb)"
496 " failed with reason: %d", __FUNCTION__,
497 result);
499 if (port->tty) {
500 c_cflag = port->tty->termios->c_cflag;
501 if (c_cflag & HUPCL) {
502 /* drop DTR and RTS */
503 priv = usb_get_serial_port_data(port);
504 spin_lock_irqsave(&priv->lock, flags);
505 priv->line_control = 0;
506 spin_unlock_irqrestore (&priv->lock, flags);
507 set_control_lines (port->serial->dev, 0);
513 static int pl2303_tiocmset (struct usb_serial_port *port, struct file *file,
514 unsigned int set, unsigned int clear)
516 struct pl2303_private *priv = usb_get_serial_port_data(port);
517 unsigned long flags;
518 u8 control;
520 spin_lock_irqsave (&priv->lock, flags);
521 if (set & TIOCM_RTS)
522 priv->line_control |= CONTROL_RTS;
523 if (set & TIOCM_DTR)
524 priv->line_control |= CONTROL_DTR;
525 if (clear & TIOCM_RTS)
526 priv->line_control &= ~CONTROL_RTS;
527 if (clear & TIOCM_DTR)
528 priv->line_control &= ~CONTROL_DTR;
529 control = priv->line_control;
530 spin_unlock_irqrestore (&priv->lock, flags);
532 return set_control_lines (port->serial->dev, control);
535 static int pl2303_tiocmget (struct usb_serial_port *port, struct file *file)
537 struct pl2303_private *priv = usb_get_serial_port_data(port);
538 unsigned long flags;
539 unsigned int mcr;
540 unsigned int status;
541 unsigned int result;
543 dbg("%s (%d)", __FUNCTION__, port->number);
545 spin_lock_irqsave (&priv->lock, flags);
546 mcr = priv->line_control;
547 status = priv->line_status;
548 spin_unlock_irqrestore (&priv->lock, flags);
550 result = ((mcr & CONTROL_DTR) ? TIOCM_DTR : 0)
551 | ((mcr & CONTROL_RTS) ? TIOCM_RTS : 0)
552 | ((status & UART_CTS) ? TIOCM_CTS : 0)
553 | ((status & UART_DSR) ? TIOCM_DSR : 0);
555 dbg("%s - result = %x", __FUNCTION__, result);
557 return result;
560 static int pl2303_ioctl (struct usb_serial_port *port, struct file *file, unsigned int cmd, unsigned long arg)
562 dbg("%s (%d) cmd = 0x%04x", __FUNCTION__, port->number, cmd);
564 switch (cmd) {
565 default:
566 dbg("%s not supported = 0x%04x", __FUNCTION__, cmd);
567 break;
570 return -ENOIOCTLCMD;
573 static void pl2303_break_ctl (struct usb_serial_port *port, int break_state)
575 struct usb_serial *serial = port->serial;
576 u16 state;
577 int result;
579 dbg("%s - port %d", __FUNCTION__, port->number);
581 if (break_state == 0)
582 state = BREAK_OFF;
583 else
584 state = BREAK_ON;
585 dbg("%s - turning break %s", state==BREAK_OFF ? "off" : "on", __FUNCTION__);
587 result = usb_control_msg (serial->dev, usb_sndctrlpipe (serial->dev, 0),
588 BREAK_REQUEST, BREAK_REQUEST_TYPE, state,
589 0, NULL, 0, 100);
590 if (result)
591 dbg("%s - error sending break = %d", __FUNCTION__, result);
595 static void pl2303_shutdown (struct usb_serial *serial)
597 int i;
599 dbg("%s", __FUNCTION__);
601 for (i = 0; i < serial->num_ports; ++i) {
602 kfree (usb_get_serial_port_data(&serial->port[i]));
603 usb_set_serial_port_data(&serial->port[i], NULL);
608 static void pl2303_read_int_callback (struct urb *urb, struct pt_regs *regs)
610 struct usb_serial_port *port = (struct usb_serial_port *) urb->context;
611 struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
612 struct pl2303_private *priv = usb_get_serial_port_data(port);
613 unsigned char *data = urb->transfer_buffer;
614 unsigned long flags;
615 int status;
617 dbg("%s (%d)", __FUNCTION__, port->number);
619 switch (urb->status) {
620 case 0:
621 /* success */
622 break;
623 case -ECONNRESET:
624 case -ENOENT:
625 case -ESHUTDOWN:
626 /* this urb is terminated, clean up */
627 dbg("%s - urb shutting down with status: %d", __FUNCTION__, urb->status);
628 return;
629 default:
630 dbg("%s - nonzero urb status received: %d", __FUNCTION__, urb->status);
631 goto exit;
634 if (!serial) {
635 return;
638 usb_serial_debug_data (__FILE__, __FUNCTION__, urb->actual_length, urb->transfer_buffer);
640 if (urb->actual_length > UART_STATE)
641 goto exit;
643 /* Save off the uart status for others to look at */
644 spin_lock_irqsave(&priv->lock, flags);
645 priv->line_status = data[UART_STATE];
646 spin_unlock_irqrestore(&priv->lock, flags);
648 exit:
649 status = usb_submit_urb (urb, GFP_ATOMIC);
650 if (status)
651 dev_err(&urb->dev->dev, "%s - usb_submit_urb failed with result %d\n",
652 __FUNCTION__, status);
656 static void pl2303_read_bulk_callback (struct urb *urb, struct pt_regs *regs)
658 struct usb_serial_port *port = (struct usb_serial_port *) urb->context;
659 struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
660 struct pl2303_private *priv = usb_get_serial_port_data(port);
661 struct tty_struct *tty;
662 unsigned char *data = urb->transfer_buffer;
663 unsigned long flags;
664 int i;
665 int result;
666 u8 status;
667 char tty_flag;
669 if (port_paranoia_check (port, __FUNCTION__))
670 return;
672 dbg("%s - port %d", __FUNCTION__, port->number);
674 if (!serial) {
675 dbg("%s - bad serial pointer, exiting", __FUNCTION__);
676 return;
679 if (urb->status) {
680 dbg("%s - urb->status = %d", __FUNCTION__, urb->status);
681 if (!port->open_count) {
682 dbg("%s - port is closed, exiting.", __FUNCTION__);
683 return;
685 if (urb->status == -EPROTO) {
686 /* PL2303 mysteriously fails with -EPROTO reschedule the read */
687 dbg("%s - caught -EPROTO, resubmitting the urb", __FUNCTION__);
688 urb->status = 0;
689 urb->dev = serial->dev;
690 result = usb_submit_urb(urb, GFP_ATOMIC);
691 if (result)
692 dev_err(&urb->dev->dev, "%s - failed resubmitting read urb, error %d\n", __FUNCTION__, result);
693 return;
695 dbg("%s - unable to handle the error, exiting.", __FUNCTION__);
696 return;
699 usb_serial_debug_data (__FILE__, __FUNCTION__, urb->actual_length, data);
701 /* get tty_flag from status */
702 tty_flag = TTY_NORMAL;
704 spin_lock_irqsave(&priv->lock, flags);
705 status = priv->line_status;
706 spin_unlock_irqrestore(&priv->lock, flags);
708 /* break takes precedence over parity, */
709 /* which takes precedence over framing errors */
710 if (status & UART_BREAK_ERROR )
711 tty_flag = TTY_BREAK;
712 else if (status & UART_PARITY_ERROR)
713 tty_flag = TTY_PARITY;
714 else if (status & UART_FRAME_ERROR)
715 tty_flag = TTY_FRAME;
716 dbg("%s - tty_flag = %d", __FUNCTION__, tty_flag);
718 tty = port->tty;
719 if (tty && urb->actual_length) {
720 /* overrun is special, not associated with a char */
721 if (status & UART_OVERRUN_ERROR)
722 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
724 for (i = 0; i < urb->actual_length; ++i) {
725 if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
726 tty_flip_buffer_push(tty);
728 tty_insert_flip_char (tty, data[i], tty_flag);
730 tty_flip_buffer_push (tty);
733 /* Schedule the next read _if_ we are still open */
734 if (port->open_count) {
735 urb->dev = serial->dev;
736 result = usb_submit_urb(urb, GFP_ATOMIC);
737 if (result)
738 dev_err(&urb->dev->dev, "%s - failed resubmitting read urb, error %d\n", __FUNCTION__, result);
741 return;
746 static void pl2303_write_bulk_callback (struct urb *urb, struct pt_regs *regs)
748 struct usb_serial_port *port = (struct usb_serial_port *) urb->context;
749 int result;
751 if (port_paranoia_check (port, __FUNCTION__))
752 return;
754 dbg("%s - port %d", __FUNCTION__, port->number);
756 if (urb->status) {
757 /* error in the urb, so we have to resubmit it */
758 if (serial_paranoia_check (port->serial, __FUNCTION__)) {
759 return;
761 dbg("%s - Overflow in write", __FUNCTION__);
762 dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, urb->status);
763 port->write_urb->transfer_buffer_length = 1;
764 port->write_urb->dev = port->serial->dev;
765 result = usb_submit_urb (port->write_urb, GFP_ATOMIC);
766 if (result)
767 dev_err(&urb->dev->dev, "%s - failed resubmitting write urb, error %d\n", __FUNCTION__, result);
769 return;
772 schedule_work(&port->work);
776 static int __init pl2303_init (void)
778 usb_serial_register (&pl2303_device);
779 usb_register (&pl2303_driver);
780 info(DRIVER_DESC " " DRIVER_VERSION);
781 return 0;
785 static void __exit pl2303_exit (void)
787 usb_deregister (&pl2303_driver);
788 usb_serial_deregister (&pl2303_device);
792 module_init(pl2303_init);
793 module_exit(pl2303_exit);
795 MODULE_DESCRIPTION(DRIVER_DESC);
796 MODULE_LICENSE("GPL");
798 MODULE_PARM(debug, "i");
799 MODULE_PARM_DESC(debug, "Debug enabled or not");