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;
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 static struct usb_driver iuu_driver = {
55 .name = "iuu_phoenix",
60 static int boost = 100;
61 static int clockmode = 1;
62 static int cdmode = 1;
63 static int iuu_cardin;
64 static int iuu_cardout;
66 static int vcc_default = 5;
68 static void read_rxcmd_callback(struct urb *urb);
71 spinlock_t lock; /* store irq state */
72 wait_queue_head_t delta_msr_wait;
74 int tiostatus; /* store IUART SIGNAL for tiocmget call */
75 u8 reset; /* if 1 reset is needed */
76 int poll; /* number of poll */
77 u8 *writebuf; /* buffer for writing to device */
78 int writelen; /* num of byte to write to device */
79 u8 *buf; /* used for initialize speed */
80 u8 *dbgbuf; /* debug buffer */
82 int vcc; /* vcc (either 3 or 5 V) */
89 static void iuu_free_buf(struct iuu_private *priv)
93 kfree(priv->writebuf);
96 static int iuu_alloc_buf(struct iuu_private *priv)
98 priv->buf = kzalloc(256, GFP_KERNEL);
99 priv->dbgbuf = kzalloc(256, GFP_KERNEL);
100 priv->writebuf = kzalloc(256, GFP_KERNEL);
101 if (!priv->buf || !priv->dbgbuf || !priv->writebuf) {
103 dbg("%s problem allocation buffer", __func__);
106 dbg("%s - Privates buffers allocation success", __func__);
110 static int iuu_startup(struct usb_serial *serial)
112 struct iuu_private *priv;
113 priv = kzalloc(sizeof(struct iuu_private), GFP_KERNEL);
114 dbg("%s- priv allocation success", __func__);
117 if (iuu_alloc_buf(priv)) {
121 priv->vcc = vcc_default;
122 spin_lock_init(&priv->lock);
123 init_waitqueue_head(&priv->delta_msr_wait);
124 usb_set_serial_port_data(serial->port[0], priv);
128 /* Release function */
129 static void iuu_release(struct usb_serial *serial)
131 struct usb_serial_port *port = serial->port[0];
132 struct iuu_private *priv = usb_get_serial_port_data(port);
138 dbg("%s - I will free all", __func__);
139 usb_set_serial_port_data(port, NULL);
141 dbg("%s - priv is not anymore in port structure", __func__);
144 dbg("%s priv is now kfree", __func__);
148 static int iuu_tiocmset(struct tty_struct *tty,
149 unsigned int set, unsigned int clear)
151 struct usb_serial_port *port = tty->driver_data;
152 struct iuu_private *priv = usb_get_serial_port_data(port);
155 /* FIXME: locking on tiomstatus */
156 dbg("%s (%d) msg : SET = 0x%04x, CLEAR = 0x%04x ", __func__,
157 port->number, set, clear);
159 spin_lock_irqsave(&priv->lock, flags);
161 if ((set & TIOCM_RTS) && !(priv->tiostatus == TIOCM_RTS)) {
162 dbg("%s TIOCMSET RESET called !!!", __func__);
166 priv->tiostatus = TIOCM_RTS;
168 spin_unlock_irqrestore(&priv->lock, flags);
172 /* This is used to provide a carrier detect mechanism
173 * When a card is present, the response is 0x00
174 * When no card , the reader respond with TIOCM_CD
175 * This is known as CD autodetect mechanism
177 static int iuu_tiocmget(struct tty_struct *tty)
179 struct usb_serial_port *port = tty->driver_data;
180 struct iuu_private *priv = usb_get_serial_port_data(port);
184 spin_lock_irqsave(&priv->lock, flags);
185 rc = priv->tiostatus;
186 spin_unlock_irqrestore(&priv->lock, flags);
191 static void iuu_rxcmd(struct urb *urb)
193 struct usb_serial_port *port = urb->context;
195 int status = urb->status;
198 dbg("%s - status = %d", __func__, status);
204 memset(port->write_urb->transfer_buffer, IUU_UART_RX, 1);
205 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
206 usb_sndbulkpipe(port->serial->dev,
207 port->bulk_out_endpointAddress),
208 port->write_urb->transfer_buffer, 1,
209 read_rxcmd_callback, port);
210 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
213 static int iuu_reset(struct usb_serial_port *port, u8 wt)
215 struct iuu_private *priv = usb_get_serial_port_data(port);
217 char *buf_ptr = port->write_urb->transfer_buffer;
219 /* Prepare the reset sequence */
221 *buf_ptr++ = IUU_RST_SET;
222 *buf_ptr++ = IUU_DELAY_MS;
224 *buf_ptr = IUU_RST_CLEAR;
226 /* send the sequence */
228 usb_fill_bulk_urb(port->write_urb,
230 usb_sndbulkpipe(port->serial->dev,
231 port->bulk_out_endpointAddress),
232 port->write_urb->transfer_buffer, 4, iuu_rxcmd, port);
233 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
244 static void iuu_update_status_callback(struct urb *urb)
246 struct usb_serial_port *port = urb->context;
247 struct iuu_private *priv = usb_get_serial_port_data(port);
249 int status = urb->status;
252 dbg("%s - status = %d", __func__, status);
257 st = urb->transfer_buffer;
258 dbg("%s - enter", __func__);
259 if (urb->actual_length == 1) {
262 priv->tiostatus = iuu_cardout;
265 priv->tiostatus = iuu_cardin;
268 priv->tiostatus = iuu_cardin;
274 static void iuu_status_callback(struct urb *urb)
276 struct usb_serial_port *port = urb->context;
278 int status = urb->status;
280 dbg("%s - status = %d", __func__, status);
281 usb_fill_bulk_urb(port->read_urb, port->serial->dev,
282 usb_rcvbulkpipe(port->serial->dev,
283 port->bulk_in_endpointAddress),
284 port->read_urb->transfer_buffer, 256,
285 iuu_update_status_callback, port);
286 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
289 static int iuu_status(struct usb_serial_port *port)
293 memset(port->write_urb->transfer_buffer, IUU_GET_STATE_REGISTER, 1);
294 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
295 usb_sndbulkpipe(port->serial->dev,
296 port->bulk_out_endpointAddress),
297 port->write_urb->transfer_buffer, 1,
298 iuu_status_callback, port);
299 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
304 static int bulk_immediate(struct usb_serial_port *port, u8 *buf, u8 count)
307 struct usb_serial *serial = port->serial;
310 /* send the data out the bulk port */
313 usb_bulk_msg(serial->dev,
314 usb_sndbulkpipe(serial->dev,
315 port->bulk_out_endpointAddress), buf,
316 count, &actual, HZ * 1);
318 if (status != IUU_OPERATION_OK)
319 dbg("%s - error = %2x", __func__, status);
321 dbg("%s - write OK !", __func__);
325 static int read_immediate(struct usb_serial_port *port, u8 *buf, u8 count)
328 struct usb_serial *serial = port->serial;
331 /* send the data out the bulk port */
333 usb_bulk_msg(serial->dev,
334 usb_rcvbulkpipe(serial->dev,
335 port->bulk_in_endpointAddress), buf,
336 count, &actual, HZ * 1);
338 if (status != IUU_OPERATION_OK)
339 dbg("%s - error = %2x", __func__, status);
341 dbg("%s - read OK !", __func__);
345 static int iuu_led(struct usb_serial_port *port, unsigned int R,
346 unsigned int G, unsigned int B, u8 f)
350 buf = kmalloc(8, GFP_KERNEL);
354 buf[0] = IUU_SET_LED;
356 buf[2] = (R >> 8) & 0xFF;
358 buf[4] = (G >> 8) & 0xFF;
360 buf[6] = (B >> 8) & 0xFF;
362 status = bulk_immediate(port, buf, 8);
364 if (status != IUU_OPERATION_OK)
365 dbg("%s - led error status = %2x", __func__, status);
367 dbg("%s - led OK !", __func__);
368 return IUU_OPERATION_OK;
371 static void iuu_rgbf_fill_buffer(u8 *buf, u8 r1, u8 r2, u8 g1, u8 g2, u8 b1,
374 *buf++ = IUU_SET_LED;
384 static void iuu_led_activity_on(struct urb *urb)
386 struct usb_serial_port *port = urb->context;
388 char *buf_ptr = port->write_urb->transfer_buffer;
389 *buf_ptr++ = IUU_SET_LED;
391 get_random_bytes(buf_ptr, 6);
394 iuu_rgbf_fill_buffer(buf_ptr, 255, 255, 0, 0, 0, 0, 255);
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 ,
402 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
405 static void iuu_led_activity_off(struct urb *urb)
407 struct usb_serial_port *port = urb->context;
409 char *buf_ptr = port->write_urb->transfer_buffer;
414 *buf_ptr++ = IUU_SET_LED;
415 iuu_rgbf_fill_buffer(buf_ptr, 0, 0, 255, 255, 0, 0, 255);
417 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
418 usb_sndbulkpipe(port->serial->dev,
419 port->bulk_out_endpointAddress),
420 port->write_urb->transfer_buffer, 8 ,
422 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
427 static int iuu_clk(struct usb_serial_port *port, int dwFrq)
430 struct iuu_private *priv = usb_get_serial_port_data(port);
433 u8 DIV = 0; /* 8bit */
434 u8 XDRV = 0; /* 8bit */
435 u8 PUMP = 0; /* 3bit */
436 u8 PBmsb = 0; /* 2bit */
437 u8 PBlsb = 0; /* 8bit */
438 u8 PO = 0; /* 1bit */
443 int frq = (int)dwFrq;
446 priv->buf[Count++] = IUU_UART_WRITE_I2C;
447 priv->buf[Count++] = FrqGenAdr << 1;
448 priv->buf[Count++] = 0x09;
449 priv->buf[Count++] = 0x00;
451 status = bulk_immediate(port, (u8 *) priv->buf, Count);
453 dbg("%s - write error ", __func__);
456 } else if (frq == 3579000) {
461 } else if (frq == 3680000) {
466 } else if (frq == 6000000) {
472 unsigned int result = 0;
473 unsigned int tmp = 0;
478 unsigned int lP = 2055;
479 unsigned int lDiv = 4;
481 for (lQ = 2; lQ <= 47 && !found; lQ++)
482 for (lP = 2055; lP >= 8 && !found; lP--)
483 for (lDiv = 4; lDiv <= 127 && !found; lDiv++) {
484 tmp = (12000000 / lDiv) * (lP / lQ);
485 if (abs((int)(tmp - frq)) <
486 abs((int)(frq - result))) {
487 check2 = (12000000 / lQ);
490 check = (12000000 / lQ) * lP;
491 if (check > 400000000)
493 if (check < 100000000)
495 if (lDiv < 4 || lDiv > 127)
506 P2 = ((P - PO) / 2) - 4;
509 PBmsb = (P2 >> 8 & 0x03);
511 PO = (P >> 10) & 0x01;
514 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
515 priv->buf[Count++] = FrqGenAdr << 1;
516 priv->buf[Count++] = 0x09;
517 priv->buf[Count++] = 0x20; /* Adr = 0x09 */
518 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
519 priv->buf[Count++] = FrqGenAdr << 1;
520 priv->buf[Count++] = 0x0C;
521 priv->buf[Count++] = DIV; /* Adr = 0x0C */
522 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
523 priv->buf[Count++] = FrqGenAdr << 1;
524 priv->buf[Count++] = 0x12;
525 priv->buf[Count++] = XDRV; /* Adr = 0x12 */
526 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
527 priv->buf[Count++] = FrqGenAdr << 1;
528 priv->buf[Count++] = 0x13;
529 priv->buf[Count++] = 0x6B; /* Adr = 0x13 */
530 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
531 priv->buf[Count++] = FrqGenAdr << 1;
532 priv->buf[Count++] = 0x40;
533 priv->buf[Count++] = (0xC0 | ((PUMP & 0x07) << 2)) |
534 (PBmsb & 0x03); /* Adr = 0x40 */
535 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
536 priv->buf[Count++] = FrqGenAdr << 1;
537 priv->buf[Count++] = 0x41;
538 priv->buf[Count++] = PBlsb; /* Adr = 0x41 */
539 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
540 priv->buf[Count++] = FrqGenAdr << 1;
541 priv->buf[Count++] = 0x42;
542 priv->buf[Count++] = Q | (((PO & 0x01) << 7)); /* Adr = 0x42 */
543 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
544 priv->buf[Count++] = FrqGenAdr << 1;
545 priv->buf[Count++] = 0x44;
546 priv->buf[Count++] = (char)0xFF; /* Adr = 0x44 */
547 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
548 priv->buf[Count++] = FrqGenAdr << 1;
549 priv->buf[Count++] = 0x45;
550 priv->buf[Count++] = (char)0xFE; /* Adr = 0x45 */
551 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
552 priv->buf[Count++] = FrqGenAdr << 1;
553 priv->buf[Count++] = 0x46;
554 priv->buf[Count++] = 0x7F; /* Adr = 0x46 */
555 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
556 priv->buf[Count++] = FrqGenAdr << 1;
557 priv->buf[Count++] = 0x47;
558 priv->buf[Count++] = (char)0x84; /* Adr = 0x47 */
560 status = bulk_immediate(port, (u8 *) priv->buf, Count);
561 if (status != IUU_OPERATION_OK)
562 dbg("%s - write error ", __func__);
566 static int iuu_uart_flush(struct usb_serial_port *port)
570 u8 rxcmd = IUU_UART_RX;
571 struct iuu_private *priv = usb_get_serial_port_data(port);
573 if (iuu_led(port, 0xF000, 0, 0, 0xFF) < 0)
576 for (i = 0; i < 2; i++) {
577 status = bulk_immediate(port, &rxcmd, 1);
578 if (status != IUU_OPERATION_OK) {
579 dbg("%s - uart_flush_write error", __func__);
583 status = read_immediate(port, &priv->len, 1);
584 if (status != IUU_OPERATION_OK) {
585 dbg("%s - uart_flush_read error", __func__);
590 dbg("%s - uart_flush datalen is : %i ", __func__,
592 status = read_immediate(port, priv->buf, priv->len);
593 if (status != IUU_OPERATION_OK) {
594 dbg("%s - uart_flush_read error", __func__);
599 dbg("%s - uart_flush_read OK!", __func__);
600 iuu_led(port, 0, 0xF000, 0, 0xFF);
604 static void read_buf_callback(struct urb *urb)
606 struct usb_serial_port *port = urb->context;
607 unsigned char *data = urb->transfer_buffer;
608 struct tty_struct *tty;
609 int status = urb->status;
612 if (status == -EPROTO) {
613 /* reschedule needed */
618 dbg("%s - %i chars to write", __func__, urb->actual_length);
619 tty = tty_port_tty_get(&port->port);
621 dbg("%s - data is NULL !!!", __func__);
622 if (tty && urb->actual_length && data) {
623 tty_insert_flip_string(tty, data, urb->actual_length);
624 tty_flip_buffer_push(tty);
627 iuu_led_activity_on(urb);
630 static int iuu_bulk_write(struct usb_serial_port *port)
632 struct iuu_private *priv = usb_get_serial_port_data(port);
637 char *buf_ptr = port->write_urb->transfer_buffer;
639 spin_lock_irqsave(&priv->lock, flags);
640 *buf_ptr++ = IUU_UART_ESC;
641 *buf_ptr++ = IUU_UART_TX;
642 *buf_ptr++ = priv->writelen;
644 memcpy(buf_ptr, priv->writebuf, priv->writelen);
645 buf_len = priv->writelen;
647 spin_unlock_irqrestore(&priv->lock, flags);
649 for (i = 0; i < buf_len; i++)
650 sprintf(priv->dbgbuf + i*2 ,
651 "%02X", priv->writebuf[i]);
652 priv->dbgbuf[buf_len+i*2] = 0;
653 dbg("%s - writing %i chars : %s", __func__,
654 buf_len, priv->dbgbuf);
656 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
657 usb_sndbulkpipe(port->serial->dev,
658 port->bulk_out_endpointAddress),
659 port->write_urb->transfer_buffer, buf_len + 3,
661 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
662 usb_serial_port_softint(port);
666 static int iuu_read_buf(struct usb_serial_port *port, int len)
670 usb_fill_bulk_urb(port->read_urb, port->serial->dev,
671 usb_rcvbulkpipe(port->serial->dev,
672 port->bulk_in_endpointAddress),
673 port->read_urb->transfer_buffer, len,
674 read_buf_callback, port);
675 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
679 static void iuu_uart_read_callback(struct urb *urb)
681 struct usb_serial_port *port = urb->context;
682 struct iuu_private *priv = usb_get_serial_port_data(port);
684 int status = urb->status;
687 unsigned char *data = urb->transfer_buffer;
691 dbg("%s - status = %d", __func__, status);
696 dbg("%s - data is NULL !!!", __func__);
698 if (urb->actual_length == 1 && data != NULL)
701 if (urb->actual_length > 1) {
702 dbg("%s - urb->actual_length = %i", __func__,
707 /* if len > 0 call readbuf */
709 if (len > 0 && error == 0) {
710 dbg("%s - call read buf - len to read is %i ",
712 status = iuu_read_buf(port, len);
715 /* need to update status ? */
716 if (priv->poll > 99) {
717 status = iuu_status(port);
722 /* reset waiting ? */
724 if (priv->reset == 1) {
725 status = iuu_reset(port, 0xC);
728 /* Writebuf is waiting */
729 spin_lock_irqsave(&priv->lock, flags);
730 if (priv->writelen > 0) {
731 spin_unlock_irqrestore(&priv->lock, flags);
732 status = iuu_bulk_write(port);
735 spin_unlock_irqrestore(&priv->lock, flags);
736 /* if nothing to write call again rxcmd */
737 dbg("%s - rxcmd recall", __func__);
738 iuu_led_activity_off(urb);
741 static int iuu_uart_write(struct tty_struct *tty, struct usb_serial_port *port,
742 const u8 *buf, int count)
744 struct iuu_private *priv = usb_get_serial_port_data(port);
750 spin_lock_irqsave(&priv->lock, flags);
752 /* fill the buffer */
753 memcpy(priv->writebuf + priv->writelen, buf, count);
754 priv->writelen += count;
755 spin_unlock_irqrestore(&priv->lock, flags);
760 static void read_rxcmd_callback(struct urb *urb)
762 struct usb_serial_port *port = urb->context;
764 int status = urb->status;
771 usb_fill_bulk_urb(port->read_urb, port->serial->dev,
772 usb_rcvbulkpipe(port->serial->dev,
773 port->bulk_in_endpointAddress),
774 port->read_urb->transfer_buffer, 256,
775 iuu_uart_read_callback, port);
776 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
777 dbg("%s - submit result = %d", __func__, result);
780 static int iuu_uart_on(struct usb_serial_port *port)
785 buf = kmalloc(sizeof(u8) * 4, GFP_KERNEL);
790 buf[0] = IUU_UART_ENABLE;
791 buf[1] = (u8) ((IUU_BAUD_9600 >> 8) & 0x00FF);
792 buf[2] = (u8) (0x00FF & IUU_BAUD_9600);
793 buf[3] = (u8) (0x0F0 & IUU_ONE_STOP_BIT) | (0x07 & IUU_PARITY_EVEN);
795 status = bulk_immediate(port, buf, 4);
796 if (status != IUU_OPERATION_OK) {
797 dbg("%s - uart_on error", __func__);
798 goto uart_enable_failed;
800 /* iuu_reset() the card after iuu_uart_on() */
801 status = iuu_uart_flush(port);
802 if (status != IUU_OPERATION_OK)
803 dbg("%s - uart_flush error", __func__);
809 /* Diables the IUU UART (a.k.a. the Phoenix voiderface) */
810 static int iuu_uart_off(struct usb_serial_port *port)
814 buf = kmalloc(1, GFP_KERNEL);
817 buf[0] = IUU_UART_DISABLE;
819 status = bulk_immediate(port, buf, 1);
820 if (status != IUU_OPERATION_OK)
821 dbg("%s - uart_off error", __func__);
827 static int iuu_uart_baud(struct usb_serial_port *port, u32 baud_base,
828 u32 *actual, u8 parity)
836 unsigned int T1FrekvensHZ = 0;
838 dbg("%s - enter baud_base=%d", __func__, baud_base);
839 dataout = kmalloc(sizeof(u8) * 5, GFP_KERNEL);
843 /*baud = (((priv->clk / 35) * baud_base) / 100000); */
846 if (baud < 1200 || baud > 230400) {
848 return IUU_INVALID_PARAMETER;
852 T1FrekvensHZ = 500000;
857 T1FrekvensHZ = 2000000;
862 T1FrekvensHZ = 6000000;
867 T1FrekvensHZ = 24000000;
870 T1reload = 256 - (u8) (T1FrekvensHZ / (baud * 2));
872 /* magic number here: ENTER_FIRMWARE_UPDATE; */
873 dataout[DataCount++] = IUU_UART_ESC;
874 /* magic number here: CHANGE_BAUD; */
875 dataout[DataCount++] = IUU_UART_CHANGE;
876 dataout[DataCount++] = T1Frekvens;
877 dataout[DataCount++] = T1reload;
879 *actual = (T1FrekvensHZ / (256 - T1reload)) / 2;
881 switch (parity & 0x0F) {
882 case IUU_PARITY_NONE:
883 dataout[DataCount++] = 0x00;
885 case IUU_PARITY_EVEN:
886 dataout[DataCount++] = 0x01;
889 dataout[DataCount++] = 0x02;
891 case IUU_PARITY_MARK:
892 dataout[DataCount++] = 0x03;
894 case IUU_PARITY_SPACE:
895 dataout[DataCount++] = 0x04;
899 return IUU_INVALID_PARAMETER;
903 switch (parity & 0xF0) {
904 case IUU_ONE_STOP_BIT:
905 dataout[DataCount - 1] |= IUU_ONE_STOP_BIT;
908 case IUU_TWO_STOP_BITS:
909 dataout[DataCount - 1] |= IUU_TWO_STOP_BITS;
913 return IUU_INVALID_PARAMETER;
917 status = bulk_immediate(port, dataout, DataCount);
918 if (status != IUU_OPERATION_OK)
919 dbg("%s - uart_off error", __func__);
924 static void iuu_set_termios(struct tty_struct *tty,
925 struct usb_serial_port *port, struct ktermios *old_termios)
927 const u32 supported_mask = CMSPAR|PARENB|PARODD;
928 struct iuu_private *priv = usb_get_serial_port_data(port);
929 unsigned int cflag = tty->termios->c_cflag;
935 u32 newval = cflag & supported_mask;
937 /* Just use the ospeed. ispeed should be the same. */
938 baud = tty->termios->c_ospeed;
940 dbg("%s - enter c_ospeed or baud=%d", __func__, baud);
942 /* compute the parity parameter */
944 if (cflag & CMSPAR) { /* Using mark space */
946 parity |= IUU_PARITY_SPACE;
948 parity |= IUU_PARITY_MARK;
949 } else if (!(cflag & PARENB)) {
950 parity |= IUU_PARITY_NONE;
952 } else if (cflag & PARODD)
953 parity |= IUU_PARITY_ODD;
955 parity |= IUU_PARITY_EVEN;
957 parity |= (cflag & CSTOPB ? IUU_TWO_STOP_BITS : IUU_ONE_STOP_BIT);
960 status = iuu_uart_baud(port,
961 baud * priv->boost / 100,
964 /* set the termios value to the real one, so the user now what has
965 * changed. We support few fields so its easies to copy the old hw
966 * settings back over and then adjust them
969 tty_termios_copy_hw(tty->termios, old_termios);
970 if (status != 0) /* Set failed - return old bits */
972 /* Re-encode speed, parity and csize */
973 tty_encode_baud_rate(tty, baud, baud);
974 tty->termios->c_cflag &= ~(supported_mask|CSIZE);
975 tty->termios->c_cflag |= newval | csize;
978 static void iuu_close(struct usb_serial_port *port)
980 /* iuu_led (port,255,0,0,0); */
981 struct usb_serial *serial;
983 serial = port->serial;
990 /* shutdown our urbs */
991 dbg("%s - shutting down urbs", __func__);
992 usb_kill_urb(port->write_urb);
993 usb_kill_urb(port->read_urb);
994 usb_kill_urb(port->interrupt_in_urb);
995 iuu_led(port, 0, 0, 0xF000, 0xFF);
999 static void iuu_init_termios(struct tty_struct *tty)
1001 *(tty->termios) = tty_std_termios;
1002 tty->termios->c_cflag = CLOCAL | CREAD | CS8 | B9600
1003 | TIOCM_CTS | CSTOPB | PARENB;
1004 tty->termios->c_ispeed = 9600;
1005 tty->termios->c_ospeed = 9600;
1006 tty->termios->c_lflag = 0;
1007 tty->termios->c_oflag = 0;
1008 tty->termios->c_iflag = 0;
1011 static int iuu_open(struct tty_struct *tty, struct usb_serial_port *port)
1013 struct usb_serial *serial = port->serial;
1018 struct iuu_private *priv = usb_get_serial_port_data(port);
1020 baud = tty->termios->c_ospeed;
1021 tty->termios->c_ispeed = baud;
1022 /* Re-encode speed */
1023 tty_encode_baud_rate(tty, baud, baud);
1025 dbg("%s - port %d, baud %d", __func__, port->number, baud);
1026 usb_clear_halt(serial->dev, port->write_urb->pipe);
1027 usb_clear_halt(serial->dev, port->read_urb->pipe);
1029 buf = kmalloc(10, GFP_KERNEL);
1035 /* initialize writebuf */
1036 #define FISH(a, b, c, d) do { \
1037 result = usb_control_msg(port->serial->dev, \
1038 usb_rcvctrlpipe(port->serial->dev, 0), \
1039 b, a, c, d, buf, 1, 1000); \
1040 dbg("0x%x:0x%x:0x%x:0x%x %d - %x", a, b, c, d, result, \
1041 buf[0]); } while (0);
1043 #define SOUP(a, b, c, d) do { \
1044 result = usb_control_msg(port->serial->dev, \
1045 usb_sndctrlpipe(port->serial->dev, 0), \
1046 b, a, c, d, NULL, 0, 1000); \
1047 dbg("0x%x:0x%x:0x%x:0x%x %d", a, b, c, d, result); } while (0)
1049 /* This is not UART related but IUU USB driver related or something */
1050 /* like that. Basically no IUU will accept any commands from the USB */
1051 /* host unless it has received the following message */
1052 /* sprintf(buf ,"%c%c%c%c",0x03,0x02,0x02,0x0); */
1054 SOUP(0x03, 0x02, 0x02, 0x0);
1056 iuu_led(port, 0xF000, 0xF000, 0, 0xFF);
1060 priv->boost = boost;
1062 switch (clockmode) {
1063 case 2: /* 3.680 Mhz */
1064 priv->clk = IUU_CLK_3680000;
1065 iuu_clk(port, IUU_CLK_3680000 * boost / 100);
1067 iuu_uart_baud(port, baud * boost / 100, &actual,
1070 case 3: /* 6.00 Mhz */
1071 iuu_clk(port, IUU_CLK_6000000 * boost / 100);
1072 priv->clk = IUU_CLK_6000000;
1073 /* Ratio of 6000000 to 3500000 for baud 9600 */
1075 iuu_uart_baud(port, 16457 * boost / 100, &actual,
1078 default: /* 3.579 Mhz */
1079 iuu_clk(port, IUU_CLK_3579000 * boost / 100);
1080 priv->clk = IUU_CLK_3579000;
1082 iuu_uart_baud(port, baud * boost / 100, &actual,
1086 /* set the cardin cardout signals */
1093 iuu_cardin = TIOCM_CD;
1098 iuu_cardout = TIOCM_CD;
1101 iuu_cardin = TIOCM_DSR;
1106 iuu_cardout = TIOCM_DSR;
1109 iuu_cardin = TIOCM_CTS;
1114 iuu_cardout = TIOCM_CTS;
1117 iuu_cardin = TIOCM_RNG;
1122 iuu_cardout = TIOCM_RNG;
1125 iuu_uart_flush(port);
1127 dbg("%s - initialization done", __func__);
1129 memset(port->write_urb->transfer_buffer, IUU_UART_RX, 1);
1130 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
1131 usb_sndbulkpipe(port->serial->dev,
1132 port->bulk_out_endpointAddress),
1133 port->write_urb->transfer_buffer, 1,
1134 read_rxcmd_callback, port);
1135 result = usb_submit_urb(port->write_urb, GFP_KERNEL);
1137 dev_err(&port->dev, "%s - failed submitting read urb,"
1138 " error %d\n", __func__, result);
1141 dbg("%s - rxcmd OK", __func__);
1147 /* how to change VCC */
1148 static int iuu_vcc_set(struct usb_serial_port *port, unsigned int vcc)
1153 buf = kmalloc(5, GFP_KERNEL);
1157 buf[0] = IUU_SET_VCC;
1158 buf[1] = vcc & 0xFF;
1159 buf[2] = (vcc >> 8) & 0xFF;
1160 buf[3] = (vcc >> 16) & 0xFF;
1161 buf[4] = (vcc >> 24) & 0xFF;
1163 status = bulk_immediate(port, buf, 5);
1166 if (status != IUU_OPERATION_OK)
1167 dbg("%s - vcc error status = %2x", __func__, status);
1169 dbg("%s - vcc OK !", __func__);
1178 static ssize_t show_vcc_mode(struct device *dev,
1179 struct device_attribute *attr, char *buf)
1181 struct usb_serial_port *port = to_usb_serial_port(dev);
1182 struct iuu_private *priv = usb_get_serial_port_data(port);
1184 return sprintf(buf, "%d\n", priv->vcc);
1187 static ssize_t store_vcc_mode(struct device *dev,
1188 struct device_attribute *attr, const char *buf, size_t count)
1190 struct usb_serial_port *port = to_usb_serial_port(dev);
1191 struct iuu_private *priv = usb_get_serial_port_data(port);
1194 if (strict_strtoul(buf, 10, &v)) {
1195 dev_err(dev, "%s - vcc_mode: %s is not a unsigned long\n",
1197 goto fail_store_vcc_mode;
1200 dbg("%s: setting vcc_mode = %ld", __func__, v);
1202 if ((v != 3) && (v != 5)) {
1203 dev_err(dev, "%s - vcc_mode %ld is invalid\n", __func__, v);
1205 iuu_vcc_set(port, v);
1208 fail_store_vcc_mode:
1212 static DEVICE_ATTR(vcc_mode, S_IRUSR | S_IWUSR, show_vcc_mode,
1215 static int iuu_create_sysfs_attrs(struct usb_serial_port *port)
1217 return device_create_file(&port->dev, &dev_attr_vcc_mode);
1220 static int iuu_remove_sysfs_attrs(struct usb_serial_port *port)
1222 device_remove_file(&port->dev, &dev_attr_vcc_mode);
1227 * End Sysfs Attributes
1230 static struct usb_serial_driver iuu_device = {
1232 .owner = THIS_MODULE,
1233 .name = "iuu_phoenix",
1235 .id_table = id_table,
1237 .bulk_in_size = 512,
1238 .bulk_out_size = 512,
1239 .port_probe = iuu_create_sysfs_attrs,
1240 .port_remove = iuu_remove_sysfs_attrs,
1243 .write = iuu_uart_write,
1244 .read_bulk_callback = iuu_uart_read_callback,
1245 .tiocmget = iuu_tiocmget,
1246 .tiocmset = iuu_tiocmset,
1247 .set_termios = iuu_set_termios,
1248 .init_termios = iuu_init_termios,
1249 .attach = iuu_startup,
1250 .release = iuu_release,
1253 static struct usb_serial_driver * const serial_drivers[] = {
1257 module_usb_serial_driver(iuu_driver, serial_drivers);
1259 MODULE_AUTHOR("Alain Degreffe eczema@ecze.com");
1261 MODULE_DESCRIPTION(DRIVER_DESC);
1262 MODULE_LICENSE("GPL");
1264 MODULE_VERSION(DRIVER_VERSION);
1265 module_param(debug, bool, S_IRUGO | S_IWUSR);
1266 MODULE_PARM_DESC(debug, "Debug enabled or not");
1268 module_param(xmas, bool, S_IRUGO | S_IWUSR);
1269 MODULE_PARM_DESC(xmas, "Xmas colors enabled or not");
1271 module_param(boost, int, S_IRUGO | S_IWUSR);
1272 MODULE_PARM_DESC(boost, "Card overclock boost (in percent 100-500)");
1274 module_param(clockmode, int, S_IRUGO | S_IWUSR);
1275 MODULE_PARM_DESC(clockmode, "Card clock mode (1=3.579 MHz, 2=3.680 MHz, "
1278 module_param(cdmode, int, S_IRUGO | S_IWUSR);
1279 MODULE_PARM_DESC(cdmode, "Card detect mode (0=none, 1=CD, 2=!CD, 3=DSR, "
1280 "4=!DSR, 5=CTS, 6=!CTS, 7=RING, 8=!RING)");
1282 module_param(vcc_default, int, S_IRUGO | S_IWUSR);
1283 MODULE_PARM_DESC(vcc_default, "Set default VCC (either 3 for 3.3V or 5 "
1284 "for 5V). Default to 5.");