blob: 3b4fa94ecf2830b4abd8dd515c4dbd1252fc3ded [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 Shelley59fba742008-04-12 16:15:54 +010056 { USB_DEVICE(0x0489, 0xE000) }, /* Pirelli Broadband S.p.A, DP-L10 SIP/GSM Mobile */
Edouard Lafargue1f4f7142007-09-25 10:15:36 +020057 { USB_DEVICE(0x08e6, 0x5501) }, /* Gemalto Prox-PU/CU contactless smartcard reader */
Craig Shelley198b9512005-08-28 09:51:15 +010058 { USB_DEVICE(0x0FCF, 0x1003) }, /* Dynastream ANT development board */
Martin Kusserow014840e2007-12-21 12:02:17 +010059 { USB_DEVICE(0x0FCF, 0x1004) }, /* Dynastream ANT2USB */
Craig Shelley59fba742008-04-12 16:15:54 +010060 { USB_DEVICE(0x0FCF, 0x1006) }, /* Dynastream ANT development board */
Craig Shelley198b9512005-08-28 09:51:15 +010061 { USB_DEVICE(0x10A6, 0xAA26) }, /* Knock-off DCU-11 cable */
Craig Shelleye988fc82006-01-20 00:06:19 +000062 { USB_DEVICE(0x10AB, 0x10C5) }, /* Siemens MC60 Cable */
63 { USB_DEVICE(0x10B5, 0xAC70) }, /* Nokia CA-42 USB */
Craig Shelley5de8b3a2008-01-03 12:45:59 +000064 { USB_DEVICE(0x10C4, 0x800A) }, /* SPORTident BSM7-D-USB main station */
Craig Shelley59224f52006-03-11 11:29:02 +000065 { USB_DEVICE(0x10C4, 0x803B) }, /* Pololu USB-serial converter */
Craig Shelley92f5cae2007-04-20 14:37:17 +010066 { USB_DEVICE(0x10C4, 0x8053) }, /* Enfora EDG1228 */
Vitja Makarov212a4b42006-05-31 00:40:06 +040067 { USB_DEVICE(0x10C4, 0x8066) }, /* Argussoft In-System Programmer */
Craig Shelleye988fc82006-01-20 00:06:19 +000068 { USB_DEVICE(0x10C4, 0x807A) }, /* Crumb128 board */
69 { USB_DEVICE(0x10C4, 0x80CA) }, /* Degree Controls Inc */
Craig Shelley92f5cae2007-04-20 14:37:17 +010070 { USB_DEVICE(0x10C4, 0x80DD) }, /* Tracient RFID */
Craig Shelleye988fc82006-01-20 00:06:19 +000071 { USB_DEVICE(0x10C4, 0x80F6) }, /* Suunto sports instrument */
72 { USB_DEVICE(0x10C4, 0x813D) }, /* Burnside Telecom Deskmobile */
Jon K Hellanfc4bd642007-02-28 23:04:33 +010073 { USB_DEVICE(0x10C4, 0x814A) }, /* West Mountain Radio RIGblaster P&P */
74 { USB_DEVICE(0x10C4, 0x814B) }, /* West Mountain Radio RIGtalk */
Craig Shelleye988fc82006-01-20 00:06:19 +000075 { USB_DEVICE(0x10C4, 0x815E) }, /* Helicomm IP-Link 1220-DVM */
Craig Shelley59fba742008-04-12 16:15:54 +010076 { USB_DEVICE(0x10C4, 0x81AC) }, /* MSD Dash Hawk */
Bjorn Schneider78001e32006-10-28 12:42:04 +020077 { USB_DEVICE(0x10C4, 0x81C8) }, /* Lipowsky Industrie Elektronik GmbH, Baby-JTAG */
78 { USB_DEVICE(0x10C4, 0x81E2) }, /* Lipowsky Industrie Elektronik GmbH, Baby-LIN */
Jeff Long8be27c22007-11-28 11:02:09 -050079 { USB_DEVICE(0x10C4, 0x81E7) }, /* Aerocomm Radio */
Bjorn Schneider78001e32006-10-28 12:42:04 +020080 { USB_DEVICE(0x10C4, 0x8218) }, /* Lipowsky Industrie Elektronik GmbH, HARP-1 */
Florian Fainellie8898682008-03-25 17:32:16 +010081 { USB_DEVICE(0x10c4, 0x8293) }, /* Telegesys ETRX2USB */
Craig Shelleye988fc82006-01-20 00:06:19 +000082 { USB_DEVICE(0x10C4, 0xEA60) }, /* Silicon Labs factory default */
Craig Shelley61926b92006-10-12 22:09:56 +010083 { USB_DEVICE(0x10C4, 0xEA61) }, /* Silicon Labs factory default */
Craig Shelley5de8b3a2008-01-03 12:45:59 +000084 { USB_DEVICE(0x10C4, 0xF001) }, /* Elan Digital Systems USBscope50 */
85 { USB_DEVICE(0x10C4, 0xF002) }, /* Elan Digital Systems USBwave12 */
86 { USB_DEVICE(0x10C4, 0xF003) }, /* Elan Digital Systems USBpulse100 */
87 { USB_DEVICE(0x10C4, 0xF004) }, /* Elan Digital Systems USBcount50 */
Pozsar Balazsebaf4942007-02-16 12:47:49 +010088 { USB_DEVICE(0x10C5, 0xEA61) }, /* Silicon Labs MobiData GPRS USB Modem */
Johannes Hoelzle05998d2006-12-02 16:54:27 +010089 { USB_DEVICE(0x13AD, 0x9999) }, /* Baltech card reader */
Craig Shelley5de8b3a2008-01-03 12:45:59 +000090 { USB_DEVICE(0x166A, 0x0303) }, /* Clipsal 5500PCU C-Bus USB interface */
Josef Balatkab0ce84d2005-11-17 09:47:24 -080091 { USB_DEVICE(0x16D6, 0x0001) }, /* Jablotron serial interface */
Craig Shelley39a66b82005-05-27 00:09:56 +010092 { } /* Terminating Entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -070093};
94
95MODULE_DEVICE_TABLE (usb, id_table);
96
97static struct usb_driver cp2101_driver = {
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -070098 .name = "cp2101",
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 .probe = usb_serial_probe,
100 .disconnect = usb_serial_disconnect,
101 .id_table = id_table,
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800102 .no_dynamic_id = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103};
104
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700105static struct usb_serial_driver cp2101_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700106 .driver = {
107 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700108 .name = "cp2101",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700109 },
Johannes Hölzld9b1b782006-12-17 21:50:24 +0100110 .usb_driver = &cp2101_driver,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 .id_table = id_table,
112 .num_interrupt_in = 0,
Jeff Long8be27c22007-11-28 11:02:09 -0500113 .num_bulk_in = NUM_DONT_CARE,
114 .num_bulk_out = NUM_DONT_CARE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 .num_ports = 1,
116 .open = cp2101_open,
117 .close = cp2101_close,
118 .break_ctl = cp2101_break_ctl,
119 .set_termios = cp2101_set_termios,
Craig Shelley39a66b82005-05-27 00:09:56 +0100120 .tiocmget = cp2101_tiocmget,
121 .tiocmset = cp2101_tiocmset,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 .attach = cp2101_startup,
123 .shutdown = cp2101_shutdown,
124};
125
Craig Shelley39a66b82005-05-27 00:09:56 +0100126/* Config request types */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127#define REQTYPE_HOST_TO_DEVICE 0x41
128#define REQTYPE_DEVICE_TO_HOST 0xc1
129
Craig Shelley39a66b82005-05-27 00:09:56 +0100130/* Config SET requests. To GET, add 1 to the request number */
131#define CP2101_UART 0x00 /* Enable / Disable */
132#define CP2101_BAUDRATE 0x01 /* (BAUD_RATE_GEN_FREQ / baudrate) */
133#define CP2101_BITS 0x03 /* 0x(0)(databits)(parity)(stopbits) */
134#define CP2101_BREAK 0x05 /* On / Off */
135#define CP2101_CONTROL 0x07 /* Flow control line states */
136#define CP2101_MODEMCTL 0x13 /* Modem controls */
137#define CP2101_CONFIG_6 0x19 /* 6 bytes of config data ??? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Craig Shelley39a66b82005-05-27 00:09:56 +0100139/* CP2101_UART */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140#define UART_ENABLE 0x0001
141#define UART_DISABLE 0x0000
142
Craig Shelley39a66b82005-05-27 00:09:56 +0100143/* CP2101_BAUDRATE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144#define BAUD_RATE_GEN_FREQ 0x384000
145
Craig Shelley39a66b82005-05-27 00:09:56 +0100146/* CP2101_BITS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147#define BITS_DATA_MASK 0X0f00
Craig Shelley39a66b82005-05-27 00:09:56 +0100148#define BITS_DATA_5 0X0500
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149#define BITS_DATA_6 0X0600
150#define BITS_DATA_7 0X0700
151#define BITS_DATA_8 0X0800
152#define BITS_DATA_9 0X0900
153
154#define BITS_PARITY_MASK 0x00f0
155#define BITS_PARITY_NONE 0x0000
156#define BITS_PARITY_ODD 0x0010
157#define BITS_PARITY_EVEN 0x0020
158#define BITS_PARITY_MARK 0x0030
159#define BITS_PARITY_SPACE 0x0040
160
161#define BITS_STOP_MASK 0x000f
162#define BITS_STOP_1 0x0000
163#define BITS_STOP_1_5 0x0001
164#define BITS_STOP_2 0x0002
Craig Shelley39a66b82005-05-27 00:09:56 +0100165
166/* CP2101_BREAK */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167#define BREAK_ON 0x0000
168#define BREAK_OFF 0x0001
169
Craig Shelley39a66b82005-05-27 00:09:56 +0100170/* CP2101_CONTROL */
171#define CONTROL_DTR 0x0001
172#define CONTROL_RTS 0x0002
173#define CONTROL_CTS 0x0010
174#define CONTROL_DSR 0x0020
175#define CONTROL_RING 0x0040
176#define CONTROL_DCD 0x0080
177#define CONTROL_WRITE_DTR 0x0100
178#define CONTROL_WRITE_RTS 0x0200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Craig Shelley39a66b82005-05-27 00:09:56 +0100180/*
181 * cp2101_get_config
182 * Reads from the CP2101 configuration registers
183 * 'size' is specified in bytes.
184 * 'data' is a pointer to a pre-allocated array of integers large
185 * enough to hold 'size' bytes (with 4 bytes to each integer)
186 */
187static int cp2101_get_config(struct usb_serial_port* port, u8 request,
188 unsigned int *data, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189{
190 struct usb_serial *serial = port->serial;
Al Viro53ebb3b2007-02-09 16:39:55 +0000191 __le32 *buf;
Craig Shelley39a66b82005-05-27 00:09:56 +0100192 int result, i, length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Craig Shelley39a66b82005-05-27 00:09:56 +0100194 /* Number of integers required to contain the array */
195 length = (((size - 1) | 3) + 1)/4;
196
Al Viro53ebb3b2007-02-09 16:39:55 +0000197 buf = kcalloc(length, sizeof(__le32), GFP_KERNEL);
Craig Shelley39a66b82005-05-27 00:09:56 +0100198 if (!buf) {
199 dev_err(&port->dev, "%s - out of memory.\n", __FUNCTION__);
200 return -ENOMEM;
201 }
202
203 /* For get requests, the request number must be incremented */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 request++;
205
Craig Shelley39a66b82005-05-27 00:09:56 +0100206 /* Issue the request, attempting to read 'size' bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 result = usb_control_msg (serial->dev,usb_rcvctrlpipe (serial->dev, 0),
208 request, REQTYPE_DEVICE_TO_HOST, 0x0000,
Craig Shelley39a66b82005-05-27 00:09:56 +0100209 0, buf, size, 300);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Craig Shelley39a66b82005-05-27 00:09:56 +0100211 /* Convert data into an array of integers */
212 for (i=0; i<length; i++)
213 data[i] = le32_to_cpu(buf[i]);
214
215 kfree(buf);
216
217 if (result != size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 dev_err(&port->dev, "%s - Unable to send config request, "
Craig Shelley39a66b82005-05-27 00:09:56 +0100219 "request=0x%x size=%d result=%d\n",
220 __FUNCTION__, request, size, result);
221 return -EPROTO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 }
223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 return 0;
225}
226
Craig Shelley39a66b82005-05-27 00:09:56 +0100227/*
228 * cp2101_set_config
229 * Writes to the CP2101 configuration registers
230 * Values less than 16 bits wide are sent directly
231 * 'size' is specified in bytes.
232 */
233static int cp2101_set_config(struct usb_serial_port* port, u8 request,
234 unsigned int *data, int size)
235{
236 struct usb_serial *serial = port->serial;
Al Viro53ebb3b2007-02-09 16:39:55 +0000237 __le32 *buf;
Craig Shelley39a66b82005-05-27 00:09:56 +0100238 int result, i, length;
239
240 /* Number of integers required to contain the array */
241 length = (((size - 1) | 3) + 1)/4;
242
Al Viro53ebb3b2007-02-09 16:39:55 +0000243 buf = kmalloc(length * sizeof(__le32), GFP_KERNEL);
Craig Shelley39a66b82005-05-27 00:09:56 +0100244 if (!buf) {
245 dev_err(&port->dev, "%s - out of memory.\n",
246 __FUNCTION__);
247 return -ENOMEM;
248 }
249
250 /* Array of integers into bytes */
251 for (i = 0; i < length; i++)
252 buf[i] = cpu_to_le32(data[i]);
253
254 if (size > 2) {
255 result = usb_control_msg (serial->dev,
256 usb_sndctrlpipe(serial->dev, 0),
257 request, REQTYPE_HOST_TO_DEVICE, 0x0000,
258 0, buf, size, 300);
259 } else {
260 result = usb_control_msg (serial->dev,
261 usb_sndctrlpipe(serial->dev, 0),
262 request, REQTYPE_HOST_TO_DEVICE, data[0],
263 0, NULL, 0, 300);
264 }
265
266 kfree(buf);
267
268 if ((size > 2 && result != size) || result < 0) {
269 dev_err(&port->dev, "%s - Unable to send request, "
270 "request=0x%x size=%d result=%d\n",
271 __FUNCTION__, request, size, result);
272 return -EPROTO;
273 }
274
275 /* Single data value */
276 result = usb_control_msg (serial->dev,
277 usb_sndctrlpipe(serial->dev, 0),
278 request, REQTYPE_HOST_TO_DEVICE, data[0],
279 0, NULL, 0, 300);
280 return 0;
281}
282
283/*
284 * cp2101_set_config_single
285 * Convenience function for calling cp2101_set_config on single data values
286 * without requiring an integer pointer
287 */
288static inline int cp2101_set_config_single(struct usb_serial_port* port,
289 u8 request, unsigned int data)
290{
291 return cp2101_set_config(port, request, &data, 2);
292}
293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294static int cp2101_open (struct usb_serial_port *port, struct file *filp)
295{
296 struct usb_serial *serial = port->serial;
297 int result;
298
299 dbg("%s - port %d", __FUNCTION__, port->number);
300
Craig Shelley39a66b82005-05-27 00:09:56 +0100301 if (cp2101_set_config_single(port, CP2101_UART, UART_ENABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 dev_err(&port->dev, "%s - Unable to enable UART\n",
303 __FUNCTION__);
304 return -EPROTO;
305 }
306
307 /* Start reading from the device */
308 usb_fill_bulk_urb (port->read_urb, serial->dev,
309 usb_rcvbulkpipe(serial->dev,
310 port->bulk_in_endpointAddress),
311 port->read_urb->transfer_buffer,
312 port->read_urb->transfer_buffer_length,
313 serial->type->read_bulk_callback,
314 port);
315 result = usb_submit_urb(port->read_urb, GFP_KERNEL);
316 if (result) {
317 dev_err(&port->dev, "%s - failed resubmitting read urb, "
318 "error %d\n", __FUNCTION__, result);
319 return result;
320 }
321
Craig Shelley39a66b82005-05-27 00:09:56 +0100322 /* Configure the termios structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 cp2101_get_termios(port);
324
Craig Shelley39a66b82005-05-27 00:09:56 +0100325 /* Set the DTR and RTS pins low */
326 cp2101_tiocmset(port, NULL, TIOCM_DTR | TIOCM_RTS, 0);
327
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 return 0;
329}
330
331static void cp2101_cleanup (struct usb_serial_port *port)
332{
333 struct usb_serial *serial = port->serial;
334
335 dbg("%s - port %d", __FUNCTION__, port->number);
336
337 if (serial->dev) {
338 /* shutdown any bulk reads that might be going on */
339 if (serial->num_bulk_out)
340 usb_kill_urb(port->write_urb);
341 if (serial->num_bulk_in)
342 usb_kill_urb(port->read_urb);
343 }
344}
345
346static void cp2101_close (struct usb_serial_port *port, struct file * filp)
347{
348 dbg("%s - port %d", __FUNCTION__, port->number);
349
350 /* shutdown our urbs */
351 dbg("%s - shutting down urbs", __FUNCTION__);
352 usb_kill_urb(port->write_urb);
353 usb_kill_urb(port->read_urb);
354
Oliver Neukum95bef012008-01-22 13:56:18 +0100355 mutex_lock(&port->serial->disc_mutex);
356 if (!port->serial->disconnected)
357 cp2101_set_config_single(port, CP2101_UART, UART_DISABLE);
358 mutex_unlock(&port->serial->disc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359}
360
Craig Shelley39a66b82005-05-27 00:09:56 +0100361/*
362 * cp2101_get_termios
363 * Reads the baud rate, data bits, parity, stop bits and flow control mode
364 * from the device, corrects any unsupported values, and configures the
365 * termios structure to reflect the state of the device
366 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367static void cp2101_get_termios (struct usb_serial_port *port)
368{
Craig Shelley39a66b82005-05-27 00:09:56 +0100369 unsigned int cflag, modem_ctl[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 int baud;
371 int bits;
372
373 dbg("%s - port %d", __FUNCTION__, port->number);
374
Alan Cox01e96d22007-07-26 18:54:12 +0100375 if (!port->tty || !port->tty->termios) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 dbg("%s - no tty structures", __FUNCTION__);
377 return;
378 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Craig Shelley39a66b82005-05-27 00:09:56 +0100380 cp2101_get_config(port, CP2101_BAUDRATE, &baud, 2);
381 /* Convert to baudrate */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 if (baud)
383 baud = BAUD_RATE_GEN_FREQ / baud;
384
385 dbg("%s - baud rate = %d", __FUNCTION__, baud);
Alan Cox9ab0f202007-10-18 01:24:24 -0700386
387 tty_encode_baud_rate(port->tty, baud, baud);
388 cflag = port->tty->termios->c_cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Craig Shelley39a66b82005-05-27 00:09:56 +0100390 cp2101_get_config(port, CP2101_BITS, &bits, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 cflag &= ~CSIZE;
392 switch(bits & BITS_DATA_MASK) {
Craig Shelley39a66b82005-05-27 00:09:56 +0100393 case BITS_DATA_5:
394 dbg("%s - data bits = 5", __FUNCTION__);
395 cflag |= CS5;
396 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 case BITS_DATA_6:
398 dbg("%s - data bits = 6", __FUNCTION__);
399 cflag |= CS6;
400 break;
401 case BITS_DATA_7:
402 dbg("%s - data bits = 7", __FUNCTION__);
403 cflag |= CS7;
404 break;
405 case BITS_DATA_8:
406 dbg("%s - data bits = 8", __FUNCTION__);
407 cflag |= CS8;
408 break;
409 case BITS_DATA_9:
410 dbg("%s - data bits = 9 (not supported, "
411 "using 8 data bits)", __FUNCTION__);
412 cflag |= CS8;
413 bits &= ~BITS_DATA_MASK;
414 bits |= BITS_DATA_8;
Craig Shelley39a66b82005-05-27 00:09:56 +0100415 cp2101_set_config(port, CP2101_BITS, &bits, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 break;
417 default:
418 dbg("%s - Unknown number of data bits, "
419 "using 8", __FUNCTION__);
420 cflag |= CS8;
421 bits &= ~BITS_DATA_MASK;
422 bits |= BITS_DATA_8;
Craig Shelley39a66b82005-05-27 00:09:56 +0100423 cp2101_set_config(port, CP2101_BITS, &bits, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 break;
425 }
426
427 switch(bits & BITS_PARITY_MASK) {
428 case BITS_PARITY_NONE:
429 dbg("%s - parity = NONE", __FUNCTION__);
430 cflag &= ~PARENB;
431 break;
432 case BITS_PARITY_ODD:
433 dbg("%s - parity = ODD", __FUNCTION__);
434 cflag |= (PARENB|PARODD);
435 break;
436 case BITS_PARITY_EVEN:
437 dbg("%s - parity = EVEN", __FUNCTION__);
438 cflag &= ~PARODD;
439 cflag |= PARENB;
440 break;
441 case BITS_PARITY_MARK:
442 dbg("%s - parity = MARK (not supported, "
443 "disabling parity)", __FUNCTION__);
444 cflag &= ~PARENB;
445 bits &= ~BITS_PARITY_MASK;
Craig Shelley39a66b82005-05-27 00:09:56 +0100446 cp2101_set_config(port, CP2101_BITS, &bits, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 break;
448 case BITS_PARITY_SPACE:
449 dbg("%s - parity = SPACE (not supported, "
450 "disabling parity)", __FUNCTION__);
451 cflag &= ~PARENB;
452 bits &= ~BITS_PARITY_MASK;
Craig Shelley39a66b82005-05-27 00:09:56 +0100453 cp2101_set_config(port, CP2101_BITS, &bits, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 break;
455 default:
456 dbg("%s - Unknown parity mode, "
457 "disabling parity", __FUNCTION__);
458 cflag &= ~PARENB;
459 bits &= ~BITS_PARITY_MASK;
Craig Shelley39a66b82005-05-27 00:09:56 +0100460 cp2101_set_config(port, CP2101_BITS, &bits, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 break;
462 }
463
464 cflag &= ~CSTOPB;
465 switch(bits & BITS_STOP_MASK) {
466 case BITS_STOP_1:
467 dbg("%s - stop bits = 1", __FUNCTION__);
468 break;
469 case BITS_STOP_1_5:
470 dbg("%s - stop bits = 1.5 (not supported, "
Craig Shelley39a66b82005-05-27 00:09:56 +0100471 "using 1 stop bit)", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 bits &= ~BITS_STOP_MASK;
Craig Shelley39a66b82005-05-27 00:09:56 +0100473 cp2101_set_config(port, CP2101_BITS, &bits, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 break;
475 case BITS_STOP_2:
476 dbg("%s - stop bits = 2", __FUNCTION__);
477 cflag |= CSTOPB;
478 break;
479 default:
480 dbg("%s - Unknown number of stop bits, "
481 "using 1 stop bit", __FUNCTION__);
482 bits &= ~BITS_STOP_MASK;
Craig Shelley39a66b82005-05-27 00:09:56 +0100483 cp2101_set_config(port, CP2101_BITS, &bits, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 break;
485 }
486
Craig Shelley39a66b82005-05-27 00:09:56 +0100487 cp2101_get_config(port, CP2101_MODEMCTL, modem_ctl, 16);
488 if (modem_ctl[0] & 0x0008) {
489 dbg("%s - flow control = CRTSCTS", __FUNCTION__);
490 cflag |= CRTSCTS;
491 } else {
492 dbg("%s - flow control = NONE", __FUNCTION__);
493 cflag &= ~CRTSCTS;
494 }
495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 port->tty->termios->c_cflag = cflag;
497}
498
499static void cp2101_set_termios (struct usb_serial_port *port,
Alan Cox606d0992006-12-08 02:38:45 -0800500 struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
Alan Cox9ab0f202007-10-18 01:24:24 -0700502 unsigned int cflag, old_cflag;
Craig Shelley39a66b82005-05-27 00:09:56 +0100503 int baud=0, bits;
504 unsigned int modem_ctl[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
506 dbg("%s - port %d", __FUNCTION__, port->number);
507
Alan Coxc4e41562007-08-22 23:06:48 +0100508 if (!port->tty || !port->tty->termios) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 dbg("%s - no tty structures", __FUNCTION__);
510 return;
511 }
Alan Cox9ab0f202007-10-18 01:24:24 -0700512 port->tty->termios->c_cflag &= ~CMSPAR;
513
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 cflag = port->tty->termios->c_cflag;
Alan Cox01e96d22007-07-26 18:54:12 +0100515 old_cflag = old_termios->c_cflag;
516 baud = tty_get_baud_rate(port->tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
518 /* If the baud rate is to be updated*/
Alan Cox01e96d22007-07-26 18:54:12 +0100519 if (baud != tty_termios_baud_rate(old_termios)) {
520 switch (baud) {
521 case 0:
522 case 600:
523 case 1200:
524 case 1800:
525 case 2400:
526 case 4800:
527 case 7200:
528 case 9600:
529 case 14400:
530 case 19200:
531 case 28800:
532 case 38400:
533 case 55854:
534 case 57600:
535 case 115200:
536 case 127117:
537 case 230400:
538 case 460800:
539 case 921600:
540 case 3686400:
541 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 default:
Alan Cox01e96d22007-07-26 18:54:12 +0100543 baud = 9600;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 break;
545 }
546
547 if (baud) {
548 dbg("%s - Setting baud rate to %d baud", __FUNCTION__,
549 baud);
Craig Shelley39a66b82005-05-27 00:09:56 +0100550 if (cp2101_set_config_single(port, CP2101_BAUDRATE,
Alan Cox9ab0f202007-10-18 01:24:24 -0700551 (BAUD_RATE_GEN_FREQ / baud))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 dev_err(&port->dev, "Baud rate requested not "
553 "supported by device\n");
Alan Cox9ab0f202007-10-18 01:24:24 -0700554 baud = tty_termios_baud_rate(old_termios);
555 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 }
557 }
Alan Cox9ab0f202007-10-18 01:24:24 -0700558 /* Report back the resulting baud rate */
559 tty_encode_baud_rate(port->tty, baud, baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
Craig Shelley39a66b82005-05-27 00:09:56 +0100561 /* If the number of data bits is to be updated */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 if ((cflag & CSIZE) != (old_cflag & CSIZE)) {
Craig Shelley39a66b82005-05-27 00:09:56 +0100563 cp2101_get_config(port, CP2101_BITS, &bits, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 bits &= ~BITS_DATA_MASK;
565 switch (cflag & CSIZE) {
Craig Shelley39a66b82005-05-27 00:09:56 +0100566 case CS5:
567 bits |= BITS_DATA_5;
568 dbg("%s - data bits = 5", __FUNCTION__);
569 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 case CS6:
571 bits |= BITS_DATA_6;
572 dbg("%s - data bits = 6", __FUNCTION__);
573 break;
574 case CS7:
575 bits |= BITS_DATA_7;
576 dbg("%s - data bits = 7", __FUNCTION__);
577 break;
578 case CS8:
579 bits |= BITS_DATA_8;
580 dbg("%s - data bits = 8", __FUNCTION__);
581 break;
582 /*case CS9:
583 bits |= BITS_DATA_9;
584 dbg("%s - data bits = 9", __FUNCTION__);
585 break;*/
586 default:
587 dev_err(&port->dev, "cp2101 driver does not "
588 "support the number of bits requested,"
589 " using 8 bit mode\n");
590 bits |= BITS_DATA_8;
591 break;
592 }
Craig Shelley39a66b82005-05-27 00:09:56 +0100593 if (cp2101_set_config(port, CP2101_BITS, &bits, 2))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 dev_err(&port->dev, "Number of data bits requested "
595 "not supported by device\n");
596 }
597
598 if ((cflag & (PARENB|PARODD)) != (old_cflag & (PARENB|PARODD))) {
Craig Shelley39a66b82005-05-27 00:09:56 +0100599 cp2101_get_config(port, CP2101_BITS, &bits, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 bits &= ~BITS_PARITY_MASK;
601 if (cflag & PARENB) {
602 if (cflag & PARODD) {
603 bits |= BITS_PARITY_ODD;
604 dbg("%s - parity = ODD", __FUNCTION__);
605 } else {
606 bits |= BITS_PARITY_EVEN;
607 dbg("%s - parity = EVEN", __FUNCTION__);
608 }
609 }
Craig Shelley39a66b82005-05-27 00:09:56 +0100610 if (cp2101_set_config(port, CP2101_BITS, &bits, 2))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 dev_err(&port->dev, "Parity mode not supported "
612 "by device\n");
613 }
614
615 if ((cflag & CSTOPB) != (old_cflag & CSTOPB)) {
Craig Shelley39a66b82005-05-27 00:09:56 +0100616 cp2101_get_config(port, CP2101_BITS, &bits, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 bits &= ~BITS_STOP_MASK;
618 if (cflag & CSTOPB) {
619 bits |= BITS_STOP_2;
620 dbg("%s - stop bits = 2", __FUNCTION__);
621 } else {
622 bits |= BITS_STOP_1;
623 dbg("%s - stop bits = 1", __FUNCTION__);
624 }
Craig Shelley39a66b82005-05-27 00:09:56 +0100625 if (cp2101_set_config(port, CP2101_BITS, &bits, 2))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 dev_err(&port->dev, "Number of stop bits requested "
627 "not supported by device\n");
628 }
Craig Shelley39a66b82005-05-27 00:09:56 +0100629
630 if ((cflag & CRTSCTS) != (old_cflag & CRTSCTS)) {
631 cp2101_get_config(port, CP2101_MODEMCTL, modem_ctl, 16);
632 dbg("%s - read modem controls = 0x%.4x 0x%.4x 0x%.4x 0x%.4x",
633 __FUNCTION__, modem_ctl[0], modem_ctl[1],
634 modem_ctl[2], modem_ctl[3]);
635
636 if (cflag & CRTSCTS) {
637 modem_ctl[0] &= ~0x7B;
638 modem_ctl[0] |= 0x09;
639 modem_ctl[1] = 0x80;
640 dbg("%s - flow control = CRTSCTS", __FUNCTION__);
641 } else {
642 modem_ctl[0] &= ~0x7B;
643 modem_ctl[0] |= 0x01;
644 modem_ctl[1] |= 0x40;
645 dbg("%s - flow control = NONE", __FUNCTION__);
646 }
647
648 dbg("%s - write modem controls = 0x%.4x 0x%.4x 0x%.4x 0x%.4x",
649 __FUNCTION__, modem_ctl[0], modem_ctl[1],
650 modem_ctl[2], modem_ctl[3]);
651 cp2101_set_config(port, CP2101_MODEMCTL, modem_ctl, 16);
652 }
653
654}
655
656static int cp2101_tiocmset (struct usb_serial_port *port, struct file *file,
657 unsigned int set, unsigned int clear)
658{
659 int control = 0;
660
661 dbg("%s - port %d", __FUNCTION__, port->number);
662
663 if (set & TIOCM_RTS) {
664 control |= CONTROL_RTS;
665 control |= CONTROL_WRITE_RTS;
666 }
667 if (set & TIOCM_DTR) {
668 control |= CONTROL_DTR;
669 control |= CONTROL_WRITE_DTR;
670 }
671 if (clear & TIOCM_RTS) {
672 control &= ~CONTROL_RTS;
673 control |= CONTROL_WRITE_RTS;
674 }
675 if (clear & TIOCM_DTR) {
676 control &= ~CONTROL_DTR;
677 control |= CONTROL_WRITE_DTR;
678 }
679
680 dbg("%s - control = 0x%.4x", __FUNCTION__, control);
681
682 return cp2101_set_config(port, CP2101_CONTROL, &control, 2);
683
684}
685
686static int cp2101_tiocmget (struct usb_serial_port *port, struct file *file)
687{
688 int control, result;
689
690 dbg("%s - port %d", __FUNCTION__, port->number);
691
692 cp2101_get_config(port, CP2101_CONTROL, &control, 1);
693
694 result = ((control & CONTROL_DTR) ? TIOCM_DTR : 0)
695 |((control & CONTROL_RTS) ? TIOCM_RTS : 0)
696 |((control & CONTROL_CTS) ? TIOCM_CTS : 0)
697 |((control & CONTROL_DSR) ? TIOCM_DSR : 0)
698 |((control & CONTROL_RING)? TIOCM_RI : 0)
699 |((control & CONTROL_DCD) ? TIOCM_CD : 0);
700
701 dbg("%s - control = 0x%.2x", __FUNCTION__, control);
702
703 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704}
705
706static void cp2101_break_ctl (struct usb_serial_port *port, int break_state)
707{
Craig Shelley39a66b82005-05-27 00:09:56 +0100708 int state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
710 dbg("%s - port %d", __FUNCTION__, port->number);
711 if (break_state == 0)
712 state = BREAK_OFF;
713 else
714 state = BREAK_ON;
715 dbg("%s - turning break %s", __FUNCTION__,
716 state==BREAK_OFF ? "off" : "on");
Craig Shelley39a66b82005-05-27 00:09:56 +0100717 cp2101_set_config(port, CP2101_BREAK, &state, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718}
719
720static int cp2101_startup (struct usb_serial *serial)
721{
Craig Shelley39a66b82005-05-27 00:09:56 +0100722 /* CP2101 buffers behave strangely unless device is reset */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 usb_reset_device(serial->dev);
724 return 0;
725}
726
727static void cp2101_shutdown (struct usb_serial *serial)
728{
729 int i;
730
731 dbg("%s", __FUNCTION__);
732
Craig Shelley39a66b82005-05-27 00:09:56 +0100733 /* Stop reads and writes on all ports */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 for (i=0; i < serial->num_ports; ++i) {
735 cp2101_cleanup(serial->port[i]);
736 }
737}
738
739static int __init cp2101_init (void)
740{
741 int retval;
742
743 retval = usb_serial_register(&cp2101_device);
744 if (retval)
Craig Shelley39a66b82005-05-27 00:09:56 +0100745 return retval; /* Failed to register */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
747 retval = usb_register(&cp2101_driver);
748 if (retval) {
Craig Shelley39a66b82005-05-27 00:09:56 +0100749 /* Failed to register */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 usb_serial_deregister(&cp2101_device);
751 return retval;
752 }
753
Craig Shelley39a66b82005-05-27 00:09:56 +0100754 /* Success */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 info(DRIVER_DESC " " DRIVER_VERSION);
756 return 0;
757}
758
759static void __exit cp2101_exit (void)
760{
761 usb_deregister (&cp2101_driver);
762 usb_serial_deregister (&cp2101_device);
763}
764
765module_init(cp2101_init);
766module_exit(cp2101_exit);
767
768MODULE_DESCRIPTION(DRIVER_DESC);
769MODULE_VERSION(DRIVER_VERSION);
770MODULE_LICENSE("GPL");
771
772module_param(debug, bool, S_IRUGO | S_IWUSR);
773MODULE_PARM_DESC(debug, "Enable verbose debugging messages");