blob: 3b811feb35fdb3ce104333a0ea2f5eb9884b25e7 [file] [log] [blame]
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -07001/*
bart.hartgers@gmail.com149fc792009-10-28 10:43:25 +01002 * Copyright (C) 2009 by Bart Hartgers (bart.hartgers+ark3116@gmail.com)
3 * Original version:
Werner Lemberg2f430b42006-07-18 17:00:52 +02004 * Copyright (C) 2006
5 * Simon Schulz (ark3116_driver <at> auctionant.de)
6 *
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -07007 * 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.com149fc792009-10-28 10:43:25 +010011 * 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-Hartman815ddc92006-05-12 11:05:29 -070013 *
bart.hartgers@gmail.com149fc792009-10-28 10:43:25 +010014 * 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-Hartman815ddc92006-05-12 11:05:29 -070018 *
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -070019 * 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.com149fc792009-10-28 10:43:25 +010027#include <linux/ioctl.h>
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -070028#include <linux/tty.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
bart.hartgers@gmail.com149fc792009-10-28 10:43:25 +010030#include <linux/tty_flip.h>
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -070031#include <linux/module.h>
32#include <linux/usb.h>
Greg Kroah-Hartmana9698882006-07-11 21:22:58 -070033#include <linux/usb/serial.h>
Werner Lemberg2f430b42006-07-18 17:00:52 +020034#include <linux/serial.h>
bart.hartgers@gmail.com149fc792009-10-28 10:43:25 +010035#include <linux/serial_reg.h>
Alan Coxc4d0f8c2008-04-29 14:35:39 +010036#include <linux/uaccess.h>
bart.hartgers@gmail.com149fc792009-10-28 10:43:25 +010037#include <linux/mutex.h>
38#include <linux/spinlock.h>
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -070039
bart.hartgers@gmail.com149fc792009-10-28 10:43:25 +010040#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-Hartman815ddc92006-05-12 11:05:29 -070047
Németh Márton7d40d7e2010-01-10 15:34:24 +010048static const struct usb_device_id id_table[] = {
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -070049 { USB_DEVICE(0x6547, 0x0232) },
Ondrej Zary5128a662009-08-06 16:09:52 -070050 { USB_DEVICE(0x18ec, 0x3118) }, /* USB to IrDA adapter */
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -070051 { },
52};
53MODULE_DEVICE_TABLE(usb, id_table);
54
Ondrej Zary5128a662009-08-06 16:09:52 -070055static 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.com149fc792009-10-28 10:43:25 +010064struct ark3116_private {
bart.hartgers@gmail.com149fc792009-10-28 10:43:25 +010065 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
83static 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
95static 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.comf4c1e8d2009-10-28 10:43:26 +0100110static 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-Hartman815ddc92006-05-12 11:05:29 -0700120static int ark3116_attach(struct usb_serial *serial)
121{
bart.hartgers@gmail.comf4c1e8d2009-10-28 10:43:26 +0100122 /* 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 Hovold7bdce712012-10-15 18:20:52 +0200136 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);
bart.hartgers@gmail.comf4c1e8d2009-10-28 10:43:26 +0100145 if (!priv)
Alan Coxfe1ae7f2009-09-19 13:13:33 -0700146 return -ENOMEM;
bart.hartgers@gmail.comf4c1e8d2009-10-28 10:43:26 +0100147
bart.hartgers@gmail.comf4c1e8d2009-10-28 10:43:26 +0100148 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-Hartman815ddc92006-05-12 11:05:29 -0700173 }
174
bart.hartgers@gmail.comf4c1e8d2009-10-28 10:43:26 +0100175 /* setup baudrate */
176 ark3116_write_reg(serial, UART_LCR, UART_LCR_DLAB);
Ondrej Zary5128a662009-08-06 16:09:52 -0700177
bart.hartgers@gmail.comf4c1e8d2009-10-28 10:43:26 +0100178 /* 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 Zary5128a662009-08-06 16:09:52 -0700182
bart.hartgers@gmail.comf4c1e8d2009-10-28 10:43:26 +0100183 priv->lcr = UART_LCR_WLEN8;
184 ark3116_write_reg(serial, UART_LCR, UART_LCR_WLEN8);
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700185
bart.hartgers@gmail.comf4c1e8d2009-10-28 10:43:26 +0100186 ark3116_write_reg(serial, 0xe, 0);
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700187
bart.hartgers@gmail.comf4c1e8d2009-10-28 10:43:26 +0100188 if (priv->irda)
189 ark3116_write_reg(serial, 0x9, 0);
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700190
bart.hartgers@gmail.comf4c1e8d2009-10-28 10:43:26 +0100191 dev_info(&serial->dev->dev,
192 "%s using %s mode\n",
193 KBUILD_MODNAME,
194 priv->irda ? "IrDA" : "RS232");
Werner Lemberg988440e2006-07-18 17:00:22 +0200195 return 0;
Alan Coxfe1ae7f2009-09-19 13:13:33 -0700196}
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700197
Johan Hovold7bdce712012-10-15 18:20:52 +0200198static int ark3116_port_remove(struct usb_serial_port *port)
bart.hartgers@gmail.comf4c1e8d2009-10-28 10:43:26 +0100199{
bart.hartgers@gmail.comf4c1e8d2009-10-28 10:43:26 +0100200 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.comf4c1e8d2009-10-28 10:43:26 +0100203 mutex_destroy(&priv->hw_lock);
bart.hartgers@gmail.comf4c1e8d2009-10-28 10:43:26 +0100204 kfree(priv);
Johan Hovold7bdce712012-10-15 18:20:52 +0200205
206 return 0;
bart.hartgers@gmail.comf4c1e8d2009-10-28 10:43:26 +0100207}
208
Alan Coxfe1ae7f2009-09-19 13:13:33 -0700209static void ark3116_init_termios(struct tty_struct *tty)
210{
Alan Coxadc8d742012-07-14 15:31:47 +0100211 struct ktermios *termios = &tty->termios;
Alan Coxfe1ae7f2009-09-19 13:13:33 -0700212 *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-Hartman815ddc92006-05-12 11:05:29 -0700217}
218
Alan Cox95da3102008-07-22 11:09:07 +0100219static void ark3116_set_termios(struct tty_struct *tty,
220 struct usb_serial_port *port,
Alan Cox606d0992006-12-08 02:38:45 -0800221 struct ktermios *old_termios)
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700222{
223 struct usb_serial *serial = port->serial;
bart.hartgers@gmail.comf4c1e8d2009-10-28 10:43:26 +0100224 struct ark3116_private *priv = usb_get_serial_port_data(port);
Alan Coxadc8d742012-07-14 15:31:47 +0100225 struct ktermios *termios = &tty->termios;
Alan Coxadb5dca2007-10-18 01:24:17 -0700226 unsigned int cflag = termios->c_cflag;
bart.hartgers@gmail.comf4c1e8d2009-10-28 10:43:26 +0100227 int bps = tty_get_baud_rate(tty);
228 int quot;
229 __u8 lcr, hcr, eval;
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700230
bart.hartgers@gmail.comf4c1e8d2009-10-28 10:43:26 +0100231 /* set data bit count */
232 switch (cflag & CSIZE) {
233 case CS5:
234 lcr = UART_LCR_WLEN5;
Alan Coxc4d0f8c2008-04-29 14:35:39 +0100235 break;
bart.hartgers@gmail.comf4c1e8d2009-10-28 10:43:26 +0100236 case CS6:
237 lcr = UART_LCR_WLEN6;
238 break;
239 case CS7:
240 lcr = UART_LCR_WLEN7;
241 break;
Alan Coxc4d0f8c2008-04-29 14:35:39 +0100242 default:
bart.hartgers@gmail.comf4c1e8d2009-10-28 10:43:26 +0100243 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-Hartmand2f20e12012-05-15 16:27:11 -0700261 dev_dbg(&port->dev, "%s - setting bps to %d\n", __func__, bps);
bart.hartgers@gmail.comf4c1e8d2009-10-28 10:43:26 +0100262 eval = 0;
263 switch (bps) {
Alan Coxc4d0f8c2008-04-29 14:35:39 +0100264 case 0:
bart.hartgers@gmail.comf4c1e8d2009-10-28 10:43:26 +0100265 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 Cox568c24a2007-06-22 14:36:29 +0100280 }
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700281
bart.hartgers@gmail.comf4c1e8d2009-10-28 10:43:26 +0100282 /* Update state: synchronize */
283 mutex_lock(&priv->hw_lock);
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700284
bart.hartgers@gmail.comf4c1e8d2009-10-28 10:43:26 +0100285 /* keep old LCR_SBC bit */
286 lcr |= (priv->lcr & UART_LCR_SBC);
Werner Lemberg988440e2006-07-18 17:00:22 +0200287
Greg Kroah-Hartmand2f20e12012-05-15 16:27:11 -0700288 dev_dbg(&port->dev, "%s - setting hcr:0x%02x,lcr:0x%02x,quot:%d\n",
289 __func__, hcr, lcr, quot);
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700290
bart.hartgers@gmail.comf4c1e8d2009-10-28 10:43:26 +0100291 /* handshake control */
292 if (priv->hcr != hcr) {
293 priv->hcr = hcr;
294 ark3116_write_reg(serial, 0x8, hcr);
295 }
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700296
bart.hartgers@gmail.comf4c1e8d2009-10-28 10:43:26 +0100297 /* baudrate */
298 if (priv->quot != quot) {
299 priv->quot = quot;
300 priv->lcr = lcr; /* need to write lcr anyway */
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700301
bart.hartgers@gmail.comf4c1e8d2009-10-28 10:43:26 +0100302 /* disable DMA since transmit/receive is
303 * shadowed by UART_DLL
304 */
305 ark3116_write_reg(serial, UART_FCR, 0);
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700306
bart.hartgers@gmail.comf4c1e8d2009-10-28 10:43:26 +0100307 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-Hartman815ddc92006-05-12 11:05:29 -0700311
bart.hartgers@gmail.comf4c1e8d2009-10-28 10:43:26 +0100312 /* 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-Hartman815ddc92006-05-12 11:05:29 -0700318
bart.hartgers@gmail.comf4c1e8d2009-10-28 10:43:26 +0100319 /* 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 Coxadb5dca2007-10-18 01:24:17 -0700325
bart.hartgers@gmail.comf4c1e8d2009-10-28 10:43:26 +0100326 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
340static void ark3116_close(struct usb_serial_port *port)
341{
342 struct usb_serial *serial = port->serial;
343
Johan Hovold5c327522013-03-21 12:36:28 +0100344 /* disable DMA */
345 ark3116_write_reg(serial, UART_FCR, 0);
bart.hartgers@gmail.comf4c1e8d2009-10-28 10:43:26 +0100346
Johan Hovold5c327522013-03-21 12:36:28 +0100347 /* deactivate interrupts */
348 ark3116_write_reg(serial, UART_IER, 0);
bart.hartgers@gmail.comf4c1e8d2009-10-28 10:43:26 +0100349
Johan Hovold5c327522013-03-21 12:36:28 +0100350 usb_serial_generic_close(port);
351 if (serial->num_interrupt_in)
352 usb_kill_urb(port->interrupt_in_urb);
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700353}
354
Alan Coxa509a7e2009-09-19 13:13:26 -0700355static int ark3116_open(struct tty_struct *tty, struct usb_serial_port *port)
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700356{
bart.hartgers@gmail.comf4c1e8d2009-10-28 10:43:26 +0100357 struct ark3116_private *priv = usb_get_serial_port_data(port);
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700358 struct usb_serial *serial = port->serial;
bart.hartgers@gmail.comf4c1e8d2009-10-28 10:43:26 +0100359 unsigned char *buf;
360 int result;
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700361
362 buf = kmalloc(1, GFP_KERNEL);
bart.hartgers@gmail.comf4c1e8d2009-10-28 10:43:26 +0100363 if (buf == NULL)
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700364 return -ENOMEM;
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700365
Alan Coxa509a7e2009-09-19 13:13:26 -0700366 result = usb_serial_generic_open(tty, port);
bart.hartgers@gmail.comf4c1e8d2009-10-28 10:43:26 +0100367 if (result) {
Greg Kroah-Hartmand2f20e12012-05-15 16:27:11 -0700368 dev_dbg(&port->dev,
369 "%s - usb_serial_generic_open failed: %d\n",
370 __func__, result);
Oliver Neukum4edf2c82007-03-26 18:12:44 +0200371 goto err_out;
bart.hartgers@gmail.comf4c1e8d2009-10-28 10:43:26 +0100372 }
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700373
bart.hartgers@gmail.comf4c1e8d2009-10-28 10:43:26 +0100374 /* 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-Hartman815ddc92006-05-12 11:05:29 -0700395
Bart Hartgers583182b2011-10-26 13:29:42 +0200396 /* setup termios */
397 if (tty)
398 ark3116_set_termios(tty, port, NULL);
399
Oliver Neukum4edf2c82007-03-26 18:12:44 +0200400err_out:
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700401 kfree(buf);
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700402 return result;
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700403}
404
Alan Cox0bca1b92010-09-16 18:21:40 +0100405static 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 Cox00a0d0d2011-02-14 16:27:06 +0000425static int ark3116_ioctl(struct tty_struct *tty,
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700426 unsigned int cmd, unsigned long arg)
427{
Alan Cox95da3102008-07-22 11:09:07 +0100428 struct usb_serial_port *port = tty->driver_data;
bart.hartgers@gmail.comf4c1e8d2009-10-28 10:43:26 +0100429 struct ark3116_private *priv = usb_get_serial_port_data(port);
Werner Lemberg2f430b42006-07-18 17:00:52 +0200430 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 Coxc4d0f8c2008-04-29 14:35:39 +0100436 memset(&serstruct, 0, sizeof(serstruct));
Werner Lemberg2f430b42006-07-18 17:00:52 +0200437 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 Coxc4d0f8c2008-04-29 14:35:39 +0100443 if (copy_to_user(user_arg, &serstruct, sizeof(serstruct)))
Werner Lemberg2f430b42006-07-18 17:00:52 +0200444 return -EFAULT;
445
446 return 0;
447 case TIOCSSERIAL:
Alan Coxc4d0f8c2008-04-29 14:35:39 +0100448 if (copy_from_user(&serstruct, user_arg, sizeof(serstruct)))
Werner Lemberg2f430b42006-07-18 17:00:52 +0200449 return -EFAULT;
450 return 0;
bart.hartgers@gmail.comf4c1e8d2009-10-28 10:43:26 +0100451 case TIOCMIWAIT:
452 for (;;) {
453 struct async_icount prev = priv->icount;
Johan Hovold50188602013-03-19 09:21:11 +0100454 interruptible_sleep_on(&port->delta_msr_wait);
bart.hartgers@gmail.comf4c1e8d2009-10-28 10:43:26 +0100455 /* see if a signal did it */
456 if (signal_pending(current))
457 return -ERESTARTSYS;
Johan Hovold50188602013-03-19 09:21:11 +0100458
459 if (port->serial->disconnected)
460 return -EIO;
461
bart.hartgers@gmail.comf4c1e8d2009-10-28 10:43:26 +0100462 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 Lemberg2f430b42006-07-18 17:00:52 +0200477 break;
478 }
479
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700480 return -ENOIOCTLCMD;
481}
482
Alan Cox60b33c12011-02-14 16:26:14 +0000483static int ark3116_tiocmget(struct tty_struct *tty)
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700484{
Alan Cox95da3102008-07-22 11:09:07 +0100485 struct usb_serial_port *port = tty->driver_data;
bart.hartgers@gmail.com1f719102009-10-28 10:43:27 +0100486 struct ark3116_private *priv = usb_get_serial_port_data(port);
487 __u32 status;
488 __u32 ctrl;
489 unsigned long flags;
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700490
bart.hartgers@gmail.com1f719102009-10-28 10:43:27 +0100491 mutex_lock(&priv->hw_lock);
492 ctrl = priv->mcr;
493 mutex_unlock(&priv->hw_lock);
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700494
bart.hartgers@gmail.com1f719102009-10-28 10:43:27 +0100495 spin_lock_irqsave(&priv->status_lock, flags);
496 status = priv->msr;
497 spin_unlock_irqrestore(&priv->status_lock, flags);
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700498
bart.hartgers@gmail.com1f719102009-10-28 10:43:27 +0100499 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-Hartman815ddc92006-05-12 11:05:29 -0700507}
508
Alan Cox20b9d172011-02-14 16:26:50 +0000509static int ark3116_tiocmset(struct tty_struct *tty,
bart.hartgers@gmail.com546b7422009-10-28 10:43:28 +0100510 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
545static 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.com62d826c2009-10-28 10:43:29 +0100563static 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 Hovold50188602013-03-19 09:21:11 +0100582 wake_up_interruptible(&port->delta_msr_wait);
bart.hartgers@gmail.com62d826c2009-10-28 10:43:29 +0100583 }
584}
585
586static 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
608static 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-Hartmand2f20e12012-05-15 16:27:11 -0700620 dev_dbg(&port->dev, "%s - urb shutting down with status: %d\n",
621 __func__, status);
bart.hartgers@gmail.com62d826c2009-10-28 10:43:29 +0100622 return;
623 default:
Greg Kroah-Hartmand2f20e12012-05-15 16:27:11 -0700624 dev_dbg(&port->dev, "%s - nonzero urb status received: %d\n",
625 __func__, status);
bart.hartgers@gmail.com62d826c2009-10-28 10:43:29 +0100626 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-Hartmand2f20e12012-05-15 16:27:11 -0700631 dev_dbg(&port->dev, "%s: iir=%02x\n", __func__, data[1]);
bart.hartgers@gmail.com62d826c2009-10-28 10:43:29 +0100632 if (id == UART_IIR_MSI) {
Greg Kroah-Hartmand2f20e12012-05-15 16:27:11 -0700633 dev_dbg(&port->dev, "%s: msr=%02x\n",
634 __func__, data[3]);
bart.hartgers@gmail.com62d826c2009-10-28 10:43:29 +0100635 ark3116_update_msr(port, data[3]);
636 break;
637 } else if (id == UART_IIR_RLSI) {
Greg Kroah-Hartmand2f20e12012-05-15 16:27:11 -0700638 dev_dbg(&port->dev, "%s: lsr=%02x\n",
639 __func__, data[2]);
bart.hartgers@gmail.com62d826c2009-10-28 10:43:29 +0100640 ark3116_update_lsr(port, data[2]);
641 break;
642 }
643 }
644 /*
645 * Not sure what this data meant...
646 */
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +0100647 usb_serial_debug_data(&port->dev, __func__,
bart.hartgers@gmail.com62d826c2009-10-28 10:43:29 +0100648 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 Hovold82b71cf2010-05-07 20:45:34 +0200672static void ark3116_process_read_urb(struct urb *urb)
bart.hartgers@gmail.com62d826c2009-10-28 10:43:29 +0100673{
Johan Hovold82b71cf2010-05-07 20:45:34 +0200674 struct usb_serial_port *port = urb->context;
bart.hartgers@gmail.com62d826c2009-10-28 10:43:29 +0100675 struct ark3116_private *priv = usb_get_serial_port_data(port);
Johan Hovold82b71cf2010-05-07 20:45:34 +0200676 unsigned char *data = urb->transfer_buffer;
677 char tty_flag = TTY_NORMAL;
bart.hartgers@gmail.com62d826c2009-10-28 10:43:29 +0100678 unsigned long flags;
bart.hartgers@gmail.com62d826c2009-10-28 10:43:29 +0100679 __u32 lsr;
680
Johan Hovold82b71cf2010-05-07 20:45:34 +0200681 /* 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.com62d826c2009-10-28 10:43:29 +0100688 return;
bart.hartgers@gmail.com62d826c2009-10-28 10:43:29 +0100689
Johan Hovold82b71cf2010-05-07 20:45:34 +0200690 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.com62d826c2009-10-28 10:43:29 +0100697
Johan Hovold82b71cf2010-05-07 20:45:34 +0200698 /* overrun is special, not associated with a char */
699 if (lsr & UART_LSR_OE)
Jiri Slaby92a19f92013-01-03 15:53:03 +0100700 tty_insert_flip_char(&port->port, 0, TTY_OVERRUN);
bart.hartgers@gmail.com62d826c2009-10-28 10:43:29 +0100701 }
Jiri Slaby2f693352013-01-03 15:53:02 +0100702 tty_insert_flip_string_fixed_flag(&port->port, data, tty_flag,
Johan Hovold82b71cf2010-05-07 20:45:34 +0200703 urb->actual_length);
Jiri Slaby2e124b42013-01-03 15:53:06 +0100704 tty_flip_buffer_push(&port->port);
bart.hartgers@gmail.com62d826c2009-10-28 10:43:29 +0100705}
706
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700707static struct usb_serial_driver ark3116_device = {
708 .driver = {
709 .owner = THIS_MODULE,
710 .name = "ark3116",
711 },
712 .id_table = id_table,
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700713 .num_ports = 1,
714 .attach = ark3116_attach,
Johan Hovold7bdce712012-10-15 18:20:52 +0200715 .port_probe = ark3116_port_probe,
716 .port_remove = ark3116_port_remove,
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700717 .set_termios = ark3116_set_termios,
Alan Coxfe1ae7f2009-09-19 13:13:33 -0700718 .init_termios = ark3116_init_termios,
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700719 .ioctl = ark3116_ioctl,
720 .tiocmget = ark3116_tiocmget,
bart.hartgers@gmail.com546b7422009-10-28 10:43:28 +0100721 .tiocmset = ark3116_tiocmset,
Alan Cox0bca1b92010-09-16 18:21:40 +0100722 .get_icount = ark3116_get_icount,
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700723 .open = ark3116_open,
bart.hartgers@gmail.comf4c1e8d2009-10-28 10:43:26 +0100724 .close = ark3116_close,
bart.hartgers@gmail.com546b7422009-10-28 10:43:28 +0100725 .break_ctl = ark3116_break_ctl,
bart.hartgers@gmail.com62d826c2009-10-28 10:43:29 +0100726 .read_int_callback = ark3116_read_int_callback,
Johan Hovold82b71cf2010-05-07 20:45:34 +0200727 .process_read_urb = ark3116_process_read_urb,
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700728};
729
Alan Stern08a4f6b2012-02-23 14:56:17 -0500730static struct usb_serial_driver * const serial_drivers[] = {
731 &ark3116_device, NULL
732};
733
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -0700734module_usb_serial_driver(serial_drivers, id_table);
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700735
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700736MODULE_LICENSE("GPL");
737
bart.hartgers@gmail.com149fc792009-10-28 10:43:25 +0100738MODULE_AUTHOR(DRIVER_AUTHOR);
739MODULE_DESCRIPTION(DRIVER_DESC);
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700740
bart.hartgers@gmail.com149fc792009-10-28 10:43:25 +0100741/*
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 */