6f4303cef0d8e44c9e4eb022bef0caa6673f15c1
[platform/adaptation/renesas_rcar/renesas_kernel.git] / drivers / usb / serial / mct_u232.c
1 /*
2  * MCT (Magic Control Technology Corp.) USB RS232 Converter Driver
3  *
4  *   Copyright (C) 2000 Wolfgang Grandegger (wolfgang@ces.ch)
5  *
6  *   This program is free software; you can redistribute it and/or modify
7  *   it under the terms of the GNU General Public License as published by
8  *   the Free Software Foundation; either version 2 of the License, or
9  *   (at your option) any later version.
10  *
11  * This program is largely derived from the Belkin USB Serial Adapter Driver
12  * (see belkin_sa.[ch]). All of the information about the device was acquired
13  * by using SniffUSB on Windows98. For technical details see mct_u232.h.
14  *
15  * William G. Greathouse and Greg Kroah-Hartman provided great help on how to
16  * do the reverse engineering and how to write a USB serial device driver.
17  *
18  * TO BE DONE, TO BE CHECKED:
19  *   DTR/RTS signal handling may be incomplete or incorrect. I have mainly
20  *   implemented what I have seen with SniffUSB or found in belkin_sa.c.
21  *   For further TODOs check also belkin_sa.c.
22  */
23
24 #include <linux/kernel.h>
25 #include <linux/errno.h>
26 #include <linux/init.h>
27 #include <linux/slab.h>
28 #include <linux/tty.h>
29 #include <linux/tty_driver.h>
30 #include <linux/tty_flip.h>
31 #include <linux/module.h>
32 #include <linux/spinlock.h>
33 #include <linux/uaccess.h>
34 #include <asm/unaligned.h>
35 #include <linux/usb.h>
36 #include <linux/usb/serial.h>
37 #include <linux/serial.h>
38 #include <linux/ioctl.h>
39 #include "mct_u232.h"
40
41 #define DRIVER_AUTHOR "Wolfgang Grandegger <wolfgang@ces.ch>"
42 #define DRIVER_DESC "Magic Control Technology USB-RS232 converter driver"
43
44 /*
45  * Function prototypes
46  */
47 static int  mct_u232_startup(struct usb_serial *serial);
48 static int  mct_u232_port_probe(struct usb_serial_port *port);
49 static int  mct_u232_port_remove(struct usb_serial_port *remove);
50 static int  mct_u232_open(struct tty_struct *tty, struct usb_serial_port *port);
51 static void mct_u232_close(struct usb_serial_port *port);
52 static void mct_u232_dtr_rts(struct usb_serial_port *port, int on);
53 static void mct_u232_read_int_callback(struct urb *urb);
54 static void mct_u232_set_termios(struct tty_struct *tty,
55                         struct usb_serial_port *port, struct ktermios *old);
56 static void mct_u232_break_ctl(struct tty_struct *tty, int break_state);
57 static int  mct_u232_tiocmget(struct tty_struct *tty);
58 static int  mct_u232_tiocmset(struct tty_struct *tty,
59                         unsigned int set, unsigned int clear);
60 static int  mct_u232_ioctl(struct tty_struct *tty,
61                         unsigned int cmd, unsigned long arg);
62 static void mct_u232_throttle(struct tty_struct *tty);
63 static void mct_u232_unthrottle(struct tty_struct *tty);
64
65
66 /*
67  * All of the device info needed for the MCT USB-RS232 converter.
68  */
69 static const struct usb_device_id id_table[] = {
70         { USB_DEVICE(MCT_U232_VID, MCT_U232_PID) },
71         { USB_DEVICE(MCT_U232_VID, MCT_U232_SITECOM_PID) },
72         { USB_DEVICE(MCT_U232_VID, MCT_U232_DU_H3SP_PID) },
73         { USB_DEVICE(MCT_U232_BELKIN_F5U109_VID, MCT_U232_BELKIN_F5U109_PID) },
74         { }             /* Terminating entry */
75 };
76 MODULE_DEVICE_TABLE(usb, id_table);
77
78 static struct usb_serial_driver mct_u232_device = {
79         .driver = {
80                 .owner =        THIS_MODULE,
81                 .name =         "mct_u232",
82         },
83         .description =       "MCT U232",
84         .id_table =          id_table,
85         .num_ports =         1,
86         .open =              mct_u232_open,
87         .close =             mct_u232_close,
88         .dtr_rts =           mct_u232_dtr_rts,
89         .throttle =          mct_u232_throttle,
90         .unthrottle =        mct_u232_unthrottle,
91         .read_int_callback = mct_u232_read_int_callback,
92         .set_termios =       mct_u232_set_termios,
93         .break_ctl =         mct_u232_break_ctl,
94         .tiocmget =          mct_u232_tiocmget,
95         .tiocmset =          mct_u232_tiocmset,
96         .attach =            mct_u232_startup,
97         .port_probe =        mct_u232_port_probe,
98         .port_remove =       mct_u232_port_remove,
99         .ioctl =             mct_u232_ioctl,
100         .get_icount =        usb_serial_generic_get_icount,
101 };
102
103 static struct usb_serial_driver * const serial_drivers[] = {
104         &mct_u232_device, NULL
105 };
106
107 struct mct_u232_private {
108         spinlock_t lock;
109         unsigned int         control_state; /* Modem Line Setting (TIOCM) */
110         unsigned char        last_lcr;      /* Line Control Register */
111         unsigned char        last_lsr;      /* Line Status Register */
112         unsigned char        last_msr;      /* Modem Status Register */
113         unsigned int         rx_flags;      /* Throttling flags */
114 };
115
116 #define THROTTLED               0x01
117
118 /*
119  * Handle vendor specific USB requests
120  */
121
122 #define WDR_TIMEOUT 5000 /* default urb timeout */
123
124 /*
125  * Later day 2.6.0-test kernels have new baud rates like B230400 which
126  * we do not know how to support. We ignore them for the moment.
127  */
128 static int mct_u232_calculate_baud_rate(struct usb_serial *serial,
129                                         speed_t value, speed_t *result)
130 {
131         *result = value;
132
133         if (le16_to_cpu(serial->dev->descriptor.idProduct) == MCT_U232_SITECOM_PID
134                 || le16_to_cpu(serial->dev->descriptor.idProduct) == MCT_U232_BELKIN_F5U109_PID) {
135                 switch (value) {
136                 case 300:
137                         return 0x01;
138                 case 600:
139                         return 0x02; /* this one not tested */
140                 case 1200:
141                         return 0x03;
142                 case 2400:
143                         return 0x04;
144                 case 4800:
145                         return 0x06;
146                 case 9600:
147                         return 0x08;
148                 case 19200:
149                         return 0x09;
150                 case 38400:
151                         return 0x0a;
152                 case 57600:
153                         return 0x0b;
154                 case 115200:
155                         return 0x0c;
156                 default:
157                         *result = 9600;
158                         return 0x08;
159                 }
160         } else {
161                 /* FIXME: Can we use any divider - should we do
162                    divider = 115200/value;
163                    real baud = 115200/divider */
164                 switch (value) {
165                 case 300: break;
166                 case 600: break;
167                 case 1200: break;
168                 case 2400: break;
169                 case 4800: break;
170                 case 9600: break;
171                 case 19200: break;
172                 case 38400: break;
173                 case 57600: break;
174                 case 115200: break;
175                 default:
176                         value = 9600;
177                         *result = 9600;
178                 }
179                 return 115200/value;
180         }
181 }
182
183 static int mct_u232_set_baud_rate(struct tty_struct *tty,
184         struct usb_serial *serial, struct usb_serial_port *port, speed_t value)
185 {
186         unsigned int divisor;
187         int rc;
188         unsigned char *buf;
189         unsigned char cts_enable_byte = 0;
190         speed_t speed;
191
192         buf = kmalloc(MCT_U232_MAX_SIZE, GFP_KERNEL);
193         if (buf == NULL)
194                 return -ENOMEM;
195
196         divisor = mct_u232_calculate_baud_rate(serial, value, &speed);
197         put_unaligned_le32(cpu_to_le32(divisor), buf);
198         rc = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
199                                 MCT_U232_SET_BAUD_RATE_REQUEST,
200                                 MCT_U232_SET_REQUEST_TYPE,
201                                 0, 0, buf, MCT_U232_SET_BAUD_RATE_SIZE,
202                                 WDR_TIMEOUT);
203         if (rc < 0)     /*FIXME: What value speed results */
204                 dev_err(&port->dev, "Set BAUD RATE %d failed (error = %d)\n",
205                         value, rc);
206         else
207                 tty_encode_baud_rate(tty, speed, speed);
208         dev_dbg(&port->dev, "set_baud_rate: value: 0x%x, divisor: 0x%x\n", value, divisor);
209
210         /* Mimic the MCT-supplied Windows driver (version 1.21P.0104), which
211            always sends two extra USB 'device request' messages after the
212            'baud rate change' message.  The actual functionality of the
213            request codes in these messages is not fully understood but these
214            particular codes are never seen in any operation besides a baud
215            rate change.  Both of these messages send a single byte of data.
216            In the first message, the value of this byte is always zero.
217
218            The second message has been determined experimentally to control
219            whether data will be transmitted to a device which is not asserting
220            the 'CTS' signal.  If the second message's data byte is zero, data
221            will be transmitted even if 'CTS' is not asserted (i.e. no hardware
222            flow control).  if the second message's data byte is nonzero (a
223            value of 1 is used by this driver), data will not be transmitted to
224            a device which is not asserting 'CTS'.
225         */
226
227         buf[0] = 0;
228         rc = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
229                                 MCT_U232_SET_UNKNOWN1_REQUEST,
230                                 MCT_U232_SET_REQUEST_TYPE,
231                                 0, 0, buf, MCT_U232_SET_UNKNOWN1_SIZE,
232                                 WDR_TIMEOUT);
233         if (rc < 0)
234                 dev_err(&port->dev, "Sending USB device request code %d "
235                         "failed (error = %d)\n", MCT_U232_SET_UNKNOWN1_REQUEST,
236                         rc);
237
238         if (port && C_CRTSCTS(tty))
239            cts_enable_byte = 1;
240
241         dev_dbg(&port->dev, "set_baud_rate: send second control message, data = %02X\n",
242                 cts_enable_byte);
243         buf[0] = cts_enable_byte;
244         rc = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
245                         MCT_U232_SET_CTS_REQUEST,
246                         MCT_U232_SET_REQUEST_TYPE,
247                         0, 0, buf, MCT_U232_SET_CTS_SIZE,
248                         WDR_TIMEOUT);
249         if (rc < 0)
250                 dev_err(&port->dev, "Sending USB device request code %d "
251                         "failed (error = %d)\n", MCT_U232_SET_CTS_REQUEST, rc);
252
253         kfree(buf);
254         return rc;
255 } /* mct_u232_set_baud_rate */
256
257 static int mct_u232_set_line_ctrl(struct usb_serial_port *port,
258                                   unsigned char lcr)
259 {
260         int rc;
261         unsigned char *buf;
262
263         buf = kmalloc(MCT_U232_MAX_SIZE, GFP_KERNEL);
264         if (buf == NULL)
265                 return -ENOMEM;
266
267         buf[0] = lcr;
268         rc = usb_control_msg(port->serial->dev, usb_sndctrlpipe(port->serial->dev, 0),
269                         MCT_U232_SET_LINE_CTRL_REQUEST,
270                         MCT_U232_SET_REQUEST_TYPE,
271                         0, 0, buf, MCT_U232_SET_LINE_CTRL_SIZE,
272                         WDR_TIMEOUT);
273         if (rc < 0)
274                 dev_err(&port->dev, "Set LINE CTRL 0x%x failed (error = %d)\n", lcr, rc);
275         dev_dbg(&port->dev, "set_line_ctrl: 0x%x\n", lcr);
276         kfree(buf);
277         return rc;
278 } /* mct_u232_set_line_ctrl */
279
280 static int mct_u232_set_modem_ctrl(struct usb_serial_port *port,
281                                    unsigned int control_state)
282 {
283         int rc;
284         unsigned char mcr;
285         unsigned char *buf;
286
287         buf = kmalloc(MCT_U232_MAX_SIZE, GFP_KERNEL);
288         if (buf == NULL)
289                 return -ENOMEM;
290
291         mcr = MCT_U232_MCR_NONE;
292         if (control_state & TIOCM_DTR)
293                 mcr |= MCT_U232_MCR_DTR;
294         if (control_state & TIOCM_RTS)
295                 mcr |= MCT_U232_MCR_RTS;
296
297         buf[0] = mcr;
298         rc = usb_control_msg(port->serial->dev, usb_sndctrlpipe(port->serial->dev, 0),
299                         MCT_U232_SET_MODEM_CTRL_REQUEST,
300                         MCT_U232_SET_REQUEST_TYPE,
301                         0, 0, buf, MCT_U232_SET_MODEM_CTRL_SIZE,
302                         WDR_TIMEOUT);
303         kfree(buf);
304
305         dev_dbg(&port->dev, "set_modem_ctrl: state=0x%x ==> mcr=0x%x\n", control_state, mcr);
306
307         if (rc < 0) {
308                 dev_err(&port->dev, "Set MODEM CTRL 0x%x failed (error = %d)\n", mcr, rc);
309                 return rc;
310         }
311         return 0;
312 } /* mct_u232_set_modem_ctrl */
313
314 static int mct_u232_get_modem_stat(struct usb_serial_port *port,
315                                    unsigned char *msr)
316 {
317         int rc;
318         unsigned char *buf;
319
320         buf = kmalloc(MCT_U232_MAX_SIZE, GFP_KERNEL);
321         if (buf == NULL) {
322                 *msr = 0;
323                 return -ENOMEM;
324         }
325         rc = usb_control_msg(port->serial->dev, usb_rcvctrlpipe(port->serial->dev, 0),
326                         MCT_U232_GET_MODEM_STAT_REQUEST,
327                         MCT_U232_GET_REQUEST_TYPE,
328                         0, 0, buf, MCT_U232_GET_MODEM_STAT_SIZE,
329                         WDR_TIMEOUT);
330         if (rc < 0) {
331                 dev_err(&port->dev, "Get MODEM STATus failed (error = %d)\n", rc);
332                 *msr = 0;
333         } else {
334                 *msr = buf[0];
335         }
336         dev_dbg(&port->dev, "get_modem_stat: 0x%x\n", *msr);
337         kfree(buf);
338         return rc;
339 } /* mct_u232_get_modem_stat */
340
341 static void mct_u232_msr_to_icount(struct async_icount *icount,
342                                                 unsigned char msr)
343 {
344         /* Translate Control Line states */
345         if (msr & MCT_U232_MSR_DDSR)
346                 icount->dsr++;
347         if (msr & MCT_U232_MSR_DCTS)
348                 icount->cts++;
349         if (msr & MCT_U232_MSR_DRI)
350                 icount->rng++;
351         if (msr & MCT_U232_MSR_DCD)
352                 icount->dcd++;
353 } /* mct_u232_msr_to_icount */
354
355 static void mct_u232_msr_to_state(struct usb_serial_port *port,
356                                   unsigned int *control_state, unsigned char msr)
357 {
358         /* Translate Control Line states */
359         if (msr & MCT_U232_MSR_DSR)
360                 *control_state |=  TIOCM_DSR;
361         else
362                 *control_state &= ~TIOCM_DSR;
363         if (msr & MCT_U232_MSR_CTS)
364                 *control_state |=  TIOCM_CTS;
365         else
366                 *control_state &= ~TIOCM_CTS;
367         if (msr & MCT_U232_MSR_RI)
368                 *control_state |=  TIOCM_RI;
369         else
370                 *control_state &= ~TIOCM_RI;
371         if (msr & MCT_U232_MSR_CD)
372                 *control_state |=  TIOCM_CD;
373         else
374                 *control_state &= ~TIOCM_CD;
375         dev_dbg(&port->dev, "msr_to_state: msr=0x%x ==> state=0x%x\n", msr, *control_state);
376 } /* mct_u232_msr_to_state */
377
378 /*
379  * Driver's tty interface functions
380  */
381
382 static int mct_u232_startup(struct usb_serial *serial)
383 {
384         struct usb_serial_port *port, *rport;
385
386         /* Puh, that's dirty */
387         port = serial->port[0];
388         rport = serial->port[1];
389         /* No unlinking, it wasn't submitted yet. */
390         usb_free_urb(port->read_urb);
391         port->read_urb = rport->interrupt_in_urb;
392         rport->interrupt_in_urb = NULL;
393         port->read_urb->context = port;
394
395         return 0;
396 } /* mct_u232_startup */
397
398 static int mct_u232_port_probe(struct usb_serial_port *port)
399 {
400         struct mct_u232_private *priv;
401
402         priv = kzalloc(sizeof(*priv), GFP_KERNEL);
403         if (!priv)
404                 return -ENOMEM;
405
406         spin_lock_init(&priv->lock);
407
408         usb_set_serial_port_data(port, priv);
409
410         return 0;
411 }
412
413 static int mct_u232_port_remove(struct usb_serial_port *port)
414 {
415         struct mct_u232_private *priv;
416
417         priv = usb_get_serial_port_data(port);
418         kfree(priv);
419
420         return 0;
421 }
422
423 static int  mct_u232_open(struct tty_struct *tty, struct usb_serial_port *port)
424 {
425         struct usb_serial *serial = port->serial;
426         struct mct_u232_private *priv = usb_get_serial_port_data(port);
427         int retval = 0;
428         unsigned int control_state;
429         unsigned long flags;
430         unsigned char last_lcr;
431         unsigned char last_msr;
432
433         /* Compensate for a hardware bug: although the Sitecom U232-P25
434          * device reports a maximum output packet size of 32 bytes,
435          * it seems to be able to accept only 16 bytes (and that's what
436          * SniffUSB says too...)
437          */
438         if (le16_to_cpu(serial->dev->descriptor.idProduct)
439                                                 == MCT_U232_SITECOM_PID)
440                 port->bulk_out_size = 16;
441
442         /* Do a defined restart: the normal serial device seems to
443          * always turn on DTR and RTS here, so do the same. I'm not
444          * sure if this is really necessary. But it should not harm
445          * either.
446          */
447         spin_lock_irqsave(&priv->lock, flags);
448         if (tty && (tty->termios.c_cflag & CBAUD))
449                 priv->control_state = TIOCM_DTR | TIOCM_RTS;
450         else
451                 priv->control_state = 0;
452
453         priv->last_lcr = (MCT_U232_DATA_BITS_8 |
454                           MCT_U232_PARITY_NONE |
455                           MCT_U232_STOP_BITS_1);
456         control_state = priv->control_state;
457         last_lcr = priv->last_lcr;
458         spin_unlock_irqrestore(&priv->lock, flags);
459         mct_u232_set_modem_ctrl(port, control_state);
460         mct_u232_set_line_ctrl(port, last_lcr);
461
462         /* Read modem status and update control state */
463         mct_u232_get_modem_stat(port, &last_msr);
464         spin_lock_irqsave(&priv->lock, flags);
465         priv->last_msr = last_msr;
466         mct_u232_msr_to_state(port, &priv->control_state, priv->last_msr);
467         spin_unlock_irqrestore(&priv->lock, flags);
468
469         retval = usb_submit_urb(port->read_urb, GFP_KERNEL);
470         if (retval) {
471                 dev_err(&port->dev,
472                         "usb_submit_urb(read bulk) failed pipe 0x%x err %d\n",
473                         port->read_urb->pipe, retval);
474                 goto error;
475         }
476
477         retval = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
478         if (retval) {
479                 usb_kill_urb(port->read_urb);
480                 dev_err(&port->dev,
481                         "usb_submit_urb(read int) failed pipe 0x%x err %d",
482                         port->interrupt_in_urb->pipe, retval);
483                 goto error;
484         }
485         return 0;
486
487 error:
488         return retval;
489 } /* mct_u232_open */
490
491 static void mct_u232_dtr_rts(struct usb_serial_port *port, int on)
492 {
493         unsigned int control_state;
494         struct mct_u232_private *priv = usb_get_serial_port_data(port);
495
496         spin_lock_irq(&priv->lock);
497         if (on)
498                 priv->control_state |= TIOCM_DTR | TIOCM_RTS;
499         else
500                 priv->control_state &= ~(TIOCM_DTR | TIOCM_RTS);
501         control_state = priv->control_state;
502         spin_unlock_irq(&priv->lock);
503
504         mct_u232_set_modem_ctrl(port, control_state);
505 }
506
507 static void mct_u232_close(struct usb_serial_port *port)
508 {
509         /*
510          * Must kill the read urb as it is actually an interrupt urb, which
511          * generic close thus fails to kill.
512          */
513         usb_kill_urb(port->read_urb);
514         usb_kill_urb(port->interrupt_in_urb);
515
516         usb_serial_generic_close(port);
517 } /* mct_u232_close */
518
519
520 static void mct_u232_read_int_callback(struct urb *urb)
521 {
522         struct usb_serial_port *port = urb->context;
523         struct mct_u232_private *priv = usb_get_serial_port_data(port);
524         unsigned char *data = urb->transfer_buffer;
525         int retval;
526         int status = urb->status;
527         unsigned long flags;
528
529         switch (status) {
530         case 0:
531                 /* success */
532                 break;
533         case -ECONNRESET:
534         case -ENOENT:
535         case -ESHUTDOWN:
536                 /* this urb is terminated, clean up */
537                 dev_dbg(&port->dev, "%s - urb shutting down with status: %d\n",
538                         __func__, status);
539                 return;
540         default:
541                 dev_dbg(&port->dev, "%s - nonzero urb status received: %d\n",
542                         __func__, status);
543                 goto exit;
544         }
545
546         usb_serial_debug_data(&port->dev, __func__, urb->actual_length, data);
547
548         /*
549          * Work-a-round: handle the 'usual' bulk-in pipe here
550          */
551         if (urb->transfer_buffer_length > 2) {
552                 if (urb->actual_length) {
553                         tty_insert_flip_string(&port->port, data,
554                                         urb->actual_length);
555                         tty_flip_buffer_push(&port->port);
556                 }
557                 goto exit;
558         }
559
560         /*
561          * The interrupt-in pipe signals exceptional conditions (modem line
562          * signal changes and errors). data[0] holds MSR, data[1] holds LSR.
563          */
564         spin_lock_irqsave(&priv->lock, flags);
565         priv->last_msr = data[MCT_U232_MSR_INDEX];
566
567         /* Record Control Line states */
568         mct_u232_msr_to_state(port, &priv->control_state, priv->last_msr);
569
570         mct_u232_msr_to_icount(&port->icount, priv->last_msr);
571
572 #if 0
573         /* Not yet handled. See belkin_sa.c for further information */
574         /* Now to report any errors */
575         priv->last_lsr = data[MCT_U232_LSR_INDEX];
576         /*
577          * fill in the flip buffer here, but I do not know the relation
578          * to the current/next receive buffer or characters.  I need
579          * to look in to this before committing any code.
580          */
581         if (priv->last_lsr & MCT_U232_LSR_ERR) {
582                 tty = tty_port_tty_get(&port->port);
583                 /* Overrun Error */
584                 if (priv->last_lsr & MCT_U232_LSR_OE) {
585                 }
586                 /* Parity Error */
587                 if (priv->last_lsr & MCT_U232_LSR_PE) {
588                 }
589                 /* Framing Error */
590                 if (priv->last_lsr & MCT_U232_LSR_FE) {
591                 }
592                 /* Break Indicator */
593                 if (priv->last_lsr & MCT_U232_LSR_BI) {
594                 }
595                 tty_kref_put(tty);
596         }
597 #endif
598         wake_up_interruptible(&port->delta_msr_wait);
599         spin_unlock_irqrestore(&priv->lock, flags);
600 exit:
601         retval = usb_submit_urb(urb, GFP_ATOMIC);
602         if (retval)
603                 dev_err(&port->dev,
604                         "%s - usb_submit_urb failed with result %d\n",
605                         __func__, retval);
606 } /* mct_u232_read_int_callback */
607
608 static void mct_u232_set_termios(struct tty_struct *tty,
609                                  struct usb_serial_port *port,
610                                  struct ktermios *old_termios)
611 {
612         struct usb_serial *serial = port->serial;
613         struct mct_u232_private *priv = usb_get_serial_port_data(port);
614         struct ktermios *termios = &tty->termios;
615         unsigned int cflag = termios->c_cflag;
616         unsigned int old_cflag = old_termios->c_cflag;
617         unsigned long flags;
618         unsigned int control_state;
619         unsigned char last_lcr;
620
621         /* get a local copy of the current port settings */
622         spin_lock_irqsave(&priv->lock, flags);
623         control_state = priv->control_state;
624         spin_unlock_irqrestore(&priv->lock, flags);
625         last_lcr = 0;
626
627         /*
628          * Update baud rate.
629          * Do not attempt to cache old rates and skip settings,
630          * disconnects screw such tricks up completely.
631          * Premature optimization is the root of all evil.
632          */
633
634         /* reassert DTR and RTS on transition from B0 */
635         if ((old_cflag & CBAUD) == B0) {
636                 dev_dbg(&port->dev, "%s: baud was B0\n", __func__);
637                 control_state |= TIOCM_DTR | TIOCM_RTS;
638                 mct_u232_set_modem_ctrl(port, control_state);
639         }
640
641         mct_u232_set_baud_rate(tty, serial, port, tty_get_baud_rate(tty));
642
643         if ((cflag & CBAUD) == B0) {
644                 dev_dbg(&port->dev, "%s: baud is B0\n", __func__);
645                 /* Drop RTS and DTR */
646                 control_state &= ~(TIOCM_DTR | TIOCM_RTS);
647                 mct_u232_set_modem_ctrl(port, control_state);
648         }
649
650         /*
651          * Update line control register (LCR)
652          */
653
654         /* set the parity */
655         if (cflag & PARENB)
656                 last_lcr |= (cflag & PARODD) ?
657                         MCT_U232_PARITY_ODD : MCT_U232_PARITY_EVEN;
658         else
659                 last_lcr |= MCT_U232_PARITY_NONE;
660
661         /* set the number of data bits */
662         switch (cflag & CSIZE) {
663         case CS5:
664                 last_lcr |= MCT_U232_DATA_BITS_5; break;
665         case CS6:
666                 last_lcr |= MCT_U232_DATA_BITS_6; break;
667         case CS7:
668                 last_lcr |= MCT_U232_DATA_BITS_7; break;
669         case CS8:
670                 last_lcr |= MCT_U232_DATA_BITS_8; break;
671         default:
672                 dev_err(&port->dev,
673                         "CSIZE was not CS5-CS8, using default of 8\n");
674                 last_lcr |= MCT_U232_DATA_BITS_8;
675                 break;
676         }
677
678         termios->c_cflag &= ~CMSPAR;
679
680         /* set the number of stop bits */
681         last_lcr |= (cflag & CSTOPB) ?
682                 MCT_U232_STOP_BITS_2 : MCT_U232_STOP_BITS_1;
683
684         mct_u232_set_line_ctrl(port, last_lcr);
685
686         /* save off the modified port settings */
687         spin_lock_irqsave(&priv->lock, flags);
688         priv->control_state = control_state;
689         priv->last_lcr = last_lcr;
690         spin_unlock_irqrestore(&priv->lock, flags);
691 } /* mct_u232_set_termios */
692
693 static void mct_u232_break_ctl(struct tty_struct *tty, int break_state)
694 {
695         struct usb_serial_port *port = tty->driver_data;
696         struct mct_u232_private *priv = usb_get_serial_port_data(port);
697         unsigned char lcr;
698         unsigned long flags;
699
700         spin_lock_irqsave(&priv->lock, flags);
701         lcr = priv->last_lcr;
702
703         if (break_state)
704                 lcr |= MCT_U232_SET_BREAK;
705         spin_unlock_irqrestore(&priv->lock, flags);
706
707         mct_u232_set_line_ctrl(port, lcr);
708 } /* mct_u232_break_ctl */
709
710
711 static int mct_u232_tiocmget(struct tty_struct *tty)
712 {
713         struct usb_serial_port *port = tty->driver_data;
714         struct mct_u232_private *priv = usb_get_serial_port_data(port);
715         unsigned int control_state;
716         unsigned long flags;
717
718         spin_lock_irqsave(&priv->lock, flags);
719         control_state = priv->control_state;
720         spin_unlock_irqrestore(&priv->lock, flags);
721
722         return control_state;
723 }
724
725 static int mct_u232_tiocmset(struct tty_struct *tty,
726                               unsigned int set, unsigned int clear)
727 {
728         struct usb_serial_port *port = tty->driver_data;
729         struct mct_u232_private *priv = usb_get_serial_port_data(port);
730         unsigned int control_state;
731         unsigned long flags;
732
733         spin_lock_irqsave(&priv->lock, flags);
734         control_state = priv->control_state;
735
736         if (set & TIOCM_RTS)
737                 control_state |= TIOCM_RTS;
738         if (set & TIOCM_DTR)
739                 control_state |= TIOCM_DTR;
740         if (clear & TIOCM_RTS)
741                 control_state &= ~TIOCM_RTS;
742         if (clear & TIOCM_DTR)
743                 control_state &= ~TIOCM_DTR;
744
745         priv->control_state = control_state;
746         spin_unlock_irqrestore(&priv->lock, flags);
747         return mct_u232_set_modem_ctrl(port, control_state);
748 }
749
750 static void mct_u232_throttle(struct tty_struct *tty)
751 {
752         struct usb_serial_port *port = tty->driver_data;
753         struct mct_u232_private *priv = usb_get_serial_port_data(port);
754         unsigned int control_state;
755
756         spin_lock_irq(&priv->lock);
757         priv->rx_flags |= THROTTLED;
758         if (C_CRTSCTS(tty)) {
759                 priv->control_state &= ~TIOCM_RTS;
760                 control_state = priv->control_state;
761                 spin_unlock_irq(&priv->lock);
762                 mct_u232_set_modem_ctrl(port, control_state);
763         } else {
764                 spin_unlock_irq(&priv->lock);
765         }
766 }
767
768 static void mct_u232_unthrottle(struct tty_struct *tty)
769 {
770         struct usb_serial_port *port = tty->driver_data;
771         struct mct_u232_private *priv = usb_get_serial_port_data(port);
772         unsigned int control_state;
773
774         spin_lock_irq(&priv->lock);
775         if ((priv->rx_flags & THROTTLED) && C_CRTSCTS(tty)) {
776                 priv->rx_flags &= ~THROTTLED;
777                 priv->control_state |= TIOCM_RTS;
778                 control_state = priv->control_state;
779                 spin_unlock_irq(&priv->lock);
780                 mct_u232_set_modem_ctrl(port, control_state);
781         } else {
782                 spin_unlock_irq(&priv->lock);
783         }
784 }
785
786 static int  mct_u232_ioctl(struct tty_struct *tty,
787                         unsigned int cmd, unsigned long arg)
788 {
789         DEFINE_WAIT(wait);
790         struct usb_serial_port *port = tty->driver_data;
791         struct mct_u232_private *mct_u232_port = usb_get_serial_port_data(port);
792         struct async_icount cnow, cprev;
793         unsigned long flags;
794
795         dev_dbg(&port->dev, "%s - cmd = 0x%x\n", __func__, cmd);
796
797         switch (cmd) {
798
799         case TIOCMIWAIT:
800
801                 dev_dbg(&port->dev, "%s TIOCMIWAIT", __func__);
802
803                 spin_lock_irqsave(&mct_u232_port->lock, flags);
804                 cprev = port->icount;
805                 spin_unlock_irqrestore(&mct_u232_port->lock, flags);
806                 for ( ; ; ) {
807                         prepare_to_wait(&port->delta_msr_wait,
808                                         &wait, TASK_INTERRUPTIBLE);
809                         schedule();
810                         finish_wait(&port->delta_msr_wait, &wait);
811                         /* see if a signal did it */
812                         if (signal_pending(current))
813                                 return -ERESTARTSYS;
814
815                         if (port->serial->disconnected)
816                                 return -EIO;
817
818                         spin_lock_irqsave(&mct_u232_port->lock, flags);
819                         cnow = port->icount;
820                         spin_unlock_irqrestore(&mct_u232_port->lock, flags);
821                         if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
822                             cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
823                                 return -EIO; /* no change => error */
824                         if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
825                             ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
826                             ((arg & TIOCM_CD)  && (cnow.dcd != cprev.dcd)) ||
827                             ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
828                                 return 0;
829                         }
830                         cprev = cnow;
831                 }
832
833         }
834         return -ENOIOCTLCMD;
835 }
836
837 module_usb_serial_driver(serial_drivers, id_table);
838
839 MODULE_AUTHOR(DRIVER_AUTHOR);
840 MODULE_DESCRIPTION(DRIVER_DESC);
841 MODULE_LICENSE("GPL");