net/freescale: remove __dev* attributes
[linux/fpc-iii.git] / drivers / usb / serial / ssu100.c
blob868d1e6852e2ab6086a354cd62a352a241cb51c7
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 /* Version Information */
50 #define DRIVER_VERSION "v0.1"
51 #define DRIVER_DESC "Quatech SSU-100 USB to Serial Driver"
53 #define USB_VENDOR_ID_QUATECH 0x061d /* Quatech VID */
54 #define QUATECH_SSU100 0xC020 /* SSU100 */
56 static const struct usb_device_id id_table[] = {
57 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_SSU100)},
58 {} /* Terminating entry */
60 MODULE_DEVICE_TABLE(usb, id_table);
62 struct ssu100_port_private {
63 spinlock_t status_lock;
64 u8 shadowLSR;
65 u8 shadowMSR;
66 wait_queue_head_t delta_msr_wait; /* Used for TIOCMIWAIT */
67 struct async_icount icount;
70 static inline int ssu100_control_msg(struct usb_device *dev,
71 u8 request, u16 data, u16 index)
73 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
74 request, 0x40, data, index,
75 NULL, 0, 300);
78 static inline int ssu100_setdevice(struct usb_device *dev, u8 *data)
80 u16 x = ((u16)(data[1] << 8) | (u16)(data[0]));
82 return ssu100_control_msg(dev, QT_SET_GET_DEVICE, x, 0);
86 static inline int ssu100_getdevice(struct usb_device *dev, u8 *data)
88 return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
89 QT_SET_GET_DEVICE, 0xc0, 0, 0,
90 data, 3, 300);
93 static inline int ssu100_getregister(struct usb_device *dev,
94 unsigned short uart,
95 unsigned short reg,
96 u8 *data)
98 return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
99 QT_SET_GET_REGISTER, 0xc0, reg,
100 uart, data, sizeof(*data), 300);
105 static inline int ssu100_setregister(struct usb_device *dev,
106 unsigned short uart,
107 unsigned short reg,
108 u16 data)
110 u16 value = (data << 8) | reg;
112 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
113 QT_SET_GET_REGISTER, 0x40, value, uart,
114 NULL, 0, 300);
118 #define set_mctrl(dev, set) update_mctrl((dev), (set), 0)
119 #define clear_mctrl(dev, clear) update_mctrl((dev), 0, (clear))
121 /* these do not deal with device that have more than 1 port */
122 static inline int update_mctrl(struct usb_device *dev, unsigned int set,
123 unsigned int clear)
125 unsigned urb_value;
126 int result;
128 if (((set | clear) & (TIOCM_DTR | TIOCM_RTS)) == 0) {
129 dev_dbg(&dev->dev, "%s - DTR|RTS not being set|cleared\n", __func__);
130 return 0; /* no change */
133 clear &= ~set; /* 'set' takes precedence over 'clear' */
134 urb_value = 0;
135 if (set & TIOCM_DTR)
136 urb_value |= UART_MCR_DTR;
137 if (set & TIOCM_RTS)
138 urb_value |= UART_MCR_RTS;
140 result = ssu100_setregister(dev, 0, UART_MCR, urb_value);
141 if (result < 0)
142 dev_dbg(&dev->dev, "%s Error from MODEM_CTRL urb\n", __func__);
144 return result;
147 static int ssu100_initdevice(struct usb_device *dev)
149 u8 *data;
150 int result = 0;
152 data = kzalloc(3, GFP_KERNEL);
153 if (!data)
154 return -ENOMEM;
156 result = ssu100_getdevice(dev, data);
157 if (result < 0) {
158 dev_dbg(&dev->dev, "%s - get_device failed %i\n", __func__, result);
159 goto out;
162 data[1] &= ~FULLPWRBIT;
164 result = ssu100_setdevice(dev, data);
165 if (result < 0) {
166 dev_dbg(&dev->dev, "%s - setdevice failed %i\n", __func__, result);
167 goto out;
170 result = ssu100_control_msg(dev, QT_GET_SET_PREBUF_TRIG_LVL, 128, 0);
171 if (result < 0) {
172 dev_dbg(&dev->dev, "%s - set prebuffer level failed %i\n", __func__, result);
173 goto out;
176 result = ssu100_control_msg(dev, QT_SET_ATF, ATC_DISABLED, 0);
177 if (result < 0) {
178 dev_dbg(&dev->dev, "%s - set ATFprebuffer level failed %i\n", __func__, result);
179 goto out;
182 result = ssu100_getdevice(dev, data);
183 if (result < 0) {
184 dev_dbg(&dev->dev, "%s - get_device failed %i\n", __func__, result);
185 goto out;
188 data[0] &= ~(RR_BITS | DUPMODE_BITS);
189 data[0] |= CLKS_X4;
190 data[1] &= ~(LOOPMODE_BITS);
191 data[1] |= RS232_MODE;
193 result = ssu100_setdevice(dev, data);
194 if (result < 0) {
195 dev_dbg(&dev->dev, "%s - setdevice failed %i\n", __func__, result);
196 goto out;
199 out: kfree(data);
200 return result;
205 static void ssu100_set_termios(struct tty_struct *tty,
206 struct usb_serial_port *port,
207 struct ktermios *old_termios)
209 struct usb_device *dev = port->serial->dev;
210 struct ktermios *termios = &tty->termios;
211 u16 baud, divisor, remainder;
212 unsigned int cflag = termios->c_cflag;
213 u16 urb_value = 0; /* will hold the new flags */
214 int result;
216 if (cflag & PARENB) {
217 if (cflag & PARODD)
218 urb_value |= UART_LCR_PARITY;
219 else
220 urb_value |= SERIAL_EVEN_PARITY;
223 switch (cflag & CSIZE) {
224 case CS5:
225 urb_value |= UART_LCR_WLEN5;
226 break;
227 case CS6:
228 urb_value |= UART_LCR_WLEN6;
229 break;
230 case CS7:
231 urb_value |= UART_LCR_WLEN7;
232 break;
233 default:
234 case CS8:
235 urb_value |= UART_LCR_WLEN8;
236 break;
239 baud = tty_get_baud_rate(tty);
240 if (!baud)
241 baud = 9600;
243 dev_dbg(&port->dev, "%s - got baud = %d\n", __func__, baud);
246 divisor = MAX_BAUD_RATE / baud;
247 remainder = MAX_BAUD_RATE % baud;
248 if (((remainder * 2) >= baud) && (baud != 110))
249 divisor++;
251 urb_value = urb_value << 8;
253 result = ssu100_control_msg(dev, QT_GET_SET_UART, divisor, urb_value);
254 if (result < 0)
255 dev_dbg(&port->dev, "%s - set uart failed\n", __func__);
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)
264 dev_dbg(&port->dev, "%s - set HW flow control failed\n", __func__);
266 if (I_IXOFF(tty) || I_IXON(tty)) {
267 u16 x = ((u16)(START_CHAR(tty) << 8) | (u16)(STOP_CHAR(tty)));
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);
275 if (result < 0)
276 dev_dbg(&port->dev, "%s - set SW flow control failed\n", __func__);
281 static int ssu100_open(struct tty_struct *tty, struct usb_serial_port *port)
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;
287 unsigned long flags;
289 data = kzalloc(2, GFP_KERNEL);
290 if (!data)
291 return -ENOMEM;
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) {
298 dev_dbg(&port->dev, "%s - open failed %i\n", __func__, result);
299 kfree(data);
300 return result;
303 spin_lock_irqsave(&priv->status_lock, flags);
304 priv->shadowLSR = data[0];
305 priv->shadowMSR = data[1];
306 spin_unlock_irqrestore(&priv->status_lock, flags);
308 kfree(data);
310 /* set to 9600 */
311 result = ssu100_control_msg(dev, QT_GET_SET_UART, 0x30, 0x0300);
312 if (result < 0)
313 dev_dbg(&port->dev, "%s - set uart failed\n", __func__);
315 if (tty)
316 ssu100_set_termios(tty, port, &tty->termios);
318 return usb_serial_generic_open(tty, port);
321 static void ssu100_close(struct usb_serial_port *port)
323 usb_serial_generic_close(port);
326 static int get_serial_info(struct usb_serial_port *port,
327 struct serial_struct __user *retinfo)
329 struct serial_struct tmp;
331 if (!retinfo)
332 return -EFAULT;
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;
344 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
345 return -EFAULT;
346 return 0;
349 static int wait_modem_info(struct usb_serial_port *port, unsigned int arg)
351 struct ssu100_port_private *priv = usb_get_serial_port_data(port);
352 struct async_icount prev, cur;
353 unsigned long flags;
355 spin_lock_irqsave(&priv->status_lock, flags);
356 prev = priv->icount;
357 spin_unlock_irqrestore(&priv->status_lock, flags);
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)));
366 if (signal_pending(current))
367 return -ERESTARTSYS;
369 spin_lock_irqsave(&priv->status_lock, flags);
370 cur = priv->icount;
371 spin_unlock_irqrestore(&priv->status_lock, flags);
373 if ((prev.rng == cur.rng) &&
374 (prev.dsr == cur.dsr) &&
375 (prev.dcd == cur.dcd) &&
376 (prev.cts == cur.cts))
377 return -EIO;
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;
385 return 0;
388 static int ssu100_get_icount(struct tty_struct *tty,
389 struct serial_icounter_struct *icount)
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;
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;
407 return 0;
412 static int ssu100_ioctl(struct tty_struct *tty,
413 unsigned int cmd, unsigned long arg)
415 struct usb_serial_port *port = tty->driver_data;
417 dev_dbg(&port->dev, "%s cmd 0x%04x\n", __func__, cmd);
419 switch (cmd) {
420 case TIOCGSERIAL:
421 return get_serial_info(port,
422 (struct serial_struct __user *) arg);
424 case TIOCMIWAIT:
425 return wait_modem_info(port, arg);
427 default:
428 break;
431 dev_dbg(&port->dev, "%s arg not supported\n", __func__);
433 return -ENOIOCTLCMD;
436 static int ssu100_attach(struct usb_serial *serial)
438 return ssu100_initdevice(serial->dev);
441 static int ssu100_port_probe(struct usb_serial_port *port)
443 struct ssu100_port_private *priv;
445 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
446 if (!priv)
447 return -ENOMEM;
449 spin_lock_init(&priv->status_lock);
450 init_waitqueue_head(&priv->delta_msr_wait);
452 usb_set_serial_port_data(port, priv);
454 return 0;
457 static int ssu100_port_remove(struct usb_serial_port *port)
459 struct ssu100_port_private *priv;
461 priv = usb_get_serial_port_data(port);
462 kfree(priv);
464 return 0;
467 static int ssu100_tiocmget(struct tty_struct *tty)
469 struct usb_serial_port *port = tty->driver_data;
470 struct usb_device *dev = port->serial->dev;
471 u8 *d;
472 int r;
474 d = kzalloc(2, GFP_KERNEL);
475 if (!d)
476 return -ENOMEM;
478 r = ssu100_getregister(dev, 0, UART_MCR, d);
479 if (r < 0)
480 goto mget_out;
482 r = ssu100_getregister(dev, 0, UART_MSR, d+1);
483 if (r < 0)
484 goto mget_out;
486 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);
493 mget_out:
494 kfree(d);
495 return r;
498 static int ssu100_tiocmset(struct tty_struct *tty,
499 unsigned int set, unsigned int clear)
501 struct usb_serial_port *port = tty->driver_data;
502 struct usb_device *dev = port->serial->dev;
504 return update_mctrl(dev, set, clear);
507 static void ssu100_dtr_rts(struct usb_serial_port *port, int on)
509 struct usb_device *dev = port->serial->dev;
511 mutex_lock(&port->serial->disc_mutex);
512 if (!port->serial->disconnected) {
513 /* Disable flow control */
514 if (!on &&
515 ssu100_setregister(dev, 0, UART_MCR, 0) < 0)
516 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);
523 mutex_unlock(&port->serial->disc_mutex);
526 static void ssu100_update_msr(struct usb_serial_port *port, u8 msr)
528 struct ssu100_port_private *priv = usb_get_serial_port_data(port);
529 unsigned long flags;
531 spin_lock_irqsave(&priv->status_lock, flags);
532 priv->shadowMSR = msr;
533 spin_unlock_irqrestore(&priv->status_lock, flags);
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);
549 static void ssu100_update_lsr(struct usb_serial_port *port, u8 lsr,
550 char *tty_flag)
552 struct ssu100_port_private *priv = usb_get_serial_port_data(port);
553 unsigned long flags;
555 spin_lock_irqsave(&priv->status_lock, flags);
556 priv->shadowLSR = lsr;
557 spin_unlock_irqrestore(&priv->status_lock, flags);
559 *tty_flag = TTY_NORMAL;
560 if (lsr & UART_LSR_BRK_ERROR_BITS) {
561 /* we always want to update icount, but we only want to
562 * update tty_flag for one case */
563 if (lsr & UART_LSR_BI) {
564 priv->icount.brk++;
565 *tty_flag = TTY_BREAK;
566 usb_serial_handle_break(port);
568 if (lsr & UART_LSR_PE) {
569 priv->icount.parity++;
570 if (*tty_flag == TTY_NORMAL)
571 *tty_flag = TTY_PARITY;
573 if (lsr & UART_LSR_FE) {
574 priv->icount.frame++;
575 if (*tty_flag == TTY_NORMAL)
576 *tty_flag = TTY_FRAME;
578 if (lsr & UART_LSR_OE){
579 priv->icount.overrun++;
580 if (*tty_flag == TTY_NORMAL)
581 *tty_flag = TTY_OVERRUN;
587 static int ssu100_process_packet(struct urb *urb,
588 struct tty_struct *tty)
590 struct usb_serial_port *port = urb->context;
591 char *packet = (char *)urb->transfer_buffer;
592 char flag = TTY_NORMAL;
593 u32 len = urb->actual_length;
594 int i;
595 char *ch;
597 if ((len >= 4) &&
598 (packet[0] == 0x1b) && (packet[1] == 0x1b) &&
599 ((packet[2] == 0x00) || (packet[2] == 0x01))) {
600 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);
605 if (packet[2] == 0x01)
606 ssu100_update_msr(port, packet[3]);
608 len -= 4;
609 ch = packet + 4;
610 } else
611 ch = packet;
613 if (!len)
614 return 0; /* status only */
616 if (port->port.console && port->sysrq) {
617 for (i = 0; i < len; i++, ch++) {
618 if (!usb_serial_handle_sysrq_char(port, *ch))
619 tty_insert_flip_char(tty, *ch, flag);
621 } else
622 tty_insert_flip_string_fixed_flag(tty, ch, flag, len);
624 return len;
627 static void ssu100_process_read_urb(struct urb *urb)
629 struct usb_serial_port *port = urb->context;
630 struct tty_struct *tty;
631 int count;
633 tty = tty_port_tty_get(&port->port);
634 if (!tty)
635 return;
637 count = ssu100_process_packet(urb, tty);
639 if (count)
640 tty_flip_buffer_push(tty);
641 tty_kref_put(tty);
644 static struct usb_serial_driver ssu100_device = {
645 .driver = {
646 .owner = THIS_MODULE,
647 .name = "ssu100",
649 .description = DRIVER_DESC,
650 .id_table = id_table,
651 .num_ports = 1,
652 .open = ssu100_open,
653 .close = ssu100_close,
654 .attach = ssu100_attach,
655 .port_probe = ssu100_port_probe,
656 .port_remove = ssu100_port_remove,
657 .dtr_rts = ssu100_dtr_rts,
658 .process_read_urb = ssu100_process_read_urb,
659 .tiocmget = ssu100_tiocmget,
660 .tiocmset = ssu100_tiocmset,
661 .get_icount = ssu100_get_icount,
662 .ioctl = ssu100_ioctl,
663 .set_termios = ssu100_set_termios,
664 .disconnect = usb_serial_generic_disconnect,
667 static struct usb_serial_driver * const serial_drivers[] = {
668 &ssu100_device, NULL
671 module_usb_serial_driver(serial_drivers, id_table);
673 MODULE_DESCRIPTION(DRIVER_DESC);
674 MODULE_LICENSE("GPL");