blob: d12df9484677f93b22b52b9d39beb2ec9753ce1f [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;
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700531 struct usb_serial *serial = port->serial;
532 char *buf;
533 char temp;
534
Werner Lemberg988440e2006-07-18 17:00:22 +0200535 /* seems like serial port status info (RTS, CTS, ...) is stored
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700536 * in reg(?) 0x0006
537 * pcb connection point 11 = GND -> sets bit4 of response
538 * pcb connection point 7 = GND -> sets bit6 of response
539 */
540
541 buf = kmalloc(1, GFP_KERNEL);
542 if (!buf) {
543 dbg("error kmalloc");
544 return -ENOMEM;
545 }
546
Werner Lemberg988440e2006-07-18 17:00:22 +0200547 /* read register */
548 ARK3116_RCV_QUIET(serial, 0xFE, 0xC0, 0x0000, 0x0006, buf);
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700549 temp = buf[0];
550 kfree(buf);
551
Werner Lemberg988440e2006-07-18 17:00:22 +0200552 /* i do not really know if bit4=CTS and bit6=DSR... just a
553 * quick guess!
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700554 */
Werner Lemberg988440e2006-07-18 17:00:22 +0200555 return (temp & (1<<4) ? TIOCM_CTS : 0)
556 | (temp & (1<<6) ? TIOCM_DSR : 0);
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700557}
558
559static struct usb_driver ark3116_driver = {
560 .name = "ark3116",
561 .probe = usb_serial_probe,
562 .disconnect = usb_serial_disconnect,
563 .id_table = id_table,
Johannes Hölzld9b1b782006-12-17 21:50:24 +0100564 .no_dynamic_id = 1,
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700565};
566
567static struct usb_serial_driver ark3116_device = {
568 .driver = {
569 .owner = THIS_MODULE,
570 .name = "ark3116",
571 },
572 .id_table = id_table,
Johannes Hölzld9b1b782006-12-17 21:50:24 +0100573 .usb_driver = &ark3116_driver,
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700574 .num_ports = 1,
575 .attach = ark3116_attach,
bart.hartgers@gmail.comf4c1e8d2009-10-28 10:43:26 +0100576 .release = ark3116_release,
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700577 .set_termios = ark3116_set_termios,
Alan Coxfe1ae7f2009-09-19 13:13:33 -0700578 .init_termios = ark3116_init_termios,
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700579 .ioctl = ark3116_ioctl,
580 .tiocmget = ark3116_tiocmget,
581 .open = ark3116_open,
bart.hartgers@gmail.comf4c1e8d2009-10-28 10:43:26 +0100582 .close = ark3116_close,
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700583};
584
585static int __init ark3116_init(void)
586{
587 int retval;
588
589 retval = usb_serial_register(&ark3116_device);
590 if (retval)
591 return retval;
592 retval = usb_register(&ark3116_driver);
bart.hartgers@gmail.com149fc792009-10-28 10:43:25 +0100593 if (retval == 0) {
594 printk(KERN_INFO "%s:"
595 DRIVER_VERSION ":"
596 DRIVER_DESC "\n",
597 KBUILD_MODNAME);
598 } else
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700599 usb_serial_deregister(&ark3116_device);
600 return retval;
601}
602
603static void __exit ark3116_exit(void)
604{
605 usb_deregister(&ark3116_driver);
606 usb_serial_deregister(&ark3116_device);
607}
608
609module_init(ark3116_init);
610module_exit(ark3116_exit);
611MODULE_LICENSE("GPL");
612
bart.hartgers@gmail.com149fc792009-10-28 10:43:25 +0100613MODULE_AUTHOR(DRIVER_AUTHOR);
614MODULE_DESCRIPTION(DRIVER_DESC);
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700615
bart.hartgers@gmail.com149fc792009-10-28 10:43:25 +0100616module_param(debug, bool, S_IRUGO | S_IWUSR);
617MODULE_PARM_DESC(debug, "Enable debug");
618
619/*
620 * The following describes what I learned from studying the old
621 * ark3116.c driver, disassembling the windows driver, and some lucky
622 * guesses. Since I do not have any datasheet or other
623 * documentation, inaccuracies are almost guaranteed.
624 *
625 * Some specs for the ARK3116 can be found here:
626 * http://web.archive.org/web/20060318000438/
627 * www.arkmicro.com/en/products/view.php?id=10
628 * On that page, 2 GPIO pins are mentioned: I assume these are the
629 * OUT1 and OUT2 pins of the UART, so I added support for those
630 * through the MCR. Since the pins are not available on my hardware,
631 * I could not verify this.
632 * Also, it states there is "on-chip hardware flow control". I have
633 * discovered how to enable that. Unfortunately, I do not know how to
634 * enable XON/XOFF (software) flow control, which would need support
635 * from the chip as well to work. Because of the wording on the web
636 * page there is a real possibility the chip simply does not support
637 * software flow control.
638 *
639 * I got my ark3116 as part of a mobile phone adapter cable. On the
640 * PCB, the following numbered contacts are present:
641 *
642 * 1:- +5V
643 * 2:o DTR
644 * 3:i RX
645 * 4:i DCD
646 * 5:o RTS
647 * 6:o TX
648 * 7:i RI
649 * 8:i DSR
650 * 10:- 0V
651 * 11:i CTS
652 *
653 * On my chip, all signals seem to be 3.3V, but 5V tolerant. But that
654 * may be different for the one you have ;-).
655 *
656 * The windows driver limits the registers to 0-F, so I assume there
657 * are actually 16 present on the device.
658 *
659 * On an UART interrupt, 4 bytes of data come in on the interrupt
660 * endpoint. The bytes are 0xe8 IIR LSR MSR.
661 *
662 * The baudrate seems to be generated from the 12MHz crystal, using
663 * 4-times subsampling. So quot=12e6/(4*baud). Also see description
664 * of register E.
665 *
666 * Registers 0-7:
667 * These seem to be the same as for a regular 16450. The FCR is set
668 * to UART_FCR_DMA_SELECT (0x8), I guess to enable transfers between
669 * the UART and the USB bridge/DMA engine.
670 *
671 * Register 8:
672 * By trial and error, I found out that bit 0 enables hardware CTS,
673 * stopping TX when CTS is +5V. Bit 1 does the same for RTS, making
674 * RTS +5V when the 3116 cannot transfer the data to the USB bus
675 * (verified by disabling the reading URB). Note that as far as I can
676 * tell, the windows driver does NOT use this, so there might be some
677 * hardware bug or something.
678 *
679 * According to a patch provided here
680 * (http://lkml.org/lkml/2009/7/26/56), the ARK3116 can also be used
681 * as an IrDA dongle. Since I do not have such a thing, I could not
682 * investigate that aspect. However, I can speculate ;-).
683 *
684 * - IrDA encodes data differently than RS232. Most likely, one of
685 * the bits in registers 9..E enables the IR ENDEC (encoder/decoder).
686 * - Depending on the IR transceiver, the input and output need to be
687 * inverted, so there are probably bits for that as well.
688 * - IrDA is half-duplex, so there should be a bit for selecting that.
689 *
690 * This still leaves at least two registers unaccounted for. Perhaps
691 * The chip can do XON/XOFF or CRC in HW?
692 *
693 * Register 9:
694 * Set to 0x00 for IrDA, when the baudrate is initialised.
695 *
696 * Register A:
697 * Set to 0x01 for IrDA, at init.
698 *
699 * Register B:
700 * Set to 0x01 for IrDA, 0x00 for RS232, at init.
701 *
702 * Register C:
703 * Set to 00 for IrDA, at init.
704 *
705 * Register D:
706 * Set to 0x41 for IrDA, at init.
707 *
708 * Register E:
709 * Somekind of baudrate override. The windows driver seems to set
710 * this to 0x00 for normal baudrates, 0x01 for 460800, 0x02 for 921600.
711 * Since 460800 and 921600 cannot be obtained by dividing 3MHz by an integer,
712 * it could be somekind of subdivisor thingy.
713 * However,it does not seem to do anything: selecting 921600 (divisor 3,
714 * reg E=2), still gets 1 MHz. I also checked if registers 9, C or F would
715 * work, but they don't.
716 *
717 * Register F: unknown
718 */