Merge branch 'upstream-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/linvil...
[linux-2.6] / drivers / usb / serial / kl5kusb105.c
1 /*
2  * KLSI KL5KUSB105 chip RS232 converter driver
3  *
4  *   Copyright (C) 2001 Utz-Uwe Haus <haus@uuhaus.de>
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  * All information about the device was acquired using SniffUSB ans snoopUSB
12  * on Windows98.
13  * It was written out of frustration with the PalmConnect USB Serial adapter
14  * sold by Palm Inc.
15  * Neither Palm, nor their contractor (MCCI) or their supplier (KLSI) provided
16  * information that was not already available.
17  *
18  * It seems that KLSI bought some silicon-design information from ScanLogic, 
19  * whose SL11R processor is at the core of the KL5KUSB chipset from KLSI.
20  * KLSI has firmware available for their devices; it is probable that the
21  * firmware differs from that used by KLSI in their products. If you have an
22  * original KLSI device and can provide some information on it, I would be 
23  * most interested in adding support for it here. If you have any information 
24  * on the protocol used (or find errors in my reverse-engineered stuff), please
25  * let me know.
26  *
27  * The code was only tested with a PalmConnect USB adapter; if you
28  * are adventurous, try it with any KLSI-based device and let me know how it
29  * breaks so that I can fix it!
30  */
31
32 /* TODO:
33  *      check modem line signals
34  *      implement handshaking or decide that we do not support it
35  */
36
37 /* History:
38  *   0.3a - implemented pools of write URBs
39  *   0.3  - alpha version for public testing
40  *   0.2  - TIOCMGET works, so autopilot(1) can be used!
41  *   0.1  - can be used to to pilot-xfer -p /dev/ttyUSB0 -l
42  *
43  *   The driver skeleton is mainly based on mct_u232.c and various other 
44  *   pieces of code shamelessly copied from the drivers/usb/serial/ directory.
45  */
46
47
48 #include <linux/kernel.h>
49 #include <linux/errno.h>
50 #include <linux/init.h>
51 #include <linux/slab.h>
52 #include <linux/tty.h>
53 #include <linux/tty_driver.h>
54 #include <linux/tty_flip.h>
55 #include <linux/module.h>
56 #include <asm/uaccess.h>
57 #include <linux/usb.h>
58 #include <linux/usb/serial.h>
59 #include "kl5kusb105.h"
60
61 static int debug;
62
63 /*
64  * Version Information
65  */
66 #define DRIVER_VERSION "v0.3a"
67 #define DRIVER_AUTHOR "Utz-Uwe Haus <haus@uuhaus.de>"
68 #define DRIVER_DESC "KLSI KL5KUSB105 chipset USB->Serial Converter driver"
69
70
71 /*
72  * Function prototypes
73  */
74 static int  klsi_105_startup             (struct usb_serial *serial);
75 static void klsi_105_shutdown            (struct usb_serial *serial);
76 static int  klsi_105_open                (struct usb_serial_port *port,
77                                           struct file *filp);
78 static void klsi_105_close               (struct usb_serial_port *port,
79                                           struct file *filp);
80 static int  klsi_105_write               (struct usb_serial_port *port,
81                                           const unsigned char *buf,
82                                           int count);
83 static void klsi_105_write_bulk_callback (struct urb *urb);
84 static int  klsi_105_chars_in_buffer     (struct usb_serial_port *port);
85 static int  klsi_105_write_room          (struct usb_serial_port *port);
86
87 static void klsi_105_read_bulk_callback  (struct urb *urb);
88 static void klsi_105_set_termios         (struct usb_serial_port *port,
89                                           struct ktermios *old);
90 static void klsi_105_throttle            (struct usb_serial_port *port);
91 static void klsi_105_unthrottle          (struct usb_serial_port *port);
92 /*
93 static void klsi_105_break_ctl           (struct usb_serial_port *port,
94                                           int break_state );
95  */
96 static int  klsi_105_tiocmget            (struct usb_serial_port *port,
97                                           struct file *file);
98 static int  klsi_105_tiocmset            (struct usb_serial_port *port,
99                                           struct file *file, unsigned int set,
100                                           unsigned int clear);
101
102 /*
103  * All of the device info needed for the KLSI converters.
104  */
105 static struct usb_device_id id_table [] = {
106         { USB_DEVICE(PALMCONNECT_VID, PALMCONNECT_PID) },
107         { USB_DEVICE(KLSI_VID, KLSI_KL5KUSB105D_PID) },
108         { }             /* Terminating entry */
109 };
110
111 MODULE_DEVICE_TABLE (usb, id_table);
112
113 static struct usb_driver kl5kusb105d_driver = {
114         .name =         "kl5kusb105d",
115         .probe =        usb_serial_probe,
116         .disconnect =   usb_serial_disconnect,
117         .id_table =     id_table,
118         .no_dynamic_id =        1,
119 };
120
121 static struct usb_serial_driver kl5kusb105d_device = {
122         .driver = {
123                 .owner =        THIS_MODULE,
124                 .name =         "kl5kusb105d",
125         },
126         .description =       "KL5KUSB105D / PalmConnect",
127         .id_table =          id_table,
128         .num_interrupt_in =  1,
129         .num_bulk_in =       1,
130         .num_bulk_out =      1,
131         .num_ports =         1,
132         .open =              klsi_105_open,
133         .close =             klsi_105_close,
134         .write =             klsi_105_write,
135         .write_bulk_callback = klsi_105_write_bulk_callback,
136         .chars_in_buffer =   klsi_105_chars_in_buffer,
137         .write_room =        klsi_105_write_room,
138         .read_bulk_callback =klsi_105_read_bulk_callback,
139         .set_termios =       klsi_105_set_termios,
140         /*.break_ctl =       klsi_105_break_ctl,*/
141         .tiocmget =          klsi_105_tiocmget,
142         .tiocmset =          klsi_105_tiocmset,
143         .attach =            klsi_105_startup,
144         .shutdown =          klsi_105_shutdown,
145         .throttle =          klsi_105_throttle,
146         .unthrottle =        klsi_105_unthrottle,
147 };
148
149 struct klsi_105_port_settings {
150         __u8    pktlen;         /* always 5, it seems */
151         __u8    baudrate;
152         __u8    databits;
153         __u8    unknown1;
154         __u8    unknown2;
155 } __attribute__ ((packed));
156
157 /* we implement a pool of NUM_URBS urbs per usb_serial */
158 #define NUM_URBS                        1
159 #define URB_TRANSFER_BUFFER_SIZE        64
160 struct klsi_105_private {
161         struct klsi_105_port_settings   cfg;
162         struct ktermios                 termios;
163         unsigned long                   line_state; /* modem line settings */
164         /* write pool */
165         struct urb *                    write_urb_pool[NUM_URBS];
166         spinlock_t                      lock;
167         unsigned long                   bytes_in;
168         unsigned long                   bytes_out;
169 };
170
171
172 /*
173  * Handle vendor specific USB requests
174  */
175
176
177 #define KLSI_TIMEOUT     5000 /* default urb timeout */
178
179 static int klsi_105_chg_port_settings(struct usb_serial_port *port,
180                                       struct klsi_105_port_settings *settings)
181 {
182         int rc;
183
184         rc = usb_control_msg(port->serial->dev,
185                              usb_sndctrlpipe(port->serial->dev, 0),
186                              KL5KUSB105A_SIO_SET_DATA,
187                              USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_INTERFACE,
188                              0, /* value */
189                              0, /* index */
190                              settings,
191                              sizeof(struct klsi_105_port_settings),
192                              KLSI_TIMEOUT);
193         if (rc < 0)
194                 err("Change port settings failed (error = %d)", rc);
195         info("%s - %d byte block, baudrate %x, databits %d, u1 %d, u2 %d",
196             __FUNCTION__,
197             settings->pktlen,
198             settings->baudrate, settings->databits,
199             settings->unknown1, settings->unknown2);
200         return rc;
201 } /* klsi_105_chg_port_settings */
202
203 /* translate a 16-bit status value from the device to linux's TIO bits */
204 static unsigned long klsi_105_status2linestate(const __u16 status)
205 {
206         unsigned long res = 0;
207
208         res =   ((status & KL5KUSB105A_DSR) ? TIOCM_DSR : 0)
209               | ((status & KL5KUSB105A_CTS) ? TIOCM_CTS : 0)
210               ;
211
212         return res;
213 }
214 /* 
215  * Read line control via vendor command and return result through
216  * *line_state_p 
217  */
218 /* It seems that the status buffer has always only 2 bytes length */
219 #define KLSI_STATUSBUF_LEN      2
220 static int klsi_105_get_line_state(struct usb_serial_port *port,
221                                    unsigned long *line_state_p)
222 {
223         int rc;
224         __u8 status_buf[KLSI_STATUSBUF_LEN] = { -1,-1};
225         __u16 status;
226
227         info("%s - sending SIO Poll request", __FUNCTION__);
228         rc = usb_control_msg(port->serial->dev,
229                              usb_rcvctrlpipe(port->serial->dev, 0),
230                              KL5KUSB105A_SIO_POLL,
231                              USB_TYPE_VENDOR | USB_DIR_IN,
232                              0, /* value */
233                              0, /* index */
234                              status_buf, KLSI_STATUSBUF_LEN,
235                              10000
236                              );
237         if (rc < 0)
238                 err("Reading line status failed (error = %d)", rc);
239         else {
240                 status = status_buf[0] + (status_buf[1]<<8);
241
242                 info("%s - read status %x %x", __FUNCTION__,
243                      status_buf[0], status_buf[1]);
244
245                 *line_state_p = klsi_105_status2linestate(status);
246         }
247
248         return rc;
249 }
250
251
252 /*
253  * Driver's tty interface functions
254  */
255
256 static int klsi_105_startup (struct usb_serial *serial)
257 {
258         struct klsi_105_private *priv;
259         int i;
260
261         /* check if we support the product id (see keyspan.c)
262          * FIXME
263          */
264
265         /* allocate the private data structure */
266         for (i=0; i<serial->num_ports; i++) {
267                 int j;
268                 priv = kmalloc(sizeof(struct klsi_105_private),
269                                                    GFP_KERNEL);
270                 if (!priv) {
271                         dbg("%skmalloc for klsi_105_private failed.", __FUNCTION__);
272                         return -ENOMEM;
273                 }
274                 /* set initial values for control structures */
275                 priv->cfg.pktlen    = 5;
276                 priv->cfg.baudrate  = kl5kusb105a_sio_b9600;
277                 priv->cfg.databits  = kl5kusb105a_dtb_8;
278                 priv->cfg.unknown1  = 0;
279                 priv->cfg.unknown2  = 1;
280
281                 priv->line_state    = 0;
282
283                 priv->bytes_in      = 0;
284                 priv->bytes_out     = 0;
285                 usb_set_serial_port_data(serial->port[i], priv);
286
287                 spin_lock_init (&priv->lock);
288                 for (j=0; j<NUM_URBS; j++) {
289                         struct urb* urb = usb_alloc_urb(0, GFP_KERNEL);
290
291                         priv->write_urb_pool[j] = urb;
292                         if (urb == NULL) {
293                                 err("No more urbs???");
294                                 continue;
295                         }
296
297                         urb->transfer_buffer = NULL;
298                         urb->transfer_buffer = kmalloc (URB_TRANSFER_BUFFER_SIZE,
299                                                         GFP_KERNEL);
300                         if (!urb->transfer_buffer) {
301                                 err("%s - out of memory for urb buffers.", __FUNCTION__);
302                                 continue;
303                         }
304                 }
305
306                 /* priv->termios is left uninitalized until port opening */
307                 init_waitqueue_head(&serial->port[i]->write_wait);
308         }
309         
310         return (0);
311 } /* klsi_105_startup */
312
313
314 static void klsi_105_shutdown (struct usb_serial *serial)
315 {
316         int i;
317         
318         dbg("%s", __FUNCTION__);
319
320         /* stop reads and writes on all ports */
321         for (i=0; i < serial->num_ports; ++i) {
322                 struct klsi_105_private *priv = usb_get_serial_port_data(serial->port[i]);
323                 unsigned long flags;
324
325                 if (priv) {
326                         /* kill our write urb pool */
327                         int j;
328                         struct urb **write_urbs = priv->write_urb_pool;
329                         spin_lock_irqsave(&priv->lock,flags);
330
331                         for (j = 0; j < NUM_URBS; j++) {
332                                 if (write_urbs[j]) {
333                                         /* FIXME - uncomment the following
334                                          * usb_kill_urb call when the host
335                                          * controllers get fixed to set
336                                          * urb->dev = NULL after the urb is
337                                          * finished.  Otherwise this call
338                                          * oopses. */
339                                         /* usb_kill_urb(write_urbs[j]); */
340                                         kfree(write_urbs[j]->transfer_buffer);
341                                         usb_free_urb (write_urbs[j]);
342                                 }
343                         }
344
345                         spin_unlock_irqrestore (&priv->lock, flags);
346
347                         kfree(priv);
348                         usb_set_serial_port_data(serial->port[i], NULL);
349                 }
350         }
351 } /* klsi_105_shutdown */
352
353 static int  klsi_105_open (struct usb_serial_port *port, struct file *filp)
354 {
355         struct klsi_105_private *priv = usb_get_serial_port_data(port);
356         int retval = 0;
357         int rc;
358         int i;
359         unsigned long line_state;
360         struct klsi_105_port_settings cfg;
361         unsigned long flags;
362
363         dbg("%s port %d", __FUNCTION__, port->number);
364
365         /* force low_latency on so that our tty_push actually forces
366          * the data through
367          * port->tty->low_latency = 1; */
368
369         /* Do a defined restart:
370          * Set up sane default baud rate and send the 'READ_ON'
371          * vendor command. 
372          * FIXME: set modem line control (how?)
373          * Then read the modem line control and store values in
374          * priv->line_state.
375          */
376         cfg.pktlen   = 5;
377         cfg.baudrate = kl5kusb105a_sio_b9600;
378         cfg.databits = kl5kusb105a_dtb_8;
379         cfg.unknown1 = 0;
380         cfg.unknown2 = 1;
381         klsi_105_chg_port_settings(port, &cfg);
382         
383         /* set up termios structure */
384         spin_lock_irqsave (&priv->lock, flags);
385         priv->termios.c_iflag = port->tty->termios->c_iflag;
386         priv->termios.c_oflag = port->tty->termios->c_oflag;
387         priv->termios.c_cflag = port->tty->termios->c_cflag;
388         priv->termios.c_lflag = port->tty->termios->c_lflag;
389         for (i=0; i<NCCS; i++)
390                 priv->termios.c_cc[i] = port->tty->termios->c_cc[i];
391         priv->cfg.pktlen   = cfg.pktlen;
392         priv->cfg.baudrate = cfg.baudrate;
393         priv->cfg.databits = cfg.databits;
394         priv->cfg.unknown1 = cfg.unknown1;
395         priv->cfg.unknown2 = cfg.unknown2;
396         spin_unlock_irqrestore (&priv->lock, flags);
397
398         /* READ_ON and urb submission */
399         usb_fill_bulk_urb(port->read_urb, port->serial->dev, 
400                       usb_rcvbulkpipe(port->serial->dev,
401                                       port->bulk_in_endpointAddress),
402                       port->read_urb->transfer_buffer,
403                       port->read_urb->transfer_buffer_length,
404                       klsi_105_read_bulk_callback,
405                       port);
406
407         rc = usb_submit_urb(port->read_urb, GFP_KERNEL);
408         if (rc) {
409                 err("%s - failed submitting read urb, error %d", __FUNCTION__, rc);
410                 retval = rc;
411                 goto exit;
412         }
413
414         rc = usb_control_msg(port->serial->dev,
415                              usb_sndctrlpipe(port->serial->dev,0),
416                              KL5KUSB105A_SIO_CONFIGURE,
417                              USB_TYPE_VENDOR|USB_DIR_OUT|USB_RECIP_INTERFACE,
418                              KL5KUSB105A_SIO_CONFIGURE_READ_ON,
419                              0, /* index */
420                              NULL,
421                              0,
422                              KLSI_TIMEOUT);
423         if (rc < 0) {
424                 err("Enabling read failed (error = %d)", rc);
425                 retval = rc;
426         } else 
427                 dbg("%s - enabled reading", __FUNCTION__);
428
429         rc = klsi_105_get_line_state(port, &line_state);
430         if (rc >= 0) {
431                 spin_lock_irqsave (&priv->lock, flags);
432                 priv->line_state = line_state;
433                 spin_unlock_irqrestore (&priv->lock, flags);
434                 dbg("%s - read line state 0x%lx", __FUNCTION__, line_state);
435                 retval = 0;
436         } else
437                 retval = rc;
438
439 exit:
440         return retval;
441 } /* klsi_105_open */
442
443
444 static void klsi_105_close (struct usb_serial_port *port, struct file *filp)
445 {
446         struct klsi_105_private *priv = usb_get_serial_port_data(port);
447         int rc;
448
449         dbg("%s port %d", __FUNCTION__, port->number);
450
451         /* send READ_OFF */
452         rc = usb_control_msg (port->serial->dev,
453                               usb_sndctrlpipe(port->serial->dev, 0),
454                               KL5KUSB105A_SIO_CONFIGURE,
455                               USB_TYPE_VENDOR | USB_DIR_OUT,
456                               KL5KUSB105A_SIO_CONFIGURE_READ_OFF,
457                               0, /* index */
458                               NULL, 0,
459                               KLSI_TIMEOUT);
460         if (rc < 0)
461                     err("Disabling read failed (error = %d)", rc);
462
463         /* shutdown our bulk reads and writes */
464         usb_kill_urb(port->write_urb);
465         usb_kill_urb(port->read_urb);
466         /* unlink our write pool */
467         /* FIXME */
468         /* wgg - do I need this? I think so. */
469         usb_kill_urb(port->interrupt_in_urb);
470         info("kl5kusb105 port stats: %ld bytes in, %ld bytes out", priv->bytes_in, priv->bytes_out);
471 } /* klsi_105_close */
472
473
474 /* We need to write a complete 64-byte data block and encode the
475  * number actually sent in the first double-byte, LSB-order. That 
476  * leaves at most 62 bytes of payload.
477  */
478 #define KLSI_105_DATA_OFFSET    2   /* in the bulk urb data block */
479
480
481 static int klsi_105_write (struct usb_serial_port *port,
482                            const unsigned char *buf, int count)
483 {
484         struct klsi_105_private *priv = usb_get_serial_port_data(port);
485         int result, size;
486         int bytes_sent=0;
487
488         dbg("%s - port %d", __FUNCTION__, port->number);
489
490         while (count > 0) {
491                 /* try to find a free urb (write 0 bytes if none) */
492                 struct urb *urb = NULL;
493                 unsigned long flags;
494                 int i;
495                 /* since the pool is per-port we might not need the spin lock !? */
496                 spin_lock_irqsave (&priv->lock, flags);
497                 for (i=0; i<NUM_URBS; i++) {
498                         if (priv->write_urb_pool[i]->status != -EINPROGRESS) {
499                                 urb = priv->write_urb_pool[i];
500                                 dbg("%s - using pool URB %d", __FUNCTION__, i);
501                                 break;
502                         }
503                 }
504                 spin_unlock_irqrestore (&priv->lock, flags);
505
506                 if (urb==NULL) {
507                         dbg("%s - no more free urbs", __FUNCTION__);
508                         goto exit;
509                 }
510
511                 if (urb->transfer_buffer == NULL) {
512                         urb->transfer_buffer = kmalloc (URB_TRANSFER_BUFFER_SIZE, GFP_ATOMIC);
513                         if (urb->transfer_buffer == NULL) {
514                                 err("%s - no more kernel memory...", __FUNCTION__);
515                                 goto exit;
516                         }
517                 }
518
519                 size = min (count, port->bulk_out_size - KLSI_105_DATA_OFFSET);
520                 size = min (size, URB_TRANSFER_BUFFER_SIZE - KLSI_105_DATA_OFFSET);
521
522                 memcpy (urb->transfer_buffer + KLSI_105_DATA_OFFSET, buf, size);
523
524                 /* write payload size into transfer buffer */
525                 ((__u8 *)urb->transfer_buffer)[0] = (__u8) (size & 0xFF);
526                 ((__u8 *)urb->transfer_buffer)[1] = (__u8) ((size & 0xFF00)>>8);
527
528                 /* set up our urb */
529                 usb_fill_bulk_urb(urb, port->serial->dev,
530                               usb_sndbulkpipe(port->serial->dev,
531                                               port->bulk_out_endpointAddress),
532                               urb->transfer_buffer,
533                               URB_TRANSFER_BUFFER_SIZE,
534                               klsi_105_write_bulk_callback,
535                               port);
536
537                 /* send the data out the bulk port */
538                 result = usb_submit_urb(urb, GFP_ATOMIC);
539                 if (result) {
540                         err("%s - failed submitting write urb, error %d", __FUNCTION__, result);
541                         goto exit;
542                 }
543                 buf += size;
544                 bytes_sent += size;
545                 count -= size;
546         }
547 exit:
548         /* lockless, but it's for debug info only... */
549         priv->bytes_out+=bytes_sent;
550
551         return bytes_sent;      /* that's how much we wrote */
552 } /* klsi_105_write */
553
554 static void klsi_105_write_bulk_callback ( struct urb *urb)
555 {
556         struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
557
558         dbg("%s - port %d", __FUNCTION__, port->number);
559         
560         if (urb->status) {
561                 dbg("%s - nonzero write bulk status received: %d", __FUNCTION__,
562                     urb->status);
563                 return;
564         }
565
566         usb_serial_port_softint(port);
567 } /* klsi_105_write_bulk_completion_callback */
568
569
570 /* return number of characters currently in the writing process */
571 static int klsi_105_chars_in_buffer (struct usb_serial_port *port)
572 {
573         int chars = 0;
574         int i;
575         unsigned long flags;
576         struct klsi_105_private *priv = usb_get_serial_port_data(port);
577
578         spin_lock_irqsave (&priv->lock, flags);
579
580         for (i = 0; i < NUM_URBS; ++i) {
581                 if (priv->write_urb_pool[i]->status == -EINPROGRESS) {
582                         chars += URB_TRANSFER_BUFFER_SIZE;
583                 }
584         }
585
586         spin_unlock_irqrestore (&priv->lock, flags);
587
588         dbg("%s - returns %d", __FUNCTION__, chars);
589         return (chars);
590 }
591
592 static int klsi_105_write_room (struct usb_serial_port *port)
593 {
594         unsigned long flags;
595         int i;
596         int room = 0;
597         struct klsi_105_private *priv = usb_get_serial_port_data(port);
598
599         spin_lock_irqsave (&priv->lock, flags);
600         for (i = 0; i < NUM_URBS; ++i) {
601                 if (priv->write_urb_pool[i]->status != -EINPROGRESS) {
602                         room += URB_TRANSFER_BUFFER_SIZE;
603                 }
604         }
605
606         spin_unlock_irqrestore (&priv->lock, flags);
607
608         dbg("%s - returns %d", __FUNCTION__, room);
609         return (room);
610 }
611
612
613
614 static void klsi_105_read_bulk_callback (struct urb *urb)
615 {
616         struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
617         struct klsi_105_private *priv = usb_get_serial_port_data(port);
618         struct tty_struct *tty;
619         unsigned char *data = urb->transfer_buffer;
620         int rc;
621
622         dbg("%s - port %d", __FUNCTION__, port->number);
623
624         /* The urb might have been killed. */
625         if (urb->status) {
626                 dbg("%s - nonzero read bulk status received: %d", __FUNCTION__,
627                     urb->status);
628                 return;
629         }
630         
631         /* The data received is again preceded by a length double-byte in LSB-
632          * first order (see klsi_105_write() )
633          */
634         if (urb->actual_length == 0) {
635                 /* empty urbs seem to happen, we ignore them */
636                 /* dbg("%s - emtpy URB", __FUNCTION__); */
637                ;
638         } else if (urb->actual_length <= 2) {
639                 dbg("%s - size %d URB not understood", __FUNCTION__,
640                     urb->actual_length);
641                 usb_serial_debug_data(debug, &port->dev, __FUNCTION__,
642                                       urb->actual_length, data);
643         } else {
644                 int bytes_sent = ((__u8 *) data)[0] +
645                                  ((unsigned int) ((__u8 *) data)[1] << 8);
646                 tty = port->tty;
647                 /* we should immediately resubmit the URB, before attempting
648                  * to pass the data on to the tty layer. But that needs locking
649                  * against re-entry an then mixed-up data because of
650                  * intermixed tty_flip_buffer_push()s
651                  * FIXME
652                  */ 
653                 usb_serial_debug_data(debug, &port->dev, __FUNCTION__,
654                                       urb->actual_length, data);
655
656                 if (bytes_sent + 2 > urb->actual_length) {
657                         dbg("%s - trying to read more data than available"
658                             " (%d vs. %d)", __FUNCTION__,
659                             bytes_sent+2, urb->actual_length);
660                         /* cap at implied limit */
661                         bytes_sent = urb->actual_length - 2;
662                 }
663
664                 tty_buffer_request_room(tty, bytes_sent);
665                 tty_insert_flip_string(tty, data + 2, bytes_sent);
666                 tty_flip_buffer_push(tty);
667
668                 /* again lockless, but debug info only */
669                 priv->bytes_in += bytes_sent;
670         }
671         /* Continue trying to always read  */
672         usb_fill_bulk_urb(port->read_urb, port->serial->dev, 
673                       usb_rcvbulkpipe(port->serial->dev,
674                                       port->bulk_in_endpointAddress),
675                       port->read_urb->transfer_buffer,
676                       port->read_urb->transfer_buffer_length,
677                       klsi_105_read_bulk_callback,
678                       port);
679         rc = usb_submit_urb(port->read_urb, GFP_ATOMIC);
680         if (rc)
681                 err("%s - failed resubmitting read urb, error %d", __FUNCTION__, rc);
682 } /* klsi_105_read_bulk_callback */
683
684
685 static void klsi_105_set_termios (struct usb_serial_port *port,
686                                   struct ktermios *old_termios)
687 {
688         struct klsi_105_private *priv = usb_get_serial_port_data(port);
689         unsigned int iflag = port->tty->termios->c_iflag;
690         unsigned int old_iflag = old_termios->c_iflag;
691         unsigned int cflag = port->tty->termios->c_cflag;
692         unsigned int old_cflag = old_termios->c_cflag;
693         struct klsi_105_port_settings cfg;
694         unsigned long flags;
695         
696         /* lock while we are modifying the settings */
697         spin_lock_irqsave (&priv->lock, flags);
698         
699         /*
700          * Update baud rate
701          */
702         if( (cflag & CBAUD) != (old_cflag & CBAUD) ) {
703                 /* reassert DTR and (maybe) RTS on transition from B0 */
704                 if( (old_cflag & CBAUD) == B0 ) {
705                         dbg("%s: baud was B0", __FUNCTION__);
706 #if 0
707                         priv->control_state |= TIOCM_DTR;
708                         /* don't set RTS if using hardware flow control */
709                         if (!(old_cflag & CRTSCTS)) {
710                                 priv->control_state |= TIOCM_RTS;
711                         }
712                         mct_u232_set_modem_ctrl(serial, priv->control_state);
713 #endif
714                 }
715                 
716                 switch(cflag & CBAUD) {
717                 case B0: /* handled below */
718                         break;
719                 case B1200: priv->cfg.baudrate = kl5kusb105a_sio_b1200;
720                         break;
721                 case B2400: priv->cfg.baudrate = kl5kusb105a_sio_b2400;
722                         break;
723                 case B4800: priv->cfg.baudrate = kl5kusb105a_sio_b4800;
724                         break;
725                 case B9600: priv->cfg.baudrate = kl5kusb105a_sio_b9600;
726                         break;
727                 case B19200: priv->cfg.baudrate = kl5kusb105a_sio_b19200;
728                         break;
729                 case B38400: priv->cfg.baudrate = kl5kusb105a_sio_b38400;
730                         break;
731                 case B57600: priv->cfg.baudrate = kl5kusb105a_sio_b57600;
732                         break;
733                 case B115200: priv->cfg.baudrate = kl5kusb105a_sio_b115200;
734                         break;
735                 default:
736                         err("KLSI USB->Serial converter:"
737                             " unsupported baudrate request, using default"
738                             " of 9600");
739                         priv->cfg.baudrate = kl5kusb105a_sio_b9600;
740                         break;
741                 }
742                 if ((cflag & CBAUD) == B0 ) {
743                         dbg("%s: baud is B0", __FUNCTION__);
744                         /* Drop RTS and DTR */
745                         /* maybe this should be simulated by sending read
746                          * disable and read enable messages?
747                          */
748                         ;
749 #if 0
750                         priv->control_state &= ~(TIOCM_DTR | TIOCM_RTS);
751                         mct_u232_set_modem_ctrl(serial, priv->control_state);
752 #endif
753                 }
754         }
755
756         if ((cflag & CSIZE) != (old_cflag & CSIZE)) {
757                 /* set the number of data bits */
758                 switch (cflag & CSIZE) {
759                 case CS5:
760                         dbg("%s - 5 bits/byte not supported", __FUNCTION__);
761                         spin_unlock_irqrestore (&priv->lock, flags);
762                         return ;
763                 case CS6:
764                         dbg("%s - 6 bits/byte not supported", __FUNCTION__);
765                         spin_unlock_irqrestore (&priv->lock, flags);
766                         return ;
767                 case CS7:
768                         priv->cfg.databits = kl5kusb105a_dtb_7;
769                         break;
770                 case CS8:
771                         priv->cfg.databits = kl5kusb105a_dtb_8;
772                         break;
773                 default:
774                         err("CSIZE was not CS5-CS8, using default of 8");
775                         priv->cfg.databits = kl5kusb105a_dtb_8;
776                         break;
777                 }
778         }
779
780         /*
781          * Update line control register (LCR)
782          */
783         if ((cflag & (PARENB|PARODD)) != (old_cflag & (PARENB|PARODD))
784             || (cflag & CSTOPB) != (old_cflag & CSTOPB) ) {
785                 
786 #if 0
787                 priv->last_lcr = 0;
788
789                 /* set the parity */
790                 if (cflag & PARENB)
791                         priv->last_lcr |= (cflag & PARODD) ?
792                                 MCT_U232_PARITY_ODD : MCT_U232_PARITY_EVEN;
793                 else
794                         priv->last_lcr |= MCT_U232_PARITY_NONE;
795
796                 /* set the number of stop bits */
797                 priv->last_lcr |= (cflag & CSTOPB) ?
798                         MCT_U232_STOP_BITS_2 : MCT_U232_STOP_BITS_1;
799
800                 mct_u232_set_line_ctrl(serial, priv->last_lcr);
801 #endif
802                 ;
803         }
804         
805         /*
806          * Set flow control: well, I do not really now how to handle DTR/RTS.
807          * Just do what we have seen with SniffUSB on Win98.
808          */
809         if( (iflag & IXOFF) != (old_iflag & IXOFF)
810             || (iflag & IXON) != (old_iflag & IXON)
811             ||  (cflag & CRTSCTS) != (old_cflag & CRTSCTS) ) {
812                 
813                 /* Drop DTR/RTS if no flow control otherwise assert */
814 #if 0
815                 if ((iflag & IXOFF) || (iflag & IXON) || (cflag & CRTSCTS) )
816                         priv->control_state |= TIOCM_DTR | TIOCM_RTS;
817                 else
818                         priv->control_state &= ~(TIOCM_DTR | TIOCM_RTS);
819                 mct_u232_set_modem_ctrl(serial, priv->control_state);
820 #endif
821                 ;
822         }
823         memcpy (&cfg, &priv->cfg, sizeof(cfg));
824         spin_unlock_irqrestore (&priv->lock, flags);
825         
826         /* now commit changes to device */
827         klsi_105_chg_port_settings(port, &cfg);
828 } /* klsi_105_set_termios */
829
830
831 #if 0
832 static void mct_u232_break_ctl( struct usb_serial_port *port, int break_state )
833 {
834         struct usb_serial *serial = port->serial;
835         struct mct_u232_private *priv = (struct mct_u232_private *)port->private;
836         unsigned char lcr = priv->last_lcr;
837
838         dbg("%sstate=%d", __FUNCTION__, break_state);
839
840         if (break_state)
841                 lcr |= MCT_U232_SET_BREAK;
842
843         mct_u232_set_line_ctrl(serial, lcr);
844 } /* mct_u232_break_ctl */
845 #endif
846
847 static int klsi_105_tiocmget (struct usb_serial_port *port, struct file *file)
848 {
849         struct klsi_105_private *priv = usb_get_serial_port_data(port);
850         unsigned long flags;
851         int rc;
852         unsigned long line_state;
853         dbg("%s - request, just guessing", __FUNCTION__);
854
855         rc = klsi_105_get_line_state(port, &line_state);
856         if (rc < 0) {
857                 err("Reading line control failed (error = %d)", rc);
858                 /* better return value? EAGAIN? */
859                 return rc;
860         }
861
862         spin_lock_irqsave (&priv->lock, flags);
863         priv->line_state = line_state;
864         spin_unlock_irqrestore (&priv->lock, flags);
865         dbg("%s - read line state 0x%lx", __FUNCTION__, line_state);
866         return (int)line_state;
867 }
868
869 static int klsi_105_tiocmset (struct usb_serial_port *port, struct file *file,
870                               unsigned int set, unsigned int clear)
871 {
872         int retval = -EINVAL;
873         
874         dbg("%s", __FUNCTION__);
875
876 /* if this ever gets implemented, it should be done something like this:
877         struct usb_serial *serial = port->serial;
878         struct klsi_105_private *priv = usb_get_serial_port_data(port);
879         unsigned long flags;
880         int control;
881
882         spin_lock_irqsave (&priv->lock, flags);
883         if (set & TIOCM_RTS)
884                 priv->control_state |= TIOCM_RTS;
885         if (set & TIOCM_DTR)
886                 priv->control_state |= TIOCM_DTR;
887         if (clear & TIOCM_RTS)
888                 priv->control_state &= ~TIOCM_RTS;
889         if (clear & TIOCM_DTR)
890                 priv->control_state &= ~TIOCM_DTR;
891         control = priv->control_state;
892         spin_unlock_irqrestore (&priv->lock, flags);
893         retval = mct_u232_set_modem_ctrl(serial, control);
894 */
895         return retval;
896 }
897
898 static void klsi_105_throttle (struct usb_serial_port *port)
899 {
900         dbg("%s - port %d", __FUNCTION__, port->number);
901         usb_kill_urb(port->read_urb);
902 }
903
904 static void klsi_105_unthrottle (struct usb_serial_port *port)
905 {
906         int result;
907
908         dbg("%s - port %d", __FUNCTION__, port->number);
909
910         port->read_urb->dev = port->serial->dev;
911         result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
912         if (result)
913                 err("%s - failed submitting read urb, error %d", __FUNCTION__,
914                     result);
915 }
916
917
918
919 static int __init klsi_105_init (void)
920 {
921         int retval;
922         retval = usb_serial_register(&kl5kusb105d_device);
923         if (retval)
924                 goto failed_usb_serial_register;
925         retval = usb_register(&kl5kusb105d_driver);
926         if (retval)
927                 goto failed_usb_register;
928
929         info(DRIVER_DESC " " DRIVER_VERSION);
930         return 0;
931 failed_usb_register:
932         usb_serial_deregister(&kl5kusb105d_device);
933 failed_usb_serial_register:
934         return retval;
935 }
936
937
938 static void __exit klsi_105_exit (void)
939 {
940         usb_deregister (&kl5kusb105d_driver);
941         usb_serial_deregister (&kl5kusb105d_device);
942 }
943
944
945 module_init (klsi_105_init);
946 module_exit (klsi_105_exit);
947
948 MODULE_AUTHOR( DRIVER_AUTHOR );
949 MODULE_DESCRIPTION( DRIVER_DESC );
950 MODULE_LICENSE("GPL"); 
951
952
953 module_param(debug, bool, S_IRUGO | S_IWUSR);
954 MODULE_PARM_DESC(debug, "enable extensive debugging messages");
955
956 /* vim: set sts=8 ts=8 sw=8: */