blob: 01da3ea36e89d5363a8890339eac6165dec0e14c [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/*
36 * Version Information
37 */
James Courtier-Dutton89b54392010-05-21 11:53:25 +010038#define DRIVER_VERSION "v0.12"
Alain Degreffe60a8fc02007-10-26 13:51:49 +020039#define DRIVER_DESC "Infinity USB Unlimited Phoenix driver"
40
Németh Márton7d40d7e2010-01-10 15:34:24 +010041static const struct usb_device_id id_table[] = {
Alain Degreffe60a8fc02007-10-26 13:51:49 +020042 {USB_DEVICE(IUU_USB_VENDOR_ID, IUU_USB_PRODUCT_ID)},
43 {} /* Terminating entry */
44};
45MODULE_DEVICE_TABLE(usb, id_table);
46
Alain Degreffe60a8fc02007-10-26 13:51:49 +020047/* turbo parameter */
48static int boost = 100;
49static int clockmode = 1;
50static int cdmode = 1;
51static int iuu_cardin;
52static int iuu_cardout;
Rusty Russell90ab5ee2012-01-13 09:32:20 +103053static bool xmas;
Olivier Bornete55c6d02009-08-18 21:05:57 +020054static int vcc_default = 5;
Alain Degreffe60a8fc02007-10-26 13:51:49 +020055
56static void read_rxcmd_callback(struct urb *urb);
57
58struct iuu_private {
59 spinlock_t lock; /* store irq state */
60 wait_queue_head_t delta_msr_wait;
Alain Degreffe60a8fc02007-10-26 13:51:49 +020061 u8 line_status;
Alain Degreffe60a8fc02007-10-26 13:51:49 +020062 int tiostatus; /* store IUART SIGNAL for tiocmget call */
63 u8 reset; /* if 1 reset is needed */
64 int poll; /* number of poll */
65 u8 *writebuf; /* buffer for writing to device */
66 int writelen; /* num of byte to write to device */
67 u8 *buf; /* used for initialize speed */
Alain Degreffe60a8fc02007-10-26 13:51:49 +020068 u8 len;
Olivier Bornet20eda942009-08-18 21:05:55 +020069 int vcc; /* vcc (either 3 or 5 V) */
James Courtier-Dutton89b54392010-05-21 11:53:25 +010070 u32 baud;
71 u32 boost;
72 u32 clk;
Alain Degreffe60a8fc02007-10-26 13:51:49 +020073};
74
75
76static void iuu_free_buf(struct iuu_private *priv)
77{
78 kfree(priv->buf);
Alain Degreffe60a8fc02007-10-26 13:51:49 +020079 kfree(priv->writebuf);
80}
81
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -070082static int iuu_alloc_buf(struct usb_serial *serial, struct iuu_private *priv)
Alain Degreffe60a8fc02007-10-26 13:51:49 +020083{
84 priv->buf = kzalloc(256, GFP_KERNEL);
Alain Degreffe60a8fc02007-10-26 13:51:49 +020085 priv->writebuf = kzalloc(256, GFP_KERNEL);
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -070086 if (!priv->buf || !priv->writebuf) {
Alain Degreffe60a8fc02007-10-26 13:51:49 +020087 iuu_free_buf(priv);
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -070088 dev_dbg(&serial->dev->dev, "%s problem allocation buffer\n", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +020089 return -ENOMEM;
90 }
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -070091 dev_dbg(&serial->dev->dev, "%s - Privates buffers allocation success\n", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +020092 return 0;
93}
94
95static int iuu_startup(struct usb_serial *serial)
96{
97 struct iuu_private *priv;
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -070098
Alain Degreffe60a8fc02007-10-26 13:51:49 +020099 priv = kzalloc(sizeof(struct iuu_private), GFP_KERNEL);
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700100 dev_dbg(&serial->dev->dev, "%s- priv allocation success\n", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200101 if (!priv)
102 return -ENOMEM;
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700103 if (iuu_alloc_buf(serial, priv)) {
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200104 kfree(priv);
105 return -ENOMEM;
106 }
Olivier Bornete55c6d02009-08-18 21:05:57 +0200107 priv->vcc = vcc_default;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200108 spin_lock_init(&priv->lock);
109 init_waitqueue_head(&priv->delta_msr_wait);
110 usb_set_serial_port_data(serial->port[0], priv);
111 return 0;
112}
113
Alan Sternf9c99bb2009-06-02 11:53:55 -0400114/* Release function */
115static void iuu_release(struct usb_serial *serial)
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200116{
117 struct usb_serial_port *port = serial->port[0];
118 struct iuu_private *priv = usb_get_serial_port_data(port);
119 if (!port)
120 return;
121
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200122 if (priv) {
123 iuu_free_buf(priv);
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700124 dev_dbg(&port->dev, "%s - I will free all\n", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200125 usb_set_serial_port_data(port, NULL);
126
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700127 dev_dbg(&port->dev, "%s - priv is not anymore in port structure\n", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200128 kfree(priv);
129
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700130 dev_dbg(&port->dev, "%s priv is now kfree\n", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200131 }
132}
133
Alan Cox20b9d172011-02-14 16:26:50 +0000134static int iuu_tiocmset(struct tty_struct *tty,
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200135 unsigned int set, unsigned int clear)
136{
Alan Cox95da3102008-07-22 11:09:07 +0100137 struct usb_serial_port *port = tty->driver_data;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200138 struct iuu_private *priv = usb_get_serial_port_data(port);
Alan Cox7b1fc8b2008-02-20 21:39:25 +0000139 unsigned long flags;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200140
Alan Cox7b1fc8b2008-02-20 21:39:25 +0000141 /* FIXME: locking on tiomstatus */
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700142 dev_dbg(&port->dev, "%s msg : SET = 0x%04x, CLEAR = 0x%04x\n",
143 __func__, set, clear);
Alan Cox7b1fc8b2008-02-20 21:39:25 +0000144
145 spin_lock_irqsave(&priv->lock, flags);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200146
James Courtier-Dutton89b54392010-05-21 11:53:25 +0100147 if ((set & TIOCM_RTS) && !(priv->tiostatus == TIOCM_RTS)) {
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700148 dev_dbg(&port->dev, "%s TIOCMSET RESET called !!!\n", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200149 priv->reset = 1;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200150 }
James Courtier-Dutton89b54392010-05-21 11:53:25 +0100151 if (set & TIOCM_RTS)
152 priv->tiostatus = TIOCM_RTS;
153
Alan Cox7b1fc8b2008-02-20 21:39:25 +0000154 spin_unlock_irqrestore(&priv->lock, flags);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200155 return 0;
156}
157
158/* This is used to provide a carrier detect mechanism
159 * When a card is present, the response is 0x00
160 * When no card , the reader respond with TIOCM_CD
161 * This is known as CD autodetect mechanism
162 */
Alan Cox60b33c12011-02-14 16:26:14 +0000163static int iuu_tiocmget(struct tty_struct *tty)
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200164{
Alan Cox95da3102008-07-22 11:09:07 +0100165 struct usb_serial_port *port = tty->driver_data;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200166 struct iuu_private *priv = usb_get_serial_port_data(port);
Alan Cox7b1fc8b2008-02-20 21:39:25 +0000167 unsigned long flags;
168 int rc;
169
170 spin_lock_irqsave(&priv->lock, flags);
171 rc = priv->tiostatus;
172 spin_unlock_irqrestore(&priv->lock, flags);
173
174 return rc;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200175}
176
177static void iuu_rxcmd(struct urb *urb)
178{
Ming Leicdc97792008-02-24 18:41:47 +0800179 struct usb_serial_port *port = urb->context;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200180 int result;
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800181 int status = urb->status;
182
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800183 if (status) {
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700184 dev_dbg(&port->dev, "%s - status = %d\n", __func__, status);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200185 /* error stop all */
186 return;
187 }
188
189
190 memset(port->write_urb->transfer_buffer, IUU_UART_RX, 1);
191 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
192 usb_sndbulkpipe(port->serial->dev,
193 port->bulk_out_endpointAddress),
194 port->write_urb->transfer_buffer, 1,
195 read_rxcmd_callback, port);
196 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
197}
198
199static int iuu_reset(struct usb_serial_port *port, u8 wt)
200{
201 struct iuu_private *priv = usb_get_serial_port_data(port);
202 int result;
203 char *buf_ptr = port->write_urb->transfer_buffer;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200204
205 /* Prepare the reset sequence */
206
207 *buf_ptr++ = IUU_RST_SET;
208 *buf_ptr++ = IUU_DELAY_MS;
209 *buf_ptr++ = wt;
210 *buf_ptr = IUU_RST_CLEAR;
211
212 /* send the sequence */
213
214 usb_fill_bulk_urb(port->write_urb,
215 port->serial->dev,
216 usb_sndbulkpipe(port->serial->dev,
217 port->bulk_out_endpointAddress),
218 port->write_urb->transfer_buffer, 4, iuu_rxcmd, port);
219 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
220 priv->reset = 0;
221 return result;
222}
223
224/* Status Function
225 * Return value is
226 * 0x00 = no card
227 * 0x01 = smartcard
228 * 0x02 = sim card
229 */
230static void iuu_update_status_callback(struct urb *urb)
231{
Ming Leicdc97792008-02-24 18:41:47 +0800232 struct usb_serial_port *port = urb->context;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200233 struct iuu_private *priv = usb_get_serial_port_data(port);
234 u8 *st;
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800235 int status = urb->status;
236
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800237 if (status) {
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700238 dev_dbg(&port->dev, "%s - status = %d\n", __func__, status);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200239 /* error stop all */
240 return;
241 }
242
243 st = urb->transfer_buffer;
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700244 dev_dbg(&port->dev, "%s - enter\n", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200245 if (urb->actual_length == 1) {
246 switch (st[0]) {
247 case 0x1:
248 priv->tiostatus = iuu_cardout;
249 break;
250 case 0x0:
251 priv->tiostatus = iuu_cardin;
252 break;
253 default:
254 priv->tiostatus = iuu_cardin;
255 }
256 }
257 iuu_rxcmd(urb);
258}
259
260static void iuu_status_callback(struct urb *urb)
261{
Ming Leicdc97792008-02-24 18:41:47 +0800262 struct usb_serial_port *port = urb->context;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200263 int result;
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800264 int status = urb->status;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200265
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700266 dev_dbg(&port->dev, "%s - status = %d\n", __func__, status);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200267 usb_fill_bulk_urb(port->read_urb, port->serial->dev,
268 usb_rcvbulkpipe(port->serial->dev,
269 port->bulk_in_endpointAddress),
270 port->read_urb->transfer_buffer, 256,
271 iuu_update_status_callback, port);
272 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
273}
274
275static int iuu_status(struct usb_serial_port *port)
276{
277 int result;
278
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200279 memset(port->write_urb->transfer_buffer, IUU_GET_STATE_REGISTER, 1);
280 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
281 usb_sndbulkpipe(port->serial->dev,
282 port->bulk_out_endpointAddress),
283 port->write_urb->transfer_buffer, 1,
284 iuu_status_callback, port);
285 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
286 return result;
287
288}
289
290static int bulk_immediate(struct usb_serial_port *port, u8 *buf, u8 count)
291{
292 int status;
293 struct usb_serial *serial = port->serial;
294 int actual = 0;
295
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200296 /* send the data out the bulk port */
297
298 status =
299 usb_bulk_msg(serial->dev,
300 usb_sndbulkpipe(serial->dev,
301 port->bulk_out_endpointAddress), buf,
302 count, &actual, HZ * 1);
303
Alan Cox9e8e2d22008-07-22 11:12:59 +0100304 if (status != IUU_OPERATION_OK)
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700305 dev_dbg(&port->dev, "%s - error = %2x\n", __func__, status);
Alan Cox9e8e2d22008-07-22 11:12:59 +0100306 else
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700307 dev_dbg(&port->dev, "%s - write OK !\n", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200308 return status;
309}
310
311static int read_immediate(struct usb_serial_port *port, u8 *buf, u8 count)
312{
313 int status;
314 struct usb_serial *serial = port->serial;
315 int actual = 0;
316
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200317 /* send the data out the bulk port */
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200318 status =
319 usb_bulk_msg(serial->dev,
320 usb_rcvbulkpipe(serial->dev,
321 port->bulk_in_endpointAddress), buf,
322 count, &actual, HZ * 1);
323
Alan Cox9e8e2d22008-07-22 11:12:59 +0100324 if (status != IUU_OPERATION_OK)
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700325 dev_dbg(&port->dev, "%s - error = %2x\n", __func__, status);
Alan Cox9e8e2d22008-07-22 11:12:59 +0100326 else
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700327 dev_dbg(&port->dev, "%s - read OK !\n", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200328 return status;
329}
330
331static int iuu_led(struct usb_serial_port *port, unsigned int R,
332 unsigned int G, unsigned int B, u8 f)
333{
334 int status;
335 u8 *buf;
336 buf = kmalloc(8, GFP_KERNEL);
337 if (!buf)
338 return -ENOMEM;
339
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200340 buf[0] = IUU_SET_LED;
341 buf[1] = R & 0xFF;
342 buf[2] = (R >> 8) & 0xFF;
343 buf[3] = G & 0xFF;
344 buf[4] = (G >> 8) & 0xFF;
345 buf[5] = B & 0xFF;
346 buf[6] = (B >> 8) & 0xFF;
347 buf[7] = f;
348 status = bulk_immediate(port, buf, 8);
349 kfree(buf);
350 if (status != IUU_OPERATION_OK)
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700351 dev_dbg(&port->dev, "%s - led error status = %2x\n", __func__, status);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200352 else
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700353 dev_dbg(&port->dev, "%s - led OK !\n", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200354 return IUU_OPERATION_OK;
355}
356
357static void iuu_rgbf_fill_buffer(u8 *buf, u8 r1, u8 r2, u8 g1, u8 g2, u8 b1,
358 u8 b2, u8 freq)
359{
360 *buf++ = IUU_SET_LED;
361 *buf++ = r1;
362 *buf++ = r2;
363 *buf++ = g1;
364 *buf++ = g2;
365 *buf++ = b1;
366 *buf++ = b2;
367 *buf = freq;
368}
369
370static void iuu_led_activity_on(struct urb *urb)
371{
Ming Leicdc97792008-02-24 18:41:47 +0800372 struct usb_serial_port *port = urb->context;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200373 int result;
374 char *buf_ptr = port->write_urb->transfer_buffer;
375 *buf_ptr++ = IUU_SET_LED;
376 if (xmas == 1) {
377 get_random_bytes(buf_ptr, 6);
378 *(buf_ptr+7) = 1;
379 } else {
380 iuu_rgbf_fill_buffer(buf_ptr, 255, 255, 0, 0, 0, 0, 255);
381 }
382
383 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
384 usb_sndbulkpipe(port->serial->dev,
385 port->bulk_out_endpointAddress),
386 port->write_urb->transfer_buffer, 8 ,
387 iuu_rxcmd, port);
388 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
389}
390
391static void iuu_led_activity_off(struct urb *urb)
392{
Ming Leicdc97792008-02-24 18:41:47 +0800393 struct usb_serial_port *port = urb->context;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200394 int result;
395 char *buf_ptr = port->write_urb->transfer_buffer;
396 if (xmas == 1) {
397 iuu_rxcmd(urb);
398 return;
399 } else {
400 *buf_ptr++ = IUU_SET_LED;
401 iuu_rgbf_fill_buffer(buf_ptr, 0, 0, 255, 255, 0, 0, 255);
402 }
403 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
404 usb_sndbulkpipe(port->serial->dev,
405 port->bulk_out_endpointAddress),
406 port->write_urb->transfer_buffer, 8 ,
407 iuu_rxcmd, port);
408 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
409}
410
411
412
413static int iuu_clk(struct usb_serial_port *port, int dwFrq)
414{
415 int status;
416 struct iuu_private *priv = usb_get_serial_port_data(port);
417 int Count = 0;
418 u8 FrqGenAdr = 0x69;
419 u8 DIV = 0; /* 8bit */
420 u8 XDRV = 0; /* 8bit */
421 u8 PUMP = 0; /* 3bit */
422 u8 PBmsb = 0; /* 2bit */
423 u8 PBlsb = 0; /* 8bit */
424 u8 PO = 0; /* 1bit */
425 u8 Q = 0; /* 7bit */
426 /* 24bit = 3bytes */
427 unsigned int P = 0;
428 unsigned int P2 = 0;
429 int frq = (int)dwFrq;
430
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200431 if (frq == 0) {
432 priv->buf[Count++] = IUU_UART_WRITE_I2C;
433 priv->buf[Count++] = FrqGenAdr << 1;
434 priv->buf[Count++] = 0x09;
435 priv->buf[Count++] = 0x00;
436
437 status = bulk_immediate(port, (u8 *) priv->buf, Count);
438 if (status != 0) {
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700439 dev_dbg(&port->dev, "%s - write error\n", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200440 return status;
441 }
442 } else if (frq == 3579000) {
443 DIV = 100;
444 P = 1193;
445 Q = 40;
446 XDRV = 0;
447 } else if (frq == 3680000) {
448 DIV = 105;
449 P = 161;
450 Q = 5;
451 XDRV = 0;
452 } else if (frq == 6000000) {
453 DIV = 66;
454 P = 66;
455 Q = 2;
456 XDRV = 0x28;
457 } else {
458 unsigned int result = 0;
459 unsigned int tmp = 0;
460 unsigned int check;
461 unsigned int check2;
462 char found = 0x00;
463 unsigned int lQ = 2;
464 unsigned int lP = 2055;
465 unsigned int lDiv = 4;
466
467 for (lQ = 2; lQ <= 47 && !found; lQ++)
468 for (lP = 2055; lP >= 8 && !found; lP--)
469 for (lDiv = 4; lDiv <= 127 && !found; lDiv++) {
470 tmp = (12000000 / lDiv) * (lP / lQ);
471 if (abs((int)(tmp - frq)) <
472 abs((int)(frq - result))) {
473 check2 = (12000000 / lQ);
474 if (check2 < 250000)
475 continue;
476 check = (12000000 / lQ) * lP;
477 if (check > 400000000)
478 continue;
479 if (check < 100000000)
480 continue;
481 if (lDiv < 4 || lDiv > 127)
482 continue;
483 result = tmp;
484 P = lP;
485 DIV = lDiv;
486 Q = lQ;
487 if (result == frq)
488 found = 0x01;
489 }
490 }
491 }
492 P2 = ((P - PO) / 2) - 4;
493 DIV = DIV;
494 PUMP = 0x04;
495 PBmsb = (P2 >> 8 & 0x03);
496 PBlsb = P2 & 0xFF;
497 PO = (P >> 10) & 0x01;
498 Q = Q - 2;
499
500 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
501 priv->buf[Count++] = FrqGenAdr << 1;
502 priv->buf[Count++] = 0x09;
503 priv->buf[Count++] = 0x20; /* Adr = 0x09 */
504 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
505 priv->buf[Count++] = FrqGenAdr << 1;
506 priv->buf[Count++] = 0x0C;
507 priv->buf[Count++] = DIV; /* Adr = 0x0C */
508 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
509 priv->buf[Count++] = FrqGenAdr << 1;
510 priv->buf[Count++] = 0x12;
511 priv->buf[Count++] = XDRV; /* Adr = 0x12 */
512 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
513 priv->buf[Count++] = FrqGenAdr << 1;
514 priv->buf[Count++] = 0x13;
515 priv->buf[Count++] = 0x6B; /* Adr = 0x13 */
516 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
517 priv->buf[Count++] = FrqGenAdr << 1;
518 priv->buf[Count++] = 0x40;
519 priv->buf[Count++] = (0xC0 | ((PUMP & 0x07) << 2)) |
520 (PBmsb & 0x03); /* Adr = 0x40 */
521 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
522 priv->buf[Count++] = FrqGenAdr << 1;
523 priv->buf[Count++] = 0x41;
524 priv->buf[Count++] = PBlsb; /* Adr = 0x41 */
525 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
526 priv->buf[Count++] = FrqGenAdr << 1;
527 priv->buf[Count++] = 0x42;
528 priv->buf[Count++] = Q | (((PO & 0x01) << 7)); /* Adr = 0x42 */
529 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
530 priv->buf[Count++] = FrqGenAdr << 1;
531 priv->buf[Count++] = 0x44;
532 priv->buf[Count++] = (char)0xFF; /* Adr = 0x44 */
533 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
534 priv->buf[Count++] = FrqGenAdr << 1;
535 priv->buf[Count++] = 0x45;
536 priv->buf[Count++] = (char)0xFE; /* Adr = 0x45 */
537 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
538 priv->buf[Count++] = FrqGenAdr << 1;
539 priv->buf[Count++] = 0x46;
540 priv->buf[Count++] = 0x7F; /* Adr = 0x46 */
541 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
542 priv->buf[Count++] = FrqGenAdr << 1;
543 priv->buf[Count++] = 0x47;
544 priv->buf[Count++] = (char)0x84; /* Adr = 0x47 */
545
546 status = bulk_immediate(port, (u8 *) priv->buf, Count);
547 if (status != IUU_OPERATION_OK)
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700548 dev_dbg(&port->dev, "%s - write error\n", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200549 return status;
550}
551
552static int iuu_uart_flush(struct usb_serial_port *port)
553{
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700554 struct device *dev = &port->dev;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200555 int i;
556 int status;
557 u8 rxcmd = IUU_UART_RX;
558 struct iuu_private *priv = usb_get_serial_port_data(port);
559
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200560 if (iuu_led(port, 0xF000, 0, 0, 0xFF) < 0)
561 return -EIO;
562
563 for (i = 0; i < 2; i++) {
564 status = bulk_immediate(port, &rxcmd, 1);
565 if (status != IUU_OPERATION_OK) {
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700566 dev_dbg(dev, "%s - uart_flush_write error\n", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200567 return status;
568 }
569
570 status = read_immediate(port, &priv->len, 1);
571 if (status != IUU_OPERATION_OK) {
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700572 dev_dbg(dev, "%s - uart_flush_read error\n", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200573 return status;
574 }
575
576 if (priv->len > 0) {
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700577 dev_dbg(dev, "%s - uart_flush datalen is : %i\n", __func__, priv->len);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200578 status = read_immediate(port, priv->buf, priv->len);
579 if (status != IUU_OPERATION_OK) {
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700580 dev_dbg(dev, "%s - uart_flush_read error\n", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200581 return status;
582 }
583 }
584 }
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700585 dev_dbg(dev, "%s - uart_flush_read OK!\n", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200586 iuu_led(port, 0, 0xF000, 0, 0xFF);
587 return status;
588}
589
590static void read_buf_callback(struct urb *urb)
591{
Ming Leicdc97792008-02-24 18:41:47 +0800592 struct usb_serial_port *port = urb->context;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200593 unsigned char *data = urb->transfer_buffer;
594 struct tty_struct *tty;
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800595 int status = urb->status;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200596
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800597 if (status) {
598 if (status == -EPROTO) {
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200599 /* reschedule needed */
600 }
601 return;
602 }
603
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700604 dev_dbg(&port->dev, "%s - %i chars to write\n", __func__, urb->actual_length);
Alan Cox4a90f092008-10-13 10:39:46 +0100605 tty = tty_port_tty_get(&port->port);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200606 if (data == NULL)
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700607 dev_dbg(&port->dev, "%s - data is NULL !!!\n", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200608 if (tty && urb->actual_length && data) {
609 tty_insert_flip_string(tty, data, urb->actual_length);
610 tty_flip_buffer_push(tty);
611 }
Alan Cox4a90f092008-10-13 10:39:46 +0100612 tty_kref_put(tty);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200613 iuu_led_activity_on(urb);
614}
615
616static int iuu_bulk_write(struct usb_serial_port *port)
617{
618 struct iuu_private *priv = usb_get_serial_port_data(port);
Steven Rostedt85943032008-05-06 20:42:31 -0700619 unsigned long flags;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200620 int result;
Olivier Bornet5fcf62b2009-06-11 12:52:26 +0100621 int buf_len;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200622 char *buf_ptr = port->write_urb->transfer_buffer;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200623
Olivier Bornet5fcf62b2009-06-11 12:52:26 +0100624 spin_lock_irqsave(&priv->lock, flags);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200625 *buf_ptr++ = IUU_UART_ESC;
626 *buf_ptr++ = IUU_UART_TX;
627 *buf_ptr++ = priv->writelen;
628
Olivier Bornet5fcf62b2009-06-11 12:52:26 +0100629 memcpy(buf_ptr, priv->writebuf, priv->writelen);
630 buf_len = priv->writelen;
631 priv->writelen = 0;
632 spin_unlock_irqrestore(&priv->lock, flags);
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700633 dev_dbg(&port->dev, "%s - writing %i chars : %*ph\n", __func__,
634 buf_len, buf_len, buf_ptr);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200635 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
636 usb_sndbulkpipe(port->serial->dev,
637 port->bulk_out_endpointAddress),
Olivier Bornet5fcf62b2009-06-11 12:52:26 +0100638 port->write_urb->transfer_buffer, buf_len + 3,
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200639 iuu_rxcmd, port);
640 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200641 usb_serial_port_softint(port);
642 return result;
643}
644
645static int iuu_read_buf(struct usb_serial_port *port, int len)
646{
647 int result;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200648
649 usb_fill_bulk_urb(port->read_urb, port->serial->dev,
650 usb_rcvbulkpipe(port->serial->dev,
651 port->bulk_in_endpointAddress),
652 port->read_urb->transfer_buffer, len,
653 read_buf_callback, port);
654 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
655 return result;
656}
657
658static void iuu_uart_read_callback(struct urb *urb)
659{
Ming Leicdc97792008-02-24 18:41:47 +0800660 struct usb_serial_port *port = urb->context;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200661 struct iuu_private *priv = usb_get_serial_port_data(port);
Steven Rostedt85943032008-05-06 20:42:31 -0700662 unsigned long flags;
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800663 int status = urb->status;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200664 int error = 0;
665 int len = 0;
666 unsigned char *data = urb->transfer_buffer;
667 priv->poll++;
668
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800669 if (status) {
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700670 dev_dbg(&port->dev, "%s - status = %d\n", __func__, status);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200671 /* error stop all */
672 return;
673 }
674 if (data == NULL)
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700675 dev_dbg(&port->dev, "%s - data is NULL !!!\n", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200676
677 if (urb->actual_length == 1 && data != NULL)
678 len = (int) data[0];
679
680 if (urb->actual_length > 1) {
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700681 dev_dbg(&port->dev, "%s - urb->actual_length = %i\n", __func__,
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200682 urb->actual_length);
683 error = 1;
684 return;
685 }
686 /* if len > 0 call readbuf */
687
688 if (len > 0 && error == 0) {
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700689 dev_dbg(&port->dev, "%s - call read buf - len to read is %i\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800690 __func__, len);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200691 status = iuu_read_buf(port, len);
692 return;
693 }
694 /* need to update status ? */
695 if (priv->poll > 99) {
696 status = iuu_status(port);
697 priv->poll = 0;
698 return;
699 }
700
701 /* reset waiting ? */
702
703 if (priv->reset == 1) {
704 status = iuu_reset(port, 0xC);
705 return;
706 }
707 /* Writebuf is waiting */
708 spin_lock_irqsave(&priv->lock, flags);
709 if (priv->writelen > 0) {
710 spin_unlock_irqrestore(&priv->lock, flags);
711 status = iuu_bulk_write(port);
712 return;
713 }
714 spin_unlock_irqrestore(&priv->lock, flags);
715 /* if nothing to write call again rxcmd */
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700716 dev_dbg(&port->dev, "%s - rxcmd recall\n", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200717 iuu_led_activity_off(urb);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200718}
719
Alan Cox95da3102008-07-22 11:09:07 +0100720static int iuu_uart_write(struct tty_struct *tty, struct usb_serial_port *port,
721 const u8 *buf, int count)
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200722{
723 struct iuu_private *priv = usb_get_serial_port_data(port);
Steven Rostedt85943032008-05-06 20:42:31 -0700724 unsigned long flags;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200725
726 if (count > 256)
727 return -ENOMEM;
728
729 spin_lock_irqsave(&priv->lock, flags);
Olivier Bornet5fcf62b2009-06-11 12:52:26 +0100730
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200731 /* fill the buffer */
Olivier Bornet5fcf62b2009-06-11 12:52:26 +0100732 memcpy(priv->writebuf + priv->writelen, buf, count);
733 priv->writelen += count;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200734 spin_unlock_irqrestore(&priv->lock, flags);
735
Alan Cox9e8e2d22008-07-22 11:12:59 +0100736 return count;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200737}
738
739static void read_rxcmd_callback(struct urb *urb)
740{
Ming Leicdc97792008-02-24 18:41:47 +0800741 struct usb_serial_port *port = urb->context;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200742 int result;
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800743 int status = urb->status;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200744
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800745 if (status) {
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200746 /* error stop all */
747 return;
748 }
749
750 usb_fill_bulk_urb(port->read_urb, port->serial->dev,
751 usb_rcvbulkpipe(port->serial->dev,
752 port->bulk_in_endpointAddress),
753 port->read_urb->transfer_buffer, 256,
754 iuu_uart_read_callback, port);
755 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700756 dev_dbg(&port->dev, "%s - submit result = %d\n", __func__, result);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200757}
758
759static int iuu_uart_on(struct usb_serial_port *port)
760{
761 int status;
762 u8 *buf;
763
764 buf = kmalloc(sizeof(u8) * 4, GFP_KERNEL);
765
766 if (!buf)
767 return -ENOMEM;
768
769 buf[0] = IUU_UART_ENABLE;
770 buf[1] = (u8) ((IUU_BAUD_9600 >> 8) & 0x00FF);
771 buf[2] = (u8) (0x00FF & IUU_BAUD_9600);
Olivier Bornetcc3447d2009-06-11 12:53:24 +0100772 buf[3] = (u8) (0x0F0 & IUU_ONE_STOP_BIT) | (0x07 & IUU_PARITY_EVEN);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200773
774 status = bulk_immediate(port, buf, 4);
775 if (status != IUU_OPERATION_OK) {
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700776 dev_dbg(&port->dev, "%s - uart_on error\n", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200777 goto uart_enable_failed;
778 }
779 /* iuu_reset() the card after iuu_uart_on() */
780 status = iuu_uart_flush(port);
781 if (status != IUU_OPERATION_OK)
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700782 dev_dbg(&port->dev, "%s - uart_flush error\n", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200783uart_enable_failed:
784 kfree(buf);
785 return status;
786}
787
788/* Diables the IUU UART (a.k.a. the Phoenix voiderface) */
789static int iuu_uart_off(struct usb_serial_port *port)
790{
791 int status;
792 u8 *buf;
793 buf = kmalloc(1, GFP_KERNEL);
794 if (!buf)
795 return -ENOMEM;
796 buf[0] = IUU_UART_DISABLE;
797
798 status = bulk_immediate(port, buf, 1);
799 if (status != IUU_OPERATION_OK)
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700800 dev_dbg(&port->dev, "%s - uart_off error\n", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200801
802 kfree(buf);
803 return status;
804}
805
James Courtier-Dutton89b54392010-05-21 11:53:25 +0100806static int iuu_uart_baud(struct usb_serial_port *port, u32 baud_base,
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200807 u32 *actual, u8 parity)
808{
809 int status;
James Courtier-Dutton89b54392010-05-21 11:53:25 +0100810 u32 baud;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200811 u8 *dataout;
812 u8 DataCount = 0;
813 u8 T1Frekvens = 0;
814 u8 T1reload = 0;
815 unsigned int T1FrekvensHZ = 0;
816
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700817 dev_dbg(&port->dev, "%s - enter baud_base=%d\n", __func__, baud_base);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200818 dataout = kmalloc(sizeof(u8) * 5, GFP_KERNEL);
819
820 if (!dataout)
821 return -ENOMEM;
James Courtier-Dutton89b54392010-05-21 11:53:25 +0100822 /*baud = (((priv->clk / 35) * baud_base) / 100000); */
823 baud = baud_base;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200824
825 if (baud < 1200 || baud > 230400) {
826 kfree(dataout);
827 return IUU_INVALID_PARAMETER;
828 }
829 if (baud > 977) {
830 T1Frekvens = 3;
831 T1FrekvensHZ = 500000;
832 }
833
834 if (baud > 3906) {
835 T1Frekvens = 2;
836 T1FrekvensHZ = 2000000;
837 }
838
839 if (baud > 11718) {
840 T1Frekvens = 1;
841 T1FrekvensHZ = 6000000;
842 }
843
844 if (baud > 46875) {
845 T1Frekvens = 0;
846 T1FrekvensHZ = 24000000;
847 }
848
849 T1reload = 256 - (u8) (T1FrekvensHZ / (baud * 2));
850
851 /* magic number here: ENTER_FIRMWARE_UPDATE; */
852 dataout[DataCount++] = IUU_UART_ESC;
853 /* magic number here: CHANGE_BAUD; */
854 dataout[DataCount++] = IUU_UART_CHANGE;
855 dataout[DataCount++] = T1Frekvens;
856 dataout[DataCount++] = T1reload;
857
858 *actual = (T1FrekvensHZ / (256 - T1reload)) / 2;
859
860 switch (parity & 0x0F) {
861 case IUU_PARITY_NONE:
862 dataout[DataCount++] = 0x00;
863 break;
864 case IUU_PARITY_EVEN:
865 dataout[DataCount++] = 0x01;
866 break;
867 case IUU_PARITY_ODD:
868 dataout[DataCount++] = 0x02;
869 break;
870 case IUU_PARITY_MARK:
871 dataout[DataCount++] = 0x03;
872 break;
873 case IUU_PARITY_SPACE:
874 dataout[DataCount++] = 0x04;
875 break;
876 default:
877 kfree(dataout);
878 return IUU_INVALID_PARAMETER;
879 break;
880 }
881
882 switch (parity & 0xF0) {
883 case IUU_ONE_STOP_BIT:
884 dataout[DataCount - 1] |= IUU_ONE_STOP_BIT;
885 break;
886
887 case IUU_TWO_STOP_BITS:
888 dataout[DataCount - 1] |= IUU_TWO_STOP_BITS;
889 break;
890 default:
891 kfree(dataout);
892 return IUU_INVALID_PARAMETER;
893 break;
894 }
895
896 status = bulk_immediate(port, dataout, DataCount);
897 if (status != IUU_OPERATION_OK)
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700898 dev_dbg(&port->dev, "%s - uart_off error\n", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200899 kfree(dataout);
900 return status;
901}
902
Olivier Bornet96dab772009-06-11 12:54:20 +0100903static void iuu_set_termios(struct tty_struct *tty,
904 struct usb_serial_port *port, struct ktermios *old_termios)
905{
906 const u32 supported_mask = CMSPAR|PARENB|PARODD;
James Courtier-Dutton89b54392010-05-21 11:53:25 +0100907 struct iuu_private *priv = usb_get_serial_port_data(port);
Alan Coxadc8d742012-07-14 15:31:47 +0100908 unsigned int cflag = tty->termios.c_cflag;
Olivier Bornet96dab772009-06-11 12:54:20 +0100909 int status;
910 u32 actual;
911 u32 parity;
912 int csize = CS7;
James Courtier-Dutton89b54392010-05-21 11:53:25 +0100913 int baud;
Olivier Bornet96dab772009-06-11 12:54:20 +0100914 u32 newval = cflag & supported_mask;
915
James Courtier-Dutton89b54392010-05-21 11:53:25 +0100916 /* Just use the ospeed. ispeed should be the same. */
Alan Coxadc8d742012-07-14 15:31:47 +0100917 baud = tty->termios.c_ospeed;
James Courtier-Dutton89b54392010-05-21 11:53:25 +0100918
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700919 dev_dbg(&port->dev, "%s - enter c_ospeed or baud=%d\n", __func__, baud);
James Courtier-Dutton89b54392010-05-21 11:53:25 +0100920
Olivier Bornet96dab772009-06-11 12:54:20 +0100921 /* compute the parity parameter */
922 parity = 0;
923 if (cflag & CMSPAR) { /* Using mark space */
924 if (cflag & PARODD)
925 parity |= IUU_PARITY_SPACE;
926 else
927 parity |= IUU_PARITY_MARK;
928 } else if (!(cflag & PARENB)) {
929 parity |= IUU_PARITY_NONE;
930 csize = CS8;
931 } else if (cflag & PARODD)
932 parity |= IUU_PARITY_ODD;
933 else
934 parity |= IUU_PARITY_EVEN;
935
936 parity |= (cflag & CSTOPB ? IUU_TWO_STOP_BITS : IUU_ONE_STOP_BIT);
937
938 /* set it */
939 status = iuu_uart_baud(port,
James Courtier-Dutton89b54392010-05-21 11:53:25 +0100940 baud * priv->boost / 100,
Olivier Bornet96dab772009-06-11 12:54:20 +0100941 &actual, parity);
942
943 /* set the termios value to the real one, so the user now what has
944 * changed. We support few fields so its easies to copy the old hw
945 * settings back over and then adjust them
946 */
James Courtier-Dutton89b54392010-05-21 11:53:25 +0100947 if (old_termios)
Alan Coxadc8d742012-07-14 15:31:47 +0100948 tty_termios_copy_hw(&tty->termios, old_termios);
Olivier Bornet96dab772009-06-11 12:54:20 +0100949 if (status != 0) /* Set failed - return old bits */
950 return;
951 /* Re-encode speed, parity and csize */
952 tty_encode_baud_rate(tty, baud, baud);
Alan Coxadc8d742012-07-14 15:31:47 +0100953 tty->termios.c_cflag &= ~(supported_mask|CSIZE);
954 tty->termios.c_cflag |= newval | csize;
Olivier Bornet96dab772009-06-11 12:54:20 +0100955}
956
Alan Cox335f8512009-06-11 12:26:29 +0100957static void iuu_close(struct usb_serial_port *port)
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200958{
959 /* iuu_led (port,255,0,0,0); */
960 struct usb_serial *serial;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200961
962 serial = port->serial;
963 if (!serial)
964 return;
965
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200966 iuu_uart_off(port);
967 if (serial->dev) {
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200968 /* free writebuf */
969 /* shutdown our urbs */
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700970 dev_dbg(&port->dev, "%s - shutting down urbs\n", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200971 usb_kill_urb(port->write_urb);
972 usb_kill_urb(port->read_urb);
973 usb_kill_urb(port->interrupt_in_urb);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200974 iuu_led(port, 0, 0, 0xF000, 0xFF);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200975 }
976}
977
Alan Coxfe1ae7f2009-09-19 13:13:33 -0700978static void iuu_init_termios(struct tty_struct *tty)
979{
Alan Coxadc8d742012-07-14 15:31:47 +0100980 tty->termios = tty_std_termios;
981 tty->termios.c_cflag = CLOCAL | CREAD | CS8 | B9600
Alan Coxfe1ae7f2009-09-19 13:13:33 -0700982 | TIOCM_CTS | CSTOPB | PARENB;
Alan Coxadc8d742012-07-14 15:31:47 +0100983 tty->termios.c_ispeed = 9600;
984 tty->termios.c_ospeed = 9600;
985 tty->termios.c_lflag = 0;
986 tty->termios.c_oflag = 0;
987 tty->termios.c_iflag = 0;
Alan Coxfe1ae7f2009-09-19 13:13:33 -0700988}
989
Alan Coxa509a7e2009-09-19 13:13:26 -0700990static int iuu_open(struct tty_struct *tty, struct usb_serial_port *port)
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200991{
992 struct usb_serial *serial = port->serial;
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700993 struct device *dev = &port->dev;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200994 u8 *buf;
995 int result;
James Courtier-Dutton89b54392010-05-21 11:53:25 +0100996 int baud;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200997 u32 actual;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200998 struct iuu_private *priv = usb_get_serial_port_data(port);
999
Alan Coxadc8d742012-07-14 15:31:47 +01001000 baud = tty->termios.c_ospeed;
1001 tty->termios.c_ispeed = baud;
James Courtier-Dutton89b54392010-05-21 11:53:25 +01001002 /* Re-encode speed */
1003 tty_encode_baud_rate(tty, baud, baud);
1004
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -07001005 dev_dbg(dev, "%s - baud %d\n", __func__, baud);
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001006 usb_clear_halt(serial->dev, port->write_urb->pipe);
1007 usb_clear_halt(serial->dev, port->read_urb->pipe);
1008
1009 buf = kmalloc(10, GFP_KERNEL);
1010 if (buf == NULL)
1011 return -ENOMEM;
1012
Alan Coxfe1ae7f2009-09-19 13:13:33 -07001013 priv->poll = 0;
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001014
1015 /* initialize writebuf */
1016#define FISH(a, b, c, d) do { \
1017 result = usb_control_msg(port->serial->dev, \
1018 usb_rcvctrlpipe(port->serial->dev, 0), \
1019 b, a, c, d, buf, 1, 1000); \
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -07001020 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 +02001021 buf[0]); } while (0);
1022
1023#define SOUP(a, b, c, d) do { \
1024 result = usb_control_msg(port->serial->dev, \
1025 usb_sndctrlpipe(port->serial->dev, 0), \
1026 b, a, c, d, NULL, 0, 1000); \
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -07001027 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 +02001028
1029 /* This is not UART related but IUU USB driver related or something */
1030 /* like that. Basically no IUU will accept any commands from the USB */
1031 /* host unless it has received the following message */
1032 /* sprintf(buf ,"%c%c%c%c",0x03,0x02,0x02,0x0); */
1033
1034 SOUP(0x03, 0x02, 0x02, 0x0);
1035 kfree(buf);
1036 iuu_led(port, 0xF000, 0xF000, 0, 0xFF);
1037 iuu_uart_on(port);
1038 if (boost < 100)
1039 boost = 100;
James Courtier-Dutton89b54392010-05-21 11:53:25 +01001040 priv->boost = boost;
1041 priv->baud = baud;
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001042 switch (clockmode) {
1043 case 2: /* 3.680 Mhz */
James Courtier-Dutton89b54392010-05-21 11:53:25 +01001044 priv->clk = IUU_CLK_3680000;
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001045 iuu_clk(port, IUU_CLK_3680000 * boost / 100);
1046 result =
James Courtier-Dutton89b54392010-05-21 11:53:25 +01001047 iuu_uart_baud(port, baud * boost / 100, &actual,
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001048 IUU_PARITY_EVEN);
1049 break;
1050 case 3: /* 6.00 Mhz */
1051 iuu_clk(port, IUU_CLK_6000000 * boost / 100);
James Courtier-Dutton89b54392010-05-21 11:53:25 +01001052 priv->clk = IUU_CLK_6000000;
1053 /* Ratio of 6000000 to 3500000 for baud 9600 */
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001054 result =
1055 iuu_uart_baud(port, 16457 * boost / 100, &actual,
1056 IUU_PARITY_EVEN);
1057 break;
1058 default: /* 3.579 Mhz */
1059 iuu_clk(port, IUU_CLK_3579000 * boost / 100);
James Courtier-Dutton89b54392010-05-21 11:53:25 +01001060 priv->clk = IUU_CLK_3579000;
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001061 result =
James Courtier-Dutton89b54392010-05-21 11:53:25 +01001062 iuu_uart_baud(port, baud * boost / 100, &actual,
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001063 IUU_PARITY_EVEN);
1064 }
1065
1066 /* set the cardin cardout signals */
1067 switch (cdmode) {
1068 case 0:
1069 iuu_cardin = 0;
1070 iuu_cardout = 0;
1071 break;
1072 case 1:
1073 iuu_cardin = TIOCM_CD;
1074 iuu_cardout = 0;
1075 break;
1076 case 2:
1077 iuu_cardin = 0;
1078 iuu_cardout = TIOCM_CD;
1079 break;
1080 case 3:
1081 iuu_cardin = TIOCM_DSR;
1082 iuu_cardout = 0;
1083 break;
1084 case 4:
1085 iuu_cardin = 0;
1086 iuu_cardout = TIOCM_DSR;
1087 break;
1088 case 5:
1089 iuu_cardin = TIOCM_CTS;
1090 iuu_cardout = 0;
1091 break;
1092 case 6:
1093 iuu_cardin = 0;
1094 iuu_cardout = TIOCM_CTS;
1095 break;
1096 case 7:
1097 iuu_cardin = TIOCM_RNG;
1098 iuu_cardout = 0;
1099 break;
1100 case 8:
1101 iuu_cardin = 0;
1102 iuu_cardout = TIOCM_RNG;
1103 }
1104
1105 iuu_uart_flush(port);
1106
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -07001107 dev_dbg(dev, "%s - initialization done\n", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001108
1109 memset(port->write_urb->transfer_buffer, IUU_UART_RX, 1);
1110 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
1111 usb_sndbulkpipe(port->serial->dev,
1112 port->bulk_out_endpointAddress),
1113 port->write_urb->transfer_buffer, 1,
1114 read_rxcmd_callback, port);
1115 result = usb_submit_urb(port->write_urb, GFP_KERNEL);
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001116 if (result) {
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -07001117 dev_err(dev, "%s - failed submitting read urb, error %d\n", __func__, result);
Alan Cox335f8512009-06-11 12:26:29 +01001118 iuu_close(port);
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001119 } else {
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -07001120 dev_dbg(dev, "%s - rxcmd OK\n", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001121 }
Johan Hovoldb58a6462011-11-10 14:58:30 +01001122
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001123 return result;
1124}
1125
Olivier Bornet20eda942009-08-18 21:05:55 +02001126/* how to change VCC */
1127static int iuu_vcc_set(struct usb_serial_port *port, unsigned int vcc)
1128{
1129 int status;
1130 u8 *buf;
1131
1132 buf = kmalloc(5, GFP_KERNEL);
1133 if (!buf)
1134 return -ENOMEM;
1135
Olivier Bornet20eda942009-08-18 21:05:55 +02001136 buf[0] = IUU_SET_VCC;
1137 buf[1] = vcc & 0xFF;
1138 buf[2] = (vcc >> 8) & 0xFF;
1139 buf[3] = (vcc >> 16) & 0xFF;
1140 buf[4] = (vcc >> 24) & 0xFF;
1141
1142 status = bulk_immediate(port, buf, 5);
1143 kfree(buf);
1144
1145 if (status != IUU_OPERATION_OK)
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -07001146 dev_dbg(&port->dev, "%s - vcc error status = %2x\n", __func__, status);
Olivier Bornet20eda942009-08-18 21:05:55 +02001147 else
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -07001148 dev_dbg(&port->dev, "%s - vcc OK !\n", __func__);
Olivier Bornet20eda942009-08-18 21:05:55 +02001149
1150 return status;
1151}
1152
1153/*
1154 * Sysfs Attributes
1155 */
1156
1157static ssize_t show_vcc_mode(struct device *dev,
1158 struct device_attribute *attr, char *buf)
1159{
1160 struct usb_serial_port *port = to_usb_serial_port(dev);
1161 struct iuu_private *priv = usb_get_serial_port_data(port);
1162
1163 return sprintf(buf, "%d\n", priv->vcc);
1164}
1165
1166static ssize_t store_vcc_mode(struct device *dev,
1167 struct device_attribute *attr, const char *buf, size_t count)
1168{
1169 struct usb_serial_port *port = to_usb_serial_port(dev);
1170 struct iuu_private *priv = usb_get_serial_port_data(port);
1171 unsigned long v;
1172
1173 if (strict_strtoul(buf, 10, &v)) {
1174 dev_err(dev, "%s - vcc_mode: %s is not a unsigned long\n",
1175 __func__, buf);
1176 goto fail_store_vcc_mode;
1177 }
1178
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -07001179 dev_dbg(dev, "%s: setting vcc_mode = %ld", __func__, v);
Olivier Bornet20eda942009-08-18 21:05:55 +02001180
1181 if ((v != 3) && (v != 5)) {
1182 dev_err(dev, "%s - vcc_mode %ld is invalid\n", __func__, v);
1183 } else {
1184 iuu_vcc_set(port, v);
1185 priv->vcc = v;
1186 }
1187fail_store_vcc_mode:
1188 return count;
1189}
1190
1191static DEVICE_ATTR(vcc_mode, S_IRUSR | S_IWUSR, show_vcc_mode,
1192 store_vcc_mode);
1193
1194static int iuu_create_sysfs_attrs(struct usb_serial_port *port)
1195{
Olivier Bornet20eda942009-08-18 21:05:55 +02001196 return device_create_file(&port->dev, &dev_attr_vcc_mode);
1197}
1198
1199static int iuu_remove_sysfs_attrs(struct usb_serial_port *port)
1200{
Olivier Bornet20eda942009-08-18 21:05:55 +02001201 device_remove_file(&port->dev, &dev_attr_vcc_mode);
1202 return 0;
1203}
1204
1205/*
1206 * End Sysfs Attributes
1207 */
1208
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001209static struct usb_serial_driver iuu_device = {
1210 .driver = {
1211 .owner = THIS_MODULE,
1212 .name = "iuu_phoenix",
1213 },
1214 .id_table = id_table,
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001215 .num_ports = 1,
Johan Hovoldbbcb2b92010-03-17 23:00:37 +01001216 .bulk_in_size = 512,
1217 .bulk_out_size = 512,
Olivier Bornet20eda942009-08-18 21:05:55 +02001218 .port_probe = iuu_create_sysfs_attrs,
1219 .port_remove = iuu_remove_sysfs_attrs,
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001220 .open = iuu_open,
1221 .close = iuu_close,
1222 .write = iuu_uart_write,
1223 .read_bulk_callback = iuu_uart_read_callback,
1224 .tiocmget = iuu_tiocmget,
1225 .tiocmset = iuu_tiocmset,
Olivier Bornet96dab772009-06-11 12:54:20 +01001226 .set_termios = iuu_set_termios,
Alan Coxfe1ae7f2009-09-19 13:13:33 -07001227 .init_termios = iuu_init_termios,
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001228 .attach = iuu_startup,
Alan Sternf9c99bb2009-06-02 11:53:55 -04001229 .release = iuu_release,
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001230};
1231
Alan Stern7dbe2462012-02-23 14:56:57 -05001232static struct usb_serial_driver * const serial_drivers[] = {
1233 &iuu_device, NULL
1234};
1235
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -07001236module_usb_serial_driver(serial_drivers, id_table);
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001237
1238MODULE_AUTHOR("Alain Degreffe eczema@ecze.com");
1239
1240MODULE_DESCRIPTION(DRIVER_DESC);
1241MODULE_LICENSE("GPL");
1242
1243MODULE_VERSION(DRIVER_VERSION);
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001244
1245module_param(xmas, bool, S_IRUGO | S_IWUSR);
Olivier Bornet8844a322009-08-18 21:05:54 +02001246MODULE_PARM_DESC(xmas, "Xmas colors enabled or not");
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001247
1248module_param(boost, int, S_IRUGO | S_IWUSR);
Olivier Bornet8844a322009-08-18 21:05:54 +02001249MODULE_PARM_DESC(boost, "Card overclock boost (in percent 100-500)");
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001250
1251module_param(clockmode, int, S_IRUGO | S_IWUSR);
Olivier Bornet8844a322009-08-18 21:05:54 +02001252MODULE_PARM_DESC(clockmode, "Card clock mode (1=3.579 MHz, 2=3.680 MHz, "
1253 "3=6 Mhz)");
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001254
1255module_param(cdmode, int, S_IRUGO | S_IWUSR);
Olivier Bornet8844a322009-08-18 21:05:54 +02001256MODULE_PARM_DESC(cdmode, "Card detect mode (0=none, 1=CD, 2=!CD, 3=DSR, "
1257 "4=!DSR, 5=CTS, 6=!CTS, 7=RING, 8=!RING)");
Olivier Bornete55c6d02009-08-18 21:05:57 +02001258
1259module_param(vcc_default, int, S_IRUGO | S_IWUSR);
1260MODULE_PARM_DESC(vcc_default, "Set default VCC (either 3 for 3.3V or 5 "
1261 "for 5V). Default to 5.");