Merge branch 'core-fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-2.6] / drivers / usb / serial / iuu_phoenix.c
1 /*
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
35 static int debug = 1;
36 #else
37 static int debug;
38 #endif
39
40 /*
41  * Version Information
42  */
43 #define DRIVER_VERSION "v0.5"
44 #define DRIVER_DESC "Infinity USB Unlimited Phoenix driver"
45
46 static struct usb_device_id id_table[] = {
47         {USB_DEVICE(IUU_USB_VENDOR_ID, IUU_USB_PRODUCT_ID)},
48         {}                      /* Terminating entry */
49 };
50 MODULE_DEVICE_TABLE(usb, id_table);
51
52 static 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 */
61 static int boost = 100;
62 static int clockmode = 1;
63 static int cdmode = 1;
64 static int iuu_cardin;
65 static int iuu_cardout;
66 static int xmas;
67
68 static void read_rxcmd_callback(struct urb *urb);
69
70 struct iuu_private {
71         spinlock_t lock;        /* store irq state */
72         wait_queue_head_t delta_msr_wait;
73         u8 line_control;
74         u8 line_status;
75         u8 termios_initialized;
76         int tiostatus;          /* store IUART SIGNAL for tiocmget call */
77         u8 reset;               /* if 1 reset is needed */
78         int poll;               /* number of poll */
79         u8 *writebuf;           /* buffer for writing to device */
80         int writelen;           /* num of byte to write to device */
81         u8 *buf;                /* used for initialize speed */
82         u8 *dbgbuf;             /* debug buffer */
83         u8 len;
84 };
85
86
87 static void iuu_free_buf(struct iuu_private *priv)
88 {
89         kfree(priv->buf);
90         kfree(priv->dbgbuf);
91         kfree(priv->writebuf);
92 }
93
94 static 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);
101                 dbg("%s problem allocation buffer", __func__);
102                 return -ENOMEM;
103         }
104         dbg("%s - Privates buffers allocation success", __func__);
105         return 0;
106 }
107
108 static int iuu_startup(struct usb_serial *serial)
109 {
110         struct iuu_private *priv;
111         priv = kzalloc(sizeof(struct iuu_private), GFP_KERNEL);
112         dbg("%s- priv allocation success", __func__);
113         if (!priv)
114                 return -ENOMEM;
115         if (iuu_alloc_buf(priv)) {
116                 kfree(priv);
117                 return -ENOMEM;
118         }
119         spin_lock_init(&priv->lock);
120         init_waitqueue_head(&priv->delta_msr_wait);
121         usb_set_serial_port_data(serial->port[0], priv);
122         return 0;
123 }
124
125 /* Shutdown function */
126 static void iuu_shutdown(struct usb_serial *serial)
127 {
128         struct usb_serial_port *port = serial->port[0];
129         struct iuu_private *priv = usb_get_serial_port_data(port);
130         if (!port)
131                 return;
132
133         dbg("%s", __func__);
134
135         if (priv) {
136                 iuu_free_buf(priv);
137                 dbg("%s - I will free all", __func__);
138                 usb_set_serial_port_data(port, NULL);
139
140                 dbg("%s - priv is not anymore in port structure", __func__);
141                 kfree(priv);
142
143                 dbg("%s priv is now kfree", __func__);
144         }
145 }
146
147 static int iuu_tiocmset(struct tty_struct *tty, struct file *file,
148                         unsigned int set, unsigned int clear)
149 {
150         struct usb_serial_port *port = tty->driver_data;
151         struct iuu_private *priv = usb_get_serial_port_data(port);
152         unsigned long flags;
153
154         /* FIXME: locking on tiomstatus */
155         dbg("%s (%d) msg : SET = 0x%04x, CLEAR = 0x%04x ", __func__,
156             port->number, set, clear);
157
158         spin_lock_irqsave(&priv->lock, flags);
159         if (set & TIOCM_RTS)
160                 priv->tiostatus = TIOCM_RTS;
161
162         if (!(set & TIOCM_RTS) && priv->tiostatus == TIOCM_RTS) {
163                 dbg("%s TIOCMSET RESET called !!!", __func__);
164                 priv->reset = 1;
165         }
166         spin_unlock_irqrestore(&priv->lock, flags);
167         return 0;
168 }
169
170 /* This is used to provide a carrier detect mechanism
171  * When a card is present, the response is 0x00
172  * When no card , the reader respond with TIOCM_CD
173  * This is known as CD autodetect mechanism
174  */
175 static int iuu_tiocmget(struct tty_struct *tty, struct file *file)
176 {
177         struct usb_serial_port *port = tty->driver_data;
178         struct iuu_private *priv = usb_get_serial_port_data(port);
179         unsigned long flags;
180         int rc;
181
182         spin_lock_irqsave(&priv->lock, flags);
183         rc = priv->tiostatus;
184         spin_unlock_irqrestore(&priv->lock, flags);
185
186         return rc;
187 }
188
189 static void iuu_rxcmd(struct urb *urb)
190 {
191         struct usb_serial_port *port = urb->context;
192         int result;
193         dbg("%s - enter", __func__);
194
195         if (urb->status) {
196                 dbg("%s - urb->status = %d", __func__, urb->status);
197                 /* error stop all */
198                 return;
199         }
200
201
202         memset(port->write_urb->transfer_buffer, IUU_UART_RX, 1);
203         usb_fill_bulk_urb(port->write_urb, port->serial->dev,
204                           usb_sndbulkpipe(port->serial->dev,
205                                           port->bulk_out_endpointAddress),
206                           port->write_urb->transfer_buffer, 1,
207                           read_rxcmd_callback, port);
208         result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
209 }
210
211 static int iuu_reset(struct usb_serial_port *port, u8 wt)
212 {
213         struct iuu_private *priv = usb_get_serial_port_data(port);
214         int result;
215         char *buf_ptr = port->write_urb->transfer_buffer;
216         dbg("%s - enter", __func__);
217
218         /* Prepare the reset sequence */
219
220         *buf_ptr++ = IUU_RST_SET;
221         *buf_ptr++ = IUU_DELAY_MS;
222         *buf_ptr++ = wt;
223         *buf_ptr = IUU_RST_CLEAR;
224
225         /* send the sequence */
226
227         usb_fill_bulk_urb(port->write_urb,
228                           port->serial->dev,
229                           usb_sndbulkpipe(port->serial->dev,
230                                           port->bulk_out_endpointAddress),
231                           port->write_urb->transfer_buffer, 4, iuu_rxcmd, port);
232         result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
233         priv->reset = 0;
234         return result;
235 }
236
237 /* Status Function
238  * Return value is
239  * 0x00 = no card
240  * 0x01 = smartcard
241  * 0x02 = sim card
242  */
243 static void iuu_update_status_callback(struct urb *urb)
244 {
245         struct usb_serial_port *port = urb->context;
246         struct iuu_private *priv = usb_get_serial_port_data(port);
247         u8 *st;
248         dbg("%s - enter", __func__);
249
250         if (urb->status) {
251                 dbg("%s - urb->status = %d", __func__, urb->status);
252                 /* error stop all */
253                 return;
254         }
255
256         st = urb->transfer_buffer;
257         dbg("%s - enter", __func__);
258         if (urb->actual_length == 1) {
259                 switch (st[0]) {
260                 case 0x1:
261                         priv->tiostatus = iuu_cardout;
262                         break;
263                 case 0x0:
264                         priv->tiostatus = iuu_cardin;
265                         break;
266                 default:
267                         priv->tiostatus = iuu_cardin;
268                 }
269         }
270         iuu_rxcmd(urb);
271 }
272
273 static void iuu_status_callback(struct urb *urb)
274 {
275         struct usb_serial_port *port = urb->context;
276         int result;
277         dbg("%s - enter", __func__);
278
279         dbg("%s - urb->status = %d", __func__, urb->status);
280         usb_fill_bulk_urb(port->read_urb, port->serial->dev,
281                           usb_rcvbulkpipe(port->serial->dev,
282                                           port->bulk_in_endpointAddress),
283                           port->read_urb->transfer_buffer, 256,
284                           iuu_update_status_callback, port);
285         result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
286 }
287
288 static int iuu_status(struct usb_serial_port *port)
289 {
290         int result;
291
292         dbg("%s - enter", __func__);
293
294         memset(port->write_urb->transfer_buffer, IUU_GET_STATE_REGISTER, 1);
295         usb_fill_bulk_urb(port->write_urb, port->serial->dev,
296                           usb_sndbulkpipe(port->serial->dev,
297                                           port->bulk_out_endpointAddress),
298                           port->write_urb->transfer_buffer, 1,
299                           iuu_status_callback, port);
300         result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
301         return result;
302
303 }
304
305 static int bulk_immediate(struct usb_serial_port *port, u8 *buf, u8 count)
306 {
307         int status;
308         struct usb_serial *serial = port->serial;
309         int actual = 0;
310
311         dbg("%s - enter", __func__);
312
313         /* send the data out the bulk port */
314
315         status =
316             usb_bulk_msg(serial->dev,
317                          usb_sndbulkpipe(serial->dev,
318                                          port->bulk_out_endpointAddress), buf,
319                          count, &actual, HZ * 1);
320
321         if (status != IUU_OPERATION_OK)
322                 dbg("%s - error = %2x", __func__, status);
323         else
324                 dbg("%s - write OK !", __func__);
325         return status;
326 }
327
328 static int read_immediate(struct usb_serial_port *port, u8 *buf, u8 count)
329 {
330         int status;
331         struct usb_serial *serial = port->serial;
332         int actual = 0;
333
334         dbg("%s - enter", __func__);
335
336         /* send the data out the bulk port */
337
338         status =
339             usb_bulk_msg(serial->dev,
340                          usb_rcvbulkpipe(serial->dev,
341                                          port->bulk_in_endpointAddress), buf,
342                          count, &actual, HZ * 1);
343
344         if (status != IUU_OPERATION_OK)
345                 dbg("%s - error = %2x", __func__, status);
346         else
347                 dbg("%s - read OK !", __func__);
348         return status;
349 }
350
351 static int iuu_led(struct usb_serial_port *port, unsigned int R,
352                    unsigned int G, unsigned int B, u8 f)
353 {
354         int status;
355         u8 *buf;
356         buf = kmalloc(8, GFP_KERNEL);
357         if (!buf)
358                 return -ENOMEM;
359
360         dbg("%s - enter", __func__);
361
362         buf[0] = IUU_SET_LED;
363         buf[1] = R & 0xFF;
364         buf[2] = (R >> 8) & 0xFF;
365         buf[3] = G & 0xFF;
366         buf[4] = (G >> 8) & 0xFF;
367         buf[5] = B & 0xFF;
368         buf[6] = (B >> 8) & 0xFF;
369         buf[7] = f;
370         status = bulk_immediate(port, buf, 8);
371         kfree(buf);
372         if (status != IUU_OPERATION_OK)
373                 dbg("%s - led error status = %2x", __func__, status);
374         else
375                 dbg("%s - led OK !", __func__);
376         return IUU_OPERATION_OK;
377 }
378
379 static void iuu_rgbf_fill_buffer(u8 *buf, u8 r1, u8 r2, u8 g1, u8 g2, u8 b1,
380                                  u8 b2, u8 freq)
381 {
382         *buf++ = IUU_SET_LED;
383         *buf++ = r1;
384         *buf++ = r2;
385         *buf++ = g1;
386         *buf++ = g2;
387         *buf++ = b1;
388         *buf++ = b2;
389         *buf = freq;
390 }
391
392 static void iuu_led_activity_on(struct urb *urb)
393 {
394         struct usb_serial_port *port = urb->context;
395         int result;
396         char *buf_ptr = port->write_urb->transfer_buffer;
397         *buf_ptr++ = IUU_SET_LED;
398         if (xmas == 1) {
399                 get_random_bytes(buf_ptr, 6);
400                 *(buf_ptr+7) = 1;
401         } else {
402                 iuu_rgbf_fill_buffer(buf_ptr, 255, 255, 0, 0, 0, 0, 255);
403         }
404
405         usb_fill_bulk_urb(port->write_urb, port->serial->dev,
406                           usb_sndbulkpipe(port->serial->dev,
407                                           port->bulk_out_endpointAddress),
408                           port->write_urb->transfer_buffer, 8 ,
409                           iuu_rxcmd, port);
410         result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
411 }
412
413 static void iuu_led_activity_off(struct urb *urb)
414 {
415         struct usb_serial_port *port = urb->context;
416         int result;
417         char *buf_ptr = port->write_urb->transfer_buffer;
418         if (xmas == 1) {
419                 iuu_rxcmd(urb);
420                 return;
421         } else {
422                 *buf_ptr++ = IUU_SET_LED;
423                 iuu_rgbf_fill_buffer(buf_ptr, 0, 0, 255, 255, 0, 0, 255);
424         }
425         usb_fill_bulk_urb(port->write_urb, port->serial->dev,
426                           usb_sndbulkpipe(port->serial->dev,
427                                           port->bulk_out_endpointAddress),
428                           port->write_urb->transfer_buffer, 8 ,
429                           iuu_rxcmd, port);
430         result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
431 }
432
433
434
435 static int iuu_clk(struct usb_serial_port *port, int dwFrq)
436 {
437         int status;
438         struct iuu_private *priv = usb_get_serial_port_data(port);
439         int Count = 0;
440         u8 FrqGenAdr = 0x69;
441         u8 DIV = 0;             /* 8bit */
442         u8 XDRV = 0;            /* 8bit */
443         u8 PUMP = 0;            /* 3bit */
444         u8 PBmsb = 0;           /* 2bit */
445         u8 PBlsb = 0;           /* 8bit */
446         u8 PO = 0;              /* 1bit */
447         u8 Q = 0;               /* 7bit */
448         /* 24bit = 3bytes */
449         unsigned int P = 0;
450         unsigned int P2 = 0;
451         int frq = (int)dwFrq;
452
453         dbg("%s - enter", __func__);
454
455         if (frq == 0) {
456                 priv->buf[Count++] = IUU_UART_WRITE_I2C;
457                 priv->buf[Count++] = FrqGenAdr << 1;
458                 priv->buf[Count++] = 0x09;
459                 priv->buf[Count++] = 0x00;
460
461                 status = bulk_immediate(port, (u8 *) priv->buf, Count);
462                 if (status != 0) {
463                         dbg("%s - write error ", __func__);
464                         return status;
465                 }
466         } else if (frq == 3579000) {
467                 DIV = 100;
468                 P = 1193;
469                 Q = 40;
470                 XDRV = 0;
471         } else if (frq == 3680000) {
472                 DIV = 105;
473                 P = 161;
474                 Q = 5;
475                 XDRV = 0;
476         } else if (frq == 6000000) {
477                 DIV = 66;
478                 P = 66;
479                 Q = 2;
480                 XDRV = 0x28;
481         } else {
482                 unsigned int result = 0;
483                 unsigned int tmp = 0;
484                 unsigned int check;
485                 unsigned int check2;
486                 char found = 0x00;
487                 unsigned int lQ = 2;
488                 unsigned int lP = 2055;
489                 unsigned int lDiv = 4;
490
491                 for (lQ = 2; lQ <= 47 && !found; lQ++)
492                         for (lP = 2055; lP >= 8 && !found; lP--)
493                                 for (lDiv = 4; lDiv <= 127 && !found; lDiv++) {
494                                         tmp = (12000000 / lDiv) * (lP / lQ);
495                                         if (abs((int)(tmp - frq)) <
496                                             abs((int)(frq - result))) {
497                                                 check2 = (12000000 / lQ);
498                                                 if (check2 < 250000)
499                                                         continue;
500                                                 check = (12000000 / lQ) * lP;
501                                                 if (check > 400000000)
502                                                         continue;
503                                                 if (check < 100000000)
504                                                         continue;
505                                                 if (lDiv < 4 || lDiv > 127)
506                                                         continue;
507                                                 result = tmp;
508                                                 P = lP;
509                                                 DIV = lDiv;
510                                                 Q = lQ;
511                                                 if (result == frq)
512                                                         found = 0x01;
513                                         }
514                                 }
515         }
516         P2 = ((P - PO) / 2) - 4;
517         DIV = DIV;
518         PUMP = 0x04;
519         PBmsb = (P2 >> 8 & 0x03);
520         PBlsb = P2 & 0xFF;
521         PO = (P >> 10) & 0x01;
522         Q = Q - 2;
523
524         priv->buf[Count++] = IUU_UART_WRITE_I2C;        /* 0x4C */
525         priv->buf[Count++] = FrqGenAdr << 1;
526         priv->buf[Count++] = 0x09;
527         priv->buf[Count++] = 0x20;      /* Adr = 0x09 */
528         priv->buf[Count++] = IUU_UART_WRITE_I2C;        /* 0x4C */
529         priv->buf[Count++] = FrqGenAdr << 1;
530         priv->buf[Count++] = 0x0C;
531         priv->buf[Count++] = DIV;       /* Adr = 0x0C */
532         priv->buf[Count++] = IUU_UART_WRITE_I2C;        /* 0x4C */
533         priv->buf[Count++] = FrqGenAdr << 1;
534         priv->buf[Count++] = 0x12;
535         priv->buf[Count++] = XDRV;      /* Adr = 0x12 */
536         priv->buf[Count++] = IUU_UART_WRITE_I2C;        /*  0x4C */
537         priv->buf[Count++] = FrqGenAdr << 1;
538         priv->buf[Count++] = 0x13;
539         priv->buf[Count++] = 0x6B;      /* Adr = 0x13 */
540         priv->buf[Count++] = IUU_UART_WRITE_I2C;        /*  0x4C */
541         priv->buf[Count++] = FrqGenAdr << 1;
542         priv->buf[Count++] = 0x40;
543         priv->buf[Count++] = (0xC0 | ((PUMP & 0x07) << 2)) |
544                              (PBmsb & 0x03);    /* Adr = 0x40 */
545         priv->buf[Count++] = IUU_UART_WRITE_I2C;        /*  0x4C */
546         priv->buf[Count++] = FrqGenAdr << 1;
547         priv->buf[Count++] = 0x41;
548         priv->buf[Count++] = PBlsb;     /* Adr = 0x41 */
549         priv->buf[Count++] = IUU_UART_WRITE_I2C;        /*  0x4C */
550         priv->buf[Count++] = FrqGenAdr << 1;
551         priv->buf[Count++] = 0x42;
552         priv->buf[Count++] = Q | (((PO & 0x01) << 7));  /* Adr = 0x42 */
553         priv->buf[Count++] = IUU_UART_WRITE_I2C;        /*  0x4C */
554         priv->buf[Count++] = FrqGenAdr << 1;
555         priv->buf[Count++] = 0x44;
556         priv->buf[Count++] = (char)0xFF;        /* Adr = 0x44 */
557         priv->buf[Count++] = IUU_UART_WRITE_I2C;        /*  0x4C */
558         priv->buf[Count++] = FrqGenAdr << 1;
559         priv->buf[Count++] = 0x45;
560         priv->buf[Count++] = (char)0xFE;        /* Adr = 0x45 */
561         priv->buf[Count++] = IUU_UART_WRITE_I2C;        /*  0x4C */
562         priv->buf[Count++] = FrqGenAdr << 1;
563         priv->buf[Count++] = 0x46;
564         priv->buf[Count++] = 0x7F;      /* Adr = 0x46 */
565         priv->buf[Count++] = IUU_UART_WRITE_I2C;        /*  0x4C */
566         priv->buf[Count++] = FrqGenAdr << 1;
567         priv->buf[Count++] = 0x47;
568         priv->buf[Count++] = (char)0x84;        /* Adr = 0x47 */
569
570         status = bulk_immediate(port, (u8 *) priv->buf, Count);
571         if (status != IUU_OPERATION_OK)
572                 dbg("%s - write error ", __func__);
573         return status;
574 }
575
576 static int iuu_uart_flush(struct usb_serial_port *port)
577 {
578         int i;
579         int status;
580         u8 rxcmd = IUU_UART_RX;
581         struct iuu_private *priv = usb_get_serial_port_data(port);
582
583         dbg("%s - enter", __func__);
584
585         if (iuu_led(port, 0xF000, 0, 0, 0xFF) < 0)
586                 return -EIO;
587
588         for (i = 0; i < 2; i++) {
589                 status = bulk_immediate(port, &rxcmd, 1);
590                 if (status != IUU_OPERATION_OK) {
591                         dbg("%s - uart_flush_write error", __func__);
592                         return status;
593                 }
594
595                 status = read_immediate(port, &priv->len, 1);
596                 if (status != IUU_OPERATION_OK) {
597                         dbg("%s - uart_flush_read error", __func__);
598                         return status;
599                 }
600
601                 if (priv->len > 0) {
602                         dbg("%s - uart_flush datalen is : %i ", __func__,
603                             priv->len);
604                         status = read_immediate(port, priv->buf, priv->len);
605                         if (status != IUU_OPERATION_OK) {
606                                 dbg("%s - uart_flush_read error", __func__);
607                                 return status;
608                         }
609                 }
610         }
611         dbg("%s - uart_flush_read OK!", __func__);
612         iuu_led(port, 0, 0xF000, 0, 0xFF);
613         return status;
614 }
615
616 static void read_buf_callback(struct urb *urb)
617 {
618         struct usb_serial_port *port = urb->context;
619         unsigned char *data = urb->transfer_buffer;
620         struct tty_struct *tty;
621         dbg("%s - urb->status = %d", __func__, urb->status);
622
623         if (urb->status) {
624                 dbg("%s - urb->status = %d", __func__, urb->status);
625                 if (urb->status == -EPROTO) {
626                         /* reschedule needed */
627                 }
628                 return;
629         }
630
631         dbg("%s - %i chars to write", __func__, urb->actual_length);
632         tty = port->port.tty;
633         if (data == NULL)
634                 dbg("%s - data is NULL !!!", __func__);
635         if (tty && urb->actual_length && data) {
636                 tty_insert_flip_string(tty, data, urb->actual_length);
637                 tty_flip_buffer_push(tty);
638         }
639         iuu_led_activity_on(urb);
640 }
641
642 static int iuu_bulk_write(struct usb_serial_port *port)
643 {
644         struct iuu_private *priv = usb_get_serial_port_data(port);
645         unsigned long flags;
646         int result;
647         int i;
648         char *buf_ptr = port->write_urb->transfer_buffer;
649         dbg("%s - enter", __func__);
650
651         *buf_ptr++ = IUU_UART_ESC;
652         *buf_ptr++ = IUU_UART_TX;
653         *buf_ptr++ = priv->writelen;
654
655         memcpy(buf_ptr, priv->writebuf,
656                priv->writelen);
657         if (debug == 1) {
658                 for (i = 0; i < priv->writelen; i++)
659                         sprintf(priv->dbgbuf + i*2 ,
660                                 "%02X", priv->writebuf[i]);
661                 priv->dbgbuf[priv->writelen+i*2] = 0;
662                 dbg("%s - writing %i chars : %s", __func__,
663                     priv->writelen, priv->dbgbuf);
664         }
665         usb_fill_bulk_urb(port->write_urb, port->serial->dev,
666                           usb_sndbulkpipe(port->serial->dev,
667                                           port->bulk_out_endpointAddress),
668                           port->write_urb->transfer_buffer, priv->writelen + 3,
669                           iuu_rxcmd, port);
670         result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
671         spin_lock_irqsave(&priv->lock, flags);
672         priv->writelen = 0;
673         spin_unlock_irqrestore(&priv->lock, flags);
674         usb_serial_port_softint(port);
675         return result;
676 }
677
678 static int iuu_read_buf(struct usb_serial_port *port, int len)
679 {
680         int result;
681         dbg("%s - enter", __func__);
682
683         usb_fill_bulk_urb(port->read_urb, port->serial->dev,
684                           usb_rcvbulkpipe(port->serial->dev,
685                                           port->bulk_in_endpointAddress),
686                           port->read_urb->transfer_buffer, len,
687                           read_buf_callback, port);
688         result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
689         return result;
690 }
691
692 static void iuu_uart_read_callback(struct urb *urb)
693 {
694         struct usb_serial_port *port = urb->context;
695         struct iuu_private *priv = usb_get_serial_port_data(port);
696         unsigned long flags;
697         int status;
698         int error = 0;
699         int len = 0;
700         unsigned char *data = urb->transfer_buffer;
701         priv->poll++;
702
703         dbg("%s - enter", __func__);
704
705         if (urb->status) {
706                 dbg("%s - urb->status = %d", __func__, urb->status);
707                 /* error stop all */
708                 return;
709         }
710         if (data == NULL)
711                 dbg("%s - data is NULL !!!", __func__);
712
713         if (urb->actual_length == 1  && data != NULL)
714                 len = (int) data[0];
715
716         if (urb->actual_length > 1) {
717                 dbg("%s - urb->actual_length = %i", __func__,
718                     urb->actual_length);
719                 error = 1;
720                 return;
721         }
722         /* if len > 0 call readbuf */
723
724         if (len > 0 && error == 0) {
725                 dbg("%s - call read buf - len to read is %i ",
726                         __func__, len);
727                 status = iuu_read_buf(port, len);
728                 return;
729         }
730         /* need to update status  ? */
731         if (priv->poll > 99) {
732                 status = iuu_status(port);
733                 priv->poll = 0;
734                 return;
735         }
736
737         /* reset waiting ? */
738
739         if (priv->reset == 1) {
740                 status = iuu_reset(port, 0xC);
741                 return;
742         }
743         /* Writebuf is waiting */
744         spin_lock_irqsave(&priv->lock, flags);
745         if (priv->writelen > 0) {
746                 spin_unlock_irqrestore(&priv->lock, flags);
747                 status = iuu_bulk_write(port);
748                 return;
749         }
750         spin_unlock_irqrestore(&priv->lock, flags);
751         /* if nothing to write call again rxcmd */
752         dbg("%s - rxcmd recall", __func__);
753         iuu_led_activity_off(urb);
754 }
755
756 static int iuu_uart_write(struct tty_struct *tty, struct usb_serial_port *port,
757                           const u8 *buf, int count)
758 {
759         struct iuu_private *priv = usb_get_serial_port_data(port);
760         unsigned long flags;
761         dbg("%s - enter", __func__);
762
763         if (count > 256)
764                 return -ENOMEM;
765
766         spin_lock_irqsave(&priv->lock, flags);
767         if (priv->writelen > 0) {
768                 /* buffer already filled but not commited */
769                 spin_unlock_irqrestore(&priv->lock, flags);
770                 return 0;
771         }
772         /* fill the buffer */
773         memcpy(priv->writebuf, buf, count);
774         priv->writelen = count;
775         spin_unlock_irqrestore(&priv->lock, flags);
776
777         return count;
778 }
779
780 static void read_rxcmd_callback(struct urb *urb)
781 {
782         struct usb_serial_port *port = urb->context;
783         int result;
784         dbg("%s - enter", __func__);
785
786         dbg("%s - urb->status = %d", __func__, urb->status);
787
788         if (urb->status) {
789                 dbg("%s - urb->status = %d", __func__, urb->status);
790                 /* error stop all */
791                 return;
792         }
793
794         usb_fill_bulk_urb(port->read_urb, port->serial->dev,
795                           usb_rcvbulkpipe(port->serial->dev,
796                                           port->bulk_in_endpointAddress),
797                           port->read_urb->transfer_buffer, 256,
798                           iuu_uart_read_callback, port);
799         result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
800         dbg("%s - submit result = %d", __func__, result);
801         return;
802 }
803
804 static int iuu_uart_on(struct usb_serial_port *port)
805 {
806         int status;
807         u8 *buf;
808
809         buf = kmalloc(sizeof(u8) * 4, GFP_KERNEL);
810
811         if (!buf)
812                 return -ENOMEM;
813
814         buf[0] = IUU_UART_ENABLE;
815         buf[1] = (u8) ((IUU_BAUD_9600 >> 8) & 0x00FF);
816         buf[2] = (u8) (0x00FF & IUU_BAUD_9600);
817         buf[3] = (u8) (0x0F0 & IUU_TWO_STOP_BITS) | (0x07 & IUU_PARITY_EVEN);
818
819         status = bulk_immediate(port, buf, 4);
820         if (status != IUU_OPERATION_OK) {
821                 dbg("%s - uart_on error", __func__);
822                 goto uart_enable_failed;
823         }
824         /*  iuu_reset() the card after iuu_uart_on() */
825         status = iuu_uart_flush(port);
826         if (status != IUU_OPERATION_OK)
827                 dbg("%s - uart_flush error", __func__);
828 uart_enable_failed:
829         kfree(buf);
830         return status;
831 }
832
833 /*  Diables the IUU UART (a.k.a. the Phoenix voiderface) */
834 static int iuu_uart_off(struct usb_serial_port *port)
835 {
836         int status;
837         u8 *buf;
838         buf = kmalloc(1, GFP_KERNEL);
839         if (!buf)
840                 return -ENOMEM;
841         buf[0] = IUU_UART_DISABLE;
842
843         status = bulk_immediate(port, buf, 1);
844         if (status != IUU_OPERATION_OK)
845                 dbg("%s - uart_off error", __func__);
846
847         kfree(buf);
848         return status;
849 }
850
851 static int iuu_uart_baud(struct usb_serial_port *port, u32 baud,
852                          u32 *actual, u8 parity)
853 {
854         int status;
855         u8 *dataout;
856         u8 DataCount = 0;
857         u8 T1Frekvens = 0;
858         u8 T1reload = 0;
859         unsigned int T1FrekvensHZ = 0;
860
861         dataout = kmalloc(sizeof(u8) * 5, GFP_KERNEL);
862
863         if (!dataout)
864                 return -ENOMEM;
865
866         if (baud < 1200 || baud > 230400) {
867                 kfree(dataout);
868                 return IUU_INVALID_PARAMETER;
869         }
870         if (baud > 977) {
871                 T1Frekvens = 3;
872                 T1FrekvensHZ = 500000;
873         }
874
875         if (baud > 3906) {
876                 T1Frekvens = 2;
877                 T1FrekvensHZ = 2000000;
878         }
879
880         if (baud > 11718) {
881                 T1Frekvens = 1;
882                 T1FrekvensHZ = 6000000;
883         }
884
885         if (baud > 46875) {
886                 T1Frekvens = 0;
887                 T1FrekvensHZ = 24000000;
888         }
889
890         T1reload = 256 - (u8) (T1FrekvensHZ / (baud * 2));
891
892         /*  magic number here:  ENTER_FIRMWARE_UPDATE; */
893         dataout[DataCount++] = IUU_UART_ESC;
894         /*  magic number here:  CHANGE_BAUD; */
895         dataout[DataCount++] = IUU_UART_CHANGE;
896         dataout[DataCount++] = T1Frekvens;
897         dataout[DataCount++] = T1reload;
898
899         *actual = (T1FrekvensHZ / (256 - T1reload)) / 2;
900
901         switch (parity & 0x0F) {
902         case IUU_PARITY_NONE:
903                 dataout[DataCount++] = 0x00;
904                 break;
905         case IUU_PARITY_EVEN:
906                 dataout[DataCount++] = 0x01;
907                 break;
908         case IUU_PARITY_ODD:
909                 dataout[DataCount++] = 0x02;
910                 break;
911         case IUU_PARITY_MARK:
912                 dataout[DataCount++] = 0x03;
913                 break;
914         case IUU_PARITY_SPACE:
915                 dataout[DataCount++] = 0x04;
916                 break;
917         default:
918                 kfree(dataout);
919                 return IUU_INVALID_PARAMETER;
920                 break;
921         }
922
923         switch (parity & 0xF0) {
924         case IUU_ONE_STOP_BIT:
925                 dataout[DataCount - 1] |= IUU_ONE_STOP_BIT;
926                 break;
927
928         case IUU_TWO_STOP_BITS:
929                 dataout[DataCount - 1] |= IUU_TWO_STOP_BITS;
930                 break;
931         default:
932                 kfree(dataout);
933                 return IUU_INVALID_PARAMETER;
934                 break;
935         }
936
937         status = bulk_immediate(port, dataout, DataCount);
938         if (status != IUU_OPERATION_OK)
939                 dbg("%s - uart_off error", __func__);
940         kfree(dataout);
941         return status;
942 }
943
944 static int set_control_lines(struct usb_device *dev, u8 value)
945 {
946         return 0;
947 }
948
949 static void iuu_close(struct tty_struct *tty,
950                         struct usb_serial_port *port, struct file *filp)
951 {
952         /* iuu_led (port,255,0,0,0); */
953         struct usb_serial *serial;
954         struct iuu_private *priv = usb_get_serial_port_data(port);
955         unsigned long flags;
956         unsigned int c_cflag;
957
958         serial = port->serial;
959         if (!serial)
960                 return;
961
962         dbg("%s - port %d", __func__, port->number);
963
964         iuu_uart_off(port);
965         if (serial->dev) {
966                 if (tty) {
967                         c_cflag = tty->termios->c_cflag;
968                         if (c_cflag & HUPCL) {
969                                 /* drop DTR and RTS */
970                                 priv = usb_get_serial_port_data(port);
971                                 spin_lock_irqsave(&priv->lock, flags);
972                                 priv->line_control = 0;
973                                 spin_unlock_irqrestore(&priv->lock, flags);
974                                 set_control_lines(port->serial->dev, 0);
975                         }
976                 }
977                 /* free writebuf */
978                 /* shutdown our urbs */
979                 dbg("%s - shutting down urbs", __func__);
980                 usb_kill_urb(port->write_urb);
981                 usb_kill_urb(port->read_urb);
982                 usb_kill_urb(port->interrupt_in_urb);
983                 msleep(1000);
984                 /* wait one second to free all buffers */
985                 iuu_led(port, 0, 0, 0xF000, 0xFF);
986                 msleep(1000);
987                 usb_reset_device(port->serial->dev);
988         }
989 }
990
991 static int iuu_open(struct tty_struct *tty,
992                         struct usb_serial_port *port, struct file *filp)
993 {
994         struct usb_serial *serial = port->serial;
995         u8 *buf;
996         int result;
997         u32 actual;
998         unsigned long flags;
999         struct iuu_private *priv = usb_get_serial_port_data(port);
1000
1001         dbg("%s -  port %d", __func__, port->number);
1002         usb_clear_halt(serial->dev, port->write_urb->pipe);
1003         usb_clear_halt(serial->dev, port->read_urb->pipe);
1004
1005         buf = kmalloc(10, GFP_KERNEL);
1006         if (buf == NULL)
1007                 return -ENOMEM;
1008
1009         /* fixup the endpoint buffer size */
1010         kfree(port->bulk_out_buffer);
1011         port->bulk_out_buffer = kmalloc(512, GFP_KERNEL);
1012         port->bulk_out_size = 512;
1013         kfree(port->bulk_in_buffer);
1014         port->bulk_in_buffer = kmalloc(512, GFP_KERNEL);
1015         port->bulk_in_size = 512;
1016
1017         if (!port->bulk_out_buffer || !port->bulk_in_buffer) {
1018                 kfree(port->bulk_out_buffer);
1019                 kfree(port->bulk_in_buffer);
1020                 kfree(buf);
1021                 return -ENOMEM;
1022         }
1023
1024         usb_fill_bulk_urb(port->write_urb, port->serial->dev,
1025                           usb_sndbulkpipe(port->serial->dev,
1026                                           port->bulk_out_endpointAddress),
1027                           port->bulk_out_buffer, 512,
1028                           NULL, NULL);
1029
1030
1031         usb_fill_bulk_urb(port->read_urb, port->serial->dev,
1032                           usb_rcvbulkpipe(port->serial->dev,
1033                                           port->bulk_in_endpointAddress),
1034                           port->bulk_in_buffer, 512,
1035                           NULL, NULL);
1036
1037         /* set the termios structure */
1038         spin_lock_irqsave(&priv->lock, flags);
1039         if (tty && !priv->termios_initialized) {
1040                 *(tty->termios) = tty_std_termios;
1041                 tty->termios->c_cflag = CLOCAL | CREAD | CS8 | B9600
1042                                         | TIOCM_CTS | CSTOPB | PARENB;
1043                 tty->termios->c_ispeed = 9600;
1044                 tty->termios->c_ospeed = 9600;
1045                 tty->termios->c_lflag = 0;
1046                 tty->termios->c_oflag = 0;
1047                 tty->termios->c_iflag = 0;
1048                 priv->termios_initialized = 1;
1049                 tty->low_latency = 1;
1050                 priv->poll = 0;
1051          }
1052         spin_unlock_irqrestore(&priv->lock, flags);
1053
1054         /* initialize writebuf */
1055 #define FISH(a, b, c, d) do { \
1056         result = usb_control_msg(port->serial->dev,     \
1057                                 usb_rcvctrlpipe(port->serial->dev, 0),  \
1058                                 b, a, c, d, buf, 1, 1000); \
1059         dbg("0x%x:0x%x:0x%x:0x%x  %d - %x", a, b, c, d, result, \
1060                                 buf[0]); } while (0);
1061
1062 #define SOUP(a, b, c, d)  do { \
1063         result = usb_control_msg(port->serial->dev,     \
1064                                 usb_sndctrlpipe(port->serial->dev, 0),  \
1065                                 b, a, c, d, NULL, 0, 1000); \
1066         dbg("0x%x:0x%x:0x%x:0x%x  %d", a, b, c, d, result); } while (0)
1067
1068         /*  This is not UART related but IUU USB driver related or something */
1069         /*  like that. Basically no IUU will accept any commands from the USB */
1070         /*  host unless it has received the following message */
1071         /* sprintf(buf ,"%c%c%c%c",0x03,0x02,0x02,0x0); */
1072
1073         SOUP(0x03, 0x02, 0x02, 0x0);
1074         kfree(buf);
1075         iuu_led(port, 0xF000, 0xF000, 0, 0xFF);
1076         iuu_uart_on(port);
1077         if (boost < 100)
1078                 boost = 100;
1079         switch (clockmode) {
1080         case 2:         /*  3.680 Mhz */
1081                 iuu_clk(port, IUU_CLK_3680000 * boost / 100);
1082                 result =
1083                     iuu_uart_baud(port, 9600 * boost / 100, &actual,
1084                                   IUU_PARITY_EVEN);
1085                 break;
1086         case 3:         /*  6.00 Mhz */
1087                 iuu_clk(port, IUU_CLK_6000000 * boost / 100);
1088                 result =
1089                     iuu_uart_baud(port, 16457 * boost / 100, &actual,
1090                                   IUU_PARITY_EVEN);
1091                 break;
1092         default:                /*  3.579 Mhz */
1093                 iuu_clk(port, IUU_CLK_3579000 * boost / 100);
1094                 result =
1095                     iuu_uart_baud(port, 9600 * boost / 100, &actual,
1096                                   IUU_PARITY_EVEN);
1097         }
1098
1099         /* set the cardin cardout signals */
1100         switch (cdmode) {
1101         case 0:
1102                 iuu_cardin = 0;
1103                 iuu_cardout = 0;
1104                 break;
1105         case 1:
1106                 iuu_cardin = TIOCM_CD;
1107                 iuu_cardout =  0;
1108                 break;
1109         case 2:
1110                 iuu_cardin = 0;
1111                 iuu_cardout = TIOCM_CD;
1112                 break;
1113         case 3:
1114                 iuu_cardin = TIOCM_DSR;
1115                 iuu_cardout = 0;
1116                 break;
1117         case 4:
1118                 iuu_cardin = 0;
1119                 iuu_cardout = TIOCM_DSR;
1120                 break;
1121         case 5:
1122                 iuu_cardin = TIOCM_CTS;
1123                 iuu_cardout = 0;
1124                 break;
1125         case 6:
1126                 iuu_cardin = 0;
1127                 iuu_cardout = TIOCM_CTS;
1128                 break;
1129         case 7:
1130                 iuu_cardin = TIOCM_RNG;
1131                 iuu_cardout = 0;
1132                 break;
1133         case 8:
1134                 iuu_cardin = 0;
1135                 iuu_cardout = TIOCM_RNG;
1136         }
1137
1138         iuu_uart_flush(port);
1139
1140         dbg("%s - initialization done", __func__);
1141
1142         memset(port->write_urb->transfer_buffer, IUU_UART_RX, 1);
1143         usb_fill_bulk_urb(port->write_urb, port->serial->dev,
1144                           usb_sndbulkpipe(port->serial->dev,
1145                                           port->bulk_out_endpointAddress),
1146                           port->write_urb->transfer_buffer, 1,
1147                           read_rxcmd_callback, port);
1148         result = usb_submit_urb(port->write_urb, GFP_KERNEL);
1149
1150         if (result) {
1151                 dev_err(&port->dev, "%s - failed submitting read urb,"
1152                         " error %d\n", __func__, result);
1153                 iuu_close(tty, port, NULL);
1154                 return -EPROTO;
1155         } else {
1156                 dbg("%s - rxcmd OK", __func__);
1157         }
1158         return result;
1159 }
1160
1161 static struct usb_serial_driver iuu_device = {
1162         .driver = {
1163                    .owner = THIS_MODULE,
1164                    .name = "iuu_phoenix",
1165                    },
1166         .id_table = id_table,
1167         .num_ports = 1,
1168         .open = iuu_open,
1169         .close = iuu_close,
1170         .write = iuu_uart_write,
1171         .read_bulk_callback = iuu_uart_read_callback,
1172         .tiocmget = iuu_tiocmget,
1173         .tiocmset = iuu_tiocmset,
1174         .attach = iuu_startup,
1175         .shutdown = iuu_shutdown,
1176 };
1177
1178 static int __init iuu_init(void)
1179 {
1180         int retval;
1181         retval = usb_serial_register(&iuu_device);
1182         if (retval)
1183                 goto failed_usb_serial_register;
1184         retval = usb_register(&iuu_driver);
1185         if (retval)
1186                 goto failed_usb_register;
1187         info(DRIVER_DESC " " DRIVER_VERSION);
1188         return 0;
1189 failed_usb_register:
1190         usb_serial_deregister(&iuu_device);
1191 failed_usb_serial_register:
1192         return retval;
1193 }
1194
1195 static void __exit iuu_exit(void)
1196 {
1197         usb_deregister(&iuu_driver);
1198         usb_serial_deregister(&iuu_device);
1199 }
1200
1201 module_init(iuu_init);
1202 module_exit(iuu_exit);
1203
1204 MODULE_AUTHOR("Alain Degreffe eczema@ecze.com");
1205
1206 MODULE_DESCRIPTION(DRIVER_DESC);
1207 MODULE_LICENSE("GPL");
1208
1209 MODULE_VERSION(DRIVER_VERSION);
1210 module_param(debug, bool, S_IRUGO | S_IWUSR);
1211 MODULE_PARM_DESC(debug, "Debug enabled or not");
1212
1213 module_param(xmas, bool, S_IRUGO | S_IWUSR);
1214 MODULE_PARM_DESC(xmas, "xmas color enabled or not");
1215
1216 module_param(boost, int, S_IRUGO | S_IWUSR);
1217 MODULE_PARM_DESC(boost, "overclock boost percent 100 to 500");
1218
1219 module_param(clockmode, int, S_IRUGO | S_IWUSR);
1220 MODULE_PARM_DESC(clockmode, "1=3Mhz579,2=3Mhz680,3=6Mhz");
1221
1222 module_param(cdmode, int, S_IRUGO | S_IWUSR);
1223 MODULE_PARM_DESC(cdmode, "Card detect mode 0=none, 1=CD, 2=!CD, 3=DSR, "
1224                  "4=!DSR, 5=CTS, 6=!CTS, 7=RING, 8=!RING");