2 * Silicon Laboratories CP210x 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.
10 * Support to set flow control line levels using TIOCMGET and TIOCMSET
11 * thanks to Karl Hiramoto karl@hiramoto.org. RTSCTS hardware flow
12 * control thanks to Munir Nassar nassarmu@real-time.com
16 #include <linux/kernel.h>
17 #include <linux/errno.h>
18 #include <linux/slab.h>
19 #include <linux/tty.h>
20 #include <linux/tty_flip.h>
21 #include <linux/module.h>
22 #include <linux/moduleparam.h>
23 #include <linux/usb.h>
24 #include <asm/uaccess.h>
25 #include <linux/usb/serial.h>
31 #define DRIVER_VERSION "vxxPKGVERxxtmi"
32 #define DRIVER_DESC "Silicon Labs CP210x RS232 serial adaptor driver"
37 static int cp210x_open(struct usb_serial_port
*, struct file
*);
38 static void cp210x_cleanup(struct usb_serial_port
*);
39 static void cp210x_close(struct usb_serial_port
*, struct file
*);
40 static void cp210x_get_termios(struct usb_serial_port
*);
41 static void cp210x_set_termios(struct usb_serial_port
*, struct ktermios
*);
42 static int cp210x_tiocmget(struct usb_serial_port
*, struct file
*);
43 static int cp210x_tiocmset(struct usb_serial_port
*, struct file
*,
44 unsigned int, unsigned int);
45 static void cp210x_break_ctl(struct usb_serial_port
*, int);
46 static int cp210x_startup(struct usb_serial
*);
47 static void cp210x_shutdown(struct usb_serial
*);
48 static int cp210x_ioctl(struct usb_serial_port
*, struct file
*,
49 unsigned int, unsigned long);
53 static const struct usb_device_id id_table
[] = {
54 { USB_DEVICE(0x0471, 0x066A) }, /* AKTAKOM ACE-1001 cable */
55 { USB_DEVICE(0x0489, 0xE000) }, /* Pirelli Broadband S.p.A, DP-L10 SIP/GSM Mobile */
56 { USB_DEVICE(0x0745, 0x1000) }, /* CipherLab USB CCD Barcode Scanner 1000 */
57 { USB_DEVICE(0x08e6, 0x5501) }, /* Gemalto Prox-PU/CU contactless smartcard reader */
58 { USB_DEVICE(0x08FD, 0x000A) }, /* Digianswer A/S , ZigBee/802.15.4 MAC Device */
59 { USB_DEVICE(0x0FCF, 0x1003) }, /* Dynastream ANT development board */
60 { USB_DEVICE(0x0FCF, 0x1004) }, /* Dynastream ANT2USB */
61 { USB_DEVICE(0x0FCF, 0x1006) }, /* Dynastream ANT development board */
62 { USB_DEVICE(0x10A6, 0xAA26) }, /* Knock-off DCU-11 cable */
63 { USB_DEVICE(0x10AB, 0x10C5) }, /* Siemens MC60 Cable */
64 { USB_DEVICE(0x10B5, 0xAC70) }, /* Nokia CA-42 USB */
65 { USB_DEVICE(0x10C4, 0x0F91) }, /* Vstabi */
66 { USB_DEVICE(0x10C4, 0x1101) }, /* Arkham Technology DS101 Bus Monitor */
67 { USB_DEVICE(0x10C4, 0x1601) }, /* Arkham Technology DS101 Adapter */
68 { USB_DEVICE(0x10C4, 0x800A) }, /* SPORTident BSM7-D-USB main station */
69 { USB_DEVICE(0x10C4, 0x803B) }, /* Pololu USB-serial converter */
70 { USB_DEVICE(0x10C4, 0x8053) }, /* Enfora EDG1228 */
71 { USB_DEVICE(0x10C4, 0x8054) }, /* Enfora GSM2228 */
72 { USB_DEVICE(0x10C4, 0x8066) }, /* Argussoft In-System Programmer */
73 { USB_DEVICE(0x10C4, 0x807A) }, /* Crumb128 board */
74 { USB_DEVICE(0x10C4, 0x80CA) }, /* Degree Controls Inc */
75 { USB_DEVICE(0x10C4, 0x80DD) }, /* Tracient RFID */
76 { USB_DEVICE(0x10C4, 0x80F6) }, /* Suunto sports instrument */
77 { USB_DEVICE(0x10C4, 0x8115) }, /* Arygon NFC/Mifare Reader */
78 { USB_DEVICE(0x10C4, 0x813D) }, /* Burnside Telecom Deskmobile */
79 { USB_DEVICE(0x10C4, 0x813F) }, /* Tams Master Easy Control */
80 { USB_DEVICE(0x10C4, 0x814A) }, /* West Mountain Radio RIGblaster P&P */
81 { USB_DEVICE(0x10C4, 0x814B) }, /* West Mountain Radio RIGtalk */
82 { USB_DEVICE(0x10C4, 0x815E) }, /* Helicomm IP-Link 1220-DVM */
83 { USB_DEVICE(0x10C4, 0x819F) }, /* MJS USB Toslink Switcher */
84 { USB_DEVICE(0x10C4, 0x81A6) }, /* ThinkOptics WavIt */
85 { USB_DEVICE(0x10C4, 0x81AC) }, /* MSD Dash Hawk */
86 { USB_DEVICE(0x10C4, 0x81C8) }, /* Lipowsky Industrie Elektronik GmbH, Baby-JTAG */
87 { USB_DEVICE(0x10C4, 0x81E2) }, /* Lipowsky Industrie Elektronik GmbH, Baby-LIN */
88 { USB_DEVICE(0x10C4, 0x81E7) }, /* Aerocomm Radio */
89 { USB_DEVICE(0x10C4, 0x81E8) }, /* Zephyr Bioharness */
90 { USB_DEVICE(0x10C4, 0x81F2) }, /* C1007 HF band RFID controller */
91 { USB_DEVICE(0x10C4, 0x8218) }, /* Lipowsky Industrie Elektronik GmbH, HARP-1 */
92 { USB_DEVICE(0x10C4, 0x822B) }, /* Modem EDGE(GSM) Comander 2 */
93 { USB_DEVICE(0x10C4, 0x826B) }, /* Cygnal Integrated Products, Inc., Fasttrax GPS demostration module */
94 { USB_DEVICE(0x10C4, 0x8293) }, /* Telegesys ETRX2USB */
95 { USB_DEVICE(0x10C4, 0x82F9) }, /* Procyon AVS */
96 { USB_DEVICE(0x10C4, 0x8341) }, /* Siemens MC35PU GPRS Modem */
97 { USB_DEVICE(0x10C4, 0x8382) }, /* Cygnal Integrated Products, Inc. */
98 { USB_DEVICE(0x10C4, 0x83A8) }, /* Amber Wireless AMB2560 */
99 { USB_DEVICE(0x10C4, 0x8411) }, /* Kyocera GPS Module */
100 { USB_DEVICE(0x10C4, 0x846E) }, /* BEI USB Sensor Interface (VCP) */
101 { USB_DEVICE(0x10C4, 0xEA60) }, /* Silicon Labs factory default */
102 { USB_DEVICE(0x10C4, 0xEA61) }, /* Silicon Labs factory default */
103 { USB_DEVICE(0x10C4, 0xF001) }, /* Elan Digital Systems USBscope50 */
104 { USB_DEVICE(0x10C4, 0xF002) }, /* Elan Digital Systems USBwave12 */
105 { USB_DEVICE(0x10C4, 0xF003) }, /* Elan Digital Systems USBpulse100 */
106 { USB_DEVICE(0x10C4, 0xF004) }, /* Elan Digital Systems USBcount50 */
107 { USB_DEVICE(0x10C5, 0xEA61) }, /* Silicon Labs MobiData GPRS USB Modem */
108 { USB_DEVICE(0x10CE, 0xEA6A) }, /* Silicon Labs MobiData GPRS USB Modem 100EU */
109 { USB_DEVICE(0x13AD, 0x9999) }, /* Baltech card reader */
110 { USB_DEVICE(0x1555, 0x0004) }, /* Owen AC4 USB-RS485 Converter */
111 { USB_DEVICE(0x166A, 0x0303) }, /* Clipsal 5500PCU C-Bus USB interface */
112 { USB_DEVICE(0x16D6, 0x0001) }, /* Jablotron serial interface */
113 { USB_DEVICE(0x18EF, 0xE00F) }, /* ELV USB-I2C-Interface */
114 { USB_DEVICE(0x413C, 0x9500) }, /* DW700 GPS USB interface */
115 { } /* Terminating Entry */
118 MODULE_DEVICE_TABLE(usb
, id_table
);
120 static struct usb_driver cp210x_driver
= {
122 .probe
= usb_serial_probe
,
123 .disconnect
= usb_serial_disconnect
,
124 .id_table
= id_table
,
128 static struct usb_serial_driver cp210x_device
= {
130 .owner
= THIS_MODULE
,
133 .usb_driver
= &cp210x_driver
,
134 .id_table
= id_table
,
135 .num_interrupt_in
= 0,
136 .num_bulk_in
= NUM_DONT_CARE
,
137 .num_bulk_out
= NUM_DONT_CARE
,
140 .close
= cp210x_close
,
141 .ioctl
= cp210x_ioctl
,
142 .break_ctl
= cp210x_break_ctl
,
143 .set_termios
= cp210x_set_termios
,
144 .tiocmget
= cp210x_tiocmget
,
145 .tiocmset
= cp210x_tiocmset
,
146 .attach
= cp210x_startup
,
147 .shutdown
= cp210x_shutdown
,
150 /* Config request types */
151 #define REQTYPE_HOST_TO_DEVICE 0x41
152 #define REQTYPE_DEVICE_TO_HOST 0xc1
154 /* Config SET requests. To GET, add 1 to the request number */
155 #define CP2101_UART 0x00 /* Enable / Disable */
156 #define CP2101_BAUDRATE 0x01 /* (BAUD_RATE_GEN_FREQ / baudrate) */
157 #define CP2101_BITS 0x03 /* 0x(0)(databits)(parity)(stopbits) */
158 #define CP2101_BREAK 0x05 /* On / Off */
159 #define CP2101_CONTROL 0x07 /* Flow control line states */
160 #define CP2101_MODEMCTL 0x13 /* Modem controls */
161 #define CP2101_CONFIG_6 0x19 /* 6 bytes of config data ??? */
164 #define UART_ENABLE 0x0001
165 #define UART_DISABLE 0x0000
167 /* CP2101_BAUDRATE */
168 #define BAUD_RATE_GEN_FREQ 0x384000
171 #define BITS_DATA_MASK 0X0f00
172 #define BITS_DATA_5 0X0500
173 #define BITS_DATA_6 0X0600
174 #define BITS_DATA_7 0X0700
175 #define BITS_DATA_8 0X0800
176 #define BITS_DATA_9 0X0900
178 #define BITS_PARITY_MASK 0x00f0
179 #define BITS_PARITY_NONE 0x0000
180 #define BITS_PARITY_ODD 0x0010
181 #define BITS_PARITY_EVEN 0x0020
182 #define BITS_PARITY_MARK 0x0030
183 #define BITS_PARITY_SPACE 0x0040
185 #define BITS_STOP_MASK 0x000f
186 #define BITS_STOP_1 0x0000
187 #define BITS_STOP_1_5 0x0001
188 #define BITS_STOP_2 0x0002
191 #define BREAK_ON 0x0000
192 #define BREAK_OFF 0x0001
195 #define CONTROL_DTR 0x0001
196 #define CONTROL_RTS 0x0002
197 #define CONTROL_CTS 0x0010
198 #define CONTROL_DSR 0x0020
199 #define CONTROL_RING 0x0040
200 #define CONTROL_DCD 0x0080
201 #define CONTROL_WRITE_DTR 0x0100
202 #define CONTROL_WRITE_RTS 0x0200
204 /* cp210x_get_partnum() return codes */
205 #define CP210x_PART_UNKNOWN 0x00
206 #define CP210x_PART_CP2101 0x01
207 #define CP210x_PART_CP2102 0x02
208 #define CP210x_PART_CP2103 0x03
210 /* Helper to make usb string size */
211 #define USBSTRLEN(x) (x * 2 + 2)
213 /* Populates usbstr with: (len) + (0x03) + unicode(str). Each char in str
214 * takes two bytes in unicode format.
215 * Returns the resulting length of the string in usbstr.
216 * This function can accept overlapping usbstr and str as long as the overlap
217 * does not cause data written to usbstr to overwrite data not yet read from
220 static int make_usb_string(char *usbstr
, size_t usblen
, char *src
,
225 if (usbstr
&& usblen
>= 2 && src
&& *src
&& srclen
) {
234 while (srclen
&& len
< usblen
) {
246 * cp210x_usbstr_from_user
247 * Populate kbuf with a usb string derived from a user space variable. klen
248 * is the size of the buffer at kbuf.
249 * Returns the number of bytes used in kbuf.
251 static size_t cp210x_usbstr_from_user(char *kbuf
, unsigned long ubuf
, size_t klen
)
253 struct cp210x_buffer t
;
257 if (!kbuf
|| !ubuf
|| !klen
)
259 if (copy_from_user(&t
, (struct cp210x_buffer __user
*)ubuf
, sizeof(t
)))
261 if (!t
.buf
|| !t
.len
|| USBSTRLEN(t
.len
) > klen
)
263 slen
= (klen
- 2) / 2;
266 str
= kbuf
+ klen
- slen
;
267 if (copy_from_user(str
, (u8 __user
*)t
.buf
, slen
))
269 return make_usb_string(kbuf
, klen
, str
, slen
);
273 * Returns 1 if the CP210X part includes firmware that allows setting the
274 * USB MFG descriptor, else 0. As of this writing, no CP210X firmware allows
275 * this. SiLabs has suggested this may change in future firmware versions or
278 static inline int cp210x_has_setmfg(void)
285 * A generic usb control message interface.
286 * Returns the actual size of the data read or written within the message, 0
287 * if no data were read or written, or a negative value to indicate an error.
289 static int cp210x_ctlmsg(struct usb_serial_port
*port
, u8 request
,
290 u8 requestype
, u16 value
, u16 index
, void *data
, u16 size
)
292 struct usb_device
*dev
= port
->serial
->dev
;
296 if (!(tbuf
= kmalloc(size
, GFP_KERNEL
)))
298 if (requestype
& 0x80) {
299 ret
= usb_control_msg(dev
, usb_rcvctrlpipe(dev
, 0), request
,
300 requestype
, value
, index
, tbuf
, size
, 300);
302 memcpy(data
, tbuf
, size
);
305 memcpy(tbuf
, data
, size
);
306 ret
= usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0), request
,
307 requestype
, value
, index
, tbuf
, size
, 300);
310 if (ret
< 0 && ret
!= -EPIPE
) {
311 dev_printk(KERN_DEBUG
, &dev
->dev
, "cp210x: control failed cmd rqt %u "
312 "rq %u len %u ret %d\n", requestype
, request
, size
, ret
);
317 static int cp210x_reset(struct usb_serial_port
*port
)
319 dbg("%s", __FUNCTION__
);
322 /* Is this better than usb_device_reset? It may be. Once a client issues
323 * the reset ioctl, it must disconnect and reconnect, since the USB
324 * connections are torn down. We also ignore the error return, since
325 * the part resets and doesn't send one...
327 cp210x_ctlmsg(port
, 0xff, 0x40, 0x0008, 0x00, NULL
, 0);
329 usb_reset_device(port
->serial
->dev
);
334 static int cp210x_get_partnum(struct usb_serial_port
*port
)
336 static u8 _partnum
= CP210x_PART_UNKNOWN
;
338 if (_partnum
== CP210x_PART_UNKNOWN
) {
339 u8 addr
= port
->serial
->dev
->actconfig
->interface
[0]->
340 cur_altsetting
->endpoint
[0].desc
.bEndpointAddress
&
341 USB_ENDPOINT_NUMBER_MASK
;
343 if (addr
== 0x03 || addr
== 0x02)
344 _partnum
= CP210x_PART_CP2101
;
345 else if (addr
== 0x01) {
346 /* Must query part to determine part number */
347 if (cp210x_ctlmsg(port
, 0xff, 0xc0, 0x370b, 0x00,
349 _partnum
= CP210x_PART_UNKNOWN
;
352 dbg("%s - partnum %u", __FUNCTION__
, _partnum
);
356 static inline int cp210x_setu16(struct usb_serial_port
*port
, int cmd
,
359 return cp210x_ctlmsg(port
, 0xff, 0x40, 0x3700 | (cmd
& 0xff),
366 * Set a USB string descriptor using proprietary cp210x control messages.
367 * Return the number of characters actually written.
369 static int cp210x_setstr(struct usb_serial_port
*port
, int cmd
, char *usbstr
)
371 unsigned len
= usbstr
[0];
372 int ret
= cp210x_ctlmsg(port
, 0xff, 0x40, 0x3700 | (cmd
& 0xff), 0,
374 dbg("%s - cmd 0x%02x len %d ret %d", __FUNCTION__
, cmd
, len
, ret
);
378 /* Set all gpio simultaneously */
379 static int cp210x_gpioset(struct usb_serial_port
*port
, u8 gpio
)
381 dbg("%s - port %d, gpio = 0x%.2x", __FUNCTION__
, port
->number
, gpio
);
383 return cp210x_ctlmsg(port
, 0xff, 0x40, 0x37e1,
384 ((uint16_t)gpio
<< 8) | GPIO_MASK
, NULL
, 0);
387 /* Set select gpio bits */
388 static int cp210x_gpiosetb(struct usb_serial_port
*port
, u8 set
, u8 clear
)
392 /* The bitmask is in the LSB, the values in the MSB */
394 gpio
|= (GPIO_0
<< 8)|GPIO_0
;
396 gpio
|= (GPIO_1
<< 8)|GPIO_1
;
398 gpio
|= (GPIO_2
<< 8)|GPIO_2
;
400 gpio
|= (GPIO_3
<< 8)|GPIO_3
;
402 gpio
= (gpio
& ~(GPIO_0
<< 8))|GPIO_0
;
404 gpio
= (gpio
& ~(GPIO_1
<< 8))|GPIO_1
;
406 gpio
= (gpio
& ~(GPIO_2
<< 8))|GPIO_2
;
408 gpio
= (gpio
& ~(GPIO_3
<< 8))|GPIO_3
;
410 dbg("%s - port %d, gpiob = 0x%.4x", __FUNCTION__
, port
->number
, gpio
);
412 /* FIXME: how about REQTYPE_HOST_TO_DEVICE instead of 0x40? */
413 return cp210x_ctlmsg(port
, 0xff, 0x40, 0x37e1, gpio
, NULL
, 0);
416 static int cp210x_gpioget(struct usb_serial_port
*port
, u8
*gpio
)
420 dbg("%s - port %d", __FUNCTION__
, port
->number
);
422 /* FIXME: how about REQTYPE_DEVICE_TO_HOST instead of 0xc0? */
423 ret
= cp210x_ctlmsg(port
, 0xff, 0xc0, 0x00c2, 0, gpio
, 1);
425 dbg("%s - gpio = 0x%.2x (%d)", __FUNCTION__
, *gpio
, ret
);
427 return (ret
== 1) ? 0 : -1;
430 static int cp210x_portconfset(struct usb_serial_port
*port
,
431 struct cp210x_port_config
*config
)
433 struct cp210x_port_config lconfig
;
436 dbg("%s", __FUNCTION__
);
438 memcpy(&lconfig
, config
, sizeof(lconfig
));
440 /* Words from cp2103 are MSB */
441 lconfig
.reset
.mode
= cpu_to_be16(config
->reset
.mode
);
442 lconfig
.reset
.latch
= cpu_to_be16(config
->reset
.latch
);
443 lconfig
.suspend
.mode
= cpu_to_be16(config
->suspend
.mode
);
444 lconfig
.suspend
.latch
= cpu_to_be16(config
->suspend
.latch
);
445 #if 0 /* apparently not implemented by CP210X firmware */
446 lconfig
.reset
.lowPower
= cpu_to_be16(config
->reset
.lowPower
);
447 lconfig
.suspend
.lowPower
= cpu_to_be16(config
->suspend
.lowPower
);
449 lconfig
.suspend
.lowPower
= 0;
450 lconfig
.reset
.lowPower
= 0;
453 ret
= cp210x_ctlmsg(port
, 0xff, 0x40, 0x370c, 0, &lconfig
,
454 sizeof(struct cp210x_port_config
));
455 if (ret
== sizeof(struct cp210x_port_config
))
463 static int cp210x_portconfget(struct usb_serial_port
*port
,
464 struct cp210x_port_config
*config
)
468 dbg("%s", __FUNCTION__
);
470 ret
= cp210x_ctlmsg(port
, 0xff, 0xc0, 0x370c, 0, config
,
471 sizeof(struct cp210x_port_config
));
472 if (ret
== sizeof(struct cp210x_port_config
)) {
473 /* Words from cp2103 are MSB */
474 config
->reset
.mode
= be16_to_cpu(config
->reset
.mode
);
475 config
->reset
.lowPower
= be16_to_cpu(config
->reset
.lowPower
);
476 config
->reset
.latch
= be16_to_cpu(config
->reset
.latch
);
477 config
->suspend
.mode
= be16_to_cpu(config
->suspend
.mode
);
478 config
->suspend
.lowPower
=
479 be16_to_cpu(config
->suspend
.lowPower
);
480 config
->suspend
.latch
= be16_to_cpu(config
->suspend
.latch
);
482 /* apparently not implemented yet */
483 config
->reset
.lowPower
= 0;
484 config
->suspend
.lowPower
= 0;
496 * Reads from the CP210x configuration registers
497 * 'size' is specified in bytes.
498 * 'data' is a pointer to a pre-allocated array of integers large
499 * enough to hold 'size' bytes (with 4 bytes to each integer)
501 static int cp210x_get_config(struct usb_serial_port
*port
, u8 request
,
502 unsigned int *data
, int size
)
504 struct usb_serial
*serial
= port
->serial
;
506 int result
, i
, length
;
508 /* Number of integers required to contain the array */
509 length
= (((size
- 1) | 3) + 1)/4;
511 buf
= kcalloc(length
, sizeof(__le32
), GFP_KERNEL
);
513 dev_err(&port
->dev
, "%s - out of memory.\n", __FUNCTION__
);
517 /* For get requests, the request number must be incremented */
520 /* Issue the request, attempting to read 'size' bytes */
521 result
= usb_control_msg(serial
->dev
, usb_rcvctrlpipe(serial
->dev
, 0),
522 request
, REQTYPE_DEVICE_TO_HOST
, 0x0000,
525 /* Convert data into an array of integers */
526 for (i
= 0; i
< length
; i
++)
527 data
[i
] = le32_to_cpu(buf
[i
]);
531 if (result
!= size
) {
532 dev_err(&port
->dev
, "%s - Unable to send config request, "
533 "request=0x%x size=%d result=%d\n",
534 __FUNCTION__
, request
, size
, result
);
543 * Writes to the CP210x configuration registers
544 * Values less than 16 bits wide are sent directly
545 * 'size' is specified in bytes.
547 static int cp210x_set_config(struct usb_serial_port
*port
, u8 request
,
548 unsigned int *data
, int size
)
550 struct usb_serial
*serial
= port
->serial
;
552 int result
, i
, length
;
554 /* Number of integers required to contain the array */
555 length
= (((size
- 1) | 3) + 1)/4;
557 buf
= kmalloc(length
* sizeof(__le32
), GFP_KERNEL
);
559 dev_err(&port
->dev
, "%s - out of memory.\n",
564 /* Array of integers into bytes */
565 for (i
= 0; i
< length
; i
++)
566 buf
[i
] = cpu_to_le32(data
[i
]);
569 result
= usb_control_msg(serial
->dev
,
570 usb_sndctrlpipe(serial
->dev
, 0),
571 request
, REQTYPE_HOST_TO_DEVICE
, 0x0000,
574 result
= usb_control_msg(serial
->dev
,
575 usb_sndctrlpipe(serial
->dev
, 0),
576 request
, REQTYPE_HOST_TO_DEVICE
, data
[0],
582 if ((size
> 2 && result
!= size
) || result
< 0) {
583 dev_err(&port
->dev
, "%s - Unable to send request, "
584 "request=0x%x size=%d result=%d\n",
585 __FUNCTION__
, request
, size
, result
);
593 * cp210x_set_config_single
594 * Convenience function for calling cp210x_set_config on single data values
595 * without requiring an integer pointer
597 static inline int cp210x_set_config_single(struct usb_serial_port
*port
,
598 u8 request
, unsigned int data
)
600 return cp210x_set_config(port
, request
, &data
, 2);
603 static int cp210x_open(struct usb_serial_port
*port
, struct file
*filp
)
605 struct usb_serial
*serial
= port
->serial
;
608 dbg("%s - port %d", __FUNCTION__
, port
->number
);
610 if (cp210x_set_config_single(port
, CP2101_UART
, UART_ENABLE
)) {
611 dev_err(&port
->dev
, "%s - Unable to enable UART\n",
616 /* Start reading from the device */
617 usb_fill_bulk_urb(port
->read_urb
, serial
->dev
,
618 usb_rcvbulkpipe(serial
->dev
,
619 port
->bulk_in_endpointAddress
),
620 port
->read_urb
->transfer_buffer
,
621 port
->read_urb
->transfer_buffer_length
,
622 serial
->type
->read_bulk_callback
,
624 result
= usb_submit_urb(port
->read_urb
, GFP_KERNEL
);
626 dev_err(&port
->dev
, "%s - failed resubmitting read urb, "
627 "error %d\n", __FUNCTION__
, result
);
631 /* Configure the termios structure */
632 cp210x_get_termios(port
);
634 /* Set the DTR and RTS pins low */
635 cp210x_tiocmset(port
, NULL
, TIOCM_DTR
| TIOCM_RTS
, 0);
640 static void cp210x_cleanup(struct usb_serial_port
*port
)
642 struct usb_serial
*serial
= port
->serial
;
644 dbg("%s - port %d", __FUNCTION__
, port
->number
);
647 /* shutdown any bulk reads that might be going on */
648 if (serial
->num_bulk_out
)
649 usb_kill_urb(port
->write_urb
);
650 if (serial
->num_bulk_in
)
651 usb_kill_urb(port
->read_urb
);
655 static void cp210x_close(struct usb_serial_port
*port
, struct file
*filp
)
657 dbg("%s - port %d", __FUNCTION__
, port
->number
);
659 /* shutdown our urbs */
660 dbg("%s - shutting down urbs", __FUNCTION__
);
661 usb_kill_urb(port
->write_urb
);
662 usb_kill_urb(port
->read_urb
);
664 cp210x_set_config_single(port
, CP2101_UART
, UART_DISABLE
);
667 static int cp210x_ioctl(struct usb_serial_port
*port
, struct file
*file
,
668 unsigned int cmd
, unsigned long arg
)
670 dbg("%s (%d) cmd = 0x%04x", __FUNCTION__
, port
->number
, cmd
);
676 int result
= cp210x_tiocmget(port
, file
);
677 if (copy_to_user((int __user
*)&arg
, &result
, sizeof(int)))
689 if (copy_from_user(&val
, (int __user
*)&arg
, sizeof(int)))
692 /* this looks wrong: TIOCMSET isn't going to work right */
693 if (cp210x_tiocmset(port
, file
, cmd
==TIOCMBIC
? 0 : val
,
694 cmd
==TIOCMBIC
? val
: 0))
700 case CP210x_IOCTL_GPIOGET
:
701 if (cp210x_get_partnum(port
) == CP210x_PART_CP2103
) {
703 if (!cp210x_gpioget(port
, &gpio
) && !copy_to_user(
704 (u8 __user
*)arg
, &gpio
, sizeof(gpio
)))
710 case CP210x_IOCTL_GPIOSET
:
711 if (cp210x_get_partnum(port
) == CP210x_PART_CP2103
&&
712 !cp210x_gpioset(port
, arg
))
717 case CP210x_IOCTL_GPIOBIC
:
718 case CP210x_IOCTL_GPIOBIS
:
719 if (cp210x_get_partnum(port
) == CP210x_PART_CP2103
&&
720 !cp210x_gpiosetb(port
,
721 (cmd
==CP210x_IOCTL_GPIOBIC
) ? 0 : arg
,
722 (cmd
==CP210x_IOCTL_GPIOBIC
) ? arg
: 0))
727 case CP210x_IOCTL_DEVICERESET
:
728 return cp210x_reset(port
);
731 case CP210x_IOCTL_PORTCONFGET
:
733 struct cp210x_port_config config
;
734 if (!cp210x_portconfget(port
, &config
) && !copy_to_user(
735 (struct cp210x_port_config __user
*)arg
,
736 &config
, sizeof(config
)))
742 case CP210x_IOCTL_PORTCONFSET
:
744 struct cp210x_port_config config
;
745 if (!copy_from_user(&config
, (struct cp210x_port_config __user
*)arg
,
747 !cp210x_portconfset(port
, &config
))
753 case CP210x_IOCTL_SETVID
:
756 if (!copy_from_user(&vid
, (unsigned int __user
*)arg
,
757 sizeof(unsigned int)) &&
758 !cp210x_setu16(port
, 0x01, vid
))
764 case CP210x_IOCTL_SETPID
:
767 if (!copy_from_user(&pid
, (unsigned int __user
*)arg
,
768 sizeof(unsigned int)) &&
769 !cp210x_setu16(port
, 0x02, pid
))
775 case CP210x_IOCTL_SETMFG
:
776 if (cp210x_has_setmfg()) {
777 char usbstr
[USBSTRLEN(CP210x_MAX_MFG_STRLEN
)];
778 size_t len
= cp210x_usbstr_from_user(usbstr
, arg
,
780 if (len
&& cp210x_setstr(port
, 0x00, usbstr
) == len
)
786 case CP210x_IOCTL_SETPRODUCT
:
788 char usbstr
[USBSTRLEN(CP210x_MAX_PRODUCT_STRLEN
)];
789 size_t len
= cp210x_usbstr_from_user(usbstr
, arg
,
791 if (len
&& cp210x_setstr(port
, 0x03, usbstr
) == len
)
797 case CP210x_IOCTL_SETSERIAL
:
799 char usbstr
[USBSTRLEN(CP210x_MAX_SERIAL_STRLEN
)];
800 size_t len
= cp210x_usbstr_from_user(usbstr
, arg
,
802 if (len
&& cp210x_setstr(port
, 0x04, usbstr
) == len
)
808 case CP210x_IOCTL_SETDEVVER
:
811 if (!copy_from_user(&ver
, (unsigned int __user
*)arg
,
812 sizeof(unsigned int)) &&
813 !cp210x_setu16(port
, 0x07, ver
))
820 dbg("%s not supported = 0x%04x", __FUNCTION__
, cmd
);
829 * Reads the baud rate, data bits, parity, stop bits and flow control mode
830 * from the device, corrects any unsupported values, and configures the
831 * termios structure to reflect the state of the device
833 static void cp210x_get_termios(struct usb_serial_port
*port
)
835 unsigned int cflag
, modem_ctl
[4];
839 dbg("%s - port %d", __FUNCTION__
, port
->number
);
841 if (!port
->tty
|| !port
->tty
->termios
) {
842 dbg("%s - no tty structures", __FUNCTION__
);
846 cp210x_get_config(port
, CP2101_BAUDRATE
, &baud
, 2);
847 /* Convert to baudrate */
849 baud
= BAUD_RATE_GEN_FREQ
/ baud
;
851 dbg("%s - baud rate = %d", __FUNCTION__
, baud
);
853 tty_encode_baud_rate(port
->tty
, baud
, baud
);
854 cflag
= port
->tty
->termios
->c_cflag
;
856 cp210x_get_config(port
, CP2101_BITS
, &bits
, 2);
858 switch(bits
& BITS_DATA_MASK
) {
860 dbg("%s - data bits = 5", __FUNCTION__
);
864 dbg("%s - data bits = 6", __FUNCTION__
);
868 dbg("%s - data bits = 7", __FUNCTION__
);
872 dbg("%s - data bits = 8", __FUNCTION__
);
876 dbg("%s - data bits = 9 (not supported, "
877 "using 8 data bits)", __FUNCTION__
);
879 bits
&= ~BITS_DATA_MASK
;
881 cp210x_set_config(port
, CP2101_BITS
, &bits
, 2);
884 dbg("%s - Unknown number of data bits, "
885 "using 8", __FUNCTION__
);
887 bits
&= ~BITS_DATA_MASK
;
889 cp210x_set_config(port
, CP2101_BITS
, &bits
, 2);
893 switch(bits
& BITS_PARITY_MASK
) {
894 case BITS_PARITY_NONE
:
895 dbg("%s - parity = NONE", __FUNCTION__
);
898 case BITS_PARITY_ODD
:
899 dbg("%s - parity = ODD", __FUNCTION__
);
900 cflag
|= (PARENB
|PARODD
);
902 case BITS_PARITY_EVEN
:
903 dbg("%s - parity = EVEN", __FUNCTION__
);
907 case BITS_PARITY_MARK
:
908 dbg("%s - parity = MARK (not supported, "
909 "disabling parity)", __FUNCTION__
);
911 bits
&= ~BITS_PARITY_MASK
;
912 cp210x_set_config(port
, CP2101_BITS
, &bits
, 2);
914 case BITS_PARITY_SPACE
:
915 dbg("%s - parity = SPACE (not supported, "
916 "disabling parity)", __FUNCTION__
);
918 bits
&= ~BITS_PARITY_MASK
;
919 cp210x_set_config(port
, CP2101_BITS
, &bits
, 2);
922 dbg("%s - Unknown parity mode, "
923 "disabling parity", __FUNCTION__
);
925 bits
&= ~BITS_PARITY_MASK
;
926 cp210x_set_config(port
, CP2101_BITS
, &bits
, 2);
931 switch(bits
& BITS_STOP_MASK
) {
933 dbg("%s - stop bits = 1", __FUNCTION__
);
936 dbg("%s - stop bits = 1.5 (not supported, "
937 "using 1 stop bit)", __FUNCTION__
);
938 bits
&= ~BITS_STOP_MASK
;
939 cp210x_set_config(port
, CP2101_BITS
, &bits
, 2);
942 dbg("%s - stop bits = 2", __FUNCTION__
);
946 dbg("%s - Unknown number of stop bits, "
947 "using 1 stop bit", __FUNCTION__
);
948 bits
&= ~BITS_STOP_MASK
;
949 cp210x_set_config(port
, CP2101_BITS
, &bits
, 2);
953 cp210x_get_config(port
, CP2101_MODEMCTL
, modem_ctl
, 16);
954 if (modem_ctl
[0] & 0x0008) {
955 dbg("%s - flow control = CRTSCTS", __FUNCTION__
);
958 dbg("%s - flow control = NONE", __FUNCTION__
);
962 port
->tty
->termios
->c_cflag
= cflag
;
965 static void cp210x_set_termios(struct usb_serial_port
*port
,
966 struct ktermios
*old_termios
)
968 unsigned int cflag
, old_cflag
;
970 unsigned int modem_ctl
[4];
972 dbg("%s - port %d", __FUNCTION__
, port
->number
);
974 if (!port
->tty
|| !port
->tty
->termios
) {
975 dbg("%s - no tty structures", __FUNCTION__
);
978 port
->tty
->termios
->c_cflag
&= ~CMSPAR
;
980 cflag
= port
->tty
->termios
->c_cflag
;
981 old_cflag
= old_termios
->c_cflag
;
982 baud
= tty_get_baud_rate(port
->tty
);
984 /* If the baud rate is to be updated*/
985 if (baud
!= tty_termios_baud_rate(old_termios
)) {
1014 dbg("%s - Setting baud rate to %d baud", __FUNCTION__
,
1016 if (cp210x_set_config_single(port
, CP2101_BAUDRATE
,
1017 (BAUD_RATE_GEN_FREQ
/ baud
))) {
1018 dev_err(&port
->dev
, "Baud rate requested not "
1019 "supported by device\n");
1020 baud
= tty_termios_baud_rate(old_termios
);
1024 /* Report back the resulting baud rate */
1025 tty_encode_baud_rate(port
->tty
, baud
, baud
);
1027 /* If the number of data bits is to be updated */
1028 if ((cflag
& CSIZE
) != (old_cflag
& CSIZE
)) {
1029 cp210x_get_config(port
, CP2101_BITS
, &bits
, 2);
1030 bits
&= ~BITS_DATA_MASK
;
1031 switch (cflag
& CSIZE
) {
1033 bits
|= BITS_DATA_5
;
1034 dbg("%s - data bits = 5", __FUNCTION__
);
1037 bits
|= BITS_DATA_6
;
1038 dbg("%s - data bits = 6", __FUNCTION__
);
1041 bits
|= BITS_DATA_7
;
1042 dbg("%s - data bits = 7", __FUNCTION__
);
1045 bits
|= BITS_DATA_8
;
1046 dbg("%s - data bits = 8", __FUNCTION__
);
1049 bits |= BITS_DATA_9;
1050 dbg("%s - data bits = 9", __FUNCTION__);
1053 dev_err(&port
->dev
, "cp210x driver does not "
1054 "support the number of bits requested,"
1055 " using 8 bit mode\n");
1056 bits
|= BITS_DATA_8
;
1059 if (cp210x_set_config(port
, CP2101_BITS
, &bits
, 2))
1060 dev_err(&port
->dev
, "Number of data bits requested "
1061 "not supported by device\n");
1064 if ((cflag
& (PARENB
|PARODD
)) != (old_cflag
& (PARENB
|PARODD
))) {
1065 cp210x_get_config(port
, CP2101_BITS
, &bits
, 2);
1066 bits
&= ~BITS_PARITY_MASK
;
1067 if (cflag
& PARENB
) {
1068 if (cflag
& PARODD
) {
1069 bits
|= BITS_PARITY_ODD
;
1070 dbg("%s - parity = ODD", __FUNCTION__
);
1072 bits
|= BITS_PARITY_EVEN
;
1073 dbg("%s - parity = EVEN", __FUNCTION__
);
1076 if (cp210x_set_config(port
, CP2101_BITS
, &bits
, 2))
1077 dev_err(&port
->dev
, "Parity mode not supported "
1081 if ((cflag
& CSTOPB
) != (old_cflag
& CSTOPB
)) {
1082 cp210x_get_config(port
, CP2101_BITS
, &bits
, 2);
1083 bits
&= ~BITS_STOP_MASK
;
1084 if (cflag
& CSTOPB
) {
1085 bits
|= BITS_STOP_2
;
1086 dbg("%s - stop bits = 2", __FUNCTION__
);
1088 bits
|= BITS_STOP_1
;
1089 dbg("%s - stop bits = 1", __FUNCTION__
);
1091 if (cp210x_set_config(port
, CP2101_BITS
, &bits
, 2))
1092 dev_err(&port
->dev
, "Number of stop bits requested "
1093 "not supported by device\n");
1096 if ((cflag
& CRTSCTS
) != (old_cflag
& CRTSCTS
)) {
1097 cp210x_get_config(port
, CP2101_MODEMCTL
, modem_ctl
, 16);
1098 dbg("%s - read modem controls = 0x%.4x 0x%.4x 0x%.4x 0x%.4x",
1099 __FUNCTION__
, modem_ctl
[0], modem_ctl
[1],
1100 modem_ctl
[2], modem_ctl
[3]);
1102 if (cflag
& CRTSCTS
) {
1103 modem_ctl
[0] &= ~0x7B;
1104 modem_ctl
[0] |= 0x09;
1105 modem_ctl
[1] = 0x80;
1106 dbg("%s - flow control = CRTSCTS", __FUNCTION__
);
1108 modem_ctl
[0] &= ~0x7B;
1109 modem_ctl
[0] |= 0x01;
1110 modem_ctl
[1] |= 0x40;
1111 dbg("%s - flow control = NONE", __FUNCTION__
);
1114 dbg("%s - write modem controls = 0x%.4x 0x%.4x 0x%.4x 0x%.4x",
1115 __FUNCTION__
, modem_ctl
[0], modem_ctl
[1],
1116 modem_ctl
[2], modem_ctl
[3]);
1117 cp210x_set_config(port
, CP2101_MODEMCTL
, modem_ctl
, 16);
1122 static int cp210x_tiocmset(struct usb_serial_port
*port
, struct file
*file
,
1123 unsigned int set
, unsigned int clear
)
1127 dbg("%s - port %d", __FUNCTION__
, port
->number
);
1129 if (set
& TIOCM_RTS
) {
1130 control
|= CONTROL_RTS
;
1131 control
|= CONTROL_WRITE_RTS
;
1133 if (set
& TIOCM_DTR
) {
1134 control
|= CONTROL_DTR
;
1135 control
|= CONTROL_WRITE_DTR
;
1137 if (clear
& TIOCM_RTS
) {
1138 control
&= ~CONTROL_RTS
;
1139 control
|= CONTROL_WRITE_RTS
;
1141 if (clear
& TIOCM_DTR
) {
1142 control
&= ~CONTROL_DTR
;
1143 control
|= CONTROL_WRITE_DTR
;
1146 dbg("%s - control = 0x%.4x", __FUNCTION__
, control
);
1148 return cp210x_set_config(port
, CP2101_CONTROL
, &control
, 2);
1152 static int cp210x_tiocmget(struct usb_serial_port
*port
, struct file
*file
)
1154 int control
, result
;
1156 dbg("%s - port %d", __FUNCTION__
, port
->number
);
1158 cp210x_get_config(port
, CP2101_CONTROL
, &control
, 1);
1160 result
= ((control
& CONTROL_DTR
) ? TIOCM_DTR
: 0)
1161 |((control
& CONTROL_RTS
) ? TIOCM_RTS
: 0)
1162 |((control
& CONTROL_CTS
) ? TIOCM_CTS
: 0)
1163 |((control
& CONTROL_DSR
) ? TIOCM_DSR
: 0)
1164 |((control
& CONTROL_RING
)? TIOCM_RI
: 0)
1165 |((control
& CONTROL_DCD
) ? TIOCM_CD
: 0);
1167 dbg("%s - control = 0x%.2x", __FUNCTION__
, control
);
1172 static void cp210x_break_ctl(struct usb_serial_port
*port
, int break_state
)
1176 dbg("%s - port %d", __FUNCTION__
, port
->number
);
1177 if (break_state
== 0)
1181 dbg("%s - turning break %s", __FUNCTION__
,
1182 state
==BREAK_OFF
? "off" : "on");
1183 cp210x_set_config(port
, CP2101_BREAK
, &state
, 2);
1186 static int cp210x_startup(struct usb_serial
*serial
)
1188 /* CP2101 buffers behave strangely unless device is reset */
1189 usb_reset_device(serial
->dev
);
1193 static void cp210x_shutdown(struct usb_serial
*serial
)
1197 dbg("%s", __FUNCTION__
);
1199 /* Stop reads and writes on all ports */
1200 for (i
= 0; i
< serial
->num_ports
; ++i
) {
1201 cp210x_cleanup(serial
->port
[i
]);
1205 static int __init
cp210x_init(void)
1209 retval
= usb_serial_register(&cp210x_device
);
1211 return retval
; /* Failed to register */
1213 retval
= usb_register(&cp210x_driver
);
1215 /* Failed to register */
1216 usb_serial_deregister(&cp210x_device
);
1221 info(DRIVER_DESC
" " DRIVER_VERSION
);
1225 static void __exit
cp210x_exit(void)
1227 usb_deregister(&cp210x_driver
);
1228 usb_serial_deregister(&cp210x_device
);
1231 module_init(cp210x_init
);
1232 module_exit(cp210x_exit
);
1234 MODULE_DESCRIPTION(DRIVER_DESC
);
1235 MODULE_VERSION(DRIVER_VERSION
);
1236 MODULE_LICENSE("GPL");
1238 module_param(debug
, bool, S_IRUGO
| S_IWUSR
);
1239 MODULE_PARM_DESC(debug
, "Enable verbose debugging messages");