blob: 500e8fbdd9e524a3348265a9a63b46474d559276 [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
54static struct usb_driver iuu_driver = {
55 .name = "iuu_phoenix",
Alain Degreffe60a8fc02007-10-26 13:51:49 +020056 .id_table = id_table,
Alain Degreffe60a8fc02007-10-26 13:51:49 +020057};
58
59/* turbo parameter */
60static int boost = 100;
61static int clockmode = 1;
62static int cdmode = 1;
63static int iuu_cardin;
64static int iuu_cardout;
Rusty Russell90ab5ee2012-01-13 09:32:20 +103065static bool xmas;
Olivier Bornete55c6d02009-08-18 21:05:57 +020066static int vcc_default = 5;
Alain Degreffe60a8fc02007-10-26 13:51:49 +020067
68static void read_rxcmd_callback(struct urb *urb);
69
70struct iuu_private {
71 spinlock_t lock; /* store irq state */
72 wait_queue_head_t delta_msr_wait;
Alain Degreffe60a8fc02007-10-26 13:51:49 +020073 u8 line_status;
Alain Degreffe60a8fc02007-10-26 13:51:49 +020074 int tiostatus; /* store IUART SIGNAL for tiocmget call */
75 u8 reset; /* if 1 reset is needed */
76 int poll; /* number of poll */
77 u8 *writebuf; /* buffer for writing to device */
78 int writelen; /* num of byte to write to device */
79 u8 *buf; /* used for initialize speed */
80 u8 *dbgbuf; /* debug buffer */
81 u8 len;
Olivier Bornet20eda942009-08-18 21:05:55 +020082 int vcc; /* vcc (either 3 or 5 V) */
James Courtier-Dutton89b54392010-05-21 11:53:25 +010083 u32 baud;
84 u32 boost;
85 u32 clk;
Alain Degreffe60a8fc02007-10-26 13:51:49 +020086};
87
88
89static void iuu_free_buf(struct iuu_private *priv)
90{
91 kfree(priv->buf);
92 kfree(priv->dbgbuf);
93 kfree(priv->writebuf);
94}
95
96static int iuu_alloc_buf(struct iuu_private *priv)
97{
98 priv->buf = kzalloc(256, GFP_KERNEL);
99 priv->dbgbuf = kzalloc(256, GFP_KERNEL);
100 priv->writebuf = kzalloc(256, GFP_KERNEL);
101 if (!priv->buf || !priv->dbgbuf || !priv->writebuf) {
102 iuu_free_buf(priv);
Harvey Harrison441b62c2008-03-03 16:08:34 -0800103 dbg("%s problem allocation buffer", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200104 return -ENOMEM;
105 }
Harvey Harrison441b62c2008-03-03 16:08:34 -0800106 dbg("%s - Privates buffers allocation success", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200107 return 0;
108}
109
110static int iuu_startup(struct usb_serial *serial)
111{
112 struct iuu_private *priv;
113 priv = kzalloc(sizeof(struct iuu_private), GFP_KERNEL);
Harvey Harrison441b62c2008-03-03 16:08:34 -0800114 dbg("%s- priv allocation success", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200115 if (!priv)
116 return -ENOMEM;
117 if (iuu_alloc_buf(priv)) {
118 kfree(priv);
119 return -ENOMEM;
120 }
Olivier Bornete55c6d02009-08-18 21:05:57 +0200121 priv->vcc = vcc_default;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200122 spin_lock_init(&priv->lock);
123 init_waitqueue_head(&priv->delta_msr_wait);
124 usb_set_serial_port_data(serial->port[0], priv);
125 return 0;
126}
127
Alan Sternf9c99bb2009-06-02 11:53:55 -0400128/* Release function */
129static void iuu_release(struct usb_serial *serial)
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200130{
131 struct usb_serial_port *port = serial->port[0];
132 struct iuu_private *priv = usb_get_serial_port_data(port);
133 if (!port)
134 return;
135
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200136 if (priv) {
137 iuu_free_buf(priv);
Harvey Harrison441b62c2008-03-03 16:08:34 -0800138 dbg("%s - I will free all", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200139 usb_set_serial_port_data(port, NULL);
140
Harvey Harrison441b62c2008-03-03 16:08:34 -0800141 dbg("%s - priv is not anymore in port structure", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200142 kfree(priv);
143
Harvey Harrison441b62c2008-03-03 16:08:34 -0800144 dbg("%s priv is now kfree", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200145 }
146}
147
Alan Cox20b9d172011-02-14 16:26:50 +0000148static int iuu_tiocmset(struct tty_struct *tty,
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200149 unsigned int set, unsigned int clear)
150{
Alan Cox95da3102008-07-22 11:09:07 +0100151 struct usb_serial_port *port = tty->driver_data;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200152 struct iuu_private *priv = usb_get_serial_port_data(port);
Alan Cox7b1fc8b2008-02-20 21:39:25 +0000153 unsigned long flags;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200154
Alan Cox7b1fc8b2008-02-20 21:39:25 +0000155 /* FIXME: locking on tiomstatus */
Harvey Harrison441b62c2008-03-03 16:08:34 -0800156 dbg("%s (%d) msg : SET = 0x%04x, CLEAR = 0x%04x ", __func__,
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200157 port->number, set, clear);
Alan Cox7b1fc8b2008-02-20 21:39:25 +0000158
159 spin_lock_irqsave(&priv->lock, flags);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200160
James Courtier-Dutton89b54392010-05-21 11:53:25 +0100161 if ((set & TIOCM_RTS) && !(priv->tiostatus == TIOCM_RTS)) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800162 dbg("%s TIOCMSET RESET called !!!", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200163 priv->reset = 1;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200164 }
James Courtier-Dutton89b54392010-05-21 11:53:25 +0100165 if (set & TIOCM_RTS)
166 priv->tiostatus = TIOCM_RTS;
167
Alan Cox7b1fc8b2008-02-20 21:39:25 +0000168 spin_unlock_irqrestore(&priv->lock, flags);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200169 return 0;
170}
171
172/* This is used to provide a carrier detect mechanism
173 * When a card is present, the response is 0x00
174 * When no card , the reader respond with TIOCM_CD
175 * This is known as CD autodetect mechanism
176 */
Alan Cox60b33c12011-02-14 16:26:14 +0000177static int iuu_tiocmget(struct tty_struct *tty)
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200178{
Alan Cox95da3102008-07-22 11:09:07 +0100179 struct usb_serial_port *port = tty->driver_data;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200180 struct iuu_private *priv = usb_get_serial_port_data(port);
Alan Cox7b1fc8b2008-02-20 21:39:25 +0000181 unsigned long flags;
182 int rc;
183
184 spin_lock_irqsave(&priv->lock, flags);
185 rc = priv->tiostatus;
186 spin_unlock_irqrestore(&priv->lock, flags);
187
188 return rc;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200189}
190
191static void iuu_rxcmd(struct urb *urb)
192{
Ming Leicdc97792008-02-24 18:41:47 +0800193 struct usb_serial_port *port = urb->context;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200194 int result;
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800195 int status = urb->status;
196
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800197 if (status) {
198 dbg("%s - status = %d", __func__, status);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200199 /* error stop all */
200 return;
201 }
202
203
204 memset(port->write_urb->transfer_buffer, IUU_UART_RX, 1);
205 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
206 usb_sndbulkpipe(port->serial->dev,
207 port->bulk_out_endpointAddress),
208 port->write_urb->transfer_buffer, 1,
209 read_rxcmd_callback, port);
210 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
211}
212
213static int iuu_reset(struct usb_serial_port *port, u8 wt)
214{
215 struct iuu_private *priv = usb_get_serial_port_data(port);
216 int result;
217 char *buf_ptr = port->write_urb->transfer_buffer;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200218
219 /* Prepare the reset sequence */
220
221 *buf_ptr++ = IUU_RST_SET;
222 *buf_ptr++ = IUU_DELAY_MS;
223 *buf_ptr++ = wt;
224 *buf_ptr = IUU_RST_CLEAR;
225
226 /* send the sequence */
227
228 usb_fill_bulk_urb(port->write_urb,
229 port->serial->dev,
230 usb_sndbulkpipe(port->serial->dev,
231 port->bulk_out_endpointAddress),
232 port->write_urb->transfer_buffer, 4, iuu_rxcmd, port);
233 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
234 priv->reset = 0;
235 return result;
236}
237
238/* Status Function
239 * Return value is
240 * 0x00 = no card
241 * 0x01 = smartcard
242 * 0x02 = sim card
243 */
244static void iuu_update_status_callback(struct urb *urb)
245{
Ming Leicdc97792008-02-24 18:41:47 +0800246 struct usb_serial_port *port = urb->context;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200247 struct iuu_private *priv = usb_get_serial_port_data(port);
248 u8 *st;
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800249 int status = urb->status;
250
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800251 if (status) {
252 dbg("%s - status = %d", __func__, status);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200253 /* error stop all */
254 return;
255 }
256
257 st = urb->transfer_buffer;
Harvey Harrison441b62c2008-03-03 16:08:34 -0800258 dbg("%s - enter", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200259 if (urb->actual_length == 1) {
260 switch (st[0]) {
261 case 0x1:
262 priv->tiostatus = iuu_cardout;
263 break;
264 case 0x0:
265 priv->tiostatus = iuu_cardin;
266 break;
267 default:
268 priv->tiostatus = iuu_cardin;
269 }
270 }
271 iuu_rxcmd(urb);
272}
273
274static void iuu_status_callback(struct urb *urb)
275{
Ming Leicdc97792008-02-24 18:41:47 +0800276 struct usb_serial_port *port = urb->context;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200277 int result;
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800278 int status = urb->status;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200279
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800280 dbg("%s - status = %d", __func__, status);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200281 usb_fill_bulk_urb(port->read_urb, port->serial->dev,
282 usb_rcvbulkpipe(port->serial->dev,
283 port->bulk_in_endpointAddress),
284 port->read_urb->transfer_buffer, 256,
285 iuu_update_status_callback, port);
286 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
287}
288
289static int iuu_status(struct usb_serial_port *port)
290{
291 int result;
292
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200293 memset(port->write_urb->transfer_buffer, IUU_GET_STATE_REGISTER, 1);
294 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
295 usb_sndbulkpipe(port->serial->dev,
296 port->bulk_out_endpointAddress),
297 port->write_urb->transfer_buffer, 1,
298 iuu_status_callback, port);
299 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
300 return result;
301
302}
303
304static int bulk_immediate(struct usb_serial_port *port, u8 *buf, u8 count)
305{
306 int status;
307 struct usb_serial *serial = port->serial;
308 int actual = 0;
309
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200310 /* send the data out the bulk port */
311
312 status =
313 usb_bulk_msg(serial->dev,
314 usb_sndbulkpipe(serial->dev,
315 port->bulk_out_endpointAddress), buf,
316 count, &actual, HZ * 1);
317
Alan Cox9e8e2d22008-07-22 11:12:59 +0100318 if (status != IUU_OPERATION_OK)
Harvey Harrison441b62c2008-03-03 16:08:34 -0800319 dbg("%s - error = %2x", __func__, status);
Alan Cox9e8e2d22008-07-22 11:12:59 +0100320 else
Harvey Harrison441b62c2008-03-03 16:08:34 -0800321 dbg("%s - write OK !", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200322 return status;
323}
324
325static int read_immediate(struct usb_serial_port *port, u8 *buf, u8 count)
326{
327 int status;
328 struct usb_serial *serial = port->serial;
329 int actual = 0;
330
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200331 /* send the data out the bulk port */
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200332 status =
333 usb_bulk_msg(serial->dev,
334 usb_rcvbulkpipe(serial->dev,
335 port->bulk_in_endpointAddress), buf,
336 count, &actual, HZ * 1);
337
Alan Cox9e8e2d22008-07-22 11:12:59 +0100338 if (status != IUU_OPERATION_OK)
Harvey Harrison441b62c2008-03-03 16:08:34 -0800339 dbg("%s - error = %2x", __func__, status);
Alan Cox9e8e2d22008-07-22 11:12:59 +0100340 else
Harvey Harrison441b62c2008-03-03 16:08:34 -0800341 dbg("%s - read OK !", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200342 return status;
343}
344
345static int iuu_led(struct usb_serial_port *port, unsigned int R,
346 unsigned int G, unsigned int B, u8 f)
347{
348 int status;
349 u8 *buf;
350 buf = kmalloc(8, GFP_KERNEL);
351 if (!buf)
352 return -ENOMEM;
353
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200354 buf[0] = IUU_SET_LED;
355 buf[1] = R & 0xFF;
356 buf[2] = (R >> 8) & 0xFF;
357 buf[3] = G & 0xFF;
358 buf[4] = (G >> 8) & 0xFF;
359 buf[5] = B & 0xFF;
360 buf[6] = (B >> 8) & 0xFF;
361 buf[7] = f;
362 status = bulk_immediate(port, buf, 8);
363 kfree(buf);
364 if (status != IUU_OPERATION_OK)
Harvey Harrison441b62c2008-03-03 16:08:34 -0800365 dbg("%s - led error status = %2x", __func__, status);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200366 else
Harvey Harrison441b62c2008-03-03 16:08:34 -0800367 dbg("%s - led OK !", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200368 return IUU_OPERATION_OK;
369}
370
371static void iuu_rgbf_fill_buffer(u8 *buf, u8 r1, u8 r2, u8 g1, u8 g2, u8 b1,
372 u8 b2, u8 freq)
373{
374 *buf++ = IUU_SET_LED;
375 *buf++ = r1;
376 *buf++ = r2;
377 *buf++ = g1;
378 *buf++ = g2;
379 *buf++ = b1;
380 *buf++ = b2;
381 *buf = freq;
382}
383
384static void iuu_led_activity_on(struct urb *urb)
385{
Ming Leicdc97792008-02-24 18:41:47 +0800386 struct usb_serial_port *port = urb->context;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200387 int result;
388 char *buf_ptr = port->write_urb->transfer_buffer;
389 *buf_ptr++ = IUU_SET_LED;
390 if (xmas == 1) {
391 get_random_bytes(buf_ptr, 6);
392 *(buf_ptr+7) = 1;
393 } else {
394 iuu_rgbf_fill_buffer(buf_ptr, 255, 255, 0, 0, 0, 0, 255);
395 }
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
405static void iuu_led_activity_off(struct urb *urb)
406{
Ming Leicdc97792008-02-24 18:41:47 +0800407 struct usb_serial_port *port = urb->context;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200408 int result;
409 char *buf_ptr = port->write_urb->transfer_buffer;
410 if (xmas == 1) {
411 iuu_rxcmd(urb);
412 return;
413 } else {
414 *buf_ptr++ = IUU_SET_LED;
415 iuu_rgbf_fill_buffer(buf_ptr, 0, 0, 255, 255, 0, 0, 255);
416 }
417 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
418 usb_sndbulkpipe(port->serial->dev,
419 port->bulk_out_endpointAddress),
420 port->write_urb->transfer_buffer, 8 ,
421 iuu_rxcmd, port);
422 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
423}
424
425
426
427static int iuu_clk(struct usb_serial_port *port, int dwFrq)
428{
429 int status;
430 struct iuu_private *priv = usb_get_serial_port_data(port);
431 int Count = 0;
432 u8 FrqGenAdr = 0x69;
433 u8 DIV = 0; /* 8bit */
434 u8 XDRV = 0; /* 8bit */
435 u8 PUMP = 0; /* 3bit */
436 u8 PBmsb = 0; /* 2bit */
437 u8 PBlsb = 0; /* 8bit */
438 u8 PO = 0; /* 1bit */
439 u8 Q = 0; /* 7bit */
440 /* 24bit = 3bytes */
441 unsigned int P = 0;
442 unsigned int P2 = 0;
443 int frq = (int)dwFrq;
444
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200445 if (frq == 0) {
446 priv->buf[Count++] = IUU_UART_WRITE_I2C;
447 priv->buf[Count++] = FrqGenAdr << 1;
448 priv->buf[Count++] = 0x09;
449 priv->buf[Count++] = 0x00;
450
451 status = bulk_immediate(port, (u8 *) priv->buf, Count);
452 if (status != 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800453 dbg("%s - write error ", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200454 return status;
455 }
456 } else if (frq == 3579000) {
457 DIV = 100;
458 P = 1193;
459 Q = 40;
460 XDRV = 0;
461 } else if (frq == 3680000) {
462 DIV = 105;
463 P = 161;
464 Q = 5;
465 XDRV = 0;
466 } else if (frq == 6000000) {
467 DIV = 66;
468 P = 66;
469 Q = 2;
470 XDRV = 0x28;
471 } else {
472 unsigned int result = 0;
473 unsigned int tmp = 0;
474 unsigned int check;
475 unsigned int check2;
476 char found = 0x00;
477 unsigned int lQ = 2;
478 unsigned int lP = 2055;
479 unsigned int lDiv = 4;
480
481 for (lQ = 2; lQ <= 47 && !found; lQ++)
482 for (lP = 2055; lP >= 8 && !found; lP--)
483 for (lDiv = 4; lDiv <= 127 && !found; lDiv++) {
484 tmp = (12000000 / lDiv) * (lP / lQ);
485 if (abs((int)(tmp - frq)) <
486 abs((int)(frq - result))) {
487 check2 = (12000000 / lQ);
488 if (check2 < 250000)
489 continue;
490 check = (12000000 / lQ) * lP;
491 if (check > 400000000)
492 continue;
493 if (check < 100000000)
494 continue;
495 if (lDiv < 4 || lDiv > 127)
496 continue;
497 result = tmp;
498 P = lP;
499 DIV = lDiv;
500 Q = lQ;
501 if (result == frq)
502 found = 0x01;
503 }
504 }
505 }
506 P2 = ((P - PO) / 2) - 4;
507 DIV = DIV;
508 PUMP = 0x04;
509 PBmsb = (P2 >> 8 & 0x03);
510 PBlsb = P2 & 0xFF;
511 PO = (P >> 10) & 0x01;
512 Q = Q - 2;
513
514 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
515 priv->buf[Count++] = FrqGenAdr << 1;
516 priv->buf[Count++] = 0x09;
517 priv->buf[Count++] = 0x20; /* Adr = 0x09 */
518 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
519 priv->buf[Count++] = FrqGenAdr << 1;
520 priv->buf[Count++] = 0x0C;
521 priv->buf[Count++] = DIV; /* Adr = 0x0C */
522 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
523 priv->buf[Count++] = FrqGenAdr << 1;
524 priv->buf[Count++] = 0x12;
525 priv->buf[Count++] = XDRV; /* Adr = 0x12 */
526 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
527 priv->buf[Count++] = FrqGenAdr << 1;
528 priv->buf[Count++] = 0x13;
529 priv->buf[Count++] = 0x6B; /* Adr = 0x13 */
530 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
531 priv->buf[Count++] = FrqGenAdr << 1;
532 priv->buf[Count++] = 0x40;
533 priv->buf[Count++] = (0xC0 | ((PUMP & 0x07) << 2)) |
534 (PBmsb & 0x03); /* Adr = 0x40 */
535 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
536 priv->buf[Count++] = FrqGenAdr << 1;
537 priv->buf[Count++] = 0x41;
538 priv->buf[Count++] = PBlsb; /* Adr = 0x41 */
539 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
540 priv->buf[Count++] = FrqGenAdr << 1;
541 priv->buf[Count++] = 0x42;
542 priv->buf[Count++] = Q | (((PO & 0x01) << 7)); /* Adr = 0x42 */
543 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
544 priv->buf[Count++] = FrqGenAdr << 1;
545 priv->buf[Count++] = 0x44;
546 priv->buf[Count++] = (char)0xFF; /* Adr = 0x44 */
547 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
548 priv->buf[Count++] = FrqGenAdr << 1;
549 priv->buf[Count++] = 0x45;
550 priv->buf[Count++] = (char)0xFE; /* Adr = 0x45 */
551 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
552 priv->buf[Count++] = FrqGenAdr << 1;
553 priv->buf[Count++] = 0x46;
554 priv->buf[Count++] = 0x7F; /* Adr = 0x46 */
555 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
556 priv->buf[Count++] = FrqGenAdr << 1;
557 priv->buf[Count++] = 0x47;
558 priv->buf[Count++] = (char)0x84; /* Adr = 0x47 */
559
560 status = bulk_immediate(port, (u8 *) priv->buf, Count);
561 if (status != IUU_OPERATION_OK)
Harvey Harrison441b62c2008-03-03 16:08:34 -0800562 dbg("%s - write error ", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200563 return status;
564}
565
566static int iuu_uart_flush(struct usb_serial_port *port)
567{
568 int i;
569 int status;
570 u8 rxcmd = IUU_UART_RX;
571 struct iuu_private *priv = usb_get_serial_port_data(port);
572
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200573 if (iuu_led(port, 0xF000, 0, 0, 0xFF) < 0)
574 return -EIO;
575
576 for (i = 0; i < 2; i++) {
577 status = bulk_immediate(port, &rxcmd, 1);
578 if (status != IUU_OPERATION_OK) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800579 dbg("%s - uart_flush_write error", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200580 return status;
581 }
582
583 status = read_immediate(port, &priv->len, 1);
584 if (status != IUU_OPERATION_OK) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800585 dbg("%s - uart_flush_read error", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200586 return status;
587 }
588
589 if (priv->len > 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800590 dbg("%s - uart_flush datalen is : %i ", __func__,
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200591 priv->len);
592 status = read_immediate(port, priv->buf, priv->len);
593 if (status != IUU_OPERATION_OK) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800594 dbg("%s - uart_flush_read error", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200595 return status;
596 }
597 }
598 }
Harvey Harrison441b62c2008-03-03 16:08:34 -0800599 dbg("%s - uart_flush_read OK!", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200600 iuu_led(port, 0, 0xF000, 0, 0xFF);
601 return status;
602}
603
604static void read_buf_callback(struct urb *urb)
605{
Ming Leicdc97792008-02-24 18:41:47 +0800606 struct usb_serial_port *port = urb->context;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200607 unsigned char *data = urb->transfer_buffer;
608 struct tty_struct *tty;
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800609 int status = urb->status;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200610
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800611 if (status) {
612 if (status == -EPROTO) {
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200613 /* reschedule needed */
614 }
615 return;
616 }
617
Harvey Harrison441b62c2008-03-03 16:08:34 -0800618 dbg("%s - %i chars to write", __func__, urb->actual_length);
Alan Cox4a90f092008-10-13 10:39:46 +0100619 tty = tty_port_tty_get(&port->port);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200620 if (data == NULL)
Harvey Harrison441b62c2008-03-03 16:08:34 -0800621 dbg("%s - data is NULL !!!", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200622 if (tty && urb->actual_length && data) {
623 tty_insert_flip_string(tty, data, urb->actual_length);
624 tty_flip_buffer_push(tty);
625 }
Alan Cox4a90f092008-10-13 10:39:46 +0100626 tty_kref_put(tty);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200627 iuu_led_activity_on(urb);
628}
629
630static int iuu_bulk_write(struct usb_serial_port *port)
631{
632 struct iuu_private *priv = usb_get_serial_port_data(port);
Steven Rostedt85943032008-05-06 20:42:31 -0700633 unsigned long flags;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200634 int result;
635 int i;
Olivier Bornet5fcf62b2009-06-11 12:52:26 +0100636 int buf_len;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200637 char *buf_ptr = port->write_urb->transfer_buffer;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200638
Olivier Bornet5fcf62b2009-06-11 12:52:26 +0100639 spin_lock_irqsave(&priv->lock, flags);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200640 *buf_ptr++ = IUU_UART_ESC;
641 *buf_ptr++ = IUU_UART_TX;
642 *buf_ptr++ = priv->writelen;
643
Olivier Bornet5fcf62b2009-06-11 12:52:26 +0100644 memcpy(buf_ptr, priv->writebuf, priv->writelen);
645 buf_len = priv->writelen;
646 priv->writelen = 0;
647 spin_unlock_irqrestore(&priv->lock, flags);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200648 if (debug == 1) {
Olivier Bornet5fcf62b2009-06-11 12:52:26 +0100649 for (i = 0; i < buf_len; i++)
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200650 sprintf(priv->dbgbuf + i*2 ,
651 "%02X", priv->writebuf[i]);
Olivier Bornet5fcf62b2009-06-11 12:52:26 +0100652 priv->dbgbuf[buf_len+i*2] = 0;
Harvey Harrison441b62c2008-03-03 16:08:34 -0800653 dbg("%s - writing %i chars : %s", __func__,
Olivier Bornet5fcf62b2009-06-11 12:52:26 +0100654 buf_len, priv->dbgbuf);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200655 }
656 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
657 usb_sndbulkpipe(port->serial->dev,
658 port->bulk_out_endpointAddress),
Olivier Bornet5fcf62b2009-06-11 12:52:26 +0100659 port->write_urb->transfer_buffer, buf_len + 3,
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200660 iuu_rxcmd, port);
661 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200662 usb_serial_port_softint(port);
663 return result;
664}
665
666static int iuu_read_buf(struct usb_serial_port *port, int len)
667{
668 int result;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200669
670 usb_fill_bulk_urb(port->read_urb, port->serial->dev,
671 usb_rcvbulkpipe(port->serial->dev,
672 port->bulk_in_endpointAddress),
673 port->read_urb->transfer_buffer, len,
674 read_buf_callback, port);
675 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
676 return result;
677}
678
679static void iuu_uart_read_callback(struct urb *urb)
680{
Ming Leicdc97792008-02-24 18:41:47 +0800681 struct usb_serial_port *port = urb->context;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200682 struct iuu_private *priv = usb_get_serial_port_data(port);
Steven Rostedt85943032008-05-06 20:42:31 -0700683 unsigned long flags;
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800684 int status = urb->status;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200685 int error = 0;
686 int len = 0;
687 unsigned char *data = urb->transfer_buffer;
688 priv->poll++;
689
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800690 if (status) {
691 dbg("%s - status = %d", __func__, status);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200692 /* error stop all */
693 return;
694 }
695 if (data == NULL)
Harvey Harrison441b62c2008-03-03 16:08:34 -0800696 dbg("%s - data is NULL !!!", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200697
698 if (urb->actual_length == 1 && data != NULL)
699 len = (int) data[0];
700
701 if (urb->actual_length > 1) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800702 dbg("%s - urb->actual_length = %i", __func__,
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200703 urb->actual_length);
704 error = 1;
705 return;
706 }
707 /* if len > 0 call readbuf */
708
709 if (len > 0 && error == 0) {
710 dbg("%s - call read buf - len to read is %i ",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800711 __func__, len);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200712 status = iuu_read_buf(port, len);
713 return;
714 }
715 /* need to update status ? */
716 if (priv->poll > 99) {
717 status = iuu_status(port);
718 priv->poll = 0;
719 return;
720 }
721
722 /* reset waiting ? */
723
724 if (priv->reset == 1) {
725 status = iuu_reset(port, 0xC);
726 return;
727 }
728 /* Writebuf is waiting */
729 spin_lock_irqsave(&priv->lock, flags);
730 if (priv->writelen > 0) {
731 spin_unlock_irqrestore(&priv->lock, flags);
732 status = iuu_bulk_write(port);
733 return;
734 }
735 spin_unlock_irqrestore(&priv->lock, flags);
736 /* if nothing to write call again rxcmd */
Harvey Harrison441b62c2008-03-03 16:08:34 -0800737 dbg("%s - rxcmd recall", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200738 iuu_led_activity_off(urb);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200739}
740
Alan Cox95da3102008-07-22 11:09:07 +0100741static int iuu_uart_write(struct tty_struct *tty, struct usb_serial_port *port,
742 const u8 *buf, int count)
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200743{
744 struct iuu_private *priv = usb_get_serial_port_data(port);
Steven Rostedt85943032008-05-06 20:42:31 -0700745 unsigned long flags;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200746
747 if (count > 256)
748 return -ENOMEM;
749
750 spin_lock_irqsave(&priv->lock, flags);
Olivier Bornet5fcf62b2009-06-11 12:52:26 +0100751
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200752 /* fill the buffer */
Olivier Bornet5fcf62b2009-06-11 12:52:26 +0100753 memcpy(priv->writebuf + priv->writelen, buf, count);
754 priv->writelen += count;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200755 spin_unlock_irqrestore(&priv->lock, flags);
756
Alan Cox9e8e2d22008-07-22 11:12:59 +0100757 return count;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200758}
759
760static void read_rxcmd_callback(struct urb *urb)
761{
Ming Leicdc97792008-02-24 18:41:47 +0800762 struct usb_serial_port *port = urb->context;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200763 int result;
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800764 int status = urb->status;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200765
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800766 if (status) {
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200767 /* error stop all */
768 return;
769 }
770
771 usb_fill_bulk_urb(port->read_urb, port->serial->dev,
772 usb_rcvbulkpipe(port->serial->dev,
773 port->bulk_in_endpointAddress),
774 port->read_urb->transfer_buffer, 256,
775 iuu_uart_read_callback, port);
776 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
Harvey Harrison441b62c2008-03-03 16:08:34 -0800777 dbg("%s - submit result = %d", __func__, result);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200778}
779
780static int iuu_uart_on(struct usb_serial_port *port)
781{
782 int status;
783 u8 *buf;
784
785 buf = kmalloc(sizeof(u8) * 4, GFP_KERNEL);
786
787 if (!buf)
788 return -ENOMEM;
789
790 buf[0] = IUU_UART_ENABLE;
791 buf[1] = (u8) ((IUU_BAUD_9600 >> 8) & 0x00FF);
792 buf[2] = (u8) (0x00FF & IUU_BAUD_9600);
Olivier Bornetcc3447d2009-06-11 12:53:24 +0100793 buf[3] = (u8) (0x0F0 & IUU_ONE_STOP_BIT) | (0x07 & IUU_PARITY_EVEN);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200794
795 status = bulk_immediate(port, buf, 4);
796 if (status != IUU_OPERATION_OK) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800797 dbg("%s - uart_on error", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200798 goto uart_enable_failed;
799 }
800 /* iuu_reset() the card after iuu_uart_on() */
801 status = iuu_uart_flush(port);
802 if (status != IUU_OPERATION_OK)
Harvey Harrison441b62c2008-03-03 16:08:34 -0800803 dbg("%s - uart_flush error", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200804uart_enable_failed:
805 kfree(buf);
806 return status;
807}
808
809/* Diables the IUU UART (a.k.a. the Phoenix voiderface) */
810static int iuu_uart_off(struct usb_serial_port *port)
811{
812 int status;
813 u8 *buf;
814 buf = kmalloc(1, GFP_KERNEL);
815 if (!buf)
816 return -ENOMEM;
817 buf[0] = IUU_UART_DISABLE;
818
819 status = bulk_immediate(port, buf, 1);
820 if (status != IUU_OPERATION_OK)
Harvey Harrison441b62c2008-03-03 16:08:34 -0800821 dbg("%s - uart_off error", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200822
823 kfree(buf);
824 return status;
825}
826
James Courtier-Dutton89b54392010-05-21 11:53:25 +0100827static int iuu_uart_baud(struct usb_serial_port *port, u32 baud_base,
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200828 u32 *actual, u8 parity)
829{
830 int status;
James Courtier-Dutton89b54392010-05-21 11:53:25 +0100831 u32 baud;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200832 u8 *dataout;
833 u8 DataCount = 0;
834 u8 T1Frekvens = 0;
835 u8 T1reload = 0;
836 unsigned int T1FrekvensHZ = 0;
837
James Courtier-Dutton89b54392010-05-21 11:53:25 +0100838 dbg("%s - enter baud_base=%d", __func__, baud_base);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200839 dataout = kmalloc(sizeof(u8) * 5, GFP_KERNEL);
840
841 if (!dataout)
842 return -ENOMEM;
James Courtier-Dutton89b54392010-05-21 11:53:25 +0100843 /*baud = (((priv->clk / 35) * baud_base) / 100000); */
844 baud = baud_base;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200845
846 if (baud < 1200 || baud > 230400) {
847 kfree(dataout);
848 return IUU_INVALID_PARAMETER;
849 }
850 if (baud > 977) {
851 T1Frekvens = 3;
852 T1FrekvensHZ = 500000;
853 }
854
855 if (baud > 3906) {
856 T1Frekvens = 2;
857 T1FrekvensHZ = 2000000;
858 }
859
860 if (baud > 11718) {
861 T1Frekvens = 1;
862 T1FrekvensHZ = 6000000;
863 }
864
865 if (baud > 46875) {
866 T1Frekvens = 0;
867 T1FrekvensHZ = 24000000;
868 }
869
870 T1reload = 256 - (u8) (T1FrekvensHZ / (baud * 2));
871
872 /* magic number here: ENTER_FIRMWARE_UPDATE; */
873 dataout[DataCount++] = IUU_UART_ESC;
874 /* magic number here: CHANGE_BAUD; */
875 dataout[DataCount++] = IUU_UART_CHANGE;
876 dataout[DataCount++] = T1Frekvens;
877 dataout[DataCount++] = T1reload;
878
879 *actual = (T1FrekvensHZ / (256 - T1reload)) / 2;
880
881 switch (parity & 0x0F) {
882 case IUU_PARITY_NONE:
883 dataout[DataCount++] = 0x00;
884 break;
885 case IUU_PARITY_EVEN:
886 dataout[DataCount++] = 0x01;
887 break;
888 case IUU_PARITY_ODD:
889 dataout[DataCount++] = 0x02;
890 break;
891 case IUU_PARITY_MARK:
892 dataout[DataCount++] = 0x03;
893 break;
894 case IUU_PARITY_SPACE:
895 dataout[DataCount++] = 0x04;
896 break;
897 default:
898 kfree(dataout);
899 return IUU_INVALID_PARAMETER;
900 break;
901 }
902
903 switch (parity & 0xF0) {
904 case IUU_ONE_STOP_BIT:
905 dataout[DataCount - 1] |= IUU_ONE_STOP_BIT;
906 break;
907
908 case IUU_TWO_STOP_BITS:
909 dataout[DataCount - 1] |= IUU_TWO_STOP_BITS;
910 break;
911 default:
912 kfree(dataout);
913 return IUU_INVALID_PARAMETER;
914 break;
915 }
916
917 status = bulk_immediate(port, dataout, DataCount);
918 if (status != IUU_OPERATION_OK)
Harvey Harrison441b62c2008-03-03 16:08:34 -0800919 dbg("%s - uart_off error", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200920 kfree(dataout);
921 return status;
922}
923
Olivier Bornet96dab772009-06-11 12:54:20 +0100924static void iuu_set_termios(struct tty_struct *tty,
925 struct usb_serial_port *port, struct ktermios *old_termios)
926{
927 const u32 supported_mask = CMSPAR|PARENB|PARODD;
James Courtier-Dutton89b54392010-05-21 11:53:25 +0100928 struct iuu_private *priv = usb_get_serial_port_data(port);
Olivier Bornet96dab772009-06-11 12:54:20 +0100929 unsigned int cflag = tty->termios->c_cflag;
930 int status;
931 u32 actual;
932 u32 parity;
933 int csize = CS7;
James Courtier-Dutton89b54392010-05-21 11:53:25 +0100934 int baud;
Olivier Bornet96dab772009-06-11 12:54:20 +0100935 u32 newval = cflag & supported_mask;
936
James Courtier-Dutton89b54392010-05-21 11:53:25 +0100937 /* Just use the ospeed. ispeed should be the same. */
938 baud = tty->termios->c_ospeed;
939
940 dbg("%s - enter c_ospeed or baud=%d", __func__, baud);
941
Olivier Bornet96dab772009-06-11 12:54:20 +0100942 /* compute the parity parameter */
943 parity = 0;
944 if (cflag & CMSPAR) { /* Using mark space */
945 if (cflag & PARODD)
946 parity |= IUU_PARITY_SPACE;
947 else
948 parity |= IUU_PARITY_MARK;
949 } else if (!(cflag & PARENB)) {
950 parity |= IUU_PARITY_NONE;
951 csize = CS8;
952 } else if (cflag & PARODD)
953 parity |= IUU_PARITY_ODD;
954 else
955 parity |= IUU_PARITY_EVEN;
956
957 parity |= (cflag & CSTOPB ? IUU_TWO_STOP_BITS : IUU_ONE_STOP_BIT);
958
959 /* set it */
960 status = iuu_uart_baud(port,
James Courtier-Dutton89b54392010-05-21 11:53:25 +0100961 baud * priv->boost / 100,
Olivier Bornet96dab772009-06-11 12:54:20 +0100962 &actual, parity);
963
964 /* set the termios value to the real one, so the user now what has
965 * changed. We support few fields so its easies to copy the old hw
966 * settings back over and then adjust them
967 */
James Courtier-Dutton89b54392010-05-21 11:53:25 +0100968 if (old_termios)
969 tty_termios_copy_hw(tty->termios, old_termios);
Olivier Bornet96dab772009-06-11 12:54:20 +0100970 if (status != 0) /* Set failed - return old bits */
971 return;
972 /* Re-encode speed, parity and csize */
973 tty_encode_baud_rate(tty, baud, baud);
974 tty->termios->c_cflag &= ~(supported_mask|CSIZE);
975 tty->termios->c_cflag |= newval | csize;
976}
977
Alan Cox335f8512009-06-11 12:26:29 +0100978static void iuu_close(struct usb_serial_port *port)
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200979{
980 /* iuu_led (port,255,0,0,0); */
981 struct usb_serial *serial;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200982
983 serial = port->serial;
984 if (!serial)
985 return;
986
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200987 iuu_uart_off(port);
988 if (serial->dev) {
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200989 /* free writebuf */
990 /* shutdown our urbs */
Harvey Harrison441b62c2008-03-03 16:08:34 -0800991 dbg("%s - shutting down urbs", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200992 usb_kill_urb(port->write_urb);
993 usb_kill_urb(port->read_urb);
994 usb_kill_urb(port->interrupt_in_urb);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200995 iuu_led(port, 0, 0, 0xF000, 0xFF);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200996 }
997}
998
Alan Coxfe1ae7f2009-09-19 13:13:33 -0700999static void iuu_init_termios(struct tty_struct *tty)
1000{
1001 *(tty->termios) = tty_std_termios;
1002 tty->termios->c_cflag = CLOCAL | CREAD | CS8 | B9600
1003 | TIOCM_CTS | CSTOPB | PARENB;
1004 tty->termios->c_ispeed = 9600;
1005 tty->termios->c_ospeed = 9600;
1006 tty->termios->c_lflag = 0;
1007 tty->termios->c_oflag = 0;
1008 tty->termios->c_iflag = 0;
1009}
1010
Alan Coxa509a7e2009-09-19 13:13:26 -07001011static int iuu_open(struct tty_struct *tty, struct usb_serial_port *port)
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001012{
1013 struct usb_serial *serial = port->serial;
1014 u8 *buf;
1015 int result;
James Courtier-Dutton89b54392010-05-21 11:53:25 +01001016 int baud;
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001017 u32 actual;
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001018 struct iuu_private *priv = usb_get_serial_port_data(port);
1019
James Courtier-Dutton89b54392010-05-21 11:53:25 +01001020 baud = tty->termios->c_ospeed;
1021 tty->termios->c_ispeed = baud;
1022 /* Re-encode speed */
1023 tty_encode_baud_rate(tty, baud, baud);
1024
1025 dbg("%s - port %d, baud %d", __func__, port->number, baud);
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001026 usb_clear_halt(serial->dev, port->write_urb->pipe);
1027 usb_clear_halt(serial->dev, port->read_urb->pipe);
1028
1029 buf = kmalloc(10, GFP_KERNEL);
1030 if (buf == NULL)
1031 return -ENOMEM;
1032
Alan Coxfe1ae7f2009-09-19 13:13:33 -07001033 priv->poll = 0;
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001034
1035 /* initialize writebuf */
1036#define FISH(a, b, c, d) do { \
1037 result = usb_control_msg(port->serial->dev, \
1038 usb_rcvctrlpipe(port->serial->dev, 0), \
1039 b, a, c, d, buf, 1, 1000); \
1040 dbg("0x%x:0x%x:0x%x:0x%x %d - %x", a, b, c, d, result, \
1041 buf[0]); } while (0);
1042
1043#define SOUP(a, b, c, d) do { \
1044 result = usb_control_msg(port->serial->dev, \
1045 usb_sndctrlpipe(port->serial->dev, 0), \
1046 b, a, c, d, NULL, 0, 1000); \
1047 dbg("0x%x:0x%x:0x%x:0x%x %d", a, b, c, d, result); } while (0)
1048
1049 /* This is not UART related but IUU USB driver related or something */
1050 /* like that. Basically no IUU will accept any commands from the USB */
1051 /* host unless it has received the following message */
1052 /* sprintf(buf ,"%c%c%c%c",0x03,0x02,0x02,0x0); */
1053
1054 SOUP(0x03, 0x02, 0x02, 0x0);
1055 kfree(buf);
1056 iuu_led(port, 0xF000, 0xF000, 0, 0xFF);
1057 iuu_uart_on(port);
1058 if (boost < 100)
1059 boost = 100;
James Courtier-Dutton89b54392010-05-21 11:53:25 +01001060 priv->boost = boost;
1061 priv->baud = baud;
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001062 switch (clockmode) {
1063 case 2: /* 3.680 Mhz */
James Courtier-Dutton89b54392010-05-21 11:53:25 +01001064 priv->clk = IUU_CLK_3680000;
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001065 iuu_clk(port, IUU_CLK_3680000 * boost / 100);
1066 result =
James Courtier-Dutton89b54392010-05-21 11:53:25 +01001067 iuu_uart_baud(port, baud * boost / 100, &actual,
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001068 IUU_PARITY_EVEN);
1069 break;
1070 case 3: /* 6.00 Mhz */
1071 iuu_clk(port, IUU_CLK_6000000 * boost / 100);
James Courtier-Dutton89b54392010-05-21 11:53:25 +01001072 priv->clk = IUU_CLK_6000000;
1073 /* Ratio of 6000000 to 3500000 for baud 9600 */
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001074 result =
1075 iuu_uart_baud(port, 16457 * boost / 100, &actual,
1076 IUU_PARITY_EVEN);
1077 break;
1078 default: /* 3.579 Mhz */
1079 iuu_clk(port, IUU_CLK_3579000 * boost / 100);
James Courtier-Dutton89b54392010-05-21 11:53:25 +01001080 priv->clk = IUU_CLK_3579000;
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001081 result =
James Courtier-Dutton89b54392010-05-21 11:53:25 +01001082 iuu_uart_baud(port, baud * boost / 100, &actual,
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001083 IUU_PARITY_EVEN);
1084 }
1085
1086 /* set the cardin cardout signals */
1087 switch (cdmode) {
1088 case 0:
1089 iuu_cardin = 0;
1090 iuu_cardout = 0;
1091 break;
1092 case 1:
1093 iuu_cardin = TIOCM_CD;
1094 iuu_cardout = 0;
1095 break;
1096 case 2:
1097 iuu_cardin = 0;
1098 iuu_cardout = TIOCM_CD;
1099 break;
1100 case 3:
1101 iuu_cardin = TIOCM_DSR;
1102 iuu_cardout = 0;
1103 break;
1104 case 4:
1105 iuu_cardin = 0;
1106 iuu_cardout = TIOCM_DSR;
1107 break;
1108 case 5:
1109 iuu_cardin = TIOCM_CTS;
1110 iuu_cardout = 0;
1111 break;
1112 case 6:
1113 iuu_cardin = 0;
1114 iuu_cardout = TIOCM_CTS;
1115 break;
1116 case 7:
1117 iuu_cardin = TIOCM_RNG;
1118 iuu_cardout = 0;
1119 break;
1120 case 8:
1121 iuu_cardin = 0;
1122 iuu_cardout = TIOCM_RNG;
1123 }
1124
1125 iuu_uart_flush(port);
1126
Harvey Harrison441b62c2008-03-03 16:08:34 -08001127 dbg("%s - initialization done", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001128
1129 memset(port->write_urb->transfer_buffer, IUU_UART_RX, 1);
1130 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
1131 usb_sndbulkpipe(port->serial->dev,
1132 port->bulk_out_endpointAddress),
1133 port->write_urb->transfer_buffer, 1,
1134 read_rxcmd_callback, port);
1135 result = usb_submit_urb(port->write_urb, GFP_KERNEL);
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001136 if (result) {
1137 dev_err(&port->dev, "%s - failed submitting read urb,"
Harvey Harrison441b62c2008-03-03 16:08:34 -08001138 " error %d\n", __func__, result);
Alan Cox335f8512009-06-11 12:26:29 +01001139 iuu_close(port);
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001140 } else {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001141 dbg("%s - rxcmd OK", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001142 }
Johan Hovoldb58a6462011-11-10 14:58:30 +01001143
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001144 return result;
1145}
1146
Olivier Bornet20eda942009-08-18 21:05:55 +02001147/* how to change VCC */
1148static int iuu_vcc_set(struct usb_serial_port *port, unsigned int vcc)
1149{
1150 int status;
1151 u8 *buf;
1152
1153 buf = kmalloc(5, GFP_KERNEL);
1154 if (!buf)
1155 return -ENOMEM;
1156
Olivier Bornet20eda942009-08-18 21:05:55 +02001157 buf[0] = IUU_SET_VCC;
1158 buf[1] = vcc & 0xFF;
1159 buf[2] = (vcc >> 8) & 0xFF;
1160 buf[3] = (vcc >> 16) & 0xFF;
1161 buf[4] = (vcc >> 24) & 0xFF;
1162
1163 status = bulk_immediate(port, buf, 5);
1164 kfree(buf);
1165
1166 if (status != IUU_OPERATION_OK)
1167 dbg("%s - vcc error status = %2x", __func__, status);
1168 else
1169 dbg("%s - vcc OK !", __func__);
1170
1171 return status;
1172}
1173
1174/*
1175 * Sysfs Attributes
1176 */
1177
1178static ssize_t show_vcc_mode(struct device *dev,
1179 struct device_attribute *attr, char *buf)
1180{
1181 struct usb_serial_port *port = to_usb_serial_port(dev);
1182 struct iuu_private *priv = usb_get_serial_port_data(port);
1183
1184 return sprintf(buf, "%d\n", priv->vcc);
1185}
1186
1187static ssize_t store_vcc_mode(struct device *dev,
1188 struct device_attribute *attr, const char *buf, size_t count)
1189{
1190 struct usb_serial_port *port = to_usb_serial_port(dev);
1191 struct iuu_private *priv = usb_get_serial_port_data(port);
1192 unsigned long v;
1193
1194 if (strict_strtoul(buf, 10, &v)) {
1195 dev_err(dev, "%s - vcc_mode: %s is not a unsigned long\n",
1196 __func__, buf);
1197 goto fail_store_vcc_mode;
1198 }
1199
1200 dbg("%s: setting vcc_mode = %ld", __func__, v);
1201
1202 if ((v != 3) && (v != 5)) {
1203 dev_err(dev, "%s - vcc_mode %ld is invalid\n", __func__, v);
1204 } else {
1205 iuu_vcc_set(port, v);
1206 priv->vcc = v;
1207 }
1208fail_store_vcc_mode:
1209 return count;
1210}
1211
1212static DEVICE_ATTR(vcc_mode, S_IRUSR | S_IWUSR, show_vcc_mode,
1213 store_vcc_mode);
1214
1215static int iuu_create_sysfs_attrs(struct usb_serial_port *port)
1216{
Olivier Bornet20eda942009-08-18 21:05:55 +02001217 return device_create_file(&port->dev, &dev_attr_vcc_mode);
1218}
1219
1220static int iuu_remove_sysfs_attrs(struct usb_serial_port *port)
1221{
Olivier Bornet20eda942009-08-18 21:05:55 +02001222 device_remove_file(&port->dev, &dev_attr_vcc_mode);
1223 return 0;
1224}
1225
1226/*
1227 * End Sysfs Attributes
1228 */
1229
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001230static struct usb_serial_driver iuu_device = {
1231 .driver = {
1232 .owner = THIS_MODULE,
1233 .name = "iuu_phoenix",
1234 },
1235 .id_table = id_table,
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001236 .num_ports = 1,
Johan Hovoldbbcb2b92010-03-17 23:00:37 +01001237 .bulk_in_size = 512,
1238 .bulk_out_size = 512,
Olivier Bornet20eda942009-08-18 21:05:55 +02001239 .port_probe = iuu_create_sysfs_attrs,
1240 .port_remove = iuu_remove_sysfs_attrs,
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001241 .open = iuu_open,
1242 .close = iuu_close,
1243 .write = iuu_uart_write,
1244 .read_bulk_callback = iuu_uart_read_callback,
1245 .tiocmget = iuu_tiocmget,
1246 .tiocmset = iuu_tiocmset,
Olivier Bornet96dab772009-06-11 12:54:20 +01001247 .set_termios = iuu_set_termios,
Alan Coxfe1ae7f2009-09-19 13:13:33 -07001248 .init_termios = iuu_init_termios,
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001249 .attach = iuu_startup,
Alan Sternf9c99bb2009-06-02 11:53:55 -04001250 .release = iuu_release,
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001251};
1252
Alan Stern7dbe2462012-02-23 14:56:57 -05001253static struct usb_serial_driver * const serial_drivers[] = {
1254 &iuu_device, NULL
1255};
1256
Greg Kroah-Hartmand01bf412012-02-28 13:11:58 -08001257module_usb_serial_driver(iuu_driver, serial_drivers);
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001258
1259MODULE_AUTHOR("Alain Degreffe eczema@ecze.com");
1260
1261MODULE_DESCRIPTION(DRIVER_DESC);
1262MODULE_LICENSE("GPL");
1263
1264MODULE_VERSION(DRIVER_VERSION);
1265module_param(debug, bool, S_IRUGO | S_IWUSR);
1266MODULE_PARM_DESC(debug, "Debug enabled or not");
1267
1268module_param(xmas, bool, S_IRUGO | S_IWUSR);
Olivier Bornet8844a322009-08-18 21:05:54 +02001269MODULE_PARM_DESC(xmas, "Xmas colors enabled or not");
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001270
1271module_param(boost, int, S_IRUGO | S_IWUSR);
Olivier Bornet8844a322009-08-18 21:05:54 +02001272MODULE_PARM_DESC(boost, "Card overclock boost (in percent 100-500)");
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001273
1274module_param(clockmode, int, S_IRUGO | S_IWUSR);
Olivier Bornet8844a322009-08-18 21:05:54 +02001275MODULE_PARM_DESC(clockmode, "Card clock mode (1=3.579 MHz, 2=3.680 MHz, "
1276 "3=6 Mhz)");
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001277
1278module_param(cdmode, int, S_IRUGO | S_IWUSR);
Olivier Bornet8844a322009-08-18 21:05:54 +02001279MODULE_PARM_DESC(cdmode, "Card detect mode (0=none, 1=CD, 2=!CD, 3=DSR, "
1280 "4=!DSR, 5=CTS, 6=!CTS, 7=RING, 8=!RING)");
Olivier Bornete55c6d02009-08-18 21:05:57 +02001281
1282module_param(vcc_default, int, S_IRUGO | S_IWUSR);
1283MODULE_PARM_DESC(vcc_default, "Set default VCC (either 3 for 3.3V or 5 "
1284 "for 5V). Default to 5.");