18 #include <linux/kernel.h>
19 #include <linux/errno.h>
21 #include <linux/slab.h>
22 #include <linux/tty.h>
25 #include <linux/serial.h>
26 #include <linux/module.h>
37 #define DRIVER_DESC "Prolific PL2303 USB to serial adaptor driver"
96 #define SET_LINE_REQUEST_TYPE 0x21
97 #define SET_LINE_REQUEST 0x20
99 #define SET_CONTROL_REQUEST_TYPE 0x21
100 #define SET_CONTROL_REQUEST 0x22
101 #define CONTROL_DTR 0x01
102 #define CONTROL_RTS 0x02
104 #define BREAK_REQUEST_TYPE 0x21
105 #define BREAK_REQUEST 0x23
106 #define BREAK_ON 0xffff
107 #define BREAK_OFF 0x0000
109 #define GET_LINE_REQUEST_TYPE 0xa1
110 #define GET_LINE_REQUEST 0x21
112 #define VENDOR_WRITE_REQUEST_TYPE 0x40
113 #define VENDOR_WRITE_REQUEST 0x01
115 #define VENDOR_READ_REQUEST_TYPE 0xc0
116 #define VENDOR_READ_REQUEST 0x01
118 #define UART_STATE 0x08
119 #define UART_STATE_TRANSIENT_MASK 0x74
120 #define UART_DCD 0x01
121 #define UART_DSR 0x02
122 #define UART_BREAK_ERROR 0x04
123 #define UART_RING 0x08
124 #define UART_FRAME_ERROR 0x10
125 #define UART_PARITY_ERROR 0x20
126 #define UART_OVERRUN_ERROR 0x40
127 #define UART_CTS 0x80
152 value, index, buf, 1, 100);
153 dev_dbg(&serial->
dev->dev,
"0x%x:0x%x:0x%x:0x%x %d - %x\n",
164 value, index,
NULL, 0, 100);
165 dev_dbg(&serial->
dev->dev,
"0x%x:0x%x:0x%x:0x%x %d\n",
171 static int pl2303_startup(
struct usb_serial *serial)
187 if (serial->
dev->descriptor.bDeviceClass == 0x02)
189 else if (serial->
dev->descriptor.bMaxPacketSize0 == 0x40)
191 else if (serial->
dev->descriptor.bDeviceClass == 0x00)
193 else if (serial->
dev->descriptor.bDeviceClass == 0xFF)
198 usb_set_serial_data(serial, spriv);
200 pl2303_vendor_read(0x8484, 0, serial, buf);
201 pl2303_vendor_write(0x0404, 0, serial);
202 pl2303_vendor_read(0x8484, 0, serial, buf);
203 pl2303_vendor_read(0x8383, 0, serial, buf);
204 pl2303_vendor_read(0x8484, 0, serial, buf);
205 pl2303_vendor_write(0x0404, 1, serial);
206 pl2303_vendor_read(0x8484, 0, serial, buf);
207 pl2303_vendor_read(0x8383, 0, serial, buf);
208 pl2303_vendor_write(0, 1, serial);
209 pl2303_vendor_write(1, 0, serial);
211 pl2303_vendor_write(2, 0x44, serial);
213 pl2303_vendor_write(2, 0x24, serial);
219 static void pl2303_release(
struct usb_serial *serial)
223 spriv = usb_get_serial_data(serial);
238 usb_set_serial_port_data(port, priv);
247 priv = usb_get_serial_port_data(port);
253 static int set_control_lines(
struct usb_device *
dev,
u8 value)
259 value, 0,
NULL, 0, 100);
260 dev_dbg(&dev->dev,
"%s - value = %d, retval = %d\n", __func__,
265 static void pl2303_set_termios(
struct tty_struct *tty,
277 const int baud_sup[] = { 75, 150, 300, 600, 1200, 1800, 2400, 3600,
278 4800, 7200, 9600, 14400, 19200, 28800, 38400,
279 57600, 115200, 230400, 460800, 614400,
280 921600, 1228800, 2457600, 3000000, 6000000 };
281 int baud_floor, baud_ceil;
291 cflag = tty->termios.c_cflag;
295 dev_err(&port->
dev,
"%s - out of memory.\n", __func__);
297 tty->termios = *old_termios;
304 dev_dbg(&port->
dev,
"0xa1:0x21:0:0 %d - %x %x %x %x %x %x %x\n", i,
305 buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6]);
308 switch (cflag & CSIZE) {
323 dev_dbg(&port->
dev,
"data bits = %d\n", buf[6]);
332 dev_dbg(&port->
dev,
"baud requested = %d\n", baud);
336 if (baud_sup[k] / baud) {
337 baud_ceil = baud_sup[
k];
341 baud_floor = baud_sup[k-1];
342 if ((baud_ceil % baud)
343 > (baud % baud_floor))
351 if (baud > 1228800) {
355 else if (baud > 6000000)
359 if (baud <= 115200) {
360 buf[0] = baud & 0xff;
361 buf[1] = (baud >> 8) & 0xff;
362 buf[2] = (baud >> 16) & 0xff;
363 buf[3] = (baud >> 24) & 0xff;
368 unsigned tmp = 12*1000*1000*32 /
baud;
371 buf[1] = (tmp >= 256);
387 if ((cflag & CSIZE) ==
CS5) {
437 else if ((old_termios->
c_cflag & CBAUD) ==
B0)
441 spin_unlock_irqrestore(&priv->
lock, flags);
442 set_control_lines(serial->
dev, control);
444 spin_unlock_irqrestore(&priv->
lock, flags);
447 buf[0] = buf[1] = buf[2] = buf[3] = buf[4] = buf[5] = buf[6] = 0;
452 dev_dbg(&port->
dev,
"0xa1:0x21:0:0 %d - %x %x %x %x %x %x %x\n", i,
453 buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6]);
457 pl2303_vendor_write(0x0, 0x61, serial);
459 pl2303_vendor_write(0x0, 0x41, serial);
461 pl2303_vendor_write(0x0, 0x0, serial);
484 spin_unlock_irqrestore(&priv->
lock, flags);
485 set_control_lines(port->
serial->dev, control);
506 pl2303_vendor_write(8, 0, serial);
507 pl2303_vendor_write(9, 0, serial);
512 pl2303_set_termios(tty, port, &tmp_termios);
516 dev_err(&port->
dev,
"%s - failed submitting interrupt urb,"
517 " error %d\n", __func__, result);
527 port->
port.drain_delay = 256;
531 static int pl2303_tiocmset(
struct tty_struct *tty,
532 unsigned int set,
unsigned int clear)
551 spin_unlock_irqrestore(&priv->
lock, flags);
555 ret = set_control_lines(serial->
dev, control);
563 static int pl2303_tiocmget(
struct tty_struct *tty)
575 spin_unlock_irqrestore(&priv->
lock, flags);
584 dev_dbg(&port->
dev,
"%s - result = %x\n", __func__, result);
601 unsigned int prevstatus;
607 spin_unlock_irqrestore(&priv->
lock, flags);
617 spin_unlock_irqrestore(&priv->
lock, flags);
619 changed = prevstatus ^
status;
633 static int pl2303_ioctl(
struct tty_struct *tty,
634 unsigned int cmd,
unsigned long arg)
639 dev_dbg(&port->
dev,
"%s cmd = 0x%04x\n", __func__, cmd);
647 ser.baud_base = 460800;
655 dev_dbg(&port->
dev,
"%s TIOCMIWAIT\n", __func__);
656 return wait_modem_info(port, arg);
658 dev_dbg(&port->
dev,
"%s not supported = 0x%04x\n", __func__, cmd);
664 static void pl2303_break_ctl(
struct tty_struct *tty,
int break_state)
671 if (break_state == 0)
675 dev_dbg(&port->
dev,
"%s - turning break %s\n", __func__,
682 dev_err(&port->
dev,
"error sending break = %d\n", result);
687 unsigned int actual_length)
712 if (actual_length < length)
719 spin_unlock_irqrestore(&priv->
lock, flags);
727 if ((priv->
line_status ^ prev_line_status) & UART_DCD)
733 static void pl2303_read_int_callback(
struct urb *
urb)
736 unsigned char *data = urb->transfer_buffer;
737 unsigned int actual_length = urb->actual_length;
738 int status = urb->status;
749 dev_dbg(&port->
dev,
"%s - urb shutting down with status: %d\n",
753 dev_dbg(&port->
dev,
"%s - nonzero urb status received: %d\n",
758 usb_serial_debug_data(&port->
dev, __func__,
759 urb->actual_length, urb->transfer_buffer);
761 pl2303_update_line_status(port, data, actual_length);
767 "%s - usb_submit_urb failed with result %d\n",
771 static void pl2303_process_read_urb(
struct urb *urb)
776 unsigned char *data = urb->transfer_buffer;
786 spin_unlock_irqrestore(&priv->
lock, flags);
789 if (!urb->actual_length)
804 dev_dbg(&port->
dev,
"%s - tty_flag = %d\n", __func__, tty_flag);
811 for (i = 0; i < urb->actual_length; ++
i)
813 tty_insert_flip_char(tty, data[i], tty_flag);
832 .bulk_out_size = 256,
834 .close = pl2303_close,
835 .dtr_rts = pl2303_dtr_rts,
836 .carrier_raised = pl2303_carrier_raised,
837 .ioctl = pl2303_ioctl,
838 .break_ctl = pl2303_break_ctl,
839 .set_termios = pl2303_set_termios,
840 .tiocmget = pl2303_tiocmget,
841 .tiocmset = pl2303_tiocmset,
842 .process_read_urb = pl2303_process_read_urb,
843 .read_int_callback = pl2303_read_int_callback,
844 .attach = pl2303_startup,
845 .release = pl2303_release,
846 .port_probe = pl2303_port_probe,
847 .port_remove = pl2303_port_remove,