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