blob: 5c947410c8578c6738c38c74d4d0ee7e6c1d3f34 [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
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700155static int ark3116_attach(struct usb_serial *serial)
156{
157 char *buf;
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700158
159 buf = kmalloc(1, GFP_KERNEL);
160 if (!buf) {
Werner Lemberg988440e2006-07-18 17:00:22 +0200161 dbg("error kmalloc -> out of mem?");
Alan Coxfe1ae7f2009-09-19 13:13:33 -0700162 return -ENOMEM;
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700163 }
164
Ondrej Zary5128a662009-08-06 16:09:52 -0700165 if (is_irda(serial))
166 dbg("IrDA mode");
167
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700168 /* 3 */
Werner Lemberg988440e2006-07-18 17:00:22 +0200169 ARK3116_SND(serial, 3, 0xFE, 0x40, 0x0008, 0x0002);
170 ARK3116_SND(serial, 4, 0xFE, 0x40, 0x0008, 0x0001);
171 ARK3116_SND(serial, 5, 0xFE, 0x40, 0x0000, 0x0008);
Ondrej Zary5128a662009-08-06 16:09:52 -0700172 ARK3116_SND(serial, 6, 0xFE, 0x40, is_irda(serial) ? 0x0001 : 0x0000,
173 0x000B);
174
175 if (is_irda(serial)) {
176 ARK3116_SND(serial, 1001, 0xFE, 0x40, 0x0000, 0x000C);
177 ARK3116_SND(serial, 1002, 0xFE, 0x40, 0x0041, 0x000D);
178 ARK3116_SND(serial, 1003, 0xFE, 0x40, 0x0001, 0x000A);
179 }
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700180
181 /* <-- seq7 */
Werner Lemberg988440e2006-07-18 17:00:22 +0200182 ARK3116_RCV(serial, 7, 0xFE, 0xC0, 0x0000, 0x0003, 0x00, buf);
183 ARK3116_SND(serial, 8, 0xFE, 0x40, 0x0080, 0x0003);
184 ARK3116_SND(serial, 9, 0xFE, 0x40, 0x001A, 0x0000);
185 ARK3116_SND(serial, 10, 0xFE, 0x40, 0x0000, 0x0001);
186 ARK3116_SND(serial, 11, 0xFE, 0x40, 0x0000, 0x0003);
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700187
188 /* <-- seq12 */
Werner Lemberg988440e2006-07-18 17:00:22 +0200189 ARK3116_RCV(serial, 12, 0xFE, 0xC0, 0x0000, 0x0004, 0x00, buf);
190 ARK3116_SND(serial, 13, 0xFE, 0x40, 0x0000, 0x0004);
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700191
192 /* 14 */
Werner Lemberg988440e2006-07-18 17:00:22 +0200193 ARK3116_RCV(serial, 14, 0xFE, 0xC0, 0x0000, 0x0004, 0x00, buf);
194 ARK3116_SND(serial, 15, 0xFE, 0x40, 0x0000, 0x0004);
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700195
196 /* 16 */
Werner Lemberg988440e2006-07-18 17:00:22 +0200197 ARK3116_RCV(serial, 16, 0xFE, 0xC0, 0x0000, 0x0004, 0x00, buf);
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700198 /* --> seq17 */
Werner Lemberg988440e2006-07-18 17:00:22 +0200199 ARK3116_SND(serial, 17, 0xFE, 0x40, 0x0001, 0x0004);
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700200
201 /* <-- seq18 */
Werner Lemberg988440e2006-07-18 17:00:22 +0200202 ARK3116_RCV(serial, 18, 0xFE, 0xC0, 0x0000, 0x0004, 0x01, buf);
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700203
204 /* --> seq19 */
Werner Lemberg988440e2006-07-18 17:00:22 +0200205 ARK3116_SND(serial, 19, 0xFE, 0x40, 0x0003, 0x0004);
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700206
207 /* <-- seq20 */
Werner Lemberg988440e2006-07-18 17:00:22 +0200208 /* seems like serial port status info (RTS, CTS, ...) */
209 /* returns modem control line status?! */
210 ARK3116_RCV(serial, 20, 0xFE, 0xC0, 0x0000, 0x0006, 0xFF, buf);
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700211
Werner Lemberg988440e2006-07-18 17:00:22 +0200212 /* set 9600 baud & do some init?! */
213 ARK3116_SND(serial, 147, 0xFE, 0x40, 0x0083, 0x0003);
214 ARK3116_SND(serial, 148, 0xFE, 0x40, 0x0038, 0x0000);
215 ARK3116_SND(serial, 149, 0xFE, 0x40, 0x0001, 0x0001);
Ondrej Zary5128a662009-08-06 16:09:52 -0700216 if (is_irda(serial))
217 ARK3116_SND(serial, 1004, 0xFE, 0x40, 0x0000, 0x0009);
Werner Lemberg988440e2006-07-18 17:00:22 +0200218 ARK3116_SND(serial, 150, 0xFE, 0x40, 0x0003, 0x0003);
219 ARK3116_RCV(serial, 151, 0xFE, 0xC0, 0x0000, 0x0004, 0x03, buf);
220 ARK3116_SND(serial, 152, 0xFE, 0x40, 0x0000, 0x0003);
221 ARK3116_RCV(serial, 153, 0xFE, 0xC0, 0x0000, 0x0003, 0x00, buf);
222 ARK3116_SND(serial, 154, 0xFE, 0x40, 0x0003, 0x0003);
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700223
224 kfree(buf);
Werner Lemberg988440e2006-07-18 17:00:22 +0200225 return 0;
Alan Coxfe1ae7f2009-09-19 13:13:33 -0700226}
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700227
Alan Coxfe1ae7f2009-09-19 13:13:33 -0700228static void ark3116_init_termios(struct tty_struct *tty)
229{
230 struct ktermios *termios = tty->termios;
231 *termios = tty_std_termios;
232 termios->c_cflag = B9600 | CS8
233 | CREAD | HUPCL | CLOCAL;
234 termios->c_ispeed = 9600;
235 termios->c_ospeed = 9600;
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700236}
237
Alan Cox95da3102008-07-22 11:09:07 +0100238static void ark3116_set_termios(struct tty_struct *tty,
239 struct usb_serial_port *port,
Alan Cox606d0992006-12-08 02:38:45 -0800240 struct ktermios *old_termios)
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700241{
242 struct usb_serial *serial = port->serial;
Alan Cox95da3102008-07-22 11:09:07 +0100243 struct ktermios *termios = tty->termios;
Alan Coxadb5dca2007-10-18 01:24:17 -0700244 unsigned int cflag = termios->c_cflag;
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700245 int baud;
246 int ark3116_baud;
247 char *buf;
248 char config;
249
250 config = 0;
251
Harvey Harrison441b62c2008-03-03 16:08:34 -0800252 dbg("%s - port %d", __func__, port->number);
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700253
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700254
Alan Coxadb5dca2007-10-18 01:24:17 -0700255 cflag = termios->c_cflag;
256 termios->c_cflag &= ~(CMSPAR|CRTSCTS);
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700257
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700258 buf = kmalloc(1, GFP_KERNEL);
259 if (!buf) {
260 dbg("error kmalloc");
Alan Cox95da3102008-07-22 11:09:07 +0100261 *termios = *old_termios;
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700262 return;
263 }
264
265 /* set data bit count (8/7/6/5) */
Werner Lemberg988440e2006-07-18 17:00:22 +0200266 if (cflag & CSIZE) {
267 switch (cflag & CSIZE) {
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700268 case CS5:
269 config |= 0x00;
270 dbg("setting CS5");
271 break;
272 case CS6:
273 config |= 0x01;
274 dbg("setting CS6");
275 break;
276 case CS7:
277 config |= 0x02;
278 dbg("setting CS7");
279 break;
280 default:
Alan Cox568c24a2007-06-22 14:36:29 +0100281 dbg("CSIZE was set but not CS5-CS8, using CS8!");
Werner Lemberg988440e2006-07-18 17:00:22 +0200282 /* fall through */
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700283 case CS8:
284 config |= 0x03;
285 dbg("setting CS8");
286 break;
287 }
288 }
289
Werner Lemberg988440e2006-07-18 17:00:22 +0200290 /* set parity (NONE/EVEN/ODD) */
291 if (cflag & PARENB) {
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700292 if (cflag & PARODD) {
293 config |= 0x08;
294 dbg("setting parity to ODD");
295 } else {
296 config |= 0x18;
297 dbg("setting parity to EVEN");
298 }
299 } else {
300 dbg("setting parity to NONE");
301 }
302
Werner Lemberg988440e2006-07-18 17:00:22 +0200303 /* set stop bit (1/2) */
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700304 if (cflag & CSTOPB) {
305 config |= 0x04;
Werner Lemberg988440e2006-07-18 17:00:22 +0200306 dbg("setting 2 stop bits");
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700307 } else {
Werner Lemberg988440e2006-07-18 17:00:22 +0200308 dbg("setting 1 stop bit");
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700309 }
310
Werner Lemberg988440e2006-07-18 17:00:22 +0200311 /* set baudrate */
Alan Cox95da3102008-07-22 11:09:07 +0100312 baud = tty_get_baud_rate(tty);
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700313
Alan Cox568c24a2007-06-22 14:36:29 +0100314 switch (baud) {
Alan Coxc4d0f8c2008-04-29 14:35:39 +0100315 case 75:
316 case 150:
317 case 300:
318 case 600:
319 case 1200:
320 case 1800:
321 case 2400:
322 case 4800:
323 case 9600:
324 case 19200:
325 case 38400:
326 case 57600:
327 case 115200:
328 case 230400:
329 case 460800:
330 /* Report the resulting rate back to the caller */
Alan Cox95da3102008-07-22 11:09:07 +0100331 tty_encode_baud_rate(tty, baud, baud);
Alan Coxc4d0f8c2008-04-29 14:35:39 +0100332 break;
333 /* set 9600 as default (if given baudrate is invalid for example) */
334 default:
Alan Cox95da3102008-07-22 11:09:07 +0100335 tty_encode_baud_rate(tty, 9600, 9600);
Alan Coxc4d0f8c2008-04-29 14:35:39 +0100336 case 0:
337 baud = 9600;
Alan Cox568c24a2007-06-22 14:36:29 +0100338 }
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700339
340 /*
341 * found by try'n'error, be careful, maybe there are other options
Alan Cox568c24a2007-06-22 14:36:29 +0100342 * for multiplicator etc! (3.5 for example)
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700343 */
344 if (baud == 460800)
345 /* strange, for 460800 the formula is wrong
Werner Lemberg988440e2006-07-18 17:00:22 +0200346 * if using round() then 9600baud is wrong) */
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700347 ark3116_baud = 7;
348 else
349 ark3116_baud = 3000000 / baud;
350
351 /* ? */
Werner Lemberg988440e2006-07-18 17:00:22 +0200352 ARK3116_RCV(serial, 0, 0xFE, 0xC0, 0x0000, 0x0003, 0x03, buf);
353
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700354 /* offset = buf[0]; */
355 /* offset = 0x03; */
Werner Lemberg988440e2006-07-18 17:00:22 +0200356 /* dbg("using 0x%04X as target for 0x0003:", 0x0080 + offset); */
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700357
358 /* set baudrate */
Werner Lemberg988440e2006-07-18 17:00:22 +0200359 dbg("setting baudrate to %d (->reg=%d)", baud, ark3116_baud);
360 ARK3116_SND(serial, 147, 0xFE, 0x40, 0x0083, 0x0003);
361 ARK3116_SND(serial, 148, 0xFE, 0x40,
362 (ark3116_baud & 0x00FF), 0x0000);
363 ARK3116_SND(serial, 149, 0xFE, 0x40,
364 (ark3116_baud & 0xFF00) >> 8, 0x0001);
365 ARK3116_SND(serial, 150, 0xFE, 0x40, 0x0003, 0x0003);
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700366
367 /* ? */
Werner Lemberg988440e2006-07-18 17:00:22 +0200368 ARK3116_RCV(serial, 151, 0xFE, 0xC0, 0x0000, 0x0004, 0x03, buf);
369 ARK3116_SND(serial, 152, 0xFE, 0x40, 0x0000, 0x0003);
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700370
371 /* set data bit count, stop bit count & parity: */
372 dbg("updating bit count, stop bit or parity (cfg=0x%02X)", config);
Werner Lemberg988440e2006-07-18 17:00:22 +0200373 ARK3116_RCV(serial, 153, 0xFE, 0xC0, 0x0000, 0x0003, 0x00, buf);
374 ARK3116_SND(serial, 154, 0xFE, 0x40, config, 0x0003);
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700375
376 if (cflag & CRTSCTS)
Werner Lemberg988440e2006-07-18 17:00:22 +0200377 dbg("CRTSCTS not supported by chipset?!");
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700378
Werner Lemberg988440e2006-07-18 17:00:22 +0200379 /* TEST ARK3116_SND(154, 0xFE, 0x40, 0xFFFF, 0x0006); */
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700380
381 kfree(buf);
Alan Coxadb5dca2007-10-18 01:24:17 -0700382
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700383 return;
384}
385
Alan Coxa509a7e2009-09-19 13:13:26 -0700386static int ark3116_open(struct tty_struct *tty, struct usb_serial_port *port)
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700387{
Alan Cox606d0992006-12-08 02:38:45 -0800388 struct ktermios tmp_termios;
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700389 struct usb_serial *serial = port->serial;
390 char *buf;
391 int result = 0;
392
Harvey Harrison441b62c2008-03-03 16:08:34 -0800393 dbg("%s - port %d", __func__, port->number);
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700394
395 buf = kmalloc(1, GFP_KERNEL);
396 if (!buf) {
Werner Lemberg988440e2006-07-18 17:00:22 +0200397 dbg("error kmalloc -> out of mem?");
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700398 return -ENOMEM;
399 }
400
Alan Coxa509a7e2009-09-19 13:13:26 -0700401 result = usb_serial_generic_open(tty, port);
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700402 if (result)
Oliver Neukum4edf2c82007-03-26 18:12:44 +0200403 goto err_out;
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700404
405 /* open */
Werner Lemberg988440e2006-07-18 17:00:22 +0200406 ARK3116_RCV(serial, 111, 0xFE, 0xC0, 0x0000, 0x0003, 0x02, buf);
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700407
Werner Lemberg988440e2006-07-18 17:00:22 +0200408 ARK3116_SND(serial, 112, 0xFE, 0x40, 0x0082, 0x0003);
409 ARK3116_SND(serial, 113, 0xFE, 0x40, 0x001A, 0x0000);
410 ARK3116_SND(serial, 114, 0xFE, 0x40, 0x0000, 0x0001);
411 ARK3116_SND(serial, 115, 0xFE, 0x40, 0x0002, 0x0003);
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700412
Werner Lemberg988440e2006-07-18 17:00:22 +0200413 ARK3116_RCV(serial, 116, 0xFE, 0xC0, 0x0000, 0x0004, 0x03, buf);
414 ARK3116_SND(serial, 117, 0xFE, 0x40, 0x0002, 0x0004);
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700415
Werner Lemberg988440e2006-07-18 17:00:22 +0200416 ARK3116_RCV(serial, 118, 0xFE, 0xC0, 0x0000, 0x0004, 0x02, buf);
417 ARK3116_SND(serial, 119, 0xFE, 0x40, 0x0000, 0x0004);
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700418
Werner Lemberg988440e2006-07-18 17:00:22 +0200419 ARK3116_RCV(serial, 120, 0xFE, 0xC0, 0x0000, 0x0004, 0x00, buf);
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700420
Werner Lemberg988440e2006-07-18 17:00:22 +0200421 ARK3116_SND(serial, 121, 0xFE, 0x40, 0x0001, 0x0004);
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700422
Werner Lemberg988440e2006-07-18 17:00:22 +0200423 ARK3116_RCV(serial, 122, 0xFE, 0xC0, 0x0000, 0x0004, 0x01, buf);
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700424
Werner Lemberg988440e2006-07-18 17:00:22 +0200425 ARK3116_SND(serial, 123, 0xFE, 0x40, 0x0003, 0x0004);
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700426
Werner Lemberg988440e2006-07-18 17:00:22 +0200427 /* returns different values (control lines?!) */
428 ARK3116_RCV(serial, 124, 0xFE, 0xC0, 0x0000, 0x0006, 0xFF, buf);
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700429
Werner Lemberg988440e2006-07-18 17:00:22 +0200430 /* initialise termios */
Alan Cox95da3102008-07-22 11:09:07 +0100431 if (tty)
432 ark3116_set_termios(tty, port, &tmp_termios);
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700433
Oliver Neukum4edf2c82007-03-26 18:12:44 +0200434err_out:
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700435 kfree(buf);
436
437 return result;
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700438}
439
Alan Cox95da3102008-07-22 11:09:07 +0100440static int ark3116_ioctl(struct tty_struct *tty, struct file *file,
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700441 unsigned int cmd, unsigned long arg)
442{
Alan Cox95da3102008-07-22 11:09:07 +0100443 struct usb_serial_port *port = tty->driver_data;
Werner Lemberg2f430b42006-07-18 17:00:52 +0200444 struct serial_struct serstruct;
445 void __user *user_arg = (void __user *)arg;
446
447 switch (cmd) {
448 case TIOCGSERIAL:
449 /* XXX: Some of these values are probably wrong. */
Alan Coxc4d0f8c2008-04-29 14:35:39 +0100450 memset(&serstruct, 0, sizeof(serstruct));
Werner Lemberg2f430b42006-07-18 17:00:52 +0200451 serstruct.type = PORT_16654;
452 serstruct.line = port->serial->minor;
453 serstruct.port = port->number;
454 serstruct.custom_divisor = 0;
455 serstruct.baud_base = 460800;
456
Alan Coxc4d0f8c2008-04-29 14:35:39 +0100457 if (copy_to_user(user_arg, &serstruct, sizeof(serstruct)))
Werner Lemberg2f430b42006-07-18 17:00:52 +0200458 return -EFAULT;
459
460 return 0;
461 case TIOCSSERIAL:
Alan Coxc4d0f8c2008-04-29 14:35:39 +0100462 if (copy_from_user(&serstruct, user_arg, sizeof(serstruct)))
Werner Lemberg2f430b42006-07-18 17:00:52 +0200463 return -EFAULT;
464 return 0;
465 default:
Harvey Harrison441b62c2008-03-03 16:08:34 -0800466 dbg("%s cmd 0x%04x not supported", __func__, cmd);
Werner Lemberg2f430b42006-07-18 17:00:52 +0200467 break;
468 }
469
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700470 return -ENOIOCTLCMD;
471}
472
Alan Cox95da3102008-07-22 11:09:07 +0100473static int ark3116_tiocmget(struct tty_struct *tty, struct file *file)
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700474{
Alan Cox95da3102008-07-22 11:09:07 +0100475 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700476 struct usb_serial *serial = port->serial;
477 char *buf;
478 char temp;
479
Werner Lemberg988440e2006-07-18 17:00:22 +0200480 /* seems like serial port status info (RTS, CTS, ...) is stored
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700481 * in reg(?) 0x0006
482 * pcb connection point 11 = GND -> sets bit4 of response
483 * pcb connection point 7 = GND -> sets bit6 of response
484 */
485
486 buf = kmalloc(1, GFP_KERNEL);
487 if (!buf) {
488 dbg("error kmalloc");
489 return -ENOMEM;
490 }
491
Werner Lemberg988440e2006-07-18 17:00:22 +0200492 /* read register */
493 ARK3116_RCV_QUIET(serial, 0xFE, 0xC0, 0x0000, 0x0006, buf);
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700494 temp = buf[0];
495 kfree(buf);
496
Werner Lemberg988440e2006-07-18 17:00:22 +0200497 /* i do not really know if bit4=CTS and bit6=DSR... just a
498 * quick guess!
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700499 */
Werner Lemberg988440e2006-07-18 17:00:22 +0200500 return (temp & (1<<4) ? TIOCM_CTS : 0)
501 | (temp & (1<<6) ? TIOCM_DSR : 0);
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700502}
503
504static struct usb_driver ark3116_driver = {
505 .name = "ark3116",
506 .probe = usb_serial_probe,
507 .disconnect = usb_serial_disconnect,
508 .id_table = id_table,
Johannes Hölzld9b1b782006-12-17 21:50:24 +0100509 .no_dynamic_id = 1,
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700510};
511
512static struct usb_serial_driver ark3116_device = {
513 .driver = {
514 .owner = THIS_MODULE,
515 .name = "ark3116",
516 },
517 .id_table = id_table,
Johannes Hölzld9b1b782006-12-17 21:50:24 +0100518 .usb_driver = &ark3116_driver,
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700519 .num_ports = 1,
520 .attach = ark3116_attach,
521 .set_termios = ark3116_set_termios,
Alan Coxfe1ae7f2009-09-19 13:13:33 -0700522 .init_termios = ark3116_init_termios,
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700523 .ioctl = ark3116_ioctl,
524 .tiocmget = ark3116_tiocmget,
525 .open = ark3116_open,
526};
527
528static int __init ark3116_init(void)
529{
530 int retval;
531
532 retval = usb_serial_register(&ark3116_device);
533 if (retval)
534 return retval;
535 retval = usb_register(&ark3116_driver);
bart.hartgers@gmail.com149fc792009-10-28 10:43:25 +0100536 if (retval == 0) {
537 printk(KERN_INFO "%s:"
538 DRIVER_VERSION ":"
539 DRIVER_DESC "\n",
540 KBUILD_MODNAME);
541 } else
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700542 usb_serial_deregister(&ark3116_device);
543 return retval;
544}
545
546static void __exit ark3116_exit(void)
547{
548 usb_deregister(&ark3116_driver);
549 usb_serial_deregister(&ark3116_device);
550}
551
552module_init(ark3116_init);
553module_exit(ark3116_exit);
554MODULE_LICENSE("GPL");
555
bart.hartgers@gmail.com149fc792009-10-28 10:43:25 +0100556MODULE_AUTHOR(DRIVER_AUTHOR);
557MODULE_DESCRIPTION(DRIVER_DESC);
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700558
bart.hartgers@gmail.com149fc792009-10-28 10:43:25 +0100559module_param(debug, bool, S_IRUGO | S_IWUSR);
560MODULE_PARM_DESC(debug, "Enable debug");
561
562/*
563 * The following describes what I learned from studying the old
564 * ark3116.c driver, disassembling the windows driver, and some lucky
565 * guesses. Since I do not have any datasheet or other
566 * documentation, inaccuracies are almost guaranteed.
567 *
568 * Some specs for the ARK3116 can be found here:
569 * http://web.archive.org/web/20060318000438/
570 * www.arkmicro.com/en/products/view.php?id=10
571 * On that page, 2 GPIO pins are mentioned: I assume these are the
572 * OUT1 and OUT2 pins of the UART, so I added support for those
573 * through the MCR. Since the pins are not available on my hardware,
574 * I could not verify this.
575 * Also, it states there is "on-chip hardware flow control". I have
576 * discovered how to enable that. Unfortunately, I do not know how to
577 * enable XON/XOFF (software) flow control, which would need support
578 * from the chip as well to work. Because of the wording on the web
579 * page there is a real possibility the chip simply does not support
580 * software flow control.
581 *
582 * I got my ark3116 as part of a mobile phone adapter cable. On the
583 * PCB, the following numbered contacts are present:
584 *
585 * 1:- +5V
586 * 2:o DTR
587 * 3:i RX
588 * 4:i DCD
589 * 5:o RTS
590 * 6:o TX
591 * 7:i RI
592 * 8:i DSR
593 * 10:- 0V
594 * 11:i CTS
595 *
596 * On my chip, all signals seem to be 3.3V, but 5V tolerant. But that
597 * may be different for the one you have ;-).
598 *
599 * The windows driver limits the registers to 0-F, so I assume there
600 * are actually 16 present on the device.
601 *
602 * On an UART interrupt, 4 bytes of data come in on the interrupt
603 * endpoint. The bytes are 0xe8 IIR LSR MSR.
604 *
605 * The baudrate seems to be generated from the 12MHz crystal, using
606 * 4-times subsampling. So quot=12e6/(4*baud). Also see description
607 * of register E.
608 *
609 * Registers 0-7:
610 * These seem to be the same as for a regular 16450. The FCR is set
611 * to UART_FCR_DMA_SELECT (0x8), I guess to enable transfers between
612 * the UART and the USB bridge/DMA engine.
613 *
614 * Register 8:
615 * By trial and error, I found out that bit 0 enables hardware CTS,
616 * stopping TX when CTS is +5V. Bit 1 does the same for RTS, making
617 * RTS +5V when the 3116 cannot transfer the data to the USB bus
618 * (verified by disabling the reading URB). Note that as far as I can
619 * tell, the windows driver does NOT use this, so there might be some
620 * hardware bug or something.
621 *
622 * According to a patch provided here
623 * (http://lkml.org/lkml/2009/7/26/56), the ARK3116 can also be used
624 * as an IrDA dongle. Since I do not have such a thing, I could not
625 * investigate that aspect. However, I can speculate ;-).
626 *
627 * - IrDA encodes data differently than RS232. Most likely, one of
628 * the bits in registers 9..E enables the IR ENDEC (encoder/decoder).
629 * - Depending on the IR transceiver, the input and output need to be
630 * inverted, so there are probably bits for that as well.
631 * - IrDA is half-duplex, so there should be a bit for selecting that.
632 *
633 * This still leaves at least two registers unaccounted for. Perhaps
634 * The chip can do XON/XOFF or CRC in HW?
635 *
636 * Register 9:
637 * Set to 0x00 for IrDA, when the baudrate is initialised.
638 *
639 * Register A:
640 * Set to 0x01 for IrDA, at init.
641 *
642 * Register B:
643 * Set to 0x01 for IrDA, 0x00 for RS232, at init.
644 *
645 * Register C:
646 * Set to 00 for IrDA, at init.
647 *
648 * Register D:
649 * Set to 0x41 for IrDA, at init.
650 *
651 * Register E:
652 * Somekind of baudrate override. The windows driver seems to set
653 * this to 0x00 for normal baudrates, 0x01 for 460800, 0x02 for 921600.
654 * Since 460800 and 921600 cannot be obtained by dividing 3MHz by an integer,
655 * it could be somekind of subdivisor thingy.
656 * However,it does not seem to do anything: selecting 921600 (divisor 3,
657 * reg E=2), still gets 1 MHz. I also checked if registers 9, C or F would
658 * work, but they don't.
659 *
660 * Register F: unknown
661 */