blob: 4775f8209e5502b7f893d0a00a8f51622746a138 [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
344 if (serial->dev) {
345 /* disable DMA */
346 ark3116_write_reg(serial, UART_FCR, 0);
347
348 /* deactivate interrupts */
349 ark3116_write_reg(serial, UART_IER, 0);
350
Johan Hovoldf26788d2010-03-17 23:00:45 +0100351 usb_serial_generic_close(port);
bart.hartgers@gmail.comf4c1e8d2009-10-28 10:43:26 +0100352 if (serial->num_interrupt_in)
353 usb_kill_urb(port->interrupt_in_urb);
354 }
Johan Hovoldf26788d2010-03-17 23:00:45 +0100355
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700356}
357
Alan Coxa509a7e2009-09-19 13:13:26 -0700358static int ark3116_open(struct tty_struct *tty, struct usb_serial_port *port)
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700359{
bart.hartgers@gmail.comf4c1e8d2009-10-28 10:43:26 +0100360 struct ark3116_private *priv = usb_get_serial_port_data(port);
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700361 struct usb_serial *serial = port->serial;
bart.hartgers@gmail.comf4c1e8d2009-10-28 10:43:26 +0100362 unsigned char *buf;
363 int result;
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700364
365 buf = kmalloc(1, GFP_KERNEL);
bart.hartgers@gmail.comf4c1e8d2009-10-28 10:43:26 +0100366 if (buf == NULL)
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700367 return -ENOMEM;
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700368
Alan Coxa509a7e2009-09-19 13:13:26 -0700369 result = usb_serial_generic_open(tty, port);
bart.hartgers@gmail.comf4c1e8d2009-10-28 10:43:26 +0100370 if (result) {
Greg Kroah-Hartmand2f20e12012-05-15 16:27:11 -0700371 dev_dbg(&port->dev,
372 "%s - usb_serial_generic_open failed: %d\n",
373 __func__, result);
Oliver Neukum4edf2c82007-03-26 18:12:44 +0200374 goto err_out;
bart.hartgers@gmail.comf4c1e8d2009-10-28 10:43:26 +0100375 }
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700376
bart.hartgers@gmail.comf4c1e8d2009-10-28 10:43:26 +0100377 /* remove any data still left: also clears error state */
378 ark3116_read_reg(serial, UART_RX, buf);
379
380 /* read modem status */
381 priv->msr = ark3116_read_reg(serial, UART_MSR, buf);
382 /* read line status */
383 priv->lsr = ark3116_read_reg(serial, UART_LSR, buf);
384
385 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
386 if (result) {
387 dev_err(&port->dev, "submit irq_in urb failed %d\n",
388 result);
389 ark3116_close(port);
390 goto err_out;
391 }
392
393 /* activate interrupts */
394 ark3116_write_reg(port->serial, UART_IER, UART_IER_MSI|UART_IER_RLSI);
395
396 /* enable DMA */
397 ark3116_write_reg(port->serial, UART_FCR, UART_FCR_DMA_SELECT);
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700398
Bart Hartgers583182b2011-10-26 13:29:42 +0200399 /* setup termios */
400 if (tty)
401 ark3116_set_termios(tty, port, NULL);
402
Oliver Neukum4edf2c82007-03-26 18:12:44 +0200403err_out:
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700404 kfree(buf);
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700405 return result;
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700406}
407
Alan Cox0bca1b92010-09-16 18:21:40 +0100408static int ark3116_get_icount(struct tty_struct *tty,
409 struct serial_icounter_struct *icount)
410{
411 struct usb_serial_port *port = tty->driver_data;
412 struct ark3116_private *priv = usb_get_serial_port_data(port);
413 struct async_icount cnow = priv->icount;
414 icount->cts = cnow.cts;
415 icount->dsr = cnow.dsr;
416 icount->rng = cnow.rng;
417 icount->dcd = cnow.dcd;
418 icount->rx = cnow.rx;
419 icount->tx = cnow.tx;
420 icount->frame = cnow.frame;
421 icount->overrun = cnow.overrun;
422 icount->parity = cnow.parity;
423 icount->brk = cnow.brk;
424 icount->buf_overrun = cnow.buf_overrun;
425 return 0;
426}
427
Alan Cox00a0d0d2011-02-14 16:27:06 +0000428static int ark3116_ioctl(struct tty_struct *tty,
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700429 unsigned int cmd, unsigned long arg)
430{
Alan Cox95da3102008-07-22 11:09:07 +0100431 struct usb_serial_port *port = tty->driver_data;
bart.hartgers@gmail.comf4c1e8d2009-10-28 10:43:26 +0100432 struct ark3116_private *priv = usb_get_serial_port_data(port);
Werner Lemberg2f430b42006-07-18 17:00:52 +0200433 struct serial_struct serstruct;
434 void __user *user_arg = (void __user *)arg;
435
436 switch (cmd) {
437 case TIOCGSERIAL:
438 /* XXX: Some of these values are probably wrong. */
Alan Coxc4d0f8c2008-04-29 14:35:39 +0100439 memset(&serstruct, 0, sizeof(serstruct));
Werner Lemberg2f430b42006-07-18 17:00:52 +0200440 serstruct.type = PORT_16654;
441 serstruct.line = port->serial->minor;
442 serstruct.port = port->number;
443 serstruct.custom_divisor = 0;
444 serstruct.baud_base = 460800;
445
Alan Coxc4d0f8c2008-04-29 14:35:39 +0100446 if (copy_to_user(user_arg, &serstruct, sizeof(serstruct)))
Werner Lemberg2f430b42006-07-18 17:00:52 +0200447 return -EFAULT;
448
449 return 0;
450 case TIOCSSERIAL:
Alan Coxc4d0f8c2008-04-29 14:35:39 +0100451 if (copy_from_user(&serstruct, user_arg, sizeof(serstruct)))
Werner Lemberg2f430b42006-07-18 17:00:52 +0200452 return -EFAULT;
453 return 0;
bart.hartgers@gmail.comf4c1e8d2009-10-28 10:43:26 +0100454 case TIOCMIWAIT:
455 for (;;) {
456 struct async_icount prev = priv->icount;
Johan Hovold50188602013-03-19 09:21:11 +0100457 interruptible_sleep_on(&port->delta_msr_wait);
bart.hartgers@gmail.comf4c1e8d2009-10-28 10:43:26 +0100458 /* see if a signal did it */
459 if (signal_pending(current))
460 return -ERESTARTSYS;
Johan Hovold50188602013-03-19 09:21:11 +0100461
462 if (port->serial->disconnected)
463 return -EIO;
464
bart.hartgers@gmail.comf4c1e8d2009-10-28 10:43:26 +0100465 if ((prev.rng == priv->icount.rng) &&
466 (prev.dsr == priv->icount.dsr) &&
467 (prev.dcd == priv->icount.dcd) &&
468 (prev.cts == priv->icount.cts))
469 return -EIO;
470 if ((arg & TIOCM_RNG &&
471 (prev.rng != priv->icount.rng)) ||
472 (arg & TIOCM_DSR &&
473 (prev.dsr != priv->icount.dsr)) ||
474 (arg & TIOCM_CD &&
475 (prev.dcd != priv->icount.dcd)) ||
476 (arg & TIOCM_CTS &&
477 (prev.cts != priv->icount.cts)))
478 return 0;
479 }
Werner Lemberg2f430b42006-07-18 17:00:52 +0200480 break;
481 }
482
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700483 return -ENOIOCTLCMD;
484}
485
Alan Cox60b33c12011-02-14 16:26:14 +0000486static int ark3116_tiocmget(struct tty_struct *tty)
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700487{
Alan Cox95da3102008-07-22 11:09:07 +0100488 struct usb_serial_port *port = tty->driver_data;
bart.hartgers@gmail.com1f719102009-10-28 10:43:27 +0100489 struct ark3116_private *priv = usb_get_serial_port_data(port);
490 __u32 status;
491 __u32 ctrl;
492 unsigned long flags;
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700493
bart.hartgers@gmail.com1f719102009-10-28 10:43:27 +0100494 mutex_lock(&priv->hw_lock);
495 ctrl = priv->mcr;
496 mutex_unlock(&priv->hw_lock);
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700497
bart.hartgers@gmail.com1f719102009-10-28 10:43:27 +0100498 spin_lock_irqsave(&priv->status_lock, flags);
499 status = priv->msr;
500 spin_unlock_irqrestore(&priv->status_lock, flags);
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700501
bart.hartgers@gmail.com1f719102009-10-28 10:43:27 +0100502 return (status & UART_MSR_DSR ? TIOCM_DSR : 0) |
503 (status & UART_MSR_CTS ? TIOCM_CTS : 0) |
504 (status & UART_MSR_RI ? TIOCM_RI : 0) |
505 (status & UART_MSR_DCD ? TIOCM_CD : 0) |
506 (ctrl & UART_MCR_DTR ? TIOCM_DTR : 0) |
507 (ctrl & UART_MCR_RTS ? TIOCM_RTS : 0) |
508 (ctrl & UART_MCR_OUT1 ? TIOCM_OUT1 : 0) |
509 (ctrl & UART_MCR_OUT2 ? TIOCM_OUT2 : 0);
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700510}
511
Alan Cox20b9d172011-02-14 16:26:50 +0000512static int ark3116_tiocmset(struct tty_struct *tty,
bart.hartgers@gmail.com546b7422009-10-28 10:43:28 +0100513 unsigned set, unsigned clr)
514{
515 struct usb_serial_port *port = tty->driver_data;
516 struct ark3116_private *priv = usb_get_serial_port_data(port);
517
518 /* we need to take the mutex here, to make sure that the value
519 * in priv->mcr is actually the one that is in the hardware
520 */
521
522 mutex_lock(&priv->hw_lock);
523
524 if (set & TIOCM_RTS)
525 priv->mcr |= UART_MCR_RTS;
526 if (set & TIOCM_DTR)
527 priv->mcr |= UART_MCR_DTR;
528 if (set & TIOCM_OUT1)
529 priv->mcr |= UART_MCR_OUT1;
530 if (set & TIOCM_OUT2)
531 priv->mcr |= UART_MCR_OUT2;
532 if (clr & TIOCM_RTS)
533 priv->mcr &= ~UART_MCR_RTS;
534 if (clr & TIOCM_DTR)
535 priv->mcr &= ~UART_MCR_DTR;
536 if (clr & TIOCM_OUT1)
537 priv->mcr &= ~UART_MCR_OUT1;
538 if (clr & TIOCM_OUT2)
539 priv->mcr &= ~UART_MCR_OUT2;
540
541 ark3116_write_reg(port->serial, UART_MCR, priv->mcr);
542
543 mutex_unlock(&priv->hw_lock);
544
545 return 0;
546}
547
548static void ark3116_break_ctl(struct tty_struct *tty, int break_state)
549{
550 struct usb_serial_port *port = tty->driver_data;
551 struct ark3116_private *priv = usb_get_serial_port_data(port);
552
553 /* LCR is also used for other things: protect access */
554 mutex_lock(&priv->hw_lock);
555
556 if (break_state)
557 priv->lcr |= UART_LCR_SBC;
558 else
559 priv->lcr &= ~UART_LCR_SBC;
560
561 ark3116_write_reg(port->serial, UART_LCR, priv->lcr);
562
563 mutex_unlock(&priv->hw_lock);
564}
565
bart.hartgers@gmail.com62d826c2009-10-28 10:43:29 +0100566static void ark3116_update_msr(struct usb_serial_port *port, __u8 msr)
567{
568 struct ark3116_private *priv = usb_get_serial_port_data(port);
569 unsigned long flags;
570
571 spin_lock_irqsave(&priv->status_lock, flags);
572 priv->msr = msr;
573 spin_unlock_irqrestore(&priv->status_lock, flags);
574
575 if (msr & UART_MSR_ANY_DELTA) {
576 /* update input line counters */
577 if (msr & UART_MSR_DCTS)
578 priv->icount.cts++;
579 if (msr & UART_MSR_DDSR)
580 priv->icount.dsr++;
581 if (msr & UART_MSR_DDCD)
582 priv->icount.dcd++;
583 if (msr & UART_MSR_TERI)
584 priv->icount.rng++;
Johan Hovold50188602013-03-19 09:21:11 +0100585 wake_up_interruptible(&port->delta_msr_wait);
bart.hartgers@gmail.com62d826c2009-10-28 10:43:29 +0100586 }
587}
588
589static void ark3116_update_lsr(struct usb_serial_port *port, __u8 lsr)
590{
591 struct ark3116_private *priv = usb_get_serial_port_data(port);
592 unsigned long flags;
593
594 spin_lock_irqsave(&priv->status_lock, flags);
595 /* combine bits */
596 priv->lsr |= lsr;
597 spin_unlock_irqrestore(&priv->status_lock, flags);
598
599 if (lsr&UART_LSR_BRK_ERROR_BITS) {
600 if (lsr & UART_LSR_BI)
601 priv->icount.brk++;
602 if (lsr & UART_LSR_FE)
603 priv->icount.frame++;
604 if (lsr & UART_LSR_PE)
605 priv->icount.parity++;
606 if (lsr & UART_LSR_OE)
607 priv->icount.overrun++;
608 }
609}
610
611static void ark3116_read_int_callback(struct urb *urb)
612{
613 struct usb_serial_port *port = urb->context;
614 int status = urb->status;
615 const __u8 *data = urb->transfer_buffer;
616 int result;
617
618 switch (status) {
619 case -ECONNRESET:
620 case -ENOENT:
621 case -ESHUTDOWN:
622 /* this urb is terminated, clean up */
Greg Kroah-Hartmand2f20e12012-05-15 16:27:11 -0700623 dev_dbg(&port->dev, "%s - urb shutting down with status: %d\n",
624 __func__, status);
bart.hartgers@gmail.com62d826c2009-10-28 10:43:29 +0100625 return;
626 default:
Greg Kroah-Hartmand2f20e12012-05-15 16:27:11 -0700627 dev_dbg(&port->dev, "%s - nonzero urb status received: %d\n",
628 __func__, status);
bart.hartgers@gmail.com62d826c2009-10-28 10:43:29 +0100629 break;
630 case 0: /* success */
631 /* discovered this by trail and error... */
632 if ((urb->actual_length == 4) && (data[0] == 0xe8)) {
633 const __u8 id = data[1]&UART_IIR_ID;
Greg Kroah-Hartmand2f20e12012-05-15 16:27:11 -0700634 dev_dbg(&port->dev, "%s: iir=%02x\n", __func__, data[1]);
bart.hartgers@gmail.com62d826c2009-10-28 10:43:29 +0100635 if (id == UART_IIR_MSI) {
Greg Kroah-Hartmand2f20e12012-05-15 16:27:11 -0700636 dev_dbg(&port->dev, "%s: msr=%02x\n",
637 __func__, data[3]);
bart.hartgers@gmail.com62d826c2009-10-28 10:43:29 +0100638 ark3116_update_msr(port, data[3]);
639 break;
640 } else if (id == UART_IIR_RLSI) {
Greg Kroah-Hartmand2f20e12012-05-15 16:27:11 -0700641 dev_dbg(&port->dev, "%s: lsr=%02x\n",
642 __func__, data[2]);
bart.hartgers@gmail.com62d826c2009-10-28 10:43:29 +0100643 ark3116_update_lsr(port, data[2]);
644 break;
645 }
646 }
647 /*
648 * Not sure what this data meant...
649 */
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +0100650 usb_serial_debug_data(&port->dev, __func__,
bart.hartgers@gmail.com62d826c2009-10-28 10:43:29 +0100651 urb->actual_length,
652 urb->transfer_buffer);
653 break;
654 }
655
656 result = usb_submit_urb(urb, GFP_ATOMIC);
657 if (result)
658 dev_err(&urb->dev->dev,
659 "%s - Error %d submitting interrupt urb\n",
660 __func__, result);
661}
662
663
664/* Data comes in via the bulk (data) URB, erors/interrupts via the int URB.
665 * This means that we cannot be sure which data byte has an associated error
666 * condition, so we report an error for all data in the next bulk read.
667 *
668 * Actually, there might even be a window between the bulk data leaving the
669 * ark and reading/resetting the lsr in the read_bulk_callback where an
670 * interrupt for the next data block could come in.
671 * Without somekind of ordering on the ark, we would have to report the
672 * error for the next block of data as well...
673 * For now, let's pretend this can't happen.
674 */
Johan Hovold82b71cf2010-05-07 20:45:34 +0200675static void ark3116_process_read_urb(struct urb *urb)
bart.hartgers@gmail.com62d826c2009-10-28 10:43:29 +0100676{
Johan Hovold82b71cf2010-05-07 20:45:34 +0200677 struct usb_serial_port *port = urb->context;
bart.hartgers@gmail.com62d826c2009-10-28 10:43:29 +0100678 struct ark3116_private *priv = usb_get_serial_port_data(port);
Johan Hovold82b71cf2010-05-07 20:45:34 +0200679 unsigned char *data = urb->transfer_buffer;
680 char tty_flag = TTY_NORMAL;
bart.hartgers@gmail.com62d826c2009-10-28 10:43:29 +0100681 unsigned long flags;
bart.hartgers@gmail.com62d826c2009-10-28 10:43:29 +0100682 __u32 lsr;
683
Johan Hovold82b71cf2010-05-07 20:45:34 +0200684 /* update line status */
685 spin_lock_irqsave(&priv->status_lock, flags);
686 lsr = priv->lsr;
687 priv->lsr &= ~UART_LSR_BRK_ERROR_BITS;
688 spin_unlock_irqrestore(&priv->status_lock, flags);
689
690 if (!urb->actual_length)
bart.hartgers@gmail.com62d826c2009-10-28 10:43:29 +0100691 return;
bart.hartgers@gmail.com62d826c2009-10-28 10:43:29 +0100692
Johan Hovold82b71cf2010-05-07 20:45:34 +0200693 if (lsr & UART_LSR_BRK_ERROR_BITS) {
694 if (lsr & UART_LSR_BI)
695 tty_flag = TTY_BREAK;
696 else if (lsr & UART_LSR_PE)
697 tty_flag = TTY_PARITY;
698 else if (lsr & UART_LSR_FE)
699 tty_flag = TTY_FRAME;
bart.hartgers@gmail.com62d826c2009-10-28 10:43:29 +0100700
Johan Hovold82b71cf2010-05-07 20:45:34 +0200701 /* overrun is special, not associated with a char */
702 if (lsr & UART_LSR_OE)
Jiri Slaby92a19f92013-01-03 15:53:03 +0100703 tty_insert_flip_char(&port->port, 0, TTY_OVERRUN);
bart.hartgers@gmail.com62d826c2009-10-28 10:43:29 +0100704 }
Jiri Slaby2f693352013-01-03 15:53:02 +0100705 tty_insert_flip_string_fixed_flag(&port->port, data, tty_flag,
Johan Hovold82b71cf2010-05-07 20:45:34 +0200706 urb->actual_length);
Jiri Slaby2e124b42013-01-03 15:53:06 +0100707 tty_flip_buffer_push(&port->port);
bart.hartgers@gmail.com62d826c2009-10-28 10:43:29 +0100708}
709
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700710static struct usb_serial_driver ark3116_device = {
711 .driver = {
712 .owner = THIS_MODULE,
713 .name = "ark3116",
714 },
715 .id_table = id_table,
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700716 .num_ports = 1,
717 .attach = ark3116_attach,
Johan Hovold7bdce712012-10-15 18:20:52 +0200718 .port_probe = ark3116_port_probe,
719 .port_remove = ark3116_port_remove,
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700720 .set_termios = ark3116_set_termios,
Alan Coxfe1ae7f2009-09-19 13:13:33 -0700721 .init_termios = ark3116_init_termios,
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700722 .ioctl = ark3116_ioctl,
723 .tiocmget = ark3116_tiocmget,
bart.hartgers@gmail.com546b7422009-10-28 10:43:28 +0100724 .tiocmset = ark3116_tiocmset,
Alan Cox0bca1b92010-09-16 18:21:40 +0100725 .get_icount = ark3116_get_icount,
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700726 .open = ark3116_open,
bart.hartgers@gmail.comf4c1e8d2009-10-28 10:43:26 +0100727 .close = ark3116_close,
bart.hartgers@gmail.com546b7422009-10-28 10:43:28 +0100728 .break_ctl = ark3116_break_ctl,
bart.hartgers@gmail.com62d826c2009-10-28 10:43:29 +0100729 .read_int_callback = ark3116_read_int_callback,
Johan Hovold82b71cf2010-05-07 20:45:34 +0200730 .process_read_urb = ark3116_process_read_urb,
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700731};
732
Alan Stern08a4f6b2012-02-23 14:56:17 -0500733static struct usb_serial_driver * const serial_drivers[] = {
734 &ark3116_device, NULL
735};
736
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -0700737module_usb_serial_driver(serial_drivers, id_table);
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700738
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700739MODULE_LICENSE("GPL");
740
bart.hartgers@gmail.com149fc792009-10-28 10:43:25 +0100741MODULE_AUTHOR(DRIVER_AUTHOR);
742MODULE_DESCRIPTION(DRIVER_DESC);
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700743
bart.hartgers@gmail.com149fc792009-10-28 10:43:25 +0100744/*
745 * The following describes what I learned from studying the old
746 * ark3116.c driver, disassembling the windows driver, and some lucky
747 * guesses. Since I do not have any datasheet or other
748 * documentation, inaccuracies are almost guaranteed.
749 *
750 * Some specs for the ARK3116 can be found here:
751 * http://web.archive.org/web/20060318000438/
752 * www.arkmicro.com/en/products/view.php?id=10
753 * On that page, 2 GPIO pins are mentioned: I assume these are the
754 * OUT1 and OUT2 pins of the UART, so I added support for those
755 * through the MCR. Since the pins are not available on my hardware,
756 * I could not verify this.
757 * Also, it states there is "on-chip hardware flow control". I have
758 * discovered how to enable that. Unfortunately, I do not know how to
759 * enable XON/XOFF (software) flow control, which would need support
760 * from the chip as well to work. Because of the wording on the web
761 * page there is a real possibility the chip simply does not support
762 * software flow control.
763 *
764 * I got my ark3116 as part of a mobile phone adapter cable. On the
765 * PCB, the following numbered contacts are present:
766 *
767 * 1:- +5V
768 * 2:o DTR
769 * 3:i RX
770 * 4:i DCD
771 * 5:o RTS
772 * 6:o TX
773 * 7:i RI
774 * 8:i DSR
775 * 10:- 0V
776 * 11:i CTS
777 *
778 * On my chip, all signals seem to be 3.3V, but 5V tolerant. But that
779 * may be different for the one you have ;-).
780 *
781 * The windows driver limits the registers to 0-F, so I assume there
782 * are actually 16 present on the device.
783 *
784 * On an UART interrupt, 4 bytes of data come in on the interrupt
785 * endpoint. The bytes are 0xe8 IIR LSR MSR.
786 *
787 * The baudrate seems to be generated from the 12MHz crystal, using
788 * 4-times subsampling. So quot=12e6/(4*baud). Also see description
789 * of register E.
790 *
791 * Registers 0-7:
792 * These seem to be the same as for a regular 16450. The FCR is set
793 * to UART_FCR_DMA_SELECT (0x8), I guess to enable transfers between
794 * the UART and the USB bridge/DMA engine.
795 *
796 * Register 8:
797 * By trial and error, I found out that bit 0 enables hardware CTS,
798 * stopping TX when CTS is +5V. Bit 1 does the same for RTS, making
799 * RTS +5V when the 3116 cannot transfer the data to the USB bus
800 * (verified by disabling the reading URB). Note that as far as I can
801 * tell, the windows driver does NOT use this, so there might be some
802 * hardware bug or something.
803 *
804 * According to a patch provided here
805 * (http://lkml.org/lkml/2009/7/26/56), the ARK3116 can also be used
806 * as an IrDA dongle. Since I do not have such a thing, I could not
807 * investigate that aspect. However, I can speculate ;-).
808 *
809 * - IrDA encodes data differently than RS232. Most likely, one of
810 * the bits in registers 9..E enables the IR ENDEC (encoder/decoder).
811 * - Depending on the IR transceiver, the input and output need to be
812 * inverted, so there are probably bits for that as well.
813 * - IrDA is half-duplex, so there should be a bit for selecting that.
814 *
815 * This still leaves at least two registers unaccounted for. Perhaps
816 * The chip can do XON/XOFF or CRC in HW?
817 *
818 * Register 9:
819 * Set to 0x00 for IrDA, when the baudrate is initialised.
820 *
821 * Register A:
822 * Set to 0x01 for IrDA, at init.
823 *
824 * Register B:
825 * Set to 0x01 for IrDA, 0x00 for RS232, at init.
826 *
827 * Register C:
828 * Set to 00 for IrDA, at init.
829 *
830 * Register D:
831 * Set to 0x41 for IrDA, at init.
832 *
833 * Register E:
834 * Somekind of baudrate override. The windows driver seems to set
835 * this to 0x00 for normal baudrates, 0x01 for 460800, 0x02 for 921600.
836 * Since 460800 and 921600 cannot be obtained by dividing 3MHz by an integer,
837 * it could be somekind of subdivisor thingy.
838 * However,it does not seem to do anything: selecting 921600 (divisor 3,
839 * reg E=2), still gets 1 MHz. I also checked if registers 9, C or F would
840 * work, but they don't.
841 *
842 * Register F: unknown
843 */