16 #include <linux/kernel.h>
17 #include <linux/errno.h>
18 #include <linux/slab.h>
19 #include <linux/tty.h>
21 #include <linux/module.h>
30 #define DRIVER_VERSION "v0.09"
31 #define DRIVER_DESC "Silicon Labs CP210x RS232 serial adaptor driver"
38 static void cp210x_get_termios(
struct tty_struct *,
41 unsigned int *cflagp,
unsigned int *baudp);
46 static int cp210x_tiocmget(
struct tty_struct *);
47 static int cp210x_tiocmset(
struct tty_struct *,
unsigned int,
unsigned int);
49 unsigned int,
unsigned int);
50 static void cp210x_break_ctl(
struct tty_struct *,
int);
51 static int cp210x_startup(
struct usb_serial *);
52 static void cp210x_release(
struct usb_serial *);
56 { USB_DEVICE(0x045B, 0x0053) },
57 { USB_DEVICE(0x0471, 0x066A) },
58 { USB_DEVICE(0x0489, 0xE000) },
59 { USB_DEVICE(0x0489, 0xE003) },
60 { USB_DEVICE(0x0745, 0x1000) },
61 { USB_DEVICE(0x08e6, 0x5501) },
62 { USB_DEVICE(0x08FD, 0x000A) },
63 { USB_DEVICE(0x0BED, 0x1100) },
64 { USB_DEVICE(0x0BED, 0x1101) },
65 { USB_DEVICE(0x0FCF, 0x1003) },
66 { USB_DEVICE(0x0FCF, 0x1004) },
67 { USB_DEVICE(0x0FCF, 0x1006) },
68 { USB_DEVICE(0x10A6, 0xAA26) },
69 { USB_DEVICE(0x10AB, 0x10C5) },
70 { USB_DEVICE(0x10B5, 0xAC70) },
71 { USB_DEVICE(0x10C4, 0x0F91) },
72 { USB_DEVICE(0x10C4, 0x1101) },
73 { USB_DEVICE(0x10C4, 0x1601) },
74 { USB_DEVICE(0x10C4, 0x800A) },
75 { USB_DEVICE(0x10C4, 0x803B) },
76 { USB_DEVICE(0x10C4, 0x8044) },
77 { USB_DEVICE(0x10C4, 0x804E) },
78 { USB_DEVICE(0x10C4, 0x8053) },
79 { USB_DEVICE(0x10C4, 0x8054) },
80 { USB_DEVICE(0x10C4, 0x8066) },
81 { USB_DEVICE(0x10C4, 0x806F) },
82 { USB_DEVICE(0x10C4, 0x807A) },
83 { USB_DEVICE(0x10C4, 0x80C4) },
84 { USB_DEVICE(0x10C4, 0x80CA) },
85 { USB_DEVICE(0x10C4, 0x80DD) },
86 { USB_DEVICE(0x10C4, 0x80F6) },
87 { USB_DEVICE(0x10C4, 0x8115) },
88 { USB_DEVICE(0x10C4, 0x813D) },
89 { USB_DEVICE(0x10C4, 0x813F) },
90 { USB_DEVICE(0x10C4, 0x814A) },
91 { USB_DEVICE(0x10C4, 0x814B) },
92 { USB_DEVICE(0x10C4, 0x8156) },
93 { USB_DEVICE(0x10C4, 0x815E) },
94 { USB_DEVICE(0x10C4, 0x815F) },
95 { USB_DEVICE(0x10C4, 0x818B) },
96 { USB_DEVICE(0x10C4, 0x819F) },
97 { USB_DEVICE(0x10C4, 0x81A6) },
98 { USB_DEVICE(0x10C4, 0x81A9) },
99 { USB_DEVICE(0x10C4, 0x81AC) },
100 { USB_DEVICE(0x10C4, 0x81AD) },
101 { USB_DEVICE(0x10C4, 0x81C8) },
102 { USB_DEVICE(0x10C4, 0x81E2) },
103 { USB_DEVICE(0x10C4, 0x81E7) },
104 { USB_DEVICE(0x10C4, 0x81E8) },
105 { USB_DEVICE(0x10C4, 0x81F2) },
106 { USB_DEVICE(0x10C4, 0x8218) },
107 { USB_DEVICE(0x10C4, 0x822B) },
108 { USB_DEVICE(0x10C4, 0x826B) },
109 { USB_DEVICE(0x10C4, 0x8293) },
110 { USB_DEVICE(0x10C4, 0x82F9) },
111 { USB_DEVICE(0x10C4, 0x8341) },
112 { USB_DEVICE(0x10C4, 0x8382) },
113 { USB_DEVICE(0x10C4, 0x83A8) },
114 { USB_DEVICE(0x10C4, 0x83D8) },
115 { USB_DEVICE(0x10C4, 0x8411) },
116 { USB_DEVICE(0x10C4, 0x8418) },
117 { USB_DEVICE(0x10C4, 0x846E) },
118 { USB_DEVICE(0x10C4, 0x8477) },
119 { USB_DEVICE(0x10C4, 0x85EA) },
120 { USB_DEVICE(0x10C4, 0x85EB) },
121 { USB_DEVICE(0x10C4, 0x85F8) },
122 { USB_DEVICE(0x10C4, 0x8664) },
123 { USB_DEVICE(0x10C4, 0x8665) },
124 { USB_DEVICE(0x10C4, 0xEA60) },
125 { USB_DEVICE(0x10C4, 0xEA61) },
126 { USB_DEVICE(0x10C4, 0xEA70) },
127 { USB_DEVICE(0x10C4, 0xEA80) },
128 { USB_DEVICE(0x10C4, 0xEA71) },
129 { USB_DEVICE(0x10C4, 0xF001) },
130 { USB_DEVICE(0x10C4, 0xF002) },
131 { USB_DEVICE(0x10C4, 0xF003) },
132 { USB_DEVICE(0x10C4, 0xF004) },
133 { USB_DEVICE(0x10C5, 0xEA61) },
134 { USB_DEVICE(0x10CE, 0xEA6A) },
135 { USB_DEVICE(0x13AD, 0x9999) },
136 { USB_DEVICE(0x1555, 0x0004) },
137 { USB_DEVICE(0x166A, 0x0201) },
138 { USB_DEVICE(0x166A, 0x0301) },
139 { USB_DEVICE(0x166A, 0x0303) },
140 { USB_DEVICE(0x166A, 0x0304) },
141 { USB_DEVICE(0x166A, 0x0305) },
142 { USB_DEVICE(0x166A, 0x0401) },
143 { USB_DEVICE(0x166A, 0x0101) },
144 { USB_DEVICE(0x16D6, 0x0001) },
145 { USB_DEVICE(0x16DC, 0x0010) },
146 { USB_DEVICE(0x16DC, 0x0011) },
147 { USB_DEVICE(0x16DC, 0x0012) },
148 { USB_DEVICE(0x16DC, 0x0015) },
149 { USB_DEVICE(0x17A8, 0x0001) },
150 { USB_DEVICE(0x17A8, 0x0005) },
151 { USB_DEVICE(0x17F4, 0xAAAA) },
152 { USB_DEVICE(0x1843, 0x0200) },
153 { USB_DEVICE(0x18EF, 0xE00F) },
154 { USB_DEVICE(0x1BE3, 0x07A6) },
155 { USB_DEVICE(0x1E29, 0x0102) },
156 { USB_DEVICE(0x1E29, 0x0501) },
157 { USB_DEVICE(0x3195, 0xF190) },
158 { USB_DEVICE(0x3195, 0xF280) },
159 { USB_DEVICE(0x3195, 0xF281) },
160 { USB_DEVICE(0x413C, 0x9500) },
178 .bulk_out_size = 256,
180 .close = cp210x_close,
181 .break_ctl = cp210x_break_ctl,
182 .set_termios = cp210x_set_termios,
183 .tiocmget = cp210x_tiocmget,
184 .tiocmset = cp210x_tiocmset,
185 .attach = cp210x_startup,
186 .release = cp210x_release,
187 .dtr_rts = cp210x_dtr_rts
195 #define REQTYPE_HOST_TO_INTERFACE 0x41
196 #define REQTYPE_INTERFACE_TO_HOST 0xc1
197 #define REQTYPE_HOST_TO_DEVICE 0x40
198 #define REQTYPE_DEVICE_TO_HOST 0xc0
201 #define CP210X_IFC_ENABLE 0x00
202 #define CP210X_SET_BAUDDIV 0x01
203 #define CP210X_GET_BAUDDIV 0x02
204 #define CP210X_SET_LINE_CTL 0x03
205 #define CP210X_GET_LINE_CTL 0x04
206 #define CP210X_SET_BREAK 0x05
207 #define CP210X_IMM_CHAR 0x06
208 #define CP210X_SET_MHS 0x07
209 #define CP210X_GET_MDMSTS 0x08
210 #define CP210X_SET_XON 0x09
211 #define CP210X_SET_XOFF 0x0A
212 #define CP210X_SET_EVENTMASK 0x0B
213 #define CP210X_GET_EVENTMASK 0x0C
214 #define CP210X_SET_CHAR 0x0D
215 #define CP210X_GET_CHARS 0x0E
216 #define CP210X_GET_PROPS 0x0F
217 #define CP210X_GET_COMM_STATUS 0x10
218 #define CP210X_RESET 0x11
219 #define CP210X_PURGE 0x12
220 #define CP210X_SET_FLOW 0x13
221 #define CP210X_GET_FLOW 0x14
222 #define CP210X_EMBED_EVENTS 0x15
223 #define CP210X_GET_EVENTSTATE 0x16
224 #define CP210X_SET_CHARS 0x19
225 #define CP210X_GET_BAUDRATE 0x1D
226 #define CP210X_SET_BAUDRATE 0x1E
229 #define UART_ENABLE 0x0001
230 #define UART_DISABLE 0x0000
233 #define BAUD_RATE_GEN_FREQ 0x384000
236 #define BITS_DATA_MASK 0X0f00
237 #define BITS_DATA_5 0X0500
238 #define BITS_DATA_6 0X0600
239 #define BITS_DATA_7 0X0700
240 #define BITS_DATA_8 0X0800
241 #define BITS_DATA_9 0X0900
243 #define BITS_PARITY_MASK 0x00f0
244 #define BITS_PARITY_NONE 0x0000
245 #define BITS_PARITY_ODD 0x0010
246 #define BITS_PARITY_EVEN 0x0020
247 #define BITS_PARITY_MARK 0x0030
248 #define BITS_PARITY_SPACE 0x0040
250 #define BITS_STOP_MASK 0x000f
251 #define BITS_STOP_1 0x0000
252 #define BITS_STOP_1_5 0x0001
253 #define BITS_STOP_2 0x0002
256 #define BREAK_ON 0x0001
257 #define BREAK_OFF 0x0000
260 #define CONTROL_DTR 0x0001
261 #define CONTROL_RTS 0x0002
262 #define CONTROL_CTS 0x0010
263 #define CONTROL_DSR 0x0020
264 #define CONTROL_RING 0x0040
265 #define CONTROL_DCD 0x0080
266 #define CONTROL_WRITE_DTR 0x0100
267 #define CONTROL_WRITE_RTS 0x0200
285 length = (((size - 1) | 3) + 1)/4;
289 dev_err(&port->
dev,
"%s - out of memory.\n", __func__);
297 USB_CTRL_GET_TIMEOUT);
300 for (i = 0; i <
length; i++)
305 if (result != size) {
306 dev_dbg(&port->
dev,
"%s - Unable to send config request, request=0x%x size=%d result=%d\n",
307 __func__, request, size, result);
332 length = (((size - 1) | 3) + 1)/4;
342 for (i = 0; i <
length; i++)
347 usb_sndctrlpipe(serial->
dev, 0),
350 USB_CTRL_SET_TIMEOUT);
353 usb_sndctrlpipe(serial->
dev, 0),
356 USB_CTRL_SET_TIMEOUT);
361 if ((size > 2 && result != size) || result < 0) {
362 dev_dbg(&port->
dev,
"%s - Unable to send request, request=0x%x size=%d result=%d\n",
363 __func__, request, size, result);
378 static inline int cp210x_set_config_single(
struct usb_serial_port *port,
379 u8 request,
unsigned int data)
381 return cp210x_set_config(port, request, &data, 2);
388 static unsigned int cp210x_quantise_baudrate(
unsigned int baud) {
391 else if (baud <= 600) baud = 600;
392 else if (baud <= 1200) baud = 1200;
393 else if (baud <= 1800) baud = 1800;
394 else if (baud <= 2400) baud = 2400;
395 else if (baud <= 4000) baud = 4000;
396 else if (baud <= 4803) baud = 4800;
397 else if (baud <= 7207) baud = 7200;
398 else if (baud <= 9612) baud = 9600;
399 else if (baud <= 14428) baud = 14400;
400 else if (baud <= 16062) baud = 16000;
401 else if (baud <= 19250) baud = 19200;
402 else if (baud <= 28912) baud = 28800;
403 else if (baud <= 38601) baud = 38400;
404 else if (baud <= 51558) baud = 51200;
405 else if (baud <= 56280) baud = 56000;
406 else if (baud <= 58053) baud = 57600;
407 else if (baud <= 64111) baud = 64000;
408 else if (baud <= 77608) baud = 76800;
409 else if (baud <= 117028) baud = 115200;
410 else if (baud <= 129347) baud = 128000;
411 else if (baud <= 156868) baud = 153600;
412 else if (baud <= 237832) baud = 230400;
413 else if (baud <= 254234) baud = 250000;
414 else if (baud <= 273066) baud = 256000;
415 else if (baud <= 491520) baud = 460800;
416 else if (baud <= 567138) baud = 500000;
417 else if (baud <= 670254) baud = 576000;
418 else if (baud < 1000000)
420 else if (baud > 2000000)
432 dev_err(&port->
dev,
"%s - Unable to enable UART\n", __func__);
437 cp210x_get_termios(tty, port);
441 cp210x_change_speed(tty, port,
NULL);
451 if (!port->
serial->disconnected)
462 static void cp210x_get_termios(
struct tty_struct *tty,
469 &tty->termios.c_cflag, &baud);
476 cp210x_get_termios_port(port, &cflag, &baud);
485 unsigned int *cflagp,
unsigned int *baudp)
488 unsigned int cflag, modem_ctl[4];
494 dev_dbg(dev,
"%s - baud rate = %d\n", __func__, baud);
503 dev_dbg(dev,
"%s - data bits = 5\n", __func__);
507 dev_dbg(dev,
"%s - data bits = 6\n", __func__);
511 dev_dbg(dev,
"%s - data bits = 7\n", __func__);
515 dev_dbg(dev,
"%s - data bits = 8\n", __func__);
519 dev_dbg(dev,
"%s - data bits = 9 (not supported, using 8 data bits)\n", __func__);
521 bits &= ~BITS_DATA_MASK;
526 dev_dbg(dev,
"%s - Unknown number of data bits, using 8\n", __func__);
528 bits &= ~BITS_DATA_MASK;
536 dev_dbg(dev,
"%s - parity = NONE\n", __func__);
540 dev_dbg(dev,
"%s - parity = ODD\n", __func__);
544 dev_dbg(dev,
"%s - parity = EVEN\n", __func__);
549 dev_dbg(dev,
"%s - parity = MARK\n", __func__);
553 dev_dbg(dev,
"%s - parity = SPACE\n", __func__);
558 dev_dbg(dev,
"%s - Unknown parity mode, disabling parity\n", __func__);
560 bits &= ~BITS_PARITY_MASK;
568 dev_dbg(dev,
"%s - stop bits = 1\n", __func__);
571 dev_dbg(dev,
"%s - stop bits = 1.5 (not supported, using 1 stop bit)\n", __func__);
572 bits &= ~BITS_STOP_MASK;
576 dev_dbg(dev,
"%s - stop bits = 2\n", __func__);
580 dev_dbg(dev,
"%s - Unknown number of stop bits, using 1 stop bit\n", __func__);
581 bits &= ~BITS_STOP_MASK;
587 if (modem_ctl[0] & 0x0008) {
588 dev_dbg(dev,
"%s - flow control = CRTSCTS\n", __func__);
591 dev_dbg(dev,
"%s - flow control = NONE\n", __func__);
624 static void cp210x_change_speed(
struct tty_struct *tty,
629 baud = tty->termios.c_ospeed;
636 baud = cp210x_quantise_baudrate(baud);
638 dev_dbg(&port->
dev,
"%s - setting baud rate to %u\n", __func__, baud);
641 dev_warn(&port->
dev,
"failed to set baud rate to %u\n", baud);
651 static void cp210x_set_termios(
struct tty_struct *tty,
655 unsigned int cflag, old_cflag;
657 unsigned int modem_ctl[4];
664 cflag = tty->termios.c_cflag;
665 old_cflag = old_termios->
c_cflag;
667 if (tty->termios.c_ospeed != old_termios->
c_ospeed)
668 cp210x_change_speed(tty, port, old_termios);
671 if ((cflag &
CSIZE) != (old_cflag & CSIZE)) {
673 bits &= ~BITS_DATA_MASK;
674 switch (cflag & CSIZE) {
677 dev_dbg(dev,
"%s - data bits = 5\n", __func__);
681 dev_dbg(dev,
"%s - data bits = 6\n", __func__);
685 dev_dbg(dev,
"%s - data bits = 7\n", __func__);
689 dev_dbg(dev,
"%s - data bits = 8\n", __func__);
696 dev_dbg(dev,
"cp210x driver does not support the number of bits requested, using 8 bit mode\n");
701 dev_dbg(dev,
"Number of data bits requested not supported by device\n");
707 bits &= ~BITS_PARITY_MASK;
712 dev_dbg(dev,
"%s - parity = MARK\n", __func__);
715 dev_dbg(dev,
"%s - parity = SPACE\n", __func__);
720 dev_dbg(dev,
"%s - parity = ODD\n", __func__);
723 dev_dbg(dev,
"%s - parity = EVEN\n", __func__);
728 dev_dbg(dev,
"Parity mode not supported by device\n");
731 if ((cflag &
CSTOPB) != (old_cflag & CSTOPB)) {
733 bits &= ~BITS_STOP_MASK;
734 if (cflag & CSTOPB) {
736 dev_dbg(dev,
"%s - stop bits = 2\n", __func__);
739 dev_dbg(dev,
"%s - stop bits = 1\n", __func__);
742 dev_dbg(dev,
"Number of stop bits requested not supported by device\n");
745 if ((cflag &
CRTSCTS) != (old_cflag & CRTSCTS)) {
747 dev_dbg(dev,
"%s - read modem controls = 0x%.4x 0x%.4x 0x%.4x 0x%.4x\n",
748 __func__, modem_ctl[0], modem_ctl[1],
749 modem_ctl[2], modem_ctl[3]);
751 if (cflag & CRTSCTS) {
752 modem_ctl[0] &= ~0x7B;
753 modem_ctl[0] |= 0x09;
755 dev_dbg(dev,
"%s - flow control = CRTSCTS\n", __func__);
757 modem_ctl[0] &= ~0x7B;
758 modem_ctl[0] |= 0x01;
759 modem_ctl[1] |= 0x40;
760 dev_dbg(dev,
"%s - flow control = NONE\n", __func__);
763 dev_dbg(dev,
"%s - write modem controls = 0x%.4x 0x%.4x 0x%.4x 0x%.4x\n",
764 __func__, modem_ctl[0], modem_ctl[1],
765 modem_ctl[2], modem_ctl[3]);
771 static int cp210x_tiocmset (
struct tty_struct *tty,
772 unsigned int set,
unsigned int clear)
775 return cp210x_tiocmset_port(port,
set, clear);
779 unsigned int set,
unsigned int clear)
800 dev_dbg(&port->
dev,
"%s - control = 0x%.4x\n", __func__, control);
808 cp210x_tiocmset_port(p,
TIOCM_DTR|TIOCM_RTS, 0);
810 cp210x_tiocmset_port(p, 0,
TIOCM_DTR|TIOCM_RTS);
813 static int cp210x_tiocmget (
struct tty_struct *tty)
828 dev_dbg(&port->
dev,
"%s - control = 0x%.2x\n", __func__, control);
833 static void cp210x_break_ctl (
struct tty_struct *tty,
int break_state)
838 if (break_state == 0)
842 dev_dbg(&port->
dev,
"%s - turning break %s\n", __func__,
847 static int cp210x_startup(
struct usb_serial *serial)
849 struct usb_host_interface *cur_altsetting;
859 cur_altsetting = serial->
interface->cur_altsetting;
862 usb_set_serial_data(serial, spriv);
867 static void cp210x_release(
struct usb_serial *serial)
871 spriv = usb_get_serial_data(serial);