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