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.11"
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 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) */
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 priv->vcc = 5; /* 5 V for vcc by default */
119 spin_lock_init(&priv->lock);
120 init_waitqueue_head(&priv->delta_msr_wait);
121 usb_set_serial_port_data(serial->port[0], priv);
125 /* Release function */
126 static void iuu_release(struct usb_serial *serial)
128 struct usb_serial_port *port = serial->port[0];
129 struct iuu_private *priv = usb_get_serial_port_data(port);
137 dbg("%s - I will free all", __func__);
138 usb_set_serial_port_data(port, NULL);
140 dbg("%s - priv is not anymore in port structure", __func__);
143 dbg("%s priv is now kfree", __func__);
147 static int iuu_tiocmset(struct tty_struct *tty, struct file *file,
148 unsigned int set, unsigned int clear)
150 struct usb_serial_port *port = tty->driver_data;
151 struct iuu_private *priv = usb_get_serial_port_data(port);
154 /* FIXME: locking on tiomstatus */
155 dbg("%s (%d) msg : SET = 0x%04x, CLEAR = 0x%04x ", __func__,
156 port->number, set, clear);
158 spin_lock_irqsave(&priv->lock, flags);
160 priv->tiostatus = TIOCM_RTS;
162 if (!(set & TIOCM_RTS) && priv->tiostatus == TIOCM_RTS) {
163 dbg("%s TIOCMSET RESET called !!!", __func__);
166 spin_unlock_irqrestore(&priv->lock, flags);
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, struct file *file)
177 struct usb_serial_port *port = tty->driver_data;
178 struct iuu_private *priv = usb_get_serial_port_data(port);
182 spin_lock_irqsave(&priv->lock, flags);
183 rc = priv->tiostatus;
184 spin_unlock_irqrestore(&priv->lock, flags);
189 static void iuu_rxcmd(struct urb *urb)
191 struct usb_serial_port *port = urb->context;
193 int status = urb->status;
195 dbg("%s - enter", __func__);
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;
218 dbg("%s - enter", __func__);
220 /* Prepare the reset sequence */
222 *buf_ptr++ = IUU_RST_SET;
223 *buf_ptr++ = IUU_DELAY_MS;
225 *buf_ptr = IUU_RST_CLEAR;
227 /* send the sequence */
229 usb_fill_bulk_urb(port->write_urb,
231 usb_sndbulkpipe(port->serial->dev,
232 port->bulk_out_endpointAddress),
233 port->write_urb->transfer_buffer, 4, iuu_rxcmd, port);
234 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
245 static void iuu_update_status_callback(struct urb *urb)
247 struct usb_serial_port *port = urb->context;
248 struct iuu_private *priv = usb_get_serial_port_data(port);
250 int status = urb->status;
252 dbg("%s - enter", __func__);
255 dbg("%s - status = %d", __func__, status);
260 st = urb->transfer_buffer;
261 dbg("%s - enter", __func__);
262 if (urb->actual_length == 1) {
265 priv->tiostatus = iuu_cardout;
268 priv->tiostatus = iuu_cardin;
271 priv->tiostatus = iuu_cardin;
277 static void iuu_status_callback(struct urb *urb)
279 struct usb_serial_port *port = urb->context;
281 int status = urb->status;
283 dbg("%s - status = %d", __func__, status);
284 usb_fill_bulk_urb(port->read_urb, port->serial->dev,
285 usb_rcvbulkpipe(port->serial->dev,
286 port->bulk_in_endpointAddress),
287 port->read_urb->transfer_buffer, 256,
288 iuu_update_status_callback, port);
289 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
292 static int iuu_status(struct usb_serial_port *port)
296 dbg("%s - enter", __func__);
298 memset(port->write_urb->transfer_buffer, IUU_GET_STATE_REGISTER, 1);
299 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
300 usb_sndbulkpipe(port->serial->dev,
301 port->bulk_out_endpointAddress),
302 port->write_urb->transfer_buffer, 1,
303 iuu_status_callback, port);
304 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
309 static int bulk_immediate(struct usb_serial_port *port, u8 *buf, u8 count)
312 struct usb_serial *serial = port->serial;
315 dbg("%s - enter", __func__);
317 /* send the data out the bulk port */
320 usb_bulk_msg(serial->dev,
321 usb_sndbulkpipe(serial->dev,
322 port->bulk_out_endpointAddress), buf,
323 count, &actual, HZ * 1);
325 if (status != IUU_OPERATION_OK)
326 dbg("%s - error = %2x", __func__, status);
328 dbg("%s - write OK !", __func__);
332 static int read_immediate(struct usb_serial_port *port, u8 *buf, u8 count)
335 struct usb_serial *serial = port->serial;
338 dbg("%s - enter", __func__);
340 /* send the data out the bulk port */
343 usb_bulk_msg(serial->dev,
344 usb_rcvbulkpipe(serial->dev,
345 port->bulk_in_endpointAddress), buf,
346 count, &actual, HZ * 1);
348 if (status != IUU_OPERATION_OK)
349 dbg("%s - error = %2x", __func__, status);
351 dbg("%s - read OK !", __func__);
355 static int iuu_led(struct usb_serial_port *port, unsigned int R,
356 unsigned int G, unsigned int B, u8 f)
360 buf = kmalloc(8, GFP_KERNEL);
364 dbg("%s - enter", __func__);
366 buf[0] = IUU_SET_LED;
368 buf[2] = (R >> 8) & 0xFF;
370 buf[4] = (G >> 8) & 0xFF;
372 buf[6] = (B >> 8) & 0xFF;
374 status = bulk_immediate(port, buf, 8);
376 if (status != IUU_OPERATION_OK)
377 dbg("%s - led error status = %2x", __func__, status);
379 dbg("%s - led OK !", __func__);
380 return IUU_OPERATION_OK;
383 static void iuu_rgbf_fill_buffer(u8 *buf, u8 r1, u8 r2, u8 g1, u8 g2, u8 b1,
386 *buf++ = IUU_SET_LED;
396 static void iuu_led_activity_on(struct urb *urb)
398 struct usb_serial_port *port = urb->context;
400 char *buf_ptr = port->write_urb->transfer_buffer;
401 *buf_ptr++ = IUU_SET_LED;
403 get_random_bytes(buf_ptr, 6);
406 iuu_rgbf_fill_buffer(buf_ptr, 255, 255, 0, 0, 0, 0, 255);
409 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
410 usb_sndbulkpipe(port->serial->dev,
411 port->bulk_out_endpointAddress),
412 port->write_urb->transfer_buffer, 8 ,
414 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
417 static void iuu_led_activity_off(struct urb *urb)
419 struct usb_serial_port *port = urb->context;
421 char *buf_ptr = port->write_urb->transfer_buffer;
426 *buf_ptr++ = IUU_SET_LED;
427 iuu_rgbf_fill_buffer(buf_ptr, 0, 0, 255, 255, 0, 0, 255);
429 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
430 usb_sndbulkpipe(port->serial->dev,
431 port->bulk_out_endpointAddress),
432 port->write_urb->transfer_buffer, 8 ,
434 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
439 static int iuu_clk(struct usb_serial_port *port, int dwFrq)
442 struct iuu_private *priv = usb_get_serial_port_data(port);
445 u8 DIV = 0; /* 8bit */
446 u8 XDRV = 0; /* 8bit */
447 u8 PUMP = 0; /* 3bit */
448 u8 PBmsb = 0; /* 2bit */
449 u8 PBlsb = 0; /* 8bit */
450 u8 PO = 0; /* 1bit */
455 int frq = (int)dwFrq;
457 dbg("%s - enter", __func__);
460 priv->buf[Count++] = IUU_UART_WRITE_I2C;
461 priv->buf[Count++] = FrqGenAdr << 1;
462 priv->buf[Count++] = 0x09;
463 priv->buf[Count++] = 0x00;
465 status = bulk_immediate(port, (u8 *) priv->buf, Count);
467 dbg("%s - write error ", __func__);
470 } else if (frq == 3579000) {
475 } else if (frq == 3680000) {
480 } else if (frq == 6000000) {
486 unsigned int result = 0;
487 unsigned int tmp = 0;
492 unsigned int lP = 2055;
493 unsigned int lDiv = 4;
495 for (lQ = 2; lQ <= 47 && !found; lQ++)
496 for (lP = 2055; lP >= 8 && !found; lP--)
497 for (lDiv = 4; lDiv <= 127 && !found; lDiv++) {
498 tmp = (12000000 / lDiv) * (lP / lQ);
499 if (abs((int)(tmp - frq)) <
500 abs((int)(frq - result))) {
501 check2 = (12000000 / lQ);
504 check = (12000000 / lQ) * lP;
505 if (check > 400000000)
507 if (check < 100000000)
509 if (lDiv < 4 || lDiv > 127)
520 P2 = ((P - PO) / 2) - 4;
523 PBmsb = (P2 >> 8 & 0x03);
525 PO = (P >> 10) & 0x01;
528 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
529 priv->buf[Count++] = FrqGenAdr << 1;
530 priv->buf[Count++] = 0x09;
531 priv->buf[Count++] = 0x20; /* Adr = 0x09 */
532 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
533 priv->buf[Count++] = FrqGenAdr << 1;
534 priv->buf[Count++] = 0x0C;
535 priv->buf[Count++] = DIV; /* Adr = 0x0C */
536 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
537 priv->buf[Count++] = FrqGenAdr << 1;
538 priv->buf[Count++] = 0x12;
539 priv->buf[Count++] = XDRV; /* Adr = 0x12 */
540 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
541 priv->buf[Count++] = FrqGenAdr << 1;
542 priv->buf[Count++] = 0x13;
543 priv->buf[Count++] = 0x6B; /* Adr = 0x13 */
544 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
545 priv->buf[Count++] = FrqGenAdr << 1;
546 priv->buf[Count++] = 0x40;
547 priv->buf[Count++] = (0xC0 | ((PUMP & 0x07) << 2)) |
548 (PBmsb & 0x03); /* Adr = 0x40 */
549 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
550 priv->buf[Count++] = FrqGenAdr << 1;
551 priv->buf[Count++] = 0x41;
552 priv->buf[Count++] = PBlsb; /* Adr = 0x41 */
553 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
554 priv->buf[Count++] = FrqGenAdr << 1;
555 priv->buf[Count++] = 0x42;
556 priv->buf[Count++] = Q | (((PO & 0x01) << 7)); /* Adr = 0x42 */
557 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
558 priv->buf[Count++] = FrqGenAdr << 1;
559 priv->buf[Count++] = 0x44;
560 priv->buf[Count++] = (char)0xFF; /* Adr = 0x44 */
561 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
562 priv->buf[Count++] = FrqGenAdr << 1;
563 priv->buf[Count++] = 0x45;
564 priv->buf[Count++] = (char)0xFE; /* Adr = 0x45 */
565 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
566 priv->buf[Count++] = FrqGenAdr << 1;
567 priv->buf[Count++] = 0x46;
568 priv->buf[Count++] = 0x7F; /* Adr = 0x46 */
569 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
570 priv->buf[Count++] = FrqGenAdr << 1;
571 priv->buf[Count++] = 0x47;
572 priv->buf[Count++] = (char)0x84; /* Adr = 0x47 */
574 status = bulk_immediate(port, (u8 *) priv->buf, Count);
575 if (status != IUU_OPERATION_OK)
576 dbg("%s - write error ", __func__);
580 static int iuu_uart_flush(struct usb_serial_port *port)
584 u8 rxcmd = IUU_UART_RX;
585 struct iuu_private *priv = usb_get_serial_port_data(port);
587 dbg("%s - enter", __func__);
589 if (iuu_led(port, 0xF000, 0, 0, 0xFF) < 0)
592 for (i = 0; i < 2; i++) {
593 status = bulk_immediate(port, &rxcmd, 1);
594 if (status != IUU_OPERATION_OK) {
595 dbg("%s - uart_flush_write error", __func__);
599 status = read_immediate(port, &priv->len, 1);
600 if (status != IUU_OPERATION_OK) {
601 dbg("%s - uart_flush_read error", __func__);
606 dbg("%s - uart_flush datalen is : %i ", __func__,
608 status = read_immediate(port, priv->buf, priv->len);
609 if (status != IUU_OPERATION_OK) {
610 dbg("%s - uart_flush_read error", __func__);
615 dbg("%s - uart_flush_read OK!", __func__);
616 iuu_led(port, 0, 0xF000, 0, 0xFF);
620 static void read_buf_callback(struct urb *urb)
622 struct usb_serial_port *port = urb->context;
623 unsigned char *data = urb->transfer_buffer;
624 struct tty_struct *tty;
625 int status = urb->status;
627 dbg("%s - status = %d", __func__, status);
630 if (status == -EPROTO) {
631 /* reschedule needed */
636 dbg("%s - %i chars to write", __func__, urb->actual_length);
637 tty = tty_port_tty_get(&port->port);
639 dbg("%s - data is NULL !!!", __func__);
640 if (tty && urb->actual_length && data) {
641 tty_insert_flip_string(tty, data, urb->actual_length);
642 tty_flip_buffer_push(tty);
645 iuu_led_activity_on(urb);
648 static int iuu_bulk_write(struct usb_serial_port *port)
650 struct iuu_private *priv = usb_get_serial_port_data(port);
655 char *buf_ptr = port->write_urb->transfer_buffer;
656 dbg("%s - enter", __func__);
658 spin_lock_irqsave(&priv->lock, flags);
659 *buf_ptr++ = IUU_UART_ESC;
660 *buf_ptr++ = IUU_UART_TX;
661 *buf_ptr++ = priv->writelen;
663 memcpy(buf_ptr, priv->writebuf, priv->writelen);
664 buf_len = priv->writelen;
666 spin_unlock_irqrestore(&priv->lock, flags);
668 for (i = 0; i < buf_len; i++)
669 sprintf(priv->dbgbuf + i*2 ,
670 "%02X", priv->writebuf[i]);
671 priv->dbgbuf[buf_len+i*2] = 0;
672 dbg("%s - writing %i chars : %s", __func__,
673 buf_len, priv->dbgbuf);
675 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
676 usb_sndbulkpipe(port->serial->dev,
677 port->bulk_out_endpointAddress),
678 port->write_urb->transfer_buffer, buf_len + 3,
680 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
681 usb_serial_port_softint(port);
685 static int iuu_read_buf(struct usb_serial_port *port, int len)
688 dbg("%s - enter", __func__);
690 usb_fill_bulk_urb(port->read_urb, port->serial->dev,
691 usb_rcvbulkpipe(port->serial->dev,
692 port->bulk_in_endpointAddress),
693 port->read_urb->transfer_buffer, len,
694 read_buf_callback, port);
695 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
699 static void iuu_uart_read_callback(struct urb *urb)
701 struct usb_serial_port *port = urb->context;
702 struct iuu_private *priv = usb_get_serial_port_data(port);
704 int status = urb->status;
707 unsigned char *data = urb->transfer_buffer;
710 dbg("%s - enter", __func__);
713 dbg("%s - status = %d", __func__, status);
718 dbg("%s - data is NULL !!!", __func__);
720 if (urb->actual_length == 1 && data != NULL)
723 if (urb->actual_length > 1) {
724 dbg("%s - urb->actual_length = %i", __func__,
729 /* if len > 0 call readbuf */
731 if (len > 0 && error == 0) {
732 dbg("%s - call read buf - len to read is %i ",
734 status = iuu_read_buf(port, len);
737 /* need to update status ? */
738 if (priv->poll > 99) {
739 status = iuu_status(port);
744 /* reset waiting ? */
746 if (priv->reset == 1) {
747 status = iuu_reset(port, 0xC);
750 /* Writebuf is waiting */
751 spin_lock_irqsave(&priv->lock, flags);
752 if (priv->writelen > 0) {
753 spin_unlock_irqrestore(&priv->lock, flags);
754 status = iuu_bulk_write(port);
757 spin_unlock_irqrestore(&priv->lock, flags);
758 /* if nothing to write call again rxcmd */
759 dbg("%s - rxcmd recall", __func__);
760 iuu_led_activity_off(urb);
763 static int iuu_uart_write(struct tty_struct *tty, struct usb_serial_port *port,
764 const u8 *buf, int count)
766 struct iuu_private *priv = usb_get_serial_port_data(port);
768 dbg("%s - enter", __func__);
773 spin_lock_irqsave(&priv->lock, flags);
775 /* fill the buffer */
776 memcpy(priv->writebuf + priv->writelen, buf, count);
777 priv->writelen += count;
778 spin_unlock_irqrestore(&priv->lock, flags);
783 static void read_rxcmd_callback(struct urb *urb)
785 struct usb_serial_port *port = urb->context;
787 int status = urb->status;
789 dbg("%s - status = %d", __func__, status);
796 usb_fill_bulk_urb(port->read_urb, port->serial->dev,
797 usb_rcvbulkpipe(port->serial->dev,
798 port->bulk_in_endpointAddress),
799 port->read_urb->transfer_buffer, 256,
800 iuu_uart_read_callback, port);
801 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
802 dbg("%s - submit result = %d", __func__, result);
806 static int iuu_uart_on(struct usb_serial_port *port)
811 buf = kmalloc(sizeof(u8) * 4, GFP_KERNEL);
816 buf[0] = IUU_UART_ENABLE;
817 buf[1] = (u8) ((IUU_BAUD_9600 >> 8) & 0x00FF);
818 buf[2] = (u8) (0x00FF & IUU_BAUD_9600);
819 buf[3] = (u8) (0x0F0 & IUU_ONE_STOP_BIT) | (0x07 & IUU_PARITY_EVEN);
821 status = bulk_immediate(port, buf, 4);
822 if (status != IUU_OPERATION_OK) {
823 dbg("%s - uart_on error", __func__);
824 goto uart_enable_failed;
826 /* iuu_reset() the card after iuu_uart_on() */
827 status = iuu_uart_flush(port);
828 if (status != IUU_OPERATION_OK)
829 dbg("%s - uart_flush error", __func__);
835 /* Diables the IUU UART (a.k.a. the Phoenix voiderface) */
836 static int iuu_uart_off(struct usb_serial_port *port)
840 buf = kmalloc(1, GFP_KERNEL);
843 buf[0] = IUU_UART_DISABLE;
845 status = bulk_immediate(port, buf, 1);
846 if (status != IUU_OPERATION_OK)
847 dbg("%s - uart_off error", __func__);
853 static int iuu_uart_baud(struct usb_serial_port *port, u32 baud,
854 u32 *actual, u8 parity)
861 unsigned int T1FrekvensHZ = 0;
863 dataout = kmalloc(sizeof(u8) * 5, GFP_KERNEL);
868 if (baud < 1200 || baud > 230400) {
870 return IUU_INVALID_PARAMETER;
874 T1FrekvensHZ = 500000;
879 T1FrekvensHZ = 2000000;
884 T1FrekvensHZ = 6000000;
889 T1FrekvensHZ = 24000000;
892 T1reload = 256 - (u8) (T1FrekvensHZ / (baud * 2));
894 /* magic number here: ENTER_FIRMWARE_UPDATE; */
895 dataout[DataCount++] = IUU_UART_ESC;
896 /* magic number here: CHANGE_BAUD; */
897 dataout[DataCount++] = IUU_UART_CHANGE;
898 dataout[DataCount++] = T1Frekvens;
899 dataout[DataCount++] = T1reload;
901 *actual = (T1FrekvensHZ / (256 - T1reload)) / 2;
903 switch (parity & 0x0F) {
904 case IUU_PARITY_NONE:
905 dataout[DataCount++] = 0x00;
907 case IUU_PARITY_EVEN:
908 dataout[DataCount++] = 0x01;
911 dataout[DataCount++] = 0x02;
913 case IUU_PARITY_MARK:
914 dataout[DataCount++] = 0x03;
916 case IUU_PARITY_SPACE:
917 dataout[DataCount++] = 0x04;
921 return IUU_INVALID_PARAMETER;
925 switch (parity & 0xF0) {
926 case IUU_ONE_STOP_BIT:
927 dataout[DataCount - 1] |= IUU_ONE_STOP_BIT;
930 case IUU_TWO_STOP_BITS:
931 dataout[DataCount - 1] |= IUU_TWO_STOP_BITS;
935 return IUU_INVALID_PARAMETER;
939 status = bulk_immediate(port, dataout, DataCount);
940 if (status != IUU_OPERATION_OK)
941 dbg("%s - uart_off error", __func__);
946 static void iuu_set_termios(struct tty_struct *tty,
947 struct usb_serial_port *port, struct ktermios *old_termios)
949 const u32 supported_mask = CMSPAR|PARENB|PARODD;
951 unsigned int cflag = tty->termios->c_cflag;
956 int baud = 9600; /* Fixed for the moment */
957 u32 newval = cflag & supported_mask;
959 /* compute the parity parameter */
961 if (cflag & CMSPAR) { /* Using mark space */
963 parity |= IUU_PARITY_SPACE;
965 parity |= IUU_PARITY_MARK;
966 } else if (!(cflag & PARENB)) {
967 parity |= IUU_PARITY_NONE;
969 } else if (cflag & PARODD)
970 parity |= IUU_PARITY_ODD;
972 parity |= IUU_PARITY_EVEN;
974 parity |= (cflag & CSTOPB ? IUU_TWO_STOP_BITS : IUU_ONE_STOP_BIT);
977 status = iuu_uart_baud(port,
978 (clockmode == 2) ? 16457 : 9600 * boost / 100,
981 /* set the termios value to the real one, so the user now what has
982 * changed. We support few fields so its easies to copy the old hw
983 * settings back over and then adjust them
986 tty_termios_copy_hw(tty->termios, old_termios);
987 if (status != 0) /* Set failed - return old bits */
989 /* Re-encode speed, parity and csize */
990 tty_encode_baud_rate(tty, baud, baud);
991 tty->termios->c_cflag &= ~(supported_mask|CSIZE);
992 tty->termios->c_cflag |= newval | csize;
995 static void iuu_close(struct usb_serial_port *port)
997 /* iuu_led (port,255,0,0,0); */
998 struct usb_serial *serial;
1000 serial = port->serial;
1004 dbg("%s - port %d", __func__, port->number);
1009 /* shutdown our urbs */
1010 dbg("%s - shutting down urbs", __func__);
1011 usb_kill_urb(port->write_urb);
1012 usb_kill_urb(port->read_urb);
1013 usb_kill_urb(port->interrupt_in_urb);
1014 iuu_led(port, 0, 0, 0xF000, 0xFF);
1018 static void iuu_init_termios(struct tty_struct *tty)
1020 *(tty->termios) = tty_std_termios;
1021 tty->termios->c_cflag = CLOCAL | CREAD | CS8 | B9600
1022 | TIOCM_CTS | CSTOPB | PARENB;
1023 tty->termios->c_ispeed = 9600;
1024 tty->termios->c_ospeed = 9600;
1025 tty->termios->c_lflag = 0;
1026 tty->termios->c_oflag = 0;
1027 tty->termios->c_iflag = 0;
1030 static int iuu_open(struct tty_struct *tty, struct usb_serial_port *port)
1032 struct usb_serial *serial = port->serial;
1036 struct iuu_private *priv = usb_get_serial_port_data(port);
1038 dbg("%s - port %d", __func__, port->number);
1039 usb_clear_halt(serial->dev, port->write_urb->pipe);
1040 usb_clear_halt(serial->dev, port->read_urb->pipe);
1042 buf = kmalloc(10, GFP_KERNEL);
1046 /* fixup the endpoint buffer size */
1047 kfree(port->bulk_out_buffer);
1048 port->bulk_out_buffer = kmalloc(512, GFP_KERNEL);
1049 port->bulk_out_size = 512;
1050 kfree(port->bulk_in_buffer);
1051 port->bulk_in_buffer = kmalloc(512, GFP_KERNEL);
1052 port->bulk_in_size = 512;
1054 if (!port->bulk_out_buffer || !port->bulk_in_buffer) {
1055 kfree(port->bulk_out_buffer);
1056 kfree(port->bulk_in_buffer);
1061 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
1062 usb_sndbulkpipe(port->serial->dev,
1063 port->bulk_out_endpointAddress),
1064 port->bulk_out_buffer, 512,
1068 usb_fill_bulk_urb(port->read_urb, port->serial->dev,
1069 usb_rcvbulkpipe(port->serial->dev,
1070 port->bulk_in_endpointAddress),
1071 port->bulk_in_buffer, 512,
1076 /* initialize writebuf */
1077 #define FISH(a, b, c, d) do { \
1078 result = usb_control_msg(port->serial->dev, \
1079 usb_rcvctrlpipe(port->serial->dev, 0), \
1080 b, a, c, d, buf, 1, 1000); \
1081 dbg("0x%x:0x%x:0x%x:0x%x %d - %x", a, b, c, d, result, \
1082 buf[0]); } while (0);
1084 #define SOUP(a, b, c, d) do { \
1085 result = usb_control_msg(port->serial->dev, \
1086 usb_sndctrlpipe(port->serial->dev, 0), \
1087 b, a, c, d, NULL, 0, 1000); \
1088 dbg("0x%x:0x%x:0x%x:0x%x %d", a, b, c, d, result); } while (0)
1090 /* This is not UART related but IUU USB driver related or something */
1091 /* like that. Basically no IUU will accept any commands from the USB */
1092 /* host unless it has received the following message */
1093 /* sprintf(buf ,"%c%c%c%c",0x03,0x02,0x02,0x0); */
1095 SOUP(0x03, 0x02, 0x02, 0x0);
1097 iuu_led(port, 0xF000, 0xF000, 0, 0xFF);
1101 switch (clockmode) {
1102 case 2: /* 3.680 Mhz */
1103 iuu_clk(port, IUU_CLK_3680000 * boost / 100);
1105 iuu_uart_baud(port, 9600 * boost / 100, &actual,
1108 case 3: /* 6.00 Mhz */
1109 iuu_clk(port, IUU_CLK_6000000 * boost / 100);
1111 iuu_uart_baud(port, 16457 * boost / 100, &actual,
1114 default: /* 3.579 Mhz */
1115 iuu_clk(port, IUU_CLK_3579000 * boost / 100);
1117 iuu_uart_baud(port, 9600 * boost / 100, &actual,
1121 /* set the cardin cardout signals */
1128 iuu_cardin = TIOCM_CD;
1133 iuu_cardout = TIOCM_CD;
1136 iuu_cardin = TIOCM_DSR;
1141 iuu_cardout = TIOCM_DSR;
1144 iuu_cardin = TIOCM_CTS;
1149 iuu_cardout = TIOCM_CTS;
1152 iuu_cardin = TIOCM_RNG;
1157 iuu_cardout = TIOCM_RNG;
1160 iuu_uart_flush(port);
1162 dbg("%s - initialization done", __func__);
1164 memset(port->write_urb->transfer_buffer, IUU_UART_RX, 1);
1165 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
1166 usb_sndbulkpipe(port->serial->dev,
1167 port->bulk_out_endpointAddress),
1168 port->write_urb->transfer_buffer, 1,
1169 read_rxcmd_callback, port);
1170 result = usb_submit_urb(port->write_urb, GFP_KERNEL);
1173 dev_err(&port->dev, "%s - failed submitting read urb,"
1174 " error %d\n", __func__, result);
1178 dbg("%s - rxcmd OK", __func__);
1183 /* how to change VCC */
1184 static int iuu_vcc_set(struct usb_serial_port *port, unsigned int vcc)
1189 buf = kmalloc(5, GFP_KERNEL);
1193 dbg("%s - enter", __func__);
1195 buf[0] = IUU_SET_VCC;
1196 buf[1] = vcc & 0xFF;
1197 buf[2] = (vcc >> 8) & 0xFF;
1198 buf[3] = (vcc >> 16) & 0xFF;
1199 buf[4] = (vcc >> 24) & 0xFF;
1201 status = bulk_immediate(port, buf, 5);
1204 if (status != IUU_OPERATION_OK)
1205 dbg("%s - vcc error status = %2x", __func__, status);
1207 dbg("%s - vcc OK !", __func__);
1216 static ssize_t show_vcc_mode(struct device *dev,
1217 struct device_attribute *attr, char *buf)
1219 struct usb_serial_port *port = to_usb_serial_port(dev);
1220 struct iuu_private *priv = usb_get_serial_port_data(port);
1222 return sprintf(buf, "%d\n", priv->vcc);
1225 static ssize_t store_vcc_mode(struct device *dev,
1226 struct device_attribute *attr, const char *buf, size_t count)
1228 struct usb_serial_port *port = to_usb_serial_port(dev);
1229 struct iuu_private *priv = usb_get_serial_port_data(port);
1232 if (strict_strtoul(buf, 10, &v)) {
1233 dev_err(dev, "%s - vcc_mode: %s is not a unsigned long\n",
1235 goto fail_store_vcc_mode;
1238 dbg("%s: setting vcc_mode = %ld", __func__, v);
1240 if ((v != 3) && (v != 5)) {
1241 dev_err(dev, "%s - vcc_mode %ld is invalid\n", __func__, v);
1243 iuu_vcc_set(port, v);
1246 fail_store_vcc_mode:
1250 static DEVICE_ATTR(vcc_mode, S_IRUSR | S_IWUSR, show_vcc_mode,
1253 static int iuu_create_sysfs_attrs(struct usb_serial_port *port)
1255 dbg("%s", __func__);
1257 return device_create_file(&port->dev, &dev_attr_vcc_mode);
1260 static int iuu_remove_sysfs_attrs(struct usb_serial_port *port)
1262 dbg("%s", __func__);
1264 device_remove_file(&port->dev, &dev_attr_vcc_mode);
1269 * End Sysfs Attributes
1272 static struct usb_serial_driver iuu_device = {
1274 .owner = THIS_MODULE,
1275 .name = "iuu_phoenix",
1277 .id_table = id_table,
1279 .port_probe = iuu_create_sysfs_attrs,
1280 .port_remove = iuu_remove_sysfs_attrs,
1283 .write = iuu_uart_write,
1284 .read_bulk_callback = iuu_uart_read_callback,
1285 .tiocmget = iuu_tiocmget,
1286 .tiocmset = iuu_tiocmset,
1287 .set_termios = iuu_set_termios,
1288 .init_termios = iuu_init_termios,
1289 .attach = iuu_startup,
1290 .release = iuu_release,
1293 static int __init iuu_init(void)
1296 retval = usb_serial_register(&iuu_device);
1298 goto failed_usb_serial_register;
1299 retval = usb_register(&iuu_driver);
1301 goto failed_usb_register;
1302 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
1305 failed_usb_register:
1306 usb_serial_deregister(&iuu_device);
1307 failed_usb_serial_register:
1311 static void __exit iuu_exit(void)
1313 usb_deregister(&iuu_driver);
1314 usb_serial_deregister(&iuu_device);
1317 module_init(iuu_init);
1318 module_exit(iuu_exit);
1320 MODULE_AUTHOR("Alain Degreffe eczema@ecze.com");
1322 MODULE_DESCRIPTION(DRIVER_DESC);
1323 MODULE_LICENSE("GPL");
1325 MODULE_VERSION(DRIVER_VERSION);
1326 module_param(debug, bool, S_IRUGO | S_IWUSR);
1327 MODULE_PARM_DESC(debug, "Debug enabled or not");
1329 module_param(xmas, bool, S_IRUGO | S_IWUSR);
1330 MODULE_PARM_DESC(xmas, "Xmas colors enabled or not");
1332 module_param(boost, int, S_IRUGO | S_IWUSR);
1333 MODULE_PARM_DESC(boost, "Card overclock boost (in percent 100-500)");
1335 module_param(clockmode, int, S_IRUGO | S_IWUSR);
1336 MODULE_PARM_DESC(clockmode, "Card clock mode (1=3.579 MHz, 2=3.680 MHz, "
1339 module_param(cdmode, int, S_IRUGO | S_IWUSR);
1340 MODULE_PARM_DESC(cdmode, "Card detect mode (0=none, 1=CD, 2=!CD, 3=DSR, "
1341 "4=!DSR, 5=CTS, 6=!CTS, 7=RING, 8=!RING)");