813bab37e076febd58ba325f0338fc49d6e1af56
[linux-2.6.git] / 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/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  */
35 #define DRIVER_VERSION "v0.05"
36 #define DRIVER_DESC "Silicon Labs CP2101/CP2102 RS232 serial adaptor driver"
37
38 /*
39  * Function Prototypes
40  */
41 static int cp2101_open(struct usb_serial_port*, struct file*);
42 static void cp2101_cleanup(struct usb_serial_port*);
43 static void cp2101_close(struct usb_serial_port*, struct file*);
44 static void cp2101_get_termios(struct usb_serial_port*);
45 static void cp2101_set_termios(struct usb_serial_port*, struct termios*);
46 static int cp2101_tiocmget (struct usb_serial_port *, struct file *);
47 static int cp2101_tiocmset (struct usb_serial_port *, struct file *,
48                 unsigned int, unsigned int);
49 static void cp2101_break_ctl(struct usb_serial_port*, int);
50 static int cp2101_startup (struct usb_serial *);
51 static void cp2101_shutdown(struct usb_serial*);
52
53
54 static int debug;
55
56 static struct usb_device_id id_table [] = {
57         { USB_DEVICE(0x0FCF, 0x1003) }, /* Dynastream ANT development board */
58         { USB_DEVICE(0x10C4, 0xEA60) }, /* Silicon Labs factory default */
59         { USB_DEVICE(0x10C4, 0x80CA) }, /* Degree Controls Inc */
60         { USB_DEVICE(0x10C4, 0x80F6) }, /* Suunto sports instrument */
61         { USB_DEVICE(0x10A6, 0xAA26) }, /* Knock-off DCU-11 cable */
62         { USB_DEVICE(0x10AB, 0x10C5) }, /* Siemens MC60 Cable */
63         { USB_DEVICE(0x16D6, 0x0001) }, /* Jablotron serial interface */
64         { } /* Terminating Entry */
65 };
66
67 MODULE_DEVICE_TABLE (usb, id_table);
68
69 static struct usb_driver cp2101_driver = {
70         .owner          = THIS_MODULE,
71         .name           = "cp2101",
72         .probe          = usb_serial_probe,
73         .disconnect     = usb_serial_disconnect,
74         .id_table       = id_table,
75         .no_dynamic_id  =       1,
76 };
77
78 static struct usb_serial_driver cp2101_device = {
79         .driver = {
80                 .owner =        THIS_MODULE,
81                 .name =         "cp2101",
82         },
83         .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,
92         .tiocmget               = cp2101_tiocmget,
93         .tiocmset               = cp2101_tiocmset,
94         .attach                 = cp2101_startup,
95         .shutdown               = cp2101_shutdown,
96 };
97
98 /* Config request types */
99 #define REQTYPE_HOST_TO_DEVICE  0x41
100 #define REQTYPE_DEVICE_TO_HOST  0xc1
101
102 /* 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 ??? */
110
111 /* CP2101_UART */
112 #define UART_ENABLE             0x0001
113 #define UART_DISABLE            0x0000
114
115 /* CP2101_BAUDRATE */
116 #define BAUD_RATE_GEN_FREQ      0x384000
117
118 /* CP2101_BITS */
119 #define BITS_DATA_MASK          0X0f00
120 #define BITS_DATA_5             0X0500
121 #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
137
138 /* CP2101_BREAK */
139 #define BREAK_ON                0x0000
140 #define BREAK_OFF               0x0001
141
142 /* 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
151
152 /*
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  */
159 static int cp2101_get_config(struct usb_serial_port* port, u8 request,
160                 unsigned int *data, int size)
161 {
162         struct usb_serial *serial = port->serial;
163         u32 *buf;
164         int result, i, length;
165
166         /* 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 */
178         request++;
179
180         /* Issue the request, attempting to read 'size' bytes */
181         result = usb_control_msg (serial->dev,usb_rcvctrlpipe (serial->dev, 0),
182                                 request, REQTYPE_DEVICE_TO_HOST, 0x0000,
183                                 0, buf, size, 300);
184
185         /* 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) {
192                 dev_err(&port->dev, "%s - Unable to send config request, "
193                                 "request=0x%x size=%d result=%d\n",
194                                 __FUNCTION__, request, size, result);
195                 return -EPROTO;
196         }
197
198         return 0;
199 }
200
201 /*
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  */
207 static 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  */
262 static 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
268 static 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
275         if (cp2101_set_config_single(port, CP2101_UART, UART_ENABLE)) {
276                 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
296         /* Configure the termios structure */
297         cp2101_get_termios(port);
298
299         /* Set the DTR and RTS pins low */
300         cp2101_tiocmset(port, NULL, TIOCM_DTR | TIOCM_RTS, 0);
301
302         return 0;
303 }
304
305 static 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
320 static 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
329         cp2101_set_config_single(port, CP2101_UART, UART_DISABLE);
330 }
331
332 /*
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  */
338 static void cp2101_get_termios (struct usb_serial_port *port)
339 {
340         unsigned int cflag, modem_ctl[4];
341         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
352         cp2101_get_config(port, CP2101_BAUDRATE, &baud, 2);
353         /* Convert to baudrate */
354         if (baud)
355                 baud = BAUD_RATE_GEN_FREQ / baud;
356
357         dbg("%s - baud rate = %d", __FUNCTION__, baud);
358         cflag &= ~CBAUD;
359         switch (baud) {
360                 /*
361                  * The baud rates which are commented out below
362                  * 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;
388                         cp2101_set_config_single(port, CP2101_BAUDRATE,
389                                         (BAUD_RATE_GEN_FREQ/9600));
390                         break;
391         }
392
393         cp2101_get_config(port, CP2101_BITS, &bits, 2);
394         cflag &= ~CSIZE;
395         switch(bits & BITS_DATA_MASK) {
396                 case BITS_DATA_5:
397                         dbg("%s - data bits = 5", __FUNCTION__);
398                         cflag |= CS5;
399                         break;
400                 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;
418                         cp2101_set_config(port, CP2101_BITS, &bits, 2);
419                         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;
426                         cp2101_set_config(port, CP2101_BITS, &bits, 2);
427                         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;
449                         cp2101_set_config(port, CP2101_BITS, &bits, 2);
450                         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;
456                         cp2101_set_config(port, CP2101_BITS, &bits, 2);
457                         break;
458                 default:
459                         dbg("%s - Unknown parity mode, "
460                                         "disabling parity", __FUNCTION__);
461                         cflag &= ~PARENB;
462                         bits &= ~BITS_PARITY_MASK;
463                         cp2101_set_config(port, CP2101_BITS, &bits, 2);
464                         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, "
474                                         "using 1 stop bit)", __FUNCTION__);
475                         bits &= ~BITS_STOP_MASK;
476                         cp2101_set_config(port, CP2101_BITS, &bits, 2);
477                         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;
486                         cp2101_set_config(port, CP2101_BITS, &bits, 2);
487                         break;
488         }
489
490         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
499         port->tty->termios->c_cflag = cflag;
500 }
501
502 static void cp2101_set_termios (struct usb_serial_port *port,
503                 struct termios *old_termios)
504 {
505         unsigned int cflag, old_cflag=0;
506         int baud=0, bits;
507         unsigned int modem_ctl[4];
508
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
517         /* Check that they really want us to change something */
518         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) {
532                         /*
533                          * The baud rates which are commented out below
534                          * 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);
566                         if (cp2101_set_config_single(port, CP2101_BAUDRATE,
567                                                 (BAUD_RATE_GEN_FREQ / baud)))
568                                 dev_err(&port->dev, "Baud rate requested not "
569                                                 "supported by device\n");
570                 }
571         }
572
573         /* If the number of data bits is to be updated */
574         if ((cflag & CSIZE) != (old_cflag & CSIZE)) {
575                 cp2101_get_config(port, CP2101_BITS, &bits, 2);
576                 bits &= ~BITS_DATA_MASK;
577                 switch (cflag & CSIZE) {
578                         case CS5:
579                                 bits |= BITS_DATA_5;
580                                 dbg("%s - data bits = 5", __FUNCTION__);
581                                 break;
582                         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                 }
605                 if (cp2101_set_config(port, CP2101_BITS, &bits, 2))
606                         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))) {
611                 cp2101_get_config(port, CP2101_BITS, &bits, 2);
612                 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                 }
622                 if (cp2101_set_config(port, CP2101_BITS, &bits, 2))
623                         dev_err(&port->dev, "Parity mode not supported "
624                                         "by device\n");
625         }
626
627         if ((cflag & CSTOPB) != (old_cflag & CSTOPB)) {
628                 cp2101_get_config(port, CP2101_BITS, &bits, 2);
629                 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                 }
637                 if (cp2101_set_config(port, CP2101_BITS, &bits, 2))
638                         dev_err(&port->dev, "Number of stop bits requested "
639                                         "not supported by device\n");
640         }
641
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
668 static 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
698 static 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;
716 }
717
718 static void cp2101_break_ctl (struct usb_serial_port *port, int break_state)
719 {
720         int state;
721
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");
729         cp2101_set_config(port, CP2101_BREAK, &state, 2);
730 }
731
732 static int cp2101_startup (struct usb_serial *serial)
733 {
734         /* CP2101 buffers behave strangely unless device is reset */
735         usb_reset_device(serial->dev);
736         return 0;
737 }
738
739 static void cp2101_shutdown (struct usb_serial *serial)
740 {
741         int i;
742
743         dbg("%s", __FUNCTION__);
744
745         /* Stop reads and writes on all ports */
746         for (i=0; i < serial->num_ports; ++i) {
747                 cp2101_cleanup(serial->port[i]);
748         }
749 }
750
751 static int __init cp2101_init (void)
752 {
753         int retval;
754
755         retval = usb_serial_register(&cp2101_device);
756         if (retval)
757                 return retval; /* Failed to register */
758
759         retval = usb_register(&cp2101_driver);
760         if (retval) {
761                 /* Failed to register */
762                 usb_serial_deregister(&cp2101_device);
763                 return retval;
764         }
765
766         /* Success */
767         info(DRIVER_DESC " " DRIVER_VERSION);
768         return 0;
769 }
770
771 static void __exit cp2101_exit (void)
772 {
773         usb_deregister (&cp2101_driver);
774         usb_serial_deregister (&cp2101_device);
775 }
776
777 module_init(cp2101_init);
778 module_exit(cp2101_exit);
779
780 MODULE_DESCRIPTION(DRIVER_DESC);
781 MODULE_VERSION(DRIVER_VERSION);
782 MODULE_LICENSE("GPL");
783
784 module_param(debug, bool, S_IRUGO | S_IWUSR);
785 MODULE_PARM_DESC(debug, "Enable verbose debugging messages");