blob: fa817c66b3e8c827bb6ce0837591f2406dd73f70 [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);
76static void klsi_105_shutdown(struct usb_serial *serial);
77static int klsi_105_open(struct tty_struct *tty,
78 struct usb_serial_port *port, struct file *filp);
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 */
97static struct usb_device_id id_table [] = {
98 { 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,
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800110 .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 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700118 .description = "KL5KUSB105D / PalmConnect",
Johannes Hölzld9b1b782006-12-17 21:50:24 +0100119 .usb_driver = &kl5kusb105d_driver,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 .id_table = id_table,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 .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,
Alan Cox0c265f42008-07-22 11:14:00 +0100128 .read_bulk_callback = klsi_105_read_bulk_callback,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 .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 .shutdown = klsi_105_shutdown,
135 .throttle = klsi_105_throttle,
136 .unthrottle = klsi_105_unthrottle,
137};
138
139struct klsi_105_port_settings {
140 __u8 pktlen; /* always 5, it seems */
141 __u8 baudrate;
142 __u8 databits;
143 __u8 unknown1;
144 __u8 unknown2;
145} __attribute__ ((packed));
146
147/* we implement a pool of NUM_URBS urbs per usb_serial */
148#define NUM_URBS 1
149#define URB_TRANSFER_BUFFER_SIZE 64
150struct klsi_105_private {
151 struct klsi_105_port_settings cfg;
Alan Cox606d0992006-12-08 02:38:45 -0800152 struct ktermios termios;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 unsigned long line_state; /* modem line settings */
154 /* write pool */
Alan Cox0c265f42008-07-22 11:14:00 +0100155 struct urb *write_urb_pool[NUM_URBS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 spinlock_t lock;
157 unsigned long bytes_in;
158 unsigned long bytes_out;
159};
160
161
162/*
163 * Handle vendor specific USB requests
164 */
165
166
167#define KLSI_TIMEOUT 5000 /* default urb timeout */
168
169static int klsi_105_chg_port_settings(struct usb_serial_port *port,
170 struct klsi_105_port_settings *settings)
171{
172 int rc;
173
Alan Cox0c265f42008-07-22 11:14:00 +0100174 rc = usb_control_msg(port->serial->dev,
175 usb_sndctrlpipe(port->serial->dev, 0),
176 KL5KUSB105A_SIO_SET_DATA,
177 USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_INTERFACE,
178 0, /* value */
179 0, /* index */
180 settings,
181 sizeof(struct klsi_105_port_settings),
182 KLSI_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 if (rc < 0)
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700184 dev_err(&port->dev,
185 "Change port settings failed (error = %d)\n", rc);
Greg Kroah-Hartmanc197a8d2008-08-18 13:21:04 -0700186 dev_info(&port->serial->dev->dev,
187 "%d byte block, baudrate %x, databits %d, u1 %d, u2 %d\n",
188 settings->pktlen, settings->baudrate, settings->databits,
189 settings->unknown1, settings->unknown2);
Alan Cox0c265f42008-07-22 11:14:00 +0100190 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191} /* klsi_105_chg_port_settings */
192
193/* translate a 16-bit status value from the device to linux's TIO bits */
194static unsigned long klsi_105_status2linestate(const __u16 status)
195{
196 unsigned long res = 0;
197
198 res = ((status & KL5KUSB105A_DSR) ? TIOCM_DSR : 0)
199 | ((status & KL5KUSB105A_CTS) ? TIOCM_CTS : 0)
200 ;
201
202 return res;
203}
Alan Cox0c265f42008-07-22 11:14:00 +0100204/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 * Read line control via vendor command and return result through
Alan Cox0c265f42008-07-22 11:14:00 +0100206 * *line_state_p
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 */
208/* It seems that the status buffer has always only 2 bytes length */
209#define KLSI_STATUSBUF_LEN 2
210static int klsi_105_get_line_state(struct usb_serial_port *port,
211 unsigned long *line_state_p)
212{
213 int rc;
Alan Cox0c265f42008-07-22 11:14:00 +0100214 __u8 status_buf[KLSI_STATUSBUF_LEN] = { -1, -1};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 __u16 status;
216
Greg Kroah-Hartmanc197a8d2008-08-18 13:21:04 -0700217 dev_info(&port->serial->dev->dev, "sending SIO Poll request\n");
Alan Cox0c265f42008-07-22 11:14:00 +0100218 rc = usb_control_msg(port->serial->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 usb_rcvctrlpipe(port->serial->dev, 0),
220 KL5KUSB105A_SIO_POLL,
Alan Cox0c265f42008-07-22 11:14:00 +0100221 USB_TYPE_VENDOR | USB_DIR_IN,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 0, /* value */
223 0, /* index */
224 status_buf, KLSI_STATUSBUF_LEN,
225 10000
226 );
227 if (rc < 0)
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700228 dev_err(&port->dev, "Reading line status failed (error = %d)\n",
229 rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 else {
Harvey Harrisonbd2c7842008-05-01 20:52:57 -0700231 status = get_unaligned_le16(status_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Greg Kroah-Hartmanc197a8d2008-08-18 13:21:04 -0700233 dev_info(&port->serial->dev->dev, "read status %x %x",
234 status_buf[0], status_buf[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236 *line_state_p = klsi_105_status2linestate(status);
237 }
Alan Cox0c265f42008-07-22 11:14:00 +0100238 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239}
240
241
242/*
243 * Driver's tty interface functions
244 */
245
Alan Cox0c265f42008-07-22 11:14:00 +0100246static int klsi_105_startup(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
248 struct klsi_105_private *priv;
Oliver Neukum9306fff2007-03-29 11:23:54 +0200249 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
251 /* check if we support the product id (see keyspan.c)
252 * FIXME
253 */
254
255 /* allocate the private data structure */
Alan Cox0c265f42008-07-22 11:14:00 +0100256 for (i = 0; i < serial->num_ports; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 priv = kmalloc(sizeof(struct klsi_105_private),
258 GFP_KERNEL);
259 if (!priv) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800260 dbg("%skmalloc for klsi_105_private failed.", __func__);
Oliver Neukum9306fff2007-03-29 11:23:54 +0200261 i--;
262 goto err_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 }
264 /* set initial values for control structures */
265 priv->cfg.pktlen = 5;
266 priv->cfg.baudrate = kl5kusb105a_sio_b9600;
267 priv->cfg.databits = kl5kusb105a_dtb_8;
268 priv->cfg.unknown1 = 0;
269 priv->cfg.unknown2 = 1;
270
271 priv->line_state = 0;
272
273 priv->bytes_in = 0;
274 priv->bytes_out = 0;
275 usb_set_serial_port_data(serial->port[i], priv);
276
Alan Cox0c265f42008-07-22 11:14:00 +0100277 spin_lock_init(&priv->lock);
278 for (j = 0; j < NUM_URBS; j++) {
279 struct urb *urb = usb_alloc_urb(0, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
281 priv->write_urb_pool[j] = urb;
282 if (urb == NULL) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700283 dev_err(&serial->dev->dev, "No more urbs???\n");
Oliver Neukum9306fff2007-03-29 11:23:54 +0200284 goto err_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 }
286
Alan Cox0c265f42008-07-22 11:14:00 +0100287 urb->transfer_buffer =
288 kmalloc(URB_TRANSFER_BUFFER_SIZE, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 if (!urb->transfer_buffer) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700290 dev_err(&serial->dev->dev,
291 "%s - out of memory for urb buffers.\n",
292 __func__);
Oliver Neukum9306fff2007-03-29 11:23:54 +0200293 goto err_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 }
295 }
296
297 /* priv->termios is left uninitalized until port opening */
298 init_waitqueue_head(&serial->port[i]->write_wait);
299 }
Alan Cox0c265f42008-07-22 11:14:00 +0100300
Oliver Neukum9306fff2007-03-29 11:23:54 +0200301 return 0;
302
303err_cleanup:
304 for (; i >= 0; i--) {
305 priv = usb_get_serial_port_data(serial->port[i]);
Alan Cox0c265f42008-07-22 11:14:00 +0100306 for (j = 0; j < NUM_URBS; j++) {
Oliver Neukum9306fff2007-03-29 11:23:54 +0200307 if (priv->write_urb_pool[j]) {
308 kfree(priv->write_urb_pool[j]->transfer_buffer);
309 usb_free_urb(priv->write_urb_pool[j]);
310 }
311 }
312 usb_set_serial_port_data(serial->port[i], NULL);
313 }
314 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315} /* klsi_105_startup */
316
317
Alan Cox0c265f42008-07-22 11:14:00 +0100318static void klsi_105_shutdown(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319{
320 int i;
Alan Cox0c265f42008-07-22 11:14:00 +0100321
Harvey Harrison441b62c2008-03-03 16:08:34 -0800322 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
324 /* stop reads and writes on all ports */
Alan Cox0c265f42008-07-22 11:14:00 +0100325 for (i = 0; i < serial->num_ports; ++i) {
326 struct klsi_105_private *priv =
327 usb_get_serial_port_data(serial->port[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 unsigned long flags;
329
330 if (priv) {
331 /* kill our write urb pool */
332 int j;
333 struct urb **write_urbs = priv->write_urb_pool;
Alan Cox0c265f42008-07-22 11:14:00 +0100334 spin_lock_irqsave(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
336 for (j = 0; j < NUM_URBS; j++) {
337 if (write_urbs[j]) {
338 /* FIXME - uncomment the following
339 * usb_kill_urb call when the host
340 * controllers get fixed to set
341 * urb->dev = NULL after the urb is
342 * finished. Otherwise this call
343 * oopses. */
344 /* usb_kill_urb(write_urbs[j]); */
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -0700345 kfree(write_urbs[j]->transfer_buffer);
Alan Cox0c265f42008-07-22 11:14:00 +0100346 usb_free_urb(write_urbs[j]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 }
348 }
Alan Cox0c265f42008-07-22 11:14:00 +0100349 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 kfree(priv);
351 usb_set_serial_port_data(serial->port[i], NULL);
352 }
353 }
354} /* klsi_105_shutdown */
355
Alan Cox95da3102008-07-22 11:09:07 +0100356static int klsi_105_open(struct tty_struct *tty,
357 struct usb_serial_port *port, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358{
359 struct klsi_105_private *priv = usb_get_serial_port_data(port);
360 int retval = 0;
361 int rc;
362 int i;
363 unsigned long line_state;
364 struct klsi_105_port_settings cfg;
365 unsigned long flags;
366
Harvey Harrison441b62c2008-03-03 16:08:34 -0800367 dbg("%s port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
369 /* force low_latency on so that our tty_push actually forces
370 * the data through
Alan Cox95da3102008-07-22 11:09:07 +0100371 * tty->low_latency = 1; */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
373 /* Do a defined restart:
374 * Set up sane default baud rate and send the 'READ_ON'
Alan Cox0c265f42008-07-22 11:14:00 +0100375 * vendor command.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 * FIXME: set modem line control (how?)
377 * Then read the modem line control and store values in
378 * priv->line_state.
379 */
380 cfg.pktlen = 5;
381 cfg.baudrate = kl5kusb105a_sio_b9600;
382 cfg.databits = kl5kusb105a_dtb_8;
383 cfg.unknown1 = 0;
384 cfg.unknown2 = 1;
385 klsi_105_chg_port_settings(port, &cfg);
Alan Cox0c265f42008-07-22 11:14:00 +0100386
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 /* set up termios structure */
Alan Cox0c265f42008-07-22 11:14:00 +0100388 spin_lock_irqsave(&priv->lock, flags);
Alan Cox95da3102008-07-22 11:09:07 +0100389 priv->termios.c_iflag = tty->termios->c_iflag;
390 priv->termios.c_oflag = tty->termios->c_oflag;
391 priv->termios.c_cflag = tty->termios->c_cflag;
392 priv->termios.c_lflag = tty->termios->c_lflag;
Alan Cox0c265f42008-07-22 11:14:00 +0100393 for (i = 0; i < NCCS; i++)
Alan Cox95da3102008-07-22 11:09:07 +0100394 priv->termios.c_cc[i] = tty->termios->c_cc[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 priv->cfg.pktlen = cfg.pktlen;
396 priv->cfg.baudrate = cfg.baudrate;
397 priv->cfg.databits = cfg.databits;
398 priv->cfg.unknown1 = cfg.unknown1;
399 priv->cfg.unknown2 = cfg.unknown2;
Alan Cox0c265f42008-07-22 11:14:00 +0100400 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
402 /* READ_ON and urb submission */
Alan Cox0c265f42008-07-22 11:14:00 +0100403 usb_fill_bulk_urb(port->read_urb, port->serial->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 usb_rcvbulkpipe(port->serial->dev,
405 port->bulk_in_endpointAddress),
406 port->read_urb->transfer_buffer,
407 port->read_urb->transfer_buffer_length,
408 klsi_105_read_bulk_callback,
409 port);
410
411 rc = usb_submit_urb(port->read_urb, GFP_KERNEL);
412 if (rc) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700413 dev_err(&port->dev, "%s - failed submitting read urb, "
414 "error %d\n", __func__, rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 retval = rc;
416 goto exit;
417 }
418
419 rc = usb_control_msg(port->serial->dev,
Alan Cox0c265f42008-07-22 11:14:00 +0100420 usb_sndctrlpipe(port->serial->dev, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 KL5KUSB105A_SIO_CONFIGURE,
422 USB_TYPE_VENDOR|USB_DIR_OUT|USB_RECIP_INTERFACE,
423 KL5KUSB105A_SIO_CONFIGURE_READ_ON,
424 0, /* index */
425 NULL,
426 0,
427 KLSI_TIMEOUT);
428 if (rc < 0) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700429 dev_err(&port->dev, "Enabling read failed (error = %d)\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 retval = rc;
Alan Cox0c265f42008-07-22 11:14:00 +0100431 } else
Harvey Harrison441b62c2008-03-03 16:08:34 -0800432 dbg("%s - enabled reading", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
434 rc = klsi_105_get_line_state(port, &line_state);
435 if (rc >= 0) {
Alan Cox0c265f42008-07-22 11:14:00 +0100436 spin_lock_irqsave(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 priv->line_state = line_state;
Alan Cox0c265f42008-07-22 11:14:00 +0100438 spin_unlock_irqrestore(&priv->lock, flags);
Harvey Harrison441b62c2008-03-03 16:08:34 -0800439 dbg("%s - read line state 0x%lx", __func__, line_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 retval = 0;
441 } else
442 retval = rc;
443
444exit:
445 return retval;
446} /* klsi_105_open */
447
448
Alan Cox335f8512009-06-11 12:26:29 +0100449static void klsi_105_close(struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450{
451 struct klsi_105_private *priv = usb_get_serial_port_data(port);
452 int rc;
453
Harvey Harrison441b62c2008-03-03 16:08:34 -0800454 dbg("%s port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Oliver Neukum3edbc982008-01-22 12:47:15 +0100456 mutex_lock(&port->serial->disc_mutex);
457 if (!port->serial->disconnected) {
458 /* send READ_OFF */
Alan Cox0c265f42008-07-22 11:14:00 +0100459 rc = usb_control_msg(port->serial->dev,
460 usb_sndctrlpipe(port->serial->dev, 0),
461 KL5KUSB105A_SIO_CONFIGURE,
462 USB_TYPE_VENDOR | USB_DIR_OUT,
463 KL5KUSB105A_SIO_CONFIGURE_READ_OFF,
464 0, /* index */
465 NULL, 0,
466 KLSI_TIMEOUT);
Oliver Neukum3edbc982008-01-22 12:47:15 +0100467 if (rc < 0)
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700468 dev_err(&port->dev,
469 "Disabling read failed (error = %d)\n", rc);
Oliver Neukum3edbc982008-01-22 12:47:15 +0100470 }
471 mutex_unlock(&port->serial->disc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
473 /* shutdown our bulk reads and writes */
474 usb_kill_urb(port->write_urb);
475 usb_kill_urb(port->read_urb);
476 /* unlink our write pool */
477 /* FIXME */
478 /* wgg - do I need this? I think so. */
479 usb_kill_urb(port->interrupt_in_urb);
Greg Kroah-Hartmanc197a8d2008-08-18 13:21:04 -0700480 dev_info(&port->serial->dev->dev,
481 "port stats: %ld bytes in, %ld bytes out\n",
482 priv->bytes_in, priv->bytes_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483} /* klsi_105_close */
484
485
486/* We need to write a complete 64-byte data block and encode the
Alan Cox0c265f42008-07-22 11:14:00 +0100487 * number actually sent in the first double-byte, LSB-order. That
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 * leaves at most 62 bytes of payload.
489 */
490#define KLSI_105_DATA_OFFSET 2 /* in the bulk urb data block */
491
492
Alan Cox95da3102008-07-22 11:09:07 +0100493static int klsi_105_write(struct tty_struct *tty,
494 struct usb_serial_port *port, const unsigned char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495{
496 struct klsi_105_private *priv = usb_get_serial_port_data(port);
497 int result, size;
Alan Cox0c265f42008-07-22 11:14:00 +0100498 int bytes_sent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Harvey Harrison441b62c2008-03-03 16:08:34 -0800500 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
502 while (count > 0) {
503 /* try to find a free urb (write 0 bytes if none) */
504 struct urb *urb = NULL;
505 unsigned long flags;
506 int i;
Alan Cox0c265f42008-07-22 11:14:00 +0100507 /* since the pool is per-port we might not need
508 the spin lock !? */
509 spin_lock_irqsave(&priv->lock, flags);
510 for (i = 0; i < NUM_URBS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 if (priv->write_urb_pool[i]->status != -EINPROGRESS) {
512 urb = priv->write_urb_pool[i];
Harvey Harrison441b62c2008-03-03 16:08:34 -0800513 dbg("%s - using pool URB %d", __func__, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 break;
515 }
516 }
Alan Cox0c265f42008-07-22 11:14:00 +0100517 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Alan Cox0c265f42008-07-22 11:14:00 +0100519 if (urb == NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800520 dbg("%s - no more free urbs", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 goto exit;
522 }
523
524 if (urb->transfer_buffer == NULL) {
Alan Cox0c265f42008-07-22 11:14:00 +0100525 urb->transfer_buffer =
526 kmalloc(URB_TRANSFER_BUFFER_SIZE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 if (urb->transfer_buffer == NULL) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700528 dev_err(&port->dev,
529 "%s - no more kernel memory...\n",
530 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 goto exit;
532 }
533 }
534
Alan Cox0c265f42008-07-22 11:14:00 +0100535 size = min(count, port->bulk_out_size - KLSI_105_DATA_OFFSET);
536 size = min(size, URB_TRANSFER_BUFFER_SIZE -
537 KLSI_105_DATA_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
Alan Cox0c265f42008-07-22 11:14:00 +0100539 memcpy(urb->transfer_buffer + KLSI_105_DATA_OFFSET, buf, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
541 /* write payload size into transfer buffer */
542 ((__u8 *)urb->transfer_buffer)[0] = (__u8) (size & 0xFF);
543 ((__u8 *)urb->transfer_buffer)[1] = (__u8) ((size & 0xFF00)>>8);
544
545 /* set up our urb */
546 usb_fill_bulk_urb(urb, port->serial->dev,
547 usb_sndbulkpipe(port->serial->dev,
548 port->bulk_out_endpointAddress),
549 urb->transfer_buffer,
550 URB_TRANSFER_BUFFER_SIZE,
551 klsi_105_write_bulk_callback,
552 port);
553
554 /* send the data out the bulk port */
555 result = usb_submit_urb(urb, GFP_ATOMIC);
556 if (result) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700557 dev_err(&port->dev,
558 "%s - failed submitting write urb, error %d\n",
559 __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 goto exit;
561 }
562 buf += size;
563 bytes_sent += size;
564 count -= size;
565 }
566exit:
567 /* lockless, but it's for debug info only... */
Alan Cox0c265f42008-07-22 11:14:00 +0100568 priv->bytes_out += bytes_sent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
570 return bytes_sent; /* that's how much we wrote */
571} /* klsi_105_write */
572
Alan Cox0c265f42008-07-22 11:14:00 +0100573static void klsi_105_write_bulk_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574{
Ming Leicdc97792008-02-24 18:41:47 +0800575 struct usb_serial_port *port = urb->context;
Greg Kroah-Hartman17c1b352007-06-15 15:44:13 -0700576 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
Harvey Harrison441b62c2008-03-03 16:08:34 -0800578 dbg("%s - port %d", __func__, port->number);
Greg Kroah-Hartman17c1b352007-06-15 15:44:13 -0700579
580 if (status) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800581 dbg("%s - nonzero write bulk status received: %d", __func__,
Greg Kroah-Hartman17c1b352007-06-15 15:44:13 -0700582 status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 return;
584 }
585
Pete Zaitcevcf2c7482006-05-22 21:58:49 -0700586 usb_serial_port_softint(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587} /* klsi_105_write_bulk_completion_callback */
588
589
590/* return number of characters currently in the writing process */
Alan Cox0c265f42008-07-22 11:14:00 +0100591static int klsi_105_chars_in_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592{
Alan Cox95da3102008-07-22 11:09:07 +0100593 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 int chars = 0;
595 int i;
596 unsigned long flags;
597 struct klsi_105_private *priv = usb_get_serial_port_data(port);
598
Alan Cox0c265f42008-07-22 11:14:00 +0100599 spin_lock_irqsave(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
601 for (i = 0; i < NUM_URBS; ++i) {
Alan Cox0c265f42008-07-22 11:14:00 +0100602 if (priv->write_urb_pool[i]->status == -EINPROGRESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 chars += URB_TRANSFER_BUFFER_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 }
605
Alan Cox0c265f42008-07-22 11:14:00 +0100606 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
Harvey Harrison441b62c2008-03-03 16:08:34 -0800608 dbg("%s - returns %d", __func__, chars);
Alan Cox0c265f42008-07-22 11:14:00 +0100609 return chars;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610}
611
Alan Cox0c265f42008-07-22 11:14:00 +0100612static int klsi_105_write_room(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613{
Alan Cox95da3102008-07-22 11:09:07 +0100614 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 unsigned long flags;
616 int i;
617 int room = 0;
618 struct klsi_105_private *priv = usb_get_serial_port_data(port);
619
Alan Cox0c265f42008-07-22 11:14:00 +0100620 spin_lock_irqsave(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 for (i = 0; i < NUM_URBS; ++i) {
Alan Cox0c265f42008-07-22 11:14:00 +0100622 if (priv->write_urb_pool[i]->status != -EINPROGRESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 room += URB_TRANSFER_BUFFER_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 }
625
Alan Cox0c265f42008-07-22 11:14:00 +0100626 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Harvey Harrison441b62c2008-03-03 16:08:34 -0800628 dbg("%s - returns %d", __func__, room);
Alan Cox0c265f42008-07-22 11:14:00 +0100629 return room;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630}
631
632
633
Alan Cox0c265f42008-07-22 11:14:00 +0100634static void klsi_105_read_bulk_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635{
Ming Leicdc97792008-02-24 18:41:47 +0800636 struct usb_serial_port *port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 struct klsi_105_private *priv = usb_get_serial_port_data(port);
638 struct tty_struct *tty;
639 unsigned char *data = urb->transfer_buffer;
640 int rc;
Greg Kroah-Hartman17c1b352007-06-15 15:44:13 -0700641 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
Harvey Harrison441b62c2008-03-03 16:08:34 -0800643 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
645 /* The urb might have been killed. */
Greg Kroah-Hartman17c1b352007-06-15 15:44:13 -0700646 if (status) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800647 dbg("%s - nonzero read bulk status received: %d", __func__,
Greg Kroah-Hartman17c1b352007-06-15 15:44:13 -0700648 status);
649 return;
650 }
651
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 /* The data received is again preceded by a length double-byte in LSB-
653 * first order (see klsi_105_write() )
654 */
655 if (urb->actual_length == 0) {
656 /* empty urbs seem to happen, we ignore them */
Harvey Harrison441b62c2008-03-03 16:08:34 -0800657 /* dbg("%s - emtpy URB", __func__); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 ;
659 } else if (urb->actual_length <= 2) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800660 dbg("%s - size %d URB not understood", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 urb->actual_length);
Harvey Harrison441b62c2008-03-03 16:08:34 -0800662 usb_serial_debug_data(debug, &port->dev, __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 urb->actual_length, data);
664 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 int bytes_sent = ((__u8 *) data)[0] +
666 ((unsigned int) ((__u8 *) data)[1] << 8);
Alan Cox4a90f092008-10-13 10:39:46 +0100667 tty = tty_port_tty_get(&port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 /* we should immediately resubmit the URB, before attempting
669 * to pass the data on to the tty layer. But that needs locking
670 * against re-entry an then mixed-up data because of
671 * intermixed tty_flip_buffer_push()s
672 * FIXME
Alan Cox0c265f42008-07-22 11:14:00 +0100673 */
Harvey Harrison441b62c2008-03-03 16:08:34 -0800674 usb_serial_debug_data(debug, &port->dev, __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 urb->actual_length, data);
676
677 if (bytes_sent + 2 > urb->actual_length) {
678 dbg("%s - trying to read more data than available"
Harvey Harrison441b62c2008-03-03 16:08:34 -0800679 " (%d vs. %d)", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 bytes_sent+2, urb->actual_length);
681 /* cap at implied limit */
682 bytes_sent = urb->actual_length - 2;
683 }
684
Alan Cox33f0f882006-01-09 20:54:13 -0800685 tty_buffer_request_room(tty, bytes_sent);
686 tty_insert_flip_string(tty, data + 2, bytes_sent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 tty_flip_buffer_push(tty);
Alan Cox4a90f092008-10-13 10:39:46 +0100688 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
690 /* again lockless, but debug info only */
691 priv->bytes_in += bytes_sent;
692 }
693 /* Continue trying to always read */
Alan Cox0c265f42008-07-22 11:14:00 +0100694 usb_fill_bulk_urb(port->read_urb, port->serial->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 usb_rcvbulkpipe(port->serial->dev,
696 port->bulk_in_endpointAddress),
697 port->read_urb->transfer_buffer,
698 port->read_urb->transfer_buffer_length,
699 klsi_105_read_bulk_callback,
700 port);
701 rc = usb_submit_urb(port->read_urb, GFP_ATOMIC);
702 if (rc)
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700703 dev_err(&port->dev,
704 "%s - failed resubmitting read urb, error %d\n",
705 __func__, rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706} /* klsi_105_read_bulk_callback */
707
708
Alan Cox0c265f42008-07-22 11:14:00 +0100709static void klsi_105_set_termios(struct tty_struct *tty,
710 struct usb_serial_port *port,
711 struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712{
713 struct klsi_105_private *priv = usb_get_serial_port_data(port);
Alan Coxa5b6f602008-04-08 17:16:06 +0100714 unsigned int iflag = tty->termios->c_iflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 unsigned int old_iflag = old_termios->c_iflag;
Alan Coxa5b6f602008-04-08 17:16:06 +0100716 unsigned int cflag = tty->termios->c_cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 unsigned int old_cflag = old_termios->c_cflag;
718 struct klsi_105_port_settings cfg;
719 unsigned long flags;
Alan Coxa5b6f602008-04-08 17:16:06 +0100720 speed_t baud;
Alan Cox0c265f42008-07-22 11:14:00 +0100721
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 /* lock while we are modifying the settings */
Alan Cox0c265f42008-07-22 11:14:00 +0100723 spin_lock_irqsave(&priv->lock, flags);
724
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 /*
726 * Update baud rate
727 */
Alan Coxa5b6f602008-04-08 17:16:06 +0100728 baud = tty_get_baud_rate(tty);
729
Alan Cox0c265f42008-07-22 11:14:00 +0100730 if ((cflag & CBAUD) != (old_cflag & CBAUD)) {
731 /* reassert DTR and (maybe) RTS on transition from B0 */
732 if ((old_cflag & CBAUD) == B0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800733 dbg("%s: baud was B0", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734#if 0
735 priv->control_state |= TIOCM_DTR;
736 /* don't set RTS if using hardware flow control */
Alan Cox0c265f42008-07-22 11:14:00 +0100737 if (!(old_cflag & CRTSCTS))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 priv->control_state |= TIOCM_RTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 mct_u232_set_modem_ctrl(serial, priv->control_state);
740#endif
741 }
Alan Coxa5b6f602008-04-08 17:16:06 +0100742 }
Alan Cox0c265f42008-07-22 11:14:00 +0100743 switch (baud) {
744 case 0: /* handled below */
745 break;
746 case 1200:
747 priv->cfg.baudrate = kl5kusb105a_sio_b1200;
748 break;
749 case 2400:
750 priv->cfg.baudrate = kl5kusb105a_sio_b2400;
751 break;
752 case 4800:
753 priv->cfg.baudrate = kl5kusb105a_sio_b4800;
754 break;
755 case 9600:
756 priv->cfg.baudrate = kl5kusb105a_sio_b9600;
757 break;
758 case 19200:
759 priv->cfg.baudrate = kl5kusb105a_sio_b19200;
760 break;
761 case 38400:
762 priv->cfg.baudrate = kl5kusb105a_sio_b38400;
763 break;
764 case 57600:
765 priv->cfg.baudrate = kl5kusb105a_sio_b57600;
766 break;
767 case 115200:
768 priv->cfg.baudrate = kl5kusb105a_sio_b115200;
769 break;
770 default:
771 dbg("KLSI USB->Serial converter:"
772 " unsupported baudrate request, using default of 9600");
Alan Cox3f6ff6e2007-08-10 14:53:34 -0700773 priv->cfg.baudrate = kl5kusb105a_sio_b9600;
Alan Cox0c265f42008-07-22 11:14:00 +0100774 baud = 9600;
775 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 }
Alan Cox0c265f42008-07-22 11:14:00 +0100777 if ((cflag & CBAUD) == B0) {
Alan Coxa5b6f602008-04-08 17:16:06 +0100778 dbg("%s: baud is B0", __func__);
779 /* Drop RTS and DTR */
780 /* maybe this should be simulated by sending read
781 * disable and read enable messages?
782 */
783 ;
784#if 0
785 priv->control_state &= ~(TIOCM_DTR | TIOCM_RTS);
Alan Cox0c265f42008-07-22 11:14:00 +0100786 mct_u232_set_modem_ctrl(serial, priv->control_state);
Alan Coxa5b6f602008-04-08 17:16:06 +0100787#endif
788 }
789 tty_encode_baud_rate(tty, baud, baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
791 if ((cflag & CSIZE) != (old_cflag & CSIZE)) {
792 /* set the number of data bits */
793 switch (cflag & CSIZE) {
794 case CS5:
Harvey Harrison441b62c2008-03-03 16:08:34 -0800795 dbg("%s - 5 bits/byte not supported", __func__);
Alan Cox0c265f42008-07-22 11:14:00 +0100796 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 return ;
798 case CS6:
Harvey Harrison441b62c2008-03-03 16:08:34 -0800799 dbg("%s - 6 bits/byte not supported", __func__);
Alan Cox0c265f42008-07-22 11:14:00 +0100800 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 return ;
802 case CS7:
803 priv->cfg.databits = kl5kusb105a_dtb_7;
804 break;
805 case CS8:
806 priv->cfg.databits = kl5kusb105a_dtb_8;
807 break;
808 default:
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700809 dev_err(&port->dev,
810 "CSIZE was not CS5-CS8, using default of 8\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 priv->cfg.databits = kl5kusb105a_dtb_8;
812 break;
813 }
814 }
815
816 /*
817 * Update line control register (LCR)
818 */
819 if ((cflag & (PARENB|PARODD)) != (old_cflag & (PARENB|PARODD))
Alan Cox0c265f42008-07-22 11:14:00 +0100820 || (cflag & CSTOPB) != (old_cflag & CSTOPB)) {
Alan Coxa5b6f602008-04-08 17:16:06 +0100821 /* Not currently supported */
822 tty->termios->c_cflag &= ~(PARENB|PARODD|CSTOPB);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823#if 0
824 priv->last_lcr = 0;
825
826 /* set the parity */
827 if (cflag & PARENB)
828 priv->last_lcr |= (cflag & PARODD) ?
829 MCT_U232_PARITY_ODD : MCT_U232_PARITY_EVEN;
830 else
831 priv->last_lcr |= MCT_U232_PARITY_NONE;
832
833 /* set the number of stop bits */
834 priv->last_lcr |= (cflag & CSTOPB) ?
835 MCT_U232_STOP_BITS_2 : MCT_U232_STOP_BITS_1;
836
837 mct_u232_set_line_ctrl(serial, priv->last_lcr);
838#endif
839 ;
840 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 /*
842 * Set flow control: well, I do not really now how to handle DTR/RTS.
843 * Just do what we have seen with SniffUSB on Win98.
844 */
Alan Cox0c265f42008-07-22 11:14:00 +0100845 if ((iflag & IXOFF) != (old_iflag & IXOFF)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 || (iflag & IXON) != (old_iflag & IXON)
Alan Cox0c265f42008-07-22 11:14:00 +0100847 || (cflag & CRTSCTS) != (old_cflag & CRTSCTS)) {
Alan Coxa5b6f602008-04-08 17:16:06 +0100848 /* Not currently supported */
849 tty->termios->c_cflag &= ~CRTSCTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 /* Drop DTR/RTS if no flow control otherwise assert */
851#if 0
Alan Cox0c265f42008-07-22 11:14:00 +0100852 if ((iflag & IXOFF) || (iflag & IXON) || (cflag & CRTSCTS))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 priv->control_state |= TIOCM_DTR | TIOCM_RTS;
854 else
855 priv->control_state &= ~(TIOCM_DTR | TIOCM_RTS);
856 mct_u232_set_modem_ctrl(serial, priv->control_state);
857#endif
858 ;
859 }
Alan Cox0c265f42008-07-22 11:14:00 +0100860 memcpy(&cfg, &priv->cfg, sizeof(cfg));
861 spin_unlock_irqrestore(&priv->lock, flags);
862
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 /* now commit changes to device */
864 klsi_105_chg_port_settings(port, &cfg);
865} /* klsi_105_set_termios */
866
867
868#if 0
Alan Cox0c265f42008-07-22 11:14:00 +0100869static void mct_u232_break_ctl(struct tty_struct *tty, int break_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870{
Alan Cox95da3102008-07-22 11:09:07 +0100871 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 struct usb_serial *serial = port->serial;
Alan Cox0c265f42008-07-22 11:14:00 +0100873 struct mct_u232_private *priv =
874 (struct mct_u232_private *)port->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 unsigned char lcr = priv->last_lcr;
876
Harvey Harrison441b62c2008-03-03 16:08:34 -0800877 dbg("%sstate=%d", __func__, break_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
Alan Cox6b447f042009-01-02 13:48:56 +0000879 /* LOCKING */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 if (break_state)
881 lcr |= MCT_U232_SET_BREAK;
882
883 mct_u232_set_line_ctrl(serial, lcr);
884} /* mct_u232_break_ctl */
885#endif
886
Alan Cox0c265f42008-07-22 11:14:00 +0100887static int klsi_105_tiocmget(struct tty_struct *tty, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888{
Alan Cox95da3102008-07-22 11:09:07 +0100889 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 struct klsi_105_private *priv = usb_get_serial_port_data(port);
891 unsigned long flags;
892 int rc;
893 unsigned long line_state;
Harvey Harrison441b62c2008-03-03 16:08:34 -0800894 dbg("%s - request, just guessing", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
896 rc = klsi_105_get_line_state(port, &line_state);
897 if (rc < 0) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700898 dev_err(&port->dev,
899 "Reading line control failed (error = %d)\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 /* better return value? EAGAIN? */
901 return rc;
902 }
903
Alan Cox0c265f42008-07-22 11:14:00 +0100904 spin_lock_irqsave(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 priv->line_state = line_state;
Alan Cox0c265f42008-07-22 11:14:00 +0100906 spin_unlock_irqrestore(&priv->lock, flags);
Harvey Harrison441b62c2008-03-03 16:08:34 -0800907 dbg("%s - read line state 0x%lx", __func__, line_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 return (int)line_state;
909}
910
Alan Cox0c265f42008-07-22 11:14:00 +0100911static int klsi_105_tiocmset(struct tty_struct *tty, struct file *file,
912 unsigned int set, unsigned int clear)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913{
914 int retval = -EINVAL;
Alan Cox0c265f42008-07-22 11:14:00 +0100915
Harvey Harrison441b62c2008-03-03 16:08:34 -0800916 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
918/* if this ever gets implemented, it should be done something like this:
919 struct usb_serial *serial = port->serial;
920 struct klsi_105_private *priv = usb_get_serial_port_data(port);
921 unsigned long flags;
922 int control;
923
924 spin_lock_irqsave (&priv->lock, flags);
925 if (set & TIOCM_RTS)
926 priv->control_state |= TIOCM_RTS;
927 if (set & TIOCM_DTR)
928 priv->control_state |= TIOCM_DTR;
929 if (clear & TIOCM_RTS)
930 priv->control_state &= ~TIOCM_RTS;
931 if (clear & TIOCM_DTR)
932 priv->control_state &= ~TIOCM_DTR;
933 control = priv->control_state;
934 spin_unlock_irqrestore (&priv->lock, flags);
935 retval = mct_u232_set_modem_ctrl(serial, control);
936*/
937 return retval;
938}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
Alan Cox0c265f42008-07-22 11:14:00 +0100940static void klsi_105_throttle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941{
Alan Cox95da3102008-07-22 11:09:07 +0100942 struct usb_serial_port *port = tty->driver_data;
Harvey Harrison441b62c2008-03-03 16:08:34 -0800943 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 usb_kill_urb(port->read_urb);
945}
946
Alan Cox0c265f42008-07-22 11:14:00 +0100947static void klsi_105_unthrottle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948{
Alan Cox95da3102008-07-22 11:09:07 +0100949 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 int result;
951
Harvey Harrison441b62c2008-03-03 16:08:34 -0800952 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
954 port->read_urb->dev = port->serial->dev;
955 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
956 if (result)
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700957 dev_err(&port->dev,
958 "%s - failed submitting read urb, error %d\n",
959 __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960}
961
962
963
Alan Cox0c265f42008-07-22 11:14:00 +0100964static int __init klsi_105_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965{
966 int retval;
967 retval = usb_serial_register(&kl5kusb105d_device);
968 if (retval)
969 goto failed_usb_serial_register;
970 retval = usb_register(&kl5kusb105d_driver);
971 if (retval)
972 goto failed_usb_register;
973
Greg Kroah-Hartmanc197a8d2008-08-18 13:21:04 -0700974 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
975 DRIVER_DESC "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 return 0;
977failed_usb_register:
978 usb_serial_deregister(&kl5kusb105d_device);
979failed_usb_serial_register:
980 return retval;
981}
982
983
Alan Cox0c265f42008-07-22 11:14:00 +0100984static void __exit klsi_105_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985{
Alan Cox0c265f42008-07-22 11:14:00 +0100986 usb_deregister(&kl5kusb105d_driver);
987 usb_serial_deregister(&kl5kusb105d_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988}
989
990
Alan Cox0c265f42008-07-22 11:14:00 +0100991module_init(klsi_105_init);
992module_exit(klsi_105_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
Alan Cox0c265f42008-07-22 11:14:00 +0100994MODULE_AUTHOR(DRIVER_AUTHOR);
995MODULE_DESCRIPTION(DRIVER_DESC);
996MODULE_LICENSE("GPL");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
998
999module_param(debug, bool, S_IRUGO | S_IWUSR);
1000MODULE_PARM_DESC(debug, "enable extensive debugging messages");
1001
1002/* vim: set sts=8 ts=8 sw=8: */