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