blob: 2a2f3e2da0553a3b3488fcb0a196a3a13953efb8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * KLSI KL5KUSB105 chip RS232 converter driver
3 *
4 * Copyright (C) 2001 Utz-Uwe Haus <haus@uuhaus.de>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * All information about the device was acquired using SniffUSB ans snoopUSB
12 * on Windows98.
13 * It was written out of frustration with the PalmConnect USB Serial adapter
14 * sold by Palm Inc.
15 * Neither Palm, nor their contractor (MCCI) or their supplier (KLSI) provided
16 * information that was not already available.
17 *
18 * It seems that KLSI bought some silicon-design information from ScanLogic,
19 * whose SL11R processor is at the core of the KL5KUSB chipset from KLSI.
20 * KLSI has firmware available for their devices; it is probable that the
21 * firmware differs from that used by KLSI in their products. If you have an
22 * original KLSI device and can provide some information on it, I would be
23 * most interested in adding support for it here. If you have any information
24 * on the protocol used (or find errors in my reverse-engineered stuff), please
25 * let me know.
26 *
27 * The code was only tested with a PalmConnect USB adapter; if you
28 * are adventurous, try it with any KLSI-based device and let me know how it
29 * breaks so that I can fix it!
30 */
31
32/* TODO:
33 * check modem line signals
34 * implement handshaking or decide that we do not support it
35 */
36
37/* History:
38 * 0.3a - implemented pools of write URBs
39 * 0.3 - alpha version for public testing
40 * 0.2 - TIOCMGET works, so autopilot(1) can be used!
41 * 0.1 - can be used to to pilot-xfer -p /dev/ttyUSB0 -l
42 *
43 * The driver skeleton is mainly based on mct_u232.c and various other
44 * pieces of code shamelessly copied from the drivers/usb/serial/ directory.
45 */
46
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/kernel.h>
49#include <linux/errno.h>
50#include <linux/init.h>
51#include <linux/slab.h>
52#include <linux/tty.h>
53#include <linux/tty_driver.h>
54#include <linux/tty_flip.h>
55#include <linux/module.h>
56#include <asm/uaccess.h>
57#include <linux/usb.h>
Greg Kroah-Hartmana9698882006-07-11 21:22:58 -070058#include <linux/usb/serial.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#include "kl5kusb105.h"
60
61static int debug;
62
63/*
64 * Version Information
65 */
66#define DRIVER_VERSION "v0.3a"
67#define DRIVER_AUTHOR "Utz-Uwe Haus <haus@uuhaus.de>"
68#define DRIVER_DESC "KLSI KL5KUSB105 chipset USB->Serial Converter driver"
69
70
71/*
72 * Function prototypes
73 */
74static int klsi_105_startup (struct usb_serial *serial);
75static void klsi_105_shutdown (struct usb_serial *serial);
76static int klsi_105_open (struct usb_serial_port *port,
77 struct file *filp);
78static void klsi_105_close (struct usb_serial_port *port,
79 struct file *filp);
80static int klsi_105_write (struct usb_serial_port *port,
81 const unsigned char *buf,
82 int count);
83static void klsi_105_write_bulk_callback (struct urb *urb, struct pt_regs *regs);
84static int klsi_105_chars_in_buffer (struct usb_serial_port *port);
85static int klsi_105_write_room (struct usb_serial_port *port);
86
87static void klsi_105_read_bulk_callback (struct urb *urb, struct pt_regs *regs);
88static void klsi_105_set_termios (struct usb_serial_port *port,
89 struct termios * old);
90static int klsi_105_ioctl (struct usb_serial_port *port,
91 struct file * file,
92 unsigned int cmd,
93 unsigned long arg);
94static void klsi_105_throttle (struct usb_serial_port *port);
95static void klsi_105_unthrottle (struct usb_serial_port *port);
96/*
97static void klsi_105_break_ctl (struct usb_serial_port *port,
98 int break_state );
99 */
100static int klsi_105_tiocmget (struct usb_serial_port *port,
101 struct file *file);
102static int klsi_105_tiocmset (struct usb_serial_port *port,
103 struct file *file, unsigned int set,
104 unsigned int clear);
105
106/*
107 * All of the device info needed for the KLSI converters.
108 */
109static struct usb_device_id id_table [] = {
110 { USB_DEVICE(PALMCONNECT_VID, PALMCONNECT_PID) },
111 { USB_DEVICE(KLSI_VID, KLSI_KL5KUSB105D_PID) },
112 { } /* Terminating entry */
113};
114
115MODULE_DEVICE_TABLE (usb, id_table);
116
117static struct usb_driver kl5kusb105d_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 .name = "kl5kusb105d",
119 .probe = usb_serial_probe,
120 .disconnect = usb_serial_disconnect,
121 .id_table = id_table,
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800122 .no_dynamic_id = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123};
124
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700125static struct usb_serial_driver kl5kusb105d_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700126 .driver = {
127 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700128 .name = "kl5kusb105d",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700129 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700130 .description = "KL5KUSB105D / PalmConnect",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 .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,
152};
153
154struct 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));
161
162/* we implement a pool of NUM_URBS urbs per usb_serial */
163#define NUM_URBS 1
164#define URB_TRANSFER_BUFFER_SIZE 64
165struct 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;
174};
175
176
177/*
178 * Handle vendor specific USB requests
179 */
180
181
182#define KLSI_TIMEOUT 5000 /* default urb timeout */
183
184static int klsi_105_chg_port_settings(struct usb_serial_port *port,
185 struct klsi_105_port_settings *settings)
186{
187 int rc;
188
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 */
207
208/* translate a 16-bit status value from the device to linux's TIO bits */
209static unsigned long klsi_105_status2linestate(const __u16 status)
210{
211 unsigned long res = 0;
212
213 res = ((status & KL5KUSB105A_DSR) ? TIOCM_DSR : 0)
214 | ((status & KL5KUSB105A_CTS) ? TIOCM_CTS : 0)
215 ;
216
217 return res;
218}
219/*
220 * Read line control via vendor command and return result through
221 * *line_state_p
222 */
223/* It seems that the status buffer has always only 2 bytes length */
224#define KLSI_STATUSBUF_LEN 2
225static int klsi_105_get_line_state(struct usb_serial_port *port,
226 unsigned long *line_state_p)
227{
228 int rc;
229 __u8 status_buf[KLSI_STATUSBUF_LEN] = { -1,-1};
230 __u16 status;
231
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 10000
241 );
242 if (rc < 0)
243 err("Reading line status failed (error = %d)", rc);
244 else {
245 status = status_buf[0] + (status_buf[1]<<8);
246
247 info("%s - read status %x %x", __FUNCTION__,
248 status_buf[0], status_buf[1]);
249
250 *line_state_p = klsi_105_status2linestate(status);
251 }
252
253 return rc;
254}
255
256
257/*
258 * Driver's tty interface functions
259 */
260
261static int klsi_105_startup (struct usb_serial *serial)
262{
263 struct klsi_105_private *priv;
264 int i;
265
266 /* check if we support the product id (see keyspan.c)
267 * FIXME
268 */
269
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;
278 }
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;
285
286 priv->line_state = 0;
287
288 priv->bytes_in = 0;
289 priv->bytes_out = 0;
290 usb_set_serial_port_data(serial->port[i], priv);
291
292 spin_lock_init (&priv->lock);
293 for (j=0; j<NUM_URBS; j++) {
294 struct urb* urb = usb_alloc_urb(0, GFP_KERNEL);
295
296 priv->write_urb_pool[j] = urb;
297 if (urb == NULL) {
298 err("No more urbs???");
299 continue;
300 }
301
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;
308 }
309 }
310
311 /* priv->termios is left uninitalized until port opening */
312 init_waitqueue_head(&serial->port[i]->write_wait);
313 }
314
315 return (0);
316} /* klsi_105_startup */
317
318
319static void klsi_105_shutdown (struct usb_serial *serial)
320{
321 int i;
322
323 dbg("%s", __FUNCTION__);
324
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;
329
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);
335
336 for (j = 0; j < NUM_URBS; j++) {
337 if (write_urbs[j]) {
338 /* FIXME - uncomment the following
339 * usb_kill_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_kill_urb(write_urbs[j]); */
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -0700345 kfree(write_urbs[j]->transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 usb_free_urb (write_urbs[j]);
347 }
348 }
349
350 spin_unlock_irqrestore (&priv->lock, flags);
351
352 kfree(priv);
353 usb_set_serial_port_data(serial->port[i], NULL);
354 }
355 }
356} /* klsi_105_shutdown */
357
358static int klsi_105_open (struct usb_serial_port *port, struct file *filp)
359{
360 struct klsi_105_private *priv = usb_get_serial_port_data(port);
361 int retval = 0;
362 int rc;
363 int i;
364 unsigned long line_state;
365 struct klsi_105_port_settings cfg;
366 unsigned long flags;
367
368 dbg("%s port %d", __FUNCTION__, port->number);
369
370 /* force low_latency on so that our tty_push actually forces
371 * the data through
372 * port->tty->low_latency = 1; */
373
374 /* Do a defined restart:
375 * Set up sane default baud rate and send the 'READ_ON'
376 * vendor command.
377 * FIXME: set modem line control (how?)
378 * Then read the modem line control and store values in
379 * priv->line_state.
380 */
381 cfg.pktlen = 5;
382 cfg.baudrate = kl5kusb105a_sio_b9600;
383 cfg.databits = kl5kusb105a_dtb_8;
384 cfg.unknown1 = 0;
385 cfg.unknown2 = 1;
386 klsi_105_chg_port_settings(port, &cfg);
387
388 /* set up termios structure */
389 spin_lock_irqsave (&priv->lock, flags);
390 priv->termios.c_iflag = port->tty->termios->c_iflag;
391 priv->termios.c_oflag = port->tty->termios->c_oflag;
392 priv->termios.c_cflag = port->tty->termios->c_cflag;
393 priv->termios.c_lflag = port->tty->termios->c_lflag;
394 for (i=0; i<NCCS; i++)
395 priv->termios.c_cc[i] = port->tty->termios->c_cc[i];
396 priv->cfg.pktlen = cfg.pktlen;
397 priv->cfg.baudrate = cfg.baudrate;
398 priv->cfg.databits = cfg.databits;
399 priv->cfg.unknown1 = cfg.unknown1;
400 priv->cfg.unknown2 = cfg.unknown2;
401 spin_unlock_irqrestore (&priv->lock, flags);
402
403 /* READ_ON and urb submission */
404 usb_fill_bulk_urb(port->read_urb, port->serial->dev,
405 usb_rcvbulkpipe(port->serial->dev,
406 port->bulk_in_endpointAddress),
407 port->read_urb->transfer_buffer,
408 port->read_urb->transfer_buffer_length,
409 klsi_105_read_bulk_callback,
410 port);
411
412 rc = usb_submit_urb(port->read_urb, GFP_KERNEL);
413 if (rc) {
414 err("%s - failed submitting read urb, error %d", __FUNCTION__, rc);
415 retval = rc;
416 goto exit;
417 }
418
419 rc = usb_control_msg(port->serial->dev,
420 usb_sndctrlpipe(port->serial->dev,0),
421 KL5KUSB105A_SIO_CONFIGURE,
422 USB_TYPE_VENDOR|USB_DIR_OUT|USB_RECIP_INTERFACE,
423 KL5KUSB105A_SIO_CONFIGURE_READ_ON,
424 0, /* index */
425 NULL,
426 0,
427 KLSI_TIMEOUT);
428 if (rc < 0) {
429 err("Enabling read failed (error = %d)", rc);
430 retval = rc;
431 } else
432 dbg("%s - enabled reading", __FUNCTION__);
433
434 rc = klsi_105_get_line_state(port, &line_state);
435 if (rc >= 0) {
436 spin_lock_irqsave (&priv->lock, flags);
437 priv->line_state = line_state;
438 spin_unlock_irqrestore (&priv->lock, flags);
439 dbg("%s - read line state 0x%lx", __FUNCTION__, line_state);
440 retval = 0;
441 } else
442 retval = rc;
443
444exit:
445 return retval;
446} /* klsi_105_open */
447
448
449static void klsi_105_close (struct usb_serial_port *port, struct file *filp)
450{
451 struct klsi_105_private *priv = usb_get_serial_port_data(port);
452 int rc;
453
454 dbg("%s port %d", __FUNCTION__, port->number);
455
456 /* send READ_OFF */
457 rc = usb_control_msg (port->serial->dev,
458 usb_sndctrlpipe(port->serial->dev, 0),
459 KL5KUSB105A_SIO_CONFIGURE,
460 USB_TYPE_VENDOR | USB_DIR_OUT,
461 KL5KUSB105A_SIO_CONFIGURE_READ_OFF,
462 0, /* index */
463 NULL, 0,
464 KLSI_TIMEOUT);
465 if (rc < 0)
466 err("Disabling read failed (error = %d)", rc);
467
468 /* shutdown our bulk reads and writes */
469 usb_kill_urb(port->write_urb);
470 usb_kill_urb(port->read_urb);
471 /* unlink our write pool */
472 /* FIXME */
473 /* wgg - do I need this? I think so. */
474 usb_kill_urb(port->interrupt_in_urb);
475 info("kl5kusb105 port stats: %ld bytes in, %ld bytes out", priv->bytes_in, priv->bytes_out);
476} /* klsi_105_close */
477
478
479/* We need to write a complete 64-byte data block and encode the
480 * number actually sent in the first double-byte, LSB-order. That
481 * leaves at most 62 bytes of payload.
482 */
483#define KLSI_105_DATA_OFFSET 2 /* in the bulk urb data block */
484
485
486static int klsi_105_write (struct usb_serial_port *port,
487 const unsigned char *buf, int count)
488{
489 struct klsi_105_private *priv = usb_get_serial_port_data(port);
490 int result, size;
491 int bytes_sent=0;
492
493 dbg("%s - port %d", __FUNCTION__, port->number);
494
495 while (count > 0) {
496 /* try to find a free urb (write 0 bytes if none) */
497 struct urb *urb = NULL;
498 unsigned long flags;
499 int i;
500 /* since the pool is per-port we might not need the spin lock !? */
501 spin_lock_irqsave (&priv->lock, flags);
502 for (i=0; i<NUM_URBS; i++) {
503 if (priv->write_urb_pool[i]->status != -EINPROGRESS) {
504 urb = priv->write_urb_pool[i];
505 dbg("%s - using pool URB %d", __FUNCTION__, i);
506 break;
507 }
508 }
509 spin_unlock_irqrestore (&priv->lock, flags);
510
511 if (urb==NULL) {
512 dbg("%s - no more free urbs", __FUNCTION__);
513 goto exit;
514 }
515
516 if (urb->transfer_buffer == NULL) {
517 urb->transfer_buffer = kmalloc (URB_TRANSFER_BUFFER_SIZE, GFP_ATOMIC);
518 if (urb->transfer_buffer == NULL) {
519 err("%s - no more kernel memory...", __FUNCTION__);
520 goto exit;
521 }
522 }
523
524 size = min (count, port->bulk_out_size - KLSI_105_DATA_OFFSET);
525 size = min (size, URB_TRANSFER_BUFFER_SIZE - KLSI_105_DATA_OFFSET);
526
527 memcpy (urb->transfer_buffer + KLSI_105_DATA_OFFSET, buf, size);
528
529 /* write payload size into transfer buffer */
530 ((__u8 *)urb->transfer_buffer)[0] = (__u8) (size & 0xFF);
531 ((__u8 *)urb->transfer_buffer)[1] = (__u8) ((size & 0xFF00)>>8);
532
533 /* set up our urb */
534 usb_fill_bulk_urb(urb, port->serial->dev,
535 usb_sndbulkpipe(port->serial->dev,
536 port->bulk_out_endpointAddress),
537 urb->transfer_buffer,
538 URB_TRANSFER_BUFFER_SIZE,
539 klsi_105_write_bulk_callback,
540 port);
541
542 /* send the data out the bulk port */
543 result = usb_submit_urb(urb, GFP_ATOMIC);
544 if (result) {
545 err("%s - failed submitting write urb, error %d", __FUNCTION__, result);
546 goto exit;
547 }
548 buf += size;
549 bytes_sent += size;
550 count -= size;
551 }
552exit:
553 /* lockless, but it's for debug info only... */
554 priv->bytes_out+=bytes_sent;
555
556 return bytes_sent; /* that's how much we wrote */
557} /* klsi_105_write */
558
559static void klsi_105_write_bulk_callback ( struct urb *urb, struct pt_regs *regs)
560{
561 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
562
563 dbg("%s - port %d", __FUNCTION__, port->number);
564
565 if (urb->status) {
566 dbg("%s - nonzero write bulk status received: %d", __FUNCTION__,
567 urb->status);
568 return;
569 }
570
Pete Zaitcevcf2c7482006-05-22 21:58:49 -0700571 usb_serial_port_softint(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572} /* klsi_105_write_bulk_completion_callback */
573
574
575/* return number of characters currently in the writing process */
576static int klsi_105_chars_in_buffer (struct usb_serial_port *port)
577{
578 int chars = 0;
579 int i;
580 unsigned long flags;
581 struct klsi_105_private *priv = usb_get_serial_port_data(port);
582
583 spin_lock_irqsave (&priv->lock, flags);
584
585 for (i = 0; i < NUM_URBS; ++i) {
586 if (priv->write_urb_pool[i]->status == -EINPROGRESS) {
587 chars += URB_TRANSFER_BUFFER_SIZE;
588 }
589 }
590
591 spin_unlock_irqrestore (&priv->lock, flags);
592
593 dbg("%s - returns %d", __FUNCTION__, chars);
594 return (chars);
595}
596
597static int klsi_105_write_room (struct usb_serial_port *port)
598{
599 unsigned long flags;
600 int i;
601 int room = 0;
602 struct klsi_105_private *priv = usb_get_serial_port_data(port);
603
604 spin_lock_irqsave (&priv->lock, flags);
605 for (i = 0; i < NUM_URBS; ++i) {
606 if (priv->write_urb_pool[i]->status != -EINPROGRESS) {
607 room += URB_TRANSFER_BUFFER_SIZE;
608 }
609 }
610
611 spin_unlock_irqrestore (&priv->lock, flags);
612
613 dbg("%s - returns %d", __FUNCTION__, room);
614 return (room);
615}
616
617
618
619static void klsi_105_read_bulk_callback (struct urb *urb, struct pt_regs *regs)
620{
621 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
622 struct klsi_105_private *priv = usb_get_serial_port_data(port);
623 struct tty_struct *tty;
624 unsigned char *data = urb->transfer_buffer;
625 int rc;
626
627 dbg("%s - port %d", __FUNCTION__, port->number);
628
629 /* The urb might have been killed. */
630 if (urb->status) {
631 dbg("%s - nonzero read bulk status received: %d", __FUNCTION__,
632 urb->status);
633 return;
634 }
635
636 /* The data received is again preceded by a length double-byte in LSB-
637 * first order (see klsi_105_write() )
638 */
639 if (urb->actual_length == 0) {
640 /* empty urbs seem to happen, we ignore them */
641 /* dbg("%s - emtpy URB", __FUNCTION__); */
642 ;
643 } else if (urb->actual_length <= 2) {
644 dbg("%s - size %d URB not understood", __FUNCTION__,
645 urb->actual_length);
646 usb_serial_debug_data(debug, &port->dev, __FUNCTION__,
647 urb->actual_length, data);
648 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 int bytes_sent = ((__u8 *) data)[0] +
650 ((unsigned int) ((__u8 *) data)[1] << 8);
651 tty = port->tty;
652 /* we should immediately resubmit the URB, before attempting
653 * to pass the data on to the tty layer. But that needs locking
654 * against re-entry an then mixed-up data because of
655 * intermixed tty_flip_buffer_push()s
656 * FIXME
657 */
658 usb_serial_debug_data(debug, &port->dev, __FUNCTION__,
659 urb->actual_length, data);
660
661 if (bytes_sent + 2 > urb->actual_length) {
662 dbg("%s - trying to read more data than available"
663 " (%d vs. %d)", __FUNCTION__,
664 bytes_sent+2, urb->actual_length);
665 /* cap at implied limit */
666 bytes_sent = urb->actual_length - 2;
667 }
668
Alan Cox33f0f882006-01-09 20:54:13 -0800669 tty_buffer_request_room(tty, bytes_sent);
670 tty_insert_flip_string(tty, data + 2, bytes_sent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 tty_flip_buffer_push(tty);
672
673 /* again lockless, but debug info only */
674 priv->bytes_in += bytes_sent;
675 }
676 /* Continue trying to always read */
677 usb_fill_bulk_urb(port->read_urb, port->serial->dev,
678 usb_rcvbulkpipe(port->serial->dev,
679 port->bulk_in_endpointAddress),
680 port->read_urb->transfer_buffer,
681 port->read_urb->transfer_buffer_length,
682 klsi_105_read_bulk_callback,
683 port);
684 rc = usb_submit_urb(port->read_urb, GFP_ATOMIC);
685 if (rc)
686 err("%s - failed resubmitting read urb, error %d", __FUNCTION__, rc);
687} /* klsi_105_read_bulk_callback */
688
689
690static void klsi_105_set_termios (struct usb_serial_port *port,
691 struct termios *old_termios)
692{
693 struct klsi_105_private *priv = usb_get_serial_port_data(port);
694 unsigned int iflag = port->tty->termios->c_iflag;
695 unsigned int old_iflag = old_termios->c_iflag;
696 unsigned int cflag = port->tty->termios->c_cflag;
697 unsigned int old_cflag = old_termios->c_cflag;
698 struct klsi_105_port_settings cfg;
699 unsigned long flags;
700
701 /* lock while we are modifying the settings */
702 spin_lock_irqsave (&priv->lock, flags);
703
704 /*
705 * Update baud rate
706 */
707 if( (cflag & CBAUD) != (old_cflag & CBAUD) ) {
708 /* reassert DTR and (maybe) RTS on transition from B0 */
709 if( (old_cflag & CBAUD) == B0 ) {
710 dbg("%s: baud was B0", __FUNCTION__);
711#if 0
712 priv->control_state |= TIOCM_DTR;
713 /* don't set RTS if using hardware flow control */
714 if (!(old_cflag & CRTSCTS)) {
715 priv->control_state |= TIOCM_RTS;
716 }
717 mct_u232_set_modem_ctrl(serial, priv->control_state);
718#endif
719 }
720
721 switch(cflag & CBAUD) {
722 case B0: /* handled below */
723 break;
724 case B1200: priv->cfg.baudrate = kl5kusb105a_sio_b1200;
725 break;
726 case B2400: priv->cfg.baudrate = kl5kusb105a_sio_b2400;
727 break;
728 case B4800: priv->cfg.baudrate = kl5kusb105a_sio_b4800;
729 break;
730 case B9600: priv->cfg.baudrate = kl5kusb105a_sio_b9600;
731 break;
732 case B19200: priv->cfg.baudrate = kl5kusb105a_sio_b19200;
733 break;
734 case B38400: priv->cfg.baudrate = kl5kusb105a_sio_b38400;
735 break;
736 case B57600: priv->cfg.baudrate = kl5kusb105a_sio_b57600;
737 break;
738 case B115200: priv->cfg.baudrate = kl5kusb105a_sio_b115200;
739 break;
740 default:
741 err("KLSI USB->Serial converter:"
742 " unsupported baudrate request, using default"
743 " of 9600");
744 priv->cfg.baudrate = kl5kusb105a_sio_b9600;
745 break;
746 }
747 if ((cflag & CBAUD) == B0 ) {
748 dbg("%s: baud is B0", __FUNCTION__);
749 /* Drop RTS and DTR */
750 /* maybe this should be simulated by sending read
751 * disable and read enable messages?
752 */
753 ;
754#if 0
755 priv->control_state &= ~(TIOCM_DTR | TIOCM_RTS);
756 mct_u232_set_modem_ctrl(serial, priv->control_state);
757#endif
758 }
759 }
760
761 if ((cflag & CSIZE) != (old_cflag & CSIZE)) {
762 /* set the number of data bits */
763 switch (cflag & CSIZE) {
764 case CS5:
765 dbg("%s - 5 bits/byte not supported", __FUNCTION__);
766 spin_unlock_irqrestore (&priv->lock, flags);
767 return ;
768 case CS6:
769 dbg("%s - 6 bits/byte not supported", __FUNCTION__);
770 spin_unlock_irqrestore (&priv->lock, flags);
771 return ;
772 case CS7:
773 priv->cfg.databits = kl5kusb105a_dtb_7;
774 break;
775 case CS8:
776 priv->cfg.databits = kl5kusb105a_dtb_8;
777 break;
778 default:
779 err("CSIZE was not CS5-CS8, using default of 8");
780 priv->cfg.databits = kl5kusb105a_dtb_8;
781 break;
782 }
783 }
784
785 /*
786 * Update line control register (LCR)
787 */
788 if ((cflag & (PARENB|PARODD)) != (old_cflag & (PARENB|PARODD))
789 || (cflag & CSTOPB) != (old_cflag & CSTOPB) ) {
790
791#if 0
792 priv->last_lcr = 0;
793
794 /* set the parity */
795 if (cflag & PARENB)
796 priv->last_lcr |= (cflag & PARODD) ?
797 MCT_U232_PARITY_ODD : MCT_U232_PARITY_EVEN;
798 else
799 priv->last_lcr |= MCT_U232_PARITY_NONE;
800
801 /* set the number of stop bits */
802 priv->last_lcr |= (cflag & CSTOPB) ?
803 MCT_U232_STOP_BITS_2 : MCT_U232_STOP_BITS_1;
804
805 mct_u232_set_line_ctrl(serial, priv->last_lcr);
806#endif
807 ;
808 }
809
810 /*
811 * Set flow control: well, I do not really now how to handle DTR/RTS.
812 * Just do what we have seen with SniffUSB on Win98.
813 */
814 if( (iflag & IXOFF) != (old_iflag & IXOFF)
815 || (iflag & IXON) != (old_iflag & IXON)
816 || (cflag & CRTSCTS) != (old_cflag & CRTSCTS) ) {
817
818 /* Drop DTR/RTS if no flow control otherwise assert */
819#if 0
820 if ((iflag & IXOFF) || (iflag & IXON) || (cflag & CRTSCTS) )
821 priv->control_state |= TIOCM_DTR | TIOCM_RTS;
822 else
823 priv->control_state &= ~(TIOCM_DTR | TIOCM_RTS);
824 mct_u232_set_modem_ctrl(serial, priv->control_state);
825#endif
826 ;
827 }
828 memcpy (&cfg, &priv->cfg, sizeof(cfg));
829 spin_unlock_irqrestore (&priv->lock, flags);
830
831 /* now commit changes to device */
832 klsi_105_chg_port_settings(port, &cfg);
833} /* klsi_105_set_termios */
834
835
836#if 0
837static void mct_u232_break_ctl( struct usb_serial_port *port, int break_state )
838{
839 struct usb_serial *serial = port->serial;
840 struct mct_u232_private *priv = (struct mct_u232_private *)port->private;
841 unsigned char lcr = priv->last_lcr;
842
843 dbg("%sstate=%d", __FUNCTION__, break_state);
844
845 if (break_state)
846 lcr |= MCT_U232_SET_BREAK;
847
848 mct_u232_set_line_ctrl(serial, lcr);
849} /* mct_u232_break_ctl */
850#endif
851
852static int klsi_105_tiocmget (struct usb_serial_port *port, struct file *file)
853{
854 struct klsi_105_private *priv = usb_get_serial_port_data(port);
855 unsigned long flags;
856 int rc;
857 unsigned long line_state;
858 dbg("%s - request, just guessing", __FUNCTION__);
859
860 rc = klsi_105_get_line_state(port, &line_state);
861 if (rc < 0) {
862 err("Reading line control failed (error = %d)", rc);
863 /* better return value? EAGAIN? */
864 return rc;
865 }
866
867 spin_lock_irqsave (&priv->lock, flags);
868 priv->line_state = line_state;
869 spin_unlock_irqrestore (&priv->lock, flags);
870 dbg("%s - read line state 0x%lx", __FUNCTION__, line_state);
871 return (int)line_state;
872}
873
874static int klsi_105_tiocmset (struct usb_serial_port *port, struct file *file,
875 unsigned int set, unsigned int clear)
876{
877 int retval = -EINVAL;
878
879 dbg("%s", __FUNCTION__);
880
881/* if this ever gets implemented, it should be done something like this:
882 struct usb_serial *serial = port->serial;
883 struct klsi_105_private *priv = usb_get_serial_port_data(port);
884 unsigned long flags;
885 int control;
886
887 spin_lock_irqsave (&priv->lock, flags);
888 if (set & TIOCM_RTS)
889 priv->control_state |= TIOCM_RTS;
890 if (set & TIOCM_DTR)
891 priv->control_state |= TIOCM_DTR;
892 if (clear & TIOCM_RTS)
893 priv->control_state &= ~TIOCM_RTS;
894 if (clear & TIOCM_DTR)
895 priv->control_state &= ~TIOCM_DTR;
896 control = priv->control_state;
897 spin_unlock_irqrestore (&priv->lock, flags);
898 retval = mct_u232_set_modem_ctrl(serial, control);
899*/
900 return retval;
901}
902
903static int klsi_105_ioctl (struct usb_serial_port *port, struct file * file,
904 unsigned int cmd, unsigned long arg)
905{
906 struct klsi_105_private *priv = usb_get_serial_port_data(port);
907 void __user *user_arg = (void __user *)arg;
908
909 dbg("%scmd=0x%x", __FUNCTION__, cmd);
910
911 /* Based on code from acm.c and others */
912 switch (cmd) {
913 case TIOCMIWAIT:
914 /* wait for any of the 4 modem inputs (DCD,RI,DSR,CTS)*/
915 /* TODO */
916 dbg("%s - TIOCMIWAIT not handled", __FUNCTION__);
917 return -ENOIOCTLCMD;
918 case TIOCGICOUNT:
919 /* return count of modemline transitions */
920 /* TODO */
921 dbg("%s - TIOCGICOUNT not handled", __FUNCTION__);
922 return -ENOIOCTLCMD;
923 case TCGETS:
924 /* return current info to caller */
925 dbg("%s - TCGETS data faked/incomplete", __FUNCTION__);
926
927 if (!access_ok(VERIFY_WRITE, user_arg, sizeof(struct termios)))
928 return -EFAULT;
929
930 if (kernel_termios_to_user_termios((struct termios __user *)arg,
931 &priv->termios))
932 return -EFAULT;
933 return 0;
934 case TCSETS:
935 /* set port termios to the one given by the user */
936 dbg("%s - TCSETS not handled", __FUNCTION__);
937
938 if (!access_ok(VERIFY_READ, user_arg, sizeof(struct termios)))
939 return -EFAULT;
940
941 if (user_termios_to_kernel_termios(&priv->termios,
942 (struct termios __user *)arg))
943 return -EFAULT;
944 klsi_105_set_termios(port, &priv->termios);
945 return 0;
946 case TCSETSW: {
947 /* set port termios and try to wait for completion of last
948 * write operation */
949 /* We guess here. If there are not too many write urbs
950 * outstanding, we lie. */
951 /* what is the right way to wait here? schedule() ? */
952 /*
953 while (klsi_105_chars_in_buffer(port) > (NUM_URBS / 4 ) * URB_TRANSFER_BUFFER_SIZE)
954 schedule();
955 */
956 return -ENOIOCTLCMD;
957 }
958 default:
959 dbg("%s: arg not supported - 0x%04x", __FUNCTION__,cmd);
960 return(-ENOIOCTLCMD);
961 break;
962 }
963 return 0;
964} /* klsi_105_ioctl */
965
966static void klsi_105_throttle (struct usb_serial_port *port)
967{
968 dbg("%s - port %d", __FUNCTION__, port->number);
969 usb_kill_urb(port->read_urb);
970}
971
972static void klsi_105_unthrottle (struct usb_serial_port *port)
973{
974 int result;
975
976 dbg("%s - port %d", __FUNCTION__, port->number);
977
978 port->read_urb->dev = port->serial->dev;
979 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
980 if (result)
981 err("%s - failed submitting read urb, error %d", __FUNCTION__,
982 result);
983}
984
985
986
987static int __init klsi_105_init (void)
988{
989 int retval;
990 retval = usb_serial_register(&kl5kusb105d_device);
991 if (retval)
992 goto failed_usb_serial_register;
993 retval = usb_register(&kl5kusb105d_driver);
994 if (retval)
995 goto failed_usb_register;
996
997 info(DRIVER_DESC " " DRIVER_VERSION);
998 return 0;
999failed_usb_register:
1000 usb_serial_deregister(&kl5kusb105d_device);
1001failed_usb_serial_register:
1002 return retval;
1003}
1004
1005
1006static void __exit klsi_105_exit (void)
1007{
1008 usb_deregister (&kl5kusb105d_driver);
1009 usb_serial_deregister (&kl5kusb105d_device);
1010}
1011
1012
1013module_init (klsi_105_init);
1014module_exit (klsi_105_exit);
1015
1016MODULE_AUTHOR( DRIVER_AUTHOR );
1017MODULE_DESCRIPTION( DRIVER_DESC );
1018MODULE_LICENSE("GPL");
1019
1020
1021module_param(debug, bool, S_IRUGO | S_IWUSR);
1022MODULE_PARM_DESC(debug, "enable extensive debugging messages");
1023
1024/* vim: set sts=8 ts=8 sw=8: */