blob: 8d94a5a7c4b6bdbdc07fcb218ac18386e30478eb [file] [log] [blame]
Bill Pemberton52af9542010-07-29 11:05:41 -04001/*
2 * usb-serial driver for Quatech SSU-100
3 *
4 * based on ftdi_sio.c and the original serqt_usb.c from Quatech
5 *
6 */
7
8#include <linux/errno.h>
9#include <linux/init.h>
10#include <linux/slab.h>
11#include <linux/tty.h>
12#include <linux/tty_driver.h>
13#include <linux/tty_flip.h>
14#include <linux/module.h>
15#include <linux/serial.h>
16#include <linux/usb.h>
17#include <linux/usb/serial.h>
Bill Pemberton79f203a2010-08-05 17:01:07 -040018#include <linux/serial_reg.h>
Bill Pemberton52af9542010-07-29 11:05:41 -040019#include <linux/uaccess.h>
20
21#define QT_OPEN_CLOSE_CHANNEL 0xca
22#define QT_SET_GET_DEVICE 0xc2
23#define QT_SET_GET_REGISTER 0xc0
24#define QT_GET_SET_PREBUF_TRIG_LVL 0xcc
25#define QT_SET_ATF 0xcd
26#define QT_GET_SET_UART 0xc1
27#define QT_TRANSFER_IN 0xc0
28#define QT_HW_FLOW_CONTROL_MASK 0xc5
29#define QT_SW_FLOW_CONTROL_MASK 0xc6
30
Bill Pemberton52af9542010-07-29 11:05:41 -040031#define SERIAL_MSR_MASK 0xf0
32
Bill Pemberton79f203a2010-08-05 17:01:07 -040033#define SERIAL_CRTSCTS ((UART_MCR_RTS << 8) | UART_MSR_CTS)
Bill Pemberton52af9542010-07-29 11:05:41 -040034
Bill Pemberton79f203a2010-08-05 17:01:07 -040035#define SERIAL_EVEN_PARITY (UART_LCR_PARITY | UART_LCR_EPAR)
Bill Pemberton52af9542010-07-29 11:05:41 -040036
37#define MAX_BAUD_RATE 460800
38
39#define ATC_DISABLED 0x00
40#define DUPMODE_BITS 0xc0
41#define RR_BITS 0x03
42#define LOOPMODE_BITS 0x41
43#define RS232_MODE 0x00
44#define RTSCTS_TO_CONNECTOR 0x40
45#define CLKS_X4 0x02
46#define FULLPWRBIT 0x00000080
47#define NEXT_BOARD_POWER_BIT 0x00000004
48
Bill Pemberton52af9542010-07-29 11:05:41 -040049#define DRIVER_DESC "Quatech SSU-100 USB to Serial Driver"
50
51#define USB_VENDOR_ID_QUATECH 0x061d /* Quatech VID */
52#define QUATECH_SSU100 0xC020 /* SSU100 */
53
54static const struct usb_device_id id_table[] = {
55 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_SSU100)},
56 {} /* Terminating entry */
57};
Bill Pemberton52af9542010-07-29 11:05:41 -040058MODULE_DEVICE_TABLE(usb, id_table);
59
Bill Pemberton52af9542010-07-29 11:05:41 -040060struct ssu100_port_private {
Bill Pemberton17523052010-08-05 17:01:05 -040061 spinlock_t status_lock;
Bill Pemberton52af9542010-07-29 11:05:41 -040062 u8 shadowLSR;
63 u8 shadowMSR;
Bill Pemberton52af9542010-07-29 11:05:41 -040064};
65
Bill Pemberton52af9542010-07-29 11:05:41 -040066static inline int ssu100_control_msg(struct usb_device *dev,
67 u8 request, u16 data, u16 index)
68{
69 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
70 request, 0x40, data, index,
71 NULL, 0, 300);
72}
73
74static inline int ssu100_setdevice(struct usb_device *dev, u8 *data)
75{
76 u16 x = ((u16)(data[1] << 8) | (u16)(data[0]));
77
78 return ssu100_control_msg(dev, QT_SET_GET_DEVICE, x, 0);
79}
80
81
82static inline int ssu100_getdevice(struct usb_device *dev, u8 *data)
83{
84 return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
85 QT_SET_GET_DEVICE, 0xc0, 0, 0,
86 data, 3, 300);
87}
88
89static inline int ssu100_getregister(struct usb_device *dev,
90 unsigned short uart,
91 unsigned short reg,
92 u8 *data)
93{
94 return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
95 QT_SET_GET_REGISTER, 0xc0, reg,
96 uart, data, sizeof(*data), 300);
97
98}
99
100
101static inline int ssu100_setregister(struct usb_device *dev,
102 unsigned short uart,
Bill Pemberton556f1a02010-08-05 17:01:08 -0400103 unsigned short reg,
Bill Pemberton52af9542010-07-29 11:05:41 -0400104 u16 data)
105{
Bill Pemberton556f1a02010-08-05 17:01:08 -0400106 u16 value = (data << 8) | reg;
Bill Pemberton52af9542010-07-29 11:05:41 -0400107
108 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
109 QT_SET_GET_REGISTER, 0x40, value, uart,
110 NULL, 0, 300);
111
112}
113
114#define set_mctrl(dev, set) update_mctrl((dev), (set), 0)
115#define clear_mctrl(dev, clear) update_mctrl((dev), 0, (clear))
116
117/* these do not deal with device that have more than 1 port */
118static inline int update_mctrl(struct usb_device *dev, unsigned int set,
119 unsigned int clear)
120{
121 unsigned urb_value;
122 int result;
123
124 if (((set | clear) & (TIOCM_DTR | TIOCM_RTS)) == 0) {
Greg Kroah-Hartman4f0c6412012-09-14 11:50:35 -0700125 dev_dbg(&dev->dev, "%s - DTR|RTS not being set|cleared\n", __func__);
Bill Pemberton52af9542010-07-29 11:05:41 -0400126 return 0; /* no change */
127 }
128
129 clear &= ~set; /* 'set' takes precedence over 'clear' */
130 urb_value = 0;
131 if (set & TIOCM_DTR)
Bill Pemberton79f203a2010-08-05 17:01:07 -0400132 urb_value |= UART_MCR_DTR;
Bill Pemberton52af9542010-07-29 11:05:41 -0400133 if (set & TIOCM_RTS)
Bill Pemberton79f203a2010-08-05 17:01:07 -0400134 urb_value |= UART_MCR_RTS;
Bill Pemberton52af9542010-07-29 11:05:41 -0400135
Bill Pemberton556f1a02010-08-05 17:01:08 -0400136 result = ssu100_setregister(dev, 0, UART_MCR, urb_value);
Bill Pemberton52af9542010-07-29 11:05:41 -0400137 if (result < 0)
Greg Kroah-Hartman4f0c6412012-09-14 11:50:35 -0700138 dev_dbg(&dev->dev, "%s Error from MODEM_CTRL urb\n", __func__);
Bill Pemberton52af9542010-07-29 11:05:41 -0400139
140 return result;
141}
142
143static int ssu100_initdevice(struct usb_device *dev)
144{
145 u8 *data;
146 int result = 0;
147
Bill Pemberton52af9542010-07-29 11:05:41 -0400148 data = kzalloc(3, GFP_KERNEL);
149 if (!data)
150 return -ENOMEM;
151
152 result = ssu100_getdevice(dev, data);
153 if (result < 0) {
Greg Kroah-Hartman4f0c6412012-09-14 11:50:35 -0700154 dev_dbg(&dev->dev, "%s - get_device failed %i\n", __func__, result);
Bill Pemberton52af9542010-07-29 11:05:41 -0400155 goto out;
156 }
157
158 data[1] &= ~FULLPWRBIT;
159
160 result = ssu100_setdevice(dev, data);
161 if (result < 0) {
Greg Kroah-Hartman4f0c6412012-09-14 11:50:35 -0700162 dev_dbg(&dev->dev, "%s - setdevice failed %i\n", __func__, result);
Bill Pemberton52af9542010-07-29 11:05:41 -0400163 goto out;
164 }
165
166 result = ssu100_control_msg(dev, QT_GET_SET_PREBUF_TRIG_LVL, 128, 0);
167 if (result < 0) {
Greg Kroah-Hartman4f0c6412012-09-14 11:50:35 -0700168 dev_dbg(&dev->dev, "%s - set prebuffer level failed %i\n", __func__, result);
Bill Pemberton52af9542010-07-29 11:05:41 -0400169 goto out;
170 }
171
172 result = ssu100_control_msg(dev, QT_SET_ATF, ATC_DISABLED, 0);
173 if (result < 0) {
Greg Kroah-Hartman4f0c6412012-09-14 11:50:35 -0700174 dev_dbg(&dev->dev, "%s - set ATFprebuffer level failed %i\n", __func__, result);
Bill Pemberton52af9542010-07-29 11:05:41 -0400175 goto out;
176 }
177
178 result = ssu100_getdevice(dev, data);
179 if (result < 0) {
Greg Kroah-Hartman4f0c6412012-09-14 11:50:35 -0700180 dev_dbg(&dev->dev, "%s - get_device failed %i\n", __func__, result);
Bill Pemberton52af9542010-07-29 11:05:41 -0400181 goto out;
182 }
183
184 data[0] &= ~(RR_BITS | DUPMODE_BITS);
185 data[0] |= CLKS_X4;
186 data[1] &= ~(LOOPMODE_BITS);
187 data[1] |= RS232_MODE;
188
189 result = ssu100_setdevice(dev, data);
190 if (result < 0) {
Greg Kroah-Hartman4f0c6412012-09-14 11:50:35 -0700191 dev_dbg(&dev->dev, "%s - setdevice failed %i\n", __func__, result);
Bill Pemberton52af9542010-07-29 11:05:41 -0400192 goto out;
193 }
194
195out: kfree(data);
196 return result;
197
198}
199
200
201static void ssu100_set_termios(struct tty_struct *tty,
202 struct usb_serial_port *port,
203 struct ktermios *old_termios)
204{
205 struct usb_device *dev = port->serial->dev;
Alan Coxadc8d742012-07-14 15:31:47 +0100206 struct ktermios *termios = &tty->termios;
Bill Pemberton52af9542010-07-29 11:05:41 -0400207 u16 baud, divisor, remainder;
208 unsigned int cflag = termios->c_cflag;
209 u16 urb_value = 0; /* will hold the new flags */
210 int result;
211
Bill Pemberton52af9542010-07-29 11:05:41 -0400212 if (cflag & PARENB) {
213 if (cflag & PARODD)
Bill Pemberton79f203a2010-08-05 17:01:07 -0400214 urb_value |= UART_LCR_PARITY;
Bill Pemberton52af9542010-07-29 11:05:41 -0400215 else
216 urb_value |= SERIAL_EVEN_PARITY;
217 }
218
219 switch (cflag & CSIZE) {
220 case CS5:
Bill Pemberton79f203a2010-08-05 17:01:07 -0400221 urb_value |= UART_LCR_WLEN5;
Bill Pemberton52af9542010-07-29 11:05:41 -0400222 break;
223 case CS6:
Bill Pemberton79f203a2010-08-05 17:01:07 -0400224 urb_value |= UART_LCR_WLEN6;
Bill Pemberton52af9542010-07-29 11:05:41 -0400225 break;
226 case CS7:
Bill Pemberton79f203a2010-08-05 17:01:07 -0400227 urb_value |= UART_LCR_WLEN7;
Bill Pemberton52af9542010-07-29 11:05:41 -0400228 break;
229 default:
230 case CS8:
Bill Pemberton79f203a2010-08-05 17:01:07 -0400231 urb_value |= UART_LCR_WLEN8;
Bill Pemberton52af9542010-07-29 11:05:41 -0400232 break;
233 }
234
235 baud = tty_get_baud_rate(tty);
236 if (!baud)
237 baud = 9600;
238
Greg Kroah-Hartman4f0c6412012-09-14 11:50:35 -0700239 dev_dbg(&port->dev, "%s - got baud = %d\n", __func__, baud);
Bill Pemberton52af9542010-07-29 11:05:41 -0400240
241
242 divisor = MAX_BAUD_RATE / baud;
243 remainder = MAX_BAUD_RATE % baud;
244 if (((remainder * 2) >= baud) && (baud != 110))
245 divisor++;
246
247 urb_value = urb_value << 8;
248
249 result = ssu100_control_msg(dev, QT_GET_SET_UART, divisor, urb_value);
250 if (result < 0)
Greg Kroah-Hartman4f0c6412012-09-14 11:50:35 -0700251 dev_dbg(&port->dev, "%s - set uart failed\n", __func__);
Bill Pemberton52af9542010-07-29 11:05:41 -0400252
253 if (cflag & CRTSCTS)
254 result = ssu100_control_msg(dev, QT_HW_FLOW_CONTROL_MASK,
255 SERIAL_CRTSCTS, 0);
256 else
257 result = ssu100_control_msg(dev, QT_HW_FLOW_CONTROL_MASK,
258 0, 0);
259 if (result < 0)
Greg Kroah-Hartman4f0c6412012-09-14 11:50:35 -0700260 dev_dbg(&port->dev, "%s - set HW flow control failed\n", __func__);
Bill Pemberton52af9542010-07-29 11:05:41 -0400261
262 if (I_IXOFF(tty) || I_IXON(tty)) {
263 u16 x = ((u16)(START_CHAR(tty) << 8) | (u16)(STOP_CHAR(tty)));
264
265 result = ssu100_control_msg(dev, QT_SW_FLOW_CONTROL_MASK,
266 x, 0);
267 } else
268 result = ssu100_control_msg(dev, QT_SW_FLOW_CONTROL_MASK,
269 0, 0);
270
271 if (result < 0)
Greg Kroah-Hartman4f0c6412012-09-14 11:50:35 -0700272 dev_dbg(&port->dev, "%s - set SW flow control failed\n", __func__);
Bill Pemberton52af9542010-07-29 11:05:41 -0400273
274}
275
276
277static int ssu100_open(struct tty_struct *tty, struct usb_serial_port *port)
278{
279 struct usb_device *dev = port->serial->dev;
280 struct ssu100_port_private *priv = usb_get_serial_port_data(port);
281 u8 *data;
282 int result;
Bill Pemberton17523052010-08-05 17:01:05 -0400283 unsigned long flags;
Bill Pemberton52af9542010-07-29 11:05:41 -0400284
Bill Pemberton52af9542010-07-29 11:05:41 -0400285 data = kzalloc(2, GFP_KERNEL);
286 if (!data)
287 return -ENOMEM;
288
289 result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
290 QT_OPEN_CLOSE_CHANNEL,
291 QT_TRANSFER_IN, 0x01,
292 0, data, 2, 300);
293 if (result < 0) {
Greg Kroah-Hartman4f0c6412012-09-14 11:50:35 -0700294 dev_dbg(&port->dev, "%s - open failed %i\n", __func__, result);
Bill Pemberton52af9542010-07-29 11:05:41 -0400295 kfree(data);
296 return result;
297 }
298
Bill Pemberton17523052010-08-05 17:01:05 -0400299 spin_lock_irqsave(&priv->status_lock, flags);
Bill Pembertonf81c83d2010-08-05 17:01:09 -0400300 priv->shadowLSR = data[0];
301 priv->shadowMSR = data[1];
Bill Pemberton17523052010-08-05 17:01:05 -0400302 spin_unlock_irqrestore(&priv->status_lock, flags);
Bill Pemberton52af9542010-07-29 11:05:41 -0400303
304 kfree(data);
305
306/* set to 9600 */
307 result = ssu100_control_msg(dev, QT_GET_SET_UART, 0x30, 0x0300);
308 if (result < 0)
Greg Kroah-Hartman4f0c6412012-09-14 11:50:35 -0700309 dev_dbg(&port->dev, "%s - set uart failed\n", __func__);
Bill Pemberton52af9542010-07-29 11:05:41 -0400310
311 if (tty)
Alan Coxadc8d742012-07-14 15:31:47 +0100312 ssu100_set_termios(tty, port, &tty->termios);
Bill Pemberton52af9542010-07-29 11:05:41 -0400313
314 return usb_serial_generic_open(tty, port);
315}
316
Bill Pemberton52af9542010-07-29 11:05:41 -0400317static int get_serial_info(struct usb_serial_port *port,
318 struct serial_struct __user *retinfo)
319{
320 struct serial_struct tmp;
321
322 if (!retinfo)
323 return -EFAULT;
324
325 memset(&tmp, 0, sizeof(tmp));
326 tmp.line = port->serial->minor;
327 tmp.port = 0;
328 tmp.irq = 0;
329 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
330 tmp.xmit_fifo_size = port->bulk_out_size;
331 tmp.baud_base = 9600;
332 tmp.close_delay = 5*HZ;
333 tmp.closing_wait = 30*HZ;
334
335 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
336 return -EFAULT;
337 return 0;
338}
339
Bill Pembertonf81c83d2010-08-05 17:01:09 -0400340static int wait_modem_info(struct usb_serial_port *port, unsigned int arg)
341{
342 struct ssu100_port_private *priv = usb_get_serial_port_data(port);
343 struct async_icount prev, cur;
344 unsigned long flags;
345
346 spin_lock_irqsave(&priv->status_lock, flags);
Johan Hovold31ecdb62013-03-21 12:37:31 +0100347 prev = port->icount;
Bill Pembertonf81c83d2010-08-05 17:01:09 -0400348 spin_unlock_irqrestore(&priv->status_lock, flags);
349
350 while (1) {
Johan Hovold43a66b42013-03-19 09:21:25 +0100351 wait_event_interruptible(port->delta_msr_wait,
352 (port->serial->disconnected ||
Johan Hovold31ecdb62013-03-21 12:37:31 +0100353 (port->icount.rng != prev.rng) ||
354 (port->icount.dsr != prev.dsr) ||
355 (port->icount.dcd != prev.dcd) ||
356 (port->icount.cts != prev.cts)));
Bill Pembertonf81c83d2010-08-05 17:01:09 -0400357
358 if (signal_pending(current))
359 return -ERESTARTSYS;
360
Johan Hovold43a66b42013-03-19 09:21:25 +0100361 if (port->serial->disconnected)
362 return -EIO;
363
Bill Pembertonf81c83d2010-08-05 17:01:09 -0400364 spin_lock_irqsave(&priv->status_lock, flags);
Johan Hovold31ecdb62013-03-21 12:37:31 +0100365 cur = port->icount;
Bill Pembertonf81c83d2010-08-05 17:01:09 -0400366 spin_unlock_irqrestore(&priv->status_lock, flags);
367
368 if ((prev.rng == cur.rng) &&
369 (prev.dsr == cur.dsr) &&
370 (prev.dcd == cur.dcd) &&
371 (prev.cts == cur.cts))
372 return -EIO;
373
374 if ((arg & TIOCM_RNG && (prev.rng != cur.rng)) ||
375 (arg & TIOCM_DSR && (prev.dsr != cur.dsr)) ||
376 (arg & TIOCM_CD && (prev.dcd != cur.dcd)) ||
377 (arg & TIOCM_CTS && (prev.cts != cur.cts)))
378 return 0;
379 }
380 return 0;
381}
382
Alan Cox00a0d0d2011-02-14 16:27:06 +0000383static int ssu100_ioctl(struct tty_struct *tty,
Bill Pemberton52af9542010-07-29 11:05:41 -0400384 unsigned int cmd, unsigned long arg)
385{
386 struct usb_serial_port *port = tty->driver_data;
Bill Pemberton52af9542010-07-29 11:05:41 -0400387
Greg Kroah-Hartman4f0c6412012-09-14 11:50:35 -0700388 dev_dbg(&port->dev, "%s cmd 0x%04x\n", __func__, cmd);
Bill Pemberton52af9542010-07-29 11:05:41 -0400389
390 switch (cmd) {
391 case TIOCGSERIAL:
392 return get_serial_info(port,
393 (struct serial_struct __user *) arg);
394
395 case TIOCMIWAIT:
Bill Pembertonf81c83d2010-08-05 17:01:09 -0400396 return wait_modem_info(port, arg);
Bill Pemberton52af9542010-07-29 11:05:41 -0400397
Bill Pemberton52af9542010-07-29 11:05:41 -0400398 default:
399 break;
400 }
401
Greg Kroah-Hartman4f0c6412012-09-14 11:50:35 -0700402 dev_dbg(&port->dev, "%s arg not supported\n", __func__);
Bill Pemberton52af9542010-07-29 11:05:41 -0400403
404 return -ENOIOCTLCMD;
405}
406
Bill Pemberton52af9542010-07-29 11:05:41 -0400407static int ssu100_attach(struct usb_serial *serial)
408{
Johan Hovold638b9e12012-10-17 16:31:34 +0200409 return ssu100_initdevice(serial->dev);
410}
411
412static int ssu100_port_probe(struct usb_serial_port *port)
413{
Bill Pemberton52af9542010-07-29 11:05:41 -0400414 struct ssu100_port_private *priv;
Bill Pemberton52af9542010-07-29 11:05:41 -0400415
Bill Pemberton52af9542010-07-29 11:05:41 -0400416 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
Johan Hovold638b9e12012-10-17 16:31:34 +0200417 if (!priv)
Bill Pemberton52af9542010-07-29 11:05:41 -0400418 return -ENOMEM;
Bill Pemberton52af9542010-07-29 11:05:41 -0400419
Bill Pemberton17523052010-08-05 17:01:05 -0400420 spin_lock_init(&priv->status_lock);
Johan Hovold638b9e12012-10-17 16:31:34 +0200421
Bill Pemberton52af9542010-07-29 11:05:41 -0400422 usb_set_serial_port_data(port, priv);
Bill Pemberton52af9542010-07-29 11:05:41 -0400423
Johan Hovold638b9e12012-10-17 16:31:34 +0200424 return 0;
425}
426
427static int ssu100_port_remove(struct usb_serial_port *port)
428{
429 struct ssu100_port_private *priv;
430
431 priv = usb_get_serial_port_data(port);
432 kfree(priv);
433
434 return 0;
Bill Pemberton52af9542010-07-29 11:05:41 -0400435}
436
Alan Cox60b33c12011-02-14 16:26:14 +0000437static int ssu100_tiocmget(struct tty_struct *tty)
Bill Pemberton52af9542010-07-29 11:05:41 -0400438{
439 struct usb_serial_port *port = tty->driver_data;
440 struct usb_device *dev = port->serial->dev;
441 u8 *d;
442 int r;
443
Bill Pemberton52af9542010-07-29 11:05:41 -0400444 d = kzalloc(2, GFP_KERNEL);
445 if (!d)
446 return -ENOMEM;
447
Bill Pemberton79f203a2010-08-05 17:01:07 -0400448 r = ssu100_getregister(dev, 0, UART_MCR, d);
Bill Pemberton52af9542010-07-29 11:05:41 -0400449 if (r < 0)
450 goto mget_out;
451
Bill Pemberton79f203a2010-08-05 17:01:07 -0400452 r = ssu100_getregister(dev, 0, UART_MSR, d+1);
Bill Pemberton52af9542010-07-29 11:05:41 -0400453 if (r < 0)
454 goto mget_out;
455
Bill Pemberton79f203a2010-08-05 17:01:07 -0400456 r = (d[0] & UART_MCR_DTR ? TIOCM_DTR : 0) |
457 (d[0] & UART_MCR_RTS ? TIOCM_RTS : 0) |
458 (d[1] & UART_MSR_CTS ? TIOCM_CTS : 0) |
459 (d[1] & UART_MSR_DCD ? TIOCM_CAR : 0) |
460 (d[1] & UART_MSR_RI ? TIOCM_RI : 0) |
461 (d[1] & UART_MSR_DSR ? TIOCM_DSR : 0);
Bill Pemberton52af9542010-07-29 11:05:41 -0400462
463mget_out:
464 kfree(d);
465 return r;
466}
467
Alan Cox20b9d172011-02-14 16:26:50 +0000468static int ssu100_tiocmset(struct tty_struct *tty,
Bill Pemberton52af9542010-07-29 11:05:41 -0400469 unsigned int set, unsigned int clear)
470{
471 struct usb_serial_port *port = tty->driver_data;
472 struct usb_device *dev = port->serial->dev;
473
Bill Pemberton52af9542010-07-29 11:05:41 -0400474 return update_mctrl(dev, set, clear);
475}
476
477static void ssu100_dtr_rts(struct usb_serial_port *port, int on)
478{
479 struct usb_device *dev = port->serial->dev;
480
Johan Hovoldb2ca6992013-02-13 17:53:28 +0100481 /* Disable flow control */
482 if (!on) {
483 if (ssu100_setregister(dev, 0, UART_MCR, 0) < 0)
Bill Pemberton52af9542010-07-29 11:05:41 -0400484 dev_err(&port->dev, "error from flowcontrol urb\n");
Bill Pemberton52af9542010-07-29 11:05:41 -0400485 }
Johan Hovoldb2ca6992013-02-13 17:53:28 +0100486 /* drop RTS and DTR */
487 if (on)
488 set_mctrl(dev, TIOCM_DTR | TIOCM_RTS);
489 else
490 clear_mctrl(dev, TIOCM_DTR | TIOCM_RTS);
Bill Pemberton52af9542010-07-29 11:05:41 -0400491}
492
Bill Pembertonf81c83d2010-08-05 17:01:09 -0400493static void ssu100_update_msr(struct usb_serial_port *port, u8 msr)
494{
495 struct ssu100_port_private *priv = usb_get_serial_port_data(port);
496 unsigned long flags;
497
498 spin_lock_irqsave(&priv->status_lock, flags);
499 priv->shadowMSR = msr;
500 spin_unlock_irqrestore(&priv->status_lock, flags);
501
502 if (msr & UART_MSR_ANY_DELTA) {
503 /* update input line counters */
504 if (msr & UART_MSR_DCTS)
Johan Hovold31ecdb62013-03-21 12:37:31 +0100505 port->icount.cts++;
Bill Pembertonf81c83d2010-08-05 17:01:09 -0400506 if (msr & UART_MSR_DDSR)
Johan Hovold31ecdb62013-03-21 12:37:31 +0100507 port->icount.dsr++;
Bill Pembertonf81c83d2010-08-05 17:01:09 -0400508 if (msr & UART_MSR_DDCD)
Johan Hovold31ecdb62013-03-21 12:37:31 +0100509 port->icount.dcd++;
Bill Pembertonf81c83d2010-08-05 17:01:09 -0400510 if (msr & UART_MSR_TERI)
Johan Hovold31ecdb62013-03-21 12:37:31 +0100511 port->icount.rng++;
Johan Hovold43a66b42013-03-19 09:21:25 +0100512 wake_up_interruptible(&port->delta_msr_wait);
Bill Pembertonf81c83d2010-08-05 17:01:09 -0400513 }
514}
515
Bill Pemberton6b8f1ca2010-08-13 09:59:31 -0400516static void ssu100_update_lsr(struct usb_serial_port *port, u8 lsr,
517 char *tty_flag)
Bill Pembertonf81c83d2010-08-05 17:01:09 -0400518{
519 struct ssu100_port_private *priv = usb_get_serial_port_data(port);
520 unsigned long flags;
521
522 spin_lock_irqsave(&priv->status_lock, flags);
523 priv->shadowLSR = lsr;
524 spin_unlock_irqrestore(&priv->status_lock, flags);
525
Bill Pemberton6b8f1ca2010-08-13 09:59:31 -0400526 *tty_flag = TTY_NORMAL;
Bill Pembertonf81c83d2010-08-05 17:01:09 -0400527 if (lsr & UART_LSR_BRK_ERROR_BITS) {
Bill Pemberton6b8f1ca2010-08-13 09:59:31 -0400528 /* we always want to update icount, but we only want to
529 * update tty_flag for one case */
530 if (lsr & UART_LSR_BI) {
Johan Hovold31ecdb62013-03-21 12:37:31 +0100531 port->icount.brk++;
Bill Pemberton6b8f1ca2010-08-13 09:59:31 -0400532 *tty_flag = TTY_BREAK;
533 usb_serial_handle_break(port);
534 }
535 if (lsr & UART_LSR_PE) {
Johan Hovold31ecdb62013-03-21 12:37:31 +0100536 port->icount.parity++;
Bill Pemberton6b8f1ca2010-08-13 09:59:31 -0400537 if (*tty_flag == TTY_NORMAL)
538 *tty_flag = TTY_PARITY;
539 }
540 if (lsr & UART_LSR_FE) {
Johan Hovold31ecdb62013-03-21 12:37:31 +0100541 port->icount.frame++;
Bill Pemberton6b8f1ca2010-08-13 09:59:31 -0400542 if (*tty_flag == TTY_NORMAL)
543 *tty_flag = TTY_FRAME;
544 }
545 if (lsr & UART_LSR_OE){
Johan Hovold31ecdb62013-03-21 12:37:31 +0100546 port->icount.overrun++;
Bill Pemberton6b8f1ca2010-08-13 09:59:31 -0400547 if (*tty_flag == TTY_NORMAL)
548 *tty_flag = TTY_OVERRUN;
549 }
Bill Pembertonf81c83d2010-08-05 17:01:09 -0400550 }
Bill Pemberton6b8f1ca2010-08-13 09:59:31 -0400551
Bill Pembertonf81c83d2010-08-05 17:01:09 -0400552}
553
Jiri Slaby2e124b42013-01-03 15:53:06 +0100554static void ssu100_process_read_urb(struct urb *urb)
Bill Pemberton52af9542010-07-29 11:05:41 -0400555{
Bill Pembertonf7043ec2010-10-21 14:43:05 -0400556 struct usb_serial_port *port = urb->context;
557 char *packet = (char *)urb->transfer_buffer;
Bill Pemberton6b8f1ca2010-08-13 09:59:31 -0400558 char flag = TTY_NORMAL;
Bill Pembertonf7043ec2010-10-21 14:43:05 -0400559 u32 len = urb->actual_length;
560 int i;
Bill Pemberton52af9542010-07-29 11:05:41 -0400561 char *ch;
562
Bill Pemberton9b2cef32010-08-05 17:01:06 -0400563 if ((len >= 4) &&
564 (packet[0] == 0x1b) && (packet[1] == 0x1b) &&
Bill Pemberton52af9542010-07-29 11:05:41 -0400565 ((packet[2] == 0x00) || (packet[2] == 0x01))) {
Bill Pemberton6b8f1ca2010-08-13 09:59:31 -0400566 if (packet[2] == 0x00) {
567 ssu100_update_lsr(port, packet[3], &flag);
568 if (flag == TTY_OVERRUN)
Jiri Slaby92a19f92013-01-03 15:53:03 +0100569 tty_insert_flip_char(&port->port, 0,
570 TTY_OVERRUN);
Bill Pemberton6b8f1ca2010-08-13 09:59:31 -0400571 }
Bill Pembertonf81c83d2010-08-05 17:01:09 -0400572 if (packet[2] == 0x01)
573 ssu100_update_msr(port, packet[3]);
Bill Pemberton52af9542010-07-29 11:05:41 -0400574
575 len -= 4;
576 ch = packet + 4;
577 } else
578 ch = packet;
579
580 if (!len)
Jiri Slaby2e124b42013-01-03 15:53:06 +0100581 return; /* status only */
Bill Pemberton52af9542010-07-29 11:05:41 -0400582
583 if (port->port.console && port->sysrq) {
584 for (i = 0; i < len; i++, ch++) {
Dmitry Torokhov6ee9f4b2010-08-17 21:15:47 -0700585 if (!usb_serial_handle_sysrq_char(port, *ch))
Jiri Slaby92a19f92013-01-03 15:53:03 +0100586 tty_insert_flip_char(&port->port, *ch, flag);
Bill Pemberton52af9542010-07-29 11:05:41 -0400587 }
588 } else
Jiri Slaby2f693352013-01-03 15:53:02 +0100589 tty_insert_flip_string_fixed_flag(&port->port, ch, flag, len);
Bill Pemberton52af9542010-07-29 11:05:41 -0400590
Jiri Slaby2e124b42013-01-03 15:53:06 +0100591 tty_flip_buffer_push(&port->port);
Bill Pemberton52af9542010-07-29 11:05:41 -0400592}
593
Bill Pemberton52af9542010-07-29 11:05:41 -0400594static struct usb_serial_driver ssu100_device = {
595 .driver = {
596 .owner = THIS_MODULE,
597 .name = "ssu100",
598 },
599 .description = DRIVER_DESC,
600 .id_table = id_table,
Bill Pemberton52af9542010-07-29 11:05:41 -0400601 .num_ports = 1,
Bill Pemberton52af9542010-07-29 11:05:41 -0400602 .open = ssu100_open,
Bill Pemberton52af9542010-07-29 11:05:41 -0400603 .attach = ssu100_attach,
Johan Hovold638b9e12012-10-17 16:31:34 +0200604 .port_probe = ssu100_port_probe,
605 .port_remove = ssu100_port_remove,
Bill Pemberton52af9542010-07-29 11:05:41 -0400606 .dtr_rts = ssu100_dtr_rts,
607 .process_read_urb = ssu100_process_read_urb,
608 .tiocmget = ssu100_tiocmget,
609 .tiocmset = ssu100_tiocmset,
Johan Hovold31ecdb62013-03-21 12:37:31 +0100610 .get_icount = usb_serial_generic_get_icount,
Bill Pemberton52af9542010-07-29 11:05:41 -0400611 .ioctl = ssu100_ioctl,
612 .set_termios = ssu100_set_termios,
Bill Pemberton52af9542010-07-29 11:05:41 -0400613};
614
Alan Sternd8603222012-02-23 14:57:25 -0500615static struct usb_serial_driver * const serial_drivers[] = {
616 &ssu100_device, NULL
617};
618
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -0700619module_usb_serial_driver(serial_drivers, id_table);
Bill Pemberton52af9542010-07-29 11:05:41 -0400620
621MODULE_DESCRIPTION(DRIVER_DESC);
622MODULE_LICENSE("GPL");