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>
59 #include "usb-serial.h"
60 #include "kl5kusb105.h"
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"
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
,
79 static void klsi_105_close (struct usb_serial_port
*port
,
81 static int klsi_105_write (struct usb_serial_port
*port
,
82 const unsigned char *buf
,
84 static void klsi_105_write_bulk_callback (struct urb
*urb
, struct pt_regs
*regs
);
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
, struct pt_regs
*regs
);
89 static void klsi_105_set_termios (struct usb_serial_port
*port
,
90 struct termios
* old
);
91 static int klsi_105_ioctl (struct usb_serial_port
*port
,
95 static void klsi_105_throttle (struct usb_serial_port
*port
);
96 static void klsi_105_unthrottle (struct usb_serial_port
*port
);
98 static void klsi_105_break_ctl (struct usb_serial_port *port,
101 static int klsi_105_tiocmget (struct usb_serial_port
*port
,
103 static int klsi_105_tiocmset (struct usb_serial_port
*port
,
104 struct file
*file
, unsigned int set
,
108 * All of the device info needed for the KLSI converters.
110 static struct usb_device_id id_table
[] = {
111 { USB_DEVICE(PALMCONNECT_VID
, PALMCONNECT_PID
) },
112 { USB_DEVICE(KLSI_VID
, KLSI_KL5KUSB105D_PID
) },
113 { } /* Terminating entry */
116 MODULE_DEVICE_TABLE (usb
, id_table
);
118 static struct usb_driver kl5kusb105d_driver
= {
119 .owner
= THIS_MODULE
,
120 .name
= "kl5kusb105d",
121 .probe
= usb_serial_probe
,
122 .disconnect
= usb_serial_disconnect
,
123 .id_table
= id_table
,
126 static struct usb_serial_driver kl5kusb105d_device
= {
128 .owner
= THIS_MODULE
,
129 .name
= "kl5kusb105d",
131 .description
= "KL5KUSB105D / PalmConnect",
132 .id_table
= id_table
,
133 .num_interrupt_in
= 1,
137 .open
= klsi_105_open
,
138 .close
= klsi_105_close
,
139 .write
= klsi_105_write
,
140 .write_bulk_callback
= klsi_105_write_bulk_callback
,
141 .chars_in_buffer
= klsi_105_chars_in_buffer
,
142 .write_room
= klsi_105_write_room
,
143 .read_bulk_callback
=klsi_105_read_bulk_callback
,
144 .ioctl
= klsi_105_ioctl
,
145 .set_termios
= klsi_105_set_termios
,
146 /*.break_ctl = klsi_105_break_ctl,*/
147 .tiocmget
= klsi_105_tiocmget
,
148 .tiocmset
= klsi_105_tiocmset
,
149 .attach
= klsi_105_startup
,
150 .shutdown
= klsi_105_shutdown
,
151 .throttle
= klsi_105_throttle
,
152 .unthrottle
= klsi_105_unthrottle
,
155 struct klsi_105_port_settings
{
156 __u8 pktlen
; /* always 5, it seems */
161 } __attribute__ ((packed
));
163 /* we implement a pool of NUM_URBS urbs per usb_serial */
165 #define URB_TRANSFER_BUFFER_SIZE 64
166 struct klsi_105_private
{
167 struct klsi_105_port_settings cfg
;
168 struct termios termios
;
169 unsigned long line_state
; /* modem line settings */
171 struct urb
* write_urb_pool
[NUM_URBS
];
173 unsigned long bytes_in
;
174 unsigned long bytes_out
;
179 * Handle vendor specific USB requests
183 #define KLSI_TIMEOUT 5000 /* default urb timeout */
185 static int klsi_105_chg_port_settings(struct usb_serial_port
*port
,
186 struct klsi_105_port_settings
*settings
)
190 rc
= usb_control_msg(port
->serial
->dev
,
191 usb_sndctrlpipe(port
->serial
->dev
, 0),
192 KL5KUSB105A_SIO_SET_DATA
,
193 USB_TYPE_VENDOR
| USB_DIR_OUT
| USB_RECIP_INTERFACE
,
197 sizeof(struct klsi_105_port_settings
),
200 err("Change port settings failed (error = %d)", rc
);
201 info("%s - %d byte block, baudrate %x, databits %d, u1 %d, u2 %d",
204 settings
->baudrate
, settings
->databits
,
205 settings
->unknown1
, settings
->unknown2
);
207 } /* klsi_105_chg_port_settings */
209 /* translate a 16-bit status value from the device to linux's TIO bits */
210 static unsigned long klsi_105_status2linestate(const __u16 status
)
212 unsigned long res
= 0;
214 res
= ((status
& KL5KUSB105A_DSR
) ? TIOCM_DSR
: 0)
215 | ((status
& KL5KUSB105A_CTS
) ? TIOCM_CTS
: 0)
221 * Read line control via vendor command and return result through
224 /* It seems that the status buffer has always only 2 bytes length */
225 #define KLSI_STATUSBUF_LEN 2
226 static int klsi_105_get_line_state(struct usb_serial_port
*port
,
227 unsigned long *line_state_p
)
230 __u8 status_buf
[KLSI_STATUSBUF_LEN
] = { -1,-1};
233 info("%s - sending SIO Poll request", __FUNCTION__
);
234 rc
= usb_control_msg(port
->serial
->dev
,
235 usb_rcvctrlpipe(port
->serial
->dev
, 0),
236 KL5KUSB105A_SIO_POLL
,
237 USB_TYPE_VENDOR
| USB_DIR_IN
,
240 status_buf
, KLSI_STATUSBUF_LEN
,
244 err("Reading line status failed (error = %d)", rc
);
246 status
= status_buf
[0] + (status_buf
[1]<<8);
248 info("%s - read status %x %x", __FUNCTION__
,
249 status_buf
[0], status_buf
[1]);
251 *line_state_p
= klsi_105_status2linestate(status
);
259 * Driver's tty interface functions
262 static int klsi_105_startup (struct usb_serial
*serial
)
264 struct klsi_105_private
*priv
;
267 /* check if we support the product id (see keyspan.c)
271 /* allocate the private data structure */
272 for (i
=0; i
<serial
->num_ports
; i
++) {
274 priv
= kmalloc(sizeof(struct klsi_105_private
),
277 dbg("%skmalloc for klsi_105_private failed.", __FUNCTION__
);
280 /* set initial values for control structures */
281 priv
->cfg
.pktlen
= 5;
282 priv
->cfg
.baudrate
= kl5kusb105a_sio_b9600
;
283 priv
->cfg
.databits
= kl5kusb105a_dtb_8
;
284 priv
->cfg
.unknown1
= 0;
285 priv
->cfg
.unknown2
= 1;
287 priv
->line_state
= 0;
291 usb_set_serial_port_data(serial
->port
[i
], priv
);
293 spin_lock_init (&priv
->lock
);
294 for (j
=0; j
<NUM_URBS
; j
++) {
295 struct urb
* urb
= usb_alloc_urb(0, GFP_KERNEL
);
297 priv
->write_urb_pool
[j
] = urb
;
299 err("No more urbs???");
303 urb
->transfer_buffer
= NULL
;
304 urb
->transfer_buffer
= kmalloc (URB_TRANSFER_BUFFER_SIZE
,
306 if (!urb
->transfer_buffer
) {
307 err("%s - out of memory for urb buffers.", __FUNCTION__
);
312 /* priv->termios is left uninitalized until port opening */
313 init_waitqueue_head(&serial
->port
[i
]->write_wait
);
317 } /* klsi_105_startup */
320 static void klsi_105_shutdown (struct usb_serial
*serial
)
324 dbg("%s", __FUNCTION__
);
326 /* stop reads and writes on all ports */
327 for (i
=0; i
< serial
->num_ports
; ++i
) {
328 struct klsi_105_private
*priv
= usb_get_serial_port_data(serial
->port
[i
]);
332 /* kill our write urb pool */
334 struct urb
**write_urbs
= priv
->write_urb_pool
;
335 spin_lock_irqsave(&priv
->lock
,flags
);
337 for (j
= 0; j
< NUM_URBS
; j
++) {
339 /* FIXME - uncomment the following
340 * usb_kill_urb call when the host
341 * controllers get fixed to set
342 * urb->dev = NULL after the urb is
343 * finished. Otherwise this call
345 /* usb_kill_urb(write_urbs[j]); */
346 kfree(write_urbs
[j
]->transfer_buffer
);
347 usb_free_urb (write_urbs
[j
]);
351 spin_unlock_irqrestore (&priv
->lock
, flags
);
354 usb_set_serial_port_data(serial
->port
[i
], NULL
);
357 } /* klsi_105_shutdown */
359 static int klsi_105_open (struct usb_serial_port
*port
, struct file
*filp
)
361 struct klsi_105_private
*priv
= usb_get_serial_port_data(port
);
365 unsigned long line_state
;
366 struct klsi_105_port_settings cfg
;
369 dbg("%s port %d", __FUNCTION__
, port
->number
);
371 /* force low_latency on so that our tty_push actually forces
373 * port->tty->low_latency = 1; */
375 /* Do a defined restart:
376 * Set up sane default baud rate and send the 'READ_ON'
378 * FIXME: set modem line control (how?)
379 * Then read the modem line control and store values in
383 cfg
.baudrate
= kl5kusb105a_sio_b9600
;
384 cfg
.databits
= kl5kusb105a_dtb_8
;
387 klsi_105_chg_port_settings(port
, &cfg
);
389 /* set up termios structure */
390 spin_lock_irqsave (&priv
->lock
, flags
);
391 priv
->termios
.c_iflag
= port
->tty
->termios
->c_iflag
;
392 priv
->termios
.c_oflag
= port
->tty
->termios
->c_oflag
;
393 priv
->termios
.c_cflag
= port
->tty
->termios
->c_cflag
;
394 priv
->termios
.c_lflag
= port
->tty
->termios
->c_lflag
;
395 for (i
=0; i
<NCCS
; i
++)
396 priv
->termios
.c_cc
[i
] = port
->tty
->termios
->c_cc
[i
];
397 priv
->cfg
.pktlen
= cfg
.pktlen
;
398 priv
->cfg
.baudrate
= cfg
.baudrate
;
399 priv
->cfg
.databits
= cfg
.databits
;
400 priv
->cfg
.unknown1
= cfg
.unknown1
;
401 priv
->cfg
.unknown2
= cfg
.unknown2
;
402 spin_unlock_irqrestore (&priv
->lock
, flags
);
404 /* READ_ON and urb submission */
405 usb_fill_bulk_urb(port
->read_urb
, port
->serial
->dev
,
406 usb_rcvbulkpipe(port
->serial
->dev
,
407 port
->bulk_in_endpointAddress
),
408 port
->read_urb
->transfer_buffer
,
409 port
->read_urb
->transfer_buffer_length
,
410 klsi_105_read_bulk_callback
,
413 rc
= usb_submit_urb(port
->read_urb
, GFP_KERNEL
);
415 err("%s - failed submitting read urb, error %d", __FUNCTION__
, rc
);
420 rc
= usb_control_msg(port
->serial
->dev
,
421 usb_sndctrlpipe(port
->serial
->dev
,0),
422 KL5KUSB105A_SIO_CONFIGURE
,
423 USB_TYPE_VENDOR
|USB_DIR_OUT
|USB_RECIP_INTERFACE
,
424 KL5KUSB105A_SIO_CONFIGURE_READ_ON
,
430 err("Enabling read failed (error = %d)", rc
);
433 dbg("%s - enabled reading", __FUNCTION__
);
435 rc
= klsi_105_get_line_state(port
, &line_state
);
437 spin_lock_irqsave (&priv
->lock
, flags
);
438 priv
->line_state
= line_state
;
439 spin_unlock_irqrestore (&priv
->lock
, flags
);
440 dbg("%s - read line state 0x%lx", __FUNCTION__
, line_state
);
447 } /* klsi_105_open */
450 static void klsi_105_close (struct usb_serial_port
*port
, struct file
*filp
)
452 struct klsi_105_private
*priv
= usb_get_serial_port_data(port
);
455 dbg("%s port %d", __FUNCTION__
, port
->number
);
458 rc
= usb_control_msg (port
->serial
->dev
,
459 usb_sndctrlpipe(port
->serial
->dev
, 0),
460 KL5KUSB105A_SIO_CONFIGURE
,
461 USB_TYPE_VENDOR
| USB_DIR_OUT
,
462 KL5KUSB105A_SIO_CONFIGURE_READ_OFF
,
467 err("Disabling read failed (error = %d)", rc
);
469 /* shutdown our bulk reads and writes */
470 usb_kill_urb(port
->write_urb
);
471 usb_kill_urb(port
->read_urb
);
472 /* unlink our write pool */
474 /* wgg - do I need this? I think so. */
475 usb_kill_urb(port
->interrupt_in_urb
);
476 info("kl5kusb105 port stats: %ld bytes in, %ld bytes out", priv
->bytes_in
, priv
->bytes_out
);
477 } /* klsi_105_close */
480 /* We need to write a complete 64-byte data block and encode the
481 * number actually sent in the first double-byte, LSB-order. That
482 * leaves at most 62 bytes of payload.
484 #define KLSI_105_DATA_OFFSET 2 /* in the bulk urb data block */
487 static int klsi_105_write (struct usb_serial_port
*port
,
488 const unsigned char *buf
, int count
)
490 struct klsi_105_private
*priv
= usb_get_serial_port_data(port
);
494 dbg("%s - port %d", __FUNCTION__
, port
->number
);
497 /* try to find a free urb (write 0 bytes if none) */
498 struct urb
*urb
= NULL
;
501 /* since the pool is per-port we might not need the spin lock !? */
502 spin_lock_irqsave (&priv
->lock
, flags
);
503 for (i
=0; i
<NUM_URBS
; i
++) {
504 if (priv
->write_urb_pool
[i
]->status
!= -EINPROGRESS
) {
505 urb
= priv
->write_urb_pool
[i
];
506 dbg("%s - using pool URB %d", __FUNCTION__
, i
);
510 spin_unlock_irqrestore (&priv
->lock
, flags
);
513 dbg("%s - no more free urbs", __FUNCTION__
);
517 if (urb
->transfer_buffer
== NULL
) {
518 urb
->transfer_buffer
= kmalloc (URB_TRANSFER_BUFFER_SIZE
, GFP_ATOMIC
);
519 if (urb
->transfer_buffer
== NULL
) {
520 err("%s - no more kernel memory...", __FUNCTION__
);
525 size
= min (count
, port
->bulk_out_size
- KLSI_105_DATA_OFFSET
);
526 size
= min (size
, URB_TRANSFER_BUFFER_SIZE
- KLSI_105_DATA_OFFSET
);
528 memcpy (urb
->transfer_buffer
+ KLSI_105_DATA_OFFSET
, buf
, size
);
530 /* write payload size into transfer buffer */
531 ((__u8
*)urb
->transfer_buffer
)[0] = (__u8
) (size
& 0xFF);
532 ((__u8
*)urb
->transfer_buffer
)[1] = (__u8
) ((size
& 0xFF00)>>8);
535 usb_fill_bulk_urb(urb
, port
->serial
->dev
,
536 usb_sndbulkpipe(port
->serial
->dev
,
537 port
->bulk_out_endpointAddress
),
538 urb
->transfer_buffer
,
539 URB_TRANSFER_BUFFER_SIZE
,
540 klsi_105_write_bulk_callback
,
543 /* send the data out the bulk port */
544 result
= usb_submit_urb(urb
, GFP_ATOMIC
);
546 err("%s - failed submitting write urb, error %d", __FUNCTION__
, result
);
554 /* lockless, but it's for debug info only... */
555 priv
->bytes_out
+=bytes_sent
;
557 return bytes_sent
; /* that's how much we wrote */
558 } /* klsi_105_write */
560 static void klsi_105_write_bulk_callback ( struct urb
*urb
, struct pt_regs
*regs
)
562 struct usb_serial_port
*port
= (struct usb_serial_port
*)urb
->context
;
564 dbg("%s - port %d", __FUNCTION__
, port
->number
);
567 dbg("%s - nonzero write bulk status received: %d", __FUNCTION__
,
572 /* from generic_write_bulk_callback */
573 schedule_work(&port
->work
);
574 } /* klsi_105_write_bulk_completion_callback */
577 /* return number of characters currently in the writing process */
578 static int klsi_105_chars_in_buffer (struct usb_serial_port
*port
)
583 struct klsi_105_private
*priv
= usb_get_serial_port_data(port
);
585 spin_lock_irqsave (&priv
->lock
, flags
);
587 for (i
= 0; i
< NUM_URBS
; ++i
) {
588 if (priv
->write_urb_pool
[i
]->status
== -EINPROGRESS
) {
589 chars
+= URB_TRANSFER_BUFFER_SIZE
;
593 spin_unlock_irqrestore (&priv
->lock
, flags
);
595 dbg("%s - returns %d", __FUNCTION__
, chars
);
599 static int klsi_105_write_room (struct usb_serial_port
*port
)
604 struct klsi_105_private
*priv
= usb_get_serial_port_data(port
);
606 spin_lock_irqsave (&priv
->lock
, flags
);
607 for (i
= 0; i
< NUM_URBS
; ++i
) {
608 if (priv
->write_urb_pool
[i
]->status
!= -EINPROGRESS
) {
609 room
+= URB_TRANSFER_BUFFER_SIZE
;
613 spin_unlock_irqrestore (&priv
->lock
, flags
);
615 dbg("%s - returns %d", __FUNCTION__
, room
);
621 static void klsi_105_read_bulk_callback (struct urb
*urb
, struct pt_regs
*regs
)
623 struct usb_serial_port
*port
= (struct usb_serial_port
*)urb
->context
;
624 struct klsi_105_private
*priv
= usb_get_serial_port_data(port
);
625 struct tty_struct
*tty
;
626 unsigned char *data
= urb
->transfer_buffer
;
629 dbg("%s - port %d", __FUNCTION__
, port
->number
);
631 /* The urb might have been killed. */
633 dbg("%s - nonzero read bulk status received: %d", __FUNCTION__
,
638 /* The data received is again preceded by a length double-byte in LSB-
639 * first order (see klsi_105_write() )
641 if (urb
->actual_length
== 0) {
642 /* empty urbs seem to happen, we ignore them */
643 /* dbg("%s - emtpy URB", __FUNCTION__); */
645 } else if (urb
->actual_length
<= 2) {
646 dbg("%s - size %d URB not understood", __FUNCTION__
,
648 usb_serial_debug_data(debug
, &port
->dev
, __FUNCTION__
,
649 urb
->actual_length
, data
);
652 int bytes_sent
= ((__u8
*) data
)[0] +
653 ((unsigned int) ((__u8
*) data
)[1] << 8);
655 /* we should immediately resubmit the URB, before attempting
656 * to pass the data on to the tty layer. But that needs locking
657 * against re-entry an then mixed-up data because of
658 * intermixed tty_flip_buffer_push()s
661 usb_serial_debug_data(debug
, &port
->dev
, __FUNCTION__
,
662 urb
->actual_length
, data
);
664 if (bytes_sent
+ 2 > urb
->actual_length
) {
665 dbg("%s - trying to read more data than available"
666 " (%d vs. %d)", __FUNCTION__
,
667 bytes_sent
+2, urb
->actual_length
);
668 /* cap at implied limit */
669 bytes_sent
= urb
->actual_length
- 2;
672 for (i
= 2; i
< 2+bytes_sent
; i
++) {
673 /* if we insert more than TTY_FLIPBUF_SIZE characters,
675 if(tty
->flip
.count
>= TTY_FLIPBUF_SIZE
) {
676 tty_flip_buffer_push(tty
);
678 /* this doesn't actually push the data through unless
679 * tty->low_latency is set */
680 tty_insert_flip_char(tty
, ((__u8
*) data
)[i
], 0);
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
,
695 rc
= usb_submit_urb(port
->read_urb
, GFP_ATOMIC
);
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 termios
*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
;
712 /* lock while we are modifying the settings */
713 spin_lock_irqsave (&priv
->lock
, flags
);
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__
);
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
);
732 switch(cflag
& CBAUD
) {
733 case B0
: /* handled below */
735 case B1200
: priv
->cfg
.baudrate
= kl5kusb105a_sio_b1200
;
737 case B2400
: priv
->cfg
.baudrate
= kl5kusb105a_sio_b2400
;
739 case B4800
: priv
->cfg
.baudrate
= kl5kusb105a_sio_b4800
;
741 case B9600
: priv
->cfg
.baudrate
= kl5kusb105a_sio_b9600
;
743 case B19200
: priv
->cfg
.baudrate
= kl5kusb105a_sio_b19200
;
745 case B38400
: priv
->cfg
.baudrate
= kl5kusb105a_sio_b38400
;
747 case B57600
: priv
->cfg
.baudrate
= kl5kusb105a_sio_b57600
;
749 case B115200
: priv
->cfg
.baudrate
= kl5kusb105a_sio_b115200
;
752 err("KLSI USB->Serial converter:"
753 " unsupported baudrate request, using default"
755 priv
->cfg
.baudrate
= kl5kusb105a_sio_b9600
;
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?
766 priv
->control_state
&= ~(TIOCM_DTR
| TIOCM_RTS
);
767 mct_u232_set_modem_ctrl(serial
, priv
->control_state
);
772 if ((cflag
& CSIZE
) != (old_cflag
& CSIZE
)) {
773 /* set the number of data bits */
774 switch (cflag
& CSIZE
) {
776 dbg("%s - 5 bits/byte not supported", __FUNCTION__
);
777 spin_unlock_irqrestore (&priv
->lock
, flags
);
780 dbg("%s - 6 bits/byte not supported", __FUNCTION__
);
781 spin_unlock_irqrestore (&priv
->lock
, flags
);
784 priv
->cfg
.databits
= kl5kusb105a_dtb_7
;
787 priv
->cfg
.databits
= kl5kusb105a_dtb_8
;
790 err("CSIZE was not CS5-CS8, using default of 8");
791 priv
->cfg
.databits
= kl5kusb105a_dtb_8
;
797 * Update line control register (LCR)
799 if ((cflag
& (PARENB
|PARODD
)) != (old_cflag
& (PARENB
|PARODD
))
800 || (cflag
& CSTOPB
) != (old_cflag
& CSTOPB
) ) {
807 priv
->last_lcr
|= (cflag
& PARODD
) ?
808 MCT_U232_PARITY_ODD
: MCT_U232_PARITY_EVEN
;
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
);
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 */
831 if ((iflag
& IXOFF
) || (iflag
& IXON
) || (cflag
& CRTSCTS
) )
832 priv
->control_state
|= TIOCM_DTR
| TIOCM_RTS
;
834 priv
->control_state
&= ~(TIOCM_DTR
| TIOCM_RTS
);
835 mct_u232_set_modem_ctrl(serial
, priv
->control_state
);
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 */
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
);
857 lcr
|= MCT_U232_SET_BREAK
;
859 mct_u232_set_line_ctrl(serial
, lcr
);
860 } /* mct_u232_break_ctl */
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
);
868 unsigned long line_state
;
869 dbg("%s - request, just guessing", __FUNCTION__
);
871 rc
= klsi_105_get_line_state(port
, &line_state
);
873 err("Reading line control failed (error = %d)", rc
);
874 /* better return value? EAGAIN? */
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);
898 spin_lock_irqsave (&priv->lock, flags);
900 priv->control_state |= TIOCM_RTS;
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);
914 static int klsi_105_ioctl (struct usb_serial_port
*port
, struct file
* file
,
915 unsigned int cmd
, unsigned long arg
)
917 struct klsi_105_private
*priv
= usb_get_serial_port_data(port
);
918 void __user
*user_arg
= (void __user
*)arg
;
920 dbg("%scmd=0x%x", __FUNCTION__
, cmd
);
922 /* Based on code from acm.c and others */
925 /* wait for any of the 4 modem inputs (DCD,RI,DSR,CTS)*/
927 dbg("%s - TIOCMIWAIT not handled", __FUNCTION__
);
930 /* return count of modemline transitions */
932 dbg("%s - TIOCGICOUNT not handled", __FUNCTION__
);
935 /* return current info to caller */
936 dbg("%s - TCGETS data faked/incomplete", __FUNCTION__
);
938 if (!access_ok(VERIFY_WRITE
, user_arg
, sizeof(struct termios
)))
941 if (kernel_termios_to_user_termios((struct termios __user
*)arg
,
946 /* set port termios to the one given by the user */
947 dbg("%s - TCSETS not handled", __FUNCTION__
);
949 if (!access_ok(VERIFY_READ
, user_arg
, sizeof(struct termios
)))
952 if (user_termios_to_kernel_termios(&priv
->termios
,
953 (struct termios __user
*)arg
))
955 klsi_105_set_termios(port
, &priv
->termios
);
958 /* set port termios and try to wait for completion of last
960 /* We guess here. If there are not too many write urbs
961 * outstanding, we lie. */
962 /* what is the right way to wait here? schedule() ? */
964 while (klsi_105_chars_in_buffer(port) > (NUM_URBS / 4 ) * URB_TRANSFER_BUFFER_SIZE)
970 dbg("%s: arg not supported - 0x%04x", __FUNCTION__
,cmd
);
971 return(-ENOIOCTLCMD
);
975 } /* klsi_105_ioctl */
977 static void klsi_105_throttle (struct usb_serial_port
*port
)
979 dbg("%s - port %d", __FUNCTION__
, port
->number
);
980 usb_kill_urb(port
->read_urb
);
983 static void klsi_105_unthrottle (struct usb_serial_port
*port
)
987 dbg("%s - port %d", __FUNCTION__
, port
->number
);
989 port
->read_urb
->dev
= port
->serial
->dev
;
990 result
= usb_submit_urb(port
->read_urb
, GFP_ATOMIC
);
992 err("%s - failed submitting read urb, error %d", __FUNCTION__
,
998 static int __init
klsi_105_init (void)
1001 retval
= usb_serial_register(&kl5kusb105d_device
);
1003 goto failed_usb_serial_register
;
1004 retval
= usb_register(&kl5kusb105d_driver
);
1006 goto failed_usb_register
;
1008 info(DRIVER_DESC
" " DRIVER_VERSION
);
1010 failed_usb_register
:
1011 usb_serial_deregister(&kl5kusb105d_device
);
1012 failed_usb_serial_register
:
1017 static void __exit
klsi_105_exit (void)
1019 usb_deregister (&kl5kusb105d_driver
);
1020 usb_serial_deregister (&kl5kusb105d_device
);
1024 module_init (klsi_105_init
);
1025 module_exit (klsi_105_exit
);
1027 MODULE_AUTHOR( DRIVER_AUTHOR
);
1028 MODULE_DESCRIPTION( DRIVER_DESC
);
1029 MODULE_LICENSE("GPL");
1032 module_param(debug
, bool, S_IRUGO
| S_IWUSR
);
1033 MODULE_PARM_DESC(debug
, "enable extensive debugging messages");
1035 /* vim: set sts=8 ts=8 sw=8: */