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