blob: 868d1e6852e2ab6086a354cd62a352a241cb51c7 [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
Bill Pemberton52af9542010-07-29 11:05:41 -040070static inline int ssu100_control_msg(struct usb_device *dev,
71 u8 request, u16 data, u16 index)
72{
73 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
74 request, 0x40, data, index,
75 NULL, 0, 300);
76}
77
78static inline int ssu100_setdevice(struct usb_device *dev, u8 *data)
79{
80 u16 x = ((u16)(data[1] << 8) | (u16)(data[0]));
81
82 return ssu100_control_msg(dev, QT_SET_GET_DEVICE, x, 0);
83}
84
85
86static inline int ssu100_getdevice(struct usb_device *dev, u8 *data)
87{
88 return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
89 QT_SET_GET_DEVICE, 0xc0, 0, 0,
90 data, 3, 300);
91}
92
93static inline int ssu100_getregister(struct usb_device *dev,
94 unsigned short uart,
95 unsigned short reg,
96 u8 *data)
97{
98 return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
99 QT_SET_GET_REGISTER, 0xc0, reg,
100 uart, data, sizeof(*data), 300);
101
102}
103
104
105static inline int ssu100_setregister(struct usb_device *dev,
106 unsigned short uart,
Bill Pemberton556f1a02010-08-05 17:01:08 -0400107 unsigned short reg,
Bill Pemberton52af9542010-07-29 11:05:41 -0400108 u16 data)
109{
Bill Pemberton556f1a02010-08-05 17:01:08 -0400110 u16 value = (data << 8) | reg;
Bill Pemberton52af9542010-07-29 11:05:41 -0400111
112 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
113 QT_SET_GET_REGISTER, 0x40, value, uart,
114 NULL, 0, 300);
115
116}
117
118#define set_mctrl(dev, set) update_mctrl((dev), (set), 0)
119#define clear_mctrl(dev, clear) update_mctrl((dev), 0, (clear))
120
121/* these do not deal with device that have more than 1 port */
122static inline int update_mctrl(struct usb_device *dev, unsigned int set,
123 unsigned int clear)
124{
125 unsigned urb_value;
126 int result;
127
128 if (((set | clear) & (TIOCM_DTR | TIOCM_RTS)) == 0) {
Greg Kroah-Hartman4f0c6412012-09-14 11:50:35 -0700129 dev_dbg(&dev->dev, "%s - DTR|RTS not being set|cleared\n", __func__);
Bill Pemberton52af9542010-07-29 11:05:41 -0400130 return 0; /* no change */
131 }
132
133 clear &= ~set; /* 'set' takes precedence over 'clear' */
134 urb_value = 0;
135 if (set & TIOCM_DTR)
Bill Pemberton79f203a2010-08-05 17:01:07 -0400136 urb_value |= UART_MCR_DTR;
Bill Pemberton52af9542010-07-29 11:05:41 -0400137 if (set & TIOCM_RTS)
Bill Pemberton79f203a2010-08-05 17:01:07 -0400138 urb_value |= UART_MCR_RTS;
Bill Pemberton52af9542010-07-29 11:05:41 -0400139
Bill Pemberton556f1a02010-08-05 17:01:08 -0400140 result = ssu100_setregister(dev, 0, UART_MCR, urb_value);
Bill Pemberton52af9542010-07-29 11:05:41 -0400141 if (result < 0)
Greg Kroah-Hartman4f0c6412012-09-14 11:50:35 -0700142 dev_dbg(&dev->dev, "%s Error from MODEM_CTRL urb\n", __func__);
Bill Pemberton52af9542010-07-29 11:05:41 -0400143
144 return result;
145}
146
147static int ssu100_initdevice(struct usb_device *dev)
148{
149 u8 *data;
150 int result = 0;
151
Bill Pemberton52af9542010-07-29 11:05:41 -0400152 data = kzalloc(3, GFP_KERNEL);
153 if (!data)
154 return -ENOMEM;
155
156 result = ssu100_getdevice(dev, data);
157 if (result < 0) {
Greg Kroah-Hartman4f0c6412012-09-14 11:50:35 -0700158 dev_dbg(&dev->dev, "%s - get_device failed %i\n", __func__, result);
Bill Pemberton52af9542010-07-29 11:05:41 -0400159 goto out;
160 }
161
162 data[1] &= ~FULLPWRBIT;
163
164 result = ssu100_setdevice(dev, data);
165 if (result < 0) {
Greg Kroah-Hartman4f0c6412012-09-14 11:50:35 -0700166 dev_dbg(&dev->dev, "%s - setdevice failed %i\n", __func__, result);
Bill Pemberton52af9542010-07-29 11:05:41 -0400167 goto out;
168 }
169
170 result = ssu100_control_msg(dev, QT_GET_SET_PREBUF_TRIG_LVL, 128, 0);
171 if (result < 0) {
Greg Kroah-Hartman4f0c6412012-09-14 11:50:35 -0700172 dev_dbg(&dev->dev, "%s - set prebuffer level failed %i\n", __func__, result);
Bill Pemberton52af9542010-07-29 11:05:41 -0400173 goto out;
174 }
175
176 result = ssu100_control_msg(dev, QT_SET_ATF, ATC_DISABLED, 0);
177 if (result < 0) {
Greg Kroah-Hartman4f0c6412012-09-14 11:50:35 -0700178 dev_dbg(&dev->dev, "%s - set ATFprebuffer level failed %i\n", __func__, result);
Bill Pemberton52af9542010-07-29 11:05:41 -0400179 goto out;
180 }
181
182 result = ssu100_getdevice(dev, data);
183 if (result < 0) {
Greg Kroah-Hartman4f0c6412012-09-14 11:50:35 -0700184 dev_dbg(&dev->dev, "%s - get_device failed %i\n", __func__, result);
Bill Pemberton52af9542010-07-29 11:05:41 -0400185 goto out;
186 }
187
188 data[0] &= ~(RR_BITS | DUPMODE_BITS);
189 data[0] |= CLKS_X4;
190 data[1] &= ~(LOOPMODE_BITS);
191 data[1] |= RS232_MODE;
192
193 result = ssu100_setdevice(dev, data);
194 if (result < 0) {
Greg Kroah-Hartman4f0c6412012-09-14 11:50:35 -0700195 dev_dbg(&dev->dev, "%s - setdevice failed %i\n", __func__, result);
Bill Pemberton52af9542010-07-29 11:05:41 -0400196 goto out;
197 }
198
199out: kfree(data);
200 return result;
201
202}
203
204
205static void ssu100_set_termios(struct tty_struct *tty,
206 struct usb_serial_port *port,
207 struct ktermios *old_termios)
208{
209 struct usb_device *dev = port->serial->dev;
Alan Coxadc8d742012-07-14 15:31:47 +0100210 struct ktermios *termios = &tty->termios;
Bill Pemberton52af9542010-07-29 11:05:41 -0400211 u16 baud, divisor, remainder;
212 unsigned int cflag = termios->c_cflag;
213 u16 urb_value = 0; /* will hold the new flags */
214 int result;
215
Bill Pemberton52af9542010-07-29 11:05:41 -0400216 if (cflag & PARENB) {
217 if (cflag & PARODD)
Bill Pemberton79f203a2010-08-05 17:01:07 -0400218 urb_value |= UART_LCR_PARITY;
Bill Pemberton52af9542010-07-29 11:05:41 -0400219 else
220 urb_value |= SERIAL_EVEN_PARITY;
221 }
222
223 switch (cflag & CSIZE) {
224 case CS5:
Bill Pemberton79f203a2010-08-05 17:01:07 -0400225 urb_value |= UART_LCR_WLEN5;
Bill Pemberton52af9542010-07-29 11:05:41 -0400226 break;
227 case CS6:
Bill Pemberton79f203a2010-08-05 17:01:07 -0400228 urb_value |= UART_LCR_WLEN6;
Bill Pemberton52af9542010-07-29 11:05:41 -0400229 break;
230 case CS7:
Bill Pemberton79f203a2010-08-05 17:01:07 -0400231 urb_value |= UART_LCR_WLEN7;
Bill Pemberton52af9542010-07-29 11:05:41 -0400232 break;
233 default:
234 case CS8:
Bill Pemberton79f203a2010-08-05 17:01:07 -0400235 urb_value |= UART_LCR_WLEN8;
Bill Pemberton52af9542010-07-29 11:05:41 -0400236 break;
237 }
238
239 baud = tty_get_baud_rate(tty);
240 if (!baud)
241 baud = 9600;
242
Greg Kroah-Hartman4f0c6412012-09-14 11:50:35 -0700243 dev_dbg(&port->dev, "%s - got baud = %d\n", __func__, baud);
Bill Pemberton52af9542010-07-29 11:05:41 -0400244
245
246 divisor = MAX_BAUD_RATE / baud;
247 remainder = MAX_BAUD_RATE % baud;
248 if (((remainder * 2) >= baud) && (baud != 110))
249 divisor++;
250
251 urb_value = urb_value << 8;
252
253 result = ssu100_control_msg(dev, QT_GET_SET_UART, divisor, urb_value);
254 if (result < 0)
Greg Kroah-Hartman4f0c6412012-09-14 11:50:35 -0700255 dev_dbg(&port->dev, "%s - set uart failed\n", __func__);
Bill Pemberton52af9542010-07-29 11:05:41 -0400256
257 if (cflag & CRTSCTS)
258 result = ssu100_control_msg(dev, QT_HW_FLOW_CONTROL_MASK,
259 SERIAL_CRTSCTS, 0);
260 else
261 result = ssu100_control_msg(dev, QT_HW_FLOW_CONTROL_MASK,
262 0, 0);
263 if (result < 0)
Greg Kroah-Hartman4f0c6412012-09-14 11:50:35 -0700264 dev_dbg(&port->dev, "%s - set HW flow control failed\n", __func__);
Bill Pemberton52af9542010-07-29 11:05:41 -0400265
266 if (I_IXOFF(tty) || I_IXON(tty)) {
267 u16 x = ((u16)(START_CHAR(tty) << 8) | (u16)(STOP_CHAR(tty)));
268
269 result = ssu100_control_msg(dev, QT_SW_FLOW_CONTROL_MASK,
270 x, 0);
271 } else
272 result = ssu100_control_msg(dev, QT_SW_FLOW_CONTROL_MASK,
273 0, 0);
274
275 if (result < 0)
Greg Kroah-Hartman4f0c6412012-09-14 11:50:35 -0700276 dev_dbg(&port->dev, "%s - set SW flow control failed\n", __func__);
Bill Pemberton52af9542010-07-29 11:05:41 -0400277
278}
279
280
281static int ssu100_open(struct tty_struct *tty, struct usb_serial_port *port)
282{
283 struct usb_device *dev = port->serial->dev;
284 struct ssu100_port_private *priv = usb_get_serial_port_data(port);
285 u8 *data;
286 int result;
Bill Pemberton17523052010-08-05 17:01:05 -0400287 unsigned long flags;
Bill Pemberton52af9542010-07-29 11:05:41 -0400288
Bill Pemberton52af9542010-07-29 11:05:41 -0400289 data = kzalloc(2, GFP_KERNEL);
290 if (!data)
291 return -ENOMEM;
292
293 result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
294 QT_OPEN_CLOSE_CHANNEL,
295 QT_TRANSFER_IN, 0x01,
296 0, data, 2, 300);
297 if (result < 0) {
Greg Kroah-Hartman4f0c6412012-09-14 11:50:35 -0700298 dev_dbg(&port->dev, "%s - open failed %i\n", __func__, result);
Bill Pemberton52af9542010-07-29 11:05:41 -0400299 kfree(data);
300 return result;
301 }
302
Bill Pemberton17523052010-08-05 17:01:05 -0400303 spin_lock_irqsave(&priv->status_lock, flags);
Bill Pembertonf81c83d2010-08-05 17:01:09 -0400304 priv->shadowLSR = data[0];
305 priv->shadowMSR = data[1];
Bill Pemberton17523052010-08-05 17:01:05 -0400306 spin_unlock_irqrestore(&priv->status_lock, flags);
Bill Pemberton52af9542010-07-29 11:05:41 -0400307
308 kfree(data);
309
310/* set to 9600 */
311 result = ssu100_control_msg(dev, QT_GET_SET_UART, 0x30, 0x0300);
312 if (result < 0)
Greg Kroah-Hartman4f0c6412012-09-14 11:50:35 -0700313 dev_dbg(&port->dev, "%s - set uart failed\n", __func__);
Bill Pemberton52af9542010-07-29 11:05:41 -0400314
315 if (tty)
Alan Coxadc8d742012-07-14 15:31:47 +0100316 ssu100_set_termios(tty, port, &tty->termios);
Bill Pemberton52af9542010-07-29 11:05:41 -0400317
318 return usb_serial_generic_open(tty, port);
319}
320
321static void ssu100_close(struct usb_serial_port *port)
322{
Bill Pemberton52af9542010-07-29 11:05:41 -0400323 usb_serial_generic_close(port);
324}
325
326static int get_serial_info(struct usb_serial_port *port,
327 struct serial_struct __user *retinfo)
328{
329 struct serial_struct tmp;
330
331 if (!retinfo)
332 return -EFAULT;
333
334 memset(&tmp, 0, sizeof(tmp));
335 tmp.line = port->serial->minor;
336 tmp.port = 0;
337 tmp.irq = 0;
338 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
339 tmp.xmit_fifo_size = port->bulk_out_size;
340 tmp.baud_base = 9600;
341 tmp.close_delay = 5*HZ;
342 tmp.closing_wait = 30*HZ;
343
344 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
345 return -EFAULT;
346 return 0;
347}
348
Bill Pembertonf81c83d2010-08-05 17:01:09 -0400349static int wait_modem_info(struct usb_serial_port *port, unsigned int arg)
350{
351 struct ssu100_port_private *priv = usb_get_serial_port_data(port);
352 struct async_icount prev, cur;
353 unsigned long flags;
354
355 spin_lock_irqsave(&priv->status_lock, flags);
356 prev = priv->icount;
357 spin_unlock_irqrestore(&priv->status_lock, flags);
358
359 while (1) {
360 wait_event_interruptible(priv->delta_msr_wait,
361 ((priv->icount.rng != prev.rng) ||
362 (priv->icount.dsr != prev.dsr) ||
363 (priv->icount.dcd != prev.dcd) ||
364 (priv->icount.cts != prev.cts)));
365
366 if (signal_pending(current))
367 return -ERESTARTSYS;
368
369 spin_lock_irqsave(&priv->status_lock, flags);
370 cur = priv->icount;
371 spin_unlock_irqrestore(&priv->status_lock, flags);
372
373 if ((prev.rng == cur.rng) &&
374 (prev.dsr == cur.dsr) &&
375 (prev.dcd == cur.dcd) &&
376 (prev.cts == cur.cts))
377 return -EIO;
378
379 if ((arg & TIOCM_RNG && (prev.rng != cur.rng)) ||
380 (arg & TIOCM_DSR && (prev.dsr != cur.dsr)) ||
381 (arg & TIOCM_CD && (prev.dcd != cur.dcd)) ||
382 (arg & TIOCM_CTS && (prev.cts != cur.cts)))
383 return 0;
384 }
385 return 0;
386}
387
Alan Cox0bca1b92010-09-16 18:21:40 +0100388static int ssu100_get_icount(struct tty_struct *tty,
389 struct serial_icounter_struct *icount)
390{
391 struct usb_serial_port *port = tty->driver_data;
392 struct ssu100_port_private *priv = usb_get_serial_port_data(port);
393 struct async_icount cnow = priv->icount;
394
395 icount->cts = cnow.cts;
396 icount->dsr = cnow.dsr;
397 icount->rng = cnow.rng;
398 icount->dcd = cnow.dcd;
399 icount->rx = cnow.rx;
400 icount->tx = cnow.tx;
401 icount->frame = cnow.frame;
402 icount->overrun = cnow.overrun;
403 icount->parity = cnow.parity;
404 icount->brk = cnow.brk;
405 icount->buf_overrun = cnow.buf_overrun;
406
407 return 0;
408}
409
410
411
Alan Cox00a0d0d2011-02-14 16:27:06 +0000412static int ssu100_ioctl(struct tty_struct *tty,
Bill Pemberton52af9542010-07-29 11:05:41 -0400413 unsigned int cmd, unsigned long arg)
414{
415 struct usb_serial_port *port = tty->driver_data;
Bill Pemberton52af9542010-07-29 11:05:41 -0400416
Greg Kroah-Hartman4f0c6412012-09-14 11:50:35 -0700417 dev_dbg(&port->dev, "%s cmd 0x%04x\n", __func__, cmd);
Bill Pemberton52af9542010-07-29 11:05:41 -0400418
419 switch (cmd) {
420 case TIOCGSERIAL:
421 return get_serial_info(port,
422 (struct serial_struct __user *) arg);
423
424 case TIOCMIWAIT:
Bill Pembertonf81c83d2010-08-05 17:01:09 -0400425 return wait_modem_info(port, arg);
Bill Pemberton52af9542010-07-29 11:05:41 -0400426
Bill Pemberton52af9542010-07-29 11:05:41 -0400427 default:
428 break;
429 }
430
Greg Kroah-Hartman4f0c6412012-09-14 11:50:35 -0700431 dev_dbg(&port->dev, "%s arg not supported\n", __func__);
Bill Pemberton52af9542010-07-29 11:05:41 -0400432
433 return -ENOIOCTLCMD;
434}
435
Bill Pemberton52af9542010-07-29 11:05:41 -0400436static int ssu100_attach(struct usb_serial *serial)
437{
Johan Hovold638b9e12012-10-17 16:31:34 +0200438 return ssu100_initdevice(serial->dev);
439}
440
441static int ssu100_port_probe(struct usb_serial_port *port)
442{
Bill Pemberton52af9542010-07-29 11:05:41 -0400443 struct ssu100_port_private *priv;
Bill Pemberton52af9542010-07-29 11:05:41 -0400444
Bill Pemberton52af9542010-07-29 11:05:41 -0400445 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
Johan Hovold638b9e12012-10-17 16:31:34 +0200446 if (!priv)
Bill Pemberton52af9542010-07-29 11:05:41 -0400447 return -ENOMEM;
Bill Pemberton52af9542010-07-29 11:05:41 -0400448
Bill Pemberton17523052010-08-05 17:01:05 -0400449 spin_lock_init(&priv->status_lock);
Bill Pemberton52af9542010-07-29 11:05:41 -0400450 init_waitqueue_head(&priv->delta_msr_wait);
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
Bill Pemberton52af9542010-07-29 11:05:41 -0400511 mutex_lock(&port->serial->disc_mutex);
512 if (!port->serial->disconnected) {
513 /* Disable flow control */
514 if (!on &&
Bill Pemberton556f1a02010-08-05 17:01:08 -0400515 ssu100_setregister(dev, 0, UART_MCR, 0) < 0)
Bill Pemberton52af9542010-07-29 11:05:41 -0400516 dev_err(&port->dev, "error from flowcontrol urb\n");
517 /* drop RTS and DTR */
518 if (on)
519 set_mctrl(dev, TIOCM_DTR | TIOCM_RTS);
520 else
521 clear_mctrl(dev, TIOCM_DTR | TIOCM_RTS);
522 }
523 mutex_unlock(&port->serial->disc_mutex);
524}
525
Bill Pembertonf81c83d2010-08-05 17:01:09 -0400526static void ssu100_update_msr(struct usb_serial_port *port, u8 msr)
527{
528 struct ssu100_port_private *priv = usb_get_serial_port_data(port);
529 unsigned long flags;
530
531 spin_lock_irqsave(&priv->status_lock, flags);
532 priv->shadowMSR = msr;
533 spin_unlock_irqrestore(&priv->status_lock, flags);
534
535 if (msr & UART_MSR_ANY_DELTA) {
536 /* update input line counters */
537 if (msr & UART_MSR_DCTS)
538 priv->icount.cts++;
539 if (msr & UART_MSR_DDSR)
540 priv->icount.dsr++;
541 if (msr & UART_MSR_DDCD)
542 priv->icount.dcd++;
543 if (msr & UART_MSR_TERI)
544 priv->icount.rng++;
545 wake_up_interruptible(&priv->delta_msr_wait);
546 }
547}
548
Bill Pemberton6b8f1ca2010-08-13 09:59:31 -0400549static void ssu100_update_lsr(struct usb_serial_port *port, u8 lsr,
550 char *tty_flag)
Bill Pembertonf81c83d2010-08-05 17:01:09 -0400551{
552 struct ssu100_port_private *priv = usb_get_serial_port_data(port);
553 unsigned long flags;
554
555 spin_lock_irqsave(&priv->status_lock, flags);
556 priv->shadowLSR = lsr;
557 spin_unlock_irqrestore(&priv->status_lock, flags);
558
Bill Pemberton6b8f1ca2010-08-13 09:59:31 -0400559 *tty_flag = TTY_NORMAL;
Bill Pembertonf81c83d2010-08-05 17:01:09 -0400560 if (lsr & UART_LSR_BRK_ERROR_BITS) {
Bill Pemberton6b8f1ca2010-08-13 09:59:31 -0400561 /* we always want to update icount, but we only want to
562 * update tty_flag for one case */
563 if (lsr & UART_LSR_BI) {
Bill Pembertonf81c83d2010-08-05 17:01:09 -0400564 priv->icount.brk++;
Bill Pemberton6b8f1ca2010-08-13 09:59:31 -0400565 *tty_flag = TTY_BREAK;
566 usb_serial_handle_break(port);
567 }
568 if (lsr & UART_LSR_PE) {
Bill Pembertonf81c83d2010-08-05 17:01:09 -0400569 priv->icount.parity++;
Bill Pemberton6b8f1ca2010-08-13 09:59:31 -0400570 if (*tty_flag == TTY_NORMAL)
571 *tty_flag = TTY_PARITY;
572 }
573 if (lsr & UART_LSR_FE) {
574 priv->icount.frame++;
575 if (*tty_flag == TTY_NORMAL)
576 *tty_flag = TTY_FRAME;
577 }
578 if (lsr & UART_LSR_OE){
Bill Pembertonf81c83d2010-08-05 17:01:09 -0400579 priv->icount.overrun++;
Bill Pemberton6b8f1ca2010-08-13 09:59:31 -0400580 if (*tty_flag == TTY_NORMAL)
581 *tty_flag = TTY_OVERRUN;
582 }
Bill Pembertonf81c83d2010-08-05 17:01:09 -0400583 }
Bill Pemberton6b8f1ca2010-08-13 09:59:31 -0400584
Bill Pembertonf81c83d2010-08-05 17:01:09 -0400585}
586
Bill Pembertonf7043ec2010-10-21 14:43:05 -0400587static int ssu100_process_packet(struct urb *urb,
588 struct tty_struct *tty)
Bill Pemberton52af9542010-07-29 11:05:41 -0400589{
Bill Pembertonf7043ec2010-10-21 14:43:05 -0400590 struct usb_serial_port *port = urb->context;
591 char *packet = (char *)urb->transfer_buffer;
Bill Pemberton6b8f1ca2010-08-13 09:59:31 -0400592 char flag = TTY_NORMAL;
Bill Pembertonf7043ec2010-10-21 14:43:05 -0400593 u32 len = urb->actual_length;
594 int i;
Bill Pemberton52af9542010-07-29 11:05:41 -0400595 char *ch;
596
Bill Pemberton9b2cef32010-08-05 17:01:06 -0400597 if ((len >= 4) &&
598 (packet[0] == 0x1b) && (packet[1] == 0x1b) &&
Bill Pemberton52af9542010-07-29 11:05:41 -0400599 ((packet[2] == 0x00) || (packet[2] == 0x01))) {
Bill Pemberton6b8f1ca2010-08-13 09:59:31 -0400600 if (packet[2] == 0x00) {
601 ssu100_update_lsr(port, packet[3], &flag);
602 if (flag == TTY_OVERRUN)
603 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
604 }
Bill Pembertonf81c83d2010-08-05 17:01:09 -0400605 if (packet[2] == 0x01)
606 ssu100_update_msr(port, packet[3]);
Bill Pemberton52af9542010-07-29 11:05:41 -0400607
608 len -= 4;
609 ch = packet + 4;
610 } else
611 ch = packet;
612
613 if (!len)
614 return 0; /* status only */
615
616 if (port->port.console && port->sysrq) {
617 for (i = 0; i < len; i++, ch++) {
Dmitry Torokhov6ee9f4b2010-08-17 21:15:47 -0700618 if (!usb_serial_handle_sysrq_char(port, *ch))
Bill Pemberton52af9542010-07-29 11:05:41 -0400619 tty_insert_flip_char(tty, *ch, flag);
620 }
621 } else
622 tty_insert_flip_string_fixed_flag(tty, ch, flag, len);
623
624 return len;
625}
626
627static void ssu100_process_read_urb(struct urb *urb)
628{
629 struct usb_serial_port *port = urb->context;
Bill Pemberton52af9542010-07-29 11:05:41 -0400630 struct tty_struct *tty;
Bill Pembertonf7043ec2010-10-21 14:43:05 -0400631 int count;
Bill Pemberton52af9542010-07-29 11:05:41 -0400632
Bill Pemberton52af9542010-07-29 11:05:41 -0400633 tty = tty_port_tty_get(&port->port);
634 if (!tty)
635 return;
636
Bill Pembertonf7043ec2010-10-21 14:43:05 -0400637 count = ssu100_process_packet(urb, tty);
Bill Pemberton52af9542010-07-29 11:05:41 -0400638
639 if (count)
640 tty_flip_buffer_push(tty);
641 tty_kref_put(tty);
642}
643
Bill Pemberton52af9542010-07-29 11:05:41 -0400644static struct usb_serial_driver ssu100_device = {
645 .driver = {
646 .owner = THIS_MODULE,
647 .name = "ssu100",
648 },
649 .description = DRIVER_DESC,
650 .id_table = id_table,
Bill Pemberton52af9542010-07-29 11:05:41 -0400651 .num_ports = 1,
Bill Pemberton52af9542010-07-29 11:05:41 -0400652 .open = ssu100_open,
653 .close = ssu100_close,
654 .attach = ssu100_attach,
Johan Hovold638b9e12012-10-17 16:31:34 +0200655 .port_probe = ssu100_port_probe,
656 .port_remove = ssu100_port_remove,
Bill Pemberton52af9542010-07-29 11:05:41 -0400657 .dtr_rts = ssu100_dtr_rts,
658 .process_read_urb = ssu100_process_read_urb,
659 .tiocmget = ssu100_tiocmget,
660 .tiocmset = ssu100_tiocmset,
Alan Cox0bca1b92010-09-16 18:21:40 +0100661 .get_icount = ssu100_get_icount,
Bill Pemberton52af9542010-07-29 11:05:41 -0400662 .ioctl = ssu100_ioctl,
663 .set_termios = ssu100_set_termios,
Bill Pemberton85dee132010-08-05 17:01:11 -0400664 .disconnect = usb_serial_generic_disconnect,
Bill Pemberton52af9542010-07-29 11:05:41 -0400665};
666
Alan Sternd8603222012-02-23 14:57:25 -0500667static struct usb_serial_driver * const serial_drivers[] = {
668 &ssu100_device, NULL
669};
670
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -0700671module_usb_serial_driver(serial_drivers, id_table);
Bill Pemberton52af9542010-07-29 11:05:41 -0400672
673MODULE_DESCRIPTION(DRIVER_DESC);
674MODULE_LICENSE("GPL");