]> err.no Git - linux-2.6/blob - drivers/usb/serial/cp2101.c
USB: Remove EXPERIMENTAL designation from USB serial/ Kconfig entries
[linux-2.6] / drivers / usb / serial / cp2101.c
1 /*
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  *
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
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  *
18  */
19
20 #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>
29 #include <linux/usb/serial.h>
30
31 /*
32  * Version Information
33  */
34 #define DRIVER_VERSION "v0.07"
35 #define DRIVER_DESC "Silicon Labs CP2101/CP2102 RS232 serial adaptor driver"
36
37 /*
38  * Function Prototypes
39  */
40 static int cp2101_open(struct usb_serial_port*, struct file*);
41 static void cp2101_cleanup(struct usb_serial_port*);
42 static void cp2101_close(struct usb_serial_port*, struct file*);
43 static void cp2101_get_termios(struct usb_serial_port*);
44 static void cp2101_set_termios(struct usb_serial_port*, struct ktermios*);
45 static int cp2101_tiocmget (struct usb_serial_port *, struct file *);
46 static int cp2101_tiocmset (struct usb_serial_port *, struct file *,
47                 unsigned int, unsigned int);
48 static void cp2101_break_ctl(struct usb_serial_port*, int);
49 static int cp2101_startup (struct usb_serial *);
50 static void cp2101_shutdown(struct usb_serial*);
51
52
53 static int debug;
54
55 static struct usb_device_id id_table [] = {
56         { USB_DEVICE(0x0489, 0xE000) }, /* Pirelli Broadband S.p.A, DP-L10 SIP/GSM Mobile */
57         { USB_DEVICE(0x08e6, 0x5501) }, /* Gemalto Prox-PU/CU contactless smartcard reader */
58         { USB_DEVICE(0x0FCF, 0x1003) }, /* Dynastream ANT development board */
59         { USB_DEVICE(0x0FCF, 0x1004) }, /* Dynastream ANT2USB */
60         { USB_DEVICE(0x0FCF, 0x1006) }, /* Dynastream ANT development board */
61         { USB_DEVICE(0x10A6, 0xAA26) }, /* Knock-off DCU-11 cable */
62         { USB_DEVICE(0x10AB, 0x10C5) }, /* Siemens MC60 Cable */
63         { USB_DEVICE(0x10B5, 0xAC70) }, /* Nokia CA-42 USB */
64         { USB_DEVICE(0x10C4, 0x800A) }, /* SPORTident BSM7-D-USB main station */
65         { USB_DEVICE(0x10C4, 0x803B) }, /* Pololu USB-serial converter */
66         { USB_DEVICE(0x10C4, 0x8053) }, /* Enfora EDG1228 */
67         { USB_DEVICE(0x10C4, 0x8066) }, /* Argussoft In-System Programmer */
68         { USB_DEVICE(0x10C4, 0x807A) }, /* Crumb128 board */
69         { USB_DEVICE(0x10C4, 0x80CA) }, /* Degree Controls Inc */
70         { USB_DEVICE(0x10C4, 0x80DD) }, /* Tracient RFID */
71         { USB_DEVICE(0x10C4, 0x80F6) }, /* Suunto sports instrument */
72         { USB_DEVICE(0x10C4, 0x813D) }, /* Burnside Telecom Deskmobile */
73         { USB_DEVICE(0x10C4, 0x814A) }, /* West Mountain Radio RIGblaster P&P */
74         { USB_DEVICE(0x10C4, 0x814B) }, /* West Mountain Radio RIGtalk */
75         { USB_DEVICE(0x10C4, 0x815E) }, /* Helicomm IP-Link 1220-DVM */
76         { USB_DEVICE(0x10C4, 0x81AC) }, /* MSD Dash Hawk */
77         { USB_DEVICE(0x10C4, 0x81C8) }, /* Lipowsky Industrie Elektronik GmbH, Baby-JTAG */
78         { USB_DEVICE(0x10C4, 0x81E2) }, /* Lipowsky Industrie Elektronik GmbH, Baby-LIN */
79         { USB_DEVICE(0x10C4, 0x81E7) }, /* Aerocomm Radio */
80         { USB_DEVICE(0x10C4, 0x8218) }, /* Lipowsky Industrie Elektronik GmbH, HARP-1 */
81         { USB_DEVICE(0x10c4, 0x8293) }, /* Telegesys ETRX2USB */
82         { USB_DEVICE(0x10C4, 0xEA60) }, /* Silicon Labs factory default */
83         { USB_DEVICE(0x10C4, 0xEA61) }, /* Silicon Labs factory default */
84         { 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 */
88         { USB_DEVICE(0x10C5, 0xEA61) }, /* Silicon Labs MobiData GPRS USB Modem */
89         { USB_DEVICE(0x13AD, 0x9999) }, /* Baltech card reader */
90         { USB_DEVICE(0x166A, 0x0303) }, /* Clipsal 5500PCU C-Bus USB interface */
91         { USB_DEVICE(0x16D6, 0x0001) }, /* Jablotron serial interface */
92         { } /* Terminating Entry */
93 };
94
95 MODULE_DEVICE_TABLE (usb, id_table);
96
97 static struct usb_driver cp2101_driver = {
98         .name           = "cp2101",
99         .probe          = usb_serial_probe,
100         .disconnect     = usb_serial_disconnect,
101         .id_table       = id_table,
102         .no_dynamic_id  =       1,
103 };
104
105 static struct usb_serial_driver cp2101_device = {
106         .driver = {
107                 .owner =        THIS_MODULE,
108                 .name =         "cp2101",
109         },
110         .usb_driver             = &cp2101_driver,
111         .id_table               = id_table,
112         .num_interrupt_in       = 0,
113         .num_bulk_in            = NUM_DONT_CARE,
114         .num_bulk_out           = NUM_DONT_CARE,
115         .num_ports              = 1,
116         .open                   = cp2101_open,
117         .close                  = cp2101_close,
118         .break_ctl              = cp2101_break_ctl,
119         .set_termios            = cp2101_set_termios,
120         .tiocmget               = cp2101_tiocmget,
121         .tiocmset               = cp2101_tiocmset,
122         .attach                 = cp2101_startup,
123         .shutdown               = cp2101_shutdown,
124 };
125
126 /* Config request types */
127 #define REQTYPE_HOST_TO_DEVICE  0x41
128 #define REQTYPE_DEVICE_TO_HOST  0xc1
129
130 /* 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 ??? */
138
139 /* CP2101_UART */
140 #define UART_ENABLE             0x0001
141 #define UART_DISABLE            0x0000
142
143 /* CP2101_BAUDRATE */
144 #define BAUD_RATE_GEN_FREQ      0x384000
145
146 /* CP2101_BITS */
147 #define BITS_DATA_MASK          0X0f00
148 #define BITS_DATA_5             0X0500
149 #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
165
166 /* CP2101_BREAK */
167 #define BREAK_ON                0x0000
168 #define BREAK_OFF               0x0001
169
170 /* 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
179
180 /*
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  */
187 static int cp2101_get_config(struct usb_serial_port* port, u8 request,
188                 unsigned int *data, int size)
189 {
190         struct usb_serial *serial = port->serial;
191         __le32 *buf;
192         int result, i, length;
193
194         /* Number of integers required to contain the array */
195         length = (((size - 1) | 3) + 1)/4;
196
197         buf = kcalloc(length, sizeof(__le32), GFP_KERNEL);
198         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 */
204         request++;
205
206         /* Issue the request, attempting to read 'size' bytes */
207         result = usb_control_msg (serial->dev,usb_rcvctrlpipe (serial->dev, 0),
208                                 request, REQTYPE_DEVICE_TO_HOST, 0x0000,
209                                 0, buf, size, 300);
210
211         /* 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) {
218                 dev_err(&port->dev, "%s - Unable to send config request, "
219                                 "request=0x%x size=%d result=%d\n",
220                                 __FUNCTION__, request, size, result);
221                 return -EPROTO;
222         }
223
224         return 0;
225 }
226
227 /*
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  */
233 static 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;
237         __le32 *buf;
238         int result, i, length;
239
240         /* Number of integers required to contain the array */
241         length = (((size - 1) | 3) + 1)/4;
242
243         buf = kmalloc(length * sizeof(__le32), GFP_KERNEL);
244         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  */
288 static 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
294 static 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
301         if (cp2101_set_config_single(port, CP2101_UART, UART_ENABLE)) {
302                 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
322         /* Configure the termios structure */
323         cp2101_get_termios(port);
324
325         /* Set the DTR and RTS pins low */
326         cp2101_tiocmset(port, NULL, TIOCM_DTR | TIOCM_RTS, 0);
327
328         return 0;
329 }
330
331 static 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
346 static 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
355         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);
359 }
360
361 /*
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  */
367 static void cp2101_get_termios (struct usb_serial_port *port)
368 {
369         unsigned int cflag, modem_ctl[4];
370         int baud;
371         int bits;
372
373         dbg("%s - port %d", __FUNCTION__, port->number);
374
375         if (!port->tty || !port->tty->termios) {
376                 dbg("%s - no tty structures", __FUNCTION__);
377                 return;
378         }
379
380         cp2101_get_config(port, CP2101_BAUDRATE, &baud, 2);
381         /* Convert to baudrate */
382         if (baud)
383                 baud = BAUD_RATE_GEN_FREQ / baud;
384
385         dbg("%s - baud rate = %d", __FUNCTION__, baud);
386
387         tty_encode_baud_rate(port->tty, baud, baud);
388         cflag = port->tty->termios->c_cflag;
389
390         cp2101_get_config(port, CP2101_BITS, &bits, 2);
391         cflag &= ~CSIZE;
392         switch(bits & BITS_DATA_MASK) {
393                 case BITS_DATA_5:
394                         dbg("%s - data bits = 5", __FUNCTION__);
395                         cflag |= CS5;
396                         break;
397                 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;
415                         cp2101_set_config(port, CP2101_BITS, &bits, 2);
416                         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;
423                         cp2101_set_config(port, CP2101_BITS, &bits, 2);
424                         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;
446                         cp2101_set_config(port, CP2101_BITS, &bits, 2);
447                         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;
453                         cp2101_set_config(port, CP2101_BITS, &bits, 2);
454                         break;
455                 default:
456                         dbg("%s - Unknown parity mode, "
457                                         "disabling parity", __FUNCTION__);
458                         cflag &= ~PARENB;
459                         bits &= ~BITS_PARITY_MASK;
460                         cp2101_set_config(port, CP2101_BITS, &bits, 2);
461                         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, "
471                                         "using 1 stop bit)", __FUNCTION__);
472                         bits &= ~BITS_STOP_MASK;
473                         cp2101_set_config(port, CP2101_BITS, &bits, 2);
474                         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;
483                         cp2101_set_config(port, CP2101_BITS, &bits, 2);
484                         break;
485         }
486
487         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
496         port->tty->termios->c_cflag = cflag;
497 }
498
499 static void cp2101_set_termios (struct usb_serial_port *port,
500                 struct ktermios *old_termios)
501 {
502         unsigned int cflag, old_cflag;
503         int baud=0, bits;
504         unsigned int modem_ctl[4];
505
506         dbg("%s - port %d", __FUNCTION__, port->number);
507
508         if (!port->tty || !port->tty->termios) {
509                 dbg("%s - no tty structures", __FUNCTION__);
510                 return;
511         }
512         port->tty->termios->c_cflag &= ~CMSPAR;
513
514         cflag = port->tty->termios->c_cflag;
515         old_cflag = old_termios->c_cflag;
516         baud = tty_get_baud_rate(port->tty);
517
518         /* If the baud rate is to be updated*/
519         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;
542                         default:
543                                 baud = 9600;
544                                 break;
545                 }
546
547                 if (baud) {
548                         dbg("%s - Setting baud rate to %d baud", __FUNCTION__,
549                                         baud);
550                         if (cp2101_set_config_single(port, CP2101_BAUDRATE,
551                                                 (BAUD_RATE_GEN_FREQ / baud))) {
552                                 dev_err(&port->dev, "Baud rate requested not "
553                                                 "supported by device\n");
554                                 baud = tty_termios_baud_rate(old_termios);
555                         }
556                 }
557         }
558         /* Report back the resulting baud rate */
559         tty_encode_baud_rate(port->tty, baud, baud);
560
561         /* If the number of data bits is to be updated */
562         if ((cflag & CSIZE) != (old_cflag & CSIZE)) {
563                 cp2101_get_config(port, CP2101_BITS, &bits, 2);
564                 bits &= ~BITS_DATA_MASK;
565                 switch (cflag & CSIZE) {
566                         case CS5:
567                                 bits |= BITS_DATA_5;
568                                 dbg("%s - data bits = 5", __FUNCTION__);
569                                 break;
570                         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                 }
593                 if (cp2101_set_config(port, CP2101_BITS, &bits, 2))
594                         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))) {
599                 cp2101_get_config(port, CP2101_BITS, &bits, 2);
600                 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                 }
610                 if (cp2101_set_config(port, CP2101_BITS, &bits, 2))
611                         dev_err(&port->dev, "Parity mode not supported "
612                                         "by device\n");
613         }
614
615         if ((cflag & CSTOPB) != (old_cflag & CSTOPB)) {
616                 cp2101_get_config(port, CP2101_BITS, &bits, 2);
617                 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                 }
625                 if (cp2101_set_config(port, CP2101_BITS, &bits, 2))
626                         dev_err(&port->dev, "Number of stop bits requested "
627                                         "not supported by device\n");
628         }
629
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
656 static 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
686 static 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;
704 }
705
706 static void cp2101_break_ctl (struct usb_serial_port *port, int break_state)
707 {
708         int state;
709
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");
717         cp2101_set_config(port, CP2101_BREAK, &state, 2);
718 }
719
720 static int cp2101_startup (struct usb_serial *serial)
721 {
722         /* CP2101 buffers behave strangely unless device is reset */
723         usb_reset_device(serial->dev);
724         return 0;
725 }
726
727 static void cp2101_shutdown (struct usb_serial *serial)
728 {
729         int i;
730
731         dbg("%s", __FUNCTION__);
732
733         /* Stop reads and writes on all ports */
734         for (i=0; i < serial->num_ports; ++i) {
735                 cp2101_cleanup(serial->port[i]);
736         }
737 }
738
739 static int __init cp2101_init (void)
740 {
741         int retval;
742
743         retval = usb_serial_register(&cp2101_device);
744         if (retval)
745                 return retval; /* Failed to register */
746
747         retval = usb_register(&cp2101_driver);
748         if (retval) {
749                 /* Failed to register */
750                 usb_serial_deregister(&cp2101_device);
751                 return retval;
752         }
753
754         /* Success */
755         info(DRIVER_DESC " " DRIVER_VERSION);
756         return 0;
757 }
758
759 static void __exit cp2101_exit (void)
760 {
761         usb_deregister (&cp2101_driver);
762         usb_serial_deregister (&cp2101_device);
763 }
764
765 module_init(cp2101_init);
766 module_exit(cp2101_exit);
767
768 MODULE_DESCRIPTION(DRIVER_DESC);
769 MODULE_VERSION(DRIVER_VERSION);
770 MODULE_LICENSE("GPL");
771
772 module_param(debug, bool, S_IRUGO | S_IWUSR);
773 MODULE_PARM_DESC(debug, "Enable verbose debugging messages");