Adding support for MOXA ART SoC. Testing port of linux-2.6.32.60-moxart.
[linux-3.6.7-moxart.git] / drivers / usb / serial / iuu_phoenix.c
blob8d3692bef861f89d947e7066a571d89eacd1652a
1 /*
2 * Infinity Unlimited USB Phoenix driver
4 * Copyright (C) 2010 James Courtier-Dutton (James@superbug.co.uk)
6 * Copyright (C) 2007 Alain Degreffe (eczema@ecze.com)
8 * Original code taken from iuutool (Copyright (C) 2006 Juan Carlos Borrás)
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.
15 * And tested with help of WB Electronics
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>
36 #ifdef CONFIG_USB_SERIAL_DEBUG
37 static bool debug = 1;
38 #else
39 static bool debug;
40 #endif
43 * Version Information
45 #define DRIVER_VERSION "v0.12"
46 #define DRIVER_DESC "Infinity USB Unlimited Phoenix driver"
48 static const struct usb_device_id id_table[] = {
49 {USB_DEVICE(IUU_USB_VENDOR_ID, IUU_USB_PRODUCT_ID)},
50 {} /* Terminating entry */
52 MODULE_DEVICE_TABLE(usb, id_table);
54 /* turbo parameter */
55 static int boost = 100;
56 static int clockmode = 1;
57 static int cdmode = 1;
58 static int iuu_cardin;
59 static int iuu_cardout;
60 static bool xmas;
61 static int vcc_default = 5;
63 static int iuu_create_sysfs_attrs(struct usb_serial_port *port);
64 static int iuu_remove_sysfs_attrs(struct usb_serial_port *port);
65 static void read_rxcmd_callback(struct urb *urb);
67 struct iuu_private {
68 spinlock_t lock; /* store irq state */
69 wait_queue_head_t delta_msr_wait;
70 u8 line_status;
71 int tiostatus; /* store IUART SIGNAL for tiocmget call */
72 u8 reset; /* if 1 reset is needed */
73 int poll; /* number of poll */
74 u8 *writebuf; /* buffer for writing to device */
75 int writelen; /* num of byte to write to device */
76 u8 *buf; /* used for initialize speed */
77 u8 *dbgbuf; /* debug buffer */
78 u8 len;
79 int vcc; /* vcc (either 3 or 5 V) */
80 u32 baud;
81 u32 boost;
82 u32 clk;
85 static int iuu_port_probe(struct usb_serial_port *port)
87 struct iuu_private *priv;
88 int ret;
90 priv = kzalloc(sizeof(struct iuu_private), GFP_KERNEL);
91 if (!priv)
92 return -ENOMEM;
94 priv->buf = kzalloc(256, GFP_KERNEL);
95 if (!priv->buf) {
96 kfree(priv);
97 return -ENOMEM;
100 priv->writebuf = kzalloc(256, GFP_KERNEL);
101 if (!priv->writebuf) {
102 kfree(priv->buf);
103 kfree(priv);
104 return -ENOMEM;
107 priv->dbgbuf = kzalloc(256, GFP_KERNEL);
108 if (!priv->dbgbuf) {
109 kfree(priv->writebuf);
110 kfree(priv->buf);
111 kfree(priv);
112 return -ENOMEM;
115 priv->vcc = vcc_default;
116 spin_lock_init(&priv->lock);
117 init_waitqueue_head(&priv->delta_msr_wait);
119 usb_set_serial_port_data(port, priv);
121 ret = iuu_create_sysfs_attrs(port);
122 if (ret) {
123 kfree(priv->dbgbuf);
124 kfree(priv->writebuf);
125 kfree(priv->buf);
126 kfree(priv);
127 return ret;
130 return 0;
133 static int iuu_port_remove(struct usb_serial_port *port)
135 struct iuu_private *priv = usb_get_serial_port_data(port);
137 iuu_remove_sysfs_attrs(port);
138 kfree(priv->dbgbuf);
139 kfree(priv->writebuf);
140 kfree(priv->buf);
141 kfree(priv);
143 return 0;
146 static int iuu_tiocmset(struct tty_struct *tty,
147 unsigned int set, unsigned int clear)
149 struct usb_serial_port *port = tty->driver_data;
150 struct iuu_private *priv = usb_get_serial_port_data(port);
151 unsigned long flags;
153 /* FIXME: locking on tiomstatus */
154 dbg("%s (%d) msg : SET = 0x%04x, CLEAR = 0x%04x ", __func__,
155 port->number, set, clear);
157 spin_lock_irqsave(&priv->lock, flags);
159 if ((set & TIOCM_RTS) && !(priv->tiostatus == TIOCM_RTS)) {
160 dbg("%s TIOCMSET RESET called !!!", __func__);
161 priv->reset = 1;
163 if (set & TIOCM_RTS)
164 priv->tiostatus = TIOCM_RTS;
166 spin_unlock_irqrestore(&priv->lock, flags);
167 return 0;
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
175 static int iuu_tiocmget(struct tty_struct *tty)
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;
182 spin_lock_irqsave(&priv->lock, flags);
183 rc = priv->tiostatus;
184 spin_unlock_irqrestore(&priv->lock, flags);
186 return rc;
189 static void iuu_rxcmd(struct urb *urb)
191 struct usb_serial_port *port = urb->context;
192 int result;
193 int status = urb->status;
195 if (status) {
196 dbg("%s - status = %d", __func__, status);
197 /* error stop all */
198 return;
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);
211 static int iuu_reset(struct usb_serial_port *port, u8 wt)
213 struct iuu_private *priv = usb_get_serial_port_data(port);
214 int result;
215 char *buf_ptr = port->write_urb->transfer_buffer;
217 /* Prepare the reset sequence */
219 *buf_ptr++ = IUU_RST_SET;
220 *buf_ptr++ = IUU_DELAY_MS;
221 *buf_ptr++ = wt;
222 *buf_ptr = IUU_RST_CLEAR;
224 /* send the sequence */
226 usb_fill_bulk_urb(port->write_urb,
227 port->serial->dev,
228 usb_sndbulkpipe(port->serial->dev,
229 port->bulk_out_endpointAddress),
230 port->write_urb->transfer_buffer, 4, iuu_rxcmd, port);
231 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
232 priv->reset = 0;
233 return result;
236 /* Status Function
237 * Return value is
238 * 0x00 = no card
239 * 0x01 = smartcard
240 * 0x02 = sim card
242 static void iuu_update_status_callback(struct urb *urb)
244 struct usb_serial_port *port = urb->context;
245 struct iuu_private *priv = usb_get_serial_port_data(port);
246 u8 *st;
247 int status = urb->status;
249 if (status) {
250 dbg("%s - status = %d", __func__, status);
251 /* error stop all */
252 return;
255 st = urb->transfer_buffer;
256 dbg("%s - enter", __func__);
257 if (urb->actual_length == 1) {
258 switch (st[0]) {
259 case 0x1:
260 priv->tiostatus = iuu_cardout;
261 break;
262 case 0x0:
263 priv->tiostatus = iuu_cardin;
264 break;
265 default:
266 priv->tiostatus = iuu_cardin;
269 iuu_rxcmd(urb);
272 static void iuu_status_callback(struct urb *urb)
274 struct usb_serial_port *port = urb->context;
275 int result;
276 int status = urb->status;
278 dbg("%s - status = %d", __func__, status);
279 usb_fill_bulk_urb(port->read_urb, port->serial->dev,
280 usb_rcvbulkpipe(port->serial->dev,
281 port->bulk_in_endpointAddress),
282 port->read_urb->transfer_buffer, 256,
283 iuu_update_status_callback, port);
284 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
287 static int iuu_status(struct usb_serial_port *port)
289 int result;
291 memset(port->write_urb->transfer_buffer, IUU_GET_STATE_REGISTER, 1);
292 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
293 usb_sndbulkpipe(port->serial->dev,
294 port->bulk_out_endpointAddress),
295 port->write_urb->transfer_buffer, 1,
296 iuu_status_callback, port);
297 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
298 return result;
302 static int bulk_immediate(struct usb_serial_port *port, u8 *buf, u8 count)
304 int status;
305 struct usb_serial *serial = port->serial;
306 int actual = 0;
308 /* send the data out the bulk port */
310 status =
311 usb_bulk_msg(serial->dev,
312 usb_sndbulkpipe(serial->dev,
313 port->bulk_out_endpointAddress), buf,
314 count, &actual, HZ * 1);
316 if (status != IUU_OPERATION_OK)
317 dbg("%s - error = %2x", __func__, status);
318 else
319 dbg("%s - write OK !", __func__);
320 return status;
323 static int read_immediate(struct usb_serial_port *port, u8 *buf, u8 count)
325 int status;
326 struct usb_serial *serial = port->serial;
327 int actual = 0;
329 /* send the data out the bulk port */
330 status =
331 usb_bulk_msg(serial->dev,
332 usb_rcvbulkpipe(serial->dev,
333 port->bulk_in_endpointAddress), buf,
334 count, &actual, HZ * 1);
336 if (status != IUU_OPERATION_OK)
337 dbg("%s - error = %2x", __func__, status);
338 else
339 dbg("%s - read OK !", __func__);
340 return status;
343 static int iuu_led(struct usb_serial_port *port, unsigned int R,
344 unsigned int G, unsigned int B, u8 f)
346 int status;
347 u8 *buf;
348 buf = kmalloc(8, GFP_KERNEL);
349 if (!buf)
350 return -ENOMEM;
352 buf[0] = IUU_SET_LED;
353 buf[1] = R & 0xFF;
354 buf[2] = (R >> 8) & 0xFF;
355 buf[3] = G & 0xFF;
356 buf[4] = (G >> 8) & 0xFF;
357 buf[5] = B & 0xFF;
358 buf[6] = (B >> 8) & 0xFF;
359 buf[7] = f;
360 status = bulk_immediate(port, buf, 8);
361 kfree(buf);
362 if (status != IUU_OPERATION_OK)
363 dbg("%s - led error status = %2x", __func__, status);
364 else
365 dbg("%s - led OK !", __func__);
366 return IUU_OPERATION_OK;
369 static void iuu_rgbf_fill_buffer(u8 *buf, u8 r1, u8 r2, u8 g1, u8 g2, u8 b1,
370 u8 b2, u8 freq)
372 *buf++ = IUU_SET_LED;
373 *buf++ = r1;
374 *buf++ = r2;
375 *buf++ = g1;
376 *buf++ = g2;
377 *buf++ = b1;
378 *buf++ = b2;
379 *buf = freq;
382 static void iuu_led_activity_on(struct urb *urb)
384 struct usb_serial_port *port = urb->context;
385 int result;
386 char *buf_ptr = port->write_urb->transfer_buffer;
387 *buf_ptr++ = IUU_SET_LED;
388 if (xmas == 1) {
389 get_random_bytes(buf_ptr, 6);
390 *(buf_ptr+7) = 1;
391 } else {
392 iuu_rgbf_fill_buffer(buf_ptr, 255, 255, 0, 0, 0, 0, 255);
395 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
396 usb_sndbulkpipe(port->serial->dev,
397 port->bulk_out_endpointAddress),
398 port->write_urb->transfer_buffer, 8 ,
399 iuu_rxcmd, port);
400 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
403 static void iuu_led_activity_off(struct urb *urb)
405 struct usb_serial_port *port = urb->context;
406 int result;
407 char *buf_ptr = port->write_urb->transfer_buffer;
408 if (xmas == 1) {
409 iuu_rxcmd(urb);
410 return;
411 } else {
412 *buf_ptr++ = IUU_SET_LED;
413 iuu_rgbf_fill_buffer(buf_ptr, 0, 0, 255, 255, 0, 0, 255);
415 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
416 usb_sndbulkpipe(port->serial->dev,
417 port->bulk_out_endpointAddress),
418 port->write_urb->transfer_buffer, 8 ,
419 iuu_rxcmd, port);
420 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
425 static int iuu_clk(struct usb_serial_port *port, int dwFrq)
427 int status;
428 struct iuu_private *priv = usb_get_serial_port_data(port);
429 int Count = 0;
430 u8 FrqGenAdr = 0x69;
431 u8 DIV = 0; /* 8bit */
432 u8 XDRV = 0; /* 8bit */
433 u8 PUMP = 0; /* 3bit */
434 u8 PBmsb = 0; /* 2bit */
435 u8 PBlsb = 0; /* 8bit */
436 u8 PO = 0; /* 1bit */
437 u8 Q = 0; /* 7bit */
438 /* 24bit = 3bytes */
439 unsigned int P = 0;
440 unsigned int P2 = 0;
441 int frq = (int)dwFrq;
443 if (frq == 0) {
444 priv->buf[Count++] = IUU_UART_WRITE_I2C;
445 priv->buf[Count++] = FrqGenAdr << 1;
446 priv->buf[Count++] = 0x09;
447 priv->buf[Count++] = 0x00;
449 status = bulk_immediate(port, (u8 *) priv->buf, Count);
450 if (status != 0) {
451 dbg("%s - write error ", __func__);
452 return status;
454 } else if (frq == 3579000) {
455 DIV = 100;
456 P = 1193;
457 Q = 40;
458 XDRV = 0;
459 } else if (frq == 3680000) {
460 DIV = 105;
461 P = 161;
462 Q = 5;
463 XDRV = 0;
464 } else if (frq == 6000000) {
465 DIV = 66;
466 P = 66;
467 Q = 2;
468 XDRV = 0x28;
469 } else {
470 unsigned int result = 0;
471 unsigned int tmp = 0;
472 unsigned int check;
473 unsigned int check2;
474 char found = 0x00;
475 unsigned int lQ = 2;
476 unsigned int lP = 2055;
477 unsigned int lDiv = 4;
479 for (lQ = 2; lQ <= 47 && !found; lQ++)
480 for (lP = 2055; lP >= 8 && !found; lP--)
481 for (lDiv = 4; lDiv <= 127 && !found; lDiv++) {
482 tmp = (12000000 / lDiv) * (lP / lQ);
483 if (abs((int)(tmp - frq)) <
484 abs((int)(frq - result))) {
485 check2 = (12000000 / lQ);
486 if (check2 < 250000)
487 continue;
488 check = (12000000 / lQ) * lP;
489 if (check > 400000000)
490 continue;
491 if (check < 100000000)
492 continue;
493 if (lDiv < 4 || lDiv > 127)
494 continue;
495 result = tmp;
496 P = lP;
497 DIV = lDiv;
498 Q = lQ;
499 if (result == frq)
500 found = 0x01;
504 P2 = ((P - PO) / 2) - 4;
505 DIV = DIV;
506 PUMP = 0x04;
507 PBmsb = (P2 >> 8 & 0x03);
508 PBlsb = P2 & 0xFF;
509 PO = (P >> 10) & 0x01;
510 Q = Q - 2;
512 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
513 priv->buf[Count++] = FrqGenAdr << 1;
514 priv->buf[Count++] = 0x09;
515 priv->buf[Count++] = 0x20; /* Adr = 0x09 */
516 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
517 priv->buf[Count++] = FrqGenAdr << 1;
518 priv->buf[Count++] = 0x0C;
519 priv->buf[Count++] = DIV; /* Adr = 0x0C */
520 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
521 priv->buf[Count++] = FrqGenAdr << 1;
522 priv->buf[Count++] = 0x12;
523 priv->buf[Count++] = XDRV; /* Adr = 0x12 */
524 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
525 priv->buf[Count++] = FrqGenAdr << 1;
526 priv->buf[Count++] = 0x13;
527 priv->buf[Count++] = 0x6B; /* Adr = 0x13 */
528 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
529 priv->buf[Count++] = FrqGenAdr << 1;
530 priv->buf[Count++] = 0x40;
531 priv->buf[Count++] = (0xC0 | ((PUMP & 0x07) << 2)) |
532 (PBmsb & 0x03); /* Adr = 0x40 */
533 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
534 priv->buf[Count++] = FrqGenAdr << 1;
535 priv->buf[Count++] = 0x41;
536 priv->buf[Count++] = PBlsb; /* Adr = 0x41 */
537 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
538 priv->buf[Count++] = FrqGenAdr << 1;
539 priv->buf[Count++] = 0x42;
540 priv->buf[Count++] = Q | (((PO & 0x01) << 7)); /* Adr = 0x42 */
541 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
542 priv->buf[Count++] = FrqGenAdr << 1;
543 priv->buf[Count++] = 0x44;
544 priv->buf[Count++] = (char)0xFF; /* Adr = 0x44 */
545 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
546 priv->buf[Count++] = FrqGenAdr << 1;
547 priv->buf[Count++] = 0x45;
548 priv->buf[Count++] = (char)0xFE; /* Adr = 0x45 */
549 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
550 priv->buf[Count++] = FrqGenAdr << 1;
551 priv->buf[Count++] = 0x46;
552 priv->buf[Count++] = 0x7F; /* Adr = 0x46 */
553 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
554 priv->buf[Count++] = FrqGenAdr << 1;
555 priv->buf[Count++] = 0x47;
556 priv->buf[Count++] = (char)0x84; /* Adr = 0x47 */
558 status = bulk_immediate(port, (u8 *) priv->buf, Count);
559 if (status != IUU_OPERATION_OK)
560 dbg("%s - write error ", __func__);
561 return status;
564 static int iuu_uart_flush(struct usb_serial_port *port)
566 int i;
567 int status;
568 u8 rxcmd = IUU_UART_RX;
569 struct iuu_private *priv = usb_get_serial_port_data(port);
571 if (iuu_led(port, 0xF000, 0, 0, 0xFF) < 0)
572 return -EIO;
574 for (i = 0; i < 2; i++) {
575 status = bulk_immediate(port, &rxcmd, 1);
576 if (status != IUU_OPERATION_OK) {
577 dbg("%s - uart_flush_write error", __func__);
578 return status;
581 status = read_immediate(port, &priv->len, 1);
582 if (status != IUU_OPERATION_OK) {
583 dbg("%s - uart_flush_read error", __func__);
584 return status;
587 if (priv->len > 0) {
588 dbg("%s - uart_flush datalen is : %i ", __func__,
589 priv->len);
590 status = read_immediate(port, priv->buf, priv->len);
591 if (status != IUU_OPERATION_OK) {
592 dbg("%s - uart_flush_read error", __func__);
593 return status;
597 dbg("%s - uart_flush_read OK!", __func__);
598 iuu_led(port, 0, 0xF000, 0, 0xFF);
599 return status;
602 static void read_buf_callback(struct urb *urb)
604 struct usb_serial_port *port = urb->context;
605 unsigned char *data = urb->transfer_buffer;
606 struct tty_struct *tty;
607 int status = urb->status;
609 if (status) {
610 if (status == -EPROTO) {
611 /* reschedule needed */
613 return;
616 dbg("%s - %i chars to write", __func__, urb->actual_length);
617 tty = tty_port_tty_get(&port->port);
618 if (data == NULL)
619 dbg("%s - data is NULL !!!", __func__);
620 if (tty && urb->actual_length && data) {
621 tty_insert_flip_string(tty, data, urb->actual_length);
622 tty_flip_buffer_push(tty);
624 tty_kref_put(tty);
625 iuu_led_activity_on(urb);
628 static int iuu_bulk_write(struct usb_serial_port *port)
630 struct iuu_private *priv = usb_get_serial_port_data(port);
631 unsigned long flags;
632 int result;
633 int i;
634 int buf_len;
635 char *buf_ptr = port->write_urb->transfer_buffer;
637 spin_lock_irqsave(&priv->lock, flags);
638 *buf_ptr++ = IUU_UART_ESC;
639 *buf_ptr++ = IUU_UART_TX;
640 *buf_ptr++ = priv->writelen;
642 memcpy(buf_ptr, priv->writebuf, priv->writelen);
643 buf_len = priv->writelen;
644 priv->writelen = 0;
645 spin_unlock_irqrestore(&priv->lock, flags);
646 if (debug == 1) {
647 for (i = 0; i < buf_len; i++)
648 sprintf(priv->dbgbuf + i*2 ,
649 "%02X", priv->writebuf[i]);
650 priv->dbgbuf[buf_len+i*2] = 0;
651 dbg("%s - writing %i chars : %s", __func__,
652 buf_len, priv->dbgbuf);
654 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
655 usb_sndbulkpipe(port->serial->dev,
656 port->bulk_out_endpointAddress),
657 port->write_urb->transfer_buffer, buf_len + 3,
658 iuu_rxcmd, port);
659 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
660 usb_serial_port_softint(port);
661 return result;
664 static int iuu_read_buf(struct usb_serial_port *port, int len)
666 int result;
668 usb_fill_bulk_urb(port->read_urb, port->serial->dev,
669 usb_rcvbulkpipe(port->serial->dev,
670 port->bulk_in_endpointAddress),
671 port->read_urb->transfer_buffer, len,
672 read_buf_callback, port);
673 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
674 return result;
677 static void iuu_uart_read_callback(struct urb *urb)
679 struct usb_serial_port *port = urb->context;
680 struct iuu_private *priv = usb_get_serial_port_data(port);
681 unsigned long flags;
682 int status = urb->status;
683 int error = 0;
684 int len = 0;
685 unsigned char *data = urb->transfer_buffer;
686 priv->poll++;
688 if (status) {
689 dbg("%s - status = %d", __func__, status);
690 /* error stop all */
691 return;
693 if (data == NULL)
694 dbg("%s - data is NULL !!!", __func__);
696 if (urb->actual_length == 1 && data != NULL)
697 len = (int) data[0];
699 if (urb->actual_length > 1) {
700 dbg("%s - urb->actual_length = %i", __func__,
701 urb->actual_length);
702 error = 1;
703 return;
705 /* if len > 0 call readbuf */
707 if (len > 0 && error == 0) {
708 dbg("%s - call read buf - len to read is %i ",
709 __func__, len);
710 status = iuu_read_buf(port, len);
711 return;
713 /* need to update status ? */
714 if (priv->poll > 99) {
715 status = iuu_status(port);
716 priv->poll = 0;
717 return;
720 /* reset waiting ? */
722 if (priv->reset == 1) {
723 status = iuu_reset(port, 0xC);
724 return;
726 /* Writebuf is waiting */
727 spin_lock_irqsave(&priv->lock, flags);
728 if (priv->writelen > 0) {
729 spin_unlock_irqrestore(&priv->lock, flags);
730 status = iuu_bulk_write(port);
731 return;
733 spin_unlock_irqrestore(&priv->lock, flags);
734 /* if nothing to write call again rxcmd */
735 dbg("%s - rxcmd recall", __func__);
736 iuu_led_activity_off(urb);
739 static int iuu_uart_write(struct tty_struct *tty, struct usb_serial_port *port,
740 const u8 *buf, int count)
742 struct iuu_private *priv = usb_get_serial_port_data(port);
743 unsigned long flags;
745 if (count > 256)
746 return -ENOMEM;
748 spin_lock_irqsave(&priv->lock, flags);
750 /* fill the buffer */
751 memcpy(priv->writebuf + priv->writelen, buf, count);
752 priv->writelen += count;
753 spin_unlock_irqrestore(&priv->lock, flags);
755 return count;
758 static void read_rxcmd_callback(struct urb *urb)
760 struct usb_serial_port *port = urb->context;
761 int result;
762 int status = urb->status;
764 if (status) {
765 /* error stop all */
766 return;
769 usb_fill_bulk_urb(port->read_urb, port->serial->dev,
770 usb_rcvbulkpipe(port->serial->dev,
771 port->bulk_in_endpointAddress),
772 port->read_urb->transfer_buffer, 256,
773 iuu_uart_read_callback, port);
774 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
775 dbg("%s - submit result = %d", __func__, result);
778 static int iuu_uart_on(struct usb_serial_port *port)
780 int status;
781 u8 *buf;
783 buf = kmalloc(sizeof(u8) * 4, GFP_KERNEL);
785 if (!buf)
786 return -ENOMEM;
788 buf[0] = IUU_UART_ENABLE;
789 buf[1] = (u8) ((IUU_BAUD_9600 >> 8) & 0x00FF);
790 buf[2] = (u8) (0x00FF & IUU_BAUD_9600);
791 buf[3] = (u8) (0x0F0 & IUU_ONE_STOP_BIT) | (0x07 & IUU_PARITY_EVEN);
793 status = bulk_immediate(port, buf, 4);
794 if (status != IUU_OPERATION_OK) {
795 dbg("%s - uart_on error", __func__);
796 goto uart_enable_failed;
798 /* iuu_reset() the card after iuu_uart_on() */
799 status = iuu_uart_flush(port);
800 if (status != IUU_OPERATION_OK)
801 dbg("%s - uart_flush error", __func__);
802 uart_enable_failed:
803 kfree(buf);
804 return status;
807 /* Diables the IUU UART (a.k.a. the Phoenix voiderface) */
808 static int iuu_uart_off(struct usb_serial_port *port)
810 int status;
811 u8 *buf;
812 buf = kmalloc(1, GFP_KERNEL);
813 if (!buf)
814 return -ENOMEM;
815 buf[0] = IUU_UART_DISABLE;
817 status = bulk_immediate(port, buf, 1);
818 if (status != IUU_OPERATION_OK)
819 dbg("%s - uart_off error", __func__);
821 kfree(buf);
822 return status;
825 static int iuu_uart_baud(struct usb_serial_port *port, u32 baud_base,
826 u32 *actual, u8 parity)
828 int status;
829 u32 baud;
830 u8 *dataout;
831 u8 DataCount = 0;
832 u8 T1Frekvens = 0;
833 u8 T1reload = 0;
834 unsigned int T1FrekvensHZ = 0;
836 dbg("%s - enter baud_base=%d", __func__, baud_base);
837 dataout = kmalloc(sizeof(u8) * 5, GFP_KERNEL);
839 if (!dataout)
840 return -ENOMEM;
841 /*baud = (((priv->clk / 35) * baud_base) / 100000); */
842 baud = baud_base;
844 if (baud < 1200 || baud > 230400) {
845 kfree(dataout);
846 return IUU_INVALID_PARAMETER;
848 if (baud > 977) {
849 T1Frekvens = 3;
850 T1FrekvensHZ = 500000;
853 if (baud > 3906) {
854 T1Frekvens = 2;
855 T1FrekvensHZ = 2000000;
858 if (baud > 11718) {
859 T1Frekvens = 1;
860 T1FrekvensHZ = 6000000;
863 if (baud > 46875) {
864 T1Frekvens = 0;
865 T1FrekvensHZ = 24000000;
868 T1reload = 256 - (u8) (T1FrekvensHZ / (baud * 2));
870 /* magic number here: ENTER_FIRMWARE_UPDATE; */
871 dataout[DataCount++] = IUU_UART_ESC;
872 /* magic number here: CHANGE_BAUD; */
873 dataout[DataCount++] = IUU_UART_CHANGE;
874 dataout[DataCount++] = T1Frekvens;
875 dataout[DataCount++] = T1reload;
877 *actual = (T1FrekvensHZ / (256 - T1reload)) / 2;
879 switch (parity & 0x0F) {
880 case IUU_PARITY_NONE:
881 dataout[DataCount++] = 0x00;
882 break;
883 case IUU_PARITY_EVEN:
884 dataout[DataCount++] = 0x01;
885 break;
886 case IUU_PARITY_ODD:
887 dataout[DataCount++] = 0x02;
888 break;
889 case IUU_PARITY_MARK:
890 dataout[DataCount++] = 0x03;
891 break;
892 case IUU_PARITY_SPACE:
893 dataout[DataCount++] = 0x04;
894 break;
895 default:
896 kfree(dataout);
897 return IUU_INVALID_PARAMETER;
898 break;
901 switch (parity & 0xF0) {
902 case IUU_ONE_STOP_BIT:
903 dataout[DataCount - 1] |= IUU_ONE_STOP_BIT;
904 break;
906 case IUU_TWO_STOP_BITS:
907 dataout[DataCount - 1] |= IUU_TWO_STOP_BITS;
908 break;
909 default:
910 kfree(dataout);
911 return IUU_INVALID_PARAMETER;
912 break;
915 status = bulk_immediate(port, dataout, DataCount);
916 if (status != IUU_OPERATION_OK)
917 dbg("%s - uart_off error", __func__);
918 kfree(dataout);
919 return status;
922 static void iuu_set_termios(struct tty_struct *tty,
923 struct usb_serial_port *port, struct ktermios *old_termios)
925 const u32 supported_mask = CMSPAR|PARENB|PARODD;
926 struct iuu_private *priv = usb_get_serial_port_data(port);
927 unsigned int cflag = tty->termios->c_cflag;
928 int status;
929 u32 actual;
930 u32 parity;
931 int csize = CS7;
932 int baud;
933 u32 newval = cflag & supported_mask;
935 /* Just use the ospeed. ispeed should be the same. */
936 baud = tty->termios->c_ospeed;
938 dbg("%s - enter c_ospeed or baud=%d", __func__, baud);
940 /* compute the parity parameter */
941 parity = 0;
942 if (cflag & CMSPAR) { /* Using mark space */
943 if (cflag & PARODD)
944 parity |= IUU_PARITY_SPACE;
945 else
946 parity |= IUU_PARITY_MARK;
947 } else if (!(cflag & PARENB)) {
948 parity |= IUU_PARITY_NONE;
949 csize = CS8;
950 } else if (cflag & PARODD)
951 parity |= IUU_PARITY_ODD;
952 else
953 parity |= IUU_PARITY_EVEN;
955 parity |= (cflag & CSTOPB ? IUU_TWO_STOP_BITS : IUU_ONE_STOP_BIT);
957 /* set it */
958 status = iuu_uart_baud(port,
959 baud * priv->boost / 100,
960 &actual, parity);
962 /* set the termios value to the real one, so the user now what has
963 * changed. We support few fields so its easies to copy the old hw
964 * settings back over and then adjust them
966 if (old_termios)
967 tty_termios_copy_hw(tty->termios, old_termios);
968 if (status != 0) /* Set failed - return old bits */
969 return;
970 /* Re-encode speed, parity and csize */
971 tty_encode_baud_rate(tty, baud, baud);
972 tty->termios->c_cflag &= ~(supported_mask|CSIZE);
973 tty->termios->c_cflag |= newval | csize;
976 static void iuu_close(struct usb_serial_port *port)
978 /* iuu_led (port,255,0,0,0); */
979 struct usb_serial *serial;
981 serial = port->serial;
982 if (!serial)
983 return;
985 iuu_uart_off(port);
986 if (serial->dev) {
987 /* free writebuf */
988 /* shutdown our urbs */
989 dbg("%s - shutting down urbs", __func__);
990 usb_kill_urb(port->write_urb);
991 usb_kill_urb(port->read_urb);
992 usb_kill_urb(port->interrupt_in_urb);
993 iuu_led(port, 0, 0, 0xF000, 0xFF);
997 static void iuu_init_termios(struct tty_struct *tty)
999 *(tty->termios) = tty_std_termios;
1000 tty->termios->c_cflag = CLOCAL | CREAD | CS8 | B9600
1001 | TIOCM_CTS | CSTOPB | PARENB;
1002 tty->termios->c_ispeed = 9600;
1003 tty->termios->c_ospeed = 9600;
1004 tty->termios->c_lflag = 0;
1005 tty->termios->c_oflag = 0;
1006 tty->termios->c_iflag = 0;
1009 static int iuu_open(struct tty_struct *tty, struct usb_serial_port *port)
1011 struct usb_serial *serial = port->serial;
1012 u8 *buf;
1013 int result;
1014 int baud;
1015 u32 actual;
1016 struct iuu_private *priv = usb_get_serial_port_data(port);
1018 baud = tty->termios->c_ospeed;
1019 tty->termios->c_ispeed = baud;
1020 /* Re-encode speed */
1021 tty_encode_baud_rate(tty, baud, baud);
1023 dbg("%s - port %d, baud %d", __func__, port->number, baud);
1024 usb_clear_halt(serial->dev, port->write_urb->pipe);
1025 usb_clear_halt(serial->dev, port->read_urb->pipe);
1027 buf = kmalloc(10, GFP_KERNEL);
1028 if (buf == NULL)
1029 return -ENOMEM;
1031 priv->poll = 0;
1033 /* initialize writebuf */
1034 #define FISH(a, b, c, d) do { \
1035 result = usb_control_msg(port->serial->dev, \
1036 usb_rcvctrlpipe(port->serial->dev, 0), \
1037 b, a, c, d, buf, 1, 1000); \
1038 dbg("0x%x:0x%x:0x%x:0x%x %d - %x", a, b, c, d, result, \
1039 buf[0]); } while (0);
1041 #define SOUP(a, b, c, d) do { \
1042 result = usb_control_msg(port->serial->dev, \
1043 usb_sndctrlpipe(port->serial->dev, 0), \
1044 b, a, c, d, NULL, 0, 1000); \
1045 dbg("0x%x:0x%x:0x%x:0x%x %d", a, b, c, d, result); } while (0)
1047 /* This is not UART related but IUU USB driver related or something */
1048 /* like that. Basically no IUU will accept any commands from the USB */
1049 /* host unless it has received the following message */
1050 /* sprintf(buf ,"%c%c%c%c",0x03,0x02,0x02,0x0); */
1052 SOUP(0x03, 0x02, 0x02, 0x0);
1053 kfree(buf);
1054 iuu_led(port, 0xF000, 0xF000, 0, 0xFF);
1055 iuu_uart_on(port);
1056 if (boost < 100)
1057 boost = 100;
1058 priv->boost = boost;
1059 priv->baud = baud;
1060 switch (clockmode) {
1061 case 2: /* 3.680 Mhz */
1062 priv->clk = IUU_CLK_3680000;
1063 iuu_clk(port, IUU_CLK_3680000 * boost / 100);
1064 result =
1065 iuu_uart_baud(port, baud * boost / 100, &actual,
1066 IUU_PARITY_EVEN);
1067 break;
1068 case 3: /* 6.00 Mhz */
1069 iuu_clk(port, IUU_CLK_6000000 * boost / 100);
1070 priv->clk = IUU_CLK_6000000;
1071 /* Ratio of 6000000 to 3500000 for baud 9600 */
1072 result =
1073 iuu_uart_baud(port, 16457 * boost / 100, &actual,
1074 IUU_PARITY_EVEN);
1075 break;
1076 default: /* 3.579 Mhz */
1077 iuu_clk(port, IUU_CLK_3579000 * boost / 100);
1078 priv->clk = IUU_CLK_3579000;
1079 result =
1080 iuu_uart_baud(port, baud * boost / 100, &actual,
1081 IUU_PARITY_EVEN);
1084 /* set the cardin cardout signals */
1085 switch (cdmode) {
1086 case 0:
1087 iuu_cardin = 0;
1088 iuu_cardout = 0;
1089 break;
1090 case 1:
1091 iuu_cardin = TIOCM_CD;
1092 iuu_cardout = 0;
1093 break;
1094 case 2:
1095 iuu_cardin = 0;
1096 iuu_cardout = TIOCM_CD;
1097 break;
1098 case 3:
1099 iuu_cardin = TIOCM_DSR;
1100 iuu_cardout = 0;
1101 break;
1102 case 4:
1103 iuu_cardin = 0;
1104 iuu_cardout = TIOCM_DSR;
1105 break;
1106 case 5:
1107 iuu_cardin = TIOCM_CTS;
1108 iuu_cardout = 0;
1109 break;
1110 case 6:
1111 iuu_cardin = 0;
1112 iuu_cardout = TIOCM_CTS;
1113 break;
1114 case 7:
1115 iuu_cardin = TIOCM_RNG;
1116 iuu_cardout = 0;
1117 break;
1118 case 8:
1119 iuu_cardin = 0;
1120 iuu_cardout = TIOCM_RNG;
1123 iuu_uart_flush(port);
1125 dbg("%s - initialization done", __func__);
1127 memset(port->write_urb->transfer_buffer, IUU_UART_RX, 1);
1128 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
1129 usb_sndbulkpipe(port->serial->dev,
1130 port->bulk_out_endpointAddress),
1131 port->write_urb->transfer_buffer, 1,
1132 read_rxcmd_callback, port);
1133 result = usb_submit_urb(port->write_urb, GFP_KERNEL);
1134 if (result) {
1135 dev_err(&port->dev, "%s - failed submitting read urb,"
1136 " error %d\n", __func__, result);
1137 iuu_close(port);
1138 } else {
1139 dbg("%s - rxcmd OK", __func__);
1142 return result;
1145 /* how to change VCC */
1146 static int iuu_vcc_set(struct usb_serial_port *port, unsigned int vcc)
1148 int status;
1149 u8 *buf;
1151 buf = kmalloc(5, GFP_KERNEL);
1152 if (!buf)
1153 return -ENOMEM;
1155 buf[0] = IUU_SET_VCC;
1156 buf[1] = vcc & 0xFF;
1157 buf[2] = (vcc >> 8) & 0xFF;
1158 buf[3] = (vcc >> 16) & 0xFF;
1159 buf[4] = (vcc >> 24) & 0xFF;
1161 status = bulk_immediate(port, buf, 5);
1162 kfree(buf);
1164 if (status != IUU_OPERATION_OK)
1165 dbg("%s - vcc error status = %2x", __func__, status);
1166 else
1167 dbg("%s - vcc OK !", __func__);
1169 return status;
1173 * Sysfs Attributes
1176 static ssize_t show_vcc_mode(struct device *dev,
1177 struct device_attribute *attr, char *buf)
1179 struct usb_serial_port *port = to_usb_serial_port(dev);
1180 struct iuu_private *priv = usb_get_serial_port_data(port);
1182 return sprintf(buf, "%d\n", priv->vcc);
1185 static ssize_t store_vcc_mode(struct device *dev,
1186 struct device_attribute *attr, const char *buf, size_t count)
1188 struct usb_serial_port *port = to_usb_serial_port(dev);
1189 struct iuu_private *priv = usb_get_serial_port_data(port);
1190 unsigned long v;
1192 if (strict_strtoul(buf, 10, &v)) {
1193 dev_err(dev, "%s - vcc_mode: %s is not a unsigned long\n",
1194 __func__, buf);
1195 goto fail_store_vcc_mode;
1198 dbg("%s: setting vcc_mode = %ld", __func__, v);
1200 if ((v != 3) && (v != 5)) {
1201 dev_err(dev, "%s - vcc_mode %ld is invalid\n", __func__, v);
1202 } else {
1203 iuu_vcc_set(port, v);
1204 priv->vcc = v;
1206 fail_store_vcc_mode:
1207 return count;
1210 static DEVICE_ATTR(vcc_mode, S_IRUSR | S_IWUSR, show_vcc_mode,
1211 store_vcc_mode);
1213 static int iuu_create_sysfs_attrs(struct usb_serial_port *port)
1215 return device_create_file(&port->dev, &dev_attr_vcc_mode);
1218 static int iuu_remove_sysfs_attrs(struct usb_serial_port *port)
1220 device_remove_file(&port->dev, &dev_attr_vcc_mode);
1221 return 0;
1225 * End Sysfs Attributes
1228 static struct usb_serial_driver iuu_device = {
1229 .driver = {
1230 .owner = THIS_MODULE,
1231 .name = "iuu_phoenix",
1233 .id_table = id_table,
1234 .num_ports = 1,
1235 .bulk_in_size = 512,
1236 .bulk_out_size = 512,
1237 .open = iuu_open,
1238 .close = iuu_close,
1239 .write = iuu_uart_write,
1240 .read_bulk_callback = iuu_uart_read_callback,
1241 .tiocmget = iuu_tiocmget,
1242 .tiocmset = iuu_tiocmset,
1243 .set_termios = iuu_set_termios,
1244 .init_termios = iuu_init_termios,
1245 .port_probe = iuu_port_probe,
1246 .port_remove = iuu_port_remove,
1249 static struct usb_serial_driver * const serial_drivers[] = {
1250 &iuu_device, NULL
1253 module_usb_serial_driver(serial_drivers, id_table);
1255 MODULE_AUTHOR("Alain Degreffe eczema@ecze.com");
1257 MODULE_DESCRIPTION(DRIVER_DESC);
1258 MODULE_LICENSE("GPL");
1260 MODULE_VERSION(DRIVER_VERSION);
1261 module_param(debug, bool, S_IRUGO | S_IWUSR);
1262 MODULE_PARM_DESC(debug, "Debug enabled or not");
1264 module_param(xmas, bool, S_IRUGO | S_IWUSR);
1265 MODULE_PARM_DESC(xmas, "Xmas colors enabled or not");
1267 module_param(boost, int, S_IRUGO | S_IWUSR);
1268 MODULE_PARM_DESC(boost, "Card overclock boost (in percent 100-500)");
1270 module_param(clockmode, int, S_IRUGO | S_IWUSR);
1271 MODULE_PARM_DESC(clockmode, "Card clock mode (1=3.579 MHz, 2=3.680 MHz, "
1272 "3=6 Mhz)");
1274 module_param(cdmode, int, S_IRUGO | S_IWUSR);
1275 MODULE_PARM_DESC(cdmode, "Card detect mode (0=none, 1=CD, 2=!CD, 3=DSR, "
1276 "4=!DSR, 5=CTS, 6=!CTS, 7=RING, 8=!RING)");
1278 module_param(vcc_default, int, S_IRUGO | S_IWUSR);
1279 MODULE_PARM_DESC(vcc_default, "Set default VCC (either 3 for 3.3V or 5 "
1280 "for 5V). Default to 5.");