blob: 22b1eb5040b7648815be691e215bcfe306cb2d78 [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
35
36#ifdef CONFIG_USB_SERIAL_DEBUG
Rusty Russell90ab5ee2012-01-13 09:32:20 +103037static bool debug = 1;
Alain Degreffe60a8fc02007-10-26 13:51:49 +020038#else
Rusty Russell90ab5ee2012-01-13 09:32:20 +103039static bool debug;
Alain Degreffe60a8fc02007-10-26 13:51:49 +020040#endif
41
42/*
43 * Version Information
44 */
James Courtier-Dutton89b54392010-05-21 11:53:25 +010045#define DRIVER_VERSION "v0.12"
Alain Degreffe60a8fc02007-10-26 13:51:49 +020046#define DRIVER_DESC "Infinity USB Unlimited Phoenix driver"
47
Németh Márton7d40d7e2010-01-10 15:34:24 +010048static const struct usb_device_id id_table[] = {
Alain Degreffe60a8fc02007-10-26 13:51:49 +020049 {USB_DEVICE(IUU_USB_VENDOR_ID, IUU_USB_PRODUCT_ID)},
50 {} /* Terminating entry */
51};
52MODULE_DEVICE_TABLE(usb, id_table);
53
Alain Degreffe60a8fc02007-10-26 13:51:49 +020054/* turbo parameter */
55static int boost = 100;
56static int clockmode = 1;
57static int cdmode = 1;
58static int iuu_cardin;
59static int iuu_cardout;
Rusty Russell90ab5ee2012-01-13 09:32:20 +103060static bool xmas;
Olivier Bornete55c6d02009-08-18 21:05:57 +020061static int vcc_default = 5;
Alain Degreffe60a8fc02007-10-26 13:51:49 +020062
63static void read_rxcmd_callback(struct urb *urb);
64
65struct iuu_private {
66 spinlock_t lock; /* store irq state */
67 wait_queue_head_t delta_msr_wait;
Alain Degreffe60a8fc02007-10-26 13:51:49 +020068 u8 line_status;
Alain Degreffe60a8fc02007-10-26 13:51:49 +020069 int tiostatus; /* store IUART SIGNAL for tiocmget call */
70 u8 reset; /* if 1 reset is needed */
71 int poll; /* number of poll */
72 u8 *writebuf; /* buffer for writing to device */
73 int writelen; /* num of byte to write to device */
74 u8 *buf; /* used for initialize speed */
75 u8 *dbgbuf; /* debug buffer */
76 u8 len;
Olivier Bornet20eda942009-08-18 21:05:55 +020077 int vcc; /* vcc (either 3 or 5 V) */
James Courtier-Dutton89b54392010-05-21 11:53:25 +010078 u32 baud;
79 u32 boost;
80 u32 clk;
Alain Degreffe60a8fc02007-10-26 13:51:49 +020081};
82
83
84static void iuu_free_buf(struct iuu_private *priv)
85{
86 kfree(priv->buf);
87 kfree(priv->dbgbuf);
88 kfree(priv->writebuf);
89}
90
91static int iuu_alloc_buf(struct iuu_private *priv)
92{
93 priv->buf = kzalloc(256, GFP_KERNEL);
94 priv->dbgbuf = kzalloc(256, GFP_KERNEL);
95 priv->writebuf = kzalloc(256, GFP_KERNEL);
96 if (!priv->buf || !priv->dbgbuf || !priv->writebuf) {
97 iuu_free_buf(priv);
Harvey Harrison441b62c2008-03-03 16:08:34 -080098 dbg("%s problem allocation buffer", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +020099 return -ENOMEM;
100 }
Harvey Harrison441b62c2008-03-03 16:08:34 -0800101 dbg("%s - Privates buffers allocation success", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200102 return 0;
103}
104
105static int iuu_startup(struct usb_serial *serial)
106{
107 struct iuu_private *priv;
108 priv = kzalloc(sizeof(struct iuu_private), GFP_KERNEL);
Harvey Harrison441b62c2008-03-03 16:08:34 -0800109 dbg("%s- priv allocation success", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200110 if (!priv)
111 return -ENOMEM;
112 if (iuu_alloc_buf(priv)) {
113 kfree(priv);
114 return -ENOMEM;
115 }
Olivier Bornete55c6d02009-08-18 21:05:57 +0200116 priv->vcc = vcc_default;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200117 spin_lock_init(&priv->lock);
118 init_waitqueue_head(&priv->delta_msr_wait);
119 usb_set_serial_port_data(serial->port[0], priv);
120 return 0;
121}
122
Alan Sternf9c99bb2009-06-02 11:53:55 -0400123/* Release function */
124static void iuu_release(struct usb_serial *serial)
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200125{
126 struct usb_serial_port *port = serial->port[0];
127 struct iuu_private *priv = usb_get_serial_port_data(port);
128 if (!port)
129 return;
130
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200131 if (priv) {
132 iuu_free_buf(priv);
Harvey Harrison441b62c2008-03-03 16:08:34 -0800133 dbg("%s - I will free all", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200134 usb_set_serial_port_data(port, NULL);
135
Harvey Harrison441b62c2008-03-03 16:08:34 -0800136 dbg("%s - priv is not anymore in port structure", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200137 kfree(priv);
138
Harvey Harrison441b62c2008-03-03 16:08:34 -0800139 dbg("%s priv is now kfree", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200140 }
141}
142
Alan Cox20b9d172011-02-14 16:26:50 +0000143static int iuu_tiocmset(struct tty_struct *tty,
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200144 unsigned int set, unsigned int clear)
145{
Alan Cox95da3102008-07-22 11:09:07 +0100146 struct usb_serial_port *port = tty->driver_data;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200147 struct iuu_private *priv = usb_get_serial_port_data(port);
Alan Cox7b1fc8b2008-02-20 21:39:25 +0000148 unsigned long flags;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200149
Alan Cox7b1fc8b2008-02-20 21:39:25 +0000150 /* FIXME: locking on tiomstatus */
Harvey Harrison441b62c2008-03-03 16:08:34 -0800151 dbg("%s (%d) msg : SET = 0x%04x, CLEAR = 0x%04x ", __func__,
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200152 port->number, set, clear);
Alan Cox7b1fc8b2008-02-20 21:39:25 +0000153
154 spin_lock_irqsave(&priv->lock, flags);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200155
James Courtier-Dutton89b54392010-05-21 11:53:25 +0100156 if ((set & TIOCM_RTS) && !(priv->tiostatus == TIOCM_RTS)) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800157 dbg("%s TIOCMSET RESET called !!!", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200158 priv->reset = 1;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200159 }
James Courtier-Dutton89b54392010-05-21 11:53:25 +0100160 if (set & TIOCM_RTS)
161 priv->tiostatus = TIOCM_RTS;
162
Alan Cox7b1fc8b2008-02-20 21:39:25 +0000163 spin_unlock_irqrestore(&priv->lock, flags);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200164 return 0;
165}
166
167/* This is used to provide a carrier detect mechanism
168 * When a card is present, the response is 0x00
169 * When no card , the reader respond with TIOCM_CD
170 * This is known as CD autodetect mechanism
171 */
Alan Cox60b33c12011-02-14 16:26:14 +0000172static int iuu_tiocmget(struct tty_struct *tty)
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200173{
Alan Cox95da3102008-07-22 11:09:07 +0100174 struct usb_serial_port *port = tty->driver_data;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200175 struct iuu_private *priv = usb_get_serial_port_data(port);
Alan Cox7b1fc8b2008-02-20 21:39:25 +0000176 unsigned long flags;
177 int rc;
178
179 spin_lock_irqsave(&priv->lock, flags);
180 rc = priv->tiostatus;
181 spin_unlock_irqrestore(&priv->lock, flags);
182
183 return rc;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200184}
185
186static void iuu_rxcmd(struct urb *urb)
187{
Ming Leicdc97792008-02-24 18:41:47 +0800188 struct usb_serial_port *port = urb->context;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200189 int result;
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800190 int status = urb->status;
191
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800192 if (status) {
193 dbg("%s - status = %d", __func__, status);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200194 /* error stop all */
195 return;
196 }
197
198
199 memset(port->write_urb->transfer_buffer, IUU_UART_RX, 1);
200 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
201 usb_sndbulkpipe(port->serial->dev,
202 port->bulk_out_endpointAddress),
203 port->write_urb->transfer_buffer, 1,
204 read_rxcmd_callback, port);
205 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
206}
207
208static int iuu_reset(struct usb_serial_port *port, u8 wt)
209{
210 struct iuu_private *priv = usb_get_serial_port_data(port);
211 int result;
212 char *buf_ptr = port->write_urb->transfer_buffer;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200213
214 /* Prepare the reset sequence */
215
216 *buf_ptr++ = IUU_RST_SET;
217 *buf_ptr++ = IUU_DELAY_MS;
218 *buf_ptr++ = wt;
219 *buf_ptr = IUU_RST_CLEAR;
220
221 /* send the sequence */
222
223 usb_fill_bulk_urb(port->write_urb,
224 port->serial->dev,
225 usb_sndbulkpipe(port->serial->dev,
226 port->bulk_out_endpointAddress),
227 port->write_urb->transfer_buffer, 4, iuu_rxcmd, port);
228 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
229 priv->reset = 0;
230 return result;
231}
232
233/* Status Function
234 * Return value is
235 * 0x00 = no card
236 * 0x01 = smartcard
237 * 0x02 = sim card
238 */
239static void iuu_update_status_callback(struct urb *urb)
240{
Ming Leicdc97792008-02-24 18:41:47 +0800241 struct usb_serial_port *port = urb->context;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200242 struct iuu_private *priv = usb_get_serial_port_data(port);
243 u8 *st;
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800244 int status = urb->status;
245
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800246 if (status) {
247 dbg("%s - status = %d", __func__, status);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200248 /* error stop all */
249 return;
250 }
251
252 st = urb->transfer_buffer;
Harvey Harrison441b62c2008-03-03 16:08:34 -0800253 dbg("%s - enter", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200254 if (urb->actual_length == 1) {
255 switch (st[0]) {
256 case 0x1:
257 priv->tiostatus = iuu_cardout;
258 break;
259 case 0x0:
260 priv->tiostatus = iuu_cardin;
261 break;
262 default:
263 priv->tiostatus = iuu_cardin;
264 }
265 }
266 iuu_rxcmd(urb);
267}
268
269static void iuu_status_callback(struct urb *urb)
270{
Ming Leicdc97792008-02-24 18:41:47 +0800271 struct usb_serial_port *port = urb->context;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200272 int result;
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800273 int status = urb->status;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200274
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800275 dbg("%s - status = %d", __func__, status);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200276 usb_fill_bulk_urb(port->read_urb, port->serial->dev,
277 usb_rcvbulkpipe(port->serial->dev,
278 port->bulk_in_endpointAddress),
279 port->read_urb->transfer_buffer, 256,
280 iuu_update_status_callback, port);
281 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
282}
283
284static int iuu_status(struct usb_serial_port *port)
285{
286 int result;
287
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200288 memset(port->write_urb->transfer_buffer, IUU_GET_STATE_REGISTER, 1);
289 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
290 usb_sndbulkpipe(port->serial->dev,
291 port->bulk_out_endpointAddress),
292 port->write_urb->transfer_buffer, 1,
293 iuu_status_callback, port);
294 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
295 return result;
296
297}
298
299static int bulk_immediate(struct usb_serial_port *port, u8 *buf, u8 count)
300{
301 int status;
302 struct usb_serial *serial = port->serial;
303 int actual = 0;
304
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200305 /* send the data out the bulk port */
306
307 status =
308 usb_bulk_msg(serial->dev,
309 usb_sndbulkpipe(serial->dev,
310 port->bulk_out_endpointAddress), buf,
311 count, &actual, HZ * 1);
312
Alan Cox9e8e2d22008-07-22 11:12:59 +0100313 if (status != IUU_OPERATION_OK)
Harvey Harrison441b62c2008-03-03 16:08:34 -0800314 dbg("%s - error = %2x", __func__, status);
Alan Cox9e8e2d22008-07-22 11:12:59 +0100315 else
Harvey Harrison441b62c2008-03-03 16:08:34 -0800316 dbg("%s - write OK !", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200317 return status;
318}
319
320static int read_immediate(struct usb_serial_port *port, u8 *buf, u8 count)
321{
322 int status;
323 struct usb_serial *serial = port->serial;
324 int actual = 0;
325
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200326 /* send the data out the bulk port */
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200327 status =
328 usb_bulk_msg(serial->dev,
329 usb_rcvbulkpipe(serial->dev,
330 port->bulk_in_endpointAddress), buf,
331 count, &actual, HZ * 1);
332
Alan Cox9e8e2d22008-07-22 11:12:59 +0100333 if (status != IUU_OPERATION_OK)
Harvey Harrison441b62c2008-03-03 16:08:34 -0800334 dbg("%s - error = %2x", __func__, status);
Alan Cox9e8e2d22008-07-22 11:12:59 +0100335 else
Harvey Harrison441b62c2008-03-03 16:08:34 -0800336 dbg("%s - read OK !", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200337 return status;
338}
339
340static int iuu_led(struct usb_serial_port *port, unsigned int R,
341 unsigned int G, unsigned int B, u8 f)
342{
343 int status;
344 u8 *buf;
345 buf = kmalloc(8, GFP_KERNEL);
346 if (!buf)
347 return -ENOMEM;
348
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200349 buf[0] = IUU_SET_LED;
350 buf[1] = R & 0xFF;
351 buf[2] = (R >> 8) & 0xFF;
352 buf[3] = G & 0xFF;
353 buf[4] = (G >> 8) & 0xFF;
354 buf[5] = B & 0xFF;
355 buf[6] = (B >> 8) & 0xFF;
356 buf[7] = f;
357 status = bulk_immediate(port, buf, 8);
358 kfree(buf);
359 if (status != IUU_OPERATION_OK)
Harvey Harrison441b62c2008-03-03 16:08:34 -0800360 dbg("%s - led error status = %2x", __func__, status);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200361 else
Harvey Harrison441b62c2008-03-03 16:08:34 -0800362 dbg("%s - led OK !", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200363 return IUU_OPERATION_OK;
364}
365
366static void iuu_rgbf_fill_buffer(u8 *buf, u8 r1, u8 r2, u8 g1, u8 g2, u8 b1,
367 u8 b2, u8 freq)
368{
369 *buf++ = IUU_SET_LED;
370 *buf++ = r1;
371 *buf++ = r2;
372 *buf++ = g1;
373 *buf++ = g2;
374 *buf++ = b1;
375 *buf++ = b2;
376 *buf = freq;
377}
378
379static void iuu_led_activity_on(struct urb *urb)
380{
Ming Leicdc97792008-02-24 18:41:47 +0800381 struct usb_serial_port *port = urb->context;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200382 int result;
383 char *buf_ptr = port->write_urb->transfer_buffer;
384 *buf_ptr++ = IUU_SET_LED;
385 if (xmas == 1) {
386 get_random_bytes(buf_ptr, 6);
387 *(buf_ptr+7) = 1;
388 } else {
389 iuu_rgbf_fill_buffer(buf_ptr, 255, 255, 0, 0, 0, 0, 255);
390 }
391
392 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
393 usb_sndbulkpipe(port->serial->dev,
394 port->bulk_out_endpointAddress),
395 port->write_urb->transfer_buffer, 8 ,
396 iuu_rxcmd, port);
397 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
398}
399
400static void iuu_led_activity_off(struct urb *urb)
401{
Ming Leicdc97792008-02-24 18:41:47 +0800402 struct usb_serial_port *port = urb->context;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200403 int result;
404 char *buf_ptr = port->write_urb->transfer_buffer;
405 if (xmas == 1) {
406 iuu_rxcmd(urb);
407 return;
408 } else {
409 *buf_ptr++ = IUU_SET_LED;
410 iuu_rgbf_fill_buffer(buf_ptr, 0, 0, 255, 255, 0, 0, 255);
411 }
412 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
413 usb_sndbulkpipe(port->serial->dev,
414 port->bulk_out_endpointAddress),
415 port->write_urb->transfer_buffer, 8 ,
416 iuu_rxcmd, port);
417 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
418}
419
420
421
422static int iuu_clk(struct usb_serial_port *port, int dwFrq)
423{
424 int status;
425 struct iuu_private *priv = usb_get_serial_port_data(port);
426 int Count = 0;
427 u8 FrqGenAdr = 0x69;
428 u8 DIV = 0; /* 8bit */
429 u8 XDRV = 0; /* 8bit */
430 u8 PUMP = 0; /* 3bit */
431 u8 PBmsb = 0; /* 2bit */
432 u8 PBlsb = 0; /* 8bit */
433 u8 PO = 0; /* 1bit */
434 u8 Q = 0; /* 7bit */
435 /* 24bit = 3bytes */
436 unsigned int P = 0;
437 unsigned int P2 = 0;
438 int frq = (int)dwFrq;
439
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200440 if (frq == 0) {
441 priv->buf[Count++] = IUU_UART_WRITE_I2C;
442 priv->buf[Count++] = FrqGenAdr << 1;
443 priv->buf[Count++] = 0x09;
444 priv->buf[Count++] = 0x00;
445
446 status = bulk_immediate(port, (u8 *) priv->buf, Count);
447 if (status != 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800448 dbg("%s - write error ", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200449 return status;
450 }
451 } else if (frq == 3579000) {
452 DIV = 100;
453 P = 1193;
454 Q = 40;
455 XDRV = 0;
456 } else if (frq == 3680000) {
457 DIV = 105;
458 P = 161;
459 Q = 5;
460 XDRV = 0;
461 } else if (frq == 6000000) {
462 DIV = 66;
463 P = 66;
464 Q = 2;
465 XDRV = 0x28;
466 } else {
467 unsigned int result = 0;
468 unsigned int tmp = 0;
469 unsigned int check;
470 unsigned int check2;
471 char found = 0x00;
472 unsigned int lQ = 2;
473 unsigned int lP = 2055;
474 unsigned int lDiv = 4;
475
476 for (lQ = 2; lQ <= 47 && !found; lQ++)
477 for (lP = 2055; lP >= 8 && !found; lP--)
478 for (lDiv = 4; lDiv <= 127 && !found; lDiv++) {
479 tmp = (12000000 / lDiv) * (lP / lQ);
480 if (abs((int)(tmp - frq)) <
481 abs((int)(frq - result))) {
482 check2 = (12000000 / lQ);
483 if (check2 < 250000)
484 continue;
485 check = (12000000 / lQ) * lP;
486 if (check > 400000000)
487 continue;
488 if (check < 100000000)
489 continue;
490 if (lDiv < 4 || lDiv > 127)
491 continue;
492 result = tmp;
493 P = lP;
494 DIV = lDiv;
495 Q = lQ;
496 if (result == frq)
497 found = 0x01;
498 }
499 }
500 }
501 P2 = ((P - PO) / 2) - 4;
502 DIV = DIV;
503 PUMP = 0x04;
504 PBmsb = (P2 >> 8 & 0x03);
505 PBlsb = P2 & 0xFF;
506 PO = (P >> 10) & 0x01;
507 Q = Q - 2;
508
509 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
510 priv->buf[Count++] = FrqGenAdr << 1;
511 priv->buf[Count++] = 0x09;
512 priv->buf[Count++] = 0x20; /* Adr = 0x09 */
513 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
514 priv->buf[Count++] = FrqGenAdr << 1;
515 priv->buf[Count++] = 0x0C;
516 priv->buf[Count++] = DIV; /* Adr = 0x0C */
517 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
518 priv->buf[Count++] = FrqGenAdr << 1;
519 priv->buf[Count++] = 0x12;
520 priv->buf[Count++] = XDRV; /* Adr = 0x12 */
521 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
522 priv->buf[Count++] = FrqGenAdr << 1;
523 priv->buf[Count++] = 0x13;
524 priv->buf[Count++] = 0x6B; /* Adr = 0x13 */
525 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
526 priv->buf[Count++] = FrqGenAdr << 1;
527 priv->buf[Count++] = 0x40;
528 priv->buf[Count++] = (0xC0 | ((PUMP & 0x07) << 2)) |
529 (PBmsb & 0x03); /* Adr = 0x40 */
530 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
531 priv->buf[Count++] = FrqGenAdr << 1;
532 priv->buf[Count++] = 0x41;
533 priv->buf[Count++] = PBlsb; /* Adr = 0x41 */
534 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
535 priv->buf[Count++] = FrqGenAdr << 1;
536 priv->buf[Count++] = 0x42;
537 priv->buf[Count++] = Q | (((PO & 0x01) << 7)); /* Adr = 0x42 */
538 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
539 priv->buf[Count++] = FrqGenAdr << 1;
540 priv->buf[Count++] = 0x44;
541 priv->buf[Count++] = (char)0xFF; /* Adr = 0x44 */
542 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
543 priv->buf[Count++] = FrqGenAdr << 1;
544 priv->buf[Count++] = 0x45;
545 priv->buf[Count++] = (char)0xFE; /* Adr = 0x45 */
546 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
547 priv->buf[Count++] = FrqGenAdr << 1;
548 priv->buf[Count++] = 0x46;
549 priv->buf[Count++] = 0x7F; /* Adr = 0x46 */
550 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
551 priv->buf[Count++] = FrqGenAdr << 1;
552 priv->buf[Count++] = 0x47;
553 priv->buf[Count++] = (char)0x84; /* Adr = 0x47 */
554
555 status = bulk_immediate(port, (u8 *) priv->buf, Count);
556 if (status != IUU_OPERATION_OK)
Harvey Harrison441b62c2008-03-03 16:08:34 -0800557 dbg("%s - write error ", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200558 return status;
559}
560
561static int iuu_uart_flush(struct usb_serial_port *port)
562{
563 int i;
564 int status;
565 u8 rxcmd = IUU_UART_RX;
566 struct iuu_private *priv = usb_get_serial_port_data(port);
567
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200568 if (iuu_led(port, 0xF000, 0, 0, 0xFF) < 0)
569 return -EIO;
570
571 for (i = 0; i < 2; i++) {
572 status = bulk_immediate(port, &rxcmd, 1);
573 if (status != IUU_OPERATION_OK) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800574 dbg("%s - uart_flush_write error", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200575 return status;
576 }
577
578 status = read_immediate(port, &priv->len, 1);
579 if (status != IUU_OPERATION_OK) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800580 dbg("%s - uart_flush_read error", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200581 return status;
582 }
583
584 if (priv->len > 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800585 dbg("%s - uart_flush datalen is : %i ", __func__,
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200586 priv->len);
587 status = read_immediate(port, priv->buf, priv->len);
588 if (status != IUU_OPERATION_OK) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800589 dbg("%s - uart_flush_read error", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200590 return status;
591 }
592 }
593 }
Harvey Harrison441b62c2008-03-03 16:08:34 -0800594 dbg("%s - uart_flush_read OK!", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200595 iuu_led(port, 0, 0xF000, 0, 0xFF);
596 return status;
597}
598
599static void read_buf_callback(struct urb *urb)
600{
Ming Leicdc97792008-02-24 18:41:47 +0800601 struct usb_serial_port *port = urb->context;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200602 unsigned char *data = urb->transfer_buffer;
603 struct tty_struct *tty;
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800604 int status = urb->status;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200605
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800606 if (status) {
607 if (status == -EPROTO) {
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200608 /* reschedule needed */
609 }
610 return;
611 }
612
Harvey Harrison441b62c2008-03-03 16:08:34 -0800613 dbg("%s - %i chars to write", __func__, urb->actual_length);
Alan Cox4a90f092008-10-13 10:39:46 +0100614 tty = tty_port_tty_get(&port->port);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200615 if (data == NULL)
Harvey Harrison441b62c2008-03-03 16:08:34 -0800616 dbg("%s - data is NULL !!!", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200617 if (tty && urb->actual_length && data) {
618 tty_insert_flip_string(tty, data, urb->actual_length);
619 tty_flip_buffer_push(tty);
620 }
Alan Cox4a90f092008-10-13 10:39:46 +0100621 tty_kref_put(tty);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200622 iuu_led_activity_on(urb);
623}
624
625static int iuu_bulk_write(struct usb_serial_port *port)
626{
627 struct iuu_private *priv = usb_get_serial_port_data(port);
Steven Rostedt85943032008-05-06 20:42:31 -0700628 unsigned long flags;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200629 int result;
630 int i;
Olivier Bornet5fcf62b2009-06-11 12:52:26 +0100631 int buf_len;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200632 char *buf_ptr = port->write_urb->transfer_buffer;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200633
Olivier Bornet5fcf62b2009-06-11 12:52:26 +0100634 spin_lock_irqsave(&priv->lock, flags);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200635 *buf_ptr++ = IUU_UART_ESC;
636 *buf_ptr++ = IUU_UART_TX;
637 *buf_ptr++ = priv->writelen;
638
Olivier Bornet5fcf62b2009-06-11 12:52:26 +0100639 memcpy(buf_ptr, priv->writebuf, priv->writelen);
640 buf_len = priv->writelen;
641 priv->writelen = 0;
642 spin_unlock_irqrestore(&priv->lock, flags);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200643 if (debug == 1) {
Olivier Bornet5fcf62b2009-06-11 12:52:26 +0100644 for (i = 0; i < buf_len; i++)
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200645 sprintf(priv->dbgbuf + i*2 ,
646 "%02X", priv->writebuf[i]);
Olivier Bornet5fcf62b2009-06-11 12:52:26 +0100647 priv->dbgbuf[buf_len+i*2] = 0;
Harvey Harrison441b62c2008-03-03 16:08:34 -0800648 dbg("%s - writing %i chars : %s", __func__,
Olivier Bornet5fcf62b2009-06-11 12:52:26 +0100649 buf_len, priv->dbgbuf);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200650 }
651 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
652 usb_sndbulkpipe(port->serial->dev,
653 port->bulk_out_endpointAddress),
Olivier Bornet5fcf62b2009-06-11 12:52:26 +0100654 port->write_urb->transfer_buffer, buf_len + 3,
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200655 iuu_rxcmd, port);
656 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200657 usb_serial_port_softint(port);
658 return result;
659}
660
661static int iuu_read_buf(struct usb_serial_port *port, int len)
662{
663 int result;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200664
665 usb_fill_bulk_urb(port->read_urb, port->serial->dev,
666 usb_rcvbulkpipe(port->serial->dev,
667 port->bulk_in_endpointAddress),
668 port->read_urb->transfer_buffer, len,
669 read_buf_callback, port);
670 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
671 return result;
672}
673
674static void iuu_uart_read_callback(struct urb *urb)
675{
Ming Leicdc97792008-02-24 18:41:47 +0800676 struct usb_serial_port *port = urb->context;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200677 struct iuu_private *priv = usb_get_serial_port_data(port);
Steven Rostedt85943032008-05-06 20:42:31 -0700678 unsigned long flags;
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800679 int status = urb->status;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200680 int error = 0;
681 int len = 0;
682 unsigned char *data = urb->transfer_buffer;
683 priv->poll++;
684
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800685 if (status) {
686 dbg("%s - status = %d", __func__, status);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200687 /* error stop all */
688 return;
689 }
690 if (data == NULL)
Harvey Harrison441b62c2008-03-03 16:08:34 -0800691 dbg("%s - data is NULL !!!", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200692
693 if (urb->actual_length == 1 && data != NULL)
694 len = (int) data[0];
695
696 if (urb->actual_length > 1) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800697 dbg("%s - urb->actual_length = %i", __func__,
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200698 urb->actual_length);
699 error = 1;
700 return;
701 }
702 /* if len > 0 call readbuf */
703
704 if (len > 0 && error == 0) {
705 dbg("%s - call read buf - len to read is %i ",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800706 __func__, len);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200707 status = iuu_read_buf(port, len);
708 return;
709 }
710 /* need to update status ? */
711 if (priv->poll > 99) {
712 status = iuu_status(port);
713 priv->poll = 0;
714 return;
715 }
716
717 /* reset waiting ? */
718
719 if (priv->reset == 1) {
720 status = iuu_reset(port, 0xC);
721 return;
722 }
723 /* Writebuf is waiting */
724 spin_lock_irqsave(&priv->lock, flags);
725 if (priv->writelen > 0) {
726 spin_unlock_irqrestore(&priv->lock, flags);
727 status = iuu_bulk_write(port);
728 return;
729 }
730 spin_unlock_irqrestore(&priv->lock, flags);
731 /* if nothing to write call again rxcmd */
Harvey Harrison441b62c2008-03-03 16:08:34 -0800732 dbg("%s - rxcmd recall", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200733 iuu_led_activity_off(urb);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200734}
735
Alan Cox95da3102008-07-22 11:09:07 +0100736static int iuu_uart_write(struct tty_struct *tty, struct usb_serial_port *port,
737 const u8 *buf, int count)
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200738{
739 struct iuu_private *priv = usb_get_serial_port_data(port);
Steven Rostedt85943032008-05-06 20:42:31 -0700740 unsigned long flags;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200741
742 if (count > 256)
743 return -ENOMEM;
744
745 spin_lock_irqsave(&priv->lock, flags);
Olivier Bornet5fcf62b2009-06-11 12:52:26 +0100746
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200747 /* fill the buffer */
Olivier Bornet5fcf62b2009-06-11 12:52:26 +0100748 memcpy(priv->writebuf + priv->writelen, buf, count);
749 priv->writelen += count;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200750 spin_unlock_irqrestore(&priv->lock, flags);
751
Alan Cox9e8e2d22008-07-22 11:12:59 +0100752 return count;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200753}
754
755static void read_rxcmd_callback(struct urb *urb)
756{
Ming Leicdc97792008-02-24 18:41:47 +0800757 struct usb_serial_port *port = urb->context;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200758 int result;
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800759 int status = urb->status;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200760
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800761 if (status) {
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200762 /* error stop all */
763 return;
764 }
765
766 usb_fill_bulk_urb(port->read_urb, port->serial->dev,
767 usb_rcvbulkpipe(port->serial->dev,
768 port->bulk_in_endpointAddress),
769 port->read_urb->transfer_buffer, 256,
770 iuu_uart_read_callback, port);
771 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
Harvey Harrison441b62c2008-03-03 16:08:34 -0800772 dbg("%s - submit result = %d", __func__, result);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200773}
774
775static int iuu_uart_on(struct usb_serial_port *port)
776{
777 int status;
778 u8 *buf;
779
780 buf = kmalloc(sizeof(u8) * 4, GFP_KERNEL);
781
782 if (!buf)
783 return -ENOMEM;
784
785 buf[0] = IUU_UART_ENABLE;
786 buf[1] = (u8) ((IUU_BAUD_9600 >> 8) & 0x00FF);
787 buf[2] = (u8) (0x00FF & IUU_BAUD_9600);
Olivier Bornetcc3447d2009-06-11 12:53:24 +0100788 buf[3] = (u8) (0x0F0 & IUU_ONE_STOP_BIT) | (0x07 & IUU_PARITY_EVEN);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200789
790 status = bulk_immediate(port, buf, 4);
791 if (status != IUU_OPERATION_OK) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800792 dbg("%s - uart_on error", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200793 goto uart_enable_failed;
794 }
795 /* iuu_reset() the card after iuu_uart_on() */
796 status = iuu_uart_flush(port);
797 if (status != IUU_OPERATION_OK)
Harvey Harrison441b62c2008-03-03 16:08:34 -0800798 dbg("%s - uart_flush error", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200799uart_enable_failed:
800 kfree(buf);
801 return status;
802}
803
804/* Diables the IUU UART (a.k.a. the Phoenix voiderface) */
805static int iuu_uart_off(struct usb_serial_port *port)
806{
807 int status;
808 u8 *buf;
809 buf = kmalloc(1, GFP_KERNEL);
810 if (!buf)
811 return -ENOMEM;
812 buf[0] = IUU_UART_DISABLE;
813
814 status = bulk_immediate(port, buf, 1);
815 if (status != IUU_OPERATION_OK)
Harvey Harrison441b62c2008-03-03 16:08:34 -0800816 dbg("%s - uart_off error", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200817
818 kfree(buf);
819 return status;
820}
821
James Courtier-Dutton89b54392010-05-21 11:53:25 +0100822static int iuu_uart_baud(struct usb_serial_port *port, u32 baud_base,
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200823 u32 *actual, u8 parity)
824{
825 int status;
James Courtier-Dutton89b54392010-05-21 11:53:25 +0100826 u32 baud;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200827 u8 *dataout;
828 u8 DataCount = 0;
829 u8 T1Frekvens = 0;
830 u8 T1reload = 0;
831 unsigned int T1FrekvensHZ = 0;
832
James Courtier-Dutton89b54392010-05-21 11:53:25 +0100833 dbg("%s - enter baud_base=%d", __func__, baud_base);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200834 dataout = kmalloc(sizeof(u8) * 5, GFP_KERNEL);
835
836 if (!dataout)
837 return -ENOMEM;
James Courtier-Dutton89b54392010-05-21 11:53:25 +0100838 /*baud = (((priv->clk / 35) * baud_base) / 100000); */
839 baud = baud_base;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200840
841 if (baud < 1200 || baud > 230400) {
842 kfree(dataout);
843 return IUU_INVALID_PARAMETER;
844 }
845 if (baud > 977) {
846 T1Frekvens = 3;
847 T1FrekvensHZ = 500000;
848 }
849
850 if (baud > 3906) {
851 T1Frekvens = 2;
852 T1FrekvensHZ = 2000000;
853 }
854
855 if (baud > 11718) {
856 T1Frekvens = 1;
857 T1FrekvensHZ = 6000000;
858 }
859
860 if (baud > 46875) {
861 T1Frekvens = 0;
862 T1FrekvensHZ = 24000000;
863 }
864
865 T1reload = 256 - (u8) (T1FrekvensHZ / (baud * 2));
866
867 /* magic number here: ENTER_FIRMWARE_UPDATE; */
868 dataout[DataCount++] = IUU_UART_ESC;
869 /* magic number here: CHANGE_BAUD; */
870 dataout[DataCount++] = IUU_UART_CHANGE;
871 dataout[DataCount++] = T1Frekvens;
872 dataout[DataCount++] = T1reload;
873
874 *actual = (T1FrekvensHZ / (256 - T1reload)) / 2;
875
876 switch (parity & 0x0F) {
877 case IUU_PARITY_NONE:
878 dataout[DataCount++] = 0x00;
879 break;
880 case IUU_PARITY_EVEN:
881 dataout[DataCount++] = 0x01;
882 break;
883 case IUU_PARITY_ODD:
884 dataout[DataCount++] = 0x02;
885 break;
886 case IUU_PARITY_MARK:
887 dataout[DataCount++] = 0x03;
888 break;
889 case IUU_PARITY_SPACE:
890 dataout[DataCount++] = 0x04;
891 break;
892 default:
893 kfree(dataout);
894 return IUU_INVALID_PARAMETER;
895 break;
896 }
897
898 switch (parity & 0xF0) {
899 case IUU_ONE_STOP_BIT:
900 dataout[DataCount - 1] |= IUU_ONE_STOP_BIT;
901 break;
902
903 case IUU_TWO_STOP_BITS:
904 dataout[DataCount - 1] |= IUU_TWO_STOP_BITS;
905 break;
906 default:
907 kfree(dataout);
908 return IUU_INVALID_PARAMETER;
909 break;
910 }
911
912 status = bulk_immediate(port, dataout, DataCount);
913 if (status != IUU_OPERATION_OK)
Harvey Harrison441b62c2008-03-03 16:08:34 -0800914 dbg("%s - uart_off error", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200915 kfree(dataout);
916 return status;
917}
918
Olivier Bornet96dab772009-06-11 12:54:20 +0100919static void iuu_set_termios(struct tty_struct *tty,
920 struct usb_serial_port *port, struct ktermios *old_termios)
921{
922 const u32 supported_mask = CMSPAR|PARENB|PARODD;
James Courtier-Dutton89b54392010-05-21 11:53:25 +0100923 struct iuu_private *priv = usb_get_serial_port_data(port);
Olivier Bornet96dab772009-06-11 12:54:20 +0100924 unsigned int cflag = tty->termios->c_cflag;
925 int status;
926 u32 actual;
927 u32 parity;
928 int csize = CS7;
James Courtier-Dutton89b54392010-05-21 11:53:25 +0100929 int baud;
Olivier Bornet96dab772009-06-11 12:54:20 +0100930 u32 newval = cflag & supported_mask;
931
James Courtier-Dutton89b54392010-05-21 11:53:25 +0100932 /* Just use the ospeed. ispeed should be the same. */
933 baud = tty->termios->c_ospeed;
934
935 dbg("%s - enter c_ospeed or baud=%d", __func__, baud);
936
Olivier Bornet96dab772009-06-11 12:54:20 +0100937 /* compute the parity parameter */
938 parity = 0;
939 if (cflag & CMSPAR) { /* Using mark space */
940 if (cflag & PARODD)
941 parity |= IUU_PARITY_SPACE;
942 else
943 parity |= IUU_PARITY_MARK;
944 } else if (!(cflag & PARENB)) {
945 parity |= IUU_PARITY_NONE;
946 csize = CS8;
947 } else if (cflag & PARODD)
948 parity |= IUU_PARITY_ODD;
949 else
950 parity |= IUU_PARITY_EVEN;
951
952 parity |= (cflag & CSTOPB ? IUU_TWO_STOP_BITS : IUU_ONE_STOP_BIT);
953
954 /* set it */
955 status = iuu_uart_baud(port,
James Courtier-Dutton89b54392010-05-21 11:53:25 +0100956 baud * priv->boost / 100,
Olivier Bornet96dab772009-06-11 12:54:20 +0100957 &actual, parity);
958
959 /* set the termios value to the real one, so the user now what has
960 * changed. We support few fields so its easies to copy the old hw
961 * settings back over and then adjust them
962 */
James Courtier-Dutton89b54392010-05-21 11:53:25 +0100963 if (old_termios)
964 tty_termios_copy_hw(tty->termios, old_termios);
Olivier Bornet96dab772009-06-11 12:54:20 +0100965 if (status != 0) /* Set failed - return old bits */
966 return;
967 /* Re-encode speed, parity and csize */
968 tty_encode_baud_rate(tty, baud, baud);
969 tty->termios->c_cflag &= ~(supported_mask|CSIZE);
970 tty->termios->c_cflag |= newval | csize;
971}
972
Alan Cox335f8512009-06-11 12:26:29 +0100973static void iuu_close(struct usb_serial_port *port)
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200974{
975 /* iuu_led (port,255,0,0,0); */
976 struct usb_serial *serial;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200977
978 serial = port->serial;
979 if (!serial)
980 return;
981
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200982 iuu_uart_off(port);
983 if (serial->dev) {
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200984 /* free writebuf */
985 /* shutdown our urbs */
Harvey Harrison441b62c2008-03-03 16:08:34 -0800986 dbg("%s - shutting down urbs", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200987 usb_kill_urb(port->write_urb);
988 usb_kill_urb(port->read_urb);
989 usb_kill_urb(port->interrupt_in_urb);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200990 iuu_led(port, 0, 0, 0xF000, 0xFF);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200991 }
992}
993
Alan Coxfe1ae7f2009-09-19 13:13:33 -0700994static void iuu_init_termios(struct tty_struct *tty)
995{
996 *(tty->termios) = tty_std_termios;
997 tty->termios->c_cflag = CLOCAL | CREAD | CS8 | B9600
998 | TIOCM_CTS | CSTOPB | PARENB;
999 tty->termios->c_ispeed = 9600;
1000 tty->termios->c_ospeed = 9600;
1001 tty->termios->c_lflag = 0;
1002 tty->termios->c_oflag = 0;
1003 tty->termios->c_iflag = 0;
1004}
1005
Alan Coxa509a7e2009-09-19 13:13:26 -07001006static int iuu_open(struct tty_struct *tty, struct usb_serial_port *port)
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001007{
1008 struct usb_serial *serial = port->serial;
1009 u8 *buf;
1010 int result;
James Courtier-Dutton89b54392010-05-21 11:53:25 +01001011 int baud;
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001012 u32 actual;
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001013 struct iuu_private *priv = usb_get_serial_port_data(port);
1014
James Courtier-Dutton89b54392010-05-21 11:53:25 +01001015 baud = tty->termios->c_ospeed;
1016 tty->termios->c_ispeed = baud;
1017 /* Re-encode speed */
1018 tty_encode_baud_rate(tty, baud, baud);
1019
1020 dbg("%s - port %d, baud %d", __func__, port->number, baud);
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001021 usb_clear_halt(serial->dev, port->write_urb->pipe);
1022 usb_clear_halt(serial->dev, port->read_urb->pipe);
1023
1024 buf = kmalloc(10, GFP_KERNEL);
1025 if (buf == NULL)
1026 return -ENOMEM;
1027
Alan Coxfe1ae7f2009-09-19 13:13:33 -07001028 priv->poll = 0;
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001029
1030 /* initialize writebuf */
1031#define FISH(a, b, c, d) do { \
1032 result = usb_control_msg(port->serial->dev, \
1033 usb_rcvctrlpipe(port->serial->dev, 0), \
1034 b, a, c, d, buf, 1, 1000); \
1035 dbg("0x%x:0x%x:0x%x:0x%x %d - %x", a, b, c, d, result, \
1036 buf[0]); } while (0);
1037
1038#define SOUP(a, b, c, d) do { \
1039 result = usb_control_msg(port->serial->dev, \
1040 usb_sndctrlpipe(port->serial->dev, 0), \
1041 b, a, c, d, NULL, 0, 1000); \
1042 dbg("0x%x:0x%x:0x%x:0x%x %d", a, b, c, d, result); } while (0)
1043
1044 /* This is not UART related but IUU USB driver related or something */
1045 /* like that. Basically no IUU will accept any commands from the USB */
1046 /* host unless it has received the following message */
1047 /* sprintf(buf ,"%c%c%c%c",0x03,0x02,0x02,0x0); */
1048
1049 SOUP(0x03, 0x02, 0x02, 0x0);
1050 kfree(buf);
1051 iuu_led(port, 0xF000, 0xF000, 0, 0xFF);
1052 iuu_uart_on(port);
1053 if (boost < 100)
1054 boost = 100;
James Courtier-Dutton89b54392010-05-21 11:53:25 +01001055 priv->boost = boost;
1056 priv->baud = baud;
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001057 switch (clockmode) {
1058 case 2: /* 3.680 Mhz */
James Courtier-Dutton89b54392010-05-21 11:53:25 +01001059 priv->clk = IUU_CLK_3680000;
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001060 iuu_clk(port, IUU_CLK_3680000 * boost / 100);
1061 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 break;
1065 case 3: /* 6.00 Mhz */
1066 iuu_clk(port, IUU_CLK_6000000 * boost / 100);
James Courtier-Dutton89b54392010-05-21 11:53:25 +01001067 priv->clk = IUU_CLK_6000000;
1068 /* Ratio of 6000000 to 3500000 for baud 9600 */
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001069 result =
1070 iuu_uart_baud(port, 16457 * boost / 100, &actual,
1071 IUU_PARITY_EVEN);
1072 break;
1073 default: /* 3.579 Mhz */
1074 iuu_clk(port, IUU_CLK_3579000 * boost / 100);
James Courtier-Dutton89b54392010-05-21 11:53:25 +01001075 priv->clk = IUU_CLK_3579000;
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001076 result =
James Courtier-Dutton89b54392010-05-21 11:53:25 +01001077 iuu_uart_baud(port, baud * boost / 100, &actual,
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001078 IUU_PARITY_EVEN);
1079 }
1080
1081 /* set the cardin cardout signals */
1082 switch (cdmode) {
1083 case 0:
1084 iuu_cardin = 0;
1085 iuu_cardout = 0;
1086 break;
1087 case 1:
1088 iuu_cardin = TIOCM_CD;
1089 iuu_cardout = 0;
1090 break;
1091 case 2:
1092 iuu_cardin = 0;
1093 iuu_cardout = TIOCM_CD;
1094 break;
1095 case 3:
1096 iuu_cardin = TIOCM_DSR;
1097 iuu_cardout = 0;
1098 break;
1099 case 4:
1100 iuu_cardin = 0;
1101 iuu_cardout = TIOCM_DSR;
1102 break;
1103 case 5:
1104 iuu_cardin = TIOCM_CTS;
1105 iuu_cardout = 0;
1106 break;
1107 case 6:
1108 iuu_cardin = 0;
1109 iuu_cardout = TIOCM_CTS;
1110 break;
1111 case 7:
1112 iuu_cardin = TIOCM_RNG;
1113 iuu_cardout = 0;
1114 break;
1115 case 8:
1116 iuu_cardin = 0;
1117 iuu_cardout = TIOCM_RNG;
1118 }
1119
1120 iuu_uart_flush(port);
1121
Harvey Harrison441b62c2008-03-03 16:08:34 -08001122 dbg("%s - initialization done", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001123
1124 memset(port->write_urb->transfer_buffer, IUU_UART_RX, 1);
1125 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
1126 usb_sndbulkpipe(port->serial->dev,
1127 port->bulk_out_endpointAddress),
1128 port->write_urb->transfer_buffer, 1,
1129 read_rxcmd_callback, port);
1130 result = usb_submit_urb(port->write_urb, GFP_KERNEL);
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001131 if (result) {
1132 dev_err(&port->dev, "%s - failed submitting read urb,"
Harvey Harrison441b62c2008-03-03 16:08:34 -08001133 " error %d\n", __func__, result);
Alan Cox335f8512009-06-11 12:26:29 +01001134 iuu_close(port);
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001135 } else {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001136 dbg("%s - rxcmd OK", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001137 }
Johan Hovoldb58a6462011-11-10 14:58:30 +01001138
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001139 return result;
1140}
1141
Olivier Bornet20eda942009-08-18 21:05:55 +02001142/* how to change VCC */
1143static int iuu_vcc_set(struct usb_serial_port *port, unsigned int vcc)
1144{
1145 int status;
1146 u8 *buf;
1147
1148 buf = kmalloc(5, GFP_KERNEL);
1149 if (!buf)
1150 return -ENOMEM;
1151
Olivier Bornet20eda942009-08-18 21:05:55 +02001152 buf[0] = IUU_SET_VCC;
1153 buf[1] = vcc & 0xFF;
1154 buf[2] = (vcc >> 8) & 0xFF;
1155 buf[3] = (vcc >> 16) & 0xFF;
1156 buf[4] = (vcc >> 24) & 0xFF;
1157
1158 status = bulk_immediate(port, buf, 5);
1159 kfree(buf);
1160
1161 if (status != IUU_OPERATION_OK)
1162 dbg("%s - vcc error status = %2x", __func__, status);
1163 else
1164 dbg("%s - vcc OK !", __func__);
1165
1166 return status;
1167}
1168
1169/*
1170 * Sysfs Attributes
1171 */
1172
1173static ssize_t show_vcc_mode(struct device *dev,
1174 struct device_attribute *attr, char *buf)
1175{
1176 struct usb_serial_port *port = to_usb_serial_port(dev);
1177 struct iuu_private *priv = usb_get_serial_port_data(port);
1178
1179 return sprintf(buf, "%d\n", priv->vcc);
1180}
1181
1182static ssize_t store_vcc_mode(struct device *dev,
1183 struct device_attribute *attr, const char *buf, size_t count)
1184{
1185 struct usb_serial_port *port = to_usb_serial_port(dev);
1186 struct iuu_private *priv = usb_get_serial_port_data(port);
1187 unsigned long v;
1188
1189 if (strict_strtoul(buf, 10, &v)) {
1190 dev_err(dev, "%s - vcc_mode: %s is not a unsigned long\n",
1191 __func__, buf);
1192 goto fail_store_vcc_mode;
1193 }
1194
1195 dbg("%s: setting vcc_mode = %ld", __func__, v);
1196
1197 if ((v != 3) && (v != 5)) {
1198 dev_err(dev, "%s - vcc_mode %ld is invalid\n", __func__, v);
1199 } else {
1200 iuu_vcc_set(port, v);
1201 priv->vcc = v;
1202 }
1203fail_store_vcc_mode:
1204 return count;
1205}
1206
1207static DEVICE_ATTR(vcc_mode, S_IRUSR | S_IWUSR, show_vcc_mode,
1208 store_vcc_mode);
1209
1210static int iuu_create_sysfs_attrs(struct usb_serial_port *port)
1211{
Olivier Bornet20eda942009-08-18 21:05:55 +02001212 return device_create_file(&port->dev, &dev_attr_vcc_mode);
1213}
1214
1215static int iuu_remove_sysfs_attrs(struct usb_serial_port *port)
1216{
Olivier Bornet20eda942009-08-18 21:05:55 +02001217 device_remove_file(&port->dev, &dev_attr_vcc_mode);
1218 return 0;
1219}
1220
1221/*
1222 * End Sysfs Attributes
1223 */
1224
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001225static struct usb_serial_driver iuu_device = {
1226 .driver = {
1227 .owner = THIS_MODULE,
1228 .name = "iuu_phoenix",
1229 },
1230 .id_table = id_table,
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001231 .num_ports = 1,
Johan Hovoldbbcb2b92010-03-17 23:00:37 +01001232 .bulk_in_size = 512,
1233 .bulk_out_size = 512,
Olivier Bornet20eda942009-08-18 21:05:55 +02001234 .port_probe = iuu_create_sysfs_attrs,
1235 .port_remove = iuu_remove_sysfs_attrs,
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001236 .open = iuu_open,
1237 .close = iuu_close,
1238 .write = iuu_uart_write,
1239 .read_bulk_callback = iuu_uart_read_callback,
1240 .tiocmget = iuu_tiocmget,
1241 .tiocmset = iuu_tiocmset,
Olivier Bornet96dab772009-06-11 12:54:20 +01001242 .set_termios = iuu_set_termios,
Alan Coxfe1ae7f2009-09-19 13:13:33 -07001243 .init_termios = iuu_init_termios,
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001244 .attach = iuu_startup,
Alan Sternf9c99bb2009-06-02 11:53:55 -04001245 .release = iuu_release,
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001246};
1247
Alan Stern7dbe2462012-02-23 14:56:57 -05001248static struct usb_serial_driver * const serial_drivers[] = {
1249 &iuu_device, NULL
1250};
1251
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -07001252module_usb_serial_driver(serial_drivers, id_table);
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001253
1254MODULE_AUTHOR("Alain Degreffe eczema@ecze.com");
1255
1256MODULE_DESCRIPTION(DRIVER_DESC);
1257MODULE_LICENSE("GPL");
1258
1259MODULE_VERSION(DRIVER_VERSION);
1260module_param(debug, bool, S_IRUGO | S_IWUSR);
1261MODULE_PARM_DESC(debug, "Debug enabled or not");
1262
1263module_param(xmas, bool, S_IRUGO | S_IWUSR);
Olivier Bornet8844a322009-08-18 21:05:54 +02001264MODULE_PARM_DESC(xmas, "Xmas colors enabled or not");
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001265
1266module_param(boost, int, S_IRUGO | S_IWUSR);
Olivier Bornet8844a322009-08-18 21:05:54 +02001267MODULE_PARM_DESC(boost, "Card overclock boost (in percent 100-500)");
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001268
1269module_param(clockmode, int, S_IRUGO | S_IWUSR);
Olivier Bornet8844a322009-08-18 21:05:54 +02001270MODULE_PARM_DESC(clockmode, "Card clock mode (1=3.579 MHz, 2=3.680 MHz, "
1271 "3=6 Mhz)");
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001272
1273module_param(cdmode, int, S_IRUGO | S_IWUSR);
Olivier Bornet8844a322009-08-18 21:05:54 +02001274MODULE_PARM_DESC(cdmode, "Card detect mode (0=none, 1=CD, 2=!CD, 3=DSR, "
1275 "4=!DSR, 5=CTS, 6=!CTS, 7=RING, 8=!RING)");
Olivier Bornete55c6d02009-08-18 21:05:57 +02001276
1277module_param(vcc_default, int, S_IRUGO | S_IWUSR);
1278MODULE_PARM_DESC(vcc_default, "Set default VCC (either 3 for 3.3V or 5 "
1279 "for 5V). Default to 5.");