2 * Prolific PL2303 USB to serial adaptor driver
4 * Copyright (C) 2001-2007 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
10 * modify it under the terms of the GNU General Public License version
11 * 2 as published by the Free Software Foundation.
13 * See Documentation/usb/usb-serial.txt for more information on using this driver
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>
36 #define DRIVER_DESC "Prolific PL2303 USB to serial adaptor driver"
40 #define PL2303_CLOSING_WAIT (30*HZ)
42 #define PL2303_BUF_SIZE 1024
43 #define PL2303_TMP_BUF_SIZE 1024
46 unsigned int buf_size;
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 */
93 MODULE_DEVICE_TABLE(usb, id_table);
95 static struct usb_driver pl2303_driver = {
97 .probe = usb_serial_probe,
98 .disconnect = usb_serial_disconnect,
103 #define SET_LINE_REQUEST_TYPE 0x21
104 #define SET_LINE_REQUEST 0x20
106 #define SET_CONTROL_REQUEST_TYPE 0x21
107 #define SET_CONTROL_REQUEST 0x22
108 #define CONTROL_DTR 0x01
109 #define CONTROL_RTS 0x02
111 #define BREAK_REQUEST_TYPE 0x21
112 #define BREAK_REQUEST 0x23
113 #define BREAK_ON 0xffff
114 #define BREAK_OFF 0x0000
116 #define GET_LINE_REQUEST_TYPE 0xa1
117 #define GET_LINE_REQUEST 0x21
119 #define VENDOR_WRITE_REQUEST_TYPE 0x40
120 #define VENDOR_WRITE_REQUEST 0x01
122 #define VENDOR_READ_REQUEST_TYPE 0xc0
123 #define VENDOR_READ_REQUEST 0x01
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
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 */
143 struct pl2303_private {
145 struct pl2303_buf *buf;
146 int write_urb_in_use;
147 wait_queue_head_t delta_msr_wait;
150 u8 termios_initialized;
151 enum pl2303_type type;
157 * Allocate a circular buffer and all associated memory.
159 static struct pl2303_buf *pl2303_buf_alloc(unsigned int size)
161 struct pl2303_buf *pb;
166 pb = kmalloc(sizeof(struct pl2303_buf), GFP_KERNEL);
170 pb->buf_buf = kmalloc(size, GFP_KERNEL);
171 if (pb->buf_buf == NULL) {
177 pb->buf_get = pb->buf_put = pb->buf_buf;
185 * Free the buffer and all associated memory.
187 static void pl2303_buf_free(struct pl2303_buf *pb)
198 * Clear out all data in the circular buffer.
200 static void pl2303_buf_clear(struct pl2303_buf *pb)
203 pb->buf_get = pb->buf_put;
204 /* equivalent to a get of all data available */
208 * pl2303_buf_data_avail
210 * Return the number of bytes of data available in the circular
213 static unsigned int pl2303_buf_data_avail(struct pl2303_buf *pb)
218 return ((pb->buf_size + pb->buf_put - pb->buf_get) % pb->buf_size);
222 * pl2303_buf_space_avail
224 * Return the number of bytes of space available in the circular
227 static unsigned int pl2303_buf_space_avail(struct pl2303_buf *pb)
232 return ((pb->buf_size + pb->buf_get - pb->buf_put - 1) % pb->buf_size);
238 * Copy data data from a user buffer and put it into the circular buffer.
239 * Restrict to the amount of space available.
241 * Return the number of bytes copied.
243 static unsigned int pl2303_buf_put(struct pl2303_buf *pb, const char *buf,
251 len = pl2303_buf_space_avail(pb);
258 len = pb->buf_buf + pb->buf_size - pb->buf_put;
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;
264 memcpy(pb->buf_put, buf, count);
266 pb->buf_put += count;
267 else /* count == len */
268 pb->buf_put = pb->buf_buf;
277 * Get data from the circular buffer and copy to the given buffer.
278 * Restrict to the amount of data available.
280 * Return the number of bytes copied.
282 static unsigned int pl2303_buf_get(struct pl2303_buf *pb, char *buf,
290 len = pl2303_buf_data_avail(pb);
297 len = pb->buf_buf + pb->buf_size - pb->buf_get;
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;
303 memcpy(buf, pb->buf_get, count);
305 pb->buf_get += count;
306 else /* count == len */
307 pb->buf_get = pb->buf_buf;
313 static int pl2303_startup(struct usb_serial *serial)
315 struct pl2303_private *priv;
316 enum pl2303_type type = type_0;
319 if (serial->dev->descriptor.bDeviceClass == 0x02)
321 else if (serial->dev->descriptor.bMaxPacketSize0 == 0x40)
323 else if (serial->dev->descriptor.bDeviceClass == 0x00)
325 else if (serial->dev->descriptor.bDeviceClass == 0xFF)
327 dbg("device type: %d", type);
329 for (i = 0; i < serial->num_ports; ++i) {
330 priv = kzalloc(sizeof(struct pl2303_private), GFP_KERNEL);
333 spin_lock_init(&priv->lock);
334 priv->buf = pl2303_buf_alloc(PL2303_BUF_SIZE);
335 if (priv->buf == NULL) {
339 init_waitqueue_head(&priv->delta_msr_wait);
341 usb_set_serial_port_data(serial->port[i], priv);
346 for (--i; i>=0; --i) {
347 priv = usb_get_serial_port_data(serial->port[i]);
348 pl2303_buf_free(priv->buf);
350 usb_set_serial_port_data(serial->port[i], NULL);
355 static int set_control_lines(struct usb_device *dev, u8 value)
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);
366 static void pl2303_send(struct usb_serial_port *port)
369 struct pl2303_private *priv = usb_get_serial_port_data(port);
372 dbg("%s - port %d", __FUNCTION__, port->number);
374 spin_lock_irqsave(&priv->lock, flags);
376 if (priv->write_urb_in_use) {
377 spin_unlock_irqrestore(&priv->lock, flags);
381 count = pl2303_buf_get(priv->buf, port->write_urb->transfer_buffer,
382 port->bulk_out_size);
385 spin_unlock_irqrestore(&priv->lock, flags);
389 priv->write_urb_in_use = 1;
391 spin_unlock_irqrestore(&priv->lock, flags);
393 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count,
394 port->write_urb->transfer_buffer);
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);
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
406 usb_serial_port_softint(port);
409 static int pl2303_write(struct usb_serial_port *port, const unsigned char *buf,
412 struct pl2303_private *priv = usb_get_serial_port_data(port);
415 dbg("%s - port %d, %d bytes", __FUNCTION__, port->number, count);
420 spin_lock_irqsave(&priv->lock, flags);
421 count = pl2303_buf_put(priv->buf, buf, count);
422 spin_unlock_irqrestore(&priv->lock, flags);
429 static int pl2303_write_room(struct usb_serial_port *port)
431 struct pl2303_private *priv = usb_get_serial_port_data(port);
435 dbg("%s - port %d", __FUNCTION__, port->number);
437 spin_lock_irqsave(&priv->lock, flags);
438 room = pl2303_buf_space_avail(priv->buf);
439 spin_unlock_irqrestore(&priv->lock, flags);
441 dbg("%s - returns %d", __FUNCTION__, room);
445 static int pl2303_chars_in_buffer(struct usb_serial_port *port)
447 struct pl2303_private *priv = usb_get_serial_port_data(port);
451 dbg("%s - port %d", __FUNCTION__, port->number);
453 spin_lock_irqsave(&priv->lock, flags);
454 chars = pl2303_buf_data_avail(priv->buf);
455 spin_unlock_irqrestore(&priv->lock, flags);
457 dbg("%s - returns %d", __FUNCTION__, chars);
461 static void pl2303_set_termios(struct usb_serial_port *port,
462 struct ktermios *old_termios)
464 struct usb_serial *serial = port->serial;
465 struct pl2303_private *priv = usb_get_serial_port_data(port);
473 dbg("%s - port %d", __FUNCTION__, port->number);
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 |
480 port->tty->termios->c_ispeed = 9600;
481 port->tty->termios->c_ospeed = 9600;
482 priv->termios_initialized = 1;
484 spin_unlock_irqrestore(&priv->lock, flags);
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 */
490 if (!tty_termios_hw_change(port->tty->termios, old_termios))
493 cflag = port->tty->termios->c_cflag;
495 buf = kzalloc(7, GFP_KERNEL);
497 dev_err(&port->dev, "%s - out of memory.\n", __FUNCTION__);
501 i = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
502 GET_LINE_REQUEST, GET_LINE_REQUEST_TYPE,
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]);
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;
513 case CS8: buf[6] = 8; break;
515 dbg("%s - data bits = %d", __FUNCTION__, buf[6]);
518 baud = tty_get_baud_rate(port->tty);;
519 dbg("%s - baud = %d", __FUNCTION__, baud);
521 buf[0] = baud & 0xff;
522 buf[1] = (baud >> 8) & 0xff;
523 buf[2] = (baud >> 16) & 0xff;
524 buf[3] = (baud >> 24) & 0xff;
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) {
532 dbg("%s - stop bits = 2", __FUNCTION__);
535 dbg("%s - stop bits = 1", __FUNCTION__);
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) {
546 dbg("%s - parity = odd", __FUNCTION__);
549 dbg("%s - parity = even", __FUNCTION__);
553 dbg("%s - parity = none", __FUNCTION__);
556 i = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
557 SET_LINE_REQUEST, SET_LINE_REQUEST_TYPE,
559 dbg("0x21:0x20:0:0 %d", i);
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);
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);
573 spin_unlock_irqrestore(&priv->lock, flags);
576 buf[0] = buf[1] = buf[2] = buf[3] = buf[4] = buf[5] = buf[6] = 0;
578 i = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
579 GET_LINE_REQUEST, GET_LINE_REQUEST_TYPE,
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]);
584 if (cflag & CRTSCTS) {
586 if (priv->type == HX)
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);
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);
605 /* FIXME: Need to read back resulting baud rate */
607 tty_encode_baud_rate(port->tty, baud, baud);
612 static void pl2303_close(struct usb_serial_port *port, struct file *filp)
614 struct pl2303_private *priv = usb_get_serial_port_data(port);
616 unsigned int c_cflag;
621 dbg("%s - port %d", __FUNCTION__, port->number);
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);
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 */
634 spin_unlock_irqrestore(&priv->lock, flags);
635 timeout = schedule_timeout(timeout);
636 spin_lock_irqsave(&priv->lock, flags);
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);
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);
653 timeout = max((HZ*2560)/bps,HZ/10);
656 schedule_timeout_interruptible(timeout);
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);
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);
676 static int pl2303_open(struct usb_serial_port *port, struct file *filp)
678 struct ktermios tmp_termios;
679 struct usb_serial *serial = port->serial;
680 struct pl2303_private *priv = usb_get_serial_port_data(port);
684 dbg("%s - port %d", __FUNCTION__, port->number);
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);
691 buf = kmalloc(10, GFP_KERNEL);
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]);
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);
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);
716 if (priv->type == HX) {
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);
723 SOUP (VENDOR_WRITE_REQUEST_TYPE, VENDOR_WRITE_REQUEST, 2, 0x24);
730 pl2303_set_termios(port, &tmp_termios);
733 //FIXME: need to assert RTS and DTR if CRTSCTS off
735 dbg("%s - submitting read urb", __FUNCTION__);
736 port->read_urb->dev = serial->dev;
737 result = usb_submit_urb(port->read_urb, GFP_KERNEL);
739 dev_err(&port->dev, "%s - failed submitting read urb,"
740 " error %d\n", __FUNCTION__, result);
741 pl2303_close(port, NULL);
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);
749 dev_err(&port->dev, "%s - failed submitting interrupt urb,"
750 " error %d\n", __FUNCTION__, result);
751 pl2303_close(port, NULL);
757 static int pl2303_tiocmset(struct usb_serial_port *port, struct file *file,
758 unsigned int set, unsigned int clear)
760 struct pl2303_private *priv = usb_get_serial_port_data(port);
764 if (!usb_get_intfdata(port->serial->interface))
767 spin_lock_irqsave(&priv->lock, flags);
769 priv->line_control |= CONTROL_RTS;
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);
779 return set_control_lines(port->serial->dev, control);
782 static int pl2303_tiocmget(struct usb_serial_port *port, struct file *file)
784 struct pl2303_private *priv = usb_get_serial_port_data(port);
790 dbg("%s (%d)", __FUNCTION__, port->number);
792 if (!usb_get_intfdata(port->serial->interface))
795 spin_lock_irqsave(&priv->lock, flags);
796 mcr = priv->line_control;
797 status = priv->line_status;
798 spin_unlock_irqrestore(&priv->lock, flags);
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);
807 dbg("%s - result = %x", __FUNCTION__, result);
812 static int wait_modem_info(struct usb_serial_port *port, unsigned int arg)
814 struct pl2303_private *priv = usb_get_serial_port_data(port);
816 unsigned int prevstatus;
818 unsigned int changed;
820 spin_lock_irqsave(&priv->lock, flags);
821 prevstatus = priv->line_status;
822 spin_unlock_irqrestore(&priv->lock, flags);
825 interruptible_sleep_on(&priv->delta_msr_wait);
826 /* see if a signal did it */
827 if (signal_pending(current))
830 spin_lock_irqsave(&priv->lock, flags);
831 status = priv->line_status;
832 spin_unlock_irqrestore(&priv->lock, flags);
834 changed=prevstatus^status;
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)) ) {
848 static int pl2303_ioctl(struct usb_serial_port *port, struct file *file,
849 unsigned int cmd, unsigned long arg)
851 dbg("%s (%d) cmd = 0x%04x", __FUNCTION__, port->number, cmd);
855 dbg("%s (%d) TIOCMIWAIT", __FUNCTION__, port->number);
856 return wait_modem_info(port, arg);
859 dbg("%s not supported = 0x%04x", __FUNCTION__, cmd);
866 static void pl2303_break_ctl(struct usb_serial_port *port, int break_state)
868 struct usb_serial *serial = port->serial;
872 dbg("%s - port %d", __FUNCTION__, port->number);
874 if (break_state == 0)
878 dbg("%s - turning break %s", __FUNCTION__, state==BREAK_OFF ? "off" : "on");
880 result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
881 BREAK_REQUEST, BREAK_REQUEST_TYPE, state,
884 dbg("%s - error sending break = %d", __FUNCTION__, result);
887 static void pl2303_shutdown(struct usb_serial *serial)
890 struct pl2303_private *priv;
892 dbg("%s", __FUNCTION__);
894 for (i = 0; i < serial->num_ports; ++i) {
895 priv = usb_get_serial_port_data(serial->port[i]);
897 pl2303_buf_free(priv->buf);
899 usb_set_serial_port_data(serial->port[i], NULL);
904 static void pl2303_update_line_status(struct usb_serial_port *port,
906 unsigned int actual_length)
909 struct pl2303_private *priv = usb_get_serial_port_data(port);
911 u8 status_idx = UART_STATE;
912 u8 length = UART_STATE + 1;
915 idv = le16_to_cpu(port->serial->dev->descriptor.idVendor);
916 idp = le16_to_cpu(port->serial->dev->descriptor.idProduct);
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) {
929 if (actual_length < length)
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);
939 static void pl2303_read_int_callback(struct urb *urb)
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;
947 dbg("%s (%d)", __FUNCTION__, port->number);
956 /* this urb is terminated, clean up */
957 dbg("%s - urb shutting down with status: %d", __FUNCTION__,
961 dbg("%s - nonzero urb status received: %d", __FUNCTION__,
966 usb_serial_debug_data(debug, &port->dev, __FUNCTION__,
967 urb->actual_length, urb->transfer_buffer);
969 pl2303_update_line_status(port, data, actual_length);
972 retval = usb_submit_urb(urb, GFP_ATOMIC);
974 dev_err(&urb->dev->dev,
975 "%s - usb_submit_urb failed with result %d\n",
976 __FUNCTION__, retval);
979 static void pl2303_read_bulk_callback(struct urb *urb)
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;
988 int status = urb->status;
992 dbg("%s - port %d", __FUNCTION__, port->number);
995 dbg("%s - urb status = %d", __FUNCTION__, status);
996 if (!port->open_count) {
997 dbg("%s - port is closed, exiting.", __FUNCTION__);
1000 if (status == -EPROTO) {
1001 /* PL2303 mysteriously fails with -EPROTO reschedule
1003 dbg("%s - caught -EPROTO, resubmitting the urb",
1005 urb->dev = port->serial->dev;
1006 result = usb_submit_urb(urb, GFP_ATOMIC);
1008 dev_err(&urb->dev->dev, "%s - failed"
1009 " resubmitting read urb, error %d\n",
1010 __FUNCTION__, result);
1013 dbg("%s - unable to handle the error, exiting.", __FUNCTION__);
1017 usb_serial_debug_data(debug, &port->dev, __FUNCTION__,
1018 urb->actual_length, data);
1020 /* get tty_flag from status */
1021 tty_flag = TTY_NORMAL;
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);
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);
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);
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);
1055 dev_err(&urb->dev->dev, "%s - failed resubmitting"
1056 " read urb, error %d\n", __FUNCTION__, result);
1062 static void pl2303_write_bulk_callback(struct urb *urb)
1064 struct usb_serial_port *port = (struct usb_serial_port *) urb->context;
1065 struct pl2303_private *priv = usb_get_serial_port_data(port);
1067 int status = urb->status;
1069 dbg("%s - port %d", __FUNCTION__, port->number);
1078 /* this urb is terminated, clean up */
1079 dbg("%s - urb shutting down with status: %d", __FUNCTION__,
1081 priv->write_urb_in_use = 0;
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__,
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);
1092 dev_err(&urb->dev->dev, "%s - failed resubmitting write"
1093 " urb, error %d\n", __FUNCTION__, result);
1098 priv->write_urb_in_use = 0;
1100 /* send any buffered data */
1104 /* All of the device info needed for the PL2303 SIO serial converter */
1105 static struct usb_serial_driver pl2303_device = {
1107 .owner = THIS_MODULE,
1110 .id_table = id_table,
1111 .usb_driver = &pl2303_driver,
1112 .num_interrupt_in = NUM_DONT_CARE,
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,
1133 static int __init pl2303_init(void)
1137 retval = usb_serial_register(&pl2303_device);
1139 goto failed_usb_serial_register;
1140 retval = usb_register(&pl2303_driver);
1142 goto failed_usb_register;
1145 failed_usb_register:
1146 usb_serial_deregister(&pl2303_device);
1147 failed_usb_serial_register:
1151 static void __exit pl2303_exit(void)
1153 usb_deregister(&pl2303_driver);
1154 usb_serial_deregister(&pl2303_device);
1157 module_init(pl2303_init);
1158 module_exit(pl2303_exit);
1160 MODULE_DESCRIPTION(DRIVER_DESC);
1161 MODULE_LICENSE("GPL");
1163 module_param(debug, bool, S_IRUGO | S_IWUSR);
1164 MODULE_PARM_DESC(debug, "Debug enabled or not");