Greg Kroah-Hartman | 815ddc9 | 2006-05-12 11:05:29 -0700 | [diff] [blame] | 1 | /* |
bart.hartgers@gmail.com | 149fc79 | 2009-10-28 10:43:25 +0100 | [diff] [blame] | 2 | * Copyright (C) 2009 by Bart Hartgers (bart.hartgers+ark3116@gmail.com) |
| 3 | * Original version: |
Werner Lemberg | 2f430b4 | 2006-07-18 17:00:52 +0200 | [diff] [blame] | 4 | * Copyright (C) 2006 |
| 5 | * Simon Schulz (ark3116_driver <at> auctionant.de) |
| 6 | * |
Greg Kroah-Hartman | 815ddc9 | 2006-05-12 11:05:29 -0700 | [diff] [blame] | 7 | * ark3116 |
| 8 | * - implements a driver for the arkmicro ark3116 chipset (vendor=0x6547, |
| 9 | * productid=0x0232) (used in a datacable called KQ-U8A) |
| 10 | * |
bart.hartgers@gmail.com | 149fc79 | 2009-10-28 10:43:25 +0100 | [diff] [blame] | 11 | * Supports full modem status lines, break, hardware flow control. Does not |
| 12 | * support software flow control, since I do not know how to enable it in hw. |
Greg Kroah-Hartman | 815ddc9 | 2006-05-12 11:05:29 -0700 | [diff] [blame] | 13 | * |
bart.hartgers@gmail.com | 149fc79 | 2009-10-28 10:43:25 +0100 | [diff] [blame] | 14 | * This driver is a essentially new implementation. I initially dug |
| 15 | * into the old ark3116.c driver and suddenly realized the ark3116 is |
| 16 | * a 16450 with a USB interface glued to it. See comments at the |
| 17 | * bottom of this file. |
Greg Kroah-Hartman | 815ddc9 | 2006-05-12 11:05:29 -0700 | [diff] [blame] | 18 | * |
Greg Kroah-Hartman | 815ddc9 | 2006-05-12 11:05:29 -0700 | [diff] [blame] | 19 | * This program is free software; you can redistribute it and/or modify it |
| 20 | * under the terms of the GNU General Public License as published by the |
| 21 | * Free Software Foundation; either version 2 of the License, or (at your |
| 22 | * option) any later version. |
| 23 | */ |
| 24 | |
| 25 | #include <linux/kernel.h> |
| 26 | #include <linux/init.h> |
bart.hartgers@gmail.com | 149fc79 | 2009-10-28 10:43:25 +0100 | [diff] [blame] | 27 | #include <linux/ioctl.h> |
Greg Kroah-Hartman | 815ddc9 | 2006-05-12 11:05:29 -0700 | [diff] [blame] | 28 | #include <linux/tty.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 29 | #include <linux/slab.h> |
bart.hartgers@gmail.com | 149fc79 | 2009-10-28 10:43:25 +0100 | [diff] [blame] | 30 | #include <linux/tty_flip.h> |
Greg Kroah-Hartman | 815ddc9 | 2006-05-12 11:05:29 -0700 | [diff] [blame] | 31 | #include <linux/module.h> |
| 32 | #include <linux/usb.h> |
Greg Kroah-Hartman | a969888 | 2006-07-11 21:22:58 -0700 | [diff] [blame] | 33 | #include <linux/usb/serial.h> |
Werner Lemberg | 2f430b4 | 2006-07-18 17:00:52 +0200 | [diff] [blame] | 34 | #include <linux/serial.h> |
bart.hartgers@gmail.com | 149fc79 | 2009-10-28 10:43:25 +0100 | [diff] [blame] | 35 | #include <linux/serial_reg.h> |
Alan Cox | c4d0f8c | 2008-04-29 14:35:39 +0100 | [diff] [blame] | 36 | #include <linux/uaccess.h> |
bart.hartgers@gmail.com | 149fc79 | 2009-10-28 10:43:25 +0100 | [diff] [blame] | 37 | #include <linux/mutex.h> |
| 38 | #include <linux/spinlock.h> |
Greg Kroah-Hartman | 815ddc9 | 2006-05-12 11:05:29 -0700 | [diff] [blame] | 39 | |
bart.hartgers@gmail.com | 149fc79 | 2009-10-28 10:43:25 +0100 | [diff] [blame] | 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 | /* usb timeout of 1 second */ |
| 46 | #define ARK_TIMEOUT (1*HZ) |
Greg Kroah-Hartman | 815ddc9 | 2006-05-12 11:05:29 -0700 | [diff] [blame] | 47 | |
Németh Márton | 7d40d7e | 2010-01-10 15:34:24 +0100 | [diff] [blame] | 48 | static const struct usb_device_id id_table[] = { |
Greg Kroah-Hartman | 815ddc9 | 2006-05-12 11:05:29 -0700 | [diff] [blame] | 49 | { USB_DEVICE(0x6547, 0x0232) }, |
Ondrej Zary | 5128a66 | 2009-08-06 16:09:52 -0700 | [diff] [blame] | 50 | { USB_DEVICE(0x18ec, 0x3118) }, /* USB to IrDA adapter */ |
Greg Kroah-Hartman | 815ddc9 | 2006-05-12 11:05:29 -0700 | [diff] [blame] | 51 | { }, |
| 52 | }; |
| 53 | MODULE_DEVICE_TABLE(usb, id_table); |
| 54 | |
Ondrej Zary | 5128a66 | 2009-08-06 16:09:52 -0700 | [diff] [blame] | 55 | static 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 | |
bart.hartgers@gmail.com | 149fc79 | 2009-10-28 10:43:25 +0100 | [diff] [blame] | 64 | struct ark3116_private { |
bart.hartgers@gmail.com | 149fc79 | 2009-10-28 10:43:25 +0100 | [diff] [blame] | 65 | struct async_icount icount; |
| 66 | int irda; /* 1 for irda device */ |
| 67 | |
| 68 | /* protects hw register updates */ |
| 69 | struct mutex hw_lock; |
| 70 | |
| 71 | int quot; /* baudrate divisor */ |
| 72 | __u32 lcr; /* line control register value */ |
| 73 | __u32 hcr; /* handshake control register (0x8) |
| 74 | * value */ |
| 75 | __u32 mcr; /* modem contol register value */ |
| 76 | |
| 77 | /* protects the status values below */ |
| 78 | spinlock_t status_lock; |
| 79 | __u32 msr; /* modem status register value */ |
| 80 | __u32 lsr; /* line status register value */ |
| 81 | }; |
| 82 | |
| 83 | static int ark3116_write_reg(struct usb_serial *serial, |
| 84 | unsigned reg, __u8 val) |
| 85 | { |
| 86 | int result; |
| 87 | /* 0xfe 0x40 are magic values taken from original driver */ |
| 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 | |
| 95 | static int ark3116_read_reg(struct usb_serial *serial, |
| 96 | unsigned reg, unsigned char *buf) |
| 97 | { |
| 98 | int result; |
| 99 | /* 0xfe 0xc0 are magic values taken from original driver */ |
| 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 | |
bart.hartgers@gmail.com | f4c1e8d | 2009-10-28 10:43:26 +0100 | [diff] [blame] | 110 | static inline int calc_divisor(int bps) |
| 111 | { |
| 112 | /* Original ark3116 made some exceptions in rounding here |
| 113 | * because windows did the same. Assume that is not really |
| 114 | * necessary. |
| 115 | * Crystal is 12MHz, probably because of USB, but we divide by 4? |
| 116 | */ |
| 117 | return (12000000 + 2*bps) / (4*bps); |
| 118 | } |
| 119 | |
Greg Kroah-Hartman | 815ddc9 | 2006-05-12 11:05:29 -0700 | [diff] [blame] | 120 | static int ark3116_attach(struct usb_serial *serial) |
| 121 | { |
bart.hartgers@gmail.com | f4c1e8d | 2009-10-28 10:43:26 +0100 | [diff] [blame] | 122 | /* make sure we have our end-points */ |
| 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 | |
Johan Hovold | 7bdce71 | 2012-10-15 18:20:52 +0200 | [diff] [blame] | 136 | return 0; |
| 137 | } |
| 138 | |
| 139 | static 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); |
bart.hartgers@gmail.com | f4c1e8d | 2009-10-28 10:43:26 +0100 | [diff] [blame] | 145 | if (!priv) |
Alan Cox | fe1ae7f | 2009-09-19 13:13:33 -0700 | [diff] [blame] | 146 | return -ENOMEM; |
bart.hartgers@gmail.com | f4c1e8d | 2009-10-28 10:43:26 +0100 | [diff] [blame] | 147 | |
bart.hartgers@gmail.com | f4c1e8d | 2009-10-28 10:43:26 +0100 | [diff] [blame] | 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 | /* setup the hardware */ |
| 156 | ark3116_write_reg(serial, UART_IER, 0); |
| 157 | /* disable DMA */ |
| 158 | ark3116_write_reg(serial, UART_FCR, 0); |
| 159 | /* handshake control */ |
| 160 | priv->hcr = 0; |
| 161 | ark3116_write_reg(serial, 0x8 , 0); |
| 162 | /* modem control */ |
| 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); |
Greg Kroah-Hartman | 815ddc9 | 2006-05-12 11:05:29 -0700 | [diff] [blame] | 173 | } |
| 174 | |
bart.hartgers@gmail.com | f4c1e8d | 2009-10-28 10:43:26 +0100 | [diff] [blame] | 175 | /* setup baudrate */ |
| 176 | ark3116_write_reg(serial, UART_LCR, UART_LCR_DLAB); |
Ondrej Zary | 5128a66 | 2009-08-06 16:09:52 -0700 | [diff] [blame] | 177 | |
bart.hartgers@gmail.com | f4c1e8d | 2009-10-28 10:43:26 +0100 | [diff] [blame] | 178 | /* setup for 9600 8N1 */ |
| 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); |
Ondrej Zary | 5128a66 | 2009-08-06 16:09:52 -0700 | [diff] [blame] | 182 | |
bart.hartgers@gmail.com | f4c1e8d | 2009-10-28 10:43:26 +0100 | [diff] [blame] | 183 | priv->lcr = UART_LCR_WLEN8; |
| 184 | ark3116_write_reg(serial, UART_LCR, UART_LCR_WLEN8); |
Greg Kroah-Hartman | 815ddc9 | 2006-05-12 11:05:29 -0700 | [diff] [blame] | 185 | |
bart.hartgers@gmail.com | f4c1e8d | 2009-10-28 10:43:26 +0100 | [diff] [blame] | 186 | ark3116_write_reg(serial, 0xe, 0); |
Greg Kroah-Hartman | 815ddc9 | 2006-05-12 11:05:29 -0700 | [diff] [blame] | 187 | |
bart.hartgers@gmail.com | f4c1e8d | 2009-10-28 10:43:26 +0100 | [diff] [blame] | 188 | if (priv->irda) |
| 189 | ark3116_write_reg(serial, 0x9, 0); |
Greg Kroah-Hartman | 815ddc9 | 2006-05-12 11:05:29 -0700 | [diff] [blame] | 190 | |
bart.hartgers@gmail.com | f4c1e8d | 2009-10-28 10:43:26 +0100 | [diff] [blame] | 191 | dev_info(&serial->dev->dev, |
| 192 | "%s using %s mode\n", |
| 193 | KBUILD_MODNAME, |
| 194 | priv->irda ? "IrDA" : "RS232"); |
Werner Lemberg | 988440e | 2006-07-18 17:00:22 +0200 | [diff] [blame] | 195 | return 0; |
Alan Cox | fe1ae7f | 2009-09-19 13:13:33 -0700 | [diff] [blame] | 196 | } |
Greg Kroah-Hartman | 815ddc9 | 2006-05-12 11:05:29 -0700 | [diff] [blame] | 197 | |
Johan Hovold | 7bdce71 | 2012-10-15 18:20:52 +0200 | [diff] [blame] | 198 | static int ark3116_port_remove(struct usb_serial_port *port) |
bart.hartgers@gmail.com | f4c1e8d | 2009-10-28 10:43:26 +0100 | [diff] [blame] | 199 | { |
bart.hartgers@gmail.com | f4c1e8d | 2009-10-28 10:43:26 +0100 | [diff] [blame] | 200 | struct ark3116_private *priv = usb_get_serial_port_data(port); |
| 201 | |
| 202 | /* device is closed, so URBs and DMA should be down */ |
bart.hartgers@gmail.com | f4c1e8d | 2009-10-28 10:43:26 +0100 | [diff] [blame] | 203 | mutex_destroy(&priv->hw_lock); |
bart.hartgers@gmail.com | f4c1e8d | 2009-10-28 10:43:26 +0100 | [diff] [blame] | 204 | kfree(priv); |
Johan Hovold | 7bdce71 | 2012-10-15 18:20:52 +0200 | [diff] [blame] | 205 | |
| 206 | return 0; |
bart.hartgers@gmail.com | f4c1e8d | 2009-10-28 10:43:26 +0100 | [diff] [blame] | 207 | } |
| 208 | |
Alan Cox | fe1ae7f | 2009-09-19 13:13:33 -0700 | [diff] [blame] | 209 | static void ark3116_init_termios(struct tty_struct *tty) |
| 210 | { |
Alan Cox | adc8d74 | 2012-07-14 15:31:47 +0100 | [diff] [blame] | 211 | struct ktermios *termios = &tty->termios; |
Alan Cox | fe1ae7f | 2009-09-19 13:13:33 -0700 | [diff] [blame] | 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; |
Greg Kroah-Hartman | 815ddc9 | 2006-05-12 11:05:29 -0700 | [diff] [blame] | 217 | } |
| 218 | |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 219 | static void ark3116_set_termios(struct tty_struct *tty, |
| 220 | struct usb_serial_port *port, |
Alan Cox | 606d099 | 2006-12-08 02:38:45 -0800 | [diff] [blame] | 221 | struct ktermios *old_termios) |
Greg Kroah-Hartman | 815ddc9 | 2006-05-12 11:05:29 -0700 | [diff] [blame] | 222 | { |
| 223 | struct usb_serial *serial = port->serial; |
bart.hartgers@gmail.com | f4c1e8d | 2009-10-28 10:43:26 +0100 | [diff] [blame] | 224 | struct ark3116_private *priv = usb_get_serial_port_data(port); |
Alan Cox | adc8d74 | 2012-07-14 15:31:47 +0100 | [diff] [blame] | 225 | struct ktermios *termios = &tty->termios; |
Alan Cox | adb5dca | 2007-10-18 01:24:17 -0700 | [diff] [blame] | 226 | unsigned int cflag = termios->c_cflag; |
bart.hartgers@gmail.com | f4c1e8d | 2009-10-28 10:43:26 +0100 | [diff] [blame] | 227 | int bps = tty_get_baud_rate(tty); |
| 228 | int quot; |
| 229 | __u8 lcr, hcr, eval; |
Greg Kroah-Hartman | 815ddc9 | 2006-05-12 11:05:29 -0700 | [diff] [blame] | 230 | |
bart.hartgers@gmail.com | f4c1e8d | 2009-10-28 10:43:26 +0100 | [diff] [blame] | 231 | /* set data bit count */ |
| 232 | switch (cflag & CSIZE) { |
| 233 | case CS5: |
| 234 | lcr = UART_LCR_WLEN5; |
Alan Cox | c4d0f8c | 2008-04-29 14:35:39 +0100 | [diff] [blame] | 235 | break; |
bart.hartgers@gmail.com | f4c1e8d | 2009-10-28 10:43:26 +0100 | [diff] [blame] | 236 | case CS6: |
| 237 | lcr = UART_LCR_WLEN6; |
| 238 | break; |
| 239 | case CS7: |
| 240 | lcr = UART_LCR_WLEN7; |
| 241 | break; |
Alan Cox | c4d0f8c | 2008-04-29 14:35:39 +0100 | [diff] [blame] | 242 | default: |
bart.hartgers@gmail.com | f4c1e8d | 2009-10-28 10:43:26 +0100 | [diff] [blame] | 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 | /* handshake control */ |
| 258 | hcr = (cflag & CRTSCTS) ? 0x03 : 0x00; |
| 259 | |
| 260 | /* calc baudrate */ |
Greg Kroah-Hartman | d2f20e1 | 2012-05-15 16:27:11 -0700 | [diff] [blame] | 261 | dev_dbg(&port->dev, "%s - setting bps to %d\n", __func__, bps); |
bart.hartgers@gmail.com | f4c1e8d | 2009-10-28 10:43:26 +0100 | [diff] [blame] | 262 | eval = 0; |
| 263 | switch (bps) { |
Alan Cox | c4d0f8c | 2008-04-29 14:35:39 +0100 | [diff] [blame] | 264 | case 0: |
bart.hartgers@gmail.com | f4c1e8d | 2009-10-28 10:43:26 +0100 | [diff] [blame] | 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; |
Alan Cox | 568c24a | 2007-06-22 14:36:29 +0100 | [diff] [blame] | 280 | } |
Greg Kroah-Hartman | 815ddc9 | 2006-05-12 11:05:29 -0700 | [diff] [blame] | 281 | |
bart.hartgers@gmail.com | f4c1e8d | 2009-10-28 10:43:26 +0100 | [diff] [blame] | 282 | /* Update state: synchronize */ |
| 283 | mutex_lock(&priv->hw_lock); |
Greg Kroah-Hartman | 815ddc9 | 2006-05-12 11:05:29 -0700 | [diff] [blame] | 284 | |
bart.hartgers@gmail.com | f4c1e8d | 2009-10-28 10:43:26 +0100 | [diff] [blame] | 285 | /* keep old LCR_SBC bit */ |
| 286 | lcr |= (priv->lcr & UART_LCR_SBC); |
Werner Lemberg | 988440e | 2006-07-18 17:00:22 +0200 | [diff] [blame] | 287 | |
Greg Kroah-Hartman | d2f20e1 | 2012-05-15 16:27:11 -0700 | [diff] [blame] | 288 | dev_dbg(&port->dev, "%s - setting hcr:0x%02x,lcr:0x%02x,quot:%d\n", |
| 289 | __func__, hcr, lcr, quot); |
Greg Kroah-Hartman | 815ddc9 | 2006-05-12 11:05:29 -0700 | [diff] [blame] | 290 | |
bart.hartgers@gmail.com | f4c1e8d | 2009-10-28 10:43:26 +0100 | [diff] [blame] | 291 | /* handshake control */ |
| 292 | if (priv->hcr != hcr) { |
| 293 | priv->hcr = hcr; |
| 294 | ark3116_write_reg(serial, 0x8, hcr); |
| 295 | } |
Greg Kroah-Hartman | 815ddc9 | 2006-05-12 11:05:29 -0700 | [diff] [blame] | 296 | |
bart.hartgers@gmail.com | f4c1e8d | 2009-10-28 10:43:26 +0100 | [diff] [blame] | 297 | /* baudrate */ |
| 298 | if (priv->quot != quot) { |
| 299 | priv->quot = quot; |
| 300 | priv->lcr = lcr; /* need to write lcr anyway */ |
Greg Kroah-Hartman | 815ddc9 | 2006-05-12 11:05:29 -0700 | [diff] [blame] | 301 | |
bart.hartgers@gmail.com | f4c1e8d | 2009-10-28 10:43:26 +0100 | [diff] [blame] | 302 | /* disable DMA since transmit/receive is |
| 303 | * shadowed by UART_DLL |
| 304 | */ |
| 305 | ark3116_write_reg(serial, UART_FCR, 0); |
Greg Kroah-Hartman | 815ddc9 | 2006-05-12 11:05:29 -0700 | [diff] [blame] | 306 | |
bart.hartgers@gmail.com | f4c1e8d | 2009-10-28 10:43:26 +0100 | [diff] [blame] | 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); |
Greg Kroah-Hartman | 815ddc9 | 2006-05-12 11:05:29 -0700 | [diff] [blame] | 311 | |
bart.hartgers@gmail.com | f4c1e8d | 2009-10-28 10:43:26 +0100 | [diff] [blame] | 312 | /* restore lcr */ |
| 313 | ark3116_write_reg(serial, UART_LCR, lcr); |
| 314 | /* magic baudrate thingy: not sure what it does, |
| 315 | * but windows does this as well. |
| 316 | */ |
| 317 | ark3116_write_reg(serial, 0xe, eval); |
Greg Kroah-Hartman | 815ddc9 | 2006-05-12 11:05:29 -0700 | [diff] [blame] | 318 | |
bart.hartgers@gmail.com | f4c1e8d | 2009-10-28 10:43:26 +0100 | [diff] [blame] | 319 | /* enable DMA */ |
| 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 | } |
Alan Cox | adb5dca | 2007-10-18 01:24:17 -0700 | [diff] [blame] | 325 | |
bart.hartgers@gmail.com | f4c1e8d | 2009-10-28 10:43:26 +0100 | [diff] [blame] | 326 | mutex_unlock(&priv->hw_lock); |
| 327 | |
| 328 | /* check for software flow control */ |
| 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 | /* Don't rewrite B0 */ |
| 336 | if (tty_termios_baud_rate(termios)) |
| 337 | tty_termios_encode_baud_rate(termios, bps, bps); |
| 338 | } |
| 339 | |
| 340 | static void ark3116_close(struct usb_serial_port *port) |
| 341 | { |
| 342 | struct usb_serial *serial = port->serial; |
| 343 | |
Johan Hovold | 5c32752 | 2013-03-21 12:36:28 +0100 | [diff] [blame^] | 344 | /* disable DMA */ |
| 345 | ark3116_write_reg(serial, UART_FCR, 0); |
bart.hartgers@gmail.com | f4c1e8d | 2009-10-28 10:43:26 +0100 | [diff] [blame] | 346 | |
Johan Hovold | 5c32752 | 2013-03-21 12:36:28 +0100 | [diff] [blame^] | 347 | /* deactivate interrupts */ |
| 348 | ark3116_write_reg(serial, UART_IER, 0); |
bart.hartgers@gmail.com | f4c1e8d | 2009-10-28 10:43:26 +0100 | [diff] [blame] | 349 | |
Johan Hovold | 5c32752 | 2013-03-21 12:36:28 +0100 | [diff] [blame^] | 350 | usb_serial_generic_close(port); |
| 351 | if (serial->num_interrupt_in) |
| 352 | usb_kill_urb(port->interrupt_in_urb); |
Greg Kroah-Hartman | 815ddc9 | 2006-05-12 11:05:29 -0700 | [diff] [blame] | 353 | } |
| 354 | |
Alan Cox | a509a7e | 2009-09-19 13:13:26 -0700 | [diff] [blame] | 355 | static int ark3116_open(struct tty_struct *tty, struct usb_serial_port *port) |
Greg Kroah-Hartman | 815ddc9 | 2006-05-12 11:05:29 -0700 | [diff] [blame] | 356 | { |
bart.hartgers@gmail.com | f4c1e8d | 2009-10-28 10:43:26 +0100 | [diff] [blame] | 357 | struct ark3116_private *priv = usb_get_serial_port_data(port); |
Greg Kroah-Hartman | 815ddc9 | 2006-05-12 11:05:29 -0700 | [diff] [blame] | 358 | struct usb_serial *serial = port->serial; |
bart.hartgers@gmail.com | f4c1e8d | 2009-10-28 10:43:26 +0100 | [diff] [blame] | 359 | unsigned char *buf; |
| 360 | int result; |
Greg Kroah-Hartman | 815ddc9 | 2006-05-12 11:05:29 -0700 | [diff] [blame] | 361 | |
| 362 | buf = kmalloc(1, GFP_KERNEL); |
bart.hartgers@gmail.com | f4c1e8d | 2009-10-28 10:43:26 +0100 | [diff] [blame] | 363 | if (buf == NULL) |
Greg Kroah-Hartman | 815ddc9 | 2006-05-12 11:05:29 -0700 | [diff] [blame] | 364 | return -ENOMEM; |
Greg Kroah-Hartman | 815ddc9 | 2006-05-12 11:05:29 -0700 | [diff] [blame] | 365 | |
Alan Cox | a509a7e | 2009-09-19 13:13:26 -0700 | [diff] [blame] | 366 | result = usb_serial_generic_open(tty, port); |
bart.hartgers@gmail.com | f4c1e8d | 2009-10-28 10:43:26 +0100 | [diff] [blame] | 367 | if (result) { |
Greg Kroah-Hartman | d2f20e1 | 2012-05-15 16:27:11 -0700 | [diff] [blame] | 368 | dev_dbg(&port->dev, |
| 369 | "%s - usb_serial_generic_open failed: %d\n", |
| 370 | __func__, result); |
Oliver Neukum | 4edf2c8 | 2007-03-26 18:12:44 +0200 | [diff] [blame] | 371 | goto err_out; |
bart.hartgers@gmail.com | f4c1e8d | 2009-10-28 10:43:26 +0100 | [diff] [blame] | 372 | } |
Greg Kroah-Hartman | 815ddc9 | 2006-05-12 11:05:29 -0700 | [diff] [blame] | 373 | |
bart.hartgers@gmail.com | f4c1e8d | 2009-10-28 10:43:26 +0100 | [diff] [blame] | 374 | /* remove any data still left: also clears error state */ |
| 375 | ark3116_read_reg(serial, UART_RX, buf); |
| 376 | |
| 377 | /* read modem status */ |
| 378 | priv->msr = ark3116_read_reg(serial, UART_MSR, buf); |
| 379 | /* read line status */ |
| 380 | priv->lsr = ark3116_read_reg(serial, UART_LSR, buf); |
| 381 | |
| 382 | result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL); |
| 383 | if (result) { |
| 384 | dev_err(&port->dev, "submit irq_in urb failed %d\n", |
| 385 | result); |
| 386 | ark3116_close(port); |
| 387 | goto err_out; |
| 388 | } |
| 389 | |
| 390 | /* activate interrupts */ |
| 391 | ark3116_write_reg(port->serial, UART_IER, UART_IER_MSI|UART_IER_RLSI); |
| 392 | |
| 393 | /* enable DMA */ |
| 394 | ark3116_write_reg(port->serial, UART_FCR, UART_FCR_DMA_SELECT); |
Greg Kroah-Hartman | 815ddc9 | 2006-05-12 11:05:29 -0700 | [diff] [blame] | 395 | |
Bart Hartgers | 583182b | 2011-10-26 13:29:42 +0200 | [diff] [blame] | 396 | /* setup termios */ |
| 397 | if (tty) |
| 398 | ark3116_set_termios(tty, port, NULL); |
| 399 | |
Oliver Neukum | 4edf2c8 | 2007-03-26 18:12:44 +0200 | [diff] [blame] | 400 | err_out: |
Greg Kroah-Hartman | 815ddc9 | 2006-05-12 11:05:29 -0700 | [diff] [blame] | 401 | kfree(buf); |
Greg Kroah-Hartman | 815ddc9 | 2006-05-12 11:05:29 -0700 | [diff] [blame] | 402 | return result; |
Greg Kroah-Hartman | 815ddc9 | 2006-05-12 11:05:29 -0700 | [diff] [blame] | 403 | } |
| 404 | |
Alan Cox | 0bca1b9 | 2010-09-16 18:21:40 +0100 | [diff] [blame] | 405 | static int ark3116_get_icount(struct tty_struct *tty, |
| 406 | struct serial_icounter_struct *icount) |
| 407 | { |
| 408 | struct usb_serial_port *port = tty->driver_data; |
| 409 | struct ark3116_private *priv = usb_get_serial_port_data(port); |
| 410 | struct async_icount cnow = priv->icount; |
| 411 | icount->cts = cnow.cts; |
| 412 | icount->dsr = cnow.dsr; |
| 413 | icount->rng = cnow.rng; |
| 414 | icount->dcd = cnow.dcd; |
| 415 | icount->rx = cnow.rx; |
| 416 | icount->tx = cnow.tx; |
| 417 | icount->frame = cnow.frame; |
| 418 | icount->overrun = cnow.overrun; |
| 419 | icount->parity = cnow.parity; |
| 420 | icount->brk = cnow.brk; |
| 421 | icount->buf_overrun = cnow.buf_overrun; |
| 422 | return 0; |
| 423 | } |
| 424 | |
Alan Cox | 00a0d0d | 2011-02-14 16:27:06 +0000 | [diff] [blame] | 425 | static int ark3116_ioctl(struct tty_struct *tty, |
Greg Kroah-Hartman | 815ddc9 | 2006-05-12 11:05:29 -0700 | [diff] [blame] | 426 | unsigned int cmd, unsigned long arg) |
| 427 | { |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 428 | struct usb_serial_port *port = tty->driver_data; |
bart.hartgers@gmail.com | f4c1e8d | 2009-10-28 10:43:26 +0100 | [diff] [blame] | 429 | struct ark3116_private *priv = usb_get_serial_port_data(port); |
Werner Lemberg | 2f430b4 | 2006-07-18 17:00:52 +0200 | [diff] [blame] | 430 | struct serial_struct serstruct; |
| 431 | void __user *user_arg = (void __user *)arg; |
| 432 | |
| 433 | switch (cmd) { |
| 434 | case TIOCGSERIAL: |
| 435 | /* XXX: Some of these values are probably wrong. */ |
Alan Cox | c4d0f8c | 2008-04-29 14:35:39 +0100 | [diff] [blame] | 436 | memset(&serstruct, 0, sizeof(serstruct)); |
Werner Lemberg | 2f430b4 | 2006-07-18 17:00:52 +0200 | [diff] [blame] | 437 | serstruct.type = PORT_16654; |
| 438 | serstruct.line = port->serial->minor; |
| 439 | serstruct.port = port->number; |
| 440 | serstruct.custom_divisor = 0; |
| 441 | serstruct.baud_base = 460800; |
| 442 | |
Alan Cox | c4d0f8c | 2008-04-29 14:35:39 +0100 | [diff] [blame] | 443 | if (copy_to_user(user_arg, &serstruct, sizeof(serstruct))) |
Werner Lemberg | 2f430b4 | 2006-07-18 17:00:52 +0200 | [diff] [blame] | 444 | return -EFAULT; |
| 445 | |
| 446 | return 0; |
| 447 | case TIOCSSERIAL: |
Alan Cox | c4d0f8c | 2008-04-29 14:35:39 +0100 | [diff] [blame] | 448 | if (copy_from_user(&serstruct, user_arg, sizeof(serstruct))) |
Werner Lemberg | 2f430b4 | 2006-07-18 17:00:52 +0200 | [diff] [blame] | 449 | return -EFAULT; |
| 450 | return 0; |
bart.hartgers@gmail.com | f4c1e8d | 2009-10-28 10:43:26 +0100 | [diff] [blame] | 451 | case TIOCMIWAIT: |
| 452 | for (;;) { |
| 453 | struct async_icount prev = priv->icount; |
Johan Hovold | 5018860 | 2013-03-19 09:21:11 +0100 | [diff] [blame] | 454 | interruptible_sleep_on(&port->delta_msr_wait); |
bart.hartgers@gmail.com | f4c1e8d | 2009-10-28 10:43:26 +0100 | [diff] [blame] | 455 | /* see if a signal did it */ |
| 456 | if (signal_pending(current)) |
| 457 | return -ERESTARTSYS; |
Johan Hovold | 5018860 | 2013-03-19 09:21:11 +0100 | [diff] [blame] | 458 | |
| 459 | if (port->serial->disconnected) |
| 460 | return -EIO; |
| 461 | |
bart.hartgers@gmail.com | f4c1e8d | 2009-10-28 10:43:26 +0100 | [diff] [blame] | 462 | if ((prev.rng == priv->icount.rng) && |
| 463 | (prev.dsr == priv->icount.dsr) && |
| 464 | (prev.dcd == priv->icount.dcd) && |
| 465 | (prev.cts == priv->icount.cts)) |
| 466 | return -EIO; |
| 467 | if ((arg & TIOCM_RNG && |
| 468 | (prev.rng != priv->icount.rng)) || |
| 469 | (arg & TIOCM_DSR && |
| 470 | (prev.dsr != priv->icount.dsr)) || |
| 471 | (arg & TIOCM_CD && |
| 472 | (prev.dcd != priv->icount.dcd)) || |
| 473 | (arg & TIOCM_CTS && |
| 474 | (prev.cts != priv->icount.cts))) |
| 475 | return 0; |
| 476 | } |
Werner Lemberg | 2f430b4 | 2006-07-18 17:00:52 +0200 | [diff] [blame] | 477 | break; |
| 478 | } |
| 479 | |
Greg Kroah-Hartman | 815ddc9 | 2006-05-12 11:05:29 -0700 | [diff] [blame] | 480 | return -ENOIOCTLCMD; |
| 481 | } |
| 482 | |
Alan Cox | 60b33c1 | 2011-02-14 16:26:14 +0000 | [diff] [blame] | 483 | static int ark3116_tiocmget(struct tty_struct *tty) |
Greg Kroah-Hartman | 815ddc9 | 2006-05-12 11:05:29 -0700 | [diff] [blame] | 484 | { |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 485 | struct usb_serial_port *port = tty->driver_data; |
bart.hartgers@gmail.com | 1f71910 | 2009-10-28 10:43:27 +0100 | [diff] [blame] | 486 | struct ark3116_private *priv = usb_get_serial_port_data(port); |
| 487 | __u32 status; |
| 488 | __u32 ctrl; |
| 489 | unsigned long flags; |
Greg Kroah-Hartman | 815ddc9 | 2006-05-12 11:05:29 -0700 | [diff] [blame] | 490 | |
bart.hartgers@gmail.com | 1f71910 | 2009-10-28 10:43:27 +0100 | [diff] [blame] | 491 | mutex_lock(&priv->hw_lock); |
| 492 | ctrl = priv->mcr; |
| 493 | mutex_unlock(&priv->hw_lock); |
Greg Kroah-Hartman | 815ddc9 | 2006-05-12 11:05:29 -0700 | [diff] [blame] | 494 | |
bart.hartgers@gmail.com | 1f71910 | 2009-10-28 10:43:27 +0100 | [diff] [blame] | 495 | spin_lock_irqsave(&priv->status_lock, flags); |
| 496 | status = priv->msr; |
| 497 | spin_unlock_irqrestore(&priv->status_lock, flags); |
Greg Kroah-Hartman | 815ddc9 | 2006-05-12 11:05:29 -0700 | [diff] [blame] | 498 | |
bart.hartgers@gmail.com | 1f71910 | 2009-10-28 10:43:27 +0100 | [diff] [blame] | 499 | return (status & UART_MSR_DSR ? TIOCM_DSR : 0) | |
| 500 | (status & UART_MSR_CTS ? TIOCM_CTS : 0) | |
| 501 | (status & UART_MSR_RI ? TIOCM_RI : 0) | |
| 502 | (status & UART_MSR_DCD ? TIOCM_CD : 0) | |
| 503 | (ctrl & UART_MCR_DTR ? TIOCM_DTR : 0) | |
| 504 | (ctrl & UART_MCR_RTS ? TIOCM_RTS : 0) | |
| 505 | (ctrl & UART_MCR_OUT1 ? TIOCM_OUT1 : 0) | |
| 506 | (ctrl & UART_MCR_OUT2 ? TIOCM_OUT2 : 0); |
Greg Kroah-Hartman | 815ddc9 | 2006-05-12 11:05:29 -0700 | [diff] [blame] | 507 | } |
| 508 | |
Alan Cox | 20b9d17 | 2011-02-14 16:26:50 +0000 | [diff] [blame] | 509 | static int ark3116_tiocmset(struct tty_struct *tty, |
bart.hartgers@gmail.com | 546b742 | 2009-10-28 10:43:28 +0100 | [diff] [blame] | 510 | unsigned set, unsigned clr) |
| 511 | { |
| 512 | struct usb_serial_port *port = tty->driver_data; |
| 513 | struct ark3116_private *priv = usb_get_serial_port_data(port); |
| 514 | |
| 515 | /* we need to take the mutex here, to make sure that the value |
| 516 | * in priv->mcr is actually the one that is in the hardware |
| 517 | */ |
| 518 | |
| 519 | mutex_lock(&priv->hw_lock); |
| 520 | |
| 521 | if (set & TIOCM_RTS) |
| 522 | priv->mcr |= UART_MCR_RTS; |
| 523 | if (set & TIOCM_DTR) |
| 524 | priv->mcr |= UART_MCR_DTR; |
| 525 | if (set & TIOCM_OUT1) |
| 526 | priv->mcr |= UART_MCR_OUT1; |
| 527 | if (set & TIOCM_OUT2) |
| 528 | priv->mcr |= UART_MCR_OUT2; |
| 529 | if (clr & TIOCM_RTS) |
| 530 | priv->mcr &= ~UART_MCR_RTS; |
| 531 | if (clr & TIOCM_DTR) |
| 532 | priv->mcr &= ~UART_MCR_DTR; |
| 533 | if (clr & TIOCM_OUT1) |
| 534 | priv->mcr &= ~UART_MCR_OUT1; |
| 535 | if (clr & TIOCM_OUT2) |
| 536 | priv->mcr &= ~UART_MCR_OUT2; |
| 537 | |
| 538 | ark3116_write_reg(port->serial, UART_MCR, priv->mcr); |
| 539 | |
| 540 | mutex_unlock(&priv->hw_lock); |
| 541 | |
| 542 | return 0; |
| 543 | } |
| 544 | |
| 545 | static void ark3116_break_ctl(struct tty_struct *tty, int break_state) |
| 546 | { |
| 547 | struct usb_serial_port *port = tty->driver_data; |
| 548 | struct ark3116_private *priv = usb_get_serial_port_data(port); |
| 549 | |
| 550 | /* LCR is also used for other things: protect access */ |
| 551 | mutex_lock(&priv->hw_lock); |
| 552 | |
| 553 | if (break_state) |
| 554 | priv->lcr |= UART_LCR_SBC; |
| 555 | else |
| 556 | priv->lcr &= ~UART_LCR_SBC; |
| 557 | |
| 558 | ark3116_write_reg(port->serial, UART_LCR, priv->lcr); |
| 559 | |
| 560 | mutex_unlock(&priv->hw_lock); |
| 561 | } |
| 562 | |
bart.hartgers@gmail.com | 62d826c | 2009-10-28 10:43:29 +0100 | [diff] [blame] | 563 | static void ark3116_update_msr(struct usb_serial_port *port, __u8 msr) |
| 564 | { |
| 565 | struct ark3116_private *priv = usb_get_serial_port_data(port); |
| 566 | unsigned long flags; |
| 567 | |
| 568 | spin_lock_irqsave(&priv->status_lock, flags); |
| 569 | priv->msr = msr; |
| 570 | spin_unlock_irqrestore(&priv->status_lock, flags); |
| 571 | |
| 572 | if (msr & UART_MSR_ANY_DELTA) { |
| 573 | /* update input line counters */ |
| 574 | if (msr & UART_MSR_DCTS) |
| 575 | priv->icount.cts++; |
| 576 | if (msr & UART_MSR_DDSR) |
| 577 | priv->icount.dsr++; |
| 578 | if (msr & UART_MSR_DDCD) |
| 579 | priv->icount.dcd++; |
| 580 | if (msr & UART_MSR_TERI) |
| 581 | priv->icount.rng++; |
Johan Hovold | 5018860 | 2013-03-19 09:21:11 +0100 | [diff] [blame] | 582 | wake_up_interruptible(&port->delta_msr_wait); |
bart.hartgers@gmail.com | 62d826c | 2009-10-28 10:43:29 +0100 | [diff] [blame] | 583 | } |
| 584 | } |
| 585 | |
| 586 | static void ark3116_update_lsr(struct usb_serial_port *port, __u8 lsr) |
| 587 | { |
| 588 | struct ark3116_private *priv = usb_get_serial_port_data(port); |
| 589 | unsigned long flags; |
| 590 | |
| 591 | spin_lock_irqsave(&priv->status_lock, flags); |
| 592 | /* combine bits */ |
| 593 | priv->lsr |= lsr; |
| 594 | spin_unlock_irqrestore(&priv->status_lock, flags); |
| 595 | |
| 596 | if (lsr&UART_LSR_BRK_ERROR_BITS) { |
| 597 | if (lsr & UART_LSR_BI) |
| 598 | priv->icount.brk++; |
| 599 | if (lsr & UART_LSR_FE) |
| 600 | priv->icount.frame++; |
| 601 | if (lsr & UART_LSR_PE) |
| 602 | priv->icount.parity++; |
| 603 | if (lsr & UART_LSR_OE) |
| 604 | priv->icount.overrun++; |
| 605 | } |
| 606 | } |
| 607 | |
| 608 | static void ark3116_read_int_callback(struct urb *urb) |
| 609 | { |
| 610 | struct usb_serial_port *port = urb->context; |
| 611 | int status = urb->status; |
| 612 | const __u8 *data = urb->transfer_buffer; |
| 613 | int result; |
| 614 | |
| 615 | switch (status) { |
| 616 | case -ECONNRESET: |
| 617 | case -ENOENT: |
| 618 | case -ESHUTDOWN: |
| 619 | /* this urb is terminated, clean up */ |
Greg Kroah-Hartman | d2f20e1 | 2012-05-15 16:27:11 -0700 | [diff] [blame] | 620 | dev_dbg(&port->dev, "%s - urb shutting down with status: %d\n", |
| 621 | __func__, status); |
bart.hartgers@gmail.com | 62d826c | 2009-10-28 10:43:29 +0100 | [diff] [blame] | 622 | return; |
| 623 | default: |
Greg Kroah-Hartman | d2f20e1 | 2012-05-15 16:27:11 -0700 | [diff] [blame] | 624 | dev_dbg(&port->dev, "%s - nonzero urb status received: %d\n", |
| 625 | __func__, status); |
bart.hartgers@gmail.com | 62d826c | 2009-10-28 10:43:29 +0100 | [diff] [blame] | 626 | break; |
| 627 | case 0: /* success */ |
| 628 | /* discovered this by trail and error... */ |
| 629 | if ((urb->actual_length == 4) && (data[0] == 0xe8)) { |
| 630 | const __u8 id = data[1]&UART_IIR_ID; |
Greg Kroah-Hartman | d2f20e1 | 2012-05-15 16:27:11 -0700 | [diff] [blame] | 631 | dev_dbg(&port->dev, "%s: iir=%02x\n", __func__, data[1]); |
bart.hartgers@gmail.com | 62d826c | 2009-10-28 10:43:29 +0100 | [diff] [blame] | 632 | if (id == UART_IIR_MSI) { |
Greg Kroah-Hartman | d2f20e1 | 2012-05-15 16:27:11 -0700 | [diff] [blame] | 633 | dev_dbg(&port->dev, "%s: msr=%02x\n", |
| 634 | __func__, data[3]); |
bart.hartgers@gmail.com | 62d826c | 2009-10-28 10:43:29 +0100 | [diff] [blame] | 635 | ark3116_update_msr(port, data[3]); |
| 636 | break; |
| 637 | } else if (id == UART_IIR_RLSI) { |
Greg Kroah-Hartman | d2f20e1 | 2012-05-15 16:27:11 -0700 | [diff] [blame] | 638 | dev_dbg(&port->dev, "%s: lsr=%02x\n", |
| 639 | __func__, data[2]); |
bart.hartgers@gmail.com | 62d826c | 2009-10-28 10:43:29 +0100 | [diff] [blame] | 640 | ark3116_update_lsr(port, data[2]); |
| 641 | break; |
| 642 | } |
| 643 | } |
| 644 | /* |
| 645 | * Not sure what this data meant... |
| 646 | */ |
Greg Kroah-Hartman | 59d33f2 | 2012-09-18 09:58:57 +0100 | [diff] [blame] | 647 | usb_serial_debug_data(&port->dev, __func__, |
bart.hartgers@gmail.com | 62d826c | 2009-10-28 10:43:29 +0100 | [diff] [blame] | 648 | urb->actual_length, |
| 649 | urb->transfer_buffer); |
| 650 | break; |
| 651 | } |
| 652 | |
| 653 | result = usb_submit_urb(urb, GFP_ATOMIC); |
| 654 | if (result) |
| 655 | dev_err(&urb->dev->dev, |
| 656 | "%s - Error %d submitting interrupt urb\n", |
| 657 | __func__, result); |
| 658 | } |
| 659 | |
| 660 | |
| 661 | /* Data comes in via the bulk (data) URB, erors/interrupts via the int URB. |
| 662 | * This means that we cannot be sure which data byte has an associated error |
| 663 | * condition, so we report an error for all data in the next bulk read. |
| 664 | * |
| 665 | * Actually, there might even be a window between the bulk data leaving the |
| 666 | * ark and reading/resetting the lsr in the read_bulk_callback where an |
| 667 | * interrupt for the next data block could come in. |
| 668 | * Without somekind of ordering on the ark, we would have to report the |
| 669 | * error for the next block of data as well... |
| 670 | * For now, let's pretend this can't happen. |
| 671 | */ |
Johan Hovold | 82b71cf | 2010-05-07 20:45:34 +0200 | [diff] [blame] | 672 | static void ark3116_process_read_urb(struct urb *urb) |
bart.hartgers@gmail.com | 62d826c | 2009-10-28 10:43:29 +0100 | [diff] [blame] | 673 | { |
Johan Hovold | 82b71cf | 2010-05-07 20:45:34 +0200 | [diff] [blame] | 674 | struct usb_serial_port *port = urb->context; |
bart.hartgers@gmail.com | 62d826c | 2009-10-28 10:43:29 +0100 | [diff] [blame] | 675 | struct ark3116_private *priv = usb_get_serial_port_data(port); |
Johan Hovold | 82b71cf | 2010-05-07 20:45:34 +0200 | [diff] [blame] | 676 | unsigned char *data = urb->transfer_buffer; |
| 677 | char tty_flag = TTY_NORMAL; |
bart.hartgers@gmail.com | 62d826c | 2009-10-28 10:43:29 +0100 | [diff] [blame] | 678 | unsigned long flags; |
bart.hartgers@gmail.com | 62d826c | 2009-10-28 10:43:29 +0100 | [diff] [blame] | 679 | __u32 lsr; |
| 680 | |
Johan Hovold | 82b71cf | 2010-05-07 20:45:34 +0200 | [diff] [blame] | 681 | /* update line status */ |
| 682 | spin_lock_irqsave(&priv->status_lock, flags); |
| 683 | lsr = priv->lsr; |
| 684 | priv->lsr &= ~UART_LSR_BRK_ERROR_BITS; |
| 685 | spin_unlock_irqrestore(&priv->status_lock, flags); |
| 686 | |
| 687 | if (!urb->actual_length) |
bart.hartgers@gmail.com | 62d826c | 2009-10-28 10:43:29 +0100 | [diff] [blame] | 688 | return; |
bart.hartgers@gmail.com | 62d826c | 2009-10-28 10:43:29 +0100 | [diff] [blame] | 689 | |
Johan Hovold | 82b71cf | 2010-05-07 20:45:34 +0200 | [diff] [blame] | 690 | if (lsr & UART_LSR_BRK_ERROR_BITS) { |
| 691 | if (lsr & UART_LSR_BI) |
| 692 | tty_flag = TTY_BREAK; |
| 693 | else if (lsr & UART_LSR_PE) |
| 694 | tty_flag = TTY_PARITY; |
| 695 | else if (lsr & UART_LSR_FE) |
| 696 | tty_flag = TTY_FRAME; |
bart.hartgers@gmail.com | 62d826c | 2009-10-28 10:43:29 +0100 | [diff] [blame] | 697 | |
Johan Hovold | 82b71cf | 2010-05-07 20:45:34 +0200 | [diff] [blame] | 698 | /* overrun is special, not associated with a char */ |
| 699 | if (lsr & UART_LSR_OE) |
Jiri Slaby | 92a19f9 | 2013-01-03 15:53:03 +0100 | [diff] [blame] | 700 | tty_insert_flip_char(&port->port, 0, TTY_OVERRUN); |
bart.hartgers@gmail.com | 62d826c | 2009-10-28 10:43:29 +0100 | [diff] [blame] | 701 | } |
Jiri Slaby | 2f69335 | 2013-01-03 15:53:02 +0100 | [diff] [blame] | 702 | tty_insert_flip_string_fixed_flag(&port->port, data, tty_flag, |
Johan Hovold | 82b71cf | 2010-05-07 20:45:34 +0200 | [diff] [blame] | 703 | urb->actual_length); |
Jiri Slaby | 2e124b4 | 2013-01-03 15:53:06 +0100 | [diff] [blame] | 704 | tty_flip_buffer_push(&port->port); |
bart.hartgers@gmail.com | 62d826c | 2009-10-28 10:43:29 +0100 | [diff] [blame] | 705 | } |
| 706 | |
Greg Kroah-Hartman | 815ddc9 | 2006-05-12 11:05:29 -0700 | [diff] [blame] | 707 | static struct usb_serial_driver ark3116_device = { |
| 708 | .driver = { |
| 709 | .owner = THIS_MODULE, |
| 710 | .name = "ark3116", |
| 711 | }, |
| 712 | .id_table = id_table, |
Greg Kroah-Hartman | 815ddc9 | 2006-05-12 11:05:29 -0700 | [diff] [blame] | 713 | .num_ports = 1, |
| 714 | .attach = ark3116_attach, |
Johan Hovold | 7bdce71 | 2012-10-15 18:20:52 +0200 | [diff] [blame] | 715 | .port_probe = ark3116_port_probe, |
| 716 | .port_remove = ark3116_port_remove, |
Greg Kroah-Hartman | 815ddc9 | 2006-05-12 11:05:29 -0700 | [diff] [blame] | 717 | .set_termios = ark3116_set_termios, |
Alan Cox | fe1ae7f | 2009-09-19 13:13:33 -0700 | [diff] [blame] | 718 | .init_termios = ark3116_init_termios, |
Greg Kroah-Hartman | 815ddc9 | 2006-05-12 11:05:29 -0700 | [diff] [blame] | 719 | .ioctl = ark3116_ioctl, |
| 720 | .tiocmget = ark3116_tiocmget, |
bart.hartgers@gmail.com | 546b742 | 2009-10-28 10:43:28 +0100 | [diff] [blame] | 721 | .tiocmset = ark3116_tiocmset, |
Alan Cox | 0bca1b9 | 2010-09-16 18:21:40 +0100 | [diff] [blame] | 722 | .get_icount = ark3116_get_icount, |
Greg Kroah-Hartman | 815ddc9 | 2006-05-12 11:05:29 -0700 | [diff] [blame] | 723 | .open = ark3116_open, |
bart.hartgers@gmail.com | f4c1e8d | 2009-10-28 10:43:26 +0100 | [diff] [blame] | 724 | .close = ark3116_close, |
bart.hartgers@gmail.com | 546b742 | 2009-10-28 10:43:28 +0100 | [diff] [blame] | 725 | .break_ctl = ark3116_break_ctl, |
bart.hartgers@gmail.com | 62d826c | 2009-10-28 10:43:29 +0100 | [diff] [blame] | 726 | .read_int_callback = ark3116_read_int_callback, |
Johan Hovold | 82b71cf | 2010-05-07 20:45:34 +0200 | [diff] [blame] | 727 | .process_read_urb = ark3116_process_read_urb, |
Greg Kroah-Hartman | 815ddc9 | 2006-05-12 11:05:29 -0700 | [diff] [blame] | 728 | }; |
| 729 | |
Alan Stern | 08a4f6b | 2012-02-23 14:56:17 -0500 | [diff] [blame] | 730 | static struct usb_serial_driver * const serial_drivers[] = { |
| 731 | &ark3116_device, NULL |
| 732 | }; |
| 733 | |
Greg Kroah-Hartman | 68e2411 | 2012-05-08 15:46:14 -0700 | [diff] [blame] | 734 | module_usb_serial_driver(serial_drivers, id_table); |
Greg Kroah-Hartman | 815ddc9 | 2006-05-12 11:05:29 -0700 | [diff] [blame] | 735 | |
Greg Kroah-Hartman | 815ddc9 | 2006-05-12 11:05:29 -0700 | [diff] [blame] | 736 | MODULE_LICENSE("GPL"); |
| 737 | |
bart.hartgers@gmail.com | 149fc79 | 2009-10-28 10:43:25 +0100 | [diff] [blame] | 738 | MODULE_AUTHOR(DRIVER_AUTHOR); |
| 739 | MODULE_DESCRIPTION(DRIVER_DESC); |
Greg Kroah-Hartman | 815ddc9 | 2006-05-12 11:05:29 -0700 | [diff] [blame] | 740 | |
bart.hartgers@gmail.com | 149fc79 | 2009-10-28 10:43:25 +0100 | [diff] [blame] | 741 | /* |
| 742 | * The following describes what I learned from studying the old |
| 743 | * ark3116.c driver, disassembling the windows driver, and some lucky |
| 744 | * guesses. Since I do not have any datasheet or other |
| 745 | * documentation, inaccuracies are almost guaranteed. |
| 746 | * |
| 747 | * Some specs for the ARK3116 can be found here: |
| 748 | * http://web.archive.org/web/20060318000438/ |
| 749 | * www.arkmicro.com/en/products/view.php?id=10 |
| 750 | * On that page, 2 GPIO pins are mentioned: I assume these are the |
| 751 | * OUT1 and OUT2 pins of the UART, so I added support for those |
| 752 | * through the MCR. Since the pins are not available on my hardware, |
| 753 | * I could not verify this. |
| 754 | * Also, it states there is "on-chip hardware flow control". I have |
| 755 | * discovered how to enable that. Unfortunately, I do not know how to |
| 756 | * enable XON/XOFF (software) flow control, which would need support |
| 757 | * from the chip as well to work. Because of the wording on the web |
| 758 | * page there is a real possibility the chip simply does not support |
| 759 | * software flow control. |
| 760 | * |
| 761 | * I got my ark3116 as part of a mobile phone adapter cable. On the |
| 762 | * PCB, the following numbered contacts are present: |
| 763 | * |
| 764 | * 1:- +5V |
| 765 | * 2:o DTR |
| 766 | * 3:i RX |
| 767 | * 4:i DCD |
| 768 | * 5:o RTS |
| 769 | * 6:o TX |
| 770 | * 7:i RI |
| 771 | * 8:i DSR |
| 772 | * 10:- 0V |
| 773 | * 11:i CTS |
| 774 | * |
| 775 | * On my chip, all signals seem to be 3.3V, but 5V tolerant. But that |
| 776 | * may be different for the one you have ;-). |
| 777 | * |
| 778 | * The windows driver limits the registers to 0-F, so I assume there |
| 779 | * are actually 16 present on the device. |
| 780 | * |
| 781 | * On an UART interrupt, 4 bytes of data come in on the interrupt |
| 782 | * endpoint. The bytes are 0xe8 IIR LSR MSR. |
| 783 | * |
| 784 | * The baudrate seems to be generated from the 12MHz crystal, using |
| 785 | * 4-times subsampling. So quot=12e6/(4*baud). Also see description |
| 786 | * of register E. |
| 787 | * |
| 788 | * Registers 0-7: |
| 789 | * These seem to be the same as for a regular 16450. The FCR is set |
| 790 | * to UART_FCR_DMA_SELECT (0x8), I guess to enable transfers between |
| 791 | * the UART and the USB bridge/DMA engine. |
| 792 | * |
| 793 | * Register 8: |
| 794 | * By trial and error, I found out that bit 0 enables hardware CTS, |
| 795 | * stopping TX when CTS is +5V. Bit 1 does the same for RTS, making |
| 796 | * RTS +5V when the 3116 cannot transfer the data to the USB bus |
| 797 | * (verified by disabling the reading URB). Note that as far as I can |
| 798 | * tell, the windows driver does NOT use this, so there might be some |
| 799 | * hardware bug or something. |
| 800 | * |
| 801 | * According to a patch provided here |
| 802 | * (http://lkml.org/lkml/2009/7/26/56), the ARK3116 can also be used |
| 803 | * as an IrDA dongle. Since I do not have such a thing, I could not |
| 804 | * investigate that aspect. However, I can speculate ;-). |
| 805 | * |
| 806 | * - IrDA encodes data differently than RS232. Most likely, one of |
| 807 | * the bits in registers 9..E enables the IR ENDEC (encoder/decoder). |
| 808 | * - Depending on the IR transceiver, the input and output need to be |
| 809 | * inverted, so there are probably bits for that as well. |
| 810 | * - IrDA is half-duplex, so there should be a bit for selecting that. |
| 811 | * |
| 812 | * This still leaves at least two registers unaccounted for. Perhaps |
| 813 | * The chip can do XON/XOFF or CRC in HW? |
| 814 | * |
| 815 | * Register 9: |
| 816 | * Set to 0x00 for IrDA, when the baudrate is initialised. |
| 817 | * |
| 818 | * Register A: |
| 819 | * Set to 0x01 for IrDA, at init. |
| 820 | * |
| 821 | * Register B: |
| 822 | * Set to 0x01 for IrDA, 0x00 for RS232, at init. |
| 823 | * |
| 824 | * Register C: |
| 825 | * Set to 00 for IrDA, at init. |
| 826 | * |
| 827 | * Register D: |
| 828 | * Set to 0x41 for IrDA, at init. |
| 829 | * |
| 830 | * Register E: |
| 831 | * Somekind of baudrate override. The windows driver seems to set |
| 832 | * this to 0x00 for normal baudrates, 0x01 for 460800, 0x02 for 921600. |
| 833 | * Since 460800 and 921600 cannot be obtained by dividing 3MHz by an integer, |
| 834 | * it could be somekind of subdivisor thingy. |
| 835 | * However,it does not seem to do anything: selecting 921600 (divisor 3, |
| 836 | * reg E=2), still gets 1 MHz. I also checked if registers 9, C or F would |
| 837 | * work, but they don't. |
| 838 | * |
| 839 | * Register F: unknown |
| 840 | */ |