18 #include <linux/kernel.h>
19 #include <linux/errno.h>
21 #include <linux/slab.h>
22 #include <linux/tty.h>
25 #include <linux/serial.h>
26 #include <linux/module.h>
33 #include <linux/random.h>
38 #define DRIVER_VERSION "v0.12"
39 #define DRIVER_DESC "Infinity USB Unlimited Phoenix driver"
48 static int boost = 100;
49 static int clockmode = 1;
50 static int cdmode = 1;
51 static int iuu_cardin;
52 static int iuu_cardout;
54 static int vcc_default = 5;
58 static void read_rxcmd_callback(
struct urb *
urb);
99 priv->
vcc = vcc_default;
103 usb_set_serial_port_data(port, priv);
105 ret = iuu_create_sysfs_attrs(port);
118 struct iuu_private *priv = usb_get_serial_port_data(port);
120 iuu_remove_sysfs_attrs(port);
128 static int iuu_tiocmset(
struct tty_struct *tty,
129 unsigned int set,
unsigned int clear)
132 struct iuu_private *priv = usb_get_serial_port_data(port);
136 dev_dbg(&port->
dev,
"%s msg : SET = 0x%04x, CLEAR = 0x%04x\n",
137 __func__,
set, clear);
142 dev_dbg(&port->
dev,
"%s TIOCMSET RESET called !!!\n", __func__);
148 spin_unlock_irqrestore(&priv->
lock, flags);
157 static int iuu_tiocmget(
struct tty_struct *tty)
160 struct iuu_private *priv = usb_get_serial_port_data(port);
166 spin_unlock_irqrestore(&priv->
lock, flags);
171 static void iuu_rxcmd(
struct urb *
urb)
178 dev_dbg(&port->
dev,
"%s - status = %d\n", __func__, status);
186 usb_sndbulkpipe(port->
serial->dev,
189 read_rxcmd_callback, port);
195 struct iuu_private *priv = usb_get_serial_port_data(port);
210 usb_sndbulkpipe(port->
serial->dev,
212 port->
write_urb->transfer_buffer, 4, iuu_rxcmd, port);
224 static void iuu_update_status_callback(
struct urb *urb)
227 struct iuu_private *priv = usb_get_serial_port_data(port);
229 int status = urb->status;
232 dev_dbg(&port->
dev,
"%s - status = %d\n", __func__, status);
237 st = urb->transfer_buffer;
238 dev_dbg(&port->
dev,
"%s - enter\n", __func__);
239 if (urb->actual_length == 1) {
254 static void iuu_status_callback(
struct urb *urb)
258 int status = urb->status;
260 dev_dbg(&port->
dev,
"%s - status = %d\n", __func__, status);
262 usb_rcvbulkpipe(port->
serial->dev,
264 port->
read_urb->transfer_buffer, 256,
265 iuu_update_status_callback, port);
275 usb_sndbulkpipe(port->
serial->dev,
278 iuu_status_callback, port);
294 usb_sndbulkpipe(serial->
dev,
296 count, &actual,
HZ * 1);
299 dev_dbg(&port->
dev,
"%s - error = %2x\n", __func__, status);
301 dev_dbg(&port->
dev,
"%s - write OK !\n", __func__);
314 usb_rcvbulkpipe(serial->
dev,
316 count, &actual,
HZ * 1);
319 dev_dbg(&port->
dev,
"%s - error = %2x\n", __func__, status);
321 dev_dbg(&port->
dev,
"%s - read OK !\n", __func__);
326 unsigned int G,
unsigned int B,
u8 f)
336 buf[2] = (R >> 8) & 0xFF;
338 buf[4] = (G >> 8) & 0xFF;
340 buf[6] = (B >> 8) & 0xFF;
342 status = bulk_immediate(port, buf, 8);
345 dev_dbg(&port->
dev,
"%s - led error status = %2x\n", __func__, status);
347 dev_dbg(&port->
dev,
"%s - led OK !\n", __func__);
351 static void iuu_rgbf_fill_buffer(
u8 *buf,
u8 r1,
u8 r2,
u8 g1,
u8 g2,
u8 b1,
364 static void iuu_led_activity_on(
struct urb *urb)
368 char *buf_ptr = port->
write_urb->transfer_buffer;
374 iuu_rgbf_fill_buffer(buf_ptr, 255, 255, 0, 0, 0, 0, 255);
378 usb_sndbulkpipe(port->
serial->dev,
385 static void iuu_led_activity_off(
struct urb *urb)
389 char *buf_ptr = port->
write_urb->transfer_buffer;
395 iuu_rgbf_fill_buffer(buf_ptr, 0, 0, 255, 255, 0, 0, 255);
398 usb_sndbulkpipe(port->
serial->dev,
410 struct iuu_private *priv = usb_get_serial_port_data(port);
423 int frq = (
int)dwFrq;
427 priv->
buf[Count++] = FrqGenAdr << 1;
428 priv->
buf[Count++] = 0x09;
429 priv->
buf[Count++] = 0x00;
431 status = bulk_immediate(port, (
u8 *) priv->
buf, Count);
433 dev_dbg(&port->
dev,
"%s - write error\n", __func__);
436 }
else if (frq == 3579000) {
441 }
else if (frq == 3680000) {
446 }
else if (frq == 6000000) {
452 unsigned int result = 0;
453 unsigned int tmp = 0;
458 unsigned int lP = 2055;
459 unsigned int lDiv = 4;
461 for (lQ = 2; lQ <= 47 && !found; lQ++)
462 for (lP = 2055; lP >= 8 && !found; lP--)
463 for (lDiv = 4; lDiv <= 127 && !found; lDiv++) {
464 tmp = (12000000 / lDiv) * (lP / lQ);
465 if (
abs((
int)(tmp - frq)) <
466 abs((
int)(frq - result))) {
467 check2 = (12000000 / lQ);
470 check = (12000000 / lQ) * lP;
471 if (check > 400000000)
473 if (check < 100000000)
475 if (lDiv < 4 || lDiv > 127)
486 P2 = ((P - PO) / 2) - 4;
489 PBmsb = (P2 >> 8 & 0x03);
491 PO = (P >> 10) & 0x01;
495 priv->
buf[Count++] = FrqGenAdr << 1;
496 priv->
buf[Count++] = 0x09;
497 priv->
buf[Count++] = 0x20;
499 priv->
buf[Count++] = FrqGenAdr << 1;
500 priv->
buf[Count++] = 0x0C;
501 priv->
buf[Count++] = DIV;
503 priv->
buf[Count++] = FrqGenAdr << 1;
504 priv->
buf[Count++] = 0x12;
505 priv->
buf[Count++] = XDRV;
507 priv->
buf[Count++] = FrqGenAdr << 1;
508 priv->
buf[Count++] = 0x13;
509 priv->
buf[Count++] = 0x6B;
511 priv->
buf[Count++] = FrqGenAdr << 1;
512 priv->
buf[Count++] = 0x40;
513 priv->
buf[Count++] = (0xC0 | ((PUMP & 0x07) << 2)) |
516 priv->
buf[Count++] = FrqGenAdr << 1;
517 priv->
buf[Count++] = 0x41;
518 priv->
buf[Count++] = PBlsb;
520 priv->
buf[Count++] = FrqGenAdr << 1;
521 priv->
buf[Count++] = 0x42;
522 priv->
buf[Count++] = Q | (((PO & 0x01) << 7));
524 priv->
buf[Count++] = FrqGenAdr << 1;
525 priv->
buf[Count++] = 0x44;
526 priv->
buf[Count++] = (
char)0xFF;
528 priv->
buf[Count++] = FrqGenAdr << 1;
529 priv->
buf[Count++] = 0x45;
530 priv->
buf[Count++] = (
char)0xFE;
532 priv->
buf[Count++] = FrqGenAdr << 1;
533 priv->
buf[Count++] = 0x46;
534 priv->
buf[Count++] = 0x7F;
536 priv->
buf[Count++] = FrqGenAdr << 1;
537 priv->
buf[Count++] = 0x47;
538 priv->
buf[Count++] = (
char)0x84;
540 status = bulk_immediate(port, (
u8 *) priv->
buf, Count);
542 dev_dbg(&port->
dev,
"%s - write error\n", __func__);
552 struct iuu_private *priv = usb_get_serial_port_data(port);
554 if (iuu_led(port, 0xF000, 0, 0, 0xFF) < 0)
557 for (i = 0; i < 2; i++) {
558 status = bulk_immediate(port, &rxcmd, 1);
560 dev_dbg(dev,
"%s - uart_flush_write error\n", __func__);
564 status = read_immediate(port, &priv->
len, 1);
566 dev_dbg(dev,
"%s - uart_flush_read error\n", __func__);
571 dev_dbg(dev,
"%s - uart_flush datalen is : %i\n", __func__, priv->
len);
572 status = read_immediate(port, priv->
buf, priv->
len);
574 dev_dbg(dev,
"%s - uart_flush_read error\n", __func__);
579 dev_dbg(dev,
"%s - uart_flush_read OK!\n", __func__);
580 iuu_led(port, 0, 0xF000, 0, 0xFF);
584 static void read_buf_callback(
struct urb *urb)
587 unsigned char *
data = urb->transfer_buffer;
589 int status = urb->status;
598 dev_dbg(&port->
dev,
"%s - %i chars to write\n", __func__, urb->actual_length);
601 dev_dbg(&port->
dev,
"%s - data is NULL !!!\n", __func__);
602 if (tty && urb->actual_length && data) {
603 tty_insert_flip_string(tty, data, urb->actual_length);
607 iuu_led_activity_on(urb);
612 struct iuu_private *priv = usb_get_serial_port_data(port);
616 char *buf_ptr = port->
write_urb->transfer_buffer;
626 spin_unlock_irqrestore(&priv->
lock, flags);
627 dev_dbg(&port->
dev,
"%s - writing %i chars : %*ph\n", __func__,
628 buf_len, buf_len, buf_ptr);
630 usb_sndbulkpipe(port->
serial->dev,
632 port->
write_urb->transfer_buffer, buf_len + 3,
644 usb_rcvbulkpipe(port->
serial->dev,
646 port->
read_urb->transfer_buffer, len,
647 read_buf_callback, port);
652 static void iuu_uart_read_callback(
struct urb *urb)
655 struct iuu_private *priv = usb_get_serial_port_data(port);
657 int status = urb->status;
660 unsigned char *data = urb->transfer_buffer;
664 dev_dbg(&port->
dev,
"%s - status = %d\n", __func__, status);
669 dev_dbg(&port->
dev,
"%s - data is NULL !!!\n", __func__);
671 if (urb->actual_length == 1 && data !=
NULL)
674 if (urb->actual_length > 1) {
675 dev_dbg(&port->
dev,
"%s - urb->actual_length = %i\n", __func__,
682 if (len > 0 && error == 0) {
683 dev_dbg(&port->
dev,
"%s - call read buf - len to read is %i\n",
685 status = iuu_read_buf(port, len);
689 if (priv->
poll > 99) {
690 status = iuu_status(port);
697 if (priv->
reset == 1) {
698 status = iuu_reset(port, 0xC);
704 spin_unlock_irqrestore(&priv->
lock, flags);
705 status = iuu_bulk_write(port);
708 spin_unlock_irqrestore(&priv->
lock, flags);
710 dev_dbg(&port->
dev,
"%s - rxcmd recall\n", __func__);
711 iuu_led_activity_off(urb);
715 const u8 *buf,
int count)
717 struct iuu_private *priv = usb_get_serial_port_data(port);
728 spin_unlock_irqrestore(&priv->
lock, flags);
733 static void read_rxcmd_callback(
struct urb *urb)
737 int status = urb->status;
745 usb_rcvbulkpipe(port->
serial->dev,
747 port->
read_urb->transfer_buffer, 256,
748 iuu_uart_read_callback, port);
750 dev_dbg(&port->
dev,
"%s - submit result = %d\n", __func__, result);
768 status = bulk_immediate(port, buf, 4);
770 dev_dbg(&port->
dev,
"%s - uart_on error\n", __func__);
771 goto uart_enable_failed;
774 status = iuu_uart_flush(port);
776 dev_dbg(&port->
dev,
"%s - uart_flush error\n", __func__);
792 status = bulk_immediate(port, buf, 1);
794 dev_dbg(&port->
dev,
"%s - uart_off error\n", __func__);
801 u32 *actual,
u8 parity)
809 unsigned int T1FrekvensHZ = 0;
811 dev_dbg(&port->
dev,
"%s - enter baud_base=%d\n", __func__, baud_base);
819 if (baud < 1200 || baud > 230400) {
825 T1FrekvensHZ = 500000;
830 T1FrekvensHZ = 2000000;
835 T1FrekvensHZ = 6000000;
840 T1FrekvensHZ = 24000000;
843 T1reload = 256 - (
u8) (T1FrekvensHZ / (baud * 2));
849 dataout[DataCount++] = T1Frekvens;
850 dataout[DataCount++] = T1reload;
852 *actual = (T1FrekvensHZ / (256 - T1reload)) / 2;
854 switch (parity & 0x0F) {
856 dataout[DataCount++] = 0x00;
859 dataout[DataCount++] = 0x01;
862 dataout[DataCount++] = 0x02;
865 dataout[DataCount++] = 0x03;
868 dataout[DataCount++] = 0x04;
876 switch (parity & 0xF0) {
877 case IUU_ONE_STOP_BIT:
890 status = bulk_immediate(port, dataout, DataCount);
892 dev_dbg(&port->
dev,
"%s - uart_off error\n", __func__);
897 static void iuu_set_termios(
struct tty_struct *tty,
901 struct iuu_private *priv = usb_get_serial_port_data(port);
902 unsigned int cflag = tty->termios.c_cflag;
908 u32 newval = cflag & supported_mask;
911 baud = tty->termios.c_ospeed;
913 dev_dbg(&port->
dev,
"%s - enter c_ospeed or baud=%d\n", __func__, baud);
922 }
else if (!(cflag &
PARENB)) {
925 }
else if (cflag &
PARODD)
933 status = iuu_uart_baud(port,
934 baud * priv->
boost / 100,
947 tty->termios.c_cflag &= ~(supported_mask|
CSIZE);
948 tty->termios.c_cflag |= newval | csize;
964 dev_dbg(&port->
dev,
"%s - shutting down urbs\n", __func__);
968 iuu_led(port, 0, 0, 0xF000, 0xFF);
972 static void iuu_init_termios(
struct tty_struct *tty)
977 tty->termios.c_ispeed = 9600;
978 tty->termios.c_ospeed = 9600;
979 tty->termios.c_lflag = 0;
980 tty->termios.c_oflag = 0;
981 tty->termios.c_iflag = 0;
992 struct iuu_private *priv = usb_get_serial_port_data(port);
994 baud = tty->termios.c_ospeed;
995 tty->termios.c_ispeed =
baud;
999 dev_dbg(dev,
"%s - baud %d\n", __func__, baud);
1010 #define FISH(a, b, c, d) do { \
1011 result = usb_control_msg(port->serial->dev, \
1012 usb_rcvctrlpipe(port->serial->dev, 0), \
1013 b, a, c, d, buf, 1, 1000); \
1014 dev_dbg(dev, "0x%x:0x%x:0x%x:0x%x %d - %x\n", a, b, c, d, result, \
1015 buf[0]); } while (0);
1017 #define SOUP(a, b, c, d) do { \
1018 result = usb_control_msg(port->serial->dev, \
1019 usb_sndctrlpipe(port->serial->dev, 0), \
1020 b, a, c, d, NULL, 0, 1000); \
1021 dev_dbg(dev, "0x%x:0x%x:0x%x:0x%x %d\n", a, b, c, d, result); } while (0)
1028 SOUP(0x03, 0x02, 0x02, 0x0);
1030 iuu_led(port, 0xF000, 0xF000, 0, 0xFF);
1036 switch (clockmode) {
1041 iuu_uart_baud(port, baud *
boost / 100, &actual,
1049 iuu_uart_baud(port, 16457 *
boost / 100, &actual,
1056 iuu_uart_baud(port, baud *
boost / 100, &actual,
1099 iuu_uart_flush(port);
1101 dev_dbg(dev,
"%s - initialization done\n", __func__);
1105 usb_sndbulkpipe(port->
serial->dev,
1108 read_rxcmd_callback, port);
1111 dev_err(dev,
"%s - failed submitting read urb, error %d\n", __func__, result);
1114 dev_dbg(dev,
"%s - rxcmd OK\n", __func__);
1131 buf[1] = vcc & 0xFF;
1132 buf[2] = (vcc >> 8) & 0xFF;
1133 buf[3] = (vcc >> 16) & 0xFF;
1134 buf[4] = (vcc >> 24) & 0xFF;
1136 status = bulk_immediate(port, buf, 5);
1140 dev_dbg(&port->
dev,
"%s - vcc error status = %2x\n", __func__, status);
1142 dev_dbg(&port->
dev,
"%s - vcc OK !\n", __func__);
1155 struct iuu_private *priv = usb_get_serial_port_data(port);
1164 struct iuu_private *priv = usb_get_serial_port_data(port);
1168 dev_err(dev,
"%s - vcc_mode: %s is not a unsigned long\n",
1170 goto fail_store_vcc_mode;
1173 dev_dbg(dev,
"%s: setting vcc_mode = %ld", __func__, v);
1175 if ((v != 3) && (v != 5)) {
1176 dev_err(dev,
"%s - vcc_mode %ld is invalid\n", __func__, v);
1178 iuu_vcc_set(port, v);
1181 fail_store_vcc_mode:
1206 .name =
"iuu_phoenix",
1210 .bulk_in_size = 512,
1211 .bulk_out_size = 512,
1214 .write = iuu_uart_write,
1215 .read_bulk_callback = iuu_uart_read_callback,
1216 .tiocmget = iuu_tiocmget,
1217 .tiocmset = iuu_tiocmset,
1218 .set_termios = iuu_set_termios,
1219 .init_termios = iuu_init_termios,
1220 .port_probe = iuu_port_probe,
1221 .port_remove = iuu_port_remove,
1249 "4=!DSR, 5=CTS, 6=!CTS, 7=RING, 8=!RING)");
1253 "for 5V). Default to 5.");