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
13 * It was written out of frustration with the PalmConnect USB Serial adapter
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
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!
33 * check modem line signals
34 * implement handshaking or decide that we do not support it
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/config.h>
49 #include <linux/kernel.h>
50 #include <linux/errno.h>
51 #include <linux/init.h>
52 #include <linux/slab.h>
53 #include <linux/tty.h>
54 #include <linux/tty_driver.h>
55 #include <linux/tty_flip.h>
56 #include <linux/module.h>
57 #include <asm/uaccess.h>
58 #include <linux/usb.h>
60 #ifdef CONFIG_USB_SERIAL_DEBUG
66 #include "usb-serial.h"
67 #include "kl5kusb105.h"
73 #define DRIVER_VERSION "v0.3a"
74 #define DRIVER_AUTHOR "Utz-Uwe Haus <haus@uuhaus.de>"
75 #define DRIVER_DESC "KLSI KL5KUSB105 chipset USB->Serial Converter driver"
81 static int klsi_105_startup (struct usb_serial *serial);
82 static void klsi_105_shutdown (struct usb_serial *serial);
83 static int klsi_105_open (struct usb_serial_port *port,
85 static void klsi_105_close (struct usb_serial_port *port,
87 static int klsi_105_write (struct usb_serial_port *port,
89 const unsigned char *buf,
91 static void klsi_105_write_bulk_callback (struct urb *urb, struct pt_regs *regs);
92 static int klsi_105_chars_in_buffer (struct usb_serial_port *port);
93 static int klsi_105_write_room (struct usb_serial_port *port);
95 static void klsi_105_read_bulk_callback (struct urb *urb, struct pt_regs *regs);
96 static void klsi_105_set_termios (struct usb_serial_port *port,
97 struct termios * old);
98 static int klsi_105_ioctl (struct usb_serial_port *port,
102 static void klsi_105_throttle (struct usb_serial_port *port);
103 static void klsi_105_unthrottle (struct usb_serial_port *port);
105 static void klsi_105_break_ctl (struct usb_serial_port *port,
108 static int klsi_105_tiocmget (struct usb_serial_port *port,
110 static int klsi_105_tiocmset (struct usb_serial_port *port,
111 struct file *file, unsigned int set,
115 * All of the device info needed for the KLSI converters.
117 static struct usb_device_id id_table [] = {
118 { USB_DEVICE(PALMCONNECT_VID, PALMCONNECT_PID) },
119 { USB_DEVICE(KLSI_VID, KLSI_KL5KUSB105D_PID) },
120 { } /* Terminating entry */
123 MODULE_DEVICE_TABLE (usb, id_table);
125 static struct usb_driver kl5kusb105d_driver = {
126 .owner = THIS_MODULE,
127 .name = "kl5kusb105d",
128 .probe = usb_serial_probe,
129 .disconnect = usb_serial_disconnect,
130 .id_table = id_table,
133 static struct usb_serial_device_type kl5kusb105d_device = {
134 .owner = THIS_MODULE,
135 .name = "KL5KUSB105D / PalmConnect",
136 .short_name = "kl5kusb105d",
137 .id_table = id_table,
138 .num_interrupt_in = 1,
142 .open = klsi_105_open,
143 .close = klsi_105_close,
144 .write = klsi_105_write,
145 .write_bulk_callback = klsi_105_write_bulk_callback,
146 .chars_in_buffer = klsi_105_chars_in_buffer,
147 .write_room = klsi_105_write_room,
148 .read_bulk_callback =klsi_105_read_bulk_callback,
149 .ioctl = klsi_105_ioctl,
150 .set_termios = klsi_105_set_termios,
151 /*.break_ctl = klsi_105_break_ctl,*/
152 .tiocmget = klsi_105_tiocmget,
153 .tiocmset = klsi_105_tiocmset,
154 .attach = klsi_105_startup,
155 .shutdown = klsi_105_shutdown,
156 .throttle = klsi_105_throttle,
157 .unthrottle = klsi_105_unthrottle,
160 struct klsi_105_port_settings {
161 __u8 pktlen; /* always 5, it seems */
166 } __attribute__ ((packed));
168 /* we implement a pool of NUM_URBS urbs per usb_serial */
170 #define URB_TRANSFER_BUFFER_SIZE 64
171 struct klsi_105_private {
172 struct klsi_105_port_settings cfg;
173 struct termios termios;
174 unsigned long line_state; /* modem line settings */
176 struct urb * write_urb_pool[NUM_URBS];
178 unsigned long bytes_in;
179 unsigned long bytes_out;
184 * Handle vendor specific USB requests
188 #define KLSI_TIMEOUT (HZ * 5 ) /* default urb timeout */
190 static int klsi_105_chg_port_settings(struct usb_serial *serial,
191 struct klsi_105_port_settings *settings)
195 rc = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
196 KL5KUSB105A_SIO_SET_DATA,
197 USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_INTERFACE,
201 sizeof(struct klsi_105_port_settings),
204 err("Change port settings failed (error = %d)", rc);
205 info("%s - %d byte block, baudrate %x, databits %d, u1 %d, u2 %d",
208 settings->baudrate, settings->databits,
209 settings->unknown1, settings->unknown2);
211 } /* klsi_105_chg_port_settings */
213 /* translate a 16-bit status value from the device to linux's TIO bits */
214 static unsigned long klsi_105_status2linestate(const __u16 status)
216 unsigned long res = 0;
218 res = ((status & KL5KUSB105A_DSR) ? TIOCM_DSR : 0)
219 | ((status & KL5KUSB105A_CTS) ? TIOCM_CTS : 0)
225 * Read line control via vendor command and return result through
228 /* It seems that the status buffer has always only 2 bytes length */
229 #define KLSI_STATUSBUF_LEN 2
230 static int klsi_105_get_line_state(struct usb_serial *serial,
231 unsigned long *line_state_p)
234 __u8 status_buf[KLSI_STATUSBUF_LEN] = { -1,-1};
237 info("%s - sending SIO Poll request", __FUNCTION__);
238 rc = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
239 KL5KUSB105A_SIO_POLL,
240 USB_TYPE_VENDOR | USB_DIR_IN,
243 status_buf, KLSI_STATUSBUF_LEN,
247 err("Reading line status failed (error = %d)", rc);
249 status = status_buf[0] + (status_buf[1]<<8);
251 info("%s - read status %x %x", __FUNCTION__,
252 status_buf[0], status_buf[1]);
254 *line_state_p = klsi_105_status2linestate(status);
262 * Driver's tty interface functions
265 static int klsi_105_startup (struct usb_serial *serial)
267 struct klsi_105_private *priv;
270 /* check if we support the product id (see keyspan.c)
274 /* allocate the private data structure */
275 for (i=0; i<serial->num_ports; i++) {
277 priv = kmalloc(sizeof(struct klsi_105_private),
280 dbg("%skmalloc for klsi_105_private failed.", __FUNCTION__);
283 /* set initial values for control structures */
284 priv->cfg.pktlen = 5;
285 priv->cfg.baudrate = kl5kusb105a_sio_b9600;
286 priv->cfg.databits = kl5kusb105a_dtb_8;
287 priv->cfg.unknown1 = 0;
288 priv->cfg.unknown2 = 1;
290 priv->line_state = 0;
294 usb_set_serial_port_data(serial->port[i], priv);
296 spin_lock_init (&priv->lock);
297 for (j=0; j<NUM_URBS; j++) {
298 struct urb* urb = usb_alloc_urb(0, GFP_KERNEL);
300 priv->write_urb_pool[j] = urb;
302 err("No more urbs???");
306 urb->transfer_buffer = NULL;
307 urb->transfer_buffer = kmalloc (URB_TRANSFER_BUFFER_SIZE,
309 if (!urb->transfer_buffer) {
310 err("%s - out of memory for urb buffers.", __FUNCTION__);
315 /* priv->termios is left uninitalized until port opening */
316 init_waitqueue_head(&serial->port[i]->write_wait);
320 } /* klsi_105_startup */
323 static void klsi_105_shutdown (struct usb_serial *serial)
327 dbg("%s", __FUNCTION__);
329 /* stop reads and writes on all ports */
330 for (i=0; i < serial->num_ports; ++i) {
331 struct klsi_105_private *priv = usb_get_serial_port_data(serial->port[i]);
335 /* kill our write urb pool */
337 struct urb **write_urbs = priv->write_urb_pool;
338 spin_lock_irqsave(&priv->lock,flags);
340 for (j = 0; j < NUM_URBS; j++) {
342 /* FIXME - uncomment the following
343 * usb_unlink_urb call when the host
344 * controllers get fixed to set
345 * urb->dev = NULL after the urb is
346 * finished. Otherwise this call
348 /* usb_unlink_urb(write_urbs[j]); */
349 if (write_urbs[j]->transfer_buffer)
350 kfree(write_urbs[j]->transfer_buffer);
351 usb_free_urb (write_urbs[j]);
355 spin_unlock_irqrestore (&priv->lock, flags);
358 usb_set_serial_port_data(serial->port[i], NULL);
361 } /* klsi_105_shutdown */
363 static int klsi_105_open (struct usb_serial_port *port, struct file *filp)
365 struct usb_serial *serial = port->serial;
366 struct klsi_105_private *priv = usb_get_serial_port_data(port);
370 unsigned long line_state;
371 struct klsi_105_port_settings cfg;
374 dbg("%s port %d", __FUNCTION__, port->number);
376 /* force low_latency on so that our tty_push actually forces
378 * port->tty->low_latency = 1; */
380 /* Do a defined restart:
381 * Set up sane default baud rate and send the 'READ_ON'
383 * FIXME: set modem line control (how?)
384 * Then read the modem line control and store values in
388 cfg.baudrate = kl5kusb105a_sio_b9600;
389 cfg.databits = kl5kusb105a_dtb_8;
392 klsi_105_chg_port_settings(serial, &cfg);
394 /* set up termios structure */
395 spin_lock_irqsave (&priv->lock, flags);
396 priv->termios.c_iflag = port->tty->termios->c_iflag;
397 priv->termios.c_oflag = port->tty->termios->c_oflag;
398 priv->termios.c_cflag = port->tty->termios->c_cflag;
399 priv->termios.c_lflag = port->tty->termios->c_lflag;
400 for (i=0; i<NCCS; i++)
401 priv->termios.c_cc[i] = port->tty->termios->c_cc[i];
402 priv->cfg.pktlen = cfg.pktlen;
403 priv->cfg.baudrate = cfg.baudrate;
404 priv->cfg.databits = cfg.databits;
405 priv->cfg.unknown1 = cfg.unknown1;
406 priv->cfg.unknown2 = cfg.unknown2;
407 spin_unlock_irqrestore (&priv->lock, flags);
409 /* READ_ON and urb submission */
410 usb_fill_bulk_urb(port->read_urb, serial->dev,
411 usb_rcvbulkpipe(serial->dev,
412 port->bulk_in_endpointAddress),
413 port->read_urb->transfer_buffer,
414 port->read_urb->transfer_buffer_length,
415 klsi_105_read_bulk_callback,
418 rc = usb_submit_urb(port->read_urb, GFP_KERNEL);
420 err("%s - failed submitting read urb, error %d", __FUNCTION__, rc);
425 rc = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev,0),
426 KL5KUSB105A_SIO_CONFIGURE,
427 USB_TYPE_VENDOR|USB_DIR_OUT|USB_RECIP_INTERFACE,
428 KL5KUSB105A_SIO_CONFIGURE_READ_ON,
434 err("Enabling read failed (error = %d)", rc);
437 dbg("%s - enabled reading", __FUNCTION__);
439 rc = klsi_105_get_line_state(serial, &line_state);
441 spin_lock_irqsave (&priv->lock, flags);
442 priv->line_state = line_state;
443 spin_unlock_irqrestore (&priv->lock, flags);
444 dbg("%s - read line state 0x%lx", __FUNCTION__, line_state);
451 } /* klsi_105_open */
454 static void klsi_105_close (struct usb_serial_port *port, struct file *filp)
456 struct usb_serial *serial;
457 struct klsi_105_private *priv = usb_get_serial_port_data(port);
460 dbg("%s port %d", __FUNCTION__, port->number);
462 serial = get_usb_serial (port, __FUNCTION__);
468 rc = usb_control_msg (serial->dev,
469 usb_sndctrlpipe(serial->dev, 0),
470 KL5KUSB105A_SIO_CONFIGURE,
471 USB_TYPE_VENDOR | USB_DIR_OUT,
472 KL5KUSB105A_SIO_CONFIGURE_READ_OFF,
477 err("Disabling read failed (error = %d)", rc);
479 /* shutdown our bulk reads and writes */
480 usb_unlink_urb (port->write_urb);
481 usb_unlink_urb (port->read_urb);
482 /* unlink our write pool */
484 /* wgg - do I need this? I think so. */
485 usb_unlink_urb (port->interrupt_in_urb);
486 info("kl5kusb105 port stats: %ld bytes in, %ld bytes out", priv->bytes_in, priv->bytes_out);
487 } /* klsi_105_close */
490 /* We need to write a complete 64-byte data block and encode the
491 * number actually sent in the first double-byte, LSB-order. That
492 * leaves at most 62 bytes of payload.
494 #define KLSI_105_DATA_OFFSET 2 /* in the bulk urb data block */
497 static int klsi_105_write (struct usb_serial_port *port, int from_user,
498 const unsigned char *buf, int count)
500 struct usb_serial *serial = port->serial;
501 struct klsi_105_private *priv = usb_get_serial_port_data(port);
505 dbg("%s - port %d", __FUNCTION__, port->number);
508 /* try to find a free urb (write 0 bytes if none) */
509 struct urb *urb = NULL;
512 /* since the pool is per-port we might not need the spin lock !? */
513 spin_lock_irqsave (&priv->lock, flags);
514 for (i=0; i<NUM_URBS; i++) {
515 if (priv->write_urb_pool[i]->status != -EINPROGRESS) {
516 urb = priv->write_urb_pool[i];
517 dbg("%s - using pool URB %d", __FUNCTION__, i);
521 spin_unlock_irqrestore (&priv->lock, flags);
524 dbg("%s - no more free urbs", __FUNCTION__);
528 if (urb->transfer_buffer == NULL) {
529 urb->transfer_buffer = kmalloc (URB_TRANSFER_BUFFER_SIZE, GFP_ATOMIC);
530 if (urb->transfer_buffer == NULL) {
531 err("%s - no more kernel memory...", __FUNCTION__);
536 size = min (count, port->bulk_out_size - KLSI_105_DATA_OFFSET);
537 size = min (size, URB_TRANSFER_BUFFER_SIZE - KLSI_105_DATA_OFFSET);
540 if (copy_from_user(urb->transfer_buffer
541 + KLSI_105_DATA_OFFSET, buf, size)) {
545 memcpy (urb->transfer_buffer + KLSI_105_DATA_OFFSET,
549 /* write payload size into transfer buffer */
550 ((__u8 *)urb->transfer_buffer)[0] = (__u8) (size & 0xFF);
551 ((__u8 *)urb->transfer_buffer)[1] = (__u8) ((size & 0xFF00)>>8);
554 usb_fill_bulk_urb(urb, serial->dev,
555 usb_sndbulkpipe(serial->dev,
556 port->bulk_out_endpointAddress),
557 urb->transfer_buffer,
558 URB_TRANSFER_BUFFER_SIZE,
559 klsi_105_write_bulk_callback,
562 /* send the data out the bulk port */
563 result = usb_submit_urb(urb, GFP_ATOMIC);
565 err("%s - failed submitting write urb, error %d", __FUNCTION__, result);
573 /* lockless, but it's for debug info only... */
574 priv->bytes_out+=bytes_sent;
576 return bytes_sent; /* that's how much we wrote */
577 } /* klsi_105_write */
579 static void klsi_105_write_bulk_callback ( struct urb *urb, struct pt_regs *regs)
581 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
582 struct usb_serial *serial = port->serial;
584 dbg("%s - port %d", __FUNCTION__, port->number);
587 dbg("%s - bad serial pointer, exiting", __FUNCTION__);
592 dbg("%s - nonzero write bulk status received: %d", __FUNCTION__,
597 /* from generic_write_bulk_callback */
598 schedule_work(&port->work);
599 } /* klsi_105_write_bulk_completion_callback */
602 /* return number of characters currently in the writing process */
603 static int klsi_105_chars_in_buffer (struct usb_serial_port *port)
608 struct klsi_105_private *priv = usb_get_serial_port_data(port);
610 spin_lock_irqsave (&priv->lock, flags);
612 for (i = 0; i < NUM_URBS; ++i) {
613 if (priv->write_urb_pool[i]->status == -EINPROGRESS) {
614 chars += URB_TRANSFER_BUFFER_SIZE;
618 spin_unlock_irqrestore (&priv->lock, flags);
620 dbg("%s - returns %d", __FUNCTION__, chars);
624 static int klsi_105_write_room (struct usb_serial_port *port)
629 struct klsi_105_private *priv = usb_get_serial_port_data(port);
631 spin_lock_irqsave (&priv->lock, flags);
632 for (i = 0; i < NUM_URBS; ++i) {
633 if (priv->write_urb_pool[i]->status != -EINPROGRESS) {
634 room += URB_TRANSFER_BUFFER_SIZE;
638 spin_unlock_irqrestore (&priv->lock, flags);
640 dbg("%s - returns %d", __FUNCTION__, room);
646 static void klsi_105_read_bulk_callback (struct urb *urb, struct pt_regs *regs)
648 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
649 struct usb_serial *serial = port->serial;
650 struct klsi_105_private *priv = usb_get_serial_port_data(port);
651 struct tty_struct *tty;
652 unsigned char *data = urb->transfer_buffer;
655 dbg("%s - port %d", __FUNCTION__, port->number);
657 /* The urb might have been killed. */
659 dbg("%s - nonzero read bulk status received: %d", __FUNCTION__,
664 dbg("%s - bad serial pointer, exiting", __FUNCTION__);
668 /* The data received is again preceded by a length double-byte in LSB-
669 * first order (see klsi_105_write() )
671 if (urb->actual_length == 0) {
672 /* empty urbs seem to happen, we ignore them */
673 /* dbg("%s - emtpy URB", __FUNCTION__); */
675 } else if (urb->actual_length <= 2) {
676 dbg("%s - size %d URB not understood", __FUNCTION__,
678 usb_serial_debug_data (__FILE__, __FUNCTION__, urb->actual_length, data);
681 int bytes_sent = ((__u8 *) data)[0] +
682 ((unsigned int) ((__u8 *) data)[1] << 8);
684 /* we should immediately resubmit the URB, before attempting
685 * to pass the data on to the tty layer. But that needs locking
686 * against re-entry an then mixed-up data because of
687 * intermixed tty_flip_buffer_push()s
690 usb_serial_debug_data (__FILE__, __FUNCTION__,
691 urb->actual_length, data);
693 if (bytes_sent + 2 > urb->actual_length) {
694 dbg("%s - trying to read more data than available"
695 " (%d vs. %d)", __FUNCTION__,
696 bytes_sent+2, urb->actual_length);
697 /* cap at implied limit */
698 bytes_sent = urb->actual_length - 2;
701 for (i = 2; i < 2+bytes_sent; i++) {
702 /* if we insert more than TTY_FLIPBUF_SIZE characters,
704 if(tty->flip.count >= TTY_FLIPBUF_SIZE) {
705 tty_flip_buffer_push(tty);
707 /* this doesn't actually push the data through unless
708 * tty->low_latency is set */
709 tty_insert_flip_char(tty, ((__u8*) data)[i], 0);
711 tty_flip_buffer_push(tty);
713 /* again lockless, but debug info only */
714 priv->bytes_in += bytes_sent;
716 /* Continue trying to always read */
717 usb_fill_bulk_urb(port->read_urb, serial->dev,
718 usb_rcvbulkpipe(serial->dev,
719 port->bulk_in_endpointAddress),
720 port->read_urb->transfer_buffer,
721 port->read_urb->transfer_buffer_length,
722 klsi_105_read_bulk_callback,
724 rc = usb_submit_urb(port->read_urb, GFP_ATOMIC);
726 err("%s - failed resubmitting read urb, error %d", __FUNCTION__, rc);
727 } /* klsi_105_read_bulk_callback */
730 static void klsi_105_set_termios (struct usb_serial_port *port,
731 struct termios *old_termios)
733 struct usb_serial *serial = port->serial;
734 struct klsi_105_private *priv = usb_get_serial_port_data(port);
735 unsigned int iflag = port->tty->termios->c_iflag;
736 unsigned int old_iflag = old_termios->c_iflag;
737 unsigned int cflag = port->tty->termios->c_cflag;
738 unsigned int old_cflag = old_termios->c_cflag;
739 struct klsi_105_port_settings cfg;
742 /* lock while we are modifying the settings */
743 spin_lock_irqsave (&priv->lock, flags);
748 if( (cflag & CBAUD) != (old_cflag & CBAUD) ) {
749 /* reassert DTR and (maybe) RTS on transition from B0 */
750 if( (old_cflag & CBAUD) == B0 ) {
751 dbg("%s: baud was B0", __FUNCTION__);
753 priv->control_state |= TIOCM_DTR;
754 /* don't set RTS if using hardware flow control */
755 if (!(old_cflag & CRTSCTS)) {
756 priv->control_state |= TIOCM_RTS;
758 mct_u232_set_modem_ctrl(serial, priv->control_state);
762 switch(cflag & CBAUD) {
763 case B0: /* handled below */
765 case B1200: priv->cfg.baudrate = kl5kusb105a_sio_b1200;
767 case B2400: priv->cfg.baudrate = kl5kusb105a_sio_b2400;
769 case B4800: priv->cfg.baudrate = kl5kusb105a_sio_b4800;
771 case B9600: priv->cfg.baudrate = kl5kusb105a_sio_b9600;
773 case B19200: priv->cfg.baudrate = kl5kusb105a_sio_b19200;
775 case B38400: priv->cfg.baudrate = kl5kusb105a_sio_b38400;
777 case B57600: priv->cfg.baudrate = kl5kusb105a_sio_b57600;
779 case B115200: priv->cfg.baudrate = kl5kusb105a_sio_b115200;
782 err("KLSI USB->Serial converter:"
783 " unsupported baudrate request, using default"
785 priv->cfg.baudrate = kl5kusb105a_sio_b9600;
788 if ((cflag & CBAUD) == B0 ) {
789 dbg("%s: baud is B0", __FUNCTION__);
790 /* Drop RTS and DTR */
791 /* maybe this should be simulated by sending read
792 * disable and read enable messages?
796 priv->control_state &= ~(TIOCM_DTR | TIOCM_RTS);
797 mct_u232_set_modem_ctrl(serial, priv->control_state);
802 if ((cflag & CSIZE) != (old_cflag & CSIZE)) {
803 /* set the number of data bits */
804 switch (cflag & CSIZE) {
806 dbg("%s - 5 bits/byte not supported", __FUNCTION__);
809 dbg("%s - 6 bits/byte not supported", __FUNCTION__);
812 priv->cfg.databits = kl5kusb105a_dtb_7;
815 priv->cfg.databits = kl5kusb105a_dtb_8;
818 err("CSIZE was not CS5-CS8, using default of 8");
819 priv->cfg.databits = kl5kusb105a_dtb_8;
825 * Update line control register (LCR)
827 if ((cflag & (PARENB|PARODD)) != (old_cflag & (PARENB|PARODD))
828 || (cflag & CSTOPB) != (old_cflag & CSTOPB) ) {
835 priv->last_lcr |= (cflag & PARODD) ?
836 MCT_U232_PARITY_ODD : MCT_U232_PARITY_EVEN;
838 priv->last_lcr |= MCT_U232_PARITY_NONE;
840 /* set the number of stop bits */
841 priv->last_lcr |= (cflag & CSTOPB) ?
842 MCT_U232_STOP_BITS_2 : MCT_U232_STOP_BITS_1;
844 mct_u232_set_line_ctrl(serial, priv->last_lcr);
850 * Set flow control: well, I do not really now how to handle DTR/RTS.
851 * Just do what we have seen with SniffUSB on Win98.
853 if( (iflag & IXOFF) != (old_iflag & IXOFF)
854 || (iflag & IXON) != (old_iflag & IXON)
855 || (cflag & CRTSCTS) != (old_cflag & CRTSCTS) ) {
857 /* Drop DTR/RTS if no flow control otherwise assert */
859 if ((iflag & IXOFF) || (iflag & IXON) || (cflag & CRTSCTS) )
860 priv->control_state |= TIOCM_DTR | TIOCM_RTS;
862 priv->control_state &= ~(TIOCM_DTR | TIOCM_RTS);
863 mct_u232_set_modem_ctrl(serial, priv->control_state);
867 memcpy (&cfg, &priv->cfg, sizeof(cfg));
868 spin_unlock_irqrestore (&priv->lock, flags);
870 /* now commit changes to device */
871 klsi_105_chg_port_settings(serial, &cfg);
872 } /* klsi_105_set_termios */
876 static void mct_u232_break_ctl( struct usb_serial_port *port, int break_state )
878 struct usb_serial *serial = port->serial;
879 struct mct_u232_private *priv = (struct mct_u232_private *)port->private;
880 unsigned char lcr = priv->last_lcr;
882 dbg("%sstate=%d", __FUNCTION__, break_state);
885 lcr |= MCT_U232_SET_BREAK;
887 mct_u232_set_line_ctrl(serial, lcr);
888 } /* mct_u232_break_ctl */
891 static int klsi_105_tiocmget (struct usb_serial_port *port, struct file *file)
893 struct usb_serial *serial = port->serial;
894 struct klsi_105_private *priv = usb_get_serial_port_data(port);
897 unsigned long line_state;
898 dbg("%s - request, just guessing", __FUNCTION__);
900 rc = klsi_105_get_line_state(serial, &line_state);
902 err("Reading line control failed (error = %d)", rc);
903 /* better return value? EAGAIN? */
907 spin_lock_irqsave (&priv->lock, flags);
908 priv->line_state = line_state;
909 spin_unlock_irqrestore (&priv->lock, flags);
910 dbg("%s - read line state 0x%lx", __FUNCTION__, line_state);
911 return (int)line_state;
914 static int klsi_105_tiocmset (struct usb_serial_port *port, struct file *file,
915 unsigned int set, unsigned int clear)
917 int retval = -EINVAL;
919 dbg("%s", __FUNCTION__);
921 /* if this ever gets implemented, it should be done something like this:
922 struct usb_serial *serial = port->serial;
923 struct klsi_105_private *priv = usb_get_serial_port_data(port);
927 spin_lock_irqsave (&priv->lock, flags);
929 priv->control_state |= TIOCM_RTS;
931 priv->control_state |= TIOCM_DTR;
932 if (clear & TIOCM_RTS)
933 priv->control_state &= ~TIOCM_RTS;
934 if (clear & TIOCM_DTR)
935 priv->control_state &= ~TIOCM_DTR;
936 control = priv->control_state;
937 spin_unlock_irqrestore (&priv->lock, flags);
938 retval = mct_u232_set_modem_ctrl(serial, control);
943 static int klsi_105_ioctl (struct usb_serial_port *port, struct file * file,
944 unsigned int cmd, unsigned long arg)
946 struct klsi_105_private *priv = usb_get_serial_port_data(port);
948 dbg("%scmd=0x%x", __FUNCTION__, cmd);
950 /* Based on code from acm.c and others */
953 /* wait for any of the 4 modem inputs (DCD,RI,DSR,CTS)*/
955 dbg("%s - TIOCMIWAIT not handled", __FUNCTION__);
959 /* return count of modemline transitions */
961 dbg("%s - TIOCGICOUNT not handled", __FUNCTION__);
964 /* return current info to caller */
967 dbg("%s - TCGETS data faked/incomplete", __FUNCTION__);
969 retval = verify_area(VERIFY_WRITE, (void *)arg,
970 sizeof(struct termios));
975 if (kernel_termios_to_user_termios((struct termios *)arg,
981 /* set port termios to the one given by the user */
984 dbg("%s - TCSETS not handled", __FUNCTION__);
986 retval = verify_area(VERIFY_READ, (void *)arg,
987 sizeof(struct termios));
992 if (user_termios_to_kernel_termios(&priv->termios,
993 (struct termios *)arg))
995 klsi_105_set_termios(port, &priv->termios);
999 /* set port termios and try to wait for completion of last
1000 * write operation */
1001 /* We guess here. If there are not too many write urbs
1002 * outstanding, we lie. */
1003 /* what is the right way to wait here? schedule() ? */
1005 while (klsi_105_chars_in_buffer(port) > (NUM_URBS / 4 ) * URB_TRANSFER_BUFFER_SIZE)
1008 return -ENOIOCTLCMD;
1011 dbg("%s: arg not supported - 0x%04x", __FUNCTION__,cmd);
1012 return(-ENOIOCTLCMD);
1016 } /* klsi_105_ioctl */
1018 static void klsi_105_throttle (struct usb_serial_port *port)
1020 dbg("%s - port %d", __FUNCTION__, port->number);
1021 usb_unlink_urb (port->read_urb);
1024 static void klsi_105_unthrottle (struct usb_serial_port *port)
1028 dbg("%s - port %d", __FUNCTION__, port->number);
1030 port->read_urb->dev = port->serial->dev;
1031 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1033 err("%s - failed submitting read urb, error %d", __FUNCTION__,
1039 static int __init klsi_105_init (void)
1042 retval = usb_serial_register(&kl5kusb105d_device);
1044 goto failed_usb_serial_register;
1045 retval = usb_register(&kl5kusb105d_driver);
1047 goto failed_usb_register;
1049 info(DRIVER_DESC " " DRIVER_VERSION);
1051 failed_usb_register:
1052 usb_serial_deregister(&kl5kusb105d_device);
1053 failed_usb_serial_register:
1058 static void __exit klsi_105_exit (void)
1060 usb_deregister (&kl5kusb105d_driver);
1061 usb_serial_deregister (&kl5kusb105d_device);
1065 module_init (klsi_105_init);
1066 module_exit (klsi_105_exit);
1068 MODULE_AUTHOR( DRIVER_AUTHOR );
1069 MODULE_DESCRIPTION( DRIVER_DESC );
1070 MODULE_LICENSE("GPL");
1073 MODULE_PARM(debug, "i");
1074 MODULE_PARM_DESC(debug, "enable extensive debugging messages");
1076 MODULE_PARM(num_urbs, "i");
1077 MODULE_PARM_DESC(num_urbs, "number of URBs to use in write pool");
1080 /* vim: set sts=8 ts=8 sw=8: */