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_port_probe(struct usb_serial_port *port)
73 struct iuu_private *priv;
76 priv = kzalloc(sizeof(struct iuu_private), GFP_KERNEL);
80 priv->buf = kzalloc(256, GFP_KERNEL);
86 priv->writebuf = kzalloc(256, GFP_KERNEL);
87 if (!priv->writebuf) {
93 priv->vcc = vcc_default;
94 spin_lock_init(&priv->lock);
96 usb_set_serial_port_data(port, priv);
98 ret = iuu_create_sysfs_attrs(port);
100 kfree(priv->writebuf);
109 static int iuu_port_remove(struct usb_serial_port *port)
111 struct iuu_private *priv = usb_get_serial_port_data(port);
113 iuu_remove_sysfs_attrs(port);
114 kfree(priv->writebuf);
121 static int iuu_tiocmset(struct tty_struct *tty,
122 unsigned int set, unsigned int clear)
124 struct usb_serial_port *port = tty->driver_data;
125 struct iuu_private *priv = usb_get_serial_port_data(port);
128 /* FIXME: locking on tiomstatus */
129 dev_dbg(&port->dev, "%s msg : SET = 0x%04x, CLEAR = 0x%04x\n",
130 __func__, set, clear);
132 spin_lock_irqsave(&priv->lock, flags);
134 if ((set & TIOCM_RTS) && !(priv->tiostatus == TIOCM_RTS)) {
135 dev_dbg(&port->dev, "%s TIOCMSET RESET called !!!\n", __func__);
139 priv->tiostatus = TIOCM_RTS;
141 spin_unlock_irqrestore(&priv->lock, flags);
145 /* This is used to provide a carrier detect mechanism
146 * When a card is present, the response is 0x00
147 * When no card , the reader respond with TIOCM_CD
148 * This is known as CD autodetect mechanism
150 static int iuu_tiocmget(struct tty_struct *tty)
152 struct usb_serial_port *port = tty->driver_data;
153 struct iuu_private *priv = usb_get_serial_port_data(port);
157 spin_lock_irqsave(&priv->lock, flags);
158 rc = priv->tiostatus;
159 spin_unlock_irqrestore(&priv->lock, flags);
164 static void iuu_rxcmd(struct urb *urb)
166 struct usb_serial_port *port = urb->context;
168 int status = urb->status;
171 dev_dbg(&port->dev, "%s - status = %d\n", __func__, status);
177 memset(port->write_urb->transfer_buffer, IUU_UART_RX, 1);
178 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
179 usb_sndbulkpipe(port->serial->dev,
180 port->bulk_out_endpointAddress),
181 port->write_urb->transfer_buffer, 1,
182 read_rxcmd_callback, port);
183 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
186 static int iuu_reset(struct usb_serial_port *port, u8 wt)
188 struct iuu_private *priv = usb_get_serial_port_data(port);
190 char *buf_ptr = port->write_urb->transfer_buffer;
192 /* Prepare the reset sequence */
194 *buf_ptr++ = IUU_RST_SET;
195 *buf_ptr++ = IUU_DELAY_MS;
197 *buf_ptr = IUU_RST_CLEAR;
199 /* send the sequence */
201 usb_fill_bulk_urb(port->write_urb,
203 usb_sndbulkpipe(port->serial->dev,
204 port->bulk_out_endpointAddress),
205 port->write_urb->transfer_buffer, 4, iuu_rxcmd, port);
206 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
217 static void iuu_update_status_callback(struct urb *urb)
219 struct usb_serial_port *port = urb->context;
220 struct iuu_private *priv = usb_get_serial_port_data(port);
222 int status = urb->status;
225 dev_dbg(&port->dev, "%s - status = %d\n", __func__, status);
230 st = urb->transfer_buffer;
231 dev_dbg(&port->dev, "%s - enter\n", __func__);
232 if (urb->actual_length == 1) {
235 priv->tiostatus = iuu_cardout;
238 priv->tiostatus = iuu_cardin;
241 priv->tiostatus = iuu_cardin;
247 static void iuu_status_callback(struct urb *urb)
249 struct usb_serial_port *port = urb->context;
251 int status = urb->status;
253 dev_dbg(&port->dev, "%s - status = %d\n", __func__, status);
254 usb_fill_bulk_urb(port->read_urb, port->serial->dev,
255 usb_rcvbulkpipe(port->serial->dev,
256 port->bulk_in_endpointAddress),
257 port->read_urb->transfer_buffer, 256,
258 iuu_update_status_callback, port);
259 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
262 static int iuu_status(struct usb_serial_port *port)
266 memset(port->write_urb->transfer_buffer, IUU_GET_STATE_REGISTER, 1);
267 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
268 usb_sndbulkpipe(port->serial->dev,
269 port->bulk_out_endpointAddress),
270 port->write_urb->transfer_buffer, 1,
271 iuu_status_callback, port);
272 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
277 static int bulk_immediate(struct usb_serial_port *port, u8 *buf, u8 count)
280 struct usb_serial *serial = port->serial;
283 /* send the data out the bulk port */
286 usb_bulk_msg(serial->dev,
287 usb_sndbulkpipe(serial->dev,
288 port->bulk_out_endpointAddress), buf,
289 count, &actual, 1000);
291 if (status != IUU_OPERATION_OK)
292 dev_dbg(&port->dev, "%s - error = %2x\n", __func__, status);
294 dev_dbg(&port->dev, "%s - write OK !\n", __func__);
298 static int read_immediate(struct usb_serial_port *port, u8 *buf, u8 count)
301 struct usb_serial *serial = port->serial;
304 /* send the data out the bulk port */
306 usb_bulk_msg(serial->dev,
307 usb_rcvbulkpipe(serial->dev,
308 port->bulk_in_endpointAddress), buf,
309 count, &actual, 1000);
311 if (status != IUU_OPERATION_OK)
312 dev_dbg(&port->dev, "%s - error = %2x\n", __func__, status);
314 dev_dbg(&port->dev, "%s - read OK !\n", __func__);
318 static int iuu_led(struct usb_serial_port *port, unsigned int R,
319 unsigned int G, unsigned int B, u8 f)
323 buf = kmalloc(8, GFP_KERNEL);
327 buf[0] = IUU_SET_LED;
329 buf[2] = (R >> 8) & 0xFF;
331 buf[4] = (G >> 8) & 0xFF;
333 buf[6] = (B >> 8) & 0xFF;
335 status = bulk_immediate(port, buf, 8);
337 if (status != IUU_OPERATION_OK)
338 dev_dbg(&port->dev, "%s - led error status = %2x\n", __func__, status);
340 dev_dbg(&port->dev, "%s - led OK !\n", __func__);
341 return IUU_OPERATION_OK;
344 static void iuu_rgbf_fill_buffer(u8 *buf, u8 r1, u8 r2, u8 g1, u8 g2, u8 b1,
347 *buf++ = IUU_SET_LED;
357 static void iuu_led_activity_on(struct urb *urb)
359 struct usb_serial_port *port = urb->context;
361 char *buf_ptr = port->write_urb->transfer_buffer;
362 *buf_ptr++ = IUU_SET_LED;
364 get_random_bytes(buf_ptr, 6);
367 iuu_rgbf_fill_buffer(buf_ptr, 255, 255, 0, 0, 0, 0, 255);
370 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
371 usb_sndbulkpipe(port->serial->dev,
372 port->bulk_out_endpointAddress),
373 port->write_urb->transfer_buffer, 8 ,
375 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
378 static void iuu_led_activity_off(struct urb *urb)
380 struct usb_serial_port *port = urb->context;
382 char *buf_ptr = port->write_urb->transfer_buffer;
387 *buf_ptr++ = IUU_SET_LED;
388 iuu_rgbf_fill_buffer(buf_ptr, 0, 0, 255, 255, 0, 0, 255);
390 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
391 usb_sndbulkpipe(port->serial->dev,
392 port->bulk_out_endpointAddress),
393 port->write_urb->transfer_buffer, 8 ,
395 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
400 static int iuu_clk(struct usb_serial_port *port, int dwFrq)
403 struct iuu_private *priv = usb_get_serial_port_data(port);
406 u8 DIV = 0; /* 8bit */
407 u8 XDRV = 0; /* 8bit */
408 u8 PUMP = 0; /* 3bit */
409 u8 PBmsb = 0; /* 2bit */
410 u8 PBlsb = 0; /* 8bit */
411 u8 PO = 0; /* 1bit */
416 int frq = (int)dwFrq;
419 priv->buf[Count++] = IUU_UART_WRITE_I2C;
420 priv->buf[Count++] = FrqGenAdr << 1;
421 priv->buf[Count++] = 0x09;
422 priv->buf[Count++] = 0x00;
424 status = bulk_immediate(port, (u8 *) priv->buf, Count);
426 dev_dbg(&port->dev, "%s - write error\n", __func__);
429 } else if (frq == 3579000) {
434 } else if (frq == 3680000) {
439 } else if (frq == 6000000) {
445 unsigned int result = 0;
446 unsigned int tmp = 0;
451 unsigned int lP = 2055;
452 unsigned int lDiv = 4;
454 for (lQ = 2; lQ <= 47 && !found; lQ++)
455 for (lP = 2055; lP >= 8 && !found; lP--)
456 for (lDiv = 4; lDiv <= 127 && !found; lDiv++) {
457 tmp = (12000000 / lDiv) * (lP / lQ);
458 if (abs((int)(tmp - frq)) <
459 abs((int)(frq - result))) {
460 check2 = (12000000 / lQ);
463 check = (12000000 / lQ) * lP;
464 if (check > 400000000)
466 if (check < 100000000)
468 if (lDiv < 4 || lDiv > 127)
479 P2 = ((P - PO) / 2) - 4;
482 PBmsb = (P2 >> 8 & 0x03);
484 PO = (P >> 10) & 0x01;
487 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
488 priv->buf[Count++] = FrqGenAdr << 1;
489 priv->buf[Count++] = 0x09;
490 priv->buf[Count++] = 0x20; /* Adr = 0x09 */
491 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
492 priv->buf[Count++] = FrqGenAdr << 1;
493 priv->buf[Count++] = 0x0C;
494 priv->buf[Count++] = DIV; /* Adr = 0x0C */
495 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
496 priv->buf[Count++] = FrqGenAdr << 1;
497 priv->buf[Count++] = 0x12;
498 priv->buf[Count++] = XDRV; /* Adr = 0x12 */
499 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
500 priv->buf[Count++] = FrqGenAdr << 1;
501 priv->buf[Count++] = 0x13;
502 priv->buf[Count++] = 0x6B; /* Adr = 0x13 */
503 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
504 priv->buf[Count++] = FrqGenAdr << 1;
505 priv->buf[Count++] = 0x40;
506 priv->buf[Count++] = (0xC0 | ((PUMP & 0x07) << 2)) |
507 (PBmsb & 0x03); /* Adr = 0x40 */
508 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
509 priv->buf[Count++] = FrqGenAdr << 1;
510 priv->buf[Count++] = 0x41;
511 priv->buf[Count++] = PBlsb; /* Adr = 0x41 */
512 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
513 priv->buf[Count++] = FrqGenAdr << 1;
514 priv->buf[Count++] = 0x42;
515 priv->buf[Count++] = Q | (((PO & 0x01) << 7)); /* Adr = 0x42 */
516 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
517 priv->buf[Count++] = FrqGenAdr << 1;
518 priv->buf[Count++] = 0x44;
519 priv->buf[Count++] = (char)0xFF; /* Adr = 0x44 */
520 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
521 priv->buf[Count++] = FrqGenAdr << 1;
522 priv->buf[Count++] = 0x45;
523 priv->buf[Count++] = (char)0xFE; /* Adr = 0x45 */
524 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
525 priv->buf[Count++] = FrqGenAdr << 1;
526 priv->buf[Count++] = 0x46;
527 priv->buf[Count++] = 0x7F; /* Adr = 0x46 */
528 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
529 priv->buf[Count++] = FrqGenAdr << 1;
530 priv->buf[Count++] = 0x47;
531 priv->buf[Count++] = (char)0x84; /* Adr = 0x47 */
533 status = bulk_immediate(port, (u8 *) priv->buf, Count);
534 if (status != IUU_OPERATION_OK)
535 dev_dbg(&port->dev, "%s - write error\n", __func__);
539 static int iuu_uart_flush(struct usb_serial_port *port)
541 struct device *dev = &port->dev;
544 u8 rxcmd = IUU_UART_RX;
545 struct iuu_private *priv = usb_get_serial_port_data(port);
547 if (iuu_led(port, 0xF000, 0, 0, 0xFF) < 0)
550 for (i = 0; i < 2; i++) {
551 status = bulk_immediate(port, &rxcmd, 1);
552 if (status != IUU_OPERATION_OK) {
553 dev_dbg(dev, "%s - uart_flush_write error\n", __func__);
557 status = read_immediate(port, &priv->len, 1);
558 if (status != IUU_OPERATION_OK) {
559 dev_dbg(dev, "%s - uart_flush_read error\n", __func__);
564 dev_dbg(dev, "%s - uart_flush datalen is : %i\n", __func__, priv->len);
565 status = read_immediate(port, priv->buf, priv->len);
566 if (status != IUU_OPERATION_OK) {
567 dev_dbg(dev, "%s - uart_flush_read error\n", __func__);
572 dev_dbg(dev, "%s - uart_flush_read OK!\n", __func__);
573 iuu_led(port, 0, 0xF000, 0, 0xFF);
577 static void read_buf_callback(struct urb *urb)
579 struct usb_serial_port *port = urb->context;
580 unsigned char *data = urb->transfer_buffer;
581 int status = urb->status;
584 if (status == -EPROTO) {
585 /* reschedule needed */
590 dev_dbg(&port->dev, "%s - %i chars to write\n", __func__, urb->actual_length);
592 if (urb->actual_length) {
593 tty_insert_flip_string(&port->port, data, urb->actual_length);
594 tty_flip_buffer_push(&port->port);
596 iuu_led_activity_on(urb);
599 static int iuu_bulk_write(struct usb_serial_port *port)
601 struct iuu_private *priv = usb_get_serial_port_data(port);
605 char *buf_ptr = port->write_urb->transfer_buffer;
607 spin_lock_irqsave(&priv->lock, flags);
608 *buf_ptr++ = IUU_UART_ESC;
609 *buf_ptr++ = IUU_UART_TX;
610 *buf_ptr++ = priv->writelen;
612 memcpy(buf_ptr, priv->writebuf, priv->writelen);
613 buf_len = priv->writelen;
615 spin_unlock_irqrestore(&priv->lock, flags);
616 dev_dbg(&port->dev, "%s - writing %i chars : %*ph\n", __func__,
617 buf_len, buf_len, buf_ptr);
618 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
619 usb_sndbulkpipe(port->serial->dev,
620 port->bulk_out_endpointAddress),
621 port->write_urb->transfer_buffer, buf_len + 3,
623 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
624 usb_serial_port_softint(port);
628 static int iuu_read_buf(struct usb_serial_port *port, int len)
632 usb_fill_bulk_urb(port->read_urb, port->serial->dev,
633 usb_rcvbulkpipe(port->serial->dev,
634 port->bulk_in_endpointAddress),
635 port->read_urb->transfer_buffer, len,
636 read_buf_callback, port);
637 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
641 static void iuu_uart_read_callback(struct urb *urb)
643 struct usb_serial_port *port = urb->context;
644 struct iuu_private *priv = usb_get_serial_port_data(port);
646 int status = urb->status;
649 unsigned char *data = urb->transfer_buffer;
653 dev_dbg(&port->dev, "%s - status = %d\n", __func__, status);
658 if (urb->actual_length == 1)
661 if (urb->actual_length > 1) {
662 dev_dbg(&port->dev, "%s - urb->actual_length = %i\n", __func__,
667 /* if len > 0 call readbuf */
669 if (len > 0 && error == 0) {
670 dev_dbg(&port->dev, "%s - call read buf - len to read is %i\n",
672 status = iuu_read_buf(port, len);
675 /* need to update status ? */
676 if (priv->poll > 99) {
677 status = iuu_status(port);
682 /* reset waiting ? */
684 if (priv->reset == 1) {
685 status = iuu_reset(port, 0xC);
688 /* Writebuf is waiting */
689 spin_lock_irqsave(&priv->lock, flags);
690 if (priv->writelen > 0) {
691 spin_unlock_irqrestore(&priv->lock, flags);
692 status = iuu_bulk_write(port);
695 spin_unlock_irqrestore(&priv->lock, flags);
696 /* if nothing to write call again rxcmd */
697 dev_dbg(&port->dev, "%s - rxcmd recall\n", __func__);
698 iuu_led_activity_off(urb);
701 static int iuu_uart_write(struct tty_struct *tty, struct usb_serial_port *port,
702 const u8 *buf, int count)
704 struct iuu_private *priv = usb_get_serial_port_data(port);
710 spin_lock_irqsave(&priv->lock, flags);
712 /* fill the buffer */
713 memcpy(priv->writebuf + priv->writelen, buf, count);
714 priv->writelen += count;
715 spin_unlock_irqrestore(&priv->lock, flags);
720 static void read_rxcmd_callback(struct urb *urb)
722 struct usb_serial_port *port = urb->context;
724 int status = urb->status;
731 usb_fill_bulk_urb(port->read_urb, port->serial->dev,
732 usb_rcvbulkpipe(port->serial->dev,
733 port->bulk_in_endpointAddress),
734 port->read_urb->transfer_buffer, 256,
735 iuu_uart_read_callback, port);
736 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
737 dev_dbg(&port->dev, "%s - submit result = %d\n", __func__, result);
740 static int iuu_uart_on(struct usb_serial_port *port)
745 buf = kmalloc(sizeof(u8) * 4, GFP_KERNEL);
750 buf[0] = IUU_UART_ENABLE;
751 buf[1] = (u8) ((IUU_BAUD_9600 >> 8) & 0x00FF);
752 buf[2] = (u8) (0x00FF & IUU_BAUD_9600);
753 buf[3] = (u8) (0x0F0 & IUU_ONE_STOP_BIT) | (0x07 & IUU_PARITY_EVEN);
755 status = bulk_immediate(port, buf, 4);
756 if (status != IUU_OPERATION_OK) {
757 dev_dbg(&port->dev, "%s - uart_on error\n", __func__);
758 goto uart_enable_failed;
760 /* iuu_reset() the card after iuu_uart_on() */
761 status = iuu_uart_flush(port);
762 if (status != IUU_OPERATION_OK)
763 dev_dbg(&port->dev, "%s - uart_flush error\n", __func__);
769 /* Disables the IUU UART (a.k.a. the Phoenix voiderface) */
770 static int iuu_uart_off(struct usb_serial_port *port)
774 buf = kmalloc(1, GFP_KERNEL);
777 buf[0] = IUU_UART_DISABLE;
779 status = bulk_immediate(port, buf, 1);
780 if (status != IUU_OPERATION_OK)
781 dev_dbg(&port->dev, "%s - uart_off error\n", __func__);
787 static int iuu_uart_baud(struct usb_serial_port *port, u32 baud_base,
788 u32 *actual, u8 parity)
796 unsigned int T1FrekvensHZ = 0;
798 dev_dbg(&port->dev, "%s - enter baud_base=%d\n", __func__, baud_base);
799 dataout = kmalloc(sizeof(u8) * 5, GFP_KERNEL);
803 /*baud = (((priv->clk / 35) * baud_base) / 100000); */
806 if (baud < 1200 || baud > 230400) {
808 return IUU_INVALID_PARAMETER;
812 T1FrekvensHZ = 500000;
817 T1FrekvensHZ = 2000000;
822 T1FrekvensHZ = 6000000;
827 T1FrekvensHZ = 24000000;
830 T1reload = 256 - (u8) (T1FrekvensHZ / (baud * 2));
832 /* magic number here: ENTER_FIRMWARE_UPDATE; */
833 dataout[DataCount++] = IUU_UART_ESC;
834 /* magic number here: CHANGE_BAUD; */
835 dataout[DataCount++] = IUU_UART_CHANGE;
836 dataout[DataCount++] = T1Frekvens;
837 dataout[DataCount++] = T1reload;
839 *actual = (T1FrekvensHZ / (256 - T1reload)) / 2;
841 switch (parity & 0x0F) {
842 case IUU_PARITY_NONE:
843 dataout[DataCount++] = 0x00;
845 case IUU_PARITY_EVEN:
846 dataout[DataCount++] = 0x01;
849 dataout[DataCount++] = 0x02;
851 case IUU_PARITY_MARK:
852 dataout[DataCount++] = 0x03;
854 case IUU_PARITY_SPACE:
855 dataout[DataCount++] = 0x04;
859 return IUU_INVALID_PARAMETER;
863 switch (parity & 0xF0) {
864 case IUU_ONE_STOP_BIT:
865 dataout[DataCount - 1] |= IUU_ONE_STOP_BIT;
868 case IUU_TWO_STOP_BITS:
869 dataout[DataCount - 1] |= IUU_TWO_STOP_BITS;
873 return IUU_INVALID_PARAMETER;
877 status = bulk_immediate(port, dataout, DataCount);
878 if (status != IUU_OPERATION_OK)
879 dev_dbg(&port->dev, "%s - uart_off error\n", __func__);
884 static void iuu_set_termios(struct tty_struct *tty,
885 struct usb_serial_port *port, struct ktermios *old_termios)
887 const u32 supported_mask = CMSPAR|PARENB|PARODD;
888 struct iuu_private *priv = usb_get_serial_port_data(port);
889 unsigned int cflag = tty->termios.c_cflag;
895 u32 newval = cflag & supported_mask;
897 /* Just use the ospeed. ispeed should be the same. */
898 baud = tty->termios.c_ospeed;
900 dev_dbg(&port->dev, "%s - enter c_ospeed or baud=%d\n", __func__, baud);
902 /* compute the parity parameter */
904 if (cflag & CMSPAR) { /* Using mark space */
906 parity |= IUU_PARITY_SPACE;
908 parity |= IUU_PARITY_MARK;
909 } else if (!(cflag & PARENB)) {
910 parity |= IUU_PARITY_NONE;
912 } else if (cflag & PARODD)
913 parity |= IUU_PARITY_ODD;
915 parity |= IUU_PARITY_EVEN;
917 parity |= (cflag & CSTOPB ? IUU_TWO_STOP_BITS : IUU_ONE_STOP_BIT);
920 status = iuu_uart_baud(port,
921 baud * priv->boost / 100,
924 /* set the termios value to the real one, so the user now what has
925 * changed. We support few fields so its easies to copy the old hw
926 * settings back over and then adjust them
929 tty_termios_copy_hw(&tty->termios, old_termios);
930 if (status != 0) /* Set failed - return old bits */
932 /* Re-encode speed, parity and csize */
933 tty_encode_baud_rate(tty, baud, baud);
934 tty->termios.c_cflag &= ~(supported_mask|CSIZE);
935 tty->termios.c_cflag |= newval | csize;
938 static void iuu_close(struct usb_serial_port *port)
940 /* iuu_led (port,255,0,0,0); */
944 usb_kill_urb(port->write_urb);
945 usb_kill_urb(port->read_urb);
947 iuu_led(port, 0, 0, 0xF000, 0xFF);
950 static void iuu_init_termios(struct tty_struct *tty)
952 tty->termios = tty_std_termios;
953 tty->termios.c_cflag = CLOCAL | CREAD | CS8 | B9600
954 | TIOCM_CTS | CSTOPB | PARENB;
955 tty->termios.c_ispeed = 9600;
956 tty->termios.c_ospeed = 9600;
957 tty->termios.c_lflag = 0;
958 tty->termios.c_oflag = 0;
959 tty->termios.c_iflag = 0;
962 static int iuu_open(struct tty_struct *tty, struct usb_serial_port *port)
964 struct usb_serial *serial = port->serial;
965 struct device *dev = &port->dev;
969 struct iuu_private *priv = usb_get_serial_port_data(port);
971 baud = tty->termios.c_ospeed;
972 tty->termios.c_ispeed = baud;
973 /* Re-encode speed */
974 tty_encode_baud_rate(tty, baud, baud);
976 dev_dbg(dev, "%s - baud %d\n", __func__, baud);
977 usb_clear_halt(serial->dev, port->write_urb->pipe);
978 usb_clear_halt(serial->dev, port->read_urb->pipe);
982 #define SOUP(a, b, c, d) do { \
983 result = usb_control_msg(port->serial->dev, \
984 usb_sndctrlpipe(port->serial->dev, 0), \
985 b, a, c, d, NULL, 0, 1000); \
986 dev_dbg(dev, "0x%x:0x%x:0x%x:0x%x %d\n", a, b, c, d, result); } while (0)
988 /* This is not UART related but IUU USB driver related or something */
989 /* like that. Basically no IUU will accept any commands from the USB */
990 /* host unless it has received the following message */
991 /* sprintf(buf ,"%c%c%c%c",0x03,0x02,0x02,0x0); */
993 SOUP(0x03, 0x02, 0x02, 0x0);
995 iuu_led(port, 0xF000, 0xF000, 0, 0xFF);
1001 switch (clockmode) {
1002 case 2: /* 3.680 Mhz */
1003 priv->clk = IUU_CLK_3680000;
1004 iuu_clk(port, IUU_CLK_3680000 * boost / 100);
1006 iuu_uart_baud(port, baud * boost / 100, &actual,
1009 case 3: /* 6.00 Mhz */
1010 iuu_clk(port, IUU_CLK_6000000 * boost / 100);
1011 priv->clk = IUU_CLK_6000000;
1012 /* Ratio of 6000000 to 3500000 for baud 9600 */
1014 iuu_uart_baud(port, 16457 * boost / 100, &actual,
1017 default: /* 3.579 Mhz */
1018 iuu_clk(port, IUU_CLK_3579000 * boost / 100);
1019 priv->clk = IUU_CLK_3579000;
1021 iuu_uart_baud(port, baud * boost / 100, &actual,
1025 /* set the cardin cardout signals */
1032 iuu_cardin = TIOCM_CD;
1037 iuu_cardout = TIOCM_CD;
1040 iuu_cardin = TIOCM_DSR;
1045 iuu_cardout = TIOCM_DSR;
1048 iuu_cardin = TIOCM_CTS;
1053 iuu_cardout = TIOCM_CTS;
1056 iuu_cardin = TIOCM_RNG;
1061 iuu_cardout = TIOCM_RNG;
1064 iuu_uart_flush(port);
1066 dev_dbg(dev, "%s - initialization done\n", __func__);
1068 memset(port->write_urb->transfer_buffer, IUU_UART_RX, 1);
1069 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
1070 usb_sndbulkpipe(port->serial->dev,
1071 port->bulk_out_endpointAddress),
1072 port->write_urb->transfer_buffer, 1,
1073 read_rxcmd_callback, port);
1074 result = usb_submit_urb(port->write_urb, GFP_KERNEL);
1076 dev_err(dev, "%s - failed submitting read urb, error %d\n", __func__, result);
1079 dev_dbg(dev, "%s - rxcmd OK\n", __func__);
1085 /* how to change VCC */
1086 static int iuu_vcc_set(struct usb_serial_port *port, unsigned int vcc)
1091 buf = kmalloc(5, GFP_KERNEL);
1095 buf[0] = IUU_SET_VCC;
1096 buf[1] = vcc & 0xFF;
1097 buf[2] = (vcc >> 8) & 0xFF;
1098 buf[3] = (vcc >> 16) & 0xFF;
1099 buf[4] = (vcc >> 24) & 0xFF;
1101 status = bulk_immediate(port, buf, 5);
1104 if (status != IUU_OPERATION_OK)
1105 dev_dbg(&port->dev, "%s - vcc error status = %2x\n", __func__, status);
1107 dev_dbg(&port->dev, "%s - vcc OK !\n", __func__);
1116 static ssize_t vcc_mode_show(struct device *dev,
1117 struct device_attribute *attr, char *buf)
1119 struct usb_serial_port *port = to_usb_serial_port(dev);
1120 struct iuu_private *priv = usb_get_serial_port_data(port);
1122 return sprintf(buf, "%d\n", priv->vcc);
1125 static ssize_t vcc_mode_store(struct device *dev,
1126 struct device_attribute *attr, const char *buf, size_t count)
1128 struct usb_serial_port *port = to_usb_serial_port(dev);
1129 struct iuu_private *priv = usb_get_serial_port_data(port);
1132 if (kstrtoul(buf, 10, &v)) {
1133 dev_err(dev, "%s - vcc_mode: %s is not a unsigned long\n",
1135 goto fail_store_vcc_mode;
1138 dev_dbg(dev, "%s: setting vcc_mode = %ld\n", __func__, v);
1140 if ((v != 3) && (v != 5)) {
1141 dev_err(dev, "%s - vcc_mode %ld is invalid\n", __func__, v);
1143 iuu_vcc_set(port, v);
1146 fail_store_vcc_mode:
1149 static DEVICE_ATTR_RW(vcc_mode);
1151 static int iuu_create_sysfs_attrs(struct usb_serial_port *port)
1153 return device_create_file(&port->dev, &dev_attr_vcc_mode);
1156 static int iuu_remove_sysfs_attrs(struct usb_serial_port *port)
1158 device_remove_file(&port->dev, &dev_attr_vcc_mode);
1163 * End Sysfs Attributes
1166 static struct usb_serial_driver iuu_device = {
1168 .owner = THIS_MODULE,
1169 .name = "iuu_phoenix",
1171 .id_table = id_table,
1175 .bulk_in_size = 512,
1176 .bulk_out_size = 512,
1179 .write = iuu_uart_write,
1180 .read_bulk_callback = iuu_uart_read_callback,
1181 .tiocmget = iuu_tiocmget,
1182 .tiocmset = iuu_tiocmset,
1183 .set_termios = iuu_set_termios,
1184 .init_termios = iuu_init_termios,
1185 .port_probe = iuu_port_probe,
1186 .port_remove = iuu_port_remove,
1189 static struct usb_serial_driver * const serial_drivers[] = {
1193 module_usb_serial_driver(serial_drivers, id_table);
1195 MODULE_AUTHOR("Alain Degreffe eczema@ecze.com");
1197 MODULE_DESCRIPTION(DRIVER_DESC);
1198 MODULE_LICENSE("GPL");
1200 module_param(xmas, bool, S_IRUGO | S_IWUSR);
1201 MODULE_PARM_DESC(xmas, "Xmas colors enabled or not");
1203 module_param(boost, int, S_IRUGO | S_IWUSR);
1204 MODULE_PARM_DESC(boost, "Card overclock boost (in percent 100-500)");
1206 module_param(clockmode, int, S_IRUGO | S_IWUSR);
1207 MODULE_PARM_DESC(clockmode, "Card clock mode (1=3.579 MHz, 2=3.680 MHz, "
1210 module_param(cdmode, int, S_IRUGO | S_IWUSR);
1211 MODULE_PARM_DESC(cdmode, "Card detect mode (0=none, 1=CD, 2=!CD, 3=DSR, "
1212 "4=!DSR, 5=CTS, 6=!CTS, 7=RING, 8=!RING)");
1214 module_param(vcc_default, int, S_IRUGO | S_IWUSR);
1215 MODULE_PARM_DESC(vcc_default, "Set default VCC (either 3 for 3.3V or 5 "
1216 "for 5V). Default to 5.");