Adding support for MOXA ART SoC. Testing port of linux-2.6.32.60-moxart.
[linux-3.6.7-moxart.git] / drivers / usb / serial / ssu100.c
blobfe3a8a0132dd6a0f6cc726ccf649b87dd2533493
1 /*
2 * usb-serial driver for Quatech SSU-100
4 * based on ftdi_sio.c and the original serqt_usb.c from Quatech
6 */
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>
18 #include <linux/serial_reg.h>
19 #include <linux/uaccess.h>
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
31 #define SERIAL_MSR_MASK 0xf0
33 #define SERIAL_CRTSCTS ((UART_MCR_RTS << 8) | UART_MSR_CTS)
35 #define SERIAL_EVEN_PARITY (UART_LCR_PARITY | UART_LCR_EPAR)
37 #define MAX_BAUD_RATE 460800
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
49 static bool debug;
51 /* Version Information */
52 #define DRIVER_VERSION "v0.1"
53 #define DRIVER_DESC "Quatech SSU-100 USB to Serial Driver"
55 #define USB_VENDOR_ID_QUATECH 0x061d /* Quatech VID */
56 #define QUATECH_SSU100 0xC020 /* SSU100 */
58 static const struct usb_device_id id_table[] = {
59 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_SSU100)},
60 {} /* Terminating entry */
62 MODULE_DEVICE_TABLE(usb, id_table);
64 struct ssu100_port_private {
65 spinlock_t status_lock;
66 u8 shadowLSR;
67 u8 shadowMSR;
68 wait_queue_head_t delta_msr_wait; /* Used for TIOCMIWAIT */
69 struct async_icount icount;
72 static inline int ssu100_control_msg(struct usb_device *dev,
73 u8 request, u16 data, u16 index)
75 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
76 request, 0x40, data, index,
77 NULL, 0, 300);
80 static inline int ssu100_setdevice(struct usb_device *dev, u8 *data)
82 u16 x = ((u16)(data[1] << 8) | (u16)(data[0]));
84 return ssu100_control_msg(dev, QT_SET_GET_DEVICE, x, 0);
88 static inline int ssu100_getdevice(struct usb_device *dev, u8 *data)
90 return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
91 QT_SET_GET_DEVICE, 0xc0, 0, 0,
92 data, 3, 300);
95 static inline int ssu100_getregister(struct usb_device *dev,
96 unsigned short uart,
97 unsigned short reg,
98 u8 *data)
100 return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
101 QT_SET_GET_REGISTER, 0xc0, reg,
102 uart, data, sizeof(*data), 300);
107 static inline int ssu100_setregister(struct usb_device *dev,
108 unsigned short uart,
109 unsigned short reg,
110 u16 data)
112 u16 value = (data << 8) | reg;
114 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
115 QT_SET_GET_REGISTER, 0x40, value, uart,
116 NULL, 0, 300);
120 #define set_mctrl(dev, set) update_mctrl((dev), (set), 0)
121 #define clear_mctrl(dev, clear) update_mctrl((dev), 0, (clear))
123 /* these do not deal with device that have more than 1 port */
124 static inline int update_mctrl(struct usb_device *dev, unsigned int set,
125 unsigned int clear)
127 unsigned urb_value;
128 int result;
130 if (((set | clear) & (TIOCM_DTR | TIOCM_RTS)) == 0) {
131 dbg("%s - DTR|RTS not being set|cleared", __func__);
132 return 0; /* no change */
135 clear &= ~set; /* 'set' takes precedence over 'clear' */
136 urb_value = 0;
137 if (set & TIOCM_DTR)
138 urb_value |= UART_MCR_DTR;
139 if (set & TIOCM_RTS)
140 urb_value |= UART_MCR_RTS;
142 result = ssu100_setregister(dev, 0, UART_MCR, urb_value);
143 if (result < 0)
144 dbg("%s Error from MODEM_CTRL urb", __func__);
146 return result;
149 static int ssu100_initdevice(struct usb_device *dev)
151 u8 *data;
152 int result = 0;
154 data = kzalloc(3, GFP_KERNEL);
155 if (!data)
156 return -ENOMEM;
158 result = ssu100_getdevice(dev, data);
159 if (result < 0) {
160 dbg("%s - get_device failed %i", __func__, result);
161 goto out;
164 data[1] &= ~FULLPWRBIT;
166 result = ssu100_setdevice(dev, data);
167 if (result < 0) {
168 dbg("%s - setdevice failed %i", __func__, result);
169 goto out;
172 result = ssu100_control_msg(dev, QT_GET_SET_PREBUF_TRIG_LVL, 128, 0);
173 if (result < 0) {
174 dbg("%s - set prebuffer level failed %i", __func__, result);
175 goto out;
178 result = ssu100_control_msg(dev, QT_SET_ATF, ATC_DISABLED, 0);
179 if (result < 0) {
180 dbg("%s - set ATFprebuffer level failed %i", __func__, result);
181 goto out;
184 result = ssu100_getdevice(dev, data);
185 if (result < 0) {
186 dbg("%s - get_device failed %i", __func__, result);
187 goto out;
190 data[0] &= ~(RR_BITS | DUPMODE_BITS);
191 data[0] |= CLKS_X4;
192 data[1] &= ~(LOOPMODE_BITS);
193 data[1] |= RS232_MODE;
195 result = ssu100_setdevice(dev, data);
196 if (result < 0) {
197 dbg("%s - setdevice failed %i", __func__, result);
198 goto out;
201 out: kfree(data);
202 return result;
207 static void ssu100_set_termios(struct tty_struct *tty,
208 struct usb_serial_port *port,
209 struct ktermios *old_termios)
211 struct usb_device *dev = port->serial->dev;
212 struct ktermios *termios = tty->termios;
213 u16 baud, divisor, remainder;
214 unsigned int cflag = termios->c_cflag;
215 u16 urb_value = 0; /* will hold the new flags */
216 int result;
218 if (cflag & PARENB) {
219 if (cflag & PARODD)
220 urb_value |= UART_LCR_PARITY;
221 else
222 urb_value |= SERIAL_EVEN_PARITY;
225 switch (cflag & CSIZE) {
226 case CS5:
227 urb_value |= UART_LCR_WLEN5;
228 break;
229 case CS6:
230 urb_value |= UART_LCR_WLEN6;
231 break;
232 case CS7:
233 urb_value |= UART_LCR_WLEN7;
234 break;
235 default:
236 case CS8:
237 urb_value |= UART_LCR_WLEN8;
238 break;
241 baud = tty_get_baud_rate(tty);
242 if (!baud)
243 baud = 9600;
245 dbg("%s - got baud = %d\n", __func__, baud);
248 divisor = MAX_BAUD_RATE / baud;
249 remainder = MAX_BAUD_RATE % baud;
250 if (((remainder * 2) >= baud) && (baud != 110))
251 divisor++;
253 urb_value = urb_value << 8;
255 result = ssu100_control_msg(dev, QT_GET_SET_UART, divisor, urb_value);
256 if (result < 0)
257 dbg("%s - set uart failed", __func__);
259 if (cflag & CRTSCTS)
260 result = ssu100_control_msg(dev, QT_HW_FLOW_CONTROL_MASK,
261 SERIAL_CRTSCTS, 0);
262 else
263 result = ssu100_control_msg(dev, QT_HW_FLOW_CONTROL_MASK,
264 0, 0);
265 if (result < 0)
266 dbg("%s - set HW flow control failed", __func__);
268 if (I_IXOFF(tty) || I_IXON(tty)) {
269 u16 x = ((u16)(START_CHAR(tty) << 8) | (u16)(STOP_CHAR(tty)));
271 result = ssu100_control_msg(dev, QT_SW_FLOW_CONTROL_MASK,
272 x, 0);
273 } else
274 result = ssu100_control_msg(dev, QT_SW_FLOW_CONTROL_MASK,
275 0, 0);
277 if (result < 0)
278 dbg("%s - set SW flow control failed", __func__);
283 static int ssu100_open(struct tty_struct *tty, struct usb_serial_port *port)
285 struct usb_device *dev = port->serial->dev;
286 struct ssu100_port_private *priv = usb_get_serial_port_data(port);
287 u8 *data;
288 int result;
289 unsigned long flags;
291 data = kzalloc(2, GFP_KERNEL);
292 if (!data)
293 return -ENOMEM;
295 result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
296 QT_OPEN_CLOSE_CHANNEL,
297 QT_TRANSFER_IN, 0x01,
298 0, data, 2, 300);
299 if (result < 0) {
300 dbg("%s - open failed %i", __func__, result);
301 kfree(data);
302 return result;
305 spin_lock_irqsave(&priv->status_lock, flags);
306 priv->shadowLSR = data[0];
307 priv->shadowMSR = data[1];
308 spin_unlock_irqrestore(&priv->status_lock, flags);
310 kfree(data);
312 /* set to 9600 */
313 result = ssu100_control_msg(dev, QT_GET_SET_UART, 0x30, 0x0300);
314 if (result < 0)
315 dbg("%s - set uart failed", __func__);
317 if (tty)
318 ssu100_set_termios(tty, port, tty->termios);
320 return usb_serial_generic_open(tty, port);
323 static void ssu100_close(struct usb_serial_port *port)
325 usb_serial_generic_close(port);
328 static int get_serial_info(struct usb_serial_port *port,
329 struct serial_struct __user *retinfo)
331 struct serial_struct tmp;
333 if (!retinfo)
334 return -EFAULT;
336 memset(&tmp, 0, sizeof(tmp));
337 tmp.line = port->serial->minor;
338 tmp.port = 0;
339 tmp.irq = 0;
340 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
341 tmp.xmit_fifo_size = port->bulk_out_size;
342 tmp.baud_base = 9600;
343 tmp.close_delay = 5*HZ;
344 tmp.closing_wait = 30*HZ;
346 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
347 return -EFAULT;
348 return 0;
351 static int wait_modem_info(struct usb_serial_port *port, unsigned int arg)
353 struct ssu100_port_private *priv = usb_get_serial_port_data(port);
354 struct async_icount prev, cur;
355 unsigned long flags;
357 spin_lock_irqsave(&priv->status_lock, flags);
358 prev = priv->icount;
359 spin_unlock_irqrestore(&priv->status_lock, flags);
361 while (1) {
362 wait_event_interruptible(priv->delta_msr_wait,
363 ((priv->icount.rng != prev.rng) ||
364 (priv->icount.dsr != prev.dsr) ||
365 (priv->icount.dcd != prev.dcd) ||
366 (priv->icount.cts != prev.cts)));
368 if (signal_pending(current))
369 return -ERESTARTSYS;
371 spin_lock_irqsave(&priv->status_lock, flags);
372 cur = priv->icount;
373 spin_unlock_irqrestore(&priv->status_lock, flags);
375 if ((prev.rng == cur.rng) &&
376 (prev.dsr == cur.dsr) &&
377 (prev.dcd == cur.dcd) &&
378 (prev.cts == cur.cts))
379 return -EIO;
381 if ((arg & TIOCM_RNG && (prev.rng != cur.rng)) ||
382 (arg & TIOCM_DSR && (prev.dsr != cur.dsr)) ||
383 (arg & TIOCM_CD && (prev.dcd != cur.dcd)) ||
384 (arg & TIOCM_CTS && (prev.cts != cur.cts)))
385 return 0;
387 return 0;
390 static int ssu100_get_icount(struct tty_struct *tty,
391 struct serial_icounter_struct *icount)
393 struct usb_serial_port *port = tty->driver_data;
394 struct ssu100_port_private *priv = usb_get_serial_port_data(port);
395 struct async_icount cnow = priv->icount;
397 icount->cts = cnow.cts;
398 icount->dsr = cnow.dsr;
399 icount->rng = cnow.rng;
400 icount->dcd = cnow.dcd;
401 icount->rx = cnow.rx;
402 icount->tx = cnow.tx;
403 icount->frame = cnow.frame;
404 icount->overrun = cnow.overrun;
405 icount->parity = cnow.parity;
406 icount->brk = cnow.brk;
407 icount->buf_overrun = cnow.buf_overrun;
409 return 0;
414 static int ssu100_ioctl(struct tty_struct *tty,
415 unsigned int cmd, unsigned long arg)
417 struct usb_serial_port *port = tty->driver_data;
419 dbg("%s cmd 0x%04x", __func__, cmd);
421 switch (cmd) {
422 case TIOCGSERIAL:
423 return get_serial_info(port,
424 (struct serial_struct __user *) arg);
426 case TIOCMIWAIT:
427 return wait_modem_info(port, arg);
429 default:
430 break;
433 dbg("%s arg not supported", __func__);
435 return -ENOIOCTLCMD;
438 static int ssu100_attach(struct usb_serial *serial)
440 return ssu100_initdevice(serial->dev);
443 static int ssu100_port_probe(struct usb_serial_port *port)
445 struct ssu100_port_private *priv;
447 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
448 if (!priv)
449 return -ENOMEM;
451 spin_lock_init(&priv->status_lock);
452 init_waitqueue_head(&priv->delta_msr_wait);
454 usb_set_serial_port_data(port, priv);
456 return 0;
459 static int ssu100_port_remove(struct usb_serial_port *port)
461 struct ssu100_port_private *priv;
463 priv = usb_get_serial_port_data(port);
464 kfree(priv);
466 return 0;
469 static int ssu100_tiocmget(struct tty_struct *tty)
471 struct usb_serial_port *port = tty->driver_data;
472 struct usb_device *dev = port->serial->dev;
473 u8 *d;
474 int r;
476 d = kzalloc(2, GFP_KERNEL);
477 if (!d)
478 return -ENOMEM;
480 r = ssu100_getregister(dev, 0, UART_MCR, d);
481 if (r < 0)
482 goto mget_out;
484 r = ssu100_getregister(dev, 0, UART_MSR, d+1);
485 if (r < 0)
486 goto mget_out;
488 r = (d[0] & UART_MCR_DTR ? TIOCM_DTR : 0) |
489 (d[0] & UART_MCR_RTS ? TIOCM_RTS : 0) |
490 (d[1] & UART_MSR_CTS ? TIOCM_CTS : 0) |
491 (d[1] & UART_MSR_DCD ? TIOCM_CAR : 0) |
492 (d[1] & UART_MSR_RI ? TIOCM_RI : 0) |
493 (d[1] & UART_MSR_DSR ? TIOCM_DSR : 0);
495 mget_out:
496 kfree(d);
497 return r;
500 static int ssu100_tiocmset(struct tty_struct *tty,
501 unsigned int set, unsigned int clear)
503 struct usb_serial_port *port = tty->driver_data;
504 struct usb_device *dev = port->serial->dev;
506 return update_mctrl(dev, set, clear);
509 static void ssu100_dtr_rts(struct usb_serial_port *port, int on)
511 struct usb_device *dev = port->serial->dev;
513 mutex_lock(&port->serial->disc_mutex);
514 if (!port->serial->disconnected) {
515 /* Disable flow control */
516 if (!on &&
517 ssu100_setregister(dev, 0, UART_MCR, 0) < 0)
518 dev_err(&port->dev, "error from flowcontrol urb\n");
519 /* drop RTS and DTR */
520 if (on)
521 set_mctrl(dev, TIOCM_DTR | TIOCM_RTS);
522 else
523 clear_mctrl(dev, TIOCM_DTR | TIOCM_RTS);
525 mutex_unlock(&port->serial->disc_mutex);
528 static void ssu100_update_msr(struct usb_serial_port *port, u8 msr)
530 struct ssu100_port_private *priv = usb_get_serial_port_data(port);
531 unsigned long flags;
533 spin_lock_irqsave(&priv->status_lock, flags);
534 priv->shadowMSR = msr;
535 spin_unlock_irqrestore(&priv->status_lock, flags);
537 if (msr & UART_MSR_ANY_DELTA) {
538 /* update input line counters */
539 if (msr & UART_MSR_DCTS)
540 priv->icount.cts++;
541 if (msr & UART_MSR_DDSR)
542 priv->icount.dsr++;
543 if (msr & UART_MSR_DDCD)
544 priv->icount.dcd++;
545 if (msr & UART_MSR_TERI)
546 priv->icount.rng++;
547 wake_up_interruptible(&priv->delta_msr_wait);
551 static void ssu100_update_lsr(struct usb_serial_port *port, u8 lsr,
552 char *tty_flag)
554 struct ssu100_port_private *priv = usb_get_serial_port_data(port);
555 unsigned long flags;
557 spin_lock_irqsave(&priv->status_lock, flags);
558 priv->shadowLSR = lsr;
559 spin_unlock_irqrestore(&priv->status_lock, flags);
561 *tty_flag = TTY_NORMAL;
562 if (lsr & UART_LSR_BRK_ERROR_BITS) {
563 /* we always want to update icount, but we only want to
564 * update tty_flag for one case */
565 if (lsr & UART_LSR_BI) {
566 priv->icount.brk++;
567 *tty_flag = TTY_BREAK;
568 usb_serial_handle_break(port);
570 if (lsr & UART_LSR_PE) {
571 priv->icount.parity++;
572 if (*tty_flag == TTY_NORMAL)
573 *tty_flag = TTY_PARITY;
575 if (lsr & UART_LSR_FE) {
576 priv->icount.frame++;
577 if (*tty_flag == TTY_NORMAL)
578 *tty_flag = TTY_FRAME;
580 if (lsr & UART_LSR_OE){
581 priv->icount.overrun++;
582 if (*tty_flag == TTY_NORMAL)
583 *tty_flag = TTY_OVERRUN;
589 static int ssu100_process_packet(struct urb *urb,
590 struct tty_struct *tty)
592 struct usb_serial_port *port = urb->context;
593 char *packet = (char *)urb->transfer_buffer;
594 char flag = TTY_NORMAL;
595 u32 len = urb->actual_length;
596 int i;
597 char *ch;
599 if ((len >= 4) &&
600 (packet[0] == 0x1b) && (packet[1] == 0x1b) &&
601 ((packet[2] == 0x00) || (packet[2] == 0x01))) {
602 if (packet[2] == 0x00) {
603 ssu100_update_lsr(port, packet[3], &flag);
604 if (flag == TTY_OVERRUN)
605 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
607 if (packet[2] == 0x01)
608 ssu100_update_msr(port, packet[3]);
610 len -= 4;
611 ch = packet + 4;
612 } else
613 ch = packet;
615 if (!len)
616 return 0; /* status only */
618 if (port->port.console && port->sysrq) {
619 for (i = 0; i < len; i++, ch++) {
620 if (!usb_serial_handle_sysrq_char(port, *ch))
621 tty_insert_flip_char(tty, *ch, flag);
623 } else
624 tty_insert_flip_string_fixed_flag(tty, ch, flag, len);
626 return len;
629 static void ssu100_process_read_urb(struct urb *urb)
631 struct usb_serial_port *port = urb->context;
632 struct tty_struct *tty;
633 int count;
635 tty = tty_port_tty_get(&port->port);
636 if (!tty)
637 return;
639 count = ssu100_process_packet(urb, tty);
641 if (count)
642 tty_flip_buffer_push(tty);
643 tty_kref_put(tty);
646 static struct usb_serial_driver ssu100_device = {
647 .driver = {
648 .owner = THIS_MODULE,
649 .name = "ssu100",
651 .description = DRIVER_DESC,
652 .id_table = id_table,
653 .num_ports = 1,
654 .open = ssu100_open,
655 .close = ssu100_close,
656 .attach = ssu100_attach,
657 .port_probe = ssu100_port_probe,
658 .port_remove = ssu100_port_remove,
659 .dtr_rts = ssu100_dtr_rts,
660 .process_read_urb = ssu100_process_read_urb,
661 .tiocmget = ssu100_tiocmget,
662 .tiocmset = ssu100_tiocmset,
663 .get_icount = ssu100_get_icount,
664 .ioctl = ssu100_ioctl,
665 .set_termios = ssu100_set_termios,
666 .disconnect = usb_serial_generic_disconnect,
669 static struct usb_serial_driver * const serial_drivers[] = {
670 &ssu100_device, NULL
673 module_usb_serial_driver(serial_drivers, id_table);
675 MODULE_DESCRIPTION(DRIVER_DESC);
676 MODULE_LICENSE("GPL");
678 module_param(debug, bool, S_IRUGO | S_IWUSR);
679 MODULE_PARM_DESC(debug, "Debug enabled or not");