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/slab.h>
21 #include <linux/tty.h>
22 #include <linux/tty_driver.h>
23 #include <linux/tty_flip.h>
24 #include <linux/serial.h>
25 #include <linux/module.h>
26 #include <linux/moduleparam.h>
27 #include <linux/spinlock.h>
28 #include <linux/uaccess.h>
29 #include <linux/usb.h>
30 #include <linux/usb/serial.h>
31 #include "iuu_phoenix.h"
32 #include <linux/random.h>
34 #define DRIVER_DESC "Infinity USB Unlimited Phoenix driver"
36 static const struct usb_device_id id_table[] = {
37 {USB_DEVICE(IUU_USB_VENDOR_ID, IUU_USB_PRODUCT_ID)},
38 {} /* Terminating entry */
40 MODULE_DEVICE_TABLE(usb, id_table);
43 static int boost = 100;
44 static int clockmode = 1;
45 static int cdmode = 1;
46 static int iuu_cardin;
47 static int iuu_cardout;
49 static int vcc_default = 5;
51 static int iuu_create_sysfs_attrs(struct usb_serial_port *port);
52 static int iuu_remove_sysfs_attrs(struct usb_serial_port *port);
53 static void read_rxcmd_callback(struct urb *urb);
56 spinlock_t lock; /* store irq state */
58 int tiostatus; /* store IUART SIGNAL for tiocmget call */
59 u8 reset; /* if 1 reset is needed */
60 int poll; /* number of poll */
61 u8 *writebuf; /* buffer for writing to device */
62 int writelen; /* num of byte to write to device */
63 u8 *buf; /* used for initialize speed */
65 int vcc; /* vcc (either 3 or 5 V) */
71 static int iuu_attach(struct usb_serial *serial)
73 unsigned char num_ports = serial->num_ports;
75 if (serial->num_bulk_in < num_ports || serial->num_bulk_out < num_ports)
81 static int iuu_port_probe(struct usb_serial_port *port)
83 struct iuu_private *priv;
86 priv = kzalloc(sizeof(struct iuu_private), GFP_KERNEL);
90 priv->buf = kzalloc(256, GFP_KERNEL);
96 priv->writebuf = kzalloc(256, GFP_KERNEL);
97 if (!priv->writebuf) {
103 priv->vcc = vcc_default;
104 spin_lock_init(&priv->lock);
106 usb_set_serial_port_data(port, priv);
108 ret = iuu_create_sysfs_attrs(port);
110 kfree(priv->writebuf);
119 static int iuu_port_remove(struct usb_serial_port *port)
121 struct iuu_private *priv = usb_get_serial_port_data(port);
123 iuu_remove_sysfs_attrs(port);
124 kfree(priv->writebuf);
131 static int iuu_tiocmset(struct tty_struct *tty,
132 unsigned int set, unsigned int clear)
134 struct usb_serial_port *port = tty->driver_data;
135 struct iuu_private *priv = usb_get_serial_port_data(port);
138 /* FIXME: locking on tiomstatus */
139 dev_dbg(&port->dev, "%s msg : SET = 0x%04x, CLEAR = 0x%04x\n",
140 __func__, set, clear);
142 spin_lock_irqsave(&priv->lock, flags);
144 if ((set & TIOCM_RTS) && !(priv->tiostatus == TIOCM_RTS)) {
145 dev_dbg(&port->dev, "%s TIOCMSET RESET called !!!\n", __func__);
149 priv->tiostatus = TIOCM_RTS;
151 spin_unlock_irqrestore(&priv->lock, flags);
155 /* This is used to provide a carrier detect mechanism
156 * When a card is present, the response is 0x00
157 * When no card , the reader respond with TIOCM_CD
158 * This is known as CD autodetect mechanism
160 static int iuu_tiocmget(struct tty_struct *tty)
162 struct usb_serial_port *port = tty->driver_data;
163 struct iuu_private *priv = usb_get_serial_port_data(port);
167 spin_lock_irqsave(&priv->lock, flags);
168 rc = priv->tiostatus;
169 spin_unlock_irqrestore(&priv->lock, flags);
174 static void iuu_rxcmd(struct urb *urb)
176 struct usb_serial_port *port = urb->context;
178 int status = urb->status;
181 dev_dbg(&port->dev, "%s - status = %d\n", __func__, status);
187 memset(port->write_urb->transfer_buffer, IUU_UART_RX, 1);
188 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
189 usb_sndbulkpipe(port->serial->dev,
190 port->bulk_out_endpointAddress),
191 port->write_urb->transfer_buffer, 1,
192 read_rxcmd_callback, port);
193 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
196 static int iuu_reset(struct usb_serial_port *port, u8 wt)
198 struct iuu_private *priv = usb_get_serial_port_data(port);
200 char *buf_ptr = port->write_urb->transfer_buffer;
202 /* Prepare the reset sequence */
204 *buf_ptr++ = IUU_RST_SET;
205 *buf_ptr++ = IUU_DELAY_MS;
207 *buf_ptr = IUU_RST_CLEAR;
209 /* send the sequence */
211 usb_fill_bulk_urb(port->write_urb,
213 usb_sndbulkpipe(port->serial->dev,
214 port->bulk_out_endpointAddress),
215 port->write_urb->transfer_buffer, 4, iuu_rxcmd, port);
216 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
227 static void iuu_update_status_callback(struct urb *urb)
229 struct usb_serial_port *port = urb->context;
230 struct iuu_private *priv = usb_get_serial_port_data(port);
232 int status = urb->status;
235 dev_dbg(&port->dev, "%s - status = %d\n", __func__, status);
240 st = urb->transfer_buffer;
241 dev_dbg(&port->dev, "%s - enter\n", __func__);
242 if (urb->actual_length == 1) {
245 priv->tiostatus = iuu_cardout;
248 priv->tiostatus = iuu_cardin;
251 priv->tiostatus = iuu_cardin;
257 static void iuu_status_callback(struct urb *urb)
259 struct usb_serial_port *port = urb->context;
261 int status = urb->status;
263 dev_dbg(&port->dev, "%s - status = %d\n", __func__, status);
264 usb_fill_bulk_urb(port->read_urb, port->serial->dev,
265 usb_rcvbulkpipe(port->serial->dev,
266 port->bulk_in_endpointAddress),
267 port->read_urb->transfer_buffer, 256,
268 iuu_update_status_callback, port);
269 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
272 static int iuu_status(struct usb_serial_port *port)
276 memset(port->write_urb->transfer_buffer, IUU_GET_STATE_REGISTER, 1);
277 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
278 usb_sndbulkpipe(port->serial->dev,
279 port->bulk_out_endpointAddress),
280 port->write_urb->transfer_buffer, 1,
281 iuu_status_callback, port);
282 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
287 static int bulk_immediate(struct usb_serial_port *port, u8 *buf, u8 count)
290 struct usb_serial *serial = port->serial;
293 /* send the data out the bulk port */
296 usb_bulk_msg(serial->dev,
297 usb_sndbulkpipe(serial->dev,
298 port->bulk_out_endpointAddress), buf,
299 count, &actual, 1000);
301 if (status != IUU_OPERATION_OK)
302 dev_dbg(&port->dev, "%s - error = %2x\n", __func__, status);
304 dev_dbg(&port->dev, "%s - write OK !\n", __func__);
308 static int read_immediate(struct usb_serial_port *port, u8 *buf, u8 count)
311 struct usb_serial *serial = port->serial;
314 /* send the data out the bulk port */
316 usb_bulk_msg(serial->dev,
317 usb_rcvbulkpipe(serial->dev,
318 port->bulk_in_endpointAddress), buf,
319 count, &actual, 1000);
321 if (status != IUU_OPERATION_OK)
322 dev_dbg(&port->dev, "%s - error = %2x\n", __func__, status);
324 dev_dbg(&port->dev, "%s - read OK !\n", __func__);
328 static int iuu_led(struct usb_serial_port *port, unsigned int R,
329 unsigned int G, unsigned int B, u8 f)
333 buf = kmalloc(8, GFP_KERNEL);
337 buf[0] = IUU_SET_LED;
339 buf[2] = (R >> 8) & 0xFF;
341 buf[4] = (G >> 8) & 0xFF;
343 buf[6] = (B >> 8) & 0xFF;
345 status = bulk_immediate(port, buf, 8);
347 if (status != IUU_OPERATION_OK)
348 dev_dbg(&port->dev, "%s - led error status = %2x\n", __func__, status);
350 dev_dbg(&port->dev, "%s - led OK !\n", __func__);
351 return IUU_OPERATION_OK;
354 static void iuu_rgbf_fill_buffer(u8 *buf, u8 r1, u8 r2, u8 g1, u8 g2, u8 b1,
357 *buf++ = IUU_SET_LED;
367 static void iuu_led_activity_on(struct urb *urb)
369 struct usb_serial_port *port = urb->context;
371 char *buf_ptr = port->write_urb->transfer_buffer;
374 buf_ptr[0] = IUU_SET_LED;
375 get_random_bytes(buf_ptr + 1, 6);
378 iuu_rgbf_fill_buffer(buf_ptr, 255, 255, 0, 0, 0, 0, 255);
381 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
382 usb_sndbulkpipe(port->serial->dev,
383 port->bulk_out_endpointAddress),
384 port->write_urb->transfer_buffer, 8 ,
386 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
389 static void iuu_led_activity_off(struct urb *urb)
391 struct usb_serial_port *port = urb->context;
393 char *buf_ptr = port->write_urb->transfer_buffer;
400 iuu_rgbf_fill_buffer(buf_ptr, 0, 0, 255, 255, 0, 0, 255);
402 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
403 usb_sndbulkpipe(port->serial->dev,
404 port->bulk_out_endpointAddress),
405 port->write_urb->transfer_buffer, 8 ,
407 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
412 static int iuu_clk(struct usb_serial_port *port, int dwFrq)
415 struct iuu_private *priv = usb_get_serial_port_data(port);
418 u8 DIV = 0; /* 8bit */
419 u8 XDRV = 0; /* 8bit */
420 u8 PUMP = 0; /* 3bit */
421 u8 PBmsb = 0; /* 2bit */
422 u8 PBlsb = 0; /* 8bit */
423 u8 PO = 0; /* 1bit */
428 int frq = (int)dwFrq;
431 priv->buf[Count++] = IUU_UART_WRITE_I2C;
432 priv->buf[Count++] = FrqGenAdr << 1;
433 priv->buf[Count++] = 0x09;
434 priv->buf[Count++] = 0x00;
436 status = bulk_immediate(port, (u8 *) priv->buf, Count);
438 dev_dbg(&port->dev, "%s - write error\n", __func__);
441 } else if (frq == 3579000) {
446 } else if (frq == 3680000) {
451 } else if (frq == 6000000) {
457 unsigned int result = 0;
458 unsigned int tmp = 0;
463 unsigned int lP = 2055;
464 unsigned int lDiv = 4;
466 for (lQ = 2; lQ <= 47 && !found; lQ++)
467 for (lP = 2055; lP >= 8 && !found; lP--)
468 for (lDiv = 4; lDiv <= 127 && !found; lDiv++) {
469 tmp = (12000000 / lDiv) * (lP / lQ);
470 if (abs((int)(tmp - frq)) <
471 abs((int)(frq - result))) {
472 check2 = (12000000 / lQ);
475 check = (12000000 / lQ) * lP;
476 if (check > 400000000)
478 if (check < 100000000)
480 if (lDiv < 4 || lDiv > 127)
491 P2 = ((P - PO) / 2) - 4;
494 PBmsb = (P2 >> 8 & 0x03);
496 PO = (P >> 10) & 0x01;
499 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
500 priv->buf[Count++] = FrqGenAdr << 1;
501 priv->buf[Count++] = 0x09;
502 priv->buf[Count++] = 0x20; /* Adr = 0x09 */
503 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
504 priv->buf[Count++] = FrqGenAdr << 1;
505 priv->buf[Count++] = 0x0C;
506 priv->buf[Count++] = DIV; /* Adr = 0x0C */
507 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
508 priv->buf[Count++] = FrqGenAdr << 1;
509 priv->buf[Count++] = 0x12;
510 priv->buf[Count++] = XDRV; /* Adr = 0x12 */
511 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
512 priv->buf[Count++] = FrqGenAdr << 1;
513 priv->buf[Count++] = 0x13;
514 priv->buf[Count++] = 0x6B; /* Adr = 0x13 */
515 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
516 priv->buf[Count++] = FrqGenAdr << 1;
517 priv->buf[Count++] = 0x40;
518 priv->buf[Count++] = (0xC0 | ((PUMP & 0x07) << 2)) |
519 (PBmsb & 0x03); /* Adr = 0x40 */
520 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
521 priv->buf[Count++] = FrqGenAdr << 1;
522 priv->buf[Count++] = 0x41;
523 priv->buf[Count++] = PBlsb; /* Adr = 0x41 */
524 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
525 priv->buf[Count++] = FrqGenAdr << 1;
526 priv->buf[Count++] = 0x42;
527 priv->buf[Count++] = Q | (((PO & 0x01) << 7)); /* Adr = 0x42 */
528 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
529 priv->buf[Count++] = FrqGenAdr << 1;
530 priv->buf[Count++] = 0x44;
531 priv->buf[Count++] = (char)0xFF; /* Adr = 0x44 */
532 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
533 priv->buf[Count++] = FrqGenAdr << 1;
534 priv->buf[Count++] = 0x45;
535 priv->buf[Count++] = (char)0xFE; /* Adr = 0x45 */
536 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
537 priv->buf[Count++] = FrqGenAdr << 1;
538 priv->buf[Count++] = 0x46;
539 priv->buf[Count++] = 0x7F; /* Adr = 0x46 */
540 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
541 priv->buf[Count++] = FrqGenAdr << 1;
542 priv->buf[Count++] = 0x47;
543 priv->buf[Count++] = (char)0x84; /* Adr = 0x47 */
545 status = bulk_immediate(port, (u8 *) priv->buf, Count);
546 if (status != IUU_OPERATION_OK)
547 dev_dbg(&port->dev, "%s - write error\n", __func__);
551 static int iuu_uart_flush(struct usb_serial_port *port)
553 struct device *dev = &port->dev;
557 struct iuu_private *priv = usb_get_serial_port_data(port);
559 if (iuu_led(port, 0xF000, 0, 0, 0xFF) < 0)
562 rxcmd = kmalloc(1, GFP_KERNEL);
566 rxcmd[0] = IUU_UART_RX;
568 for (i = 0; i < 2; i++) {
569 status = bulk_immediate(port, rxcmd, 1);
570 if (status != IUU_OPERATION_OK) {
571 dev_dbg(dev, "%s - uart_flush_write error\n", __func__);
575 status = read_immediate(port, &priv->len, 1);
576 if (status != IUU_OPERATION_OK) {
577 dev_dbg(dev, "%s - uart_flush_read error\n", __func__);
582 dev_dbg(dev, "%s - uart_flush datalen is : %i\n", __func__, priv->len);
583 status = read_immediate(port, priv->buf, priv->len);
584 if (status != IUU_OPERATION_OK) {
585 dev_dbg(dev, "%s - uart_flush_read error\n", __func__);
590 dev_dbg(dev, "%s - uart_flush_read OK!\n", __func__);
591 iuu_led(port, 0, 0xF000, 0, 0xFF);
599 static void read_buf_callback(struct urb *urb)
601 struct usb_serial_port *port = urb->context;
602 unsigned char *data = urb->transfer_buffer;
603 int status = urb->status;
606 if (status == -EPROTO) {
607 /* reschedule needed */
612 dev_dbg(&port->dev, "%s - %i chars to write\n", __func__, urb->actual_length);
614 dev_dbg(&port->dev, "%s - data is NULL !!!\n", __func__);
615 if (urb->actual_length && data) {
616 tty_insert_flip_string(&port->port, data, urb->actual_length);
617 tty_flip_buffer_push(&port->port);
619 iuu_led_activity_on(urb);
622 static int iuu_bulk_write(struct usb_serial_port *port)
624 struct iuu_private *priv = usb_get_serial_port_data(port);
628 char *buf_ptr = port->write_urb->transfer_buffer;
630 spin_lock_irqsave(&priv->lock, flags);
631 *buf_ptr++ = IUU_UART_ESC;
632 *buf_ptr++ = IUU_UART_TX;
633 *buf_ptr++ = priv->writelen;
635 memcpy(buf_ptr, priv->writebuf, priv->writelen);
636 buf_len = priv->writelen;
638 spin_unlock_irqrestore(&priv->lock, flags);
639 dev_dbg(&port->dev, "%s - writing %i chars : %*ph\n", __func__,
640 buf_len, buf_len, buf_ptr);
641 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
642 usb_sndbulkpipe(port->serial->dev,
643 port->bulk_out_endpointAddress),
644 port->write_urb->transfer_buffer, buf_len + 3,
646 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
647 usb_serial_port_softint(port);
651 static int iuu_read_buf(struct usb_serial_port *port, int len)
655 usb_fill_bulk_urb(port->read_urb, port->serial->dev,
656 usb_rcvbulkpipe(port->serial->dev,
657 port->bulk_in_endpointAddress),
658 port->read_urb->transfer_buffer, len,
659 read_buf_callback, port);
660 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
664 static void iuu_uart_read_callback(struct urb *urb)
666 struct usb_serial_port *port = urb->context;
667 struct iuu_private *priv = usb_get_serial_port_data(port);
669 int status = urb->status;
672 unsigned char *data = urb->transfer_buffer;
676 dev_dbg(&port->dev, "%s - status = %d\n", __func__, status);
681 dev_dbg(&port->dev, "%s - data is NULL !!!\n", __func__);
683 if (urb->actual_length == 1 && data != NULL)
686 if (urb->actual_length > 1) {
687 dev_dbg(&port->dev, "%s - urb->actual_length = %i\n", __func__,
692 /* if len > 0 call readbuf */
694 if (len > 0 && error == 0) {
695 dev_dbg(&port->dev, "%s - call read buf - len to read is %i\n",
697 status = iuu_read_buf(port, len);
700 /* need to update status ? */
701 if (priv->poll > 99) {
702 status = iuu_status(port);
707 /* reset waiting ? */
709 if (priv->reset == 1) {
710 status = iuu_reset(port, 0xC);
713 /* Writebuf is waiting */
714 spin_lock_irqsave(&priv->lock, flags);
715 if (priv->writelen > 0) {
716 spin_unlock_irqrestore(&priv->lock, flags);
717 status = iuu_bulk_write(port);
720 spin_unlock_irqrestore(&priv->lock, flags);
721 /* if nothing to write call again rxcmd */
722 dev_dbg(&port->dev, "%s - rxcmd recall\n", __func__);
723 iuu_led_activity_off(urb);
726 static int iuu_uart_write(struct tty_struct *tty, struct usb_serial_port *port,
727 const u8 *buf, int count)
729 struct iuu_private *priv = usb_get_serial_port_data(port);
732 spin_lock_irqsave(&priv->lock, flags);
734 count = min(count, 256 - priv->writelen);
738 /* fill the buffer */
739 memcpy(priv->writebuf + priv->writelen, buf, count);
740 priv->writelen += count;
742 spin_unlock_irqrestore(&priv->lock, flags);
747 static void read_rxcmd_callback(struct urb *urb)
749 struct usb_serial_port *port = urb->context;
751 int status = urb->status;
758 usb_fill_bulk_urb(port->read_urb, port->serial->dev,
759 usb_rcvbulkpipe(port->serial->dev,
760 port->bulk_in_endpointAddress),
761 port->read_urb->transfer_buffer, 256,
762 iuu_uart_read_callback, port);
763 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
764 dev_dbg(&port->dev, "%s - submit result = %d\n", __func__, result);
767 static int iuu_uart_on(struct usb_serial_port *port)
772 buf = kmalloc(sizeof(u8) * 4, GFP_KERNEL);
777 buf[0] = IUU_UART_ENABLE;
778 buf[1] = (u8) ((IUU_BAUD_9600 >> 8) & 0x00FF);
779 buf[2] = (u8) (0x00FF & IUU_BAUD_9600);
780 buf[3] = (u8) (0x0F0 & IUU_ONE_STOP_BIT) | (0x07 & IUU_PARITY_EVEN);
782 status = bulk_immediate(port, buf, 4);
783 if (status != IUU_OPERATION_OK) {
784 dev_dbg(&port->dev, "%s - uart_on error\n", __func__);
785 goto uart_enable_failed;
787 /* iuu_reset() the card after iuu_uart_on() */
788 status = iuu_uart_flush(port);
789 if (status != IUU_OPERATION_OK)
790 dev_dbg(&port->dev, "%s - uart_flush error\n", __func__);
796 /* Disables the IUU UART (a.k.a. the Phoenix voiderface) */
797 static int iuu_uart_off(struct usb_serial_port *port)
801 buf = kmalloc(1, GFP_KERNEL);
804 buf[0] = IUU_UART_DISABLE;
806 status = bulk_immediate(port, buf, 1);
807 if (status != IUU_OPERATION_OK)
808 dev_dbg(&port->dev, "%s - uart_off error\n", __func__);
814 static int iuu_uart_baud(struct usb_serial_port *port, u32 baud_base,
815 u32 *actual, u8 parity)
823 unsigned int T1FrekvensHZ = 0;
825 dev_dbg(&port->dev, "%s - enter baud_base=%d\n", __func__, baud_base);
826 dataout = kmalloc(sizeof(u8) * 5, GFP_KERNEL);
830 /*baud = (((priv->clk / 35) * baud_base) / 100000); */
833 if (baud < 1200 || baud > 230400) {
835 return IUU_INVALID_PARAMETER;
839 T1FrekvensHZ = 500000;
844 T1FrekvensHZ = 2000000;
849 T1FrekvensHZ = 6000000;
854 T1FrekvensHZ = 24000000;
857 T1reload = 256 - (u8) (T1FrekvensHZ / (baud * 2));
859 /* magic number here: ENTER_FIRMWARE_UPDATE; */
860 dataout[DataCount++] = IUU_UART_ESC;
861 /* magic number here: CHANGE_BAUD; */
862 dataout[DataCount++] = IUU_UART_CHANGE;
863 dataout[DataCount++] = T1Frekvens;
864 dataout[DataCount++] = T1reload;
866 *actual = (T1FrekvensHZ / (256 - T1reload)) / 2;
868 switch (parity & 0x0F) {
869 case IUU_PARITY_NONE:
870 dataout[DataCount++] = 0x00;
872 case IUU_PARITY_EVEN:
873 dataout[DataCount++] = 0x01;
876 dataout[DataCount++] = 0x02;
878 case IUU_PARITY_MARK:
879 dataout[DataCount++] = 0x03;
881 case IUU_PARITY_SPACE:
882 dataout[DataCount++] = 0x04;
886 return IUU_INVALID_PARAMETER;
890 switch (parity & 0xF0) {
891 case IUU_ONE_STOP_BIT:
892 dataout[DataCount - 1] |= IUU_ONE_STOP_BIT;
895 case IUU_TWO_STOP_BITS:
896 dataout[DataCount - 1] |= IUU_TWO_STOP_BITS;
900 return IUU_INVALID_PARAMETER;
904 status = bulk_immediate(port, dataout, DataCount);
905 if (status != IUU_OPERATION_OK)
906 dev_dbg(&port->dev, "%s - uart_off error\n", __func__);
911 static void iuu_set_termios(struct tty_struct *tty,
912 struct usb_serial_port *port, struct ktermios *old_termios)
914 const u32 supported_mask = CMSPAR|PARENB|PARODD;
915 struct iuu_private *priv = usb_get_serial_port_data(port);
916 unsigned int cflag = tty->termios.c_cflag;
922 u32 newval = cflag & supported_mask;
924 /* Just use the ospeed. ispeed should be the same. */
925 baud = tty->termios.c_ospeed;
927 dev_dbg(&port->dev, "%s - enter c_ospeed or baud=%d\n", __func__, baud);
929 /* compute the parity parameter */
931 if (cflag & CMSPAR) { /* Using mark space */
933 parity |= IUU_PARITY_SPACE;
935 parity |= IUU_PARITY_MARK;
936 } else if (!(cflag & PARENB)) {
937 parity |= IUU_PARITY_NONE;
939 } else if (cflag & PARODD)
940 parity |= IUU_PARITY_ODD;
942 parity |= IUU_PARITY_EVEN;
944 parity |= (cflag & CSTOPB ? IUU_TWO_STOP_BITS : IUU_ONE_STOP_BIT);
947 status = iuu_uart_baud(port,
948 baud * priv->boost / 100,
951 /* set the termios value to the real one, so the user now what has
952 * changed. We support few fields so its easies to copy the old hw
953 * settings back over and then adjust them
956 tty_termios_copy_hw(&tty->termios, old_termios);
957 if (status != 0) /* Set failed - return old bits */
959 /* Re-encode speed, parity and csize */
960 tty_encode_baud_rate(tty, baud, baud);
961 tty->termios.c_cflag &= ~(supported_mask|CSIZE);
962 tty->termios.c_cflag |= newval | csize;
965 static void iuu_close(struct usb_serial_port *port)
967 /* iuu_led (port,255,0,0,0); */
971 usb_kill_urb(port->write_urb);
972 usb_kill_urb(port->read_urb);
974 iuu_led(port, 0, 0, 0xF000, 0xFF);
977 static void iuu_init_termios(struct tty_struct *tty)
979 tty->termios = tty_std_termios;
980 tty->termios.c_cflag = CLOCAL | CREAD | CS8 | B9600
981 | TIOCM_CTS | CSTOPB | PARENB;
982 tty->termios.c_ispeed = 9600;
983 tty->termios.c_ospeed = 9600;
984 tty->termios.c_lflag = 0;
985 tty->termios.c_oflag = 0;
986 tty->termios.c_iflag = 0;
989 static int iuu_open(struct tty_struct *tty, struct usb_serial_port *port)
991 struct usb_serial *serial = port->serial;
992 struct device *dev = &port->dev;
997 struct iuu_private *priv = usb_get_serial_port_data(port);
999 baud = tty->termios.c_ospeed;
1000 tty->termios.c_ispeed = baud;
1001 /* Re-encode speed */
1002 tty_encode_baud_rate(tty, baud, baud);
1004 dev_dbg(dev, "%s - baud %d\n", __func__, baud);
1005 usb_clear_halt(serial->dev, port->write_urb->pipe);
1006 usb_clear_halt(serial->dev, port->read_urb->pipe);
1008 buf = kmalloc(10, GFP_KERNEL);
1014 /* initialize writebuf */
1015 #define FISH(a, b, c, d) do { \
1016 result = usb_control_msg(port->serial->dev, \
1017 usb_rcvctrlpipe(port->serial->dev, 0), \
1018 b, a, c, d, buf, 1, 1000); \
1019 dev_dbg(dev, "0x%x:0x%x:0x%x:0x%x %d - %x\n", a, b, c, d, result, \
1020 buf[0]); } while (0);
1022 #define SOUP(a, b, c, d) do { \
1023 result = usb_control_msg(port->serial->dev, \
1024 usb_sndctrlpipe(port->serial->dev, 0), \
1025 b, a, c, d, NULL, 0, 1000); \
1026 dev_dbg(dev, "0x%x:0x%x:0x%x:0x%x %d\n", a, b, c, d, result); } while (0)
1028 /* This is not UART related but IUU USB driver related or something */
1029 /* like that. Basically no IUU will accept any commands from the USB */
1030 /* host unless it has received the following message */
1031 /* sprintf(buf ,"%c%c%c%c",0x03,0x02,0x02,0x0); */
1033 SOUP(0x03, 0x02, 0x02, 0x0);
1035 iuu_led(port, 0xF000, 0xF000, 0, 0xFF);
1039 priv->boost = boost;
1041 switch (clockmode) {
1042 case 2: /* 3.680 Mhz */
1043 priv->clk = IUU_CLK_3680000;
1044 iuu_clk(port, IUU_CLK_3680000 * boost / 100);
1046 iuu_uart_baud(port, baud * boost / 100, &actual,
1049 case 3: /* 6.00 Mhz */
1050 iuu_clk(port, IUU_CLK_6000000 * boost / 100);
1051 priv->clk = IUU_CLK_6000000;
1052 /* Ratio of 6000000 to 3500000 for baud 9600 */
1054 iuu_uart_baud(port, 16457 * boost / 100, &actual,
1057 default: /* 3.579 Mhz */
1058 iuu_clk(port, IUU_CLK_3579000 * boost / 100);
1059 priv->clk = IUU_CLK_3579000;
1061 iuu_uart_baud(port, baud * boost / 100, &actual,
1065 /* set the cardin cardout signals */
1072 iuu_cardin = TIOCM_CD;
1077 iuu_cardout = TIOCM_CD;
1080 iuu_cardin = TIOCM_DSR;
1085 iuu_cardout = TIOCM_DSR;
1088 iuu_cardin = TIOCM_CTS;
1093 iuu_cardout = TIOCM_CTS;
1096 iuu_cardin = TIOCM_RNG;
1101 iuu_cardout = TIOCM_RNG;
1104 iuu_uart_flush(port);
1106 dev_dbg(dev, "%s - initialization done\n", __func__);
1108 memset(port->write_urb->transfer_buffer, IUU_UART_RX, 1);
1109 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
1110 usb_sndbulkpipe(port->serial->dev,
1111 port->bulk_out_endpointAddress),
1112 port->write_urb->transfer_buffer, 1,
1113 read_rxcmd_callback, port);
1114 result = usb_submit_urb(port->write_urb, GFP_KERNEL);
1116 dev_err(dev, "%s - failed submitting read urb, error %d\n", __func__, result);
1119 dev_dbg(dev, "%s - rxcmd OK\n", __func__);
1125 /* how to change VCC */
1126 static int iuu_vcc_set(struct usb_serial_port *port, unsigned int vcc)
1131 buf = kmalloc(5, GFP_KERNEL);
1135 buf[0] = IUU_SET_VCC;
1136 buf[1] = vcc & 0xFF;
1137 buf[2] = (vcc >> 8) & 0xFF;
1138 buf[3] = (vcc >> 16) & 0xFF;
1139 buf[4] = (vcc >> 24) & 0xFF;
1141 status = bulk_immediate(port, buf, 5);
1144 if (status != IUU_OPERATION_OK)
1145 dev_dbg(&port->dev, "%s - vcc error status = %2x\n", __func__, status);
1147 dev_dbg(&port->dev, "%s - vcc OK !\n", __func__);
1156 static ssize_t vcc_mode_show(struct device *dev,
1157 struct device_attribute *attr, char *buf)
1159 struct usb_serial_port *port = to_usb_serial_port(dev);
1160 struct iuu_private *priv = usb_get_serial_port_data(port);
1162 return sprintf(buf, "%d\n", priv->vcc);
1165 static ssize_t vcc_mode_store(struct device *dev,
1166 struct device_attribute *attr, const char *buf, size_t count)
1168 struct usb_serial_port *port = to_usb_serial_port(dev);
1169 struct iuu_private *priv = usb_get_serial_port_data(port);
1172 if (kstrtoul(buf, 10, &v)) {
1173 dev_err(dev, "%s - vcc_mode: %s is not a unsigned long\n",
1175 goto fail_store_vcc_mode;
1178 dev_dbg(dev, "%s: setting vcc_mode = %ld\n", __func__, v);
1180 if ((v != 3) && (v != 5)) {
1181 dev_err(dev, "%s - vcc_mode %ld is invalid\n", __func__, v);
1183 iuu_vcc_set(port, v);
1186 fail_store_vcc_mode:
1189 static DEVICE_ATTR_RW(vcc_mode);
1191 static int iuu_create_sysfs_attrs(struct usb_serial_port *port)
1193 return device_create_file(&port->dev, &dev_attr_vcc_mode);
1196 static int iuu_remove_sysfs_attrs(struct usb_serial_port *port)
1198 device_remove_file(&port->dev, &dev_attr_vcc_mode);
1203 * End Sysfs Attributes
1206 static struct usb_serial_driver iuu_device = {
1208 .owner = THIS_MODULE,
1209 .name = "iuu_phoenix",
1211 .id_table = id_table,
1213 .bulk_in_size = 512,
1214 .bulk_out_size = 512,
1217 .write = iuu_uart_write,
1218 .read_bulk_callback = iuu_uart_read_callback,
1219 .tiocmget = iuu_tiocmget,
1220 .tiocmset = iuu_tiocmset,
1221 .set_termios = iuu_set_termios,
1222 .init_termios = iuu_init_termios,
1223 .attach = iuu_attach,
1224 .port_probe = iuu_port_probe,
1225 .port_remove = iuu_port_remove,
1228 static struct usb_serial_driver * const serial_drivers[] = {
1232 module_usb_serial_driver(serial_drivers, id_table);
1234 MODULE_AUTHOR("Alain Degreffe eczema@ecze.com");
1236 MODULE_DESCRIPTION(DRIVER_DESC);
1237 MODULE_LICENSE("GPL");
1239 module_param(xmas, bool, S_IRUGO | S_IWUSR);
1240 MODULE_PARM_DESC(xmas, "Xmas colors enabled or not");
1242 module_param(boost, int, S_IRUGO | S_IWUSR);
1243 MODULE_PARM_DESC(boost, "Card overclock boost (in percent 100-500)");
1245 module_param(clockmode, int, S_IRUGO | S_IWUSR);
1246 MODULE_PARM_DESC(clockmode, "Card clock mode (1=3.579 MHz, 2=3.680 MHz, "
1249 module_param(cdmode, int, S_IRUGO | S_IWUSR);
1250 MODULE_PARM_DESC(cdmode, "Card detect mode (0=none, 1=CD, 2=!CD, 3=DSR, "
1251 "4=!DSR, 5=CTS, 6=!CTS, 7=RING, 8=!RING)");
1253 module_param(vcc_default, int, S_IRUGO | S_IWUSR);
1254 MODULE_PARM_DESC(vcc_default, "Set default VCC (either 3 for 3.3V or 5 "
1255 "for 5V). Default to 5.");