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>
35 #define DRIVER_DESC "Infinity USB Unlimited Phoenix driver"
37 static const struct usb_device_id id_table[] = {
38 {USB_DEVICE(IUU_USB_VENDOR_ID, IUU_USB_PRODUCT_ID)},
39 {} /* Terminating entry */
41 MODULE_DEVICE_TABLE(usb, id_table);
44 static int boost = 100;
45 static int clockmode = 1;
46 static int cdmode = 1;
47 static int iuu_cardin;
48 static int iuu_cardout;
50 static int vcc_default = 5;
52 static int iuu_create_sysfs_attrs(struct usb_serial_port *port);
53 static int iuu_remove_sysfs_attrs(struct usb_serial_port *port);
54 static void read_rxcmd_callback(struct urb *urb);
57 spinlock_t lock; /* store irq state */
58 wait_queue_head_t delta_msr_wait;
60 int tiostatus; /* store IUART SIGNAL for tiocmget call */
61 u8 reset; /* if 1 reset is needed */
62 int poll; /* number of poll */
63 u8 *writebuf; /* buffer for writing to device */
64 int writelen; /* num of byte to write to device */
65 u8 *buf; /* used for initialize speed */
67 int vcc; /* vcc (either 3 or 5 V) */
73 static int iuu_port_probe(struct usb_serial_port *port)
75 struct iuu_private *priv;
78 priv = kzalloc(sizeof(struct iuu_private), GFP_KERNEL);
82 priv->buf = kzalloc(256, GFP_KERNEL);
88 priv->writebuf = kzalloc(256, GFP_KERNEL);
89 if (!priv->writebuf) {
95 priv->vcc = vcc_default;
96 spin_lock_init(&priv->lock);
97 init_waitqueue_head(&priv->delta_msr_wait);
99 usb_set_serial_port_data(port, priv);
101 ret = iuu_create_sysfs_attrs(port);
103 kfree(priv->writebuf);
112 static int iuu_port_remove(struct usb_serial_port *port)
114 struct iuu_private *priv = usb_get_serial_port_data(port);
116 iuu_remove_sysfs_attrs(port);
117 kfree(priv->writebuf);
124 static int iuu_tiocmset(struct tty_struct *tty,
125 unsigned int set, unsigned int clear)
127 struct usb_serial_port *port = tty->driver_data;
128 struct iuu_private *priv = usb_get_serial_port_data(port);
131 /* FIXME: locking on tiomstatus */
132 dev_dbg(&port->dev, "%s msg : SET = 0x%04x, CLEAR = 0x%04x\n",
133 __func__, set, clear);
135 spin_lock_irqsave(&priv->lock, flags);
137 if ((set & TIOCM_RTS) && !(priv->tiostatus == TIOCM_RTS)) {
138 dev_dbg(&port->dev, "%s TIOCMSET RESET called !!!\n", __func__);
142 priv->tiostatus = TIOCM_RTS;
144 spin_unlock_irqrestore(&priv->lock, flags);
148 /* This is used to provide a carrier detect mechanism
149 * When a card is present, the response is 0x00
150 * When no card , the reader respond with TIOCM_CD
151 * This is known as CD autodetect mechanism
153 static int iuu_tiocmget(struct tty_struct *tty)
155 struct usb_serial_port *port = tty->driver_data;
156 struct iuu_private *priv = usb_get_serial_port_data(port);
160 spin_lock_irqsave(&priv->lock, flags);
161 rc = priv->tiostatus;
162 spin_unlock_irqrestore(&priv->lock, flags);
167 static void iuu_rxcmd(struct urb *urb)
169 struct usb_serial_port *port = urb->context;
171 int status = urb->status;
174 dev_dbg(&port->dev, "%s - status = %d\n", __func__, status);
180 memset(port->write_urb->transfer_buffer, IUU_UART_RX, 1);
181 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
182 usb_sndbulkpipe(port->serial->dev,
183 port->bulk_out_endpointAddress),
184 port->write_urb->transfer_buffer, 1,
185 read_rxcmd_callback, port);
186 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
189 static int iuu_reset(struct usb_serial_port *port, u8 wt)
191 struct iuu_private *priv = usb_get_serial_port_data(port);
193 char *buf_ptr = port->write_urb->transfer_buffer;
195 /* Prepare the reset sequence */
197 *buf_ptr++ = IUU_RST_SET;
198 *buf_ptr++ = IUU_DELAY_MS;
200 *buf_ptr = IUU_RST_CLEAR;
202 /* send the sequence */
204 usb_fill_bulk_urb(port->write_urb,
206 usb_sndbulkpipe(port->serial->dev,
207 port->bulk_out_endpointAddress),
208 port->write_urb->transfer_buffer, 4, iuu_rxcmd, port);
209 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
220 static void iuu_update_status_callback(struct urb *urb)
222 struct usb_serial_port *port = urb->context;
223 struct iuu_private *priv = usb_get_serial_port_data(port);
225 int status = urb->status;
228 dev_dbg(&port->dev, "%s - status = %d\n", __func__, status);
233 st = urb->transfer_buffer;
234 dev_dbg(&port->dev, "%s - enter\n", __func__);
235 if (urb->actual_length == 1) {
238 priv->tiostatus = iuu_cardout;
241 priv->tiostatus = iuu_cardin;
244 priv->tiostatus = iuu_cardin;
250 static void iuu_status_callback(struct urb *urb)
252 struct usb_serial_port *port = urb->context;
254 int status = urb->status;
256 dev_dbg(&port->dev, "%s - status = %d\n", __func__, status);
257 usb_fill_bulk_urb(port->read_urb, port->serial->dev,
258 usb_rcvbulkpipe(port->serial->dev,
259 port->bulk_in_endpointAddress),
260 port->read_urb->transfer_buffer, 256,
261 iuu_update_status_callback, port);
262 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
265 static int iuu_status(struct usb_serial_port *port)
269 memset(port->write_urb->transfer_buffer, IUU_GET_STATE_REGISTER, 1);
270 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
271 usb_sndbulkpipe(port->serial->dev,
272 port->bulk_out_endpointAddress),
273 port->write_urb->transfer_buffer, 1,
274 iuu_status_callback, port);
275 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
280 static int bulk_immediate(struct usb_serial_port *port, u8 *buf, u8 count)
283 struct usb_serial *serial = port->serial;
286 /* send the data out the bulk port */
289 usb_bulk_msg(serial->dev,
290 usb_sndbulkpipe(serial->dev,
291 port->bulk_out_endpointAddress), buf,
292 count, &actual, HZ * 1);
294 if (status != IUU_OPERATION_OK)
295 dev_dbg(&port->dev, "%s - error = %2x\n", __func__, status);
297 dev_dbg(&port->dev, "%s - write OK !\n", __func__);
301 static int read_immediate(struct usb_serial_port *port, u8 *buf, u8 count)
304 struct usb_serial *serial = port->serial;
307 /* send the data out the bulk port */
309 usb_bulk_msg(serial->dev,
310 usb_rcvbulkpipe(serial->dev,
311 port->bulk_in_endpointAddress), buf,
312 count, &actual, HZ * 1);
314 if (status != IUU_OPERATION_OK)
315 dev_dbg(&port->dev, "%s - error = %2x\n", __func__, status);
317 dev_dbg(&port->dev, "%s - read OK !\n", __func__);
321 static int iuu_led(struct usb_serial_port *port, unsigned int R,
322 unsigned int G, unsigned int B, u8 f)
326 buf = kmalloc(8, GFP_KERNEL);
330 buf[0] = IUU_SET_LED;
332 buf[2] = (R >> 8) & 0xFF;
334 buf[4] = (G >> 8) & 0xFF;
336 buf[6] = (B >> 8) & 0xFF;
338 status = bulk_immediate(port, buf, 8);
340 if (status != IUU_OPERATION_OK)
341 dev_dbg(&port->dev, "%s - led error status = %2x\n", __func__, status);
343 dev_dbg(&port->dev, "%s - led OK !\n", __func__);
344 return IUU_OPERATION_OK;
347 static void iuu_rgbf_fill_buffer(u8 *buf, u8 r1, u8 r2, u8 g1, u8 g2, u8 b1,
350 *buf++ = IUU_SET_LED;
360 static void iuu_led_activity_on(struct urb *urb)
362 struct usb_serial_port *port = urb->context;
364 char *buf_ptr = port->write_urb->transfer_buffer;
365 *buf_ptr++ = IUU_SET_LED;
367 get_random_bytes(buf_ptr, 6);
370 iuu_rgbf_fill_buffer(buf_ptr, 255, 255, 0, 0, 0, 0, 255);
373 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
374 usb_sndbulkpipe(port->serial->dev,
375 port->bulk_out_endpointAddress),
376 port->write_urb->transfer_buffer, 8 ,
378 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
381 static void iuu_led_activity_off(struct urb *urb)
383 struct usb_serial_port *port = urb->context;
385 char *buf_ptr = port->write_urb->transfer_buffer;
390 *buf_ptr++ = IUU_SET_LED;
391 iuu_rgbf_fill_buffer(buf_ptr, 0, 0, 255, 255, 0, 0, 255);
393 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
394 usb_sndbulkpipe(port->serial->dev,
395 port->bulk_out_endpointAddress),
396 port->write_urb->transfer_buffer, 8 ,
398 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
403 static int iuu_clk(struct usb_serial_port *port, int dwFrq)
406 struct iuu_private *priv = usb_get_serial_port_data(port);
409 u8 DIV = 0; /* 8bit */
410 u8 XDRV = 0; /* 8bit */
411 u8 PUMP = 0; /* 3bit */
412 u8 PBmsb = 0; /* 2bit */
413 u8 PBlsb = 0; /* 8bit */
414 u8 PO = 0; /* 1bit */
419 int frq = (int)dwFrq;
422 priv->buf[Count++] = IUU_UART_WRITE_I2C;
423 priv->buf[Count++] = FrqGenAdr << 1;
424 priv->buf[Count++] = 0x09;
425 priv->buf[Count++] = 0x00;
427 status = bulk_immediate(port, (u8 *) priv->buf, Count);
429 dev_dbg(&port->dev, "%s - write error\n", __func__);
432 } else if (frq == 3579000) {
437 } else if (frq == 3680000) {
442 } else if (frq == 6000000) {
448 unsigned int result = 0;
449 unsigned int tmp = 0;
454 unsigned int lP = 2055;
455 unsigned int lDiv = 4;
457 for (lQ = 2; lQ <= 47 && !found; lQ++)
458 for (lP = 2055; lP >= 8 && !found; lP--)
459 for (lDiv = 4; lDiv <= 127 && !found; lDiv++) {
460 tmp = (12000000 / lDiv) * (lP / lQ);
461 if (abs((int)(tmp - frq)) <
462 abs((int)(frq - result))) {
463 check2 = (12000000 / lQ);
466 check = (12000000 / lQ) * lP;
467 if (check > 400000000)
469 if (check < 100000000)
471 if (lDiv < 4 || lDiv > 127)
482 P2 = ((P - PO) / 2) - 4;
485 PBmsb = (P2 >> 8 & 0x03);
487 PO = (P >> 10) & 0x01;
490 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
491 priv->buf[Count++] = FrqGenAdr << 1;
492 priv->buf[Count++] = 0x09;
493 priv->buf[Count++] = 0x20; /* Adr = 0x09 */
494 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
495 priv->buf[Count++] = FrqGenAdr << 1;
496 priv->buf[Count++] = 0x0C;
497 priv->buf[Count++] = DIV; /* Adr = 0x0C */
498 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
499 priv->buf[Count++] = FrqGenAdr << 1;
500 priv->buf[Count++] = 0x12;
501 priv->buf[Count++] = XDRV; /* Adr = 0x12 */
502 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
503 priv->buf[Count++] = FrqGenAdr << 1;
504 priv->buf[Count++] = 0x13;
505 priv->buf[Count++] = 0x6B; /* Adr = 0x13 */
506 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
507 priv->buf[Count++] = FrqGenAdr << 1;
508 priv->buf[Count++] = 0x40;
509 priv->buf[Count++] = (0xC0 | ((PUMP & 0x07) << 2)) |
510 (PBmsb & 0x03); /* Adr = 0x40 */
511 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
512 priv->buf[Count++] = FrqGenAdr << 1;
513 priv->buf[Count++] = 0x41;
514 priv->buf[Count++] = PBlsb; /* Adr = 0x41 */
515 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
516 priv->buf[Count++] = FrqGenAdr << 1;
517 priv->buf[Count++] = 0x42;
518 priv->buf[Count++] = Q | (((PO & 0x01) << 7)); /* Adr = 0x42 */
519 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
520 priv->buf[Count++] = FrqGenAdr << 1;
521 priv->buf[Count++] = 0x44;
522 priv->buf[Count++] = (char)0xFF; /* Adr = 0x44 */
523 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
524 priv->buf[Count++] = FrqGenAdr << 1;
525 priv->buf[Count++] = 0x45;
526 priv->buf[Count++] = (char)0xFE; /* Adr = 0x45 */
527 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
528 priv->buf[Count++] = FrqGenAdr << 1;
529 priv->buf[Count++] = 0x46;
530 priv->buf[Count++] = 0x7F; /* Adr = 0x46 */
531 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
532 priv->buf[Count++] = FrqGenAdr << 1;
533 priv->buf[Count++] = 0x47;
534 priv->buf[Count++] = (char)0x84; /* Adr = 0x47 */
536 status = bulk_immediate(port, (u8 *) priv->buf, Count);
537 if (status != IUU_OPERATION_OK)
538 dev_dbg(&port->dev, "%s - write error\n", __func__);
542 static int iuu_uart_flush(struct usb_serial_port *port)
544 struct device *dev = &port->dev;
547 u8 rxcmd = IUU_UART_RX;
548 struct iuu_private *priv = usb_get_serial_port_data(port);
550 if (iuu_led(port, 0xF000, 0, 0, 0xFF) < 0)
553 for (i = 0; i < 2; i++) {
554 status = bulk_immediate(port, &rxcmd, 1);
555 if (status != IUU_OPERATION_OK) {
556 dev_dbg(dev, "%s - uart_flush_write error\n", __func__);
560 status = read_immediate(port, &priv->len, 1);
561 if (status != IUU_OPERATION_OK) {
562 dev_dbg(dev, "%s - uart_flush_read error\n", __func__);
567 dev_dbg(dev, "%s - uart_flush datalen is : %i\n", __func__, priv->len);
568 status = read_immediate(port, priv->buf, priv->len);
569 if (status != IUU_OPERATION_OK) {
570 dev_dbg(dev, "%s - uart_flush_read error\n", __func__);
575 dev_dbg(dev, "%s - uart_flush_read OK!\n", __func__);
576 iuu_led(port, 0, 0xF000, 0, 0xFF);
580 static void read_buf_callback(struct urb *urb)
582 struct usb_serial_port *port = urb->context;
583 unsigned char *data = urb->transfer_buffer;
584 struct tty_struct *tty;
585 int status = urb->status;
588 if (status == -EPROTO) {
589 /* reschedule needed */
594 dev_dbg(&port->dev, "%s - %i chars to write\n", __func__, urb->actual_length);
595 tty = tty_port_tty_get(&port->port);
597 dev_dbg(&port->dev, "%s - data is NULL !!!\n", __func__);
598 if (tty && urb->actual_length && data) {
599 tty_insert_flip_string(tty, data, urb->actual_length);
600 tty_flip_buffer_push(tty);
603 iuu_led_activity_on(urb);
606 static int iuu_bulk_write(struct usb_serial_port *port)
608 struct iuu_private *priv = usb_get_serial_port_data(port);
612 char *buf_ptr = port->write_urb->transfer_buffer;
614 spin_lock_irqsave(&priv->lock, flags);
615 *buf_ptr++ = IUU_UART_ESC;
616 *buf_ptr++ = IUU_UART_TX;
617 *buf_ptr++ = priv->writelen;
619 memcpy(buf_ptr, priv->writebuf, priv->writelen);
620 buf_len = priv->writelen;
622 spin_unlock_irqrestore(&priv->lock, flags);
623 dev_dbg(&port->dev, "%s - writing %i chars : %*ph\n", __func__,
624 buf_len, buf_len, buf_ptr);
625 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
626 usb_sndbulkpipe(port->serial->dev,
627 port->bulk_out_endpointAddress),
628 port->write_urb->transfer_buffer, buf_len + 3,
630 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
631 usb_serial_port_softint(port);
635 static int iuu_read_buf(struct usb_serial_port *port, int len)
639 usb_fill_bulk_urb(port->read_urb, port->serial->dev,
640 usb_rcvbulkpipe(port->serial->dev,
641 port->bulk_in_endpointAddress),
642 port->read_urb->transfer_buffer, len,
643 read_buf_callback, port);
644 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
648 static void iuu_uart_read_callback(struct urb *urb)
650 struct usb_serial_port *port = urb->context;
651 struct iuu_private *priv = usb_get_serial_port_data(port);
653 int status = urb->status;
656 unsigned char *data = urb->transfer_buffer;
660 dev_dbg(&port->dev, "%s - status = %d\n", __func__, status);
665 dev_dbg(&port->dev, "%s - data is NULL !!!\n", __func__);
667 if (urb->actual_length == 1 && data != NULL)
670 if (urb->actual_length > 1) {
671 dev_dbg(&port->dev, "%s - urb->actual_length = %i\n", __func__,
676 /* if len > 0 call readbuf */
678 if (len > 0 && error == 0) {
679 dev_dbg(&port->dev, "%s - call read buf - len to read is %i\n",
681 status = iuu_read_buf(port, len);
684 /* need to update status ? */
685 if (priv->poll > 99) {
686 status = iuu_status(port);
691 /* reset waiting ? */
693 if (priv->reset == 1) {
694 status = iuu_reset(port, 0xC);
697 /* Writebuf is waiting */
698 spin_lock_irqsave(&priv->lock, flags);
699 if (priv->writelen > 0) {
700 spin_unlock_irqrestore(&priv->lock, flags);
701 status = iuu_bulk_write(port);
704 spin_unlock_irqrestore(&priv->lock, flags);
705 /* if nothing to write call again rxcmd */
706 dev_dbg(&port->dev, "%s - rxcmd recall\n", __func__);
707 iuu_led_activity_off(urb);
710 static int iuu_uart_write(struct tty_struct *tty, struct usb_serial_port *port,
711 const u8 *buf, int count)
713 struct iuu_private *priv = usb_get_serial_port_data(port);
719 spin_lock_irqsave(&priv->lock, flags);
721 /* fill the buffer */
722 memcpy(priv->writebuf + priv->writelen, buf, count);
723 priv->writelen += count;
724 spin_unlock_irqrestore(&priv->lock, flags);
729 static void read_rxcmd_callback(struct urb *urb)
731 struct usb_serial_port *port = urb->context;
733 int status = urb->status;
740 usb_fill_bulk_urb(port->read_urb, port->serial->dev,
741 usb_rcvbulkpipe(port->serial->dev,
742 port->bulk_in_endpointAddress),
743 port->read_urb->transfer_buffer, 256,
744 iuu_uart_read_callback, port);
745 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
746 dev_dbg(&port->dev, "%s - submit result = %d\n", __func__, result);
749 static int iuu_uart_on(struct usb_serial_port *port)
754 buf = kmalloc(sizeof(u8) * 4, GFP_KERNEL);
759 buf[0] = IUU_UART_ENABLE;
760 buf[1] = (u8) ((IUU_BAUD_9600 >> 8) & 0x00FF);
761 buf[2] = (u8) (0x00FF & IUU_BAUD_9600);
762 buf[3] = (u8) (0x0F0 & IUU_ONE_STOP_BIT) | (0x07 & IUU_PARITY_EVEN);
764 status = bulk_immediate(port, buf, 4);
765 if (status != IUU_OPERATION_OK) {
766 dev_dbg(&port->dev, "%s - uart_on error\n", __func__);
767 goto uart_enable_failed;
769 /* iuu_reset() the card after iuu_uart_on() */
770 status = iuu_uart_flush(port);
771 if (status != IUU_OPERATION_OK)
772 dev_dbg(&port->dev, "%s - uart_flush error\n", __func__);
778 /* Diables the IUU UART (a.k.a. the Phoenix voiderface) */
779 static int iuu_uart_off(struct usb_serial_port *port)
783 buf = kmalloc(1, GFP_KERNEL);
786 buf[0] = IUU_UART_DISABLE;
788 status = bulk_immediate(port, buf, 1);
789 if (status != IUU_OPERATION_OK)
790 dev_dbg(&port->dev, "%s - uart_off error\n", __func__);
796 static int iuu_uart_baud(struct usb_serial_port *port, u32 baud_base,
797 u32 *actual, u8 parity)
805 unsigned int T1FrekvensHZ = 0;
807 dev_dbg(&port->dev, "%s - enter baud_base=%d\n", __func__, baud_base);
808 dataout = kmalloc(sizeof(u8) * 5, GFP_KERNEL);
812 /*baud = (((priv->clk / 35) * baud_base) / 100000); */
815 if (baud < 1200 || baud > 230400) {
817 return IUU_INVALID_PARAMETER;
821 T1FrekvensHZ = 500000;
826 T1FrekvensHZ = 2000000;
831 T1FrekvensHZ = 6000000;
836 T1FrekvensHZ = 24000000;
839 T1reload = 256 - (u8) (T1FrekvensHZ / (baud * 2));
841 /* magic number here: ENTER_FIRMWARE_UPDATE; */
842 dataout[DataCount++] = IUU_UART_ESC;
843 /* magic number here: CHANGE_BAUD; */
844 dataout[DataCount++] = IUU_UART_CHANGE;
845 dataout[DataCount++] = T1Frekvens;
846 dataout[DataCount++] = T1reload;
848 *actual = (T1FrekvensHZ / (256 - T1reload)) / 2;
850 switch (parity & 0x0F) {
851 case IUU_PARITY_NONE:
852 dataout[DataCount++] = 0x00;
854 case IUU_PARITY_EVEN:
855 dataout[DataCount++] = 0x01;
858 dataout[DataCount++] = 0x02;
860 case IUU_PARITY_MARK:
861 dataout[DataCount++] = 0x03;
863 case IUU_PARITY_SPACE:
864 dataout[DataCount++] = 0x04;
868 return IUU_INVALID_PARAMETER;
872 switch (parity & 0xF0) {
873 case IUU_ONE_STOP_BIT:
874 dataout[DataCount - 1] |= IUU_ONE_STOP_BIT;
877 case IUU_TWO_STOP_BITS:
878 dataout[DataCount - 1] |= IUU_TWO_STOP_BITS;
882 return IUU_INVALID_PARAMETER;
886 status = bulk_immediate(port, dataout, DataCount);
887 if (status != IUU_OPERATION_OK)
888 dev_dbg(&port->dev, "%s - uart_off error\n", __func__);
893 static void iuu_set_termios(struct tty_struct *tty,
894 struct usb_serial_port *port, struct ktermios *old_termios)
896 const u32 supported_mask = CMSPAR|PARENB|PARODD;
897 struct iuu_private *priv = usb_get_serial_port_data(port);
898 unsigned int cflag = tty->termios.c_cflag;
904 u32 newval = cflag & supported_mask;
906 /* Just use the ospeed. ispeed should be the same. */
907 baud = tty->termios.c_ospeed;
909 dev_dbg(&port->dev, "%s - enter c_ospeed or baud=%d\n", __func__, baud);
911 /* compute the parity parameter */
913 if (cflag & CMSPAR) { /* Using mark space */
915 parity |= IUU_PARITY_SPACE;
917 parity |= IUU_PARITY_MARK;
918 } else if (!(cflag & PARENB)) {
919 parity |= IUU_PARITY_NONE;
921 } else if (cflag & PARODD)
922 parity |= IUU_PARITY_ODD;
924 parity |= IUU_PARITY_EVEN;
926 parity |= (cflag & CSTOPB ? IUU_TWO_STOP_BITS : IUU_ONE_STOP_BIT);
929 status = iuu_uart_baud(port,
930 baud * priv->boost / 100,
933 /* set the termios value to the real one, so the user now what has
934 * changed. We support few fields so its easies to copy the old hw
935 * settings back over and then adjust them
938 tty_termios_copy_hw(&tty->termios, old_termios);
939 if (status != 0) /* Set failed - return old bits */
941 /* Re-encode speed, parity and csize */
942 tty_encode_baud_rate(tty, baud, baud);
943 tty->termios.c_cflag &= ~(supported_mask|CSIZE);
944 tty->termios.c_cflag |= newval | csize;
947 static void iuu_close(struct usb_serial_port *port)
949 /* iuu_led (port,255,0,0,0); */
950 struct usb_serial *serial;
952 serial = port->serial;
959 /* shutdown our urbs */
960 dev_dbg(&port->dev, "%s - shutting down urbs\n", __func__);
961 usb_kill_urb(port->write_urb);
962 usb_kill_urb(port->read_urb);
963 usb_kill_urb(port->interrupt_in_urb);
964 iuu_led(port, 0, 0, 0xF000, 0xFF);
968 static void iuu_init_termios(struct tty_struct *tty)
970 tty->termios = tty_std_termios;
971 tty->termios.c_cflag = CLOCAL | CREAD | CS8 | B9600
972 | TIOCM_CTS | CSTOPB | PARENB;
973 tty->termios.c_ispeed = 9600;
974 tty->termios.c_ospeed = 9600;
975 tty->termios.c_lflag = 0;
976 tty->termios.c_oflag = 0;
977 tty->termios.c_iflag = 0;
980 static int iuu_open(struct tty_struct *tty, struct usb_serial_port *port)
982 struct usb_serial *serial = port->serial;
983 struct device *dev = &port->dev;
988 struct iuu_private *priv = usb_get_serial_port_data(port);
990 baud = tty->termios.c_ospeed;
991 tty->termios.c_ispeed = baud;
992 /* Re-encode speed */
993 tty_encode_baud_rate(tty, baud, baud);
995 dev_dbg(dev, "%s - baud %d\n", __func__, baud);
996 usb_clear_halt(serial->dev, port->write_urb->pipe);
997 usb_clear_halt(serial->dev, port->read_urb->pipe);
999 buf = kmalloc(10, GFP_KERNEL);
1005 /* initialize writebuf */
1006 #define FISH(a, b, c, d) do { \
1007 result = usb_control_msg(port->serial->dev, \
1008 usb_rcvctrlpipe(port->serial->dev, 0), \
1009 b, a, c, d, buf, 1, 1000); \
1010 dev_dbg(dev, "0x%x:0x%x:0x%x:0x%x %d - %x\n", a, b, c, d, result, \
1011 buf[0]); } while (0);
1013 #define SOUP(a, b, c, d) do { \
1014 result = usb_control_msg(port->serial->dev, \
1015 usb_sndctrlpipe(port->serial->dev, 0), \
1016 b, a, c, d, NULL, 0, 1000); \
1017 dev_dbg(dev, "0x%x:0x%x:0x%x:0x%x %d\n", a, b, c, d, result); } while (0)
1019 /* This is not UART related but IUU USB driver related or something */
1020 /* like that. Basically no IUU will accept any commands from the USB */
1021 /* host unless it has received the following message */
1022 /* sprintf(buf ,"%c%c%c%c",0x03,0x02,0x02,0x0); */
1024 SOUP(0x03, 0x02, 0x02, 0x0);
1026 iuu_led(port, 0xF000, 0xF000, 0, 0xFF);
1030 priv->boost = boost;
1032 switch (clockmode) {
1033 case 2: /* 3.680 Mhz */
1034 priv->clk = IUU_CLK_3680000;
1035 iuu_clk(port, IUU_CLK_3680000 * boost / 100);
1037 iuu_uart_baud(port, baud * boost / 100, &actual,
1040 case 3: /* 6.00 Mhz */
1041 iuu_clk(port, IUU_CLK_6000000 * boost / 100);
1042 priv->clk = IUU_CLK_6000000;
1043 /* Ratio of 6000000 to 3500000 for baud 9600 */
1045 iuu_uart_baud(port, 16457 * boost / 100, &actual,
1048 default: /* 3.579 Mhz */
1049 iuu_clk(port, IUU_CLK_3579000 * boost / 100);
1050 priv->clk = IUU_CLK_3579000;
1052 iuu_uart_baud(port, baud * boost / 100, &actual,
1056 /* set the cardin cardout signals */
1063 iuu_cardin = TIOCM_CD;
1068 iuu_cardout = TIOCM_CD;
1071 iuu_cardin = TIOCM_DSR;
1076 iuu_cardout = TIOCM_DSR;
1079 iuu_cardin = TIOCM_CTS;
1084 iuu_cardout = TIOCM_CTS;
1087 iuu_cardin = TIOCM_RNG;
1092 iuu_cardout = TIOCM_RNG;
1095 iuu_uart_flush(port);
1097 dev_dbg(dev, "%s - initialization done\n", __func__);
1099 memset(port->write_urb->transfer_buffer, IUU_UART_RX, 1);
1100 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
1101 usb_sndbulkpipe(port->serial->dev,
1102 port->bulk_out_endpointAddress),
1103 port->write_urb->transfer_buffer, 1,
1104 read_rxcmd_callback, port);
1105 result = usb_submit_urb(port->write_urb, GFP_KERNEL);
1107 dev_err(dev, "%s - failed submitting read urb, error %d\n", __func__, result);
1110 dev_dbg(dev, "%s - rxcmd OK\n", __func__);
1116 /* how to change VCC */
1117 static int iuu_vcc_set(struct usb_serial_port *port, unsigned int vcc)
1122 buf = kmalloc(5, GFP_KERNEL);
1126 buf[0] = IUU_SET_VCC;
1127 buf[1] = vcc & 0xFF;
1128 buf[2] = (vcc >> 8) & 0xFF;
1129 buf[3] = (vcc >> 16) & 0xFF;
1130 buf[4] = (vcc >> 24) & 0xFF;
1132 status = bulk_immediate(port, buf, 5);
1135 if (status != IUU_OPERATION_OK)
1136 dev_dbg(&port->dev, "%s - vcc error status = %2x\n", __func__, status);
1138 dev_dbg(&port->dev, "%s - vcc OK !\n", __func__);
1147 static ssize_t show_vcc_mode(struct device *dev,
1148 struct device_attribute *attr, char *buf)
1150 struct usb_serial_port *port = to_usb_serial_port(dev);
1151 struct iuu_private *priv = usb_get_serial_port_data(port);
1153 return sprintf(buf, "%d\n", priv->vcc);
1156 static ssize_t store_vcc_mode(struct device *dev,
1157 struct device_attribute *attr, const char *buf, size_t count)
1159 struct usb_serial_port *port = to_usb_serial_port(dev);
1160 struct iuu_private *priv = usb_get_serial_port_data(port);
1163 if (kstrtoul(buf, 10, &v)) {
1164 dev_err(dev, "%s - vcc_mode: %s is not a unsigned long\n",
1166 goto fail_store_vcc_mode;
1169 dev_dbg(dev, "%s: setting vcc_mode = %ld", __func__, v);
1171 if ((v != 3) && (v != 5)) {
1172 dev_err(dev, "%s - vcc_mode %ld is invalid\n", __func__, v);
1174 iuu_vcc_set(port, v);
1177 fail_store_vcc_mode:
1181 static DEVICE_ATTR(vcc_mode, S_IRUSR | S_IWUSR, show_vcc_mode,
1184 static int iuu_create_sysfs_attrs(struct usb_serial_port *port)
1186 return device_create_file(&port->dev, &dev_attr_vcc_mode);
1189 static int iuu_remove_sysfs_attrs(struct usb_serial_port *port)
1191 device_remove_file(&port->dev, &dev_attr_vcc_mode);
1196 * End Sysfs Attributes
1199 static struct usb_serial_driver iuu_device = {
1201 .owner = THIS_MODULE,
1202 .name = "iuu_phoenix",
1204 .id_table = id_table,
1206 .bulk_in_size = 512,
1207 .bulk_out_size = 512,
1210 .write = iuu_uart_write,
1211 .read_bulk_callback = iuu_uart_read_callback,
1212 .tiocmget = iuu_tiocmget,
1213 .tiocmset = iuu_tiocmset,
1214 .set_termios = iuu_set_termios,
1215 .init_termios = iuu_init_termios,
1216 .port_probe = iuu_port_probe,
1217 .port_remove = iuu_port_remove,
1220 static struct usb_serial_driver * const serial_drivers[] = {
1224 module_usb_serial_driver(serial_drivers, id_table);
1226 MODULE_AUTHOR("Alain Degreffe eczema@ecze.com");
1228 MODULE_DESCRIPTION(DRIVER_DESC);
1229 MODULE_LICENSE("GPL");
1231 module_param(xmas, bool, S_IRUGO | S_IWUSR);
1232 MODULE_PARM_DESC(xmas, "Xmas colors enabled or not");
1234 module_param(boost, int, S_IRUGO | S_IWUSR);
1235 MODULE_PARM_DESC(boost, "Card overclock boost (in percent 100-500)");
1237 module_param(clockmode, int, S_IRUGO | S_IWUSR);
1238 MODULE_PARM_DESC(clockmode, "Card clock mode (1=3.579 MHz, 2=3.680 MHz, "
1241 module_param(cdmode, int, S_IRUGO | S_IWUSR);
1242 MODULE_PARM_DESC(cdmode, "Card detect mode (0=none, 1=CD, 2=!CD, 3=DSR, "
1243 "4=!DSR, 5=CTS, 6=!CTS, 7=RING, 8=!RING)");
1245 module_param(vcc_default, int, S_IRUGO | S_IWUSR);
1246 MODULE_PARM_DESC(vcc_default, "Set default VCC (either 3 for 3.3V or 5 "
1247 "for 5V). Default to 5.");