2 * Infinity Unlimited USB Phoenix driver
4 * Copyright (C) 2010 James Courtier-Dutton (James@superbug.co.uk)
6 * Copyright (C) 2007 Alain Degreffe (eczema@ecze.com)
8 * Original code taken from iuutool (Copyright (C) 2006 Juan Carlos Borrás)
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * And tested with help of WB Electronics
18 #include <linux/kernel.h>
19 #include <linux/errno.h>
20 #include <linux/init.h>
21 #include <linux/slab.h>
22 #include <linux/tty.h>
23 #include <linux/tty_driver.h>
24 #include <linux/tty_flip.h>
25 #include <linux/serial.h>
26 #include <linux/module.h>
27 #include <linux/moduleparam.h>
28 #include <linux/spinlock.h>
29 #include <linux/uaccess.h>
30 #include <linux/usb.h>
31 #include <linux/usb/serial.h>
32 #include "iuu_phoenix.h"
33 #include <linux/random.h>
38 #define DRIVER_VERSION "v0.12"
39 #define DRIVER_DESC "Infinity USB Unlimited Phoenix driver"
41 static const struct usb_device_id id_table[] = {
42 {USB_DEVICE(IUU_USB_VENDOR_ID, IUU_USB_PRODUCT_ID)},
43 {} /* Terminating entry */
45 MODULE_DEVICE_TABLE(usb, id_table);
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;
56 static void read_rxcmd_callback(struct urb *urb);
59 spinlock_t lock; /* store irq state */
60 wait_queue_head_t delta_msr_wait;
62 int tiostatus; /* store IUART SIGNAL for tiocmget call */
63 u8 reset; /* if 1 reset is needed */
64 int poll; /* number of poll */
65 u8 *writebuf; /* buffer for writing to device */
66 int writelen; /* num of byte to write to device */
67 u8 *buf; /* used for initialize speed */
69 int vcc; /* vcc (either 3 or 5 V) */
75 static int iuu_port_probe(struct usb_serial_port *port)
77 struct iuu_private *priv;
79 priv = kzalloc(sizeof(struct iuu_private), GFP_KERNEL);
83 priv->buf = kzalloc(256, GFP_KERNEL);
89 priv->writebuf = kzalloc(256, GFP_KERNEL);
90 if (!priv->writebuf) {
96 priv->vcc = vcc_default;
97 spin_lock_init(&priv->lock);
98 init_waitqueue_head(&priv->delta_msr_wait);
100 usb_set_serial_port_data(port, priv);
105 static int iuu_port_remove(struct usb_serial_port *port)
107 struct iuu_private *priv = usb_get_serial_port_data(port);
109 kfree(priv->writebuf);
116 static int iuu_tiocmset(struct tty_struct *tty,
117 unsigned int set, unsigned int clear)
119 struct usb_serial_port *port = tty->driver_data;
120 struct iuu_private *priv = usb_get_serial_port_data(port);
123 /* FIXME: locking on tiomstatus */
124 dev_dbg(&port->dev, "%s msg : SET = 0x%04x, CLEAR = 0x%04x\n",
125 __func__, set, clear);
127 spin_lock_irqsave(&priv->lock, flags);
129 if ((set & TIOCM_RTS) && !(priv->tiostatus == TIOCM_RTS)) {
130 dev_dbg(&port->dev, "%s TIOCMSET RESET called !!!\n", __func__);
134 priv->tiostatus = TIOCM_RTS;
136 spin_unlock_irqrestore(&priv->lock, flags);
140 /* This is used to provide a carrier detect mechanism
141 * When a card is present, the response is 0x00
142 * When no card , the reader respond with TIOCM_CD
143 * This is known as CD autodetect mechanism
145 static int iuu_tiocmget(struct tty_struct *tty)
147 struct usb_serial_port *port = tty->driver_data;
148 struct iuu_private *priv = usb_get_serial_port_data(port);
152 spin_lock_irqsave(&priv->lock, flags);
153 rc = priv->tiostatus;
154 spin_unlock_irqrestore(&priv->lock, flags);
159 static void iuu_rxcmd(struct urb *urb)
161 struct usb_serial_port *port = urb->context;
163 int status = urb->status;
166 dev_dbg(&port->dev, "%s - status = %d\n", __func__, status);
172 memset(port->write_urb->transfer_buffer, IUU_UART_RX, 1);
173 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
174 usb_sndbulkpipe(port->serial->dev,
175 port->bulk_out_endpointAddress),
176 port->write_urb->transfer_buffer, 1,
177 read_rxcmd_callback, port);
178 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
181 static int iuu_reset(struct usb_serial_port *port, u8 wt)
183 struct iuu_private *priv = usb_get_serial_port_data(port);
185 char *buf_ptr = port->write_urb->transfer_buffer;
187 /* Prepare the reset sequence */
189 *buf_ptr++ = IUU_RST_SET;
190 *buf_ptr++ = IUU_DELAY_MS;
192 *buf_ptr = IUU_RST_CLEAR;
194 /* send the sequence */
196 usb_fill_bulk_urb(port->write_urb,
198 usb_sndbulkpipe(port->serial->dev,
199 port->bulk_out_endpointAddress),
200 port->write_urb->transfer_buffer, 4, iuu_rxcmd, port);
201 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
212 static void iuu_update_status_callback(struct urb *urb)
214 struct usb_serial_port *port = urb->context;
215 struct iuu_private *priv = usb_get_serial_port_data(port);
217 int status = urb->status;
220 dev_dbg(&port->dev, "%s - status = %d\n", __func__, status);
225 st = urb->transfer_buffer;
226 dev_dbg(&port->dev, "%s - enter\n", __func__);
227 if (urb->actual_length == 1) {
230 priv->tiostatus = iuu_cardout;
233 priv->tiostatus = iuu_cardin;
236 priv->tiostatus = iuu_cardin;
242 static void iuu_status_callback(struct urb *urb)
244 struct usb_serial_port *port = urb->context;
246 int status = urb->status;
248 dev_dbg(&port->dev, "%s - status = %d\n", __func__, status);
249 usb_fill_bulk_urb(port->read_urb, port->serial->dev,
250 usb_rcvbulkpipe(port->serial->dev,
251 port->bulk_in_endpointAddress),
252 port->read_urb->transfer_buffer, 256,
253 iuu_update_status_callback, port);
254 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
257 static int iuu_status(struct usb_serial_port *port)
261 memset(port->write_urb->transfer_buffer, IUU_GET_STATE_REGISTER, 1);
262 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
263 usb_sndbulkpipe(port->serial->dev,
264 port->bulk_out_endpointAddress),
265 port->write_urb->transfer_buffer, 1,
266 iuu_status_callback, port);
267 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
272 static int bulk_immediate(struct usb_serial_port *port, u8 *buf, u8 count)
275 struct usb_serial *serial = port->serial;
278 /* send the data out the bulk port */
281 usb_bulk_msg(serial->dev,
282 usb_sndbulkpipe(serial->dev,
283 port->bulk_out_endpointAddress), buf,
284 count, &actual, HZ * 1);
286 if (status != IUU_OPERATION_OK)
287 dev_dbg(&port->dev, "%s - error = %2x\n", __func__, status);
289 dev_dbg(&port->dev, "%s - write OK !\n", __func__);
293 static int read_immediate(struct usb_serial_port *port, u8 *buf, u8 count)
296 struct usb_serial *serial = port->serial;
299 /* send the data out the bulk port */
301 usb_bulk_msg(serial->dev,
302 usb_rcvbulkpipe(serial->dev,
303 port->bulk_in_endpointAddress), buf,
304 count, &actual, HZ * 1);
306 if (status != IUU_OPERATION_OK)
307 dev_dbg(&port->dev, "%s - error = %2x\n", __func__, status);
309 dev_dbg(&port->dev, "%s - read OK !\n", __func__);
313 static int iuu_led(struct usb_serial_port *port, unsigned int R,
314 unsigned int G, unsigned int B, u8 f)
318 buf = kmalloc(8, GFP_KERNEL);
322 buf[0] = IUU_SET_LED;
324 buf[2] = (R >> 8) & 0xFF;
326 buf[4] = (G >> 8) & 0xFF;
328 buf[6] = (B >> 8) & 0xFF;
330 status = bulk_immediate(port, buf, 8);
332 if (status != IUU_OPERATION_OK)
333 dev_dbg(&port->dev, "%s - led error status = %2x\n", __func__, status);
335 dev_dbg(&port->dev, "%s - led OK !\n", __func__);
336 return IUU_OPERATION_OK;
339 static void iuu_rgbf_fill_buffer(u8 *buf, u8 r1, u8 r2, u8 g1, u8 g2, u8 b1,
342 *buf++ = IUU_SET_LED;
352 static void iuu_led_activity_on(struct urb *urb)
354 struct usb_serial_port *port = urb->context;
356 char *buf_ptr = port->write_urb->transfer_buffer;
357 *buf_ptr++ = IUU_SET_LED;
359 get_random_bytes(buf_ptr, 6);
362 iuu_rgbf_fill_buffer(buf_ptr, 255, 255, 0, 0, 0, 0, 255);
365 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
366 usb_sndbulkpipe(port->serial->dev,
367 port->bulk_out_endpointAddress),
368 port->write_urb->transfer_buffer, 8 ,
370 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
373 static void iuu_led_activity_off(struct urb *urb)
375 struct usb_serial_port *port = urb->context;
377 char *buf_ptr = port->write_urb->transfer_buffer;
382 *buf_ptr++ = IUU_SET_LED;
383 iuu_rgbf_fill_buffer(buf_ptr, 0, 0, 255, 255, 0, 0, 255);
385 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
386 usb_sndbulkpipe(port->serial->dev,
387 port->bulk_out_endpointAddress),
388 port->write_urb->transfer_buffer, 8 ,
390 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
395 static int iuu_clk(struct usb_serial_port *port, int dwFrq)
398 struct iuu_private *priv = usb_get_serial_port_data(port);
401 u8 DIV = 0; /* 8bit */
402 u8 XDRV = 0; /* 8bit */
403 u8 PUMP = 0; /* 3bit */
404 u8 PBmsb = 0; /* 2bit */
405 u8 PBlsb = 0; /* 8bit */
406 u8 PO = 0; /* 1bit */
411 int frq = (int)dwFrq;
414 priv->buf[Count++] = IUU_UART_WRITE_I2C;
415 priv->buf[Count++] = FrqGenAdr << 1;
416 priv->buf[Count++] = 0x09;
417 priv->buf[Count++] = 0x00;
419 status = bulk_immediate(port, (u8 *) priv->buf, Count);
421 dev_dbg(&port->dev, "%s - write error\n", __func__);
424 } else if (frq == 3579000) {
429 } else if (frq == 3680000) {
434 } else if (frq == 6000000) {
440 unsigned int result = 0;
441 unsigned int tmp = 0;
446 unsigned int lP = 2055;
447 unsigned int lDiv = 4;
449 for (lQ = 2; lQ <= 47 && !found; lQ++)
450 for (lP = 2055; lP >= 8 && !found; lP--)
451 for (lDiv = 4; lDiv <= 127 && !found; lDiv++) {
452 tmp = (12000000 / lDiv) * (lP / lQ);
453 if (abs((int)(tmp - frq)) <
454 abs((int)(frq - result))) {
455 check2 = (12000000 / lQ);
458 check = (12000000 / lQ) * lP;
459 if (check > 400000000)
461 if (check < 100000000)
463 if (lDiv < 4 || lDiv > 127)
474 P2 = ((P - PO) / 2) - 4;
477 PBmsb = (P2 >> 8 & 0x03);
479 PO = (P >> 10) & 0x01;
482 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
483 priv->buf[Count++] = FrqGenAdr << 1;
484 priv->buf[Count++] = 0x09;
485 priv->buf[Count++] = 0x20; /* Adr = 0x09 */
486 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
487 priv->buf[Count++] = FrqGenAdr << 1;
488 priv->buf[Count++] = 0x0C;
489 priv->buf[Count++] = DIV; /* Adr = 0x0C */
490 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
491 priv->buf[Count++] = FrqGenAdr << 1;
492 priv->buf[Count++] = 0x12;
493 priv->buf[Count++] = XDRV; /* Adr = 0x12 */
494 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
495 priv->buf[Count++] = FrqGenAdr << 1;
496 priv->buf[Count++] = 0x13;
497 priv->buf[Count++] = 0x6B; /* Adr = 0x13 */
498 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
499 priv->buf[Count++] = FrqGenAdr << 1;
500 priv->buf[Count++] = 0x40;
501 priv->buf[Count++] = (0xC0 | ((PUMP & 0x07) << 2)) |
502 (PBmsb & 0x03); /* Adr = 0x40 */
503 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
504 priv->buf[Count++] = FrqGenAdr << 1;
505 priv->buf[Count++] = 0x41;
506 priv->buf[Count++] = PBlsb; /* Adr = 0x41 */
507 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
508 priv->buf[Count++] = FrqGenAdr << 1;
509 priv->buf[Count++] = 0x42;
510 priv->buf[Count++] = Q | (((PO & 0x01) << 7)); /* Adr = 0x42 */
511 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
512 priv->buf[Count++] = FrqGenAdr << 1;
513 priv->buf[Count++] = 0x44;
514 priv->buf[Count++] = (char)0xFF; /* Adr = 0x44 */
515 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
516 priv->buf[Count++] = FrqGenAdr << 1;
517 priv->buf[Count++] = 0x45;
518 priv->buf[Count++] = (char)0xFE; /* Adr = 0x45 */
519 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
520 priv->buf[Count++] = FrqGenAdr << 1;
521 priv->buf[Count++] = 0x46;
522 priv->buf[Count++] = 0x7F; /* Adr = 0x46 */
523 priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
524 priv->buf[Count++] = FrqGenAdr << 1;
525 priv->buf[Count++] = 0x47;
526 priv->buf[Count++] = (char)0x84; /* Adr = 0x47 */
528 status = bulk_immediate(port, (u8 *) priv->buf, Count);
529 if (status != IUU_OPERATION_OK)
530 dev_dbg(&port->dev, "%s - write error\n", __func__);
534 static int iuu_uart_flush(struct usb_serial_port *port)
536 struct device *dev = &port->dev;
539 u8 rxcmd = IUU_UART_RX;
540 struct iuu_private *priv = usb_get_serial_port_data(port);
542 if (iuu_led(port, 0xF000, 0, 0, 0xFF) < 0)
545 for (i = 0; i < 2; i++) {
546 status = bulk_immediate(port, &rxcmd, 1);
547 if (status != IUU_OPERATION_OK) {
548 dev_dbg(dev, "%s - uart_flush_write error\n", __func__);
552 status = read_immediate(port, &priv->len, 1);
553 if (status != IUU_OPERATION_OK) {
554 dev_dbg(dev, "%s - uart_flush_read error\n", __func__);
559 dev_dbg(dev, "%s - uart_flush datalen is : %i\n", __func__, priv->len);
560 status = read_immediate(port, priv->buf, priv->len);
561 if (status != IUU_OPERATION_OK) {
562 dev_dbg(dev, "%s - uart_flush_read error\n", __func__);
567 dev_dbg(dev, "%s - uart_flush_read OK!\n", __func__);
568 iuu_led(port, 0, 0xF000, 0, 0xFF);
572 static void read_buf_callback(struct urb *urb)
574 struct usb_serial_port *port = urb->context;
575 unsigned char *data = urb->transfer_buffer;
576 struct tty_struct *tty;
577 int status = urb->status;
580 if (status == -EPROTO) {
581 /* reschedule needed */
586 dev_dbg(&port->dev, "%s - %i chars to write\n", __func__, urb->actual_length);
587 tty = tty_port_tty_get(&port->port);
589 dev_dbg(&port->dev, "%s - data is NULL !!!\n", __func__);
590 if (tty && urb->actual_length && data) {
591 tty_insert_flip_string(tty, data, urb->actual_length);
592 tty_flip_buffer_push(tty);
595 iuu_led_activity_on(urb);
598 static int iuu_bulk_write(struct usb_serial_port *port)
600 struct iuu_private *priv = usb_get_serial_port_data(port);
604 char *buf_ptr = port->write_urb->transfer_buffer;
606 spin_lock_irqsave(&priv->lock, flags);
607 *buf_ptr++ = IUU_UART_ESC;
608 *buf_ptr++ = IUU_UART_TX;
609 *buf_ptr++ = priv->writelen;
611 memcpy(buf_ptr, priv->writebuf, priv->writelen);
612 buf_len = priv->writelen;
614 spin_unlock_irqrestore(&priv->lock, flags);
615 dev_dbg(&port->dev, "%s - writing %i chars : %*ph\n", __func__,
616 buf_len, buf_len, buf_ptr);
617 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
618 usb_sndbulkpipe(port->serial->dev,
619 port->bulk_out_endpointAddress),
620 port->write_urb->transfer_buffer, buf_len + 3,
622 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
623 usb_serial_port_softint(port);
627 static int iuu_read_buf(struct usb_serial_port *port, int len)
631 usb_fill_bulk_urb(port->read_urb, port->serial->dev,
632 usb_rcvbulkpipe(port->serial->dev,
633 port->bulk_in_endpointAddress),
634 port->read_urb->transfer_buffer, len,
635 read_buf_callback, port);
636 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
640 static void iuu_uart_read_callback(struct urb *urb)
642 struct usb_serial_port *port = urb->context;
643 struct iuu_private *priv = usb_get_serial_port_data(port);
645 int status = urb->status;
648 unsigned char *data = urb->transfer_buffer;
652 dev_dbg(&port->dev, "%s - status = %d\n", __func__, status);
657 dev_dbg(&port->dev, "%s - data is NULL !!!\n", __func__);
659 if (urb->actual_length == 1 && data != NULL)
662 if (urb->actual_length > 1) {
663 dev_dbg(&port->dev, "%s - urb->actual_length = %i\n", __func__,
668 /* if len > 0 call readbuf */
670 if (len > 0 && error == 0) {
671 dev_dbg(&port->dev, "%s - call read buf - len to read is %i\n",
673 status = iuu_read_buf(port, len);
676 /* need to update status ? */
677 if (priv->poll > 99) {
678 status = iuu_status(port);
683 /* reset waiting ? */
685 if (priv->reset == 1) {
686 status = iuu_reset(port, 0xC);
689 /* Writebuf is waiting */
690 spin_lock_irqsave(&priv->lock, flags);
691 if (priv->writelen > 0) {
692 spin_unlock_irqrestore(&priv->lock, flags);
693 status = iuu_bulk_write(port);
696 spin_unlock_irqrestore(&priv->lock, flags);
697 /* if nothing to write call again rxcmd */
698 dev_dbg(&port->dev, "%s - rxcmd recall\n", __func__);
699 iuu_led_activity_off(urb);
702 static int iuu_uart_write(struct tty_struct *tty, struct usb_serial_port *port,
703 const u8 *buf, int count)
705 struct iuu_private *priv = usb_get_serial_port_data(port);
711 spin_lock_irqsave(&priv->lock, flags);
713 /* fill the buffer */
714 memcpy(priv->writebuf + priv->writelen, buf, count);
715 priv->writelen += count;
716 spin_unlock_irqrestore(&priv->lock, flags);
721 static void read_rxcmd_callback(struct urb *urb)
723 struct usb_serial_port *port = urb->context;
725 int status = urb->status;
732 usb_fill_bulk_urb(port->read_urb, port->serial->dev,
733 usb_rcvbulkpipe(port->serial->dev,
734 port->bulk_in_endpointAddress),
735 port->read_urb->transfer_buffer, 256,
736 iuu_uart_read_callback, port);
737 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
738 dev_dbg(&port->dev, "%s - submit result = %d\n", __func__, result);
741 static int iuu_uart_on(struct usb_serial_port *port)
746 buf = kmalloc(sizeof(u8) * 4, GFP_KERNEL);
751 buf[0] = IUU_UART_ENABLE;
752 buf[1] = (u8) ((IUU_BAUD_9600 >> 8) & 0x00FF);
753 buf[2] = (u8) (0x00FF & IUU_BAUD_9600);
754 buf[3] = (u8) (0x0F0 & IUU_ONE_STOP_BIT) | (0x07 & IUU_PARITY_EVEN);
756 status = bulk_immediate(port, buf, 4);
757 if (status != IUU_OPERATION_OK) {
758 dev_dbg(&port->dev, "%s - uart_on error\n", __func__);
759 goto uart_enable_failed;
761 /* iuu_reset() the card after iuu_uart_on() */
762 status = iuu_uart_flush(port);
763 if (status != IUU_OPERATION_OK)
764 dev_dbg(&port->dev, "%s - uart_flush error\n", __func__);
770 /* Diables the IUU UART (a.k.a. the Phoenix voiderface) */
771 static int iuu_uart_off(struct usb_serial_port *port)
775 buf = kmalloc(1, GFP_KERNEL);
778 buf[0] = IUU_UART_DISABLE;
780 status = bulk_immediate(port, buf, 1);
781 if (status != IUU_OPERATION_OK)
782 dev_dbg(&port->dev, "%s - uart_off error\n", __func__);
788 static int iuu_uart_baud(struct usb_serial_port *port, u32 baud_base,
789 u32 *actual, u8 parity)
797 unsigned int T1FrekvensHZ = 0;
799 dev_dbg(&port->dev, "%s - enter baud_base=%d\n", __func__, baud_base);
800 dataout = kmalloc(sizeof(u8) * 5, GFP_KERNEL);
804 /*baud = (((priv->clk / 35) * baud_base) / 100000); */
807 if (baud < 1200 || baud > 230400) {
809 return IUU_INVALID_PARAMETER;
813 T1FrekvensHZ = 500000;
818 T1FrekvensHZ = 2000000;
823 T1FrekvensHZ = 6000000;
828 T1FrekvensHZ = 24000000;
831 T1reload = 256 - (u8) (T1FrekvensHZ / (baud * 2));
833 /* magic number here: ENTER_FIRMWARE_UPDATE; */
834 dataout[DataCount++] = IUU_UART_ESC;
835 /* magic number here: CHANGE_BAUD; */
836 dataout[DataCount++] = IUU_UART_CHANGE;
837 dataout[DataCount++] = T1Frekvens;
838 dataout[DataCount++] = T1reload;
840 *actual = (T1FrekvensHZ / (256 - T1reload)) / 2;
842 switch (parity & 0x0F) {
843 case IUU_PARITY_NONE:
844 dataout[DataCount++] = 0x00;
846 case IUU_PARITY_EVEN:
847 dataout[DataCount++] = 0x01;
850 dataout[DataCount++] = 0x02;
852 case IUU_PARITY_MARK:
853 dataout[DataCount++] = 0x03;
855 case IUU_PARITY_SPACE:
856 dataout[DataCount++] = 0x04;
860 return IUU_INVALID_PARAMETER;
864 switch (parity & 0xF0) {
865 case IUU_ONE_STOP_BIT:
866 dataout[DataCount - 1] |= IUU_ONE_STOP_BIT;
869 case IUU_TWO_STOP_BITS:
870 dataout[DataCount - 1] |= IUU_TWO_STOP_BITS;
874 return IUU_INVALID_PARAMETER;
878 status = bulk_immediate(port, dataout, DataCount);
879 if (status != IUU_OPERATION_OK)
880 dev_dbg(&port->dev, "%s - uart_off error\n", __func__);
885 static void iuu_set_termios(struct tty_struct *tty,
886 struct usb_serial_port *port, struct ktermios *old_termios)
888 const u32 supported_mask = CMSPAR|PARENB|PARODD;
889 struct iuu_private *priv = usb_get_serial_port_data(port);
890 unsigned int cflag = tty->termios.c_cflag;
896 u32 newval = cflag & supported_mask;
898 /* Just use the ospeed. ispeed should be the same. */
899 baud = tty->termios.c_ospeed;
901 dev_dbg(&port->dev, "%s - enter c_ospeed or baud=%d\n", __func__, baud);
903 /* compute the parity parameter */
905 if (cflag & CMSPAR) { /* Using mark space */
907 parity |= IUU_PARITY_SPACE;
909 parity |= IUU_PARITY_MARK;
910 } else if (!(cflag & PARENB)) {
911 parity |= IUU_PARITY_NONE;
913 } else if (cflag & PARODD)
914 parity |= IUU_PARITY_ODD;
916 parity |= IUU_PARITY_EVEN;
918 parity |= (cflag & CSTOPB ? IUU_TWO_STOP_BITS : IUU_ONE_STOP_BIT);
921 status = iuu_uart_baud(port,
922 baud * priv->boost / 100,
925 /* set the termios value to the real one, so the user now what has
926 * changed. We support few fields so its easies to copy the old hw
927 * settings back over and then adjust them
930 tty_termios_copy_hw(&tty->termios, old_termios);
931 if (status != 0) /* Set failed - return old bits */
933 /* Re-encode speed, parity and csize */
934 tty_encode_baud_rate(tty, baud, baud);
935 tty->termios.c_cflag &= ~(supported_mask|CSIZE);
936 tty->termios.c_cflag |= newval | csize;
939 static void iuu_close(struct usb_serial_port *port)
941 /* iuu_led (port,255,0,0,0); */
942 struct usb_serial *serial;
944 serial = port->serial;
951 /* shutdown our urbs */
952 dev_dbg(&port->dev, "%s - shutting down urbs\n", __func__);
953 usb_kill_urb(port->write_urb);
954 usb_kill_urb(port->read_urb);
955 usb_kill_urb(port->interrupt_in_urb);
956 iuu_led(port, 0, 0, 0xF000, 0xFF);
960 static void iuu_init_termios(struct tty_struct *tty)
962 tty->termios = tty_std_termios;
963 tty->termios.c_cflag = CLOCAL | CREAD | CS8 | B9600
964 | TIOCM_CTS | CSTOPB | PARENB;
965 tty->termios.c_ispeed = 9600;
966 tty->termios.c_ospeed = 9600;
967 tty->termios.c_lflag = 0;
968 tty->termios.c_oflag = 0;
969 tty->termios.c_iflag = 0;
972 static int iuu_open(struct tty_struct *tty, struct usb_serial_port *port)
974 struct usb_serial *serial = port->serial;
975 struct device *dev = &port->dev;
980 struct iuu_private *priv = usb_get_serial_port_data(port);
982 baud = tty->termios.c_ospeed;
983 tty->termios.c_ispeed = baud;
984 /* Re-encode speed */
985 tty_encode_baud_rate(tty, baud, baud);
987 dev_dbg(dev, "%s - baud %d\n", __func__, baud);
988 usb_clear_halt(serial->dev, port->write_urb->pipe);
989 usb_clear_halt(serial->dev, port->read_urb->pipe);
991 buf = kmalloc(10, GFP_KERNEL);
997 /* initialize writebuf */
998 #define FISH(a, b, c, d) do { \
999 result = usb_control_msg(port->serial->dev, \
1000 usb_rcvctrlpipe(port->serial->dev, 0), \
1001 b, a, c, d, buf, 1, 1000); \
1002 dev_dbg(dev, "0x%x:0x%x:0x%x:0x%x %d - %x\n", a, b, c, d, result, \
1003 buf[0]); } while (0);
1005 #define SOUP(a, b, c, d) do { \
1006 result = usb_control_msg(port->serial->dev, \
1007 usb_sndctrlpipe(port->serial->dev, 0), \
1008 b, a, c, d, NULL, 0, 1000); \
1009 dev_dbg(dev, "0x%x:0x%x:0x%x:0x%x %d\n", a, b, c, d, result); } while (0)
1011 /* This is not UART related but IUU USB driver related or something */
1012 /* like that. Basically no IUU will accept any commands from the USB */
1013 /* host unless it has received the following message */
1014 /* sprintf(buf ,"%c%c%c%c",0x03,0x02,0x02,0x0); */
1016 SOUP(0x03, 0x02, 0x02, 0x0);
1018 iuu_led(port, 0xF000, 0xF000, 0, 0xFF);
1022 priv->boost = boost;
1024 switch (clockmode) {
1025 case 2: /* 3.680 Mhz */
1026 priv->clk = IUU_CLK_3680000;
1027 iuu_clk(port, IUU_CLK_3680000 * boost / 100);
1029 iuu_uart_baud(port, baud * boost / 100, &actual,
1032 case 3: /* 6.00 Mhz */
1033 iuu_clk(port, IUU_CLK_6000000 * boost / 100);
1034 priv->clk = IUU_CLK_6000000;
1035 /* Ratio of 6000000 to 3500000 for baud 9600 */
1037 iuu_uart_baud(port, 16457 * boost / 100, &actual,
1040 default: /* 3.579 Mhz */
1041 iuu_clk(port, IUU_CLK_3579000 * boost / 100);
1042 priv->clk = IUU_CLK_3579000;
1044 iuu_uart_baud(port, baud * boost / 100, &actual,
1048 /* set the cardin cardout signals */
1055 iuu_cardin = TIOCM_CD;
1060 iuu_cardout = TIOCM_CD;
1063 iuu_cardin = TIOCM_DSR;
1068 iuu_cardout = TIOCM_DSR;
1071 iuu_cardin = TIOCM_CTS;
1076 iuu_cardout = TIOCM_CTS;
1079 iuu_cardin = TIOCM_RNG;
1084 iuu_cardout = TIOCM_RNG;
1087 iuu_uart_flush(port);
1089 dev_dbg(dev, "%s - initialization done\n", __func__);
1091 memset(port->write_urb->transfer_buffer, IUU_UART_RX, 1);
1092 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
1093 usb_sndbulkpipe(port->serial->dev,
1094 port->bulk_out_endpointAddress),
1095 port->write_urb->transfer_buffer, 1,
1096 read_rxcmd_callback, port);
1097 result = usb_submit_urb(port->write_urb, GFP_KERNEL);
1099 dev_err(dev, "%s - failed submitting read urb, error %d\n", __func__, result);
1102 dev_dbg(dev, "%s - rxcmd OK\n", __func__);
1108 /* how to change VCC */
1109 static int iuu_vcc_set(struct usb_serial_port *port, unsigned int vcc)
1114 buf = kmalloc(5, GFP_KERNEL);
1118 buf[0] = IUU_SET_VCC;
1119 buf[1] = vcc & 0xFF;
1120 buf[2] = (vcc >> 8) & 0xFF;
1121 buf[3] = (vcc >> 16) & 0xFF;
1122 buf[4] = (vcc >> 24) & 0xFF;
1124 status = bulk_immediate(port, buf, 5);
1127 if (status != IUU_OPERATION_OK)
1128 dev_dbg(&port->dev, "%s - vcc error status = %2x\n", __func__, status);
1130 dev_dbg(&port->dev, "%s - vcc OK !\n", __func__);
1139 static ssize_t show_vcc_mode(struct device *dev,
1140 struct device_attribute *attr, char *buf)
1142 struct usb_serial_port *port = to_usb_serial_port(dev);
1143 struct iuu_private *priv = usb_get_serial_port_data(port);
1145 return sprintf(buf, "%d\n", priv->vcc);
1148 static ssize_t store_vcc_mode(struct device *dev,
1149 struct device_attribute *attr, const char *buf, size_t count)
1151 struct usb_serial_port *port = to_usb_serial_port(dev);
1152 struct iuu_private *priv = usb_get_serial_port_data(port);
1155 if (strict_strtoul(buf, 10, &v)) {
1156 dev_err(dev, "%s - vcc_mode: %s is not a unsigned long\n",
1158 goto fail_store_vcc_mode;
1161 dev_dbg(dev, "%s: setting vcc_mode = %ld", __func__, v);
1163 if ((v != 3) && (v != 5)) {
1164 dev_err(dev, "%s - vcc_mode %ld is invalid\n", __func__, v);
1166 iuu_vcc_set(port, v);
1169 fail_store_vcc_mode:
1173 static DEVICE_ATTR(vcc_mode, S_IRUSR | S_IWUSR, show_vcc_mode,
1176 static int iuu_create_sysfs_attrs(struct usb_serial_port *port)
1178 return device_create_file(&port->dev, &dev_attr_vcc_mode);
1181 static int iuu_remove_sysfs_attrs(struct usb_serial_port *port)
1183 device_remove_file(&port->dev, &dev_attr_vcc_mode);
1188 * End Sysfs Attributes
1191 static struct usb_serial_driver iuu_device = {
1193 .owner = THIS_MODULE,
1194 .name = "iuu_phoenix",
1196 .id_table = id_table,
1198 .bulk_in_size = 512,
1199 .bulk_out_size = 512,
1200 .port_probe = iuu_create_sysfs_attrs,
1201 .port_remove = iuu_remove_sysfs_attrs,
1204 .write = iuu_uart_write,
1205 .read_bulk_callback = iuu_uart_read_callback,
1206 .tiocmget = iuu_tiocmget,
1207 .tiocmset = iuu_tiocmset,
1208 .set_termios = iuu_set_termios,
1209 .init_termios = iuu_init_termios,
1210 .port_probe = iuu_port_probe,
1211 .port_remove = iuu_port_remove,
1214 static struct usb_serial_driver * const serial_drivers[] = {
1218 module_usb_serial_driver(serial_drivers, id_table);
1220 MODULE_AUTHOR("Alain Degreffe eczema@ecze.com");
1222 MODULE_DESCRIPTION(DRIVER_DESC);
1223 MODULE_LICENSE("GPL");
1225 MODULE_VERSION(DRIVER_VERSION);
1227 module_param(xmas, bool, S_IRUGO | S_IWUSR);
1228 MODULE_PARM_DESC(xmas, "Xmas colors enabled or not");
1230 module_param(boost, int, S_IRUGO | S_IWUSR);
1231 MODULE_PARM_DESC(boost, "Card overclock boost (in percent 100-500)");
1233 module_param(clockmode, int, S_IRUGO | S_IWUSR);
1234 MODULE_PARM_DESC(clockmode, "Card clock mode (1=3.579 MHz, 2=3.680 MHz, "
1237 module_param(cdmode, int, S_IRUGO | S_IWUSR);
1238 MODULE_PARM_DESC(cdmode, "Card detect mode (0=none, 1=CD, 2=!CD, 3=DSR, "
1239 "4=!DSR, 5=CTS, 6=!CTS, 7=RING, 8=!RING)");
1241 module_param(vcc_default, int, S_IRUGO | S_IWUSR);
1242 MODULE_PARM_DESC(vcc_default, "Set default VCC (either 3 for 3.3V or 5 "
1243 "for 5V). Default to 5.");