allow coexistance of N build and AC build.
[tomato.git] / release / src-rt-6.x / linux / linux-2.6 / drivers / usb / serial / kl5kusb105.c
blob814ed67827d175d54ff1ec47dfccf372e28afaad
1 /*
2 * KLSI KL5KUSB105 chip RS232 converter driver
4 * Copyright (C) 2001 Utz-Uwe Haus <haus@uuhaus.de>
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.
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.
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.
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!
32 /* TODO:
33 * check modem line signals
34 * implement handshaking or decide that we do not support it
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
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.
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 <asm/uaccess.h>
57 #include <asm/unaligned.h>
58 #include <linux/usb.h>
59 #include <linux/usb/serial.h>
60 #include "kl5kusb105.h"
62 static int debug;
65 * Version Information
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"
73 * Function prototypes
75 static int klsi_105_startup (struct usb_serial *serial);
76 static void klsi_105_shutdown (struct usb_serial *serial);
77 static int klsi_105_open (struct usb_serial_port *port,
78 struct file *filp);
79 static void klsi_105_close (struct usb_serial_port *port,
80 struct file *filp);
81 static int klsi_105_write (struct usb_serial_port *port,
82 const unsigned char *buf,
83 int count);
84 static void klsi_105_write_bulk_callback (struct urb *urb);
85 static int klsi_105_chars_in_buffer (struct usb_serial_port *port);
86 static int klsi_105_write_room (struct usb_serial_port *port);
88 static void klsi_105_read_bulk_callback (struct urb *urb);
89 static void klsi_105_set_termios (struct usb_serial_port *port,
90 struct ktermios *old);
91 static void klsi_105_throttle (struct usb_serial_port *port);
92 static void klsi_105_unthrottle (struct usb_serial_port *port);
94 static void klsi_105_break_ctl (struct usb_serial_port *port,
95 int break_state );
97 static int klsi_105_tiocmget (struct usb_serial_port *port,
98 struct file *file);
99 static int klsi_105_tiocmset (struct usb_serial_port *port,
100 struct file *file, unsigned int set,
101 unsigned int clear);
104 * All of the device info needed for the KLSI converters.
106 static struct usb_device_id id_table [] = {
107 { USB_DEVICE(PALMCONNECT_VID, PALMCONNECT_PID) },
108 { USB_DEVICE(KLSI_VID, KLSI_KL5KUSB105D_PID) },
109 { } /* Terminating entry */
112 MODULE_DEVICE_TABLE (usb, id_table);
114 static struct usb_driver kl5kusb105d_driver = {
115 .name = "kl5kusb105d",
116 .probe = usb_serial_probe,
117 .disconnect = usb_serial_disconnect,
118 .id_table = id_table,
119 .no_dynamic_id = 1,
122 static struct usb_serial_driver kl5kusb105d_device = {
123 .driver = {
124 .owner = THIS_MODULE,
125 .name = "kl5kusb105d",
127 .description = "KL5KUSB105D / PalmConnect",
128 .usb_driver = &kl5kusb105d_driver,
129 .id_table = id_table,
130 .num_interrupt_in = 1,
131 .num_bulk_in = 1,
132 .num_bulk_out = 1,
133 .num_ports = 1,
134 .open = klsi_105_open,
135 .close = klsi_105_close,
136 .write = klsi_105_write,
137 .write_bulk_callback = klsi_105_write_bulk_callback,
138 .chars_in_buffer = klsi_105_chars_in_buffer,
139 .write_room = klsi_105_write_room,
140 .read_bulk_callback =klsi_105_read_bulk_callback,
141 .set_termios = klsi_105_set_termios,
142 /*.break_ctl = klsi_105_break_ctl,*/
143 .tiocmget = klsi_105_tiocmget,
144 .tiocmset = klsi_105_tiocmset,
145 .attach = klsi_105_startup,
146 .shutdown = klsi_105_shutdown,
147 .throttle = klsi_105_throttle,
148 .unthrottle = klsi_105_unthrottle,
151 struct klsi_105_port_settings {
152 __u8 pktlen; /* always 5, it seems */
153 __u8 baudrate;
154 __u8 databits;
155 __u8 unknown1;
156 __u8 unknown2;
157 } __attribute__ ((packed));
159 /* we implement a pool of NUM_URBS urbs per usb_serial */
160 #define NUM_URBS 1
161 #define URB_TRANSFER_BUFFER_SIZE 64
162 struct klsi_105_private {
163 struct klsi_105_port_settings cfg;
164 struct ktermios termios;
165 unsigned long line_state; /* modem line settings */
166 /* write pool */
167 struct urb * write_urb_pool[NUM_URBS];
168 spinlock_t lock;
169 unsigned long bytes_in;
170 unsigned long bytes_out;
175 * Handle vendor specific USB requests
179 #define KLSI_TIMEOUT 5000 /* default urb timeout */
181 static int klsi_105_chg_port_settings(struct usb_serial_port *port,
182 struct klsi_105_port_settings *settings)
184 int rc;
186 rc = usb_control_msg(port->serial->dev,
187 usb_sndctrlpipe(port->serial->dev, 0),
188 KL5KUSB105A_SIO_SET_DATA,
189 USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_INTERFACE,
190 0, /* value */
191 0, /* index */
192 settings,
193 sizeof(struct klsi_105_port_settings),
194 KLSI_TIMEOUT);
195 if (rc < 0)
196 err("Change port settings failed (error = %d)", rc);
197 info("%s - %d byte block, baudrate %x, databits %d, u1 %d, u2 %d",
198 __FUNCTION__,
199 settings->pktlen,
200 settings->baudrate, settings->databits,
201 settings->unknown1, settings->unknown2);
202 return rc;
203 } /* klsi_105_chg_port_settings */
205 /* translate a 16-bit status value from the device to linux's TIO bits */
206 static unsigned long klsi_105_status2linestate(const __u16 status)
208 unsigned long res = 0;
210 res = ((status & KL5KUSB105A_DSR) ? TIOCM_DSR : 0)
211 | ((status & KL5KUSB105A_CTS) ? TIOCM_CTS : 0)
214 return res;
217 * Read line control via vendor command and return result through
218 * *line_state_p
220 /* It seems that the status buffer has always only 2 bytes length */
221 #define KLSI_STATUSBUF_LEN 2
222 static int klsi_105_get_line_state(struct usb_serial_port *port,
223 unsigned long *line_state_p)
225 int rc;
226 __u8 status_buf[KLSI_STATUSBUF_LEN] = { -1,-1};
227 __u16 status;
229 info("%s - sending SIO Poll request", __FUNCTION__);
230 rc = usb_control_msg(port->serial->dev,
231 usb_rcvctrlpipe(port->serial->dev, 0),
232 KL5KUSB105A_SIO_POLL,
233 USB_TYPE_VENDOR | USB_DIR_IN,
234 0, /* value */
235 0, /* index */
236 status_buf, KLSI_STATUSBUF_LEN,
237 10000
239 if (rc < 0)
240 err("Reading line status failed (error = %d)", rc);
241 else {
242 status = le16_to_cpu(get_unaligned((__le16 *)status_buf));
244 info("%s - read status %x %x", __FUNCTION__,
245 status_buf[0], status_buf[1]);
247 *line_state_p = klsi_105_status2linestate(status);
250 return rc;
255 * Driver's tty interface functions
258 static int klsi_105_startup (struct usb_serial *serial)
260 struct klsi_105_private *priv;
261 int i, j;
263 /* check if we support the product id (see keyspan.c)
264 * FIXME
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.", __FUNCTION__);
273 i--;
274 goto err_cleanup;
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;
283 priv->line_state = 0;
285 priv->bytes_in = 0;
286 priv->bytes_out = 0;
287 usb_set_serial_port_data(serial->port[i], priv);
289 spin_lock_init (&priv->lock);
290 for (j=0; j<NUM_URBS; j++) {
291 struct urb* urb = usb_alloc_urb(0, GFP_KERNEL);
293 priv->write_urb_pool[j] = urb;
294 if (urb == NULL) {
295 err("No more urbs???");
296 goto err_cleanup;
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 goto err_cleanup;
307 /* priv->termios is left uninitalized until port opening */
308 init_waitqueue_head(&serial->port[i]->write_wait);
311 return 0;
313 err_cleanup:
314 for (; i >= 0; i--) {
315 priv = usb_get_serial_port_data(serial->port[i]);
316 for (j=0; j < NUM_URBS; j++) {
317 if (priv->write_urb_pool[j]) {
318 kfree(priv->write_urb_pool[j]->transfer_buffer);
319 usb_free_urb(priv->write_urb_pool[j]);
322 usb_set_serial_port_data(serial->port[i], NULL);
324 return -ENOMEM;
325 } /* klsi_105_startup */
328 static void klsi_105_shutdown (struct usb_serial *serial)
330 int i;
332 dbg("%s", __FUNCTION__);
334 /* stop reads and writes on all ports */
335 for (i=0; i < serial->num_ports; ++i) {
336 struct klsi_105_private *priv = usb_get_serial_port_data(serial->port[i]);
337 unsigned long flags;
339 if (priv) {
340 /* kill our write urb pool */
341 int j;
342 struct urb **write_urbs = priv->write_urb_pool;
343 spin_lock_irqsave(&priv->lock,flags);
345 for (j = 0; j < NUM_URBS; j++) {
346 if (write_urbs[j]) {
347 /* FIXME - uncomment the following
348 * usb_kill_urb call when the host
349 * controllers get fixed to set
350 * urb->dev = NULL after the urb is
351 * finished. Otherwise this call
352 * oopses. */
353 /* usb_kill_urb(write_urbs[j]); */
354 kfree(write_urbs[j]->transfer_buffer);
355 usb_free_urb (write_urbs[j]);
359 spin_unlock_irqrestore (&priv->lock, flags);
361 kfree(priv);
362 usb_set_serial_port_data(serial->port[i], NULL);
365 } /* klsi_105_shutdown */
367 static int klsi_105_open (struct usb_serial_port *port, struct file *filp)
369 struct klsi_105_private *priv = usb_get_serial_port_data(port);
370 int retval = 0;
371 int rc;
372 int i;
373 unsigned long line_state;
374 struct klsi_105_port_settings cfg;
375 unsigned long flags;
377 dbg("%s port %d", __FUNCTION__, port->number);
379 /* force low_latency on so that our tty_push actually forces
380 * the data through
381 * port->tty->low_latency = 1; */
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.
390 cfg.pktlen = 5;
391 cfg.baudrate = kl5kusb105a_sio_b9600;
392 cfg.databits = kl5kusb105a_dtb_8;
393 cfg.unknown1 = 0;
394 cfg.unknown2 = 1;
395 klsi_105_chg_port_settings(port, &cfg);
397 /* set up termios structure */
398 spin_lock_irqsave (&priv->lock, flags);
399 priv->termios.c_iflag = port->tty->termios->c_iflag;
400 priv->termios.c_oflag = port->tty->termios->c_oflag;
401 priv->termios.c_cflag = port->tty->termios->c_cflag;
402 priv->termios.c_lflag = port->tty->termios->c_lflag;
403 for (i=0; i<NCCS; i++)
404 priv->termios.c_cc[i] = port->tty->termios->c_cc[i];
405 priv->cfg.pktlen = cfg.pktlen;
406 priv->cfg.baudrate = cfg.baudrate;
407 priv->cfg.databits = cfg.databits;
408 priv->cfg.unknown1 = cfg.unknown1;
409 priv->cfg.unknown2 = cfg.unknown2;
410 spin_unlock_irqrestore (&priv->lock, flags);
412 /* READ_ON and urb submission */
413 usb_fill_bulk_urb(port->read_urb, port->serial->dev,
414 usb_rcvbulkpipe(port->serial->dev,
415 port->bulk_in_endpointAddress),
416 port->read_urb->transfer_buffer,
417 port->read_urb->transfer_buffer_length,
418 klsi_105_read_bulk_callback,
419 port);
421 rc = usb_submit_urb(port->read_urb, GFP_KERNEL);
422 if (rc) {
423 err("%s - failed submitting read urb, error %d", __FUNCTION__, rc);
424 retval = rc;
425 goto exit;
428 rc = usb_control_msg(port->serial->dev,
429 usb_sndctrlpipe(port->serial->dev,0),
430 KL5KUSB105A_SIO_CONFIGURE,
431 USB_TYPE_VENDOR|USB_DIR_OUT|USB_RECIP_INTERFACE,
432 KL5KUSB105A_SIO_CONFIGURE_READ_ON,
433 0, /* index */
434 NULL,
436 KLSI_TIMEOUT);
437 if (rc < 0) {
438 err("Enabling read failed (error = %d)", rc);
439 retval = rc;
440 } else
441 dbg("%s - enabled reading", __FUNCTION__);
443 rc = klsi_105_get_line_state(port, &line_state);
444 if (rc >= 0) {
445 spin_lock_irqsave (&priv->lock, flags);
446 priv->line_state = line_state;
447 spin_unlock_irqrestore (&priv->lock, flags);
448 dbg("%s - read line state 0x%lx", __FUNCTION__, line_state);
449 retval = 0;
450 } else
451 retval = rc;
453 exit:
454 return retval;
455 } /* klsi_105_open */
458 static void klsi_105_close (struct usb_serial_port *port, struct file *filp)
460 struct klsi_105_private *priv = usb_get_serial_port_data(port);
461 int rc;
463 dbg("%s port %d", __FUNCTION__, port->number);
465 /* send READ_OFF */
466 rc = usb_control_msg (port->serial->dev,
467 usb_sndctrlpipe(port->serial->dev, 0),
468 KL5KUSB105A_SIO_CONFIGURE,
469 USB_TYPE_VENDOR | USB_DIR_OUT,
470 KL5KUSB105A_SIO_CONFIGURE_READ_OFF,
471 0, /* index */
472 NULL, 0,
473 KLSI_TIMEOUT);
474 if (rc < 0)
475 err("Disabling read failed (error = %d)", rc);
477 /* shutdown our bulk reads and writes */
478 usb_kill_urb(port->write_urb);
479 usb_kill_urb(port->read_urb);
480 /* unlink our write pool */
481 /* FIXME */
482 /* wgg - do I need this? I think so. */
483 usb_kill_urb(port->interrupt_in_urb);
484 info("kl5kusb105 port stats: %ld bytes in, %ld bytes out", priv->bytes_in, priv->bytes_out);
485 } /* klsi_105_close */
488 /* We need to write a complete 64-byte data block and encode the
489 * number actually sent in the first double-byte, LSB-order. That
490 * leaves at most 62 bytes of payload.
492 #define KLSI_105_DATA_OFFSET 2 /* in the bulk urb data block */
495 static int klsi_105_write (struct usb_serial_port *port,
496 const unsigned char *buf, int count)
498 struct klsi_105_private *priv = usb_get_serial_port_data(port);
499 int result, size;
500 int bytes_sent=0;
502 dbg("%s - port %d", __FUNCTION__, port->number);
504 while (count > 0) {
505 /* try to find a free urb (write 0 bytes if none) */
506 struct urb *urb = NULL;
507 unsigned long flags;
508 int i;
509 /* since the pool is per-port we might not need the spin lock !? */
510 spin_lock_irqsave (&priv->lock, flags);
511 for (i=0; i<NUM_URBS; i++) {
512 if (priv->write_urb_pool[i]->status != -EINPROGRESS) {
513 urb = priv->write_urb_pool[i];
514 dbg("%s - using pool URB %d", __FUNCTION__, i);
515 break;
518 spin_unlock_irqrestore (&priv->lock, flags);
520 if (urb==NULL) {
521 dbg("%s - no more free urbs", __FUNCTION__);
522 goto exit;
525 if (urb->transfer_buffer == NULL) {
526 urb->transfer_buffer = kmalloc (URB_TRANSFER_BUFFER_SIZE, GFP_ATOMIC);
527 if (urb->transfer_buffer == NULL) {
528 err("%s - no more kernel memory...", __FUNCTION__);
529 goto exit;
533 size = min (count, port->bulk_out_size - KLSI_105_DATA_OFFSET);
534 size = min (size, URB_TRANSFER_BUFFER_SIZE - KLSI_105_DATA_OFFSET);
536 memcpy (urb->transfer_buffer + KLSI_105_DATA_OFFSET, buf, size);
538 /* write payload size into transfer buffer */
539 ((__u8 *)urb->transfer_buffer)[0] = (__u8) (size & 0xFF);
540 ((__u8 *)urb->transfer_buffer)[1] = (__u8) ((size & 0xFF00)>>8);
542 /* set up our urb */
543 usb_fill_bulk_urb(urb, port->serial->dev,
544 usb_sndbulkpipe(port->serial->dev,
545 port->bulk_out_endpointAddress),
546 urb->transfer_buffer,
547 URB_TRANSFER_BUFFER_SIZE,
548 klsi_105_write_bulk_callback,
549 port);
551 /* send the data out the bulk port */
552 result = usb_submit_urb(urb, GFP_ATOMIC);
553 if (result) {
554 err("%s - failed submitting write urb, error %d", __FUNCTION__, result);
555 goto exit;
557 buf += size;
558 bytes_sent += size;
559 count -= size;
561 exit:
562 /* lockless, but it's for debug info only... */
563 priv->bytes_out+=bytes_sent;
565 return bytes_sent; /* that's how much we wrote */
566 } /* klsi_105_write */
568 static void klsi_105_write_bulk_callback ( struct urb *urb)
570 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
571 int status = urb->status;
573 dbg("%s - port %d", __FUNCTION__, port->number);
575 if (status) {
576 dbg("%s - nonzero write bulk status received: %d", __FUNCTION__,
577 status);
578 return;
581 usb_serial_port_softint(port);
582 } /* klsi_105_write_bulk_completion_callback */
585 /* return number of characters currently in the writing process */
586 static int klsi_105_chars_in_buffer (struct usb_serial_port *port)
588 int chars = 0;
589 int i;
590 unsigned long flags;
591 struct klsi_105_private *priv = usb_get_serial_port_data(port);
593 spin_lock_irqsave (&priv->lock, flags);
595 for (i = 0; i < NUM_URBS; ++i) {
596 if (priv->write_urb_pool[i]->status == -EINPROGRESS) {
597 chars += URB_TRANSFER_BUFFER_SIZE;
601 spin_unlock_irqrestore (&priv->lock, flags);
603 dbg("%s - returns %d", __FUNCTION__, chars);
604 return (chars);
607 static int klsi_105_write_room (struct usb_serial_port *port)
609 unsigned long flags;
610 int i;
611 int room = 0;
612 struct klsi_105_private *priv = usb_get_serial_port_data(port);
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;
621 spin_unlock_irqrestore (&priv->lock, flags);
623 dbg("%s - returns %d", __FUNCTION__, room);
624 return (room);
629 static void klsi_105_read_bulk_callback (struct urb *urb)
631 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
632 struct klsi_105_private *priv = usb_get_serial_port_data(port);
633 struct tty_struct *tty;
634 unsigned char *data = urb->transfer_buffer;
635 int rc;
636 int status = urb->status;
638 dbg("%s - port %d", __FUNCTION__, port->number);
640 /* The urb might have been killed. */
641 if (status) {
642 dbg("%s - nonzero read bulk status received: %d", __FUNCTION__,
643 status);
644 return;
647 /* The data received is again preceded by a length double-byte in LSB-
648 * first order (see klsi_105_write() )
650 if (urb->actual_length == 0) {
651 /* empty urbs seem to happen, we ignore them */
652 /* dbg("%s - emtpy URB", __FUNCTION__); */
654 } else if (urb->actual_length <= 2) {
655 dbg("%s - size %d URB not understood", __FUNCTION__,
656 urb->actual_length);
657 usb_serial_debug_data(debug, &port->dev, __FUNCTION__,
658 urb->actual_length, data);
659 } else {
660 int bytes_sent = ((__u8 *) data)[0] +
661 ((unsigned int) ((__u8 *) data)[1] << 8);
662 tty = port->tty;
663 /* we should immediately resubmit the URB, before attempting
664 * to pass the data on to the tty layer. But that needs locking
665 * against re-entry an then mixed-up data because of
666 * intermixed tty_flip_buffer_push()s
667 * FIXME
669 usb_serial_debug_data(debug, &port->dev, __FUNCTION__,
670 urb->actual_length, data);
672 if (bytes_sent + 2 > urb->actual_length) {
673 dbg("%s - trying to read more data than available"
674 " (%d vs. %d)", __FUNCTION__,
675 bytes_sent+2, urb->actual_length);
676 /* cap at implied limit */
677 bytes_sent = urb->actual_length - 2;
680 tty_buffer_request_room(tty, bytes_sent);
681 tty_insert_flip_string(tty, data + 2, bytes_sent);
682 tty_flip_buffer_push(tty);
684 /* again lockless, but debug info only */
685 priv->bytes_in += bytes_sent;
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", __FUNCTION__, rc);
698 } /* klsi_105_read_bulk_callback */
701 static void klsi_105_set_termios (struct usb_serial_port *port,
702 struct ktermios *old_termios)
704 struct klsi_105_private *priv = usb_get_serial_port_data(port);
705 unsigned int iflag = port->tty->termios->c_iflag;
706 unsigned int old_iflag = old_termios->c_iflag;
707 unsigned int cflag = port->tty->termios->c_cflag;
708 unsigned int old_cflag = old_termios->c_cflag;
709 struct klsi_105_port_settings cfg;
710 unsigned long flags;
712 /* lock while we are modifying the settings */
713 spin_lock_irqsave (&priv->lock, flags);
716 * Update baud rate
718 if( (cflag & CBAUD) != (old_cflag & CBAUD) ) {
719 /* reassert DTR and (maybe) RTS on transition from B0 */
720 if( (old_cflag & CBAUD) == B0 ) {
721 dbg("%s: baud was B0", __FUNCTION__);
722 #if 0
723 priv->control_state |= TIOCM_DTR;
724 /* don't set RTS if using hardware flow control */
725 if (!(old_cflag & CRTSCTS)) {
726 priv->control_state |= TIOCM_RTS;
728 mct_u232_set_modem_ctrl(serial, priv->control_state);
729 #endif
732 switch(cflag & CBAUD) {
733 case B0: /* handled below */
734 break;
735 case B1200: priv->cfg.baudrate = kl5kusb105a_sio_b1200;
736 break;
737 case B2400: priv->cfg.baudrate = kl5kusb105a_sio_b2400;
738 break;
739 case B4800: priv->cfg.baudrate = kl5kusb105a_sio_b4800;
740 break;
741 case B9600: priv->cfg.baudrate = kl5kusb105a_sio_b9600;
742 break;
743 case B19200: priv->cfg.baudrate = kl5kusb105a_sio_b19200;
744 break;
745 case B38400: priv->cfg.baudrate = kl5kusb105a_sio_b38400;
746 break;
747 case B57600: priv->cfg.baudrate = kl5kusb105a_sio_b57600;
748 break;
749 case B115200: priv->cfg.baudrate = kl5kusb105a_sio_b115200;
750 break;
751 default:
752 err("KLSI USB->Serial converter:"
753 " unsupported baudrate request, using default"
754 " of 9600");
755 priv->cfg.baudrate = kl5kusb105a_sio_b9600;
756 break;
758 if ((cflag & CBAUD) == B0 ) {
759 dbg("%s: baud is B0", __FUNCTION__);
760 /* Drop RTS and DTR */
761 /* maybe this should be simulated by sending read
762 * disable and read enable messages?
765 #if 0
766 priv->control_state &= ~(TIOCM_DTR | TIOCM_RTS);
767 mct_u232_set_modem_ctrl(serial, priv->control_state);
768 #endif
772 if ((cflag & CSIZE) != (old_cflag & CSIZE)) {
773 /* set the number of data bits */
774 switch (cflag & CSIZE) {
775 case CS5:
776 dbg("%s - 5 bits/byte not supported", __FUNCTION__);
777 spin_unlock_irqrestore (&priv->lock, flags);
778 return ;
779 case CS6:
780 dbg("%s - 6 bits/byte not supported", __FUNCTION__);
781 spin_unlock_irqrestore (&priv->lock, flags);
782 return ;
783 case CS7:
784 priv->cfg.databits = kl5kusb105a_dtb_7;
785 break;
786 case CS8:
787 priv->cfg.databits = kl5kusb105a_dtb_8;
788 break;
789 default:
790 err("CSIZE was not CS5-CS8, using default of 8");
791 priv->cfg.databits = kl5kusb105a_dtb_8;
792 break;
797 * Update line control register (LCR)
799 if ((cflag & (PARENB|PARODD)) != (old_cflag & (PARENB|PARODD))
800 || (cflag & CSTOPB) != (old_cflag & CSTOPB) ) {
802 #if 0
803 priv->last_lcr = 0;
805 /* set the parity */
806 if (cflag & PARENB)
807 priv->last_lcr |= (cflag & PARODD) ?
808 MCT_U232_PARITY_ODD : MCT_U232_PARITY_EVEN;
809 else
810 priv->last_lcr |= MCT_U232_PARITY_NONE;
812 /* set the number of stop bits */
813 priv->last_lcr |= (cflag & CSTOPB) ?
814 MCT_U232_STOP_BITS_2 : MCT_U232_STOP_BITS_1;
816 mct_u232_set_line_ctrl(serial, priv->last_lcr);
817 #endif
822 * Set flow control: well, I do not really now how to handle DTR/RTS.
823 * Just do what we have seen with SniffUSB on Win98.
825 if( (iflag & IXOFF) != (old_iflag & IXOFF)
826 || (iflag & IXON) != (old_iflag & IXON)
827 || (cflag & CRTSCTS) != (old_cflag & CRTSCTS) ) {
829 /* Drop DTR/RTS if no flow control otherwise assert */
830 #if 0
831 if ((iflag & IXOFF) || (iflag & IXON) || (cflag & CRTSCTS) )
832 priv->control_state |= TIOCM_DTR | TIOCM_RTS;
833 else
834 priv->control_state &= ~(TIOCM_DTR | TIOCM_RTS);
835 mct_u232_set_modem_ctrl(serial, priv->control_state);
836 #endif
839 memcpy (&cfg, &priv->cfg, sizeof(cfg));
840 spin_unlock_irqrestore (&priv->lock, flags);
842 /* now commit changes to device */
843 klsi_105_chg_port_settings(port, &cfg);
844 } /* klsi_105_set_termios */
847 #if 0
848 static void mct_u232_break_ctl( struct usb_serial_port *port, int break_state )
850 struct usb_serial *serial = port->serial;
851 struct mct_u232_private *priv = (struct mct_u232_private *)port->private;
852 unsigned char lcr = priv->last_lcr;
854 dbg("%sstate=%d", __FUNCTION__, break_state);
856 if (break_state)
857 lcr |= MCT_U232_SET_BREAK;
859 mct_u232_set_line_ctrl(serial, lcr);
860 } /* mct_u232_break_ctl */
861 #endif
863 static int klsi_105_tiocmget (struct usb_serial_port *port, struct file *file)
865 struct klsi_105_private *priv = usb_get_serial_port_data(port);
866 unsigned long flags;
867 int rc;
868 unsigned long line_state;
869 dbg("%s - request, just guessing", __FUNCTION__);
871 rc = klsi_105_get_line_state(port, &line_state);
872 if (rc < 0) {
873 err("Reading line control failed (error = %d)", rc);
874 /* better return value? EAGAIN? */
875 return rc;
878 spin_lock_irqsave (&priv->lock, flags);
879 priv->line_state = line_state;
880 spin_unlock_irqrestore (&priv->lock, flags);
881 dbg("%s - read line state 0x%lx", __FUNCTION__, line_state);
882 return (int)line_state;
885 static int klsi_105_tiocmset (struct usb_serial_port *port, struct file *file,
886 unsigned int set, unsigned int clear)
888 int retval = -EINVAL;
890 dbg("%s", __FUNCTION__);
892 /* if this ever gets implemented, it should be done something like this:
893 struct usb_serial *serial = port->serial;
894 struct klsi_105_private *priv = usb_get_serial_port_data(port);
895 unsigned long flags;
896 int control;
898 spin_lock_irqsave (&priv->lock, flags);
899 if (set & TIOCM_RTS)
900 priv->control_state |= TIOCM_RTS;
901 if (set & TIOCM_DTR)
902 priv->control_state |= TIOCM_DTR;
903 if (clear & TIOCM_RTS)
904 priv->control_state &= ~TIOCM_RTS;
905 if (clear & TIOCM_DTR)
906 priv->control_state &= ~TIOCM_DTR;
907 control = priv->control_state;
908 spin_unlock_irqrestore (&priv->lock, flags);
909 retval = mct_u232_set_modem_ctrl(serial, control);
911 return retval;
914 static void klsi_105_throttle (struct usb_serial_port *port)
916 dbg("%s - port %d", __FUNCTION__, port->number);
917 usb_kill_urb(port->read_urb);
920 static void klsi_105_unthrottle (struct usb_serial_port *port)
922 int result;
924 dbg("%s - port %d", __FUNCTION__, port->number);
926 port->read_urb->dev = port->serial->dev;
927 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
928 if (result)
929 err("%s - failed submitting read urb, error %d", __FUNCTION__,
930 result);
935 static int __init klsi_105_init (void)
937 int retval;
938 retval = usb_serial_register(&kl5kusb105d_device);
939 if (retval)
940 goto failed_usb_serial_register;
941 retval = usb_register(&kl5kusb105d_driver);
942 if (retval)
943 goto failed_usb_register;
945 info(DRIVER_DESC " " DRIVER_VERSION);
946 return 0;
947 failed_usb_register:
948 usb_serial_deregister(&kl5kusb105d_device);
949 failed_usb_serial_register:
950 return retval;
954 static void __exit klsi_105_exit (void)
956 usb_deregister (&kl5kusb105d_driver);
957 usb_serial_deregister (&kl5kusb105d_device);
961 module_init (klsi_105_init);
962 module_exit (klsi_105_exit);
964 MODULE_AUTHOR( DRIVER_AUTHOR );
965 MODULE_DESCRIPTION( DRIVER_DESC );
966 MODULE_LICENSE("GPL");
969 module_param(debug, bool, S_IRUGO | S_IWUSR);
970 MODULE_PARM_DESC(debug, "enable extensive debugging messages");
972 /* vim: set sts=8 ts=8 sw=8: */