blob: 4b2a19757b4d531baa8dfb48c8009517d2d21cb5 [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 Pembertonf81c83d2010-08-05 17:01:09 -040064 struct async_icount icount;
Bill Pemberton52af9542010-07-29 11:05:41 -040065};
66
Bill Pemberton52af9542010-07-29 11:05:41 -040067static inline int ssu100_control_msg(struct usb_device *dev,
68 u8 request, u16 data, u16 index)
69{
70 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
71 request, 0x40, data, index,
72 NULL, 0, 300);
73}
74
75static inline int ssu100_setdevice(struct usb_device *dev, u8 *data)
76{
77 u16 x = ((u16)(data[1] << 8) | (u16)(data[0]));
78
79 return ssu100_control_msg(dev, QT_SET_GET_DEVICE, x, 0);
80}
81
82
83static inline int ssu100_getdevice(struct usb_device *dev, u8 *data)
84{
85 return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
86 QT_SET_GET_DEVICE, 0xc0, 0, 0,
87 data, 3, 300);
88}
89
90static inline int ssu100_getregister(struct usb_device *dev,
91 unsigned short uart,
92 unsigned short reg,
93 u8 *data)
94{
95 return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
96 QT_SET_GET_REGISTER, 0xc0, reg,
97 uart, data, sizeof(*data), 300);
98
99}
100
101
102static inline int ssu100_setregister(struct usb_device *dev,
103 unsigned short uart,
Bill Pemberton556f1a02010-08-05 17:01:08 -0400104 unsigned short reg,
Bill Pemberton52af9542010-07-29 11:05:41 -0400105 u16 data)
106{
Bill Pemberton556f1a02010-08-05 17:01:08 -0400107 u16 value = (data << 8) | reg;
Bill Pemberton52af9542010-07-29 11:05:41 -0400108
109 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
110 QT_SET_GET_REGISTER, 0x40, value, uart,
111 NULL, 0, 300);
112
113}
114
115#define set_mctrl(dev, set) update_mctrl((dev), (set), 0)
116#define clear_mctrl(dev, clear) update_mctrl((dev), 0, (clear))
117
118/* these do not deal with device that have more than 1 port */
119static inline int update_mctrl(struct usb_device *dev, unsigned int set,
120 unsigned int clear)
121{
122 unsigned urb_value;
123 int result;
124
125 if (((set | clear) & (TIOCM_DTR | TIOCM_RTS)) == 0) {
Greg Kroah-Hartman4f0c6412012-09-14 11:50:35 -0700126 dev_dbg(&dev->dev, "%s - DTR|RTS not being set|cleared\n", __func__);
Bill Pemberton52af9542010-07-29 11:05:41 -0400127 return 0; /* no change */
128 }
129
130 clear &= ~set; /* 'set' takes precedence over 'clear' */
131 urb_value = 0;
132 if (set & TIOCM_DTR)
Bill Pemberton79f203a2010-08-05 17:01:07 -0400133 urb_value |= UART_MCR_DTR;
Bill Pemberton52af9542010-07-29 11:05:41 -0400134 if (set & TIOCM_RTS)
Bill Pemberton79f203a2010-08-05 17:01:07 -0400135 urb_value |= UART_MCR_RTS;
Bill Pemberton52af9542010-07-29 11:05:41 -0400136
Bill Pemberton556f1a02010-08-05 17:01:08 -0400137 result = ssu100_setregister(dev, 0, UART_MCR, urb_value);
Bill Pemberton52af9542010-07-29 11:05:41 -0400138 if (result < 0)
Greg Kroah-Hartman4f0c6412012-09-14 11:50:35 -0700139 dev_dbg(&dev->dev, "%s Error from MODEM_CTRL urb\n", __func__);
Bill Pemberton52af9542010-07-29 11:05:41 -0400140
141 return result;
142}
143
144static int ssu100_initdevice(struct usb_device *dev)
145{
146 u8 *data;
147 int result = 0;
148
Bill Pemberton52af9542010-07-29 11:05:41 -0400149 data = kzalloc(3, GFP_KERNEL);
150 if (!data)
151 return -ENOMEM;
152
153 result = ssu100_getdevice(dev, data);
154 if (result < 0) {
Greg Kroah-Hartman4f0c6412012-09-14 11:50:35 -0700155 dev_dbg(&dev->dev, "%s - get_device failed %i\n", __func__, result);
Bill Pemberton52af9542010-07-29 11:05:41 -0400156 goto out;
157 }
158
159 data[1] &= ~FULLPWRBIT;
160
161 result = ssu100_setdevice(dev, data);
162 if (result < 0) {
Greg Kroah-Hartman4f0c6412012-09-14 11:50:35 -0700163 dev_dbg(&dev->dev, "%s - setdevice failed %i\n", __func__, result);
Bill Pemberton52af9542010-07-29 11:05:41 -0400164 goto out;
165 }
166
167 result = ssu100_control_msg(dev, QT_GET_SET_PREBUF_TRIG_LVL, 128, 0);
168 if (result < 0) {
Greg Kroah-Hartman4f0c6412012-09-14 11:50:35 -0700169 dev_dbg(&dev->dev, "%s - set prebuffer level failed %i\n", __func__, result);
Bill Pemberton52af9542010-07-29 11:05:41 -0400170 goto out;
171 }
172
173 result = ssu100_control_msg(dev, QT_SET_ATF, ATC_DISABLED, 0);
174 if (result < 0) {
Greg Kroah-Hartman4f0c6412012-09-14 11:50:35 -0700175 dev_dbg(&dev->dev, "%s - set ATFprebuffer level failed %i\n", __func__, result);
Bill Pemberton52af9542010-07-29 11:05:41 -0400176 goto out;
177 }
178
179 result = ssu100_getdevice(dev, data);
180 if (result < 0) {
Greg Kroah-Hartman4f0c6412012-09-14 11:50:35 -0700181 dev_dbg(&dev->dev, "%s - get_device failed %i\n", __func__, result);
Bill Pemberton52af9542010-07-29 11:05:41 -0400182 goto out;
183 }
184
185 data[0] &= ~(RR_BITS | DUPMODE_BITS);
186 data[0] |= CLKS_X4;
187 data[1] &= ~(LOOPMODE_BITS);
188 data[1] |= RS232_MODE;
189
190 result = ssu100_setdevice(dev, data);
191 if (result < 0) {
Greg Kroah-Hartman4f0c6412012-09-14 11:50:35 -0700192 dev_dbg(&dev->dev, "%s - setdevice failed %i\n", __func__, result);
Bill Pemberton52af9542010-07-29 11:05:41 -0400193 goto out;
194 }
195
196out: kfree(data);
197 return result;
198
199}
200
201
202static void ssu100_set_termios(struct tty_struct *tty,
203 struct usb_serial_port *port,
204 struct ktermios *old_termios)
205{
206 struct usb_device *dev = port->serial->dev;
Alan Coxadc8d742012-07-14 15:31:47 +0100207 struct ktermios *termios = &tty->termios;
Bill Pemberton52af9542010-07-29 11:05:41 -0400208 u16 baud, divisor, remainder;
209 unsigned int cflag = termios->c_cflag;
210 u16 urb_value = 0; /* will hold the new flags */
211 int result;
212
Bill Pemberton52af9542010-07-29 11:05:41 -0400213 if (cflag & PARENB) {
214 if (cflag & PARODD)
Bill Pemberton79f203a2010-08-05 17:01:07 -0400215 urb_value |= UART_LCR_PARITY;
Bill Pemberton52af9542010-07-29 11:05:41 -0400216 else
217 urb_value |= SERIAL_EVEN_PARITY;
218 }
219
220 switch (cflag & CSIZE) {
221 case CS5:
Bill Pemberton79f203a2010-08-05 17:01:07 -0400222 urb_value |= UART_LCR_WLEN5;
Bill Pemberton52af9542010-07-29 11:05:41 -0400223 break;
224 case CS6:
Bill Pemberton79f203a2010-08-05 17:01:07 -0400225 urb_value |= UART_LCR_WLEN6;
Bill Pemberton52af9542010-07-29 11:05:41 -0400226 break;
227 case CS7:
Bill Pemberton79f203a2010-08-05 17:01:07 -0400228 urb_value |= UART_LCR_WLEN7;
Bill Pemberton52af9542010-07-29 11:05:41 -0400229 break;
230 default:
231 case CS8:
Bill Pemberton79f203a2010-08-05 17:01:07 -0400232 urb_value |= UART_LCR_WLEN8;
Bill Pemberton52af9542010-07-29 11:05:41 -0400233 break;
234 }
235
236 baud = tty_get_baud_rate(tty);
237 if (!baud)
238 baud = 9600;
239
Greg Kroah-Hartman4f0c6412012-09-14 11:50:35 -0700240 dev_dbg(&port->dev, "%s - got baud = %d\n", __func__, baud);
Bill Pemberton52af9542010-07-29 11:05:41 -0400241
242
243 divisor = MAX_BAUD_RATE / baud;
244 remainder = MAX_BAUD_RATE % baud;
245 if (((remainder * 2) >= baud) && (baud != 110))
246 divisor++;
247
248 urb_value = urb_value << 8;
249
250 result = ssu100_control_msg(dev, QT_GET_SET_UART, divisor, urb_value);
251 if (result < 0)
Greg Kroah-Hartman4f0c6412012-09-14 11:50:35 -0700252 dev_dbg(&port->dev, "%s - set uart failed\n", __func__);
Bill Pemberton52af9542010-07-29 11:05:41 -0400253
254 if (cflag & CRTSCTS)
255 result = ssu100_control_msg(dev, QT_HW_FLOW_CONTROL_MASK,
256 SERIAL_CRTSCTS, 0);
257 else
258 result = ssu100_control_msg(dev, QT_HW_FLOW_CONTROL_MASK,
259 0, 0);
260 if (result < 0)
Greg Kroah-Hartman4f0c6412012-09-14 11:50:35 -0700261 dev_dbg(&port->dev, "%s - set HW flow control failed\n", __func__);
Bill Pemberton52af9542010-07-29 11:05:41 -0400262
263 if (I_IXOFF(tty) || I_IXON(tty)) {
264 u16 x = ((u16)(START_CHAR(tty) << 8) | (u16)(STOP_CHAR(tty)));
265
266 result = ssu100_control_msg(dev, QT_SW_FLOW_CONTROL_MASK,
267 x, 0);
268 } else
269 result = ssu100_control_msg(dev, QT_SW_FLOW_CONTROL_MASK,
270 0, 0);
271
272 if (result < 0)
Greg Kroah-Hartman4f0c6412012-09-14 11:50:35 -0700273 dev_dbg(&port->dev, "%s - set SW flow control failed\n", __func__);
Bill Pemberton52af9542010-07-29 11:05:41 -0400274
275}
276
277
278static int ssu100_open(struct tty_struct *tty, struct usb_serial_port *port)
279{
280 struct usb_device *dev = port->serial->dev;
281 struct ssu100_port_private *priv = usb_get_serial_port_data(port);
282 u8 *data;
283 int result;
Bill Pemberton17523052010-08-05 17:01:05 -0400284 unsigned long flags;
Bill Pemberton52af9542010-07-29 11:05:41 -0400285
Bill Pemberton52af9542010-07-29 11:05:41 -0400286 data = kzalloc(2, GFP_KERNEL);
287 if (!data)
288 return -ENOMEM;
289
290 result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
291 QT_OPEN_CLOSE_CHANNEL,
292 QT_TRANSFER_IN, 0x01,
293 0, data, 2, 300);
294 if (result < 0) {
Greg Kroah-Hartman4f0c6412012-09-14 11:50:35 -0700295 dev_dbg(&port->dev, "%s - open failed %i\n", __func__, result);
Bill Pemberton52af9542010-07-29 11:05:41 -0400296 kfree(data);
297 return result;
298 }
299
Bill Pemberton17523052010-08-05 17:01:05 -0400300 spin_lock_irqsave(&priv->status_lock, flags);
Bill Pembertonf81c83d2010-08-05 17:01:09 -0400301 priv->shadowLSR = data[0];
302 priv->shadowMSR = data[1];
Bill Pemberton17523052010-08-05 17:01:05 -0400303 spin_unlock_irqrestore(&priv->status_lock, flags);
Bill Pemberton52af9542010-07-29 11:05:41 -0400304
305 kfree(data);
306
307/* set to 9600 */
308 result = ssu100_control_msg(dev, QT_GET_SET_UART, 0x30, 0x0300);
309 if (result < 0)
Greg Kroah-Hartman4f0c6412012-09-14 11:50:35 -0700310 dev_dbg(&port->dev, "%s - set uart failed\n", __func__);
Bill Pemberton52af9542010-07-29 11:05:41 -0400311
312 if (tty)
Alan Coxadc8d742012-07-14 15:31:47 +0100313 ssu100_set_termios(tty, port, &tty->termios);
Bill Pemberton52af9542010-07-29 11:05:41 -0400314
315 return usb_serial_generic_open(tty, port);
316}
317
318static void ssu100_close(struct usb_serial_port *port)
319{
Bill Pemberton52af9542010-07-29 11:05:41 -0400320 usb_serial_generic_close(port);
321}
322
323static int get_serial_info(struct usb_serial_port *port,
324 struct serial_struct __user *retinfo)
325{
326 struct serial_struct tmp;
327
328 if (!retinfo)
329 return -EFAULT;
330
331 memset(&tmp, 0, sizeof(tmp));
332 tmp.line = port->serial->minor;
333 tmp.port = 0;
334 tmp.irq = 0;
335 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
336 tmp.xmit_fifo_size = port->bulk_out_size;
337 tmp.baud_base = 9600;
338 tmp.close_delay = 5*HZ;
339 tmp.closing_wait = 30*HZ;
340
341 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
342 return -EFAULT;
343 return 0;
344}
345
Bill Pembertonf81c83d2010-08-05 17:01:09 -0400346static int wait_modem_info(struct usb_serial_port *port, unsigned int arg)
347{
348 struct ssu100_port_private *priv = usb_get_serial_port_data(port);
349 struct async_icount prev, cur;
350 unsigned long flags;
351
352 spin_lock_irqsave(&priv->status_lock, flags);
353 prev = priv->icount;
354 spin_unlock_irqrestore(&priv->status_lock, flags);
355
356 while (1) {
Johan Hovold43a66b42013-03-19 09:21:25 +0100357 wait_event_interruptible(port->delta_msr_wait,
358 (port->serial->disconnected ||
359 (priv->icount.rng != prev.rng) ||
Bill Pembertonf81c83d2010-08-05 17:01:09 -0400360 (priv->icount.dsr != prev.dsr) ||
361 (priv->icount.dcd != prev.dcd) ||
362 (priv->icount.cts != prev.cts)));
363
364 if (signal_pending(current))
365 return -ERESTARTSYS;
366
Johan Hovold43a66b42013-03-19 09:21:25 +0100367 if (port->serial->disconnected)
368 return -EIO;
369
Bill Pembertonf81c83d2010-08-05 17:01:09 -0400370 spin_lock_irqsave(&priv->status_lock, flags);
371 cur = priv->icount;
372 spin_unlock_irqrestore(&priv->status_lock, flags);
373
374 if ((prev.rng == cur.rng) &&
375 (prev.dsr == cur.dsr) &&
376 (prev.dcd == cur.dcd) &&
377 (prev.cts == cur.cts))
378 return -EIO;
379
380 if ((arg & TIOCM_RNG && (prev.rng != cur.rng)) ||
381 (arg & TIOCM_DSR && (prev.dsr != cur.dsr)) ||
382 (arg & TIOCM_CD && (prev.dcd != cur.dcd)) ||
383 (arg & TIOCM_CTS && (prev.cts != cur.cts)))
384 return 0;
385 }
386 return 0;
387}
388
Alan Cox0bca1b92010-09-16 18:21:40 +0100389static int ssu100_get_icount(struct tty_struct *tty,
390 struct serial_icounter_struct *icount)
391{
392 struct usb_serial_port *port = tty->driver_data;
393 struct ssu100_port_private *priv = usb_get_serial_port_data(port);
394 struct async_icount cnow = priv->icount;
395
396 icount->cts = cnow.cts;
397 icount->dsr = cnow.dsr;
398 icount->rng = cnow.rng;
399 icount->dcd = cnow.dcd;
400 icount->rx = cnow.rx;
401 icount->tx = cnow.tx;
402 icount->frame = cnow.frame;
403 icount->overrun = cnow.overrun;
404 icount->parity = cnow.parity;
405 icount->brk = cnow.brk;
406 icount->buf_overrun = cnow.buf_overrun;
407
408 return 0;
409}
410
411
412
Alan Cox00a0d0d2011-02-14 16:27:06 +0000413static int ssu100_ioctl(struct tty_struct *tty,
Bill Pemberton52af9542010-07-29 11:05:41 -0400414 unsigned int cmd, unsigned long arg)
415{
416 struct usb_serial_port *port = tty->driver_data;
Bill Pemberton52af9542010-07-29 11:05:41 -0400417
Greg Kroah-Hartman4f0c6412012-09-14 11:50:35 -0700418 dev_dbg(&port->dev, "%s cmd 0x%04x\n", __func__, cmd);
Bill Pemberton52af9542010-07-29 11:05:41 -0400419
420 switch (cmd) {
421 case TIOCGSERIAL:
422 return get_serial_info(port,
423 (struct serial_struct __user *) arg);
424
425 case TIOCMIWAIT:
Bill Pembertonf81c83d2010-08-05 17:01:09 -0400426 return wait_modem_info(port, arg);
Bill Pemberton52af9542010-07-29 11:05:41 -0400427
Bill Pemberton52af9542010-07-29 11:05:41 -0400428 default:
429 break;
430 }
431
Greg Kroah-Hartman4f0c6412012-09-14 11:50:35 -0700432 dev_dbg(&port->dev, "%s arg not supported\n", __func__);
Bill Pemberton52af9542010-07-29 11:05:41 -0400433
434 return -ENOIOCTLCMD;
435}
436
Bill Pemberton52af9542010-07-29 11:05:41 -0400437static int ssu100_attach(struct usb_serial *serial)
438{
Johan Hovold638b9e12012-10-17 16:31:34 +0200439 return ssu100_initdevice(serial->dev);
440}
441
442static int ssu100_port_probe(struct usb_serial_port *port)
443{
Bill Pemberton52af9542010-07-29 11:05:41 -0400444 struct ssu100_port_private *priv;
Bill Pemberton52af9542010-07-29 11:05:41 -0400445
Bill Pemberton52af9542010-07-29 11:05:41 -0400446 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
Johan Hovold638b9e12012-10-17 16:31:34 +0200447 if (!priv)
Bill Pemberton52af9542010-07-29 11:05:41 -0400448 return -ENOMEM;
Bill Pemberton52af9542010-07-29 11:05:41 -0400449
Bill Pemberton17523052010-08-05 17:01:05 -0400450 spin_lock_init(&priv->status_lock);
Johan Hovold638b9e12012-10-17 16:31:34 +0200451
Bill Pemberton52af9542010-07-29 11:05:41 -0400452 usb_set_serial_port_data(port, priv);
Bill Pemberton52af9542010-07-29 11:05:41 -0400453
Johan Hovold638b9e12012-10-17 16:31:34 +0200454 return 0;
455}
456
457static int ssu100_port_remove(struct usb_serial_port *port)
458{
459 struct ssu100_port_private *priv;
460
461 priv = usb_get_serial_port_data(port);
462 kfree(priv);
463
464 return 0;
Bill Pemberton52af9542010-07-29 11:05:41 -0400465}
466
Alan Cox60b33c12011-02-14 16:26:14 +0000467static int ssu100_tiocmget(struct tty_struct *tty)
Bill Pemberton52af9542010-07-29 11:05:41 -0400468{
469 struct usb_serial_port *port = tty->driver_data;
470 struct usb_device *dev = port->serial->dev;
471 u8 *d;
472 int r;
473
Bill Pemberton52af9542010-07-29 11:05:41 -0400474 d = kzalloc(2, GFP_KERNEL);
475 if (!d)
476 return -ENOMEM;
477
Bill Pemberton79f203a2010-08-05 17:01:07 -0400478 r = ssu100_getregister(dev, 0, UART_MCR, d);
Bill Pemberton52af9542010-07-29 11:05:41 -0400479 if (r < 0)
480 goto mget_out;
481
Bill Pemberton79f203a2010-08-05 17:01:07 -0400482 r = ssu100_getregister(dev, 0, UART_MSR, d+1);
Bill Pemberton52af9542010-07-29 11:05:41 -0400483 if (r < 0)
484 goto mget_out;
485
Bill Pemberton79f203a2010-08-05 17:01:07 -0400486 r = (d[0] & UART_MCR_DTR ? TIOCM_DTR : 0) |
487 (d[0] & UART_MCR_RTS ? TIOCM_RTS : 0) |
488 (d[1] & UART_MSR_CTS ? TIOCM_CTS : 0) |
489 (d[1] & UART_MSR_DCD ? TIOCM_CAR : 0) |
490 (d[1] & UART_MSR_RI ? TIOCM_RI : 0) |
491 (d[1] & UART_MSR_DSR ? TIOCM_DSR : 0);
Bill Pemberton52af9542010-07-29 11:05:41 -0400492
493mget_out:
494 kfree(d);
495 return r;
496}
497
Alan Cox20b9d172011-02-14 16:26:50 +0000498static int ssu100_tiocmset(struct tty_struct *tty,
Bill Pemberton52af9542010-07-29 11:05:41 -0400499 unsigned int set, unsigned int clear)
500{
501 struct usb_serial_port *port = tty->driver_data;
502 struct usb_device *dev = port->serial->dev;
503
Bill Pemberton52af9542010-07-29 11:05:41 -0400504 return update_mctrl(dev, set, clear);
505}
506
507static void ssu100_dtr_rts(struct usb_serial_port *port, int on)
508{
509 struct usb_device *dev = port->serial->dev;
510
Johan Hovoldb2ca6992013-02-13 17:53:28 +0100511 /* Disable flow control */
512 if (!on) {
513 if (ssu100_setregister(dev, 0, UART_MCR, 0) < 0)
Bill Pemberton52af9542010-07-29 11:05:41 -0400514 dev_err(&port->dev, "error from flowcontrol urb\n");
Bill Pemberton52af9542010-07-29 11:05:41 -0400515 }
Johan Hovoldb2ca6992013-02-13 17:53:28 +0100516 /* drop RTS and DTR */
517 if (on)
518 set_mctrl(dev, TIOCM_DTR | TIOCM_RTS);
519 else
520 clear_mctrl(dev, TIOCM_DTR | TIOCM_RTS);
Bill Pemberton52af9542010-07-29 11:05:41 -0400521}
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++;
Johan Hovold43a66b42013-03-19 09:21:25 +0100542 wake_up_interruptible(&port->delta_msr_wait);
Bill Pembertonf81c83d2010-08-05 17:01:09 -0400543 }
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
Jiri Slaby2e124b42013-01-03 15:53:06 +0100584static void ssu100_process_read_urb(struct urb *urb)
Bill Pemberton52af9542010-07-29 11:05:41 -0400585{
Bill Pembertonf7043ec2010-10-21 14:43:05 -0400586 struct usb_serial_port *port = urb->context;
587 char *packet = (char *)urb->transfer_buffer;
Bill Pemberton6b8f1ca2010-08-13 09:59:31 -0400588 char flag = TTY_NORMAL;
Bill Pembertonf7043ec2010-10-21 14:43:05 -0400589 u32 len = urb->actual_length;
590 int i;
Bill Pemberton52af9542010-07-29 11:05:41 -0400591 char *ch;
592
Bill Pemberton9b2cef32010-08-05 17:01:06 -0400593 if ((len >= 4) &&
594 (packet[0] == 0x1b) && (packet[1] == 0x1b) &&
Bill Pemberton52af9542010-07-29 11:05:41 -0400595 ((packet[2] == 0x00) || (packet[2] == 0x01))) {
Bill Pemberton6b8f1ca2010-08-13 09:59:31 -0400596 if (packet[2] == 0x00) {
597 ssu100_update_lsr(port, packet[3], &flag);
598 if (flag == TTY_OVERRUN)
Jiri Slaby92a19f92013-01-03 15:53:03 +0100599 tty_insert_flip_char(&port->port, 0,
600 TTY_OVERRUN);
Bill Pemberton6b8f1ca2010-08-13 09:59:31 -0400601 }
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)
Jiri Slaby2e124b42013-01-03 15:53:06 +0100611 return; /* status only */
Bill Pemberton52af9542010-07-29 11:05:41 -0400612
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))
Jiri Slaby92a19f92013-01-03 15:53:03 +0100616 tty_insert_flip_char(&port->port, *ch, flag);
Bill Pemberton52af9542010-07-29 11:05:41 -0400617 }
618 } else
Jiri Slaby2f693352013-01-03 15:53:02 +0100619 tty_insert_flip_string_fixed_flag(&port->port, ch, flag, len);
Bill Pemberton52af9542010-07-29 11:05:41 -0400620
Jiri Slaby2e124b42013-01-03 15:53:06 +0100621 tty_flip_buffer_push(&port->port);
Bill Pemberton52af9542010-07-29 11:05:41 -0400622}
623
Bill Pemberton52af9542010-07-29 11:05:41 -0400624static struct usb_serial_driver ssu100_device = {
625 .driver = {
626 .owner = THIS_MODULE,
627 .name = "ssu100",
628 },
629 .description = DRIVER_DESC,
630 .id_table = id_table,
Bill Pemberton52af9542010-07-29 11:05:41 -0400631 .num_ports = 1,
Bill Pemberton52af9542010-07-29 11:05:41 -0400632 .open = ssu100_open,
633 .close = ssu100_close,
634 .attach = ssu100_attach,
Johan Hovold638b9e12012-10-17 16:31:34 +0200635 .port_probe = ssu100_port_probe,
636 .port_remove = ssu100_port_remove,
Bill Pemberton52af9542010-07-29 11:05:41 -0400637 .dtr_rts = ssu100_dtr_rts,
638 .process_read_urb = ssu100_process_read_urb,
639 .tiocmget = ssu100_tiocmget,
640 .tiocmset = ssu100_tiocmset,
Alan Cox0bca1b92010-09-16 18:21:40 +0100641 .get_icount = ssu100_get_icount,
Bill Pemberton52af9542010-07-29 11:05:41 -0400642 .ioctl = ssu100_ioctl,
643 .set_termios = ssu100_set_termios,
Bill Pemberton85dee132010-08-05 17:01:11 -0400644 .disconnect = usb_serial_generic_disconnect,
Bill Pemberton52af9542010-07-29 11:05:41 -0400645};
646
Alan Sternd8603222012-02-23 14:57:25 -0500647static struct usb_serial_driver * const serial_drivers[] = {
648 &ssu100_device, NULL
649};
650
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -0700651module_usb_serial_driver(serial_drivers, id_table);
Bill Pemberton52af9542010-07-29 11:05:41 -0400652
653MODULE_DESCRIPTION(DRIVER_DESC);
654MODULE_LICENSE("GPL");