blob: 3fee23bf0c141718228d6b3a5ae04bc42a58a96d [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
Rusty Russell90ab5ee2012-01-13 09:32:20 +103049static bool debug;
Bill Pemberton52af9542010-07-29 11:05:41 -040050
51/* Version Information */
52#define DRIVER_VERSION "v0.1"
53#define DRIVER_DESC "Quatech SSU-100 USB to Serial Driver"
54
55#define USB_VENDOR_ID_QUATECH 0x061d /* Quatech VID */
56#define QUATECH_SSU100 0xC020 /* SSU100 */
57
58static const struct usb_device_id id_table[] = {
59 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_SSU100)},
60 {} /* Terminating entry */
61};
Bill Pemberton52af9542010-07-29 11:05:41 -040062MODULE_DEVICE_TABLE(usb, id_table);
63
Bill Pemberton52af9542010-07-29 11:05:41 -040064struct ssu100_port_private {
Bill Pemberton17523052010-08-05 17:01:05 -040065 spinlock_t status_lock;
Bill Pemberton52af9542010-07-29 11:05:41 -040066 u8 shadowLSR;
67 u8 shadowMSR;
68 wait_queue_head_t delta_msr_wait; /* Used for TIOCMIWAIT */
Bill Pembertonf81c83d2010-08-05 17:01:09 -040069 struct async_icount icount;
Bill Pemberton52af9542010-07-29 11:05:41 -040070};
71
72static void ssu100_release(struct usb_serial *serial)
73{
74 struct ssu100_port_private *priv = usb_get_serial_port_data(*serial->port);
75
Bill Pemberton52af9542010-07-29 11:05:41 -040076 kfree(priv);
77}
78
79static inline int ssu100_control_msg(struct usb_device *dev,
80 u8 request, u16 data, u16 index)
81{
82 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
83 request, 0x40, data, index,
84 NULL, 0, 300);
85}
86
87static inline int ssu100_setdevice(struct usb_device *dev, u8 *data)
88{
89 u16 x = ((u16)(data[1] << 8) | (u16)(data[0]));
90
91 return ssu100_control_msg(dev, QT_SET_GET_DEVICE, x, 0);
92}
93
94
95static inline int ssu100_getdevice(struct usb_device *dev, u8 *data)
96{
97 return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
98 QT_SET_GET_DEVICE, 0xc0, 0, 0,
99 data, 3, 300);
100}
101
102static inline int ssu100_getregister(struct usb_device *dev,
103 unsigned short uart,
104 unsigned short reg,
105 u8 *data)
106{
107 return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
108 QT_SET_GET_REGISTER, 0xc0, reg,
109 uart, data, sizeof(*data), 300);
110
111}
112
113
114static inline int ssu100_setregister(struct usb_device *dev,
115 unsigned short uart,
Bill Pemberton556f1a02010-08-05 17:01:08 -0400116 unsigned short reg,
Bill Pemberton52af9542010-07-29 11:05:41 -0400117 u16 data)
118{
Bill Pemberton556f1a02010-08-05 17:01:08 -0400119 u16 value = (data << 8) | reg;
Bill Pemberton52af9542010-07-29 11:05:41 -0400120
121 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
122 QT_SET_GET_REGISTER, 0x40, value, uart,
123 NULL, 0, 300);
124
125}
126
127#define set_mctrl(dev, set) update_mctrl((dev), (set), 0)
128#define clear_mctrl(dev, clear) update_mctrl((dev), 0, (clear))
129
130/* these do not deal with device that have more than 1 port */
131static inline int update_mctrl(struct usb_device *dev, unsigned int set,
132 unsigned int clear)
133{
134 unsigned urb_value;
135 int result;
136
137 if (((set | clear) & (TIOCM_DTR | TIOCM_RTS)) == 0) {
138 dbg("%s - DTR|RTS not being set|cleared", __func__);
139 return 0; /* no change */
140 }
141
142 clear &= ~set; /* 'set' takes precedence over 'clear' */
143 urb_value = 0;
144 if (set & TIOCM_DTR)
Bill Pemberton79f203a2010-08-05 17:01:07 -0400145 urb_value |= UART_MCR_DTR;
Bill Pemberton52af9542010-07-29 11:05:41 -0400146 if (set & TIOCM_RTS)
Bill Pemberton79f203a2010-08-05 17:01:07 -0400147 urb_value |= UART_MCR_RTS;
Bill Pemberton52af9542010-07-29 11:05:41 -0400148
Bill Pemberton556f1a02010-08-05 17:01:08 -0400149 result = ssu100_setregister(dev, 0, UART_MCR, urb_value);
Bill Pemberton52af9542010-07-29 11:05:41 -0400150 if (result < 0)
151 dbg("%s Error from MODEM_CTRL urb", __func__);
152
153 return result;
154}
155
156static int ssu100_initdevice(struct usb_device *dev)
157{
158 u8 *data;
159 int result = 0;
160
Bill Pemberton52af9542010-07-29 11:05:41 -0400161 data = kzalloc(3, GFP_KERNEL);
162 if (!data)
163 return -ENOMEM;
164
165 result = ssu100_getdevice(dev, data);
166 if (result < 0) {
167 dbg("%s - get_device failed %i", __func__, result);
168 goto out;
169 }
170
171 data[1] &= ~FULLPWRBIT;
172
173 result = ssu100_setdevice(dev, data);
174 if (result < 0) {
175 dbg("%s - setdevice failed %i", __func__, result);
176 goto out;
177 }
178
179 result = ssu100_control_msg(dev, QT_GET_SET_PREBUF_TRIG_LVL, 128, 0);
180 if (result < 0) {
181 dbg("%s - set prebuffer level failed %i", __func__, result);
182 goto out;
183 }
184
185 result = ssu100_control_msg(dev, QT_SET_ATF, ATC_DISABLED, 0);
186 if (result < 0) {
187 dbg("%s - set ATFprebuffer level failed %i", __func__, result);
188 goto out;
189 }
190
191 result = ssu100_getdevice(dev, data);
192 if (result < 0) {
193 dbg("%s - get_device failed %i", __func__, result);
194 goto out;
195 }
196
197 data[0] &= ~(RR_BITS | DUPMODE_BITS);
198 data[0] |= CLKS_X4;
199 data[1] &= ~(LOOPMODE_BITS);
200 data[1] |= RS232_MODE;
201
202 result = ssu100_setdevice(dev, data);
203 if (result < 0) {
204 dbg("%s - setdevice failed %i", __func__, result);
205 goto out;
206 }
207
208out: kfree(data);
209 return result;
210
211}
212
213
214static void ssu100_set_termios(struct tty_struct *tty,
215 struct usb_serial_port *port,
216 struct ktermios *old_termios)
217{
218 struct usb_device *dev = port->serial->dev;
219 struct ktermios *termios = tty->termios;
220 u16 baud, divisor, remainder;
221 unsigned int cflag = termios->c_cflag;
222 u16 urb_value = 0; /* will hold the new flags */
223 int result;
224
Bill Pemberton52af9542010-07-29 11:05:41 -0400225 if (cflag & PARENB) {
226 if (cflag & PARODD)
Bill Pemberton79f203a2010-08-05 17:01:07 -0400227 urb_value |= UART_LCR_PARITY;
Bill Pemberton52af9542010-07-29 11:05:41 -0400228 else
229 urb_value |= SERIAL_EVEN_PARITY;
230 }
231
232 switch (cflag & CSIZE) {
233 case CS5:
Bill Pemberton79f203a2010-08-05 17:01:07 -0400234 urb_value |= UART_LCR_WLEN5;
Bill Pemberton52af9542010-07-29 11:05:41 -0400235 break;
236 case CS6:
Bill Pemberton79f203a2010-08-05 17:01:07 -0400237 urb_value |= UART_LCR_WLEN6;
Bill Pemberton52af9542010-07-29 11:05:41 -0400238 break;
239 case CS7:
Bill Pemberton79f203a2010-08-05 17:01:07 -0400240 urb_value |= UART_LCR_WLEN7;
Bill Pemberton52af9542010-07-29 11:05:41 -0400241 break;
242 default:
243 case CS8:
Bill Pemberton79f203a2010-08-05 17:01:07 -0400244 urb_value |= UART_LCR_WLEN8;
Bill Pemberton52af9542010-07-29 11:05:41 -0400245 break;
246 }
247
248 baud = tty_get_baud_rate(tty);
249 if (!baud)
250 baud = 9600;
251
252 dbg("%s - got baud = %d\n", __func__, baud);
253
254
255 divisor = MAX_BAUD_RATE / baud;
256 remainder = MAX_BAUD_RATE % baud;
257 if (((remainder * 2) >= baud) && (baud != 110))
258 divisor++;
259
260 urb_value = urb_value << 8;
261
262 result = ssu100_control_msg(dev, QT_GET_SET_UART, divisor, urb_value);
263 if (result < 0)
264 dbg("%s - set uart failed", __func__);
265
266 if (cflag & CRTSCTS)
267 result = ssu100_control_msg(dev, QT_HW_FLOW_CONTROL_MASK,
268 SERIAL_CRTSCTS, 0);
269 else
270 result = ssu100_control_msg(dev, QT_HW_FLOW_CONTROL_MASK,
271 0, 0);
272 if (result < 0)
273 dbg("%s - set HW flow control failed", __func__);
274
275 if (I_IXOFF(tty) || I_IXON(tty)) {
276 u16 x = ((u16)(START_CHAR(tty) << 8) | (u16)(STOP_CHAR(tty)));
277
278 result = ssu100_control_msg(dev, QT_SW_FLOW_CONTROL_MASK,
279 x, 0);
280 } else
281 result = ssu100_control_msg(dev, QT_SW_FLOW_CONTROL_MASK,
282 0, 0);
283
284 if (result < 0)
285 dbg("%s - set SW flow control failed", __func__);
286
287}
288
289
290static int ssu100_open(struct tty_struct *tty, struct usb_serial_port *port)
291{
292 struct usb_device *dev = port->serial->dev;
293 struct ssu100_port_private *priv = usb_get_serial_port_data(port);
294 u8 *data;
295 int result;
Bill Pemberton17523052010-08-05 17:01:05 -0400296 unsigned long flags;
Bill Pemberton52af9542010-07-29 11:05:41 -0400297
Bill Pemberton52af9542010-07-29 11:05:41 -0400298 data = kzalloc(2, GFP_KERNEL);
299 if (!data)
300 return -ENOMEM;
301
302 result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
303 QT_OPEN_CLOSE_CHANNEL,
304 QT_TRANSFER_IN, 0x01,
305 0, data, 2, 300);
306 if (result < 0) {
307 dbg("%s - open failed %i", __func__, result);
308 kfree(data);
309 return result;
310 }
311
Bill Pemberton17523052010-08-05 17:01:05 -0400312 spin_lock_irqsave(&priv->status_lock, flags);
Bill Pembertonf81c83d2010-08-05 17:01:09 -0400313 priv->shadowLSR = data[0];
314 priv->shadowMSR = data[1];
Bill Pemberton17523052010-08-05 17:01:05 -0400315 spin_unlock_irqrestore(&priv->status_lock, flags);
Bill Pemberton52af9542010-07-29 11:05:41 -0400316
317 kfree(data);
318
319/* set to 9600 */
320 result = ssu100_control_msg(dev, QT_GET_SET_UART, 0x30, 0x0300);
321 if (result < 0)
322 dbg("%s - set uart failed", __func__);
323
324 if (tty)
325 ssu100_set_termios(tty, port, tty->termios);
326
327 return usb_serial_generic_open(tty, port);
328}
329
330static void ssu100_close(struct usb_serial_port *port)
331{
Bill Pemberton52af9542010-07-29 11:05:41 -0400332 usb_serial_generic_close(port);
333}
334
335static int get_serial_info(struct usb_serial_port *port,
336 struct serial_struct __user *retinfo)
337{
338 struct serial_struct tmp;
339
340 if (!retinfo)
341 return -EFAULT;
342
343 memset(&tmp, 0, sizeof(tmp));
344 tmp.line = port->serial->minor;
345 tmp.port = 0;
346 tmp.irq = 0;
347 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
348 tmp.xmit_fifo_size = port->bulk_out_size;
349 tmp.baud_base = 9600;
350 tmp.close_delay = 5*HZ;
351 tmp.closing_wait = 30*HZ;
352
353 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
354 return -EFAULT;
355 return 0;
356}
357
Bill Pembertonf81c83d2010-08-05 17:01:09 -0400358static int wait_modem_info(struct usb_serial_port *port, unsigned int arg)
359{
360 struct ssu100_port_private *priv = usb_get_serial_port_data(port);
361 struct async_icount prev, cur;
362 unsigned long flags;
363
364 spin_lock_irqsave(&priv->status_lock, flags);
365 prev = priv->icount;
366 spin_unlock_irqrestore(&priv->status_lock, flags);
367
368 while (1) {
369 wait_event_interruptible(priv->delta_msr_wait,
370 ((priv->icount.rng != prev.rng) ||
371 (priv->icount.dsr != prev.dsr) ||
372 (priv->icount.dcd != prev.dcd) ||
373 (priv->icount.cts != prev.cts)));
374
375 if (signal_pending(current))
376 return -ERESTARTSYS;
377
378 spin_lock_irqsave(&priv->status_lock, flags);
379 cur = priv->icount;
380 spin_unlock_irqrestore(&priv->status_lock, flags);
381
382 if ((prev.rng == cur.rng) &&
383 (prev.dsr == cur.dsr) &&
384 (prev.dcd == cur.dcd) &&
385 (prev.cts == cur.cts))
386 return -EIO;
387
388 if ((arg & TIOCM_RNG && (prev.rng != cur.rng)) ||
389 (arg & TIOCM_DSR && (prev.dsr != cur.dsr)) ||
390 (arg & TIOCM_CD && (prev.dcd != cur.dcd)) ||
391 (arg & TIOCM_CTS && (prev.cts != cur.cts)))
392 return 0;
393 }
394 return 0;
395}
396
Alan Cox0bca1b92010-09-16 18:21:40 +0100397static int ssu100_get_icount(struct tty_struct *tty,
398 struct serial_icounter_struct *icount)
399{
400 struct usb_serial_port *port = tty->driver_data;
401 struct ssu100_port_private *priv = usb_get_serial_port_data(port);
402 struct async_icount cnow = priv->icount;
403
404 icount->cts = cnow.cts;
405 icount->dsr = cnow.dsr;
406 icount->rng = cnow.rng;
407 icount->dcd = cnow.dcd;
408 icount->rx = cnow.rx;
409 icount->tx = cnow.tx;
410 icount->frame = cnow.frame;
411 icount->overrun = cnow.overrun;
412 icount->parity = cnow.parity;
413 icount->brk = cnow.brk;
414 icount->buf_overrun = cnow.buf_overrun;
415
416 return 0;
417}
418
419
420
Alan Cox00a0d0d2011-02-14 16:27:06 +0000421static int ssu100_ioctl(struct tty_struct *tty,
Bill Pemberton52af9542010-07-29 11:05:41 -0400422 unsigned int cmd, unsigned long arg)
423{
424 struct usb_serial_port *port = tty->driver_data;
Bill Pemberton52af9542010-07-29 11:05:41 -0400425
426 dbg("%s cmd 0x%04x", __func__, cmd);
427
428 switch (cmd) {
429 case TIOCGSERIAL:
430 return get_serial_info(port,
431 (struct serial_struct __user *) arg);
432
433 case TIOCMIWAIT:
Bill Pembertonf81c83d2010-08-05 17:01:09 -0400434 return wait_modem_info(port, arg);
Bill Pemberton52af9542010-07-29 11:05:41 -0400435
Bill Pemberton52af9542010-07-29 11:05:41 -0400436 default:
437 break;
438 }
439
440 dbg("%s arg not supported", __func__);
441
442 return -ENOIOCTLCMD;
443}
444
Bill Pemberton52af9542010-07-29 11:05:41 -0400445static int ssu100_attach(struct usb_serial *serial)
446{
447 struct ssu100_port_private *priv;
448 struct usb_serial_port *port = *serial->port;
449
Bill Pemberton52af9542010-07-29 11:05:41 -0400450 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
451 if (!priv) {
452 dev_err(&port->dev, "%s- kmalloc(%Zd) failed.\n", __func__,
453 sizeof(*priv));
454 return -ENOMEM;
455 }
456
Bill Pemberton17523052010-08-05 17:01:05 -0400457 spin_lock_init(&priv->status_lock);
Bill Pemberton52af9542010-07-29 11:05:41 -0400458 init_waitqueue_head(&priv->delta_msr_wait);
459 usb_set_serial_port_data(port, priv);
Bill Pemberton52af9542010-07-29 11:05:41 -0400460
461 return ssu100_initdevice(serial->dev);
462}
463
Alan Cox60b33c12011-02-14 16:26:14 +0000464static int ssu100_tiocmget(struct tty_struct *tty)
Bill Pemberton52af9542010-07-29 11:05:41 -0400465{
466 struct usb_serial_port *port = tty->driver_data;
467 struct usb_device *dev = port->serial->dev;
468 u8 *d;
469 int r;
470
Bill Pemberton52af9542010-07-29 11:05:41 -0400471 d = kzalloc(2, GFP_KERNEL);
472 if (!d)
473 return -ENOMEM;
474
Bill Pemberton79f203a2010-08-05 17:01:07 -0400475 r = ssu100_getregister(dev, 0, UART_MCR, d);
Bill Pemberton52af9542010-07-29 11:05:41 -0400476 if (r < 0)
477 goto mget_out;
478
Bill Pemberton79f203a2010-08-05 17:01:07 -0400479 r = ssu100_getregister(dev, 0, UART_MSR, d+1);
Bill Pemberton52af9542010-07-29 11:05:41 -0400480 if (r < 0)
481 goto mget_out;
482
Bill Pemberton79f203a2010-08-05 17:01:07 -0400483 r = (d[0] & UART_MCR_DTR ? TIOCM_DTR : 0) |
484 (d[0] & UART_MCR_RTS ? TIOCM_RTS : 0) |
485 (d[1] & UART_MSR_CTS ? TIOCM_CTS : 0) |
486 (d[1] & UART_MSR_DCD ? TIOCM_CAR : 0) |
487 (d[1] & UART_MSR_RI ? TIOCM_RI : 0) |
488 (d[1] & UART_MSR_DSR ? TIOCM_DSR : 0);
Bill Pemberton52af9542010-07-29 11:05:41 -0400489
490mget_out:
491 kfree(d);
492 return r;
493}
494
Alan Cox20b9d172011-02-14 16:26:50 +0000495static int ssu100_tiocmset(struct tty_struct *tty,
Bill Pemberton52af9542010-07-29 11:05:41 -0400496 unsigned int set, unsigned int clear)
497{
498 struct usb_serial_port *port = tty->driver_data;
499 struct usb_device *dev = port->serial->dev;
500
Bill Pemberton52af9542010-07-29 11:05:41 -0400501 return update_mctrl(dev, set, clear);
502}
503
504static void ssu100_dtr_rts(struct usb_serial_port *port, int on)
505{
506 struct usb_device *dev = port->serial->dev;
507
Bill Pemberton52af9542010-07-29 11:05:41 -0400508 mutex_lock(&port->serial->disc_mutex);
509 if (!port->serial->disconnected) {
510 /* Disable flow control */
511 if (!on &&
Bill Pemberton556f1a02010-08-05 17:01:08 -0400512 ssu100_setregister(dev, 0, UART_MCR, 0) < 0)
Bill Pemberton52af9542010-07-29 11:05:41 -0400513 dev_err(&port->dev, "error from flowcontrol urb\n");
514 /* drop RTS and DTR */
515 if (on)
516 set_mctrl(dev, TIOCM_DTR | TIOCM_RTS);
517 else
518 clear_mctrl(dev, TIOCM_DTR | TIOCM_RTS);
519 }
520 mutex_unlock(&port->serial->disc_mutex);
521}
522
Bill Pembertonf81c83d2010-08-05 17:01:09 -0400523static void ssu100_update_msr(struct usb_serial_port *port, u8 msr)
524{
525 struct ssu100_port_private *priv = usb_get_serial_port_data(port);
526 unsigned long flags;
527
528 spin_lock_irqsave(&priv->status_lock, flags);
529 priv->shadowMSR = msr;
530 spin_unlock_irqrestore(&priv->status_lock, flags);
531
532 if (msr & UART_MSR_ANY_DELTA) {
533 /* update input line counters */
534 if (msr & UART_MSR_DCTS)
535 priv->icount.cts++;
536 if (msr & UART_MSR_DDSR)
537 priv->icount.dsr++;
538 if (msr & UART_MSR_DDCD)
539 priv->icount.dcd++;
540 if (msr & UART_MSR_TERI)
541 priv->icount.rng++;
542 wake_up_interruptible(&priv->delta_msr_wait);
543 }
544}
545
Bill Pemberton6b8f1ca2010-08-13 09:59:31 -0400546static void ssu100_update_lsr(struct usb_serial_port *port, u8 lsr,
547 char *tty_flag)
Bill Pembertonf81c83d2010-08-05 17:01:09 -0400548{
549 struct ssu100_port_private *priv = usb_get_serial_port_data(port);
550 unsigned long flags;
551
552 spin_lock_irqsave(&priv->status_lock, flags);
553 priv->shadowLSR = lsr;
554 spin_unlock_irqrestore(&priv->status_lock, flags);
555
Bill Pemberton6b8f1ca2010-08-13 09:59:31 -0400556 *tty_flag = TTY_NORMAL;
Bill Pembertonf81c83d2010-08-05 17:01:09 -0400557 if (lsr & UART_LSR_BRK_ERROR_BITS) {
Bill Pemberton6b8f1ca2010-08-13 09:59:31 -0400558 /* we always want to update icount, but we only want to
559 * update tty_flag for one case */
560 if (lsr & UART_LSR_BI) {
Bill Pembertonf81c83d2010-08-05 17:01:09 -0400561 priv->icount.brk++;
Bill Pemberton6b8f1ca2010-08-13 09:59:31 -0400562 *tty_flag = TTY_BREAK;
563 usb_serial_handle_break(port);
564 }
565 if (lsr & UART_LSR_PE) {
Bill Pembertonf81c83d2010-08-05 17:01:09 -0400566 priv->icount.parity++;
Bill Pemberton6b8f1ca2010-08-13 09:59:31 -0400567 if (*tty_flag == TTY_NORMAL)
568 *tty_flag = TTY_PARITY;
569 }
570 if (lsr & UART_LSR_FE) {
571 priv->icount.frame++;
572 if (*tty_flag == TTY_NORMAL)
573 *tty_flag = TTY_FRAME;
574 }
575 if (lsr & UART_LSR_OE){
Bill Pembertonf81c83d2010-08-05 17:01:09 -0400576 priv->icount.overrun++;
Bill Pemberton6b8f1ca2010-08-13 09:59:31 -0400577 if (*tty_flag == TTY_NORMAL)
578 *tty_flag = TTY_OVERRUN;
579 }
Bill Pembertonf81c83d2010-08-05 17:01:09 -0400580 }
Bill Pemberton6b8f1ca2010-08-13 09:59:31 -0400581
Bill Pembertonf81c83d2010-08-05 17:01:09 -0400582}
583
Bill Pembertonf7043ec2010-10-21 14:43:05 -0400584static int ssu100_process_packet(struct urb *urb,
585 struct tty_struct *tty)
Bill Pemberton52af9542010-07-29 11:05:41 -0400586{
Bill Pembertonf7043ec2010-10-21 14:43:05 -0400587 struct usb_serial_port *port = urb->context;
588 char *packet = (char *)urb->transfer_buffer;
Bill Pemberton6b8f1ca2010-08-13 09:59:31 -0400589 char flag = TTY_NORMAL;
Bill Pembertonf7043ec2010-10-21 14:43:05 -0400590 u32 len = urb->actual_length;
591 int i;
Bill Pemberton52af9542010-07-29 11:05:41 -0400592 char *ch;
593
Bill Pemberton9b2cef32010-08-05 17:01:06 -0400594 if ((len >= 4) &&
595 (packet[0] == 0x1b) && (packet[1] == 0x1b) &&
Bill Pemberton52af9542010-07-29 11:05:41 -0400596 ((packet[2] == 0x00) || (packet[2] == 0x01))) {
Bill Pemberton6b8f1ca2010-08-13 09:59:31 -0400597 if (packet[2] == 0x00) {
598 ssu100_update_lsr(port, packet[3], &flag);
599 if (flag == TTY_OVERRUN)
600 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
601 }
Bill Pembertonf81c83d2010-08-05 17:01:09 -0400602 if (packet[2] == 0x01)
603 ssu100_update_msr(port, packet[3]);
Bill Pemberton52af9542010-07-29 11:05:41 -0400604
605 len -= 4;
606 ch = packet + 4;
607 } else
608 ch = packet;
609
610 if (!len)
611 return 0; /* status only */
612
613 if (port->port.console && port->sysrq) {
614 for (i = 0; i < len; i++, ch++) {
Dmitry Torokhov6ee9f4b2010-08-17 21:15:47 -0700615 if (!usb_serial_handle_sysrq_char(port, *ch))
Bill Pemberton52af9542010-07-29 11:05:41 -0400616 tty_insert_flip_char(tty, *ch, flag);
617 }
618 } else
619 tty_insert_flip_string_fixed_flag(tty, ch, flag, len);
620
621 return len;
622}
623
624static void ssu100_process_read_urb(struct urb *urb)
625{
626 struct usb_serial_port *port = urb->context;
Bill Pemberton52af9542010-07-29 11:05:41 -0400627 struct tty_struct *tty;
Bill Pembertonf7043ec2010-10-21 14:43:05 -0400628 int count;
Bill Pemberton52af9542010-07-29 11:05:41 -0400629
Bill Pemberton52af9542010-07-29 11:05:41 -0400630 tty = tty_port_tty_get(&port->port);
631 if (!tty)
632 return;
633
Bill Pembertonf7043ec2010-10-21 14:43:05 -0400634 count = ssu100_process_packet(urb, tty);
Bill Pemberton52af9542010-07-29 11:05:41 -0400635
636 if (count)
637 tty_flip_buffer_push(tty);
638 tty_kref_put(tty);
639}
640
Bill Pemberton52af9542010-07-29 11:05:41 -0400641static struct usb_serial_driver ssu100_device = {
642 .driver = {
643 .owner = THIS_MODULE,
644 .name = "ssu100",
645 },
646 .description = DRIVER_DESC,
647 .id_table = id_table,
Bill Pemberton52af9542010-07-29 11:05:41 -0400648 .num_ports = 1,
Bill Pemberton52af9542010-07-29 11:05:41 -0400649 .open = ssu100_open,
650 .close = ssu100_close,
651 .attach = ssu100_attach,
652 .release = ssu100_release,
653 .dtr_rts = ssu100_dtr_rts,
654 .process_read_urb = ssu100_process_read_urb,
655 .tiocmget = ssu100_tiocmget,
656 .tiocmset = ssu100_tiocmset,
Alan Cox0bca1b92010-09-16 18:21:40 +0100657 .get_icount = ssu100_get_icount,
Bill Pemberton52af9542010-07-29 11:05:41 -0400658 .ioctl = ssu100_ioctl,
659 .set_termios = ssu100_set_termios,
Bill Pemberton85dee132010-08-05 17:01:11 -0400660 .disconnect = usb_serial_generic_disconnect,
Bill Pemberton52af9542010-07-29 11:05:41 -0400661};
662
Alan Sternd8603222012-02-23 14:57:25 -0500663static struct usb_serial_driver * const serial_drivers[] = {
664 &ssu100_device, NULL
665};
666
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -0700667module_usb_serial_driver(serial_drivers, id_table);
Bill Pemberton52af9542010-07-29 11:05:41 -0400668
669MODULE_DESCRIPTION(DRIVER_DESC);
670MODULE_LICENSE("GPL");
671
672module_param(debug, bool, S_IRUGO | S_IWUSR);
673MODULE_PARM_DESC(debug, "Debug enabled or not");