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