blob: ff77027160aab7eca0ac78e4f78ae1c8e7f01877 [file] [log] [blame]
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001/*
2 * Infinity Unlimited USB Phoenix driver
3 *
James Courtier-Dutton89b54392010-05-21 11:53:25 +01004 * Copyright (C) 2010 James Courtier-Dutton (James@superbug.co.uk)
5
Alain Degreffe60a8fc02007-10-26 13:51:49 +02006 * Copyright (C) 2007 Alain Degreffe (eczema@ecze.com)
7 *
8 * Original code taken from iuutool (Copyright (C) 2006 Juan Carlos Borrás)
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * And tested with help of WB Electronics
16 *
17 */
18#include <linux/kernel.h>
19#include <linux/errno.h>
20#include <linux/init.h>
21#include <linux/slab.h>
22#include <linux/tty.h>
23#include <linux/tty_driver.h>
24#include <linux/tty_flip.h>
25#include <linux/serial.h>
26#include <linux/module.h>
27#include <linux/moduleparam.h>
28#include <linux/spinlock.h>
29#include <linux/uaccess.h>
30#include <linux/usb.h>
31#include <linux/usb/serial.h>
32#include "iuu_phoenix.h"
33#include <linux/random.h>
34
Alain Degreffe60a8fc02007-10-26 13:51:49 +020035#define DRIVER_DESC "Infinity USB Unlimited Phoenix driver"
36
Németh Márton7d40d7e2010-01-10 15:34:24 +010037static const struct usb_device_id id_table[] = {
Alain Degreffe60a8fc02007-10-26 13:51:49 +020038 {USB_DEVICE(IUU_USB_VENDOR_ID, IUU_USB_PRODUCT_ID)},
39 {} /* Terminating entry */
40};
41MODULE_DEVICE_TABLE(usb, id_table);
42
Alain Degreffe60a8fc02007-10-26 13:51:49 +020043/* turbo parameter */
44static int boost = 100;
45static int clockmode = 1;
46static int cdmode = 1;
47static int iuu_cardin;
48static int iuu_cardout;
Rusty Russell90ab5ee2012-01-13 09:32:20 +103049static bool xmas;
Olivier Bornete55c6d02009-08-18 21:05:57 +020050static int vcc_default = 5;
Alain Degreffe60a8fc02007-10-26 13:51:49 +020051
Johan Hovold0978c942012-10-18 10:52:17 +020052static int iuu_create_sysfs_attrs(struct usb_serial_port *port);
53static int iuu_remove_sysfs_attrs(struct usb_serial_port *port);
Alain Degreffe60a8fc02007-10-26 13:51:49 +020054static void read_rxcmd_callback(struct urb *urb);
55
56struct iuu_private {
57 spinlock_t lock; /* store irq state */
58 wait_queue_head_t delta_msr_wait;
Alain Degreffe60a8fc02007-10-26 13:51:49 +020059 u8 line_status;
Alain Degreffe60a8fc02007-10-26 13:51:49 +020060 int tiostatus; /* store IUART SIGNAL for tiocmget call */
61 u8 reset; /* if 1 reset is needed */
62 int poll; /* number of poll */
63 u8 *writebuf; /* buffer for writing to device */
64 int writelen; /* num of byte to write to device */
65 u8 *buf; /* used for initialize speed */
Alain Degreffe60a8fc02007-10-26 13:51:49 +020066 u8 len;
Olivier Bornet20eda942009-08-18 21:05:55 +020067 int vcc; /* vcc (either 3 or 5 V) */
James Courtier-Dutton89b54392010-05-21 11:53:25 +010068 u32 baud;
69 u32 boost;
70 u32 clk;
Alain Degreffe60a8fc02007-10-26 13:51:49 +020071};
72
Johan Hovold53636552012-10-17 13:34:59 +020073static int iuu_port_probe(struct usb_serial_port *port)
Alain Degreffe60a8fc02007-10-26 13:51:49 +020074{
75 struct iuu_private *priv;
Johan Hovold0978c942012-10-18 10:52:17 +020076 int ret;
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -070077
Alain Degreffe60a8fc02007-10-26 13:51:49 +020078 priv = kzalloc(sizeof(struct iuu_private), GFP_KERNEL);
Alain Degreffe60a8fc02007-10-26 13:51:49 +020079 if (!priv)
80 return -ENOMEM;
Johan Hovold53636552012-10-17 13:34:59 +020081
82 priv->buf = kzalloc(256, GFP_KERNEL);
83 if (!priv->buf) {
Alain Degreffe60a8fc02007-10-26 13:51:49 +020084 kfree(priv);
85 return -ENOMEM;
86 }
Johan Hovold53636552012-10-17 13:34:59 +020087
88 priv->writebuf = kzalloc(256, GFP_KERNEL);
89 if (!priv->writebuf) {
90 kfree(priv->buf);
91 kfree(priv);
92 return -ENOMEM;
93 }
94
Olivier Bornete55c6d02009-08-18 21:05:57 +020095 priv->vcc = vcc_default;
Alain Degreffe60a8fc02007-10-26 13:51:49 +020096 spin_lock_init(&priv->lock);
97 init_waitqueue_head(&priv->delta_msr_wait);
Johan Hovold53636552012-10-17 13:34:59 +020098
99 usb_set_serial_port_data(port, priv);
100
Johan Hovold0978c942012-10-18 10:52:17 +0200101 ret = iuu_create_sysfs_attrs(port);
102 if (ret) {
103 kfree(priv->writebuf);
104 kfree(priv->buf);
105 kfree(priv);
106 return ret;
107 }
108
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200109 return 0;
110}
111
Johan Hovold53636552012-10-17 13:34:59 +0200112static int iuu_port_remove(struct usb_serial_port *port)
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200113{
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200114 struct iuu_private *priv = usb_get_serial_port_data(port);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200115
Johan Hovold0978c942012-10-18 10:52:17 +0200116 iuu_remove_sysfs_attrs(port);
Johan Hovold53636552012-10-17 13:34:59 +0200117 kfree(priv->writebuf);
118 kfree(priv->buf);
119 kfree(priv);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200120
Johan Hovold53636552012-10-17 13:34:59 +0200121 return 0;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200122}
123
Alan Cox20b9d172011-02-14 16:26:50 +0000124static int iuu_tiocmset(struct tty_struct *tty,
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200125 unsigned int set, unsigned int clear)
126{
Alan Cox95da3102008-07-22 11:09:07 +0100127 struct usb_serial_port *port = tty->driver_data;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200128 struct iuu_private *priv = usb_get_serial_port_data(port);
Alan Cox7b1fc8b2008-02-20 21:39:25 +0000129 unsigned long flags;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200130
Alan Cox7b1fc8b2008-02-20 21:39:25 +0000131 /* FIXME: locking on tiomstatus */
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700132 dev_dbg(&port->dev, "%s msg : SET = 0x%04x, CLEAR = 0x%04x\n",
133 __func__, set, clear);
Alan Cox7b1fc8b2008-02-20 21:39:25 +0000134
135 spin_lock_irqsave(&priv->lock, flags);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200136
James Courtier-Dutton89b54392010-05-21 11:53:25 +0100137 if ((set & TIOCM_RTS) && !(priv->tiostatus == TIOCM_RTS)) {
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700138 dev_dbg(&port->dev, "%s TIOCMSET RESET called !!!\n", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200139 priv->reset = 1;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200140 }
James Courtier-Dutton89b54392010-05-21 11:53:25 +0100141 if (set & TIOCM_RTS)
142 priv->tiostatus = TIOCM_RTS;
143
Alan Cox7b1fc8b2008-02-20 21:39:25 +0000144 spin_unlock_irqrestore(&priv->lock, flags);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200145 return 0;
146}
147
148/* This is used to provide a carrier detect mechanism
149 * When a card is present, the response is 0x00
150 * When no card , the reader respond with TIOCM_CD
151 * This is known as CD autodetect mechanism
152 */
Alan Cox60b33c12011-02-14 16:26:14 +0000153static int iuu_tiocmget(struct tty_struct *tty)
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200154{
Alan Cox95da3102008-07-22 11:09:07 +0100155 struct usb_serial_port *port = tty->driver_data;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200156 struct iuu_private *priv = usb_get_serial_port_data(port);
Alan Cox7b1fc8b2008-02-20 21:39:25 +0000157 unsigned long flags;
158 int rc;
159
160 spin_lock_irqsave(&priv->lock, flags);
161 rc = priv->tiostatus;
162 spin_unlock_irqrestore(&priv->lock, flags);
163
164 return rc;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200165}
166
167static void iuu_rxcmd(struct urb *urb)
168{
Ming Leicdc97792008-02-24 18:41:47 +0800169 struct usb_serial_port *port = urb->context;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200170 int result;
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800171 int status = urb->status;
172
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800173 if (status) {
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700174 dev_dbg(&port->dev, "%s - status = %d\n", __func__, status);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200175 /* error stop all */
176 return;
177 }
178
179
180 memset(port->write_urb->transfer_buffer, IUU_UART_RX, 1);
181 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
182 usb_sndbulkpipe(port->serial->dev,
183 port->bulk_out_endpointAddress),
184 port->write_urb->transfer_buffer, 1,
185 read_rxcmd_callback, port);
186 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
187}
188
189static int iuu_reset(struct usb_serial_port *port, u8 wt)
190{
191 struct iuu_private *priv = usb_get_serial_port_data(port);
192 int result;
193 char *buf_ptr = port->write_urb->transfer_buffer;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200194
195 /* Prepare the reset sequence */
196
197 *buf_ptr++ = IUU_RST_SET;
198 *buf_ptr++ = IUU_DELAY_MS;
199 *buf_ptr++ = wt;
200 *buf_ptr = IUU_RST_CLEAR;
201
202 /* send the sequence */
203
204 usb_fill_bulk_urb(port->write_urb,
205 port->serial->dev,
206 usb_sndbulkpipe(port->serial->dev,
207 port->bulk_out_endpointAddress),
208 port->write_urb->transfer_buffer, 4, iuu_rxcmd, port);
209 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
210 priv->reset = 0;
211 return result;
212}
213
214/* Status Function
215 * Return value is
216 * 0x00 = no card
217 * 0x01 = smartcard
218 * 0x02 = sim card
219 */
220static void iuu_update_status_callback(struct urb *urb)
221{
Ming Leicdc97792008-02-24 18:41:47 +0800222 struct usb_serial_port *port = urb->context;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200223 struct iuu_private *priv = usb_get_serial_port_data(port);
224 u8 *st;
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800225 int status = urb->status;
226
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800227 if (status) {
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700228 dev_dbg(&port->dev, "%s - status = %d\n", __func__, status);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200229 /* error stop all */
230 return;
231 }
232
233 st = urb->transfer_buffer;
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700234 dev_dbg(&port->dev, "%s - enter\n", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200235 if (urb->actual_length == 1) {
236 switch (st[0]) {
237 case 0x1:
238 priv->tiostatus = iuu_cardout;
239 break;
240 case 0x0:
241 priv->tiostatus = iuu_cardin;
242 break;
243 default:
244 priv->tiostatus = iuu_cardin;
245 }
246 }
247 iuu_rxcmd(urb);
248}
249
250static void iuu_status_callback(struct urb *urb)
251{
Ming Leicdc97792008-02-24 18:41:47 +0800252 struct usb_serial_port *port = urb->context;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200253 int result;
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800254 int status = urb->status;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200255
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700256 dev_dbg(&port->dev, "%s - status = %d\n", __func__, status);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200257 usb_fill_bulk_urb(port->read_urb, port->serial->dev,
258 usb_rcvbulkpipe(port->serial->dev,
259 port->bulk_in_endpointAddress),
260 port->read_urb->transfer_buffer, 256,
261 iuu_update_status_callback, port);
262 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
263}
264
265static int iuu_status(struct usb_serial_port *port)
266{
267 int result;
268
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200269 memset(port->write_urb->transfer_buffer, IUU_GET_STATE_REGISTER, 1);
270 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
271 usb_sndbulkpipe(port->serial->dev,
272 port->bulk_out_endpointAddress),
273 port->write_urb->transfer_buffer, 1,
274 iuu_status_callback, port);
275 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
276 return result;
277
278}
279
280static int bulk_immediate(struct usb_serial_port *port, u8 *buf, u8 count)
281{
282 int status;
283 struct usb_serial *serial = port->serial;
284 int actual = 0;
285
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200286 /* send the data out the bulk port */
287
288 status =
289 usb_bulk_msg(serial->dev,
290 usb_sndbulkpipe(serial->dev,
291 port->bulk_out_endpointAddress), buf,
292 count, &actual, HZ * 1);
293
Alan Cox9e8e2d22008-07-22 11:12:59 +0100294 if (status != IUU_OPERATION_OK)
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700295 dev_dbg(&port->dev, "%s - error = %2x\n", __func__, status);
Alan Cox9e8e2d22008-07-22 11:12:59 +0100296 else
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700297 dev_dbg(&port->dev, "%s - write OK !\n", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200298 return status;
299}
300
301static int read_immediate(struct usb_serial_port *port, u8 *buf, u8 count)
302{
303 int status;
304 struct usb_serial *serial = port->serial;
305 int actual = 0;
306
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200307 /* send the data out the bulk port */
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200308 status =
309 usb_bulk_msg(serial->dev,
310 usb_rcvbulkpipe(serial->dev,
311 port->bulk_in_endpointAddress), buf,
312 count, &actual, HZ * 1);
313
Alan Cox9e8e2d22008-07-22 11:12:59 +0100314 if (status != IUU_OPERATION_OK)
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700315 dev_dbg(&port->dev, "%s - error = %2x\n", __func__, status);
Alan Cox9e8e2d22008-07-22 11:12:59 +0100316 else
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700317 dev_dbg(&port->dev, "%s - read OK !\n", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200318 return status;
319}
320
321static int iuu_led(struct usb_serial_port *port, unsigned int R,
322 unsigned int G, unsigned int B, u8 f)
323{
324 int status;
325 u8 *buf;
326 buf = kmalloc(8, GFP_KERNEL);
327 if (!buf)
328 return -ENOMEM;
329
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200330 buf[0] = IUU_SET_LED;
331 buf[1] = R & 0xFF;
332 buf[2] = (R >> 8) & 0xFF;
333 buf[3] = G & 0xFF;
334 buf[4] = (G >> 8) & 0xFF;
335 buf[5] = B & 0xFF;
336 buf[6] = (B >> 8) & 0xFF;
337 buf[7] = f;
338 status = bulk_immediate(port, buf, 8);
339 kfree(buf);
340 if (status != IUU_OPERATION_OK)
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700341 dev_dbg(&port->dev, "%s - led error status = %2x\n", __func__, status);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200342 else
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700343 dev_dbg(&port->dev, "%s - led OK !\n", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200344 return IUU_OPERATION_OK;
345}
346
347static void iuu_rgbf_fill_buffer(u8 *buf, u8 r1, u8 r2, u8 g1, u8 g2, u8 b1,
348 u8 b2, u8 freq)
349{
350 *buf++ = IUU_SET_LED;
351 *buf++ = r1;
352 *buf++ = r2;
353 *buf++ = g1;
354 *buf++ = g2;
355 *buf++ = b1;
356 *buf++ = b2;
357 *buf = freq;
358}
359
360static void iuu_led_activity_on(struct urb *urb)
361{
Ming Leicdc97792008-02-24 18:41:47 +0800362 struct usb_serial_port *port = urb->context;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200363 int result;
364 char *buf_ptr = port->write_urb->transfer_buffer;
365 *buf_ptr++ = IUU_SET_LED;
366 if (xmas == 1) {
367 get_random_bytes(buf_ptr, 6);
368 *(buf_ptr+7) = 1;
369 } else {
370 iuu_rgbf_fill_buffer(buf_ptr, 255, 255, 0, 0, 0, 0, 255);
371 }
372
373 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
374 usb_sndbulkpipe(port->serial->dev,
375 port->bulk_out_endpointAddress),
376 port->write_urb->transfer_buffer, 8 ,
377 iuu_rxcmd, port);
378 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
379}
380
381static void iuu_led_activity_off(struct urb *urb)
382{
Ming Leicdc97792008-02-24 18:41:47 +0800383 struct usb_serial_port *port = urb->context;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200384 int result;
385 char *buf_ptr = port->write_urb->transfer_buffer;
386 if (xmas == 1) {
387 iuu_rxcmd(urb);
388 return;
389 } else {
390 *buf_ptr++ = IUU_SET_LED;
391 iuu_rgbf_fill_buffer(buf_ptr, 0, 0, 255, 255, 0, 0, 255);
392 }
393 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
394 usb_sndbulkpipe(port->serial->dev,
395 port->bulk_out_endpointAddress),
396 port->write_urb->transfer_buffer, 8 ,
397 iuu_rxcmd, port);
398 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
399}
400
401
402
403static int iuu_clk(struct usb_serial_port *port, int dwFrq)
404{
405 int status;
406 struct iuu_private *priv = usb_get_serial_port_data(port);
407 int Count = 0;
408 u8 FrqGenAdr = 0x69;
409 u8 DIV = 0; /* 8bit */
410 u8 XDRV = 0; /* 8bit */
411 u8 PUMP = 0; /* 3bit */
412 u8 PBmsb = 0; /* 2bit */
413 u8 PBlsb = 0; /* 8bit */
414 u8 PO = 0; /* 1bit */
415 u8 Q = 0; /* 7bit */
416 /* 24bit = 3bytes */
417 unsigned int P = 0;
418 unsigned int P2 = 0;
419 int frq = (int)dwFrq;
420
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200421 if (frq == 0) {
422 priv->buf[Count++] = IUU_UART_WRITE_I2C;
423 priv->buf[Count++] = FrqGenAdr << 1;
424 priv->buf[Count++] = 0x09;
425 priv->buf[Count++] = 0x00;
426
427 status = bulk_immediate(port, (u8 *) priv->buf, Count);
428 if (status != 0) {
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700429 dev_dbg(&port->dev, "%s - write error\n", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200430 return status;
431 }
432 } else if (frq == 3579000) {
433 DIV = 100;
434 P = 1193;
435 Q = 40;
436 XDRV = 0;
437 } else if (frq == 3680000) {
438 DIV = 105;
439 P = 161;
440 Q = 5;
441 XDRV = 0;
442 } else if (frq == 6000000) {
443 DIV = 66;
444 P = 66;
445 Q = 2;
446 XDRV = 0x28;
447 } else {
448 unsigned int result = 0;
449 unsigned int tmp = 0;
450 unsigned int check;
451 unsigned int check2;
452 char found = 0x00;
453 unsigned int lQ = 2;
454 unsigned int lP = 2055;
455 unsigned int lDiv = 4;
456
457 for (lQ = 2; lQ <= 47 && !found; lQ++)
458 for (lP = 2055; lP >= 8 && !found; lP--)
459 for (lDiv = 4; lDiv <= 127 && !found; lDiv++) {
460 tmp = (12000000 / lDiv) * (lP / lQ);
461 if (abs((int)(tmp - frq)) <
462 abs((int)(frq - result))) {
463 check2 = (12000000 / lQ);
464 if (check2 < 250000)
465 continue;
466 check = (12000000 / lQ) * lP;
467 if (check > 400000000)
468 continue;
469 if (check < 100000000)
470 continue;
471 if (lDiv < 4 || lDiv > 127)
472 continue;
473 result = tmp;
474 P = lP;
475 DIV = lDiv;
476 Q = lQ;
477 if (result == frq)
478 found = 0x01;
479 }
480 }
481 }
482 P2 = ((P - PO) / 2) - 4;
483 DIV = DIV;
484 PUMP = 0x04;
485 PBmsb = (P2 >> 8 & 0x03);
486 PBlsb = P2 & 0xFF;
487 PO = (P >> 10) & 0x01;
488 Q = Q - 2;
489
490 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
491 priv->buf[Count++] = FrqGenAdr << 1;
492 priv->buf[Count++] = 0x09;
493 priv->buf[Count++] = 0x20; /* Adr = 0x09 */
494 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
495 priv->buf[Count++] = FrqGenAdr << 1;
496 priv->buf[Count++] = 0x0C;
497 priv->buf[Count++] = DIV; /* Adr = 0x0C */
498 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
499 priv->buf[Count++] = FrqGenAdr << 1;
500 priv->buf[Count++] = 0x12;
501 priv->buf[Count++] = XDRV; /* Adr = 0x12 */
502 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
503 priv->buf[Count++] = FrqGenAdr << 1;
504 priv->buf[Count++] = 0x13;
505 priv->buf[Count++] = 0x6B; /* Adr = 0x13 */
506 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
507 priv->buf[Count++] = FrqGenAdr << 1;
508 priv->buf[Count++] = 0x40;
509 priv->buf[Count++] = (0xC0 | ((PUMP & 0x07) << 2)) |
510 (PBmsb & 0x03); /* Adr = 0x40 */
511 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
512 priv->buf[Count++] = FrqGenAdr << 1;
513 priv->buf[Count++] = 0x41;
514 priv->buf[Count++] = PBlsb; /* Adr = 0x41 */
515 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
516 priv->buf[Count++] = FrqGenAdr << 1;
517 priv->buf[Count++] = 0x42;
518 priv->buf[Count++] = Q | (((PO & 0x01) << 7)); /* Adr = 0x42 */
519 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
520 priv->buf[Count++] = FrqGenAdr << 1;
521 priv->buf[Count++] = 0x44;
522 priv->buf[Count++] = (char)0xFF; /* Adr = 0x44 */
523 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
524 priv->buf[Count++] = FrqGenAdr << 1;
525 priv->buf[Count++] = 0x45;
526 priv->buf[Count++] = (char)0xFE; /* Adr = 0x45 */
527 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
528 priv->buf[Count++] = FrqGenAdr << 1;
529 priv->buf[Count++] = 0x46;
530 priv->buf[Count++] = 0x7F; /* Adr = 0x46 */
531 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
532 priv->buf[Count++] = FrqGenAdr << 1;
533 priv->buf[Count++] = 0x47;
534 priv->buf[Count++] = (char)0x84; /* Adr = 0x47 */
535
536 status = bulk_immediate(port, (u8 *) priv->buf, Count);
537 if (status != IUU_OPERATION_OK)
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700538 dev_dbg(&port->dev, "%s - write error\n", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200539 return status;
540}
541
542static int iuu_uart_flush(struct usb_serial_port *port)
543{
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700544 struct device *dev = &port->dev;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200545 int i;
546 int status;
547 u8 rxcmd = IUU_UART_RX;
548 struct iuu_private *priv = usb_get_serial_port_data(port);
549
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200550 if (iuu_led(port, 0xF000, 0, 0, 0xFF) < 0)
551 return -EIO;
552
553 for (i = 0; i < 2; i++) {
554 status = bulk_immediate(port, &rxcmd, 1);
555 if (status != IUU_OPERATION_OK) {
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700556 dev_dbg(dev, "%s - uart_flush_write error\n", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200557 return status;
558 }
559
560 status = read_immediate(port, &priv->len, 1);
561 if (status != IUU_OPERATION_OK) {
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700562 dev_dbg(dev, "%s - uart_flush_read error\n", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200563 return status;
564 }
565
566 if (priv->len > 0) {
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700567 dev_dbg(dev, "%s - uart_flush datalen is : %i\n", __func__, priv->len);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200568 status = read_immediate(port, priv->buf, priv->len);
569 if (status != IUU_OPERATION_OK) {
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700570 dev_dbg(dev, "%s - uart_flush_read error\n", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200571 return status;
572 }
573 }
574 }
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700575 dev_dbg(dev, "%s - uart_flush_read OK!\n", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200576 iuu_led(port, 0, 0xF000, 0, 0xFF);
577 return status;
578}
579
580static void read_buf_callback(struct urb *urb)
581{
Ming Leicdc97792008-02-24 18:41:47 +0800582 struct usb_serial_port *port = urb->context;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200583 unsigned char *data = urb->transfer_buffer;
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800584 int status = urb->status;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200585
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800586 if (status) {
587 if (status == -EPROTO) {
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200588 /* reschedule needed */
589 }
590 return;
591 }
592
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700593 dev_dbg(&port->dev, "%s - %i chars to write\n", __func__, urb->actual_length);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200594 if (data == NULL)
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700595 dev_dbg(&port->dev, "%s - data is NULL !!!\n", __func__);
Jiri Slaby2e124b42013-01-03 15:53:06 +0100596 if (urb->actual_length && data) {
Jiri Slaby05c7cd32013-01-03 15:53:04 +0100597 tty_insert_flip_string(&port->port, data, urb->actual_length);
Jiri Slaby2e124b42013-01-03 15:53:06 +0100598 tty_flip_buffer_push(&port->port);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200599 }
600 iuu_led_activity_on(urb);
601}
602
603static int iuu_bulk_write(struct usb_serial_port *port)
604{
605 struct iuu_private *priv = usb_get_serial_port_data(port);
Steven Rostedt85943032008-05-06 20:42:31 -0700606 unsigned long flags;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200607 int result;
Olivier Bornet5fcf62b2009-06-11 12:52:26 +0100608 int buf_len;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200609 char *buf_ptr = port->write_urb->transfer_buffer;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200610
Olivier Bornet5fcf62b2009-06-11 12:52:26 +0100611 spin_lock_irqsave(&priv->lock, flags);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200612 *buf_ptr++ = IUU_UART_ESC;
613 *buf_ptr++ = IUU_UART_TX;
614 *buf_ptr++ = priv->writelen;
615
Olivier Bornet5fcf62b2009-06-11 12:52:26 +0100616 memcpy(buf_ptr, priv->writebuf, priv->writelen);
617 buf_len = priv->writelen;
618 priv->writelen = 0;
619 spin_unlock_irqrestore(&priv->lock, flags);
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700620 dev_dbg(&port->dev, "%s - writing %i chars : %*ph\n", __func__,
621 buf_len, buf_len, buf_ptr);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200622 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
623 usb_sndbulkpipe(port->serial->dev,
624 port->bulk_out_endpointAddress),
Olivier Bornet5fcf62b2009-06-11 12:52:26 +0100625 port->write_urb->transfer_buffer, buf_len + 3,
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200626 iuu_rxcmd, port);
627 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200628 usb_serial_port_softint(port);
629 return result;
630}
631
632static int iuu_read_buf(struct usb_serial_port *port, int len)
633{
634 int result;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200635
636 usb_fill_bulk_urb(port->read_urb, port->serial->dev,
637 usb_rcvbulkpipe(port->serial->dev,
638 port->bulk_in_endpointAddress),
639 port->read_urb->transfer_buffer, len,
640 read_buf_callback, port);
641 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
642 return result;
643}
644
645static void iuu_uart_read_callback(struct urb *urb)
646{
Ming Leicdc97792008-02-24 18:41:47 +0800647 struct usb_serial_port *port = urb->context;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200648 struct iuu_private *priv = usb_get_serial_port_data(port);
Steven Rostedt85943032008-05-06 20:42:31 -0700649 unsigned long flags;
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800650 int status = urb->status;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200651 int error = 0;
652 int len = 0;
653 unsigned char *data = urb->transfer_buffer;
654 priv->poll++;
655
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800656 if (status) {
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700657 dev_dbg(&port->dev, "%s - status = %d\n", __func__, status);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200658 /* error stop all */
659 return;
660 }
661 if (data == NULL)
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700662 dev_dbg(&port->dev, "%s - data is NULL !!!\n", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200663
664 if (urb->actual_length == 1 && data != NULL)
665 len = (int) data[0];
666
667 if (urb->actual_length > 1) {
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700668 dev_dbg(&port->dev, "%s - urb->actual_length = %i\n", __func__,
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200669 urb->actual_length);
670 error = 1;
671 return;
672 }
673 /* if len > 0 call readbuf */
674
675 if (len > 0 && error == 0) {
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700676 dev_dbg(&port->dev, "%s - call read buf - len to read is %i\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800677 __func__, len);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200678 status = iuu_read_buf(port, len);
679 return;
680 }
681 /* need to update status ? */
682 if (priv->poll > 99) {
683 status = iuu_status(port);
684 priv->poll = 0;
685 return;
686 }
687
688 /* reset waiting ? */
689
690 if (priv->reset == 1) {
691 status = iuu_reset(port, 0xC);
692 return;
693 }
694 /* Writebuf is waiting */
695 spin_lock_irqsave(&priv->lock, flags);
696 if (priv->writelen > 0) {
697 spin_unlock_irqrestore(&priv->lock, flags);
698 status = iuu_bulk_write(port);
699 return;
700 }
701 spin_unlock_irqrestore(&priv->lock, flags);
702 /* if nothing to write call again rxcmd */
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700703 dev_dbg(&port->dev, "%s - rxcmd recall\n", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200704 iuu_led_activity_off(urb);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200705}
706
Alan Cox95da3102008-07-22 11:09:07 +0100707static int iuu_uart_write(struct tty_struct *tty, struct usb_serial_port *port,
708 const u8 *buf, int count)
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200709{
710 struct iuu_private *priv = usb_get_serial_port_data(port);
Steven Rostedt85943032008-05-06 20:42:31 -0700711 unsigned long flags;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200712
713 if (count > 256)
714 return -ENOMEM;
715
716 spin_lock_irqsave(&priv->lock, flags);
Olivier Bornet5fcf62b2009-06-11 12:52:26 +0100717
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200718 /* fill the buffer */
Olivier Bornet5fcf62b2009-06-11 12:52:26 +0100719 memcpy(priv->writebuf + priv->writelen, buf, count);
720 priv->writelen += count;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200721 spin_unlock_irqrestore(&priv->lock, flags);
722
Alan Cox9e8e2d22008-07-22 11:12:59 +0100723 return count;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200724}
725
726static void read_rxcmd_callback(struct urb *urb)
727{
Ming Leicdc97792008-02-24 18:41:47 +0800728 struct usb_serial_port *port = urb->context;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200729 int result;
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800730 int status = urb->status;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200731
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800732 if (status) {
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200733 /* error stop all */
734 return;
735 }
736
737 usb_fill_bulk_urb(port->read_urb, port->serial->dev,
738 usb_rcvbulkpipe(port->serial->dev,
739 port->bulk_in_endpointAddress),
740 port->read_urb->transfer_buffer, 256,
741 iuu_uart_read_callback, port);
742 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700743 dev_dbg(&port->dev, "%s - submit result = %d\n", __func__, result);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200744}
745
746static int iuu_uart_on(struct usb_serial_port *port)
747{
748 int status;
749 u8 *buf;
750
751 buf = kmalloc(sizeof(u8) * 4, GFP_KERNEL);
752
753 if (!buf)
754 return -ENOMEM;
755
756 buf[0] = IUU_UART_ENABLE;
757 buf[1] = (u8) ((IUU_BAUD_9600 >> 8) & 0x00FF);
758 buf[2] = (u8) (0x00FF & IUU_BAUD_9600);
Olivier Bornetcc3447d2009-06-11 12:53:24 +0100759 buf[3] = (u8) (0x0F0 & IUU_ONE_STOP_BIT) | (0x07 & IUU_PARITY_EVEN);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200760
761 status = bulk_immediate(port, buf, 4);
762 if (status != IUU_OPERATION_OK) {
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700763 dev_dbg(&port->dev, "%s - uart_on error\n", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200764 goto uart_enable_failed;
765 }
766 /* iuu_reset() the card after iuu_uart_on() */
767 status = iuu_uart_flush(port);
768 if (status != IUU_OPERATION_OK)
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700769 dev_dbg(&port->dev, "%s - uart_flush error\n", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200770uart_enable_failed:
771 kfree(buf);
772 return status;
773}
774
775/* Diables the IUU UART (a.k.a. the Phoenix voiderface) */
776static int iuu_uart_off(struct usb_serial_port *port)
777{
778 int status;
779 u8 *buf;
780 buf = kmalloc(1, GFP_KERNEL);
781 if (!buf)
782 return -ENOMEM;
783 buf[0] = IUU_UART_DISABLE;
784
785 status = bulk_immediate(port, buf, 1);
786 if (status != IUU_OPERATION_OK)
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700787 dev_dbg(&port->dev, "%s - uart_off error\n", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200788
789 kfree(buf);
790 return status;
791}
792
James Courtier-Dutton89b54392010-05-21 11:53:25 +0100793static int iuu_uart_baud(struct usb_serial_port *port, u32 baud_base,
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200794 u32 *actual, u8 parity)
795{
796 int status;
James Courtier-Dutton89b54392010-05-21 11:53:25 +0100797 u32 baud;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200798 u8 *dataout;
799 u8 DataCount = 0;
800 u8 T1Frekvens = 0;
801 u8 T1reload = 0;
802 unsigned int T1FrekvensHZ = 0;
803
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700804 dev_dbg(&port->dev, "%s - enter baud_base=%d\n", __func__, baud_base);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200805 dataout = kmalloc(sizeof(u8) * 5, GFP_KERNEL);
806
807 if (!dataout)
808 return -ENOMEM;
James Courtier-Dutton89b54392010-05-21 11:53:25 +0100809 /*baud = (((priv->clk / 35) * baud_base) / 100000); */
810 baud = baud_base;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200811
812 if (baud < 1200 || baud > 230400) {
813 kfree(dataout);
814 return IUU_INVALID_PARAMETER;
815 }
816 if (baud > 977) {
817 T1Frekvens = 3;
818 T1FrekvensHZ = 500000;
819 }
820
821 if (baud > 3906) {
822 T1Frekvens = 2;
823 T1FrekvensHZ = 2000000;
824 }
825
826 if (baud > 11718) {
827 T1Frekvens = 1;
828 T1FrekvensHZ = 6000000;
829 }
830
831 if (baud > 46875) {
832 T1Frekvens = 0;
833 T1FrekvensHZ = 24000000;
834 }
835
836 T1reload = 256 - (u8) (T1FrekvensHZ / (baud * 2));
837
838 /* magic number here: ENTER_FIRMWARE_UPDATE; */
839 dataout[DataCount++] = IUU_UART_ESC;
840 /* magic number here: CHANGE_BAUD; */
841 dataout[DataCount++] = IUU_UART_CHANGE;
842 dataout[DataCount++] = T1Frekvens;
843 dataout[DataCount++] = T1reload;
844
845 *actual = (T1FrekvensHZ / (256 - T1reload)) / 2;
846
847 switch (parity & 0x0F) {
848 case IUU_PARITY_NONE:
849 dataout[DataCount++] = 0x00;
850 break;
851 case IUU_PARITY_EVEN:
852 dataout[DataCount++] = 0x01;
853 break;
854 case IUU_PARITY_ODD:
855 dataout[DataCount++] = 0x02;
856 break;
857 case IUU_PARITY_MARK:
858 dataout[DataCount++] = 0x03;
859 break;
860 case IUU_PARITY_SPACE:
861 dataout[DataCount++] = 0x04;
862 break;
863 default:
864 kfree(dataout);
865 return IUU_INVALID_PARAMETER;
866 break;
867 }
868
869 switch (parity & 0xF0) {
870 case IUU_ONE_STOP_BIT:
871 dataout[DataCount - 1] |= IUU_ONE_STOP_BIT;
872 break;
873
874 case IUU_TWO_STOP_BITS:
875 dataout[DataCount - 1] |= IUU_TWO_STOP_BITS;
876 break;
877 default:
878 kfree(dataout);
879 return IUU_INVALID_PARAMETER;
880 break;
881 }
882
883 status = bulk_immediate(port, dataout, DataCount);
884 if (status != IUU_OPERATION_OK)
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700885 dev_dbg(&port->dev, "%s - uart_off error\n", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200886 kfree(dataout);
887 return status;
888}
889
Olivier Bornet96dab772009-06-11 12:54:20 +0100890static void iuu_set_termios(struct tty_struct *tty,
891 struct usb_serial_port *port, struct ktermios *old_termios)
892{
893 const u32 supported_mask = CMSPAR|PARENB|PARODD;
James Courtier-Dutton89b54392010-05-21 11:53:25 +0100894 struct iuu_private *priv = usb_get_serial_port_data(port);
Alan Coxadc8d742012-07-14 15:31:47 +0100895 unsigned int cflag = tty->termios.c_cflag;
Olivier Bornet96dab772009-06-11 12:54:20 +0100896 int status;
897 u32 actual;
898 u32 parity;
899 int csize = CS7;
James Courtier-Dutton89b54392010-05-21 11:53:25 +0100900 int baud;
Olivier Bornet96dab772009-06-11 12:54:20 +0100901 u32 newval = cflag & supported_mask;
902
James Courtier-Dutton89b54392010-05-21 11:53:25 +0100903 /* Just use the ospeed. ispeed should be the same. */
Alan Coxadc8d742012-07-14 15:31:47 +0100904 baud = tty->termios.c_ospeed;
James Courtier-Dutton89b54392010-05-21 11:53:25 +0100905
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700906 dev_dbg(&port->dev, "%s - enter c_ospeed or baud=%d\n", __func__, baud);
James Courtier-Dutton89b54392010-05-21 11:53:25 +0100907
Olivier Bornet96dab772009-06-11 12:54:20 +0100908 /* compute the parity parameter */
909 parity = 0;
910 if (cflag & CMSPAR) { /* Using mark space */
911 if (cflag & PARODD)
912 parity |= IUU_PARITY_SPACE;
913 else
914 parity |= IUU_PARITY_MARK;
915 } else if (!(cflag & PARENB)) {
916 parity |= IUU_PARITY_NONE;
917 csize = CS8;
918 } else if (cflag & PARODD)
919 parity |= IUU_PARITY_ODD;
920 else
921 parity |= IUU_PARITY_EVEN;
922
923 parity |= (cflag & CSTOPB ? IUU_TWO_STOP_BITS : IUU_ONE_STOP_BIT);
924
925 /* set it */
926 status = iuu_uart_baud(port,
James Courtier-Dutton89b54392010-05-21 11:53:25 +0100927 baud * priv->boost / 100,
Olivier Bornet96dab772009-06-11 12:54:20 +0100928 &actual, parity);
929
930 /* set the termios value to the real one, so the user now what has
931 * changed. We support few fields so its easies to copy the old hw
932 * settings back over and then adjust them
933 */
James Courtier-Dutton89b54392010-05-21 11:53:25 +0100934 if (old_termios)
Alan Coxadc8d742012-07-14 15:31:47 +0100935 tty_termios_copy_hw(&tty->termios, old_termios);
Olivier Bornet96dab772009-06-11 12:54:20 +0100936 if (status != 0) /* Set failed - return old bits */
937 return;
938 /* Re-encode speed, parity and csize */
939 tty_encode_baud_rate(tty, baud, baud);
Alan Coxadc8d742012-07-14 15:31:47 +0100940 tty->termios.c_cflag &= ~(supported_mask|CSIZE);
941 tty->termios.c_cflag |= newval | csize;
Olivier Bornet96dab772009-06-11 12:54:20 +0100942}
943
Alan Cox335f8512009-06-11 12:26:29 +0100944static void iuu_close(struct usb_serial_port *port)
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200945{
946 /* iuu_led (port,255,0,0,0); */
947 struct usb_serial *serial;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200948
949 serial = port->serial;
950 if (!serial)
951 return;
952
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200953 iuu_uart_off(port);
954 if (serial->dev) {
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200955 /* free writebuf */
956 /* shutdown our urbs */
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700957 dev_dbg(&port->dev, "%s - shutting down urbs\n", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200958 usb_kill_urb(port->write_urb);
959 usb_kill_urb(port->read_urb);
960 usb_kill_urb(port->interrupt_in_urb);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200961 iuu_led(port, 0, 0, 0xF000, 0xFF);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200962 }
963}
964
Alan Coxfe1ae7f2009-09-19 13:13:33 -0700965static void iuu_init_termios(struct tty_struct *tty)
966{
Alan Coxadc8d742012-07-14 15:31:47 +0100967 tty->termios = tty_std_termios;
968 tty->termios.c_cflag = CLOCAL | CREAD | CS8 | B9600
Alan Coxfe1ae7f2009-09-19 13:13:33 -0700969 | TIOCM_CTS | CSTOPB | PARENB;
Alan Coxadc8d742012-07-14 15:31:47 +0100970 tty->termios.c_ispeed = 9600;
971 tty->termios.c_ospeed = 9600;
972 tty->termios.c_lflag = 0;
973 tty->termios.c_oflag = 0;
974 tty->termios.c_iflag = 0;
Alan Coxfe1ae7f2009-09-19 13:13:33 -0700975}
976
Alan Coxa509a7e2009-09-19 13:13:26 -0700977static int iuu_open(struct tty_struct *tty, struct usb_serial_port *port)
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200978{
979 struct usb_serial *serial = port->serial;
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700980 struct device *dev = &port->dev;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200981 u8 *buf;
982 int result;
James Courtier-Dutton89b54392010-05-21 11:53:25 +0100983 int baud;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200984 u32 actual;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200985 struct iuu_private *priv = usb_get_serial_port_data(port);
986
Alan Coxadc8d742012-07-14 15:31:47 +0100987 baud = tty->termios.c_ospeed;
988 tty->termios.c_ispeed = baud;
James Courtier-Dutton89b54392010-05-21 11:53:25 +0100989 /* Re-encode speed */
990 tty_encode_baud_rate(tty, baud, baud);
991
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700992 dev_dbg(dev, "%s - baud %d\n", __func__, baud);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200993 usb_clear_halt(serial->dev, port->write_urb->pipe);
994 usb_clear_halt(serial->dev, port->read_urb->pipe);
995
996 buf = kmalloc(10, GFP_KERNEL);
997 if (buf == NULL)
998 return -ENOMEM;
999
Alan Coxfe1ae7f2009-09-19 13:13:33 -07001000 priv->poll = 0;
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001001
1002 /* initialize writebuf */
1003#define FISH(a, b, c, d) do { \
1004 result = usb_control_msg(port->serial->dev, \
1005 usb_rcvctrlpipe(port->serial->dev, 0), \
1006 b, a, c, d, buf, 1, 1000); \
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -07001007 dev_dbg(dev, "0x%x:0x%x:0x%x:0x%x %d - %x\n", a, b, c, d, result, \
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001008 buf[0]); } while (0);
1009
1010#define SOUP(a, b, c, d) do { \
1011 result = usb_control_msg(port->serial->dev, \
1012 usb_sndctrlpipe(port->serial->dev, 0), \
1013 b, a, c, d, NULL, 0, 1000); \
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -07001014 dev_dbg(dev, "0x%x:0x%x:0x%x:0x%x %d\n", a, b, c, d, result); } while (0)
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001015
1016 /* This is not UART related but IUU USB driver related or something */
1017 /* like that. Basically no IUU will accept any commands from the USB */
1018 /* host unless it has received the following message */
1019 /* sprintf(buf ,"%c%c%c%c",0x03,0x02,0x02,0x0); */
1020
1021 SOUP(0x03, 0x02, 0x02, 0x0);
1022 kfree(buf);
1023 iuu_led(port, 0xF000, 0xF000, 0, 0xFF);
1024 iuu_uart_on(port);
1025 if (boost < 100)
1026 boost = 100;
James Courtier-Dutton89b54392010-05-21 11:53:25 +01001027 priv->boost = boost;
1028 priv->baud = baud;
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001029 switch (clockmode) {
1030 case 2: /* 3.680 Mhz */
James Courtier-Dutton89b54392010-05-21 11:53:25 +01001031 priv->clk = IUU_CLK_3680000;
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001032 iuu_clk(port, IUU_CLK_3680000 * boost / 100);
1033 result =
James Courtier-Dutton89b54392010-05-21 11:53:25 +01001034 iuu_uart_baud(port, baud * boost / 100, &actual,
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001035 IUU_PARITY_EVEN);
1036 break;
1037 case 3: /* 6.00 Mhz */
1038 iuu_clk(port, IUU_CLK_6000000 * boost / 100);
James Courtier-Dutton89b54392010-05-21 11:53:25 +01001039 priv->clk = IUU_CLK_6000000;
1040 /* Ratio of 6000000 to 3500000 for baud 9600 */
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001041 result =
1042 iuu_uart_baud(port, 16457 * boost / 100, &actual,
1043 IUU_PARITY_EVEN);
1044 break;
1045 default: /* 3.579 Mhz */
1046 iuu_clk(port, IUU_CLK_3579000 * boost / 100);
James Courtier-Dutton89b54392010-05-21 11:53:25 +01001047 priv->clk = IUU_CLK_3579000;
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001048 result =
James Courtier-Dutton89b54392010-05-21 11:53:25 +01001049 iuu_uart_baud(port, baud * boost / 100, &actual,
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001050 IUU_PARITY_EVEN);
1051 }
1052
1053 /* set the cardin cardout signals */
1054 switch (cdmode) {
1055 case 0:
1056 iuu_cardin = 0;
1057 iuu_cardout = 0;
1058 break;
1059 case 1:
1060 iuu_cardin = TIOCM_CD;
1061 iuu_cardout = 0;
1062 break;
1063 case 2:
1064 iuu_cardin = 0;
1065 iuu_cardout = TIOCM_CD;
1066 break;
1067 case 3:
1068 iuu_cardin = TIOCM_DSR;
1069 iuu_cardout = 0;
1070 break;
1071 case 4:
1072 iuu_cardin = 0;
1073 iuu_cardout = TIOCM_DSR;
1074 break;
1075 case 5:
1076 iuu_cardin = TIOCM_CTS;
1077 iuu_cardout = 0;
1078 break;
1079 case 6:
1080 iuu_cardin = 0;
1081 iuu_cardout = TIOCM_CTS;
1082 break;
1083 case 7:
1084 iuu_cardin = TIOCM_RNG;
1085 iuu_cardout = 0;
1086 break;
1087 case 8:
1088 iuu_cardin = 0;
1089 iuu_cardout = TIOCM_RNG;
1090 }
1091
1092 iuu_uart_flush(port);
1093
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -07001094 dev_dbg(dev, "%s - initialization done\n", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001095
1096 memset(port->write_urb->transfer_buffer, IUU_UART_RX, 1);
1097 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
1098 usb_sndbulkpipe(port->serial->dev,
1099 port->bulk_out_endpointAddress),
1100 port->write_urb->transfer_buffer, 1,
1101 read_rxcmd_callback, port);
1102 result = usb_submit_urb(port->write_urb, GFP_KERNEL);
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001103 if (result) {
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -07001104 dev_err(dev, "%s - failed submitting read urb, error %d\n", __func__, result);
Alan Cox335f8512009-06-11 12:26:29 +01001105 iuu_close(port);
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001106 } else {
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -07001107 dev_dbg(dev, "%s - rxcmd OK\n", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001108 }
Johan Hovoldb58a6462011-11-10 14:58:30 +01001109
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001110 return result;
1111}
1112
Olivier Bornet20eda942009-08-18 21:05:55 +02001113/* how to change VCC */
1114static int iuu_vcc_set(struct usb_serial_port *port, unsigned int vcc)
1115{
1116 int status;
1117 u8 *buf;
1118
1119 buf = kmalloc(5, GFP_KERNEL);
1120 if (!buf)
1121 return -ENOMEM;
1122
Olivier Bornet20eda942009-08-18 21:05:55 +02001123 buf[0] = IUU_SET_VCC;
1124 buf[1] = vcc & 0xFF;
1125 buf[2] = (vcc >> 8) & 0xFF;
1126 buf[3] = (vcc >> 16) & 0xFF;
1127 buf[4] = (vcc >> 24) & 0xFF;
1128
1129 status = bulk_immediate(port, buf, 5);
1130 kfree(buf);
1131
1132 if (status != IUU_OPERATION_OK)
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -07001133 dev_dbg(&port->dev, "%s - vcc error status = %2x\n", __func__, status);
Olivier Bornet20eda942009-08-18 21:05:55 +02001134 else
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -07001135 dev_dbg(&port->dev, "%s - vcc OK !\n", __func__);
Olivier Bornet20eda942009-08-18 21:05:55 +02001136
1137 return status;
1138}
1139
1140/*
1141 * Sysfs Attributes
1142 */
1143
1144static ssize_t show_vcc_mode(struct device *dev,
1145 struct device_attribute *attr, char *buf)
1146{
1147 struct usb_serial_port *port = to_usb_serial_port(dev);
1148 struct iuu_private *priv = usb_get_serial_port_data(port);
1149
1150 return sprintf(buf, "%d\n", priv->vcc);
1151}
1152
1153static ssize_t store_vcc_mode(struct device *dev,
1154 struct device_attribute *attr, const char *buf, size_t count)
1155{
1156 struct usb_serial_port *port = to_usb_serial_port(dev);
1157 struct iuu_private *priv = usb_get_serial_port_data(port);
1158 unsigned long v;
1159
Jingoo Han487c1512012-10-29 18:37:31 +09001160 if (kstrtoul(buf, 10, &v)) {
Olivier Bornet20eda942009-08-18 21:05:55 +02001161 dev_err(dev, "%s - vcc_mode: %s is not a unsigned long\n",
1162 __func__, buf);
1163 goto fail_store_vcc_mode;
1164 }
1165
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -07001166 dev_dbg(dev, "%s: setting vcc_mode = %ld", __func__, v);
Olivier Bornet20eda942009-08-18 21:05:55 +02001167
1168 if ((v != 3) && (v != 5)) {
1169 dev_err(dev, "%s - vcc_mode %ld is invalid\n", __func__, v);
1170 } else {
1171 iuu_vcc_set(port, v);
1172 priv->vcc = v;
1173 }
1174fail_store_vcc_mode:
1175 return count;
1176}
1177
1178static DEVICE_ATTR(vcc_mode, S_IRUSR | S_IWUSR, show_vcc_mode,
1179 store_vcc_mode);
1180
1181static int iuu_create_sysfs_attrs(struct usb_serial_port *port)
1182{
Olivier Bornet20eda942009-08-18 21:05:55 +02001183 return device_create_file(&port->dev, &dev_attr_vcc_mode);
1184}
1185
1186static int iuu_remove_sysfs_attrs(struct usb_serial_port *port)
1187{
Olivier Bornet20eda942009-08-18 21:05:55 +02001188 device_remove_file(&port->dev, &dev_attr_vcc_mode);
1189 return 0;
1190}
1191
1192/*
1193 * End Sysfs Attributes
1194 */
1195
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001196static struct usb_serial_driver iuu_device = {
1197 .driver = {
1198 .owner = THIS_MODULE,
1199 .name = "iuu_phoenix",
1200 },
1201 .id_table = id_table,
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001202 .num_ports = 1,
Johan Hovoldbbcb2b92010-03-17 23:00:37 +01001203 .bulk_in_size = 512,
1204 .bulk_out_size = 512,
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001205 .open = iuu_open,
1206 .close = iuu_close,
1207 .write = iuu_uart_write,
1208 .read_bulk_callback = iuu_uart_read_callback,
1209 .tiocmget = iuu_tiocmget,
1210 .tiocmset = iuu_tiocmset,
Olivier Bornet96dab772009-06-11 12:54:20 +01001211 .set_termios = iuu_set_termios,
Alan Coxfe1ae7f2009-09-19 13:13:33 -07001212 .init_termios = iuu_init_termios,
Johan Hovold53636552012-10-17 13:34:59 +02001213 .port_probe = iuu_port_probe,
1214 .port_remove = iuu_port_remove,
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001215};
1216
Alan Stern7dbe2462012-02-23 14:56:57 -05001217static struct usb_serial_driver * const serial_drivers[] = {
1218 &iuu_device, NULL
1219};
1220
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -07001221module_usb_serial_driver(serial_drivers, id_table);
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001222
1223MODULE_AUTHOR("Alain Degreffe eczema@ecze.com");
1224
1225MODULE_DESCRIPTION(DRIVER_DESC);
1226MODULE_LICENSE("GPL");
1227
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001228module_param(xmas, bool, S_IRUGO | S_IWUSR);
Olivier Bornet8844a322009-08-18 21:05:54 +02001229MODULE_PARM_DESC(xmas, "Xmas colors enabled or not");
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001230
1231module_param(boost, int, S_IRUGO | S_IWUSR);
Olivier Bornet8844a322009-08-18 21:05:54 +02001232MODULE_PARM_DESC(boost, "Card overclock boost (in percent 100-500)");
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001233
1234module_param(clockmode, int, S_IRUGO | S_IWUSR);
Olivier Bornet8844a322009-08-18 21:05:54 +02001235MODULE_PARM_DESC(clockmode, "Card clock mode (1=3.579 MHz, 2=3.680 MHz, "
1236 "3=6 Mhz)");
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001237
1238module_param(cdmode, int, S_IRUGO | S_IWUSR);
Olivier Bornet8844a322009-08-18 21:05:54 +02001239MODULE_PARM_DESC(cdmode, "Card detect mode (0=none, 1=CD, 2=!CD, 3=DSR, "
1240 "4=!DSR, 5=CTS, 6=!CTS, 7=RING, 8=!RING)");
Olivier Bornete55c6d02009-08-18 21:05:57 +02001241
1242module_param(vcc_default, int, S_IRUGO | S_IWUSR);
1243MODULE_PARM_DESC(vcc_default, "Set default VCC (either 3 for 3.3V or 5 "
1244 "for 5V). Default to 5.");