2 * Silicon Laboratories CP2101/CP2102 USB to RS232 serial adaptor driver
4 * Copyright (C) 2005 Craig Shelley (craig@microtron.org.uk)
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License version
8 * 2 as published by the Free Software Foundation.
12 #include <linux/config.h>
13 #include <linux/kernel.h>
14 #include <linux/errno.h>
15 #include <linux/slab.h>
16 #include <linux/tty.h>
17 #include <linux/tty_flip.h>
18 #include <linux/module.h>
19 #include <linux/moduleparam.h>
20 #include <linux/usb.h>
21 #include <asm/uaccess.h>
22 #include "usb-serial.h"
27 #define DRIVER_VERSION "v0.03"
28 #define DRIVER_DESC "Silicon Labs CP2101/CP2102 RS232 serial adaptor driver"
33 static int cp2101_open(struct usb_serial_port
*, struct file
*);
34 static void cp2101_cleanup(struct usb_serial_port
*);
35 static void cp2101_close(struct usb_serial_port
*, struct file
*);
36 static void cp2101_get_termios(struct usb_serial_port
*);
37 static void cp2101_set_termios(struct usb_serial_port
*, struct termios
*);
38 static void cp2101_break_ctl(struct usb_serial_port
*, int);
39 static int cp2101_startup (struct usb_serial
*);
40 static void cp2101_shutdown(struct usb_serial
*);
45 static struct usb_device_id id_table
[] = {
46 {USB_DEVICE(0x10c4, 0xea60) }, /*Silicon labs factory default*/
47 {USB_DEVICE(0x10ab, 0x10c5) }, /*Siemens MC60 Cable*/
48 { } /* Terminating Entry*/
51 MODULE_DEVICE_TABLE (usb
, id_table
);
53 static struct usb_driver cp2101_driver
= {
56 .probe
= usb_serial_probe
,
57 .disconnect
= usb_serial_disconnect
,
61 static struct usb_serial_device_type cp2101_device
= {
65 .num_interrupt_in
= 0,
70 .close
= cp2101_close
,
71 .break_ctl
= cp2101_break_ctl
,
72 .set_termios
= cp2101_set_termios
,
73 .attach
= cp2101_startup
,
74 .shutdown
= cp2101_shutdown
,
77 /*Config request types*/
78 #define REQTYPE_HOST_TO_DEVICE 0x41
79 #define REQTYPE_DEVICE_TO_HOST 0xc1
81 /*Config SET requests. To GET, add 1 to the request number*/
82 #define CP2101_UART 0x00 /*Enable / Disable*/
83 #define CP2101_BAUDRATE 0x01 /*(BAUD_RATE_GEN_FREQ / baudrate)*/
84 #define CP2101_BITS 0x03 /*0x(0)(data bits)(parity)(stop bits)*/
85 #define CP2101_BREAK 0x05 /*On / Off*/
86 #define CP2101_DTRRTS 0x07 /*101 / 202 ???*/
87 #define CP2101_CONFIG_16 0x13 /*16 bytes of config data ???*/
88 #define CP2101_CONFIG_6 0x19 /*6 bytes of config data ???*/
91 #define UART_ENABLE 0x0001
92 #define UART_DISABLE 0x0000
95 #define BAUD_RATE_GEN_FREQ 0x384000
98 #define BITS_DATA_MASK 0X0f00
99 #define BITS_DATA_6 0X0600
100 #define BITS_DATA_7 0X0700
101 #define BITS_DATA_8 0X0800
102 #define BITS_DATA_9 0X0900
104 #define BITS_PARITY_MASK 0x00f0
105 #define BITS_PARITY_NONE 0x0000
106 #define BITS_PARITY_ODD 0x0010
107 #define BITS_PARITY_EVEN 0x0020
108 #define BITS_PARITY_MARK 0x0030
109 #define BITS_PARITY_SPACE 0x0040
111 #define BITS_STOP_MASK 0x000f
112 #define BITS_STOP_1 0x0000
113 #define BITS_STOP_1_5 0x0001
114 #define BITS_STOP_2 0x0002
115 #define BREAK_ON 0x0000
116 #define BREAK_OFF 0x0001
119 static int cp2101_get_config(struct usb_serial_port
* port
, u8 request
)
121 struct usb_serial
*serial
= port
->serial
;
122 unsigned char buf
[4];
126 /*For get requests, the request number must be incremented*/
129 /*Issue the request, attempting to read 4 bytes*/
130 result
= usb_control_msg (serial
->dev
,usb_rcvctrlpipe (serial
->dev
, 0),
131 request
, REQTYPE_DEVICE_TO_HOST
, 0x0000,
135 dev_err(&port
->dev
, "%s - Unable to send config request, "
136 "request=0x%x result=%d\n",
137 __FUNCTION__
, request
, result
);
141 /*Assemble each byte read into an integer value*/
143 for (i
=0; i
<4 && i
<result
; i
++)
144 value
|= (buf
[i
] << (i
* 8));
146 dbg( " %s - request=0x%x result=%d value=0x%x",
147 __FUNCTION__
, request
, result
, value
);
152 static int cp2101_set_config(struct usb_serial_port
* port
, u8 request
, u16 value
)
154 struct usb_serial
*serial
= port
->serial
;
156 result
= usb_control_msg (serial
->dev
, usb_sndctrlpipe(serial
->dev
, 0),
157 request
, REQTYPE_HOST_TO_DEVICE
, value
,
161 dev_err(&port
->dev
, "%s - Unable to send config request, "
162 "request=0x%x value=0x%x result=%d\n",
163 __FUNCTION__
, request
, value
, result
);
167 dbg(" %s - request=0x%x value=0x%x result=%d",
168 __FUNCTION__
, request
, value
, result
);
173 static int cp2101_open (struct usb_serial_port
*port
, struct file
*filp
)
175 struct usb_serial
*serial
= port
->serial
;
178 dbg("%s - port %d", __FUNCTION__
, port
->number
);
180 if (cp2101_set_config(port
, CP2101_UART
, UART_ENABLE
)) {
181 dev_err(&port
->dev
, "%s - Unable to enable UART\n",
186 /* Start reading from the device */
187 usb_fill_bulk_urb (port
->read_urb
, serial
->dev
,
188 usb_rcvbulkpipe(serial
->dev
,
189 port
->bulk_in_endpointAddress
),
190 port
->read_urb
->transfer_buffer
,
191 port
->read_urb
->transfer_buffer_length
,
192 serial
->type
->read_bulk_callback
,
194 result
= usb_submit_urb(port
->read_urb
, GFP_KERNEL
);
196 dev_err(&port
->dev
, "%s - failed resubmitting read urb, "
197 "error %d\n", __FUNCTION__
, result
);
201 /*Configure the termios structure*/
202 cp2101_get_termios(port
);
207 static void cp2101_cleanup (struct usb_serial_port
*port
)
209 struct usb_serial
*serial
= port
->serial
;
211 dbg("%s - port %d", __FUNCTION__
, port
->number
);
214 /* shutdown any bulk reads that might be going on */
215 if (serial
->num_bulk_out
)
216 usb_kill_urb(port
->write_urb
);
217 if (serial
->num_bulk_in
)
218 usb_kill_urb(port
->read_urb
);
222 static void cp2101_close (struct usb_serial_port
*port
, struct file
* filp
)
224 dbg("%s - port %d", __FUNCTION__
, port
->number
);
226 /* shutdown our urbs */
227 dbg("%s - shutting down urbs", __FUNCTION__
);
228 usb_kill_urb(port
->write_urb
);
229 usb_kill_urb(port
->read_urb
);
231 cp2101_set_config(port
, CP2101_UART
, UART_DISABLE
);
234 /* cp2101_get_termios*/
235 /* Reads the baud rate, data bits, parity and stop bits from the device*/
236 /* Corrects any unsupported values*/
237 /* Configures the termios structure to reflect the state of the device*/
238 static void cp2101_get_termios (struct usb_serial_port
*port
)
244 dbg("%s - port %d", __FUNCTION__
, port
->number
);
246 if ((!port
->tty
) || (!port
->tty
->termios
)) {
247 dbg("%s - no tty structures", __FUNCTION__
);
250 cflag
= port
->tty
->termios
->c_cflag
;
252 baud
= cp2101_get_config(port
, CP2101_BAUDRATE
);
253 /*Convert to baudrate*/
255 baud
= BAUD_RATE_GEN_FREQ
/ baud
;
257 dbg("%s - baud rate = %d", __FUNCTION__
, baud
);
260 /* The baud rates which are commented out below
261 * appear to be supported by the device
262 * but are non-standard
264 case 600: cflag
|= B600
; break;
265 case 1200: cflag
|= B1200
; break;
266 case 1800: cflag
|= B1800
; break;
267 case 2400: cflag
|= B2400
; break;
268 case 4800: cflag
|= B4800
; break;
269 /*case 7200: cflag |= B7200; break;*/
270 case 9600: cflag
|= B9600
; break;
271 /*case 14400: cflag |= B14400; break;*/
272 case 19200: cflag
|= B19200
; break;
273 /*case 28800: cflag |= B28800; break;*/
274 case 38400: cflag
|= B38400
; break;
275 /*case 55854: cflag |= B55054; break;*/
276 case 57600: cflag
|= B57600
; break;
277 case 115200: cflag
|= B115200
; break;
278 /*case 127117: cflag |= B127117; break;*/
279 case 230400: cflag
|= B230400
; break;
280 case 460800: cflag
|= B460800
; break;
281 case 921600: cflag
|= B921600
; break;
282 /*case 3686400: cflag |= B3686400; break;*/
284 dbg("%s - Baud rate is not supported, "
285 "using 9600 baud", __FUNCTION__
);
287 cp2101_set_config(port
, CP2101_BAUDRATE
,
288 (BAUD_RATE_GEN_FREQ
/9600));
292 bits
= cp2101_get_config(port
, CP2101_BITS
);
294 switch(bits
& BITS_DATA_MASK
) {
296 dbg("%s - data bits = 6", __FUNCTION__
);
300 dbg("%s - data bits = 7", __FUNCTION__
);
304 dbg("%s - data bits = 8", __FUNCTION__
);
308 dbg("%s - data bits = 9 (not supported, "
309 "using 8 data bits)", __FUNCTION__
);
311 bits
&= ~BITS_DATA_MASK
;
313 cp2101_set_config(port
, CP2101_BITS
, bits
);
316 dbg("%s - Unknown number of data bits, "
317 "using 8", __FUNCTION__
);
319 bits
&= ~BITS_DATA_MASK
;
321 cp2101_set_config(port
, CP2101_BITS
, bits
);
325 switch(bits
& BITS_PARITY_MASK
) {
326 case BITS_PARITY_NONE
:
327 dbg("%s - parity = NONE", __FUNCTION__
);
330 case BITS_PARITY_ODD
:
331 dbg("%s - parity = ODD", __FUNCTION__
);
332 cflag
|= (PARENB
|PARODD
);
334 case BITS_PARITY_EVEN
:
335 dbg("%s - parity = EVEN", __FUNCTION__
);
339 case BITS_PARITY_MARK
:
340 dbg("%s - parity = MARK (not supported, "
341 "disabling parity)", __FUNCTION__
);
343 bits
&= ~BITS_PARITY_MASK
;
344 cp2101_set_config(port
, CP2101_BITS
, bits
);
346 case BITS_PARITY_SPACE
:
347 dbg("%s - parity = SPACE (not supported, "
348 "disabling parity)", __FUNCTION__
);
350 bits
&= ~BITS_PARITY_MASK
;
351 cp2101_set_config(port
, CP2101_BITS
, bits
);
354 dbg("%s - Unknown parity mode, "
355 "disabling parity", __FUNCTION__
);
357 bits
&= ~BITS_PARITY_MASK
;
358 cp2101_set_config(port
, CP2101_BITS
, bits
);
363 switch(bits
& BITS_STOP_MASK
) {
365 dbg("%s - stop bits = 1", __FUNCTION__
);
368 dbg("%s - stop bits = 1.5 (not supported, "
369 "using 1 stop bit", __FUNCTION__
);
370 bits
&= ~BITS_STOP_MASK
;
371 cp2101_set_config(port
, CP2101_BITS
, bits
);
374 dbg("%s - stop bits = 2", __FUNCTION__
);
378 dbg("%s - Unknown number of stop bits, "
379 "using 1 stop bit", __FUNCTION__
);
380 bits
&= ~BITS_STOP_MASK
;
381 cp2101_set_config(port
, CP2101_BITS
, bits
);
385 port
->tty
->termios
->c_cflag
= cflag
;
388 static void cp2101_set_termios (struct usb_serial_port
*port
,
389 struct termios
*old_termios
)
391 unsigned int cflag
, old_cflag
=0;
395 dbg("%s - port %d", __FUNCTION__
, port
->number
);
397 if ((!port
->tty
) || (!port
->tty
->termios
)) {
398 dbg("%s - no tty structures", __FUNCTION__
);
401 cflag
= port
->tty
->termios
->c_cflag
;
403 /* check that they really want us to change something */
405 if ((cflag
== old_termios
->c_cflag
) &&
406 (RELEVANT_IFLAG(port
->tty
->termios
->c_iflag
)
407 == RELEVANT_IFLAG(old_termios
->c_iflag
))) {
408 dbg("%s - nothing to change...", __FUNCTION__
);
412 old_cflag
= old_termios
->c_cflag
;
415 /* If the baud rate is to be updated*/
416 if ((cflag
& CBAUD
) != (old_cflag
& CBAUD
)) {
417 switch (cflag
& CBAUD
) {
418 /* The baud rates which are commented out below
419 * appear to be supported by the device
420 * but are non-standard
422 case B0
: baud
= 0; break;
423 case B600
: baud
= 600; break;
424 case B1200
: baud
= 1200; break;
425 case B1800
: baud
= 1800; break;
426 case B2400
: baud
= 2400; break;
427 case B4800
: baud
= 4800; break;
428 /*case B7200: baud = 7200; break;*/
429 case B9600
: baud
= 9600; break;
430 /*ase B14400: baud = 14400; break;*/
431 case B19200
: baud
= 19200; break;
432 /*case B28800: baud = 28800; break;*/
433 case B38400
: baud
= 38400; break;
434 /*case B55854: baud = 55054; break;*/
435 case B57600
: baud
= 57600; break;
436 case B115200
: baud
= 115200; break;
437 /*case B127117: baud = 127117; break;*/
438 case B230400
: baud
= 230400; break;
439 case B460800
: baud
= 460800; break;
440 case B921600
: baud
= 921600; break;
441 /*case B3686400: baud = 3686400; break;*/
443 dev_err(&port
->dev
, "cp2101 driver does not "
444 "support the baudrate requested\n");
449 dbg("%s - Setting baud rate to %d baud", __FUNCTION__
,
451 if (cp2101_set_config(port
, CP2101_BAUDRATE
,
452 (BAUD_RATE_GEN_FREQ
/ baud
)))
453 dev_err(&port
->dev
, "Baud rate requested not "
454 "supported by device\n");
458 /*If the number of data bits is to be updated*/
459 if ((cflag
& CSIZE
) != (old_cflag
& CSIZE
)) {
460 bits
= cp2101_get_config(port
, CP2101_BITS
);
461 bits
&= ~BITS_DATA_MASK
;
462 switch (cflag
& CSIZE
) {
465 dbg("%s - data bits = 6", __FUNCTION__
);
469 dbg("%s - data bits = 7", __FUNCTION__
);
473 dbg("%s - data bits = 8", __FUNCTION__
);
477 dbg("%s - data bits = 9", __FUNCTION__);
480 dev_err(&port
->dev
, "cp2101 driver does not "
481 "support the number of bits requested,"
482 " using 8 bit mode\n");
486 if (cp2101_set_config(port
, CP2101_BITS
, bits
))
487 dev_err(&port
->dev
, "Number of data bits requested "
488 "not supported by device\n");
491 if ((cflag
& (PARENB
|PARODD
)) != (old_cflag
& (PARENB
|PARODD
))) {
492 bits
= cp2101_get_config(port
, CP2101_BITS
);
493 bits
&= ~BITS_PARITY_MASK
;
494 if (cflag
& PARENB
) {
495 if (cflag
& PARODD
) {
496 bits
|= BITS_PARITY_ODD
;
497 dbg("%s - parity = ODD", __FUNCTION__
);
499 bits
|= BITS_PARITY_EVEN
;
500 dbg("%s - parity = EVEN", __FUNCTION__
);
503 if (cp2101_set_config(port
, CP2101_BITS
, bits
))
504 dev_err(&port
->dev
, "Parity mode not supported "
508 if ((cflag
& CSTOPB
) != (old_cflag
& CSTOPB
)) {
509 bits
= cp2101_get_config(port
, CP2101_BITS
);
510 bits
&= ~BITS_STOP_MASK
;
511 if (cflag
& CSTOPB
) {
513 dbg("%s - stop bits = 2", __FUNCTION__
);
516 dbg("%s - stop bits = 1", __FUNCTION__
);
518 if (cp2101_set_config(port
, CP2101_BITS
, bits
))
519 dev_err(&port
->dev
, "Number of stop bits requested "
520 "not supported by device\n");
524 static void cp2101_break_ctl (struct usb_serial_port
*port
, int break_state
)
528 dbg("%s - port %d", __FUNCTION__
, port
->number
);
529 if (break_state
== 0)
533 dbg("%s - turning break %s", __FUNCTION__
,
534 state
==BREAK_OFF
? "off" : "on");
535 cp2101_set_config(port
, CP2101_BREAK
, state
);
538 static int cp2101_startup (struct usb_serial
*serial
)
540 /*CP2101 buffers behave strangely unless device is reset*/
541 usb_reset_device(serial
->dev
);
545 static void cp2101_shutdown (struct usb_serial
*serial
)
549 dbg("%s", __FUNCTION__
);
551 /* stop reads and writes on all ports */
552 for (i
=0; i
< serial
->num_ports
; ++i
) {
553 cp2101_cleanup(serial
->port
[i
]);
557 static int __init
cp2101_init (void)
561 retval
= usb_serial_register(&cp2101_device
);
563 return retval
; /*Failed to register*/
565 retval
= usb_register(&cp2101_driver
);
567 /*Failed to register*/
568 usb_serial_deregister(&cp2101_device
);
573 info(DRIVER_DESC
" " DRIVER_VERSION
);
577 static void __exit
cp2101_exit (void)
579 usb_deregister (&cp2101_driver
);
580 usb_serial_deregister (&cp2101_device
);
583 module_init(cp2101_init
);
584 module_exit(cp2101_exit
);
586 MODULE_DESCRIPTION(DRIVER_DESC
);
587 MODULE_VERSION(DRIVER_VERSION
);
588 MODULE_LICENSE("GPL");
590 module_param(debug
, bool, S_IRUGO
| S_IWUSR
);
591 MODULE_PARM_DESC(debug
, "Enable verbose debugging messages");