1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25#include <linux/kernel.h>
26#include <linux/init.h>
27#include <linux/ioctl.h>
28#include <linux/tty.h>
29#include <linux/slab.h>
30#include <linux/tty_flip.h>
31#include <linux/module.h>
32#include <linux/usb.h>
33#include <linux/usb/serial.h>
34#include <linux/serial.h>
35#include <linux/serial_reg.h>
36#include <linux/uaccess.h>
37#include <linux/mutex.h>
38#include <linux/spinlock.h>
39
40#define DRIVER_AUTHOR "Bart Hartgers <bart.hartgers+ark3116@gmail.com>"
41#define DRIVER_DESC "USB ARK3116 serial/IrDA driver"
42#define DRIVER_DEV_DESC "ARK3116 RS232/IrDA"
43#define DRIVER_NAME "ark3116"
44
45
46#define ARK_TIMEOUT 1000
47
48static const struct usb_device_id id_table[] = {
49 { USB_DEVICE(0x6547, 0x0232) },
50 { USB_DEVICE(0x18ec, 0x3118) },
51 { },
52};
53MODULE_DEVICE_TABLE(usb, id_table);
54
55static int is_irda(struct usb_serial *serial)
56{
57 struct usb_device *dev = serial->dev;
58 if (le16_to_cpu(dev->descriptor.idVendor) == 0x18ec &&
59 le16_to_cpu(dev->descriptor.idProduct) == 0x3118)
60 return 1;
61 return 0;
62}
63
64struct ark3116_private {
65 struct async_icount icount;
66 int irda;
67
68
69 struct mutex hw_lock;
70
71 int quot;
72 __u32 lcr;
73 __u32 hcr;
74
75 __u32 mcr;
76
77
78 spinlock_t status_lock;
79 __u32 msr;
80 __u32 lsr;
81};
82
83static int ark3116_write_reg(struct usb_serial *serial,
84 unsigned reg, __u8 val)
85{
86 int result;
87
88 result = usb_control_msg(serial->dev,
89 usb_sndctrlpipe(serial->dev, 0),
90 0xfe, 0x40, val, reg,
91 NULL, 0, ARK_TIMEOUT);
92 return result;
93}
94
95static int ark3116_read_reg(struct usb_serial *serial,
96 unsigned reg, unsigned char *buf)
97{
98 int result;
99
100 result = usb_control_msg(serial->dev,
101 usb_rcvctrlpipe(serial->dev, 0),
102 0xfe, 0xc0, 0, reg,
103 buf, 1, ARK_TIMEOUT);
104 if (result < 0)
105 return result;
106 else
107 return buf[0];
108}
109
110static inline int calc_divisor(int bps)
111{
112
113
114
115
116
117 return (12000000 + 2*bps) / (4*bps);
118}
119
120static int ark3116_attach(struct usb_serial *serial)
121{
122
123 if ((serial->num_bulk_in == 0) ||
124 (serial->num_bulk_out == 0) ||
125 (serial->num_interrupt_in == 0)) {
126 dev_err(&serial->dev->dev,
127 "%s - missing endpoint - "
128 "bulk in: %d, bulk out: %d, int in %d\n",
129 KBUILD_MODNAME,
130 serial->num_bulk_in,
131 serial->num_bulk_out,
132 serial->num_interrupt_in);
133 return -EINVAL;
134 }
135
136 return 0;
137}
138
139static int ark3116_port_probe(struct usb_serial_port *port)
140{
141 struct usb_serial *serial = port->serial;
142 struct ark3116_private *priv;
143
144 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
145 if (!priv)
146 return -ENOMEM;
147
148 mutex_init(&priv->hw_lock);
149 spin_lock_init(&priv->status_lock);
150
151 priv->irda = is_irda(serial);
152
153 usb_set_serial_port_data(port, priv);
154
155
156 ark3116_write_reg(serial, UART_IER, 0);
157
158 ark3116_write_reg(serial, UART_FCR, 0);
159
160 priv->hcr = 0;
161 ark3116_write_reg(serial, 0x8 , 0);
162
163 priv->mcr = 0;
164 ark3116_write_reg(serial, UART_MCR, 0);
165
166 if (!(priv->irda)) {
167 ark3116_write_reg(serial, 0xb , 0);
168 } else {
169 ark3116_write_reg(serial, 0xb , 1);
170 ark3116_write_reg(serial, 0xc , 0);
171 ark3116_write_reg(serial, 0xd , 0x41);
172 ark3116_write_reg(serial, 0xa , 1);
173 }
174
175
176 ark3116_write_reg(serial, UART_LCR, UART_LCR_DLAB);
177
178
179 priv->quot = calc_divisor(9600);
180 ark3116_write_reg(serial, UART_DLL, priv->quot & 0xff);
181 ark3116_write_reg(serial, UART_DLM, (priv->quot>>8) & 0xff);
182
183 priv->lcr = UART_LCR_WLEN8;
184 ark3116_write_reg(serial, UART_LCR, UART_LCR_WLEN8);
185
186 ark3116_write_reg(serial, 0xe, 0);
187
188 if (priv->irda)
189 ark3116_write_reg(serial, 0x9, 0);
190
191 dev_info(&serial->dev->dev,
192 "%s using %s mode\n",
193 KBUILD_MODNAME,
194 priv->irda ? "IrDA" : "RS232");
195 return 0;
196}
197
198static int ark3116_port_remove(struct usb_serial_port *port)
199{
200 struct ark3116_private *priv = usb_get_serial_port_data(port);
201
202
203 mutex_destroy(&priv->hw_lock);
204 kfree(priv);
205
206 return 0;
207}
208
209static void ark3116_init_termios(struct tty_struct *tty)
210{
211 struct ktermios *termios = &tty->termios;
212 *termios = tty_std_termios;
213 termios->c_cflag = B9600 | CS8
214 | CREAD | HUPCL | CLOCAL;
215 termios->c_ispeed = 9600;
216 termios->c_ospeed = 9600;
217}
218
219static void ark3116_set_termios(struct tty_struct *tty,
220 struct usb_serial_port *port,
221 struct ktermios *old_termios)
222{
223 struct usb_serial *serial = port->serial;
224 struct ark3116_private *priv = usb_get_serial_port_data(port);
225 struct ktermios *termios = &tty->termios;
226 unsigned int cflag = termios->c_cflag;
227 int bps = tty_get_baud_rate(tty);
228 int quot;
229 __u8 lcr, hcr, eval;
230
231
232 switch (cflag & CSIZE) {
233 case CS5:
234 lcr = UART_LCR_WLEN5;
235 break;
236 case CS6:
237 lcr = UART_LCR_WLEN6;
238 break;
239 case CS7:
240 lcr = UART_LCR_WLEN7;
241 break;
242 default:
243 case CS8:
244 lcr = UART_LCR_WLEN8;
245 break;
246 }
247 if (cflag & CSTOPB)
248 lcr |= UART_LCR_STOP;
249 if (cflag & PARENB)
250 lcr |= UART_LCR_PARITY;
251 if (!(cflag & PARODD))
252 lcr |= UART_LCR_EPAR;
253#ifdef CMSPAR
254 if (cflag & CMSPAR)
255 lcr |= UART_LCR_SPAR;
256#endif
257
258 hcr = (cflag & CRTSCTS) ? 0x03 : 0x00;
259
260
261 dev_dbg(&port->dev, "%s - setting bps to %d\n", __func__, bps);
262 eval = 0;
263 switch (bps) {
264 case 0:
265 quot = calc_divisor(9600);
266 break;
267 default:
268 if ((bps < 75) || (bps > 3000000))
269 bps = 9600;
270 quot = calc_divisor(bps);
271 break;
272 case 460800:
273 eval = 1;
274 quot = calc_divisor(bps);
275 break;
276 case 921600:
277 eval = 2;
278 quot = calc_divisor(bps);
279 break;
280 }
281
282
283 mutex_lock(&priv->hw_lock);
284
285
286 lcr |= (priv->lcr & UART_LCR_SBC);
287
288 dev_dbg(&port->dev, "%s - setting hcr:0x%02x,lcr:0x%02x,quot:%d\n",
289 __func__, hcr, lcr, quot);
290
291
292 if (priv->hcr != hcr) {
293 priv->hcr = hcr;
294 ark3116_write_reg(serial, 0x8, hcr);
295 }
296
297
298 if (priv->quot != quot) {
299 priv->quot = quot;
300 priv->lcr = lcr;
301
302
303
304
305 ark3116_write_reg(serial, UART_FCR, 0);
306
307 ark3116_write_reg(serial, UART_LCR,
308 lcr|UART_LCR_DLAB);
309 ark3116_write_reg(serial, UART_DLL, quot & 0xff);
310 ark3116_write_reg(serial, UART_DLM, (quot>>8) & 0xff);
311
312
313 ark3116_write_reg(serial, UART_LCR, lcr);
314
315
316
317 ark3116_write_reg(serial, 0xe, eval);
318
319
320 ark3116_write_reg(serial, UART_FCR, UART_FCR_DMA_SELECT);
321 } else if (priv->lcr != lcr) {
322 priv->lcr = lcr;
323 ark3116_write_reg(serial, UART_LCR, lcr);
324 }
325
326 mutex_unlock(&priv->hw_lock);
327
328
329 if (I_IXOFF(tty) || I_IXON(tty)) {
330 dev_warn(&serial->dev->dev,
331 "%s: don't know how to do software flow control\n",
332 KBUILD_MODNAME);
333 }
334
335
336 if (tty_termios_baud_rate(termios))
337 tty_termios_encode_baud_rate(termios, bps, bps);
338}
339
340static void ark3116_close(struct usb_serial_port *port)
341{
342 struct usb_serial *serial = port->serial;
343
344 if (serial->dev) {
345
346 ark3116_write_reg(serial, UART_FCR, 0);
347
348
349 ark3116_write_reg(serial, UART_IER, 0);
350
351 usb_serial_generic_close(port);
352 if (serial->num_interrupt_in)
353 usb_kill_urb(port->interrupt_in_urb);
354 }
355
356}
357
358static int ark3116_open(struct tty_struct *tty, struct usb_serial_port *port)
359{
360 struct ark3116_private *priv = usb_get_serial_port_data(port);
361 struct usb_serial *serial = port->serial;
362 unsigned char *buf;
363 int result;
364
365 buf = kmalloc(1, GFP_KERNEL);
366 if (buf == NULL)
367 return -ENOMEM;
368
369 result = usb_serial_generic_open(tty, port);
370 if (result) {
371 dev_dbg(&port->dev,
372 "%s - usb_serial_generic_open failed: %d\n",
373 __func__, result);
374 goto err_out;
375 }
376
377
378 ark3116_read_reg(serial, UART_RX, buf);
379
380
381 priv->msr = ark3116_read_reg(serial, UART_MSR, buf);
382
383 priv->lsr = ark3116_read_reg(serial, UART_LSR, buf);
384
385 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
386 if (result) {
387 dev_err(&port->dev, "submit irq_in urb failed %d\n",
388 result);
389 ark3116_close(port);
390 goto err_out;
391 }
392
393
394 ark3116_write_reg(port->serial, UART_IER, UART_IER_MSI|UART_IER_RLSI);
395
396
397 ark3116_write_reg(port->serial, UART_FCR, UART_FCR_DMA_SELECT);
398
399
400 if (tty)
401 ark3116_set_termios(tty, port, NULL);
402
403err_out:
404 kfree(buf);
405 return result;
406}
407
408static int ark3116_get_icount(struct tty_struct *tty,
409 struct serial_icounter_struct *icount)
410{
411 struct usb_serial_port *port = tty->driver_data;
412 struct ark3116_private *priv = usb_get_serial_port_data(port);
413 struct async_icount cnow = priv->icount;
414 icount->cts = cnow.cts;
415 icount->dsr = cnow.dsr;
416 icount->rng = cnow.rng;
417 icount->dcd = cnow.dcd;
418 icount->rx = cnow.rx;
419 icount->tx = cnow.tx;
420 icount->frame = cnow.frame;
421 icount->overrun = cnow.overrun;
422 icount->parity = cnow.parity;
423 icount->brk = cnow.brk;
424 icount->buf_overrun = cnow.buf_overrun;
425 return 0;
426}
427
428static int ark3116_ioctl(struct tty_struct *tty,
429 unsigned int cmd, unsigned long arg)
430{
431 struct usb_serial_port *port = tty->driver_data;
432 struct ark3116_private *priv = usb_get_serial_port_data(port);
433 struct serial_struct serstruct;
434 void __user *user_arg = (void __user *)arg;
435
436 switch (cmd) {
437 case TIOCGSERIAL:
438
439 memset(&serstruct, 0, sizeof(serstruct));
440 serstruct.type = PORT_16654;
441 serstruct.line = port->serial->minor;
442 serstruct.port = port->number;
443 serstruct.custom_divisor = 0;
444 serstruct.baud_base = 460800;
445
446 if (copy_to_user(user_arg, &serstruct, sizeof(serstruct)))
447 return -EFAULT;
448
449 return 0;
450 case TIOCSSERIAL:
451 if (copy_from_user(&serstruct, user_arg, sizeof(serstruct)))
452 return -EFAULT;
453 return 0;
454 case TIOCMIWAIT:
455 for (;;) {
456 struct async_icount prev = priv->icount;
457 interruptible_sleep_on(&port->delta_msr_wait);
458
459 if (signal_pending(current))
460 return -ERESTARTSYS;
461
462 if (port->serial->disconnected)
463 return -EIO;
464
465 if ((prev.rng == priv->icount.rng) &&
466 (prev.dsr == priv->icount.dsr) &&
467 (prev.dcd == priv->icount.dcd) &&
468 (prev.cts == priv->icount.cts))
469 return -EIO;
470 if ((arg & TIOCM_RNG &&
471 (prev.rng != priv->icount.rng)) ||
472 (arg & TIOCM_DSR &&
473 (prev.dsr != priv->icount.dsr)) ||
474 (arg & TIOCM_CD &&
475 (prev.dcd != priv->icount.dcd)) ||
476 (arg & TIOCM_CTS &&
477 (prev.cts != priv->icount.cts)))
478 return 0;
479 }
480 break;
481 }
482
483 return -ENOIOCTLCMD;
484}
485
486static int ark3116_tiocmget(struct tty_struct *tty)
487{
488 struct usb_serial_port *port = tty->driver_data;
489 struct ark3116_private *priv = usb_get_serial_port_data(port);
490 __u32 status;
491 __u32 ctrl;
492 unsigned long flags;
493
494 mutex_lock(&priv->hw_lock);
495 ctrl = priv->mcr;
496 mutex_unlock(&priv->hw_lock);
497
498 spin_lock_irqsave(&priv->status_lock, flags);
499 status = priv->msr;
500 spin_unlock_irqrestore(&priv->status_lock, flags);
501
502 return (status & UART_MSR_DSR ? TIOCM_DSR : 0) |
503 (status & UART_MSR_CTS ? TIOCM_CTS : 0) |
504 (status & UART_MSR_RI ? TIOCM_RI : 0) |
505 (status & UART_MSR_DCD ? TIOCM_CD : 0) |
506 (ctrl & UART_MCR_DTR ? TIOCM_DTR : 0) |
507 (ctrl & UART_MCR_RTS ? TIOCM_RTS : 0) |
508 (ctrl & UART_MCR_OUT1 ? TIOCM_OUT1 : 0) |
509 (ctrl & UART_MCR_OUT2 ? TIOCM_OUT2 : 0);
510}
511
512static int ark3116_tiocmset(struct tty_struct *tty,
513 unsigned set, unsigned clr)
514{
515 struct usb_serial_port *port = tty->driver_data;
516 struct ark3116_private *priv = usb_get_serial_port_data(port);
517
518
519
520
521
522 mutex_lock(&priv->hw_lock);
523
524 if (set & TIOCM_RTS)
525 priv->mcr |= UART_MCR_RTS;
526 if (set & TIOCM_DTR)
527 priv->mcr |= UART_MCR_DTR;
528 if (set & TIOCM_OUT1)
529 priv->mcr |= UART_MCR_OUT1;
530 if (set & TIOCM_OUT2)
531 priv->mcr |= UART_MCR_OUT2;
532 if (clr & TIOCM_RTS)
533 priv->mcr &= ~UART_MCR_RTS;
534 if (clr & TIOCM_DTR)
535 priv->mcr &= ~UART_MCR_DTR;
536 if (clr & TIOCM_OUT1)
537 priv->mcr &= ~UART_MCR_OUT1;
538 if (clr & TIOCM_OUT2)
539 priv->mcr &= ~UART_MCR_OUT2;
540
541 ark3116_write_reg(port->serial, UART_MCR, priv->mcr);
542
543 mutex_unlock(&priv->hw_lock);
544
545 return 0;
546}
547
548static void ark3116_break_ctl(struct tty_struct *tty, int break_state)
549{
550 struct usb_serial_port *port = tty->driver_data;
551 struct ark3116_private *priv = usb_get_serial_port_data(port);
552
553
554 mutex_lock(&priv->hw_lock);
555
556 if (break_state)
557 priv->lcr |= UART_LCR_SBC;
558 else
559 priv->lcr &= ~UART_LCR_SBC;
560
561 ark3116_write_reg(port->serial, UART_LCR, priv->lcr);
562
563 mutex_unlock(&priv->hw_lock);
564}
565
566static void ark3116_update_msr(struct usb_serial_port *port, __u8 msr)
567{
568 struct ark3116_private *priv = usb_get_serial_port_data(port);
569 unsigned long flags;
570
571 spin_lock_irqsave(&priv->status_lock, flags);
572 priv->msr = msr;
573 spin_unlock_irqrestore(&priv->status_lock, flags);
574
575 if (msr & UART_MSR_ANY_DELTA) {
576
577 if (msr & UART_MSR_DCTS)
578 priv->icount.cts++;
579 if (msr & UART_MSR_DDSR)
580 priv->icount.dsr++;
581 if (msr & UART_MSR_DDCD)
582 priv->icount.dcd++;
583 if (msr & UART_MSR_TERI)
584 priv->icount.rng++;
585 wake_up_interruptible(&port->delta_msr_wait);
586 }
587}
588
589static void ark3116_update_lsr(struct usb_serial_port *port, __u8 lsr)
590{
591 struct ark3116_private *priv = usb_get_serial_port_data(port);
592 unsigned long flags;
593
594 spin_lock_irqsave(&priv->status_lock, flags);
595
596 priv->lsr |= lsr;
597 spin_unlock_irqrestore(&priv->status_lock, flags);
598
599 if (lsr&UART_LSR_BRK_ERROR_BITS) {
600 if (lsr & UART_LSR_BI)
601 priv->icount.brk++;
602 if (lsr & UART_LSR_FE)
603 priv->icount.frame++;
604 if (lsr & UART_LSR_PE)
605 priv->icount.parity++;
606 if (lsr & UART_LSR_OE)
607 priv->icount.overrun++;
608 }
609}
610
611static void ark3116_read_int_callback(struct urb *urb)
612{
613 struct usb_serial_port *port = urb->context;
614 int status = urb->status;
615 const __u8 *data = urb->transfer_buffer;
616 int result;
617
618 switch (status) {
619 case -ECONNRESET:
620 case -ENOENT:
621 case -ESHUTDOWN:
622
623 dev_dbg(&port->dev, "%s - urb shutting down with status: %d\n",
624 __func__, status);
625 return;
626 default:
627 dev_dbg(&port->dev, "%s - nonzero urb status received: %d\n",
628 __func__, status);
629 break;
630 case 0:
631
632 if ((urb->actual_length == 4) && (data[0] == 0xe8)) {
633 const __u8 id = data[1]&UART_IIR_ID;
634 dev_dbg(&port->dev, "%s: iir=%02x\n", __func__, data[1]);
635 if (id == UART_IIR_MSI) {
636 dev_dbg(&port->dev, "%s: msr=%02x\n",
637 __func__, data[3]);
638 ark3116_update_msr(port, data[3]);
639 break;
640 } else if (id == UART_IIR_RLSI) {
641 dev_dbg(&port->dev, "%s: lsr=%02x\n",
642 __func__, data[2]);
643 ark3116_update_lsr(port, data[2]);
644 break;
645 }
646 }
647
648
649
650 usb_serial_debug_data(&port->dev, __func__,
651 urb->actual_length,
652 urb->transfer_buffer);
653 break;
654 }
655
656 result = usb_submit_urb(urb, GFP_ATOMIC);
657 if (result)
658 dev_err(&urb->dev->dev,
659 "%s - Error %d submitting interrupt urb\n",
660 __func__, result);
661}
662
663
664
665
666
667
668
669
670
671
672
673
674
675static void ark3116_process_read_urb(struct urb *urb)
676{
677 struct usb_serial_port *port = urb->context;
678 struct ark3116_private *priv = usb_get_serial_port_data(port);
679 unsigned char *data = urb->transfer_buffer;
680 char tty_flag = TTY_NORMAL;
681 unsigned long flags;
682 __u32 lsr;
683
684
685 spin_lock_irqsave(&priv->status_lock, flags);
686 lsr = priv->lsr;
687 priv->lsr &= ~UART_LSR_BRK_ERROR_BITS;
688 spin_unlock_irqrestore(&priv->status_lock, flags);
689
690 if (!urb->actual_length)
691 return;
692
693 if (lsr & UART_LSR_BRK_ERROR_BITS) {
694 if (lsr & UART_LSR_BI)
695 tty_flag = TTY_BREAK;
696 else if (lsr & UART_LSR_PE)
697 tty_flag = TTY_PARITY;
698 else if (lsr & UART_LSR_FE)
699 tty_flag = TTY_FRAME;
700
701
702 if (lsr & UART_LSR_OE)
703 tty_insert_flip_char(&port->port, 0, TTY_OVERRUN);
704 }
705 tty_insert_flip_string_fixed_flag(&port->port, data, tty_flag,
706 urb->actual_length);
707 tty_flip_buffer_push(&port->port);
708}
709
710static struct usb_serial_driver ark3116_device = {
711 .driver = {
712 .owner = THIS_MODULE,
713 .name = "ark3116",
714 },
715 .id_table = id_table,
716 .num_ports = 1,
717 .attach = ark3116_attach,
718 .port_probe = ark3116_port_probe,
719 .port_remove = ark3116_port_remove,
720 .set_termios = ark3116_set_termios,
721 .init_termios = ark3116_init_termios,
722 .ioctl = ark3116_ioctl,
723 .tiocmget = ark3116_tiocmget,
724 .tiocmset = ark3116_tiocmset,
725 .get_icount = ark3116_get_icount,
726 .open = ark3116_open,
727 .close = ark3116_close,
728 .break_ctl = ark3116_break_ctl,
729 .read_int_callback = ark3116_read_int_callback,
730 .process_read_urb = ark3116_process_read_urb,
731};
732
733static struct usb_serial_driver * const serial_drivers[] = {
734 &ark3116_device, NULL
735};
736
737module_usb_serial_driver(serial_drivers, id_table);
738
739MODULE_LICENSE("GPL");
740
741MODULE_AUTHOR(DRIVER_AUTHOR);
742MODULE_DESCRIPTION(DRIVER_DESC);
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844