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>
36 #ifdef CONFIG_USB_SERIAL_DEBUG
37 static bool debug
= 1;
45 #define DRIVER_VERSION "v0.12"
46 #define DRIVER_DESC "Infinity USB Unlimited Phoenix driver"
48 static const struct usb_device_id id_table
[] = {
49 {USB_DEVICE(IUU_USB_VENDOR_ID
, IUU_USB_PRODUCT_ID
)},
50 {} /* Terminating entry */
52 MODULE_DEVICE_TABLE(usb
, id_table
);
55 static int boost
= 100;
56 static int clockmode
= 1;
57 static int cdmode
= 1;
58 static int iuu_cardin
;
59 static int iuu_cardout
;
61 static int vcc_default
= 5;
63 static int iuu_create_sysfs_attrs(struct usb_serial_port
*port
);
64 static int iuu_remove_sysfs_attrs(struct usb_serial_port
*port
);
65 static void read_rxcmd_callback(struct urb
*urb
);
68 spinlock_t lock
; /* store irq state */
69 wait_queue_head_t delta_msr_wait
;
71 int tiostatus
; /* store IUART SIGNAL for tiocmget call */
72 u8 reset
; /* if 1 reset is needed */
73 int poll
; /* number of poll */
74 u8
*writebuf
; /* buffer for writing to device */
75 int writelen
; /* num of byte to write to device */
76 u8
*buf
; /* used for initialize speed */
77 u8
*dbgbuf
; /* debug buffer */
79 int vcc
; /* vcc (either 3 or 5 V) */
85 static int iuu_port_probe(struct usb_serial_port
*port
)
87 struct iuu_private
*priv
;
90 priv
= kzalloc(sizeof(struct iuu_private
), GFP_KERNEL
);
94 priv
->buf
= kzalloc(256, GFP_KERNEL
);
100 priv
->writebuf
= kzalloc(256, GFP_KERNEL
);
101 if (!priv
->writebuf
) {
107 priv
->dbgbuf
= kzalloc(256, GFP_KERNEL
);
109 kfree(priv
->writebuf
);
115 priv
->vcc
= vcc_default
;
116 spin_lock_init(&priv
->lock
);
117 init_waitqueue_head(&priv
->delta_msr_wait
);
119 usb_set_serial_port_data(port
, priv
);
121 ret
= iuu_create_sysfs_attrs(port
);
124 kfree(priv
->writebuf
);
133 static int iuu_port_remove(struct usb_serial_port
*port
)
135 struct iuu_private
*priv
= usb_get_serial_port_data(port
);
137 iuu_remove_sysfs_attrs(port
);
139 kfree(priv
->writebuf
);
146 static int iuu_tiocmset(struct tty_struct
*tty
,
147 unsigned int set
, unsigned int clear
)
149 struct usb_serial_port
*port
= tty
->driver_data
;
150 struct iuu_private
*priv
= usb_get_serial_port_data(port
);
153 /* FIXME: locking on tiomstatus */
154 dbg("%s (%d) msg : SET = 0x%04x, CLEAR = 0x%04x ", __func__
,
155 port
->number
, set
, clear
);
157 spin_lock_irqsave(&priv
->lock
, flags
);
159 if ((set
& TIOCM_RTS
) && !(priv
->tiostatus
== TIOCM_RTS
)) {
160 dbg("%s TIOCMSET RESET called !!!", __func__
);
164 priv
->tiostatus
= TIOCM_RTS
;
166 spin_unlock_irqrestore(&priv
->lock
, flags
);
170 /* This is used to provide a carrier detect mechanism
171 * When a card is present, the response is 0x00
172 * When no card , the reader respond with TIOCM_CD
173 * This is known as CD autodetect mechanism
175 static int iuu_tiocmget(struct tty_struct
*tty
)
177 struct usb_serial_port
*port
= tty
->driver_data
;
178 struct iuu_private
*priv
= usb_get_serial_port_data(port
);
182 spin_lock_irqsave(&priv
->lock
, flags
);
183 rc
= priv
->tiostatus
;
184 spin_unlock_irqrestore(&priv
->lock
, flags
);
189 static void iuu_rxcmd(struct urb
*urb
)
191 struct usb_serial_port
*port
= urb
->context
;
193 int status
= urb
->status
;
196 dbg("%s - status = %d", __func__
, status
);
202 memset(port
->write_urb
->transfer_buffer
, IUU_UART_RX
, 1);
203 usb_fill_bulk_urb(port
->write_urb
, port
->serial
->dev
,
204 usb_sndbulkpipe(port
->serial
->dev
,
205 port
->bulk_out_endpointAddress
),
206 port
->write_urb
->transfer_buffer
, 1,
207 read_rxcmd_callback
, port
);
208 result
= usb_submit_urb(port
->write_urb
, GFP_ATOMIC
);
211 static int iuu_reset(struct usb_serial_port
*port
, u8 wt
)
213 struct iuu_private
*priv
= usb_get_serial_port_data(port
);
215 char *buf_ptr
= port
->write_urb
->transfer_buffer
;
217 /* Prepare the reset sequence */
219 *buf_ptr
++ = IUU_RST_SET
;
220 *buf_ptr
++ = IUU_DELAY_MS
;
222 *buf_ptr
= IUU_RST_CLEAR
;
224 /* send the sequence */
226 usb_fill_bulk_urb(port
->write_urb
,
228 usb_sndbulkpipe(port
->serial
->dev
,
229 port
->bulk_out_endpointAddress
),
230 port
->write_urb
->transfer_buffer
, 4, iuu_rxcmd
, port
);
231 result
= usb_submit_urb(port
->write_urb
, GFP_ATOMIC
);
242 static void iuu_update_status_callback(struct urb
*urb
)
244 struct usb_serial_port
*port
= urb
->context
;
245 struct iuu_private
*priv
= usb_get_serial_port_data(port
);
247 int status
= urb
->status
;
250 dbg("%s - status = %d", __func__
, status
);
255 st
= urb
->transfer_buffer
;
256 dbg("%s - enter", __func__
);
257 if (urb
->actual_length
== 1) {
260 priv
->tiostatus
= iuu_cardout
;
263 priv
->tiostatus
= iuu_cardin
;
266 priv
->tiostatus
= iuu_cardin
;
272 static void iuu_status_callback(struct urb
*urb
)
274 struct usb_serial_port
*port
= urb
->context
;
276 int status
= urb
->status
;
278 dbg("%s - status = %d", __func__
, status
);
279 usb_fill_bulk_urb(port
->read_urb
, port
->serial
->dev
,
280 usb_rcvbulkpipe(port
->serial
->dev
,
281 port
->bulk_in_endpointAddress
),
282 port
->read_urb
->transfer_buffer
, 256,
283 iuu_update_status_callback
, port
);
284 result
= usb_submit_urb(port
->read_urb
, GFP_ATOMIC
);
287 static int iuu_status(struct usb_serial_port
*port
)
291 memset(port
->write_urb
->transfer_buffer
, IUU_GET_STATE_REGISTER
, 1);
292 usb_fill_bulk_urb(port
->write_urb
, port
->serial
->dev
,
293 usb_sndbulkpipe(port
->serial
->dev
,
294 port
->bulk_out_endpointAddress
),
295 port
->write_urb
->transfer_buffer
, 1,
296 iuu_status_callback
, port
);
297 result
= usb_submit_urb(port
->write_urb
, GFP_ATOMIC
);
302 static int bulk_immediate(struct usb_serial_port
*port
, u8
*buf
, u8 count
)
305 struct usb_serial
*serial
= port
->serial
;
308 /* send the data out the bulk port */
311 usb_bulk_msg(serial
->dev
,
312 usb_sndbulkpipe(serial
->dev
,
313 port
->bulk_out_endpointAddress
), buf
,
314 count
, &actual
, HZ
* 1);
316 if (status
!= IUU_OPERATION_OK
)
317 dbg("%s - error = %2x", __func__
, status
);
319 dbg("%s - write OK !", __func__
);
323 static int read_immediate(struct usb_serial_port
*port
, u8
*buf
, u8 count
)
326 struct usb_serial
*serial
= port
->serial
;
329 /* send the data out the bulk port */
331 usb_bulk_msg(serial
->dev
,
332 usb_rcvbulkpipe(serial
->dev
,
333 port
->bulk_in_endpointAddress
), buf
,
334 count
, &actual
, HZ
* 1);
336 if (status
!= IUU_OPERATION_OK
)
337 dbg("%s - error = %2x", __func__
, status
);
339 dbg("%s - read OK !", __func__
);
343 static int iuu_led(struct usb_serial_port
*port
, unsigned int R
,
344 unsigned int G
, unsigned int B
, u8 f
)
348 buf
= kmalloc(8, GFP_KERNEL
);
352 buf
[0] = IUU_SET_LED
;
354 buf
[2] = (R
>> 8) & 0xFF;
356 buf
[4] = (G
>> 8) & 0xFF;
358 buf
[6] = (B
>> 8) & 0xFF;
360 status
= bulk_immediate(port
, buf
, 8);
362 if (status
!= IUU_OPERATION_OK
)
363 dbg("%s - led error status = %2x", __func__
, status
);
365 dbg("%s - led OK !", __func__
);
366 return IUU_OPERATION_OK
;
369 static void iuu_rgbf_fill_buffer(u8
*buf
, u8 r1
, u8 r2
, u8 g1
, u8 g2
, u8 b1
,
372 *buf
++ = IUU_SET_LED
;
382 static void iuu_led_activity_on(struct urb
*urb
)
384 struct usb_serial_port
*port
= urb
->context
;
386 char *buf_ptr
= port
->write_urb
->transfer_buffer
;
387 *buf_ptr
++ = IUU_SET_LED
;
389 get_random_bytes(buf_ptr
, 6);
392 iuu_rgbf_fill_buffer(buf_ptr
, 255, 255, 0, 0, 0, 0, 255);
395 usb_fill_bulk_urb(port
->write_urb
, port
->serial
->dev
,
396 usb_sndbulkpipe(port
->serial
->dev
,
397 port
->bulk_out_endpointAddress
),
398 port
->write_urb
->transfer_buffer
, 8 ,
400 result
= usb_submit_urb(port
->write_urb
, GFP_ATOMIC
);
403 static void iuu_led_activity_off(struct urb
*urb
)
405 struct usb_serial_port
*port
= urb
->context
;
407 char *buf_ptr
= port
->write_urb
->transfer_buffer
;
412 *buf_ptr
++ = IUU_SET_LED
;
413 iuu_rgbf_fill_buffer(buf_ptr
, 0, 0, 255, 255, 0, 0, 255);
415 usb_fill_bulk_urb(port
->write_urb
, port
->serial
->dev
,
416 usb_sndbulkpipe(port
->serial
->dev
,
417 port
->bulk_out_endpointAddress
),
418 port
->write_urb
->transfer_buffer
, 8 ,
420 result
= usb_submit_urb(port
->write_urb
, GFP_ATOMIC
);
425 static int iuu_clk(struct usb_serial_port
*port
, int dwFrq
)
428 struct iuu_private
*priv
= usb_get_serial_port_data(port
);
431 u8 DIV
= 0; /* 8bit */
432 u8 XDRV
= 0; /* 8bit */
433 u8 PUMP
= 0; /* 3bit */
434 u8 PBmsb
= 0; /* 2bit */
435 u8 PBlsb
= 0; /* 8bit */
436 u8 PO
= 0; /* 1bit */
441 int frq
= (int)dwFrq
;
444 priv
->buf
[Count
++] = IUU_UART_WRITE_I2C
;
445 priv
->buf
[Count
++] = FrqGenAdr
<< 1;
446 priv
->buf
[Count
++] = 0x09;
447 priv
->buf
[Count
++] = 0x00;
449 status
= bulk_immediate(port
, (u8
*) priv
->buf
, Count
);
451 dbg("%s - write error ", __func__
);
454 } else if (frq
== 3579000) {
459 } else if (frq
== 3680000) {
464 } else if (frq
== 6000000) {
470 unsigned int result
= 0;
471 unsigned int tmp
= 0;
476 unsigned int lP
= 2055;
477 unsigned int lDiv
= 4;
479 for (lQ
= 2; lQ
<= 47 && !found
; lQ
++)
480 for (lP
= 2055; lP
>= 8 && !found
; lP
--)
481 for (lDiv
= 4; lDiv
<= 127 && !found
; lDiv
++) {
482 tmp
= (12000000 / lDiv
) * (lP
/ lQ
);
483 if (abs((int)(tmp
- frq
)) <
484 abs((int)(frq
- result
))) {
485 check2
= (12000000 / lQ
);
488 check
= (12000000 / lQ
) * lP
;
489 if (check
> 400000000)
491 if (check
< 100000000)
493 if (lDiv
< 4 || lDiv
> 127)
504 P2
= ((P
- PO
) / 2) - 4;
507 PBmsb
= (P2
>> 8 & 0x03);
509 PO
= (P
>> 10) & 0x01;
512 priv
->buf
[Count
++] = IUU_UART_WRITE_I2C
; /* 0x4C */
513 priv
->buf
[Count
++] = FrqGenAdr
<< 1;
514 priv
->buf
[Count
++] = 0x09;
515 priv
->buf
[Count
++] = 0x20; /* Adr = 0x09 */
516 priv
->buf
[Count
++] = IUU_UART_WRITE_I2C
; /* 0x4C */
517 priv
->buf
[Count
++] = FrqGenAdr
<< 1;
518 priv
->buf
[Count
++] = 0x0C;
519 priv
->buf
[Count
++] = DIV
; /* Adr = 0x0C */
520 priv
->buf
[Count
++] = IUU_UART_WRITE_I2C
; /* 0x4C */
521 priv
->buf
[Count
++] = FrqGenAdr
<< 1;
522 priv
->buf
[Count
++] = 0x12;
523 priv
->buf
[Count
++] = XDRV
; /* Adr = 0x12 */
524 priv
->buf
[Count
++] = IUU_UART_WRITE_I2C
; /* 0x4C */
525 priv
->buf
[Count
++] = FrqGenAdr
<< 1;
526 priv
->buf
[Count
++] = 0x13;
527 priv
->buf
[Count
++] = 0x6B; /* Adr = 0x13 */
528 priv
->buf
[Count
++] = IUU_UART_WRITE_I2C
; /* 0x4C */
529 priv
->buf
[Count
++] = FrqGenAdr
<< 1;
530 priv
->buf
[Count
++] = 0x40;
531 priv
->buf
[Count
++] = (0xC0 | ((PUMP
& 0x07) << 2)) |
532 (PBmsb
& 0x03); /* Adr = 0x40 */
533 priv
->buf
[Count
++] = IUU_UART_WRITE_I2C
; /* 0x4C */
534 priv
->buf
[Count
++] = FrqGenAdr
<< 1;
535 priv
->buf
[Count
++] = 0x41;
536 priv
->buf
[Count
++] = PBlsb
; /* Adr = 0x41 */
537 priv
->buf
[Count
++] = IUU_UART_WRITE_I2C
; /* 0x4C */
538 priv
->buf
[Count
++] = FrqGenAdr
<< 1;
539 priv
->buf
[Count
++] = 0x42;
540 priv
->buf
[Count
++] = Q
| (((PO
& 0x01) << 7)); /* Adr = 0x42 */
541 priv
->buf
[Count
++] = IUU_UART_WRITE_I2C
; /* 0x4C */
542 priv
->buf
[Count
++] = FrqGenAdr
<< 1;
543 priv
->buf
[Count
++] = 0x44;
544 priv
->buf
[Count
++] = (char)0xFF; /* Adr = 0x44 */
545 priv
->buf
[Count
++] = IUU_UART_WRITE_I2C
; /* 0x4C */
546 priv
->buf
[Count
++] = FrqGenAdr
<< 1;
547 priv
->buf
[Count
++] = 0x45;
548 priv
->buf
[Count
++] = (char)0xFE; /* Adr = 0x45 */
549 priv
->buf
[Count
++] = IUU_UART_WRITE_I2C
; /* 0x4C */
550 priv
->buf
[Count
++] = FrqGenAdr
<< 1;
551 priv
->buf
[Count
++] = 0x46;
552 priv
->buf
[Count
++] = 0x7F; /* Adr = 0x46 */
553 priv
->buf
[Count
++] = IUU_UART_WRITE_I2C
; /* 0x4C */
554 priv
->buf
[Count
++] = FrqGenAdr
<< 1;
555 priv
->buf
[Count
++] = 0x47;
556 priv
->buf
[Count
++] = (char)0x84; /* Adr = 0x47 */
558 status
= bulk_immediate(port
, (u8
*) priv
->buf
, Count
);
559 if (status
!= IUU_OPERATION_OK
)
560 dbg("%s - write error ", __func__
);
564 static int iuu_uart_flush(struct usb_serial_port
*port
)
568 u8 rxcmd
= IUU_UART_RX
;
569 struct iuu_private
*priv
= usb_get_serial_port_data(port
);
571 if (iuu_led(port
, 0xF000, 0, 0, 0xFF) < 0)
574 for (i
= 0; i
< 2; i
++) {
575 status
= bulk_immediate(port
, &rxcmd
, 1);
576 if (status
!= IUU_OPERATION_OK
) {
577 dbg("%s - uart_flush_write error", __func__
);
581 status
= read_immediate(port
, &priv
->len
, 1);
582 if (status
!= IUU_OPERATION_OK
) {
583 dbg("%s - uart_flush_read error", __func__
);
588 dbg("%s - uart_flush datalen is : %i ", __func__
,
590 status
= read_immediate(port
, priv
->buf
, priv
->len
);
591 if (status
!= IUU_OPERATION_OK
) {
592 dbg("%s - uart_flush_read error", __func__
);
597 dbg("%s - uart_flush_read OK!", __func__
);
598 iuu_led(port
, 0, 0xF000, 0, 0xFF);
602 static void read_buf_callback(struct urb
*urb
)
604 struct usb_serial_port
*port
= urb
->context
;
605 unsigned char *data
= urb
->transfer_buffer
;
606 struct tty_struct
*tty
;
607 int status
= urb
->status
;
610 if (status
== -EPROTO
) {
611 /* reschedule needed */
616 dbg("%s - %i chars to write", __func__
, urb
->actual_length
);
617 tty
= tty_port_tty_get(&port
->port
);
619 dbg("%s - data is NULL !!!", __func__
);
620 if (tty
&& urb
->actual_length
&& data
) {
621 tty_insert_flip_string(tty
, data
, urb
->actual_length
);
622 tty_flip_buffer_push(tty
);
625 iuu_led_activity_on(urb
);
628 static int iuu_bulk_write(struct usb_serial_port
*port
)
630 struct iuu_private
*priv
= usb_get_serial_port_data(port
);
635 char *buf_ptr
= port
->write_urb
->transfer_buffer
;
637 spin_lock_irqsave(&priv
->lock
, flags
);
638 *buf_ptr
++ = IUU_UART_ESC
;
639 *buf_ptr
++ = IUU_UART_TX
;
640 *buf_ptr
++ = priv
->writelen
;
642 memcpy(buf_ptr
, priv
->writebuf
, priv
->writelen
);
643 buf_len
= priv
->writelen
;
645 spin_unlock_irqrestore(&priv
->lock
, flags
);
647 for (i
= 0; i
< buf_len
; i
++)
648 sprintf(priv
->dbgbuf
+ i
*2 ,
649 "%02X", priv
->writebuf
[i
]);
650 priv
->dbgbuf
[buf_len
+i
*2] = 0;
651 dbg("%s - writing %i chars : %s", __func__
,
652 buf_len
, priv
->dbgbuf
);
654 usb_fill_bulk_urb(port
->write_urb
, port
->serial
->dev
,
655 usb_sndbulkpipe(port
->serial
->dev
,
656 port
->bulk_out_endpointAddress
),
657 port
->write_urb
->transfer_buffer
, buf_len
+ 3,
659 result
= usb_submit_urb(port
->write_urb
, GFP_ATOMIC
);
660 usb_serial_port_softint(port
);
664 static int iuu_read_buf(struct usb_serial_port
*port
, int len
)
668 usb_fill_bulk_urb(port
->read_urb
, port
->serial
->dev
,
669 usb_rcvbulkpipe(port
->serial
->dev
,
670 port
->bulk_in_endpointAddress
),
671 port
->read_urb
->transfer_buffer
, len
,
672 read_buf_callback
, port
);
673 result
= usb_submit_urb(port
->read_urb
, GFP_ATOMIC
);
677 static void iuu_uart_read_callback(struct urb
*urb
)
679 struct usb_serial_port
*port
= urb
->context
;
680 struct iuu_private
*priv
= usb_get_serial_port_data(port
);
682 int status
= urb
->status
;
685 unsigned char *data
= urb
->transfer_buffer
;
689 dbg("%s - status = %d", __func__
, status
);
694 dbg("%s - data is NULL !!!", __func__
);
696 if (urb
->actual_length
== 1 && data
!= NULL
)
699 if (urb
->actual_length
> 1) {
700 dbg("%s - urb->actual_length = %i", __func__
,
705 /* if len > 0 call readbuf */
707 if (len
> 0 && error
== 0) {
708 dbg("%s - call read buf - len to read is %i ",
710 status
= iuu_read_buf(port
, len
);
713 /* need to update status ? */
714 if (priv
->poll
> 99) {
715 status
= iuu_status(port
);
720 /* reset waiting ? */
722 if (priv
->reset
== 1) {
723 status
= iuu_reset(port
, 0xC);
726 /* Writebuf is waiting */
727 spin_lock_irqsave(&priv
->lock
, flags
);
728 if (priv
->writelen
> 0) {
729 spin_unlock_irqrestore(&priv
->lock
, flags
);
730 status
= iuu_bulk_write(port
);
733 spin_unlock_irqrestore(&priv
->lock
, flags
);
734 /* if nothing to write call again rxcmd */
735 dbg("%s - rxcmd recall", __func__
);
736 iuu_led_activity_off(urb
);
739 static int iuu_uart_write(struct tty_struct
*tty
, struct usb_serial_port
*port
,
740 const u8
*buf
, int count
)
742 struct iuu_private
*priv
= usb_get_serial_port_data(port
);
748 spin_lock_irqsave(&priv
->lock
, flags
);
750 /* fill the buffer */
751 memcpy(priv
->writebuf
+ priv
->writelen
, buf
, count
);
752 priv
->writelen
+= count
;
753 spin_unlock_irqrestore(&priv
->lock
, flags
);
758 static void read_rxcmd_callback(struct urb
*urb
)
760 struct usb_serial_port
*port
= urb
->context
;
762 int status
= urb
->status
;
769 usb_fill_bulk_urb(port
->read_urb
, port
->serial
->dev
,
770 usb_rcvbulkpipe(port
->serial
->dev
,
771 port
->bulk_in_endpointAddress
),
772 port
->read_urb
->transfer_buffer
, 256,
773 iuu_uart_read_callback
, port
);
774 result
= usb_submit_urb(port
->read_urb
, GFP_ATOMIC
);
775 dbg("%s - submit result = %d", __func__
, result
);
778 static int iuu_uart_on(struct usb_serial_port
*port
)
783 buf
= kmalloc(sizeof(u8
) * 4, GFP_KERNEL
);
788 buf
[0] = IUU_UART_ENABLE
;
789 buf
[1] = (u8
) ((IUU_BAUD_9600
>> 8) & 0x00FF);
790 buf
[2] = (u8
) (0x00FF & IUU_BAUD_9600
);
791 buf
[3] = (u8
) (0x0F0 & IUU_ONE_STOP_BIT
) | (0x07 & IUU_PARITY_EVEN
);
793 status
= bulk_immediate(port
, buf
, 4);
794 if (status
!= IUU_OPERATION_OK
) {
795 dbg("%s - uart_on error", __func__
);
796 goto uart_enable_failed
;
798 /* iuu_reset() the card after iuu_uart_on() */
799 status
= iuu_uart_flush(port
);
800 if (status
!= IUU_OPERATION_OK
)
801 dbg("%s - uart_flush error", __func__
);
807 /* Diables the IUU UART (a.k.a. the Phoenix voiderface) */
808 static int iuu_uart_off(struct usb_serial_port
*port
)
812 buf
= kmalloc(1, GFP_KERNEL
);
815 buf
[0] = IUU_UART_DISABLE
;
817 status
= bulk_immediate(port
, buf
, 1);
818 if (status
!= IUU_OPERATION_OK
)
819 dbg("%s - uart_off error", __func__
);
825 static int iuu_uart_baud(struct usb_serial_port
*port
, u32 baud_base
,
826 u32
*actual
, u8 parity
)
834 unsigned int T1FrekvensHZ
= 0;
836 dbg("%s - enter baud_base=%d", __func__
, baud_base
);
837 dataout
= kmalloc(sizeof(u8
) * 5, GFP_KERNEL
);
841 /*baud = (((priv->clk / 35) * baud_base) / 100000); */
844 if (baud
< 1200 || baud
> 230400) {
846 return IUU_INVALID_PARAMETER
;
850 T1FrekvensHZ
= 500000;
855 T1FrekvensHZ
= 2000000;
860 T1FrekvensHZ
= 6000000;
865 T1FrekvensHZ
= 24000000;
868 T1reload
= 256 - (u8
) (T1FrekvensHZ
/ (baud
* 2));
870 /* magic number here: ENTER_FIRMWARE_UPDATE; */
871 dataout
[DataCount
++] = IUU_UART_ESC
;
872 /* magic number here: CHANGE_BAUD; */
873 dataout
[DataCount
++] = IUU_UART_CHANGE
;
874 dataout
[DataCount
++] = T1Frekvens
;
875 dataout
[DataCount
++] = T1reload
;
877 *actual
= (T1FrekvensHZ
/ (256 - T1reload
)) / 2;
879 switch (parity
& 0x0F) {
880 case IUU_PARITY_NONE
:
881 dataout
[DataCount
++] = 0x00;
883 case IUU_PARITY_EVEN
:
884 dataout
[DataCount
++] = 0x01;
887 dataout
[DataCount
++] = 0x02;
889 case IUU_PARITY_MARK
:
890 dataout
[DataCount
++] = 0x03;
892 case IUU_PARITY_SPACE
:
893 dataout
[DataCount
++] = 0x04;
897 return IUU_INVALID_PARAMETER
;
901 switch (parity
& 0xF0) {
902 case IUU_ONE_STOP_BIT
:
903 dataout
[DataCount
- 1] |= IUU_ONE_STOP_BIT
;
906 case IUU_TWO_STOP_BITS
:
907 dataout
[DataCount
- 1] |= IUU_TWO_STOP_BITS
;
911 return IUU_INVALID_PARAMETER
;
915 status
= bulk_immediate(port
, dataout
, DataCount
);
916 if (status
!= IUU_OPERATION_OK
)
917 dbg("%s - uart_off error", __func__
);
922 static void iuu_set_termios(struct tty_struct
*tty
,
923 struct usb_serial_port
*port
, struct ktermios
*old_termios
)
925 const u32 supported_mask
= CMSPAR
|PARENB
|PARODD
;
926 struct iuu_private
*priv
= usb_get_serial_port_data(port
);
927 unsigned int cflag
= tty
->termios
->c_cflag
;
933 u32 newval
= cflag
& supported_mask
;
935 /* Just use the ospeed. ispeed should be the same. */
936 baud
= tty
->termios
->c_ospeed
;
938 dbg("%s - enter c_ospeed or baud=%d", __func__
, baud
);
940 /* compute the parity parameter */
942 if (cflag
& CMSPAR
) { /* Using mark space */
944 parity
|= IUU_PARITY_SPACE
;
946 parity
|= IUU_PARITY_MARK
;
947 } else if (!(cflag
& PARENB
)) {
948 parity
|= IUU_PARITY_NONE
;
950 } else if (cflag
& PARODD
)
951 parity
|= IUU_PARITY_ODD
;
953 parity
|= IUU_PARITY_EVEN
;
955 parity
|= (cflag
& CSTOPB
? IUU_TWO_STOP_BITS
: IUU_ONE_STOP_BIT
);
958 status
= iuu_uart_baud(port
,
959 baud
* priv
->boost
/ 100,
962 /* set the termios value to the real one, so the user now what has
963 * changed. We support few fields so its easies to copy the old hw
964 * settings back over and then adjust them
967 tty_termios_copy_hw(tty
->termios
, old_termios
);
968 if (status
!= 0) /* Set failed - return old bits */
970 /* Re-encode speed, parity and csize */
971 tty_encode_baud_rate(tty
, baud
, baud
);
972 tty
->termios
->c_cflag
&= ~(supported_mask
|CSIZE
);
973 tty
->termios
->c_cflag
|= newval
| csize
;
976 static void iuu_close(struct usb_serial_port
*port
)
978 /* iuu_led (port,255,0,0,0); */
979 struct usb_serial
*serial
;
981 serial
= port
->serial
;
988 /* shutdown our urbs */
989 dbg("%s - shutting down urbs", __func__
);
990 usb_kill_urb(port
->write_urb
);
991 usb_kill_urb(port
->read_urb
);
992 usb_kill_urb(port
->interrupt_in_urb
);
993 iuu_led(port
, 0, 0, 0xF000, 0xFF);
997 static void iuu_init_termios(struct tty_struct
*tty
)
999 *(tty
->termios
) = tty_std_termios
;
1000 tty
->termios
->c_cflag
= CLOCAL
| CREAD
| CS8
| B9600
1001 | TIOCM_CTS
| CSTOPB
| PARENB
;
1002 tty
->termios
->c_ispeed
= 9600;
1003 tty
->termios
->c_ospeed
= 9600;
1004 tty
->termios
->c_lflag
= 0;
1005 tty
->termios
->c_oflag
= 0;
1006 tty
->termios
->c_iflag
= 0;
1009 static int iuu_open(struct tty_struct
*tty
, struct usb_serial_port
*port
)
1011 struct usb_serial
*serial
= port
->serial
;
1016 struct iuu_private
*priv
= usb_get_serial_port_data(port
);
1018 baud
= tty
->termios
->c_ospeed
;
1019 tty
->termios
->c_ispeed
= baud
;
1020 /* Re-encode speed */
1021 tty_encode_baud_rate(tty
, baud
, baud
);
1023 dbg("%s - port %d, baud %d", __func__
, port
->number
, baud
);
1024 usb_clear_halt(serial
->dev
, port
->write_urb
->pipe
);
1025 usb_clear_halt(serial
->dev
, port
->read_urb
->pipe
);
1027 buf
= kmalloc(10, GFP_KERNEL
);
1033 /* initialize writebuf */
1034 #define FISH(a, b, c, d) do { \
1035 result = usb_control_msg(port->serial->dev, \
1036 usb_rcvctrlpipe(port->serial->dev, 0), \
1037 b, a, c, d, buf, 1, 1000); \
1038 dbg("0x%x:0x%x:0x%x:0x%x %d - %x", a, b, c, d, result, \
1039 buf[0]); } while (0);
1041 #define SOUP(a, b, c, d) do { \
1042 result = usb_control_msg(port->serial->dev, \
1043 usb_sndctrlpipe(port->serial->dev, 0), \
1044 b, a, c, d, NULL, 0, 1000); \
1045 dbg("0x%x:0x%x:0x%x:0x%x %d", a, b, c, d, result); } while (0)
1047 /* This is not UART related but IUU USB driver related or something */
1048 /* like that. Basically no IUU will accept any commands from the USB */
1049 /* host unless it has received the following message */
1050 /* sprintf(buf ,"%c%c%c%c",0x03,0x02,0x02,0x0); */
1052 SOUP(0x03, 0x02, 0x02, 0x0);
1054 iuu_led(port
, 0xF000, 0xF000, 0, 0xFF);
1058 priv
->boost
= boost
;
1060 switch (clockmode
) {
1061 case 2: /* 3.680 Mhz */
1062 priv
->clk
= IUU_CLK_3680000
;
1063 iuu_clk(port
, IUU_CLK_3680000
* boost
/ 100);
1065 iuu_uart_baud(port
, baud
* boost
/ 100, &actual
,
1068 case 3: /* 6.00 Mhz */
1069 iuu_clk(port
, IUU_CLK_6000000
* boost
/ 100);
1070 priv
->clk
= IUU_CLK_6000000
;
1071 /* Ratio of 6000000 to 3500000 for baud 9600 */
1073 iuu_uart_baud(port
, 16457 * boost
/ 100, &actual
,
1076 default: /* 3.579 Mhz */
1077 iuu_clk(port
, IUU_CLK_3579000
* boost
/ 100);
1078 priv
->clk
= IUU_CLK_3579000
;
1080 iuu_uart_baud(port
, baud
* boost
/ 100, &actual
,
1084 /* set the cardin cardout signals */
1091 iuu_cardin
= TIOCM_CD
;
1096 iuu_cardout
= TIOCM_CD
;
1099 iuu_cardin
= TIOCM_DSR
;
1104 iuu_cardout
= TIOCM_DSR
;
1107 iuu_cardin
= TIOCM_CTS
;
1112 iuu_cardout
= TIOCM_CTS
;
1115 iuu_cardin
= TIOCM_RNG
;
1120 iuu_cardout
= TIOCM_RNG
;
1123 iuu_uart_flush(port
);
1125 dbg("%s - initialization done", __func__
);
1127 memset(port
->write_urb
->transfer_buffer
, IUU_UART_RX
, 1);
1128 usb_fill_bulk_urb(port
->write_urb
, port
->serial
->dev
,
1129 usb_sndbulkpipe(port
->serial
->dev
,
1130 port
->bulk_out_endpointAddress
),
1131 port
->write_urb
->transfer_buffer
, 1,
1132 read_rxcmd_callback
, port
);
1133 result
= usb_submit_urb(port
->write_urb
, GFP_KERNEL
);
1135 dev_err(&port
->dev
, "%s - failed submitting read urb,"
1136 " error %d\n", __func__
, result
);
1139 dbg("%s - rxcmd OK", __func__
);
1145 /* how to change VCC */
1146 static int iuu_vcc_set(struct usb_serial_port
*port
, unsigned int vcc
)
1151 buf
= kmalloc(5, GFP_KERNEL
);
1155 buf
[0] = IUU_SET_VCC
;
1156 buf
[1] = vcc
& 0xFF;
1157 buf
[2] = (vcc
>> 8) & 0xFF;
1158 buf
[3] = (vcc
>> 16) & 0xFF;
1159 buf
[4] = (vcc
>> 24) & 0xFF;
1161 status
= bulk_immediate(port
, buf
, 5);
1164 if (status
!= IUU_OPERATION_OK
)
1165 dbg("%s - vcc error status = %2x", __func__
, status
);
1167 dbg("%s - vcc OK !", __func__
);
1176 static ssize_t
show_vcc_mode(struct device
*dev
,
1177 struct device_attribute
*attr
, char *buf
)
1179 struct usb_serial_port
*port
= to_usb_serial_port(dev
);
1180 struct iuu_private
*priv
= usb_get_serial_port_data(port
);
1182 return sprintf(buf
, "%d\n", priv
->vcc
);
1185 static ssize_t
store_vcc_mode(struct device
*dev
,
1186 struct device_attribute
*attr
, const char *buf
, size_t count
)
1188 struct usb_serial_port
*port
= to_usb_serial_port(dev
);
1189 struct iuu_private
*priv
= usb_get_serial_port_data(port
);
1192 if (strict_strtoul(buf
, 10, &v
)) {
1193 dev_err(dev
, "%s - vcc_mode: %s is not a unsigned long\n",
1195 goto fail_store_vcc_mode
;
1198 dbg("%s: setting vcc_mode = %ld", __func__
, v
);
1200 if ((v
!= 3) && (v
!= 5)) {
1201 dev_err(dev
, "%s - vcc_mode %ld is invalid\n", __func__
, v
);
1203 iuu_vcc_set(port
, v
);
1206 fail_store_vcc_mode
:
1210 static DEVICE_ATTR(vcc_mode
, S_IRUSR
| S_IWUSR
, show_vcc_mode
,
1213 static int iuu_create_sysfs_attrs(struct usb_serial_port
*port
)
1215 return device_create_file(&port
->dev
, &dev_attr_vcc_mode
);
1218 static int iuu_remove_sysfs_attrs(struct usb_serial_port
*port
)
1220 device_remove_file(&port
->dev
, &dev_attr_vcc_mode
);
1225 * End Sysfs Attributes
1228 static struct usb_serial_driver iuu_device
= {
1230 .owner
= THIS_MODULE
,
1231 .name
= "iuu_phoenix",
1233 .id_table
= id_table
,
1235 .bulk_in_size
= 512,
1236 .bulk_out_size
= 512,
1239 .write
= iuu_uart_write
,
1240 .read_bulk_callback
= iuu_uart_read_callback
,
1241 .tiocmget
= iuu_tiocmget
,
1242 .tiocmset
= iuu_tiocmset
,
1243 .set_termios
= iuu_set_termios
,
1244 .init_termios
= iuu_init_termios
,
1245 .port_probe
= iuu_port_probe
,
1246 .port_remove
= iuu_port_remove
,
1249 static struct usb_serial_driver
* const serial_drivers
[] = {
1253 module_usb_serial_driver(serial_drivers
, id_table
);
1255 MODULE_AUTHOR("Alain Degreffe eczema@ecze.com");
1257 MODULE_DESCRIPTION(DRIVER_DESC
);
1258 MODULE_LICENSE("GPL");
1260 MODULE_VERSION(DRIVER_VERSION
);
1261 module_param(debug
, bool, S_IRUGO
| S_IWUSR
);
1262 MODULE_PARM_DESC(debug
, "Debug enabled or not");
1264 module_param(xmas
, bool, S_IRUGO
| S_IWUSR
);
1265 MODULE_PARM_DESC(xmas
, "Xmas colors enabled or not");
1267 module_param(boost
, int, S_IRUGO
| S_IWUSR
);
1268 MODULE_PARM_DESC(boost
, "Card overclock boost (in percent 100-500)");
1270 module_param(clockmode
, int, S_IRUGO
| S_IWUSR
);
1271 MODULE_PARM_DESC(clockmode
, "Card clock mode (1=3.579 MHz, 2=3.680 MHz, "
1274 module_param(cdmode
, int, S_IRUGO
| S_IWUSR
);
1275 MODULE_PARM_DESC(cdmode
, "Card detect mode (0=none, 1=CD, 2=!CD, 3=DSR, "
1276 "4=!DSR, 5=CTS, 6=!CTS, 7=RING, 8=!RING)");
1278 module_param(vcc_default
, int, S_IRUGO
| S_IWUSR
);
1279 MODULE_PARM_DESC(vcc_default
, "Set default VCC (either 3 for 3.3V or 5 "
1280 "for 5V). Default to 5.");