2 * Infinity Unlimited USB Phoenix driver
4 * Copyright (C) 2007 Alain Degreffe (eczema@ecze.com)
6 * Original code taken from iuutool (Copyright (C) 2006 Juan Carlos Borrás)
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * And tested with help of WB Electronics
16 #include <linux/kernel.h>
17 #include <linux/errno.h>
18 #include <linux/init.h>
19 #include <linux/slab.h>
20 #include <linux/tty.h>
21 #include <linux/tty_driver.h>
22 #include <linux/tty_flip.h>
23 #include <linux/serial.h>
24 #include <linux/module.h>
25 #include <linux/moduleparam.h>
26 #include <linux/spinlock.h>
27 #include <linux/uaccess.h>
28 #include <linux/usb.h>
29 #include <linux/usb/serial.h>
30 #include "iuu_phoenix.h"
31 #include <linux/random.h>
34 #ifdef CONFIG_USB_SERIAL_DEBUG
43 #define DRIVER_VERSION "v0.10"
44 #define DRIVER_DESC "Infinity USB Unlimited Phoenix driver"
46 static struct usb_device_id id_table[] = {
47 {USB_DEVICE(IUU_USB_VENDOR_ID, IUU_USB_PRODUCT_ID)},
48 {} /* Terminating entry */
50 MODULE_DEVICE_TABLE(usb, id_table);
52 static struct usb_driver iuu_driver = {
53 .name = "iuu_phoenix",
54 .probe = usb_serial_probe,
55 .disconnect = usb_serial_disconnect,
61 static int boost = 100;
62 static int clockmode = 1;
63 static int cdmode = 1;
64 static int iuu_cardin;
65 static int iuu_cardout;
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 u8 termios_initialized;
75 int tiostatus; /* store IUART SIGNAL for tiocmget call */
76 u8 reset; /* if 1 reset is needed */
77 int poll; /* number of poll */
78 u8 *writebuf; /* buffer for writing to device */
79 int writelen; /* num of byte to write to device */
80 u8 *buf; /* used for initialize speed */
81 u8 *dbgbuf; /* debug buffer */
86 static void iuu_free_buf(struct iuu_private *priv)
90 kfree(priv->writebuf);
93 static int iuu_alloc_buf(struct iuu_private *priv)
95 priv->buf = kzalloc(256, GFP_KERNEL);
96 priv->dbgbuf = kzalloc(256, GFP_KERNEL);
97 priv->writebuf = kzalloc(256, GFP_KERNEL);
98 if (!priv->buf || !priv->dbgbuf || !priv->writebuf) {
100 dbg("%s problem allocation buffer", __func__);
103 dbg("%s - Privates buffers allocation success", __func__);
107 static int iuu_startup(struct usb_serial *serial)
109 struct iuu_private *priv;
110 priv = kzalloc(sizeof(struct iuu_private), GFP_KERNEL);
111 dbg("%s- priv allocation success", __func__);
114 if (iuu_alloc_buf(priv)) {
118 spin_lock_init(&priv->lock);
119 init_waitqueue_head(&priv->delta_msr_wait);
120 usb_set_serial_port_data(serial->port[0], priv);
124 /* Release function */
125 static void iuu_release(struct usb_serial *serial)
127 struct usb_serial_port *port = serial->port[0];
128 struct iuu_private *priv = usb_get_serial_port_data(port);
136 dbg("%s - I will free all", __func__);
137 usb_set_serial_port_data(port, NULL);
139 dbg("%s - priv is not anymore in port structure", __func__);
142 dbg("%s priv is now kfree", __func__);
146 static int iuu_tiocmset(struct tty_struct *tty, struct file *file,
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);
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 priv->tiostatus = TIOCM_RTS;
161 if (!(set & TIOCM_RTS) && priv->tiostatus == TIOCM_RTS) {
162 dbg("%s TIOCMSET RESET called !!!", __func__);
165 spin_unlock_irqrestore(&priv->lock, flags);
169 /* This is used to provide a carrier detect mechanism
170 * When a card is present, the response is 0x00
171 * When no card , the reader respond with TIOCM_CD
172 * This is known as CD autodetect mechanism
174 static int iuu_tiocmget(struct tty_struct *tty, struct file *file)
176 struct usb_serial_port *port = tty->driver_data;
177 struct iuu_private *priv = usb_get_serial_port_data(port);
181 spin_lock_irqsave(&priv->lock, flags);
182 rc = priv->tiostatus;
183 spin_unlock_irqrestore(&priv->lock, flags);
188 static void iuu_rxcmd(struct urb *urb)
190 struct usb_serial_port *port = urb->context;
192 int status = urb->status;
194 dbg("%s - enter", __func__);
197 dbg("%s - status = %d", __func__, status);
203 memset(port->write_urb->transfer_buffer, IUU_UART_RX, 1);
204 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
205 usb_sndbulkpipe(port->serial->dev,
206 port->bulk_out_endpointAddress),
207 port->write_urb->transfer_buffer, 1,
208 read_rxcmd_callback, port);
209 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
212 static int iuu_reset(struct usb_serial_port *port, u8 wt)
214 struct iuu_private *priv = usb_get_serial_port_data(port);
216 char *buf_ptr = port->write_urb->transfer_buffer;
217 dbg("%s - enter", __func__);
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;
251 dbg("%s - enter", __func__);
254 dbg("%s - status = %d", __func__, status);
259 st = urb->transfer_buffer;
260 dbg("%s - enter", __func__);
261 if (urb->actual_length == 1) {
264 priv->tiostatus = iuu_cardout;
267 priv->tiostatus = iuu_cardin;
270 priv->tiostatus = iuu_cardin;
276 static void iuu_status_callback(struct urb *urb)
278 struct usb_serial_port *port = urb->context;
280 int status = urb->status;
282 dbg("%s - status = %d", __func__, status);
283 usb_fill_bulk_urb(port->read_urb, port->serial->dev,
284 usb_rcvbulkpipe(port->serial->dev,
285 port->bulk_in_endpointAddress),
286 port->read_urb->transfer_buffer, 256,
287 iuu_update_status_callback, port);
288 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
291 static int iuu_status(struct usb_serial_port *port)
295 dbg("%s - enter", __func__);
297 memset(port->write_urb->transfer_buffer, IUU_GET_STATE_REGISTER, 1);
298 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
299 usb_sndbulkpipe(port->serial->dev,
300 port->bulk_out_endpointAddress),
301 port->write_urb->transfer_buffer, 1,
302 iuu_status_callback, port);
303 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
308 static int bulk_immediate(struct usb_serial_port *port, u8 *buf, u8 count)
311 struct usb_serial *serial = port->serial;
314 dbg("%s - enter", __func__);
316 /* send the data out the bulk port */
319 usb_bulk_msg(serial->dev,
320 usb_sndbulkpipe(serial->dev,
321 port->bulk_out_endpointAddress), buf,
322 count, &actual, HZ * 1);
324 if (status != IUU_OPERATION_OK)
325 dbg("%s - error = %2x", __func__, status);
327 dbg("%s - write OK !", __func__);
331 static int read_immediate(struct usb_serial_port *port, u8 *buf, u8 count)
334 struct usb_serial *serial = port->serial;
337 dbg("%s - enter", __func__);
339 /* send the data out the bulk port */
342 usb_bulk_msg(serial->dev,
343 usb_rcvbulkpipe(serial->dev,
344 port->bulk_in_endpointAddress), buf,
345 count, &actual, HZ * 1);
347 if (status != IUU_OPERATION_OK)
348 dbg("%s - error = %2x", __func__, status);
350 dbg("%s - read OK !", __func__);
354 static int iuu_led(struct usb_serial_port *port, unsigned int R,
355 unsigned int G, unsigned int B, u8 f)
359 buf = kmalloc(8, GFP_KERNEL);
363 dbg("%s - enter", __func__);
365 buf[0] = IUU_SET_LED;
367 buf[2] = (R >> 8) & 0xFF;
369 buf[4] = (G >> 8) & 0xFF;
371 buf[6] = (B >> 8) & 0xFF;
373 status = bulk_immediate(port, buf, 8);
375 if (status != IUU_OPERATION_OK)
376 dbg("%s - led error status = %2x", __func__, status);
378 dbg("%s - led OK !", __func__);
379 return IUU_OPERATION_OK;
382 static void iuu_rgbf_fill_buffer(u8 *buf, u8 r1, u8 r2, u8 g1, u8 g2, u8 b1,
385 *buf++ = IUU_SET_LED;
395 static void iuu_led_activity_on(struct urb *urb)
397 struct usb_serial_port *port = urb->context;
399 char *buf_ptr = port->write_urb->transfer_buffer;
400 *buf_ptr++ = IUU_SET_LED;
402 get_random_bytes(buf_ptr, 6);
405 iuu_rgbf_fill_buffer(buf_ptr, 255, 255, 0, 0, 0, 0, 255);
408 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
409 usb_sndbulkpipe(port->serial->dev,
410 port->bulk_out_endpointAddress),
411 port->write_urb->transfer_buffer, 8 ,
413 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
416 static void iuu_led_activity_off(struct urb *urb)
418 struct usb_serial_port *port = urb->context;
420 char *buf_ptr = port->write_urb->transfer_buffer;
425 *buf_ptr++ = IUU_SET_LED;
426 iuu_rgbf_fill_buffer(buf_ptr, 0, 0, 255, 255, 0, 0, 255);
428 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
429 usb_sndbulkpipe(port->serial->dev,
430 port->bulk_out_endpointAddress),
431 port->write_urb->transfer_buffer, 8 ,
433 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
438 static int iuu_clk(struct usb_serial_port *port, int dwFrq)
441 struct iuu_private *priv = usb_get_serial_port_data(port);
444 u8 DIV = 0; /* 8bit */
445 u8 XDRV = 0; /* 8bit */
446 u8 PUMP = 0; /* 3bit */
447 u8 PBmsb = 0; /* 2bit */
448 u8 PBlsb = 0; /* 8bit */
449 u8 PO = 0; /* 1bit */
454 int frq = (int)dwFrq;
456 dbg("%s - enter", __func__);
459 priv->buf[Count++] = IUU_UART_WRITE_I2C;
460 priv->buf[Count++] = FrqGenAdr << 1;
461 priv->buf[Count++] = 0x09;
462 priv->buf[Count++] = 0x00;
464 status = bulk_immediate(port, (u8 *) priv->buf, Count);
466 dbg("%s - write error ", __func__);
469 } else if (frq == 3579000) {
474 } else if (frq == 3680000) {
479 } else if (frq == 6000000) {
485 unsigned int result = 0;
486 unsigned int tmp = 0;
491 unsigned int lP = 2055;
492 unsigned int lDiv = 4;
494 for (lQ = 2; lQ <= 47 && !found; lQ++)
495 for (lP = 2055; lP >= 8 && !found; lP--)
496 for (lDiv = 4; lDiv <= 127 && !found; lDiv++) {
497 tmp = (12000000 / lDiv) * (lP / lQ);
498 if (abs((int)(tmp - frq)) <
499 abs((int)(frq - result))) {
500 check2 = (12000000 / lQ);
503 check = (12000000 / lQ) * lP;
504 if (check > 400000000)
506 if (check < 100000000)
508 if (lDiv < 4 || lDiv > 127)
519 P2 = ((P - PO) / 2) - 4;
522 PBmsb = (P2 >> 8 & 0x03);
524 PO = (P >> 10) & 0x01;
527 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
528 priv->buf[Count++] = FrqGenAdr << 1;
529 priv->buf[Count++] = 0x09;
530 priv->buf[Count++] = 0x20; /* Adr = 0x09 */
531 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
532 priv->buf[Count++] = FrqGenAdr << 1;
533 priv->buf[Count++] = 0x0C;
534 priv->buf[Count++] = DIV; /* Adr = 0x0C */
535 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
536 priv->buf[Count++] = FrqGenAdr << 1;
537 priv->buf[Count++] = 0x12;
538 priv->buf[Count++] = XDRV; /* Adr = 0x12 */
539 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
540 priv->buf[Count++] = FrqGenAdr << 1;
541 priv->buf[Count++] = 0x13;
542 priv->buf[Count++] = 0x6B; /* Adr = 0x13 */
543 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
544 priv->buf[Count++] = FrqGenAdr << 1;
545 priv->buf[Count++] = 0x40;
546 priv->buf[Count++] = (0xC0 | ((PUMP & 0x07) << 2)) |
547 (PBmsb & 0x03); /* Adr = 0x40 */
548 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
549 priv->buf[Count++] = FrqGenAdr << 1;
550 priv->buf[Count++] = 0x41;
551 priv->buf[Count++] = PBlsb; /* Adr = 0x41 */
552 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
553 priv->buf[Count++] = FrqGenAdr << 1;
554 priv->buf[Count++] = 0x42;
555 priv->buf[Count++] = Q | (((PO & 0x01) << 7)); /* Adr = 0x42 */
556 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
557 priv->buf[Count++] = FrqGenAdr << 1;
558 priv->buf[Count++] = 0x44;
559 priv->buf[Count++] = (char)0xFF; /* Adr = 0x44 */
560 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
561 priv->buf[Count++] = FrqGenAdr << 1;
562 priv->buf[Count++] = 0x45;
563 priv->buf[Count++] = (char)0xFE; /* Adr = 0x45 */
564 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
565 priv->buf[Count++] = FrqGenAdr << 1;
566 priv->buf[Count++] = 0x46;
567 priv->buf[Count++] = 0x7F; /* Adr = 0x46 */
568 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
569 priv->buf[Count++] = FrqGenAdr << 1;
570 priv->buf[Count++] = 0x47;
571 priv->buf[Count++] = (char)0x84; /* Adr = 0x47 */
573 status = bulk_immediate(port, (u8 *) priv->buf, Count);
574 if (status != IUU_OPERATION_OK)
575 dbg("%s - write error ", __func__);
579 static int iuu_uart_flush(struct usb_serial_port *port)
583 u8 rxcmd = IUU_UART_RX;
584 struct iuu_private *priv = usb_get_serial_port_data(port);
586 dbg("%s - enter", __func__);
588 if (iuu_led(port, 0xF000, 0, 0, 0xFF) < 0)
591 for (i = 0; i < 2; i++) {
592 status = bulk_immediate(port, &rxcmd, 1);
593 if (status != IUU_OPERATION_OK) {
594 dbg("%s - uart_flush_write error", __func__);
598 status = read_immediate(port, &priv->len, 1);
599 if (status != IUU_OPERATION_OK) {
600 dbg("%s - uart_flush_read error", __func__);
605 dbg("%s - uart_flush datalen is : %i ", __func__,
607 status = read_immediate(port, priv->buf, priv->len);
608 if (status != IUU_OPERATION_OK) {
609 dbg("%s - uart_flush_read error", __func__);
614 dbg("%s - uart_flush_read OK!", __func__);
615 iuu_led(port, 0, 0xF000, 0, 0xFF);
619 static void read_buf_callback(struct urb *urb)
621 struct usb_serial_port *port = urb->context;
622 unsigned char *data = urb->transfer_buffer;
623 struct tty_struct *tty;
624 int status = urb->status;
626 dbg("%s - status = %d", __func__, status);
629 if (status == -EPROTO) {
630 /* reschedule needed */
635 dbg("%s - %i chars to write", __func__, urb->actual_length);
636 tty = tty_port_tty_get(&port->port);
638 dbg("%s - data is NULL !!!", __func__);
639 if (tty && urb->actual_length && data) {
640 tty_insert_flip_string(tty, data, urb->actual_length);
641 tty_flip_buffer_push(tty);
644 iuu_led_activity_on(urb);
647 static int iuu_bulk_write(struct usb_serial_port *port)
649 struct iuu_private *priv = usb_get_serial_port_data(port);
654 char *buf_ptr = port->write_urb->transfer_buffer;
655 dbg("%s - enter", __func__);
657 spin_lock_irqsave(&priv->lock, flags);
658 *buf_ptr++ = IUU_UART_ESC;
659 *buf_ptr++ = IUU_UART_TX;
660 *buf_ptr++ = priv->writelen;
662 memcpy(buf_ptr, priv->writebuf, priv->writelen);
663 buf_len = priv->writelen;
665 spin_unlock_irqrestore(&priv->lock, flags);
667 for (i = 0; i < buf_len; i++)
668 sprintf(priv->dbgbuf + i*2 ,
669 "%02X", priv->writebuf[i]);
670 priv->dbgbuf[buf_len+i*2] = 0;
671 dbg("%s - writing %i chars : %s", __func__,
672 buf_len, priv->dbgbuf);
674 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
675 usb_sndbulkpipe(port->serial->dev,
676 port->bulk_out_endpointAddress),
677 port->write_urb->transfer_buffer, buf_len + 3,
679 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
680 usb_serial_port_softint(port);
684 static int iuu_read_buf(struct usb_serial_port *port, int len)
687 dbg("%s - enter", __func__);
689 usb_fill_bulk_urb(port->read_urb, port->serial->dev,
690 usb_rcvbulkpipe(port->serial->dev,
691 port->bulk_in_endpointAddress),
692 port->read_urb->transfer_buffer, len,
693 read_buf_callback, port);
694 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
698 static void iuu_uart_read_callback(struct urb *urb)
700 struct usb_serial_port *port = urb->context;
701 struct iuu_private *priv = usb_get_serial_port_data(port);
703 int status = urb->status;
706 unsigned char *data = urb->transfer_buffer;
709 dbg("%s - enter", __func__);
712 dbg("%s - status = %d", __func__, status);
717 dbg("%s - data is NULL !!!", __func__);
719 if (urb->actual_length == 1 && data != NULL)
722 if (urb->actual_length > 1) {
723 dbg("%s - urb->actual_length = %i", __func__,
728 /* if len > 0 call readbuf */
730 if (len > 0 && error == 0) {
731 dbg("%s - call read buf - len to read is %i ",
733 status = iuu_read_buf(port, len);
736 /* need to update status ? */
737 if (priv->poll > 99) {
738 status = iuu_status(port);
743 /* reset waiting ? */
745 if (priv->reset == 1) {
746 status = iuu_reset(port, 0xC);
749 /* Writebuf is waiting */
750 spin_lock_irqsave(&priv->lock, flags);
751 if (priv->writelen > 0) {
752 spin_unlock_irqrestore(&priv->lock, flags);
753 status = iuu_bulk_write(port);
756 spin_unlock_irqrestore(&priv->lock, flags);
757 /* if nothing to write call again rxcmd */
758 dbg("%s - rxcmd recall", __func__);
759 iuu_led_activity_off(urb);
762 static int iuu_uart_write(struct tty_struct *tty, struct usb_serial_port *port,
763 const u8 *buf, int count)
765 struct iuu_private *priv = usb_get_serial_port_data(port);
767 dbg("%s - enter", __func__);
772 spin_lock_irqsave(&priv->lock, flags);
774 /* fill the buffer */
775 memcpy(priv->writebuf + priv->writelen, buf, count);
776 priv->writelen += count;
777 spin_unlock_irqrestore(&priv->lock, flags);
782 static void read_rxcmd_callback(struct urb *urb)
784 struct usb_serial_port *port = urb->context;
786 int status = urb->status;
788 dbg("%s - status = %d", __func__, status);
795 usb_fill_bulk_urb(port->read_urb, port->serial->dev,
796 usb_rcvbulkpipe(port->serial->dev,
797 port->bulk_in_endpointAddress),
798 port->read_urb->transfer_buffer, 256,
799 iuu_uart_read_callback, port);
800 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
801 dbg("%s - submit result = %d", __func__, result);
805 static int iuu_uart_on(struct usb_serial_port *port)
810 buf = kmalloc(sizeof(u8) * 4, GFP_KERNEL);
815 buf[0] = IUU_UART_ENABLE;
816 buf[1] = (u8) ((IUU_BAUD_9600 >> 8) & 0x00FF);
817 buf[2] = (u8) (0x00FF & IUU_BAUD_9600);
818 buf[3] = (u8) (0x0F0 & IUU_ONE_STOP_BIT) | (0x07 & IUU_PARITY_EVEN);
820 status = bulk_immediate(port, buf, 4);
821 if (status != IUU_OPERATION_OK) {
822 dbg("%s - uart_on error", __func__);
823 goto uart_enable_failed;
825 /* iuu_reset() the card after iuu_uart_on() */
826 status = iuu_uart_flush(port);
827 if (status != IUU_OPERATION_OK)
828 dbg("%s - uart_flush error", __func__);
834 /* Diables the IUU UART (a.k.a. the Phoenix voiderface) */
835 static int iuu_uart_off(struct usb_serial_port *port)
839 buf = kmalloc(1, GFP_KERNEL);
842 buf[0] = IUU_UART_DISABLE;
844 status = bulk_immediate(port, buf, 1);
845 if (status != IUU_OPERATION_OK)
846 dbg("%s - uart_off error", __func__);
852 static int iuu_uart_baud(struct usb_serial_port *port, u32 baud,
853 u32 *actual, u8 parity)
860 unsigned int T1FrekvensHZ = 0;
862 dataout = kmalloc(sizeof(u8) * 5, GFP_KERNEL);
867 if (baud < 1200 || baud > 230400) {
869 return IUU_INVALID_PARAMETER;
873 T1FrekvensHZ = 500000;
878 T1FrekvensHZ = 2000000;
883 T1FrekvensHZ = 6000000;
888 T1FrekvensHZ = 24000000;
891 T1reload = 256 - (u8) (T1FrekvensHZ / (baud * 2));
893 /* magic number here: ENTER_FIRMWARE_UPDATE; */
894 dataout[DataCount++] = IUU_UART_ESC;
895 /* magic number here: CHANGE_BAUD; */
896 dataout[DataCount++] = IUU_UART_CHANGE;
897 dataout[DataCount++] = T1Frekvens;
898 dataout[DataCount++] = T1reload;
900 *actual = (T1FrekvensHZ / (256 - T1reload)) / 2;
902 switch (parity & 0x0F) {
903 case IUU_PARITY_NONE:
904 dataout[DataCount++] = 0x00;
906 case IUU_PARITY_EVEN:
907 dataout[DataCount++] = 0x01;
910 dataout[DataCount++] = 0x02;
912 case IUU_PARITY_MARK:
913 dataout[DataCount++] = 0x03;
915 case IUU_PARITY_SPACE:
916 dataout[DataCount++] = 0x04;
920 return IUU_INVALID_PARAMETER;
924 switch (parity & 0xF0) {
925 case IUU_ONE_STOP_BIT:
926 dataout[DataCount - 1] |= IUU_ONE_STOP_BIT;
929 case IUU_TWO_STOP_BITS:
930 dataout[DataCount - 1] |= IUU_TWO_STOP_BITS;
934 return IUU_INVALID_PARAMETER;
938 status = bulk_immediate(port, dataout, DataCount);
939 if (status != IUU_OPERATION_OK)
940 dbg("%s - uart_off error", __func__);
945 static void iuu_set_termios(struct tty_struct *tty,
946 struct usb_serial_port *port, struct ktermios *old_termios)
948 const u32 supported_mask = CMSPAR|PARENB|PARODD;
950 unsigned int cflag = tty->termios->c_cflag;
955 int baud = 9600; /* Fixed for the moment */
956 u32 newval = cflag & supported_mask;
958 /* compute the parity parameter */
960 if (cflag & CMSPAR) { /* Using mark space */
962 parity |= IUU_PARITY_SPACE;
964 parity |= IUU_PARITY_MARK;
965 } else if (!(cflag & PARENB)) {
966 parity |= IUU_PARITY_NONE;
968 } else if (cflag & PARODD)
969 parity |= IUU_PARITY_ODD;
971 parity |= IUU_PARITY_EVEN;
973 parity |= (cflag & CSTOPB ? IUU_TWO_STOP_BITS : IUU_ONE_STOP_BIT);
976 status = iuu_uart_baud(port,
977 (clockmode == 2) ? 16457 : 9600 * boost / 100,
980 /* set the termios value to the real one, so the user now what has
981 * changed. We support few fields so its easies to copy the old hw
982 * settings back over and then adjust them
985 tty_termios_copy_hw(tty->termios, old_termios);
986 if (status != 0) /* Set failed - return old bits */
988 /* Re-encode speed, parity and csize */
989 tty_encode_baud_rate(tty, baud, baud);
990 tty->termios->c_cflag &= ~(supported_mask|CSIZE);
991 tty->termios->c_cflag |= newval | csize;
994 static void iuu_close(struct usb_serial_port *port)
996 /* iuu_led (port,255,0,0,0); */
997 struct usb_serial *serial;
999 serial = port->serial;
1003 dbg("%s - port %d", __func__, port->number);
1008 /* shutdown our urbs */
1009 dbg("%s - shutting down urbs", __func__);
1010 usb_kill_urb(port->write_urb);
1011 usb_kill_urb(port->read_urb);
1012 usb_kill_urb(port->interrupt_in_urb);
1014 /* wait one second to free all buffers */
1015 iuu_led(port, 0, 0, 0xF000, 0xFF);
1017 usb_reset_device(port->serial->dev);
1021 static int iuu_open(struct tty_struct *tty,
1022 struct usb_serial_port *port, struct file *filp)
1024 struct usb_serial *serial = port->serial;
1028 unsigned long flags;
1029 struct iuu_private *priv = usb_get_serial_port_data(port);
1031 dbg("%s - port %d", __func__, port->number);
1032 usb_clear_halt(serial->dev, port->write_urb->pipe);
1033 usb_clear_halt(serial->dev, port->read_urb->pipe);
1035 buf = kmalloc(10, GFP_KERNEL);
1039 /* fixup the endpoint buffer size */
1040 kfree(port->bulk_out_buffer);
1041 port->bulk_out_buffer = kmalloc(512, GFP_KERNEL);
1042 port->bulk_out_size = 512;
1043 kfree(port->bulk_in_buffer);
1044 port->bulk_in_buffer = kmalloc(512, GFP_KERNEL);
1045 port->bulk_in_size = 512;
1047 if (!port->bulk_out_buffer || !port->bulk_in_buffer) {
1048 kfree(port->bulk_out_buffer);
1049 kfree(port->bulk_in_buffer);
1054 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
1055 usb_sndbulkpipe(port->serial->dev,
1056 port->bulk_out_endpointAddress),
1057 port->bulk_out_buffer, 512,
1061 usb_fill_bulk_urb(port->read_urb, port->serial->dev,
1062 usb_rcvbulkpipe(port->serial->dev,
1063 port->bulk_in_endpointAddress),
1064 port->bulk_in_buffer, 512,
1067 /* set the termios structure */
1068 spin_lock_irqsave(&priv->lock, flags);
1069 if (tty && !priv->termios_initialized) {
1070 *(tty->termios) = tty_std_termios;
1071 tty->termios->c_cflag = CLOCAL | CREAD | CS8 | B9600
1072 | TIOCM_CTS | CSTOPB | PARENB;
1073 tty->termios->c_ispeed = 9600;
1074 tty->termios->c_ospeed = 9600;
1075 tty->termios->c_lflag = 0;
1076 tty->termios->c_oflag = 0;
1077 tty->termios->c_iflag = 0;
1078 priv->termios_initialized = 1;
1081 spin_unlock_irqrestore(&priv->lock, flags);
1083 /* initialize writebuf */
1084 #define FISH(a, b, c, d) do { \
1085 result = usb_control_msg(port->serial->dev, \
1086 usb_rcvctrlpipe(port->serial->dev, 0), \
1087 b, a, c, d, buf, 1, 1000); \
1088 dbg("0x%x:0x%x:0x%x:0x%x %d - %x", a, b, c, d, result, \
1089 buf[0]); } while (0);
1091 #define SOUP(a, b, c, d) do { \
1092 result = usb_control_msg(port->serial->dev, \
1093 usb_sndctrlpipe(port->serial->dev, 0), \
1094 b, a, c, d, NULL, 0, 1000); \
1095 dbg("0x%x:0x%x:0x%x:0x%x %d", a, b, c, d, result); } while (0)
1097 /* This is not UART related but IUU USB driver related or something */
1098 /* like that. Basically no IUU will accept any commands from the USB */
1099 /* host unless it has received the following message */
1100 /* sprintf(buf ,"%c%c%c%c",0x03,0x02,0x02,0x0); */
1102 SOUP(0x03, 0x02, 0x02, 0x0);
1104 iuu_led(port, 0xF000, 0xF000, 0, 0xFF);
1108 switch (clockmode) {
1109 case 2: /* 3.680 Mhz */
1110 iuu_clk(port, IUU_CLK_3680000 * boost / 100);
1112 iuu_uart_baud(port, 9600 * boost / 100, &actual,
1115 case 3: /* 6.00 Mhz */
1116 iuu_clk(port, IUU_CLK_6000000 * boost / 100);
1118 iuu_uart_baud(port, 16457 * boost / 100, &actual,
1121 default: /* 3.579 Mhz */
1122 iuu_clk(port, IUU_CLK_3579000 * boost / 100);
1124 iuu_uart_baud(port, 9600 * boost / 100, &actual,
1128 /* set the cardin cardout signals */
1135 iuu_cardin = TIOCM_CD;
1140 iuu_cardout = TIOCM_CD;
1143 iuu_cardin = TIOCM_DSR;
1148 iuu_cardout = TIOCM_DSR;
1151 iuu_cardin = TIOCM_CTS;
1156 iuu_cardout = TIOCM_CTS;
1159 iuu_cardin = TIOCM_RNG;
1164 iuu_cardout = TIOCM_RNG;
1167 iuu_uart_flush(port);
1169 dbg("%s - initialization done", __func__);
1171 memset(port->write_urb->transfer_buffer, IUU_UART_RX, 1);
1172 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
1173 usb_sndbulkpipe(port->serial->dev,
1174 port->bulk_out_endpointAddress),
1175 port->write_urb->transfer_buffer, 1,
1176 read_rxcmd_callback, port);
1177 result = usb_submit_urb(port->write_urb, GFP_KERNEL);
1180 dev_err(&port->dev, "%s - failed submitting read urb,"
1181 " error %d\n", __func__, result);
1185 dbg("%s - rxcmd OK", __func__);
1190 static struct usb_serial_driver iuu_device = {
1192 .owner = THIS_MODULE,
1193 .name = "iuu_phoenix",
1195 .id_table = id_table,
1199 .write = iuu_uart_write,
1200 .read_bulk_callback = iuu_uart_read_callback,
1201 .tiocmget = iuu_tiocmget,
1202 .tiocmset = iuu_tiocmset,
1203 .set_termios = iuu_set_termios,
1204 .attach = iuu_startup,
1205 .release = iuu_release,
1208 static int __init iuu_init(void)
1211 retval = usb_serial_register(&iuu_device);
1213 goto failed_usb_serial_register;
1214 retval = usb_register(&iuu_driver);
1216 goto failed_usb_register;
1217 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
1220 failed_usb_register:
1221 usb_serial_deregister(&iuu_device);
1222 failed_usb_serial_register:
1226 static void __exit iuu_exit(void)
1228 usb_deregister(&iuu_driver);
1229 usb_serial_deregister(&iuu_device);
1232 module_init(iuu_init);
1233 module_exit(iuu_exit);
1235 MODULE_AUTHOR("Alain Degreffe eczema@ecze.com");
1237 MODULE_DESCRIPTION(DRIVER_DESC);
1238 MODULE_LICENSE("GPL");
1240 MODULE_VERSION(DRIVER_VERSION);
1241 module_param(debug, bool, S_IRUGO | S_IWUSR);
1242 MODULE_PARM_DESC(debug, "Debug enabled or not");
1244 module_param(xmas, bool, S_IRUGO | S_IWUSR);
1245 MODULE_PARM_DESC(xmas, "xmas color enabled or not");
1247 module_param(boost, int, S_IRUGO | S_IWUSR);
1248 MODULE_PARM_DESC(boost, "overclock boost percent 100 to 500");
1250 module_param(clockmode, int, S_IRUGO | S_IWUSR);
1251 MODULE_PARM_DESC(clockmode, "1=3Mhz579,2=3Mhz680,3=6Mhz");
1253 module_param(cdmode, int, S_IRUGO | S_IWUSR);
1254 MODULE_PARM_DESC(cdmode, "Card detect mode 0=none, 1=CD, 2=!CD, 3=DSR, "
1255 "4=!DSR, 5=CTS, 6=!CTS, 7=RING, 8=!RING");