blob: e6e02b178d2bbef5df8c27967061b7e0b1587ed4 [file] [log] [blame]
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001/*
2 * Infinity Unlimited USB Phoenix driver
3 *
4 * Copyright (C) 2007 Alain Degreffe (eczema@ecze.com)
5 *
6 * Original code taken from iuutool (Copyright (C) 2006 Juan Carlos Borrás)
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * And tested with help of WB Electronics
14 *
15 */
16#include <linux/kernel.h>
17#include <linux/errno.h>
18#include <linux/init.h>
19#include <linux/slab.h>
20#include <linux/tty.h>
21#include <linux/tty_driver.h>
22#include <linux/tty_flip.h>
23#include <linux/serial.h>
24#include <linux/module.h>
25#include <linux/moduleparam.h>
26#include <linux/spinlock.h>
27#include <linux/uaccess.h>
28#include <linux/usb.h>
29#include <linux/usb/serial.h>
30#include "iuu_phoenix.h"
31#include <linux/random.h>
32
33
34#ifdef CONFIG_USB_SERIAL_DEBUG
35static int debug = 1;
36#else
37static int debug;
38#endif
39
40/*
41 * Version Information
42 */
Olivier Bornet02b18062009-08-18 21:05:56 +020043#define DRIVER_VERSION "v0.11"
Alain Degreffe60a8fc02007-10-26 13:51:49 +020044#define DRIVER_DESC "Infinity USB Unlimited Phoenix driver"
45
46static struct usb_device_id id_table[] = {
47 {USB_DEVICE(IUU_USB_VENDOR_ID, IUU_USB_PRODUCT_ID)},
48 {} /* Terminating entry */
49};
50MODULE_DEVICE_TABLE(usb, id_table);
51
52static struct usb_driver iuu_driver = {
53 .name = "iuu_phoenix",
54 .probe = usb_serial_probe,
55 .disconnect = usb_serial_disconnect,
56 .id_table = id_table,
57 .no_dynamic_id = 1,
58};
59
60/* turbo parameter */
61static int boost = 100;
62static int clockmode = 1;
63static int cdmode = 1;
64static int iuu_cardin;
65static int iuu_cardout;
66static int xmas;
Olivier Bornete55c6d02009-08-18 21:05:57 +020067static int vcc_default = 5;
Alain Degreffe60a8fc02007-10-26 13:51:49 +020068
69static void read_rxcmd_callback(struct urb *urb);
70
71struct iuu_private {
72 spinlock_t lock; /* store irq state */
73 wait_queue_head_t delta_msr_wait;
Alain Degreffe60a8fc02007-10-26 13:51:49 +020074 u8 line_status;
Alain Degreffe60a8fc02007-10-26 13:51:49 +020075 int tiostatus; /* store IUART SIGNAL for tiocmget call */
76 u8 reset; /* if 1 reset is needed */
77 int poll; /* number of poll */
78 u8 *writebuf; /* buffer for writing to device */
79 int writelen; /* num of byte to write to device */
80 u8 *buf; /* used for initialize speed */
81 u8 *dbgbuf; /* debug buffer */
82 u8 len;
Olivier Bornet20eda942009-08-18 21:05:55 +020083 int vcc; /* vcc (either 3 or 5 V) */
Alain Degreffe60a8fc02007-10-26 13:51:49 +020084};
85
86
87static void iuu_free_buf(struct iuu_private *priv)
88{
89 kfree(priv->buf);
90 kfree(priv->dbgbuf);
91 kfree(priv->writebuf);
92}
93
94static int iuu_alloc_buf(struct iuu_private *priv)
95{
96 priv->buf = kzalloc(256, GFP_KERNEL);
97 priv->dbgbuf = kzalloc(256, GFP_KERNEL);
98 priv->writebuf = kzalloc(256, GFP_KERNEL);
99 if (!priv->buf || !priv->dbgbuf || !priv->writebuf) {
100 iuu_free_buf(priv);
Harvey Harrison441b62c2008-03-03 16:08:34 -0800101 dbg("%s problem allocation buffer", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200102 return -ENOMEM;
103 }
Harvey Harrison441b62c2008-03-03 16:08:34 -0800104 dbg("%s - Privates buffers allocation success", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200105 return 0;
106}
107
108static int iuu_startup(struct usb_serial *serial)
109{
110 struct iuu_private *priv;
111 priv = kzalloc(sizeof(struct iuu_private), GFP_KERNEL);
Harvey Harrison441b62c2008-03-03 16:08:34 -0800112 dbg("%s- priv allocation success", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200113 if (!priv)
114 return -ENOMEM;
115 if (iuu_alloc_buf(priv)) {
116 kfree(priv);
117 return -ENOMEM;
118 }
Olivier Bornete55c6d02009-08-18 21:05:57 +0200119 priv->vcc = vcc_default;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200120 spin_lock_init(&priv->lock);
121 init_waitqueue_head(&priv->delta_msr_wait);
122 usb_set_serial_port_data(serial->port[0], priv);
123 return 0;
124}
125
Alan Sternf9c99bb2009-06-02 11:53:55 -0400126/* Release function */
127static void iuu_release(struct usb_serial *serial)
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200128{
129 struct usb_serial_port *port = serial->port[0];
130 struct iuu_private *priv = usb_get_serial_port_data(port);
131 if (!port)
132 return;
133
Harvey Harrison441b62c2008-03-03 16:08:34 -0800134 dbg("%s", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200135
136 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 Cox95da3102008-07-22 11:09:07 +0100148static int iuu_tiocmset(struct tty_struct *tty, struct file *file,
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 if (set & TIOCM_RTS)
161 priv->tiostatus = TIOCM_RTS;
162
163 if (!(set & TIOCM_RTS) && priv->tiostatus == TIOCM_RTS) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800164 dbg("%s TIOCMSET RESET called !!!", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200165 priv->reset = 1;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200166 }
Alan Cox7b1fc8b2008-02-20 21:39:25 +0000167 spin_unlock_irqrestore(&priv->lock, flags);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200168 return 0;
169}
170
171/* This is used to provide a carrier detect mechanism
172 * When a card is present, the response is 0x00
173 * When no card , the reader respond with TIOCM_CD
174 * This is known as CD autodetect mechanism
175 */
Alan Cox95da3102008-07-22 11:09:07 +0100176static int iuu_tiocmget(struct tty_struct *tty, struct file *file)
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200177{
Alan Cox95da3102008-07-22 11:09:07 +0100178 struct usb_serial_port *port = tty->driver_data;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200179 struct iuu_private *priv = usb_get_serial_port_data(port);
Alan Cox7b1fc8b2008-02-20 21:39:25 +0000180 unsigned long flags;
181 int rc;
182
183 spin_lock_irqsave(&priv->lock, flags);
184 rc = priv->tiostatus;
185 spin_unlock_irqrestore(&priv->lock, flags);
186
187 return rc;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200188}
189
190static void iuu_rxcmd(struct urb *urb)
191{
Ming Leicdc97792008-02-24 18:41:47 +0800192 struct usb_serial_port *port = urb->context;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200193 int result;
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800194 int status = urb->status;
195
Harvey Harrison441b62c2008-03-03 16:08:34 -0800196 dbg("%s - enter", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200197
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800198 if (status) {
199 dbg("%s - status = %d", __func__, status);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200200 /* error stop all */
201 return;
202 }
203
204
205 memset(port->write_urb->transfer_buffer, IUU_UART_RX, 1);
206 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
207 usb_sndbulkpipe(port->serial->dev,
208 port->bulk_out_endpointAddress),
209 port->write_urb->transfer_buffer, 1,
210 read_rxcmd_callback, port);
211 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
212}
213
214static int iuu_reset(struct usb_serial_port *port, u8 wt)
215{
216 struct iuu_private *priv = usb_get_serial_port_data(port);
217 int result;
218 char *buf_ptr = port->write_urb->transfer_buffer;
Harvey Harrison441b62c2008-03-03 16:08:34 -0800219 dbg("%s - enter", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200220
221 /* Prepare the reset sequence */
222
223 *buf_ptr++ = IUU_RST_SET;
224 *buf_ptr++ = IUU_DELAY_MS;
225 *buf_ptr++ = wt;
226 *buf_ptr = IUU_RST_CLEAR;
227
228 /* send the sequence */
229
230 usb_fill_bulk_urb(port->write_urb,
231 port->serial->dev,
232 usb_sndbulkpipe(port->serial->dev,
233 port->bulk_out_endpointAddress),
234 port->write_urb->transfer_buffer, 4, iuu_rxcmd, port);
235 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
236 priv->reset = 0;
237 return result;
238}
239
240/* Status Function
241 * Return value is
242 * 0x00 = no card
243 * 0x01 = smartcard
244 * 0x02 = sim card
245 */
246static void iuu_update_status_callback(struct urb *urb)
247{
Ming Leicdc97792008-02-24 18:41:47 +0800248 struct usb_serial_port *port = urb->context;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200249 struct iuu_private *priv = usb_get_serial_port_data(port);
250 u8 *st;
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800251 int status = urb->status;
252
Harvey Harrison441b62c2008-03-03 16:08:34 -0800253 dbg("%s - enter", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200254
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800255 if (status) {
256 dbg("%s - status = %d", __func__, status);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200257 /* error stop all */
258 return;
259 }
260
261 st = urb->transfer_buffer;
Harvey Harrison441b62c2008-03-03 16:08:34 -0800262 dbg("%s - enter", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200263 if (urb->actual_length == 1) {
264 switch (st[0]) {
265 case 0x1:
266 priv->tiostatus = iuu_cardout;
267 break;
268 case 0x0:
269 priv->tiostatus = iuu_cardin;
270 break;
271 default:
272 priv->tiostatus = iuu_cardin;
273 }
274 }
275 iuu_rxcmd(urb);
276}
277
278static void iuu_status_callback(struct urb *urb)
279{
Ming Leicdc97792008-02-24 18:41:47 +0800280 struct usb_serial_port *port = urb->context;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200281 int result;
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800282 int status = urb->status;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200283
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800284 dbg("%s - status = %d", __func__, status);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200285 usb_fill_bulk_urb(port->read_urb, port->serial->dev,
286 usb_rcvbulkpipe(port->serial->dev,
287 port->bulk_in_endpointAddress),
288 port->read_urb->transfer_buffer, 256,
289 iuu_update_status_callback, port);
290 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
291}
292
293static int iuu_status(struct usb_serial_port *port)
294{
295 int result;
296
Harvey Harrison441b62c2008-03-03 16:08:34 -0800297 dbg("%s - enter", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200298
299 memset(port->write_urb->transfer_buffer, IUU_GET_STATE_REGISTER, 1);
300 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
301 usb_sndbulkpipe(port->serial->dev,
302 port->bulk_out_endpointAddress),
303 port->write_urb->transfer_buffer, 1,
304 iuu_status_callback, port);
305 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
306 return result;
307
308}
309
310static int bulk_immediate(struct usb_serial_port *port, u8 *buf, u8 count)
311{
312 int status;
313 struct usb_serial *serial = port->serial;
314 int actual = 0;
315
Harvey Harrison441b62c2008-03-03 16:08:34 -0800316 dbg("%s - enter", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200317
318 /* send the data out the bulk port */
319
320 status =
321 usb_bulk_msg(serial->dev,
322 usb_sndbulkpipe(serial->dev,
323 port->bulk_out_endpointAddress), buf,
324 count, &actual, HZ * 1);
325
Alan Cox9e8e2d22008-07-22 11:12:59 +0100326 if (status != IUU_OPERATION_OK)
Harvey Harrison441b62c2008-03-03 16:08:34 -0800327 dbg("%s - error = %2x", __func__, status);
Alan Cox9e8e2d22008-07-22 11:12:59 +0100328 else
Harvey Harrison441b62c2008-03-03 16:08:34 -0800329 dbg("%s - write OK !", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200330 return status;
331}
332
333static int read_immediate(struct usb_serial_port *port, u8 *buf, u8 count)
334{
335 int status;
336 struct usb_serial *serial = port->serial;
337 int actual = 0;
338
Harvey Harrison441b62c2008-03-03 16:08:34 -0800339 dbg("%s - enter", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200340
341 /* send the data out the bulk port */
342
343 status =
344 usb_bulk_msg(serial->dev,
345 usb_rcvbulkpipe(serial->dev,
346 port->bulk_in_endpointAddress), buf,
347 count, &actual, HZ * 1);
348
Alan Cox9e8e2d22008-07-22 11:12:59 +0100349 if (status != IUU_OPERATION_OK)
Harvey Harrison441b62c2008-03-03 16:08:34 -0800350 dbg("%s - error = %2x", __func__, status);
Alan Cox9e8e2d22008-07-22 11:12:59 +0100351 else
Harvey Harrison441b62c2008-03-03 16:08:34 -0800352 dbg("%s - read OK !", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200353 return status;
354}
355
356static int iuu_led(struct usb_serial_port *port, unsigned int R,
357 unsigned int G, unsigned int B, u8 f)
358{
359 int status;
360 u8 *buf;
361 buf = kmalloc(8, GFP_KERNEL);
362 if (!buf)
363 return -ENOMEM;
364
Harvey Harrison441b62c2008-03-03 16:08:34 -0800365 dbg("%s - enter", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200366
367 buf[0] = IUU_SET_LED;
368 buf[1] = R & 0xFF;
369 buf[2] = (R >> 8) & 0xFF;
370 buf[3] = G & 0xFF;
371 buf[4] = (G >> 8) & 0xFF;
372 buf[5] = B & 0xFF;
373 buf[6] = (B >> 8) & 0xFF;
374 buf[7] = f;
375 status = bulk_immediate(port, buf, 8);
376 kfree(buf);
377 if (status != IUU_OPERATION_OK)
Harvey Harrison441b62c2008-03-03 16:08:34 -0800378 dbg("%s - led error status = %2x", __func__, status);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200379 else
Harvey Harrison441b62c2008-03-03 16:08:34 -0800380 dbg("%s - led OK !", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200381 return IUU_OPERATION_OK;
382}
383
384static void iuu_rgbf_fill_buffer(u8 *buf, u8 r1, u8 r2, u8 g1, u8 g2, u8 b1,
385 u8 b2, u8 freq)
386{
387 *buf++ = IUU_SET_LED;
388 *buf++ = r1;
389 *buf++ = r2;
390 *buf++ = g1;
391 *buf++ = g2;
392 *buf++ = b1;
393 *buf++ = b2;
394 *buf = freq;
395}
396
397static void iuu_led_activity_on(struct urb *urb)
398{
Ming Leicdc97792008-02-24 18:41:47 +0800399 struct usb_serial_port *port = urb->context;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200400 int result;
401 char *buf_ptr = port->write_urb->transfer_buffer;
402 *buf_ptr++ = IUU_SET_LED;
403 if (xmas == 1) {
404 get_random_bytes(buf_ptr, 6);
405 *(buf_ptr+7) = 1;
406 } else {
407 iuu_rgbf_fill_buffer(buf_ptr, 255, 255, 0, 0, 0, 0, 255);
408 }
409
410 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
411 usb_sndbulkpipe(port->serial->dev,
412 port->bulk_out_endpointAddress),
413 port->write_urb->transfer_buffer, 8 ,
414 iuu_rxcmd, port);
415 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
416}
417
418static void iuu_led_activity_off(struct urb *urb)
419{
Ming Leicdc97792008-02-24 18:41:47 +0800420 struct usb_serial_port *port = urb->context;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200421 int result;
422 char *buf_ptr = port->write_urb->transfer_buffer;
423 if (xmas == 1) {
424 iuu_rxcmd(urb);
425 return;
426 } else {
427 *buf_ptr++ = IUU_SET_LED;
428 iuu_rgbf_fill_buffer(buf_ptr, 0, 0, 255, 255, 0, 0, 255);
429 }
430 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
431 usb_sndbulkpipe(port->serial->dev,
432 port->bulk_out_endpointAddress),
433 port->write_urb->transfer_buffer, 8 ,
434 iuu_rxcmd, port);
435 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
436}
437
438
439
440static int iuu_clk(struct usb_serial_port *port, int dwFrq)
441{
442 int status;
443 struct iuu_private *priv = usb_get_serial_port_data(port);
444 int Count = 0;
445 u8 FrqGenAdr = 0x69;
446 u8 DIV = 0; /* 8bit */
447 u8 XDRV = 0; /* 8bit */
448 u8 PUMP = 0; /* 3bit */
449 u8 PBmsb = 0; /* 2bit */
450 u8 PBlsb = 0; /* 8bit */
451 u8 PO = 0; /* 1bit */
452 u8 Q = 0; /* 7bit */
453 /* 24bit = 3bytes */
454 unsigned int P = 0;
455 unsigned int P2 = 0;
456 int frq = (int)dwFrq;
457
Harvey Harrison441b62c2008-03-03 16:08:34 -0800458 dbg("%s - enter", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200459
460 if (frq == 0) {
461 priv->buf[Count++] = IUU_UART_WRITE_I2C;
462 priv->buf[Count++] = FrqGenAdr << 1;
463 priv->buf[Count++] = 0x09;
464 priv->buf[Count++] = 0x00;
465
466 status = bulk_immediate(port, (u8 *) priv->buf, Count);
467 if (status != 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800468 dbg("%s - write error ", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200469 return status;
470 }
471 } else if (frq == 3579000) {
472 DIV = 100;
473 P = 1193;
474 Q = 40;
475 XDRV = 0;
476 } else if (frq == 3680000) {
477 DIV = 105;
478 P = 161;
479 Q = 5;
480 XDRV = 0;
481 } else if (frq == 6000000) {
482 DIV = 66;
483 P = 66;
484 Q = 2;
485 XDRV = 0x28;
486 } else {
487 unsigned int result = 0;
488 unsigned int tmp = 0;
489 unsigned int check;
490 unsigned int check2;
491 char found = 0x00;
492 unsigned int lQ = 2;
493 unsigned int lP = 2055;
494 unsigned int lDiv = 4;
495
496 for (lQ = 2; lQ <= 47 && !found; lQ++)
497 for (lP = 2055; lP >= 8 && !found; lP--)
498 for (lDiv = 4; lDiv <= 127 && !found; lDiv++) {
499 tmp = (12000000 / lDiv) * (lP / lQ);
500 if (abs((int)(tmp - frq)) <
501 abs((int)(frq - result))) {
502 check2 = (12000000 / lQ);
503 if (check2 < 250000)
504 continue;
505 check = (12000000 / lQ) * lP;
506 if (check > 400000000)
507 continue;
508 if (check < 100000000)
509 continue;
510 if (lDiv < 4 || lDiv > 127)
511 continue;
512 result = tmp;
513 P = lP;
514 DIV = lDiv;
515 Q = lQ;
516 if (result == frq)
517 found = 0x01;
518 }
519 }
520 }
521 P2 = ((P - PO) / 2) - 4;
522 DIV = DIV;
523 PUMP = 0x04;
524 PBmsb = (P2 >> 8 & 0x03);
525 PBlsb = P2 & 0xFF;
526 PO = (P >> 10) & 0x01;
527 Q = Q - 2;
528
529 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
530 priv->buf[Count++] = FrqGenAdr << 1;
531 priv->buf[Count++] = 0x09;
532 priv->buf[Count++] = 0x20; /* Adr = 0x09 */
533 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
534 priv->buf[Count++] = FrqGenAdr << 1;
535 priv->buf[Count++] = 0x0C;
536 priv->buf[Count++] = DIV; /* Adr = 0x0C */
537 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
538 priv->buf[Count++] = FrqGenAdr << 1;
539 priv->buf[Count++] = 0x12;
540 priv->buf[Count++] = XDRV; /* Adr = 0x12 */
541 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
542 priv->buf[Count++] = FrqGenAdr << 1;
543 priv->buf[Count++] = 0x13;
544 priv->buf[Count++] = 0x6B; /* Adr = 0x13 */
545 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
546 priv->buf[Count++] = FrqGenAdr << 1;
547 priv->buf[Count++] = 0x40;
548 priv->buf[Count++] = (0xC0 | ((PUMP & 0x07) << 2)) |
549 (PBmsb & 0x03); /* Adr = 0x40 */
550 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
551 priv->buf[Count++] = FrqGenAdr << 1;
552 priv->buf[Count++] = 0x41;
553 priv->buf[Count++] = PBlsb; /* Adr = 0x41 */
554 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
555 priv->buf[Count++] = FrqGenAdr << 1;
556 priv->buf[Count++] = 0x42;
557 priv->buf[Count++] = Q | (((PO & 0x01) << 7)); /* Adr = 0x42 */
558 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
559 priv->buf[Count++] = FrqGenAdr << 1;
560 priv->buf[Count++] = 0x44;
561 priv->buf[Count++] = (char)0xFF; /* Adr = 0x44 */
562 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
563 priv->buf[Count++] = FrqGenAdr << 1;
564 priv->buf[Count++] = 0x45;
565 priv->buf[Count++] = (char)0xFE; /* Adr = 0x45 */
566 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
567 priv->buf[Count++] = FrqGenAdr << 1;
568 priv->buf[Count++] = 0x46;
569 priv->buf[Count++] = 0x7F; /* Adr = 0x46 */
570 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
571 priv->buf[Count++] = FrqGenAdr << 1;
572 priv->buf[Count++] = 0x47;
573 priv->buf[Count++] = (char)0x84; /* Adr = 0x47 */
574
575 status = bulk_immediate(port, (u8 *) priv->buf, Count);
576 if (status != IUU_OPERATION_OK)
Harvey Harrison441b62c2008-03-03 16:08:34 -0800577 dbg("%s - write error ", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200578 return status;
579}
580
581static int iuu_uart_flush(struct usb_serial_port *port)
582{
583 int i;
584 int status;
585 u8 rxcmd = IUU_UART_RX;
586 struct iuu_private *priv = usb_get_serial_port_data(port);
587
Harvey Harrison441b62c2008-03-03 16:08:34 -0800588 dbg("%s - enter", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200589
590 if (iuu_led(port, 0xF000, 0, 0, 0xFF) < 0)
591 return -EIO;
592
593 for (i = 0; i < 2; i++) {
594 status = bulk_immediate(port, &rxcmd, 1);
595 if (status != IUU_OPERATION_OK) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800596 dbg("%s - uart_flush_write error", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200597 return status;
598 }
599
600 status = read_immediate(port, &priv->len, 1);
601 if (status != IUU_OPERATION_OK) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800602 dbg("%s - uart_flush_read error", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200603 return status;
604 }
605
606 if (priv->len > 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800607 dbg("%s - uart_flush datalen is : %i ", __func__,
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200608 priv->len);
609 status = read_immediate(port, priv->buf, priv->len);
610 if (status != IUU_OPERATION_OK) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800611 dbg("%s - uart_flush_read error", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200612 return status;
613 }
614 }
615 }
Harvey Harrison441b62c2008-03-03 16:08:34 -0800616 dbg("%s - uart_flush_read OK!", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200617 iuu_led(port, 0, 0xF000, 0, 0xFF);
618 return status;
619}
620
621static void read_buf_callback(struct urb *urb)
622{
Ming Leicdc97792008-02-24 18:41:47 +0800623 struct usb_serial_port *port = urb->context;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200624 unsigned char *data = urb->transfer_buffer;
625 struct tty_struct *tty;
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800626 int status = urb->status;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200627
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800628 dbg("%s - status = %d", __func__, status);
629
630 if (status) {
631 if (status == -EPROTO) {
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200632 /* reschedule needed */
633 }
634 return;
635 }
636
Harvey Harrison441b62c2008-03-03 16:08:34 -0800637 dbg("%s - %i chars to write", __func__, urb->actual_length);
Alan Cox4a90f092008-10-13 10:39:46 +0100638 tty = tty_port_tty_get(&port->port);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200639 if (data == NULL)
Harvey Harrison441b62c2008-03-03 16:08:34 -0800640 dbg("%s - data is NULL !!!", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200641 if (tty && urb->actual_length && data) {
642 tty_insert_flip_string(tty, data, urb->actual_length);
643 tty_flip_buffer_push(tty);
644 }
Alan Cox4a90f092008-10-13 10:39:46 +0100645 tty_kref_put(tty);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200646 iuu_led_activity_on(urb);
647}
648
649static int iuu_bulk_write(struct usb_serial_port *port)
650{
651 struct iuu_private *priv = usb_get_serial_port_data(port);
Steven Rostedt85943032008-05-06 20:42:31 -0700652 unsigned long flags;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200653 int result;
654 int i;
Olivier Bornet5fcf62b2009-06-11 12:52:26 +0100655 int buf_len;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200656 char *buf_ptr = port->write_urb->transfer_buffer;
Harvey Harrison441b62c2008-03-03 16:08:34 -0800657 dbg("%s - enter", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200658
Olivier Bornet5fcf62b2009-06-11 12:52:26 +0100659 spin_lock_irqsave(&priv->lock, flags);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200660 *buf_ptr++ = IUU_UART_ESC;
661 *buf_ptr++ = IUU_UART_TX;
662 *buf_ptr++ = priv->writelen;
663
Olivier Bornet5fcf62b2009-06-11 12:52:26 +0100664 memcpy(buf_ptr, priv->writebuf, priv->writelen);
665 buf_len = priv->writelen;
666 priv->writelen = 0;
667 spin_unlock_irqrestore(&priv->lock, flags);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200668 if (debug == 1) {
Olivier Bornet5fcf62b2009-06-11 12:52:26 +0100669 for (i = 0; i < buf_len; i++)
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200670 sprintf(priv->dbgbuf + i*2 ,
671 "%02X", priv->writebuf[i]);
Olivier Bornet5fcf62b2009-06-11 12:52:26 +0100672 priv->dbgbuf[buf_len+i*2] = 0;
Harvey Harrison441b62c2008-03-03 16:08:34 -0800673 dbg("%s - writing %i chars : %s", __func__,
Olivier Bornet5fcf62b2009-06-11 12:52:26 +0100674 buf_len, priv->dbgbuf);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200675 }
676 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
677 usb_sndbulkpipe(port->serial->dev,
678 port->bulk_out_endpointAddress),
Olivier Bornet5fcf62b2009-06-11 12:52:26 +0100679 port->write_urb->transfer_buffer, buf_len + 3,
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200680 iuu_rxcmd, port);
681 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200682 usb_serial_port_softint(port);
683 return result;
684}
685
686static int iuu_read_buf(struct usb_serial_port *port, int len)
687{
688 int result;
Harvey Harrison441b62c2008-03-03 16:08:34 -0800689 dbg("%s - enter", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200690
691 usb_fill_bulk_urb(port->read_urb, port->serial->dev,
692 usb_rcvbulkpipe(port->serial->dev,
693 port->bulk_in_endpointAddress),
694 port->read_urb->transfer_buffer, len,
695 read_buf_callback, port);
696 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
697 return result;
698}
699
700static void iuu_uart_read_callback(struct urb *urb)
701{
Ming Leicdc97792008-02-24 18:41:47 +0800702 struct usb_serial_port *port = urb->context;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200703 struct iuu_private *priv = usb_get_serial_port_data(port);
Steven Rostedt85943032008-05-06 20:42:31 -0700704 unsigned long flags;
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800705 int status = urb->status;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200706 int error = 0;
707 int len = 0;
708 unsigned char *data = urb->transfer_buffer;
709 priv->poll++;
710
Harvey Harrison441b62c2008-03-03 16:08:34 -0800711 dbg("%s - enter", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200712
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800713 if (status) {
714 dbg("%s - status = %d", __func__, status);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200715 /* error stop all */
716 return;
717 }
718 if (data == NULL)
Harvey Harrison441b62c2008-03-03 16:08:34 -0800719 dbg("%s - data is NULL !!!", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200720
721 if (urb->actual_length == 1 && data != NULL)
722 len = (int) data[0];
723
724 if (urb->actual_length > 1) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800725 dbg("%s - urb->actual_length = %i", __func__,
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200726 urb->actual_length);
727 error = 1;
728 return;
729 }
730 /* if len > 0 call readbuf */
731
732 if (len > 0 && error == 0) {
733 dbg("%s - call read buf - len to read is %i ",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800734 __func__, len);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200735 status = iuu_read_buf(port, len);
736 return;
737 }
738 /* need to update status ? */
739 if (priv->poll > 99) {
740 status = iuu_status(port);
741 priv->poll = 0;
742 return;
743 }
744
745 /* reset waiting ? */
746
747 if (priv->reset == 1) {
748 status = iuu_reset(port, 0xC);
749 return;
750 }
751 /* Writebuf is waiting */
752 spin_lock_irqsave(&priv->lock, flags);
753 if (priv->writelen > 0) {
754 spin_unlock_irqrestore(&priv->lock, flags);
755 status = iuu_bulk_write(port);
756 return;
757 }
758 spin_unlock_irqrestore(&priv->lock, flags);
759 /* if nothing to write call again rxcmd */
Harvey Harrison441b62c2008-03-03 16:08:34 -0800760 dbg("%s - rxcmd recall", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200761 iuu_led_activity_off(urb);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200762}
763
Alan Cox95da3102008-07-22 11:09:07 +0100764static int iuu_uart_write(struct tty_struct *tty, struct usb_serial_port *port,
765 const u8 *buf, int count)
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200766{
767 struct iuu_private *priv = usb_get_serial_port_data(port);
Steven Rostedt85943032008-05-06 20:42:31 -0700768 unsigned long flags;
Harvey Harrison441b62c2008-03-03 16:08:34 -0800769 dbg("%s - enter", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200770
771 if (count > 256)
772 return -ENOMEM;
773
774 spin_lock_irqsave(&priv->lock, flags);
Olivier Bornet5fcf62b2009-06-11 12:52:26 +0100775
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200776 /* fill the buffer */
Olivier Bornet5fcf62b2009-06-11 12:52:26 +0100777 memcpy(priv->writebuf + priv->writelen, buf, count);
778 priv->writelen += count;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200779 spin_unlock_irqrestore(&priv->lock, flags);
780
Alan Cox9e8e2d22008-07-22 11:12:59 +0100781 return count;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200782}
783
784static void read_rxcmd_callback(struct urb *urb)
785{
Ming Leicdc97792008-02-24 18:41:47 +0800786 struct usb_serial_port *port = urb->context;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200787 int result;
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800788 int status = urb->status;
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200789
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800790 dbg("%s - status = %d", __func__, status);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200791
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800792 if (status) {
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200793 /* error stop all */
794 return;
795 }
796
797 usb_fill_bulk_urb(port->read_urb, port->serial->dev,
798 usb_rcvbulkpipe(port->serial->dev,
799 port->bulk_in_endpointAddress),
800 port->read_urb->transfer_buffer, 256,
801 iuu_uart_read_callback, port);
802 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
Harvey Harrison441b62c2008-03-03 16:08:34 -0800803 dbg("%s - submit result = %d", __func__, result);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200804 return;
805}
806
807static int iuu_uart_on(struct usb_serial_port *port)
808{
809 int status;
810 u8 *buf;
811
812 buf = kmalloc(sizeof(u8) * 4, GFP_KERNEL);
813
814 if (!buf)
815 return -ENOMEM;
816
817 buf[0] = IUU_UART_ENABLE;
818 buf[1] = (u8) ((IUU_BAUD_9600 >> 8) & 0x00FF);
819 buf[2] = (u8) (0x00FF & IUU_BAUD_9600);
Olivier Bornetcc3447d2009-06-11 12:53:24 +0100820 buf[3] = (u8) (0x0F0 & IUU_ONE_STOP_BIT) | (0x07 & IUU_PARITY_EVEN);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200821
822 status = bulk_immediate(port, buf, 4);
823 if (status != IUU_OPERATION_OK) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800824 dbg("%s - uart_on error", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200825 goto uart_enable_failed;
826 }
827 /* iuu_reset() the card after iuu_uart_on() */
828 status = iuu_uart_flush(port);
829 if (status != IUU_OPERATION_OK)
Harvey Harrison441b62c2008-03-03 16:08:34 -0800830 dbg("%s - uart_flush error", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200831uart_enable_failed:
832 kfree(buf);
833 return status;
834}
835
836/* Diables the IUU UART (a.k.a. the Phoenix voiderface) */
837static int iuu_uart_off(struct usb_serial_port *port)
838{
839 int status;
840 u8 *buf;
841 buf = kmalloc(1, GFP_KERNEL);
842 if (!buf)
843 return -ENOMEM;
844 buf[0] = IUU_UART_DISABLE;
845
846 status = bulk_immediate(port, buf, 1);
847 if (status != IUU_OPERATION_OK)
Harvey Harrison441b62c2008-03-03 16:08:34 -0800848 dbg("%s - uart_off error", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200849
850 kfree(buf);
851 return status;
852}
853
854static int iuu_uart_baud(struct usb_serial_port *port, u32 baud,
855 u32 *actual, u8 parity)
856{
857 int status;
858 u8 *dataout;
859 u8 DataCount = 0;
860 u8 T1Frekvens = 0;
861 u8 T1reload = 0;
862 unsigned int T1FrekvensHZ = 0;
863
864 dataout = kmalloc(sizeof(u8) * 5, GFP_KERNEL);
865
866 if (!dataout)
867 return -ENOMEM;
868
869 if (baud < 1200 || baud > 230400) {
870 kfree(dataout);
871 return IUU_INVALID_PARAMETER;
872 }
873 if (baud > 977) {
874 T1Frekvens = 3;
875 T1FrekvensHZ = 500000;
876 }
877
878 if (baud > 3906) {
879 T1Frekvens = 2;
880 T1FrekvensHZ = 2000000;
881 }
882
883 if (baud > 11718) {
884 T1Frekvens = 1;
885 T1FrekvensHZ = 6000000;
886 }
887
888 if (baud > 46875) {
889 T1Frekvens = 0;
890 T1FrekvensHZ = 24000000;
891 }
892
893 T1reload = 256 - (u8) (T1FrekvensHZ / (baud * 2));
894
895 /* magic number here: ENTER_FIRMWARE_UPDATE; */
896 dataout[DataCount++] = IUU_UART_ESC;
897 /* magic number here: CHANGE_BAUD; */
898 dataout[DataCount++] = IUU_UART_CHANGE;
899 dataout[DataCount++] = T1Frekvens;
900 dataout[DataCount++] = T1reload;
901
902 *actual = (T1FrekvensHZ / (256 - T1reload)) / 2;
903
904 switch (parity & 0x0F) {
905 case IUU_PARITY_NONE:
906 dataout[DataCount++] = 0x00;
907 break;
908 case IUU_PARITY_EVEN:
909 dataout[DataCount++] = 0x01;
910 break;
911 case IUU_PARITY_ODD:
912 dataout[DataCount++] = 0x02;
913 break;
914 case IUU_PARITY_MARK:
915 dataout[DataCount++] = 0x03;
916 break;
917 case IUU_PARITY_SPACE:
918 dataout[DataCount++] = 0x04;
919 break;
920 default:
921 kfree(dataout);
922 return IUU_INVALID_PARAMETER;
923 break;
924 }
925
926 switch (parity & 0xF0) {
927 case IUU_ONE_STOP_BIT:
928 dataout[DataCount - 1] |= IUU_ONE_STOP_BIT;
929 break;
930
931 case IUU_TWO_STOP_BITS:
932 dataout[DataCount - 1] |= IUU_TWO_STOP_BITS;
933 break;
934 default:
935 kfree(dataout);
936 return IUU_INVALID_PARAMETER;
937 break;
938 }
939
940 status = bulk_immediate(port, dataout, DataCount);
941 if (status != IUU_OPERATION_OK)
Harvey Harrison441b62c2008-03-03 16:08:34 -0800942 dbg("%s - uart_off error", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200943 kfree(dataout);
944 return status;
945}
946
Olivier Bornet96dab772009-06-11 12:54:20 +0100947static void iuu_set_termios(struct tty_struct *tty,
948 struct usb_serial_port *port, struct ktermios *old_termios)
949{
950 const u32 supported_mask = CMSPAR|PARENB|PARODD;
951
952 unsigned int cflag = tty->termios->c_cflag;
953 int status;
954 u32 actual;
955 u32 parity;
956 int csize = CS7;
957 int baud = 9600; /* Fixed for the moment */
958 u32 newval = cflag & supported_mask;
959
960 /* compute the parity parameter */
961 parity = 0;
962 if (cflag & CMSPAR) { /* Using mark space */
963 if (cflag & PARODD)
964 parity |= IUU_PARITY_SPACE;
965 else
966 parity |= IUU_PARITY_MARK;
967 } else if (!(cflag & PARENB)) {
968 parity |= IUU_PARITY_NONE;
969 csize = CS8;
970 } else if (cflag & PARODD)
971 parity |= IUU_PARITY_ODD;
972 else
973 parity |= IUU_PARITY_EVEN;
974
975 parity |= (cflag & CSTOPB ? IUU_TWO_STOP_BITS : IUU_ONE_STOP_BIT);
976
977 /* set it */
978 status = iuu_uart_baud(port,
979 (clockmode == 2) ? 16457 : 9600 * boost / 100,
980 &actual, parity);
981
982 /* set the termios value to the real one, so the user now what has
983 * changed. We support few fields so its easies to copy the old hw
984 * settings back over and then adjust them
985 */
986 if (old_termios)
987 tty_termios_copy_hw(tty->termios, old_termios);
988 if (status != 0) /* Set failed - return old bits */
989 return;
990 /* Re-encode speed, parity and csize */
991 tty_encode_baud_rate(tty, baud, baud);
992 tty->termios->c_cflag &= ~(supported_mask|CSIZE);
993 tty->termios->c_cflag |= newval | csize;
994}
995
Alan Cox335f8512009-06-11 12:26:29 +0100996static void iuu_close(struct usb_serial_port *port)
Alain Degreffe60a8fc02007-10-26 13:51:49 +0200997{
998 /* iuu_led (port,255,0,0,0); */
999 struct usb_serial *serial;
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001000
1001 serial = port->serial;
1002 if (!serial)
1003 return;
1004
Harvey Harrison441b62c2008-03-03 16:08:34 -08001005 dbg("%s - port %d", __func__, port->number);
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001006
1007 iuu_uart_off(port);
1008 if (serial->dev) {
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001009 /* free writebuf */
1010 /* shutdown our urbs */
Harvey Harrison441b62c2008-03-03 16:08:34 -08001011 dbg("%s - shutting down urbs", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001012 usb_kill_urb(port->write_urb);
1013 usb_kill_urb(port->read_urb);
1014 usb_kill_urb(port->interrupt_in_urb);
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001015 iuu_led(port, 0, 0, 0xF000, 0xFF);
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001016 }
1017}
1018
Alan Coxfe1ae7f2009-09-19 13:13:33 -07001019static void iuu_init_termios(struct tty_struct *tty)
1020{
1021 *(tty->termios) = tty_std_termios;
1022 tty->termios->c_cflag = CLOCAL | CREAD | CS8 | B9600
1023 | TIOCM_CTS | CSTOPB | PARENB;
1024 tty->termios->c_ispeed = 9600;
1025 tty->termios->c_ospeed = 9600;
1026 tty->termios->c_lflag = 0;
1027 tty->termios->c_oflag = 0;
1028 tty->termios->c_iflag = 0;
1029}
1030
Alan Coxa509a7e2009-09-19 13:13:26 -07001031static int iuu_open(struct tty_struct *tty, struct usb_serial_port *port)
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001032{
1033 struct usb_serial *serial = port->serial;
1034 u8 *buf;
1035 int result;
1036 u32 actual;
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001037 struct iuu_private *priv = usb_get_serial_port_data(port);
1038
Harvey Harrison441b62c2008-03-03 16:08:34 -08001039 dbg("%s - port %d", __func__, port->number);
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001040 usb_clear_halt(serial->dev, port->write_urb->pipe);
1041 usb_clear_halt(serial->dev, port->read_urb->pipe);
1042
1043 buf = kmalloc(10, GFP_KERNEL);
1044 if (buf == NULL)
1045 return -ENOMEM;
1046
1047 /* fixup the endpoint buffer size */
1048 kfree(port->bulk_out_buffer);
1049 port->bulk_out_buffer = kmalloc(512, GFP_KERNEL);
1050 port->bulk_out_size = 512;
1051 kfree(port->bulk_in_buffer);
1052 port->bulk_in_buffer = kmalloc(512, GFP_KERNEL);
1053 port->bulk_in_size = 512;
1054
1055 if (!port->bulk_out_buffer || !port->bulk_in_buffer) {
1056 kfree(port->bulk_out_buffer);
1057 kfree(port->bulk_in_buffer);
1058 kfree(buf);
1059 return -ENOMEM;
1060 }
1061
1062 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
1063 usb_sndbulkpipe(port->serial->dev,
1064 port->bulk_out_endpointAddress),
1065 port->bulk_out_buffer, 512,
1066 NULL, NULL);
1067
1068
1069 usb_fill_bulk_urb(port->read_urb, port->serial->dev,
1070 usb_rcvbulkpipe(port->serial->dev,
1071 port->bulk_in_endpointAddress),
1072 port->bulk_in_buffer, 512,
1073 NULL, NULL);
1074
Alan Coxfe1ae7f2009-09-19 13:13:33 -07001075 priv->poll = 0;
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001076
1077 /* initialize writebuf */
1078#define FISH(a, b, c, d) do { \
1079 result = usb_control_msg(port->serial->dev, \
1080 usb_rcvctrlpipe(port->serial->dev, 0), \
1081 b, a, c, d, buf, 1, 1000); \
1082 dbg("0x%x:0x%x:0x%x:0x%x %d - %x", a, b, c, d, result, \
1083 buf[0]); } while (0);
1084
1085#define SOUP(a, b, c, d) do { \
1086 result = usb_control_msg(port->serial->dev, \
1087 usb_sndctrlpipe(port->serial->dev, 0), \
1088 b, a, c, d, NULL, 0, 1000); \
1089 dbg("0x%x:0x%x:0x%x:0x%x %d", a, b, c, d, result); } while (0)
1090
1091 /* This is not UART related but IUU USB driver related or something */
1092 /* like that. Basically no IUU will accept any commands from the USB */
1093 /* host unless it has received the following message */
1094 /* sprintf(buf ,"%c%c%c%c",0x03,0x02,0x02,0x0); */
1095
1096 SOUP(0x03, 0x02, 0x02, 0x0);
1097 kfree(buf);
1098 iuu_led(port, 0xF000, 0xF000, 0, 0xFF);
1099 iuu_uart_on(port);
1100 if (boost < 100)
1101 boost = 100;
1102 switch (clockmode) {
1103 case 2: /* 3.680 Mhz */
1104 iuu_clk(port, IUU_CLK_3680000 * boost / 100);
1105 result =
1106 iuu_uart_baud(port, 9600 * boost / 100, &actual,
1107 IUU_PARITY_EVEN);
1108 break;
1109 case 3: /* 6.00 Mhz */
1110 iuu_clk(port, IUU_CLK_6000000 * boost / 100);
1111 result =
1112 iuu_uart_baud(port, 16457 * boost / 100, &actual,
1113 IUU_PARITY_EVEN);
1114 break;
1115 default: /* 3.579 Mhz */
1116 iuu_clk(port, IUU_CLK_3579000 * boost / 100);
1117 result =
1118 iuu_uart_baud(port, 9600 * boost / 100, &actual,
1119 IUU_PARITY_EVEN);
1120 }
1121
1122 /* set the cardin cardout signals */
1123 switch (cdmode) {
1124 case 0:
1125 iuu_cardin = 0;
1126 iuu_cardout = 0;
1127 break;
1128 case 1:
1129 iuu_cardin = TIOCM_CD;
1130 iuu_cardout = 0;
1131 break;
1132 case 2:
1133 iuu_cardin = 0;
1134 iuu_cardout = TIOCM_CD;
1135 break;
1136 case 3:
1137 iuu_cardin = TIOCM_DSR;
1138 iuu_cardout = 0;
1139 break;
1140 case 4:
1141 iuu_cardin = 0;
1142 iuu_cardout = TIOCM_DSR;
1143 break;
1144 case 5:
1145 iuu_cardin = TIOCM_CTS;
1146 iuu_cardout = 0;
1147 break;
1148 case 6:
1149 iuu_cardin = 0;
1150 iuu_cardout = TIOCM_CTS;
1151 break;
1152 case 7:
1153 iuu_cardin = TIOCM_RNG;
1154 iuu_cardout = 0;
1155 break;
1156 case 8:
1157 iuu_cardin = 0;
1158 iuu_cardout = TIOCM_RNG;
1159 }
1160
1161 iuu_uart_flush(port);
1162
Harvey Harrison441b62c2008-03-03 16:08:34 -08001163 dbg("%s - initialization done", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001164
1165 memset(port->write_urb->transfer_buffer, IUU_UART_RX, 1);
1166 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
1167 usb_sndbulkpipe(port->serial->dev,
1168 port->bulk_out_endpointAddress),
1169 port->write_urb->transfer_buffer, 1,
1170 read_rxcmd_callback, port);
1171 result = usb_submit_urb(port->write_urb, GFP_KERNEL);
1172
1173 if (result) {
1174 dev_err(&port->dev, "%s - failed submitting read urb,"
Harvey Harrison441b62c2008-03-03 16:08:34 -08001175 " error %d\n", __func__, result);
Alan Cox335f8512009-06-11 12:26:29 +01001176 iuu_close(port);
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001177 return -EPROTO;
1178 } else {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001179 dbg("%s - rxcmd OK", __func__);
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001180 }
1181 return result;
1182}
1183
Olivier Bornet20eda942009-08-18 21:05:55 +02001184/* how to change VCC */
1185static int iuu_vcc_set(struct usb_serial_port *port, unsigned int vcc)
1186{
1187 int status;
1188 u8 *buf;
1189
1190 buf = kmalloc(5, GFP_KERNEL);
1191 if (!buf)
1192 return -ENOMEM;
1193
1194 dbg("%s - enter", __func__);
1195
1196 buf[0] = IUU_SET_VCC;
1197 buf[1] = vcc & 0xFF;
1198 buf[2] = (vcc >> 8) & 0xFF;
1199 buf[3] = (vcc >> 16) & 0xFF;
1200 buf[4] = (vcc >> 24) & 0xFF;
1201
1202 status = bulk_immediate(port, buf, 5);
1203 kfree(buf);
1204
1205 if (status != IUU_OPERATION_OK)
1206 dbg("%s - vcc error status = %2x", __func__, status);
1207 else
1208 dbg("%s - vcc OK !", __func__);
1209
1210 return status;
1211}
1212
1213/*
1214 * Sysfs Attributes
1215 */
1216
1217static ssize_t show_vcc_mode(struct device *dev,
1218 struct device_attribute *attr, char *buf)
1219{
1220 struct usb_serial_port *port = to_usb_serial_port(dev);
1221 struct iuu_private *priv = usb_get_serial_port_data(port);
1222
1223 return sprintf(buf, "%d\n", priv->vcc);
1224}
1225
1226static ssize_t store_vcc_mode(struct device *dev,
1227 struct device_attribute *attr, const char *buf, size_t count)
1228{
1229 struct usb_serial_port *port = to_usb_serial_port(dev);
1230 struct iuu_private *priv = usb_get_serial_port_data(port);
1231 unsigned long v;
1232
1233 if (strict_strtoul(buf, 10, &v)) {
1234 dev_err(dev, "%s - vcc_mode: %s is not a unsigned long\n",
1235 __func__, buf);
1236 goto fail_store_vcc_mode;
1237 }
1238
1239 dbg("%s: setting vcc_mode = %ld", __func__, v);
1240
1241 if ((v != 3) && (v != 5)) {
1242 dev_err(dev, "%s - vcc_mode %ld is invalid\n", __func__, v);
1243 } else {
1244 iuu_vcc_set(port, v);
1245 priv->vcc = v;
1246 }
1247fail_store_vcc_mode:
1248 return count;
1249}
1250
1251static DEVICE_ATTR(vcc_mode, S_IRUSR | S_IWUSR, show_vcc_mode,
1252 store_vcc_mode);
1253
1254static int iuu_create_sysfs_attrs(struct usb_serial_port *port)
1255{
1256 dbg("%s", __func__);
1257
1258 return device_create_file(&port->dev, &dev_attr_vcc_mode);
1259}
1260
1261static int iuu_remove_sysfs_attrs(struct usb_serial_port *port)
1262{
1263 dbg("%s", __func__);
1264
1265 device_remove_file(&port->dev, &dev_attr_vcc_mode);
1266 return 0;
1267}
1268
1269/*
1270 * End Sysfs Attributes
1271 */
1272
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001273static struct usb_serial_driver iuu_device = {
1274 .driver = {
1275 .owner = THIS_MODULE,
1276 .name = "iuu_phoenix",
1277 },
1278 .id_table = id_table,
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001279 .num_ports = 1,
Olivier Bornet20eda942009-08-18 21:05:55 +02001280 .port_probe = iuu_create_sysfs_attrs,
1281 .port_remove = iuu_remove_sysfs_attrs,
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001282 .open = iuu_open,
1283 .close = iuu_close,
1284 .write = iuu_uart_write,
1285 .read_bulk_callback = iuu_uart_read_callback,
1286 .tiocmget = iuu_tiocmget,
1287 .tiocmset = iuu_tiocmset,
Olivier Bornet96dab772009-06-11 12:54:20 +01001288 .set_termios = iuu_set_termios,
Alan Coxfe1ae7f2009-09-19 13:13:33 -07001289 .init_termios = iuu_init_termios,
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001290 .attach = iuu_startup,
Alan Sternf9c99bb2009-06-02 11:53:55 -04001291 .release = iuu_release,
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001292};
1293
1294static int __init iuu_init(void)
1295{
1296 int retval;
1297 retval = usb_serial_register(&iuu_device);
1298 if (retval)
1299 goto failed_usb_serial_register;
1300 retval = usb_register(&iuu_driver);
1301 if (retval)
1302 goto failed_usb_register;
Greg Kroah-Hartmanc197a8d2008-08-18 13:21:04 -07001303 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
1304 DRIVER_DESC "\n");
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001305 return 0;
1306failed_usb_register:
1307 usb_serial_deregister(&iuu_device);
1308failed_usb_serial_register:
1309 return retval;
1310}
1311
1312static void __exit iuu_exit(void)
1313{
1314 usb_deregister(&iuu_driver);
1315 usb_serial_deregister(&iuu_device);
1316}
1317
1318module_init(iuu_init);
1319module_exit(iuu_exit);
1320
1321MODULE_AUTHOR("Alain Degreffe eczema@ecze.com");
1322
1323MODULE_DESCRIPTION(DRIVER_DESC);
1324MODULE_LICENSE("GPL");
1325
1326MODULE_VERSION(DRIVER_VERSION);
1327module_param(debug, bool, S_IRUGO | S_IWUSR);
1328MODULE_PARM_DESC(debug, "Debug enabled or not");
1329
1330module_param(xmas, bool, S_IRUGO | S_IWUSR);
Olivier Bornet8844a322009-08-18 21:05:54 +02001331MODULE_PARM_DESC(xmas, "Xmas colors enabled or not");
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001332
1333module_param(boost, int, S_IRUGO | S_IWUSR);
Olivier Bornet8844a322009-08-18 21:05:54 +02001334MODULE_PARM_DESC(boost, "Card overclock boost (in percent 100-500)");
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001335
1336module_param(clockmode, int, S_IRUGO | S_IWUSR);
Olivier Bornet8844a322009-08-18 21:05:54 +02001337MODULE_PARM_DESC(clockmode, "Card clock mode (1=3.579 MHz, 2=3.680 MHz, "
1338 "3=6 Mhz)");
Alain Degreffe60a8fc02007-10-26 13:51:49 +02001339
1340module_param(cdmode, int, S_IRUGO | S_IWUSR);
Olivier Bornet8844a322009-08-18 21:05:54 +02001341MODULE_PARM_DESC(cdmode, "Card detect mode (0=none, 1=CD, 2=!CD, 3=DSR, "
1342 "4=!DSR, 5=CTS, 6=!CTS, 7=RING, 8=!RING)");
Olivier Bornete55c6d02009-08-18 21:05:57 +02001343
1344module_param(vcc_default, int, S_IRUGO | S_IWUSR);
1345MODULE_PARM_DESC(vcc_default, "Set default VCC (either 3 for 3.3V or 5 "
1346 "for 5V). Default to 5.");