blob: f328948d74e3dfc9cf21439333ef48140eff1af9 [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>
Al Virofd05e722008-04-28 07:00:16 +010057#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#include <linux/usb.h>
Greg Kroah-Hartmana9698882006-07-11 21:22:58 -070059#include <linux/usb/serial.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include "kl5kusb105.h"
61
62static int debug;
63
64/*
65 * Version Information
66 */
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"
70
71
72/*
73 * Function prototypes
74 */
75static int klsi_105_startup (struct usb_serial *serial);
76static void klsi_105_shutdown (struct usb_serial *serial);
77static int klsi_105_open (struct usb_serial_port *port,
78 struct file *filp);
79static void klsi_105_close (struct usb_serial_port *port,
80 struct file *filp);
81static int klsi_105_write (struct usb_serial_port *port,
82 const unsigned char *buf,
83 int count);
David Howells7d12e782006-10-05 14:55:46 +010084static void klsi_105_write_bulk_callback (struct urb *urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085static int klsi_105_chars_in_buffer (struct usb_serial_port *port);
86static int klsi_105_write_room (struct usb_serial_port *port);
87
David Howells7d12e782006-10-05 14:55:46 +010088static void klsi_105_read_bulk_callback (struct urb *urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089static void klsi_105_set_termios (struct usb_serial_port *port,
Alan Cox606d0992006-12-08 02:38:45 -080090 struct ktermios *old);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091static void klsi_105_throttle (struct usb_serial_port *port);
92static void klsi_105_unthrottle (struct usb_serial_port *port);
93/*
94static void klsi_105_break_ctl (struct usb_serial_port *port,
95 int break_state );
96 */
97static int klsi_105_tiocmget (struct usb_serial_port *port,
98 struct file *file);
99static int klsi_105_tiocmset (struct usb_serial_port *port,
100 struct file *file, unsigned int set,
101 unsigned int clear);
102
103/*
104 * All of the device info needed for the KLSI converters.
105 */
106static struct usb_device_id id_table [] = {
107 { USB_DEVICE(PALMCONNECT_VID, PALMCONNECT_PID) },
108 { USB_DEVICE(KLSI_VID, KLSI_KL5KUSB105D_PID) },
109 { } /* Terminating entry */
110};
111
112MODULE_DEVICE_TABLE (usb, id_table);
113
114static struct usb_driver kl5kusb105d_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 .name = "kl5kusb105d",
116 .probe = usb_serial_probe,
117 .disconnect = usb_serial_disconnect,
118 .id_table = id_table,
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800119 .no_dynamic_id = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120};
121
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700122static struct usb_serial_driver kl5kusb105d_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700123 .driver = {
124 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700125 .name = "kl5kusb105d",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700126 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700127 .description = "KL5KUSB105D / PalmConnect",
Johannes Hölzld9b1b782006-12-17 21:50:24 +0100128 .usb_driver = &kl5kusb105d_driver,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 .id_table = id_table,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 .num_ports = 1,
131 .open = klsi_105_open,
132 .close = klsi_105_close,
133 .write = klsi_105_write,
134 .write_bulk_callback = klsi_105_write_bulk_callback,
135 .chars_in_buffer = klsi_105_chars_in_buffer,
136 .write_room = klsi_105_write_room,
137 .read_bulk_callback =klsi_105_read_bulk_callback,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 .set_termios = klsi_105_set_termios,
139 /*.break_ctl = klsi_105_break_ctl,*/
140 .tiocmget = klsi_105_tiocmget,
141 .tiocmset = klsi_105_tiocmset,
142 .attach = klsi_105_startup,
143 .shutdown = klsi_105_shutdown,
144 .throttle = klsi_105_throttle,
145 .unthrottle = klsi_105_unthrottle,
146};
147
148struct klsi_105_port_settings {
149 __u8 pktlen; /* always 5, it seems */
150 __u8 baudrate;
151 __u8 databits;
152 __u8 unknown1;
153 __u8 unknown2;
154} __attribute__ ((packed));
155
156/* we implement a pool of NUM_URBS urbs per usb_serial */
157#define NUM_URBS 1
158#define URB_TRANSFER_BUFFER_SIZE 64
159struct klsi_105_private {
160 struct klsi_105_port_settings cfg;
Alan Cox606d0992006-12-08 02:38:45 -0800161 struct ktermios termios;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 unsigned long line_state; /* modem line settings */
163 /* write pool */
164 struct urb * write_urb_pool[NUM_URBS];
165 spinlock_t lock;
166 unsigned long bytes_in;
167 unsigned long bytes_out;
168};
169
170
171/*
172 * Handle vendor specific USB requests
173 */
174
175
176#define KLSI_TIMEOUT 5000 /* default urb timeout */
177
178static int klsi_105_chg_port_settings(struct usb_serial_port *port,
179 struct klsi_105_port_settings *settings)
180{
181 int rc;
182
183 rc = usb_control_msg(port->serial->dev,
184 usb_sndctrlpipe(port->serial->dev, 0),
185 KL5KUSB105A_SIO_SET_DATA,
186 USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_INTERFACE,
187 0, /* value */
188 0, /* index */
189 settings,
190 sizeof(struct klsi_105_port_settings),
191 KLSI_TIMEOUT);
192 if (rc < 0)
193 err("Change port settings failed (error = %d)", rc);
194 info("%s - %d byte block, baudrate %x, databits %d, u1 %d, u2 %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800195 __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 settings->pktlen,
197 settings->baudrate, settings->databits,
198 settings->unknown1, settings->unknown2);
199 return rc;
200} /* klsi_105_chg_port_settings */
201
202/* translate a 16-bit status value from the device to linux's TIO bits */
203static unsigned long klsi_105_status2linestate(const __u16 status)
204{
205 unsigned long res = 0;
206
207 res = ((status & KL5KUSB105A_DSR) ? TIOCM_DSR : 0)
208 | ((status & KL5KUSB105A_CTS) ? TIOCM_CTS : 0)
209 ;
210
211 return res;
212}
213/*
214 * Read line control via vendor command and return result through
215 * *line_state_p
216 */
217/* It seems that the status buffer has always only 2 bytes length */
218#define KLSI_STATUSBUF_LEN 2
219static int klsi_105_get_line_state(struct usb_serial_port *port,
220 unsigned long *line_state_p)
221{
222 int rc;
223 __u8 status_buf[KLSI_STATUSBUF_LEN] = { -1,-1};
224 __u16 status;
225
Harvey Harrison441b62c2008-03-03 16:08:34 -0800226 info("%s - sending SIO Poll request", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 rc = usb_control_msg(port->serial->dev,
228 usb_rcvctrlpipe(port->serial->dev, 0),
229 KL5KUSB105A_SIO_POLL,
230 USB_TYPE_VENDOR | USB_DIR_IN,
231 0, /* value */
232 0, /* index */
233 status_buf, KLSI_STATUSBUF_LEN,
234 10000
235 );
236 if (rc < 0)
237 err("Reading line status failed (error = %d)", rc);
238 else {
Al Virofd05e722008-04-28 07:00:16 +0100239 status = le16_to_cpu(get_unaligned((__le16 *)status_buf));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Harvey Harrison441b62c2008-03-03 16:08:34 -0800241 info("%s - read status %x %x", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 status_buf[0], status_buf[1]);
243
244 *line_state_p = klsi_105_status2linestate(status);
245 }
246
247 return rc;
248}
249
250
251/*
252 * Driver's tty interface functions
253 */
254
255static int klsi_105_startup (struct usb_serial *serial)
256{
257 struct klsi_105_private *priv;
Oliver Neukum9306fff2007-03-29 11:23:54 +0200258 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
260 /* check if we support the product id (see keyspan.c)
261 * FIXME
262 */
263
264 /* allocate the private data structure */
265 for (i=0; i<serial->num_ports; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 priv = kmalloc(sizeof(struct klsi_105_private),
267 GFP_KERNEL);
268 if (!priv) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800269 dbg("%skmalloc for klsi_105_private failed.", __func__);
Oliver Neukum9306fff2007-03-29 11:23:54 +0200270 i--;
271 goto err_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 }
273 /* set initial values for control structures */
274 priv->cfg.pktlen = 5;
275 priv->cfg.baudrate = kl5kusb105a_sio_b9600;
276 priv->cfg.databits = kl5kusb105a_dtb_8;
277 priv->cfg.unknown1 = 0;
278 priv->cfg.unknown2 = 1;
279
280 priv->line_state = 0;
281
282 priv->bytes_in = 0;
283 priv->bytes_out = 0;
284 usb_set_serial_port_data(serial->port[i], priv);
285
286 spin_lock_init (&priv->lock);
287 for (j=0; j<NUM_URBS; j++) {
288 struct urb* urb = usb_alloc_urb(0, GFP_KERNEL);
289
290 priv->write_urb_pool[j] = urb;
291 if (urb == NULL) {
292 err("No more urbs???");
Oliver Neukum9306fff2007-03-29 11:23:54 +0200293 goto err_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 }
295
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 urb->transfer_buffer = kmalloc (URB_TRANSFER_BUFFER_SIZE,
297 GFP_KERNEL);
298 if (!urb->transfer_buffer) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800299 err("%s - out of memory for urb buffers.", __func__);
Oliver Neukum9306fff2007-03-29 11:23:54 +0200300 goto err_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 }
302 }
303
304 /* priv->termios is left uninitalized until port opening */
305 init_waitqueue_head(&serial->port[i]->write_wait);
306 }
307
Oliver Neukum9306fff2007-03-29 11:23:54 +0200308 return 0;
309
310err_cleanup:
311 for (; i >= 0; i--) {
312 priv = usb_get_serial_port_data(serial->port[i]);
313 for (j=0; j < NUM_URBS; j++) {
314 if (priv->write_urb_pool[j]) {
315 kfree(priv->write_urb_pool[j]->transfer_buffer);
316 usb_free_urb(priv->write_urb_pool[j]);
317 }
318 }
319 usb_set_serial_port_data(serial->port[i], NULL);
320 }
321 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322} /* klsi_105_startup */
323
324
325static void klsi_105_shutdown (struct usb_serial *serial)
326{
327 int i;
328
Harvey Harrison441b62c2008-03-03 16:08:34 -0800329 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
331 /* stop reads and writes on all ports */
332 for (i=0; i < serial->num_ports; ++i) {
333 struct klsi_105_private *priv = usb_get_serial_port_data(serial->port[i]);
334 unsigned long flags;
335
336 if (priv) {
337 /* kill our write urb pool */
338 int j;
339 struct urb **write_urbs = priv->write_urb_pool;
340 spin_lock_irqsave(&priv->lock,flags);
341
342 for (j = 0; j < NUM_URBS; j++) {
343 if (write_urbs[j]) {
344 /* FIXME - uncomment the following
345 * usb_kill_urb call when the host
346 * controllers get fixed to set
347 * urb->dev = NULL after the urb is
348 * finished. Otherwise this call
349 * oopses. */
350 /* usb_kill_urb(write_urbs[j]); */
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -0700351 kfree(write_urbs[j]->transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 usb_free_urb (write_urbs[j]);
353 }
354 }
355
356 spin_unlock_irqrestore (&priv->lock, flags);
357
358 kfree(priv);
359 usb_set_serial_port_data(serial->port[i], NULL);
360 }
361 }
362} /* klsi_105_shutdown */
363
364static int klsi_105_open (struct usb_serial_port *port, struct file *filp)
365{
366 struct klsi_105_private *priv = usb_get_serial_port_data(port);
367 int retval = 0;
368 int rc;
369 int i;
370 unsigned long line_state;
371 struct klsi_105_port_settings cfg;
372 unsigned long flags;
373
Harvey Harrison441b62c2008-03-03 16:08:34 -0800374 dbg("%s port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
376 /* force low_latency on so that our tty_push actually forces
377 * the data through
378 * port->tty->low_latency = 1; */
379
380 /* Do a defined restart:
381 * Set up sane default baud rate and send the 'READ_ON'
382 * vendor command.
383 * FIXME: set modem line control (how?)
384 * Then read the modem line control and store values in
385 * priv->line_state.
386 */
387 cfg.pktlen = 5;
388 cfg.baudrate = kl5kusb105a_sio_b9600;
389 cfg.databits = kl5kusb105a_dtb_8;
390 cfg.unknown1 = 0;
391 cfg.unknown2 = 1;
392 klsi_105_chg_port_settings(port, &cfg);
393
394 /* set up termios structure */
395 spin_lock_irqsave (&priv->lock, flags);
396 priv->termios.c_iflag = port->tty->termios->c_iflag;
397 priv->termios.c_oflag = port->tty->termios->c_oflag;
398 priv->termios.c_cflag = port->tty->termios->c_cflag;
399 priv->termios.c_lflag = port->tty->termios->c_lflag;
400 for (i=0; i<NCCS; i++)
401 priv->termios.c_cc[i] = port->tty->termios->c_cc[i];
402 priv->cfg.pktlen = cfg.pktlen;
403 priv->cfg.baudrate = cfg.baudrate;
404 priv->cfg.databits = cfg.databits;
405 priv->cfg.unknown1 = cfg.unknown1;
406 priv->cfg.unknown2 = cfg.unknown2;
407 spin_unlock_irqrestore (&priv->lock, flags);
408
409 /* READ_ON and urb submission */
410 usb_fill_bulk_urb(port->read_urb, port->serial->dev,
411 usb_rcvbulkpipe(port->serial->dev,
412 port->bulk_in_endpointAddress),
413 port->read_urb->transfer_buffer,
414 port->read_urb->transfer_buffer_length,
415 klsi_105_read_bulk_callback,
416 port);
417
418 rc = usb_submit_urb(port->read_urb, GFP_KERNEL);
419 if (rc) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800420 err("%s - failed submitting read urb, error %d", __func__, rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 retval = rc;
422 goto exit;
423 }
424
425 rc = usb_control_msg(port->serial->dev,
426 usb_sndctrlpipe(port->serial->dev,0),
427 KL5KUSB105A_SIO_CONFIGURE,
428 USB_TYPE_VENDOR|USB_DIR_OUT|USB_RECIP_INTERFACE,
429 KL5KUSB105A_SIO_CONFIGURE_READ_ON,
430 0, /* index */
431 NULL,
432 0,
433 KLSI_TIMEOUT);
434 if (rc < 0) {
435 err("Enabling read failed (error = %d)", rc);
436 retval = rc;
437 } else
Harvey Harrison441b62c2008-03-03 16:08:34 -0800438 dbg("%s - enabled reading", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
440 rc = klsi_105_get_line_state(port, &line_state);
441 if (rc >= 0) {
442 spin_lock_irqsave (&priv->lock, flags);
443 priv->line_state = line_state;
444 spin_unlock_irqrestore (&priv->lock, flags);
Harvey Harrison441b62c2008-03-03 16:08:34 -0800445 dbg("%s - read line state 0x%lx", __func__, line_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 retval = 0;
447 } else
448 retval = rc;
449
450exit:
451 return retval;
452} /* klsi_105_open */
453
454
455static void klsi_105_close (struct usb_serial_port *port, struct file *filp)
456{
457 struct klsi_105_private *priv = usb_get_serial_port_data(port);
458 int rc;
459
Harvey Harrison441b62c2008-03-03 16:08:34 -0800460 dbg("%s port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
Oliver Neukum3edbc982008-01-22 12:47:15 +0100462 mutex_lock(&port->serial->disc_mutex);
463 if (!port->serial->disconnected) {
464 /* send READ_OFF */
465 rc = usb_control_msg (port->serial->dev,
466 usb_sndctrlpipe(port->serial->dev, 0),
467 KL5KUSB105A_SIO_CONFIGURE,
468 USB_TYPE_VENDOR | USB_DIR_OUT,
469 KL5KUSB105A_SIO_CONFIGURE_READ_OFF,
470 0, /* index */
471 NULL, 0,
472 KLSI_TIMEOUT);
473 if (rc < 0)
474 err("Disabling read failed (error = %d)", rc);
475 }
476 mutex_unlock(&port->serial->disc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
478 /* shutdown our bulk reads and writes */
479 usb_kill_urb(port->write_urb);
480 usb_kill_urb(port->read_urb);
481 /* unlink our write pool */
482 /* FIXME */
483 /* wgg - do I need this? I think so. */
484 usb_kill_urb(port->interrupt_in_urb);
485 info("kl5kusb105 port stats: %ld bytes in, %ld bytes out", priv->bytes_in, priv->bytes_out);
486} /* klsi_105_close */
487
488
489/* We need to write a complete 64-byte data block and encode the
490 * number actually sent in the first double-byte, LSB-order. That
491 * leaves at most 62 bytes of payload.
492 */
493#define KLSI_105_DATA_OFFSET 2 /* in the bulk urb data block */
494
495
496static int klsi_105_write (struct usb_serial_port *port,
497 const unsigned char *buf, int count)
498{
499 struct klsi_105_private *priv = usb_get_serial_port_data(port);
500 int result, size;
501 int bytes_sent=0;
502
Harvey Harrison441b62c2008-03-03 16:08:34 -0800503 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
505 while (count > 0) {
506 /* try to find a free urb (write 0 bytes if none) */
507 struct urb *urb = NULL;
508 unsigned long flags;
509 int i;
510 /* since the pool is per-port we might not need the spin lock !? */
511 spin_lock_irqsave (&priv->lock, flags);
512 for (i=0; i<NUM_URBS; i++) {
513 if (priv->write_urb_pool[i]->status != -EINPROGRESS) {
514 urb = priv->write_urb_pool[i];
Harvey Harrison441b62c2008-03-03 16:08:34 -0800515 dbg("%s - using pool URB %d", __func__, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 break;
517 }
518 }
519 spin_unlock_irqrestore (&priv->lock, flags);
520
521 if (urb==NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800522 dbg("%s - no more free urbs", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 goto exit;
524 }
525
526 if (urb->transfer_buffer == NULL) {
527 urb->transfer_buffer = kmalloc (URB_TRANSFER_BUFFER_SIZE, GFP_ATOMIC);
528 if (urb->transfer_buffer == NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800529 err("%s - no more kernel memory...", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 goto exit;
531 }
532 }
533
534 size = min (count, port->bulk_out_size - KLSI_105_DATA_OFFSET);
535 size = min (size, URB_TRANSFER_BUFFER_SIZE - KLSI_105_DATA_OFFSET);
536
537 memcpy (urb->transfer_buffer + KLSI_105_DATA_OFFSET, buf, size);
538
539 /* write payload size into transfer buffer */
540 ((__u8 *)urb->transfer_buffer)[0] = (__u8) (size & 0xFF);
541 ((__u8 *)urb->transfer_buffer)[1] = (__u8) ((size & 0xFF00)>>8);
542
543 /* set up our urb */
544 usb_fill_bulk_urb(urb, port->serial->dev,
545 usb_sndbulkpipe(port->serial->dev,
546 port->bulk_out_endpointAddress),
547 urb->transfer_buffer,
548 URB_TRANSFER_BUFFER_SIZE,
549 klsi_105_write_bulk_callback,
550 port);
551
552 /* send the data out the bulk port */
553 result = usb_submit_urb(urb, GFP_ATOMIC);
554 if (result) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800555 err("%s - failed submitting write urb, error %d", __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 goto exit;
557 }
558 buf += size;
559 bytes_sent += size;
560 count -= size;
561 }
562exit:
563 /* lockless, but it's for debug info only... */
564 priv->bytes_out+=bytes_sent;
565
566 return bytes_sent; /* that's how much we wrote */
567} /* klsi_105_write */
568
David Howells7d12e782006-10-05 14:55:46 +0100569static void klsi_105_write_bulk_callback ( struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570{
Ming Leicdc97792008-02-24 18:41:47 +0800571 struct usb_serial_port *port = urb->context;
Greg Kroah-Hartman17c1b352007-06-15 15:44:13 -0700572 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
Harvey Harrison441b62c2008-03-03 16:08:34 -0800574 dbg("%s - port %d", __func__, port->number);
Greg Kroah-Hartman17c1b352007-06-15 15:44:13 -0700575
576 if (status) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800577 dbg("%s - nonzero write bulk status received: %d", __func__,
Greg Kroah-Hartman17c1b352007-06-15 15:44:13 -0700578 status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 return;
580 }
581
Pete Zaitcevcf2c7482006-05-22 21:58:49 -0700582 usb_serial_port_softint(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583} /* klsi_105_write_bulk_completion_callback */
584
585
586/* return number of characters currently in the writing process */
587static int klsi_105_chars_in_buffer (struct usb_serial_port *port)
588{
589 int chars = 0;
590 int i;
591 unsigned long flags;
592 struct klsi_105_private *priv = usb_get_serial_port_data(port);
593
594 spin_lock_irqsave (&priv->lock, flags);
595
596 for (i = 0; i < NUM_URBS; ++i) {
597 if (priv->write_urb_pool[i]->status == -EINPROGRESS) {
598 chars += URB_TRANSFER_BUFFER_SIZE;
599 }
600 }
601
602 spin_unlock_irqrestore (&priv->lock, flags);
603
Harvey Harrison441b62c2008-03-03 16:08:34 -0800604 dbg("%s - returns %d", __func__, chars);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 return (chars);
606}
607
608static int klsi_105_write_room (struct usb_serial_port *port)
609{
610 unsigned long flags;
611 int i;
612 int room = 0;
613 struct klsi_105_private *priv = usb_get_serial_port_data(port);
614
615 spin_lock_irqsave (&priv->lock, flags);
616 for (i = 0; i < NUM_URBS; ++i) {
617 if (priv->write_urb_pool[i]->status != -EINPROGRESS) {
618 room += URB_TRANSFER_BUFFER_SIZE;
619 }
620 }
621
622 spin_unlock_irqrestore (&priv->lock, flags);
623
Harvey Harrison441b62c2008-03-03 16:08:34 -0800624 dbg("%s - returns %d", __func__, room);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 return (room);
626}
627
628
629
David Howells7d12e782006-10-05 14:55:46 +0100630static void klsi_105_read_bulk_callback (struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631{
Ming Leicdc97792008-02-24 18:41:47 +0800632 struct usb_serial_port *port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 struct klsi_105_private *priv = usb_get_serial_port_data(port);
634 struct tty_struct *tty;
635 unsigned char *data = urb->transfer_buffer;
636 int rc;
Greg Kroah-Hartman17c1b352007-06-15 15:44:13 -0700637 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Harvey Harrison441b62c2008-03-03 16:08:34 -0800639 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
641 /* The urb might have been killed. */
Greg Kroah-Hartman17c1b352007-06-15 15:44:13 -0700642 if (status) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800643 dbg("%s - nonzero read bulk status received: %d", __func__,
Greg Kroah-Hartman17c1b352007-06-15 15:44:13 -0700644 status);
645 return;
646 }
647
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 /* The data received is again preceded by a length double-byte in LSB-
649 * first order (see klsi_105_write() )
650 */
651 if (urb->actual_length == 0) {
652 /* empty urbs seem to happen, we ignore them */
Harvey Harrison441b62c2008-03-03 16:08:34 -0800653 /* dbg("%s - emtpy URB", __func__); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 ;
655 } else if (urb->actual_length <= 2) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800656 dbg("%s - size %d URB not understood", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 urb->actual_length);
Harvey Harrison441b62c2008-03-03 16:08:34 -0800658 usb_serial_debug_data(debug, &port->dev, __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 urb->actual_length, data);
660 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 int bytes_sent = ((__u8 *) data)[0] +
662 ((unsigned int) ((__u8 *) data)[1] << 8);
663 tty = port->tty;
664 /* we should immediately resubmit the URB, before attempting
665 * to pass the data on to the tty layer. But that needs locking
666 * against re-entry an then mixed-up data because of
667 * intermixed tty_flip_buffer_push()s
668 * FIXME
669 */
Harvey Harrison441b62c2008-03-03 16:08:34 -0800670 usb_serial_debug_data(debug, &port->dev, __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 urb->actual_length, data);
672
673 if (bytes_sent + 2 > urb->actual_length) {
674 dbg("%s - trying to read more data than available"
Harvey Harrison441b62c2008-03-03 16:08:34 -0800675 " (%d vs. %d)", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 bytes_sent+2, urb->actual_length);
677 /* cap at implied limit */
678 bytes_sent = urb->actual_length - 2;
679 }
680
Alan Cox33f0f882006-01-09 20:54:13 -0800681 tty_buffer_request_room(tty, bytes_sent);
682 tty_insert_flip_string(tty, data + 2, bytes_sent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 tty_flip_buffer_push(tty);
684
685 /* again lockless, but debug info only */
686 priv->bytes_in += bytes_sent;
687 }
688 /* Continue trying to always read */
689 usb_fill_bulk_urb(port->read_urb, port->serial->dev,
690 usb_rcvbulkpipe(port->serial->dev,
691 port->bulk_in_endpointAddress),
692 port->read_urb->transfer_buffer,
693 port->read_urb->transfer_buffer_length,
694 klsi_105_read_bulk_callback,
695 port);
696 rc = usb_submit_urb(port->read_urb, GFP_ATOMIC);
697 if (rc)
Harvey Harrison441b62c2008-03-03 16:08:34 -0800698 err("%s - failed resubmitting read urb, error %d", __func__, rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699} /* klsi_105_read_bulk_callback */
700
701
702static void klsi_105_set_termios (struct usb_serial_port *port,
Alan Cox606d0992006-12-08 02:38:45 -0800703 struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704{
705 struct klsi_105_private *priv = usb_get_serial_port_data(port);
Alan Coxa5b6f602008-04-08 17:16:06 +0100706 struct tty_struct *tty = port->tty;
707 unsigned int iflag = tty->termios->c_iflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 unsigned int old_iflag = old_termios->c_iflag;
Alan Coxa5b6f602008-04-08 17:16:06 +0100709 unsigned int cflag = tty->termios->c_cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 unsigned int old_cflag = old_termios->c_cflag;
711 struct klsi_105_port_settings cfg;
712 unsigned long flags;
Alan Coxa5b6f602008-04-08 17:16:06 +0100713 speed_t baud;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
715 /* lock while we are modifying the settings */
716 spin_lock_irqsave (&priv->lock, flags);
717
718 /*
719 * Update baud rate
720 */
Alan Coxa5b6f602008-04-08 17:16:06 +0100721 baud = tty_get_baud_rate(tty);
722
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 if( (cflag & CBAUD) != (old_cflag & CBAUD) ) {
724 /* reassert DTR and (maybe) RTS on transition from B0 */
725 if( (old_cflag & CBAUD) == B0 ) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800726 dbg("%s: baud was B0", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727#if 0
728 priv->control_state |= TIOCM_DTR;
729 /* don't set RTS if using hardware flow control */
730 if (!(old_cflag & CRTSCTS)) {
731 priv->control_state |= TIOCM_RTS;
732 }
733 mct_u232_set_modem_ctrl(serial, priv->control_state);
734#endif
735 }
Alan Coxa5b6f602008-04-08 17:16:06 +0100736 }
737 switch(baud) {
Alan Cox3f6ff6e2007-08-10 14:53:34 -0700738 case 0: /* handled below */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 break;
Alan Cox3f6ff6e2007-08-10 14:53:34 -0700740 case 1200:
741 priv->cfg.baudrate = kl5kusb105a_sio_b1200;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 break;
Alan Cox3f6ff6e2007-08-10 14:53:34 -0700743 case 2400:
744 priv->cfg.baudrate = kl5kusb105a_sio_b2400;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 break;
Alan Cox3f6ff6e2007-08-10 14:53:34 -0700746 case 4800:
747 priv->cfg.baudrate = kl5kusb105a_sio_b4800;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 break;
Alan Cox3f6ff6e2007-08-10 14:53:34 -0700749 case 9600:
750 priv->cfg.baudrate = kl5kusb105a_sio_b9600;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 break;
Alan Cox3f6ff6e2007-08-10 14:53:34 -0700752 case 19200:
753 priv->cfg.baudrate = kl5kusb105a_sio_b19200;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 break;
Alan Cox3f6ff6e2007-08-10 14:53:34 -0700755 case 38400:
756 priv->cfg.baudrate = kl5kusb105a_sio_b38400;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 break;
Alan Cox3f6ff6e2007-08-10 14:53:34 -0700758 case 57600:
759 priv->cfg.baudrate = kl5kusb105a_sio_b57600;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 break;
Alan Cox3f6ff6e2007-08-10 14:53:34 -0700761 case 115200:
762 priv->cfg.baudrate = kl5kusb105a_sio_b115200;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 break;
764 default:
Alan Coxa5b6f602008-04-08 17:16:06 +0100765 dbg("KLSI USB->Serial converter:"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 " unsupported baudrate request, using default"
767 " of 9600");
768 priv->cfg.baudrate = kl5kusb105a_sio_b9600;
Alan Coxa5b6f602008-04-08 17:16:06 +0100769 baud = 9600;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 }
Alan Coxa5b6f602008-04-08 17:16:06 +0100772 if ((cflag & CBAUD) == B0 ) {
773 dbg("%s: baud is B0", __func__);
774 /* Drop RTS and DTR */
775 /* maybe this should be simulated by sending read
776 * disable and read enable messages?
777 */
778 ;
779#if 0
780 priv->control_state &= ~(TIOCM_DTR | TIOCM_RTS);
781 mct_u232_set_modem_ctrl(serial, priv->control_state);
782#endif
783 }
784 tty_encode_baud_rate(tty, baud, baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
786 if ((cflag & CSIZE) != (old_cflag & CSIZE)) {
787 /* set the number of data bits */
788 switch (cflag & CSIZE) {
789 case CS5:
Harvey Harrison441b62c2008-03-03 16:08:34 -0800790 dbg("%s - 5 bits/byte not supported", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 spin_unlock_irqrestore (&priv->lock, flags);
792 return ;
793 case CS6:
Harvey Harrison441b62c2008-03-03 16:08:34 -0800794 dbg("%s - 6 bits/byte not supported", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 spin_unlock_irqrestore (&priv->lock, flags);
796 return ;
797 case CS7:
798 priv->cfg.databits = kl5kusb105a_dtb_7;
799 break;
800 case CS8:
801 priv->cfg.databits = kl5kusb105a_dtb_8;
802 break;
803 default:
804 err("CSIZE was not CS5-CS8, using default of 8");
805 priv->cfg.databits = kl5kusb105a_dtb_8;
806 break;
807 }
808 }
809
810 /*
811 * Update line control register (LCR)
812 */
813 if ((cflag & (PARENB|PARODD)) != (old_cflag & (PARENB|PARODD))
814 || (cflag & CSTOPB) != (old_cflag & CSTOPB) ) {
815
Alan Coxa5b6f602008-04-08 17:16:06 +0100816 /* Not currently supported */
817 tty->termios->c_cflag &= ~(PARENB|PARODD|CSTOPB);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818#if 0
819 priv->last_lcr = 0;
820
821 /* set the parity */
822 if (cflag & PARENB)
823 priv->last_lcr |= (cflag & PARODD) ?
824 MCT_U232_PARITY_ODD : MCT_U232_PARITY_EVEN;
825 else
826 priv->last_lcr |= MCT_U232_PARITY_NONE;
827
828 /* set the number of stop bits */
829 priv->last_lcr |= (cflag & CSTOPB) ?
830 MCT_U232_STOP_BITS_2 : MCT_U232_STOP_BITS_1;
831
832 mct_u232_set_line_ctrl(serial, priv->last_lcr);
833#endif
834 ;
835 }
836
837 /*
838 * Set flow control: well, I do not really now how to handle DTR/RTS.
839 * Just do what we have seen with SniffUSB on Win98.
840 */
841 if( (iflag & IXOFF) != (old_iflag & IXOFF)
842 || (iflag & IXON) != (old_iflag & IXON)
843 || (cflag & CRTSCTS) != (old_cflag & CRTSCTS) ) {
844
Alan Coxa5b6f602008-04-08 17:16:06 +0100845 /* Not currently supported */
846 tty->termios->c_cflag &= ~CRTSCTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 /* Drop DTR/RTS if no flow control otherwise assert */
848#if 0
849 if ((iflag & IXOFF) || (iflag & IXON) || (cflag & CRTSCTS) )
850 priv->control_state |= TIOCM_DTR | TIOCM_RTS;
851 else
852 priv->control_state &= ~(TIOCM_DTR | TIOCM_RTS);
853 mct_u232_set_modem_ctrl(serial, priv->control_state);
854#endif
855 ;
856 }
857 memcpy (&cfg, &priv->cfg, sizeof(cfg));
858 spin_unlock_irqrestore (&priv->lock, flags);
859
860 /* now commit changes to device */
861 klsi_105_chg_port_settings(port, &cfg);
862} /* klsi_105_set_termios */
863
864
865#if 0
866static void mct_u232_break_ctl( struct usb_serial_port *port, int break_state )
867{
868 struct usb_serial *serial = port->serial;
869 struct mct_u232_private *priv = (struct mct_u232_private *)port->private;
870 unsigned char lcr = priv->last_lcr;
871
Harvey Harrison441b62c2008-03-03 16:08:34 -0800872 dbg("%sstate=%d", __func__, break_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
874 if (break_state)
875 lcr |= MCT_U232_SET_BREAK;
876
877 mct_u232_set_line_ctrl(serial, lcr);
878} /* mct_u232_break_ctl */
879#endif
880
881static int klsi_105_tiocmget (struct usb_serial_port *port, struct file *file)
882{
883 struct klsi_105_private *priv = usb_get_serial_port_data(port);
884 unsigned long flags;
885 int rc;
886 unsigned long line_state;
Harvey Harrison441b62c2008-03-03 16:08:34 -0800887 dbg("%s - request, just guessing", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
889 rc = klsi_105_get_line_state(port, &line_state);
890 if (rc < 0) {
891 err("Reading line control failed (error = %d)", rc);
892 /* better return value? EAGAIN? */
893 return rc;
894 }
895
896 spin_lock_irqsave (&priv->lock, flags);
897 priv->line_state = line_state;
898 spin_unlock_irqrestore (&priv->lock, flags);
Harvey Harrison441b62c2008-03-03 16:08:34 -0800899 dbg("%s - read line state 0x%lx", __func__, line_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 return (int)line_state;
901}
902
903static int klsi_105_tiocmset (struct usb_serial_port *port, struct file *file,
904 unsigned int set, unsigned int clear)
905{
906 int retval = -EINVAL;
907
Harvey Harrison441b62c2008-03-03 16:08:34 -0800908 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
910/* if this ever gets implemented, it should be done something like this:
911 struct usb_serial *serial = port->serial;
912 struct klsi_105_private *priv = usb_get_serial_port_data(port);
913 unsigned long flags;
914 int control;
915
916 spin_lock_irqsave (&priv->lock, flags);
917 if (set & TIOCM_RTS)
918 priv->control_state |= TIOCM_RTS;
919 if (set & TIOCM_DTR)
920 priv->control_state |= TIOCM_DTR;
921 if (clear & TIOCM_RTS)
922 priv->control_state &= ~TIOCM_RTS;
923 if (clear & TIOCM_DTR)
924 priv->control_state &= ~TIOCM_DTR;
925 control = priv->control_state;
926 spin_unlock_irqrestore (&priv->lock, flags);
927 retval = mct_u232_set_modem_ctrl(serial, control);
928*/
929 return retval;
930}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
932static void klsi_105_throttle (struct usb_serial_port *port)
933{
Harvey Harrison441b62c2008-03-03 16:08:34 -0800934 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 usb_kill_urb(port->read_urb);
936}
937
938static void klsi_105_unthrottle (struct usb_serial_port *port)
939{
940 int result;
941
Harvey Harrison441b62c2008-03-03 16:08:34 -0800942 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
944 port->read_urb->dev = port->serial->dev;
945 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
946 if (result)
Harvey Harrison441b62c2008-03-03 16:08:34 -0800947 err("%s - failed submitting read urb, error %d", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 result);
949}
950
951
952
953static int __init klsi_105_init (void)
954{
955 int retval;
956 retval = usb_serial_register(&kl5kusb105d_device);
957 if (retval)
958 goto failed_usb_serial_register;
959 retval = usb_register(&kl5kusb105d_driver);
960 if (retval)
961 goto failed_usb_register;
962
963 info(DRIVER_DESC " " DRIVER_VERSION);
964 return 0;
965failed_usb_register:
966 usb_serial_deregister(&kl5kusb105d_device);
967failed_usb_serial_register:
968 return retval;
969}
970
971
972static void __exit klsi_105_exit (void)
973{
974 usb_deregister (&kl5kusb105d_driver);
975 usb_serial_deregister (&kl5kusb105d_device);
976}
977
978
979module_init (klsi_105_init);
980module_exit (klsi_105_exit);
981
982MODULE_AUTHOR( DRIVER_AUTHOR );
983MODULE_DESCRIPTION( DRIVER_DESC );
984MODULE_LICENSE("GPL");
985
986
987module_param(debug, bool, S_IRUGO | S_IWUSR);
988MODULE_PARM_DESC(debug, "enable extensive debugging messages");
989
990/* vim: set sts=8 ts=8 sw=8: */