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",
56 .probe = usb_serial_probe,
57 .disconnect = usb_serial_disconnect,
62 static int boost = 100;
63 static int clockmode = 1;
64 static int cdmode = 1;
65 static int iuu_cardin;
66 static int iuu_cardout;
68 static int vcc_default = 5;
70 static void read_rxcmd_callback(struct urb *urb);
73 spinlock_t lock; /* store irq state */
74 wait_queue_head_t delta_msr_wait;
76 int tiostatus; /* store IUART SIGNAL for tiocmget call */
77 u8 reset; /* if 1 reset is needed */
78 int poll; /* number of poll */
79 u8 *writebuf; /* buffer for writing to device */
80 int writelen; /* num of byte to write to device */
81 u8 *buf; /* used for initialize speed */
82 u8 *dbgbuf; /* debug buffer */
84 int vcc; /* vcc (either 3 or 5 V) */
91 static void iuu_free_buf(struct iuu_private *priv)
95 kfree(priv->writebuf);
98 static int iuu_alloc_buf(struct iuu_private *priv)
100 priv->buf = kzalloc(256, GFP_KERNEL);
101 priv->dbgbuf = kzalloc(256, GFP_KERNEL);
102 priv->writebuf = kzalloc(256, GFP_KERNEL);
103 if (!priv->buf || !priv->dbgbuf || !priv->writebuf) {
105 dbg("%s problem allocation buffer", __func__);
108 dbg("%s - Privates buffers allocation success", __func__);
112 static int iuu_startup(struct usb_serial *serial)
114 struct iuu_private *priv;
115 priv = kzalloc(sizeof(struct iuu_private), GFP_KERNEL);
116 dbg("%s- priv allocation success", __func__);
119 if (iuu_alloc_buf(priv)) {
123 priv->vcc = vcc_default;
124 spin_lock_init(&priv->lock);
125 init_waitqueue_head(&priv->delta_msr_wait);
126 usb_set_serial_port_data(serial->port[0], priv);
130 /* Release function */
131 static void iuu_release(struct usb_serial *serial)
133 struct usb_serial_port *port = serial->port[0];
134 struct iuu_private *priv = usb_get_serial_port_data(port);
140 dbg("%s - I will free all", __func__);
141 usb_set_serial_port_data(port, NULL);
143 dbg("%s - priv is not anymore in port structure", __func__);
146 dbg("%s priv is now kfree", __func__);
150 static int iuu_tiocmset(struct tty_struct *tty,
151 unsigned int set, unsigned int clear)
153 struct usb_serial_port *port = tty->driver_data;
154 struct iuu_private *priv = usb_get_serial_port_data(port);
157 /* FIXME: locking on tiomstatus */
158 dbg("%s (%d) msg : SET = 0x%04x, CLEAR = 0x%04x ", __func__,
159 port->number, set, clear);
161 spin_lock_irqsave(&priv->lock, flags);
163 if ((set & TIOCM_RTS) && !(priv->tiostatus == TIOCM_RTS)) {
164 dbg("%s TIOCMSET RESET called !!!", __func__);
168 priv->tiostatus = TIOCM_RTS;
170 spin_unlock_irqrestore(&priv->lock, flags);
174 /* This is used to provide a carrier detect mechanism
175 * When a card is present, the response is 0x00
176 * When no card , the reader respond with TIOCM_CD
177 * This is known as CD autodetect mechanism
179 static int iuu_tiocmget(struct tty_struct *tty)
181 struct usb_serial_port *port = tty->driver_data;
182 struct iuu_private *priv = usb_get_serial_port_data(port);
186 spin_lock_irqsave(&priv->lock, flags);
187 rc = priv->tiostatus;
188 spin_unlock_irqrestore(&priv->lock, flags);
193 static void iuu_rxcmd(struct urb *urb)
195 struct usb_serial_port *port = urb->context;
197 int status = urb->status;
200 dbg("%s - status = %d", __func__, status);
206 memset(port->write_urb->transfer_buffer, IUU_UART_RX, 1);
207 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
208 usb_sndbulkpipe(port->serial->dev,
209 port->bulk_out_endpointAddress),
210 port->write_urb->transfer_buffer, 1,
211 read_rxcmd_callback, port);
212 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
215 static int iuu_reset(struct usb_serial_port *port, u8 wt)
217 struct iuu_private *priv = usb_get_serial_port_data(port);
219 char *buf_ptr = port->write_urb->transfer_buffer;
221 /* Prepare the reset sequence */
223 *buf_ptr++ = IUU_RST_SET;
224 *buf_ptr++ = IUU_DELAY_MS;
226 *buf_ptr = IUU_RST_CLEAR;
228 /* send the sequence */
230 usb_fill_bulk_urb(port->write_urb,
232 usb_sndbulkpipe(port->serial->dev,
233 port->bulk_out_endpointAddress),
234 port->write_urb->transfer_buffer, 4, iuu_rxcmd, port);
235 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
246 static void iuu_update_status_callback(struct urb *urb)
248 struct usb_serial_port *port = urb->context;
249 struct iuu_private *priv = usb_get_serial_port_data(port);
251 int status = urb->status;
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 memset(port->write_urb->transfer_buffer, IUU_GET_STATE_REGISTER, 1);
296 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
297 usb_sndbulkpipe(port->serial->dev,
298 port->bulk_out_endpointAddress),
299 port->write_urb->transfer_buffer, 1,
300 iuu_status_callback, port);
301 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
306 static int bulk_immediate(struct usb_serial_port *port, u8 *buf, u8 count)
309 struct usb_serial *serial = port->serial;
312 /* send the data out the bulk port */
315 usb_bulk_msg(serial->dev,
316 usb_sndbulkpipe(serial->dev,
317 port->bulk_out_endpointAddress), buf,
318 count, &actual, HZ * 1);
320 if (status != IUU_OPERATION_OK)
321 dbg("%s - error = %2x", __func__, status);
323 dbg("%s - write OK !", __func__);
327 static int read_immediate(struct usb_serial_port *port, u8 *buf, u8 count)
330 struct usb_serial *serial = port->serial;
333 /* send the data out the bulk port */
335 usb_bulk_msg(serial->dev,
336 usb_rcvbulkpipe(serial->dev,
337 port->bulk_in_endpointAddress), buf,
338 count, &actual, HZ * 1);
340 if (status != IUU_OPERATION_OK)
341 dbg("%s - error = %2x", __func__, status);
343 dbg("%s - read OK !", __func__);
347 static int iuu_led(struct usb_serial_port *port, unsigned int R,
348 unsigned int G, unsigned int B, u8 f)
352 buf = kmalloc(8, GFP_KERNEL);
356 buf[0] = IUU_SET_LED;
358 buf[2] = (R >> 8) & 0xFF;
360 buf[4] = (G >> 8) & 0xFF;
362 buf[6] = (B >> 8) & 0xFF;
364 status = bulk_immediate(port, buf, 8);
366 if (status != IUU_OPERATION_OK)
367 dbg("%s - led error status = %2x", __func__, status);
369 dbg("%s - led OK !", __func__);
370 return IUU_OPERATION_OK;
373 static void iuu_rgbf_fill_buffer(u8 *buf, u8 r1, u8 r2, u8 g1, u8 g2, u8 b1,
376 *buf++ = IUU_SET_LED;
386 static void iuu_led_activity_on(struct urb *urb)
388 struct usb_serial_port *port = urb->context;
390 char *buf_ptr = port->write_urb->transfer_buffer;
391 *buf_ptr++ = IUU_SET_LED;
393 get_random_bytes(buf_ptr, 6);
396 iuu_rgbf_fill_buffer(buf_ptr, 255, 255, 0, 0, 0, 0, 255);
399 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
400 usb_sndbulkpipe(port->serial->dev,
401 port->bulk_out_endpointAddress),
402 port->write_urb->transfer_buffer, 8 ,
404 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
407 static void iuu_led_activity_off(struct urb *urb)
409 struct usb_serial_port *port = urb->context;
411 char *buf_ptr = port->write_urb->transfer_buffer;
416 *buf_ptr++ = IUU_SET_LED;
417 iuu_rgbf_fill_buffer(buf_ptr, 0, 0, 255, 255, 0, 0, 255);
419 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
420 usb_sndbulkpipe(port->serial->dev,
421 port->bulk_out_endpointAddress),
422 port->write_urb->transfer_buffer, 8 ,
424 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
429 static int iuu_clk(struct usb_serial_port *port, int dwFrq)
432 struct iuu_private *priv = usb_get_serial_port_data(port);
435 u8 DIV = 0; /* 8bit */
436 u8 XDRV = 0; /* 8bit */
437 u8 PUMP = 0; /* 3bit */
438 u8 PBmsb = 0; /* 2bit */
439 u8 PBlsb = 0; /* 8bit */
440 u8 PO = 0; /* 1bit */
445 int frq = (int)dwFrq;
448 priv->buf[Count++] = IUU_UART_WRITE_I2C;
449 priv->buf[Count++] = FrqGenAdr << 1;
450 priv->buf[Count++] = 0x09;
451 priv->buf[Count++] = 0x00;
453 status = bulk_immediate(port, (u8 *) priv->buf, Count);
455 dbg("%s - write error ", __func__);
458 } else if (frq == 3579000) {
463 } else if (frq == 3680000) {
468 } else if (frq == 6000000) {
474 unsigned int result = 0;
475 unsigned int tmp = 0;
480 unsigned int lP = 2055;
481 unsigned int lDiv = 4;
483 for (lQ = 2; lQ <= 47 && !found; lQ++)
484 for (lP = 2055; lP >= 8 && !found; lP--)
485 for (lDiv = 4; lDiv <= 127 && !found; lDiv++) {
486 tmp = (12000000 / lDiv) * (lP / lQ);
487 if (abs((int)(tmp - frq)) <
488 abs((int)(frq - result))) {
489 check2 = (12000000 / lQ);
492 check = (12000000 / lQ) * lP;
493 if (check > 400000000)
495 if (check < 100000000)
497 if (lDiv < 4 || lDiv > 127)
508 P2 = ((P - PO) / 2) - 4;
511 PBmsb = (P2 >> 8 & 0x03);
513 PO = (P >> 10) & 0x01;
516 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
517 priv->buf[Count++] = FrqGenAdr << 1;
518 priv->buf[Count++] = 0x09;
519 priv->buf[Count++] = 0x20; /* Adr = 0x09 */
520 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
521 priv->buf[Count++] = FrqGenAdr << 1;
522 priv->buf[Count++] = 0x0C;
523 priv->buf[Count++] = DIV; /* Adr = 0x0C */
524 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
525 priv->buf[Count++] = FrqGenAdr << 1;
526 priv->buf[Count++] = 0x12;
527 priv->buf[Count++] = XDRV; /* Adr = 0x12 */
528 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
529 priv->buf[Count++] = FrqGenAdr << 1;
530 priv->buf[Count++] = 0x13;
531 priv->buf[Count++] = 0x6B; /* Adr = 0x13 */
532 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
533 priv->buf[Count++] = FrqGenAdr << 1;
534 priv->buf[Count++] = 0x40;
535 priv->buf[Count++] = (0xC0 | ((PUMP & 0x07) << 2)) |
536 (PBmsb & 0x03); /* Adr = 0x40 */
537 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
538 priv->buf[Count++] = FrqGenAdr << 1;
539 priv->buf[Count++] = 0x41;
540 priv->buf[Count++] = PBlsb; /* Adr = 0x41 */
541 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
542 priv->buf[Count++] = FrqGenAdr << 1;
543 priv->buf[Count++] = 0x42;
544 priv->buf[Count++] = Q | (((PO & 0x01) << 7)); /* Adr = 0x42 */
545 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
546 priv->buf[Count++] = FrqGenAdr << 1;
547 priv->buf[Count++] = 0x44;
548 priv->buf[Count++] = (char)0xFF; /* Adr = 0x44 */
549 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
550 priv->buf[Count++] = FrqGenAdr << 1;
551 priv->buf[Count++] = 0x45;
552 priv->buf[Count++] = (char)0xFE; /* Adr = 0x45 */
553 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
554 priv->buf[Count++] = FrqGenAdr << 1;
555 priv->buf[Count++] = 0x46;
556 priv->buf[Count++] = 0x7F; /* Adr = 0x46 */
557 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
558 priv->buf[Count++] = FrqGenAdr << 1;
559 priv->buf[Count++] = 0x47;
560 priv->buf[Count++] = (char)0x84; /* Adr = 0x47 */
562 status = bulk_immediate(port, (u8 *) priv->buf, Count);
563 if (status != IUU_OPERATION_OK)
564 dbg("%s - write error ", __func__);
568 static int iuu_uart_flush(struct usb_serial_port *port)
572 u8 rxcmd = IUU_UART_RX;
573 struct iuu_private *priv = usb_get_serial_port_data(port);
575 if (iuu_led(port, 0xF000, 0, 0, 0xFF) < 0)
578 for (i = 0; i < 2; i++) {
579 status = bulk_immediate(port, &rxcmd, 1);
580 if (status != IUU_OPERATION_OK) {
581 dbg("%s - uart_flush_write error", __func__);
585 status = read_immediate(port, &priv->len, 1);
586 if (status != IUU_OPERATION_OK) {
587 dbg("%s - uart_flush_read error", __func__);
592 dbg("%s - uart_flush datalen is : %i ", __func__,
594 status = read_immediate(port, priv->buf, priv->len);
595 if (status != IUU_OPERATION_OK) {
596 dbg("%s - uart_flush_read error", __func__);
601 dbg("%s - uart_flush_read OK!", __func__);
602 iuu_led(port, 0, 0xF000, 0, 0xFF);
606 static void read_buf_callback(struct urb *urb)
608 struct usb_serial_port *port = urb->context;
609 unsigned char *data = urb->transfer_buffer;
610 struct tty_struct *tty;
611 int status = urb->status;
614 if (status == -EPROTO) {
615 /* reschedule needed */
620 dbg("%s - %i chars to write", __func__, urb->actual_length);
621 tty = tty_port_tty_get(&port->port);
623 dbg("%s - data is NULL !!!", __func__);
624 if (tty && urb->actual_length && data) {
625 tty_insert_flip_string(tty, data, urb->actual_length);
626 tty_flip_buffer_push(tty);
629 iuu_led_activity_on(urb);
632 static int iuu_bulk_write(struct usb_serial_port *port)
634 struct iuu_private *priv = usb_get_serial_port_data(port);
639 char *buf_ptr = port->write_urb->transfer_buffer;
641 spin_lock_irqsave(&priv->lock, flags);
642 *buf_ptr++ = IUU_UART_ESC;
643 *buf_ptr++ = IUU_UART_TX;
644 *buf_ptr++ = priv->writelen;
646 memcpy(buf_ptr, priv->writebuf, priv->writelen);
647 buf_len = priv->writelen;
649 spin_unlock_irqrestore(&priv->lock, flags);
651 for (i = 0; i < buf_len; i++)
652 sprintf(priv->dbgbuf + i*2 ,
653 "%02X", priv->writebuf[i]);
654 priv->dbgbuf[buf_len+i*2] = 0;
655 dbg("%s - writing %i chars : %s", __func__,
656 buf_len, priv->dbgbuf);
658 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
659 usb_sndbulkpipe(port->serial->dev,
660 port->bulk_out_endpointAddress),
661 port->write_urb->transfer_buffer, buf_len + 3,
663 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
664 usb_serial_port_softint(port);
668 static int iuu_read_buf(struct usb_serial_port *port, int len)
672 usb_fill_bulk_urb(port->read_urb, port->serial->dev,
673 usb_rcvbulkpipe(port->serial->dev,
674 port->bulk_in_endpointAddress),
675 port->read_urb->transfer_buffer, len,
676 read_buf_callback, port);
677 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
681 static void iuu_uart_read_callback(struct urb *urb)
683 struct usb_serial_port *port = urb->context;
684 struct iuu_private *priv = usb_get_serial_port_data(port);
686 int status = urb->status;
689 unsigned char *data = urb->transfer_buffer;
693 dbg("%s - status = %d", __func__, status);
698 dbg("%s - data is NULL !!!", __func__);
700 if (urb->actual_length == 1 && data != NULL)
703 if (urb->actual_length > 1) {
704 dbg("%s - urb->actual_length = %i", __func__,
709 /* if len > 0 call readbuf */
711 if (len > 0 && error == 0) {
712 dbg("%s - call read buf - len to read is %i ",
714 status = iuu_read_buf(port, len);
717 /* need to update status ? */
718 if (priv->poll > 99) {
719 status = iuu_status(port);
724 /* reset waiting ? */
726 if (priv->reset == 1) {
727 status = iuu_reset(port, 0xC);
730 /* Writebuf is waiting */
731 spin_lock_irqsave(&priv->lock, flags);
732 if (priv->writelen > 0) {
733 spin_unlock_irqrestore(&priv->lock, flags);
734 status = iuu_bulk_write(port);
737 spin_unlock_irqrestore(&priv->lock, flags);
738 /* if nothing to write call again rxcmd */
739 dbg("%s - rxcmd recall", __func__);
740 iuu_led_activity_off(urb);
743 static int iuu_uart_write(struct tty_struct *tty, struct usb_serial_port *port,
744 const u8 *buf, int count)
746 struct iuu_private *priv = usb_get_serial_port_data(port);
752 spin_lock_irqsave(&priv->lock, flags);
754 /* fill the buffer */
755 memcpy(priv->writebuf + priv->writelen, buf, count);
756 priv->writelen += count;
757 spin_unlock_irqrestore(&priv->lock, flags);
762 static void read_rxcmd_callback(struct urb *urb)
764 struct usb_serial_port *port = urb->context;
766 int status = urb->status;
773 usb_fill_bulk_urb(port->read_urb, port->serial->dev,
774 usb_rcvbulkpipe(port->serial->dev,
775 port->bulk_in_endpointAddress),
776 port->read_urb->transfer_buffer, 256,
777 iuu_uart_read_callback, port);
778 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
779 dbg("%s - submit result = %d", __func__, result);
782 static int iuu_uart_on(struct usb_serial_port *port)
787 buf = kmalloc(sizeof(u8) * 4, GFP_KERNEL);
792 buf[0] = IUU_UART_ENABLE;
793 buf[1] = (u8) ((IUU_BAUD_9600 >> 8) & 0x00FF);
794 buf[2] = (u8) (0x00FF & IUU_BAUD_9600);
795 buf[3] = (u8) (0x0F0 & IUU_ONE_STOP_BIT) | (0x07 & IUU_PARITY_EVEN);
797 status = bulk_immediate(port, buf, 4);
798 if (status != IUU_OPERATION_OK) {
799 dbg("%s - uart_on error", __func__);
800 goto uart_enable_failed;
802 /* iuu_reset() the card after iuu_uart_on() */
803 status = iuu_uart_flush(port);
804 if (status != IUU_OPERATION_OK)
805 dbg("%s - uart_flush error", __func__);
811 /* Diables the IUU UART (a.k.a. the Phoenix voiderface) */
812 static int iuu_uart_off(struct usb_serial_port *port)
816 buf = kmalloc(1, GFP_KERNEL);
819 buf[0] = IUU_UART_DISABLE;
821 status = bulk_immediate(port, buf, 1);
822 if (status != IUU_OPERATION_OK)
823 dbg("%s - uart_off error", __func__);
829 static int iuu_uart_baud(struct usb_serial_port *port, u32 baud_base,
830 u32 *actual, u8 parity)
838 unsigned int T1FrekvensHZ = 0;
840 dbg("%s - enter baud_base=%d", __func__, baud_base);
841 dataout = kmalloc(sizeof(u8) * 5, GFP_KERNEL);
845 /*baud = (((priv->clk / 35) * baud_base) / 100000); */
848 if (baud < 1200 || baud > 230400) {
850 return IUU_INVALID_PARAMETER;
854 T1FrekvensHZ = 500000;
859 T1FrekvensHZ = 2000000;
864 T1FrekvensHZ = 6000000;
869 T1FrekvensHZ = 24000000;
872 T1reload = 256 - (u8) (T1FrekvensHZ / (baud * 2));
874 /* magic number here: ENTER_FIRMWARE_UPDATE; */
875 dataout[DataCount++] = IUU_UART_ESC;
876 /* magic number here: CHANGE_BAUD; */
877 dataout[DataCount++] = IUU_UART_CHANGE;
878 dataout[DataCount++] = T1Frekvens;
879 dataout[DataCount++] = T1reload;
881 *actual = (T1FrekvensHZ / (256 - T1reload)) / 2;
883 switch (parity & 0x0F) {
884 case IUU_PARITY_NONE:
885 dataout[DataCount++] = 0x00;
887 case IUU_PARITY_EVEN:
888 dataout[DataCount++] = 0x01;
891 dataout[DataCount++] = 0x02;
893 case IUU_PARITY_MARK:
894 dataout[DataCount++] = 0x03;
896 case IUU_PARITY_SPACE:
897 dataout[DataCount++] = 0x04;
901 return IUU_INVALID_PARAMETER;
905 switch (parity & 0xF0) {
906 case IUU_ONE_STOP_BIT:
907 dataout[DataCount - 1] |= IUU_ONE_STOP_BIT;
910 case IUU_TWO_STOP_BITS:
911 dataout[DataCount - 1] |= IUU_TWO_STOP_BITS;
915 return IUU_INVALID_PARAMETER;
919 status = bulk_immediate(port, dataout, DataCount);
920 if (status != IUU_OPERATION_OK)
921 dbg("%s - uart_off error", __func__);
926 static void iuu_set_termios(struct tty_struct *tty,
927 struct usb_serial_port *port, struct ktermios *old_termios)
929 const u32 supported_mask = CMSPAR|PARENB|PARODD;
930 struct iuu_private *priv = usb_get_serial_port_data(port);
931 unsigned int cflag = tty->termios->c_cflag;
937 u32 newval = cflag & supported_mask;
939 /* Just use the ospeed. ispeed should be the same. */
940 baud = tty->termios->c_ospeed;
942 dbg("%s - enter c_ospeed or baud=%d", __func__, baud);
944 /* compute the parity parameter */
946 if (cflag & CMSPAR) { /* Using mark space */
948 parity |= IUU_PARITY_SPACE;
950 parity |= IUU_PARITY_MARK;
951 } else if (!(cflag & PARENB)) {
952 parity |= IUU_PARITY_NONE;
954 } else if (cflag & PARODD)
955 parity |= IUU_PARITY_ODD;
957 parity |= IUU_PARITY_EVEN;
959 parity |= (cflag & CSTOPB ? IUU_TWO_STOP_BITS : IUU_ONE_STOP_BIT);
962 status = iuu_uart_baud(port,
963 baud * priv->boost / 100,
966 /* set the termios value to the real one, so the user now what has
967 * changed. We support few fields so its easies to copy the old hw
968 * settings back over and then adjust them
971 tty_termios_copy_hw(tty->termios, old_termios);
972 if (status != 0) /* Set failed - return old bits */
974 /* Re-encode speed, parity and csize */
975 tty_encode_baud_rate(tty, baud, baud);
976 tty->termios->c_cflag &= ~(supported_mask|CSIZE);
977 tty->termios->c_cflag |= newval | csize;
980 static void iuu_close(struct usb_serial_port *port)
982 /* iuu_led (port,255,0,0,0); */
983 struct usb_serial *serial;
985 serial = port->serial;
992 /* shutdown our urbs */
993 dbg("%s - shutting down urbs", __func__);
994 usb_kill_urb(port->write_urb);
995 usb_kill_urb(port->read_urb);
996 usb_kill_urb(port->interrupt_in_urb);
997 iuu_led(port, 0, 0, 0xF000, 0xFF);
1001 static void iuu_init_termios(struct tty_struct *tty)
1003 *(tty->termios) = tty_std_termios;
1004 tty->termios->c_cflag = CLOCAL | CREAD | CS8 | B9600
1005 | TIOCM_CTS | CSTOPB | PARENB;
1006 tty->termios->c_ispeed = 9600;
1007 tty->termios->c_ospeed = 9600;
1008 tty->termios->c_lflag = 0;
1009 tty->termios->c_oflag = 0;
1010 tty->termios->c_iflag = 0;
1013 static int iuu_open(struct tty_struct *tty, struct usb_serial_port *port)
1015 struct usb_serial *serial = port->serial;
1020 struct iuu_private *priv = usb_get_serial_port_data(port);
1022 baud = tty->termios->c_ospeed;
1023 tty->termios->c_ispeed = baud;
1024 /* Re-encode speed */
1025 tty_encode_baud_rate(tty, baud, baud);
1027 dbg("%s - port %d, baud %d", __func__, port->number, baud);
1028 usb_clear_halt(serial->dev, port->write_urb->pipe);
1029 usb_clear_halt(serial->dev, port->read_urb->pipe);
1031 buf = kmalloc(10, GFP_KERNEL);
1037 /* initialize writebuf */
1038 #define FISH(a, b, c, d) do { \
1039 result = usb_control_msg(port->serial->dev, \
1040 usb_rcvctrlpipe(port->serial->dev, 0), \
1041 b, a, c, d, buf, 1, 1000); \
1042 dbg("0x%x:0x%x:0x%x:0x%x %d - %x", a, b, c, d, result, \
1043 buf[0]); } while (0);
1045 #define SOUP(a, b, c, d) do { \
1046 result = usb_control_msg(port->serial->dev, \
1047 usb_sndctrlpipe(port->serial->dev, 0), \
1048 b, a, c, d, NULL, 0, 1000); \
1049 dbg("0x%x:0x%x:0x%x:0x%x %d", a, b, c, d, result); } while (0)
1051 /* This is not UART related but IUU USB driver related or something */
1052 /* like that. Basically no IUU will accept any commands from the USB */
1053 /* host unless it has received the following message */
1054 /* sprintf(buf ,"%c%c%c%c",0x03,0x02,0x02,0x0); */
1056 SOUP(0x03, 0x02, 0x02, 0x0);
1058 iuu_led(port, 0xF000, 0xF000, 0, 0xFF);
1062 priv->boost = boost;
1064 switch (clockmode) {
1065 case 2: /* 3.680 Mhz */
1066 priv->clk = IUU_CLK_3680000;
1067 iuu_clk(port, IUU_CLK_3680000 * boost / 100);
1069 iuu_uart_baud(port, baud * boost / 100, &actual,
1072 case 3: /* 6.00 Mhz */
1073 iuu_clk(port, IUU_CLK_6000000 * boost / 100);
1074 priv->clk = IUU_CLK_6000000;
1075 /* Ratio of 6000000 to 3500000 for baud 9600 */
1077 iuu_uart_baud(port, 16457 * boost / 100, &actual,
1080 default: /* 3.579 Mhz */
1081 iuu_clk(port, IUU_CLK_3579000 * boost / 100);
1082 priv->clk = IUU_CLK_3579000;
1084 iuu_uart_baud(port, baud * boost / 100, &actual,
1088 /* set the cardin cardout signals */
1095 iuu_cardin = TIOCM_CD;
1100 iuu_cardout = TIOCM_CD;
1103 iuu_cardin = TIOCM_DSR;
1108 iuu_cardout = TIOCM_DSR;
1111 iuu_cardin = TIOCM_CTS;
1116 iuu_cardout = TIOCM_CTS;
1119 iuu_cardin = TIOCM_RNG;
1124 iuu_cardout = TIOCM_RNG;
1127 iuu_uart_flush(port);
1129 dbg("%s - initialization done", __func__);
1131 memset(port->write_urb->transfer_buffer, IUU_UART_RX, 1);
1132 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
1133 usb_sndbulkpipe(port->serial->dev,
1134 port->bulk_out_endpointAddress),
1135 port->write_urb->transfer_buffer, 1,
1136 read_rxcmd_callback, port);
1137 result = usb_submit_urb(port->write_urb, GFP_KERNEL);
1139 dev_err(&port->dev, "%s - failed submitting read urb,"
1140 " error %d\n", __func__, result);
1143 dbg("%s - rxcmd OK", __func__);
1149 /* how to change VCC */
1150 static int iuu_vcc_set(struct usb_serial_port *port, unsigned int vcc)
1155 buf = kmalloc(5, GFP_KERNEL);
1159 buf[0] = IUU_SET_VCC;
1160 buf[1] = vcc & 0xFF;
1161 buf[2] = (vcc >> 8) & 0xFF;
1162 buf[3] = (vcc >> 16) & 0xFF;
1163 buf[4] = (vcc >> 24) & 0xFF;
1165 status = bulk_immediate(port, buf, 5);
1168 if (status != IUU_OPERATION_OK)
1169 dbg("%s - vcc error status = %2x", __func__, status);
1171 dbg("%s - vcc OK !", __func__);
1180 static ssize_t show_vcc_mode(struct device *dev,
1181 struct device_attribute *attr, char *buf)
1183 struct usb_serial_port *port = to_usb_serial_port(dev);
1184 struct iuu_private *priv = usb_get_serial_port_data(port);
1186 return sprintf(buf, "%d\n", priv->vcc);
1189 static ssize_t store_vcc_mode(struct device *dev,
1190 struct device_attribute *attr, const char *buf, size_t count)
1192 struct usb_serial_port *port = to_usb_serial_port(dev);
1193 struct iuu_private *priv = usb_get_serial_port_data(port);
1196 if (strict_strtoul(buf, 10, &v)) {
1197 dev_err(dev, "%s - vcc_mode: %s is not a unsigned long\n",
1199 goto fail_store_vcc_mode;
1202 dbg("%s: setting vcc_mode = %ld", __func__, v);
1204 if ((v != 3) && (v != 5)) {
1205 dev_err(dev, "%s - vcc_mode %ld is invalid\n", __func__, v);
1207 iuu_vcc_set(port, v);
1210 fail_store_vcc_mode:
1214 static DEVICE_ATTR(vcc_mode, S_IRUSR | S_IWUSR, show_vcc_mode,
1217 static int iuu_create_sysfs_attrs(struct usb_serial_port *port)
1219 return device_create_file(&port->dev, &dev_attr_vcc_mode);
1222 static int iuu_remove_sysfs_attrs(struct usb_serial_port *port)
1224 device_remove_file(&port->dev, &dev_attr_vcc_mode);
1229 * End Sysfs Attributes
1232 static struct usb_serial_driver iuu_device = {
1234 .owner = THIS_MODULE,
1235 .name = "iuu_phoenix",
1237 .id_table = id_table,
1239 .bulk_in_size = 512,
1240 .bulk_out_size = 512,
1241 .port_probe = iuu_create_sysfs_attrs,
1242 .port_remove = iuu_remove_sysfs_attrs,
1245 .write = iuu_uart_write,
1246 .read_bulk_callback = iuu_uart_read_callback,
1247 .tiocmget = iuu_tiocmget,
1248 .tiocmset = iuu_tiocmset,
1249 .set_termios = iuu_set_termios,
1250 .init_termios = iuu_init_termios,
1251 .attach = iuu_startup,
1252 .release = iuu_release,
1255 static struct usb_serial_driver * const serial_drivers[] = {
1259 module_usb_serial_driver(iuu_driver, serial_drivers);
1261 MODULE_AUTHOR("Alain Degreffe eczema@ecze.com");
1263 MODULE_DESCRIPTION(DRIVER_DESC);
1264 MODULE_LICENSE("GPL");
1266 MODULE_VERSION(DRIVER_VERSION);
1267 module_param(debug, bool, S_IRUGO | S_IWUSR);
1268 MODULE_PARM_DESC(debug, "Debug enabled or not");
1270 module_param(xmas, bool, S_IRUGO | S_IWUSR);
1271 MODULE_PARM_DESC(xmas, "Xmas colors enabled or not");
1273 module_param(boost, int, S_IRUGO | S_IWUSR);
1274 MODULE_PARM_DESC(boost, "Card overclock boost (in percent 100-500)");
1276 module_param(clockmode, int, S_IRUGO | S_IWUSR);
1277 MODULE_PARM_DESC(clockmode, "Card clock mode (1=3.579 MHz, 2=3.680 MHz, "
1280 module_param(cdmode, int, S_IRUGO | S_IWUSR);
1281 MODULE_PARM_DESC(cdmode, "Card detect mode (0=none, 1=CD, 2=!CD, 3=DSR, "
1282 "4=!DSR, 5=CTS, 6=!CTS, 7=RING, 8=!RING)");
1284 module_param(vcc_default, int, S_IRUGO | S_IWUSR);
1285 MODULE_PARM_DESC(vcc_default, "Set default VCC (either 3 for 3.3V or 5 "
1286 "for 5V). Default to 5.");