ARC: [plat-eznps] spinlock aware for MTM
[linux/fpc-iii.git] / drivers / usb / serial / ssu100.c
blob5aa7bbbeba3dc62dad40f6f9ea1e6d830e29d835
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/slab.h>
10 #include <linux/tty.h>
11 #include <linux/tty_driver.h>
12 #include <linux/tty_flip.h>
13 #include <linux/module.h>
14 #include <linux/serial.h>
15 #include <linux/usb.h>
16 #include <linux/usb/serial.h>
17 #include <linux/serial_reg.h>
18 #include <linux/uaccess.h>
20 #define QT_OPEN_CLOSE_CHANNEL 0xca
21 #define QT_SET_GET_DEVICE 0xc2
22 #define QT_SET_GET_REGISTER 0xc0
23 #define QT_GET_SET_PREBUF_TRIG_LVL 0xcc
24 #define QT_SET_ATF 0xcd
25 #define QT_GET_SET_UART 0xc1
26 #define QT_TRANSFER_IN 0xc0
27 #define QT_HW_FLOW_CONTROL_MASK 0xc5
28 #define QT_SW_FLOW_CONTROL_MASK 0xc6
30 #define SERIAL_MSR_MASK 0xf0
32 #define SERIAL_CRTSCTS ((UART_MCR_RTS << 8) | UART_MSR_CTS)
34 #define SERIAL_EVEN_PARITY (UART_LCR_PARITY | UART_LCR_EPAR)
36 #define MAX_BAUD_RATE 460800
38 #define ATC_DISABLED 0x00
39 #define DUPMODE_BITS 0xc0
40 #define RR_BITS 0x03
41 #define LOOPMODE_BITS 0x41
42 #define RS232_MODE 0x00
43 #define RTSCTS_TO_CONNECTOR 0x40
44 #define CLKS_X4 0x02
45 #define FULLPWRBIT 0x00000080
46 #define NEXT_BOARD_POWER_BIT 0x00000004
48 #define DRIVER_DESC "Quatech SSU-100 USB to Serial Driver"
50 #define USB_VENDOR_ID_QUATECH 0x061d /* Quatech VID */
51 #define QUATECH_SSU100 0xC020 /* SSU100 */
53 static const struct usb_device_id id_table[] = {
54 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_SSU100)},
55 {} /* Terminating entry */
57 MODULE_DEVICE_TABLE(usb, id_table);
59 struct ssu100_port_private {
60 spinlock_t status_lock;
61 u8 shadowLSR;
62 u8 shadowMSR;
65 static inline int ssu100_control_msg(struct usb_device *dev,
66 u8 request, u16 data, u16 index)
68 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
69 request, 0x40, data, index,
70 NULL, 0, 300);
73 static inline int ssu100_setdevice(struct usb_device *dev, u8 *data)
75 u16 x = ((u16)(data[1] << 8) | (u16)(data[0]));
77 return ssu100_control_msg(dev, QT_SET_GET_DEVICE, x, 0);
81 static inline int ssu100_getdevice(struct usb_device *dev, u8 *data)
83 int ret;
85 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
86 QT_SET_GET_DEVICE, 0xc0, 0, 0,
87 data, 3, 300);
88 if (ret < 3) {
89 if (ret >= 0)
90 ret = -EIO;
93 return ret;
96 static inline int ssu100_getregister(struct usb_device *dev,
97 unsigned short uart,
98 unsigned short reg,
99 u8 *data)
101 int ret;
103 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
104 QT_SET_GET_REGISTER, 0xc0, reg,
105 uart, data, sizeof(*data), 300);
106 if (ret < sizeof(*data)) {
107 if (ret >= 0)
108 ret = -EIO;
111 return ret;
115 static inline int ssu100_setregister(struct usb_device *dev,
116 unsigned short uart,
117 unsigned short reg,
118 u16 data)
120 u16 value = (data << 8) | reg;
122 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
123 QT_SET_GET_REGISTER, 0x40, value, uart,
124 NULL, 0, 300);
128 #define set_mctrl(dev, set) update_mctrl((dev), (set), 0)
129 #define clear_mctrl(dev, clear) update_mctrl((dev), 0, (clear))
131 /* these do not deal with device that have more than 1 port */
132 static inline int update_mctrl(struct usb_device *dev, unsigned int set,
133 unsigned int clear)
135 unsigned urb_value;
136 int result;
138 if (((set | clear) & (TIOCM_DTR | TIOCM_RTS)) == 0) {
139 dev_dbg(&dev->dev, "%s - DTR|RTS not being set|cleared\n", __func__);
140 return 0; /* no change */
143 clear &= ~set; /* 'set' takes precedence over 'clear' */
144 urb_value = 0;
145 if (set & TIOCM_DTR)
146 urb_value |= UART_MCR_DTR;
147 if (set & TIOCM_RTS)
148 urb_value |= UART_MCR_RTS;
150 result = ssu100_setregister(dev, 0, UART_MCR, urb_value);
151 if (result < 0)
152 dev_dbg(&dev->dev, "%s Error from MODEM_CTRL urb\n", __func__);
154 return result;
157 static int ssu100_initdevice(struct usb_device *dev)
159 u8 *data;
160 int result = 0;
162 data = kzalloc(3, GFP_KERNEL);
163 if (!data)
164 return -ENOMEM;
166 result = ssu100_getdevice(dev, data);
167 if (result < 0) {
168 dev_dbg(&dev->dev, "%s - get_device failed %i\n", __func__, result);
169 goto out;
172 data[1] &= ~FULLPWRBIT;
174 result = ssu100_setdevice(dev, data);
175 if (result < 0) {
176 dev_dbg(&dev->dev, "%s - setdevice failed %i\n", __func__, result);
177 goto out;
180 result = ssu100_control_msg(dev, QT_GET_SET_PREBUF_TRIG_LVL, 128, 0);
181 if (result < 0) {
182 dev_dbg(&dev->dev, "%s - set prebuffer level failed %i\n", __func__, result);
183 goto out;
186 result = ssu100_control_msg(dev, QT_SET_ATF, ATC_DISABLED, 0);
187 if (result < 0) {
188 dev_dbg(&dev->dev, "%s - set ATFprebuffer level failed %i\n", __func__, result);
189 goto out;
192 result = ssu100_getdevice(dev, data);
193 if (result < 0) {
194 dev_dbg(&dev->dev, "%s - get_device failed %i\n", __func__, result);
195 goto out;
198 data[0] &= ~(RR_BITS | DUPMODE_BITS);
199 data[0] |= CLKS_X4;
200 data[1] &= ~(LOOPMODE_BITS);
201 data[1] |= RS232_MODE;
203 result = ssu100_setdevice(dev, data);
204 if (result < 0) {
205 dev_dbg(&dev->dev, "%s - setdevice failed %i\n", __func__, result);
206 goto out;
209 out: kfree(data);
210 return result;
215 static void ssu100_set_termios(struct tty_struct *tty,
216 struct usb_serial_port *port,
217 struct ktermios *old_termios)
219 struct usb_device *dev = port->serial->dev;
220 struct ktermios *termios = &tty->termios;
221 u16 baud, divisor, remainder;
222 unsigned int cflag = termios->c_cflag;
223 u16 urb_value = 0; /* will hold the new flags */
224 int result;
226 if (cflag & PARENB) {
227 if (cflag & PARODD)
228 urb_value |= UART_LCR_PARITY;
229 else
230 urb_value |= SERIAL_EVEN_PARITY;
233 switch (cflag & CSIZE) {
234 case CS5:
235 urb_value |= UART_LCR_WLEN5;
236 break;
237 case CS6:
238 urb_value |= UART_LCR_WLEN6;
239 break;
240 case CS7:
241 urb_value |= UART_LCR_WLEN7;
242 break;
243 default:
244 case CS8:
245 urb_value |= UART_LCR_WLEN8;
246 break;
249 baud = tty_get_baud_rate(tty);
250 if (!baud)
251 baud = 9600;
253 dev_dbg(&port->dev, "%s - got baud = %d\n", __func__, baud);
256 divisor = MAX_BAUD_RATE / baud;
257 remainder = MAX_BAUD_RATE % baud;
258 if (((remainder * 2) >= baud) && (baud != 110))
259 divisor++;
261 urb_value = urb_value << 8;
263 result = ssu100_control_msg(dev, QT_GET_SET_UART, divisor, urb_value);
264 if (result < 0)
265 dev_dbg(&port->dev, "%s - set uart failed\n", __func__);
267 if (cflag & CRTSCTS)
268 result = ssu100_control_msg(dev, QT_HW_FLOW_CONTROL_MASK,
269 SERIAL_CRTSCTS, 0);
270 else
271 result = ssu100_control_msg(dev, QT_HW_FLOW_CONTROL_MASK,
272 0, 0);
273 if (result < 0)
274 dev_dbg(&port->dev, "%s - set HW flow control failed\n", __func__);
276 if (I_IXOFF(tty) || I_IXON(tty)) {
277 u16 x = ((u16)(START_CHAR(tty) << 8) | (u16)(STOP_CHAR(tty)));
279 result = ssu100_control_msg(dev, QT_SW_FLOW_CONTROL_MASK,
280 x, 0);
281 } else
282 result = ssu100_control_msg(dev, QT_SW_FLOW_CONTROL_MASK,
283 0, 0);
285 if (result < 0)
286 dev_dbg(&port->dev, "%s - set SW flow control failed\n", __func__);
291 static int ssu100_open(struct tty_struct *tty, struct usb_serial_port *port)
293 struct usb_device *dev = port->serial->dev;
294 struct ssu100_port_private *priv = usb_get_serial_port_data(port);
295 u8 *data;
296 int result;
297 unsigned long flags;
299 data = kzalloc(2, GFP_KERNEL);
300 if (!data)
301 return -ENOMEM;
303 result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
304 QT_OPEN_CLOSE_CHANNEL,
305 QT_TRANSFER_IN, 0x01,
306 0, data, 2, 300);
307 if (result < 2) {
308 dev_dbg(&port->dev, "%s - open failed %i\n", __func__, result);
309 if (result >= 0)
310 result = -EIO;
311 kfree(data);
312 return result;
315 spin_lock_irqsave(&priv->status_lock, flags);
316 priv->shadowLSR = data[0];
317 priv->shadowMSR = data[1];
318 spin_unlock_irqrestore(&priv->status_lock, flags);
320 kfree(data);
322 /* set to 9600 */
323 result = ssu100_control_msg(dev, QT_GET_SET_UART, 0x30, 0x0300);
324 if (result < 0)
325 dev_dbg(&port->dev, "%s - set uart failed\n", __func__);
327 if (tty)
328 ssu100_set_termios(tty, port, &tty->termios);
330 return usb_serial_generic_open(tty, port);
333 static int get_serial_info(struct usb_serial_port *port,
334 struct serial_struct __user *retinfo)
336 struct serial_struct tmp;
338 memset(&tmp, 0, sizeof(tmp));
339 tmp.line = port->minor;
340 tmp.port = 0;
341 tmp.irq = 0;
342 tmp.xmit_fifo_size = port->bulk_out_size;
343 tmp.baud_base = 9600;
344 tmp.close_delay = 5*HZ;
345 tmp.closing_wait = 30*HZ;
347 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
348 return -EFAULT;
349 return 0;
352 static int ssu100_ioctl(struct tty_struct *tty,
353 unsigned int cmd, unsigned long arg)
355 struct usb_serial_port *port = tty->driver_data;
357 switch (cmd) {
358 case TIOCGSERIAL:
359 return get_serial_info(port,
360 (struct serial_struct __user *) arg);
361 default:
362 break;
365 return -ENOIOCTLCMD;
368 static int ssu100_attach(struct usb_serial *serial)
370 return ssu100_initdevice(serial->dev);
373 static int ssu100_port_probe(struct usb_serial_port *port)
375 struct ssu100_port_private *priv;
377 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
378 if (!priv)
379 return -ENOMEM;
381 spin_lock_init(&priv->status_lock);
383 usb_set_serial_port_data(port, priv);
385 return 0;
388 static int ssu100_port_remove(struct usb_serial_port *port)
390 struct ssu100_port_private *priv;
392 priv = usb_get_serial_port_data(port);
393 kfree(priv);
395 return 0;
398 static int ssu100_tiocmget(struct tty_struct *tty)
400 struct usb_serial_port *port = tty->driver_data;
401 struct usb_device *dev = port->serial->dev;
402 u8 *d;
403 int r;
405 d = kzalloc(2, GFP_KERNEL);
406 if (!d)
407 return -ENOMEM;
409 r = ssu100_getregister(dev, 0, UART_MCR, d);
410 if (r < 0)
411 goto mget_out;
413 r = ssu100_getregister(dev, 0, UART_MSR, d+1);
414 if (r < 0)
415 goto mget_out;
417 r = (d[0] & UART_MCR_DTR ? TIOCM_DTR : 0) |
418 (d[0] & UART_MCR_RTS ? TIOCM_RTS : 0) |
419 (d[1] & UART_MSR_CTS ? TIOCM_CTS : 0) |
420 (d[1] & UART_MSR_DCD ? TIOCM_CAR : 0) |
421 (d[1] & UART_MSR_RI ? TIOCM_RI : 0) |
422 (d[1] & UART_MSR_DSR ? TIOCM_DSR : 0);
424 mget_out:
425 kfree(d);
426 return r;
429 static int ssu100_tiocmset(struct tty_struct *tty,
430 unsigned int set, unsigned int clear)
432 struct usb_serial_port *port = tty->driver_data;
433 struct usb_device *dev = port->serial->dev;
435 return update_mctrl(dev, set, clear);
438 static void ssu100_dtr_rts(struct usb_serial_port *port, int on)
440 struct usb_device *dev = port->serial->dev;
442 /* Disable flow control */
443 if (!on) {
444 if (ssu100_setregister(dev, 0, UART_MCR, 0) < 0)
445 dev_err(&port->dev, "error from flowcontrol urb\n");
447 /* drop RTS and DTR */
448 if (on)
449 set_mctrl(dev, TIOCM_DTR | TIOCM_RTS);
450 else
451 clear_mctrl(dev, TIOCM_DTR | TIOCM_RTS);
454 static void ssu100_update_msr(struct usb_serial_port *port, u8 msr)
456 struct ssu100_port_private *priv = usb_get_serial_port_data(port);
457 unsigned long flags;
459 spin_lock_irqsave(&priv->status_lock, flags);
460 priv->shadowMSR = msr;
461 spin_unlock_irqrestore(&priv->status_lock, flags);
463 if (msr & UART_MSR_ANY_DELTA) {
464 /* update input line counters */
465 if (msr & UART_MSR_DCTS)
466 port->icount.cts++;
467 if (msr & UART_MSR_DDSR)
468 port->icount.dsr++;
469 if (msr & UART_MSR_DDCD)
470 port->icount.dcd++;
471 if (msr & UART_MSR_TERI)
472 port->icount.rng++;
473 wake_up_interruptible(&port->port.delta_msr_wait);
477 static void ssu100_update_lsr(struct usb_serial_port *port, u8 lsr,
478 char *tty_flag)
480 struct ssu100_port_private *priv = usb_get_serial_port_data(port);
481 unsigned long flags;
483 spin_lock_irqsave(&priv->status_lock, flags);
484 priv->shadowLSR = lsr;
485 spin_unlock_irqrestore(&priv->status_lock, flags);
487 *tty_flag = TTY_NORMAL;
488 if (lsr & UART_LSR_BRK_ERROR_BITS) {
489 /* we always want to update icount, but we only want to
490 * update tty_flag for one case */
491 if (lsr & UART_LSR_BI) {
492 port->icount.brk++;
493 *tty_flag = TTY_BREAK;
494 usb_serial_handle_break(port);
496 if (lsr & UART_LSR_PE) {
497 port->icount.parity++;
498 if (*tty_flag == TTY_NORMAL)
499 *tty_flag = TTY_PARITY;
501 if (lsr & UART_LSR_FE) {
502 port->icount.frame++;
503 if (*tty_flag == TTY_NORMAL)
504 *tty_flag = TTY_FRAME;
506 if (lsr & UART_LSR_OE) {
507 port->icount.overrun++;
508 tty_insert_flip_char(&port->port, 0, TTY_OVERRUN);
514 static void ssu100_process_read_urb(struct urb *urb)
516 struct usb_serial_port *port = urb->context;
517 char *packet = (char *)urb->transfer_buffer;
518 char flag = TTY_NORMAL;
519 u32 len = urb->actual_length;
520 int i;
521 char *ch;
523 if ((len >= 4) &&
524 (packet[0] == 0x1b) && (packet[1] == 0x1b) &&
525 ((packet[2] == 0x00) || (packet[2] == 0x01))) {
526 if (packet[2] == 0x00)
527 ssu100_update_lsr(port, packet[3], &flag);
528 if (packet[2] == 0x01)
529 ssu100_update_msr(port, packet[3]);
531 len -= 4;
532 ch = packet + 4;
533 } else
534 ch = packet;
536 if (!len)
537 return; /* status only */
539 if (port->port.console && port->sysrq) {
540 for (i = 0; i < len; i++, ch++) {
541 if (!usb_serial_handle_sysrq_char(port, *ch))
542 tty_insert_flip_char(&port->port, *ch, flag);
544 } else
545 tty_insert_flip_string_fixed_flag(&port->port, ch, flag, len);
547 tty_flip_buffer_push(&port->port);
550 static struct usb_serial_driver ssu100_device = {
551 .driver = {
552 .owner = THIS_MODULE,
553 .name = "ssu100",
555 .description = DRIVER_DESC,
556 .id_table = id_table,
557 .num_ports = 1,
558 .open = ssu100_open,
559 .attach = ssu100_attach,
560 .port_probe = ssu100_port_probe,
561 .port_remove = ssu100_port_remove,
562 .dtr_rts = ssu100_dtr_rts,
563 .process_read_urb = ssu100_process_read_urb,
564 .tiocmget = ssu100_tiocmget,
565 .tiocmset = ssu100_tiocmset,
566 .tiocmiwait = usb_serial_generic_tiocmiwait,
567 .get_icount = usb_serial_generic_get_icount,
568 .ioctl = ssu100_ioctl,
569 .set_termios = ssu100_set_termios,
572 static struct usb_serial_driver * const serial_drivers[] = {
573 &ssu100_device, NULL
576 module_usb_serial_driver(serial_drivers, id_table);
578 MODULE_DESCRIPTION(DRIVER_DESC);
579 MODULE_LICENSE("GPL");