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