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