MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / drivers / usb / serial / kl5kusb105.c
blobc5207d19401a7f64f96f46f94534b781fca812e3
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/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"
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 int from_user,
83 const unsigned char *buf,
84 int count);
85 static void klsi_105_write_bulk_callback (struct urb *urb, struct pt_regs *regs);
86 static int klsi_105_chars_in_buffer (struct usb_serial_port *port);
87 static int klsi_105_write_room (struct usb_serial_port *port);
89 static void klsi_105_read_bulk_callback (struct urb *urb, struct pt_regs *regs);
90 static void klsi_105_set_termios (struct usb_serial_port *port,
91 struct termios * old);
92 static int klsi_105_ioctl (struct usb_serial_port *port,
93 struct file * file,
94 unsigned int cmd,
95 unsigned long arg);
96 static void klsi_105_throttle (struct usb_serial_port *port);
97 static void klsi_105_unthrottle (struct usb_serial_port *port);
99 static void klsi_105_break_ctl (struct usb_serial_port *port,
100 int break_state );
102 static int klsi_105_tiocmget (struct usb_serial_port *port,
103 struct file *file);
104 static int klsi_105_tiocmset (struct usb_serial_port *port,
105 struct file *file, unsigned int set,
106 unsigned int clear);
109 * All of the device info needed for the KLSI converters.
111 static struct usb_device_id id_table [] = {
112 { USB_DEVICE(PALMCONNECT_VID, PALMCONNECT_PID) },
113 { USB_DEVICE(KLSI_VID, KLSI_KL5KUSB105D_PID) },
114 { } /* Terminating entry */
117 MODULE_DEVICE_TABLE (usb, id_table);
119 static struct usb_driver kl5kusb105d_driver = {
120 .owner = THIS_MODULE,
121 .name = "kl5kusb105d",
122 .probe = usb_serial_probe,
123 .disconnect = usb_serial_disconnect,
124 .id_table = id_table,
127 static struct usb_serial_device_type kl5kusb105d_device = {
128 .owner = THIS_MODULE,
129 .name = "KL5KUSB105D / PalmConnect",
130 .short_name = "kl5kusb105d",
131 .id_table = id_table,
132 .num_interrupt_in = 1,
133 .num_bulk_in = 1,
134 .num_bulk_out = 1,
135 .num_ports = 1,
136 .open = klsi_105_open,
137 .close = klsi_105_close,
138 .write = klsi_105_write,
139 .write_bulk_callback = klsi_105_write_bulk_callback,
140 .chars_in_buffer = klsi_105_chars_in_buffer,
141 .write_room = klsi_105_write_room,
142 .read_bulk_callback =klsi_105_read_bulk_callback,
143 .ioctl = klsi_105_ioctl,
144 .set_termios = klsi_105_set_termios,
145 /*.break_ctl = klsi_105_break_ctl,*/
146 .tiocmget = klsi_105_tiocmget,
147 .tiocmset = klsi_105_tiocmset,
148 .attach = klsi_105_startup,
149 .shutdown = klsi_105_shutdown,
150 .throttle = klsi_105_throttle,
151 .unthrottle = klsi_105_unthrottle,
154 struct klsi_105_port_settings {
155 __u8 pktlen; /* always 5, it seems */
156 __u8 baudrate;
157 __u8 databits;
158 __u8 unknown1;
159 __u8 unknown2;
160 } __attribute__ ((packed));
162 /* we implement a pool of NUM_URBS urbs per usb_serial */
163 #define NUM_URBS 1
164 #define URB_TRANSFER_BUFFER_SIZE 64
165 struct klsi_105_private {
166 struct klsi_105_port_settings cfg;
167 struct termios termios;
168 unsigned long line_state; /* modem line settings */
169 /* write pool */
170 struct urb * write_urb_pool[NUM_URBS];
171 spinlock_t lock;
172 unsigned long bytes_in;
173 unsigned long bytes_out;
178 * Handle vendor specific USB requests
182 #define KLSI_TIMEOUT (HZ * 5 ) /* default urb timeout */
184 static int klsi_105_chg_port_settings(struct usb_serial_port *port,
185 struct klsi_105_port_settings *settings)
187 int rc;
189 rc = usb_control_msg(port->serial->dev,
190 usb_sndctrlpipe(port->serial->dev, 0),
191 KL5KUSB105A_SIO_SET_DATA,
192 USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_INTERFACE,
193 0, /* value */
194 0, /* index */
195 settings,
196 sizeof(struct klsi_105_port_settings),
197 KLSI_TIMEOUT);
198 if (rc < 0)
199 err("Change port settings failed (error = %d)", rc);
200 info("%s - %d byte block, baudrate %x, databits %d, u1 %d, u2 %d",
201 __FUNCTION__,
202 settings->pktlen,
203 settings->baudrate, settings->databits,
204 settings->unknown1, settings->unknown2);
205 return rc;
206 } /* klsi_105_chg_port_settings */
208 /* translate a 16-bit status value from the device to linux's TIO bits */
209 static unsigned long klsi_105_status2linestate(const __u16 status)
211 unsigned long res = 0;
213 res = ((status & KL5KUSB105A_DSR) ? TIOCM_DSR : 0)
214 | ((status & KL5KUSB105A_CTS) ? TIOCM_CTS : 0)
217 return res;
220 * Read line control via vendor command and return result through
221 * *line_state_p
223 /* It seems that the status buffer has always only 2 bytes length */
224 #define KLSI_STATUSBUF_LEN 2
225 static int klsi_105_get_line_state(struct usb_serial_port *port,
226 unsigned long *line_state_p)
228 int rc;
229 __u8 status_buf[KLSI_STATUSBUF_LEN] = { -1,-1};
230 __u16 status;
232 info("%s - sending SIO Poll request", __FUNCTION__);
233 rc = usb_control_msg(port->serial->dev,
234 usb_rcvctrlpipe(port->serial->dev, 0),
235 KL5KUSB105A_SIO_POLL,
236 USB_TYPE_VENDOR | USB_DIR_IN,
237 0, /* value */
238 0, /* index */
239 status_buf, KLSI_STATUSBUF_LEN,
240 10*HZ
242 if (rc < 0)
243 err("Reading line status failed (error = %d)", rc);
244 else {
245 status = status_buf[0] + (status_buf[1]<<8);
247 info("%s - read status %x %x", __FUNCTION__,
248 status_buf[0], status_buf[1]);
250 *line_state_p = klsi_105_status2linestate(status);
253 return rc;
258 * Driver's tty interface functions
261 static int klsi_105_startup (struct usb_serial *serial)
263 struct klsi_105_private *priv;
264 int i;
266 /* check if we support the product id (see keyspan.c)
267 * FIXME
270 /* allocate the private data structure */
271 for (i=0; i<serial->num_ports; i++) {
272 int j;
273 priv = kmalloc(sizeof(struct klsi_105_private),
274 GFP_KERNEL);
275 if (!priv) {
276 dbg("%skmalloc for klsi_105_private failed.", __FUNCTION__);
277 return -ENOMEM;
279 /* set initial values for control structures */
280 priv->cfg.pktlen = 5;
281 priv->cfg.baudrate = kl5kusb105a_sio_b9600;
282 priv->cfg.databits = kl5kusb105a_dtb_8;
283 priv->cfg.unknown1 = 0;
284 priv->cfg.unknown2 = 1;
286 priv->line_state = 0;
288 priv->bytes_in = 0;
289 priv->bytes_out = 0;
290 usb_set_serial_port_data(serial->port[i], priv);
292 spin_lock_init (&priv->lock);
293 for (j=0; j<NUM_URBS; j++) {
294 struct urb* urb = usb_alloc_urb(0, GFP_KERNEL);
296 priv->write_urb_pool[j] = urb;
297 if (urb == NULL) {
298 err("No more urbs???");
299 continue;
302 urb->transfer_buffer = NULL;
303 urb->transfer_buffer = kmalloc (URB_TRANSFER_BUFFER_SIZE,
304 GFP_KERNEL);
305 if (!urb->transfer_buffer) {
306 err("%s - out of memory for urb buffers.", __FUNCTION__);
307 continue;
311 /* priv->termios is left uninitalized until port opening */
312 init_waitqueue_head(&serial->port[i]->write_wait);
315 return (0);
316 } /* klsi_105_startup */
319 static void klsi_105_shutdown (struct usb_serial *serial)
321 int i;
323 dbg("%s", __FUNCTION__);
325 /* stop reads and writes on all ports */
326 for (i=0; i < serial->num_ports; ++i) {
327 struct klsi_105_private *priv = usb_get_serial_port_data(serial->port[i]);
328 unsigned long flags;
330 if (priv) {
331 /* kill our write urb pool */
332 int j;
333 struct urb **write_urbs = priv->write_urb_pool;
334 spin_lock_irqsave(&priv->lock,flags);
336 for (j = 0; j < NUM_URBS; j++) {
337 if (write_urbs[j]) {
338 /* FIXME - uncomment the following
339 * usb_unlink_urb call when the host
340 * controllers get fixed to set
341 * urb->dev = NULL after the urb is
342 * finished. Otherwise this call
343 * oopses. */
344 /* usb_unlink_urb(write_urbs[j]); */
345 if (write_urbs[j]->transfer_buffer)
346 kfree(write_urbs[j]->transfer_buffer);
347 usb_free_urb (write_urbs[j]);
351 spin_unlock_irqrestore (&priv->lock, flags);
353 kfree(priv);
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);
362 int retval = 0;
363 int rc;
364 int i;
365 unsigned long line_state;
366 struct klsi_105_port_settings cfg;
367 unsigned long flags;
369 dbg("%s port %d", __FUNCTION__, port->number);
371 /* force low_latency on so that our tty_push actually forces
372 * the data through
373 * port->tty->low_latency = 1; */
375 /* Do a defined restart:
376 * Set up sane default baud rate and send the 'READ_ON'
377 * vendor command.
378 * FIXME: set modem line control (how?)
379 * Then read the modem line control and store values in
380 * priv->line_state.
382 cfg.pktlen = 5;
383 cfg.baudrate = kl5kusb105a_sio_b9600;
384 cfg.databits = kl5kusb105a_dtb_8;
385 cfg.unknown1 = 0;
386 cfg.unknown2 = 1;
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,
411 port);
413 rc = usb_submit_urb(port->read_urb, GFP_KERNEL);
414 if (rc) {
415 err("%s - failed submitting read urb, error %d", __FUNCTION__, rc);
416 retval = rc;
417 goto exit;
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,
425 0, /* index */
426 NULL,
428 KLSI_TIMEOUT);
429 if (rc < 0) {
430 err("Enabling read failed (error = %d)", rc);
431 retval = rc;
432 } else
433 dbg("%s - enabled reading", __FUNCTION__);
435 rc = klsi_105_get_line_state(port, &line_state);
436 if (rc >= 0) {
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);
441 retval = 0;
442 } else
443 retval = rc;
445 exit:
446 return retval;
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);
453 int rc;
455 dbg("%s port %d", __FUNCTION__, port->number);
457 /* send READ_OFF */
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,
463 0, /* index */
464 NULL, 0,
465 KLSI_TIMEOUT);
466 if (rc < 0)
467 err("Disabling read failed (error = %d)", rc);
469 /* shutdown our bulk reads and writes */
470 usb_unlink_urb (port->write_urb);
471 usb_unlink_urb (port->read_urb);
472 /* unlink our write pool */
473 /* FIXME */
474 /* wgg - do I need this? I think so. */
475 usb_unlink_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, int from_user,
488 const unsigned char *buf, int count)
490 struct klsi_105_private *priv = usb_get_serial_port_data(port);
491 int result, size;
492 int bytes_sent=0;
494 dbg("%s - port %d", __FUNCTION__, port->number);
496 while (count > 0) {
497 /* try to find a free urb (write 0 bytes if none) */
498 struct urb *urb = NULL;
499 unsigned long flags;
500 int i;
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);
507 break;
510 spin_unlock_irqrestore (&priv->lock, flags);
512 if (urb==NULL) {
513 dbg("%s - no more free urbs", __FUNCTION__);
514 goto exit;
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__);
521 goto exit;
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 if (from_user) {
529 if (copy_from_user(urb->transfer_buffer
530 + KLSI_105_DATA_OFFSET, buf, size)) {
531 return -EFAULT;
533 } else {
534 memcpy (urb->transfer_buffer + KLSI_105_DATA_OFFSET,
535 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, struct pt_regs *regs)
570 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
572 dbg("%s - port %d", __FUNCTION__, port->number);
574 if (urb->status) {
575 dbg("%s - nonzero write bulk status received: %d", __FUNCTION__,
576 urb->status);
577 return;
580 /* from generic_write_bulk_callback */
581 schedule_work(&port->work);
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, struct pt_regs *regs)
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;
637 dbg("%s - port %d", __FUNCTION__, port->number);
639 /* The urb might have been killed. */
640 if (urb->status) {
641 dbg("%s - nonzero read bulk status received: %d", __FUNCTION__,
642 urb->status);
643 return;
646 /* The data received is again preceded by a length double-byte in LSB-
647 * first order (see klsi_105_write() )
649 if (urb->actual_length == 0) {
650 /* empty urbs seem to happen, we ignore them */
651 /* dbg("%s - emtpy URB", __FUNCTION__); */
653 } else if (urb->actual_length <= 2) {
654 dbg("%s - size %d URB not understood", __FUNCTION__,
655 urb->actual_length);
656 usb_serial_debug_data(debug, &port->dev, __FUNCTION__,
657 urb->actual_length, data);
658 } else {
659 int i;
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 for (i = 2; i < 2+bytes_sent; i++) {
681 /* if we insert more than TTY_FLIPBUF_SIZE characters,
682 * we drop them. */
683 if(tty->flip.count >= TTY_FLIPBUF_SIZE) {
684 tty_flip_buffer_push(tty);
686 /* this doesn't actually push the data through unless
687 * tty->low_latency is set */
688 tty_insert_flip_char(tty, ((__u8*) data)[i], 0);
690 tty_flip_buffer_push(tty);
692 /* again lockless, but debug info only */
693 priv->bytes_in += bytes_sent;
695 /* Continue trying to always read */
696 usb_fill_bulk_urb(port->read_urb, port->serial->dev,
697 usb_rcvbulkpipe(port->serial->dev,
698 port->bulk_in_endpointAddress),
699 port->read_urb->transfer_buffer,
700 port->read_urb->transfer_buffer_length,
701 klsi_105_read_bulk_callback,
702 port);
703 rc = usb_submit_urb(port->read_urb, GFP_ATOMIC);
704 if (rc)
705 err("%s - failed resubmitting read urb, error %d", __FUNCTION__, rc);
706 } /* klsi_105_read_bulk_callback */
709 static void klsi_105_set_termios (struct usb_serial_port *port,
710 struct termios *old_termios)
712 struct klsi_105_private *priv = usb_get_serial_port_data(port);
713 unsigned int iflag = port->tty->termios->c_iflag;
714 unsigned int old_iflag = old_termios->c_iflag;
715 unsigned int cflag = port->tty->termios->c_cflag;
716 unsigned int old_cflag = old_termios->c_cflag;
717 struct klsi_105_port_settings cfg;
718 unsigned long flags;
720 /* lock while we are modifying the settings */
721 spin_lock_irqsave (&priv->lock, flags);
724 * Update baud rate
726 if( (cflag & CBAUD) != (old_cflag & CBAUD) ) {
727 /* reassert DTR and (maybe) RTS on transition from B0 */
728 if( (old_cflag & CBAUD) == B0 ) {
729 dbg("%s: baud was B0", __FUNCTION__);
730 #if 0
731 priv->control_state |= TIOCM_DTR;
732 /* don't set RTS if using hardware flow control */
733 if (!(old_cflag & CRTSCTS)) {
734 priv->control_state |= TIOCM_RTS;
736 mct_u232_set_modem_ctrl(serial, priv->control_state);
737 #endif
740 switch(cflag & CBAUD) {
741 case B0: /* handled below */
742 break;
743 case B1200: priv->cfg.baudrate = kl5kusb105a_sio_b1200;
744 break;
745 case B2400: priv->cfg.baudrate = kl5kusb105a_sio_b2400;
746 break;
747 case B4800: priv->cfg.baudrate = kl5kusb105a_sio_b4800;
748 break;
749 case B9600: priv->cfg.baudrate = kl5kusb105a_sio_b9600;
750 break;
751 case B19200: priv->cfg.baudrate = kl5kusb105a_sio_b19200;
752 break;
753 case B38400: priv->cfg.baudrate = kl5kusb105a_sio_b38400;
754 break;
755 case B57600: priv->cfg.baudrate = kl5kusb105a_sio_b57600;
756 break;
757 case B115200: priv->cfg.baudrate = kl5kusb105a_sio_b115200;
758 break;
759 default:
760 err("KLSI USB->Serial converter:"
761 " unsupported baudrate request, using default"
762 " of 9600");
763 priv->cfg.baudrate = kl5kusb105a_sio_b9600;
764 break;
766 if ((cflag & CBAUD) == B0 ) {
767 dbg("%s: baud is B0", __FUNCTION__);
768 /* Drop RTS and DTR */
769 /* maybe this should be simulated by sending read
770 * disable and read enable messages?
773 #if 0
774 priv->control_state &= ~(TIOCM_DTR | TIOCM_RTS);
775 mct_u232_set_modem_ctrl(serial, priv->control_state);
776 #endif
780 if ((cflag & CSIZE) != (old_cflag & CSIZE)) {
781 /* set the number of data bits */
782 switch (cflag & CSIZE) {
783 case CS5:
784 dbg("%s - 5 bits/byte not supported", __FUNCTION__);
785 return ;
786 case CS6:
787 dbg("%s - 6 bits/byte not supported", __FUNCTION__);
788 return ;
789 case CS7:
790 priv->cfg.databits = kl5kusb105a_dtb_7;
791 break;
792 case CS8:
793 priv->cfg.databits = kl5kusb105a_dtb_8;
794 break;
795 default:
796 err("CSIZE was not CS5-CS8, using default of 8");
797 priv->cfg.databits = kl5kusb105a_dtb_8;
798 break;
803 * Update line control register (LCR)
805 if ((cflag & (PARENB|PARODD)) != (old_cflag & (PARENB|PARODD))
806 || (cflag & CSTOPB) != (old_cflag & CSTOPB) ) {
808 #if 0
809 priv->last_lcr = 0;
811 /* set the parity */
812 if (cflag & PARENB)
813 priv->last_lcr |= (cflag & PARODD) ?
814 MCT_U232_PARITY_ODD : MCT_U232_PARITY_EVEN;
815 else
816 priv->last_lcr |= MCT_U232_PARITY_NONE;
818 /* set the number of stop bits */
819 priv->last_lcr |= (cflag & CSTOPB) ?
820 MCT_U232_STOP_BITS_2 : MCT_U232_STOP_BITS_1;
822 mct_u232_set_line_ctrl(serial, priv->last_lcr);
823 #endif
828 * Set flow control: well, I do not really now how to handle DTR/RTS.
829 * Just do what we have seen with SniffUSB on Win98.
831 if( (iflag & IXOFF) != (old_iflag & IXOFF)
832 || (iflag & IXON) != (old_iflag & IXON)
833 || (cflag & CRTSCTS) != (old_cflag & CRTSCTS) ) {
835 /* Drop DTR/RTS if no flow control otherwise assert */
836 #if 0
837 if ((iflag & IXOFF) || (iflag & IXON) || (cflag & CRTSCTS) )
838 priv->control_state |= TIOCM_DTR | TIOCM_RTS;
839 else
840 priv->control_state &= ~(TIOCM_DTR | TIOCM_RTS);
841 mct_u232_set_modem_ctrl(serial, priv->control_state);
842 #endif
845 memcpy (&cfg, &priv->cfg, sizeof(cfg));
846 spin_unlock_irqrestore (&priv->lock, flags);
848 /* now commit changes to device */
849 klsi_105_chg_port_settings(port, &cfg);
850 } /* klsi_105_set_termios */
853 #if 0
854 static void mct_u232_break_ctl( struct usb_serial_port *port, int break_state )
856 struct usb_serial *serial = port->serial;
857 struct mct_u232_private *priv = (struct mct_u232_private *)port->private;
858 unsigned char lcr = priv->last_lcr;
860 dbg("%sstate=%d", __FUNCTION__, break_state);
862 if (break_state)
863 lcr |= MCT_U232_SET_BREAK;
865 mct_u232_set_line_ctrl(serial, lcr);
866 } /* mct_u232_break_ctl */
867 #endif
869 static int klsi_105_tiocmget (struct usb_serial_port *port, struct file *file)
871 struct klsi_105_private *priv = usb_get_serial_port_data(port);
872 unsigned long flags;
873 int rc;
874 unsigned long line_state;
875 dbg("%s - request, just guessing", __FUNCTION__);
877 rc = klsi_105_get_line_state(port, &line_state);
878 if (rc < 0) {
879 err("Reading line control failed (error = %d)", rc);
880 /* better return value? EAGAIN? */
881 return rc;
884 spin_lock_irqsave (&priv->lock, flags);
885 priv->line_state = line_state;
886 spin_unlock_irqrestore (&priv->lock, flags);
887 dbg("%s - read line state 0x%lx", __FUNCTION__, line_state);
888 return (int)line_state;
891 static int klsi_105_tiocmset (struct usb_serial_port *port, struct file *file,
892 unsigned int set, unsigned int clear)
894 int retval = -EINVAL;
896 dbg("%s", __FUNCTION__);
898 /* if this ever gets implemented, it should be done something like this:
899 struct usb_serial *serial = port->serial;
900 struct klsi_105_private *priv = usb_get_serial_port_data(port);
901 unsigned long flags;
902 int control;
904 spin_lock_irqsave (&priv->lock, flags);
905 if (set & TIOCM_RTS)
906 priv->control_state |= TIOCM_RTS;
907 if (set & TIOCM_DTR)
908 priv->control_state |= TIOCM_DTR;
909 if (clear & TIOCM_RTS)
910 priv->control_state &= ~TIOCM_RTS;
911 if (clear & TIOCM_DTR)
912 priv->control_state &= ~TIOCM_DTR;
913 control = priv->control_state;
914 spin_unlock_irqrestore (&priv->lock, flags);
915 retval = mct_u232_set_modem_ctrl(serial, control);
917 return retval;
920 static int klsi_105_ioctl (struct usb_serial_port *port, struct file * file,
921 unsigned int cmd, unsigned long arg)
923 struct klsi_105_private *priv = usb_get_serial_port_data(port);
924 void __user *user_arg = (void __user *)arg;
926 dbg("%scmd=0x%x", __FUNCTION__, cmd);
928 /* Based on code from acm.c and others */
929 switch (cmd) {
930 case TIOCMIWAIT:
931 /* wait for any of the 4 modem inputs (DCD,RI,DSR,CTS)*/
932 /* TODO */
933 dbg("%s - TIOCMIWAIT not handled", __FUNCTION__);
934 return -ENOIOCTLCMD;
936 case TIOCGICOUNT:
937 /* return count of modemline transitions */
938 /* TODO */
939 dbg("%s - TIOCGICOUNT not handled", __FUNCTION__);
940 return -ENOIOCTLCMD;
941 case TCGETS: {
942 /* return current info to caller */
943 int retval;
945 dbg("%s - TCGETS data faked/incomplete", __FUNCTION__);
947 retval = verify_area(VERIFY_WRITE, user_arg,
948 sizeof(struct termios));
949 if (retval)
950 return retval;
952 if (kernel_termios_to_user_termios((struct termios __user *)arg,
953 &priv->termios))
954 return -EFAULT;
955 return(0);
957 case TCSETS: {
958 /* set port termios to the one given by the user */
959 int retval;
961 dbg("%s - TCSETS not handled", __FUNCTION__);
963 retval = verify_area(VERIFY_READ, user_arg,
964 sizeof(struct termios));
965 if (retval)
966 return retval;
968 if (user_termios_to_kernel_termios(&priv->termios,
969 (struct termios __user *)arg))
970 return -EFAULT;
971 klsi_105_set_termios(port, &priv->termios);
972 return(0);
974 case TCSETSW: {
975 /* set port termios and try to wait for completion of last
976 * write operation */
977 /* We guess here. If there are not too many write urbs
978 * outstanding, we lie. */
979 /* what is the right way to wait here? schedule() ? */
981 while (klsi_105_chars_in_buffer(port) > (NUM_URBS / 4 ) * URB_TRANSFER_BUFFER_SIZE)
982 schedule();
984 return -ENOIOCTLCMD;
986 default:
987 dbg("%s: arg not supported - 0x%04x", __FUNCTION__,cmd);
988 return(-ENOIOCTLCMD);
989 break;
991 return 0;
992 } /* klsi_105_ioctl */
994 static void klsi_105_throttle (struct usb_serial_port *port)
996 dbg("%s - port %d", __FUNCTION__, port->number);
997 usb_unlink_urb (port->read_urb);
1000 static void klsi_105_unthrottle (struct usb_serial_port *port)
1002 int result;
1004 dbg("%s - port %d", __FUNCTION__, port->number);
1006 port->read_urb->dev = port->serial->dev;
1007 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1008 if (result)
1009 err("%s - failed submitting read urb, error %d", __FUNCTION__,
1010 result);
1015 static int __init klsi_105_init (void)
1017 int retval;
1018 retval = usb_serial_register(&kl5kusb105d_device);
1019 if (retval)
1020 goto failed_usb_serial_register;
1021 retval = usb_register(&kl5kusb105d_driver);
1022 if (retval)
1023 goto failed_usb_register;
1025 info(DRIVER_DESC " " DRIVER_VERSION);
1026 return 0;
1027 failed_usb_register:
1028 usb_serial_deregister(&kl5kusb105d_device);
1029 failed_usb_serial_register:
1030 return retval;
1034 static void __exit klsi_105_exit (void)
1036 usb_deregister (&kl5kusb105d_driver);
1037 usb_serial_deregister (&kl5kusb105d_device);
1041 module_init (klsi_105_init);
1042 module_exit (klsi_105_exit);
1044 MODULE_AUTHOR( DRIVER_AUTHOR );
1045 MODULE_DESCRIPTION( DRIVER_DESC );
1046 MODULE_LICENSE("GPL");
1049 module_param(debug, bool, S_IRUGO | S_IWUSR);
1050 MODULE_PARM_DESC(debug, "enable extensive debugging messages");
1052 /* vim: set sts=8 ts=8 sw=8: */