VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[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                 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
583                     tty->ldisc.write_wakeup)
584                         (tty->ldisc.write_wakeup)(tty);
585                 wake_up_interruptible(&tty->write_wait);
586                 
587         } else {
588                 /* from generic_write_bulk_callback */
589                 schedule_work(&port->work);
590         }
591
592         return;
593 } /* mct_u232_write_bulk_callback */
594 #endif
595
596 static void mct_u232_read_int_callback (struct urb *urb, struct pt_regs *regs)
597 {
598         struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
599         struct mct_u232_private *priv = usb_get_serial_port_data(port);
600         struct usb_serial *serial = port->serial;
601         struct tty_struct *tty;
602         unsigned char *data = urb->transfer_buffer;
603         int status;
604         unsigned long flags;
605
606         dbg("%s - port %d", __FUNCTION__, port->number);
607
608         switch (urb->status) {
609         case 0:
610                 /* success */
611                 break;
612         case -ECONNRESET:
613         case -ENOENT:
614         case -ESHUTDOWN:
615                 /* this urb is terminated, clean up */
616                 dbg("%s - urb shutting down with status: %d", __FUNCTION__, urb->status);
617                 return;
618         default:
619                 dbg("%s - nonzero urb status received: %d", __FUNCTION__, urb->status);
620                 goto exit;
621         }
622
623         if (!serial) {
624                 dbg("%s - bad serial pointer, exiting", __FUNCTION__);
625                 return;
626         }
627         
628         usb_serial_debug_data(debug, &port->dev, __FUNCTION__, urb->actual_length, data);
629
630         /*
631          * Work-a-round: handle the 'usual' bulk-in pipe here
632          */
633         if (urb->transfer_buffer_length > 2) {
634                 int i;
635                 tty = port->tty;
636                 if (urb->actual_length) {
637                         for (i = 0; i < urb->actual_length ; ++i) {
638                                 tty_insert_flip_char(tty, data[i], 0);
639                         }
640                         tty_flip_buffer_push(tty);
641                 }
642                 goto exit;
643         }
644         
645         /*
646          * The interrupt-in pipe signals exceptional conditions (modem line
647          * signal changes and errors). data[0] holds MSR, data[1] holds LSR.
648          */
649         spin_lock_irqsave(&priv->lock, flags);
650         priv->last_msr = data[MCT_U232_MSR_INDEX];
651         
652         /* Record Control Line states */
653         mct_u232_msr_to_state(&priv->control_state, priv->last_msr);
654
655 #if 0
656         /* Not yet handled. See belin_sa.c for further information */
657         /* Now to report any errors */
658         priv->last_lsr = data[MCT_U232_LSR_INDEX];
659         /*
660          * fill in the flip buffer here, but I do not know the relation
661          * to the current/next receive buffer or characters.  I need
662          * to look in to this before committing any code.
663          */
664         if (priv->last_lsr & MCT_U232_LSR_ERR) {
665                 tty = port->tty;
666                 /* Overrun Error */
667                 if (priv->last_lsr & MCT_U232_LSR_OE) {
668                 }
669                 /* Parity Error */
670                 if (priv->last_lsr & MCT_U232_LSR_PE) {
671                 }
672                 /* Framing Error */
673                 if (priv->last_lsr & MCT_U232_LSR_FE) {
674                 }
675                 /* Break Indicator */
676                 if (priv->last_lsr & MCT_U232_LSR_BI) {
677                 }
678         }
679 #endif
680         spin_unlock_irqrestore(&priv->lock, flags);
681 exit:
682         status = usb_submit_urb (urb, GFP_ATOMIC);
683         if (status)
684                 err ("%s - usb_submit_urb failed with result %d",
685                      __FUNCTION__, status);
686 } /* mct_u232_read_int_callback */
687
688 static void mct_u232_set_termios (struct usb_serial_port *port,
689                                   struct termios *old_termios)
690 {
691         struct usb_serial *serial = port->serial;
692         struct mct_u232_private *priv = usb_get_serial_port_data(port);
693         unsigned int iflag = port->tty->termios->c_iflag;
694         unsigned int cflag = port->tty->termios->c_cflag;
695         unsigned int old_cflag = old_termios->c_cflag;
696         unsigned long flags;
697         unsigned int control_state, new_state;
698         unsigned char last_lcr;
699
700         /* get a local copy of the current port settings */
701         spin_lock_irqsave(&priv->lock, flags);
702         control_state = priv->control_state;
703         spin_unlock_irqrestore(&priv->lock, flags);
704         last_lcr = 0;
705
706         /*
707          * Update baud rate.
708          * Do not attempt to cache old rates and skip settings,
709          * disconnects screw such tricks up completely.
710          * Premature optimization is the root of all evil.
711          */
712
713         /* reassert DTR and (maybe) RTS on transition from B0 */
714         if ((old_cflag & CBAUD) == B0) {
715                 dbg("%s: baud was B0", __FUNCTION__);
716                 control_state |= TIOCM_DTR;
717                 /* don't set RTS if using hardware flow control */
718                 if (!(old_cflag & CRTSCTS)) {
719                         control_state |= TIOCM_RTS;
720                 }
721                 mct_u232_set_modem_ctrl(serial, control_state);
722         }
723
724         mct_u232_set_baud_rate(serial, cflag & CBAUD);
725
726         if ((cflag & CBAUD) == B0 ) {
727                 dbg("%s: baud is B0", __FUNCTION__);
728                 /* Drop RTS and DTR */
729                 control_state &= ~(TIOCM_DTR | TIOCM_RTS);
730                 mct_u232_set_modem_ctrl(serial, control_state);
731         }
732
733         /*
734          * Update line control register (LCR)
735          */
736
737         /* set the parity */
738         if (cflag & PARENB)
739                 last_lcr |= (cflag & PARODD) ?
740                         MCT_U232_PARITY_ODD : MCT_U232_PARITY_EVEN;
741         else
742                 last_lcr |= MCT_U232_PARITY_NONE;
743
744         /* set the number of data bits */
745         switch (cflag & CSIZE) {
746         case CS5:
747                 last_lcr |= MCT_U232_DATA_BITS_5; break;
748         case CS6:
749                 last_lcr |= MCT_U232_DATA_BITS_6; break;
750         case CS7:
751                 last_lcr |= MCT_U232_DATA_BITS_7; break;
752         case CS8:
753                 last_lcr |= MCT_U232_DATA_BITS_8; break;
754         default:
755                 err("CSIZE was not CS5-CS8, using default of 8");
756                 last_lcr |= MCT_U232_DATA_BITS_8;
757                 break;
758         }
759
760         /* set the number of stop bits */
761         last_lcr |= (cflag & CSTOPB) ?
762                 MCT_U232_STOP_BITS_2 : MCT_U232_STOP_BITS_1;
763
764         mct_u232_set_line_ctrl(serial, last_lcr);
765
766         /*
767          * Set flow control: well, I do not really now how to handle DTR/RTS.
768          * Just do what we have seen with SniffUSB on Win98.
769          */
770         /* Drop DTR/RTS if no flow control otherwise assert */
771         new_state = control_state;
772         if ((iflag & IXOFF) || (iflag & IXON) || (cflag & CRTSCTS))
773                 new_state |= TIOCM_DTR | TIOCM_RTS;
774         else
775                 new_state &= ~(TIOCM_DTR | TIOCM_RTS);
776         if (new_state != control_state) {
777                 mct_u232_set_modem_ctrl(serial, control_state);
778                 control_state = new_state;
779         }
780
781         /* save off the modified port settings */
782         spin_lock_irqsave(&priv->lock, flags);
783         priv->control_state = control_state;
784         priv->last_lcr = last_lcr;
785         spin_unlock_irqrestore(&priv->lock, flags);
786 } /* mct_u232_set_termios */
787
788 static void mct_u232_break_ctl( struct usb_serial_port *port, int break_state )
789 {
790         struct usb_serial *serial = port->serial;
791         struct mct_u232_private *priv = usb_get_serial_port_data(port);
792         unsigned char lcr;
793         unsigned long flags;
794
795         dbg("%sstate=%d", __FUNCTION__, break_state);
796
797         spin_lock_irqsave(&priv->lock, flags);
798         lcr = priv->last_lcr;
799         spin_unlock_irqrestore(&priv->lock, flags);
800
801         if (break_state)
802                 lcr |= MCT_U232_SET_BREAK;
803
804         mct_u232_set_line_ctrl(serial, lcr);
805 } /* mct_u232_break_ctl */
806
807
808 static int mct_u232_tiocmget (struct usb_serial_port *port, struct file *file)
809 {
810         struct mct_u232_private *priv = usb_get_serial_port_data(port);
811         unsigned int control_state;
812         unsigned long flags;
813         
814         dbg("%s", __FUNCTION__);
815
816         spin_lock_irqsave(&priv->lock, flags);
817         control_state = priv->control_state;
818         spin_unlock_irqrestore(&priv->lock, flags);
819
820         return control_state;
821 }
822
823 static int mct_u232_tiocmset (struct usb_serial_port *port, struct file *file,
824                               unsigned int set, unsigned int clear)
825 {
826         struct usb_serial *serial = port->serial;
827         struct mct_u232_private *priv = usb_get_serial_port_data(port);
828         unsigned int control_state;
829         unsigned long flags;
830         
831         dbg("%s", __FUNCTION__);
832
833         spin_lock_irqsave(&priv->lock, flags);
834         control_state = priv->control_state;
835
836         if (set & TIOCM_RTS)
837                 control_state |= TIOCM_RTS;
838         if (set & TIOCM_DTR)
839                 control_state |= TIOCM_DTR;
840         if (clear & TIOCM_RTS)
841                 control_state &= ~TIOCM_RTS;
842         if (clear & TIOCM_DTR)
843                 control_state &= ~TIOCM_DTR;
844
845         priv->control_state = control_state;
846         spin_unlock_irqrestore(&priv->lock, flags);
847         return mct_u232_set_modem_ctrl(serial, control_state);
848 }
849
850 static int mct_u232_ioctl (struct usb_serial_port *port, struct file * file,
851                            unsigned int cmd, unsigned long arg)
852 {
853         dbg("%scmd=0x%x", __FUNCTION__, cmd);
854
855         /* Based on code from acm.c and others */
856         switch (cmd) {
857         case TIOCMIWAIT:
858                 /* wait for any of the 4 modem inputs (DCD,RI,DSR,CTS)*/
859                 /* TODO */
860                 return( 0 );
861
862         case TIOCGICOUNT:
863                 /* return count of modemline transitions */
864                 /* TODO */
865                 return 0;
866
867         default:
868                 dbg("%s: arg not supported - 0x%04x", __FUNCTION__,cmd);
869                 return(-ENOIOCTLCMD);
870                 break;
871         }
872         return 0;
873 } /* mct_u232_ioctl */
874
875
876 static int __init mct_u232_init (void)
877 {
878         int retval;
879         retval = usb_serial_register(&mct_u232_device);
880         if (retval)
881                 goto failed_usb_serial_register;
882         retval = usb_register(&mct_u232_driver);
883         if (retval)
884                 goto failed_usb_register;
885         info(DRIVER_DESC " " DRIVER_VERSION);
886         return 0;
887 failed_usb_register:
888         usb_serial_deregister(&mct_u232_device);
889 failed_usb_serial_register:
890         return retval;
891 }
892
893
894 static void __exit mct_u232_exit (void)
895 {
896         usb_deregister (&mct_u232_driver);
897         usb_serial_deregister (&mct_u232_device);
898 }
899
900
901 module_init (mct_u232_init);
902 module_exit(mct_u232_exit);
903
904 MODULE_AUTHOR( DRIVER_AUTHOR );
905 MODULE_DESCRIPTION( DRIVER_DESC );
906 MODULE_LICENSE("GPL");
907
908 #ifdef FIX_WRITE_RETURN_CODE_PROBLEM
909 module_param(write_blocking, int, 0);
910 MODULE_PARM_DESC(write_blocking, 
911                  "The write function will block to write out all data");
912 #endif
913
914 module_param(debug, bool, S_IRUGO | S_IWUSR);
915 MODULE_PARM_DESC(debug, "Debug enabled or not");
916