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