2 * Infinity Unlimited USB Phoenix driver
8 * Original code taken from iuutool (Copyright (C) 2006 Juan Carlos Borrás)
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * And tested with help of WB Electronics
18 #include <linux/kernel.h>
19 #include <linux/errno.h>
20 #include <linux/slab.h>
21 #include <linux/tty.h>
22 #include <linux/tty_driver.h>
23 #include <linux/tty_flip.h>
24 #include <linux/serial.h>
25 #include <linux/module.h>
26 #include <linux/moduleparam.h>
27 #include <linux/spinlock.h>
28 #include <linux/uaccess.h>
29 #include <linux/usb.h>
30 #include <linux/usb/serial.h>
31 #include "iuu_phoenix.h"
32 #include <linux/random.h>
34 #define DRIVER_DESC "Infinity USB Unlimited Phoenix driver"
36 static const struct usb_device_id id_table[] = {
37 {USB_DEVICE(IUU_USB_VENDOR_ID, IUU_USB_PRODUCT_ID)},
38 {} /* Terminating entry */
40 MODULE_DEVICE_TABLE(usb, id_table);
43 static int boost = 100;
44 static int clockmode = 1;
45 static int cdmode = 1;
46 static int iuu_cardin;
47 static int iuu_cardout;
49 static int vcc_default = 5;
51 static int iuu_create_sysfs_attrs(struct usb_serial_port *port);
52 static int iuu_remove_sysfs_attrs(struct usb_serial_port *port);
53 static void read_rxcmd_callback(struct urb *urb);
56 spinlock_t lock; /* store irq state */
58 int tiostatus; /* store IUART SIGNAL for tiocmget call */
59 u8 reset; /* if 1 reset is needed */
60 int poll; /* number of poll */
61 u8 *writebuf; /* buffer for writing to device */
62 int writelen; /* num of byte to write to device */
63 u8 *buf; /* used for initialize speed */
65 int vcc; /* vcc (either 3 or 5 V) */
71 static int iuu_attach(struct usb_serial *serial)
73 unsigned char num_ports = serial->num_ports;
75 if (serial->num_bulk_in < num_ports || serial->num_bulk_out < num_ports)
81 static int iuu_port_probe(struct usb_serial_port *port)
83 struct iuu_private *priv;
86 priv = kzalloc(sizeof(struct iuu_private), GFP_KERNEL);
90 priv->buf = kzalloc(256, GFP_KERNEL);
96 priv->writebuf = kzalloc(256, GFP_KERNEL);
97 if (!priv->writebuf) {
103 priv->vcc = vcc_default;
104 spin_lock_init(&priv->lock);
106 usb_set_serial_port_data(port, priv);
108 ret = iuu_create_sysfs_attrs(port);
110 kfree(priv->writebuf);
119 static int iuu_port_remove(struct usb_serial_port *port)
121 struct iuu_private *priv = usb_get_serial_port_data(port);
123 iuu_remove_sysfs_attrs(port);
124 kfree(priv->writebuf);
131 static int iuu_tiocmset(struct tty_struct *tty,
132 unsigned int set, unsigned int clear)
134 struct usb_serial_port *port = tty->driver_data;
135 struct iuu_private *priv = usb_get_serial_port_data(port);
138 /* FIXME: locking on tiomstatus */
139 dev_dbg(&port->dev, "%s msg : SET = 0x%04x, CLEAR = 0x%04x\n",
140 __func__, set, clear);
142 spin_lock_irqsave(&priv->lock, flags);
144 if ((set & TIOCM_RTS) && !(priv->tiostatus == TIOCM_RTS)) {
145 dev_dbg(&port->dev, "%s TIOCMSET RESET called !!!\n", __func__);
149 priv->tiostatus = TIOCM_RTS;
151 spin_unlock_irqrestore(&priv->lock, flags);
155 /* This is used to provide a carrier detect mechanism
156 * When a card is present, the response is 0x00
157 * When no card , the reader respond with TIOCM_CD
158 * This is known as CD autodetect mechanism
160 static int iuu_tiocmget(struct tty_struct *tty)
162 struct usb_serial_port *port = tty->driver_data;
163 struct iuu_private *priv = usb_get_serial_port_data(port);
167 spin_lock_irqsave(&priv->lock, flags);
168 rc = priv->tiostatus;
169 spin_unlock_irqrestore(&priv->lock, flags);
174 static void iuu_rxcmd(struct urb *urb)
176 struct usb_serial_port *port = urb->context;
178 int status = urb->status;
181 dev_dbg(&port->dev, "%s - status = %d\n", __func__, status);
187 memset(port->write_urb->transfer_buffer, IUU_UART_RX, 1);
188 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
189 usb_sndbulkpipe(port->serial->dev,
190 port->bulk_out_endpointAddress),
191 port->write_urb->transfer_buffer, 1,
192 read_rxcmd_callback, port);
193 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
196 static int iuu_reset(struct usb_serial_port *port, u8 wt)
198 struct iuu_private *priv = usb_get_serial_port_data(port);
200 char *buf_ptr = port->write_urb->transfer_buffer;
202 /* Prepare the reset sequence */
204 *buf_ptr++ = IUU_RST_SET;
205 *buf_ptr++ = IUU_DELAY_MS;
207 *buf_ptr = IUU_RST_CLEAR;
209 /* send the sequence */
211 usb_fill_bulk_urb(port->write_urb,
213 usb_sndbulkpipe(port->serial->dev,
214 port->bulk_out_endpointAddress),
215 port->write_urb->transfer_buffer, 4, iuu_rxcmd, port);
216 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
227 static void iuu_update_status_callback(struct urb *urb)
229 struct usb_serial_port *port = urb->context;
230 struct iuu_private *priv = usb_get_serial_port_data(port);
232 int status = urb->status;
235 dev_dbg(&port->dev, "%s - status = %d\n", __func__, status);
240 st = urb->transfer_buffer;
241 dev_dbg(&port->dev, "%s - enter\n", __func__);
242 if (urb->actual_length == 1) {
245 priv->tiostatus = iuu_cardout;
248 priv->tiostatus = iuu_cardin;
251 priv->tiostatus = iuu_cardin;
257 static void iuu_status_callback(struct urb *urb)
259 struct usb_serial_port *port = urb->context;
261 int status = urb->status;
263 dev_dbg(&port->dev, "%s - status = %d\n", __func__, status);
264 usb_fill_bulk_urb(port->read_urb, port->serial->dev,
265 usb_rcvbulkpipe(port->serial->dev,
266 port->bulk_in_endpointAddress),
267 port->read_urb->transfer_buffer, 256,
268 iuu_update_status_callback, port);
269 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
272 static int iuu_status(struct usb_serial_port *port)
276 memset(port->write_urb->transfer_buffer, IUU_GET_STATE_REGISTER, 1);
277 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
278 usb_sndbulkpipe(port->serial->dev,
279 port->bulk_out_endpointAddress),
280 port->write_urb->transfer_buffer, 1,
281 iuu_status_callback, port);
282 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
287 static int bulk_immediate(struct usb_serial_port *port, u8 *buf, u8 count)
290 struct usb_serial *serial = port->serial;
293 /* send the data out the bulk port */
296 usb_bulk_msg(serial->dev,
297 usb_sndbulkpipe(serial->dev,
298 port->bulk_out_endpointAddress), buf,
299 count, &actual, 1000);
301 if (status != IUU_OPERATION_OK)
302 dev_dbg(&port->dev, "%s - error = %2x\n", __func__, status);
304 dev_dbg(&port->dev, "%s - write OK !\n", __func__);
308 static int read_immediate(struct usb_serial_port *port, u8 *buf, u8 count)
311 struct usb_serial *serial = port->serial;
314 /* send the data out the bulk port */
316 usb_bulk_msg(serial->dev,
317 usb_rcvbulkpipe(serial->dev,
318 port->bulk_in_endpointAddress), buf,
319 count, &actual, 1000);
321 if (status != IUU_OPERATION_OK)
322 dev_dbg(&port->dev, "%s - error = %2x\n", __func__, status);
324 dev_dbg(&port->dev, "%s - read OK !\n", __func__);
328 static int iuu_led(struct usb_serial_port *port, unsigned int R,
329 unsigned int G, unsigned int B, u8 f)
333 buf = kmalloc(8, GFP_KERNEL);
337 buf[0] = IUU_SET_LED;
339 buf[2] = (R >> 8) & 0xFF;
341 buf[4] = (G >> 8) & 0xFF;
343 buf[6] = (B >> 8) & 0xFF;
345 status = bulk_immediate(port, buf, 8);
347 if (status != IUU_OPERATION_OK)
348 dev_dbg(&port->dev, "%s - led error status = %2x\n", __func__, status);
350 dev_dbg(&port->dev, "%s - led OK !\n", __func__);
351 return IUU_OPERATION_OK;
354 static void iuu_rgbf_fill_buffer(u8 *buf, u8 r1, u8 r2, u8 g1, u8 g2, u8 b1,
357 *buf++ = IUU_SET_LED;
367 static void iuu_led_activity_on(struct urb *urb)
369 struct usb_serial_port *port = urb->context;
371 char *buf_ptr = port->write_urb->transfer_buffer;
372 *buf_ptr++ = IUU_SET_LED;
374 get_random_bytes(buf_ptr, 6);
377 iuu_rgbf_fill_buffer(buf_ptr, 255, 255, 0, 0, 0, 0, 255);
380 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
381 usb_sndbulkpipe(port->serial->dev,
382 port->bulk_out_endpointAddress),
383 port->write_urb->transfer_buffer, 8 ,
385 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
388 static void iuu_led_activity_off(struct urb *urb)
390 struct usb_serial_port *port = urb->context;
392 char *buf_ptr = port->write_urb->transfer_buffer;
397 *buf_ptr++ = IUU_SET_LED;
398 iuu_rgbf_fill_buffer(buf_ptr, 0, 0, 255, 255, 0, 0, 255);
400 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
401 usb_sndbulkpipe(port->serial->dev,
402 port->bulk_out_endpointAddress),
403 port->write_urb->transfer_buffer, 8 ,
405 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
410 static int iuu_clk(struct usb_serial_port *port, int dwFrq)
413 struct iuu_private *priv = usb_get_serial_port_data(port);
416 u8 DIV = 0; /* 8bit */
417 u8 XDRV = 0; /* 8bit */
418 u8 PUMP = 0; /* 3bit */
419 u8 PBmsb = 0; /* 2bit */
420 u8 PBlsb = 0; /* 8bit */
421 u8 PO = 0; /* 1bit */
426 int frq = (int)dwFrq;
429 priv->buf[Count++] = IUU_UART_WRITE_I2C;
430 priv->buf[Count++] = FrqGenAdr << 1;
431 priv->buf[Count++] = 0x09;
432 priv->buf[Count++] = 0x00;
434 status = bulk_immediate(port, (u8 *) priv->buf, Count);
436 dev_dbg(&port->dev, "%s - write error\n", __func__);
439 } else if (frq == 3579000) {
444 } else if (frq == 3680000) {
449 } else if (frq == 6000000) {
455 unsigned int result = 0;
456 unsigned int tmp = 0;
461 unsigned int lP = 2055;
462 unsigned int lDiv = 4;
464 for (lQ = 2; lQ <= 47 && !found; lQ++)
465 for (lP = 2055; lP >= 8 && !found; lP--)
466 for (lDiv = 4; lDiv <= 127 && !found; lDiv++) {
467 tmp = (12000000 / lDiv) * (lP / lQ);
468 if (abs((int)(tmp - frq)) <
469 abs((int)(frq - result))) {
470 check2 = (12000000 / lQ);
473 check = (12000000 / lQ) * lP;
474 if (check > 400000000)
476 if (check < 100000000)
478 if (lDiv < 4 || lDiv > 127)
489 P2 = ((P - PO) / 2) - 4;
492 PBmsb = (P2 >> 8 & 0x03);
494 PO = (P >> 10) & 0x01;
497 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
498 priv->buf[Count++] = FrqGenAdr << 1;
499 priv->buf[Count++] = 0x09;
500 priv->buf[Count++] = 0x20; /* Adr = 0x09 */
501 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
502 priv->buf[Count++] = FrqGenAdr << 1;
503 priv->buf[Count++] = 0x0C;
504 priv->buf[Count++] = DIV; /* Adr = 0x0C */
505 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
506 priv->buf[Count++] = FrqGenAdr << 1;
507 priv->buf[Count++] = 0x12;
508 priv->buf[Count++] = XDRV; /* Adr = 0x12 */
509 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
510 priv->buf[Count++] = FrqGenAdr << 1;
511 priv->buf[Count++] = 0x13;
512 priv->buf[Count++] = 0x6B; /* Adr = 0x13 */
513 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
514 priv->buf[Count++] = FrqGenAdr << 1;
515 priv->buf[Count++] = 0x40;
516 priv->buf[Count++] = (0xC0 | ((PUMP & 0x07) << 2)) |
517 (PBmsb & 0x03); /* Adr = 0x40 */
518 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
519 priv->buf[Count++] = FrqGenAdr << 1;
520 priv->buf[Count++] = 0x41;
521 priv->buf[Count++] = PBlsb; /* Adr = 0x41 */
522 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
523 priv->buf[Count++] = FrqGenAdr << 1;
524 priv->buf[Count++] = 0x42;
525 priv->buf[Count++] = Q | (((PO & 0x01) << 7)); /* Adr = 0x42 */
526 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
527 priv->buf[Count++] = FrqGenAdr << 1;
528 priv->buf[Count++] = 0x44;
529 priv->buf[Count++] = (char)0xFF; /* Adr = 0x44 */
530 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
531 priv->buf[Count++] = FrqGenAdr << 1;
532 priv->buf[Count++] = 0x45;
533 priv->buf[Count++] = (char)0xFE; /* Adr = 0x45 */
534 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
535 priv->buf[Count++] = FrqGenAdr << 1;
536 priv->buf[Count++] = 0x46;
537 priv->buf[Count++] = 0x7F; /* Adr = 0x46 */
538 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
539 priv->buf[Count++] = FrqGenAdr << 1;
540 priv->buf[Count++] = 0x47;
541 priv->buf[Count++] = (char)0x84; /* Adr = 0x47 */
543 status = bulk_immediate(port, (u8 *) priv->buf, Count);
544 if (status != IUU_OPERATION_OK)
545 dev_dbg(&port->dev, "%s - write error\n", __func__);
549 static int iuu_uart_flush(struct usb_serial_port *port)
551 struct device *dev = &port->dev;
554 u8 rxcmd = IUU_UART_RX;
555 struct iuu_private *priv = usb_get_serial_port_data(port);
557 if (iuu_led(port, 0xF000, 0, 0, 0xFF) < 0)
560 for (i = 0; i < 2; i++) {
561 status = bulk_immediate(port, &rxcmd, 1);
562 if (status != IUU_OPERATION_OK) {
563 dev_dbg(dev, "%s - uart_flush_write error\n", __func__);
567 status = read_immediate(port, &priv->len, 1);
568 if (status != IUU_OPERATION_OK) {
569 dev_dbg(dev, "%s - uart_flush_read error\n", __func__);
574 dev_dbg(dev, "%s - uart_flush datalen is : %i\n", __func__, priv->len);
575 status = read_immediate(port, priv->buf, priv->len);
576 if (status != IUU_OPERATION_OK) {
577 dev_dbg(dev, "%s - uart_flush_read error\n", __func__);
582 dev_dbg(dev, "%s - uart_flush_read OK!\n", __func__);
583 iuu_led(port, 0, 0xF000, 0, 0xFF);
587 static void read_buf_callback(struct urb *urb)
589 struct usb_serial_port *port = urb->context;
590 unsigned char *data = urb->transfer_buffer;
591 int status = urb->status;
594 if (status == -EPROTO) {
595 /* reschedule needed */
600 dev_dbg(&port->dev, "%s - %i chars to write\n", __func__, urb->actual_length);
602 dev_dbg(&port->dev, "%s - data is NULL !!!\n", __func__);
603 if (urb->actual_length && data) {
604 tty_insert_flip_string(&port->port, data, urb->actual_length);
605 tty_flip_buffer_push(&port->port);
607 iuu_led_activity_on(urb);
610 static int iuu_bulk_write(struct usb_serial_port *port)
612 struct iuu_private *priv = usb_get_serial_port_data(port);
616 char *buf_ptr = port->write_urb->transfer_buffer;
618 spin_lock_irqsave(&priv->lock, flags);
619 *buf_ptr++ = IUU_UART_ESC;
620 *buf_ptr++ = IUU_UART_TX;
621 *buf_ptr++ = priv->writelen;
623 memcpy(buf_ptr, priv->writebuf, priv->writelen);
624 buf_len = priv->writelen;
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);
629 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
630 usb_sndbulkpipe(port->serial->dev,
631 port->bulk_out_endpointAddress),
632 port->write_urb->transfer_buffer, buf_len + 3,
634 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
635 usb_serial_port_softint(port);
639 static int iuu_read_buf(struct usb_serial_port *port, int len)
643 usb_fill_bulk_urb(port->read_urb, port->serial->dev,
644 usb_rcvbulkpipe(port->serial->dev,
645 port->bulk_in_endpointAddress),
646 port->read_urb->transfer_buffer, len,
647 read_buf_callback, port);
648 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
652 static void iuu_uart_read_callback(struct urb *urb)
654 struct usb_serial_port *port = urb->context;
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__,
680 /* if len > 0 call readbuf */
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);
688 /* need to update status ? */
689 if (priv->poll > 99) {
690 status = iuu_status(port);
695 /* reset waiting ? */
697 if (priv->reset == 1) {
698 status = iuu_reset(port, 0xC);
701 /* Writebuf is waiting */
702 spin_lock_irqsave(&priv->lock, flags);
703 if (priv->writelen > 0) {
704 spin_unlock_irqrestore(&priv->lock, flags);
705 status = iuu_bulk_write(port);
708 spin_unlock_irqrestore(&priv->lock, flags);
709 /* if nothing to write call again rxcmd */
710 dev_dbg(&port->dev, "%s - rxcmd recall\n", __func__);
711 iuu_led_activity_off(urb);
714 static int iuu_uart_write(struct tty_struct *tty, struct usb_serial_port *port,
715 const u8 *buf, int count)
717 struct iuu_private *priv = usb_get_serial_port_data(port);
723 spin_lock_irqsave(&priv->lock, flags);
725 /* fill the buffer */
726 memcpy(priv->writebuf + priv->writelen, buf, count);
727 priv->writelen += count;
728 spin_unlock_irqrestore(&priv->lock, flags);
733 static void read_rxcmd_callback(struct urb *urb)
735 struct usb_serial_port *port = urb->context;
737 int status = urb->status;
744 usb_fill_bulk_urb(port->read_urb, port->serial->dev,
745 usb_rcvbulkpipe(port->serial->dev,
746 port->bulk_in_endpointAddress),
747 port->read_urb->transfer_buffer, 256,
748 iuu_uart_read_callback, port);
749 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
750 dev_dbg(&port->dev, "%s - submit result = %d\n", __func__, result);
753 static int iuu_uart_on(struct usb_serial_port *port)
758 buf = kmalloc(sizeof(u8) * 4, GFP_KERNEL);
763 buf[0] = IUU_UART_ENABLE;
764 buf[1] = (u8) ((IUU_BAUD_9600 >> 8) & 0x00FF);
765 buf[2] = (u8) (0x00FF & IUU_BAUD_9600);
766 buf[3] = (u8) (0x0F0 & IUU_ONE_STOP_BIT) | (0x07 & IUU_PARITY_EVEN);
768 status = bulk_immediate(port, buf, 4);
769 if (status != IUU_OPERATION_OK) {
770 dev_dbg(&port->dev, "%s - uart_on error\n", __func__);
771 goto uart_enable_failed;
773 /* iuu_reset() the card after iuu_uart_on() */
774 status = iuu_uart_flush(port);
775 if (status != IUU_OPERATION_OK)
776 dev_dbg(&port->dev, "%s - uart_flush error\n", __func__);
782 /* Disables the IUU UART (a.k.a. the Phoenix voiderface) */
783 static int iuu_uart_off(struct usb_serial_port *port)
787 buf = kmalloc(1, GFP_KERNEL);
790 buf[0] = IUU_UART_DISABLE;
792 status = bulk_immediate(port, buf, 1);
793 if (status != IUU_OPERATION_OK)
794 dev_dbg(&port->dev, "%s - uart_off error\n", __func__);
800 static int iuu_uart_baud(struct usb_serial_port *port, u32 baud_base,
801 u32 *actual, u8 parity)
809 unsigned int T1FrekvensHZ = 0;
811 dev_dbg(&port->dev, "%s - enter baud_base=%d\n", __func__, baud_base);
812 dataout = kmalloc(sizeof(u8) * 5, GFP_KERNEL);
816 /*baud = (((priv->clk / 35) * baud_base) / 100000); */
819 if (baud < 1200 || baud > 230400) {
821 return IUU_INVALID_PARAMETER;
825 T1FrekvensHZ = 500000;
830 T1FrekvensHZ = 2000000;
835 T1FrekvensHZ = 6000000;
840 T1FrekvensHZ = 24000000;
843 T1reload = 256 - (u8) (T1FrekvensHZ / (baud * 2));
845 /* magic number here: ENTER_FIRMWARE_UPDATE; */
846 dataout[DataCount++] = IUU_UART_ESC;
847 /* magic number here: CHANGE_BAUD; */
848 dataout[DataCount++] = IUU_UART_CHANGE;
849 dataout[DataCount++] = T1Frekvens;
850 dataout[DataCount++] = T1reload;
852 *actual = (T1FrekvensHZ / (256 - T1reload)) / 2;
854 switch (parity & 0x0F) {
855 case IUU_PARITY_NONE:
856 dataout[DataCount++] = 0x00;
858 case IUU_PARITY_EVEN:
859 dataout[DataCount++] = 0x01;
862 dataout[DataCount++] = 0x02;
864 case IUU_PARITY_MARK:
865 dataout[DataCount++] = 0x03;
867 case IUU_PARITY_SPACE:
868 dataout[DataCount++] = 0x04;
872 return IUU_INVALID_PARAMETER;
876 switch (parity & 0xF0) {
877 case IUU_ONE_STOP_BIT:
878 dataout[DataCount - 1] |= IUU_ONE_STOP_BIT;
881 case IUU_TWO_STOP_BITS:
882 dataout[DataCount - 1] |= IUU_TWO_STOP_BITS;
886 return IUU_INVALID_PARAMETER;
890 status = bulk_immediate(port, dataout, DataCount);
891 if (status != IUU_OPERATION_OK)
892 dev_dbg(&port->dev, "%s - uart_off error\n", __func__);
897 static void iuu_set_termios(struct tty_struct *tty,
898 struct usb_serial_port *port, struct ktermios *old_termios)
900 const u32 supported_mask = CMSPAR|PARENB|PARODD;
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;
910 /* Just use the ospeed. ispeed should be the same. */
911 baud = tty->termios.c_ospeed;
913 dev_dbg(&port->dev, "%s - enter c_ospeed or baud=%d\n", __func__, baud);
915 /* compute the parity parameter */
917 if (cflag & CMSPAR) { /* Using mark space */
919 parity |= IUU_PARITY_SPACE;
921 parity |= IUU_PARITY_MARK;
922 } else if (!(cflag & PARENB)) {
923 parity |= IUU_PARITY_NONE;
925 } else if (cflag & PARODD)
926 parity |= IUU_PARITY_ODD;
928 parity |= IUU_PARITY_EVEN;
930 parity |= (cflag & CSTOPB ? IUU_TWO_STOP_BITS : IUU_ONE_STOP_BIT);
933 status = iuu_uart_baud(port,
934 baud * priv->boost / 100,
937 /* set the termios value to the real one, so the user now what has
938 * changed. We support few fields so its easies to copy the old hw
939 * settings back over and then adjust them
942 tty_termios_copy_hw(&tty->termios, old_termios);
943 if (status != 0) /* Set failed - return old bits */
945 /* Re-encode speed, parity and csize */
946 tty_encode_baud_rate(tty, baud, baud);
947 tty->termios.c_cflag &= ~(supported_mask|CSIZE);
948 tty->termios.c_cflag |= newval | csize;
951 static void iuu_close(struct usb_serial_port *port)
953 /* iuu_led (port,255,0,0,0); */
957 usb_kill_urb(port->write_urb);
958 usb_kill_urb(port->read_urb);
960 iuu_led(port, 0, 0, 0xF000, 0xFF);
963 static void iuu_init_termios(struct tty_struct *tty)
965 tty->termios = tty_std_termios;
966 tty->termios.c_cflag = CLOCAL | CREAD | CS8 | B9600
967 | TIOCM_CTS | CSTOPB | PARENB;
968 tty->termios.c_ispeed = 9600;
969 tty->termios.c_ospeed = 9600;
970 tty->termios.c_lflag = 0;
971 tty->termios.c_oflag = 0;
972 tty->termios.c_iflag = 0;
975 static int iuu_open(struct tty_struct *tty, struct usb_serial_port *port)
977 struct usb_serial *serial = port->serial;
978 struct device *dev = &port->dev;
982 struct iuu_private *priv = usb_get_serial_port_data(port);
984 baud = tty->termios.c_ospeed;
985 tty->termios.c_ispeed = baud;
986 /* Re-encode speed */
987 tty_encode_baud_rate(tty, baud, baud);
989 dev_dbg(dev, "%s - baud %d\n", __func__, baud);
990 usb_clear_halt(serial->dev, port->write_urb->pipe);
991 usb_clear_halt(serial->dev, port->read_urb->pipe);
995 #define SOUP(a, b, c, d) do { \
996 result = usb_control_msg(port->serial->dev, \
997 usb_sndctrlpipe(port->serial->dev, 0), \
998 b, a, c, d, NULL, 0, 1000); \
999 dev_dbg(dev, "0x%x:0x%x:0x%x:0x%x %d\n", a, b, c, d, result); } while (0)
1001 /* This is not UART related but IUU USB driver related or something */
1002 /* like that. Basically no IUU will accept any commands from the USB */
1003 /* host unless it has received the following message */
1004 /* sprintf(buf ,"%c%c%c%c",0x03,0x02,0x02,0x0); */
1006 SOUP(0x03, 0x02, 0x02, 0x0);
1008 iuu_led(port, 0xF000, 0xF000, 0, 0xFF);
1012 priv->boost = boost;
1014 switch (clockmode) {
1015 case 2: /* 3.680 Mhz */
1016 priv->clk = IUU_CLK_3680000;
1017 iuu_clk(port, IUU_CLK_3680000 * boost / 100);
1019 iuu_uart_baud(port, baud * boost / 100, &actual,
1022 case 3: /* 6.00 Mhz */
1023 iuu_clk(port, IUU_CLK_6000000 * boost / 100);
1024 priv->clk = IUU_CLK_6000000;
1025 /* Ratio of 6000000 to 3500000 for baud 9600 */
1027 iuu_uart_baud(port, 16457 * boost / 100, &actual,
1030 default: /* 3.579 Mhz */
1031 iuu_clk(port, IUU_CLK_3579000 * boost / 100);
1032 priv->clk = IUU_CLK_3579000;
1034 iuu_uart_baud(port, baud * boost / 100, &actual,
1038 /* set the cardin cardout signals */
1045 iuu_cardin = TIOCM_CD;
1050 iuu_cardout = TIOCM_CD;
1053 iuu_cardin = TIOCM_DSR;
1058 iuu_cardout = TIOCM_DSR;
1061 iuu_cardin = TIOCM_CTS;
1066 iuu_cardout = TIOCM_CTS;
1069 iuu_cardin = TIOCM_RNG;
1074 iuu_cardout = TIOCM_RNG;
1077 iuu_uart_flush(port);
1079 dev_dbg(dev, "%s - initialization done\n", __func__);
1081 memset(port->write_urb->transfer_buffer, IUU_UART_RX, 1);
1082 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
1083 usb_sndbulkpipe(port->serial->dev,
1084 port->bulk_out_endpointAddress),
1085 port->write_urb->transfer_buffer, 1,
1086 read_rxcmd_callback, port);
1087 result = usb_submit_urb(port->write_urb, GFP_KERNEL);
1089 dev_err(dev, "%s - failed submitting read urb, error %d\n", __func__, result);
1092 dev_dbg(dev, "%s - rxcmd OK\n", __func__);
1098 /* how to change VCC */
1099 static int iuu_vcc_set(struct usb_serial_port *port, unsigned int vcc)
1104 buf = kmalloc(5, GFP_KERNEL);
1108 buf[0] = IUU_SET_VCC;
1109 buf[1] = vcc & 0xFF;
1110 buf[2] = (vcc >> 8) & 0xFF;
1111 buf[3] = (vcc >> 16) & 0xFF;
1112 buf[4] = (vcc >> 24) & 0xFF;
1114 status = bulk_immediate(port, buf, 5);
1117 if (status != IUU_OPERATION_OK)
1118 dev_dbg(&port->dev, "%s - vcc error status = %2x\n", __func__, status);
1120 dev_dbg(&port->dev, "%s - vcc OK !\n", __func__);
1129 static ssize_t vcc_mode_show(struct device *dev,
1130 struct device_attribute *attr, char *buf)
1132 struct usb_serial_port *port = to_usb_serial_port(dev);
1133 struct iuu_private *priv = usb_get_serial_port_data(port);
1135 return sprintf(buf, "%d\n", priv->vcc);
1138 static ssize_t vcc_mode_store(struct device *dev,
1139 struct device_attribute *attr, const char *buf, size_t count)
1141 struct usb_serial_port *port = to_usb_serial_port(dev);
1142 struct iuu_private *priv = usb_get_serial_port_data(port);
1145 if (kstrtoul(buf, 10, &v)) {
1146 dev_err(dev, "%s - vcc_mode: %s is not a unsigned long\n",
1148 goto fail_store_vcc_mode;
1151 dev_dbg(dev, "%s: setting vcc_mode = %ld\n", __func__, v);
1153 if ((v != 3) && (v != 5)) {
1154 dev_err(dev, "%s - vcc_mode %ld is invalid\n", __func__, v);
1156 iuu_vcc_set(port, v);
1159 fail_store_vcc_mode:
1162 static DEVICE_ATTR_RW(vcc_mode);
1164 static int iuu_create_sysfs_attrs(struct usb_serial_port *port)
1166 return device_create_file(&port->dev, &dev_attr_vcc_mode);
1169 static int iuu_remove_sysfs_attrs(struct usb_serial_port *port)
1171 device_remove_file(&port->dev, &dev_attr_vcc_mode);
1176 * End Sysfs Attributes
1179 static struct usb_serial_driver iuu_device = {
1181 .owner = THIS_MODULE,
1182 .name = "iuu_phoenix",
1184 .id_table = id_table,
1186 .bulk_in_size = 512,
1187 .bulk_out_size = 512,
1190 .write = iuu_uart_write,
1191 .read_bulk_callback = iuu_uart_read_callback,
1192 .tiocmget = iuu_tiocmget,
1193 .tiocmset = iuu_tiocmset,
1194 .set_termios = iuu_set_termios,
1195 .init_termios = iuu_init_termios,
1196 .attach = iuu_attach,
1197 .port_probe = iuu_port_probe,
1198 .port_remove = iuu_port_remove,
1201 static struct usb_serial_driver * const serial_drivers[] = {
1205 module_usb_serial_driver(serial_drivers, id_table);
1209 MODULE_DESCRIPTION(DRIVER_DESC);
1210 MODULE_LICENSE("GPL");
1212 module_param(xmas, bool, S_IRUGO | S_IWUSR);
1213 MODULE_PARM_DESC(xmas, "Xmas colors enabled or not");
1215 module_param(boost, int, S_IRUGO | S_IWUSR);
1216 MODULE_PARM_DESC(boost, "Card overclock boost (in percent 100-500)");
1218 module_param(clockmode, int, S_IRUGO | S_IWUSR);
1219 MODULE_PARM_DESC(clockmode, "Card clock mode (1=3.579 MHz, 2=3.680 MHz, "
1222 module_param(cdmode, int, S_IRUGO | S_IWUSR);
1223 MODULE_PARM_DESC(cdmode, "Card detect mode (0=none, 1=CD, 2=!CD, 3=DSR, "
1224 "4=!DSR, 5=CTS, 6=!CTS, 7=RING, 8=!RING)");
1226 module_param(vcc_default, int, S_IRUGO | S_IWUSR);
1227 MODULE_PARM_DESC(vcc_default, "Set default VCC (either 3 for 3.3V or 5 "
1228 "for 5V). Default to 5.");