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