blob: 2f9b7ac326633a6d6835164d52c7a9c00648e7a2 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Silicon Laboratories CP2101/CP2102 USB to RS232 serial adaptor driver
3 *
4 * Copyright (C) 2005 Craig Shelley (craig@microtron.org.uk)
5 *
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.
9 *
Craig Shelley39a66b82005-05-27 00:09:56 +010010 * 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
13 *
14 * Outstanding Issues:
15 * Buffers are not flushed when the port is opened.
16 * Multiple calls to write() may fail with "Resource temporarily unavailable"
17 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 */
19
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/kernel.h>
21#include <linux/errno.h>
22#include <linux/slab.h>
23#include <linux/tty.h>
24#include <linux/tty_flip.h>
25#include <linux/module.h>
26#include <linux/moduleparam.h>
27#include <linux/usb.h>
28#include <asm/uaccess.h>
Greg Kroah-Hartmana9698882006-07-11 21:22:58 -070029#include <linux/usb/serial.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31/*
32 * Version Information
33 */
Craig Shelley59224f52006-03-11 11:29:02 +000034#define DRIVER_VERSION "v0.07"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#define DRIVER_DESC "Silicon Labs CP2101/CP2102 RS232 serial adaptor driver"
36
37/*
38 * Function Prototypes
39 */
40static int cp2101_open(struct usb_serial_port*, struct file*);
41static void cp2101_cleanup(struct usb_serial_port*);
42static void cp2101_close(struct usb_serial_port*, struct file*);
43static void cp2101_get_termios(struct usb_serial_port*);
Alan Cox606d0992006-12-08 02:38:45 -080044static void cp2101_set_termios(struct usb_serial_port*, struct ktermios*);
Craig Shelley39a66b82005-05-27 00:09:56 +010045static int cp2101_tiocmget (struct usb_serial_port *, struct file *);
46static int cp2101_tiocmset (struct usb_serial_port *, struct file *,
47 unsigned int, unsigned int);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048static void cp2101_break_ctl(struct usb_serial_port*, int);
49static int cp2101_startup (struct usb_serial *);
50static void cp2101_shutdown(struct usb_serial*);
51
52
53static int debug;
54
55static struct usb_device_id id_table [] = {
Craig Shelley198b9512005-08-28 09:51:15 +010056 { USB_DEVICE(0x0FCF, 0x1003) }, /* Dynastream ANT development board */
Craig Shelley198b9512005-08-28 09:51:15 +010057 { USB_DEVICE(0x10A6, 0xAA26) }, /* Knock-off DCU-11 cable */
Craig Shelleye988fc82006-01-20 00:06:19 +000058 { USB_DEVICE(0x10AB, 0x10C5) }, /* Siemens MC60 Cable */
59 { USB_DEVICE(0x10B5, 0xAC70) }, /* Nokia CA-42 USB */
Craig Shelley59224f52006-03-11 11:29:02 +000060 { USB_DEVICE(0x10C4, 0x803B) }, /* Pololu USB-serial converter */
Vitja Makarov212a4b42006-05-31 00:40:06 +040061 { USB_DEVICE(0x10C4, 0x8066) }, /* Argussoft In-System Programmer */
Craig Shelleye988fc82006-01-20 00:06:19 +000062 { USB_DEVICE(0x10C4, 0x807A) }, /* Crumb128 board */
63 { USB_DEVICE(0x10C4, 0x80CA) }, /* Degree Controls Inc */
64 { USB_DEVICE(0x10C4, 0x80F6) }, /* Suunto sports instrument */
65 { USB_DEVICE(0x10C4, 0x813D) }, /* Burnside Telecom Deskmobile */
66 { USB_DEVICE(0x10C4, 0x815E) }, /* Helicomm IP-Link 1220-DVM */
Bjorn Schneider78001e32006-10-28 12:42:04 +020067 { USB_DEVICE(0x10C4, 0x81C8) }, /* Lipowsky Industrie Elektronik GmbH, Baby-JTAG */
68 { USB_DEVICE(0x10C4, 0x81E2) }, /* Lipowsky Industrie Elektronik GmbH, Baby-LIN */
69 { USB_DEVICE(0x10C4, 0x8218) }, /* Lipowsky Industrie Elektronik GmbH, HARP-1 */
Craig Shelleye988fc82006-01-20 00:06:19 +000070 { USB_DEVICE(0x10C4, 0xEA60) }, /* Silicon Labs factory default */
Craig Shelley61926b92006-10-12 22:09:56 +010071 { USB_DEVICE(0x10C4, 0xEA61) }, /* Silicon Labs factory default */
Josef Balatkab0ce84d2005-11-17 09:47:24 -080072 { USB_DEVICE(0x16D6, 0x0001) }, /* Jablotron serial interface */
Craig Shelley39a66b82005-05-27 00:09:56 +010073 { } /* Terminating Entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -070074};
75
76MODULE_DEVICE_TABLE (usb, id_table);
77
78static struct usb_driver cp2101_driver = {
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -070079 .name = "cp2101",
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 .probe = usb_serial_probe,
81 .disconnect = usb_serial_disconnect,
82 .id_table = id_table,
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -080083 .no_dynamic_id = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -070084};
85
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -070086static struct usb_serial_driver cp2101_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -070087 .driver = {
88 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -070089 .name = "cp2101",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -070090 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 .id_table = id_table,
92 .num_interrupt_in = 0,
93 .num_bulk_in = 0,
94 .num_bulk_out = 0,
95 .num_ports = 1,
96 .open = cp2101_open,
97 .close = cp2101_close,
98 .break_ctl = cp2101_break_ctl,
99 .set_termios = cp2101_set_termios,
Craig Shelley39a66b82005-05-27 00:09:56 +0100100 .tiocmget = cp2101_tiocmget,
101 .tiocmset = cp2101_tiocmset,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 .attach = cp2101_startup,
103 .shutdown = cp2101_shutdown,
104};
105
Craig Shelley39a66b82005-05-27 00:09:56 +0100106/* Config request types */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107#define REQTYPE_HOST_TO_DEVICE 0x41
108#define REQTYPE_DEVICE_TO_HOST 0xc1
109
Craig Shelley39a66b82005-05-27 00:09:56 +0100110/* Config SET requests. To GET, add 1 to the request number */
111#define CP2101_UART 0x00 /* Enable / Disable */
112#define CP2101_BAUDRATE 0x01 /* (BAUD_RATE_GEN_FREQ / baudrate) */
113#define CP2101_BITS 0x03 /* 0x(0)(databits)(parity)(stopbits) */
114#define CP2101_BREAK 0x05 /* On / Off */
115#define CP2101_CONTROL 0x07 /* Flow control line states */
116#define CP2101_MODEMCTL 0x13 /* Modem controls */
117#define CP2101_CONFIG_6 0x19 /* 6 bytes of config data ??? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Craig Shelley39a66b82005-05-27 00:09:56 +0100119/* CP2101_UART */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120#define UART_ENABLE 0x0001
121#define UART_DISABLE 0x0000
122
Craig Shelley39a66b82005-05-27 00:09:56 +0100123/* CP2101_BAUDRATE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124#define BAUD_RATE_GEN_FREQ 0x384000
125
Craig Shelley39a66b82005-05-27 00:09:56 +0100126/* CP2101_BITS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127#define BITS_DATA_MASK 0X0f00
Craig Shelley39a66b82005-05-27 00:09:56 +0100128#define BITS_DATA_5 0X0500
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129#define BITS_DATA_6 0X0600
130#define BITS_DATA_7 0X0700
131#define BITS_DATA_8 0X0800
132#define BITS_DATA_9 0X0900
133
134#define BITS_PARITY_MASK 0x00f0
135#define BITS_PARITY_NONE 0x0000
136#define BITS_PARITY_ODD 0x0010
137#define BITS_PARITY_EVEN 0x0020
138#define BITS_PARITY_MARK 0x0030
139#define BITS_PARITY_SPACE 0x0040
140
141#define BITS_STOP_MASK 0x000f
142#define BITS_STOP_1 0x0000
143#define BITS_STOP_1_5 0x0001
144#define BITS_STOP_2 0x0002
Craig Shelley39a66b82005-05-27 00:09:56 +0100145
146/* CP2101_BREAK */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147#define BREAK_ON 0x0000
148#define BREAK_OFF 0x0001
149
Craig Shelley39a66b82005-05-27 00:09:56 +0100150/* CP2101_CONTROL */
151#define CONTROL_DTR 0x0001
152#define CONTROL_RTS 0x0002
153#define CONTROL_CTS 0x0010
154#define CONTROL_DSR 0x0020
155#define CONTROL_RING 0x0040
156#define CONTROL_DCD 0x0080
157#define CONTROL_WRITE_DTR 0x0100
158#define CONTROL_WRITE_RTS 0x0200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
Craig Shelley39a66b82005-05-27 00:09:56 +0100160/*
161 * cp2101_get_config
162 * Reads from the CP2101 configuration registers
163 * 'size' is specified in bytes.
164 * 'data' is a pointer to a pre-allocated array of integers large
165 * enough to hold 'size' bytes (with 4 bytes to each integer)
166 */
167static int cp2101_get_config(struct usb_serial_port* port, u8 request,
168 unsigned int *data, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169{
170 struct usb_serial *serial = port->serial;
Craig Shelley39a66b82005-05-27 00:09:56 +0100171 u32 *buf;
172 int result, i, length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Craig Shelley39a66b82005-05-27 00:09:56 +0100174 /* Number of integers required to contain the array */
175 length = (((size - 1) | 3) + 1)/4;
176
Eric Sesterhenn80b6ca42006-02-27 21:29:43 +0100177 buf = kcalloc(length, sizeof(u32), GFP_KERNEL);
Craig Shelley39a66b82005-05-27 00:09:56 +0100178 if (!buf) {
179 dev_err(&port->dev, "%s - out of memory.\n", __FUNCTION__);
180 return -ENOMEM;
181 }
182
183 /* For get requests, the request number must be incremented */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 request++;
185
Craig Shelley39a66b82005-05-27 00:09:56 +0100186 /* Issue the request, attempting to read 'size' bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 result = usb_control_msg (serial->dev,usb_rcvctrlpipe (serial->dev, 0),
188 request, REQTYPE_DEVICE_TO_HOST, 0x0000,
Craig Shelley39a66b82005-05-27 00:09:56 +0100189 0, buf, size, 300);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Craig Shelley39a66b82005-05-27 00:09:56 +0100191 /* Convert data into an array of integers */
192 for (i=0; i<length; i++)
193 data[i] = le32_to_cpu(buf[i]);
194
195 kfree(buf);
196
197 if (result != size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 dev_err(&port->dev, "%s - Unable to send config request, "
Craig Shelley39a66b82005-05-27 00:09:56 +0100199 "request=0x%x size=%d result=%d\n",
200 __FUNCTION__, request, size, result);
201 return -EPROTO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 }
203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 return 0;
205}
206
Craig Shelley39a66b82005-05-27 00:09:56 +0100207/*
208 * cp2101_set_config
209 * Writes to the CP2101 configuration registers
210 * Values less than 16 bits wide are sent directly
211 * 'size' is specified in bytes.
212 */
213static int cp2101_set_config(struct usb_serial_port* port, u8 request,
214 unsigned int *data, int size)
215{
216 struct usb_serial *serial = port->serial;
217 u32 *buf;
218 int result, i, length;
219
220 /* Number of integers required to contain the array */
221 length = (((size - 1) | 3) + 1)/4;
222
223 buf = kmalloc(length * sizeof(u32), GFP_KERNEL);
224 if (!buf) {
225 dev_err(&port->dev, "%s - out of memory.\n",
226 __FUNCTION__);
227 return -ENOMEM;
228 }
229
230 /* Array of integers into bytes */
231 for (i = 0; i < length; i++)
232 buf[i] = cpu_to_le32(data[i]);
233
234 if (size > 2) {
235 result = usb_control_msg (serial->dev,
236 usb_sndctrlpipe(serial->dev, 0),
237 request, REQTYPE_HOST_TO_DEVICE, 0x0000,
238 0, buf, size, 300);
239 } else {
240 result = usb_control_msg (serial->dev,
241 usb_sndctrlpipe(serial->dev, 0),
242 request, REQTYPE_HOST_TO_DEVICE, data[0],
243 0, NULL, 0, 300);
244 }
245
246 kfree(buf);
247
248 if ((size > 2 && result != size) || result < 0) {
249 dev_err(&port->dev, "%s - Unable to send request, "
250 "request=0x%x size=%d result=%d\n",
251 __FUNCTION__, request, size, result);
252 return -EPROTO;
253 }
254
255 /* Single data value */
256 result = usb_control_msg (serial->dev,
257 usb_sndctrlpipe(serial->dev, 0),
258 request, REQTYPE_HOST_TO_DEVICE, data[0],
259 0, NULL, 0, 300);
260 return 0;
261}
262
263/*
264 * cp2101_set_config_single
265 * Convenience function for calling cp2101_set_config on single data values
266 * without requiring an integer pointer
267 */
268static inline int cp2101_set_config_single(struct usb_serial_port* port,
269 u8 request, unsigned int data)
270{
271 return cp2101_set_config(port, request, &data, 2);
272}
273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274static int cp2101_open (struct usb_serial_port *port, struct file *filp)
275{
276 struct usb_serial *serial = port->serial;
277 int result;
278
279 dbg("%s - port %d", __FUNCTION__, port->number);
280
Craig Shelley39a66b82005-05-27 00:09:56 +0100281 if (cp2101_set_config_single(port, CP2101_UART, UART_ENABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 dev_err(&port->dev, "%s - Unable to enable UART\n",
283 __FUNCTION__);
284 return -EPROTO;
285 }
286
287 /* Start reading from the device */
288 usb_fill_bulk_urb (port->read_urb, serial->dev,
289 usb_rcvbulkpipe(serial->dev,
290 port->bulk_in_endpointAddress),
291 port->read_urb->transfer_buffer,
292 port->read_urb->transfer_buffer_length,
293 serial->type->read_bulk_callback,
294 port);
295 result = usb_submit_urb(port->read_urb, GFP_KERNEL);
296 if (result) {
297 dev_err(&port->dev, "%s - failed resubmitting read urb, "
298 "error %d\n", __FUNCTION__, result);
299 return result;
300 }
301
Craig Shelley39a66b82005-05-27 00:09:56 +0100302 /* Configure the termios structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 cp2101_get_termios(port);
304
Craig Shelley39a66b82005-05-27 00:09:56 +0100305 /* Set the DTR and RTS pins low */
306 cp2101_tiocmset(port, NULL, TIOCM_DTR | TIOCM_RTS, 0);
307
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 return 0;
309}
310
311static void cp2101_cleanup (struct usb_serial_port *port)
312{
313 struct usb_serial *serial = port->serial;
314
315 dbg("%s - port %d", __FUNCTION__, port->number);
316
317 if (serial->dev) {
318 /* shutdown any bulk reads that might be going on */
319 if (serial->num_bulk_out)
320 usb_kill_urb(port->write_urb);
321 if (serial->num_bulk_in)
322 usb_kill_urb(port->read_urb);
323 }
324}
325
326static void cp2101_close (struct usb_serial_port *port, struct file * filp)
327{
328 dbg("%s - port %d", __FUNCTION__, port->number);
329
330 /* shutdown our urbs */
331 dbg("%s - shutting down urbs", __FUNCTION__);
332 usb_kill_urb(port->write_urb);
333 usb_kill_urb(port->read_urb);
334
Craig Shelley39a66b82005-05-27 00:09:56 +0100335 cp2101_set_config_single(port, CP2101_UART, UART_DISABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336}
337
Craig Shelley39a66b82005-05-27 00:09:56 +0100338/*
339 * cp2101_get_termios
340 * Reads the baud rate, data bits, parity, stop bits and flow control mode
341 * from the device, corrects any unsupported values, and configures the
342 * termios structure to reflect the state of the device
343 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344static void cp2101_get_termios (struct usb_serial_port *port)
345{
Craig Shelley39a66b82005-05-27 00:09:56 +0100346 unsigned int cflag, modem_ctl[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 int baud;
348 int bits;
349
350 dbg("%s - port %d", __FUNCTION__, port->number);
351
352 if ((!port->tty) || (!port->tty->termios)) {
353 dbg("%s - no tty structures", __FUNCTION__);
354 return;
355 }
356 cflag = port->tty->termios->c_cflag;
357
Craig Shelley39a66b82005-05-27 00:09:56 +0100358 cp2101_get_config(port, CP2101_BAUDRATE, &baud, 2);
359 /* Convert to baudrate */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 if (baud)
361 baud = BAUD_RATE_GEN_FREQ / baud;
362
363 dbg("%s - baud rate = %d", __FUNCTION__, baud);
364 cflag &= ~CBAUD;
365 switch (baud) {
Craig Shelley39a66b82005-05-27 00:09:56 +0100366 /*
367 * The baud rates which are commented out below
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 * appear to be supported by the device
369 * but are non-standard
370 */
371 case 600: cflag |= B600; break;
372 case 1200: cflag |= B1200; break;
373 case 1800: cflag |= B1800; break;
374 case 2400: cflag |= B2400; break;
375 case 4800: cflag |= B4800; break;
376 /*case 7200: cflag |= B7200; break;*/
377 case 9600: cflag |= B9600; break;
378 /*case 14400: cflag |= B14400; break;*/
379 case 19200: cflag |= B19200; break;
380 /*case 28800: cflag |= B28800; break;*/
381 case 38400: cflag |= B38400; break;
382 /*case 55854: cflag |= B55054; break;*/
383 case 57600: cflag |= B57600; break;
384 case 115200: cflag |= B115200; break;
385 /*case 127117: cflag |= B127117; break;*/
386 case 230400: cflag |= B230400; break;
387 case 460800: cflag |= B460800; break;
388 case 921600: cflag |= B921600; break;
389 /*case 3686400: cflag |= B3686400; break;*/
390 default:
391 dbg("%s - Baud rate is not supported, "
392 "using 9600 baud", __FUNCTION__);
393 cflag |= B9600;
Craig Shelley39a66b82005-05-27 00:09:56 +0100394 cp2101_set_config_single(port, CP2101_BAUDRATE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 (BAUD_RATE_GEN_FREQ/9600));
396 break;
397 }
398
Craig Shelley39a66b82005-05-27 00:09:56 +0100399 cp2101_get_config(port, CP2101_BITS, &bits, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 cflag &= ~CSIZE;
401 switch(bits & BITS_DATA_MASK) {
Craig Shelley39a66b82005-05-27 00:09:56 +0100402 case BITS_DATA_5:
403 dbg("%s - data bits = 5", __FUNCTION__);
404 cflag |= CS5;
405 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 case BITS_DATA_6:
407 dbg("%s - data bits = 6", __FUNCTION__);
408 cflag |= CS6;
409 break;
410 case BITS_DATA_7:
411 dbg("%s - data bits = 7", __FUNCTION__);
412 cflag |= CS7;
413 break;
414 case BITS_DATA_8:
415 dbg("%s - data bits = 8", __FUNCTION__);
416 cflag |= CS8;
417 break;
418 case BITS_DATA_9:
419 dbg("%s - data bits = 9 (not supported, "
420 "using 8 data bits)", __FUNCTION__);
421 cflag |= CS8;
422 bits &= ~BITS_DATA_MASK;
423 bits |= BITS_DATA_8;
Craig Shelley39a66b82005-05-27 00:09:56 +0100424 cp2101_set_config(port, CP2101_BITS, &bits, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 break;
426 default:
427 dbg("%s - Unknown number of data bits, "
428 "using 8", __FUNCTION__);
429 cflag |= CS8;
430 bits &= ~BITS_DATA_MASK;
431 bits |= BITS_DATA_8;
Craig Shelley39a66b82005-05-27 00:09:56 +0100432 cp2101_set_config(port, CP2101_BITS, &bits, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 break;
434 }
435
436 switch(bits & BITS_PARITY_MASK) {
437 case BITS_PARITY_NONE:
438 dbg("%s - parity = NONE", __FUNCTION__);
439 cflag &= ~PARENB;
440 break;
441 case BITS_PARITY_ODD:
442 dbg("%s - parity = ODD", __FUNCTION__);
443 cflag |= (PARENB|PARODD);
444 break;
445 case BITS_PARITY_EVEN:
446 dbg("%s - parity = EVEN", __FUNCTION__);
447 cflag &= ~PARODD;
448 cflag |= PARENB;
449 break;
450 case BITS_PARITY_MARK:
451 dbg("%s - parity = MARK (not supported, "
452 "disabling parity)", __FUNCTION__);
453 cflag &= ~PARENB;
454 bits &= ~BITS_PARITY_MASK;
Craig Shelley39a66b82005-05-27 00:09:56 +0100455 cp2101_set_config(port, CP2101_BITS, &bits, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 break;
457 case BITS_PARITY_SPACE:
458 dbg("%s - parity = SPACE (not supported, "
459 "disabling parity)", __FUNCTION__);
460 cflag &= ~PARENB;
461 bits &= ~BITS_PARITY_MASK;
Craig Shelley39a66b82005-05-27 00:09:56 +0100462 cp2101_set_config(port, CP2101_BITS, &bits, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 break;
464 default:
465 dbg("%s - Unknown parity mode, "
466 "disabling parity", __FUNCTION__);
467 cflag &= ~PARENB;
468 bits &= ~BITS_PARITY_MASK;
Craig Shelley39a66b82005-05-27 00:09:56 +0100469 cp2101_set_config(port, CP2101_BITS, &bits, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 break;
471 }
472
473 cflag &= ~CSTOPB;
474 switch(bits & BITS_STOP_MASK) {
475 case BITS_STOP_1:
476 dbg("%s - stop bits = 1", __FUNCTION__);
477 break;
478 case BITS_STOP_1_5:
479 dbg("%s - stop bits = 1.5 (not supported, "
Craig Shelley39a66b82005-05-27 00:09:56 +0100480 "using 1 stop bit)", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 bits &= ~BITS_STOP_MASK;
Craig Shelley39a66b82005-05-27 00:09:56 +0100482 cp2101_set_config(port, CP2101_BITS, &bits, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 break;
484 case BITS_STOP_2:
485 dbg("%s - stop bits = 2", __FUNCTION__);
486 cflag |= CSTOPB;
487 break;
488 default:
489 dbg("%s - Unknown number of stop bits, "
490 "using 1 stop bit", __FUNCTION__);
491 bits &= ~BITS_STOP_MASK;
Craig Shelley39a66b82005-05-27 00:09:56 +0100492 cp2101_set_config(port, CP2101_BITS, &bits, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 break;
494 }
495
Craig Shelley39a66b82005-05-27 00:09:56 +0100496 cp2101_get_config(port, CP2101_MODEMCTL, modem_ctl, 16);
497 if (modem_ctl[0] & 0x0008) {
498 dbg("%s - flow control = CRTSCTS", __FUNCTION__);
499 cflag |= CRTSCTS;
500 } else {
501 dbg("%s - flow control = NONE", __FUNCTION__);
502 cflag &= ~CRTSCTS;
503 }
504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 port->tty->termios->c_cflag = cflag;
506}
507
508static void cp2101_set_termios (struct usb_serial_port *port,
Alan Cox606d0992006-12-08 02:38:45 -0800509 struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510{
511 unsigned int cflag, old_cflag=0;
Craig Shelley39a66b82005-05-27 00:09:56 +0100512 int baud=0, bits;
513 unsigned int modem_ctl[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
515 dbg("%s - port %d", __FUNCTION__, port->number);
516
517 if ((!port->tty) || (!port->tty->termios)) {
518 dbg("%s - no tty structures", __FUNCTION__);
519 return;
520 }
521 cflag = port->tty->termios->c_cflag;
522
Craig Shelley39a66b82005-05-27 00:09:56 +0100523 /* Check that they really want us to change something */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 if (old_termios) {
525 if ((cflag == old_termios->c_cflag) &&
526 (RELEVANT_IFLAG(port->tty->termios->c_iflag)
527 == RELEVANT_IFLAG(old_termios->c_iflag))) {
528 dbg("%s - nothing to change...", __FUNCTION__);
529 return;
530 }
531
532 old_cflag = old_termios->c_cflag;
533 }
534
535 /* If the baud rate is to be updated*/
536 if ((cflag & CBAUD) != (old_cflag & CBAUD)) {
537 switch (cflag & CBAUD) {
Craig Shelley39a66b82005-05-27 00:09:56 +0100538 /*
539 * The baud rates which are commented out below
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 * appear to be supported by the device
541 * but are non-standard
542 */
543 case B0: baud = 0; break;
544 case B600: baud = 600; break;
545 case B1200: baud = 1200; break;
546 case B1800: baud = 1800; break;
547 case B2400: baud = 2400; break;
548 case B4800: baud = 4800; break;
549 /*case B7200: baud = 7200; break;*/
550 case B9600: baud = 9600; break;
551 /*ase B14400: baud = 14400; break;*/
552 case B19200: baud = 19200; break;
553 /*case B28800: baud = 28800; break;*/
554 case B38400: baud = 38400; break;
555 /*case B55854: baud = 55054; break;*/
556 case B57600: baud = 57600; break;
557 case B115200: baud = 115200; break;
558 /*case B127117: baud = 127117; break;*/
559 case B230400: baud = 230400; break;
560 case B460800: baud = 460800; break;
561 case B921600: baud = 921600; break;
562 /*case B3686400: baud = 3686400; break;*/
563 default:
564 dev_err(&port->dev, "cp2101 driver does not "
565 "support the baudrate requested\n");
566 break;
567 }
568
569 if (baud) {
570 dbg("%s - Setting baud rate to %d baud", __FUNCTION__,
571 baud);
Craig Shelley39a66b82005-05-27 00:09:56 +0100572 if (cp2101_set_config_single(port, CP2101_BAUDRATE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 (BAUD_RATE_GEN_FREQ / baud)))
574 dev_err(&port->dev, "Baud rate requested not "
575 "supported by device\n");
576 }
577 }
578
Craig Shelley39a66b82005-05-27 00:09:56 +0100579 /* If the number of data bits is to be updated */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 if ((cflag & CSIZE) != (old_cflag & CSIZE)) {
Craig Shelley39a66b82005-05-27 00:09:56 +0100581 cp2101_get_config(port, CP2101_BITS, &bits, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 bits &= ~BITS_DATA_MASK;
583 switch (cflag & CSIZE) {
Craig Shelley39a66b82005-05-27 00:09:56 +0100584 case CS5:
585 bits |= BITS_DATA_5;
586 dbg("%s - data bits = 5", __FUNCTION__);
587 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 case CS6:
589 bits |= BITS_DATA_6;
590 dbg("%s - data bits = 6", __FUNCTION__);
591 break;
592 case CS7:
593 bits |= BITS_DATA_7;
594 dbg("%s - data bits = 7", __FUNCTION__);
595 break;
596 case CS8:
597 bits |= BITS_DATA_8;
598 dbg("%s - data bits = 8", __FUNCTION__);
599 break;
600 /*case CS9:
601 bits |= BITS_DATA_9;
602 dbg("%s - data bits = 9", __FUNCTION__);
603 break;*/
604 default:
605 dev_err(&port->dev, "cp2101 driver does not "
606 "support the number of bits requested,"
607 " using 8 bit mode\n");
608 bits |= BITS_DATA_8;
609 break;
610 }
Craig Shelley39a66b82005-05-27 00:09:56 +0100611 if (cp2101_set_config(port, CP2101_BITS, &bits, 2))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 dev_err(&port->dev, "Number of data bits requested "
613 "not supported by device\n");
614 }
615
616 if ((cflag & (PARENB|PARODD)) != (old_cflag & (PARENB|PARODD))) {
Craig Shelley39a66b82005-05-27 00:09:56 +0100617 cp2101_get_config(port, CP2101_BITS, &bits, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 bits &= ~BITS_PARITY_MASK;
619 if (cflag & PARENB) {
620 if (cflag & PARODD) {
621 bits |= BITS_PARITY_ODD;
622 dbg("%s - parity = ODD", __FUNCTION__);
623 } else {
624 bits |= BITS_PARITY_EVEN;
625 dbg("%s - parity = EVEN", __FUNCTION__);
626 }
627 }
Craig Shelley39a66b82005-05-27 00:09:56 +0100628 if (cp2101_set_config(port, CP2101_BITS, &bits, 2))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 dev_err(&port->dev, "Parity mode not supported "
630 "by device\n");
631 }
632
633 if ((cflag & CSTOPB) != (old_cflag & CSTOPB)) {
Craig Shelley39a66b82005-05-27 00:09:56 +0100634 cp2101_get_config(port, CP2101_BITS, &bits, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 bits &= ~BITS_STOP_MASK;
636 if (cflag & CSTOPB) {
637 bits |= BITS_STOP_2;
638 dbg("%s - stop bits = 2", __FUNCTION__);
639 } else {
640 bits |= BITS_STOP_1;
641 dbg("%s - stop bits = 1", __FUNCTION__);
642 }
Craig Shelley39a66b82005-05-27 00:09:56 +0100643 if (cp2101_set_config(port, CP2101_BITS, &bits, 2))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 dev_err(&port->dev, "Number of stop bits requested "
645 "not supported by device\n");
646 }
Craig Shelley39a66b82005-05-27 00:09:56 +0100647
648 if ((cflag & CRTSCTS) != (old_cflag & CRTSCTS)) {
649 cp2101_get_config(port, CP2101_MODEMCTL, modem_ctl, 16);
650 dbg("%s - read modem controls = 0x%.4x 0x%.4x 0x%.4x 0x%.4x",
651 __FUNCTION__, modem_ctl[0], modem_ctl[1],
652 modem_ctl[2], modem_ctl[3]);
653
654 if (cflag & CRTSCTS) {
655 modem_ctl[0] &= ~0x7B;
656 modem_ctl[0] |= 0x09;
657 modem_ctl[1] = 0x80;
658 dbg("%s - flow control = CRTSCTS", __FUNCTION__);
659 } else {
660 modem_ctl[0] &= ~0x7B;
661 modem_ctl[0] |= 0x01;
662 modem_ctl[1] |= 0x40;
663 dbg("%s - flow control = NONE", __FUNCTION__);
664 }
665
666 dbg("%s - write modem controls = 0x%.4x 0x%.4x 0x%.4x 0x%.4x",
667 __FUNCTION__, modem_ctl[0], modem_ctl[1],
668 modem_ctl[2], modem_ctl[3]);
669 cp2101_set_config(port, CP2101_MODEMCTL, modem_ctl, 16);
670 }
671
672}
673
674static int cp2101_tiocmset (struct usb_serial_port *port, struct file *file,
675 unsigned int set, unsigned int clear)
676{
677 int control = 0;
678
679 dbg("%s - port %d", __FUNCTION__, port->number);
680
681 if (set & TIOCM_RTS) {
682 control |= CONTROL_RTS;
683 control |= CONTROL_WRITE_RTS;
684 }
685 if (set & TIOCM_DTR) {
686 control |= CONTROL_DTR;
687 control |= CONTROL_WRITE_DTR;
688 }
689 if (clear & TIOCM_RTS) {
690 control &= ~CONTROL_RTS;
691 control |= CONTROL_WRITE_RTS;
692 }
693 if (clear & TIOCM_DTR) {
694 control &= ~CONTROL_DTR;
695 control |= CONTROL_WRITE_DTR;
696 }
697
698 dbg("%s - control = 0x%.4x", __FUNCTION__, control);
699
700 return cp2101_set_config(port, CP2101_CONTROL, &control, 2);
701
702}
703
704static int cp2101_tiocmget (struct usb_serial_port *port, struct file *file)
705{
706 int control, result;
707
708 dbg("%s - port %d", __FUNCTION__, port->number);
709
710 cp2101_get_config(port, CP2101_CONTROL, &control, 1);
711
712 result = ((control & CONTROL_DTR) ? TIOCM_DTR : 0)
713 |((control & CONTROL_RTS) ? TIOCM_RTS : 0)
714 |((control & CONTROL_CTS) ? TIOCM_CTS : 0)
715 |((control & CONTROL_DSR) ? TIOCM_DSR : 0)
716 |((control & CONTROL_RING)? TIOCM_RI : 0)
717 |((control & CONTROL_DCD) ? TIOCM_CD : 0);
718
719 dbg("%s - control = 0x%.2x", __FUNCTION__, control);
720
721 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722}
723
724static void cp2101_break_ctl (struct usb_serial_port *port, int break_state)
725{
Craig Shelley39a66b82005-05-27 00:09:56 +0100726 int state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
728 dbg("%s - port %d", __FUNCTION__, port->number);
729 if (break_state == 0)
730 state = BREAK_OFF;
731 else
732 state = BREAK_ON;
733 dbg("%s - turning break %s", __FUNCTION__,
734 state==BREAK_OFF ? "off" : "on");
Craig Shelley39a66b82005-05-27 00:09:56 +0100735 cp2101_set_config(port, CP2101_BREAK, &state, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736}
737
738static int cp2101_startup (struct usb_serial *serial)
739{
Craig Shelley39a66b82005-05-27 00:09:56 +0100740 /* CP2101 buffers behave strangely unless device is reset */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 usb_reset_device(serial->dev);
742 return 0;
743}
744
745static void cp2101_shutdown (struct usb_serial *serial)
746{
747 int i;
748
749 dbg("%s", __FUNCTION__);
750
Craig Shelley39a66b82005-05-27 00:09:56 +0100751 /* Stop reads and writes on all ports */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 for (i=0; i < serial->num_ports; ++i) {
753 cp2101_cleanup(serial->port[i]);
754 }
755}
756
757static int __init cp2101_init (void)
758{
759 int retval;
760
761 retval = usb_serial_register(&cp2101_device);
762 if (retval)
Craig Shelley39a66b82005-05-27 00:09:56 +0100763 return retval; /* Failed to register */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
765 retval = usb_register(&cp2101_driver);
766 if (retval) {
Craig Shelley39a66b82005-05-27 00:09:56 +0100767 /* Failed to register */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 usb_serial_deregister(&cp2101_device);
769 return retval;
770 }
771
Craig Shelley39a66b82005-05-27 00:09:56 +0100772 /* Success */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 info(DRIVER_DESC " " DRIVER_VERSION);
774 return 0;
775}
776
777static void __exit cp2101_exit (void)
778{
779 usb_deregister (&cp2101_driver);
780 usb_serial_deregister (&cp2101_device);
781}
782
783module_init(cp2101_init);
784module_exit(cp2101_exit);
785
786MODULE_DESCRIPTION(DRIVER_DESC);
787MODULE_VERSION(DRIVER_VERSION);
788MODULE_LICENSE("GPL");
789
790module_param(debug, bool, S_IRUGO | S_IWUSR);
791MODULE_PARM_DESC(debug, "Enable verbose debugging messages");