blob: b3ac045ab4084cb2148301275a5f4f0f6627815d [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);
David Howells7d12e782006-10-05 14:55:46 +010083static void klsi_105_write_bulk_callback (struct urb *urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084static int klsi_105_chars_in_buffer (struct usb_serial_port *port);
85static int klsi_105_write_room (struct usb_serial_port *port);
86
David Howells7d12e782006-10-05 14:55:46 +010087static void klsi_105_read_bulk_callback (struct urb *urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088static void klsi_105_set_termios (struct usb_serial_port *port,
Alan Cox606d0992006-12-08 02:38:45 -080089 struct ktermios *old);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090static void klsi_105_throttle (struct usb_serial_port *port);
91static void klsi_105_unthrottle (struct usb_serial_port *port);
92/*
93static void klsi_105_break_ctl (struct usb_serial_port *port,
94 int break_state );
95 */
96static int klsi_105_tiocmget (struct usb_serial_port *port,
97 struct file *file);
98static int klsi_105_tiocmset (struct usb_serial_port *port,
99 struct file *file, unsigned int set,
100 unsigned int clear);
101
102/*
103 * All of the device info needed for the KLSI converters.
104 */
105static struct usb_device_id id_table [] = {
106 { USB_DEVICE(PALMCONNECT_VID, PALMCONNECT_PID) },
107 { USB_DEVICE(KLSI_VID, KLSI_KL5KUSB105D_PID) },
108 { } /* Terminating entry */
109};
110
111MODULE_DEVICE_TABLE (usb, id_table);
112
113static struct usb_driver kl5kusb105d_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 .name = "kl5kusb105d",
115 .probe = usb_serial_probe,
116 .disconnect = usb_serial_disconnect,
117 .id_table = id_table,
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800118 .no_dynamic_id = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119};
120
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700121static struct usb_serial_driver kl5kusb105d_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700122 .driver = {
123 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700124 .name = "kl5kusb105d",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700125 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700126 .description = "KL5KUSB105D / PalmConnect",
Johannes Hölzld9b1b782006-12-17 21:50:24 +0100127 .usb_driver = &kl5kusb105d_driver,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 .id_table = id_table,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 .num_ports = 1,
130 .open = klsi_105_open,
131 .close = klsi_105_close,
132 .write = klsi_105_write,
133 .write_bulk_callback = klsi_105_write_bulk_callback,
134 .chars_in_buffer = klsi_105_chars_in_buffer,
135 .write_room = klsi_105_write_room,
136 .read_bulk_callback =klsi_105_read_bulk_callback,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 .set_termios = klsi_105_set_termios,
138 /*.break_ctl = klsi_105_break_ctl,*/
139 .tiocmget = klsi_105_tiocmget,
140 .tiocmset = klsi_105_tiocmset,
141 .attach = klsi_105_startup,
142 .shutdown = klsi_105_shutdown,
143 .throttle = klsi_105_throttle,
144 .unthrottle = klsi_105_unthrottle,
145};
146
147struct klsi_105_port_settings {
148 __u8 pktlen; /* always 5, it seems */
149 __u8 baudrate;
150 __u8 databits;
151 __u8 unknown1;
152 __u8 unknown2;
153} __attribute__ ((packed));
154
155/* we implement a pool of NUM_URBS urbs per usb_serial */
156#define NUM_URBS 1
157#define URB_TRANSFER_BUFFER_SIZE 64
158struct klsi_105_private {
159 struct klsi_105_port_settings cfg;
Alan Cox606d0992006-12-08 02:38:45 -0800160 struct ktermios termios;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 unsigned long line_state; /* modem line settings */
162 /* write pool */
163 struct urb * write_urb_pool[NUM_URBS];
164 spinlock_t lock;
165 unsigned long bytes_in;
166 unsigned long bytes_out;
167};
168
169
170/*
171 * Handle vendor specific USB requests
172 */
173
174
175#define KLSI_TIMEOUT 5000 /* default urb timeout */
176
177static int klsi_105_chg_port_settings(struct usb_serial_port *port,
178 struct klsi_105_port_settings *settings)
179{
180 int rc;
181
182 rc = usb_control_msg(port->serial->dev,
183 usb_sndctrlpipe(port->serial->dev, 0),
184 KL5KUSB105A_SIO_SET_DATA,
185 USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_INTERFACE,
186 0, /* value */
187 0, /* index */
188 settings,
189 sizeof(struct klsi_105_port_settings),
190 KLSI_TIMEOUT);
191 if (rc < 0)
192 err("Change port settings failed (error = %d)", rc);
193 info("%s - %d byte block, baudrate %x, databits %d, u1 %d, u2 %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800194 __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 settings->pktlen,
196 settings->baudrate, settings->databits,
197 settings->unknown1, settings->unknown2);
198 return rc;
199} /* klsi_105_chg_port_settings */
200
201/* translate a 16-bit status value from the device to linux's TIO bits */
202static unsigned long klsi_105_status2linestate(const __u16 status)
203{
204 unsigned long res = 0;
205
206 res = ((status & KL5KUSB105A_DSR) ? TIOCM_DSR : 0)
207 | ((status & KL5KUSB105A_CTS) ? TIOCM_CTS : 0)
208 ;
209
210 return res;
211}
212/*
213 * Read line control via vendor command and return result through
214 * *line_state_p
215 */
216/* It seems that the status buffer has always only 2 bytes length */
217#define KLSI_STATUSBUF_LEN 2
218static int klsi_105_get_line_state(struct usb_serial_port *port,
219 unsigned long *line_state_p)
220{
221 int rc;
222 __u8 status_buf[KLSI_STATUSBUF_LEN] = { -1,-1};
223 __u16 status;
224
Harvey Harrison441b62c2008-03-03 16:08:34 -0800225 info("%s - sending SIO Poll request", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 rc = usb_control_msg(port->serial->dev,
227 usb_rcvctrlpipe(port->serial->dev, 0),
228 KL5KUSB105A_SIO_POLL,
229 USB_TYPE_VENDOR | USB_DIR_IN,
230 0, /* value */
231 0, /* index */
232 status_buf, KLSI_STATUSBUF_LEN,
233 10000
234 );
235 if (rc < 0)
236 err("Reading line status failed (error = %d)", rc);
237 else {
Oliver Neukum9306fff2007-03-29 11:23:54 +0200238 status = le16_to_cpu(*(u16 *)status_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Harvey Harrison441b62c2008-03-03 16:08:34 -0800240 info("%s - read status %x %x", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 status_buf[0], status_buf[1]);
242
243 *line_state_p = klsi_105_status2linestate(status);
244 }
245
246 return rc;
247}
248
249
250/*
251 * Driver's tty interface functions
252 */
253
254static int klsi_105_startup (struct usb_serial *serial)
255{
256 struct klsi_105_private *priv;
Oliver Neukum9306fff2007-03-29 11:23:54 +0200257 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
259 /* check if we support the product id (see keyspan.c)
260 * FIXME
261 */
262
263 /* allocate the private data structure */
264 for (i=0; i<serial->num_ports; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 priv = kmalloc(sizeof(struct klsi_105_private),
266 GFP_KERNEL);
267 if (!priv) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800268 dbg("%skmalloc for klsi_105_private failed.", __func__);
Oliver Neukum9306fff2007-03-29 11:23:54 +0200269 i--;
270 goto err_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 }
272 /* set initial values for control structures */
273 priv->cfg.pktlen = 5;
274 priv->cfg.baudrate = kl5kusb105a_sio_b9600;
275 priv->cfg.databits = kl5kusb105a_dtb_8;
276 priv->cfg.unknown1 = 0;
277 priv->cfg.unknown2 = 1;
278
279 priv->line_state = 0;
280
281 priv->bytes_in = 0;
282 priv->bytes_out = 0;
283 usb_set_serial_port_data(serial->port[i], priv);
284
285 spin_lock_init (&priv->lock);
286 for (j=0; j<NUM_URBS; j++) {
287 struct urb* urb = usb_alloc_urb(0, GFP_KERNEL);
288
289 priv->write_urb_pool[j] = urb;
290 if (urb == NULL) {
291 err("No more urbs???");
Oliver Neukum9306fff2007-03-29 11:23:54 +0200292 goto err_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 }
294
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 urb->transfer_buffer = kmalloc (URB_TRANSFER_BUFFER_SIZE,
296 GFP_KERNEL);
297 if (!urb->transfer_buffer) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800298 err("%s - out of memory for urb buffers.", __func__);
Oliver Neukum9306fff2007-03-29 11:23:54 +0200299 goto err_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 }
301 }
302
303 /* priv->termios is left uninitalized until port opening */
304 init_waitqueue_head(&serial->port[i]->write_wait);
305 }
306
Oliver Neukum9306fff2007-03-29 11:23:54 +0200307 return 0;
308
309err_cleanup:
310 for (; i >= 0; i--) {
311 priv = usb_get_serial_port_data(serial->port[i]);
312 for (j=0; j < NUM_URBS; j++) {
313 if (priv->write_urb_pool[j]) {
314 kfree(priv->write_urb_pool[j]->transfer_buffer);
315 usb_free_urb(priv->write_urb_pool[j]);
316 }
317 }
318 usb_set_serial_port_data(serial->port[i], NULL);
319 }
320 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321} /* klsi_105_startup */
322
323
324static void klsi_105_shutdown (struct usb_serial *serial)
325{
326 int i;
327
Harvey Harrison441b62c2008-03-03 16:08:34 -0800328 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
330 /* stop reads and writes on all ports */
331 for (i=0; i < serial->num_ports; ++i) {
332 struct klsi_105_private *priv = usb_get_serial_port_data(serial->port[i]);
333 unsigned long flags;
334
335 if (priv) {
336 /* kill our write urb pool */
337 int j;
338 struct urb **write_urbs = priv->write_urb_pool;
339 spin_lock_irqsave(&priv->lock,flags);
340
341 for (j = 0; j < NUM_URBS; j++) {
342 if (write_urbs[j]) {
343 /* FIXME - uncomment the following
344 * usb_kill_urb call when the host
345 * controllers get fixed to set
346 * urb->dev = NULL after the urb is
347 * finished. Otherwise this call
348 * oopses. */
349 /* usb_kill_urb(write_urbs[j]); */
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -0700350 kfree(write_urbs[j]->transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 usb_free_urb (write_urbs[j]);
352 }
353 }
354
355 spin_unlock_irqrestore (&priv->lock, flags);
356
357 kfree(priv);
358 usb_set_serial_port_data(serial->port[i], NULL);
359 }
360 }
361} /* klsi_105_shutdown */
362
363static int klsi_105_open (struct usb_serial_port *port, struct file *filp)
364{
365 struct klsi_105_private *priv = usb_get_serial_port_data(port);
366 int retval = 0;
367 int rc;
368 int i;
369 unsigned long line_state;
370 struct klsi_105_port_settings cfg;
371 unsigned long flags;
372
Harvey Harrison441b62c2008-03-03 16:08:34 -0800373 dbg("%s port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
375 /* force low_latency on so that our tty_push actually forces
376 * the data through
377 * port->tty->low_latency = 1; */
378
379 /* Do a defined restart:
380 * Set up sane default baud rate and send the 'READ_ON'
381 * vendor command.
382 * FIXME: set modem line control (how?)
383 * Then read the modem line control and store values in
384 * priv->line_state.
385 */
386 cfg.pktlen = 5;
387 cfg.baudrate = kl5kusb105a_sio_b9600;
388 cfg.databits = kl5kusb105a_dtb_8;
389 cfg.unknown1 = 0;
390 cfg.unknown2 = 1;
391 klsi_105_chg_port_settings(port, &cfg);
392
393 /* set up termios structure */
394 spin_lock_irqsave (&priv->lock, flags);
395 priv->termios.c_iflag = port->tty->termios->c_iflag;
396 priv->termios.c_oflag = port->tty->termios->c_oflag;
397 priv->termios.c_cflag = port->tty->termios->c_cflag;
398 priv->termios.c_lflag = port->tty->termios->c_lflag;
399 for (i=0; i<NCCS; i++)
400 priv->termios.c_cc[i] = port->tty->termios->c_cc[i];
401 priv->cfg.pktlen = cfg.pktlen;
402 priv->cfg.baudrate = cfg.baudrate;
403 priv->cfg.databits = cfg.databits;
404 priv->cfg.unknown1 = cfg.unknown1;
405 priv->cfg.unknown2 = cfg.unknown2;
406 spin_unlock_irqrestore (&priv->lock, flags);
407
408 /* READ_ON and urb submission */
409 usb_fill_bulk_urb(port->read_urb, port->serial->dev,
410 usb_rcvbulkpipe(port->serial->dev,
411 port->bulk_in_endpointAddress),
412 port->read_urb->transfer_buffer,
413 port->read_urb->transfer_buffer_length,
414 klsi_105_read_bulk_callback,
415 port);
416
417 rc = usb_submit_urb(port->read_urb, GFP_KERNEL);
418 if (rc) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800419 err("%s - failed submitting read urb, error %d", __func__, rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 retval = rc;
421 goto exit;
422 }
423
424 rc = usb_control_msg(port->serial->dev,
425 usb_sndctrlpipe(port->serial->dev,0),
426 KL5KUSB105A_SIO_CONFIGURE,
427 USB_TYPE_VENDOR|USB_DIR_OUT|USB_RECIP_INTERFACE,
428 KL5KUSB105A_SIO_CONFIGURE_READ_ON,
429 0, /* index */
430 NULL,
431 0,
432 KLSI_TIMEOUT);
433 if (rc < 0) {
434 err("Enabling read failed (error = %d)", rc);
435 retval = rc;
436 } else
Harvey Harrison441b62c2008-03-03 16:08:34 -0800437 dbg("%s - enabled reading", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
439 rc = klsi_105_get_line_state(port, &line_state);
440 if (rc >= 0) {
441 spin_lock_irqsave (&priv->lock, flags);
442 priv->line_state = line_state;
443 spin_unlock_irqrestore (&priv->lock, flags);
Harvey Harrison441b62c2008-03-03 16:08:34 -0800444 dbg("%s - read line state 0x%lx", __func__, line_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 retval = 0;
446 } else
447 retval = rc;
448
449exit:
450 return retval;
451} /* klsi_105_open */
452
453
454static void klsi_105_close (struct usb_serial_port *port, struct file *filp)
455{
456 struct klsi_105_private *priv = usb_get_serial_port_data(port);
457 int rc;
458
Harvey Harrison441b62c2008-03-03 16:08:34 -0800459 dbg("%s port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
Oliver Neukum3edbc982008-01-22 12:47:15 +0100461 mutex_lock(&port->serial->disc_mutex);
462 if (!port->serial->disconnected) {
463 /* send READ_OFF */
464 rc = usb_control_msg (port->serial->dev,
465 usb_sndctrlpipe(port->serial->dev, 0),
466 KL5KUSB105A_SIO_CONFIGURE,
467 USB_TYPE_VENDOR | USB_DIR_OUT,
468 KL5KUSB105A_SIO_CONFIGURE_READ_OFF,
469 0, /* index */
470 NULL, 0,
471 KLSI_TIMEOUT);
472 if (rc < 0)
473 err("Disabling read failed (error = %d)", rc);
474 }
475 mutex_unlock(&port->serial->disc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
477 /* shutdown our bulk reads and writes */
478 usb_kill_urb(port->write_urb);
479 usb_kill_urb(port->read_urb);
480 /* unlink our write pool */
481 /* FIXME */
482 /* wgg - do I need this? I think so. */
483 usb_kill_urb(port->interrupt_in_urb);
484 info("kl5kusb105 port stats: %ld bytes in, %ld bytes out", priv->bytes_in, priv->bytes_out);
485} /* klsi_105_close */
486
487
488/* We need to write a complete 64-byte data block and encode the
489 * number actually sent in the first double-byte, LSB-order. That
490 * leaves at most 62 bytes of payload.
491 */
492#define KLSI_105_DATA_OFFSET 2 /* in the bulk urb data block */
493
494
495static int klsi_105_write (struct usb_serial_port *port,
496 const unsigned char *buf, int count)
497{
498 struct klsi_105_private *priv = usb_get_serial_port_data(port);
499 int result, size;
500 int bytes_sent=0;
501
Harvey Harrison441b62c2008-03-03 16:08:34 -0800502 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
504 while (count > 0) {
505 /* try to find a free urb (write 0 bytes if none) */
506 struct urb *urb = NULL;
507 unsigned long flags;
508 int i;
509 /* since the pool is per-port we might not need the spin lock !? */
510 spin_lock_irqsave (&priv->lock, flags);
511 for (i=0; i<NUM_URBS; i++) {
512 if (priv->write_urb_pool[i]->status != -EINPROGRESS) {
513 urb = priv->write_urb_pool[i];
Harvey Harrison441b62c2008-03-03 16:08:34 -0800514 dbg("%s - using pool URB %d", __func__, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 break;
516 }
517 }
518 spin_unlock_irqrestore (&priv->lock, flags);
519
520 if (urb==NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800521 dbg("%s - no more free urbs", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 goto exit;
523 }
524
525 if (urb->transfer_buffer == NULL) {
526 urb->transfer_buffer = kmalloc (URB_TRANSFER_BUFFER_SIZE, GFP_ATOMIC);
527 if (urb->transfer_buffer == NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800528 err("%s - no more kernel memory...", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 goto exit;
530 }
531 }
532
533 size = min (count, port->bulk_out_size - KLSI_105_DATA_OFFSET);
534 size = min (size, URB_TRANSFER_BUFFER_SIZE - KLSI_105_DATA_OFFSET);
535
536 memcpy (urb->transfer_buffer + KLSI_105_DATA_OFFSET, buf, size);
537
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);
541
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);
550
551 /* send the data out the bulk port */
552 result = usb_submit_urb(urb, GFP_ATOMIC);
553 if (result) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800554 err("%s - failed submitting write urb, error %d", __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 goto exit;
556 }
557 buf += size;
558 bytes_sent += size;
559 count -= size;
560 }
561exit:
562 /* lockless, but it's for debug info only... */
563 priv->bytes_out+=bytes_sent;
564
565 return bytes_sent; /* that's how much we wrote */
566} /* klsi_105_write */
567
David Howells7d12e782006-10-05 14:55:46 +0100568static void klsi_105_write_bulk_callback ( struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
570 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
Greg Kroah-Hartman17c1b352007-06-15 15:44:13 -0700571 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
Harvey Harrison441b62c2008-03-03 16:08:34 -0800573 dbg("%s - port %d", __func__, port->number);
Greg Kroah-Hartman17c1b352007-06-15 15:44:13 -0700574
575 if (status) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800576 dbg("%s - nonzero write bulk status received: %d", __func__,
Greg Kroah-Hartman17c1b352007-06-15 15:44:13 -0700577 status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 return;
579 }
580
Pete Zaitcevcf2c7482006-05-22 21:58:49 -0700581 usb_serial_port_softint(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582} /* klsi_105_write_bulk_completion_callback */
583
584
585/* return number of characters currently in the writing process */
586static int klsi_105_chars_in_buffer (struct usb_serial_port *port)
587{
588 int chars = 0;
589 int i;
590 unsigned long flags;
591 struct klsi_105_private *priv = usb_get_serial_port_data(port);
592
593 spin_lock_irqsave (&priv->lock, flags);
594
595 for (i = 0; i < NUM_URBS; ++i) {
596 if (priv->write_urb_pool[i]->status == -EINPROGRESS) {
597 chars += URB_TRANSFER_BUFFER_SIZE;
598 }
599 }
600
601 spin_unlock_irqrestore (&priv->lock, flags);
602
Harvey Harrison441b62c2008-03-03 16:08:34 -0800603 dbg("%s - returns %d", __func__, chars);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 return (chars);
605}
606
607static int klsi_105_write_room (struct usb_serial_port *port)
608{
609 unsigned long flags;
610 int i;
611 int room = 0;
612 struct klsi_105_private *priv = usb_get_serial_port_data(port);
613
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;
618 }
619 }
620
621 spin_unlock_irqrestore (&priv->lock, flags);
622
Harvey Harrison441b62c2008-03-03 16:08:34 -0800623 dbg("%s - returns %d", __func__, room);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 return (room);
625}
626
627
628
David Howells7d12e782006-10-05 14:55:46 +0100629static void klsi_105_read_bulk_callback (struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630{
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;
Greg Kroah-Hartman17c1b352007-06-15 15:44:13 -0700636 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Harvey Harrison441b62c2008-03-03 16:08:34 -0800638 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
640 /* The urb might have been killed. */
Greg Kroah-Hartman17c1b352007-06-15 15:44:13 -0700641 if (status) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800642 dbg("%s - nonzero read bulk status received: %d", __func__,
Greg Kroah-Hartman17c1b352007-06-15 15:44:13 -0700643 status);
644 return;
645 }
646
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 /* The data received is again preceded by a length double-byte in LSB-
648 * first order (see klsi_105_write() )
649 */
650 if (urb->actual_length == 0) {
651 /* empty urbs seem to happen, we ignore them */
Harvey Harrison441b62c2008-03-03 16:08:34 -0800652 /* dbg("%s - emtpy URB", __func__); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 ;
654 } else if (urb->actual_length <= 2) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800655 dbg("%s - size %d URB not understood", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 urb->actual_length);
Harvey Harrison441b62c2008-03-03 16:08:34 -0800657 usb_serial_debug_data(debug, &port->dev, __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 urb->actual_length, data);
659 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 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
668 */
Harvey Harrison441b62c2008-03-03 16:08:34 -0800669 usb_serial_debug_data(debug, &port->dev, __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 urb->actual_length, data);
671
672 if (bytes_sent + 2 > urb->actual_length) {
673 dbg("%s - trying to read more data than available"
Harvey Harrison441b62c2008-03-03 16:08:34 -0800674 " (%d vs. %d)", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 bytes_sent+2, urb->actual_length);
676 /* cap at implied limit */
677 bytes_sent = urb->actual_length - 2;
678 }
679
Alan Cox33f0f882006-01-09 20:54:13 -0800680 tty_buffer_request_room(tty, bytes_sent);
681 tty_insert_flip_string(tty, data + 2, bytes_sent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 tty_flip_buffer_push(tty);
683
684 /* again lockless, but debug info only */
685 priv->bytes_in += bytes_sent;
686 }
687 /* Continue trying to always read */
688 usb_fill_bulk_urb(port->read_urb, port->serial->dev,
689 usb_rcvbulkpipe(port->serial->dev,
690 port->bulk_in_endpointAddress),
691 port->read_urb->transfer_buffer,
692 port->read_urb->transfer_buffer_length,
693 klsi_105_read_bulk_callback,
694 port);
695 rc = usb_submit_urb(port->read_urb, GFP_ATOMIC);
696 if (rc)
Harvey Harrison441b62c2008-03-03 16:08:34 -0800697 err("%s - failed resubmitting read urb, error %d", __func__, rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698} /* klsi_105_read_bulk_callback */
699
700
701static void klsi_105_set_termios (struct usb_serial_port *port,
Alan Cox606d0992006-12-08 02:38:45 -0800702 struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703{
704 struct klsi_105_private *priv = usb_get_serial_port_data(port);
Alan Coxa5b6f602008-04-08 17:16:06 +0100705 struct tty_struct *tty = port->tty;
706 unsigned int iflag = tty->termios->c_iflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 unsigned int old_iflag = old_termios->c_iflag;
Alan Coxa5b6f602008-04-08 17:16:06 +0100708 unsigned int cflag = tty->termios->c_cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 unsigned int old_cflag = old_termios->c_cflag;
710 struct klsi_105_port_settings cfg;
711 unsigned long flags;
Alan Coxa5b6f602008-04-08 17:16:06 +0100712 speed_t baud;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
714 /* lock while we are modifying the settings */
715 spin_lock_irqsave (&priv->lock, flags);
716
717 /*
718 * Update baud rate
719 */
Alan Coxa5b6f602008-04-08 17:16:06 +0100720 baud = tty_get_baud_rate(tty);
721
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 if( (cflag & CBAUD) != (old_cflag & CBAUD) ) {
723 /* reassert DTR and (maybe) RTS on transition from B0 */
724 if( (old_cflag & CBAUD) == B0 ) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800725 dbg("%s: baud was B0", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726#if 0
727 priv->control_state |= TIOCM_DTR;
728 /* don't set RTS if using hardware flow control */
729 if (!(old_cflag & CRTSCTS)) {
730 priv->control_state |= TIOCM_RTS;
731 }
732 mct_u232_set_modem_ctrl(serial, priv->control_state);
733#endif
734 }
Alan Coxa5b6f602008-04-08 17:16:06 +0100735 }
736 switch(baud) {
Alan Cox3f6ff6e2007-08-10 14:53:34 -0700737 case 0: /* handled below */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 break;
Alan Cox3f6ff6e2007-08-10 14:53:34 -0700739 case 1200:
740 priv->cfg.baudrate = kl5kusb105a_sio_b1200;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 break;
Alan Cox3f6ff6e2007-08-10 14:53:34 -0700742 case 2400:
743 priv->cfg.baudrate = kl5kusb105a_sio_b2400;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 break;
Alan Cox3f6ff6e2007-08-10 14:53:34 -0700745 case 4800:
746 priv->cfg.baudrate = kl5kusb105a_sio_b4800;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 break;
Alan Cox3f6ff6e2007-08-10 14:53:34 -0700748 case 9600:
749 priv->cfg.baudrate = kl5kusb105a_sio_b9600;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 break;
Alan Cox3f6ff6e2007-08-10 14:53:34 -0700751 case 19200:
752 priv->cfg.baudrate = kl5kusb105a_sio_b19200;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 break;
Alan Cox3f6ff6e2007-08-10 14:53:34 -0700754 case 38400:
755 priv->cfg.baudrate = kl5kusb105a_sio_b38400;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 break;
Alan Cox3f6ff6e2007-08-10 14:53:34 -0700757 case 57600:
758 priv->cfg.baudrate = kl5kusb105a_sio_b57600;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 break;
Alan Cox3f6ff6e2007-08-10 14:53:34 -0700760 case 115200:
761 priv->cfg.baudrate = kl5kusb105a_sio_b115200;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 break;
763 default:
Alan Coxa5b6f602008-04-08 17:16:06 +0100764 dbg("KLSI USB->Serial converter:"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 " unsupported baudrate request, using default"
766 " of 9600");
767 priv->cfg.baudrate = kl5kusb105a_sio_b9600;
Alan Coxa5b6f602008-04-08 17:16:06 +0100768 baud = 9600;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 }
Alan Coxa5b6f602008-04-08 17:16:06 +0100771 if ((cflag & CBAUD) == B0 ) {
772 dbg("%s: baud is B0", __func__);
773 /* Drop RTS and DTR */
774 /* maybe this should be simulated by sending read
775 * disable and read enable messages?
776 */
777 ;
778#if 0
779 priv->control_state &= ~(TIOCM_DTR | TIOCM_RTS);
780 mct_u232_set_modem_ctrl(serial, priv->control_state);
781#endif
782 }
783 tty_encode_baud_rate(tty, baud, baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
785 if ((cflag & CSIZE) != (old_cflag & CSIZE)) {
786 /* set the number of data bits */
787 switch (cflag & CSIZE) {
788 case CS5:
Harvey Harrison441b62c2008-03-03 16:08:34 -0800789 dbg("%s - 5 bits/byte not supported", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 spin_unlock_irqrestore (&priv->lock, flags);
791 return ;
792 case CS6:
Harvey Harrison441b62c2008-03-03 16:08:34 -0800793 dbg("%s - 6 bits/byte not supported", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 spin_unlock_irqrestore (&priv->lock, flags);
795 return ;
796 case CS7:
797 priv->cfg.databits = kl5kusb105a_dtb_7;
798 break;
799 case CS8:
800 priv->cfg.databits = kl5kusb105a_dtb_8;
801 break;
802 default:
803 err("CSIZE was not CS5-CS8, using default of 8");
804 priv->cfg.databits = kl5kusb105a_dtb_8;
805 break;
806 }
807 }
808
809 /*
810 * Update line control register (LCR)
811 */
812 if ((cflag & (PARENB|PARODD)) != (old_cflag & (PARENB|PARODD))
813 || (cflag & CSTOPB) != (old_cflag & CSTOPB) ) {
814
Alan Coxa5b6f602008-04-08 17:16:06 +0100815 /* Not currently supported */
816 tty->termios->c_cflag &= ~(PARENB|PARODD|CSTOPB);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817#if 0
818 priv->last_lcr = 0;
819
820 /* set the parity */
821 if (cflag & PARENB)
822 priv->last_lcr |= (cflag & PARODD) ?
823 MCT_U232_PARITY_ODD : MCT_U232_PARITY_EVEN;
824 else
825 priv->last_lcr |= MCT_U232_PARITY_NONE;
826
827 /* set the number of stop bits */
828 priv->last_lcr |= (cflag & CSTOPB) ?
829 MCT_U232_STOP_BITS_2 : MCT_U232_STOP_BITS_1;
830
831 mct_u232_set_line_ctrl(serial, priv->last_lcr);
832#endif
833 ;
834 }
835
836 /*
837 * Set flow control: well, I do not really now how to handle DTR/RTS.
838 * Just do what we have seen with SniffUSB on Win98.
839 */
840 if( (iflag & IXOFF) != (old_iflag & IXOFF)
841 || (iflag & IXON) != (old_iflag & IXON)
842 || (cflag & CRTSCTS) != (old_cflag & CRTSCTS) ) {
843
Alan Coxa5b6f602008-04-08 17:16:06 +0100844 /* Not currently supported */
845 tty->termios->c_cflag &= ~CRTSCTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 /* Drop DTR/RTS if no flow control otherwise assert */
847#if 0
848 if ((iflag & IXOFF) || (iflag & IXON) || (cflag & CRTSCTS) )
849 priv->control_state |= TIOCM_DTR | TIOCM_RTS;
850 else
851 priv->control_state &= ~(TIOCM_DTR | TIOCM_RTS);
852 mct_u232_set_modem_ctrl(serial, priv->control_state);
853#endif
854 ;
855 }
856 memcpy (&cfg, &priv->cfg, sizeof(cfg));
857 spin_unlock_irqrestore (&priv->lock, flags);
858
859 /* now commit changes to device */
860 klsi_105_chg_port_settings(port, &cfg);
861} /* klsi_105_set_termios */
862
863
864#if 0
865static void mct_u232_break_ctl( struct usb_serial_port *port, int break_state )
866{
867 struct usb_serial *serial = port->serial;
868 struct mct_u232_private *priv = (struct mct_u232_private *)port->private;
869 unsigned char lcr = priv->last_lcr;
870
Harvey Harrison441b62c2008-03-03 16:08:34 -0800871 dbg("%sstate=%d", __func__, break_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
873 if (break_state)
874 lcr |= MCT_U232_SET_BREAK;
875
876 mct_u232_set_line_ctrl(serial, lcr);
877} /* mct_u232_break_ctl */
878#endif
879
880static int klsi_105_tiocmget (struct usb_serial_port *port, struct file *file)
881{
882 struct klsi_105_private *priv = usb_get_serial_port_data(port);
883 unsigned long flags;
884 int rc;
885 unsigned long line_state;
Harvey Harrison441b62c2008-03-03 16:08:34 -0800886 dbg("%s - request, just guessing", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
888 rc = klsi_105_get_line_state(port, &line_state);
889 if (rc < 0) {
890 err("Reading line control failed (error = %d)", rc);
891 /* better return value? EAGAIN? */
892 return rc;
893 }
894
895 spin_lock_irqsave (&priv->lock, flags);
896 priv->line_state = line_state;
897 spin_unlock_irqrestore (&priv->lock, flags);
Harvey Harrison441b62c2008-03-03 16:08:34 -0800898 dbg("%s - read line state 0x%lx", __func__, line_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 return (int)line_state;
900}
901
902static int klsi_105_tiocmset (struct usb_serial_port *port, struct file *file,
903 unsigned int set, unsigned int clear)
904{
905 int retval = -EINVAL;
906
Harvey Harrison441b62c2008-03-03 16:08:34 -0800907 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
909/* if this ever gets implemented, it should be done something like this:
910 struct usb_serial *serial = port->serial;
911 struct klsi_105_private *priv = usb_get_serial_port_data(port);
912 unsigned long flags;
913 int control;
914
915 spin_lock_irqsave (&priv->lock, flags);
916 if (set & TIOCM_RTS)
917 priv->control_state |= TIOCM_RTS;
918 if (set & TIOCM_DTR)
919 priv->control_state |= TIOCM_DTR;
920 if (clear & TIOCM_RTS)
921 priv->control_state &= ~TIOCM_RTS;
922 if (clear & TIOCM_DTR)
923 priv->control_state &= ~TIOCM_DTR;
924 control = priv->control_state;
925 spin_unlock_irqrestore (&priv->lock, flags);
926 retval = mct_u232_set_modem_ctrl(serial, control);
927*/
928 return retval;
929}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
931static void klsi_105_throttle (struct usb_serial_port *port)
932{
Harvey Harrison441b62c2008-03-03 16:08:34 -0800933 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 usb_kill_urb(port->read_urb);
935}
936
937static void klsi_105_unthrottle (struct usb_serial_port *port)
938{
939 int result;
940
Harvey Harrison441b62c2008-03-03 16:08:34 -0800941 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
943 port->read_urb->dev = port->serial->dev;
944 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
945 if (result)
Harvey Harrison441b62c2008-03-03 16:08:34 -0800946 err("%s - failed submitting read urb, error %d", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 result);
948}
949
950
951
952static int __init klsi_105_init (void)
953{
954 int retval;
955 retval = usb_serial_register(&kl5kusb105d_device);
956 if (retval)
957 goto failed_usb_serial_register;
958 retval = usb_register(&kl5kusb105d_driver);
959 if (retval)
960 goto failed_usb_register;
961
962 info(DRIVER_DESC " " DRIVER_VERSION);
963 return 0;
964failed_usb_register:
965 usb_serial_deregister(&kl5kusb105d_device);
966failed_usb_serial_register:
967 return retval;
968}
969
970
971static void __exit klsi_105_exit (void)
972{
973 usb_deregister (&kl5kusb105d_driver);
974 usb_serial_deregister (&kl5kusb105d_device);
975}
976
977
978module_init (klsi_105_init);
979module_exit (klsi_105_exit);
980
981MODULE_AUTHOR( DRIVER_AUTHOR );
982MODULE_DESCRIPTION( DRIVER_DESC );
983MODULE_LICENSE("GPL");
984
985
986module_param(debug, bool, S_IRUGO | S_IWUSR);
987MODULE_PARM_DESC(debug, "enable extensive debugging messages");
988
989/* vim: set sts=8 ts=8 sw=8: */