blob: 067daf4db6469ace93d255f8b0f02e5b408fb4c5 [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>
bart.hartgers@gmail.com149fc792009-10-28 10:43:25 +010029#include <linux/tty_flip.h>
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -070030#include <linux/module.h>
31#include <linux/usb.h>
Greg Kroah-Hartmana9698882006-07-11 21:22:58 -070032#include <linux/usb/serial.h>
Werner Lemberg2f430b42006-07-18 17:00:52 +020033#include <linux/serial.h>
bart.hartgers@gmail.com149fc792009-10-28 10:43:25 +010034#include <linux/serial_reg.h>
Alan Coxc4d0f8c2008-04-29 14:35:39 +010035#include <linux/uaccess.h>
bart.hartgers@gmail.com149fc792009-10-28 10:43:25 +010036#include <linux/mutex.h>
37#include <linux/spinlock.h>
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -070038
39static int debug;
bart.hartgers@gmail.com149fc792009-10-28 10:43:25 +010040/*
41 * Version information
42 */
43
44#define DRIVER_VERSION "v0.5"
45#define DRIVER_AUTHOR "Bart Hartgers <bart.hartgers+ark3116@gmail.com>"
46#define DRIVER_DESC "USB ARK3116 serial/IrDA driver"
47#define DRIVER_DEV_DESC "ARK3116 RS232/IrDA"
48#define DRIVER_NAME "ark3116"
49
50/* usb timeout of 1 second */
51#define ARK_TIMEOUT (1*HZ)
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -070052
53static struct usb_device_id id_table [] = {
54 { USB_DEVICE(0x6547, 0x0232) },
Ondrej Zary5128a662009-08-06 16:09:52 -070055 { USB_DEVICE(0x18ec, 0x3118) }, /* USB to IrDA adapter */
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -070056 { },
57};
58MODULE_DEVICE_TABLE(usb, id_table);
59
Ondrej Zary5128a662009-08-06 16:09:52 -070060static int is_irda(struct usb_serial *serial)
61{
62 struct usb_device *dev = serial->dev;
63 if (le16_to_cpu(dev->descriptor.idVendor) == 0x18ec &&
64 le16_to_cpu(dev->descriptor.idProduct) == 0x3118)
65 return 1;
66 return 0;
67}
68
bart.hartgers@gmail.com149fc792009-10-28 10:43:25 +010069struct ark3116_private {
70 wait_queue_head_t delta_msr_wait;
71 struct async_icount icount;
72 int irda; /* 1 for irda device */
73
74 /* protects hw register updates */
75 struct mutex hw_lock;
76
77 int quot; /* baudrate divisor */
78 __u32 lcr; /* line control register value */
79 __u32 hcr; /* handshake control register (0x8)
80 * value */
81 __u32 mcr; /* modem contol register value */
82
83 /* protects the status values below */
84 spinlock_t status_lock;
85 __u32 msr; /* modem status register value */
86 __u32 lsr; /* line status register value */
87};
88
89static int ark3116_write_reg(struct usb_serial *serial,
90 unsigned reg, __u8 val)
91{
92 int result;
93 /* 0xfe 0x40 are magic values taken from original driver */
94 result = usb_control_msg(serial->dev,
95 usb_sndctrlpipe(serial->dev, 0),
96 0xfe, 0x40, val, reg,
97 NULL, 0, ARK_TIMEOUT);
98 return result;
99}
100
101static int ark3116_read_reg(struct usb_serial *serial,
102 unsigned reg, unsigned char *buf)
103{
104 int result;
105 /* 0xfe 0xc0 are magic values taken from original driver */
106 result = usb_control_msg(serial->dev,
107 usb_rcvctrlpipe(serial->dev, 0),
108 0xfe, 0xc0, 0, reg,
109 buf, 1, ARK_TIMEOUT);
110 if (result < 0)
111 return result;
112 else
113 return buf[0];
114}
115
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700116static inline void ARK3116_SND(struct usb_serial *serial, int seq,
117 __u8 request, __u8 requesttype,
118 __u16 value, __u16 index)
119{
120 int result;
121 result = usb_control_msg(serial->dev,
Werner Lemberg988440e2006-07-18 17:00:22 +0200122 usb_sndctrlpipe(serial->dev, 0),
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700123 request, requesttype, value, index,
Werner Lemberg988440e2006-07-18 17:00:22 +0200124 NULL, 0x00, 1000);
125 dbg("%03d > ok", seq);
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700126}
127
128static inline void ARK3116_RCV(struct usb_serial *serial, int seq,
129 __u8 request, __u8 requesttype,
130 __u16 value, __u16 index, __u8 expected,
131 char *buf)
132{
133 int result;
134 result = usb_control_msg(serial->dev,
Werner Lemberg988440e2006-07-18 17:00:22 +0200135 usb_rcvctrlpipe(serial->dev, 0),
136 request, requesttype, value, index,
137 buf, 0x0000001, 1000);
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700138 if (result)
Jan Engelhardtb268f482007-05-17 17:16:44 +0200139 dbg("%03d < %d bytes [0x%02X]", seq, result,
140 ((unsigned char *)buf)[0]);
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700141 else
142 dbg("%03d < 0 bytes", seq);
143}
144
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700145static inline void ARK3116_RCV_QUIET(struct usb_serial *serial,
146 __u8 request, __u8 requesttype,
147 __u16 value, __u16 index, char *buf)
148{
149 usb_control_msg(serial->dev,
Werner Lemberg988440e2006-07-18 17:00:22 +0200150 usb_rcvctrlpipe(serial->dev, 0),
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700151 request, requesttype, value, index,
152 buf, 0x0000001, 1000);
153}
154
bart.hartgers@gmail.comf4c1e8d2009-10-28 10:43:26 +0100155static inline int calc_divisor(int bps)
156{
157 /* Original ark3116 made some exceptions in rounding here
158 * because windows did the same. Assume that is not really
159 * necessary.
160 * Crystal is 12MHz, probably because of USB, but we divide by 4?
161 */
162 return (12000000 + 2*bps) / (4*bps);
163}
164
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700165static int ark3116_attach(struct usb_serial *serial)
166{
bart.hartgers@gmail.comf4c1e8d2009-10-28 10:43:26 +0100167 struct usb_serial_port *port = serial->port[0];
168 struct ark3116_private *priv;
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700169
bart.hartgers@gmail.comf4c1e8d2009-10-28 10:43:26 +0100170 /* make sure we have our end-points */
171 if ((serial->num_bulk_in == 0) ||
172 (serial->num_bulk_out == 0) ||
173 (serial->num_interrupt_in == 0)) {
174 dev_err(&serial->dev->dev,
175 "%s - missing endpoint - "
176 "bulk in: %d, bulk out: %d, int in %d\n",
177 KBUILD_MODNAME,
178 serial->num_bulk_in,
179 serial->num_bulk_out,
180 serial->num_interrupt_in);
181 return -EINVAL;
182 }
183
184 priv = kzalloc(sizeof(struct ark3116_private),
185 GFP_KERNEL);
186 if (!priv)
Alan Coxfe1ae7f2009-09-19 13:13:33 -0700187 return -ENOMEM;
bart.hartgers@gmail.comf4c1e8d2009-10-28 10:43:26 +0100188
189 init_waitqueue_head(&priv->delta_msr_wait);
190 mutex_init(&priv->hw_lock);
191 spin_lock_init(&priv->status_lock);
192
193 priv->irda = is_irda(serial);
194
195 usb_set_serial_port_data(port, priv);
196
197 /* setup the hardware */
198 ark3116_write_reg(serial, UART_IER, 0);
199 /* disable DMA */
200 ark3116_write_reg(serial, UART_FCR, 0);
201 /* handshake control */
202 priv->hcr = 0;
203 ark3116_write_reg(serial, 0x8 , 0);
204 /* modem control */
205 priv->mcr = 0;
206 ark3116_write_reg(serial, UART_MCR, 0);
207
208 if (!(priv->irda)) {
209 ark3116_write_reg(serial, 0xb , 0);
210 } else {
211 ark3116_write_reg(serial, 0xb , 1);
212 ark3116_write_reg(serial, 0xc , 0);
213 ark3116_write_reg(serial, 0xd , 0x41);
214 ark3116_write_reg(serial, 0xa , 1);
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700215 }
216
bart.hartgers@gmail.comf4c1e8d2009-10-28 10:43:26 +0100217 /* setup baudrate */
218 ark3116_write_reg(serial, UART_LCR, UART_LCR_DLAB);
Ondrej Zary5128a662009-08-06 16:09:52 -0700219
bart.hartgers@gmail.comf4c1e8d2009-10-28 10:43:26 +0100220 /* setup for 9600 8N1 */
221 priv->quot = calc_divisor(9600);
222 ark3116_write_reg(serial, UART_DLL, priv->quot & 0xff);
223 ark3116_write_reg(serial, UART_DLM, (priv->quot>>8) & 0xff);
Ondrej Zary5128a662009-08-06 16:09:52 -0700224
bart.hartgers@gmail.comf4c1e8d2009-10-28 10:43:26 +0100225 priv->lcr = UART_LCR_WLEN8;
226 ark3116_write_reg(serial, UART_LCR, UART_LCR_WLEN8);
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700227
bart.hartgers@gmail.comf4c1e8d2009-10-28 10:43:26 +0100228 ark3116_write_reg(serial, 0xe, 0);
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700229
bart.hartgers@gmail.comf4c1e8d2009-10-28 10:43:26 +0100230 if (priv->irda)
231 ark3116_write_reg(serial, 0x9, 0);
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700232
bart.hartgers@gmail.comf4c1e8d2009-10-28 10:43:26 +0100233 dev_info(&serial->dev->dev,
234 "%s using %s mode\n",
235 KBUILD_MODNAME,
236 priv->irda ? "IrDA" : "RS232");
Werner Lemberg988440e2006-07-18 17:00:22 +0200237 return 0;
Alan Coxfe1ae7f2009-09-19 13:13:33 -0700238}
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700239
bart.hartgers@gmail.comf4c1e8d2009-10-28 10:43:26 +0100240static void ark3116_release(struct usb_serial *serial)
241{
242 struct usb_serial_port *port = serial->port[0];
243 struct ark3116_private *priv = usb_get_serial_port_data(port);
244
245 /* device is closed, so URBs and DMA should be down */
246
247 usb_set_serial_port_data(port, NULL);
248
249 mutex_destroy(&priv->hw_lock);
250
251 kfree(priv);
252}
253
Alan Coxfe1ae7f2009-09-19 13:13:33 -0700254static void ark3116_init_termios(struct tty_struct *tty)
255{
256 struct ktermios *termios = tty->termios;
257 *termios = tty_std_termios;
258 termios->c_cflag = B9600 | CS8
259 | CREAD | HUPCL | CLOCAL;
260 termios->c_ispeed = 9600;
261 termios->c_ospeed = 9600;
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700262}
263
Alan Cox95da3102008-07-22 11:09:07 +0100264static void ark3116_set_termios(struct tty_struct *tty,
265 struct usb_serial_port *port,
Alan Cox606d0992006-12-08 02:38:45 -0800266 struct ktermios *old_termios)
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700267{
268 struct usb_serial *serial = port->serial;
bart.hartgers@gmail.comf4c1e8d2009-10-28 10:43:26 +0100269 struct ark3116_private *priv = usb_get_serial_port_data(port);
Alan Cox95da3102008-07-22 11:09:07 +0100270 struct ktermios *termios = tty->termios;
Alan Coxadb5dca2007-10-18 01:24:17 -0700271 unsigned int cflag = termios->c_cflag;
bart.hartgers@gmail.comf4c1e8d2009-10-28 10:43:26 +0100272 int bps = tty_get_baud_rate(tty);
273 int quot;
274 __u8 lcr, hcr, eval;
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700275
bart.hartgers@gmail.comf4c1e8d2009-10-28 10:43:26 +0100276 /* set data bit count */
277 switch (cflag & CSIZE) {
278 case CS5:
279 lcr = UART_LCR_WLEN5;
Alan Coxc4d0f8c2008-04-29 14:35:39 +0100280 break;
bart.hartgers@gmail.comf4c1e8d2009-10-28 10:43:26 +0100281 case CS6:
282 lcr = UART_LCR_WLEN6;
283 break;
284 case CS7:
285 lcr = UART_LCR_WLEN7;
286 break;
Alan Coxc4d0f8c2008-04-29 14:35:39 +0100287 default:
bart.hartgers@gmail.comf4c1e8d2009-10-28 10:43:26 +0100288 case CS8:
289 lcr = UART_LCR_WLEN8;
290 break;
291 }
292 if (cflag & CSTOPB)
293 lcr |= UART_LCR_STOP;
294 if (cflag & PARENB)
295 lcr |= UART_LCR_PARITY;
296 if (!(cflag & PARODD))
297 lcr |= UART_LCR_EPAR;
298#ifdef CMSPAR
299 if (cflag & CMSPAR)
300 lcr |= UART_LCR_SPAR;
301#endif
302 /* handshake control */
303 hcr = (cflag & CRTSCTS) ? 0x03 : 0x00;
304
305 /* calc baudrate */
306 dbg("%s - setting bps to %d", __func__, bps);
307 eval = 0;
308 switch (bps) {
Alan Coxc4d0f8c2008-04-29 14:35:39 +0100309 case 0:
bart.hartgers@gmail.comf4c1e8d2009-10-28 10:43:26 +0100310 quot = calc_divisor(9600);
311 break;
312 default:
313 if ((bps < 75) || (bps > 3000000))
314 bps = 9600;
315 quot = calc_divisor(bps);
316 break;
317 case 460800:
318 eval = 1;
319 quot = calc_divisor(bps);
320 break;
321 case 921600:
322 eval = 2;
323 quot = calc_divisor(bps);
324 break;
Alan Cox568c24a2007-06-22 14:36:29 +0100325 }
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700326
bart.hartgers@gmail.comf4c1e8d2009-10-28 10:43:26 +0100327 /* Update state: synchronize */
328 mutex_lock(&priv->hw_lock);
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700329
bart.hartgers@gmail.comf4c1e8d2009-10-28 10:43:26 +0100330 /* keep old LCR_SBC bit */
331 lcr |= (priv->lcr & UART_LCR_SBC);
Werner Lemberg988440e2006-07-18 17:00:22 +0200332
bart.hartgers@gmail.comf4c1e8d2009-10-28 10:43:26 +0100333 dbg("%s - setting hcr:0x%02x,lcr:0x%02x,quot:%d",
334 __func__, hcr, lcr, quot);
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700335
bart.hartgers@gmail.comf4c1e8d2009-10-28 10:43:26 +0100336 /* handshake control */
337 if (priv->hcr != hcr) {
338 priv->hcr = hcr;
339 ark3116_write_reg(serial, 0x8, hcr);
340 }
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700341
bart.hartgers@gmail.comf4c1e8d2009-10-28 10:43:26 +0100342 /* baudrate */
343 if (priv->quot != quot) {
344 priv->quot = quot;
345 priv->lcr = lcr; /* need to write lcr anyway */
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700346
bart.hartgers@gmail.comf4c1e8d2009-10-28 10:43:26 +0100347 /* disable DMA since transmit/receive is
348 * shadowed by UART_DLL
349 */
350 ark3116_write_reg(serial, UART_FCR, 0);
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700351
bart.hartgers@gmail.comf4c1e8d2009-10-28 10:43:26 +0100352 ark3116_write_reg(serial, UART_LCR,
353 lcr|UART_LCR_DLAB);
354 ark3116_write_reg(serial, UART_DLL, quot & 0xff);
355 ark3116_write_reg(serial, UART_DLM, (quot>>8) & 0xff);
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700356
bart.hartgers@gmail.comf4c1e8d2009-10-28 10:43:26 +0100357 /* restore lcr */
358 ark3116_write_reg(serial, UART_LCR, lcr);
359 /* magic baudrate thingy: not sure what it does,
360 * but windows does this as well.
361 */
362 ark3116_write_reg(serial, 0xe, eval);
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700363
bart.hartgers@gmail.comf4c1e8d2009-10-28 10:43:26 +0100364 /* enable DMA */
365 ark3116_write_reg(serial, UART_FCR, UART_FCR_DMA_SELECT);
366 } else if (priv->lcr != lcr) {
367 priv->lcr = lcr;
368 ark3116_write_reg(serial, UART_LCR, lcr);
369 }
Alan Coxadb5dca2007-10-18 01:24:17 -0700370
bart.hartgers@gmail.comf4c1e8d2009-10-28 10:43:26 +0100371 mutex_unlock(&priv->hw_lock);
372
373 /* check for software flow control */
374 if (I_IXOFF(tty) || I_IXON(tty)) {
375 dev_warn(&serial->dev->dev,
376 "%s: don't know how to do software flow control\n",
377 KBUILD_MODNAME);
378 }
379
380 /* Don't rewrite B0 */
381 if (tty_termios_baud_rate(termios))
382 tty_termios_encode_baud_rate(termios, bps, bps);
383}
384
385static void ark3116_close(struct usb_serial_port *port)
386{
387 struct usb_serial *serial = port->serial;
388
389 if (serial->dev) {
390 /* disable DMA */
391 ark3116_write_reg(serial, UART_FCR, 0);
392
393 /* deactivate interrupts */
394 ark3116_write_reg(serial, UART_IER, 0);
395
396 /* shutdown any bulk reads that might be going on */
397 if (serial->num_bulk_out)
398 usb_kill_urb(port->write_urb);
399 if (serial->num_bulk_in)
400 usb_kill_urb(port->read_urb);
401 if (serial->num_interrupt_in)
402 usb_kill_urb(port->interrupt_in_urb);
403 }
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700404}
405
Alan Coxa509a7e2009-09-19 13:13:26 -0700406static int ark3116_open(struct tty_struct *tty, struct usb_serial_port *port)
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700407{
bart.hartgers@gmail.comf4c1e8d2009-10-28 10:43:26 +0100408 struct ark3116_private *priv = usb_get_serial_port_data(port);
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700409 struct usb_serial *serial = port->serial;
bart.hartgers@gmail.comf4c1e8d2009-10-28 10:43:26 +0100410 unsigned char *buf;
411 int result;
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700412
413 buf = kmalloc(1, GFP_KERNEL);
bart.hartgers@gmail.comf4c1e8d2009-10-28 10:43:26 +0100414 if (buf == NULL)
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700415 return -ENOMEM;
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700416
Alan Coxa509a7e2009-09-19 13:13:26 -0700417 result = usb_serial_generic_open(tty, port);
bart.hartgers@gmail.comf4c1e8d2009-10-28 10:43:26 +0100418 if (result) {
419 dbg("%s - usb_serial_generic_open failed: %d",
420 __func__, result);
Oliver Neukum4edf2c82007-03-26 18:12:44 +0200421 goto err_out;
bart.hartgers@gmail.comf4c1e8d2009-10-28 10:43:26 +0100422 }
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700423
bart.hartgers@gmail.comf4c1e8d2009-10-28 10:43:26 +0100424 /* setup termios */
Alan Cox95da3102008-07-22 11:09:07 +0100425 if (tty)
bart.hartgers@gmail.comf4c1e8d2009-10-28 10:43:26 +0100426 ark3116_set_termios(tty, port, NULL);
427
428 /* remove any data still left: also clears error state */
429 ark3116_read_reg(serial, UART_RX, buf);
430
431 /* read modem status */
432 priv->msr = ark3116_read_reg(serial, UART_MSR, buf);
433 /* read line status */
434 priv->lsr = ark3116_read_reg(serial, UART_LSR, buf);
435
436 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
437 if (result) {
438 dev_err(&port->dev, "submit irq_in urb failed %d\n",
439 result);
440 ark3116_close(port);
441 goto err_out;
442 }
443
444 /* activate interrupts */
445 ark3116_write_reg(port->serial, UART_IER, UART_IER_MSI|UART_IER_RLSI);
446
447 /* enable DMA */
448 ark3116_write_reg(port->serial, UART_FCR, UART_FCR_DMA_SELECT);
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700449
Oliver Neukum4edf2c82007-03-26 18:12:44 +0200450err_out:
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700451 kfree(buf);
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700452 return result;
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700453}
454
Alan Cox95da3102008-07-22 11:09:07 +0100455static int ark3116_ioctl(struct tty_struct *tty, struct file *file,
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700456 unsigned int cmd, unsigned long arg)
457{
Alan Cox95da3102008-07-22 11:09:07 +0100458 struct usb_serial_port *port = tty->driver_data;
bart.hartgers@gmail.comf4c1e8d2009-10-28 10:43:26 +0100459 struct ark3116_private *priv = usb_get_serial_port_data(port);
Werner Lemberg2f430b42006-07-18 17:00:52 +0200460 struct serial_struct serstruct;
461 void __user *user_arg = (void __user *)arg;
462
463 switch (cmd) {
464 case TIOCGSERIAL:
465 /* XXX: Some of these values are probably wrong. */
Alan Coxc4d0f8c2008-04-29 14:35:39 +0100466 memset(&serstruct, 0, sizeof(serstruct));
Werner Lemberg2f430b42006-07-18 17:00:52 +0200467 serstruct.type = PORT_16654;
468 serstruct.line = port->serial->minor;
469 serstruct.port = port->number;
470 serstruct.custom_divisor = 0;
471 serstruct.baud_base = 460800;
472
Alan Coxc4d0f8c2008-04-29 14:35:39 +0100473 if (copy_to_user(user_arg, &serstruct, sizeof(serstruct)))
Werner Lemberg2f430b42006-07-18 17:00:52 +0200474 return -EFAULT;
475
476 return 0;
477 case TIOCSSERIAL:
Alan Coxc4d0f8c2008-04-29 14:35:39 +0100478 if (copy_from_user(&serstruct, user_arg, sizeof(serstruct)))
Werner Lemberg2f430b42006-07-18 17:00:52 +0200479 return -EFAULT;
480 return 0;
bart.hartgers@gmail.comf4c1e8d2009-10-28 10:43:26 +0100481 case TIOCMIWAIT:
482 for (;;) {
483 struct async_icount prev = priv->icount;
484 interruptible_sleep_on(&priv->delta_msr_wait);
485 /* see if a signal did it */
486 if (signal_pending(current))
487 return -ERESTARTSYS;
488 if ((prev.rng == priv->icount.rng) &&
489 (prev.dsr == priv->icount.dsr) &&
490 (prev.dcd == priv->icount.dcd) &&
491 (prev.cts == priv->icount.cts))
492 return -EIO;
493 if ((arg & TIOCM_RNG &&
494 (prev.rng != priv->icount.rng)) ||
495 (arg & TIOCM_DSR &&
496 (prev.dsr != priv->icount.dsr)) ||
497 (arg & TIOCM_CD &&
498 (prev.dcd != priv->icount.dcd)) ||
499 (arg & TIOCM_CTS &&
500 (prev.cts != priv->icount.cts)))
501 return 0;
502 }
Werner Lemberg2f430b42006-07-18 17:00:52 +0200503 break;
bart.hartgers@gmail.comf4c1e8d2009-10-28 10:43:26 +0100504 case TIOCGICOUNT: {
505 struct serial_icounter_struct icount;
506 struct async_icount cnow = priv->icount;
507 memset(&icount, 0, sizeof(icount));
508 icount.cts = cnow.cts;
509 icount.dsr = cnow.dsr;
510 icount.rng = cnow.rng;
511 icount.dcd = cnow.dcd;
512 icount.rx = cnow.rx;
513 icount.tx = cnow.tx;
514 icount.frame = cnow.frame;
515 icount.overrun = cnow.overrun;
516 icount.parity = cnow.parity;
517 icount.brk = cnow.brk;
518 icount.buf_overrun = cnow.buf_overrun;
519 if (copy_to_user(user_arg, &icount, sizeof(icount)))
520 return -EFAULT;
521 return 0;
522 }
Werner Lemberg2f430b42006-07-18 17:00:52 +0200523 }
524
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700525 return -ENOIOCTLCMD;
526}
527
Alan Cox95da3102008-07-22 11:09:07 +0100528static int ark3116_tiocmget(struct tty_struct *tty, struct file *file)
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700529{
Alan Cox95da3102008-07-22 11:09:07 +0100530 struct usb_serial_port *port = tty->driver_data;
bart.hartgers@gmail.com1f719102009-10-28 10:43:27 +0100531 struct ark3116_private *priv = usb_get_serial_port_data(port);
532 __u32 status;
533 __u32 ctrl;
534 unsigned long flags;
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700535
bart.hartgers@gmail.com1f719102009-10-28 10:43:27 +0100536 mutex_lock(&priv->hw_lock);
537 ctrl = priv->mcr;
538 mutex_unlock(&priv->hw_lock);
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700539
bart.hartgers@gmail.com1f719102009-10-28 10:43:27 +0100540 spin_lock_irqsave(&priv->status_lock, flags);
541 status = priv->msr;
542 spin_unlock_irqrestore(&priv->status_lock, flags);
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700543
bart.hartgers@gmail.com1f719102009-10-28 10:43:27 +0100544 return (status & UART_MSR_DSR ? TIOCM_DSR : 0) |
545 (status & UART_MSR_CTS ? TIOCM_CTS : 0) |
546 (status & UART_MSR_RI ? TIOCM_RI : 0) |
547 (status & UART_MSR_DCD ? TIOCM_CD : 0) |
548 (ctrl & UART_MCR_DTR ? TIOCM_DTR : 0) |
549 (ctrl & UART_MCR_RTS ? TIOCM_RTS : 0) |
550 (ctrl & UART_MCR_OUT1 ? TIOCM_OUT1 : 0) |
551 (ctrl & UART_MCR_OUT2 ? TIOCM_OUT2 : 0);
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700552}
553
bart.hartgers@gmail.com546b7422009-10-28 10:43:28 +0100554static int ark3116_tiocmset(struct tty_struct *tty, struct file *file,
555 unsigned set, unsigned clr)
556{
557 struct usb_serial_port *port = tty->driver_data;
558 struct ark3116_private *priv = usb_get_serial_port_data(port);
559
560 /* we need to take the mutex here, to make sure that the value
561 * in priv->mcr is actually the one that is in the hardware
562 */
563
564 mutex_lock(&priv->hw_lock);
565
566 if (set & TIOCM_RTS)
567 priv->mcr |= UART_MCR_RTS;
568 if (set & TIOCM_DTR)
569 priv->mcr |= UART_MCR_DTR;
570 if (set & TIOCM_OUT1)
571 priv->mcr |= UART_MCR_OUT1;
572 if (set & TIOCM_OUT2)
573 priv->mcr |= UART_MCR_OUT2;
574 if (clr & TIOCM_RTS)
575 priv->mcr &= ~UART_MCR_RTS;
576 if (clr & TIOCM_DTR)
577 priv->mcr &= ~UART_MCR_DTR;
578 if (clr & TIOCM_OUT1)
579 priv->mcr &= ~UART_MCR_OUT1;
580 if (clr & TIOCM_OUT2)
581 priv->mcr &= ~UART_MCR_OUT2;
582
583 ark3116_write_reg(port->serial, UART_MCR, priv->mcr);
584
585 mutex_unlock(&priv->hw_lock);
586
587 return 0;
588}
589
590static void ark3116_break_ctl(struct tty_struct *tty, int break_state)
591{
592 struct usb_serial_port *port = tty->driver_data;
593 struct ark3116_private *priv = usb_get_serial_port_data(port);
594
595 /* LCR is also used for other things: protect access */
596 mutex_lock(&priv->hw_lock);
597
598 if (break_state)
599 priv->lcr |= UART_LCR_SBC;
600 else
601 priv->lcr &= ~UART_LCR_SBC;
602
603 ark3116_write_reg(port->serial, UART_LCR, priv->lcr);
604
605 mutex_unlock(&priv->hw_lock);
606}
607
bart.hartgers@gmail.com62d826c2009-10-28 10:43:29 +0100608static void ark3116_update_msr(struct usb_serial_port *port, __u8 msr)
609{
610 struct ark3116_private *priv = usb_get_serial_port_data(port);
611 unsigned long flags;
612
613 spin_lock_irqsave(&priv->status_lock, flags);
614 priv->msr = msr;
615 spin_unlock_irqrestore(&priv->status_lock, flags);
616
617 if (msr & UART_MSR_ANY_DELTA) {
618 /* update input line counters */
619 if (msr & UART_MSR_DCTS)
620 priv->icount.cts++;
621 if (msr & UART_MSR_DDSR)
622 priv->icount.dsr++;
623 if (msr & UART_MSR_DDCD)
624 priv->icount.dcd++;
625 if (msr & UART_MSR_TERI)
626 priv->icount.rng++;
627 wake_up_interruptible(&priv->delta_msr_wait);
628 }
629}
630
631static void ark3116_update_lsr(struct usb_serial_port *port, __u8 lsr)
632{
633 struct ark3116_private *priv = usb_get_serial_port_data(port);
634 unsigned long flags;
635
636 spin_lock_irqsave(&priv->status_lock, flags);
637 /* combine bits */
638 priv->lsr |= lsr;
639 spin_unlock_irqrestore(&priv->status_lock, flags);
640
641 if (lsr&UART_LSR_BRK_ERROR_BITS) {
642 if (lsr & UART_LSR_BI)
643 priv->icount.brk++;
644 if (lsr & UART_LSR_FE)
645 priv->icount.frame++;
646 if (lsr & UART_LSR_PE)
647 priv->icount.parity++;
648 if (lsr & UART_LSR_OE)
649 priv->icount.overrun++;
650 }
651}
652
653static void ark3116_read_int_callback(struct urb *urb)
654{
655 struct usb_serial_port *port = urb->context;
656 int status = urb->status;
657 const __u8 *data = urb->transfer_buffer;
658 int result;
659
660 switch (status) {
661 case -ECONNRESET:
662 case -ENOENT:
663 case -ESHUTDOWN:
664 /* this urb is terminated, clean up */
665 dbg("%s - urb shutting down with status: %d",
666 __func__, status);
667 return;
668 default:
669 dbg("%s - nonzero urb status received: %d",
670 __func__, status);
671 break;
672 case 0: /* success */
673 /* discovered this by trail and error... */
674 if ((urb->actual_length == 4) && (data[0] == 0xe8)) {
675 const __u8 id = data[1]&UART_IIR_ID;
676 dbg("%s: iir=%02x", __func__, data[1]);
677 if (id == UART_IIR_MSI) {
678 dbg("%s: msr=%02x", __func__, data[3]);
679 ark3116_update_msr(port, data[3]);
680 break;
681 } else if (id == UART_IIR_RLSI) {
682 dbg("%s: lsr=%02x", __func__, data[2]);
683 ark3116_update_lsr(port, data[2]);
684 break;
685 }
686 }
687 /*
688 * Not sure what this data meant...
689 */
690 usb_serial_debug_data(debug, &port->dev,
691 __func__,
692 urb->actual_length,
693 urb->transfer_buffer);
694 break;
695 }
696
697 result = usb_submit_urb(urb, GFP_ATOMIC);
698 if (result)
699 dev_err(&urb->dev->dev,
700 "%s - Error %d submitting interrupt urb\n",
701 __func__, result);
702}
703
704
705/* Data comes in via the bulk (data) URB, erors/interrupts via the int URB.
706 * This means that we cannot be sure which data byte has an associated error
707 * condition, so we report an error for all data in the next bulk read.
708 *
709 * Actually, there might even be a window between the bulk data leaving the
710 * ark and reading/resetting the lsr in the read_bulk_callback where an
711 * interrupt for the next data block could come in.
712 * Without somekind of ordering on the ark, we would have to report the
713 * error for the next block of data as well...
714 * For now, let's pretend this can't happen.
715 */
716
717static void send_to_tty(struct tty_struct *tty,
718 const unsigned char *chars,
719 size_t size, char flag)
720{
721 if (size == 0)
722 return;
723 if (flag == TTY_NORMAL) {
724 tty_insert_flip_string(tty, chars, size);
725 } else {
726 int i;
727 for (i = 0; i < size; ++i)
728 tty_insert_flip_char(tty, chars[i], flag);
729 }
730}
731
732static void ark3116_read_bulk_callback(struct urb *urb)
733{
734 struct usb_serial_port *port = urb->context;
735 struct ark3116_private *priv = usb_get_serial_port_data(port);
736 const __u8 *data = urb->transfer_buffer;
737 int status = urb->status;
738 struct tty_struct *tty;
739 unsigned long flags;
740 int result;
741 char flag;
742 __u32 lsr;
743
744 switch (status) {
745 case -ECONNRESET:
746 case -ENOENT:
747 case -ESHUTDOWN:
748 /* this urb is terminated, clean up */
749 dbg("%s - urb shutting down with status: %d",
750 __func__, status);
751 return;
752 default:
753 dbg("%s - nonzero urb status received: %d",
754 __func__, status);
755 break;
756 case 0: /* success */
757
758 spin_lock_irqsave(&priv->status_lock, flags);
759 lsr = priv->lsr;
760 /* clear error bits */
761 priv->lsr &= ~UART_LSR_BRK_ERROR_BITS;
762 spin_unlock_irqrestore(&priv->status_lock, flags);
763
764 if (unlikely(lsr & UART_LSR_BI))
765 flag = TTY_BREAK;
766 else if (unlikely(lsr & UART_LSR_PE))
767 flag = TTY_PARITY;
768 else if (unlikely(lsr & UART_LSR_FE))
769 flag = TTY_FRAME;
770 else
771 flag = TTY_NORMAL;
772
773 tty = tty_port_tty_get(&port->port);
774 if (tty) {
775 tty_buffer_request_room(tty, urb->actual_length + 1);
776 /* overrun is special, not associated with a char */
777 if (unlikely(lsr & UART_LSR_OE))
778 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
779 send_to_tty(tty, data, urb->actual_length, flag);
780 tty_flip_buffer_push(tty);
781 tty_kref_put(tty);
782 }
783
784 /* Throttle the device if requested by tty */
785 spin_lock_irqsave(&port->lock, flags);
786 port->throttled = port->throttle_req;
787 if (port->throttled) {
788 spin_unlock_irqrestore(&port->lock, flags);
789 return;
790 } else
791 spin_unlock_irqrestore(&port->lock, flags);
792 }
793 /* Continue reading from device */
794 result = usb_submit_urb(urb, GFP_ATOMIC);
795 if (result)
796 dev_err(&urb->dev->dev, "%s - failed resubmitting"
797 " read urb, error %d\n", __func__, result);
798}
799
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700800static struct usb_driver ark3116_driver = {
801 .name = "ark3116",
802 .probe = usb_serial_probe,
803 .disconnect = usb_serial_disconnect,
804 .id_table = id_table,
Johannes Hölzld9b1b782006-12-17 21:50:24 +0100805 .no_dynamic_id = 1,
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700806};
807
808static struct usb_serial_driver ark3116_device = {
809 .driver = {
810 .owner = THIS_MODULE,
811 .name = "ark3116",
812 },
813 .id_table = id_table,
Johannes Hölzld9b1b782006-12-17 21:50:24 +0100814 .usb_driver = &ark3116_driver,
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700815 .num_ports = 1,
816 .attach = ark3116_attach,
bart.hartgers@gmail.comf4c1e8d2009-10-28 10:43:26 +0100817 .release = ark3116_release,
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700818 .set_termios = ark3116_set_termios,
Alan Coxfe1ae7f2009-09-19 13:13:33 -0700819 .init_termios = ark3116_init_termios,
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700820 .ioctl = ark3116_ioctl,
821 .tiocmget = ark3116_tiocmget,
bart.hartgers@gmail.com546b7422009-10-28 10:43:28 +0100822 .tiocmset = ark3116_tiocmset,
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700823 .open = ark3116_open,
bart.hartgers@gmail.comf4c1e8d2009-10-28 10:43:26 +0100824 .close = ark3116_close,
bart.hartgers@gmail.com546b7422009-10-28 10:43:28 +0100825 .break_ctl = ark3116_break_ctl,
bart.hartgers@gmail.com62d826c2009-10-28 10:43:29 +0100826 .read_int_callback = ark3116_read_int_callback,
827 .read_bulk_callback = ark3116_read_bulk_callback,
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700828};
829
830static int __init ark3116_init(void)
831{
832 int retval;
833
834 retval = usb_serial_register(&ark3116_device);
835 if (retval)
836 return retval;
837 retval = usb_register(&ark3116_driver);
bart.hartgers@gmail.com149fc792009-10-28 10:43:25 +0100838 if (retval == 0) {
839 printk(KERN_INFO "%s:"
840 DRIVER_VERSION ":"
841 DRIVER_DESC "\n",
842 KBUILD_MODNAME);
843 } else
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700844 usb_serial_deregister(&ark3116_device);
845 return retval;
846}
847
848static void __exit ark3116_exit(void)
849{
850 usb_deregister(&ark3116_driver);
851 usb_serial_deregister(&ark3116_device);
852}
853
854module_init(ark3116_init);
855module_exit(ark3116_exit);
856MODULE_LICENSE("GPL");
857
bart.hartgers@gmail.com149fc792009-10-28 10:43:25 +0100858MODULE_AUTHOR(DRIVER_AUTHOR);
859MODULE_DESCRIPTION(DRIVER_DESC);
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700860
bart.hartgers@gmail.com149fc792009-10-28 10:43:25 +0100861module_param(debug, bool, S_IRUGO | S_IWUSR);
862MODULE_PARM_DESC(debug, "Enable debug");
863
864/*
865 * The following describes what I learned from studying the old
866 * ark3116.c driver, disassembling the windows driver, and some lucky
867 * guesses. Since I do not have any datasheet or other
868 * documentation, inaccuracies are almost guaranteed.
869 *
870 * Some specs for the ARK3116 can be found here:
871 * http://web.archive.org/web/20060318000438/
872 * www.arkmicro.com/en/products/view.php?id=10
873 * On that page, 2 GPIO pins are mentioned: I assume these are the
874 * OUT1 and OUT2 pins of the UART, so I added support for those
875 * through the MCR. Since the pins are not available on my hardware,
876 * I could not verify this.
877 * Also, it states there is "on-chip hardware flow control". I have
878 * discovered how to enable that. Unfortunately, I do not know how to
879 * enable XON/XOFF (software) flow control, which would need support
880 * from the chip as well to work. Because of the wording on the web
881 * page there is a real possibility the chip simply does not support
882 * software flow control.
883 *
884 * I got my ark3116 as part of a mobile phone adapter cable. On the
885 * PCB, the following numbered contacts are present:
886 *
887 * 1:- +5V
888 * 2:o DTR
889 * 3:i RX
890 * 4:i DCD
891 * 5:o RTS
892 * 6:o TX
893 * 7:i RI
894 * 8:i DSR
895 * 10:- 0V
896 * 11:i CTS
897 *
898 * On my chip, all signals seem to be 3.3V, but 5V tolerant. But that
899 * may be different for the one you have ;-).
900 *
901 * The windows driver limits the registers to 0-F, so I assume there
902 * are actually 16 present on the device.
903 *
904 * On an UART interrupt, 4 bytes of data come in on the interrupt
905 * endpoint. The bytes are 0xe8 IIR LSR MSR.
906 *
907 * The baudrate seems to be generated from the 12MHz crystal, using
908 * 4-times subsampling. So quot=12e6/(4*baud). Also see description
909 * of register E.
910 *
911 * Registers 0-7:
912 * These seem to be the same as for a regular 16450. The FCR is set
913 * to UART_FCR_DMA_SELECT (0x8), I guess to enable transfers between
914 * the UART and the USB bridge/DMA engine.
915 *
916 * Register 8:
917 * By trial and error, I found out that bit 0 enables hardware CTS,
918 * stopping TX when CTS is +5V. Bit 1 does the same for RTS, making
919 * RTS +5V when the 3116 cannot transfer the data to the USB bus
920 * (verified by disabling the reading URB). Note that as far as I can
921 * tell, the windows driver does NOT use this, so there might be some
922 * hardware bug or something.
923 *
924 * According to a patch provided here
925 * (http://lkml.org/lkml/2009/7/26/56), the ARK3116 can also be used
926 * as an IrDA dongle. Since I do not have such a thing, I could not
927 * investigate that aspect. However, I can speculate ;-).
928 *
929 * - IrDA encodes data differently than RS232. Most likely, one of
930 * the bits in registers 9..E enables the IR ENDEC (encoder/decoder).
931 * - Depending on the IR transceiver, the input and output need to be
932 * inverted, so there are probably bits for that as well.
933 * - IrDA is half-duplex, so there should be a bit for selecting that.
934 *
935 * This still leaves at least two registers unaccounted for. Perhaps
936 * The chip can do XON/XOFF or CRC in HW?
937 *
938 * Register 9:
939 * Set to 0x00 for IrDA, when the baudrate is initialised.
940 *
941 * Register A:
942 * Set to 0x01 for IrDA, at init.
943 *
944 * Register B:
945 * Set to 0x01 for IrDA, 0x00 for RS232, at init.
946 *
947 * Register C:
948 * Set to 00 for IrDA, at init.
949 *
950 * Register D:
951 * Set to 0x41 for IrDA, at init.
952 *
953 * Register E:
954 * Somekind of baudrate override. The windows driver seems to set
955 * this to 0x00 for normal baudrates, 0x01 for 460800, 0x02 for 921600.
956 * Since 460800 and 921600 cannot be obtained by dividing 3MHz by an integer,
957 * it could be somekind of subdivisor thingy.
958 * However,it does not seem to do anything: selecting 921600 (divisor 3,
959 * reg E=2), still gets 1 MHz. I also checked if registers 9, C or F would
960 * work, but they don't.
961 *
962 * Register F: unknown
963 */