0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021 #include <linux/kernel.h>
0022 #include <linux/ioctl.h>
0023 #include <linux/tty.h>
0024 #include <linux/slab.h>
0025 #include <linux/tty_flip.h>
0026 #include <linux/module.h>
0027 #include <linux/usb.h>
0028 #include <linux/usb/serial.h>
0029 #include <linux/serial.h>
0030 #include <linux/serial_reg.h>
0031 #include <linux/uaccess.h>
0032 #include <linux/mutex.h>
0033 #include <linux/spinlock.h>
0034
0035 #define DRIVER_AUTHOR "Bart Hartgers <bart.hartgers+ark3116@gmail.com>"
0036 #define DRIVER_DESC "USB ARK3116 serial/IrDA driver"
0037 #define DRIVER_DEV_DESC "ARK3116 RS232/IrDA"
0038 #define DRIVER_NAME "ark3116"
0039
0040
0041 #define ARK_TIMEOUT 1000
0042
0043 static const struct usb_device_id id_table[] = {
0044 { USB_DEVICE(0x6547, 0x0232) },
0045 { USB_DEVICE(0x18ec, 0x3118) },
0046 { },
0047 };
0048 MODULE_DEVICE_TABLE(usb, id_table);
0049
0050 static int is_irda(struct usb_serial *serial)
0051 {
0052 struct usb_device *dev = serial->dev;
0053 if (le16_to_cpu(dev->descriptor.idVendor) == 0x18ec &&
0054 le16_to_cpu(dev->descriptor.idProduct) == 0x3118)
0055 return 1;
0056 return 0;
0057 }
0058
0059 struct ark3116_private {
0060 int irda;
0061
0062
0063 struct mutex hw_lock;
0064
0065 int quot;
0066 __u32 lcr;
0067 __u32 hcr;
0068
0069 __u32 mcr;
0070
0071
0072 spinlock_t status_lock;
0073 __u32 msr;
0074 __u32 lsr;
0075 };
0076
0077 static int ark3116_write_reg(struct usb_serial *serial,
0078 unsigned reg, __u8 val)
0079 {
0080 int result;
0081
0082 result = usb_control_msg(serial->dev,
0083 usb_sndctrlpipe(serial->dev, 0),
0084 0xfe, 0x40, val, reg,
0085 NULL, 0, ARK_TIMEOUT);
0086 if (result)
0087 return result;
0088
0089 return 0;
0090 }
0091
0092 static int ark3116_read_reg(struct usb_serial *serial,
0093 unsigned reg, unsigned char *buf)
0094 {
0095 int result;
0096
0097 result = usb_control_msg(serial->dev,
0098 usb_rcvctrlpipe(serial->dev, 0),
0099 0xfe, 0xc0, 0, reg,
0100 buf, 1, ARK_TIMEOUT);
0101 if (result < 1) {
0102 dev_err(&serial->interface->dev,
0103 "failed to read register %u: %d\n",
0104 reg, result);
0105 if (result >= 0)
0106 result = -EIO;
0107
0108 return result;
0109 }
0110
0111 return 0;
0112 }
0113
0114 static inline int calc_divisor(int bps)
0115 {
0116
0117
0118
0119
0120
0121 return (12000000 + 2*bps) / (4*bps);
0122 }
0123
0124 static int ark3116_port_probe(struct usb_serial_port *port)
0125 {
0126 struct usb_serial *serial = port->serial;
0127 struct ark3116_private *priv;
0128
0129 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
0130 if (!priv)
0131 return -ENOMEM;
0132
0133 mutex_init(&priv->hw_lock);
0134 spin_lock_init(&priv->status_lock);
0135
0136 priv->irda = is_irda(serial);
0137
0138 usb_set_serial_port_data(port, priv);
0139
0140
0141 ark3116_write_reg(serial, UART_IER, 0);
0142
0143 ark3116_write_reg(serial, UART_FCR, 0);
0144
0145 priv->hcr = 0;
0146 ark3116_write_reg(serial, 0x8 , 0);
0147
0148 priv->mcr = 0;
0149 ark3116_write_reg(serial, UART_MCR, 0);
0150
0151 if (!(priv->irda)) {
0152 ark3116_write_reg(serial, 0xb , 0);
0153 } else {
0154 ark3116_write_reg(serial, 0xb , 1);
0155 ark3116_write_reg(serial, 0xc , 0);
0156 ark3116_write_reg(serial, 0xd , 0x41);
0157 ark3116_write_reg(serial, 0xa , 1);
0158 }
0159
0160
0161 ark3116_write_reg(serial, UART_LCR, UART_LCR_DLAB);
0162
0163
0164 priv->quot = calc_divisor(9600);
0165 ark3116_write_reg(serial, UART_DLL, priv->quot & 0xff);
0166 ark3116_write_reg(serial, UART_DLM, (priv->quot>>8) & 0xff);
0167
0168 priv->lcr = UART_LCR_WLEN8;
0169 ark3116_write_reg(serial, UART_LCR, UART_LCR_WLEN8);
0170
0171 ark3116_write_reg(serial, 0xe, 0);
0172
0173 if (priv->irda)
0174 ark3116_write_reg(serial, 0x9, 0);
0175
0176 dev_info(&port->dev, "using %s mode\n", priv->irda ? "IrDA" : "RS232");
0177
0178 return 0;
0179 }
0180
0181 static void ark3116_port_remove(struct usb_serial_port *port)
0182 {
0183 struct ark3116_private *priv = usb_get_serial_port_data(port);
0184
0185
0186 mutex_destroy(&priv->hw_lock);
0187 kfree(priv);
0188 }
0189
0190 static void ark3116_set_termios(struct tty_struct *tty,
0191 struct usb_serial_port *port,
0192 struct ktermios *old_termios)
0193 {
0194 struct usb_serial *serial = port->serial;
0195 struct ark3116_private *priv = usb_get_serial_port_data(port);
0196 struct ktermios *termios = &tty->termios;
0197 unsigned int cflag = termios->c_cflag;
0198 int bps = tty_get_baud_rate(tty);
0199 int quot;
0200 __u8 lcr, hcr, eval;
0201
0202
0203 lcr = UART_LCR_WLEN(tty_get_char_size(cflag));
0204
0205 if (cflag & CSTOPB)
0206 lcr |= UART_LCR_STOP;
0207 if (cflag & PARENB)
0208 lcr |= UART_LCR_PARITY;
0209 if (!(cflag & PARODD))
0210 lcr |= UART_LCR_EPAR;
0211 if (cflag & CMSPAR)
0212 lcr |= UART_LCR_SPAR;
0213
0214
0215 hcr = (cflag & CRTSCTS) ? 0x03 : 0x00;
0216
0217
0218 dev_dbg(&port->dev, "%s - setting bps to %d\n", __func__, bps);
0219 eval = 0;
0220 switch (bps) {
0221 case 0:
0222 quot = calc_divisor(9600);
0223 break;
0224 default:
0225 if ((bps < 75) || (bps > 3000000))
0226 bps = 9600;
0227 quot = calc_divisor(bps);
0228 break;
0229 case 460800:
0230 eval = 1;
0231 quot = calc_divisor(bps);
0232 break;
0233 case 921600:
0234 eval = 2;
0235 quot = calc_divisor(bps);
0236 break;
0237 }
0238
0239
0240 mutex_lock(&priv->hw_lock);
0241
0242
0243 lcr |= (priv->lcr & UART_LCR_SBC);
0244
0245 dev_dbg(&port->dev, "%s - setting hcr:0x%02x,lcr:0x%02x,quot:%d\n",
0246 __func__, hcr, lcr, quot);
0247
0248
0249 if (priv->hcr != hcr) {
0250 priv->hcr = hcr;
0251 ark3116_write_reg(serial, 0x8, hcr);
0252 }
0253
0254
0255 if (priv->quot != quot) {
0256 priv->quot = quot;
0257 priv->lcr = lcr;
0258
0259
0260
0261
0262 ark3116_write_reg(serial, UART_FCR, 0);
0263
0264 ark3116_write_reg(serial, UART_LCR,
0265 lcr|UART_LCR_DLAB);
0266 ark3116_write_reg(serial, UART_DLL, quot & 0xff);
0267 ark3116_write_reg(serial, UART_DLM, (quot>>8) & 0xff);
0268
0269
0270 ark3116_write_reg(serial, UART_LCR, lcr);
0271
0272
0273
0274 ark3116_write_reg(serial, 0xe, eval);
0275
0276
0277 ark3116_write_reg(serial, UART_FCR, UART_FCR_DMA_SELECT);
0278 } else if (priv->lcr != lcr) {
0279 priv->lcr = lcr;
0280 ark3116_write_reg(serial, UART_LCR, lcr);
0281 }
0282
0283 mutex_unlock(&priv->hw_lock);
0284
0285
0286 if (I_IXOFF(tty) || I_IXON(tty)) {
0287 dev_warn(&port->dev,
0288 "software flow control not implemented\n");
0289 }
0290
0291
0292 if (tty_termios_baud_rate(termios))
0293 tty_termios_encode_baud_rate(termios, bps, bps);
0294 }
0295
0296 static void ark3116_close(struct usb_serial_port *port)
0297 {
0298 struct usb_serial *serial = port->serial;
0299
0300
0301 ark3116_write_reg(serial, UART_FCR, 0);
0302
0303
0304 ark3116_write_reg(serial, UART_IER, 0);
0305
0306 usb_serial_generic_close(port);
0307
0308 usb_kill_urb(port->interrupt_in_urb);
0309 }
0310
0311 static int ark3116_open(struct tty_struct *tty, struct usb_serial_port *port)
0312 {
0313 struct ark3116_private *priv = usb_get_serial_port_data(port);
0314 struct usb_serial *serial = port->serial;
0315 unsigned char *buf;
0316 int result;
0317
0318 buf = kmalloc(1, GFP_KERNEL);
0319 if (buf == NULL)
0320 return -ENOMEM;
0321
0322 result = usb_serial_generic_open(tty, port);
0323 if (result) {
0324 dev_dbg(&port->dev,
0325 "%s - usb_serial_generic_open failed: %d\n",
0326 __func__, result);
0327 goto err_free;
0328 }
0329
0330
0331 ark3116_read_reg(serial, UART_RX, buf);
0332
0333
0334 result = ark3116_read_reg(serial, UART_MSR, buf);
0335 if (result)
0336 goto err_close;
0337 priv->msr = *buf;
0338
0339
0340 result = ark3116_read_reg(serial, UART_LSR, buf);
0341 if (result)
0342 goto err_close;
0343 priv->lsr = *buf;
0344
0345 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
0346 if (result) {
0347 dev_err(&port->dev, "submit irq_in urb failed %d\n",
0348 result);
0349 goto err_close;
0350 }
0351
0352
0353 ark3116_write_reg(port->serial, UART_IER, UART_IER_MSI|UART_IER_RLSI);
0354
0355
0356 ark3116_write_reg(port->serial, UART_FCR, UART_FCR_DMA_SELECT);
0357
0358
0359 if (tty)
0360 ark3116_set_termios(tty, port, NULL);
0361
0362 kfree(buf);
0363
0364 return 0;
0365
0366 err_close:
0367 usb_serial_generic_close(port);
0368 err_free:
0369 kfree(buf);
0370
0371 return result;
0372 }
0373
0374 static int ark3116_tiocmget(struct tty_struct *tty)
0375 {
0376 struct usb_serial_port *port = tty->driver_data;
0377 struct ark3116_private *priv = usb_get_serial_port_data(port);
0378 __u32 status;
0379 __u32 ctrl;
0380 unsigned long flags;
0381
0382 mutex_lock(&priv->hw_lock);
0383 ctrl = priv->mcr;
0384 mutex_unlock(&priv->hw_lock);
0385
0386 spin_lock_irqsave(&priv->status_lock, flags);
0387 status = priv->msr;
0388 spin_unlock_irqrestore(&priv->status_lock, flags);
0389
0390 return (status & UART_MSR_DSR ? TIOCM_DSR : 0) |
0391 (status & UART_MSR_CTS ? TIOCM_CTS : 0) |
0392 (status & UART_MSR_RI ? TIOCM_RI : 0) |
0393 (status & UART_MSR_DCD ? TIOCM_CD : 0) |
0394 (ctrl & UART_MCR_DTR ? TIOCM_DTR : 0) |
0395 (ctrl & UART_MCR_RTS ? TIOCM_RTS : 0) |
0396 (ctrl & UART_MCR_OUT1 ? TIOCM_OUT1 : 0) |
0397 (ctrl & UART_MCR_OUT2 ? TIOCM_OUT2 : 0);
0398 }
0399
0400 static int ark3116_tiocmset(struct tty_struct *tty,
0401 unsigned set, unsigned clr)
0402 {
0403 struct usb_serial_port *port = tty->driver_data;
0404 struct ark3116_private *priv = usb_get_serial_port_data(port);
0405
0406
0407
0408
0409
0410 mutex_lock(&priv->hw_lock);
0411
0412 if (set & TIOCM_RTS)
0413 priv->mcr |= UART_MCR_RTS;
0414 if (set & TIOCM_DTR)
0415 priv->mcr |= UART_MCR_DTR;
0416 if (set & TIOCM_OUT1)
0417 priv->mcr |= UART_MCR_OUT1;
0418 if (set & TIOCM_OUT2)
0419 priv->mcr |= UART_MCR_OUT2;
0420 if (clr & TIOCM_RTS)
0421 priv->mcr &= ~UART_MCR_RTS;
0422 if (clr & TIOCM_DTR)
0423 priv->mcr &= ~UART_MCR_DTR;
0424 if (clr & TIOCM_OUT1)
0425 priv->mcr &= ~UART_MCR_OUT1;
0426 if (clr & TIOCM_OUT2)
0427 priv->mcr &= ~UART_MCR_OUT2;
0428
0429 ark3116_write_reg(port->serial, UART_MCR, priv->mcr);
0430
0431 mutex_unlock(&priv->hw_lock);
0432
0433 return 0;
0434 }
0435
0436 static void ark3116_break_ctl(struct tty_struct *tty, int break_state)
0437 {
0438 struct usb_serial_port *port = tty->driver_data;
0439 struct ark3116_private *priv = usb_get_serial_port_data(port);
0440
0441
0442 mutex_lock(&priv->hw_lock);
0443
0444 if (break_state)
0445 priv->lcr |= UART_LCR_SBC;
0446 else
0447 priv->lcr &= ~UART_LCR_SBC;
0448
0449 ark3116_write_reg(port->serial, UART_LCR, priv->lcr);
0450
0451 mutex_unlock(&priv->hw_lock);
0452 }
0453
0454 static void ark3116_update_msr(struct usb_serial_port *port, __u8 msr)
0455 {
0456 struct ark3116_private *priv = usb_get_serial_port_data(port);
0457 unsigned long flags;
0458
0459 spin_lock_irqsave(&priv->status_lock, flags);
0460 priv->msr = msr;
0461 spin_unlock_irqrestore(&priv->status_lock, flags);
0462
0463 if (msr & UART_MSR_ANY_DELTA) {
0464
0465 if (msr & UART_MSR_DCTS)
0466 port->icount.cts++;
0467 if (msr & UART_MSR_DDSR)
0468 port->icount.dsr++;
0469 if (msr & UART_MSR_DDCD)
0470 port->icount.dcd++;
0471 if (msr & UART_MSR_TERI)
0472 port->icount.rng++;
0473 wake_up_interruptible(&port->port.delta_msr_wait);
0474 }
0475 }
0476
0477 static void ark3116_update_lsr(struct usb_serial_port *port, __u8 lsr)
0478 {
0479 struct ark3116_private *priv = usb_get_serial_port_data(port);
0480 unsigned long flags;
0481
0482 spin_lock_irqsave(&priv->status_lock, flags);
0483
0484 priv->lsr |= lsr;
0485 spin_unlock_irqrestore(&priv->status_lock, flags);
0486
0487 if (lsr&UART_LSR_BRK_ERROR_BITS) {
0488 if (lsr & UART_LSR_BI)
0489 port->icount.brk++;
0490 if (lsr & UART_LSR_FE)
0491 port->icount.frame++;
0492 if (lsr & UART_LSR_PE)
0493 port->icount.parity++;
0494 if (lsr & UART_LSR_OE)
0495 port->icount.overrun++;
0496 }
0497 }
0498
0499 static void ark3116_read_int_callback(struct urb *urb)
0500 {
0501 struct usb_serial_port *port = urb->context;
0502 int status = urb->status;
0503 const __u8 *data = urb->transfer_buffer;
0504 int result;
0505
0506 switch (status) {
0507 case -ECONNRESET:
0508 case -ENOENT:
0509 case -ESHUTDOWN:
0510
0511 dev_dbg(&port->dev, "%s - urb shutting down with status: %d\n",
0512 __func__, status);
0513 return;
0514 default:
0515 dev_dbg(&port->dev, "%s - nonzero urb status received: %d\n",
0516 __func__, status);
0517 break;
0518 case 0:
0519
0520 if ((urb->actual_length == 4) && (data[0] == 0xe8)) {
0521 const __u8 id = data[1]&UART_IIR_ID;
0522 dev_dbg(&port->dev, "%s: iir=%02x\n", __func__, data[1]);
0523 if (id == UART_IIR_MSI) {
0524 dev_dbg(&port->dev, "%s: msr=%02x\n",
0525 __func__, data[3]);
0526 ark3116_update_msr(port, data[3]);
0527 break;
0528 } else if (id == UART_IIR_RLSI) {
0529 dev_dbg(&port->dev, "%s: lsr=%02x\n",
0530 __func__, data[2]);
0531 ark3116_update_lsr(port, data[2]);
0532 break;
0533 }
0534 }
0535
0536
0537
0538 usb_serial_debug_data(&port->dev, __func__,
0539 urb->actual_length,
0540 urb->transfer_buffer);
0541 break;
0542 }
0543
0544 result = usb_submit_urb(urb, GFP_ATOMIC);
0545 if (result)
0546 dev_err(&port->dev, "failed to resubmit interrupt urb: %d\n",
0547 result);
0548 }
0549
0550
0551
0552
0553
0554
0555
0556
0557
0558
0559
0560
0561
0562 static void ark3116_process_read_urb(struct urb *urb)
0563 {
0564 struct usb_serial_port *port = urb->context;
0565 struct ark3116_private *priv = usb_get_serial_port_data(port);
0566 unsigned char *data = urb->transfer_buffer;
0567 char tty_flag = TTY_NORMAL;
0568 unsigned long flags;
0569 __u32 lsr;
0570
0571
0572 spin_lock_irqsave(&priv->status_lock, flags);
0573 lsr = priv->lsr;
0574 priv->lsr &= ~UART_LSR_BRK_ERROR_BITS;
0575 spin_unlock_irqrestore(&priv->status_lock, flags);
0576
0577 if (!urb->actual_length)
0578 return;
0579
0580 if (lsr & UART_LSR_BRK_ERROR_BITS) {
0581 if (lsr & UART_LSR_BI)
0582 tty_flag = TTY_BREAK;
0583 else if (lsr & UART_LSR_PE)
0584 tty_flag = TTY_PARITY;
0585 else if (lsr & UART_LSR_FE)
0586 tty_flag = TTY_FRAME;
0587
0588
0589 if (lsr & UART_LSR_OE)
0590 tty_insert_flip_char(&port->port, 0, TTY_OVERRUN);
0591 }
0592 tty_insert_flip_string_fixed_flag(&port->port, data, tty_flag,
0593 urb->actual_length);
0594 tty_flip_buffer_push(&port->port);
0595 }
0596
0597 static struct usb_serial_driver ark3116_device = {
0598 .driver = {
0599 .owner = THIS_MODULE,
0600 .name = "ark3116",
0601 },
0602 .id_table = id_table,
0603 .num_ports = 1,
0604 .num_bulk_in = 1,
0605 .num_bulk_out = 1,
0606 .num_interrupt_in = 1,
0607 .port_probe = ark3116_port_probe,
0608 .port_remove = ark3116_port_remove,
0609 .set_termios = ark3116_set_termios,
0610 .tiocmget = ark3116_tiocmget,
0611 .tiocmset = ark3116_tiocmset,
0612 .tiocmiwait = usb_serial_generic_tiocmiwait,
0613 .get_icount = usb_serial_generic_get_icount,
0614 .open = ark3116_open,
0615 .close = ark3116_close,
0616 .break_ctl = ark3116_break_ctl,
0617 .read_int_callback = ark3116_read_int_callback,
0618 .process_read_urb = ark3116_process_read_urb,
0619 };
0620
0621 static struct usb_serial_driver * const serial_drivers[] = {
0622 &ark3116_device, NULL
0623 };
0624
0625 module_usb_serial_driver(serial_drivers, id_table);
0626
0627 MODULE_LICENSE("GPL");
0628
0629 MODULE_AUTHOR(DRIVER_AUTHOR);
0630 MODULE_DESCRIPTION(DRIVER_DESC);
0631
0632
0633
0634
0635
0636
0637
0638
0639
0640
0641
0642
0643
0644
0645
0646
0647
0648
0649
0650
0651
0652
0653
0654
0655
0656
0657
0658
0659
0660
0661
0662
0663
0664
0665
0666
0667
0668
0669
0670
0671
0672
0673
0674
0675
0676
0677
0678
0679
0680
0681
0682
0683
0684
0685
0686
0687
0688
0689
0690
0691
0692
0693
0694
0695
0696
0697
0698
0699
0700
0701
0702
0703
0704
0705
0706
0707
0708
0709
0710
0711
0712
0713
0714
0715
0716
0717
0718
0719
0720
0721
0722
0723
0724
0725
0726
0727
0728
0729
0730
0731
0732