blob: 0f44bb8e8d4f64a0400b969da4d8aa80760a318c [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 *
Alan Cox0c265f42008-07-22 11:14:00 +010018 * It seems that KLSI bought some silicon-design information from ScanLogic,
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 * 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
Alan Cox0c265f42008-07-22 11:14:00 +010022 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 * 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 *
Alan Cox0c265f42008-07-22 11:14:00 +010043 * The driver skeleton is mainly based on mct_u232.c and various other
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 * 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>
Alan Cox0c265f42008-07-22 11:14:00 +010056#include <linux/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 */
Alan Cox0c265f42008-07-22 11:14:00 +010075static int klsi_105_startup(struct usb_serial *serial);
Alan Sternf9c99bb2009-06-02 11:53:55 -040076static void klsi_105_disconnect(struct usb_serial *serial);
77static void klsi_105_release(struct usb_serial *serial);
Alan Cox0c265f42008-07-22 11:14:00 +010078static int klsi_105_open(struct tty_struct *tty,
79 struct usb_serial_port *port, struct file *filp);
Alan Cox335f8512009-06-11 12:26:29 +010080static void klsi_105_close(struct usb_serial_port *port);
Alan Cox0c265f42008-07-22 11:14:00 +010081static int klsi_105_write(struct tty_struct *tty,
82 struct usb_serial_port *port, const unsigned char *buf, int count);
83static void klsi_105_write_bulk_callback(struct urb *urb);
84static int klsi_105_chars_in_buffer(struct tty_struct *tty);
85static int klsi_105_write_room(struct tty_struct *tty);
86static void klsi_105_read_bulk_callback(struct urb *urb);
87static void klsi_105_set_termios(struct tty_struct *tty,
88 struct usb_serial_port *port, struct ktermios *old);
89static void klsi_105_throttle(struct tty_struct *tty);
90static void klsi_105_unthrottle(struct tty_struct *tty);
91static int klsi_105_tiocmget(struct tty_struct *tty, struct file *file);
92static int klsi_105_tiocmset(struct tty_struct *tty, struct file *file,
93 unsigned int set, unsigned int clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
95/*
96 * All of the device info needed for the KLSI converters.
97 */
98static struct usb_device_id id_table [] = {
99 { USB_DEVICE(PALMCONNECT_VID, PALMCONNECT_PID) },
100 { USB_DEVICE(KLSI_VID, KLSI_KL5KUSB105D_PID) },
101 { } /* Terminating entry */
102};
103
Alan Cox0c265f42008-07-22 11:14:00 +0100104MODULE_DEVICE_TABLE(usb, id_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106static struct usb_driver kl5kusb105d_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 .name = "kl5kusb105d",
108 .probe = usb_serial_probe,
109 .disconnect = usb_serial_disconnect,
110 .id_table = id_table,
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800111 .no_dynamic_id = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112};
113
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700114static struct usb_serial_driver kl5kusb105d_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700115 .driver = {
116 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700117 .name = "kl5kusb105d",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700118 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700119 .description = "KL5KUSB105D / PalmConnect",
Johannes Hölzld9b1b782006-12-17 21:50:24 +0100120 .usb_driver = &kl5kusb105d_driver,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 .id_table = id_table,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 .num_ports = 1,
123 .open = klsi_105_open,
124 .close = klsi_105_close,
125 .write = klsi_105_write,
126 .write_bulk_callback = klsi_105_write_bulk_callback,
127 .chars_in_buffer = klsi_105_chars_in_buffer,
128 .write_room = klsi_105_write_room,
Alan Cox0c265f42008-07-22 11:14:00 +0100129 .read_bulk_callback = klsi_105_read_bulk_callback,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 .set_termios = klsi_105_set_termios,
131 /*.break_ctl = klsi_105_break_ctl,*/
132 .tiocmget = klsi_105_tiocmget,
133 .tiocmset = klsi_105_tiocmset,
134 .attach = klsi_105_startup,
Alan Sternf9c99bb2009-06-02 11:53:55 -0400135 .disconnect = klsi_105_disconnect,
136 .release = klsi_105_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 .throttle = klsi_105_throttle,
138 .unthrottle = klsi_105_unthrottle,
139};
140
141struct klsi_105_port_settings {
142 __u8 pktlen; /* always 5, it seems */
143 __u8 baudrate;
144 __u8 databits;
145 __u8 unknown1;
146 __u8 unknown2;
147} __attribute__ ((packed));
148
149/* we implement a pool of NUM_URBS urbs per usb_serial */
150#define NUM_URBS 1
151#define URB_TRANSFER_BUFFER_SIZE 64
152struct klsi_105_private {
153 struct klsi_105_port_settings cfg;
Alan Cox606d0992006-12-08 02:38:45 -0800154 struct ktermios termios;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 unsigned long line_state; /* modem line settings */
156 /* write pool */
Alan Cox0c265f42008-07-22 11:14:00 +0100157 struct urb *write_urb_pool[NUM_URBS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 spinlock_t lock;
159 unsigned long bytes_in;
160 unsigned long bytes_out;
161};
162
163
164/*
165 * Handle vendor specific USB requests
166 */
167
168
169#define KLSI_TIMEOUT 5000 /* default urb timeout */
170
171static int klsi_105_chg_port_settings(struct usb_serial_port *port,
172 struct klsi_105_port_settings *settings)
173{
174 int rc;
175
Alan Cox0c265f42008-07-22 11:14:00 +0100176 rc = usb_control_msg(port->serial->dev,
177 usb_sndctrlpipe(port->serial->dev, 0),
178 KL5KUSB105A_SIO_SET_DATA,
179 USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_INTERFACE,
180 0, /* value */
181 0, /* index */
182 settings,
183 sizeof(struct klsi_105_port_settings),
184 KLSI_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 if (rc < 0)
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700186 dev_err(&port->dev,
187 "Change port settings failed (error = %d)\n", rc);
Greg Kroah-Hartmanc197a8d2008-08-18 13:21:04 -0700188 dev_info(&port->serial->dev->dev,
189 "%d byte block, baudrate %x, databits %d, u1 %d, u2 %d\n",
190 settings->pktlen, settings->baudrate, settings->databits,
191 settings->unknown1, settings->unknown2);
Alan Cox0c265f42008-07-22 11:14:00 +0100192 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193} /* klsi_105_chg_port_settings */
194
195/* translate a 16-bit status value from the device to linux's TIO bits */
196static unsigned long klsi_105_status2linestate(const __u16 status)
197{
198 unsigned long res = 0;
199
200 res = ((status & KL5KUSB105A_DSR) ? TIOCM_DSR : 0)
201 | ((status & KL5KUSB105A_CTS) ? TIOCM_CTS : 0)
202 ;
203
204 return res;
205}
Alan Cox0c265f42008-07-22 11:14:00 +0100206/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 * Read line control via vendor command and return result through
Alan Cox0c265f42008-07-22 11:14:00 +0100208 * *line_state_p
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 */
210/* It seems that the status buffer has always only 2 bytes length */
211#define KLSI_STATUSBUF_LEN 2
212static int klsi_105_get_line_state(struct usb_serial_port *port,
213 unsigned long *line_state_p)
214{
215 int rc;
Alan Cox0c265f42008-07-22 11:14:00 +0100216 __u8 status_buf[KLSI_STATUSBUF_LEN] = { -1, -1};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 __u16 status;
218
Greg Kroah-Hartmanc197a8d2008-08-18 13:21:04 -0700219 dev_info(&port->serial->dev->dev, "sending SIO Poll request\n");
Alan Cox0c265f42008-07-22 11:14:00 +0100220 rc = usb_control_msg(port->serial->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 usb_rcvctrlpipe(port->serial->dev, 0),
222 KL5KUSB105A_SIO_POLL,
Alan Cox0c265f42008-07-22 11:14:00 +0100223 USB_TYPE_VENDOR | USB_DIR_IN,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 0, /* value */
225 0, /* index */
226 status_buf, KLSI_STATUSBUF_LEN,
227 10000
228 );
229 if (rc < 0)
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700230 dev_err(&port->dev, "Reading line status failed (error = %d)\n",
231 rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 else {
Harvey Harrisonbd2c7842008-05-01 20:52:57 -0700233 status = get_unaligned_le16(status_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Greg Kroah-Hartmanc197a8d2008-08-18 13:21:04 -0700235 dev_info(&port->serial->dev->dev, "read status %x %x",
236 status_buf[0], status_buf[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
238 *line_state_p = klsi_105_status2linestate(status);
239 }
Alan Cox0c265f42008-07-22 11:14:00 +0100240 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241}
242
243
244/*
245 * Driver's tty interface functions
246 */
247
Alan Cox0c265f42008-07-22 11:14:00 +0100248static int klsi_105_startup(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
250 struct klsi_105_private *priv;
Oliver Neukum9306fff2007-03-29 11:23:54 +0200251 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
253 /* check if we support the product id (see keyspan.c)
254 * FIXME
255 */
256
257 /* allocate the private data structure */
Alan Cox0c265f42008-07-22 11:14:00 +0100258 for (i = 0; i < serial->num_ports; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 priv = kmalloc(sizeof(struct klsi_105_private),
260 GFP_KERNEL);
261 if (!priv) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800262 dbg("%skmalloc for klsi_105_private failed.", __func__);
Oliver Neukum9306fff2007-03-29 11:23:54 +0200263 i--;
264 goto err_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 }
266 /* set initial values for control structures */
267 priv->cfg.pktlen = 5;
268 priv->cfg.baudrate = kl5kusb105a_sio_b9600;
269 priv->cfg.databits = kl5kusb105a_dtb_8;
270 priv->cfg.unknown1 = 0;
271 priv->cfg.unknown2 = 1;
272
273 priv->line_state = 0;
274
275 priv->bytes_in = 0;
276 priv->bytes_out = 0;
277 usb_set_serial_port_data(serial->port[i], priv);
278
Alan Cox0c265f42008-07-22 11:14:00 +0100279 spin_lock_init(&priv->lock);
280 for (j = 0; j < NUM_URBS; j++) {
281 struct urb *urb = usb_alloc_urb(0, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
283 priv->write_urb_pool[j] = urb;
284 if (urb == NULL) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700285 dev_err(&serial->dev->dev, "No more urbs???\n");
Oliver Neukum9306fff2007-03-29 11:23:54 +0200286 goto err_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 }
288
Alan Cox0c265f42008-07-22 11:14:00 +0100289 urb->transfer_buffer =
290 kmalloc(URB_TRANSFER_BUFFER_SIZE, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 if (!urb->transfer_buffer) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700292 dev_err(&serial->dev->dev,
293 "%s - out of memory for urb buffers.\n",
294 __func__);
Oliver Neukum9306fff2007-03-29 11:23:54 +0200295 goto err_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 }
297 }
298
299 /* priv->termios is left uninitalized until port opening */
300 init_waitqueue_head(&serial->port[i]->write_wait);
301 }
Alan Cox0c265f42008-07-22 11:14:00 +0100302
Oliver Neukum9306fff2007-03-29 11:23:54 +0200303 return 0;
304
305err_cleanup:
306 for (; i >= 0; i--) {
307 priv = usb_get_serial_port_data(serial->port[i]);
Alan Cox0c265f42008-07-22 11:14:00 +0100308 for (j = 0; j < NUM_URBS; j++) {
Oliver Neukum9306fff2007-03-29 11:23:54 +0200309 if (priv->write_urb_pool[j]) {
310 kfree(priv->write_urb_pool[j]->transfer_buffer);
311 usb_free_urb(priv->write_urb_pool[j]);
312 }
313 }
314 usb_set_serial_port_data(serial->port[i], NULL);
315 }
316 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317} /* klsi_105_startup */
318
319
Alan Sternf9c99bb2009-06-02 11:53:55 -0400320static void klsi_105_disconnect(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321{
322 int i;
Alan Cox0c265f42008-07-22 11:14:00 +0100323
Harvey Harrison441b62c2008-03-03 16:08:34 -0800324 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
326 /* stop reads and writes on all ports */
Alan Cox0c265f42008-07-22 11:14:00 +0100327 for (i = 0; i < serial->num_ports; ++i) {
328 struct klsi_105_private *priv =
329 usb_get_serial_port_data(serial->port[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
331 if (priv) {
332 /* kill our write urb pool */
333 int j;
334 struct urb **write_urbs = priv->write_urb_pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
336 for (j = 0; j < NUM_URBS; j++) {
337 if (write_urbs[j]) {
Alan Sternf9c99bb2009-06-02 11:53:55 -0400338 usb_kill_urb(write_urbs[j]);
Alan Cox0c265f42008-07-22 11:14:00 +0100339 usb_free_urb(write_urbs[j]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 }
341 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 }
343 }
Alan Sternf9c99bb2009-06-02 11:53:55 -0400344} /* klsi_105_disconnect */
345
346
347static void klsi_105_release(struct usb_serial *serial)
348{
349 int i;
350
351 dbg("%s", __func__);
352
353 for (i = 0; i < serial->num_ports; ++i) {
354 struct klsi_105_private *priv =
355 usb_get_serial_port_data(serial->port[i]);
356
357 kfree(priv);
358 }
359} /* klsi_105_release */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
Alan Cox95da3102008-07-22 11:09:07 +0100361static int klsi_105_open(struct tty_struct *tty,
362 struct usb_serial_port *port, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363{
364 struct klsi_105_private *priv = usb_get_serial_port_data(port);
365 int retval = 0;
366 int rc;
367 int i;
368 unsigned long line_state;
369 struct klsi_105_port_settings cfg;
370 unsigned long flags;
371
Harvey Harrison441b62c2008-03-03 16:08:34 -0800372 dbg("%s port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
374 /* force low_latency on so that our tty_push actually forces
375 * the data through
Alan Cox95da3102008-07-22 11:09:07 +0100376 * tty->low_latency = 1; */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
378 /* Do a defined restart:
379 * Set up sane default baud rate and send the 'READ_ON'
Alan Cox0c265f42008-07-22 11:14:00 +0100380 * vendor command.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 * FIXME: set modem line control (how?)
382 * Then read the modem line control and store values in
383 * priv->line_state.
384 */
385 cfg.pktlen = 5;
386 cfg.baudrate = kl5kusb105a_sio_b9600;
387 cfg.databits = kl5kusb105a_dtb_8;
388 cfg.unknown1 = 0;
389 cfg.unknown2 = 1;
390 klsi_105_chg_port_settings(port, &cfg);
Alan Cox0c265f42008-07-22 11:14:00 +0100391
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 /* set up termios structure */
Alan Cox0c265f42008-07-22 11:14:00 +0100393 spin_lock_irqsave(&priv->lock, flags);
Alan Cox95da3102008-07-22 11:09:07 +0100394 priv->termios.c_iflag = tty->termios->c_iflag;
395 priv->termios.c_oflag = tty->termios->c_oflag;
396 priv->termios.c_cflag = tty->termios->c_cflag;
397 priv->termios.c_lflag = tty->termios->c_lflag;
Alan Cox0c265f42008-07-22 11:14:00 +0100398 for (i = 0; i < NCCS; i++)
Alan Cox95da3102008-07-22 11:09:07 +0100399 priv->termios.c_cc[i] = tty->termios->c_cc[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 priv->cfg.pktlen = cfg.pktlen;
401 priv->cfg.baudrate = cfg.baudrate;
402 priv->cfg.databits = cfg.databits;
403 priv->cfg.unknown1 = cfg.unknown1;
404 priv->cfg.unknown2 = cfg.unknown2;
Alan Cox0c265f42008-07-22 11:14:00 +0100405 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
407 /* READ_ON and urb submission */
Alan Cox0c265f42008-07-22 11:14:00 +0100408 usb_fill_bulk_urb(port->read_urb, port->serial->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 usb_rcvbulkpipe(port->serial->dev,
410 port->bulk_in_endpointAddress),
411 port->read_urb->transfer_buffer,
412 port->read_urb->transfer_buffer_length,
413 klsi_105_read_bulk_callback,
414 port);
415
416 rc = usb_submit_urb(port->read_urb, GFP_KERNEL);
417 if (rc) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700418 dev_err(&port->dev, "%s - failed submitting read urb, "
419 "error %d\n", __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,
Alan Cox0c265f42008-07-22 11:14:00 +0100425 usb_sndctrlpipe(port->serial->dev, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 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) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700434 dev_err(&port->dev, "Enabling read failed (error = %d)\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 retval = rc;
Alan Cox0c265f42008-07-22 11:14:00 +0100436 } 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) {
Alan Cox0c265f42008-07-22 11:14:00 +0100441 spin_lock_irqsave(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 priv->line_state = line_state;
Alan Cox0c265f42008-07-22 11:14:00 +0100443 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
Alan Cox335f8512009-06-11 12:26:29 +0100454static void klsi_105_close(struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455{
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 */
Alan Cox0c265f42008-07-22 11:14:00 +0100464 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);
Oliver Neukum3edbc982008-01-22 12:47:15 +0100472 if (rc < 0)
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700473 dev_err(&port->dev,
474 "Disabling read failed (error = %d)\n", rc);
Oliver Neukum3edbc982008-01-22 12:47:15 +0100475 }
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);
Greg Kroah-Hartmanc197a8d2008-08-18 13:21:04 -0700485 dev_info(&port->serial->dev->dev,
486 "port stats: %ld bytes in, %ld bytes out\n",
487 priv->bytes_in, priv->bytes_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488} /* klsi_105_close */
489
490
491/* We need to write a complete 64-byte data block and encode the
Alan Cox0c265f42008-07-22 11:14:00 +0100492 * number actually sent in the first double-byte, LSB-order. That
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 * leaves at most 62 bytes of payload.
494 */
495#define KLSI_105_DATA_OFFSET 2 /* in the bulk urb data block */
496
497
Alan Cox95da3102008-07-22 11:09:07 +0100498static int klsi_105_write(struct tty_struct *tty,
499 struct usb_serial_port *port, const unsigned char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500{
501 struct klsi_105_private *priv = usb_get_serial_port_data(port);
502 int result, size;
Alan Cox0c265f42008-07-22 11:14:00 +0100503 int bytes_sent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
Harvey Harrison441b62c2008-03-03 16:08:34 -0800505 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
507 while (count > 0) {
508 /* try to find a free urb (write 0 bytes if none) */
509 struct urb *urb = NULL;
510 unsigned long flags;
511 int i;
Alan Cox0c265f42008-07-22 11:14:00 +0100512 /* since the pool is per-port we might not need
513 the spin lock !? */
514 spin_lock_irqsave(&priv->lock, flags);
515 for (i = 0; i < NUM_URBS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 if (priv->write_urb_pool[i]->status != -EINPROGRESS) {
517 urb = priv->write_urb_pool[i];
Harvey Harrison441b62c2008-03-03 16:08:34 -0800518 dbg("%s - using pool URB %d", __func__, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 break;
520 }
521 }
Alan Cox0c265f42008-07-22 11:14:00 +0100522 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
Alan Cox0c265f42008-07-22 11:14:00 +0100524 if (urb == NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800525 dbg("%s - no more free urbs", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 goto exit;
527 }
528
529 if (urb->transfer_buffer == NULL) {
Alan Cox0c265f42008-07-22 11:14:00 +0100530 urb->transfer_buffer =
531 kmalloc(URB_TRANSFER_BUFFER_SIZE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 if (urb->transfer_buffer == NULL) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700533 dev_err(&port->dev,
534 "%s - no more kernel memory...\n",
535 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 goto exit;
537 }
538 }
539
Alan Cox0c265f42008-07-22 11:14:00 +0100540 size = min(count, port->bulk_out_size - KLSI_105_DATA_OFFSET);
541 size = min(size, URB_TRANSFER_BUFFER_SIZE -
542 KLSI_105_DATA_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
Alan Cox0c265f42008-07-22 11:14:00 +0100544 memcpy(urb->transfer_buffer + KLSI_105_DATA_OFFSET, buf, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
546 /* write payload size into transfer buffer */
547 ((__u8 *)urb->transfer_buffer)[0] = (__u8) (size & 0xFF);
548 ((__u8 *)urb->transfer_buffer)[1] = (__u8) ((size & 0xFF00)>>8);
549
550 /* set up our urb */
551 usb_fill_bulk_urb(urb, port->serial->dev,
552 usb_sndbulkpipe(port->serial->dev,
553 port->bulk_out_endpointAddress),
554 urb->transfer_buffer,
555 URB_TRANSFER_BUFFER_SIZE,
556 klsi_105_write_bulk_callback,
557 port);
558
559 /* send the data out the bulk port */
560 result = usb_submit_urb(urb, GFP_ATOMIC);
561 if (result) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700562 dev_err(&port->dev,
563 "%s - failed submitting write urb, error %d\n",
564 __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 goto exit;
566 }
567 buf += size;
568 bytes_sent += size;
569 count -= size;
570 }
571exit:
572 /* lockless, but it's for debug info only... */
Alan Cox0c265f42008-07-22 11:14:00 +0100573 priv->bytes_out += bytes_sent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
575 return bytes_sent; /* that's how much we wrote */
576} /* klsi_105_write */
577
Alan Cox0c265f42008-07-22 11:14:00 +0100578static void klsi_105_write_bulk_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579{
Ming Leicdc97792008-02-24 18:41:47 +0800580 struct usb_serial_port *port = urb->context;
Greg Kroah-Hartman17c1b352007-06-15 15:44:13 -0700581 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
Harvey Harrison441b62c2008-03-03 16:08:34 -0800583 dbg("%s - port %d", __func__, port->number);
Greg Kroah-Hartman17c1b352007-06-15 15:44:13 -0700584
585 if (status) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800586 dbg("%s - nonzero write bulk status received: %d", __func__,
Greg Kroah-Hartman17c1b352007-06-15 15:44:13 -0700587 status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 return;
589 }
590
Pete Zaitcevcf2c7482006-05-22 21:58:49 -0700591 usb_serial_port_softint(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592} /* klsi_105_write_bulk_completion_callback */
593
594
595/* return number of characters currently in the writing process */
Alan Cox0c265f42008-07-22 11:14:00 +0100596static int klsi_105_chars_in_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597{
Alan Cox95da3102008-07-22 11:09:07 +0100598 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 int chars = 0;
600 int i;
601 unsigned long flags;
602 struct klsi_105_private *priv = usb_get_serial_port_data(port);
603
Alan Cox0c265f42008-07-22 11:14:00 +0100604 spin_lock_irqsave(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
606 for (i = 0; i < NUM_URBS; ++i) {
Alan Cox0c265f42008-07-22 11:14:00 +0100607 if (priv->write_urb_pool[i]->status == -EINPROGRESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 chars += URB_TRANSFER_BUFFER_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 }
610
Alan Cox0c265f42008-07-22 11:14:00 +0100611 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
Harvey Harrison441b62c2008-03-03 16:08:34 -0800613 dbg("%s - returns %d", __func__, chars);
Alan Cox0c265f42008-07-22 11:14:00 +0100614 return chars;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615}
616
Alan Cox0c265f42008-07-22 11:14:00 +0100617static int klsi_105_write_room(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618{
Alan Cox95da3102008-07-22 11:09:07 +0100619 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 unsigned long flags;
621 int i;
622 int room = 0;
623 struct klsi_105_private *priv = usb_get_serial_port_data(port);
624
Alan Cox0c265f42008-07-22 11:14:00 +0100625 spin_lock_irqsave(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 for (i = 0; i < NUM_URBS; ++i) {
Alan Cox0c265f42008-07-22 11:14:00 +0100627 if (priv->write_urb_pool[i]->status != -EINPROGRESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 room += URB_TRANSFER_BUFFER_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 }
630
Alan Cox0c265f42008-07-22 11:14:00 +0100631 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
Harvey Harrison441b62c2008-03-03 16:08:34 -0800633 dbg("%s - returns %d", __func__, room);
Alan Cox0c265f42008-07-22 11:14:00 +0100634 return room;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635}
636
637
638
Alan Cox0c265f42008-07-22 11:14:00 +0100639static void klsi_105_read_bulk_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640{
Ming Leicdc97792008-02-24 18:41:47 +0800641 struct usb_serial_port *port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 struct klsi_105_private *priv = usb_get_serial_port_data(port);
643 struct tty_struct *tty;
644 unsigned char *data = urb->transfer_buffer;
645 int rc;
Greg Kroah-Hartman17c1b352007-06-15 15:44:13 -0700646 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
Harvey Harrison441b62c2008-03-03 16:08:34 -0800648 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
650 /* The urb might have been killed. */
Greg Kroah-Hartman17c1b352007-06-15 15:44:13 -0700651 if (status) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800652 dbg("%s - nonzero read bulk status received: %d", __func__,
Greg Kroah-Hartman17c1b352007-06-15 15:44:13 -0700653 status);
654 return;
655 }
656
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 /* The data received is again preceded by a length double-byte in LSB-
658 * first order (see klsi_105_write() )
659 */
660 if (urb->actual_length == 0) {
661 /* empty urbs seem to happen, we ignore them */
Harvey Harrison441b62c2008-03-03 16:08:34 -0800662 /* dbg("%s - emtpy URB", __func__); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 ;
664 } else if (urb->actual_length <= 2) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800665 dbg("%s - size %d URB not understood", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 urb->actual_length);
Harvey Harrison441b62c2008-03-03 16:08:34 -0800667 usb_serial_debug_data(debug, &port->dev, __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 urb->actual_length, data);
669 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 int bytes_sent = ((__u8 *) data)[0] +
671 ((unsigned int) ((__u8 *) data)[1] << 8);
Alan Cox4a90f092008-10-13 10:39:46 +0100672 tty = tty_port_tty_get(&port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 /* we should immediately resubmit the URB, before attempting
674 * to pass the data on to the tty layer. But that needs locking
675 * against re-entry an then mixed-up data because of
676 * intermixed tty_flip_buffer_push()s
677 * FIXME
Alan Cox0c265f42008-07-22 11:14:00 +0100678 */
Harvey Harrison441b62c2008-03-03 16:08:34 -0800679 usb_serial_debug_data(debug, &port->dev, __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 urb->actual_length, data);
681
682 if (bytes_sent + 2 > urb->actual_length) {
683 dbg("%s - trying to read more data than available"
Harvey Harrison441b62c2008-03-03 16:08:34 -0800684 " (%d vs. %d)", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 bytes_sent+2, urb->actual_length);
686 /* cap at implied limit */
687 bytes_sent = urb->actual_length - 2;
688 }
689
Alan Cox33f0f882006-01-09 20:54:13 -0800690 tty_buffer_request_room(tty, bytes_sent);
691 tty_insert_flip_string(tty, data + 2, bytes_sent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 tty_flip_buffer_push(tty);
Alan Cox4a90f092008-10-13 10:39:46 +0100693 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
695 /* again lockless, but debug info only */
696 priv->bytes_in += bytes_sent;
697 }
698 /* Continue trying to always read */
Alan Cox0c265f42008-07-22 11:14:00 +0100699 usb_fill_bulk_urb(port->read_urb, port->serial->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 usb_rcvbulkpipe(port->serial->dev,
701 port->bulk_in_endpointAddress),
702 port->read_urb->transfer_buffer,
703 port->read_urb->transfer_buffer_length,
704 klsi_105_read_bulk_callback,
705 port);
706 rc = usb_submit_urb(port->read_urb, GFP_ATOMIC);
707 if (rc)
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700708 dev_err(&port->dev,
709 "%s - failed resubmitting read urb, error %d\n",
710 __func__, rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711} /* klsi_105_read_bulk_callback */
712
713
Alan Cox0c265f42008-07-22 11:14:00 +0100714static void klsi_105_set_termios(struct tty_struct *tty,
715 struct usb_serial_port *port,
716 struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717{
718 struct klsi_105_private *priv = usb_get_serial_port_data(port);
Alan Coxa5b6f602008-04-08 17:16:06 +0100719 unsigned int iflag = tty->termios->c_iflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 unsigned int old_iflag = old_termios->c_iflag;
Alan Coxa5b6f602008-04-08 17:16:06 +0100721 unsigned int cflag = tty->termios->c_cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 unsigned int old_cflag = old_termios->c_cflag;
723 struct klsi_105_port_settings cfg;
724 unsigned long flags;
Alan Coxa5b6f602008-04-08 17:16:06 +0100725 speed_t baud;
Alan Cox0c265f42008-07-22 11:14:00 +0100726
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 /* lock while we are modifying the settings */
Alan Cox0c265f42008-07-22 11:14:00 +0100728 spin_lock_irqsave(&priv->lock, flags);
729
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 /*
731 * Update baud rate
732 */
Alan Coxa5b6f602008-04-08 17:16:06 +0100733 baud = tty_get_baud_rate(tty);
734
Alan Cox0c265f42008-07-22 11:14:00 +0100735 if ((cflag & CBAUD) != (old_cflag & CBAUD)) {
736 /* reassert DTR and (maybe) RTS on transition from B0 */
737 if ((old_cflag & CBAUD) == B0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800738 dbg("%s: baud was B0", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739#if 0
740 priv->control_state |= TIOCM_DTR;
741 /* don't set RTS if using hardware flow control */
Alan Cox0c265f42008-07-22 11:14:00 +0100742 if (!(old_cflag & CRTSCTS))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 priv->control_state |= TIOCM_RTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 mct_u232_set_modem_ctrl(serial, priv->control_state);
745#endif
746 }
Alan Coxa5b6f602008-04-08 17:16:06 +0100747 }
Alan Cox0c265f42008-07-22 11:14:00 +0100748 switch (baud) {
749 case 0: /* handled below */
750 break;
751 case 1200:
752 priv->cfg.baudrate = kl5kusb105a_sio_b1200;
753 break;
754 case 2400:
755 priv->cfg.baudrate = kl5kusb105a_sio_b2400;
756 break;
757 case 4800:
758 priv->cfg.baudrate = kl5kusb105a_sio_b4800;
759 break;
760 case 9600:
761 priv->cfg.baudrate = kl5kusb105a_sio_b9600;
762 break;
763 case 19200:
764 priv->cfg.baudrate = kl5kusb105a_sio_b19200;
765 break;
766 case 38400:
767 priv->cfg.baudrate = kl5kusb105a_sio_b38400;
768 break;
769 case 57600:
770 priv->cfg.baudrate = kl5kusb105a_sio_b57600;
771 break;
772 case 115200:
773 priv->cfg.baudrate = kl5kusb105a_sio_b115200;
774 break;
775 default:
776 dbg("KLSI USB->Serial converter:"
777 " unsupported baudrate request, using default of 9600");
Alan Cox3f6ff6e2007-08-10 14:53:34 -0700778 priv->cfg.baudrate = kl5kusb105a_sio_b9600;
Alan Cox0c265f42008-07-22 11:14:00 +0100779 baud = 9600;
780 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 }
Alan Cox0c265f42008-07-22 11:14:00 +0100782 if ((cflag & CBAUD) == B0) {
Alan Coxa5b6f602008-04-08 17:16:06 +0100783 dbg("%s: baud is B0", __func__);
784 /* Drop RTS and DTR */
785 /* maybe this should be simulated by sending read
786 * disable and read enable messages?
787 */
788 ;
789#if 0
790 priv->control_state &= ~(TIOCM_DTR | TIOCM_RTS);
Alan Cox0c265f42008-07-22 11:14:00 +0100791 mct_u232_set_modem_ctrl(serial, priv->control_state);
Alan Coxa5b6f602008-04-08 17:16:06 +0100792#endif
793 }
794 tty_encode_baud_rate(tty, baud, baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
796 if ((cflag & CSIZE) != (old_cflag & CSIZE)) {
797 /* set the number of data bits */
798 switch (cflag & CSIZE) {
799 case CS5:
Harvey Harrison441b62c2008-03-03 16:08:34 -0800800 dbg("%s - 5 bits/byte not supported", __func__);
Alan Cox0c265f42008-07-22 11:14:00 +0100801 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 return ;
803 case CS6:
Harvey Harrison441b62c2008-03-03 16:08:34 -0800804 dbg("%s - 6 bits/byte not supported", __func__);
Alan Cox0c265f42008-07-22 11:14:00 +0100805 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 return ;
807 case CS7:
808 priv->cfg.databits = kl5kusb105a_dtb_7;
809 break;
810 case CS8:
811 priv->cfg.databits = kl5kusb105a_dtb_8;
812 break;
813 default:
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700814 dev_err(&port->dev,
815 "CSIZE was not CS5-CS8, using default of 8\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 priv->cfg.databits = kl5kusb105a_dtb_8;
817 break;
818 }
819 }
820
821 /*
822 * Update line control register (LCR)
823 */
824 if ((cflag & (PARENB|PARODD)) != (old_cflag & (PARENB|PARODD))
Alan Cox0c265f42008-07-22 11:14:00 +0100825 || (cflag & CSTOPB) != (old_cflag & CSTOPB)) {
Alan Coxa5b6f602008-04-08 17:16:06 +0100826 /* Not currently supported */
827 tty->termios->c_cflag &= ~(PARENB|PARODD|CSTOPB);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828#if 0
829 priv->last_lcr = 0;
830
831 /* set the parity */
832 if (cflag & PARENB)
833 priv->last_lcr |= (cflag & PARODD) ?
834 MCT_U232_PARITY_ODD : MCT_U232_PARITY_EVEN;
835 else
836 priv->last_lcr |= MCT_U232_PARITY_NONE;
837
838 /* set the number of stop bits */
839 priv->last_lcr |= (cflag & CSTOPB) ?
840 MCT_U232_STOP_BITS_2 : MCT_U232_STOP_BITS_1;
841
842 mct_u232_set_line_ctrl(serial, priv->last_lcr);
843#endif
844 ;
845 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 /*
847 * Set flow control: well, I do not really now how to handle DTR/RTS.
848 * Just do what we have seen with SniffUSB on Win98.
849 */
Alan Cox0c265f42008-07-22 11:14:00 +0100850 if ((iflag & IXOFF) != (old_iflag & IXOFF)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 || (iflag & IXON) != (old_iflag & IXON)
Alan Cox0c265f42008-07-22 11:14:00 +0100852 || (cflag & CRTSCTS) != (old_cflag & CRTSCTS)) {
Alan Coxa5b6f602008-04-08 17:16:06 +0100853 /* Not currently supported */
854 tty->termios->c_cflag &= ~CRTSCTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 /* Drop DTR/RTS if no flow control otherwise assert */
856#if 0
Alan Cox0c265f42008-07-22 11:14:00 +0100857 if ((iflag & IXOFF) || (iflag & IXON) || (cflag & CRTSCTS))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 priv->control_state |= TIOCM_DTR | TIOCM_RTS;
859 else
860 priv->control_state &= ~(TIOCM_DTR | TIOCM_RTS);
861 mct_u232_set_modem_ctrl(serial, priv->control_state);
862#endif
863 ;
864 }
Alan Cox0c265f42008-07-22 11:14:00 +0100865 memcpy(&cfg, &priv->cfg, sizeof(cfg));
866 spin_unlock_irqrestore(&priv->lock, flags);
867
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 /* now commit changes to device */
869 klsi_105_chg_port_settings(port, &cfg);
870} /* klsi_105_set_termios */
871
872
873#if 0
Alan Cox0c265f42008-07-22 11:14:00 +0100874static void mct_u232_break_ctl(struct tty_struct *tty, int break_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875{
Alan Cox95da3102008-07-22 11:09:07 +0100876 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 struct usb_serial *serial = port->serial;
Alan Cox0c265f42008-07-22 11:14:00 +0100878 struct mct_u232_private *priv =
879 (struct mct_u232_private *)port->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 unsigned char lcr = priv->last_lcr;
881
Harvey Harrison441b62c2008-03-03 16:08:34 -0800882 dbg("%sstate=%d", __func__, break_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
Alan Cox6b447f042009-01-02 13:48:56 +0000884 /* LOCKING */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 if (break_state)
886 lcr |= MCT_U232_SET_BREAK;
887
888 mct_u232_set_line_ctrl(serial, lcr);
889} /* mct_u232_break_ctl */
890#endif
891
Alan Cox0c265f42008-07-22 11:14:00 +0100892static int klsi_105_tiocmget(struct tty_struct *tty, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893{
Alan Cox95da3102008-07-22 11:09:07 +0100894 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 struct klsi_105_private *priv = usb_get_serial_port_data(port);
896 unsigned long flags;
897 int rc;
898 unsigned long line_state;
Harvey Harrison441b62c2008-03-03 16:08:34 -0800899 dbg("%s - request, just guessing", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
901 rc = klsi_105_get_line_state(port, &line_state);
902 if (rc < 0) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700903 dev_err(&port->dev,
904 "Reading line control failed (error = %d)\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 /* better return value? EAGAIN? */
906 return rc;
907 }
908
Alan Cox0c265f42008-07-22 11:14:00 +0100909 spin_lock_irqsave(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 priv->line_state = line_state;
Alan Cox0c265f42008-07-22 11:14:00 +0100911 spin_unlock_irqrestore(&priv->lock, flags);
Harvey Harrison441b62c2008-03-03 16:08:34 -0800912 dbg("%s - read line state 0x%lx", __func__, line_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 return (int)line_state;
914}
915
Alan Cox0c265f42008-07-22 11:14:00 +0100916static int klsi_105_tiocmset(struct tty_struct *tty, struct file *file,
917 unsigned int set, unsigned int clear)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918{
919 int retval = -EINVAL;
Alan Cox0c265f42008-07-22 11:14:00 +0100920
Harvey Harrison441b62c2008-03-03 16:08:34 -0800921 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
923/* if this ever gets implemented, it should be done something like this:
924 struct usb_serial *serial = port->serial;
925 struct klsi_105_private *priv = usb_get_serial_port_data(port);
926 unsigned long flags;
927 int control;
928
929 spin_lock_irqsave (&priv->lock, flags);
930 if (set & TIOCM_RTS)
931 priv->control_state |= TIOCM_RTS;
932 if (set & TIOCM_DTR)
933 priv->control_state |= TIOCM_DTR;
934 if (clear & TIOCM_RTS)
935 priv->control_state &= ~TIOCM_RTS;
936 if (clear & TIOCM_DTR)
937 priv->control_state &= ~TIOCM_DTR;
938 control = priv->control_state;
939 spin_unlock_irqrestore (&priv->lock, flags);
940 retval = mct_u232_set_modem_ctrl(serial, control);
941*/
942 return retval;
943}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
Alan Cox0c265f42008-07-22 11:14:00 +0100945static void klsi_105_throttle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946{
Alan Cox95da3102008-07-22 11:09:07 +0100947 struct usb_serial_port *port = tty->driver_data;
Harvey Harrison441b62c2008-03-03 16:08:34 -0800948 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 usb_kill_urb(port->read_urb);
950}
951
Alan Cox0c265f42008-07-22 11:14:00 +0100952static void klsi_105_unthrottle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953{
Alan Cox95da3102008-07-22 11:09:07 +0100954 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 int result;
956
Harvey Harrison441b62c2008-03-03 16:08:34 -0800957 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
959 port->read_urb->dev = port->serial->dev;
960 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
961 if (result)
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700962 dev_err(&port->dev,
963 "%s - failed submitting read urb, error %d\n",
964 __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965}
966
967
968
Alan Cox0c265f42008-07-22 11:14:00 +0100969static int __init klsi_105_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970{
971 int retval;
972 retval = usb_serial_register(&kl5kusb105d_device);
973 if (retval)
974 goto failed_usb_serial_register;
975 retval = usb_register(&kl5kusb105d_driver);
976 if (retval)
977 goto failed_usb_register;
978
Greg Kroah-Hartmanc197a8d2008-08-18 13:21:04 -0700979 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
980 DRIVER_DESC "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 return 0;
982failed_usb_register:
983 usb_serial_deregister(&kl5kusb105d_device);
984failed_usb_serial_register:
985 return retval;
986}
987
988
Alan Cox0c265f42008-07-22 11:14:00 +0100989static void __exit klsi_105_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990{
Alan Cox0c265f42008-07-22 11:14:00 +0100991 usb_deregister(&kl5kusb105d_driver);
992 usb_serial_deregister(&kl5kusb105d_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993}
994
995
Alan Cox0c265f42008-07-22 11:14:00 +0100996module_init(klsi_105_init);
997module_exit(klsi_105_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
Alan Cox0c265f42008-07-22 11:14:00 +0100999MODULE_AUTHOR(DRIVER_AUTHOR);
1000MODULE_DESCRIPTION(DRIVER_DESC);
1001MODULE_LICENSE("GPL");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
1003
1004module_param(debug, bool, S_IRUGO | S_IWUSR);
1005MODULE_PARM_DESC(debug, "enable extensive debugging messages");
1006
1007/* vim: set sts=8 ts=8 sw=8: */