blob: 015810b3785bb72938d60691b81433a918583750 [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/* Version Information */
50#define DRIVER_VERSION "v0.1"
51#define DRIVER_DESC "Quatech SSU-100 USB to Serial Driver"
52
53#define USB_VENDOR_ID_QUATECH 0x061d /* Quatech VID */
54#define QUATECH_SSU100 0xC020 /* SSU100 */
55
56static const struct usb_device_id id_table[] = {
57 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_SSU100)},
58 {} /* Terminating entry */
59};
Bill Pemberton52af9542010-07-29 11:05:41 -040060MODULE_DEVICE_TABLE(usb, id_table);
61
Bill Pemberton52af9542010-07-29 11:05:41 -040062struct ssu100_port_private {
Bill Pemberton17523052010-08-05 17:01:05 -040063 spinlock_t status_lock;
Bill Pemberton52af9542010-07-29 11:05:41 -040064 u8 shadowLSR;
65 u8 shadowMSR;
66 wait_queue_head_t delta_msr_wait; /* Used for TIOCMIWAIT */
Bill Pembertonf81c83d2010-08-05 17:01:09 -040067 struct async_icount icount;
Bill Pemberton52af9542010-07-29 11:05:41 -040068};
69
70static void ssu100_release(struct usb_serial *serial)
71{
72 struct ssu100_port_private *priv = usb_get_serial_port_data(*serial->port);
73
Bill Pemberton52af9542010-07-29 11:05:41 -040074 kfree(priv);
75}
76
77static inline int ssu100_control_msg(struct usb_device *dev,
78 u8 request, u16 data, u16 index)
79{
80 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
81 request, 0x40, data, index,
82 NULL, 0, 300);
83}
84
85static inline int ssu100_setdevice(struct usb_device *dev, u8 *data)
86{
87 u16 x = ((u16)(data[1] << 8) | (u16)(data[0]));
88
89 return ssu100_control_msg(dev, QT_SET_GET_DEVICE, x, 0);
90}
91
92
93static inline int ssu100_getdevice(struct usb_device *dev, u8 *data)
94{
95 return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
96 QT_SET_GET_DEVICE, 0xc0, 0, 0,
97 data, 3, 300);
98}
99
100static inline int ssu100_getregister(struct usb_device *dev,
101 unsigned short uart,
102 unsigned short reg,
103 u8 *data)
104{
105 return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
106 QT_SET_GET_REGISTER, 0xc0, reg,
107 uart, data, sizeof(*data), 300);
108
109}
110
111
112static inline int ssu100_setregister(struct usb_device *dev,
113 unsigned short uart,
Bill Pemberton556f1a02010-08-05 17:01:08 -0400114 unsigned short reg,
Bill Pemberton52af9542010-07-29 11:05:41 -0400115 u16 data)
116{
Bill Pemberton556f1a02010-08-05 17:01:08 -0400117 u16 value = (data << 8) | reg;
Bill Pemberton52af9542010-07-29 11:05:41 -0400118
119 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
120 QT_SET_GET_REGISTER, 0x40, value, uart,
121 NULL, 0, 300);
122
123}
124
125#define set_mctrl(dev, set) update_mctrl((dev), (set), 0)
126#define clear_mctrl(dev, clear) update_mctrl((dev), 0, (clear))
127
128/* these do not deal with device that have more than 1 port */
129static inline int update_mctrl(struct usb_device *dev, unsigned int set,
130 unsigned int clear)
131{
132 unsigned urb_value;
133 int result;
134
135 if (((set | clear) & (TIOCM_DTR | TIOCM_RTS)) == 0) {
Greg Kroah-Hartman4f0c6412012-09-14 11:50:35 -0700136 dev_dbg(&dev->dev, "%s - DTR|RTS not being set|cleared\n", __func__);
Bill Pemberton52af9542010-07-29 11:05:41 -0400137 return 0; /* no change */
138 }
139
140 clear &= ~set; /* 'set' takes precedence over 'clear' */
141 urb_value = 0;
142 if (set & TIOCM_DTR)
Bill Pemberton79f203a2010-08-05 17:01:07 -0400143 urb_value |= UART_MCR_DTR;
Bill Pemberton52af9542010-07-29 11:05:41 -0400144 if (set & TIOCM_RTS)
Bill Pemberton79f203a2010-08-05 17:01:07 -0400145 urb_value |= UART_MCR_RTS;
Bill Pemberton52af9542010-07-29 11:05:41 -0400146
Bill Pemberton556f1a02010-08-05 17:01:08 -0400147 result = ssu100_setregister(dev, 0, UART_MCR, urb_value);
Bill Pemberton52af9542010-07-29 11:05:41 -0400148 if (result < 0)
Greg Kroah-Hartman4f0c6412012-09-14 11:50:35 -0700149 dev_dbg(&dev->dev, "%s Error from MODEM_CTRL urb\n", __func__);
Bill Pemberton52af9542010-07-29 11:05:41 -0400150
151 return result;
152}
153
154static int ssu100_initdevice(struct usb_device *dev)
155{
156 u8 *data;
157 int result = 0;
158
Bill Pemberton52af9542010-07-29 11:05:41 -0400159 data = kzalloc(3, GFP_KERNEL);
160 if (!data)
161 return -ENOMEM;
162
163 result = ssu100_getdevice(dev, data);
164 if (result < 0) {
Greg Kroah-Hartman4f0c6412012-09-14 11:50:35 -0700165 dev_dbg(&dev->dev, "%s - get_device failed %i\n", __func__, result);
Bill Pemberton52af9542010-07-29 11:05:41 -0400166 goto out;
167 }
168
169 data[1] &= ~FULLPWRBIT;
170
171 result = ssu100_setdevice(dev, data);
172 if (result < 0) {
Greg Kroah-Hartman4f0c6412012-09-14 11:50:35 -0700173 dev_dbg(&dev->dev, "%s - setdevice failed %i\n", __func__, result);
Bill Pemberton52af9542010-07-29 11:05:41 -0400174 goto out;
175 }
176
177 result = ssu100_control_msg(dev, QT_GET_SET_PREBUF_TRIG_LVL, 128, 0);
178 if (result < 0) {
Greg Kroah-Hartman4f0c6412012-09-14 11:50:35 -0700179 dev_dbg(&dev->dev, "%s - set prebuffer level failed %i\n", __func__, result);
Bill Pemberton52af9542010-07-29 11:05:41 -0400180 goto out;
181 }
182
183 result = ssu100_control_msg(dev, QT_SET_ATF, ATC_DISABLED, 0);
184 if (result < 0) {
Greg Kroah-Hartman4f0c6412012-09-14 11:50:35 -0700185 dev_dbg(&dev->dev, "%s - set ATFprebuffer level failed %i\n", __func__, result);
Bill Pemberton52af9542010-07-29 11:05:41 -0400186 goto out;
187 }
188
189 result = ssu100_getdevice(dev, data);
190 if (result < 0) {
Greg Kroah-Hartman4f0c6412012-09-14 11:50:35 -0700191 dev_dbg(&dev->dev, "%s - get_device failed %i\n", __func__, result);
Bill Pemberton52af9542010-07-29 11:05:41 -0400192 goto out;
193 }
194
195 data[0] &= ~(RR_BITS | DUPMODE_BITS);
196 data[0] |= CLKS_X4;
197 data[1] &= ~(LOOPMODE_BITS);
198 data[1] |= RS232_MODE;
199
200 result = ssu100_setdevice(dev, data);
201 if (result < 0) {
Greg Kroah-Hartman4f0c6412012-09-14 11:50:35 -0700202 dev_dbg(&dev->dev, "%s - setdevice failed %i\n", __func__, result);
Bill Pemberton52af9542010-07-29 11:05:41 -0400203 goto out;
204 }
205
206out: kfree(data);
207 return result;
208
209}
210
211
212static void ssu100_set_termios(struct tty_struct *tty,
213 struct usb_serial_port *port,
214 struct ktermios *old_termios)
215{
216 struct usb_device *dev = port->serial->dev;
Alan Coxadc8d742012-07-14 15:31:47 +0100217 struct ktermios *termios = &tty->termios;
Bill Pemberton52af9542010-07-29 11:05:41 -0400218 u16 baud, divisor, remainder;
219 unsigned int cflag = termios->c_cflag;
220 u16 urb_value = 0; /* will hold the new flags */
221 int result;
222
Bill Pemberton52af9542010-07-29 11:05:41 -0400223 if (cflag & PARENB) {
224 if (cflag & PARODD)
Bill Pemberton79f203a2010-08-05 17:01:07 -0400225 urb_value |= UART_LCR_PARITY;
Bill Pemberton52af9542010-07-29 11:05:41 -0400226 else
227 urb_value |= SERIAL_EVEN_PARITY;
228 }
229
230 switch (cflag & CSIZE) {
231 case CS5:
Bill Pemberton79f203a2010-08-05 17:01:07 -0400232 urb_value |= UART_LCR_WLEN5;
Bill Pemberton52af9542010-07-29 11:05:41 -0400233 break;
234 case CS6:
Bill Pemberton79f203a2010-08-05 17:01:07 -0400235 urb_value |= UART_LCR_WLEN6;
Bill Pemberton52af9542010-07-29 11:05:41 -0400236 break;
237 case CS7:
Bill Pemberton79f203a2010-08-05 17:01:07 -0400238 urb_value |= UART_LCR_WLEN7;
Bill Pemberton52af9542010-07-29 11:05:41 -0400239 break;
240 default:
241 case CS8:
Bill Pemberton79f203a2010-08-05 17:01:07 -0400242 urb_value |= UART_LCR_WLEN8;
Bill Pemberton52af9542010-07-29 11:05:41 -0400243 break;
244 }
245
246 baud = tty_get_baud_rate(tty);
247 if (!baud)
248 baud = 9600;
249
Greg Kroah-Hartman4f0c6412012-09-14 11:50:35 -0700250 dev_dbg(&port->dev, "%s - got baud = %d\n", __func__, baud);
Bill Pemberton52af9542010-07-29 11:05:41 -0400251
252
253 divisor = MAX_BAUD_RATE / baud;
254 remainder = MAX_BAUD_RATE % baud;
255 if (((remainder * 2) >= baud) && (baud != 110))
256 divisor++;
257
258 urb_value = urb_value << 8;
259
260 result = ssu100_control_msg(dev, QT_GET_SET_UART, divisor, urb_value);
261 if (result < 0)
Greg Kroah-Hartman4f0c6412012-09-14 11:50:35 -0700262 dev_dbg(&port->dev, "%s - set uart failed\n", __func__);
Bill Pemberton52af9542010-07-29 11:05:41 -0400263
264 if (cflag & CRTSCTS)
265 result = ssu100_control_msg(dev, QT_HW_FLOW_CONTROL_MASK,
266 SERIAL_CRTSCTS, 0);
267 else
268 result = ssu100_control_msg(dev, QT_HW_FLOW_CONTROL_MASK,
269 0, 0);
270 if (result < 0)
Greg Kroah-Hartman4f0c6412012-09-14 11:50:35 -0700271 dev_dbg(&port->dev, "%s - set HW flow control failed\n", __func__);
Bill Pemberton52af9542010-07-29 11:05:41 -0400272
273 if (I_IXOFF(tty) || I_IXON(tty)) {
274 u16 x = ((u16)(START_CHAR(tty) << 8) | (u16)(STOP_CHAR(tty)));
275
276 result = ssu100_control_msg(dev, QT_SW_FLOW_CONTROL_MASK,
277 x, 0);
278 } else
279 result = ssu100_control_msg(dev, QT_SW_FLOW_CONTROL_MASK,
280 0, 0);
281
282 if (result < 0)
Greg Kroah-Hartman4f0c6412012-09-14 11:50:35 -0700283 dev_dbg(&port->dev, "%s - set SW flow control failed\n", __func__);
Bill Pemberton52af9542010-07-29 11:05:41 -0400284
285}
286
287
288static int ssu100_open(struct tty_struct *tty, struct usb_serial_port *port)
289{
290 struct usb_device *dev = port->serial->dev;
291 struct ssu100_port_private *priv = usb_get_serial_port_data(port);
292 u8 *data;
293 int result;
Bill Pemberton17523052010-08-05 17:01:05 -0400294 unsigned long flags;
Bill Pemberton52af9542010-07-29 11:05:41 -0400295
Bill Pemberton52af9542010-07-29 11:05:41 -0400296 data = kzalloc(2, GFP_KERNEL);
297 if (!data)
298 return -ENOMEM;
299
300 result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
301 QT_OPEN_CLOSE_CHANNEL,
302 QT_TRANSFER_IN, 0x01,
303 0, data, 2, 300);
304 if (result < 0) {
Greg Kroah-Hartman4f0c6412012-09-14 11:50:35 -0700305 dev_dbg(&port->dev, "%s - open failed %i\n", __func__, result);
Bill Pemberton52af9542010-07-29 11:05:41 -0400306 kfree(data);
307 return result;
308 }
309
Bill Pemberton17523052010-08-05 17:01:05 -0400310 spin_lock_irqsave(&priv->status_lock, flags);
Bill Pembertonf81c83d2010-08-05 17:01:09 -0400311 priv->shadowLSR = data[0];
312 priv->shadowMSR = data[1];
Bill Pemberton17523052010-08-05 17:01:05 -0400313 spin_unlock_irqrestore(&priv->status_lock, flags);
Bill Pemberton52af9542010-07-29 11:05:41 -0400314
315 kfree(data);
316
317/* set to 9600 */
318 result = ssu100_control_msg(dev, QT_GET_SET_UART, 0x30, 0x0300);
319 if (result < 0)
Greg Kroah-Hartman4f0c6412012-09-14 11:50:35 -0700320 dev_dbg(&port->dev, "%s - set uart failed\n", __func__);
Bill Pemberton52af9542010-07-29 11:05:41 -0400321
322 if (tty)
Alan Coxadc8d742012-07-14 15:31:47 +0100323 ssu100_set_termios(tty, port, &tty->termios);
Bill Pemberton52af9542010-07-29 11:05:41 -0400324
325 return usb_serial_generic_open(tty, port);
326}
327
328static void ssu100_close(struct usb_serial_port *port)
329{
Bill Pemberton52af9542010-07-29 11:05:41 -0400330 usb_serial_generic_close(port);
331}
332
333static int get_serial_info(struct usb_serial_port *port,
334 struct serial_struct __user *retinfo)
335{
336 struct serial_struct tmp;
337
338 if (!retinfo)
339 return -EFAULT;
340
341 memset(&tmp, 0, sizeof(tmp));
342 tmp.line = port->serial->minor;
343 tmp.port = 0;
344 tmp.irq = 0;
345 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
346 tmp.xmit_fifo_size = port->bulk_out_size;
347 tmp.baud_base = 9600;
348 tmp.close_delay = 5*HZ;
349 tmp.closing_wait = 30*HZ;
350
351 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
352 return -EFAULT;
353 return 0;
354}
355
Bill Pembertonf81c83d2010-08-05 17:01:09 -0400356static int wait_modem_info(struct usb_serial_port *port, unsigned int arg)
357{
358 struct ssu100_port_private *priv = usb_get_serial_port_data(port);
359 struct async_icount prev, cur;
360 unsigned long flags;
361
362 spin_lock_irqsave(&priv->status_lock, flags);
363 prev = priv->icount;
364 spin_unlock_irqrestore(&priv->status_lock, flags);
365
366 while (1) {
367 wait_event_interruptible(priv->delta_msr_wait,
368 ((priv->icount.rng != prev.rng) ||
369 (priv->icount.dsr != prev.dsr) ||
370 (priv->icount.dcd != prev.dcd) ||
371 (priv->icount.cts != prev.cts)));
372
373 if (signal_pending(current))
374 return -ERESTARTSYS;
375
376 spin_lock_irqsave(&priv->status_lock, flags);
377 cur = priv->icount;
378 spin_unlock_irqrestore(&priv->status_lock, flags);
379
380 if ((prev.rng == cur.rng) &&
381 (prev.dsr == cur.dsr) &&
382 (prev.dcd == cur.dcd) &&
383 (prev.cts == cur.cts))
384 return -EIO;
385
386 if ((arg & TIOCM_RNG && (prev.rng != cur.rng)) ||
387 (arg & TIOCM_DSR && (prev.dsr != cur.dsr)) ||
388 (arg & TIOCM_CD && (prev.dcd != cur.dcd)) ||
389 (arg & TIOCM_CTS && (prev.cts != cur.cts)))
390 return 0;
391 }
392 return 0;
393}
394
Alan Cox0bca1b92010-09-16 18:21:40 +0100395static int ssu100_get_icount(struct tty_struct *tty,
396 struct serial_icounter_struct *icount)
397{
398 struct usb_serial_port *port = tty->driver_data;
399 struct ssu100_port_private *priv = usb_get_serial_port_data(port);
400 struct async_icount cnow = priv->icount;
401
402 icount->cts = cnow.cts;
403 icount->dsr = cnow.dsr;
404 icount->rng = cnow.rng;
405 icount->dcd = cnow.dcd;
406 icount->rx = cnow.rx;
407 icount->tx = cnow.tx;
408 icount->frame = cnow.frame;
409 icount->overrun = cnow.overrun;
410 icount->parity = cnow.parity;
411 icount->brk = cnow.brk;
412 icount->buf_overrun = cnow.buf_overrun;
413
414 return 0;
415}
416
417
418
Alan Cox00a0d0d2011-02-14 16:27:06 +0000419static int ssu100_ioctl(struct tty_struct *tty,
Bill Pemberton52af9542010-07-29 11:05:41 -0400420 unsigned int cmd, unsigned long arg)
421{
422 struct usb_serial_port *port = tty->driver_data;
Bill Pemberton52af9542010-07-29 11:05:41 -0400423
Greg Kroah-Hartman4f0c6412012-09-14 11:50:35 -0700424 dev_dbg(&port->dev, "%s cmd 0x%04x\n", __func__, cmd);
Bill Pemberton52af9542010-07-29 11:05:41 -0400425
426 switch (cmd) {
427 case TIOCGSERIAL:
428 return get_serial_info(port,
429 (struct serial_struct __user *) arg);
430
431 case TIOCMIWAIT:
Bill Pembertonf81c83d2010-08-05 17:01:09 -0400432 return wait_modem_info(port, arg);
Bill Pemberton52af9542010-07-29 11:05:41 -0400433
Bill Pemberton52af9542010-07-29 11:05:41 -0400434 default:
435 break;
436 }
437
Greg Kroah-Hartman4f0c6412012-09-14 11:50:35 -0700438 dev_dbg(&port->dev, "%s arg not supported\n", __func__);
Bill Pemberton52af9542010-07-29 11:05:41 -0400439
440 return -ENOIOCTLCMD;
441}
442
Bill Pemberton52af9542010-07-29 11:05:41 -0400443static int ssu100_attach(struct usb_serial *serial)
444{
445 struct ssu100_port_private *priv;
446 struct usb_serial_port *port = *serial->port;
447
Bill Pemberton52af9542010-07-29 11:05:41 -0400448 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
449 if (!priv) {
450 dev_err(&port->dev, "%s- kmalloc(%Zd) failed.\n", __func__,
451 sizeof(*priv));
452 return -ENOMEM;
453 }
454
Bill Pemberton17523052010-08-05 17:01:05 -0400455 spin_lock_init(&priv->status_lock);
Bill Pemberton52af9542010-07-29 11:05:41 -0400456 init_waitqueue_head(&priv->delta_msr_wait);
457 usb_set_serial_port_data(port, priv);
Bill Pemberton52af9542010-07-29 11:05:41 -0400458
459 return ssu100_initdevice(serial->dev);
460}
461
Alan Cox60b33c12011-02-14 16:26:14 +0000462static int ssu100_tiocmget(struct tty_struct *tty)
Bill Pemberton52af9542010-07-29 11:05:41 -0400463{
464 struct usb_serial_port *port = tty->driver_data;
465 struct usb_device *dev = port->serial->dev;
466 u8 *d;
467 int r;
468
Bill Pemberton52af9542010-07-29 11:05:41 -0400469 d = kzalloc(2, GFP_KERNEL);
470 if (!d)
471 return -ENOMEM;
472
Bill Pemberton79f203a2010-08-05 17:01:07 -0400473 r = ssu100_getregister(dev, 0, UART_MCR, d);
Bill Pemberton52af9542010-07-29 11:05:41 -0400474 if (r < 0)
475 goto mget_out;
476
Bill Pemberton79f203a2010-08-05 17:01:07 -0400477 r = ssu100_getregister(dev, 0, UART_MSR, d+1);
Bill Pemberton52af9542010-07-29 11:05:41 -0400478 if (r < 0)
479 goto mget_out;
480
Bill Pemberton79f203a2010-08-05 17:01:07 -0400481 r = (d[0] & UART_MCR_DTR ? TIOCM_DTR : 0) |
482 (d[0] & UART_MCR_RTS ? TIOCM_RTS : 0) |
483 (d[1] & UART_MSR_CTS ? TIOCM_CTS : 0) |
484 (d[1] & UART_MSR_DCD ? TIOCM_CAR : 0) |
485 (d[1] & UART_MSR_RI ? TIOCM_RI : 0) |
486 (d[1] & UART_MSR_DSR ? TIOCM_DSR : 0);
Bill Pemberton52af9542010-07-29 11:05:41 -0400487
488mget_out:
489 kfree(d);
490 return r;
491}
492
Alan Cox20b9d172011-02-14 16:26:50 +0000493static int ssu100_tiocmset(struct tty_struct *tty,
Bill Pemberton52af9542010-07-29 11:05:41 -0400494 unsigned int set, unsigned int clear)
495{
496 struct usb_serial_port *port = tty->driver_data;
497 struct usb_device *dev = port->serial->dev;
498
Bill Pemberton52af9542010-07-29 11:05:41 -0400499 return update_mctrl(dev, set, clear);
500}
501
502static void ssu100_dtr_rts(struct usb_serial_port *port, int on)
503{
504 struct usb_device *dev = port->serial->dev;
505
Bill Pemberton52af9542010-07-29 11:05:41 -0400506 mutex_lock(&port->serial->disc_mutex);
507 if (!port->serial->disconnected) {
508 /* Disable flow control */
509 if (!on &&
Bill Pemberton556f1a02010-08-05 17:01:08 -0400510 ssu100_setregister(dev, 0, UART_MCR, 0) < 0)
Bill Pemberton52af9542010-07-29 11:05:41 -0400511 dev_err(&port->dev, "error from flowcontrol urb\n");
512 /* drop RTS and DTR */
513 if (on)
514 set_mctrl(dev, TIOCM_DTR | TIOCM_RTS);
515 else
516 clear_mctrl(dev, TIOCM_DTR | TIOCM_RTS);
517 }
518 mutex_unlock(&port->serial->disc_mutex);
519}
520
Bill Pembertonf81c83d2010-08-05 17:01:09 -0400521static void ssu100_update_msr(struct usb_serial_port *port, u8 msr)
522{
523 struct ssu100_port_private *priv = usb_get_serial_port_data(port);
524 unsigned long flags;
525
526 spin_lock_irqsave(&priv->status_lock, flags);
527 priv->shadowMSR = msr;
528 spin_unlock_irqrestore(&priv->status_lock, flags);
529
530 if (msr & UART_MSR_ANY_DELTA) {
531 /* update input line counters */
532 if (msr & UART_MSR_DCTS)
533 priv->icount.cts++;
534 if (msr & UART_MSR_DDSR)
535 priv->icount.dsr++;
536 if (msr & UART_MSR_DDCD)
537 priv->icount.dcd++;
538 if (msr & UART_MSR_TERI)
539 priv->icount.rng++;
540 wake_up_interruptible(&priv->delta_msr_wait);
541 }
542}
543
Bill Pemberton6b8f1ca2010-08-13 09:59:31 -0400544static void ssu100_update_lsr(struct usb_serial_port *port, u8 lsr,
545 char *tty_flag)
Bill Pembertonf81c83d2010-08-05 17:01:09 -0400546{
547 struct ssu100_port_private *priv = usb_get_serial_port_data(port);
548 unsigned long flags;
549
550 spin_lock_irqsave(&priv->status_lock, flags);
551 priv->shadowLSR = lsr;
552 spin_unlock_irqrestore(&priv->status_lock, flags);
553
Bill Pemberton6b8f1ca2010-08-13 09:59:31 -0400554 *tty_flag = TTY_NORMAL;
Bill Pembertonf81c83d2010-08-05 17:01:09 -0400555 if (lsr & UART_LSR_BRK_ERROR_BITS) {
Bill Pemberton6b8f1ca2010-08-13 09:59:31 -0400556 /* we always want to update icount, but we only want to
557 * update tty_flag for one case */
558 if (lsr & UART_LSR_BI) {
Bill Pembertonf81c83d2010-08-05 17:01:09 -0400559 priv->icount.brk++;
Bill Pemberton6b8f1ca2010-08-13 09:59:31 -0400560 *tty_flag = TTY_BREAK;
561 usb_serial_handle_break(port);
562 }
563 if (lsr & UART_LSR_PE) {
Bill Pembertonf81c83d2010-08-05 17:01:09 -0400564 priv->icount.parity++;
Bill Pemberton6b8f1ca2010-08-13 09:59:31 -0400565 if (*tty_flag == TTY_NORMAL)
566 *tty_flag = TTY_PARITY;
567 }
568 if (lsr & UART_LSR_FE) {
569 priv->icount.frame++;
570 if (*tty_flag == TTY_NORMAL)
571 *tty_flag = TTY_FRAME;
572 }
573 if (lsr & UART_LSR_OE){
Bill Pembertonf81c83d2010-08-05 17:01:09 -0400574 priv->icount.overrun++;
Bill Pemberton6b8f1ca2010-08-13 09:59:31 -0400575 if (*tty_flag == TTY_NORMAL)
576 *tty_flag = TTY_OVERRUN;
577 }
Bill Pembertonf81c83d2010-08-05 17:01:09 -0400578 }
Bill Pemberton6b8f1ca2010-08-13 09:59:31 -0400579
Bill Pembertonf81c83d2010-08-05 17:01:09 -0400580}
581
Bill Pembertonf7043ec2010-10-21 14:43:05 -0400582static int ssu100_process_packet(struct urb *urb,
583 struct tty_struct *tty)
Bill Pemberton52af9542010-07-29 11:05:41 -0400584{
Bill Pembertonf7043ec2010-10-21 14:43:05 -0400585 struct usb_serial_port *port = urb->context;
586 char *packet = (char *)urb->transfer_buffer;
Bill Pemberton6b8f1ca2010-08-13 09:59:31 -0400587 char flag = TTY_NORMAL;
Bill Pembertonf7043ec2010-10-21 14:43:05 -0400588 u32 len = urb->actual_length;
589 int i;
Bill Pemberton52af9542010-07-29 11:05:41 -0400590 char *ch;
591
Bill Pemberton9b2cef32010-08-05 17:01:06 -0400592 if ((len >= 4) &&
593 (packet[0] == 0x1b) && (packet[1] == 0x1b) &&
Bill Pemberton52af9542010-07-29 11:05:41 -0400594 ((packet[2] == 0x00) || (packet[2] == 0x01))) {
Bill Pemberton6b8f1ca2010-08-13 09:59:31 -0400595 if (packet[2] == 0x00) {
596 ssu100_update_lsr(port, packet[3], &flag);
597 if (flag == TTY_OVERRUN)
598 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
599 }
Bill Pembertonf81c83d2010-08-05 17:01:09 -0400600 if (packet[2] == 0x01)
601 ssu100_update_msr(port, packet[3]);
Bill Pemberton52af9542010-07-29 11:05:41 -0400602
603 len -= 4;
604 ch = packet + 4;
605 } else
606 ch = packet;
607
608 if (!len)
609 return 0; /* status only */
610
611 if (port->port.console && port->sysrq) {
612 for (i = 0; i < len; i++, ch++) {
Dmitry Torokhov6ee9f4b2010-08-17 21:15:47 -0700613 if (!usb_serial_handle_sysrq_char(port, *ch))
Bill Pemberton52af9542010-07-29 11:05:41 -0400614 tty_insert_flip_char(tty, *ch, flag);
615 }
616 } else
617 tty_insert_flip_string_fixed_flag(tty, ch, flag, len);
618
619 return len;
620}
621
622static void ssu100_process_read_urb(struct urb *urb)
623{
624 struct usb_serial_port *port = urb->context;
Bill Pemberton52af9542010-07-29 11:05:41 -0400625 struct tty_struct *tty;
Bill Pembertonf7043ec2010-10-21 14:43:05 -0400626 int count;
Bill Pemberton52af9542010-07-29 11:05:41 -0400627
Bill Pemberton52af9542010-07-29 11:05:41 -0400628 tty = tty_port_tty_get(&port->port);
629 if (!tty)
630 return;
631
Bill Pembertonf7043ec2010-10-21 14:43:05 -0400632 count = ssu100_process_packet(urb, tty);
Bill Pemberton52af9542010-07-29 11:05:41 -0400633
634 if (count)
635 tty_flip_buffer_push(tty);
636 tty_kref_put(tty);
637}
638
Bill Pemberton52af9542010-07-29 11:05:41 -0400639static struct usb_serial_driver ssu100_device = {
640 .driver = {
641 .owner = THIS_MODULE,
642 .name = "ssu100",
643 },
644 .description = DRIVER_DESC,
645 .id_table = id_table,
Bill Pemberton52af9542010-07-29 11:05:41 -0400646 .num_ports = 1,
Bill Pemberton52af9542010-07-29 11:05:41 -0400647 .open = ssu100_open,
648 .close = ssu100_close,
649 .attach = ssu100_attach,
650 .release = ssu100_release,
651 .dtr_rts = ssu100_dtr_rts,
652 .process_read_urb = ssu100_process_read_urb,
653 .tiocmget = ssu100_tiocmget,
654 .tiocmset = ssu100_tiocmset,
Alan Cox0bca1b92010-09-16 18:21:40 +0100655 .get_icount = ssu100_get_icount,
Bill Pemberton52af9542010-07-29 11:05:41 -0400656 .ioctl = ssu100_ioctl,
657 .set_termios = ssu100_set_termios,
Bill Pemberton85dee132010-08-05 17:01:11 -0400658 .disconnect = usb_serial_generic_disconnect,
Bill Pemberton52af9542010-07-29 11:05:41 -0400659};
660
Alan Sternd8603222012-02-23 14:57:25 -0500661static struct usb_serial_driver * const serial_drivers[] = {
662 &ssu100_device, NULL
663};
664
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -0700665module_usb_serial_driver(serial_drivers, id_table);
Bill Pemberton52af9542010-07-29 11:05:41 -0400666
667MODULE_DESCRIPTION(DRIVER_DESC);
668MODULE_LICENSE("GPL");