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