blob: 2bfd6dd85b5ad23624cef600a2e8e5f76de5fa18 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Belkin USB Serial Adapter Driver
3 *
4 * Copyright (C) 2000 William Greathouse (wgreathouse@smva.com)
5 * Copyright (C) 2000-2001 Greg Kroah-Hartman (greg@kroah.com)
6 *
7 * This program is largely derived from work by the linux-usb group
8 * and associated source files. Please see the usb/serial files for
9 * individual credits and copyrights.
Alan Coxb69c1492008-07-22 11:09:39 +010010 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
Alan Coxb69c1492008-07-22 11:09:39 +010016 * See Documentation/usb/usb-serial.txt for more information on using this
17 * driver
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 *
19 * TODO:
20 * -- Add true modem contol line query capability. Currently we track the
21 * states reported by the interrupt and the states we request.
22 * -- Add error reporting back to application for UART error conditions.
23 * Just point me at how to implement this and I'll do it. I've put the
24 * framework in, but haven't analyzed the "tty_flip" interface yet.
25 * -- Add support for flush commands
26 * -- Add everything that is missing :)
27 *
28 * 27-Nov-2001 gkh
29 * compressed all the differnent device entries into 1.
30 *
31 * 30-May-2001 gkh
Alan Coxb69c1492008-07-22 11:09:39 +010032 * switched from using spinlock to a semaphore, which fixes lots of
33 * problems.
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 *
35 * 08-Apr-2001 gb
36 * - Identify version on module load.
37 *
38 * 12-Mar-2001 gkh
39 * - Added support for the GoHubs GO-COM232 device which is the same as the
40 * Peracom device.
41 *
42 * 06-Nov-2000 gkh
43 * - Added support for the old Belkin and Peracom devices.
44 * - Made the port able to be opened multiple times.
45 * - Added some defaults incase the line settings are things these devices
Alan Coxb69c1492008-07-22 11:09:39 +010046 * can't support.
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 *
48 * 18-Oct-2000 William Greathouse
49 * Released into the wild (linux-usb-devel)
50 *
51 * 17-Oct-2000 William Greathouse
52 * Add code to recognize firmware version and set hardware flow control
53 * appropriately. Belkin states that firmware prior to 3.05 does not
54 * operate correctly in hardware handshake mode. I have verified this
55 * on firmware 2.05 -- for both RTS and DTR input flow control, the control
56 * line is not reset. The test performed by the Belkin Win* driver is
57 * to enable hardware flow control for firmware 2.06 or greater and
58 * for 1.00 or prior. I am only enabling for 2.06 or greater.
59 *
60 * 12-Oct-2000 William Greathouse
61 * First cut at supporting Belkin USB Serial Adapter F5U103
62 * I did not have a copy of the original work to support this
63 * adapter, so pardon any stupid mistakes. All of the information
64 * I am using to write this driver was acquired by using a modified
65 * UsbSnoop on Windows2000 and from examining the other USB drivers.
66 */
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#include <linux/kernel.h>
69#include <linux/errno.h>
70#include <linux/init.h>
71#include <linux/slab.h>
72#include <linux/tty.h>
73#include <linux/tty_driver.h>
74#include <linux/tty_flip.h>
75#include <linux/module.h>
76#include <linux/spinlock.h>
Alan Coxb69c1492008-07-22 11:09:39 +010077#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070078#include <linux/usb.h>
Greg Kroah-Hartmana9698882006-07-11 21:22:58 -070079#include <linux/usb/serial.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070080#include "belkin_sa.h"
81
82static int debug;
83
84/*
85 * Version Information
86 */
87#define DRIVER_VERSION "v1.2"
88#define DRIVER_AUTHOR "William Greathouse <wgreathouse@smva.com>"
89#define DRIVER_DESC "USB Belkin Serial converter driver"
90
91/* function prototypes for a Belkin USB Serial Adapter F5U103 */
Alan Coxb69c1492008-07-22 11:09:39 +010092static int belkin_sa_startup(struct usb_serial *serial);
93static void belkin_sa_shutdown(struct usb_serial *serial);
94static int belkin_sa_open(struct tty_struct *tty,
95 struct usb_serial_port *port, struct file *filp);
Alan Cox335f8512009-06-11 12:26:29 +010096static void belkin_sa_close(struct usb_serial_port *port);
Alan Coxb69c1492008-07-22 11:09:39 +010097static void belkin_sa_read_int_callback(struct urb *urb);
98static void belkin_sa_set_termios(struct tty_struct *tty,
99 struct usb_serial_port *port, struct ktermios * old);
100static void belkin_sa_break_ctl(struct tty_struct *tty, int break_state);
101static int belkin_sa_tiocmget(struct tty_struct *tty, struct file *file);
102static int belkin_sa_tiocmset(struct tty_struct *tty, struct file *file,
103 unsigned int set, unsigned int clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
105
106static struct usb_device_id id_table_combined [] = {
107 { USB_DEVICE(BELKIN_SA_VID, BELKIN_SA_PID) },
108 { USB_DEVICE(BELKIN_OLD_VID, BELKIN_OLD_PID) },
109 { USB_DEVICE(PERACOM_VID, PERACOM_PID) },
110 { USB_DEVICE(GOHUBS_VID, GOHUBS_PID) },
111 { USB_DEVICE(GOHUBS_VID, HANDYLINK_PID) },
112 { USB_DEVICE(BELKIN_DOCKSTATION_VID, BELKIN_DOCKSTATION_PID) },
Alan Coxb69c1492008-07-22 11:09:39 +0100113 { } /* Terminating entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114};
115
Alan Coxb69c1492008-07-22 11:09:39 +0100116MODULE_DEVICE_TABLE(usb, id_table_combined);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118static struct usb_driver belkin_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 .name = "belkin",
120 .probe = usb_serial_probe,
121 .disconnect = usb_serial_disconnect,
122 .id_table = id_table_combined,
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800123 .no_dynamic_id = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124};
125
126/* All of the device info needed for the serial converters */
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700127static struct usb_serial_driver belkin_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700128 .driver = {
129 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700130 .name = "belkin",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700131 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700132 .description = "Belkin / Peracom / GoHubs USB Serial Adapter",
Johannes Hölzld9b1b782006-12-17 21:50:24 +0100133 .usb_driver = &belkin_driver,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 .id_table = id_table_combined,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 .num_ports = 1,
136 .open = belkin_sa_open,
137 .close = belkin_sa_close,
Alan Coxb69c1492008-07-22 11:09:39 +0100138 .read_int_callback = belkin_sa_read_int_callback,
139 /* How we get the status info */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 .set_termios = belkin_sa_set_termios,
141 .break_ctl = belkin_sa_break_ctl,
142 .tiocmget = belkin_sa_tiocmget,
143 .tiocmset = belkin_sa_tiocmset,
144 .attach = belkin_sa_startup,
145 .shutdown = belkin_sa_shutdown,
146};
147
148
149struct belkin_sa_private {
150 spinlock_t lock;
151 unsigned long control_state;
152 unsigned char last_lsr;
153 unsigned char last_msr;
154 int bad_flow_control;
155};
156
157
158/*
159 * ***************************************************************************
160 * Belkin USB Serial Adapter F5U103 specific driver functions
161 * ***************************************************************************
162 */
163
164#define WDR_TIMEOUT 5000 /* default urb timeout */
165
166/* assumes that struct usb_serial *serial is available */
Alan Coxb69c1492008-07-22 11:09:39 +0100167#define BSA_USB_CMD(c, v) usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 (c), BELKIN_SA_SET_REQUEST_TYPE, \
169 (v), 0, NULL, 0, WDR_TIMEOUT)
170
171/* do some startup allocations not currently performed by usb_serial_probe() */
Alan Coxb69c1492008-07-22 11:09:39 +0100172static int belkin_sa_startup(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173{
174 struct usb_device *dev = serial->dev;
175 struct belkin_sa_private *priv;
176
177 /* allocate the private data structure */
178 priv = kmalloc(sizeof(struct belkin_sa_private), GFP_KERNEL);
179 if (!priv)
Alan Coxb69c1492008-07-22 11:09:39 +0100180 return -1; /* error */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 /* set initial values for control structures */
182 spin_lock_init(&priv->lock);
183 priv->control_state = 0;
184 priv->last_lsr = 0;
185 priv->last_msr = 0;
186 /* see comments at top of file */
Alan Coxb69c1492008-07-22 11:09:39 +0100187 priv->bad_flow_control =
188 (le16_to_cpu(dev->descriptor.bcdDevice) <= 0x0206) ? 1 : 0;
Greg Kroah-Hartmanc197a8d2008-08-18 13:21:04 -0700189 dev_info(&dev->dev, "bcdDevice: %04x, bfc: %d\n",
Alan Coxb69c1492008-07-22 11:09:39 +0100190 le16_to_cpu(dev->descriptor.bcdDevice),
191 priv->bad_flow_control);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
193 init_waitqueue_head(&serial->port[0]->write_wait);
194 usb_set_serial_port_data(serial->port[0], priv);
Alan Coxb69c1492008-07-22 11:09:39 +0100195
196 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197}
198
199
Alan Cox95da3102008-07-22 11:09:07 +0100200static void belkin_sa_shutdown(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
202 struct belkin_sa_private *priv;
203 int i;
Alan Coxb69c1492008-07-22 11:09:39 +0100204
205 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
207 /* stop reads and writes on all ports */
Alan Coxb69c1492008-07-22 11:09:39 +0100208 for (i = 0; i < serial->num_ports; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 /* My special items, the standard routines free my urbs */
210 priv = usb_get_serial_port_data(serial->port[i]);
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -0700211 kfree(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 }
213}
214
215
Alan Coxb69c1492008-07-22 11:09:39 +0100216static int belkin_sa_open(struct tty_struct *tty,
217 struct usb_serial_port *port, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
219 int retval = 0;
220
Harvey Harrison441b62c2008-03-03 16:08:34 -0800221 dbg("%s port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
223 /*Start reading from the device*/
224 /* TODO: Look at possibility of submitting multiple URBs to device to
225 * enhance buffering. Win trace shows 16 initial read URBs.
226 */
227 port->read_urb->dev = port->serial->dev;
228 retval = usb_submit_urb(port->read_urb, GFP_KERNEL);
229 if (retval) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700230 dev_err(&port->dev, "usb_submit_urb(read bulk) failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 goto exit;
232 }
233
234 port->interrupt_in_urb->dev = port->serial->dev;
235 retval = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
236 if (retval) {
237 usb_kill_urb(port->read_urb);
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700238 dev_err(&port->dev, "usb_submit_urb(read int) failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 }
240
241exit:
242 return retval;
243} /* belkin_sa_open */
244
245
Alan Cox335f8512009-06-11 12:26:29 +0100246static void belkin_sa_close(struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
Harvey Harrison441b62c2008-03-03 16:08:34 -0800248 dbg("%s port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
250 /* shutdown our bulk reads and writes */
251 usb_kill_urb(port->write_urb);
252 usb_kill_urb(port->read_urb);
253 usb_kill_urb(port->interrupt_in_urb);
254} /* belkin_sa_close */
255
256
Alan Cox95da3102008-07-22 11:09:07 +0100257static void belkin_sa_read_int_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258{
Ming Leicdc97792008-02-24 18:41:47 +0800259 struct usb_serial_port *port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 struct belkin_sa_private *priv;
261 unsigned char *data = urb->transfer_buffer;
262 int retval;
Greg Kroah-Hartmanf26aad22007-06-15 15:44:13 -0700263 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 unsigned long flags;
265
Greg Kroah-Hartmanf26aad22007-06-15 15:44:13 -0700266 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 case 0:
268 /* success */
269 break;
270 case -ECONNRESET:
271 case -ENOENT:
272 case -ESHUTDOWN:
273 /* this urb is terminated, clean up */
Greg Kroah-Hartmanf26aad22007-06-15 15:44:13 -0700274 dbg("%s - urb shutting down with status: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800275 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 return;
277 default:
Greg Kroah-Hartmanf26aad22007-06-15 15:44:13 -0700278 dbg("%s - nonzero urb status received: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800279 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 goto exit;
281 }
282
Alan Coxb69c1492008-07-22 11:09:39 +0100283 usb_serial_debug_data(debug, &port->dev, __func__,
284 urb->actual_length, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286 /* Handle known interrupt data */
287 /* ignore data[0] and data[1] */
288
289 priv = usb_get_serial_port_data(port);
290 spin_lock_irqsave(&priv->lock, flags);
291 priv->last_msr = data[BELKIN_SA_MSR_INDEX];
Alan Coxb69c1492008-07-22 11:09:39 +0100292
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 /* Record Control Line states */
294 if (priv->last_msr & BELKIN_SA_MSR_DSR)
295 priv->control_state |= TIOCM_DSR;
296 else
297 priv->control_state &= ~TIOCM_DSR;
298
299 if (priv->last_msr & BELKIN_SA_MSR_CTS)
300 priv->control_state |= TIOCM_CTS;
301 else
302 priv->control_state &= ~TIOCM_CTS;
303
304 if (priv->last_msr & BELKIN_SA_MSR_RI)
305 priv->control_state |= TIOCM_RI;
306 else
307 priv->control_state &= ~TIOCM_RI;
308
309 if (priv->last_msr & BELKIN_SA_MSR_CD)
310 priv->control_state |= TIOCM_CD;
311 else
312 priv->control_state &= ~TIOCM_CD;
313
314 /* Now to report any errors */
315 priv->last_lsr = data[BELKIN_SA_LSR_INDEX];
316#if 0
317 /*
318 * fill in the flip buffer here, but I do not know the relation
319 * to the current/next receive buffer or characters. I need
320 * to look in to this before committing any code.
321 */
322 if (priv->last_lsr & BELKIN_SA_LSR_ERR) {
Alan Cox4a90f092008-10-13 10:39:46 +0100323 tty = tty_port_tty_get(&port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 /* Overrun Error */
325 if (priv->last_lsr & BELKIN_SA_LSR_OE) {
326 }
327 /* Parity Error */
328 if (priv->last_lsr & BELKIN_SA_LSR_PE) {
329 }
330 /* Framing Error */
331 if (priv->last_lsr & BELKIN_SA_LSR_FE) {
332 }
333 /* Break Indicator */
334 if (priv->last_lsr & BELKIN_SA_LSR_BI) {
335 }
Alan Cox4a90f092008-10-13 10:39:46 +0100336 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 }
338#endif
339 spin_unlock_irqrestore(&priv->lock, flags);
340exit:
Alan Coxb69c1492008-07-22 11:09:39 +0100341 retval = usb_submit_urb(urb, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 if (retval)
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700343 dev_err(&port->dev, "%s - usb_submit_urb failed with "
344 "result %d\n", __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345}
346
Alan Cox95da3102008-07-22 11:09:07 +0100347static void belkin_sa_set_termios(struct tty_struct *tty,
348 struct usb_serial_port *port, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349{
350 struct usb_serial *serial = port->serial;
351 struct belkin_sa_private *priv = usb_get_serial_port_data(port);
352 unsigned int iflag;
353 unsigned int cflag;
354 unsigned int old_iflag = 0;
355 unsigned int old_cflag = 0;
356 __u16 urb_value = 0; /* Will hold the new flags */
357 unsigned long flags;
358 unsigned long control_state;
359 int bad_flow_control;
Alan Cox9a8baec2007-06-22 14:40:18 +0100360 speed_t baud;
Alan Cox95da3102008-07-22 11:09:07 +0100361 struct ktermios *termios = tty->termios;
Alan Coxb69c1492008-07-22 11:09:39 +0100362
Alan Cox3ec466b2007-12-13 16:15:26 -0800363 iflag = termios->c_iflag;
364 cflag = termios->c_cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Alan Cox3ec466b2007-12-13 16:15:26 -0800366 termios->c_cflag &= ~CMSPAR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
368 /* get a local copy of the current port settings */
369 spin_lock_irqsave(&priv->lock, flags);
370 control_state = priv->control_state;
371 bad_flow_control = priv->bad_flow_control;
372 spin_unlock_irqrestore(&priv->lock, flags);
Alan Coxb69c1492008-07-22 11:09:39 +0100373
Alan Cox9a8baec2007-06-22 14:40:18 +0100374 old_iflag = old_termios->c_iflag;
375 old_cflag = old_termios->c_cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
377 /* Set the baud rate */
Alan Cox3ec466b2007-12-13 16:15:26 -0800378 if ((cflag & CBAUD) != (old_cflag & CBAUD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 /* reassert DTR and (maybe) RTS on transition from B0 */
Alan Coxb69c1492008-07-22 11:09:39 +0100380 if ((old_cflag & CBAUD) == B0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 control_state |= (TIOCM_DTR|TIOCM_RTS);
382 if (BSA_USB_CMD(BELKIN_SA_SET_DTR_REQUEST, 1) < 0)
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700383 dev_err(&port->dev, "Set DTR error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 /* don't set RTS if using hardware flow control */
Alan Cox3ec466b2007-12-13 16:15:26 -0800385 if (!(old_cflag & CRTSCTS))
Alan Coxb69c1492008-07-22 11:09:39 +0100386 if (BSA_USB_CMD(BELKIN_SA_SET_RTS_REQUEST
387 , 1) < 0)
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700388 dev_err(&port->dev, "Set RTS error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 }
Alan Cox9a8baec2007-06-22 14:40:18 +0100390 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
Alan Cox95da3102008-07-22 11:09:07 +0100392 baud = tty_get_baud_rate(tty);
Alan Cox3ec466b2007-12-13 16:15:26 -0800393 if (baud) {
394 urb_value = BELKIN_SA_BAUD(baud);
395 /* Clip to maximum speed */
396 if (urb_value == 0)
397 urb_value = 1;
398 /* Turn it back into a resulting real baud rate */
399 baud = BELKIN_SA_BAUD(urb_value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
Alan Cox3ec466b2007-12-13 16:15:26 -0800401 /* Report the actual baud rate back to the caller */
Alan Cox95da3102008-07-22 11:09:07 +0100402 tty_encode_baud_rate(tty, baud, baud);
Alan Cox9a8baec2007-06-22 14:40:18 +0100403 if (BSA_USB_CMD(BELKIN_SA_SET_BAUDRATE_REQUEST, urb_value) < 0)
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700404 dev_err(&port->dev, "Set baudrate error\n");
Alan Cox9a8baec2007-06-22 14:40:18 +0100405 } else {
406 /* Disable flow control */
Alan Coxb69c1492008-07-22 11:09:39 +0100407 if (BSA_USB_CMD(BELKIN_SA_SET_FLOW_CTRL_REQUEST,
408 BELKIN_SA_FLOW_NONE) < 0)
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700409 dev_err(&port->dev, "Disable flowcontrol error\n");
Alan Cox9a8baec2007-06-22 14:40:18 +0100410 /* Drop RTS and DTR */
411 control_state &= ~(TIOCM_DTR | TIOCM_RTS);
412 if (BSA_USB_CMD(BELKIN_SA_SET_DTR_REQUEST, 0) < 0)
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700413 dev_err(&port->dev, "DTR LOW error\n");
Alan Cox9a8baec2007-06-22 14:40:18 +0100414 if (BSA_USB_CMD(BELKIN_SA_SET_RTS_REQUEST, 0) < 0)
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700415 dev_err(&port->dev, "RTS LOW error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 }
417
418 /* set the parity */
Alan Coxb69c1492008-07-22 11:09:39 +0100419 if ((cflag ^ old_cflag) & (PARENB | PARODD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 if (cflag & PARENB)
Alan Coxb69c1492008-07-22 11:09:39 +0100421 urb_value = (cflag & PARODD) ? BELKIN_SA_PARITY_ODD
422 : BELKIN_SA_PARITY_EVEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 else
424 urb_value = BELKIN_SA_PARITY_NONE;
425 if (BSA_USB_CMD(BELKIN_SA_SET_PARITY_REQUEST, urb_value) < 0)
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700426 dev_err(&port->dev, "Set parity error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 }
428
429 /* set the number of data bits */
Alan Coxb69c1492008-07-22 11:09:39 +0100430 if ((cflag & CSIZE) != (old_cflag & CSIZE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 switch (cflag & CSIZE) {
Alan Coxb69c1492008-07-22 11:09:39 +0100432 case CS5:
433 urb_value = BELKIN_SA_DATA_BITS(5);
434 break;
435 case CS6:
436 urb_value = BELKIN_SA_DATA_BITS(6);
437 break;
438 case CS7:
439 urb_value = BELKIN_SA_DATA_BITS(7);
440 break;
441 case CS8:
442 urb_value = BELKIN_SA_DATA_BITS(8);
443 break;
444 default: dbg("CSIZE was not CS5-CS8, using default of 8");
445 urb_value = BELKIN_SA_DATA_BITS(8);
446 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 }
448 if (BSA_USB_CMD(BELKIN_SA_SET_DATA_BITS_REQUEST, urb_value) < 0)
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700449 dev_err(&port->dev, "Set data bits error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 }
451
452 /* set the number of stop bits */
Alan Coxb69c1492008-07-22 11:09:39 +0100453 if ((cflag & CSTOPB) != (old_cflag & CSTOPB)) {
454 urb_value = (cflag & CSTOPB) ? BELKIN_SA_STOP_BITS(2)
455 : BELKIN_SA_STOP_BITS(1);
456 if (BSA_USB_CMD(BELKIN_SA_SET_STOP_BITS_REQUEST,
457 urb_value) < 0)
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700458 dev_err(&port->dev, "Set stop bits error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 }
460
461 /* Set flow control */
Alan Coxb69c1492008-07-22 11:09:39 +0100462 if (((iflag ^ old_iflag) & (IXOFF | IXON)) ||
463 ((cflag ^ old_cflag) & CRTSCTS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 urb_value = 0;
465 if ((iflag & IXOFF) || (iflag & IXON))
466 urb_value |= (BELKIN_SA_FLOW_OXON | BELKIN_SA_FLOW_IXON);
467 else
468 urb_value &= ~(BELKIN_SA_FLOW_OXON | BELKIN_SA_FLOW_IXON);
469
470 if (cflag & CRTSCTS)
471 urb_value |= (BELKIN_SA_FLOW_OCTS | BELKIN_SA_FLOW_IRTS);
472 else
473 urb_value &= ~(BELKIN_SA_FLOW_OCTS | BELKIN_SA_FLOW_IRTS);
474
475 if (bad_flow_control)
476 urb_value &= ~(BELKIN_SA_FLOW_IRTS);
477
478 if (BSA_USB_CMD(BELKIN_SA_SET_FLOW_CTRL_REQUEST, urb_value) < 0)
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700479 dev_err(&port->dev, "Set flow control error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 }
481
482 /* save off the modified port settings */
483 spin_lock_irqsave(&priv->lock, flags);
484 priv->control_state = control_state;
485 spin_unlock_irqrestore(&priv->lock, flags);
486} /* belkin_sa_set_termios */
487
488
Alan Cox95da3102008-07-22 11:09:07 +0100489static void belkin_sa_break_ctl(struct tty_struct *tty, int break_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490{
Alan Cox95da3102008-07-22 11:09:07 +0100491 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 struct usb_serial *serial = port->serial;
493
494 if (BSA_USB_CMD(BELKIN_SA_SET_BREAK_REQUEST, break_state ? 1 : 0) < 0)
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700495 dev_err(&port->dev, "Set break_ctl %d\n", break_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496}
497
498
Alan Cox95da3102008-07-22 11:09:07 +0100499static int belkin_sa_tiocmget(struct tty_struct *tty, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500{
Alan Cox95da3102008-07-22 11:09:07 +0100501 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 struct belkin_sa_private *priv = usb_get_serial_port_data(port);
503 unsigned long control_state;
504 unsigned long flags;
Alan Coxb69c1492008-07-22 11:09:39 +0100505
Harvey Harrison441b62c2008-03-03 16:08:34 -0800506 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
508 spin_lock_irqsave(&priv->lock, flags);
509 control_state = priv->control_state;
510 spin_unlock_irqrestore(&priv->lock, flags);
511
512 return control_state;
513}
514
515
Alan Cox95da3102008-07-22 11:09:07 +0100516static int belkin_sa_tiocmset(struct tty_struct *tty, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 unsigned int set, unsigned int clear)
518{
Alan Cox95da3102008-07-22 11:09:07 +0100519 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 struct usb_serial *serial = port->serial;
521 struct belkin_sa_private *priv = usb_get_serial_port_data(port);
522 unsigned long control_state;
523 unsigned long flags;
524 int retval;
525 int rts = 0;
526 int dtr = 0;
Alan Coxb69c1492008-07-22 11:09:39 +0100527
Harvey Harrison441b62c2008-03-03 16:08:34 -0800528 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
530 spin_lock_irqsave(&priv->lock, flags);
531 control_state = priv->control_state;
532
533 if (set & TIOCM_RTS) {
534 control_state |= TIOCM_RTS;
535 rts = 1;
536 }
537 if (set & TIOCM_DTR) {
538 control_state |= TIOCM_DTR;
539 dtr = 1;
540 }
541 if (clear & TIOCM_RTS) {
542 control_state &= ~TIOCM_RTS;
543 rts = 0;
544 }
545 if (clear & TIOCM_DTR) {
546 control_state &= ~TIOCM_DTR;
547 dtr = 0;
548 }
549
550 priv->control_state = control_state;
551 spin_unlock_irqrestore(&priv->lock, flags);
552
553 retval = BSA_USB_CMD(BELKIN_SA_SET_RTS_REQUEST, rts);
554 if (retval < 0) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700555 dev_err(&port->dev, "Set RTS error %d\n", retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 goto exit;
557 }
558
559 retval = BSA_USB_CMD(BELKIN_SA_SET_DTR_REQUEST, dtr);
560 if (retval < 0) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700561 dev_err(&port->dev, "Set DTR error %d\n", retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 goto exit;
563 }
564exit:
565 return retval;
566}
567
568
Alan Cox95da3102008-07-22 11:09:07 +0100569static int __init belkin_sa_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570{
571 int retval;
572 retval = usb_serial_register(&belkin_device);
573 if (retval)
574 goto failed_usb_serial_register;
575 retval = usb_register(&belkin_driver);
576 if (retval)
577 goto failed_usb_register;
Greg Kroah-Hartmanc197a8d2008-08-18 13:21:04 -0700578 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
579 DRIVER_DESC "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 return 0;
581failed_usb_register:
582 usb_serial_deregister(&belkin_device);
583failed_usb_serial_register:
584 return retval;
585}
586
587
588static void __exit belkin_sa_exit (void)
589{
Alan Coxb69c1492008-07-22 11:09:39 +0100590 usb_deregister(&belkin_driver);
Alan Cox95da3102008-07-22 11:09:07 +0100591 usb_serial_deregister(&belkin_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592}
593
594
Alan Coxb69c1492008-07-22 11:09:39 +0100595module_init(belkin_sa_init);
596module_exit(belkin_sa_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Alan Coxb69c1492008-07-22 11:09:39 +0100598MODULE_AUTHOR(DRIVER_AUTHOR);
599MODULE_DESCRIPTION(DRIVER_DESC);
600MODULE_VERSION(DRIVER_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601MODULE_LICENSE("GPL");
602
603module_param(debug, bool, S_IRUGO | S_IWUSR);
604MODULE_PARM_DESC(debug, "Debug enabled or not");