blob: 813bab37e076febd58ba325f0338fc49d6e1af56 [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
20#include <linux/config.h>
21#include <linux/kernel.h>
22#include <linux/errno.h>
23#include <linux/slab.h>
24#include <linux/tty.h>
25#include <linux/tty_flip.h>
26#include <linux/module.h>
27#include <linux/moduleparam.h>
28#include <linux/usb.h>
29#include <asm/uaccess.h>
30#include "usb-serial.h"
31
32/*
33 * Version Information
34 */
Craig Shelley198b9512005-08-28 09:51:15 +010035#define DRIVER_VERSION "v0.05"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#define DRIVER_DESC "Silicon Labs CP2101/CP2102 RS232 serial adaptor driver"
37
38/*
39 * Function Prototypes
40 */
41static int cp2101_open(struct usb_serial_port*, struct file*);
42static void cp2101_cleanup(struct usb_serial_port*);
43static void cp2101_close(struct usb_serial_port*, struct file*);
44static void cp2101_get_termios(struct usb_serial_port*);
45static void cp2101_set_termios(struct usb_serial_port*, struct termios*);
Craig Shelley39a66b82005-05-27 00:09:56 +010046static int cp2101_tiocmget (struct usb_serial_port *, struct file *);
47static int cp2101_tiocmset (struct usb_serial_port *, struct file *,
48 unsigned int, unsigned int);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049static void cp2101_break_ctl(struct usb_serial_port*, int);
50static int cp2101_startup (struct usb_serial *);
51static void cp2101_shutdown(struct usb_serial*);
52
53
54static int debug;
55
56static struct usb_device_id id_table [] = {
Craig Shelley198b9512005-08-28 09:51:15 +010057 { USB_DEVICE(0x0FCF, 0x1003) }, /* Dynastream ANT development board */
Craig Shelley39a66b82005-05-27 00:09:56 +010058 { USB_DEVICE(0x10C4, 0xEA60) }, /* Silicon Labs factory default */
59 { USB_DEVICE(0x10C4, 0x80CA) }, /* Degree Controls Inc */
Craig Shelley198b9512005-08-28 09:51:15 +010060 { USB_DEVICE(0x10C4, 0x80F6) }, /* Suunto sports instrument */
61 { USB_DEVICE(0x10A6, 0xAA26) }, /* Knock-off DCU-11 cable */
Craig Shelley39a66b82005-05-27 00:09:56 +010062 { USB_DEVICE(0x10AB, 0x10C5) }, /* Siemens MC60 Cable */
Josef Balatkab0ce84d2005-11-17 09:47:24 -080063 { USB_DEVICE(0x16D6, 0x0001) }, /* Jablotron serial interface */
Craig Shelley39a66b82005-05-27 00:09:56 +010064 { } /* Terminating Entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -070065};
66
67MODULE_DEVICE_TABLE (usb, id_table);
68
69static struct usb_driver cp2101_driver = {
70 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -070071 .name = "cp2101",
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 .probe = usb_serial_probe,
73 .disconnect = usb_serial_disconnect,
74 .id_table = id_table,
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -080075 .no_dynamic_id = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -070076};
77
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -070078static struct usb_serial_driver cp2101_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -070079 .driver = {
80 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -070081 .name = "cp2101",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -070082 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 .id_table = id_table,
84 .num_interrupt_in = 0,
85 .num_bulk_in = 0,
86 .num_bulk_out = 0,
87 .num_ports = 1,
88 .open = cp2101_open,
89 .close = cp2101_close,
90 .break_ctl = cp2101_break_ctl,
91 .set_termios = cp2101_set_termios,
Craig Shelley39a66b82005-05-27 00:09:56 +010092 .tiocmget = cp2101_tiocmget,
93 .tiocmset = cp2101_tiocmset,
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 .attach = cp2101_startup,
95 .shutdown = cp2101_shutdown,
96};
97
Craig Shelley39a66b82005-05-27 00:09:56 +010098/* Config request types */
Linus Torvalds1da177e2005-04-16 15:20:36 -070099#define REQTYPE_HOST_TO_DEVICE 0x41
100#define REQTYPE_DEVICE_TO_HOST 0xc1
101
Craig Shelley39a66b82005-05-27 00:09:56 +0100102/* Config SET requests. To GET, add 1 to the request number */
103#define CP2101_UART 0x00 /* Enable / Disable */
104#define CP2101_BAUDRATE 0x01 /* (BAUD_RATE_GEN_FREQ / baudrate) */
105#define CP2101_BITS 0x03 /* 0x(0)(databits)(parity)(stopbits) */
106#define CP2101_BREAK 0x05 /* On / Off */
107#define CP2101_CONTROL 0x07 /* Flow control line states */
108#define CP2101_MODEMCTL 0x13 /* Modem controls */
109#define CP2101_CONFIG_6 0x19 /* 6 bytes of config data ??? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Craig Shelley39a66b82005-05-27 00:09:56 +0100111/* CP2101_UART */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112#define UART_ENABLE 0x0001
113#define UART_DISABLE 0x0000
114
Craig Shelley39a66b82005-05-27 00:09:56 +0100115/* CP2101_BAUDRATE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116#define BAUD_RATE_GEN_FREQ 0x384000
117
Craig Shelley39a66b82005-05-27 00:09:56 +0100118/* CP2101_BITS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119#define BITS_DATA_MASK 0X0f00
Craig Shelley39a66b82005-05-27 00:09:56 +0100120#define BITS_DATA_5 0X0500
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121#define BITS_DATA_6 0X0600
122#define BITS_DATA_7 0X0700
123#define BITS_DATA_8 0X0800
124#define BITS_DATA_9 0X0900
125
126#define BITS_PARITY_MASK 0x00f0
127#define BITS_PARITY_NONE 0x0000
128#define BITS_PARITY_ODD 0x0010
129#define BITS_PARITY_EVEN 0x0020
130#define BITS_PARITY_MARK 0x0030
131#define BITS_PARITY_SPACE 0x0040
132
133#define BITS_STOP_MASK 0x000f
134#define BITS_STOP_1 0x0000
135#define BITS_STOP_1_5 0x0001
136#define BITS_STOP_2 0x0002
Craig Shelley39a66b82005-05-27 00:09:56 +0100137
138/* CP2101_BREAK */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139#define BREAK_ON 0x0000
140#define BREAK_OFF 0x0001
141
Craig Shelley39a66b82005-05-27 00:09:56 +0100142/* CP2101_CONTROL */
143#define CONTROL_DTR 0x0001
144#define CONTROL_RTS 0x0002
145#define CONTROL_CTS 0x0010
146#define CONTROL_DSR 0x0020
147#define CONTROL_RING 0x0040
148#define CONTROL_DCD 0x0080
149#define CONTROL_WRITE_DTR 0x0100
150#define CONTROL_WRITE_RTS 0x0200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Craig Shelley39a66b82005-05-27 00:09:56 +0100152/*
153 * cp2101_get_config
154 * Reads from the CP2101 configuration registers
155 * 'size' is specified in bytes.
156 * 'data' is a pointer to a pre-allocated array of integers large
157 * enough to hold 'size' bytes (with 4 bytes to each integer)
158 */
159static int cp2101_get_config(struct usb_serial_port* port, u8 request,
160 unsigned int *data, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
162 struct usb_serial *serial = port->serial;
Craig Shelley39a66b82005-05-27 00:09:56 +0100163 u32 *buf;
164 int result, i, length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Craig Shelley39a66b82005-05-27 00:09:56 +0100166 /* Number of integers required to contain the array */
167 length = (((size - 1) | 3) + 1)/4;
168
169 buf = kmalloc (length * sizeof(u32), GFP_KERNEL);
170 memset(buf, 0, length * sizeof(u32));
171
172 if (!buf) {
173 dev_err(&port->dev, "%s - out of memory.\n", __FUNCTION__);
174 return -ENOMEM;
175 }
176
177 /* For get requests, the request number must be incremented */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 request++;
179
Craig Shelley39a66b82005-05-27 00:09:56 +0100180 /* Issue the request, attempting to read 'size' bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 result = usb_control_msg (serial->dev,usb_rcvctrlpipe (serial->dev, 0),
182 request, REQTYPE_DEVICE_TO_HOST, 0x0000,
Craig Shelley39a66b82005-05-27 00:09:56 +0100183 0, buf, size, 300);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Craig Shelley39a66b82005-05-27 00:09:56 +0100185 /* Convert data into an array of integers */
186 for (i=0; i<length; i++)
187 data[i] = le32_to_cpu(buf[i]);
188
189 kfree(buf);
190
191 if (result != size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 dev_err(&port->dev, "%s - Unable to send config request, "
Craig Shelley39a66b82005-05-27 00:09:56 +0100193 "request=0x%x size=%d result=%d\n",
194 __FUNCTION__, request, size, result);
195 return -EPROTO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 }
197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 return 0;
199}
200
Craig Shelley39a66b82005-05-27 00:09:56 +0100201/*
202 * cp2101_set_config
203 * Writes to the CP2101 configuration registers
204 * Values less than 16 bits wide are sent directly
205 * 'size' is specified in bytes.
206 */
207static int cp2101_set_config(struct usb_serial_port* port, u8 request,
208 unsigned int *data, int size)
209{
210 struct usb_serial *serial = port->serial;
211 u32 *buf;
212 int result, i, length;
213
214 /* Number of integers required to contain the array */
215 length = (((size - 1) | 3) + 1)/4;
216
217 buf = kmalloc(length * sizeof(u32), GFP_KERNEL);
218 if (!buf) {
219 dev_err(&port->dev, "%s - out of memory.\n",
220 __FUNCTION__);
221 return -ENOMEM;
222 }
223
224 /* Array of integers into bytes */
225 for (i = 0; i < length; i++)
226 buf[i] = cpu_to_le32(data[i]);
227
228 if (size > 2) {
229 result = usb_control_msg (serial->dev,
230 usb_sndctrlpipe(serial->dev, 0),
231 request, REQTYPE_HOST_TO_DEVICE, 0x0000,
232 0, buf, size, 300);
233 } else {
234 result = usb_control_msg (serial->dev,
235 usb_sndctrlpipe(serial->dev, 0),
236 request, REQTYPE_HOST_TO_DEVICE, data[0],
237 0, NULL, 0, 300);
238 }
239
240 kfree(buf);
241
242 if ((size > 2 && result != size) || result < 0) {
243 dev_err(&port->dev, "%s - Unable to send request, "
244 "request=0x%x size=%d result=%d\n",
245 __FUNCTION__, request, size, result);
246 return -EPROTO;
247 }
248
249 /* Single data value */
250 result = usb_control_msg (serial->dev,
251 usb_sndctrlpipe(serial->dev, 0),
252 request, REQTYPE_HOST_TO_DEVICE, data[0],
253 0, NULL, 0, 300);
254 return 0;
255}
256
257/*
258 * cp2101_set_config_single
259 * Convenience function for calling cp2101_set_config on single data values
260 * without requiring an integer pointer
261 */
262static inline int cp2101_set_config_single(struct usb_serial_port* port,
263 u8 request, unsigned int data)
264{
265 return cp2101_set_config(port, request, &data, 2);
266}
267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268static int cp2101_open (struct usb_serial_port *port, struct file *filp)
269{
270 struct usb_serial *serial = port->serial;
271 int result;
272
273 dbg("%s - port %d", __FUNCTION__, port->number);
274
Craig Shelley39a66b82005-05-27 00:09:56 +0100275 if (cp2101_set_config_single(port, CP2101_UART, UART_ENABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 dev_err(&port->dev, "%s - Unable to enable UART\n",
277 __FUNCTION__);
278 return -EPROTO;
279 }
280
281 /* Start reading from the device */
282 usb_fill_bulk_urb (port->read_urb, serial->dev,
283 usb_rcvbulkpipe(serial->dev,
284 port->bulk_in_endpointAddress),
285 port->read_urb->transfer_buffer,
286 port->read_urb->transfer_buffer_length,
287 serial->type->read_bulk_callback,
288 port);
289 result = usb_submit_urb(port->read_urb, GFP_KERNEL);
290 if (result) {
291 dev_err(&port->dev, "%s - failed resubmitting read urb, "
292 "error %d\n", __FUNCTION__, result);
293 return result;
294 }
295
Craig Shelley39a66b82005-05-27 00:09:56 +0100296 /* Configure the termios structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 cp2101_get_termios(port);
298
Craig Shelley39a66b82005-05-27 00:09:56 +0100299 /* Set the DTR and RTS pins low */
300 cp2101_tiocmset(port, NULL, TIOCM_DTR | TIOCM_RTS, 0);
301
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 return 0;
303}
304
305static void cp2101_cleanup (struct usb_serial_port *port)
306{
307 struct usb_serial *serial = port->serial;
308
309 dbg("%s - port %d", __FUNCTION__, port->number);
310
311 if (serial->dev) {
312 /* shutdown any bulk reads that might be going on */
313 if (serial->num_bulk_out)
314 usb_kill_urb(port->write_urb);
315 if (serial->num_bulk_in)
316 usb_kill_urb(port->read_urb);
317 }
318}
319
320static void cp2101_close (struct usb_serial_port *port, struct file * filp)
321{
322 dbg("%s - port %d", __FUNCTION__, port->number);
323
324 /* shutdown our urbs */
325 dbg("%s - shutting down urbs", __FUNCTION__);
326 usb_kill_urb(port->write_urb);
327 usb_kill_urb(port->read_urb);
328
Craig Shelley39a66b82005-05-27 00:09:56 +0100329 cp2101_set_config_single(port, CP2101_UART, UART_DISABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330}
331
Craig Shelley39a66b82005-05-27 00:09:56 +0100332/*
333 * cp2101_get_termios
334 * Reads the baud rate, data bits, parity, stop bits and flow control mode
335 * from the device, corrects any unsupported values, and configures the
336 * termios structure to reflect the state of the device
337 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338static void cp2101_get_termios (struct usb_serial_port *port)
339{
Craig Shelley39a66b82005-05-27 00:09:56 +0100340 unsigned int cflag, modem_ctl[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 int baud;
342 int bits;
343
344 dbg("%s - port %d", __FUNCTION__, port->number);
345
346 if ((!port->tty) || (!port->tty->termios)) {
347 dbg("%s - no tty structures", __FUNCTION__);
348 return;
349 }
350 cflag = port->tty->termios->c_cflag;
351
Craig Shelley39a66b82005-05-27 00:09:56 +0100352 cp2101_get_config(port, CP2101_BAUDRATE, &baud, 2);
353 /* Convert to baudrate */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 if (baud)
355 baud = BAUD_RATE_GEN_FREQ / baud;
356
357 dbg("%s - baud rate = %d", __FUNCTION__, baud);
358 cflag &= ~CBAUD;
359 switch (baud) {
Craig Shelley39a66b82005-05-27 00:09:56 +0100360 /*
361 * The baud rates which are commented out below
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 * appear to be supported by the device
363 * but are non-standard
364 */
365 case 600: cflag |= B600; break;
366 case 1200: cflag |= B1200; break;
367 case 1800: cflag |= B1800; break;
368 case 2400: cflag |= B2400; break;
369 case 4800: cflag |= B4800; break;
370 /*case 7200: cflag |= B7200; break;*/
371 case 9600: cflag |= B9600; break;
372 /*case 14400: cflag |= B14400; break;*/
373 case 19200: cflag |= B19200; break;
374 /*case 28800: cflag |= B28800; break;*/
375 case 38400: cflag |= B38400; break;
376 /*case 55854: cflag |= B55054; break;*/
377 case 57600: cflag |= B57600; break;
378 case 115200: cflag |= B115200; break;
379 /*case 127117: cflag |= B127117; break;*/
380 case 230400: cflag |= B230400; break;
381 case 460800: cflag |= B460800; break;
382 case 921600: cflag |= B921600; break;
383 /*case 3686400: cflag |= B3686400; break;*/
384 default:
385 dbg("%s - Baud rate is not supported, "
386 "using 9600 baud", __FUNCTION__);
387 cflag |= B9600;
Craig Shelley39a66b82005-05-27 00:09:56 +0100388 cp2101_set_config_single(port, CP2101_BAUDRATE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 (BAUD_RATE_GEN_FREQ/9600));
390 break;
391 }
392
Craig Shelley39a66b82005-05-27 00:09:56 +0100393 cp2101_get_config(port, CP2101_BITS, &bits, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 cflag &= ~CSIZE;
395 switch(bits & BITS_DATA_MASK) {
Craig Shelley39a66b82005-05-27 00:09:56 +0100396 case BITS_DATA_5:
397 dbg("%s - data bits = 5", __FUNCTION__);
398 cflag |= CS5;
399 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 case BITS_DATA_6:
401 dbg("%s - data bits = 6", __FUNCTION__);
402 cflag |= CS6;
403 break;
404 case BITS_DATA_7:
405 dbg("%s - data bits = 7", __FUNCTION__);
406 cflag |= CS7;
407 break;
408 case BITS_DATA_8:
409 dbg("%s - data bits = 8", __FUNCTION__);
410 cflag |= CS8;
411 break;
412 case BITS_DATA_9:
413 dbg("%s - data bits = 9 (not supported, "
414 "using 8 data bits)", __FUNCTION__);
415 cflag |= CS8;
416 bits &= ~BITS_DATA_MASK;
417 bits |= BITS_DATA_8;
Craig Shelley39a66b82005-05-27 00:09:56 +0100418 cp2101_set_config(port, CP2101_BITS, &bits, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 break;
420 default:
421 dbg("%s - Unknown number of data bits, "
422 "using 8", __FUNCTION__);
423 cflag |= CS8;
424 bits &= ~BITS_DATA_MASK;
425 bits |= BITS_DATA_8;
Craig Shelley39a66b82005-05-27 00:09:56 +0100426 cp2101_set_config(port, CP2101_BITS, &bits, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 break;
428 }
429
430 switch(bits & BITS_PARITY_MASK) {
431 case BITS_PARITY_NONE:
432 dbg("%s - parity = NONE", __FUNCTION__);
433 cflag &= ~PARENB;
434 break;
435 case BITS_PARITY_ODD:
436 dbg("%s - parity = ODD", __FUNCTION__);
437 cflag |= (PARENB|PARODD);
438 break;
439 case BITS_PARITY_EVEN:
440 dbg("%s - parity = EVEN", __FUNCTION__);
441 cflag &= ~PARODD;
442 cflag |= PARENB;
443 break;
444 case BITS_PARITY_MARK:
445 dbg("%s - parity = MARK (not supported, "
446 "disabling parity)", __FUNCTION__);
447 cflag &= ~PARENB;
448 bits &= ~BITS_PARITY_MASK;
Craig Shelley39a66b82005-05-27 00:09:56 +0100449 cp2101_set_config(port, CP2101_BITS, &bits, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 break;
451 case BITS_PARITY_SPACE:
452 dbg("%s - parity = SPACE (not supported, "
453 "disabling parity)", __FUNCTION__);
454 cflag &= ~PARENB;
455 bits &= ~BITS_PARITY_MASK;
Craig Shelley39a66b82005-05-27 00:09:56 +0100456 cp2101_set_config(port, CP2101_BITS, &bits, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 break;
458 default:
459 dbg("%s - Unknown parity mode, "
460 "disabling parity", __FUNCTION__);
461 cflag &= ~PARENB;
462 bits &= ~BITS_PARITY_MASK;
Craig Shelley39a66b82005-05-27 00:09:56 +0100463 cp2101_set_config(port, CP2101_BITS, &bits, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 break;
465 }
466
467 cflag &= ~CSTOPB;
468 switch(bits & BITS_STOP_MASK) {
469 case BITS_STOP_1:
470 dbg("%s - stop bits = 1", __FUNCTION__);
471 break;
472 case BITS_STOP_1_5:
473 dbg("%s - stop bits = 1.5 (not supported, "
Craig Shelley39a66b82005-05-27 00:09:56 +0100474 "using 1 stop bit)", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 bits &= ~BITS_STOP_MASK;
Craig Shelley39a66b82005-05-27 00:09:56 +0100476 cp2101_set_config(port, CP2101_BITS, &bits, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 break;
478 case BITS_STOP_2:
479 dbg("%s - stop bits = 2", __FUNCTION__);
480 cflag |= CSTOPB;
481 break;
482 default:
483 dbg("%s - Unknown number of stop bits, "
484 "using 1 stop bit", __FUNCTION__);
485 bits &= ~BITS_STOP_MASK;
Craig Shelley39a66b82005-05-27 00:09:56 +0100486 cp2101_set_config(port, CP2101_BITS, &bits, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 break;
488 }
489
Craig Shelley39a66b82005-05-27 00:09:56 +0100490 cp2101_get_config(port, CP2101_MODEMCTL, modem_ctl, 16);
491 if (modem_ctl[0] & 0x0008) {
492 dbg("%s - flow control = CRTSCTS", __FUNCTION__);
493 cflag |= CRTSCTS;
494 } else {
495 dbg("%s - flow control = NONE", __FUNCTION__);
496 cflag &= ~CRTSCTS;
497 }
498
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 port->tty->termios->c_cflag = cflag;
500}
501
502static void cp2101_set_termios (struct usb_serial_port *port,
503 struct termios *old_termios)
504{
505 unsigned int cflag, old_cflag=0;
Craig Shelley39a66b82005-05-27 00:09:56 +0100506 int baud=0, bits;
507 unsigned int modem_ctl[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
509 dbg("%s - port %d", __FUNCTION__, port->number);
510
511 if ((!port->tty) || (!port->tty->termios)) {
512 dbg("%s - no tty structures", __FUNCTION__);
513 return;
514 }
515 cflag = port->tty->termios->c_cflag;
516
Craig Shelley39a66b82005-05-27 00:09:56 +0100517 /* Check that they really want us to change something */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 if (old_termios) {
519 if ((cflag == old_termios->c_cflag) &&
520 (RELEVANT_IFLAG(port->tty->termios->c_iflag)
521 == RELEVANT_IFLAG(old_termios->c_iflag))) {
522 dbg("%s - nothing to change...", __FUNCTION__);
523 return;
524 }
525
526 old_cflag = old_termios->c_cflag;
527 }
528
529 /* If the baud rate is to be updated*/
530 if ((cflag & CBAUD) != (old_cflag & CBAUD)) {
531 switch (cflag & CBAUD) {
Craig Shelley39a66b82005-05-27 00:09:56 +0100532 /*
533 * The baud rates which are commented out below
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 * appear to be supported by the device
535 * but are non-standard
536 */
537 case B0: baud = 0; break;
538 case B600: baud = 600; break;
539 case B1200: baud = 1200; break;
540 case B1800: baud = 1800; break;
541 case B2400: baud = 2400; break;
542 case B4800: baud = 4800; break;
543 /*case B7200: baud = 7200; break;*/
544 case B9600: baud = 9600; break;
545 /*ase B14400: baud = 14400; break;*/
546 case B19200: baud = 19200; break;
547 /*case B28800: baud = 28800; break;*/
548 case B38400: baud = 38400; break;
549 /*case B55854: baud = 55054; break;*/
550 case B57600: baud = 57600; break;
551 case B115200: baud = 115200; break;
552 /*case B127117: baud = 127117; break;*/
553 case B230400: baud = 230400; break;
554 case B460800: baud = 460800; break;
555 case B921600: baud = 921600; break;
556 /*case B3686400: baud = 3686400; break;*/
557 default:
558 dev_err(&port->dev, "cp2101 driver does not "
559 "support the baudrate requested\n");
560 break;
561 }
562
563 if (baud) {
564 dbg("%s - Setting baud rate to %d baud", __FUNCTION__,
565 baud);
Craig Shelley39a66b82005-05-27 00:09:56 +0100566 if (cp2101_set_config_single(port, CP2101_BAUDRATE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 (BAUD_RATE_GEN_FREQ / baud)))
568 dev_err(&port->dev, "Baud rate requested not "
569 "supported by device\n");
570 }
571 }
572
Craig Shelley39a66b82005-05-27 00:09:56 +0100573 /* If the number of data bits is to be updated */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 if ((cflag & CSIZE) != (old_cflag & CSIZE)) {
Craig Shelley39a66b82005-05-27 00:09:56 +0100575 cp2101_get_config(port, CP2101_BITS, &bits, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 bits &= ~BITS_DATA_MASK;
577 switch (cflag & CSIZE) {
Craig Shelley39a66b82005-05-27 00:09:56 +0100578 case CS5:
579 bits |= BITS_DATA_5;
580 dbg("%s - data bits = 5", __FUNCTION__);
581 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 case CS6:
583 bits |= BITS_DATA_6;
584 dbg("%s - data bits = 6", __FUNCTION__);
585 break;
586 case CS7:
587 bits |= BITS_DATA_7;
588 dbg("%s - data bits = 7", __FUNCTION__);
589 break;
590 case CS8:
591 bits |= BITS_DATA_8;
592 dbg("%s - data bits = 8", __FUNCTION__);
593 break;
594 /*case CS9:
595 bits |= BITS_DATA_9;
596 dbg("%s - data bits = 9", __FUNCTION__);
597 break;*/
598 default:
599 dev_err(&port->dev, "cp2101 driver does not "
600 "support the number of bits requested,"
601 " using 8 bit mode\n");
602 bits |= BITS_DATA_8;
603 break;
604 }
Craig Shelley39a66b82005-05-27 00:09:56 +0100605 if (cp2101_set_config(port, CP2101_BITS, &bits, 2))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 dev_err(&port->dev, "Number of data bits requested "
607 "not supported by device\n");
608 }
609
610 if ((cflag & (PARENB|PARODD)) != (old_cflag & (PARENB|PARODD))) {
Craig Shelley39a66b82005-05-27 00:09:56 +0100611 cp2101_get_config(port, CP2101_BITS, &bits, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 bits &= ~BITS_PARITY_MASK;
613 if (cflag & PARENB) {
614 if (cflag & PARODD) {
615 bits |= BITS_PARITY_ODD;
616 dbg("%s - parity = ODD", __FUNCTION__);
617 } else {
618 bits |= BITS_PARITY_EVEN;
619 dbg("%s - parity = EVEN", __FUNCTION__);
620 }
621 }
Craig Shelley39a66b82005-05-27 00:09:56 +0100622 if (cp2101_set_config(port, CP2101_BITS, &bits, 2))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 dev_err(&port->dev, "Parity mode not supported "
624 "by device\n");
625 }
626
627 if ((cflag & CSTOPB) != (old_cflag & CSTOPB)) {
Craig Shelley39a66b82005-05-27 00:09:56 +0100628 cp2101_get_config(port, CP2101_BITS, &bits, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 bits &= ~BITS_STOP_MASK;
630 if (cflag & CSTOPB) {
631 bits |= BITS_STOP_2;
632 dbg("%s - stop bits = 2", __FUNCTION__);
633 } else {
634 bits |= BITS_STOP_1;
635 dbg("%s - stop bits = 1", __FUNCTION__);
636 }
Craig Shelley39a66b82005-05-27 00:09:56 +0100637 if (cp2101_set_config(port, CP2101_BITS, &bits, 2))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 dev_err(&port->dev, "Number of stop bits requested "
639 "not supported by device\n");
640 }
Craig Shelley39a66b82005-05-27 00:09:56 +0100641
642 if ((cflag & CRTSCTS) != (old_cflag & CRTSCTS)) {
643 cp2101_get_config(port, CP2101_MODEMCTL, modem_ctl, 16);
644 dbg("%s - read modem controls = 0x%.4x 0x%.4x 0x%.4x 0x%.4x",
645 __FUNCTION__, modem_ctl[0], modem_ctl[1],
646 modem_ctl[2], modem_ctl[3]);
647
648 if (cflag & CRTSCTS) {
649 modem_ctl[0] &= ~0x7B;
650 modem_ctl[0] |= 0x09;
651 modem_ctl[1] = 0x80;
652 dbg("%s - flow control = CRTSCTS", __FUNCTION__);
653 } else {
654 modem_ctl[0] &= ~0x7B;
655 modem_ctl[0] |= 0x01;
656 modem_ctl[1] |= 0x40;
657 dbg("%s - flow control = NONE", __FUNCTION__);
658 }
659
660 dbg("%s - write modem controls = 0x%.4x 0x%.4x 0x%.4x 0x%.4x",
661 __FUNCTION__, modem_ctl[0], modem_ctl[1],
662 modem_ctl[2], modem_ctl[3]);
663 cp2101_set_config(port, CP2101_MODEMCTL, modem_ctl, 16);
664 }
665
666}
667
668static int cp2101_tiocmset (struct usb_serial_port *port, struct file *file,
669 unsigned int set, unsigned int clear)
670{
671 int control = 0;
672
673 dbg("%s - port %d", __FUNCTION__, port->number);
674
675 if (set & TIOCM_RTS) {
676 control |= CONTROL_RTS;
677 control |= CONTROL_WRITE_RTS;
678 }
679 if (set & TIOCM_DTR) {
680 control |= CONTROL_DTR;
681 control |= CONTROL_WRITE_DTR;
682 }
683 if (clear & TIOCM_RTS) {
684 control &= ~CONTROL_RTS;
685 control |= CONTROL_WRITE_RTS;
686 }
687 if (clear & TIOCM_DTR) {
688 control &= ~CONTROL_DTR;
689 control |= CONTROL_WRITE_DTR;
690 }
691
692 dbg("%s - control = 0x%.4x", __FUNCTION__, control);
693
694 return cp2101_set_config(port, CP2101_CONTROL, &control, 2);
695
696}
697
698static int cp2101_tiocmget (struct usb_serial_port *port, struct file *file)
699{
700 int control, result;
701
702 dbg("%s - port %d", __FUNCTION__, port->number);
703
704 cp2101_get_config(port, CP2101_CONTROL, &control, 1);
705
706 result = ((control & CONTROL_DTR) ? TIOCM_DTR : 0)
707 |((control & CONTROL_RTS) ? TIOCM_RTS : 0)
708 |((control & CONTROL_CTS) ? TIOCM_CTS : 0)
709 |((control & CONTROL_DSR) ? TIOCM_DSR : 0)
710 |((control & CONTROL_RING)? TIOCM_RI : 0)
711 |((control & CONTROL_DCD) ? TIOCM_CD : 0);
712
713 dbg("%s - control = 0x%.2x", __FUNCTION__, control);
714
715 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716}
717
718static void cp2101_break_ctl (struct usb_serial_port *port, int break_state)
719{
Craig Shelley39a66b82005-05-27 00:09:56 +0100720 int state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
722 dbg("%s - port %d", __FUNCTION__, port->number);
723 if (break_state == 0)
724 state = BREAK_OFF;
725 else
726 state = BREAK_ON;
727 dbg("%s - turning break %s", __FUNCTION__,
728 state==BREAK_OFF ? "off" : "on");
Craig Shelley39a66b82005-05-27 00:09:56 +0100729 cp2101_set_config(port, CP2101_BREAK, &state, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730}
731
732static int cp2101_startup (struct usb_serial *serial)
733{
Craig Shelley39a66b82005-05-27 00:09:56 +0100734 /* CP2101 buffers behave strangely unless device is reset */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 usb_reset_device(serial->dev);
736 return 0;
737}
738
739static void cp2101_shutdown (struct usb_serial *serial)
740{
741 int i;
742
743 dbg("%s", __FUNCTION__);
744
Craig Shelley39a66b82005-05-27 00:09:56 +0100745 /* Stop reads and writes on all ports */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 for (i=0; i < serial->num_ports; ++i) {
747 cp2101_cleanup(serial->port[i]);
748 }
749}
750
751static int __init cp2101_init (void)
752{
753 int retval;
754
755 retval = usb_serial_register(&cp2101_device);
756 if (retval)
Craig Shelley39a66b82005-05-27 00:09:56 +0100757 return retval; /* Failed to register */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
759 retval = usb_register(&cp2101_driver);
760 if (retval) {
Craig Shelley39a66b82005-05-27 00:09:56 +0100761 /* Failed to register */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 usb_serial_deregister(&cp2101_device);
763 return retval;
764 }
765
Craig Shelley39a66b82005-05-27 00:09:56 +0100766 /* Success */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 info(DRIVER_DESC " " DRIVER_VERSION);
768 return 0;
769}
770
771static void __exit cp2101_exit (void)
772{
773 usb_deregister (&cp2101_driver);
774 usb_serial_deregister (&cp2101_device);
775}
776
777module_init(cp2101_init);
778module_exit(cp2101_exit);
779
780MODULE_DESCRIPTION(DRIVER_DESC);
781MODULE_VERSION(DRIVER_VERSION);
782MODULE_LICENSE("GPL");
783
784module_param(debug, bool, S_IRUGO | S_IWUSR);
785MODULE_PARM_DESC(debug, "Enable verbose debugging messages");