• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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/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>
19 
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
29 
30 #define  SERIAL_MSR_MASK            0xf0
31 
32 #define  SERIAL_CRTSCTS ((UART_MCR_RTS << 8) | UART_MSR_CTS)
33 
34 #define  SERIAL_EVEN_PARITY         (UART_LCR_PARITY | UART_LCR_EPAR)
35 
36 #define  MAX_BAUD_RATE              460800
37 
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
47 
48 #define DRIVER_DESC "Quatech SSU-100 USB to Serial Driver"
49 
50 #define	USB_VENDOR_ID_QUATECH	0x061d	/* Quatech VID */
51 #define QUATECH_SSU100	0xC020	/* SSU100 */
52 
53 static const struct usb_device_id id_table[] = {
54 	{USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_SSU100)},
55 	{}			/* Terminating entry */
56 };
57 MODULE_DEVICE_TABLE(usb, id_table);
58 
59 struct ssu100_port_private {
60 	spinlock_t status_lock;
61 	u8 shadowLSR;
62 	u8 shadowMSR;
63 };
64 
ssu100_control_msg(struct usb_device * dev,u8 request,u16 data,u16 index)65 static inline int ssu100_control_msg(struct usb_device *dev,
66 				     u8 request, u16 data, u16 index)
67 {
68 	return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
69 			       request, 0x40, data, index,
70 			       NULL, 0, 300);
71 }
72 
ssu100_setdevice(struct usb_device * dev,u8 * data)73 static inline int ssu100_setdevice(struct usb_device *dev, u8 *data)
74 {
75 	u16 x = ((u16)(data[1] << 8) | (u16)(data[0]));
76 
77 	return ssu100_control_msg(dev, QT_SET_GET_DEVICE, x, 0);
78 }
79 
80 
ssu100_getdevice(struct usb_device * dev,u8 * data)81 static inline int ssu100_getdevice(struct usb_device *dev, u8 *data)
82 {
83 	int ret;
84 
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;
91 	}
92 
93 	return ret;
94 }
95 
ssu100_getregister(struct usb_device * dev,unsigned short uart,unsigned short reg,u8 * data)96 static inline int ssu100_getregister(struct usb_device *dev,
97 				     unsigned short uart,
98 				     unsigned short reg,
99 				     u8 *data)
100 {
101 	int ret;
102 
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;
109 	}
110 
111 	return ret;
112 }
113 
114 
ssu100_setregister(struct usb_device * dev,unsigned short uart,unsigned short reg,u16 data)115 static inline int ssu100_setregister(struct usb_device *dev,
116 				     unsigned short uart,
117 				     unsigned short reg,
118 				     u16 data)
119 {
120 	u16 value = (data << 8) | reg;
121 
122 	return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
123 			       QT_SET_GET_REGISTER, 0x40, value, uart,
124 			       NULL, 0, 300);
125 
126 }
127 
128 #define set_mctrl(dev, set)		update_mctrl((dev), (set), 0)
129 #define clear_mctrl(dev, clear)	update_mctrl((dev), 0, (clear))
130 
131 /* these do not deal with device that have more than 1 port */
update_mctrl(struct usb_device * dev,unsigned int set,unsigned int clear)132 static inline int update_mctrl(struct usb_device *dev, unsigned int set,
133 			       unsigned int clear)
134 {
135 	unsigned urb_value;
136 	int result;
137 
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 */
141 	}
142 
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;
149 
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__);
153 
154 	return result;
155 }
156 
ssu100_initdevice(struct usb_device * dev)157 static int ssu100_initdevice(struct usb_device *dev)
158 {
159 	u8 *data;
160 	int result = 0;
161 
162 	data = kzalloc(3, GFP_KERNEL);
163 	if (!data)
164 		return -ENOMEM;
165 
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;
170 	}
171 
172 	data[1] &= ~FULLPWRBIT;
173 
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;
178 	}
179 
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;
184 	}
185 
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;
190 	}
191 
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;
196 	}
197 
198 	data[0] &= ~(RR_BITS | DUPMODE_BITS);
199 	data[0] |= CLKS_X4;
200 	data[1] &= ~(LOOPMODE_BITS);
201 	data[1] |= RS232_MODE;
202 
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;
207 	}
208 
209 out:	kfree(data);
210 	return result;
211 
212 }
213 
214 
ssu100_set_termios(struct tty_struct * tty,struct usb_serial_port * port,struct ktermios * old_termios)215 static void ssu100_set_termios(struct tty_struct *tty,
216 			       struct usb_serial_port *port,
217 			       struct ktermios *old_termios)
218 {
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;
225 
226 	if (cflag & PARENB) {
227 		if (cflag & PARODD)
228 			urb_value |= UART_LCR_PARITY;
229 		else
230 			urb_value |= SERIAL_EVEN_PARITY;
231 	}
232 
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;
247 	}
248 
249 	baud = tty_get_baud_rate(tty);
250 	if (!baud)
251 		baud = 9600;
252 
253 	dev_dbg(&port->dev, "%s - got baud = %d\n", __func__, baud);
254 
255 
256 	divisor = MAX_BAUD_RATE / baud;
257 	remainder = MAX_BAUD_RATE % baud;
258 	if (((remainder * 2) >= baud) && (baud != 110))
259 		divisor++;
260 
261 	urb_value = urb_value << 8;
262 
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__);
266 
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__);
275 
276 	if (I_IXOFF(tty) || I_IXON(tty)) {
277 		u16 x = ((u16)(START_CHAR(tty) << 8) | (u16)(STOP_CHAR(tty)));
278 
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);
284 
285 	if (result < 0)
286 		dev_dbg(&port->dev, "%s - set SW flow control failed\n", __func__);
287 
288 }
289 
290 
ssu100_open(struct tty_struct * tty,struct usb_serial_port * port)291 static int ssu100_open(struct tty_struct *tty, struct usb_serial_port *port)
292 {
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;
298 
299 	data = kzalloc(2, GFP_KERNEL);
300 	if (!data)
301 		return -ENOMEM;
302 
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;
313 	}
314 
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);
319 
320 	kfree(data);
321 
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__);
326 
327 	if (tty)
328 		ssu100_set_termios(tty, port, &tty->termios);
329 
330 	return usb_serial_generic_open(tty, port);
331 }
332 
get_serial_info(struct usb_serial_port * port,struct serial_struct __user * retinfo)333 static int get_serial_info(struct usb_serial_port *port,
334 			   struct serial_struct __user *retinfo)
335 {
336 	struct serial_struct tmp;
337 
338 	if (!retinfo)
339 		return -EFAULT;
340 
341 	memset(&tmp, 0, sizeof(tmp));
342 	tmp.line		= port->minor;
343 	tmp.port		= 0;
344 	tmp.irq			= 0;
345 	tmp.flags		= ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
346 	tmp.xmit_fifo_size	= port->bulk_out_size;
347 	tmp.baud_base		= 9600;
348 	tmp.close_delay		= 5*HZ;
349 	tmp.closing_wait	= 30*HZ;
350 
351 	if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
352 		return -EFAULT;
353 	return 0;
354 }
355 
ssu100_ioctl(struct tty_struct * tty,unsigned int cmd,unsigned long arg)356 static int ssu100_ioctl(struct tty_struct *tty,
357 		    unsigned int cmd, unsigned long arg)
358 {
359 	struct usb_serial_port *port = tty->driver_data;
360 
361 	switch (cmd) {
362 	case TIOCGSERIAL:
363 		return get_serial_info(port,
364 				       (struct serial_struct __user *) arg);
365 	default:
366 		break;
367 	}
368 
369 	return -ENOIOCTLCMD;
370 }
371 
ssu100_attach(struct usb_serial * serial)372 static int ssu100_attach(struct usb_serial *serial)
373 {
374 	return ssu100_initdevice(serial->dev);
375 }
376 
ssu100_port_probe(struct usb_serial_port * port)377 static int ssu100_port_probe(struct usb_serial_port *port)
378 {
379 	struct ssu100_port_private *priv;
380 
381 	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
382 	if (!priv)
383 		return -ENOMEM;
384 
385 	spin_lock_init(&priv->status_lock);
386 
387 	usb_set_serial_port_data(port, priv);
388 
389 	return 0;
390 }
391 
ssu100_port_remove(struct usb_serial_port * port)392 static int ssu100_port_remove(struct usb_serial_port *port)
393 {
394 	struct ssu100_port_private *priv;
395 
396 	priv = usb_get_serial_port_data(port);
397 	kfree(priv);
398 
399 	return 0;
400 }
401 
ssu100_tiocmget(struct tty_struct * tty)402 static int ssu100_tiocmget(struct tty_struct *tty)
403 {
404 	struct usb_serial_port *port = tty->driver_data;
405 	struct usb_device *dev = port->serial->dev;
406 	u8 *d;
407 	int r;
408 
409 	d = kzalloc(2, GFP_KERNEL);
410 	if (!d)
411 		return -ENOMEM;
412 
413 	r = ssu100_getregister(dev, 0, UART_MCR, d);
414 	if (r < 0)
415 		goto mget_out;
416 
417 	r = ssu100_getregister(dev, 0, UART_MSR, d+1);
418 	if (r < 0)
419 		goto mget_out;
420 
421 	r = (d[0] & UART_MCR_DTR ? TIOCM_DTR : 0) |
422 		(d[0] & UART_MCR_RTS ? TIOCM_RTS : 0) |
423 		(d[1] & UART_MSR_CTS ? TIOCM_CTS : 0) |
424 		(d[1] & UART_MSR_DCD ? TIOCM_CAR : 0) |
425 		(d[1] & UART_MSR_RI ? TIOCM_RI : 0) |
426 		(d[1] & UART_MSR_DSR ? TIOCM_DSR : 0);
427 
428 mget_out:
429 	kfree(d);
430 	return r;
431 }
432 
ssu100_tiocmset(struct tty_struct * tty,unsigned int set,unsigned int clear)433 static int ssu100_tiocmset(struct tty_struct *tty,
434 			   unsigned int set, unsigned int clear)
435 {
436 	struct usb_serial_port *port = tty->driver_data;
437 	struct usb_device *dev = port->serial->dev;
438 
439 	return update_mctrl(dev, set, clear);
440 }
441 
ssu100_dtr_rts(struct usb_serial_port * port,int on)442 static void ssu100_dtr_rts(struct usb_serial_port *port, int on)
443 {
444 	struct usb_device *dev = port->serial->dev;
445 
446 	/* Disable flow control */
447 	if (!on) {
448 		if (ssu100_setregister(dev, 0, UART_MCR, 0) < 0)
449 			dev_err(&port->dev, "error from flowcontrol urb\n");
450 	}
451 	/* drop RTS and DTR */
452 	if (on)
453 		set_mctrl(dev, TIOCM_DTR | TIOCM_RTS);
454 	else
455 		clear_mctrl(dev, TIOCM_DTR | TIOCM_RTS);
456 }
457 
ssu100_update_msr(struct usb_serial_port * port,u8 msr)458 static void ssu100_update_msr(struct usb_serial_port *port, u8 msr)
459 {
460 	struct ssu100_port_private *priv = usb_get_serial_port_data(port);
461 	unsigned long flags;
462 
463 	spin_lock_irqsave(&priv->status_lock, flags);
464 	priv->shadowMSR = msr;
465 	spin_unlock_irqrestore(&priv->status_lock, flags);
466 
467 	if (msr & UART_MSR_ANY_DELTA) {
468 		/* update input line counters */
469 		if (msr & UART_MSR_DCTS)
470 			port->icount.cts++;
471 		if (msr & UART_MSR_DDSR)
472 			port->icount.dsr++;
473 		if (msr & UART_MSR_DDCD)
474 			port->icount.dcd++;
475 		if (msr & UART_MSR_TERI)
476 			port->icount.rng++;
477 		wake_up_interruptible(&port->port.delta_msr_wait);
478 	}
479 }
480 
ssu100_update_lsr(struct usb_serial_port * port,u8 lsr,char * tty_flag)481 static void ssu100_update_lsr(struct usb_serial_port *port, u8 lsr,
482 			      char *tty_flag)
483 {
484 	struct ssu100_port_private *priv = usb_get_serial_port_data(port);
485 	unsigned long flags;
486 
487 	spin_lock_irqsave(&priv->status_lock, flags);
488 	priv->shadowLSR = lsr;
489 	spin_unlock_irqrestore(&priv->status_lock, flags);
490 
491 	*tty_flag = TTY_NORMAL;
492 	if (lsr & UART_LSR_BRK_ERROR_BITS) {
493 		/* we always want to update icount, but we only want to
494 		 * update tty_flag for one case */
495 		if (lsr & UART_LSR_BI) {
496 			port->icount.brk++;
497 			*tty_flag = TTY_BREAK;
498 			usb_serial_handle_break(port);
499 		}
500 		if (lsr & UART_LSR_PE) {
501 			port->icount.parity++;
502 			if (*tty_flag == TTY_NORMAL)
503 				*tty_flag = TTY_PARITY;
504 		}
505 		if (lsr & UART_LSR_FE) {
506 			port->icount.frame++;
507 			if (*tty_flag == TTY_NORMAL)
508 				*tty_flag = TTY_FRAME;
509 		}
510 		if (lsr & UART_LSR_OE) {
511 			port->icount.overrun++;
512 			tty_insert_flip_char(&port->port, 0, TTY_OVERRUN);
513 		}
514 	}
515 
516 }
517 
ssu100_process_read_urb(struct urb * urb)518 static void ssu100_process_read_urb(struct urb *urb)
519 {
520 	struct usb_serial_port *port = urb->context;
521 	char *packet = (char *)urb->transfer_buffer;
522 	char flag = TTY_NORMAL;
523 	u32 len = urb->actual_length;
524 	int i;
525 	char *ch;
526 
527 	if ((len >= 4) &&
528 	    (packet[0] == 0x1b) && (packet[1] == 0x1b) &&
529 	    ((packet[2] == 0x00) || (packet[2] == 0x01))) {
530 		if (packet[2] == 0x00)
531 			ssu100_update_lsr(port, packet[3], &flag);
532 		if (packet[2] == 0x01)
533 			ssu100_update_msr(port, packet[3]);
534 
535 		len -= 4;
536 		ch = packet + 4;
537 	} else
538 		ch = packet;
539 
540 	if (!len)
541 		return;	/* status only */
542 
543 	if (port->port.console && port->sysrq) {
544 		for (i = 0; i < len; i++, ch++) {
545 			if (!usb_serial_handle_sysrq_char(port, *ch))
546 				tty_insert_flip_char(&port->port, *ch, flag);
547 		}
548 	} else
549 		tty_insert_flip_string_fixed_flag(&port->port, ch, flag, len);
550 
551 	tty_flip_buffer_push(&port->port);
552 }
553 
554 static struct usb_serial_driver ssu100_device = {
555 	.driver = {
556 		.owner = THIS_MODULE,
557 		.name = "ssu100",
558 	},
559 	.description	     = DRIVER_DESC,
560 	.id_table	     = id_table,
561 	.num_ports	     = 1,
562 	.open		     = ssu100_open,
563 	.attach              = ssu100_attach,
564 	.port_probe          = ssu100_port_probe,
565 	.port_remove         = ssu100_port_remove,
566 	.dtr_rts             = ssu100_dtr_rts,
567 	.process_read_urb    = ssu100_process_read_urb,
568 	.tiocmget            = ssu100_tiocmget,
569 	.tiocmset            = ssu100_tiocmset,
570 	.tiocmiwait          = usb_serial_generic_tiocmiwait,
571 	.get_icount	     = usb_serial_generic_get_icount,
572 	.ioctl               = ssu100_ioctl,
573 	.set_termios         = ssu100_set_termios,
574 };
575 
576 static struct usb_serial_driver * const serial_drivers[] = {
577 	&ssu100_device, NULL
578 };
579 
580 module_usb_serial_driver(serial_drivers, id_table);
581 
582 MODULE_DESCRIPTION(DRIVER_DESC);
583 MODULE_LICENSE("GPL");
584