blob: 1e1fbed65ef244a80f3a2c41d3a117f67eb6c014 [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;
584 struct tty_struct *tty;
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800585 int status = urb->status;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200586
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800587 if (status) {
588 if (status == -EPROTO) {
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200589 /* reschedule needed */
590 }
591 return;
592 }
593
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700594 dev_dbg(&port->dev, "%s - %i chars to write\n", __func__, urb->actual_length);
Alan Cox4a90f092008-10-13 10:39:46 +0100595 tty = tty_port_tty_get(&port->port);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200596 if (data == NULL)
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700597 dev_dbg(&port->dev, "%s - data is NULL !!!\n", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200598 if (tty && urb->actual_length && data) {
599 tty_insert_flip_string(tty, data, urb->actual_length);
600 tty_flip_buffer_push(tty);
601 }
Alan Cox4a90f092008-10-13 10:39:46 +0100602 tty_kref_put(tty);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200603 iuu_led_activity_on(urb);
604}
605
606static int iuu_bulk_write(struct usb_serial_port *port)
607{
608 struct iuu_private *priv = usb_get_serial_port_data(port);
Steven Rostedt85943032008-05-06 20:42:31 -0700609 unsigned long flags;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200610 int result;
Olivier Bornet5fcf62b2009-06-11 12:52:26 +0100611 int buf_len;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200612 char *buf_ptr = port->write_urb->transfer_buffer;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200613
Olivier Bornet5fcf62b2009-06-11 12:52:26 +0100614 spin_lock_irqsave(&priv->lock, flags);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200615 *buf_ptr++ = IUU_UART_ESC;
616 *buf_ptr++ = IUU_UART_TX;
617 *buf_ptr++ = priv->writelen;
618
Olivier Bornet5fcf62b2009-06-11 12:52:26 +0100619 memcpy(buf_ptr, priv->writebuf, priv->writelen);
620 buf_len = priv->writelen;
621 priv->writelen = 0;
622 spin_unlock_irqrestore(&priv->lock, flags);
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700623 dev_dbg(&port->dev, "%s - writing %i chars : %*ph\n", __func__,
624 buf_len, buf_len, buf_ptr);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200625 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
626 usb_sndbulkpipe(port->serial->dev,
627 port->bulk_out_endpointAddress),
Olivier Bornet5fcf62b2009-06-11 12:52:26 +0100628 port->write_urb->transfer_buffer, buf_len + 3,
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200629 iuu_rxcmd, port);
630 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200631 usb_serial_port_softint(port);
632 return result;
633}
634
635static int iuu_read_buf(struct usb_serial_port *port, int len)
636{
637 int result;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200638
639 usb_fill_bulk_urb(port->read_urb, port->serial->dev,
640 usb_rcvbulkpipe(port->serial->dev,
641 port->bulk_in_endpointAddress),
642 port->read_urb->transfer_buffer, len,
643 read_buf_callback, port);
644 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
645 return result;
646}
647
648static void iuu_uart_read_callback(struct urb *urb)
649{
Ming Leicdc97792008-02-24 18:41:47 +0800650 struct usb_serial_port *port = urb->context;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200651 struct iuu_private *priv = usb_get_serial_port_data(port);
Steven Rostedt85943032008-05-06 20:42:31 -0700652 unsigned long flags;
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800653 int status = urb->status;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200654 int error = 0;
655 int len = 0;
656 unsigned char *data = urb->transfer_buffer;
657 priv->poll++;
658
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800659 if (status) {
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700660 dev_dbg(&port->dev, "%s - status = %d\n", __func__, status);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200661 /* error stop all */
662 return;
663 }
664 if (data == NULL)
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700665 dev_dbg(&port->dev, "%s - data is NULL !!!\n", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200666
667 if (urb->actual_length == 1 && data != NULL)
668 len = (int) data[0];
669
670 if (urb->actual_length > 1) {
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700671 dev_dbg(&port->dev, "%s - urb->actual_length = %i\n", __func__,
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200672 urb->actual_length);
673 error = 1;
674 return;
675 }
676 /* if len > 0 call readbuf */
677
678 if (len > 0 && error == 0) {
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700679 dev_dbg(&port->dev, "%s - call read buf - len to read is %i\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800680 __func__, len);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200681 status = iuu_read_buf(port, len);
682 return;
683 }
684 /* need to update status ? */
685 if (priv->poll > 99) {
686 status = iuu_status(port);
687 priv->poll = 0;
688 return;
689 }
690
691 /* reset waiting ? */
692
693 if (priv->reset == 1) {
694 status = iuu_reset(port, 0xC);
695 return;
696 }
697 /* Writebuf is waiting */
698 spin_lock_irqsave(&priv->lock, flags);
699 if (priv->writelen > 0) {
700 spin_unlock_irqrestore(&priv->lock, flags);
701 status = iuu_bulk_write(port);
702 return;
703 }
704 spin_unlock_irqrestore(&priv->lock, flags);
705 /* if nothing to write call again rxcmd */
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700706 dev_dbg(&port->dev, "%s - rxcmd recall\n", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200707 iuu_led_activity_off(urb);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200708}
709
Alan Cox95da3102008-07-22 11:09:07 +0100710static int iuu_uart_write(struct tty_struct *tty, struct usb_serial_port *port,
711 const u8 *buf, int count)
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200712{
713 struct iuu_private *priv = usb_get_serial_port_data(port);
Steven Rostedt85943032008-05-06 20:42:31 -0700714 unsigned long flags;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200715
716 if (count > 256)
717 return -ENOMEM;
718
719 spin_lock_irqsave(&priv->lock, flags);
Olivier Bornet5fcf62b2009-06-11 12:52:26 +0100720
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200721 /* fill the buffer */
Olivier Bornet5fcf62b2009-06-11 12:52:26 +0100722 memcpy(priv->writebuf + priv->writelen, buf, count);
723 priv->writelen += count;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200724 spin_unlock_irqrestore(&priv->lock, flags);
725
Alan Cox9e8e2d22008-07-22 11:12:59 +0100726 return count;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200727}
728
729static void read_rxcmd_callback(struct urb *urb)
730{
Ming Leicdc97792008-02-24 18:41:47 +0800731 struct usb_serial_port *port = urb->context;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200732 int result;
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800733 int status = urb->status;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200734
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800735 if (status) {
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200736 /* error stop all */
737 return;
738 }
739
740 usb_fill_bulk_urb(port->read_urb, port->serial->dev,
741 usb_rcvbulkpipe(port->serial->dev,
742 port->bulk_in_endpointAddress),
743 port->read_urb->transfer_buffer, 256,
744 iuu_uart_read_callback, port);
745 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700746 dev_dbg(&port->dev, "%s - submit result = %d\n", __func__, result);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200747}
748
749static int iuu_uart_on(struct usb_serial_port *port)
750{
751 int status;
752 u8 *buf;
753
754 buf = kmalloc(sizeof(u8) * 4, GFP_KERNEL);
755
756 if (!buf)
757 return -ENOMEM;
758
759 buf[0] = IUU_UART_ENABLE;
760 buf[1] = (u8) ((IUU_BAUD_9600 >> 8) & 0x00FF);
761 buf[2] = (u8) (0x00FF & IUU_BAUD_9600);
Olivier Bornetcc3447d2009-06-11 12:53:24 +0100762 buf[3] = (u8) (0x0F0 & IUU_ONE_STOP_BIT) | (0x07 & IUU_PARITY_EVEN);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200763
764 status = bulk_immediate(port, buf, 4);
765 if (status != IUU_OPERATION_OK) {
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700766 dev_dbg(&port->dev, "%s - uart_on error\n", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200767 goto uart_enable_failed;
768 }
769 /* iuu_reset() the card after iuu_uart_on() */
770 status = iuu_uart_flush(port);
771 if (status != IUU_OPERATION_OK)
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700772 dev_dbg(&port->dev, "%s - uart_flush error\n", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200773uart_enable_failed:
774 kfree(buf);
775 return status;
776}
777
778/* Diables the IUU UART (a.k.a. the Phoenix voiderface) */
779static int iuu_uart_off(struct usb_serial_port *port)
780{
781 int status;
782 u8 *buf;
783 buf = kmalloc(1, GFP_KERNEL);
784 if (!buf)
785 return -ENOMEM;
786 buf[0] = IUU_UART_DISABLE;
787
788 status = bulk_immediate(port, buf, 1);
789 if (status != IUU_OPERATION_OK)
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700790 dev_dbg(&port->dev, "%s - uart_off error\n", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200791
792 kfree(buf);
793 return status;
794}
795
James Courtier-Dutton89b54392010-05-21 11:53:25 +0100796static int iuu_uart_baud(struct usb_serial_port *port, u32 baud_base,
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200797 u32 *actual, u8 parity)
798{
799 int status;
James Courtier-Dutton89b54392010-05-21 11:53:25 +0100800 u32 baud;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200801 u8 *dataout;
802 u8 DataCount = 0;
803 u8 T1Frekvens = 0;
804 u8 T1reload = 0;
805 unsigned int T1FrekvensHZ = 0;
806
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700807 dev_dbg(&port->dev, "%s - enter baud_base=%d\n", __func__, baud_base);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200808 dataout = kmalloc(sizeof(u8) * 5, GFP_KERNEL);
809
810 if (!dataout)
811 return -ENOMEM;
James Courtier-Dutton89b54392010-05-21 11:53:25 +0100812 /*baud = (((priv->clk / 35) * baud_base) / 100000); */
813 baud = baud_base;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200814
815 if (baud < 1200 || baud > 230400) {
816 kfree(dataout);
817 return IUU_INVALID_PARAMETER;
818 }
819 if (baud > 977) {
820 T1Frekvens = 3;
821 T1FrekvensHZ = 500000;
822 }
823
824 if (baud > 3906) {
825 T1Frekvens = 2;
826 T1FrekvensHZ = 2000000;
827 }
828
829 if (baud > 11718) {
830 T1Frekvens = 1;
831 T1FrekvensHZ = 6000000;
832 }
833
834 if (baud > 46875) {
835 T1Frekvens = 0;
836 T1FrekvensHZ = 24000000;
837 }
838
839 T1reload = 256 - (u8) (T1FrekvensHZ / (baud * 2));
840
841 /* magic number here: ENTER_FIRMWARE_UPDATE; */
842 dataout[DataCount++] = IUU_UART_ESC;
843 /* magic number here: CHANGE_BAUD; */
844 dataout[DataCount++] = IUU_UART_CHANGE;
845 dataout[DataCount++] = T1Frekvens;
846 dataout[DataCount++] = T1reload;
847
848 *actual = (T1FrekvensHZ / (256 - T1reload)) / 2;
849
850 switch (parity & 0x0F) {
851 case IUU_PARITY_NONE:
852 dataout[DataCount++] = 0x00;
853 break;
854 case IUU_PARITY_EVEN:
855 dataout[DataCount++] = 0x01;
856 break;
857 case IUU_PARITY_ODD:
858 dataout[DataCount++] = 0x02;
859 break;
860 case IUU_PARITY_MARK:
861 dataout[DataCount++] = 0x03;
862 break;
863 case IUU_PARITY_SPACE:
864 dataout[DataCount++] = 0x04;
865 break;
866 default:
867 kfree(dataout);
868 return IUU_INVALID_PARAMETER;
869 break;
870 }
871
872 switch (parity & 0xF0) {
873 case IUU_ONE_STOP_BIT:
874 dataout[DataCount - 1] |= IUU_ONE_STOP_BIT;
875 break;
876
877 case IUU_TWO_STOP_BITS:
878 dataout[DataCount - 1] |= IUU_TWO_STOP_BITS;
879 break;
880 default:
881 kfree(dataout);
882 return IUU_INVALID_PARAMETER;
883 break;
884 }
885
886 status = bulk_immediate(port, dataout, DataCount);
887 if (status != IUU_OPERATION_OK)
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700888 dev_dbg(&port->dev, "%s - uart_off error\n", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200889 kfree(dataout);
890 return status;
891}
892
Olivier Bornet96dab772009-06-11 12:54:20 +0100893static void iuu_set_termios(struct tty_struct *tty,
894 struct usb_serial_port *port, struct ktermios *old_termios)
895{
896 const u32 supported_mask = CMSPAR|PARENB|PARODD;
James Courtier-Dutton89b54392010-05-21 11:53:25 +0100897 struct iuu_private *priv = usb_get_serial_port_data(port);
Alan Coxadc8d742012-07-14 15:31:47 +0100898 unsigned int cflag = tty->termios.c_cflag;
Olivier Bornet96dab772009-06-11 12:54:20 +0100899 int status;
900 u32 actual;
901 u32 parity;
902 int csize = CS7;
James Courtier-Dutton89b54392010-05-21 11:53:25 +0100903 int baud;
Olivier Bornet96dab772009-06-11 12:54:20 +0100904 u32 newval = cflag & supported_mask;
905
James Courtier-Dutton89b54392010-05-21 11:53:25 +0100906 /* Just use the ospeed. ispeed should be the same. */
Alan Coxadc8d742012-07-14 15:31:47 +0100907 baud = tty->termios.c_ospeed;
James Courtier-Dutton89b54392010-05-21 11:53:25 +0100908
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700909 dev_dbg(&port->dev, "%s - enter c_ospeed or baud=%d\n", __func__, baud);
James Courtier-Dutton89b54392010-05-21 11:53:25 +0100910
Olivier Bornet96dab772009-06-11 12:54:20 +0100911 /* compute the parity parameter */
912 parity = 0;
913 if (cflag & CMSPAR) { /* Using mark space */
914 if (cflag & PARODD)
915 parity |= IUU_PARITY_SPACE;
916 else
917 parity |= IUU_PARITY_MARK;
918 } else if (!(cflag & PARENB)) {
919 parity |= IUU_PARITY_NONE;
920 csize = CS8;
921 } else if (cflag & PARODD)
922 parity |= IUU_PARITY_ODD;
923 else
924 parity |= IUU_PARITY_EVEN;
925
926 parity |= (cflag & CSTOPB ? IUU_TWO_STOP_BITS : IUU_ONE_STOP_BIT);
927
928 /* set it */
929 status = iuu_uart_baud(port,
James Courtier-Dutton89b54392010-05-21 11:53:25 +0100930 baud * priv->boost / 100,
Olivier Bornet96dab772009-06-11 12:54:20 +0100931 &actual, parity);
932
933 /* set the termios value to the real one, so the user now what has
934 * changed. We support few fields so its easies to copy the old hw
935 * settings back over and then adjust them
936 */
James Courtier-Dutton89b54392010-05-21 11:53:25 +0100937 if (old_termios)
Alan Coxadc8d742012-07-14 15:31:47 +0100938 tty_termios_copy_hw(&tty->termios, old_termios);
Olivier Bornet96dab772009-06-11 12:54:20 +0100939 if (status != 0) /* Set failed - return old bits */
940 return;
941 /* Re-encode speed, parity and csize */
942 tty_encode_baud_rate(tty, baud, baud);
Alan Coxadc8d742012-07-14 15:31:47 +0100943 tty->termios.c_cflag &= ~(supported_mask|CSIZE);
944 tty->termios.c_cflag |= newval | csize;
Olivier Bornet96dab772009-06-11 12:54:20 +0100945}
946
Alan Cox335f8512009-06-11 12:26:29 +0100947static void iuu_close(struct usb_serial_port *port)
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200948{
949 /* iuu_led (port,255,0,0,0); */
950 struct usb_serial *serial;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200951
952 serial = port->serial;
953 if (!serial)
954 return;
955
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200956 iuu_uart_off(port);
957 if (serial->dev) {
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200958 /* free writebuf */
959 /* shutdown our urbs */
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700960 dev_dbg(&port->dev, "%s - shutting down urbs\n", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200961 usb_kill_urb(port->write_urb);
962 usb_kill_urb(port->read_urb);
963 usb_kill_urb(port->interrupt_in_urb);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200964 iuu_led(port, 0, 0, 0xF000, 0xFF);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200965 }
966}
967
Alan Coxfe1ae7f2009-09-19 13:13:33 -0700968static void iuu_init_termios(struct tty_struct *tty)
969{
Alan Coxadc8d742012-07-14 15:31:47 +0100970 tty->termios = tty_std_termios;
971 tty->termios.c_cflag = CLOCAL | CREAD | CS8 | B9600
Alan Coxfe1ae7f2009-09-19 13:13:33 -0700972 | TIOCM_CTS | CSTOPB | PARENB;
Alan Coxadc8d742012-07-14 15:31:47 +0100973 tty->termios.c_ispeed = 9600;
974 tty->termios.c_ospeed = 9600;
975 tty->termios.c_lflag = 0;
976 tty->termios.c_oflag = 0;
977 tty->termios.c_iflag = 0;
Alan Coxfe1ae7f2009-09-19 13:13:33 -0700978}
979
Alan Coxa509a7e2009-09-19 13:13:26 -0700980static int iuu_open(struct tty_struct *tty, struct usb_serial_port *port)
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200981{
982 struct usb_serial *serial = port->serial;
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700983 struct device *dev = &port->dev;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200984 u8 *buf;
985 int result;
James Courtier-Dutton89b54392010-05-21 11:53:25 +0100986 int baud;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200987 u32 actual;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200988 struct iuu_private *priv = usb_get_serial_port_data(port);
989
Alan Coxadc8d742012-07-14 15:31:47 +0100990 baud = tty->termios.c_ospeed;
991 tty->termios.c_ispeed = baud;
James Courtier-Dutton89b54392010-05-21 11:53:25 +0100992 /* Re-encode speed */
993 tty_encode_baud_rate(tty, baud, baud);
994
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700995 dev_dbg(dev, "%s - baud %d\n", __func__, baud);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200996 usb_clear_halt(serial->dev, port->write_urb->pipe);
997 usb_clear_halt(serial->dev, port->read_urb->pipe);
998
999 buf = kmalloc(10, GFP_KERNEL);
1000 if (buf == NULL)
1001 return -ENOMEM;
1002
Alan Coxfe1ae7f2009-09-19 13:13:33 -07001003 priv->poll = 0;
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001004
1005 /* initialize writebuf */
1006#define FISH(a, b, c, d) do { \
1007 result = usb_control_msg(port->serial->dev, \
1008 usb_rcvctrlpipe(port->serial->dev, 0), \
1009 b, a, c, d, buf, 1, 1000); \
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -07001010 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 +02001011 buf[0]); } while (0);
1012
1013#define SOUP(a, b, c, d) do { \
1014 result = usb_control_msg(port->serial->dev, \
1015 usb_sndctrlpipe(port->serial->dev, 0), \
1016 b, a, c, d, NULL, 0, 1000); \
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -07001017 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 +02001018
1019 /* This is not UART related but IUU USB driver related or something */
1020 /* like that. Basically no IUU will accept any commands from the USB */
1021 /* host unless it has received the following message */
1022 /* sprintf(buf ,"%c%c%c%c",0x03,0x02,0x02,0x0); */
1023
1024 SOUP(0x03, 0x02, 0x02, 0x0);
1025 kfree(buf);
1026 iuu_led(port, 0xF000, 0xF000, 0, 0xFF);
1027 iuu_uart_on(port);
1028 if (boost < 100)
1029 boost = 100;
James Courtier-Dutton89b54392010-05-21 11:53:25 +01001030 priv->boost = boost;
1031 priv->baud = baud;
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001032 switch (clockmode) {
1033 case 2: /* 3.680 Mhz */
James Courtier-Dutton89b54392010-05-21 11:53:25 +01001034 priv->clk = IUU_CLK_3680000;
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001035 iuu_clk(port, IUU_CLK_3680000 * boost / 100);
1036 result =
James Courtier-Dutton89b54392010-05-21 11:53:25 +01001037 iuu_uart_baud(port, baud * boost / 100, &actual,
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001038 IUU_PARITY_EVEN);
1039 break;
1040 case 3: /* 6.00 Mhz */
1041 iuu_clk(port, IUU_CLK_6000000 * boost / 100);
James Courtier-Dutton89b54392010-05-21 11:53:25 +01001042 priv->clk = IUU_CLK_6000000;
1043 /* Ratio of 6000000 to 3500000 for baud 9600 */
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001044 result =
1045 iuu_uart_baud(port, 16457 * boost / 100, &actual,
1046 IUU_PARITY_EVEN);
1047 break;
1048 default: /* 3.579 Mhz */
1049 iuu_clk(port, IUU_CLK_3579000 * boost / 100);
James Courtier-Dutton89b54392010-05-21 11:53:25 +01001050 priv->clk = IUU_CLK_3579000;
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001051 result =
James Courtier-Dutton89b54392010-05-21 11:53:25 +01001052 iuu_uart_baud(port, baud * boost / 100, &actual,
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001053 IUU_PARITY_EVEN);
1054 }
1055
1056 /* set the cardin cardout signals */
1057 switch (cdmode) {
1058 case 0:
1059 iuu_cardin = 0;
1060 iuu_cardout = 0;
1061 break;
1062 case 1:
1063 iuu_cardin = TIOCM_CD;
1064 iuu_cardout = 0;
1065 break;
1066 case 2:
1067 iuu_cardin = 0;
1068 iuu_cardout = TIOCM_CD;
1069 break;
1070 case 3:
1071 iuu_cardin = TIOCM_DSR;
1072 iuu_cardout = 0;
1073 break;
1074 case 4:
1075 iuu_cardin = 0;
1076 iuu_cardout = TIOCM_DSR;
1077 break;
1078 case 5:
1079 iuu_cardin = TIOCM_CTS;
1080 iuu_cardout = 0;
1081 break;
1082 case 6:
1083 iuu_cardin = 0;
1084 iuu_cardout = TIOCM_CTS;
1085 break;
1086 case 7:
1087 iuu_cardin = TIOCM_RNG;
1088 iuu_cardout = 0;
1089 break;
1090 case 8:
1091 iuu_cardin = 0;
1092 iuu_cardout = TIOCM_RNG;
1093 }
1094
1095 iuu_uart_flush(port);
1096
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -07001097 dev_dbg(dev, "%s - initialization done\n", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001098
1099 memset(port->write_urb->transfer_buffer, IUU_UART_RX, 1);
1100 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
1101 usb_sndbulkpipe(port->serial->dev,
1102 port->bulk_out_endpointAddress),
1103 port->write_urb->transfer_buffer, 1,
1104 read_rxcmd_callback, port);
1105 result = usb_submit_urb(port->write_urb, GFP_KERNEL);
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001106 if (result) {
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -07001107 dev_err(dev, "%s - failed submitting read urb, error %d\n", __func__, result);
Alan Cox335f8512009-06-11 12:26:29 +01001108 iuu_close(port);
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001109 } else {
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -07001110 dev_dbg(dev, "%s - rxcmd OK\n", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001111 }
Johan Hovoldb58a6462011-11-10 14:58:30 +01001112
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001113 return result;
1114}
1115
Olivier Bornet20eda942009-08-18 21:05:55 +02001116/* how to change VCC */
1117static int iuu_vcc_set(struct usb_serial_port *port, unsigned int vcc)
1118{
1119 int status;
1120 u8 *buf;
1121
1122 buf = kmalloc(5, GFP_KERNEL);
1123 if (!buf)
1124 return -ENOMEM;
1125
Olivier Bornet20eda942009-08-18 21:05:55 +02001126 buf[0] = IUU_SET_VCC;
1127 buf[1] = vcc & 0xFF;
1128 buf[2] = (vcc >> 8) & 0xFF;
1129 buf[3] = (vcc >> 16) & 0xFF;
1130 buf[4] = (vcc >> 24) & 0xFF;
1131
1132 status = bulk_immediate(port, buf, 5);
1133 kfree(buf);
1134
1135 if (status != IUU_OPERATION_OK)
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -07001136 dev_dbg(&port->dev, "%s - vcc error status = %2x\n", __func__, status);
Olivier Bornet20eda942009-08-18 21:05:55 +02001137 else
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -07001138 dev_dbg(&port->dev, "%s - vcc OK !\n", __func__);
Olivier Bornet20eda942009-08-18 21:05:55 +02001139
1140 return status;
1141}
1142
1143/*
1144 * Sysfs Attributes
1145 */
1146
1147static ssize_t show_vcc_mode(struct device *dev,
1148 struct device_attribute *attr, char *buf)
1149{
1150 struct usb_serial_port *port = to_usb_serial_port(dev);
1151 struct iuu_private *priv = usb_get_serial_port_data(port);
1152
1153 return sprintf(buf, "%d\n", priv->vcc);
1154}
1155
1156static ssize_t store_vcc_mode(struct device *dev,
1157 struct device_attribute *attr, const char *buf, size_t count)
1158{
1159 struct usb_serial_port *port = to_usb_serial_port(dev);
1160 struct iuu_private *priv = usb_get_serial_port_data(port);
1161 unsigned long v;
1162
Jingoo Han487c1512012-10-29 18:37:31 +09001163 if (kstrtoul(buf, 10, &v)) {
Olivier Bornet20eda942009-08-18 21:05:55 +02001164 dev_err(dev, "%s - vcc_mode: %s is not a unsigned long\n",
1165 __func__, buf);
1166 goto fail_store_vcc_mode;
1167 }
1168
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -07001169 dev_dbg(dev, "%s: setting vcc_mode = %ld", __func__, v);
Olivier Bornet20eda942009-08-18 21:05:55 +02001170
1171 if ((v != 3) && (v != 5)) {
1172 dev_err(dev, "%s - vcc_mode %ld is invalid\n", __func__, v);
1173 } else {
1174 iuu_vcc_set(port, v);
1175 priv->vcc = v;
1176 }
1177fail_store_vcc_mode:
1178 return count;
1179}
1180
1181static DEVICE_ATTR(vcc_mode, S_IRUSR | S_IWUSR, show_vcc_mode,
1182 store_vcc_mode);
1183
1184static int iuu_create_sysfs_attrs(struct usb_serial_port *port)
1185{
Olivier Bornet20eda942009-08-18 21:05:55 +02001186 return device_create_file(&port->dev, &dev_attr_vcc_mode);
1187}
1188
1189static int iuu_remove_sysfs_attrs(struct usb_serial_port *port)
1190{
Olivier Bornet20eda942009-08-18 21:05:55 +02001191 device_remove_file(&port->dev, &dev_attr_vcc_mode);
1192 return 0;
1193}
1194
1195/*
1196 * End Sysfs Attributes
1197 */
1198
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001199static struct usb_serial_driver iuu_device = {
1200 .driver = {
1201 .owner = THIS_MODULE,
1202 .name = "iuu_phoenix",
1203 },
1204 .id_table = id_table,
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001205 .num_ports = 1,
Johan Hovoldbbcb2b92010-03-17 23:00:37 +01001206 .bulk_in_size = 512,
1207 .bulk_out_size = 512,
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001208 .open = iuu_open,
1209 .close = iuu_close,
1210 .write = iuu_uart_write,
1211 .read_bulk_callback = iuu_uart_read_callback,
1212 .tiocmget = iuu_tiocmget,
1213 .tiocmset = iuu_tiocmset,
Olivier Bornet96dab772009-06-11 12:54:20 +01001214 .set_termios = iuu_set_termios,
Alan Coxfe1ae7f2009-09-19 13:13:33 -07001215 .init_termios = iuu_init_termios,
Johan Hovold53636552012-10-17 13:34:59 +02001216 .port_probe = iuu_port_probe,
1217 .port_remove = iuu_port_remove,
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001218};
1219
Alan Stern7dbe2462012-02-23 14:56:57 -05001220static struct usb_serial_driver * const serial_drivers[] = {
1221 &iuu_device, NULL
1222};
1223
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -07001224module_usb_serial_driver(serial_drivers, id_table);
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001225
1226MODULE_AUTHOR("Alain Degreffe eczema@ecze.com");
1227
1228MODULE_DESCRIPTION(DRIVER_DESC);
1229MODULE_LICENSE("GPL");
1230
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001231module_param(xmas, bool, S_IRUGO | S_IWUSR);
Olivier Bornet8844a322009-08-18 21:05:54 +02001232MODULE_PARM_DESC(xmas, "Xmas colors enabled or not");
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001233
1234module_param(boost, int, S_IRUGO | S_IWUSR);
Olivier Bornet8844a322009-08-18 21:05:54 +02001235MODULE_PARM_DESC(boost, "Card overclock boost (in percent 100-500)");
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001236
1237module_param(clockmode, int, S_IRUGO | S_IWUSR);
Olivier Bornet8844a322009-08-18 21:05:54 +02001238MODULE_PARM_DESC(clockmode, "Card clock mode (1=3.579 MHz, 2=3.680 MHz, "
1239 "3=6 Mhz)");
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001240
1241module_param(cdmode, int, S_IRUGO | S_IWUSR);
Olivier Bornet8844a322009-08-18 21:05:54 +02001242MODULE_PARM_DESC(cdmode, "Card detect mode (0=none, 1=CD, 2=!CD, 3=DSR, "
1243 "4=!DSR, 5=CTS, 6=!CTS, 7=RING, 8=!RING)");
Olivier Bornete55c6d02009-08-18 21:05:57 +02001244
1245module_param(vcc_default, int, S_IRUGO | S_IWUSR);
1246MODULE_PARM_DESC(vcc_default, "Set default VCC (either 3 for 3.3V or 5 "
1247 "for 5V). Default to 5.");