blob: cd5533e81de7e113d77898795996346ce036378a [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
Johan Hovold0978c942012-10-18 10:52:17 +020056static int iuu_create_sysfs_attrs(struct usb_serial_port *port);
57static int iuu_remove_sysfs_attrs(struct usb_serial_port *port);
Alain Degreffe60a8fc02007-10-26 13:51:49 +020058static void read_rxcmd_callback(struct urb *urb);
59
60struct iuu_private {
61 spinlock_t lock; /* store irq state */
62 wait_queue_head_t delta_msr_wait;
Alain Degreffe60a8fc02007-10-26 13:51:49 +020063 u8 line_status;
Alain Degreffe60a8fc02007-10-26 13:51:49 +020064 int tiostatus; /* store IUART SIGNAL for tiocmget call */
65 u8 reset; /* if 1 reset is needed */
66 int poll; /* number of poll */
67 u8 *writebuf; /* buffer for writing to device */
68 int writelen; /* num of byte to write to device */
69 u8 *buf; /* used for initialize speed */
Alain Degreffe60a8fc02007-10-26 13:51:49 +020070 u8 len;
Olivier Bornet20eda942009-08-18 21:05:55 +020071 int vcc; /* vcc (either 3 or 5 V) */
James Courtier-Dutton89b54392010-05-21 11:53:25 +010072 u32 baud;
73 u32 boost;
74 u32 clk;
Alain Degreffe60a8fc02007-10-26 13:51:49 +020075};
76
Johan Hovold53636552012-10-17 13:34:59 +020077static int iuu_port_probe(struct usb_serial_port *port)
Alain Degreffe60a8fc02007-10-26 13:51:49 +020078{
79 struct iuu_private *priv;
Johan Hovold0978c942012-10-18 10:52:17 +020080 int ret;
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -070081
Alain Degreffe60a8fc02007-10-26 13:51:49 +020082 priv = kzalloc(sizeof(struct iuu_private), GFP_KERNEL);
Alain Degreffe60a8fc02007-10-26 13:51:49 +020083 if (!priv)
84 return -ENOMEM;
Johan Hovold53636552012-10-17 13:34:59 +020085
86 priv->buf = kzalloc(256, GFP_KERNEL);
87 if (!priv->buf) {
Alain Degreffe60a8fc02007-10-26 13:51:49 +020088 kfree(priv);
89 return -ENOMEM;
90 }
Johan Hovold53636552012-10-17 13:34:59 +020091
92 priv->writebuf = kzalloc(256, GFP_KERNEL);
93 if (!priv->writebuf) {
94 kfree(priv->buf);
95 kfree(priv);
96 return -ENOMEM;
97 }
98
Olivier Bornete55c6d02009-08-18 21:05:57 +020099 priv->vcc = vcc_default;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200100 spin_lock_init(&priv->lock);
101 init_waitqueue_head(&priv->delta_msr_wait);
Johan Hovold53636552012-10-17 13:34:59 +0200102
103 usb_set_serial_port_data(port, priv);
104
Johan Hovold0978c942012-10-18 10:52:17 +0200105 ret = iuu_create_sysfs_attrs(port);
106 if (ret) {
107 kfree(priv->writebuf);
108 kfree(priv->buf);
109 kfree(priv);
110 return ret;
111 }
112
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200113 return 0;
114}
115
Johan Hovold53636552012-10-17 13:34:59 +0200116static int iuu_port_remove(struct usb_serial_port *port)
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200117{
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200118 struct iuu_private *priv = usb_get_serial_port_data(port);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200119
Johan Hovold0978c942012-10-18 10:52:17 +0200120 iuu_remove_sysfs_attrs(port);
Johan Hovold53636552012-10-17 13:34:59 +0200121 kfree(priv->writebuf);
122 kfree(priv->buf);
123 kfree(priv);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200124
Johan Hovold53636552012-10-17 13:34:59 +0200125 return 0;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200126}
127
Alan Cox20b9d172011-02-14 16:26:50 +0000128static int iuu_tiocmset(struct tty_struct *tty,
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200129 unsigned int set, unsigned int clear)
130{
Alan Cox95da3102008-07-22 11:09:07 +0100131 struct usb_serial_port *port = tty->driver_data;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200132 struct iuu_private *priv = usb_get_serial_port_data(port);
Alan Cox7b1fc8b2008-02-20 21:39:25 +0000133 unsigned long flags;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200134
Alan Cox7b1fc8b2008-02-20 21:39:25 +0000135 /* FIXME: locking on tiomstatus */
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700136 dev_dbg(&port->dev, "%s msg : SET = 0x%04x, CLEAR = 0x%04x\n",
137 __func__, set, clear);
Alan Cox7b1fc8b2008-02-20 21:39:25 +0000138
139 spin_lock_irqsave(&priv->lock, flags);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200140
James Courtier-Dutton89b54392010-05-21 11:53:25 +0100141 if ((set & TIOCM_RTS) && !(priv->tiostatus == TIOCM_RTS)) {
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700142 dev_dbg(&port->dev, "%s TIOCMSET RESET called !!!\n", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200143 priv->reset = 1;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200144 }
James Courtier-Dutton89b54392010-05-21 11:53:25 +0100145 if (set & TIOCM_RTS)
146 priv->tiostatus = TIOCM_RTS;
147
Alan Cox7b1fc8b2008-02-20 21:39:25 +0000148 spin_unlock_irqrestore(&priv->lock, flags);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200149 return 0;
150}
151
152/* This is used to provide a carrier detect mechanism
153 * When a card is present, the response is 0x00
154 * When no card , the reader respond with TIOCM_CD
155 * This is known as CD autodetect mechanism
156 */
Alan Cox60b33c12011-02-14 16:26:14 +0000157static int iuu_tiocmget(struct tty_struct *tty)
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200158{
Alan Cox95da3102008-07-22 11:09:07 +0100159 struct usb_serial_port *port = tty->driver_data;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200160 struct iuu_private *priv = usb_get_serial_port_data(port);
Alan Cox7b1fc8b2008-02-20 21:39:25 +0000161 unsigned long flags;
162 int rc;
163
164 spin_lock_irqsave(&priv->lock, flags);
165 rc = priv->tiostatus;
166 spin_unlock_irqrestore(&priv->lock, flags);
167
168 return rc;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200169}
170
171static void iuu_rxcmd(struct urb *urb)
172{
Ming Leicdc97792008-02-24 18:41:47 +0800173 struct usb_serial_port *port = urb->context;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200174 int result;
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800175 int status = urb->status;
176
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800177 if (status) {
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700178 dev_dbg(&port->dev, "%s - status = %d\n", __func__, status);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200179 /* error stop all */
180 return;
181 }
182
183
184 memset(port->write_urb->transfer_buffer, IUU_UART_RX, 1);
185 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
186 usb_sndbulkpipe(port->serial->dev,
187 port->bulk_out_endpointAddress),
188 port->write_urb->transfer_buffer, 1,
189 read_rxcmd_callback, port);
190 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
191}
192
193static int iuu_reset(struct usb_serial_port *port, u8 wt)
194{
195 struct iuu_private *priv = usb_get_serial_port_data(port);
196 int result;
197 char *buf_ptr = port->write_urb->transfer_buffer;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200198
199 /* Prepare the reset sequence */
200
201 *buf_ptr++ = IUU_RST_SET;
202 *buf_ptr++ = IUU_DELAY_MS;
203 *buf_ptr++ = wt;
204 *buf_ptr = IUU_RST_CLEAR;
205
206 /* send the sequence */
207
208 usb_fill_bulk_urb(port->write_urb,
209 port->serial->dev,
210 usb_sndbulkpipe(port->serial->dev,
211 port->bulk_out_endpointAddress),
212 port->write_urb->transfer_buffer, 4, iuu_rxcmd, port);
213 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
214 priv->reset = 0;
215 return result;
216}
217
218/* Status Function
219 * Return value is
220 * 0x00 = no card
221 * 0x01 = smartcard
222 * 0x02 = sim card
223 */
224static void iuu_update_status_callback(struct urb *urb)
225{
Ming Leicdc97792008-02-24 18:41:47 +0800226 struct usb_serial_port *port = urb->context;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200227 struct iuu_private *priv = usb_get_serial_port_data(port);
228 u8 *st;
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800229 int status = urb->status;
230
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800231 if (status) {
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700232 dev_dbg(&port->dev, "%s - status = %d\n", __func__, status);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200233 /* error stop all */
234 return;
235 }
236
237 st = urb->transfer_buffer;
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700238 dev_dbg(&port->dev, "%s - enter\n", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200239 if (urb->actual_length == 1) {
240 switch (st[0]) {
241 case 0x1:
242 priv->tiostatus = iuu_cardout;
243 break;
244 case 0x0:
245 priv->tiostatus = iuu_cardin;
246 break;
247 default:
248 priv->tiostatus = iuu_cardin;
249 }
250 }
251 iuu_rxcmd(urb);
252}
253
254static void iuu_status_callback(struct urb *urb)
255{
Ming Leicdc97792008-02-24 18:41:47 +0800256 struct usb_serial_port *port = urb->context;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200257 int result;
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800258 int status = urb->status;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200259
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700260 dev_dbg(&port->dev, "%s - status = %d\n", __func__, status);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200261 usb_fill_bulk_urb(port->read_urb, port->serial->dev,
262 usb_rcvbulkpipe(port->serial->dev,
263 port->bulk_in_endpointAddress),
264 port->read_urb->transfer_buffer, 256,
265 iuu_update_status_callback, port);
266 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
267}
268
269static int iuu_status(struct usb_serial_port *port)
270{
271 int result;
272
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200273 memset(port->write_urb->transfer_buffer, IUU_GET_STATE_REGISTER, 1);
274 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
275 usb_sndbulkpipe(port->serial->dev,
276 port->bulk_out_endpointAddress),
277 port->write_urb->transfer_buffer, 1,
278 iuu_status_callback, port);
279 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
280 return result;
281
282}
283
284static int bulk_immediate(struct usb_serial_port *port, u8 *buf, u8 count)
285{
286 int status;
287 struct usb_serial *serial = port->serial;
288 int actual = 0;
289
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200290 /* send the data out the bulk port */
291
292 status =
293 usb_bulk_msg(serial->dev,
294 usb_sndbulkpipe(serial->dev,
295 port->bulk_out_endpointAddress), buf,
296 count, &actual, HZ * 1);
297
Alan Cox9e8e2d22008-07-22 11:12:59 +0100298 if (status != IUU_OPERATION_OK)
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700299 dev_dbg(&port->dev, "%s - error = %2x\n", __func__, status);
Alan Cox9e8e2d22008-07-22 11:12:59 +0100300 else
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700301 dev_dbg(&port->dev, "%s - write OK !\n", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200302 return status;
303}
304
305static int read_immediate(struct usb_serial_port *port, u8 *buf, u8 count)
306{
307 int status;
308 struct usb_serial *serial = port->serial;
309 int actual = 0;
310
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200311 /* send the data out the bulk port */
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200312 status =
313 usb_bulk_msg(serial->dev,
314 usb_rcvbulkpipe(serial->dev,
315 port->bulk_in_endpointAddress), buf,
316 count, &actual, HZ * 1);
317
Alan Cox9e8e2d22008-07-22 11:12:59 +0100318 if (status != IUU_OPERATION_OK)
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700319 dev_dbg(&port->dev, "%s - error = %2x\n", __func__, status);
Alan Cox9e8e2d22008-07-22 11:12:59 +0100320 else
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700321 dev_dbg(&port->dev, "%s - read OK !\n", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200322 return status;
323}
324
325static int iuu_led(struct usb_serial_port *port, unsigned int R,
326 unsigned int G, unsigned int B, u8 f)
327{
328 int status;
329 u8 *buf;
330 buf = kmalloc(8, GFP_KERNEL);
331 if (!buf)
332 return -ENOMEM;
333
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200334 buf[0] = IUU_SET_LED;
335 buf[1] = R & 0xFF;
336 buf[2] = (R >> 8) & 0xFF;
337 buf[3] = G & 0xFF;
338 buf[4] = (G >> 8) & 0xFF;
339 buf[5] = B & 0xFF;
340 buf[6] = (B >> 8) & 0xFF;
341 buf[7] = f;
342 status = bulk_immediate(port, buf, 8);
343 kfree(buf);
344 if (status != IUU_OPERATION_OK)
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700345 dev_dbg(&port->dev, "%s - led error status = %2x\n", __func__, status);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200346 else
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700347 dev_dbg(&port->dev, "%s - led OK !\n", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200348 return IUU_OPERATION_OK;
349}
350
351static void iuu_rgbf_fill_buffer(u8 *buf, u8 r1, u8 r2, u8 g1, u8 g2, u8 b1,
352 u8 b2, u8 freq)
353{
354 *buf++ = IUU_SET_LED;
355 *buf++ = r1;
356 *buf++ = r2;
357 *buf++ = g1;
358 *buf++ = g2;
359 *buf++ = b1;
360 *buf++ = b2;
361 *buf = freq;
362}
363
364static void iuu_led_activity_on(struct urb *urb)
365{
Ming Leicdc97792008-02-24 18:41:47 +0800366 struct usb_serial_port *port = urb->context;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200367 int result;
368 char *buf_ptr = port->write_urb->transfer_buffer;
369 *buf_ptr++ = IUU_SET_LED;
370 if (xmas == 1) {
371 get_random_bytes(buf_ptr, 6);
372 *(buf_ptr+7) = 1;
373 } else {
374 iuu_rgbf_fill_buffer(buf_ptr, 255, 255, 0, 0, 0, 0, 255);
375 }
376
377 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
378 usb_sndbulkpipe(port->serial->dev,
379 port->bulk_out_endpointAddress),
380 port->write_urb->transfer_buffer, 8 ,
381 iuu_rxcmd, port);
382 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
383}
384
385static void iuu_led_activity_off(struct urb *urb)
386{
Ming Leicdc97792008-02-24 18:41:47 +0800387 struct usb_serial_port *port = urb->context;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200388 int result;
389 char *buf_ptr = port->write_urb->transfer_buffer;
390 if (xmas == 1) {
391 iuu_rxcmd(urb);
392 return;
393 } else {
394 *buf_ptr++ = IUU_SET_LED;
395 iuu_rgbf_fill_buffer(buf_ptr, 0, 0, 255, 255, 0, 0, 255);
396 }
397 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
398 usb_sndbulkpipe(port->serial->dev,
399 port->bulk_out_endpointAddress),
400 port->write_urb->transfer_buffer, 8 ,
401 iuu_rxcmd, port);
402 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
403}
404
405
406
407static int iuu_clk(struct usb_serial_port *port, int dwFrq)
408{
409 int status;
410 struct iuu_private *priv = usb_get_serial_port_data(port);
411 int Count = 0;
412 u8 FrqGenAdr = 0x69;
413 u8 DIV = 0; /* 8bit */
414 u8 XDRV = 0; /* 8bit */
415 u8 PUMP = 0; /* 3bit */
416 u8 PBmsb = 0; /* 2bit */
417 u8 PBlsb = 0; /* 8bit */
418 u8 PO = 0; /* 1bit */
419 u8 Q = 0; /* 7bit */
420 /* 24bit = 3bytes */
421 unsigned int P = 0;
422 unsigned int P2 = 0;
423 int frq = (int)dwFrq;
424
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200425 if (frq == 0) {
426 priv->buf[Count++] = IUU_UART_WRITE_I2C;
427 priv->buf[Count++] = FrqGenAdr << 1;
428 priv->buf[Count++] = 0x09;
429 priv->buf[Count++] = 0x00;
430
431 status = bulk_immediate(port, (u8 *) priv->buf, Count);
432 if (status != 0) {
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700433 dev_dbg(&port->dev, "%s - write error\n", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200434 return status;
435 }
436 } else if (frq == 3579000) {
437 DIV = 100;
438 P = 1193;
439 Q = 40;
440 XDRV = 0;
441 } else if (frq == 3680000) {
442 DIV = 105;
443 P = 161;
444 Q = 5;
445 XDRV = 0;
446 } else if (frq == 6000000) {
447 DIV = 66;
448 P = 66;
449 Q = 2;
450 XDRV = 0x28;
451 } else {
452 unsigned int result = 0;
453 unsigned int tmp = 0;
454 unsigned int check;
455 unsigned int check2;
456 char found = 0x00;
457 unsigned int lQ = 2;
458 unsigned int lP = 2055;
459 unsigned int lDiv = 4;
460
461 for (lQ = 2; lQ <= 47 && !found; lQ++)
462 for (lP = 2055; lP >= 8 && !found; lP--)
463 for (lDiv = 4; lDiv <= 127 && !found; lDiv++) {
464 tmp = (12000000 / lDiv) * (lP / lQ);
465 if (abs((int)(tmp - frq)) <
466 abs((int)(frq - result))) {
467 check2 = (12000000 / lQ);
468 if (check2 < 250000)
469 continue;
470 check = (12000000 / lQ) * lP;
471 if (check > 400000000)
472 continue;
473 if (check < 100000000)
474 continue;
475 if (lDiv < 4 || lDiv > 127)
476 continue;
477 result = tmp;
478 P = lP;
479 DIV = lDiv;
480 Q = lQ;
481 if (result == frq)
482 found = 0x01;
483 }
484 }
485 }
486 P2 = ((P - PO) / 2) - 4;
487 DIV = DIV;
488 PUMP = 0x04;
489 PBmsb = (P2 >> 8 & 0x03);
490 PBlsb = P2 & 0xFF;
491 PO = (P >> 10) & 0x01;
492 Q = Q - 2;
493
494 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
495 priv->buf[Count++] = FrqGenAdr << 1;
496 priv->buf[Count++] = 0x09;
497 priv->buf[Count++] = 0x20; /* Adr = 0x09 */
498 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
499 priv->buf[Count++] = FrqGenAdr << 1;
500 priv->buf[Count++] = 0x0C;
501 priv->buf[Count++] = DIV; /* Adr = 0x0C */
502 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
503 priv->buf[Count++] = FrqGenAdr << 1;
504 priv->buf[Count++] = 0x12;
505 priv->buf[Count++] = XDRV; /* Adr = 0x12 */
506 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
507 priv->buf[Count++] = FrqGenAdr << 1;
508 priv->buf[Count++] = 0x13;
509 priv->buf[Count++] = 0x6B; /* Adr = 0x13 */
510 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
511 priv->buf[Count++] = FrqGenAdr << 1;
512 priv->buf[Count++] = 0x40;
513 priv->buf[Count++] = (0xC0 | ((PUMP & 0x07) << 2)) |
514 (PBmsb & 0x03); /* Adr = 0x40 */
515 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
516 priv->buf[Count++] = FrqGenAdr << 1;
517 priv->buf[Count++] = 0x41;
518 priv->buf[Count++] = PBlsb; /* Adr = 0x41 */
519 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
520 priv->buf[Count++] = FrqGenAdr << 1;
521 priv->buf[Count++] = 0x42;
522 priv->buf[Count++] = Q | (((PO & 0x01) << 7)); /* Adr = 0x42 */
523 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
524 priv->buf[Count++] = FrqGenAdr << 1;
525 priv->buf[Count++] = 0x44;
526 priv->buf[Count++] = (char)0xFF; /* Adr = 0x44 */
527 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
528 priv->buf[Count++] = FrqGenAdr << 1;
529 priv->buf[Count++] = 0x45;
530 priv->buf[Count++] = (char)0xFE; /* Adr = 0x45 */
531 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
532 priv->buf[Count++] = FrqGenAdr << 1;
533 priv->buf[Count++] = 0x46;
534 priv->buf[Count++] = 0x7F; /* Adr = 0x46 */
535 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
536 priv->buf[Count++] = FrqGenAdr << 1;
537 priv->buf[Count++] = 0x47;
538 priv->buf[Count++] = (char)0x84; /* Adr = 0x47 */
539
540 status = bulk_immediate(port, (u8 *) priv->buf, Count);
541 if (status != IUU_OPERATION_OK)
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700542 dev_dbg(&port->dev, "%s - write error\n", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200543 return status;
544}
545
546static int iuu_uart_flush(struct usb_serial_port *port)
547{
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700548 struct device *dev = &port->dev;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200549 int i;
550 int status;
551 u8 rxcmd = IUU_UART_RX;
552 struct iuu_private *priv = usb_get_serial_port_data(port);
553
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200554 if (iuu_led(port, 0xF000, 0, 0, 0xFF) < 0)
555 return -EIO;
556
557 for (i = 0; i < 2; i++) {
558 status = bulk_immediate(port, &rxcmd, 1);
559 if (status != IUU_OPERATION_OK) {
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700560 dev_dbg(dev, "%s - uart_flush_write error\n", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200561 return status;
562 }
563
564 status = read_immediate(port, &priv->len, 1);
565 if (status != IUU_OPERATION_OK) {
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700566 dev_dbg(dev, "%s - uart_flush_read error\n", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200567 return status;
568 }
569
570 if (priv->len > 0) {
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700571 dev_dbg(dev, "%s - uart_flush datalen is : %i\n", __func__, priv->len);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200572 status = read_immediate(port, priv->buf, priv->len);
573 if (status != IUU_OPERATION_OK) {
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700574 dev_dbg(dev, "%s - uart_flush_read error\n", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200575 return status;
576 }
577 }
578 }
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700579 dev_dbg(dev, "%s - uart_flush_read OK!\n", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200580 iuu_led(port, 0, 0xF000, 0, 0xFF);
581 return status;
582}
583
584static void read_buf_callback(struct urb *urb)
585{
Ming Leicdc97792008-02-24 18:41:47 +0800586 struct usb_serial_port *port = urb->context;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200587 unsigned char *data = urb->transfer_buffer;
588 struct tty_struct *tty;
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800589 int status = urb->status;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200590
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800591 if (status) {
592 if (status == -EPROTO) {
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200593 /* reschedule needed */
594 }
595 return;
596 }
597
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700598 dev_dbg(&port->dev, "%s - %i chars to write\n", __func__, urb->actual_length);
Alan Cox4a90f092008-10-13 10:39:46 +0100599 tty = tty_port_tty_get(&port->port);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200600 if (data == NULL)
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700601 dev_dbg(&port->dev, "%s - data is NULL !!!\n", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200602 if (tty && urb->actual_length && data) {
603 tty_insert_flip_string(tty, data, urb->actual_length);
604 tty_flip_buffer_push(tty);
605 }
Alan Cox4a90f092008-10-13 10:39:46 +0100606 tty_kref_put(tty);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200607 iuu_led_activity_on(urb);
608}
609
610static int iuu_bulk_write(struct usb_serial_port *port)
611{
612 struct iuu_private *priv = usb_get_serial_port_data(port);
Steven Rostedt85943032008-05-06 20:42:31 -0700613 unsigned long flags;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200614 int result;
Olivier Bornet5fcf62b2009-06-11 12:52:26 +0100615 int buf_len;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200616 char *buf_ptr = port->write_urb->transfer_buffer;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200617
Olivier Bornet5fcf62b2009-06-11 12:52:26 +0100618 spin_lock_irqsave(&priv->lock, flags);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200619 *buf_ptr++ = IUU_UART_ESC;
620 *buf_ptr++ = IUU_UART_TX;
621 *buf_ptr++ = priv->writelen;
622
Olivier Bornet5fcf62b2009-06-11 12:52:26 +0100623 memcpy(buf_ptr, priv->writebuf, priv->writelen);
624 buf_len = priv->writelen;
625 priv->writelen = 0;
626 spin_unlock_irqrestore(&priv->lock, flags);
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700627 dev_dbg(&port->dev, "%s - writing %i chars : %*ph\n", __func__,
628 buf_len, buf_len, buf_ptr);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200629 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
630 usb_sndbulkpipe(port->serial->dev,
631 port->bulk_out_endpointAddress),
Olivier Bornet5fcf62b2009-06-11 12:52:26 +0100632 port->write_urb->transfer_buffer, buf_len + 3,
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200633 iuu_rxcmd, port);
634 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200635 usb_serial_port_softint(port);
636 return result;
637}
638
639static int iuu_read_buf(struct usb_serial_port *port, int len)
640{
641 int result;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200642
643 usb_fill_bulk_urb(port->read_urb, port->serial->dev,
644 usb_rcvbulkpipe(port->serial->dev,
645 port->bulk_in_endpointAddress),
646 port->read_urb->transfer_buffer, len,
647 read_buf_callback, port);
648 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
649 return result;
650}
651
652static void iuu_uart_read_callback(struct urb *urb)
653{
Ming Leicdc97792008-02-24 18:41:47 +0800654 struct usb_serial_port *port = urb->context;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200655 struct iuu_private *priv = usb_get_serial_port_data(port);
Steven Rostedt85943032008-05-06 20:42:31 -0700656 unsigned long flags;
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800657 int status = urb->status;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200658 int error = 0;
659 int len = 0;
660 unsigned char *data = urb->transfer_buffer;
661 priv->poll++;
662
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800663 if (status) {
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700664 dev_dbg(&port->dev, "%s - status = %d\n", __func__, status);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200665 /* error stop all */
666 return;
667 }
668 if (data == NULL)
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700669 dev_dbg(&port->dev, "%s - data is NULL !!!\n", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200670
671 if (urb->actual_length == 1 && data != NULL)
672 len = (int) data[0];
673
674 if (urb->actual_length > 1) {
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700675 dev_dbg(&port->dev, "%s - urb->actual_length = %i\n", __func__,
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200676 urb->actual_length);
677 error = 1;
678 return;
679 }
680 /* if len > 0 call readbuf */
681
682 if (len > 0 && error == 0) {
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700683 dev_dbg(&port->dev, "%s - call read buf - len to read is %i\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800684 __func__, len);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200685 status = iuu_read_buf(port, len);
686 return;
687 }
688 /* need to update status ? */
689 if (priv->poll > 99) {
690 status = iuu_status(port);
691 priv->poll = 0;
692 return;
693 }
694
695 /* reset waiting ? */
696
697 if (priv->reset == 1) {
698 status = iuu_reset(port, 0xC);
699 return;
700 }
701 /* Writebuf is waiting */
702 spin_lock_irqsave(&priv->lock, flags);
703 if (priv->writelen > 0) {
704 spin_unlock_irqrestore(&priv->lock, flags);
705 status = iuu_bulk_write(port);
706 return;
707 }
708 spin_unlock_irqrestore(&priv->lock, flags);
709 /* if nothing to write call again rxcmd */
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700710 dev_dbg(&port->dev, "%s - rxcmd recall\n", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200711 iuu_led_activity_off(urb);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200712}
713
Alan Cox95da3102008-07-22 11:09:07 +0100714static int iuu_uart_write(struct tty_struct *tty, struct usb_serial_port *port,
715 const u8 *buf, int count)
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200716{
717 struct iuu_private *priv = usb_get_serial_port_data(port);
Steven Rostedt85943032008-05-06 20:42:31 -0700718 unsigned long flags;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200719
720 if (count > 256)
721 return -ENOMEM;
722
723 spin_lock_irqsave(&priv->lock, flags);
Olivier Bornet5fcf62b2009-06-11 12:52:26 +0100724
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200725 /* fill the buffer */
Olivier Bornet5fcf62b2009-06-11 12:52:26 +0100726 memcpy(priv->writebuf + priv->writelen, buf, count);
727 priv->writelen += count;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200728 spin_unlock_irqrestore(&priv->lock, flags);
729
Alan Cox9e8e2d22008-07-22 11:12:59 +0100730 return count;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200731}
732
733static void read_rxcmd_callback(struct urb *urb)
734{
Ming Leicdc97792008-02-24 18:41:47 +0800735 struct usb_serial_port *port = urb->context;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200736 int result;
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800737 int status = urb->status;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200738
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800739 if (status) {
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200740 /* error stop all */
741 return;
742 }
743
744 usb_fill_bulk_urb(port->read_urb, port->serial->dev,
745 usb_rcvbulkpipe(port->serial->dev,
746 port->bulk_in_endpointAddress),
747 port->read_urb->transfer_buffer, 256,
748 iuu_uart_read_callback, port);
749 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700750 dev_dbg(&port->dev, "%s - submit result = %d\n", __func__, result);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200751}
752
753static int iuu_uart_on(struct usb_serial_port *port)
754{
755 int status;
756 u8 *buf;
757
758 buf = kmalloc(sizeof(u8) * 4, GFP_KERNEL);
759
760 if (!buf)
761 return -ENOMEM;
762
763 buf[0] = IUU_UART_ENABLE;
764 buf[1] = (u8) ((IUU_BAUD_9600 >> 8) & 0x00FF);
765 buf[2] = (u8) (0x00FF & IUU_BAUD_9600);
Olivier Bornetcc3447d2009-06-11 12:53:24 +0100766 buf[3] = (u8) (0x0F0 & IUU_ONE_STOP_BIT) | (0x07 & IUU_PARITY_EVEN);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200767
768 status = bulk_immediate(port, buf, 4);
769 if (status != IUU_OPERATION_OK) {
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700770 dev_dbg(&port->dev, "%s - uart_on error\n", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200771 goto uart_enable_failed;
772 }
773 /* iuu_reset() the card after iuu_uart_on() */
774 status = iuu_uart_flush(port);
775 if (status != IUU_OPERATION_OK)
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700776 dev_dbg(&port->dev, "%s - uart_flush error\n", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200777uart_enable_failed:
778 kfree(buf);
779 return status;
780}
781
782/* Diables the IUU UART (a.k.a. the Phoenix voiderface) */
783static int iuu_uart_off(struct usb_serial_port *port)
784{
785 int status;
786 u8 *buf;
787 buf = kmalloc(1, GFP_KERNEL);
788 if (!buf)
789 return -ENOMEM;
790 buf[0] = IUU_UART_DISABLE;
791
792 status = bulk_immediate(port, buf, 1);
793 if (status != IUU_OPERATION_OK)
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700794 dev_dbg(&port->dev, "%s - uart_off error\n", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200795
796 kfree(buf);
797 return status;
798}
799
James Courtier-Dutton89b54392010-05-21 11:53:25 +0100800static int iuu_uart_baud(struct usb_serial_port *port, u32 baud_base,
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200801 u32 *actual, u8 parity)
802{
803 int status;
James Courtier-Dutton89b54392010-05-21 11:53:25 +0100804 u32 baud;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200805 u8 *dataout;
806 u8 DataCount = 0;
807 u8 T1Frekvens = 0;
808 u8 T1reload = 0;
809 unsigned int T1FrekvensHZ = 0;
810
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700811 dev_dbg(&port->dev, "%s - enter baud_base=%d\n", __func__, baud_base);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200812 dataout = kmalloc(sizeof(u8) * 5, GFP_KERNEL);
813
814 if (!dataout)
815 return -ENOMEM;
James Courtier-Dutton89b54392010-05-21 11:53:25 +0100816 /*baud = (((priv->clk / 35) * baud_base) / 100000); */
817 baud = baud_base;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200818
819 if (baud < 1200 || baud > 230400) {
820 kfree(dataout);
821 return IUU_INVALID_PARAMETER;
822 }
823 if (baud > 977) {
824 T1Frekvens = 3;
825 T1FrekvensHZ = 500000;
826 }
827
828 if (baud > 3906) {
829 T1Frekvens = 2;
830 T1FrekvensHZ = 2000000;
831 }
832
833 if (baud > 11718) {
834 T1Frekvens = 1;
835 T1FrekvensHZ = 6000000;
836 }
837
838 if (baud > 46875) {
839 T1Frekvens = 0;
840 T1FrekvensHZ = 24000000;
841 }
842
843 T1reload = 256 - (u8) (T1FrekvensHZ / (baud * 2));
844
845 /* magic number here: ENTER_FIRMWARE_UPDATE; */
846 dataout[DataCount++] = IUU_UART_ESC;
847 /* magic number here: CHANGE_BAUD; */
848 dataout[DataCount++] = IUU_UART_CHANGE;
849 dataout[DataCount++] = T1Frekvens;
850 dataout[DataCount++] = T1reload;
851
852 *actual = (T1FrekvensHZ / (256 - T1reload)) / 2;
853
854 switch (parity & 0x0F) {
855 case IUU_PARITY_NONE:
856 dataout[DataCount++] = 0x00;
857 break;
858 case IUU_PARITY_EVEN:
859 dataout[DataCount++] = 0x01;
860 break;
861 case IUU_PARITY_ODD:
862 dataout[DataCount++] = 0x02;
863 break;
864 case IUU_PARITY_MARK:
865 dataout[DataCount++] = 0x03;
866 break;
867 case IUU_PARITY_SPACE:
868 dataout[DataCount++] = 0x04;
869 break;
870 default:
871 kfree(dataout);
872 return IUU_INVALID_PARAMETER;
873 break;
874 }
875
876 switch (parity & 0xF0) {
877 case IUU_ONE_STOP_BIT:
878 dataout[DataCount - 1] |= IUU_ONE_STOP_BIT;
879 break;
880
881 case IUU_TWO_STOP_BITS:
882 dataout[DataCount - 1] |= IUU_TWO_STOP_BITS;
883 break;
884 default:
885 kfree(dataout);
886 return IUU_INVALID_PARAMETER;
887 break;
888 }
889
890 status = bulk_immediate(port, dataout, DataCount);
891 if (status != IUU_OPERATION_OK)
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700892 dev_dbg(&port->dev, "%s - uart_off error\n", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200893 kfree(dataout);
894 return status;
895}
896
Olivier Bornet96dab772009-06-11 12:54:20 +0100897static void iuu_set_termios(struct tty_struct *tty,
898 struct usb_serial_port *port, struct ktermios *old_termios)
899{
900 const u32 supported_mask = CMSPAR|PARENB|PARODD;
James Courtier-Dutton89b54392010-05-21 11:53:25 +0100901 struct iuu_private *priv = usb_get_serial_port_data(port);
Alan Coxadc8d742012-07-14 15:31:47 +0100902 unsigned int cflag = tty->termios.c_cflag;
Olivier Bornet96dab772009-06-11 12:54:20 +0100903 int status;
904 u32 actual;
905 u32 parity;
906 int csize = CS7;
James Courtier-Dutton89b54392010-05-21 11:53:25 +0100907 int baud;
Olivier Bornet96dab772009-06-11 12:54:20 +0100908 u32 newval = cflag & supported_mask;
909
James Courtier-Dutton89b54392010-05-21 11:53:25 +0100910 /* Just use the ospeed. ispeed should be the same. */
Alan Coxadc8d742012-07-14 15:31:47 +0100911 baud = tty->termios.c_ospeed;
James Courtier-Dutton89b54392010-05-21 11:53:25 +0100912
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700913 dev_dbg(&port->dev, "%s - enter c_ospeed or baud=%d\n", __func__, baud);
James Courtier-Dutton89b54392010-05-21 11:53:25 +0100914
Olivier Bornet96dab772009-06-11 12:54:20 +0100915 /* compute the parity parameter */
916 parity = 0;
917 if (cflag & CMSPAR) { /* Using mark space */
918 if (cflag & PARODD)
919 parity |= IUU_PARITY_SPACE;
920 else
921 parity |= IUU_PARITY_MARK;
922 } else if (!(cflag & PARENB)) {
923 parity |= IUU_PARITY_NONE;
924 csize = CS8;
925 } else if (cflag & PARODD)
926 parity |= IUU_PARITY_ODD;
927 else
928 parity |= IUU_PARITY_EVEN;
929
930 parity |= (cflag & CSTOPB ? IUU_TWO_STOP_BITS : IUU_ONE_STOP_BIT);
931
932 /* set it */
933 status = iuu_uart_baud(port,
James Courtier-Dutton89b54392010-05-21 11:53:25 +0100934 baud * priv->boost / 100,
Olivier Bornet96dab772009-06-11 12:54:20 +0100935 &actual, parity);
936
937 /* set the termios value to the real one, so the user now what has
938 * changed. We support few fields so its easies to copy the old hw
939 * settings back over and then adjust them
940 */
James Courtier-Dutton89b54392010-05-21 11:53:25 +0100941 if (old_termios)
Alan Coxadc8d742012-07-14 15:31:47 +0100942 tty_termios_copy_hw(&tty->termios, old_termios);
Olivier Bornet96dab772009-06-11 12:54:20 +0100943 if (status != 0) /* Set failed - return old bits */
944 return;
945 /* Re-encode speed, parity and csize */
946 tty_encode_baud_rate(tty, baud, baud);
Alan Coxadc8d742012-07-14 15:31:47 +0100947 tty->termios.c_cflag &= ~(supported_mask|CSIZE);
948 tty->termios.c_cflag |= newval | csize;
Olivier Bornet96dab772009-06-11 12:54:20 +0100949}
950
Alan Cox335f8512009-06-11 12:26:29 +0100951static void iuu_close(struct usb_serial_port *port)
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200952{
953 /* iuu_led (port,255,0,0,0); */
954 struct usb_serial *serial;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200955
956 serial = port->serial;
957 if (!serial)
958 return;
959
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200960 iuu_uart_off(port);
961 if (serial->dev) {
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200962 /* free writebuf */
963 /* shutdown our urbs */
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700964 dev_dbg(&port->dev, "%s - shutting down urbs\n", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200965 usb_kill_urb(port->write_urb);
966 usb_kill_urb(port->read_urb);
967 usb_kill_urb(port->interrupt_in_urb);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200968 iuu_led(port, 0, 0, 0xF000, 0xFF);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200969 }
970}
971
Alan Coxfe1ae7f2009-09-19 13:13:33 -0700972static void iuu_init_termios(struct tty_struct *tty)
973{
Alan Coxadc8d742012-07-14 15:31:47 +0100974 tty->termios = tty_std_termios;
975 tty->termios.c_cflag = CLOCAL | CREAD | CS8 | B9600
Alan Coxfe1ae7f2009-09-19 13:13:33 -0700976 | TIOCM_CTS | CSTOPB | PARENB;
Alan Coxadc8d742012-07-14 15:31:47 +0100977 tty->termios.c_ispeed = 9600;
978 tty->termios.c_ospeed = 9600;
979 tty->termios.c_lflag = 0;
980 tty->termios.c_oflag = 0;
981 tty->termios.c_iflag = 0;
Alan Coxfe1ae7f2009-09-19 13:13:33 -0700982}
983
Alan Coxa509a7e2009-09-19 13:13:26 -0700984static int iuu_open(struct tty_struct *tty, struct usb_serial_port *port)
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200985{
986 struct usb_serial *serial = port->serial;
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700987 struct device *dev = &port->dev;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200988 u8 *buf;
989 int result;
James Courtier-Dutton89b54392010-05-21 11:53:25 +0100990 int baud;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200991 u32 actual;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200992 struct iuu_private *priv = usb_get_serial_port_data(port);
993
Alan Coxadc8d742012-07-14 15:31:47 +0100994 baud = tty->termios.c_ospeed;
995 tty->termios.c_ispeed = baud;
James Courtier-Dutton89b54392010-05-21 11:53:25 +0100996 /* Re-encode speed */
997 tty_encode_baud_rate(tty, baud, baud);
998
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -0700999 dev_dbg(dev, "%s - baud %d\n", __func__, baud);
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001000 usb_clear_halt(serial->dev, port->write_urb->pipe);
1001 usb_clear_halt(serial->dev, port->read_urb->pipe);
1002
1003 buf = kmalloc(10, GFP_KERNEL);
1004 if (buf == NULL)
1005 return -ENOMEM;
1006
Alan Coxfe1ae7f2009-09-19 13:13:33 -07001007 priv->poll = 0;
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001008
1009 /* initialize writebuf */
1010#define FISH(a, b, c, d) do { \
1011 result = usb_control_msg(port->serial->dev, \
1012 usb_rcvctrlpipe(port->serial->dev, 0), \
1013 b, a, c, d, buf, 1, 1000); \
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -07001014 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 +02001015 buf[0]); } while (0);
1016
1017#define SOUP(a, b, c, d) do { \
1018 result = usb_control_msg(port->serial->dev, \
1019 usb_sndctrlpipe(port->serial->dev, 0), \
1020 b, a, c, d, NULL, 0, 1000); \
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -07001021 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 +02001022
1023 /* This is not UART related but IUU USB driver related or something */
1024 /* like that. Basically no IUU will accept any commands from the USB */
1025 /* host unless it has received the following message */
1026 /* sprintf(buf ,"%c%c%c%c",0x03,0x02,0x02,0x0); */
1027
1028 SOUP(0x03, 0x02, 0x02, 0x0);
1029 kfree(buf);
1030 iuu_led(port, 0xF000, 0xF000, 0, 0xFF);
1031 iuu_uart_on(port);
1032 if (boost < 100)
1033 boost = 100;
James Courtier-Dutton89b54392010-05-21 11:53:25 +01001034 priv->boost = boost;
1035 priv->baud = baud;
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001036 switch (clockmode) {
1037 case 2: /* 3.680 Mhz */
James Courtier-Dutton89b54392010-05-21 11:53:25 +01001038 priv->clk = IUU_CLK_3680000;
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001039 iuu_clk(port, IUU_CLK_3680000 * boost / 100);
1040 result =
James Courtier-Dutton89b54392010-05-21 11:53:25 +01001041 iuu_uart_baud(port, baud * boost / 100, &actual,
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001042 IUU_PARITY_EVEN);
1043 break;
1044 case 3: /* 6.00 Mhz */
1045 iuu_clk(port, IUU_CLK_6000000 * boost / 100);
James Courtier-Dutton89b54392010-05-21 11:53:25 +01001046 priv->clk = IUU_CLK_6000000;
1047 /* Ratio of 6000000 to 3500000 for baud 9600 */
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001048 result =
1049 iuu_uart_baud(port, 16457 * boost / 100, &actual,
1050 IUU_PARITY_EVEN);
1051 break;
1052 default: /* 3.579 Mhz */
1053 iuu_clk(port, IUU_CLK_3579000 * boost / 100);
James Courtier-Dutton89b54392010-05-21 11:53:25 +01001054 priv->clk = IUU_CLK_3579000;
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001055 result =
James Courtier-Dutton89b54392010-05-21 11:53:25 +01001056 iuu_uart_baud(port, baud * boost / 100, &actual,
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001057 IUU_PARITY_EVEN);
1058 }
1059
1060 /* set the cardin cardout signals */
1061 switch (cdmode) {
1062 case 0:
1063 iuu_cardin = 0;
1064 iuu_cardout = 0;
1065 break;
1066 case 1:
1067 iuu_cardin = TIOCM_CD;
1068 iuu_cardout = 0;
1069 break;
1070 case 2:
1071 iuu_cardin = 0;
1072 iuu_cardout = TIOCM_CD;
1073 break;
1074 case 3:
1075 iuu_cardin = TIOCM_DSR;
1076 iuu_cardout = 0;
1077 break;
1078 case 4:
1079 iuu_cardin = 0;
1080 iuu_cardout = TIOCM_DSR;
1081 break;
1082 case 5:
1083 iuu_cardin = TIOCM_CTS;
1084 iuu_cardout = 0;
1085 break;
1086 case 6:
1087 iuu_cardin = 0;
1088 iuu_cardout = TIOCM_CTS;
1089 break;
1090 case 7:
1091 iuu_cardin = TIOCM_RNG;
1092 iuu_cardout = 0;
1093 break;
1094 case 8:
1095 iuu_cardin = 0;
1096 iuu_cardout = TIOCM_RNG;
1097 }
1098
1099 iuu_uart_flush(port);
1100
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -07001101 dev_dbg(dev, "%s - initialization done\n", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001102
1103 memset(port->write_urb->transfer_buffer, IUU_UART_RX, 1);
1104 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
1105 usb_sndbulkpipe(port->serial->dev,
1106 port->bulk_out_endpointAddress),
1107 port->write_urb->transfer_buffer, 1,
1108 read_rxcmd_callback, port);
1109 result = usb_submit_urb(port->write_urb, GFP_KERNEL);
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001110 if (result) {
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -07001111 dev_err(dev, "%s - failed submitting read urb, error %d\n", __func__, result);
Alan Cox335f8512009-06-11 12:26:29 +01001112 iuu_close(port);
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001113 } else {
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -07001114 dev_dbg(dev, "%s - rxcmd OK\n", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001115 }
Johan Hovoldb58a6462011-11-10 14:58:30 +01001116
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001117 return result;
1118}
1119
Olivier Bornet20eda942009-08-18 21:05:55 +02001120/* how to change VCC */
1121static int iuu_vcc_set(struct usb_serial_port *port, unsigned int vcc)
1122{
1123 int status;
1124 u8 *buf;
1125
1126 buf = kmalloc(5, GFP_KERNEL);
1127 if (!buf)
1128 return -ENOMEM;
1129
Olivier Bornet20eda942009-08-18 21:05:55 +02001130 buf[0] = IUU_SET_VCC;
1131 buf[1] = vcc & 0xFF;
1132 buf[2] = (vcc >> 8) & 0xFF;
1133 buf[3] = (vcc >> 16) & 0xFF;
1134 buf[4] = (vcc >> 24) & 0xFF;
1135
1136 status = bulk_immediate(port, buf, 5);
1137 kfree(buf);
1138
1139 if (status != IUU_OPERATION_OK)
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -07001140 dev_dbg(&port->dev, "%s - vcc error status = %2x\n", __func__, status);
Olivier Bornet20eda942009-08-18 21:05:55 +02001141 else
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -07001142 dev_dbg(&port->dev, "%s - vcc OK !\n", __func__);
Olivier Bornet20eda942009-08-18 21:05:55 +02001143
1144 return status;
1145}
1146
1147/*
1148 * Sysfs Attributes
1149 */
1150
1151static ssize_t show_vcc_mode(struct device *dev,
1152 struct device_attribute *attr, char *buf)
1153{
1154 struct usb_serial_port *port = to_usb_serial_port(dev);
1155 struct iuu_private *priv = usb_get_serial_port_data(port);
1156
1157 return sprintf(buf, "%d\n", priv->vcc);
1158}
1159
1160static ssize_t store_vcc_mode(struct device *dev,
1161 struct device_attribute *attr, const char *buf, size_t count)
1162{
1163 struct usb_serial_port *port = to_usb_serial_port(dev);
1164 struct iuu_private *priv = usb_get_serial_port_data(port);
1165 unsigned long v;
1166
1167 if (strict_strtoul(buf, 10, &v)) {
1168 dev_err(dev, "%s - vcc_mode: %s is not a unsigned long\n",
1169 __func__, buf);
1170 goto fail_store_vcc_mode;
1171 }
1172
Greg Kroah-Hartman2621cee2012-09-14 15:08:28 -07001173 dev_dbg(dev, "%s: setting vcc_mode = %ld", __func__, v);
Olivier Bornet20eda942009-08-18 21:05:55 +02001174
1175 if ((v != 3) && (v != 5)) {
1176 dev_err(dev, "%s - vcc_mode %ld is invalid\n", __func__, v);
1177 } else {
1178 iuu_vcc_set(port, v);
1179 priv->vcc = v;
1180 }
1181fail_store_vcc_mode:
1182 return count;
1183}
1184
1185static DEVICE_ATTR(vcc_mode, S_IRUSR | S_IWUSR, show_vcc_mode,
1186 store_vcc_mode);
1187
1188static int iuu_create_sysfs_attrs(struct usb_serial_port *port)
1189{
Olivier Bornet20eda942009-08-18 21:05:55 +02001190 return device_create_file(&port->dev, &dev_attr_vcc_mode);
1191}
1192
1193static int iuu_remove_sysfs_attrs(struct usb_serial_port *port)
1194{
Olivier Bornet20eda942009-08-18 21:05:55 +02001195 device_remove_file(&port->dev, &dev_attr_vcc_mode);
1196 return 0;
1197}
1198
1199/*
1200 * End Sysfs Attributes
1201 */
1202
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001203static struct usb_serial_driver iuu_device = {
1204 .driver = {
1205 .owner = THIS_MODULE,
1206 .name = "iuu_phoenix",
1207 },
1208 .id_table = id_table,
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001209 .num_ports = 1,
Johan Hovoldbbcb2b92010-03-17 23:00:37 +01001210 .bulk_in_size = 512,
1211 .bulk_out_size = 512,
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001212 .open = iuu_open,
1213 .close = iuu_close,
1214 .write = iuu_uart_write,
1215 .read_bulk_callback = iuu_uart_read_callback,
1216 .tiocmget = iuu_tiocmget,
1217 .tiocmset = iuu_tiocmset,
Olivier Bornet96dab772009-06-11 12:54:20 +01001218 .set_termios = iuu_set_termios,
Alan Coxfe1ae7f2009-09-19 13:13:33 -07001219 .init_termios = iuu_init_termios,
Johan Hovold53636552012-10-17 13:34:59 +02001220 .port_probe = iuu_port_probe,
1221 .port_remove = iuu_port_remove,
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001222};
1223
Alan Stern7dbe2462012-02-23 14:56:57 -05001224static struct usb_serial_driver * const serial_drivers[] = {
1225 &iuu_device, NULL
1226};
1227
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -07001228module_usb_serial_driver(serial_drivers, id_table);
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001229
1230MODULE_AUTHOR("Alain Degreffe eczema@ecze.com");
1231
1232MODULE_DESCRIPTION(DRIVER_DESC);
1233MODULE_LICENSE("GPL");
1234
1235MODULE_VERSION(DRIVER_VERSION);
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001236
1237module_param(xmas, bool, S_IRUGO | S_IWUSR);
Olivier Bornet8844a322009-08-18 21:05:54 +02001238MODULE_PARM_DESC(xmas, "Xmas colors enabled or not");
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001239
1240module_param(boost, int, S_IRUGO | S_IWUSR);
Olivier Bornet8844a322009-08-18 21:05:54 +02001241MODULE_PARM_DESC(boost, "Card overclock boost (in percent 100-500)");
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001242
1243module_param(clockmode, int, S_IRUGO | S_IWUSR);
Olivier Bornet8844a322009-08-18 21:05:54 +02001244MODULE_PARM_DESC(clockmode, "Card clock mode (1=3.579 MHz, 2=3.680 MHz, "
1245 "3=6 Mhz)");
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001246
1247module_param(cdmode, int, S_IRUGO | S_IWUSR);
Olivier Bornet8844a322009-08-18 21:05:54 +02001248MODULE_PARM_DESC(cdmode, "Card detect mode (0=none, 1=CD, 2=!CD, 3=DSR, "
1249 "4=!DSR, 5=CTS, 6=!CTS, 7=RING, 8=!RING)");
Olivier Bornete55c6d02009-08-18 21:05:57 +02001250
1251module_param(vcc_default, int, S_IRUGO | S_IWUSR);
1252MODULE_PARM_DESC(vcc_default, "Set default VCC (either 3 for 3.3V or 5 "
1253 "for 5V). Default to 5.");