blob: ad168255cc04091ef4b37bef6f35c3322faa08fb [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!
Anand Gadiyarfd589a82009-07-16 17:13:03 +020041 * 0.1 - can be used to do pilot-xfer -p /dev/ttyUSB0 -l
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 *
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 Coxa509a7e2009-09-19 13:13:26 -070078static int klsi_105_open(struct tty_struct *tty, struct usb_serial_port *port);
Alan Cox335f8512009-06-11 12:26:29 +010079static void klsi_105_close(struct usb_serial_port *port);
Alan Cox0c265f42008-07-22 11:14:00 +010080static int klsi_105_write(struct tty_struct *tty,
81 struct usb_serial_port *port, const unsigned char *buf, int count);
82static void klsi_105_write_bulk_callback(struct urb *urb);
83static int klsi_105_chars_in_buffer(struct tty_struct *tty);
84static int klsi_105_write_room(struct tty_struct *tty);
85static void klsi_105_read_bulk_callback(struct urb *urb);
86static void klsi_105_set_termios(struct tty_struct *tty,
87 struct usb_serial_port *port, struct ktermios *old);
88static void klsi_105_throttle(struct tty_struct *tty);
89static void klsi_105_unthrottle(struct tty_struct *tty);
90static int klsi_105_tiocmget(struct tty_struct *tty, struct file *file);
91static int klsi_105_tiocmset(struct tty_struct *tty, struct file *file,
92 unsigned int set, unsigned int clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
94/*
95 * All of the device info needed for the KLSI converters.
96 */
Németh Márton7d40d7e2010-01-10 15:34:24 +010097static const struct usb_device_id id_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 { USB_DEVICE(PALMCONNECT_VID, PALMCONNECT_PID) },
99 { USB_DEVICE(KLSI_VID, KLSI_KL5KUSB105D_PID) },
100 { } /* Terminating entry */
101};
102
Alan Cox0c265f42008-07-22 11:14:00 +0100103MODULE_DEVICE_TABLE(usb, id_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
105static struct usb_driver kl5kusb105d_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 .name = "kl5kusb105d",
107 .probe = usb_serial_probe,
108 .disconnect = usb_serial_disconnect,
109 .id_table = id_table,
Johan Hovoldff8c1952010-05-19 00:01:39 +0200110 .no_dynamic_id = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111};
112
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700113static struct usb_serial_driver kl5kusb105d_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700114 .driver = {
115 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700116 .name = "kl5kusb105d",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700117 },
Johan Hovoldff8c1952010-05-19 00:01:39 +0200118 .description = "KL5KUSB105D / PalmConnect",
119 .usb_driver = &kl5kusb105d_driver,
120 .id_table = id_table,
121 .num_ports = 1,
122 .open = klsi_105_open,
123 .close = klsi_105_close,
124 .write = klsi_105_write,
125 .write_bulk_callback = klsi_105_write_bulk_callback,
126 .chars_in_buffer = klsi_105_chars_in_buffer,
127 .write_room = klsi_105_write_room,
128 .read_bulk_callback = klsi_105_read_bulk_callback,
129 .set_termios = klsi_105_set_termios,
130 /*.break_ctl = klsi_105_break_ctl,*/
131 .tiocmget = klsi_105_tiocmget,
132 .tiocmset = klsi_105_tiocmset,
133 .attach = klsi_105_startup,
134 .disconnect = klsi_105_disconnect,
135 .release = klsi_105_release,
136 .throttle = klsi_105_throttle,
137 .unthrottle = klsi_105_unthrottle,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138};
139
140struct klsi_105_port_settings {
141 __u8 pktlen; /* always 5, it seems */
142 __u8 baudrate;
143 __u8 databits;
144 __u8 unknown1;
145 __u8 unknown2;
146} __attribute__ ((packed));
147
148/* we implement a pool of NUM_URBS urbs per usb_serial */
149#define NUM_URBS 1
150#define URB_TRANSFER_BUFFER_SIZE 64
151struct klsi_105_private {
152 struct klsi_105_port_settings cfg;
Alan Cox606d0992006-12-08 02:38:45 -0800153 struct ktermios termios;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 unsigned long line_state; /* modem line settings */
155 /* write pool */
Alan Cox0c265f42008-07-22 11:14:00 +0100156 struct urb *write_urb_pool[NUM_URBS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 spinlock_t lock;
158 unsigned long bytes_in;
159 unsigned long bytes_out;
160};
161
162
163/*
164 * Handle vendor specific USB requests
165 */
166
167
168#define KLSI_TIMEOUT 5000 /* default urb timeout */
169
170static int klsi_105_chg_port_settings(struct usb_serial_port *port,
171 struct klsi_105_port_settings *settings)
172{
173 int rc;
174
Alan Cox0c265f42008-07-22 11:14:00 +0100175 rc = usb_control_msg(port->serial->dev,
176 usb_sndctrlpipe(port->serial->dev, 0),
177 KL5KUSB105A_SIO_SET_DATA,
178 USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_INTERFACE,
179 0, /* value */
180 0, /* index */
181 settings,
182 sizeof(struct klsi_105_port_settings),
183 KLSI_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 if (rc < 0)
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700185 dev_err(&port->dev,
186 "Change port settings failed (error = %d)\n", rc);
Greg Kroah-Hartmanc197a8d2008-08-18 13:21:04 -0700187 dev_info(&port->serial->dev->dev,
188 "%d byte block, baudrate %x, databits %d, u1 %d, u2 %d\n",
189 settings->pktlen, settings->baudrate, settings->databits,
190 settings->unknown1, settings->unknown2);
Alan Cox0c265f42008-07-22 11:14:00 +0100191 return rc;
Johan Hovoldff8c1952010-05-19 00:01:39 +0200192}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
194/* translate a 16-bit status value from the device to linux's TIO bits */
195static unsigned long klsi_105_status2linestate(const __u16 status)
196{
197 unsigned long res = 0;
198
199 res = ((status & KL5KUSB105A_DSR) ? TIOCM_DSR : 0)
200 | ((status & KL5KUSB105A_CTS) ? TIOCM_CTS : 0)
201 ;
202
203 return res;
204}
Johan Hovoldff8c1952010-05-19 00:01:39 +0200205
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;
Johan Hovoldabf492e2009-12-28 23:01:52 +0100216 u8 *status_buf;
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");
Johan Hovoldabf492e2009-12-28 23:01:52 +0100220
221 status_buf = kmalloc(KLSI_STATUSBUF_LEN, GFP_KERNEL);
222 if (!status_buf) {
223 dev_err(&port->dev, "%s - out of memory for status buffer.\n",
224 __func__);
225 return -ENOMEM;
226 }
227 status_buf[0] = 0xff;
228 status_buf[1] = 0xff;
Alan Cox0c265f42008-07-22 11:14:00 +0100229 rc = usb_control_msg(port->serial->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 usb_rcvctrlpipe(port->serial->dev, 0),
231 KL5KUSB105A_SIO_POLL,
Alan Cox0c265f42008-07-22 11:14:00 +0100232 USB_TYPE_VENDOR | USB_DIR_IN,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 0, /* value */
234 0, /* index */
235 status_buf, KLSI_STATUSBUF_LEN,
236 10000
237 );
238 if (rc < 0)
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700239 dev_err(&port->dev, "Reading line status failed (error = %d)\n",
240 rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 else {
Harvey Harrisonbd2c7842008-05-01 20:52:57 -0700242 status = get_unaligned_le16(status_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Greg Kroah-Hartmanc197a8d2008-08-18 13:21:04 -0700244 dev_info(&port->serial->dev->dev, "read status %x %x",
245 status_buf[0], status_buf[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
247 *line_state_p = klsi_105_status2linestate(status);
248 }
Johan Hovoldabf492e2009-12-28 23:01:52 +0100249
250 kfree(status_buf);
Alan Cox0c265f42008-07-22 11:14:00 +0100251 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252}
253
254
255/*
256 * Driver's tty interface functions
257 */
258
Alan Cox0c265f42008-07-22 11:14:00 +0100259static int klsi_105_startup(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
261 struct klsi_105_private *priv;
Oliver Neukum9306fff2007-03-29 11:23:54 +0200262 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264 /* check if we support the product id (see keyspan.c)
265 * FIXME
266 */
267
268 /* allocate the private data structure */
Alan Cox0c265f42008-07-22 11:14:00 +0100269 for (i = 0; i < serial->num_ports; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 priv = kmalloc(sizeof(struct klsi_105_private),
271 GFP_KERNEL);
272 if (!priv) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800273 dbg("%skmalloc for klsi_105_private failed.", __func__);
Oliver Neukum9306fff2007-03-29 11:23:54 +0200274 i--;
275 goto err_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 }
277 /* set initial values for control structures */
278 priv->cfg.pktlen = 5;
279 priv->cfg.baudrate = kl5kusb105a_sio_b9600;
280 priv->cfg.databits = kl5kusb105a_dtb_8;
281 priv->cfg.unknown1 = 0;
282 priv->cfg.unknown2 = 1;
283
284 priv->line_state = 0;
285
286 priv->bytes_in = 0;
287 priv->bytes_out = 0;
288 usb_set_serial_port_data(serial->port[i], priv);
289
Alan Cox0c265f42008-07-22 11:14:00 +0100290 spin_lock_init(&priv->lock);
291 for (j = 0; j < NUM_URBS; j++) {
292 struct urb *urb = usb_alloc_urb(0, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
294 priv->write_urb_pool[j] = urb;
295 if (urb == NULL) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700296 dev_err(&serial->dev->dev, "No more urbs???\n");
Oliver Neukum9306fff2007-03-29 11:23:54 +0200297 goto err_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 }
299
Alan Cox0c265f42008-07-22 11:14:00 +0100300 urb->transfer_buffer =
301 kmalloc(URB_TRANSFER_BUFFER_SIZE, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 if (!urb->transfer_buffer) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700303 dev_err(&serial->dev->dev,
304 "%s - out of memory for urb buffers.\n",
305 __func__);
Oliver Neukum9306fff2007-03-29 11:23:54 +0200306 goto err_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 }
308 }
309
310 /* priv->termios is left uninitalized until port opening */
311 init_waitqueue_head(&serial->port[i]->write_wait);
312 }
Alan Cox0c265f42008-07-22 11:14:00 +0100313
Oliver Neukum9306fff2007-03-29 11:23:54 +0200314 return 0;
315
316err_cleanup:
317 for (; i >= 0; i--) {
318 priv = usb_get_serial_port_data(serial->port[i]);
Alan Cox0c265f42008-07-22 11:14:00 +0100319 for (j = 0; j < NUM_URBS; j++) {
Oliver Neukum9306fff2007-03-29 11:23:54 +0200320 if (priv->write_urb_pool[j]) {
321 kfree(priv->write_urb_pool[j]->transfer_buffer);
322 usb_free_urb(priv->write_urb_pool[j]);
323 }
324 }
Johan Hovold313b0d82010-05-19 00:01:38 +0200325 kfree(priv);
Oliver Neukum9306fff2007-03-29 11:23:54 +0200326 usb_set_serial_port_data(serial->port[i], NULL);
327 }
328 return -ENOMEM;
Johan Hovoldff8c1952010-05-19 00:01:39 +0200329}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Alan Sternf9c99bb2009-06-02 11:53:55 -0400331static void klsi_105_disconnect(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332{
333 int i;
Alan Cox0c265f42008-07-22 11:14:00 +0100334
Harvey Harrison441b62c2008-03-03 16:08:34 -0800335 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
337 /* stop reads and writes on all ports */
Alan Cox0c265f42008-07-22 11:14:00 +0100338 for (i = 0; i < serial->num_ports; ++i) {
339 struct klsi_105_private *priv =
340 usb_get_serial_port_data(serial->port[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
342 if (priv) {
343 /* kill our write urb pool */
344 int j;
345 struct urb **write_urbs = priv->write_urb_pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
347 for (j = 0; j < NUM_URBS; j++) {
348 if (write_urbs[j]) {
Alan Sternf9c99bb2009-06-02 11:53:55 -0400349 usb_kill_urb(write_urbs[j]);
Alan Cox0c265f42008-07-22 11:14:00 +0100350 usb_free_urb(write_urbs[j]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 }
352 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 }
354 }
Johan Hovoldff8c1952010-05-19 00:01:39 +0200355}
Alan Sternf9c99bb2009-06-02 11:53:55 -0400356
357static void klsi_105_release(struct usb_serial *serial)
358{
359 int i;
360
361 dbg("%s", __func__);
362
363 for (i = 0; i < serial->num_ports; ++i) {
364 struct klsi_105_private *priv =
365 usb_get_serial_port_data(serial->port[i]);
366
367 kfree(priv);
368 }
Johan Hovoldff8c1952010-05-19 00:01:39 +0200369}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Alan Coxa509a7e2009-09-19 13:13:26 -0700371static int klsi_105_open(struct tty_struct *tty, struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372{
373 struct klsi_105_private *priv = usb_get_serial_port_data(port);
374 int retval = 0;
375 int rc;
376 int i;
377 unsigned long line_state;
Johan Hovoldabf492e2009-12-28 23:01:52 +0100378 struct klsi_105_port_settings *cfg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 unsigned long flags;
380
Harvey Harrison441b62c2008-03-03 16:08:34 -0800381 dbg("%s port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 /* Do a defined restart:
384 * Set up sane default baud rate and send the 'READ_ON'
Alan Cox0c265f42008-07-22 11:14:00 +0100385 * vendor command.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 * FIXME: set modem line control (how?)
387 * Then read the modem line control and store values in
388 * priv->line_state.
389 */
Johan Hovoldabf492e2009-12-28 23:01:52 +0100390 cfg = kmalloc(sizeof(*cfg), GFP_KERNEL);
391 if (!cfg) {
392 dev_err(&port->dev, "%s - out of memory for config buffer.\n",
393 __func__);
394 return -ENOMEM;
395 }
396 cfg->pktlen = 5;
397 cfg->baudrate = kl5kusb105a_sio_b9600;
398 cfg->databits = kl5kusb105a_dtb_8;
399 cfg->unknown1 = 0;
400 cfg->unknown2 = 1;
401 klsi_105_chg_port_settings(port, cfg);
Alan Cox0c265f42008-07-22 11:14:00 +0100402
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 /* set up termios structure */
Alan Cox0c265f42008-07-22 11:14:00 +0100404 spin_lock_irqsave(&priv->lock, flags);
Alan Cox95da3102008-07-22 11:09:07 +0100405 priv->termios.c_iflag = tty->termios->c_iflag;
406 priv->termios.c_oflag = tty->termios->c_oflag;
407 priv->termios.c_cflag = tty->termios->c_cflag;
408 priv->termios.c_lflag = tty->termios->c_lflag;
Alan Cox0c265f42008-07-22 11:14:00 +0100409 for (i = 0; i < NCCS; i++)
Alan Cox95da3102008-07-22 11:09:07 +0100410 priv->termios.c_cc[i] = tty->termios->c_cc[i];
Johan Hovoldabf492e2009-12-28 23:01:52 +0100411 priv->cfg.pktlen = cfg->pktlen;
412 priv->cfg.baudrate = cfg->baudrate;
413 priv->cfg.databits = cfg->databits;
414 priv->cfg.unknown1 = cfg->unknown1;
415 priv->cfg.unknown2 = cfg->unknown2;
Alan Cox0c265f42008-07-22 11:14:00 +0100416 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
418 /* READ_ON and urb submission */
Alan Cox0c265f42008-07-22 11:14:00 +0100419 usb_fill_bulk_urb(port->read_urb, port->serial->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 usb_rcvbulkpipe(port->serial->dev,
421 port->bulk_in_endpointAddress),
422 port->read_urb->transfer_buffer,
423 port->read_urb->transfer_buffer_length,
424 klsi_105_read_bulk_callback,
425 port);
426
427 rc = usb_submit_urb(port->read_urb, GFP_KERNEL);
428 if (rc) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700429 dev_err(&port->dev, "%s - failed submitting read urb, "
430 "error %d\n", __func__, rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 retval = rc;
432 goto exit;
433 }
434
435 rc = usb_control_msg(port->serial->dev,
Alan Cox0c265f42008-07-22 11:14:00 +0100436 usb_sndctrlpipe(port->serial->dev, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 KL5KUSB105A_SIO_CONFIGURE,
438 USB_TYPE_VENDOR|USB_DIR_OUT|USB_RECIP_INTERFACE,
439 KL5KUSB105A_SIO_CONFIGURE_READ_ON,
440 0, /* index */
441 NULL,
442 0,
443 KLSI_TIMEOUT);
444 if (rc < 0) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700445 dev_err(&port->dev, "Enabling read failed (error = %d)\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 retval = rc;
Alan Cox0c265f42008-07-22 11:14:00 +0100447 } else
Harvey Harrison441b62c2008-03-03 16:08:34 -0800448 dbg("%s - enabled reading", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
450 rc = klsi_105_get_line_state(port, &line_state);
451 if (rc >= 0) {
Alan Cox0c265f42008-07-22 11:14:00 +0100452 spin_lock_irqsave(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 priv->line_state = line_state;
Alan Cox0c265f42008-07-22 11:14:00 +0100454 spin_unlock_irqrestore(&priv->lock, flags);
Harvey Harrison441b62c2008-03-03 16:08:34 -0800455 dbg("%s - read line state 0x%lx", __func__, line_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 retval = 0;
457 } else
458 retval = rc;
459
460exit:
Johan Hovoldabf492e2009-12-28 23:01:52 +0100461 kfree(cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 return retval;
Johan Hovoldff8c1952010-05-19 00:01:39 +0200463}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
Alan Cox335f8512009-06-11 12:26:29 +0100465static void klsi_105_close(struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466{
467 struct klsi_105_private *priv = usb_get_serial_port_data(port);
468 int rc;
469
Harvey Harrison441b62c2008-03-03 16:08:34 -0800470 dbg("%s port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
Oliver Neukum3edbc982008-01-22 12:47:15 +0100472 mutex_lock(&port->serial->disc_mutex);
473 if (!port->serial->disconnected) {
474 /* send READ_OFF */
Alan Cox0c265f42008-07-22 11:14:00 +0100475 rc = usb_control_msg(port->serial->dev,
476 usb_sndctrlpipe(port->serial->dev, 0),
477 KL5KUSB105A_SIO_CONFIGURE,
478 USB_TYPE_VENDOR | USB_DIR_OUT,
479 KL5KUSB105A_SIO_CONFIGURE_READ_OFF,
480 0, /* index */
481 NULL, 0,
482 KLSI_TIMEOUT);
Oliver Neukum3edbc982008-01-22 12:47:15 +0100483 if (rc < 0)
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700484 dev_err(&port->dev,
485 "Disabling read failed (error = %d)\n", rc);
Oliver Neukum3edbc982008-01-22 12:47:15 +0100486 }
487 mutex_unlock(&port->serial->disc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
489 /* shutdown our bulk reads and writes */
490 usb_kill_urb(port->write_urb);
491 usb_kill_urb(port->read_urb);
492 /* unlink our write pool */
493 /* FIXME */
494 /* wgg - do I need this? I think so. */
495 usb_kill_urb(port->interrupt_in_urb);
Greg Kroah-Hartmanc197a8d2008-08-18 13:21:04 -0700496 dev_info(&port->serial->dev->dev,
497 "port stats: %ld bytes in, %ld bytes out\n",
498 priv->bytes_in, priv->bytes_out);
Johan Hovoldff8c1952010-05-19 00:01:39 +0200499}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
501/* We need to write a complete 64-byte data block and encode the
Alan Cox0c265f42008-07-22 11:14:00 +0100502 * number actually sent in the first double-byte, LSB-order. That
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 * leaves at most 62 bytes of payload.
504 */
505#define KLSI_105_DATA_OFFSET 2 /* in the bulk urb data block */
506
Alan Cox95da3102008-07-22 11:09:07 +0100507static int klsi_105_write(struct tty_struct *tty,
508 struct usb_serial_port *port, const unsigned char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509{
510 struct klsi_105_private *priv = usb_get_serial_port_data(port);
511 int result, size;
Alan Cox0c265f42008-07-22 11:14:00 +0100512 int bytes_sent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
Harvey Harrison441b62c2008-03-03 16:08:34 -0800514 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
516 while (count > 0) {
517 /* try to find a free urb (write 0 bytes if none) */
518 struct urb *urb = NULL;
519 unsigned long flags;
520 int i;
Alan Cox0c265f42008-07-22 11:14:00 +0100521 /* since the pool is per-port we might not need
522 the spin lock !? */
523 spin_lock_irqsave(&priv->lock, flags);
524 for (i = 0; i < NUM_URBS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 if (priv->write_urb_pool[i]->status != -EINPROGRESS) {
526 urb = priv->write_urb_pool[i];
Harvey Harrison441b62c2008-03-03 16:08:34 -0800527 dbg("%s - using pool URB %d", __func__, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 break;
529 }
530 }
Alan Cox0c265f42008-07-22 11:14:00 +0100531 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
Alan Cox0c265f42008-07-22 11:14:00 +0100533 if (urb == NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800534 dbg("%s - no more free urbs", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 goto exit;
536 }
537
538 if (urb->transfer_buffer == NULL) {
Alan Cox0c265f42008-07-22 11:14:00 +0100539 urb->transfer_buffer =
540 kmalloc(URB_TRANSFER_BUFFER_SIZE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 if (urb->transfer_buffer == NULL) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700542 dev_err(&port->dev,
543 "%s - no more kernel memory...\n",
544 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 goto exit;
546 }
547 }
548
Alan Cox0c265f42008-07-22 11:14:00 +0100549 size = min(count, port->bulk_out_size - KLSI_105_DATA_OFFSET);
550 size = min(size, URB_TRANSFER_BUFFER_SIZE -
551 KLSI_105_DATA_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Alan Cox0c265f42008-07-22 11:14:00 +0100553 memcpy(urb->transfer_buffer + KLSI_105_DATA_OFFSET, buf, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
555 /* write payload size into transfer buffer */
556 ((__u8 *)urb->transfer_buffer)[0] = (__u8) (size & 0xFF);
557 ((__u8 *)urb->transfer_buffer)[1] = (__u8) ((size & 0xFF00)>>8);
558
559 /* set up our urb */
560 usb_fill_bulk_urb(urb, port->serial->dev,
561 usb_sndbulkpipe(port->serial->dev,
562 port->bulk_out_endpointAddress),
563 urb->transfer_buffer,
564 URB_TRANSFER_BUFFER_SIZE,
565 klsi_105_write_bulk_callback,
566 port);
567
568 /* send the data out the bulk port */
569 result = usb_submit_urb(urb, GFP_ATOMIC);
570 if (result) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700571 dev_err(&port->dev,
572 "%s - failed submitting write urb, error %d\n",
573 __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 goto exit;
575 }
576 buf += size;
577 bytes_sent += size;
578 count -= size;
579 }
580exit:
581 /* lockless, but it's for debug info only... */
Alan Cox0c265f42008-07-22 11:14:00 +0100582 priv->bytes_out += bytes_sent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
584 return bytes_sent; /* that's how much we wrote */
Johan Hovoldff8c1952010-05-19 00:01:39 +0200585}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
Alan Cox0c265f42008-07-22 11:14:00 +0100587static void klsi_105_write_bulk_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588{
Ming Leicdc97792008-02-24 18:41:47 +0800589 struct usb_serial_port *port = urb->context;
Greg Kroah-Hartman17c1b352007-06-15 15:44:13 -0700590 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
Harvey Harrison441b62c2008-03-03 16:08:34 -0800592 dbg("%s - port %d", __func__, port->number);
Greg Kroah-Hartman17c1b352007-06-15 15:44:13 -0700593
594 if (status) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800595 dbg("%s - nonzero write bulk status received: %d", __func__,
Greg Kroah-Hartman17c1b352007-06-15 15:44:13 -0700596 status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 return;
598 }
599
Pete Zaitcevcf2c7482006-05-22 21:58:49 -0700600 usb_serial_port_softint(port);
Johan Hovoldff8c1952010-05-19 00:01:39 +0200601}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
603/* return number of characters currently in the writing process */
Alan Cox0c265f42008-07-22 11:14:00 +0100604static int klsi_105_chars_in_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605{
Alan Cox95da3102008-07-22 11:09:07 +0100606 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 int chars = 0;
608 int i;
609 unsigned long flags;
610 struct klsi_105_private *priv = usb_get_serial_port_data(port);
611
Alan Cox0c265f42008-07-22 11:14:00 +0100612 spin_lock_irqsave(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
614 for (i = 0; i < NUM_URBS; ++i) {
Alan Cox0c265f42008-07-22 11:14:00 +0100615 if (priv->write_urb_pool[i]->status == -EINPROGRESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 chars += URB_TRANSFER_BUFFER_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 }
618
Alan Cox0c265f42008-07-22 11:14:00 +0100619 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
Harvey Harrison441b62c2008-03-03 16:08:34 -0800621 dbg("%s - returns %d", __func__, chars);
Alan Cox0c265f42008-07-22 11:14:00 +0100622 return chars;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623}
624
Alan Cox0c265f42008-07-22 11:14:00 +0100625static int klsi_105_write_room(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626{
Alan Cox95da3102008-07-22 11:09:07 +0100627 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 unsigned long flags;
629 int i;
630 int room = 0;
631 struct klsi_105_private *priv = usb_get_serial_port_data(port);
632
Alan Cox0c265f42008-07-22 11:14:00 +0100633 spin_lock_irqsave(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 for (i = 0; i < NUM_URBS; ++i) {
Alan Cox0c265f42008-07-22 11:14:00 +0100635 if (priv->write_urb_pool[i]->status != -EINPROGRESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 room += URB_TRANSFER_BUFFER_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 }
638
Alan Cox0c265f42008-07-22 11:14:00 +0100639 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
Harvey Harrison441b62c2008-03-03 16:08:34 -0800641 dbg("%s - returns %d", __func__, room);
Alan Cox0c265f42008-07-22 11:14:00 +0100642 return room;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643}
644
Alan Cox0c265f42008-07-22 11:14:00 +0100645static void klsi_105_read_bulk_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646{
Ming Leicdc97792008-02-24 18:41:47 +0800647 struct usb_serial_port *port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 struct klsi_105_private *priv = usb_get_serial_port_data(port);
649 struct tty_struct *tty;
650 unsigned char *data = urb->transfer_buffer;
651 int rc;
Greg Kroah-Hartman17c1b352007-06-15 15:44:13 -0700652 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
Harvey Harrison441b62c2008-03-03 16:08:34 -0800654 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
656 /* The urb might have been killed. */
Greg Kroah-Hartman17c1b352007-06-15 15:44:13 -0700657 if (status) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800658 dbg("%s - nonzero read bulk status received: %d", __func__,
Greg Kroah-Hartman17c1b352007-06-15 15:44:13 -0700659 status);
660 return;
661 }
662
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 /* The data received is again preceded by a length double-byte in LSB-
664 * first order (see klsi_105_write() )
665 */
666 if (urb->actual_length == 0) {
667 /* empty urbs seem to happen, we ignore them */
Harvey Harrison441b62c2008-03-03 16:08:34 -0800668 /* dbg("%s - emtpy URB", __func__); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 ;
670 } else if (urb->actual_length <= 2) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800671 dbg("%s - size %d URB not understood", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 urb->actual_length);
Harvey Harrison441b62c2008-03-03 16:08:34 -0800673 usb_serial_debug_data(debug, &port->dev, __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 urb->actual_length, data);
675 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 int bytes_sent = ((__u8 *) data)[0] +
677 ((unsigned int) ((__u8 *) data)[1] << 8);
Alan Cox4a90f092008-10-13 10:39:46 +0100678 tty = tty_port_tty_get(&port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 /* we should immediately resubmit the URB, before attempting
680 * to pass the data on to the tty layer. But that needs locking
681 * against re-entry an then mixed-up data because of
682 * intermixed tty_flip_buffer_push()s
683 * FIXME
Alan Cox0c265f42008-07-22 11:14:00 +0100684 */
Harvey Harrison441b62c2008-03-03 16:08:34 -0800685 usb_serial_debug_data(debug, &port->dev, __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 urb->actual_length, data);
687
688 if (bytes_sent + 2 > urb->actual_length) {
689 dbg("%s - trying to read more data than available"
Harvey Harrison441b62c2008-03-03 16:08:34 -0800690 " (%d vs. %d)", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 bytes_sent+2, urb->actual_length);
692 /* cap at implied limit */
693 bytes_sent = urb->actual_length - 2;
694 }
695
Alan Cox33f0f882006-01-09 20:54:13 -0800696 tty_insert_flip_string(tty, data + 2, bytes_sent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 tty_flip_buffer_push(tty);
Alan Cox4a90f092008-10-13 10:39:46 +0100698 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
700 /* again lockless, but debug info only */
701 priv->bytes_in += bytes_sent;
702 }
703 /* Continue trying to always read */
Alan Cox0c265f42008-07-22 11:14:00 +0100704 usb_fill_bulk_urb(port->read_urb, port->serial->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 usb_rcvbulkpipe(port->serial->dev,
706 port->bulk_in_endpointAddress),
707 port->read_urb->transfer_buffer,
708 port->read_urb->transfer_buffer_length,
709 klsi_105_read_bulk_callback,
710 port);
711 rc = usb_submit_urb(port->read_urb, GFP_ATOMIC);
712 if (rc)
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700713 dev_err(&port->dev,
714 "%s - failed resubmitting read urb, error %d\n",
715 __func__, rc);
Johan Hovoldff8c1952010-05-19 00:01:39 +0200716}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
Alan Cox0c265f42008-07-22 11:14:00 +0100718static void klsi_105_set_termios(struct tty_struct *tty,
719 struct usb_serial_port *port,
720 struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721{
722 struct klsi_105_private *priv = usb_get_serial_port_data(port);
Alan Coxa5b6f602008-04-08 17:16:06 +0100723 unsigned int iflag = tty->termios->c_iflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 unsigned int old_iflag = old_termios->c_iflag;
Alan Coxa5b6f602008-04-08 17:16:06 +0100725 unsigned int cflag = tty->termios->c_cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 unsigned int old_cflag = old_termios->c_cflag;
Johan Hovoldabf492e2009-12-28 23:01:52 +0100727 struct klsi_105_port_settings *cfg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 unsigned long flags;
Alan Coxa5b6f602008-04-08 17:16:06 +0100729 speed_t baud;
Alan Cox0c265f42008-07-22 11:14:00 +0100730
Johan Hovoldabf492e2009-12-28 23:01:52 +0100731 cfg = kmalloc(sizeof(*cfg), GFP_KERNEL);
732 if (!cfg) {
733 dev_err(&port->dev, "%s - out of memory for config buffer.\n",
734 __func__);
735 return;
736 }
737
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 /* lock while we are modifying the settings */
Alan Cox0c265f42008-07-22 11:14:00 +0100739 spin_lock_irqsave(&priv->lock, flags);
740
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 /*
742 * Update baud rate
743 */
Alan Coxa5b6f602008-04-08 17:16:06 +0100744 baud = tty_get_baud_rate(tty);
745
Alan Cox0c265f42008-07-22 11:14:00 +0100746 if ((cflag & CBAUD) != (old_cflag & CBAUD)) {
747 /* reassert DTR and (maybe) RTS on transition from B0 */
748 if ((old_cflag & CBAUD) == B0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800749 dbg("%s: baud was B0", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750#if 0
751 priv->control_state |= TIOCM_DTR;
752 /* don't set RTS if using hardware flow control */
Alan Cox0c265f42008-07-22 11:14:00 +0100753 if (!(old_cflag & CRTSCTS))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 priv->control_state |= TIOCM_RTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 mct_u232_set_modem_ctrl(serial, priv->control_state);
756#endif
757 }
Alan Coxa5b6f602008-04-08 17:16:06 +0100758 }
Alan Cox0c265f42008-07-22 11:14:00 +0100759 switch (baud) {
760 case 0: /* handled below */
761 break;
762 case 1200:
763 priv->cfg.baudrate = kl5kusb105a_sio_b1200;
764 break;
765 case 2400:
766 priv->cfg.baudrate = kl5kusb105a_sio_b2400;
767 break;
768 case 4800:
769 priv->cfg.baudrate = kl5kusb105a_sio_b4800;
770 break;
771 case 9600:
772 priv->cfg.baudrate = kl5kusb105a_sio_b9600;
773 break;
774 case 19200:
775 priv->cfg.baudrate = kl5kusb105a_sio_b19200;
776 break;
777 case 38400:
778 priv->cfg.baudrate = kl5kusb105a_sio_b38400;
779 break;
780 case 57600:
781 priv->cfg.baudrate = kl5kusb105a_sio_b57600;
782 break;
783 case 115200:
784 priv->cfg.baudrate = kl5kusb105a_sio_b115200;
785 break;
786 default:
787 dbg("KLSI USB->Serial converter:"
788 " unsupported baudrate request, using default of 9600");
Alan Cox3f6ff6e2007-08-10 14:53:34 -0700789 priv->cfg.baudrate = kl5kusb105a_sio_b9600;
Alan Cox0c265f42008-07-22 11:14:00 +0100790 baud = 9600;
791 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 }
Alan Cox0c265f42008-07-22 11:14:00 +0100793 if ((cflag & CBAUD) == B0) {
Alan Coxa5b6f602008-04-08 17:16:06 +0100794 dbg("%s: baud is B0", __func__);
795 /* Drop RTS and DTR */
796 /* maybe this should be simulated by sending read
797 * disable and read enable messages?
798 */
799 ;
800#if 0
801 priv->control_state &= ~(TIOCM_DTR | TIOCM_RTS);
Alan Cox0c265f42008-07-22 11:14:00 +0100802 mct_u232_set_modem_ctrl(serial, priv->control_state);
Alan Coxa5b6f602008-04-08 17:16:06 +0100803#endif
804 }
805 tty_encode_baud_rate(tty, baud, baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
807 if ((cflag & CSIZE) != (old_cflag & CSIZE)) {
808 /* set the number of data bits */
809 switch (cflag & CSIZE) {
810 case CS5:
Harvey Harrison441b62c2008-03-03 16:08:34 -0800811 dbg("%s - 5 bits/byte not supported", __func__);
Alan Cox0c265f42008-07-22 11:14:00 +0100812 spin_unlock_irqrestore(&priv->lock, flags);
Johan Hovoldabf492e2009-12-28 23:01:52 +0100813 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 case CS6:
Harvey Harrison441b62c2008-03-03 16:08:34 -0800815 dbg("%s - 6 bits/byte not supported", __func__);
Alan Cox0c265f42008-07-22 11:14:00 +0100816 spin_unlock_irqrestore(&priv->lock, flags);
Johan Hovoldabf492e2009-12-28 23:01:52 +0100817 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 case CS7:
819 priv->cfg.databits = kl5kusb105a_dtb_7;
820 break;
821 case CS8:
822 priv->cfg.databits = kl5kusb105a_dtb_8;
823 break;
824 default:
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700825 dev_err(&port->dev,
826 "CSIZE was not CS5-CS8, using default of 8\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 priv->cfg.databits = kl5kusb105a_dtb_8;
828 break;
829 }
830 }
831
832 /*
833 * Update line control register (LCR)
834 */
835 if ((cflag & (PARENB|PARODD)) != (old_cflag & (PARENB|PARODD))
Alan Cox0c265f42008-07-22 11:14:00 +0100836 || (cflag & CSTOPB) != (old_cflag & CSTOPB)) {
Alan Coxa5b6f602008-04-08 17:16:06 +0100837 /* Not currently supported */
838 tty->termios->c_cflag &= ~(PARENB|PARODD|CSTOPB);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839#if 0
840 priv->last_lcr = 0;
841
842 /* set the parity */
843 if (cflag & PARENB)
844 priv->last_lcr |= (cflag & PARODD) ?
845 MCT_U232_PARITY_ODD : MCT_U232_PARITY_EVEN;
846 else
847 priv->last_lcr |= MCT_U232_PARITY_NONE;
848
849 /* set the number of stop bits */
850 priv->last_lcr |= (cflag & CSTOPB) ?
851 MCT_U232_STOP_BITS_2 : MCT_U232_STOP_BITS_1;
852
853 mct_u232_set_line_ctrl(serial, priv->last_lcr);
854#endif
855 ;
856 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 /*
858 * Set flow control: well, I do not really now how to handle DTR/RTS.
859 * Just do what we have seen with SniffUSB on Win98.
860 */
Alan Cox0c265f42008-07-22 11:14:00 +0100861 if ((iflag & IXOFF) != (old_iflag & IXOFF)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 || (iflag & IXON) != (old_iflag & IXON)
Alan Cox0c265f42008-07-22 11:14:00 +0100863 || (cflag & CRTSCTS) != (old_cflag & CRTSCTS)) {
Alan Coxa5b6f602008-04-08 17:16:06 +0100864 /* Not currently supported */
865 tty->termios->c_cflag &= ~CRTSCTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 /* Drop DTR/RTS if no flow control otherwise assert */
867#if 0
Alan Cox0c265f42008-07-22 11:14:00 +0100868 if ((iflag & IXOFF) || (iflag & IXON) || (cflag & CRTSCTS))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 priv->control_state |= TIOCM_DTR | TIOCM_RTS;
870 else
871 priv->control_state &= ~(TIOCM_DTR | TIOCM_RTS);
872 mct_u232_set_modem_ctrl(serial, priv->control_state);
873#endif
874 ;
875 }
Johan Hovoldabf492e2009-12-28 23:01:52 +0100876 memcpy(cfg, &priv->cfg, sizeof(*cfg));
Alan Cox0c265f42008-07-22 11:14:00 +0100877 spin_unlock_irqrestore(&priv->lock, flags);
878
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 /* now commit changes to device */
Johan Hovoldabf492e2009-12-28 23:01:52 +0100880 klsi_105_chg_port_settings(port, cfg);
881err:
882 kfree(cfg);
Johan Hovoldff8c1952010-05-19 00:01:39 +0200883}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
885#if 0
Alan Cox0c265f42008-07-22 11:14:00 +0100886static void mct_u232_break_ctl(struct tty_struct *tty, int break_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887{
Alan Cox95da3102008-07-22 11:09:07 +0100888 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 struct usb_serial *serial = port->serial;
Alan Cox0c265f42008-07-22 11:14:00 +0100890 struct mct_u232_private *priv =
891 (struct mct_u232_private *)port->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 unsigned char lcr = priv->last_lcr;
893
Harvey Harrison441b62c2008-03-03 16:08:34 -0800894 dbg("%sstate=%d", __func__, break_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
Alan Cox6b447f042009-01-02 13:48:56 +0000896 /* LOCKING */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 if (break_state)
898 lcr |= MCT_U232_SET_BREAK;
899
900 mct_u232_set_line_ctrl(serial, lcr);
Johan Hovoldff8c1952010-05-19 00:01:39 +0200901}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902#endif
903
Alan Cox0c265f42008-07-22 11:14:00 +0100904static int klsi_105_tiocmget(struct tty_struct *tty, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905{
Alan Cox95da3102008-07-22 11:09:07 +0100906 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 struct klsi_105_private *priv = usb_get_serial_port_data(port);
908 unsigned long flags;
909 int rc;
910 unsigned long line_state;
Harvey Harrison441b62c2008-03-03 16:08:34 -0800911 dbg("%s - request, just guessing", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
913 rc = klsi_105_get_line_state(port, &line_state);
914 if (rc < 0) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700915 dev_err(&port->dev,
916 "Reading line control failed (error = %d)\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 /* better return value? EAGAIN? */
918 return rc;
919 }
920
Alan Cox0c265f42008-07-22 11:14:00 +0100921 spin_lock_irqsave(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 priv->line_state = line_state;
Alan Cox0c265f42008-07-22 11:14:00 +0100923 spin_unlock_irqrestore(&priv->lock, flags);
Harvey Harrison441b62c2008-03-03 16:08:34 -0800924 dbg("%s - read line state 0x%lx", __func__, line_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 return (int)line_state;
926}
927
Alan Cox0c265f42008-07-22 11:14:00 +0100928static int klsi_105_tiocmset(struct tty_struct *tty, struct file *file,
929 unsigned int set, unsigned int clear)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930{
931 int retval = -EINVAL;
Alan Cox0c265f42008-07-22 11:14:00 +0100932
Harvey Harrison441b62c2008-03-03 16:08:34 -0800933 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
935/* if this ever gets implemented, it should be done something like this:
936 struct usb_serial *serial = port->serial;
937 struct klsi_105_private *priv = usb_get_serial_port_data(port);
938 unsigned long flags;
939 int control;
940
941 spin_lock_irqsave (&priv->lock, flags);
942 if (set & TIOCM_RTS)
943 priv->control_state |= TIOCM_RTS;
944 if (set & TIOCM_DTR)
945 priv->control_state |= TIOCM_DTR;
946 if (clear & TIOCM_RTS)
947 priv->control_state &= ~TIOCM_RTS;
948 if (clear & TIOCM_DTR)
949 priv->control_state &= ~TIOCM_DTR;
950 control = priv->control_state;
951 spin_unlock_irqrestore (&priv->lock, flags);
952 retval = mct_u232_set_modem_ctrl(serial, control);
953*/
954 return retval;
955}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
Alan Cox0c265f42008-07-22 11:14:00 +0100957static void klsi_105_throttle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958{
Alan Cox95da3102008-07-22 11:09:07 +0100959 struct usb_serial_port *port = tty->driver_data;
Harvey Harrison441b62c2008-03-03 16:08:34 -0800960 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 usb_kill_urb(port->read_urb);
962}
963
Alan Cox0c265f42008-07-22 11:14:00 +0100964static void klsi_105_unthrottle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965{
Alan Cox95da3102008-07-22 11:09:07 +0100966 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 int result;
968
Harvey Harrison441b62c2008-03-03 16:08:34 -0800969 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
971 port->read_urb->dev = port->serial->dev;
Oliver Neukum63832512009-10-07 10:50:23 +0200972 result = usb_submit_urb(port->read_urb, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 if (result)
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700974 dev_err(&port->dev,
975 "%s - failed submitting read urb, error %d\n",
976 __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977}
978
979
Alan Cox0c265f42008-07-22 11:14:00 +0100980static int __init klsi_105_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981{
982 int retval;
983 retval = usb_serial_register(&kl5kusb105d_device);
984 if (retval)
985 goto failed_usb_serial_register;
986 retval = usb_register(&kl5kusb105d_driver);
987 if (retval)
988 goto failed_usb_register;
989
Greg Kroah-Hartmanc197a8d2008-08-18 13:21:04 -0700990 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
991 DRIVER_DESC "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 return 0;
993failed_usb_register:
994 usb_serial_deregister(&kl5kusb105d_device);
995failed_usb_serial_register:
996 return retval;
997}
998
Alan Cox0c265f42008-07-22 11:14:00 +0100999static void __exit klsi_105_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000{
Alan Cox0c265f42008-07-22 11:14:00 +01001001 usb_deregister(&kl5kusb105d_driver);
1002 usb_serial_deregister(&kl5kusb105d_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003}
1004
1005
Alan Cox0c265f42008-07-22 11:14:00 +01001006module_init(klsi_105_init);
1007module_exit(klsi_105_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
Alan Cox0c265f42008-07-22 11:14:00 +01001009MODULE_AUTHOR(DRIVER_AUTHOR);
1010MODULE_DESCRIPTION(DRIVER_DESC);
1011MODULE_LICENSE("GPL");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
1013
1014module_param(debug, bool, S_IRUGO | S_IWUSR);
1015MODULE_PARM_DESC(debug, "enable extensive debugging messages");