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_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;
555 struct iuu_private *priv = usb_get_serial_port_data(port);
557 if (iuu_led(port, 0xF000, 0, 0, 0xFF) < 0)
560 rxcmd = kmalloc(1, GFP_KERNEL);
564 rxcmd[0] = IUU_UART_RX;
566 for (i = 0; i < 2; i++) {
567 status = bulk_immediate(port, rxcmd, 1);
568 if (status != IUU_OPERATION_OK) {
569 dev_dbg(dev, "%s - uart_flush_write error\n", __func__);
573 status = read_immediate(port, &priv->len, 1);
574 if (status != IUU_OPERATION_OK) {
575 dev_dbg(dev, "%s - uart_flush_read error\n", __func__);
580 dev_dbg(dev, "%s - uart_flush datalen is : %i\n", __func__, priv->len);
581 status = read_immediate(port, priv->buf, priv->len);
582 if (status != IUU_OPERATION_OK) {
583 dev_dbg(dev, "%s - uart_flush_read error\n", __func__);
588 dev_dbg(dev, "%s - uart_flush_read OK!\n", __func__);
589 iuu_led(port, 0, 0xF000, 0, 0xFF);
597 static void read_buf_callback(struct urb *urb)
599 struct usb_serial_port *port = urb->context;
600 unsigned char *data = urb->transfer_buffer;
601 int status = urb->status;
604 if (status == -EPROTO) {
605 /* reschedule needed */
610 dev_dbg(&port->dev, "%s - %i chars to write\n", __func__, urb->actual_length);
612 dev_dbg(&port->dev, "%s - data is NULL !!!\n", __func__);
613 if (urb->actual_length && data) {
614 tty_insert_flip_string(&port->port, data, urb->actual_length);
615 tty_flip_buffer_push(&port->port);
617 iuu_led_activity_on(urb);
620 static int iuu_bulk_write(struct usb_serial_port *port)
622 struct iuu_private *priv = usb_get_serial_port_data(port);
626 char *buf_ptr = port->write_urb->transfer_buffer;
628 spin_lock_irqsave(&priv->lock, flags);
629 *buf_ptr++ = IUU_UART_ESC;
630 *buf_ptr++ = IUU_UART_TX;
631 *buf_ptr++ = priv->writelen;
633 memcpy(buf_ptr, priv->writebuf, priv->writelen);
634 buf_len = priv->writelen;
636 spin_unlock_irqrestore(&priv->lock, flags);
637 dev_dbg(&port->dev, "%s - writing %i chars : %*ph\n", __func__,
638 buf_len, buf_len, buf_ptr);
639 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
640 usb_sndbulkpipe(port->serial->dev,
641 port->bulk_out_endpointAddress),
642 port->write_urb->transfer_buffer, buf_len + 3,
644 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
645 usb_serial_port_softint(port);
649 static int iuu_read_buf(struct usb_serial_port *port, int len)
653 usb_fill_bulk_urb(port->read_urb, port->serial->dev,
654 usb_rcvbulkpipe(port->serial->dev,
655 port->bulk_in_endpointAddress),
656 port->read_urb->transfer_buffer, len,
657 read_buf_callback, port);
658 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
662 static void iuu_uart_read_callback(struct urb *urb)
664 struct usb_serial_port *port = urb->context;
665 struct iuu_private *priv = usb_get_serial_port_data(port);
667 int status = urb->status;
670 unsigned char *data = urb->transfer_buffer;
674 dev_dbg(&port->dev, "%s - status = %d\n", __func__, status);
679 dev_dbg(&port->dev, "%s - data is NULL !!!\n", __func__);
681 if (urb->actual_length == 1 && data != NULL)
684 if (urb->actual_length > 1) {
685 dev_dbg(&port->dev, "%s - urb->actual_length = %i\n", __func__,
690 /* if len > 0 call readbuf */
692 if (len > 0 && error == 0) {
693 dev_dbg(&port->dev, "%s - call read buf - len to read is %i\n",
695 status = iuu_read_buf(port, len);
698 /* need to update status ? */
699 if (priv->poll > 99) {
700 status = iuu_status(port);
705 /* reset waiting ? */
707 if (priv->reset == 1) {
708 status = iuu_reset(port, 0xC);
711 /* Writebuf is waiting */
712 spin_lock_irqsave(&priv->lock, flags);
713 if (priv->writelen > 0) {
714 spin_unlock_irqrestore(&priv->lock, flags);
715 status = iuu_bulk_write(port);
718 spin_unlock_irqrestore(&priv->lock, flags);
719 /* if nothing to write call again rxcmd */
720 dev_dbg(&port->dev, "%s - rxcmd recall\n", __func__);
721 iuu_led_activity_off(urb);
724 static int iuu_uart_write(struct tty_struct *tty, struct usb_serial_port *port,
725 const u8 *buf, int count)
727 struct iuu_private *priv = usb_get_serial_port_data(port);
730 spin_lock_irqsave(&priv->lock, flags);
732 count = min(count, 256 - priv->writelen);
736 /* fill the buffer */
737 memcpy(priv->writebuf + priv->writelen, buf, count);
738 priv->writelen += count;
740 spin_unlock_irqrestore(&priv->lock, flags);
745 static void read_rxcmd_callback(struct urb *urb)
747 struct usb_serial_port *port = urb->context;
749 int status = urb->status;
756 usb_fill_bulk_urb(port->read_urb, port->serial->dev,
757 usb_rcvbulkpipe(port->serial->dev,
758 port->bulk_in_endpointAddress),
759 port->read_urb->transfer_buffer, 256,
760 iuu_uart_read_callback, port);
761 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
762 dev_dbg(&port->dev, "%s - submit result = %d\n", __func__, result);
765 static int iuu_uart_on(struct usb_serial_port *port)
770 buf = kmalloc(sizeof(u8) * 4, GFP_KERNEL);
775 buf[0] = IUU_UART_ENABLE;
776 buf[1] = (u8) ((IUU_BAUD_9600 >> 8) & 0x00FF);
777 buf[2] = (u8) (0x00FF & IUU_BAUD_9600);
778 buf[3] = (u8) (0x0F0 & IUU_ONE_STOP_BIT) | (0x07 & IUU_PARITY_EVEN);
780 status = bulk_immediate(port, buf, 4);
781 if (status != IUU_OPERATION_OK) {
782 dev_dbg(&port->dev, "%s - uart_on error\n", __func__);
783 goto uart_enable_failed;
785 /* iuu_reset() the card after iuu_uart_on() */
786 status = iuu_uart_flush(port);
787 if (status != IUU_OPERATION_OK)
788 dev_dbg(&port->dev, "%s - uart_flush error\n", __func__);
794 /* Disables the IUU UART (a.k.a. the Phoenix voiderface) */
795 static int iuu_uart_off(struct usb_serial_port *port)
799 buf = kmalloc(1, GFP_KERNEL);
802 buf[0] = IUU_UART_DISABLE;
804 status = bulk_immediate(port, buf, 1);
805 if (status != IUU_OPERATION_OK)
806 dev_dbg(&port->dev, "%s - uart_off error\n", __func__);
812 static int iuu_uart_baud(struct usb_serial_port *port, u32 baud_base,
813 u32 *actual, u8 parity)
821 unsigned int T1FrekvensHZ = 0;
823 dev_dbg(&port->dev, "%s - enter baud_base=%d\n", __func__, baud_base);
824 dataout = kmalloc(sizeof(u8) * 5, GFP_KERNEL);
828 /*baud = (((priv->clk / 35) * baud_base) / 100000); */
831 if (baud < 1200 || baud > 230400) {
833 return IUU_INVALID_PARAMETER;
837 T1FrekvensHZ = 500000;
842 T1FrekvensHZ = 2000000;
847 T1FrekvensHZ = 6000000;
852 T1FrekvensHZ = 24000000;
855 T1reload = 256 - (u8) (T1FrekvensHZ / (baud * 2));
857 /* magic number here: ENTER_FIRMWARE_UPDATE; */
858 dataout[DataCount++] = IUU_UART_ESC;
859 /* magic number here: CHANGE_BAUD; */
860 dataout[DataCount++] = IUU_UART_CHANGE;
861 dataout[DataCount++] = T1Frekvens;
862 dataout[DataCount++] = T1reload;
864 *actual = (T1FrekvensHZ / (256 - T1reload)) / 2;
866 switch (parity & 0x0F) {
867 case IUU_PARITY_NONE:
868 dataout[DataCount++] = 0x00;
870 case IUU_PARITY_EVEN:
871 dataout[DataCount++] = 0x01;
874 dataout[DataCount++] = 0x02;
876 case IUU_PARITY_MARK:
877 dataout[DataCount++] = 0x03;
879 case IUU_PARITY_SPACE:
880 dataout[DataCount++] = 0x04;
884 return IUU_INVALID_PARAMETER;
888 switch (parity & 0xF0) {
889 case IUU_ONE_STOP_BIT:
890 dataout[DataCount - 1] |= IUU_ONE_STOP_BIT;
893 case IUU_TWO_STOP_BITS:
894 dataout[DataCount - 1] |= IUU_TWO_STOP_BITS;
898 return IUU_INVALID_PARAMETER;
902 status = bulk_immediate(port, dataout, DataCount);
903 if (status != IUU_OPERATION_OK)
904 dev_dbg(&port->dev, "%s - uart_off error\n", __func__);
909 static void iuu_set_termios(struct tty_struct *tty,
910 struct usb_serial_port *port, struct ktermios *old_termios)
912 const u32 supported_mask = CMSPAR|PARENB|PARODD;
913 struct iuu_private *priv = usb_get_serial_port_data(port);
914 unsigned int cflag = tty->termios.c_cflag;
920 u32 newval = cflag & supported_mask;
922 /* Just use the ospeed. ispeed should be the same. */
923 baud = tty->termios.c_ospeed;
925 dev_dbg(&port->dev, "%s - enter c_ospeed or baud=%d\n", __func__, baud);
927 /* compute the parity parameter */
929 if (cflag & CMSPAR) { /* Using mark space */
931 parity |= IUU_PARITY_SPACE;
933 parity |= IUU_PARITY_MARK;
934 } else if (!(cflag & PARENB)) {
935 parity |= IUU_PARITY_NONE;
937 } else if (cflag & PARODD)
938 parity |= IUU_PARITY_ODD;
940 parity |= IUU_PARITY_EVEN;
942 parity |= (cflag & CSTOPB ? IUU_TWO_STOP_BITS : IUU_ONE_STOP_BIT);
945 status = iuu_uart_baud(port,
946 baud * priv->boost / 100,
949 /* set the termios value to the real one, so the user now what has
950 * changed. We support few fields so its easies to copy the old hw
951 * settings back over and then adjust them
954 tty_termios_copy_hw(&tty->termios, old_termios);
955 if (status != 0) /* Set failed - return old bits */
957 /* Re-encode speed, parity and csize */
958 tty_encode_baud_rate(tty, baud, baud);
959 tty->termios.c_cflag &= ~(supported_mask|CSIZE);
960 tty->termios.c_cflag |= newval | csize;
963 static void iuu_close(struct usb_serial_port *port)
965 /* iuu_led (port,255,0,0,0); */
969 usb_kill_urb(port->write_urb);
970 usb_kill_urb(port->read_urb);
972 iuu_led(port, 0, 0, 0xF000, 0xFF);
975 static void iuu_init_termios(struct tty_struct *tty)
977 tty->termios = tty_std_termios;
978 tty->termios.c_cflag = CLOCAL | CREAD | CS8 | B9600
979 | TIOCM_CTS | CSTOPB | PARENB;
980 tty->termios.c_ispeed = 9600;
981 tty->termios.c_ospeed = 9600;
982 tty->termios.c_lflag = 0;
983 tty->termios.c_oflag = 0;
984 tty->termios.c_iflag = 0;
987 static int iuu_open(struct tty_struct *tty, struct usb_serial_port *port)
989 struct usb_serial *serial = port->serial;
990 struct device *dev = &port->dev;
995 struct iuu_private *priv = usb_get_serial_port_data(port);
997 baud = tty->termios.c_ospeed;
998 tty->termios.c_ispeed = baud;
999 /* Re-encode speed */
1000 tty_encode_baud_rate(tty, baud, baud);
1002 dev_dbg(dev, "%s - baud %d\n", __func__, baud);
1003 usb_clear_halt(serial->dev, port->write_urb->pipe);
1004 usb_clear_halt(serial->dev, port->read_urb->pipe);
1006 buf = kmalloc(10, GFP_KERNEL);
1012 /* initialize writebuf */
1013 #define FISH(a, b, c, d) do { \
1014 result = usb_control_msg(port->serial->dev, \
1015 usb_rcvctrlpipe(port->serial->dev, 0), \
1016 b, a, c, d, buf, 1, 1000); \
1017 dev_dbg(dev, "0x%x:0x%x:0x%x:0x%x %d - %x\n", a, b, c, d, result, \
1018 buf[0]); } while (0);
1020 #define SOUP(a, b, c, d) do { \
1021 result = usb_control_msg(port->serial->dev, \
1022 usb_sndctrlpipe(port->serial->dev, 0), \
1023 b, a, c, d, NULL, 0, 1000); \
1024 dev_dbg(dev, "0x%x:0x%x:0x%x:0x%x %d\n", a, b, c, d, result); } while (0)
1026 /* This is not UART related but IUU USB driver related or something */
1027 /* like that. Basically no IUU will accept any commands from the USB */
1028 /* host unless it has received the following message */
1029 /* sprintf(buf ,"%c%c%c%c",0x03,0x02,0x02,0x0); */
1031 SOUP(0x03, 0x02, 0x02, 0x0);
1033 iuu_led(port, 0xF000, 0xF000, 0, 0xFF);
1037 priv->boost = boost;
1039 switch (clockmode) {
1040 case 2: /* 3.680 Mhz */
1041 priv->clk = IUU_CLK_3680000;
1042 iuu_clk(port, IUU_CLK_3680000 * boost / 100);
1044 iuu_uart_baud(port, baud * boost / 100, &actual,
1047 case 3: /* 6.00 Mhz */
1048 iuu_clk(port, IUU_CLK_6000000 * boost / 100);
1049 priv->clk = IUU_CLK_6000000;
1050 /* Ratio of 6000000 to 3500000 for baud 9600 */
1052 iuu_uart_baud(port, 16457 * boost / 100, &actual,
1055 default: /* 3.579 Mhz */
1056 iuu_clk(port, IUU_CLK_3579000 * boost / 100);
1057 priv->clk = IUU_CLK_3579000;
1059 iuu_uart_baud(port, baud * boost / 100, &actual,
1063 /* set the cardin cardout signals */
1070 iuu_cardin = TIOCM_CD;
1075 iuu_cardout = TIOCM_CD;
1078 iuu_cardin = TIOCM_DSR;
1083 iuu_cardout = TIOCM_DSR;
1086 iuu_cardin = TIOCM_CTS;
1091 iuu_cardout = TIOCM_CTS;
1094 iuu_cardin = TIOCM_RNG;
1099 iuu_cardout = TIOCM_RNG;
1102 iuu_uart_flush(port);
1104 dev_dbg(dev, "%s - initialization done\n", __func__);
1106 memset(port->write_urb->transfer_buffer, IUU_UART_RX, 1);
1107 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
1108 usb_sndbulkpipe(port->serial->dev,
1109 port->bulk_out_endpointAddress),
1110 port->write_urb->transfer_buffer, 1,
1111 read_rxcmd_callback, port);
1112 result = usb_submit_urb(port->write_urb, GFP_KERNEL);
1114 dev_err(dev, "%s - failed submitting read urb, error %d\n", __func__, result);
1117 dev_dbg(dev, "%s - rxcmd OK\n", __func__);
1123 /* how to change VCC */
1124 static int iuu_vcc_set(struct usb_serial_port *port, unsigned int vcc)
1129 buf = kmalloc(5, GFP_KERNEL);
1133 buf[0] = IUU_SET_VCC;
1134 buf[1] = vcc & 0xFF;
1135 buf[2] = (vcc >> 8) & 0xFF;
1136 buf[3] = (vcc >> 16) & 0xFF;
1137 buf[4] = (vcc >> 24) & 0xFF;
1139 status = bulk_immediate(port, buf, 5);
1142 if (status != IUU_OPERATION_OK)
1143 dev_dbg(&port->dev, "%s - vcc error status = %2x\n", __func__, status);
1145 dev_dbg(&port->dev, "%s - vcc OK !\n", __func__);
1154 static ssize_t vcc_mode_show(struct device *dev,
1155 struct device_attribute *attr, char *buf)
1157 struct usb_serial_port *port = to_usb_serial_port(dev);
1158 struct iuu_private *priv = usb_get_serial_port_data(port);
1160 return sprintf(buf, "%d\n", priv->vcc);
1163 static ssize_t vcc_mode_store(struct device *dev,
1164 struct device_attribute *attr, const char *buf, size_t count)
1166 struct usb_serial_port *port = to_usb_serial_port(dev);
1167 struct iuu_private *priv = usb_get_serial_port_data(port);
1170 if (kstrtoul(buf, 10, &v)) {
1171 dev_err(dev, "%s - vcc_mode: %s is not a unsigned long\n",
1173 goto fail_store_vcc_mode;
1176 dev_dbg(dev, "%s: setting vcc_mode = %ld\n", __func__, v);
1178 if ((v != 3) && (v != 5)) {
1179 dev_err(dev, "%s - vcc_mode %ld is invalid\n", __func__, v);
1181 iuu_vcc_set(port, v);
1184 fail_store_vcc_mode:
1187 static DEVICE_ATTR_RW(vcc_mode);
1189 static int iuu_create_sysfs_attrs(struct usb_serial_port *port)
1191 return device_create_file(&port->dev, &dev_attr_vcc_mode);
1194 static int iuu_remove_sysfs_attrs(struct usb_serial_port *port)
1196 device_remove_file(&port->dev, &dev_attr_vcc_mode);
1201 * End Sysfs Attributes
1204 static struct usb_serial_driver iuu_device = {
1206 .owner = THIS_MODULE,
1207 .name = "iuu_phoenix",
1209 .id_table = id_table,
1211 .bulk_in_size = 512,
1212 .bulk_out_size = 512,
1215 .write = iuu_uart_write,
1216 .read_bulk_callback = iuu_uart_read_callback,
1217 .tiocmget = iuu_tiocmget,
1218 .tiocmset = iuu_tiocmset,
1219 .set_termios = iuu_set_termios,
1220 .init_termios = iuu_init_termios,
1221 .attach = iuu_attach,
1222 .port_probe = iuu_port_probe,
1223 .port_remove = iuu_port_remove,
1226 static struct usb_serial_driver * const serial_drivers[] = {
1230 module_usb_serial_driver(serial_drivers, id_table);
1232 MODULE_AUTHOR("Alain Degreffe eczema@ecze.com");
1234 MODULE_DESCRIPTION(DRIVER_DESC);
1235 MODULE_LICENSE("GPL");
1237 module_param(xmas, bool, S_IRUGO | S_IWUSR);
1238 MODULE_PARM_DESC(xmas, "Xmas colors enabled or not");
1240 module_param(boost, int, S_IRUGO | S_IWUSR);
1241 MODULE_PARM_DESC(boost, "Card overclock boost (in percent 100-500)");
1243 module_param(clockmode, int, S_IRUGO | S_IWUSR);
1244 MODULE_PARM_DESC(clockmode, "Card clock mode (1=3.579 MHz, 2=3.680 MHz, "
1247 module_param(cdmode, int, S_IRUGO | S_IWUSR);
1248 MODULE_PARM_DESC(cdmode, "Card detect mode (0=none, 1=CD, 2=!CD, 3=DSR, "
1249 "4=!DSR, 5=CTS, 6=!CTS, 7=RING, 8=!RING)");
1251 module_param(vcc_default, int, S_IRUGO | S_IWUSR);
1252 MODULE_PARM_DESC(vcc_default, "Set default VCC (either 3 for 3.3V or 5 "
1253 "for 5V). Default to 5.");