vserver 2.0 rc7
[linux-2.6.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  * TEST STATUS:
24  *   Basic tests have been performed with minicom/zmodem transfers and
25  *   modem dialing under Linux 2.4.0-test10 (for me it works fine).
26  *
27  * 04-Nov-2003 Bill Marr <marr at flex dot com>
28  *   - Mimic Windows driver by sending 2 USB 'device request' messages
29  *     following normal 'baud rate change' message.  This allows data to be
30  *     transmitted to RS-232 devices which don't assert the 'CTS' signal.
31  *
32  * 10-Nov-2001 Wolfgang Grandegger
33  *   - Fixed an endianess problem with the baudrate selection for PowerPC.
34  *
35  * 06-Dec-2001 Martin Hamilton <martinh@gnu.org>
36  *      Added support for the Belkin F5U109 DB9 adaptor
37  *
38  * 30-May-2001 Greg Kroah-Hartman
39  *      switched from using spinlock to a semaphore, which fixes lots of problems.
40  *
41  * 04-May-2001 Stelian Pop
42  *   - Set the maximum bulk output size for Sitecom U232-P25 model to 16 bytes
43  *     instead of the device reported 32 (using 32 bytes causes many data
44  *     loss, Windows driver uses 16 too).
45  *
46  * 02-May-2001 Stelian Pop
47  *   - Fixed the baud calculation for Sitecom U232-P25 model
48  *
49  * 08-Apr-2001 gb
50  *   - Identify version on module load.
51  *
52  * 06-Jan-2001 Cornel Ciocirlan 
53  *   - Added support for Sitecom U232-P25 model (Product Id 0x0230)
54  *   - Added support for D-Link DU-H3SP USB BAY (Product Id 0x0200)
55  *
56  * 29-Nov-2000 Greg Kroah-Hartman
57  *   - Added device id table to fit with 2.4.0-test11 structure.
58  *   - took out DEAL_WITH_TWO_INT_IN_ENDPOINTS #define as it's not needed
59  *     (lots of things will change if/when the usb-serial core changes to
60  *     handle these issues.
61  *
62  * 27-Nov-2000 Wolfgang Grandegger
63  *   A version for kernel 2.4.0-test10 released to the Linux community 
64  *   (via linux-usb-devel).
65  */
66
67 #include <linux/config.h>
68 #include <linux/kernel.h>
69 #include <linux/errno.h>
70 #include <linux/init.h>
71 #include <linux/slab.h>
72 #include <linux/tty.h>
73 #include <linux/tty_driver.h>
74 #include <linux/tty_flip.h>
75 #include <linux/module.h>
76 #include <linux/spinlock.h>
77 #include <asm/uaccess.h>
78 #include <linux/usb.h>
79 #include "usb-serial.h"
80 #include "mct_u232.h"
81
82 /*
83  * Version Information
84  */
85 #define DRIVER_VERSION "z2.0"           /* Linux in-kernel version */
86 #define DRIVER_AUTHOR "Wolfgang Grandegger <wolfgang@ces.ch>"
87 #define DRIVER_DESC "Magic Control Technology USB-RS232 converter driver"
88
89 static int debug;
90
91 /*
92  * Function prototypes
93  */
94 static int  mct_u232_startup             (struct usb_serial *serial);
95 static void mct_u232_shutdown            (struct usb_serial *serial);
96 static int  mct_u232_open                (struct usb_serial_port *port,
97                                           struct file *filp);
98 static void mct_u232_close               (struct usb_serial_port *port,
99                                           struct file *filp);
100 static void mct_u232_read_int_callback   (struct urb *urb, struct pt_regs *regs);
101 static void mct_u232_set_termios         (struct usb_serial_port *port,
102                                           struct termios * old);
103 static int  mct_u232_ioctl               (struct usb_serial_port *port,
104                                           struct file * file,
105                                           unsigned int cmd,
106                                           unsigned long arg);
107 static void mct_u232_break_ctl           (struct usb_serial_port *port,
108                                           int break_state );
109 static int  mct_u232_tiocmget            (struct usb_serial_port *port,
110                                           struct file *file);
111 static int  mct_u232_tiocmset            (struct usb_serial_port *port,
112                                           struct file *file, unsigned int set,
113                                           unsigned int clear);
114 /*
115  * All of the device info needed for the MCT USB-RS232 converter.
116  */
117 static struct usb_device_id id_table_combined [] = {
118         { USB_DEVICE(MCT_U232_VID, MCT_U232_PID) },
119         { USB_DEVICE(MCT_U232_VID, MCT_U232_SITECOM_PID) },
120         { USB_DEVICE(MCT_U232_VID, MCT_U232_DU_H3SP_PID) },
121         { USB_DEVICE(MCT_U232_BELKIN_F5U109_VID, MCT_U232_BELKIN_F5U109_PID) },
122         { }             /* Terminating entry */
123 };
124
125 MODULE_DEVICE_TABLE (usb, id_table_combined);
126
127 static struct usb_driver mct_u232_driver = {
128         .owner =        THIS_MODULE,
129         .name =         "mct_u232",
130         .probe =        usb_serial_probe,
131         .disconnect =   usb_serial_disconnect,
132         .id_table =     id_table_combined,
133 };
134
135 static struct usb_serial_device_type mct_u232_device = {
136         .owner =             THIS_MODULE,
137         .name =              "MCT U232",
138         .short_name =        "mct_u232",
139         .id_table =          id_table_combined,
140         .num_interrupt_in =  2,
141         .num_bulk_in =       0,
142         .num_bulk_out =      1,
143         .num_ports =         1,
144         .open =              mct_u232_open,
145         .close =             mct_u232_close,
146         .read_int_callback = mct_u232_read_int_callback,
147         .ioctl =             mct_u232_ioctl,
148         .set_termios =       mct_u232_set_termios,
149         .break_ctl =         mct_u232_break_ctl,
150         .tiocmget =          mct_u232_tiocmget,
151         .tiocmset =          mct_u232_tiocmset,
152         .attach =            mct_u232_startup,
153         .shutdown =          mct_u232_shutdown,
154 };
155
156
157 struct mct_u232_private {
158         spinlock_t lock;
159         unsigned int         control_state; /* Modem Line Setting (TIOCM) */
160         unsigned char        last_lcr;      /* Line Control Register */
161         unsigned char        last_lsr;      /* Line Status Register */
162         unsigned char        last_msr;      /* Modem Status Register */
163 };
164
165 /*
166  * Handle vendor specific USB requests
167  */
168
169 #define WDR_TIMEOUT 5000 /* default urb timeout */
170
171 /*
172  * Later day 2.6.0-test kernels have new baud rates like B230400 which
173  * we do not know how to support. We ignore them for the moment.
174  * XXX Rate-limit the error message, it's user triggerable.
175  */
176 static int mct_u232_calculate_baud_rate(struct usb_serial *serial, int value)
177 {
178         if (le16_to_cpu(serial->dev->descriptor.idProduct) == MCT_U232_SITECOM_PID
179           || le16_to_cpu(serial->dev->descriptor.idProduct) == MCT_U232_BELKIN_F5U109_PID) {
180                 switch (value) {
181                 case    B300: return 0x01;
182                 case    B600: return 0x02; /* this one not tested */
183                 case   B1200: return 0x03;
184                 case   B2400: return 0x04;
185                 case   B4800: return 0x06;
186                 case   B9600: return 0x08;
187                 case  B19200: return 0x09;
188                 case  B38400: return 0x0a;
189                 case  B57600: return 0x0b;
190                 case B115200: return 0x0c;
191                 default:
192                         err("MCT USB-RS232: unsupported baudrate request 0x%x,"
193                             " using default of B9600", value);
194                         return 0x08;
195                 }
196         } else {
197                 switch (value) {
198                 case    B300: value =     300; break;
199                 case    B600: value =     600; break;
200                 case   B1200: value =    1200; break;
201                 case   B2400: value =    2400; break;
202                 case   B4800: value =    4800; break;
203                 case   B9600: value =    9600; break;
204                 case  B19200: value =   19200; break;
205                 case  B38400: value =   38400; break;
206                 case  B57600: value =   57600; break;
207                 case B115200: value =  115200; break;
208                 default:
209                         err("MCT USB-RS232: unsupported baudrate request 0x%x,"
210                             " using default of B9600", value);
211                         value = 9600;
212                 }
213                 return 115200/value;
214         }
215 }
216
217 static int mct_u232_set_baud_rate(struct usb_serial *serial, int value)
218 {
219         __le32 divisor;
220         int rc;
221         unsigned char zero_byte = 0;
222
223         divisor = cpu_to_le32(mct_u232_calculate_baud_rate(serial, value));
224
225         rc = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
226                              MCT_U232_SET_BAUD_RATE_REQUEST,
227                              MCT_U232_SET_REQUEST_TYPE,
228                              0, 0, &divisor, MCT_U232_SET_BAUD_RATE_SIZE,
229                              WDR_TIMEOUT);
230         if (rc < 0)
231                 err("Set BAUD RATE %d failed (error = %d)", value, rc);
232         dbg("set_baud_rate: value: 0x%x, divisor: 0x%x", value, divisor);
233
234         /* Mimic the MCT-supplied Windows driver (version 1.21P.0104), which
235            always sends two extra USB 'device request' messages after the
236            'baud rate change' message.  The actual functionality of the
237            request codes in these messages is not fully understood but these
238            particular codes are never seen in any operation besides a baud
239            rate change.  Both of these messages send a single byte of data
240            whose value is always zero.  The second of these two extra messages
241            is required in order for data to be properly written to an RS-232
242            device which does not assert the 'CTS' signal. */
243
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, &zero_byte, MCT_U232_SET_UNKNOWN1_SIZE, 
248                              WDR_TIMEOUT);
249         if (rc < 0)
250                 err("Sending USB device request code %d failed (error = %d)", 
251                     MCT_U232_SET_UNKNOWN1_REQUEST, rc);
252
253         rc = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
254                              MCT_U232_SET_UNKNOWN2_REQUEST, 
255                              MCT_U232_SET_REQUEST_TYPE,
256                              0, 0, &zero_byte, MCT_U232_SET_UNKNOWN2_SIZE, 
257                              WDR_TIMEOUT);
258         if (rc < 0)
259                 err("Sending USB device request code %d failed (error = %d)", 
260                     MCT_U232_SET_UNKNOWN2_REQUEST, rc);
261
262         return rc;
263 } /* mct_u232_set_baud_rate */
264
265 static int mct_u232_set_line_ctrl(struct usb_serial *serial, unsigned char lcr)
266 {
267         int rc;
268         rc = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
269                              MCT_U232_SET_LINE_CTRL_REQUEST,
270                              MCT_U232_SET_REQUEST_TYPE,
271                              0, 0, &lcr, MCT_U232_SET_LINE_CTRL_SIZE,
272                              WDR_TIMEOUT);
273         if (rc < 0)
274                 err("Set LINE CTRL 0x%x failed (error = %d)", lcr, rc);
275         dbg("set_line_ctrl: 0x%x", lcr);
276         return rc;
277 } /* mct_u232_set_line_ctrl */
278
279 static int mct_u232_set_modem_ctrl(struct usb_serial *serial,
280                                    unsigned int control_state)
281 {
282         int rc;
283         unsigned char mcr = MCT_U232_MCR_NONE;
284
285         if (control_state & TIOCM_DTR)
286                 mcr |= MCT_U232_MCR_DTR;
287         if (control_state & TIOCM_RTS)
288                 mcr |= MCT_U232_MCR_RTS;
289
290         rc = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
291                              MCT_U232_SET_MODEM_CTRL_REQUEST,
292                              MCT_U232_SET_REQUEST_TYPE,
293                              0, 0, &mcr, MCT_U232_SET_MODEM_CTRL_SIZE,
294                              WDR_TIMEOUT);
295         if (rc < 0)
296                 err("Set MODEM CTRL 0x%x failed (error = %d)", mcr, rc);
297         dbg("set_modem_ctrl: state=0x%x ==> mcr=0x%x", control_state, mcr);
298
299         return rc;
300 } /* mct_u232_set_modem_ctrl */
301
302 static int mct_u232_get_modem_stat(struct usb_serial *serial, unsigned char *msr)
303 {
304         int rc;
305         rc = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
306                              MCT_U232_GET_MODEM_STAT_REQUEST,
307                              MCT_U232_GET_REQUEST_TYPE,
308                              0, 0, msr, MCT_U232_GET_MODEM_STAT_SIZE,
309                              WDR_TIMEOUT);
310         if (rc < 0) {
311                 err("Get MODEM STATus failed (error = %d)", rc);
312                 *msr = 0;
313         }
314         dbg("get_modem_stat: 0x%x", *msr);
315         return rc;
316 } /* mct_u232_get_modem_stat */
317
318 static void mct_u232_msr_to_state(unsigned int *control_state, unsigned char msr)
319 {
320         /* Translate Control Line states */
321         if (msr & MCT_U232_MSR_DSR)
322                 *control_state |=  TIOCM_DSR;
323         else
324                 *control_state &= ~TIOCM_DSR;
325         if (msr & MCT_U232_MSR_CTS)
326                 *control_state |=  TIOCM_CTS;
327         else
328                 *control_state &= ~TIOCM_CTS;
329         if (msr & MCT_U232_MSR_RI)
330                 *control_state |=  TIOCM_RI;
331         else
332                 *control_state &= ~TIOCM_RI;
333         if (msr & MCT_U232_MSR_CD)
334                 *control_state |=  TIOCM_CD;
335         else
336                 *control_state &= ~TIOCM_CD;
337         dbg("msr_to_state: msr=0x%x ==> state=0x%x", msr, *control_state);
338 } /* mct_u232_msr_to_state */
339
340 /*
341  * Driver's tty interface functions
342  */
343
344 static int mct_u232_startup (struct usb_serial *serial)
345 {
346         struct mct_u232_private *priv;
347         struct usb_serial_port *port, *rport;
348
349         priv = kmalloc(sizeof(struct mct_u232_private), GFP_KERNEL);
350         if (!priv)
351                 return -ENOMEM;
352         memset(priv, 0, sizeof(struct mct_u232_private));
353         spin_lock_init(&priv->lock);
354         usb_set_serial_port_data(serial->port[0], priv);
355
356         init_waitqueue_head(&serial->port[0]->write_wait);
357
358         /* Puh, that's dirty */
359         port = serial->port[0];
360         rport = serial->port[1];
361         if (port->read_urb) {
362                 /* No unlinking, it wasn't submitted yet. */
363                 usb_free_urb(port->read_urb);
364         }
365         port->read_urb = rport->interrupt_in_urb;
366         rport->interrupt_in_urb = NULL;
367         port->read_urb->context = port;
368
369         return (0);
370 } /* mct_u232_startup */
371
372
373 static void mct_u232_shutdown (struct usb_serial *serial)
374 {
375         struct mct_u232_private *priv;
376         int i;
377         
378         dbg("%s", __FUNCTION__);
379
380         for (i=0; i < serial->num_ports; ++i) {
381                 /* My special items, the standard routines free my urbs */
382                 priv = usb_get_serial_port_data(serial->port[i]);
383                 if (priv) {
384                         usb_set_serial_port_data(serial->port[i], NULL);
385                         kfree(priv);
386                 }
387         }
388 } /* mct_u232_shutdown */
389
390 static int  mct_u232_open (struct usb_serial_port *port, struct file *filp)
391 {
392         struct usb_serial *serial = port->serial;
393         struct mct_u232_private *priv = usb_get_serial_port_data(port);
394         int retval = 0;
395         unsigned int control_state;
396         unsigned long flags;
397         unsigned char last_lcr;
398         unsigned char last_msr;
399
400         dbg("%s port %d", __FUNCTION__, port->number);
401
402         /* Compensate for a hardware bug: although the Sitecom U232-P25
403          * device reports a maximum output packet size of 32 bytes,
404          * it seems to be able to accept only 16 bytes (and that's what
405          * SniffUSB says too...)
406          */
407         if (le16_to_cpu(serial->dev->descriptor.idProduct) == MCT_U232_SITECOM_PID)
408                 port->bulk_out_size = 16;
409
410         /* Do a defined restart: the normal serial device seems to 
411          * always turn on DTR and RTS here, so do the same. I'm not
412          * sure if this is really necessary. But it should not harm
413          * either.
414          */
415         spin_lock_irqsave(&priv->lock, flags);
416         if (port->tty->termios->c_cflag & CBAUD)
417                 priv->control_state = TIOCM_DTR | TIOCM_RTS;
418         else
419                 priv->control_state = 0;
420         
421         priv->last_lcr = (MCT_U232_DATA_BITS_8 | 
422                           MCT_U232_PARITY_NONE |
423                           MCT_U232_STOP_BITS_1);
424         control_state = priv->control_state;
425         last_lcr = priv->last_lcr;
426         spin_unlock_irqrestore(&priv->lock, flags);
427         mct_u232_set_modem_ctrl(serial, control_state);
428         mct_u232_set_line_ctrl(serial, last_lcr);
429
430         /* Read modem status and update control state */
431         mct_u232_get_modem_stat(serial, &last_msr);
432         spin_lock_irqsave(&priv->lock, flags);
433         priv->last_msr = last_msr;
434         mct_u232_msr_to_state(&priv->control_state, priv->last_msr);
435         spin_unlock_irqrestore(&priv->lock, flags);
436
437         port->read_urb->dev = port->serial->dev;
438         retval = usb_submit_urb(port->read_urb, GFP_KERNEL);
439         if (retval) {
440                 err("usb_submit_urb(read bulk) failed pipe 0x%x err %d",
441                     port->read_urb->pipe, retval);
442                 goto exit;
443         }
444
445         port->interrupt_in_urb->dev = port->serial->dev;
446         retval = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
447         if (retval)
448                 err(" usb_submit_urb(read int) failed pipe 0x%x err %d",
449                     port->interrupt_in_urb->pipe, retval);
450
451 exit:
452         return 0;
453 } /* mct_u232_open */
454
455
456 static void mct_u232_close (struct usb_serial_port *port, struct file *filp)
457 {
458         dbg("%s port %d", __FUNCTION__, port->number);
459
460         if (port->serial->dev) {
461                 /* shutdown our urbs */
462                 usb_kill_urb(port->write_urb);
463                 usb_kill_urb(port->read_urb);
464                 usb_kill_urb(port->interrupt_in_urb);
465         }
466 } /* mct_u232_close */
467
468
469 static void mct_u232_read_int_callback (struct urb *urb, struct pt_regs *regs)
470 {
471         struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
472         struct mct_u232_private *priv = usb_get_serial_port_data(port);
473         struct usb_serial *serial = port->serial;
474         struct tty_struct *tty;
475         unsigned char *data = urb->transfer_buffer;
476         int status;
477         unsigned long flags;
478
479         switch (urb->status) {
480         case 0:
481                 /* success */
482                 break;
483         case -ECONNRESET:
484         case -ENOENT:
485         case -ESHUTDOWN:
486                 /* this urb is terminated, clean up */
487                 dbg("%s - urb shutting down with status: %d", __FUNCTION__, urb->status);
488                 return;
489         default:
490                 dbg("%s - nonzero urb status received: %d", __FUNCTION__, urb->status);
491                 goto exit;
492         }
493
494         if (!serial) {
495                 dbg("%s - bad serial pointer, exiting", __FUNCTION__);
496                 return;
497         }
498
499         dbg("%s - port %d", __FUNCTION__, port->number);
500         usb_serial_debug_data(debug, &port->dev, __FUNCTION__, urb->actual_length, data);
501
502         /*
503          * Work-a-round: handle the 'usual' bulk-in pipe here
504          */
505         if (urb->transfer_buffer_length > 2) {
506                 int i;
507                 tty = port->tty;
508                 if (urb->actual_length) {
509                         for (i = 0; i < urb->actual_length ; ++i) {
510                                 tty_insert_flip_char(tty, data[i], 0);
511                         }
512                         tty_flip_buffer_push(tty);
513                 }
514                 goto exit;
515         }
516         
517         /*
518          * The interrupt-in pipe signals exceptional conditions (modem line
519          * signal changes and errors). data[0] holds MSR, data[1] holds LSR.
520          */
521         spin_lock_irqsave(&priv->lock, flags);
522         priv->last_msr = data[MCT_U232_MSR_INDEX];
523         
524         /* Record Control Line states */
525         mct_u232_msr_to_state(&priv->control_state, priv->last_msr);
526
527 #if 0
528         /* Not yet handled. See belin_sa.c for further information */
529         /* Now to report any errors */
530         priv->last_lsr = data[MCT_U232_LSR_INDEX];
531         /*
532          * fill in the flip buffer here, but I do not know the relation
533          * to the current/next receive buffer or characters.  I need
534          * to look in to this before committing any code.
535          */
536         if (priv->last_lsr & MCT_U232_LSR_ERR) {
537                 tty = port->tty;
538                 /* Overrun Error */
539                 if (priv->last_lsr & MCT_U232_LSR_OE) {
540                 }
541                 /* Parity Error */
542                 if (priv->last_lsr & MCT_U232_LSR_PE) {
543                 }
544                 /* Framing Error */
545                 if (priv->last_lsr & MCT_U232_LSR_FE) {
546                 }
547                 /* Break Indicator */
548                 if (priv->last_lsr & MCT_U232_LSR_BI) {
549                 }
550         }
551 #endif
552         spin_unlock_irqrestore(&priv->lock, flags);
553 exit:
554         status = usb_submit_urb (urb, GFP_ATOMIC);
555         if (status)
556                 err ("%s - usb_submit_urb failed with result %d",
557                      __FUNCTION__, status);
558 } /* mct_u232_read_int_callback */
559
560 static void mct_u232_set_termios (struct usb_serial_port *port,
561                                   struct termios *old_termios)
562 {
563         struct usb_serial *serial = port->serial;
564         struct mct_u232_private *priv = usb_get_serial_port_data(port);
565         unsigned int iflag = port->tty->termios->c_iflag;
566         unsigned int cflag = port->tty->termios->c_cflag;
567         unsigned int old_cflag = old_termios->c_cflag;
568         unsigned long flags;
569         unsigned int control_state, new_state;
570         unsigned char last_lcr;
571
572         /* get a local copy of the current port settings */
573         spin_lock_irqsave(&priv->lock, flags);
574         control_state = priv->control_state;
575         spin_unlock_irqrestore(&priv->lock, flags);
576         last_lcr = 0;
577
578         /*
579          * Update baud rate.
580          * Do not attempt to cache old rates and skip settings,
581          * disconnects screw such tricks up completely.
582          * Premature optimization is the root of all evil.
583          */
584
585         /* reassert DTR and (maybe) RTS on transition from B0 */
586         if ((old_cflag & CBAUD) == B0) {
587                 dbg("%s: baud was B0", __FUNCTION__);
588                 control_state |= TIOCM_DTR;
589                 /* don't set RTS if using hardware flow control */
590                 if (!(old_cflag & CRTSCTS)) {
591                         control_state |= TIOCM_RTS;
592                 }
593                 mct_u232_set_modem_ctrl(serial, control_state);
594         }
595
596         mct_u232_set_baud_rate(serial, cflag & CBAUD);
597
598         if ((cflag & CBAUD) == B0 ) {
599                 dbg("%s: baud is B0", __FUNCTION__);
600                 /* Drop RTS and DTR */
601                 control_state &= ~(TIOCM_DTR | TIOCM_RTS);
602                 mct_u232_set_modem_ctrl(serial, control_state);
603         }
604
605         /*
606          * Update line control register (LCR)
607          */
608
609         /* set the parity */
610         if (cflag & PARENB)
611                 last_lcr |= (cflag & PARODD) ?
612                         MCT_U232_PARITY_ODD : MCT_U232_PARITY_EVEN;
613         else
614                 last_lcr |= MCT_U232_PARITY_NONE;
615
616         /* set the number of data bits */
617         switch (cflag & CSIZE) {
618         case CS5:
619                 last_lcr |= MCT_U232_DATA_BITS_5; break;
620         case CS6:
621                 last_lcr |= MCT_U232_DATA_BITS_6; break;
622         case CS7:
623                 last_lcr |= MCT_U232_DATA_BITS_7; break;
624         case CS8:
625                 last_lcr |= MCT_U232_DATA_BITS_8; break;
626         default:
627                 err("CSIZE was not CS5-CS8, using default of 8");
628                 last_lcr |= MCT_U232_DATA_BITS_8;
629                 break;
630         }
631
632         /* set the number of stop bits */
633         last_lcr |= (cflag & CSTOPB) ?
634                 MCT_U232_STOP_BITS_2 : MCT_U232_STOP_BITS_1;
635
636         mct_u232_set_line_ctrl(serial, last_lcr);
637
638         /*
639          * Set flow control: well, I do not really now how to handle DTR/RTS.
640          * Just do what we have seen with SniffUSB on Win98.
641          */
642         /* Drop DTR/RTS if no flow control otherwise assert */
643         new_state = control_state;
644         if ((iflag & IXOFF) || (iflag & IXON) || (cflag & CRTSCTS))
645                 new_state |= TIOCM_DTR | TIOCM_RTS;
646         else
647                 new_state &= ~(TIOCM_DTR | TIOCM_RTS);
648         if (new_state != control_state) {
649                 mct_u232_set_modem_ctrl(serial, new_state);
650                 control_state = new_state;
651         }
652
653         /* save off the modified port settings */
654         spin_lock_irqsave(&priv->lock, flags);
655         priv->control_state = control_state;
656         priv->last_lcr = last_lcr;
657         spin_unlock_irqrestore(&priv->lock, flags);
658 } /* mct_u232_set_termios */
659
660 static void mct_u232_break_ctl( struct usb_serial_port *port, int break_state )
661 {
662         struct usb_serial *serial = port->serial;
663         struct mct_u232_private *priv = usb_get_serial_port_data(port);
664         unsigned char lcr;
665         unsigned long flags;
666
667         dbg("%sstate=%d", __FUNCTION__, break_state);
668
669         spin_lock_irqsave(&priv->lock, flags);
670         lcr = priv->last_lcr;
671         spin_unlock_irqrestore(&priv->lock, flags);
672
673         if (break_state)
674                 lcr |= MCT_U232_SET_BREAK;
675
676         mct_u232_set_line_ctrl(serial, lcr);
677 } /* mct_u232_break_ctl */
678
679
680 static int mct_u232_tiocmget (struct usb_serial_port *port, struct file *file)
681 {
682         struct mct_u232_private *priv = usb_get_serial_port_data(port);
683         unsigned int control_state;
684         unsigned long flags;
685         
686         dbg("%s", __FUNCTION__);
687
688         spin_lock_irqsave(&priv->lock, flags);
689         control_state = priv->control_state;
690         spin_unlock_irqrestore(&priv->lock, flags);
691
692         return control_state;
693 }
694
695 static int mct_u232_tiocmset (struct usb_serial_port *port, struct file *file,
696                               unsigned int set, unsigned int clear)
697 {
698         struct usb_serial *serial = port->serial;
699         struct mct_u232_private *priv = usb_get_serial_port_data(port);
700         unsigned int control_state;
701         unsigned long flags;
702         
703         dbg("%s", __FUNCTION__);
704
705         spin_lock_irqsave(&priv->lock, flags);
706         control_state = priv->control_state;
707
708         if (set & TIOCM_RTS)
709                 control_state |= TIOCM_RTS;
710         if (set & TIOCM_DTR)
711                 control_state |= TIOCM_DTR;
712         if (clear & TIOCM_RTS)
713                 control_state &= ~TIOCM_RTS;
714         if (clear & TIOCM_DTR)
715                 control_state &= ~TIOCM_DTR;
716
717         priv->control_state = control_state;
718         spin_unlock_irqrestore(&priv->lock, flags);
719         return mct_u232_set_modem_ctrl(serial, control_state);
720 }
721
722 static int mct_u232_ioctl (struct usb_serial_port *port, struct file * file,
723                            unsigned int cmd, unsigned long arg)
724 {
725         dbg("%scmd=0x%x", __FUNCTION__, cmd);
726
727         /* Based on code from acm.c and others */
728         switch (cmd) {
729         case TIOCMIWAIT:
730                 /* wait for any of the 4 modem inputs (DCD,RI,DSR,CTS)*/
731                 /* TODO */
732                 return( 0 );
733
734         case TIOCGICOUNT:
735                 /* return count of modemline transitions */
736                 /* TODO */
737                 return 0;
738
739         default:
740                 dbg("%s: arg not supported - 0x%04x", __FUNCTION__,cmd);
741                 return(-ENOIOCTLCMD);
742                 break;
743         }
744         return 0;
745 } /* mct_u232_ioctl */
746
747
748 static int __init mct_u232_init (void)
749 {
750         int retval;
751         retval = usb_serial_register(&mct_u232_device);
752         if (retval)
753                 goto failed_usb_serial_register;
754         retval = usb_register(&mct_u232_driver);
755         if (retval)
756                 goto failed_usb_register;
757         info(DRIVER_DESC " " DRIVER_VERSION);
758         return 0;
759 failed_usb_register:
760         usb_serial_deregister(&mct_u232_device);
761 failed_usb_serial_register:
762         return retval;
763 }
764
765
766 static void __exit mct_u232_exit (void)
767 {
768         usb_deregister (&mct_u232_driver);
769         usb_serial_deregister (&mct_u232_device);
770 }
771
772
773 module_init (mct_u232_init);
774 module_exit(mct_u232_exit);
775
776 MODULE_AUTHOR( DRIVER_AUTHOR );
777 MODULE_DESCRIPTION( DRIVER_DESC );
778 MODULE_LICENSE("GPL");
779
780 module_param(debug, bool, S_IRUGO | S_IWUSR);
781 MODULE_PARM_DESC(debug, "Debug enabled or not");