2 * Infinity Unlimited USB Phoenix driver
4 * Copyright (C) 2007 Alain Degreffe (eczema@ecze.com)
6 * Original code taken from iuutool (Copyright (C) 2006 Juan Carlos Borrás)
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * And tested with help of WB Electronics
16 #include <linux/kernel.h>
17 #include <linux/errno.h>
18 #include <linux/init.h>
19 #include <linux/slab.h>
20 #include <linux/tty.h>
21 #include <linux/tty_driver.h>
22 #include <linux/tty_flip.h>
23 #include <linux/serial.h>
24 #include <linux/module.h>
25 #include <linux/moduleparam.h>
26 #include <linux/spinlock.h>
27 #include <linux/uaccess.h>
28 #include <linux/usb.h>
29 #include <linux/usb/serial.h>
30 #include "iuu_phoenix.h"
31 #include <linux/random.h>
34 #ifdef CONFIG_USB_SERIAL_DEBUG
43 #define DRIVER_VERSION "v0.11"
44 #define DRIVER_DESC "Infinity USB Unlimited Phoenix driver"
46 static const struct usb_device_id id_table
[] = {
47 {USB_DEVICE(IUU_USB_VENDOR_ID
, IUU_USB_PRODUCT_ID
)},
48 {} /* Terminating entry */
50 MODULE_DEVICE_TABLE(usb
, id_table
);
52 static struct usb_driver iuu_driver
= {
53 .name
= "iuu_phoenix",
54 .probe
= usb_serial_probe
,
55 .disconnect
= usb_serial_disconnect
,
61 static int boost
= 100;
62 static int clockmode
= 1;
63 static int cdmode
= 1;
64 static int iuu_cardin
;
65 static int iuu_cardout
;
67 static int vcc_default
= 5;
69 static void read_rxcmd_callback(struct urb
*urb
);
72 spinlock_t lock
; /* store irq state */
73 wait_queue_head_t delta_msr_wait
;
75 int tiostatus
; /* store IUART SIGNAL for tiocmget call */
76 u8 reset
; /* if 1 reset is needed */
77 int poll
; /* number of poll */
78 u8
*writebuf
; /* buffer for writing to device */
79 int writelen
; /* num of byte to write to device */
80 u8
*buf
; /* used for initialize speed */
81 u8
*dbgbuf
; /* debug buffer */
83 int vcc
; /* vcc (either 3 or 5 V) */
87 static void iuu_free_buf(struct iuu_private
*priv
)
91 kfree(priv
->writebuf
);
94 static int iuu_alloc_buf(struct iuu_private
*priv
)
96 priv
->buf
= kzalloc(256, GFP_KERNEL
);
97 priv
->dbgbuf
= kzalloc(256, GFP_KERNEL
);
98 priv
->writebuf
= kzalloc(256, GFP_KERNEL
);
99 if (!priv
->buf
|| !priv
->dbgbuf
|| !priv
->writebuf
) {
101 dbg("%s problem allocation buffer", __func__
);
104 dbg("%s - Privates buffers allocation success", __func__
);
108 static int iuu_startup(struct usb_serial
*serial
)
110 struct iuu_private
*priv
;
111 priv
= kzalloc(sizeof(struct iuu_private
), GFP_KERNEL
);
112 dbg("%s- priv allocation success", __func__
);
115 if (iuu_alloc_buf(priv
)) {
119 priv
->vcc
= vcc_default
;
120 spin_lock_init(&priv
->lock
);
121 init_waitqueue_head(&priv
->delta_msr_wait
);
122 usb_set_serial_port_data(serial
->port
[0], priv
);
126 /* Release function */
127 static void iuu_release(struct usb_serial
*serial
)
129 struct usb_serial_port
*port
= serial
->port
[0];
130 struct iuu_private
*priv
= usb_get_serial_port_data(port
);
138 dbg("%s - I will free all", __func__
);
139 usb_set_serial_port_data(port
, NULL
);
141 dbg("%s - priv is not anymore in port structure", __func__
);
144 dbg("%s priv is now kfree", __func__
);
148 static int iuu_tiocmset(struct tty_struct
*tty
, struct file
*file
,
149 unsigned int set
, unsigned int clear
)
151 struct usb_serial_port
*port
= tty
->driver_data
;
152 struct iuu_private
*priv
= usb_get_serial_port_data(port
);
155 /* FIXME: locking on tiomstatus */
156 dbg("%s (%d) msg : SET = 0x%04x, CLEAR = 0x%04x ", __func__
,
157 port
->number
, set
, clear
);
159 spin_lock_irqsave(&priv
->lock
, flags
);
161 priv
->tiostatus
= TIOCM_RTS
;
163 if (!(set
& TIOCM_RTS
) && priv
->tiostatus
== TIOCM_RTS
) {
164 dbg("%s TIOCMSET RESET called !!!", __func__
);
167 spin_unlock_irqrestore(&priv
->lock
, flags
);
171 /* This is used to provide a carrier detect mechanism
172 * When a card is present, the response is 0x00
173 * When no card , the reader respond with TIOCM_CD
174 * This is known as CD autodetect mechanism
176 static int iuu_tiocmget(struct tty_struct
*tty
, struct file
*file
)
178 struct usb_serial_port
*port
= tty
->driver_data
;
179 struct iuu_private
*priv
= usb_get_serial_port_data(port
);
183 spin_lock_irqsave(&priv
->lock
, flags
);
184 rc
= priv
->tiostatus
;
185 spin_unlock_irqrestore(&priv
->lock
, flags
);
190 static void iuu_rxcmd(struct urb
*urb
)
192 struct usb_serial_port
*port
= urb
->context
;
194 int status
= urb
->status
;
196 dbg("%s - enter", __func__
);
199 dbg("%s - status = %d", __func__
, status
);
205 memset(port
->write_urb
->transfer_buffer
, IUU_UART_RX
, 1);
206 usb_fill_bulk_urb(port
->write_urb
, port
->serial
->dev
,
207 usb_sndbulkpipe(port
->serial
->dev
,
208 port
->bulk_out_endpointAddress
),
209 port
->write_urb
->transfer_buffer
, 1,
210 read_rxcmd_callback
, port
);
211 result
= usb_submit_urb(port
->write_urb
, GFP_ATOMIC
);
214 static int iuu_reset(struct usb_serial_port
*port
, u8 wt
)
216 struct iuu_private
*priv
= usb_get_serial_port_data(port
);
218 char *buf_ptr
= port
->write_urb
->transfer_buffer
;
219 dbg("%s - enter", __func__
);
221 /* Prepare the reset sequence */
223 *buf_ptr
++ = IUU_RST_SET
;
224 *buf_ptr
++ = IUU_DELAY_MS
;
226 *buf_ptr
= IUU_RST_CLEAR
;
228 /* send the sequence */
230 usb_fill_bulk_urb(port
->write_urb
,
232 usb_sndbulkpipe(port
->serial
->dev
,
233 port
->bulk_out_endpointAddress
),
234 port
->write_urb
->transfer_buffer
, 4, iuu_rxcmd
, port
);
235 result
= usb_submit_urb(port
->write_urb
, GFP_ATOMIC
);
246 static void iuu_update_status_callback(struct urb
*urb
)
248 struct usb_serial_port
*port
= urb
->context
;
249 struct iuu_private
*priv
= usb_get_serial_port_data(port
);
251 int status
= urb
->status
;
253 dbg("%s - enter", __func__
);
256 dbg("%s - status = %d", __func__
, status
);
261 st
= urb
->transfer_buffer
;
262 dbg("%s - enter", __func__
);
263 if (urb
->actual_length
== 1) {
266 priv
->tiostatus
= iuu_cardout
;
269 priv
->tiostatus
= iuu_cardin
;
272 priv
->tiostatus
= iuu_cardin
;
278 static void iuu_status_callback(struct urb
*urb
)
280 struct usb_serial_port
*port
= urb
->context
;
282 int status
= urb
->status
;
284 dbg("%s - status = %d", __func__
, status
);
285 usb_fill_bulk_urb(port
->read_urb
, port
->serial
->dev
,
286 usb_rcvbulkpipe(port
->serial
->dev
,
287 port
->bulk_in_endpointAddress
),
288 port
->read_urb
->transfer_buffer
, 256,
289 iuu_update_status_callback
, port
);
290 result
= usb_submit_urb(port
->read_urb
, GFP_ATOMIC
);
293 static int iuu_status(struct usb_serial_port
*port
)
297 dbg("%s - enter", __func__
);
299 memset(port
->write_urb
->transfer_buffer
, IUU_GET_STATE_REGISTER
, 1);
300 usb_fill_bulk_urb(port
->write_urb
, port
->serial
->dev
,
301 usb_sndbulkpipe(port
->serial
->dev
,
302 port
->bulk_out_endpointAddress
),
303 port
->write_urb
->transfer_buffer
, 1,
304 iuu_status_callback
, port
);
305 result
= usb_submit_urb(port
->write_urb
, GFP_ATOMIC
);
310 static int bulk_immediate(struct usb_serial_port
*port
, u8
*buf
, u8 count
)
313 struct usb_serial
*serial
= port
->serial
;
316 dbg("%s - enter", __func__
);
318 /* send the data out the bulk port */
321 usb_bulk_msg(serial
->dev
,
322 usb_sndbulkpipe(serial
->dev
,
323 port
->bulk_out_endpointAddress
), buf
,
324 count
, &actual
, HZ
* 1);
326 if (status
!= IUU_OPERATION_OK
)
327 dbg("%s - error = %2x", __func__
, status
);
329 dbg("%s - write OK !", __func__
);
333 static int read_immediate(struct usb_serial_port
*port
, u8
*buf
, u8 count
)
336 struct usb_serial
*serial
= port
->serial
;
339 dbg("%s - enter", __func__
);
341 /* send the data out the bulk port */
344 usb_bulk_msg(serial
->dev
,
345 usb_rcvbulkpipe(serial
->dev
,
346 port
->bulk_in_endpointAddress
), buf
,
347 count
, &actual
, HZ
* 1);
349 if (status
!= IUU_OPERATION_OK
)
350 dbg("%s - error = %2x", __func__
, status
);
352 dbg("%s - read OK !", __func__
);
356 static int iuu_led(struct usb_serial_port
*port
, unsigned int R
,
357 unsigned int G
, unsigned int B
, u8 f
)
361 buf
= kmalloc(8, GFP_KERNEL
);
365 dbg("%s - enter", __func__
);
367 buf
[0] = IUU_SET_LED
;
369 buf
[2] = (R
>> 8) & 0xFF;
371 buf
[4] = (G
>> 8) & 0xFF;
373 buf
[6] = (B
>> 8) & 0xFF;
375 status
= bulk_immediate(port
, buf
, 8);
377 if (status
!= IUU_OPERATION_OK
)
378 dbg("%s - led error status = %2x", __func__
, status
);
380 dbg("%s - led OK !", __func__
);
381 return IUU_OPERATION_OK
;
384 static void iuu_rgbf_fill_buffer(u8
*buf
, u8 r1
, u8 r2
, u8 g1
, u8 g2
, u8 b1
,
387 *buf
++ = IUU_SET_LED
;
397 static void iuu_led_activity_on(struct urb
*urb
)
399 struct usb_serial_port
*port
= urb
->context
;
401 char *buf_ptr
= port
->write_urb
->transfer_buffer
;
402 *buf_ptr
++ = IUU_SET_LED
;
404 get_random_bytes(buf_ptr
, 6);
407 iuu_rgbf_fill_buffer(buf_ptr
, 255, 255, 0, 0, 0, 0, 255);
410 usb_fill_bulk_urb(port
->write_urb
, port
->serial
->dev
,
411 usb_sndbulkpipe(port
->serial
->dev
,
412 port
->bulk_out_endpointAddress
),
413 port
->write_urb
->transfer_buffer
, 8 ,
415 result
= usb_submit_urb(port
->write_urb
, GFP_ATOMIC
);
418 static void iuu_led_activity_off(struct urb
*urb
)
420 struct usb_serial_port
*port
= urb
->context
;
422 char *buf_ptr
= port
->write_urb
->transfer_buffer
;
427 *buf_ptr
++ = IUU_SET_LED
;
428 iuu_rgbf_fill_buffer(buf_ptr
, 0, 0, 255, 255, 0, 0, 255);
430 usb_fill_bulk_urb(port
->write_urb
, port
->serial
->dev
,
431 usb_sndbulkpipe(port
->serial
->dev
,
432 port
->bulk_out_endpointAddress
),
433 port
->write_urb
->transfer_buffer
, 8 ,
435 result
= usb_submit_urb(port
->write_urb
, GFP_ATOMIC
);
440 static int iuu_clk(struct usb_serial_port
*port
, int dwFrq
)
443 struct iuu_private
*priv
= usb_get_serial_port_data(port
);
446 u8 DIV
= 0; /* 8bit */
447 u8 XDRV
= 0; /* 8bit */
448 u8 PUMP
= 0; /* 3bit */
449 u8 PBmsb
= 0; /* 2bit */
450 u8 PBlsb
= 0; /* 8bit */
451 u8 PO
= 0; /* 1bit */
456 int frq
= (int)dwFrq
;
458 dbg("%s - enter", __func__
);
461 priv
->buf
[Count
++] = IUU_UART_WRITE_I2C
;
462 priv
->buf
[Count
++] = FrqGenAdr
<< 1;
463 priv
->buf
[Count
++] = 0x09;
464 priv
->buf
[Count
++] = 0x00;
466 status
= bulk_immediate(port
, (u8
*) priv
->buf
, Count
);
468 dbg("%s - write error ", __func__
);
471 } else if (frq
== 3579000) {
476 } else if (frq
== 3680000) {
481 } else if (frq
== 6000000) {
487 unsigned int result
= 0;
488 unsigned int tmp
= 0;
493 unsigned int lP
= 2055;
494 unsigned int lDiv
= 4;
496 for (lQ
= 2; lQ
<= 47 && !found
; lQ
++)
497 for (lP
= 2055; lP
>= 8 && !found
; lP
--)
498 for (lDiv
= 4; lDiv
<= 127 && !found
; lDiv
++) {
499 tmp
= (12000000 / lDiv
) * (lP
/ lQ
);
500 if (abs((int)(tmp
- frq
)) <
501 abs((int)(frq
- result
))) {
502 check2
= (12000000 / lQ
);
505 check
= (12000000 / lQ
) * lP
;
506 if (check
> 400000000)
508 if (check
< 100000000)
510 if (lDiv
< 4 || lDiv
> 127)
521 P2
= ((P
- PO
) / 2) - 4;
524 PBmsb
= (P2
>> 8 & 0x03);
526 PO
= (P
>> 10) & 0x01;
529 priv
->buf
[Count
++] = IUU_UART_WRITE_I2C
; /* 0x4C */
530 priv
->buf
[Count
++] = FrqGenAdr
<< 1;
531 priv
->buf
[Count
++] = 0x09;
532 priv
->buf
[Count
++] = 0x20; /* Adr = 0x09 */
533 priv
->buf
[Count
++] = IUU_UART_WRITE_I2C
; /* 0x4C */
534 priv
->buf
[Count
++] = FrqGenAdr
<< 1;
535 priv
->buf
[Count
++] = 0x0C;
536 priv
->buf
[Count
++] = DIV
; /* Adr = 0x0C */
537 priv
->buf
[Count
++] = IUU_UART_WRITE_I2C
; /* 0x4C */
538 priv
->buf
[Count
++] = FrqGenAdr
<< 1;
539 priv
->buf
[Count
++] = 0x12;
540 priv
->buf
[Count
++] = XDRV
; /* Adr = 0x12 */
541 priv
->buf
[Count
++] = IUU_UART_WRITE_I2C
; /* 0x4C */
542 priv
->buf
[Count
++] = FrqGenAdr
<< 1;
543 priv
->buf
[Count
++] = 0x13;
544 priv
->buf
[Count
++] = 0x6B; /* Adr = 0x13 */
545 priv
->buf
[Count
++] = IUU_UART_WRITE_I2C
; /* 0x4C */
546 priv
->buf
[Count
++] = FrqGenAdr
<< 1;
547 priv
->buf
[Count
++] = 0x40;
548 priv
->buf
[Count
++] = (0xC0 | ((PUMP
& 0x07) << 2)) |
549 (PBmsb
& 0x03); /* Adr = 0x40 */
550 priv
->buf
[Count
++] = IUU_UART_WRITE_I2C
; /* 0x4C */
551 priv
->buf
[Count
++] = FrqGenAdr
<< 1;
552 priv
->buf
[Count
++] = 0x41;
553 priv
->buf
[Count
++] = PBlsb
; /* Adr = 0x41 */
554 priv
->buf
[Count
++] = IUU_UART_WRITE_I2C
; /* 0x4C */
555 priv
->buf
[Count
++] = FrqGenAdr
<< 1;
556 priv
->buf
[Count
++] = 0x42;
557 priv
->buf
[Count
++] = Q
| (((PO
& 0x01) << 7)); /* Adr = 0x42 */
558 priv
->buf
[Count
++] = IUU_UART_WRITE_I2C
; /* 0x4C */
559 priv
->buf
[Count
++] = FrqGenAdr
<< 1;
560 priv
->buf
[Count
++] = 0x44;
561 priv
->buf
[Count
++] = (char)0xFF; /* Adr = 0x44 */
562 priv
->buf
[Count
++] = IUU_UART_WRITE_I2C
; /* 0x4C */
563 priv
->buf
[Count
++] = FrqGenAdr
<< 1;
564 priv
->buf
[Count
++] = 0x45;
565 priv
->buf
[Count
++] = (char)0xFE; /* Adr = 0x45 */
566 priv
->buf
[Count
++] = IUU_UART_WRITE_I2C
; /* 0x4C */
567 priv
->buf
[Count
++] = FrqGenAdr
<< 1;
568 priv
->buf
[Count
++] = 0x46;
569 priv
->buf
[Count
++] = 0x7F; /* Adr = 0x46 */
570 priv
->buf
[Count
++] = IUU_UART_WRITE_I2C
; /* 0x4C */
571 priv
->buf
[Count
++] = FrqGenAdr
<< 1;
572 priv
->buf
[Count
++] = 0x47;
573 priv
->buf
[Count
++] = (char)0x84; /* Adr = 0x47 */
575 status
= bulk_immediate(port
, (u8
*) priv
->buf
, Count
);
576 if (status
!= IUU_OPERATION_OK
)
577 dbg("%s - write error ", __func__
);
581 static int iuu_uart_flush(struct usb_serial_port
*port
)
585 u8 rxcmd
= IUU_UART_RX
;
586 struct iuu_private
*priv
= usb_get_serial_port_data(port
);
588 dbg("%s - enter", __func__
);
590 if (iuu_led(port
, 0xF000, 0, 0, 0xFF) < 0)
593 for (i
= 0; i
< 2; i
++) {
594 status
= bulk_immediate(port
, &rxcmd
, 1);
595 if (status
!= IUU_OPERATION_OK
) {
596 dbg("%s - uart_flush_write error", __func__
);
600 status
= read_immediate(port
, &priv
->len
, 1);
601 if (status
!= IUU_OPERATION_OK
) {
602 dbg("%s - uart_flush_read error", __func__
);
607 dbg("%s - uart_flush datalen is : %i ", __func__
,
609 status
= read_immediate(port
, priv
->buf
, priv
->len
);
610 if (status
!= IUU_OPERATION_OK
) {
611 dbg("%s - uart_flush_read error", __func__
);
616 dbg("%s - uart_flush_read OK!", __func__
);
617 iuu_led(port
, 0, 0xF000, 0, 0xFF);
621 static void read_buf_callback(struct urb
*urb
)
623 struct usb_serial_port
*port
= urb
->context
;
624 unsigned char *data
= urb
->transfer_buffer
;
625 struct tty_struct
*tty
;
626 int status
= urb
->status
;
628 dbg("%s - status = %d", __func__
, status
);
631 if (status
== -EPROTO
) {
632 /* reschedule needed */
637 dbg("%s - %i chars to write", __func__
, urb
->actual_length
);
638 tty
= tty_port_tty_get(&port
->port
);
640 dbg("%s - data is NULL !!!", __func__
);
641 if (tty
&& urb
->actual_length
&& data
) {
642 tty_insert_flip_string(tty
, data
, urb
->actual_length
);
643 tty_flip_buffer_push(tty
);
646 iuu_led_activity_on(urb
);
649 static int iuu_bulk_write(struct usb_serial_port
*port
)
651 struct iuu_private
*priv
= usb_get_serial_port_data(port
);
656 char *buf_ptr
= port
->write_urb
->transfer_buffer
;
657 dbg("%s - enter", __func__
);
659 spin_lock_irqsave(&priv
->lock
, flags
);
660 *buf_ptr
++ = IUU_UART_ESC
;
661 *buf_ptr
++ = IUU_UART_TX
;
662 *buf_ptr
++ = priv
->writelen
;
664 memcpy(buf_ptr
, priv
->writebuf
, priv
->writelen
);
665 buf_len
= priv
->writelen
;
667 spin_unlock_irqrestore(&priv
->lock
, flags
);
669 for (i
= 0; i
< buf_len
; i
++)
670 sprintf(priv
->dbgbuf
+ i
*2 ,
671 "%02X", priv
->writebuf
[i
]);
672 priv
->dbgbuf
[buf_len
+i
*2] = 0;
673 dbg("%s - writing %i chars : %s", __func__
,
674 buf_len
, priv
->dbgbuf
);
676 usb_fill_bulk_urb(port
->write_urb
, port
->serial
->dev
,
677 usb_sndbulkpipe(port
->serial
->dev
,
678 port
->bulk_out_endpointAddress
),
679 port
->write_urb
->transfer_buffer
, buf_len
+ 3,
681 result
= usb_submit_urb(port
->write_urb
, GFP_ATOMIC
);
682 usb_serial_port_softint(port
);
686 static int iuu_read_buf(struct usb_serial_port
*port
, int len
)
689 dbg("%s - enter", __func__
);
691 usb_fill_bulk_urb(port
->read_urb
, port
->serial
->dev
,
692 usb_rcvbulkpipe(port
->serial
->dev
,
693 port
->bulk_in_endpointAddress
),
694 port
->read_urb
->transfer_buffer
, len
,
695 read_buf_callback
, port
);
696 result
= usb_submit_urb(port
->read_urb
, GFP_ATOMIC
);
700 static void iuu_uart_read_callback(struct urb
*urb
)
702 struct usb_serial_port
*port
= urb
->context
;
703 struct iuu_private
*priv
= usb_get_serial_port_data(port
);
705 int status
= urb
->status
;
708 unsigned char *data
= urb
->transfer_buffer
;
711 dbg("%s - enter", __func__
);
714 dbg("%s - status = %d", __func__
, status
);
719 dbg("%s - data is NULL !!!", __func__
);
721 if (urb
->actual_length
== 1 && data
!= NULL
)
724 if (urb
->actual_length
> 1) {
725 dbg("%s - urb->actual_length = %i", __func__
,
730 /* if len > 0 call readbuf */
732 if (len
> 0 && error
== 0) {
733 dbg("%s - call read buf - len to read is %i ",
735 status
= iuu_read_buf(port
, len
);
738 /* need to update status ? */
739 if (priv
->poll
> 99) {
740 status
= iuu_status(port
);
745 /* reset waiting ? */
747 if (priv
->reset
== 1) {
748 status
= iuu_reset(port
, 0xC);
751 /* Writebuf is waiting */
752 spin_lock_irqsave(&priv
->lock
, flags
);
753 if (priv
->writelen
> 0) {
754 spin_unlock_irqrestore(&priv
->lock
, flags
);
755 status
= iuu_bulk_write(port
);
758 spin_unlock_irqrestore(&priv
->lock
, flags
);
759 /* if nothing to write call again rxcmd */
760 dbg("%s - rxcmd recall", __func__
);
761 iuu_led_activity_off(urb
);
764 static int iuu_uart_write(struct tty_struct
*tty
, struct usb_serial_port
*port
,
765 const u8
*buf
, int count
)
767 struct iuu_private
*priv
= usb_get_serial_port_data(port
);
769 dbg("%s - enter", __func__
);
774 spin_lock_irqsave(&priv
->lock
, flags
);
776 /* fill the buffer */
777 memcpy(priv
->writebuf
+ priv
->writelen
, buf
, count
);
778 priv
->writelen
+= count
;
779 spin_unlock_irqrestore(&priv
->lock
, flags
);
784 static void read_rxcmd_callback(struct urb
*urb
)
786 struct usb_serial_port
*port
= urb
->context
;
788 int status
= urb
->status
;
790 dbg("%s - status = %d", __func__
, status
);
797 usb_fill_bulk_urb(port
->read_urb
, port
->serial
->dev
,
798 usb_rcvbulkpipe(port
->serial
->dev
,
799 port
->bulk_in_endpointAddress
),
800 port
->read_urb
->transfer_buffer
, 256,
801 iuu_uart_read_callback
, port
);
802 result
= usb_submit_urb(port
->read_urb
, GFP_ATOMIC
);
803 dbg("%s - submit result = %d", __func__
, result
);
807 static int iuu_uart_on(struct usb_serial_port
*port
)
812 buf
= kmalloc(sizeof(u8
) * 4, GFP_KERNEL
);
817 buf
[0] = IUU_UART_ENABLE
;
818 buf
[1] = (u8
) ((IUU_BAUD_9600
>> 8) & 0x00FF);
819 buf
[2] = (u8
) (0x00FF & IUU_BAUD_9600
);
820 buf
[3] = (u8
) (0x0F0 & IUU_ONE_STOP_BIT
) | (0x07 & IUU_PARITY_EVEN
);
822 status
= bulk_immediate(port
, buf
, 4);
823 if (status
!= IUU_OPERATION_OK
) {
824 dbg("%s - uart_on error", __func__
);
825 goto uart_enable_failed
;
827 /* iuu_reset() the card after iuu_uart_on() */
828 status
= iuu_uart_flush(port
);
829 if (status
!= IUU_OPERATION_OK
)
830 dbg("%s - uart_flush error", __func__
);
836 /* Diables the IUU UART (a.k.a. the Phoenix voiderface) */
837 static int iuu_uart_off(struct usb_serial_port
*port
)
841 buf
= kmalloc(1, GFP_KERNEL
);
844 buf
[0] = IUU_UART_DISABLE
;
846 status
= bulk_immediate(port
, buf
, 1);
847 if (status
!= IUU_OPERATION_OK
)
848 dbg("%s - uart_off error", __func__
);
854 static int iuu_uart_baud(struct usb_serial_port
*port
, u32 baud
,
855 u32
*actual
, u8 parity
)
862 unsigned int T1FrekvensHZ
= 0;
864 dataout
= kmalloc(sizeof(u8
) * 5, GFP_KERNEL
);
869 if (baud
< 1200 || baud
> 230400) {
871 return IUU_INVALID_PARAMETER
;
875 T1FrekvensHZ
= 500000;
880 T1FrekvensHZ
= 2000000;
885 T1FrekvensHZ
= 6000000;
890 T1FrekvensHZ
= 24000000;
893 T1reload
= 256 - (u8
) (T1FrekvensHZ
/ (baud
* 2));
895 /* magic number here: ENTER_FIRMWARE_UPDATE; */
896 dataout
[DataCount
++] = IUU_UART_ESC
;
897 /* magic number here: CHANGE_BAUD; */
898 dataout
[DataCount
++] = IUU_UART_CHANGE
;
899 dataout
[DataCount
++] = T1Frekvens
;
900 dataout
[DataCount
++] = T1reload
;
902 *actual
= (T1FrekvensHZ
/ (256 - T1reload
)) / 2;
904 switch (parity
& 0x0F) {
905 case IUU_PARITY_NONE
:
906 dataout
[DataCount
++] = 0x00;
908 case IUU_PARITY_EVEN
:
909 dataout
[DataCount
++] = 0x01;
912 dataout
[DataCount
++] = 0x02;
914 case IUU_PARITY_MARK
:
915 dataout
[DataCount
++] = 0x03;
917 case IUU_PARITY_SPACE
:
918 dataout
[DataCount
++] = 0x04;
922 return IUU_INVALID_PARAMETER
;
926 switch (parity
& 0xF0) {
927 case IUU_ONE_STOP_BIT
:
928 dataout
[DataCount
- 1] |= IUU_ONE_STOP_BIT
;
931 case IUU_TWO_STOP_BITS
:
932 dataout
[DataCount
- 1] |= IUU_TWO_STOP_BITS
;
936 return IUU_INVALID_PARAMETER
;
940 status
= bulk_immediate(port
, dataout
, DataCount
);
941 if (status
!= IUU_OPERATION_OK
)
942 dbg("%s - uart_off error", __func__
);
947 static void iuu_set_termios(struct tty_struct
*tty
,
948 struct usb_serial_port
*port
, struct ktermios
*old_termios
)
950 const u32 supported_mask
= CMSPAR
|PARENB
|PARODD
;
952 unsigned int cflag
= tty
->termios
->c_cflag
;
957 int baud
= 9600; /* Fixed for the moment */
958 u32 newval
= cflag
& supported_mask
;
960 /* compute the parity parameter */
962 if (cflag
& CMSPAR
) { /* Using mark space */
964 parity
|= IUU_PARITY_SPACE
;
966 parity
|= IUU_PARITY_MARK
;
967 } else if (!(cflag
& PARENB
)) {
968 parity
|= IUU_PARITY_NONE
;
970 } else if (cflag
& PARODD
)
971 parity
|= IUU_PARITY_ODD
;
973 parity
|= IUU_PARITY_EVEN
;
975 parity
|= (cflag
& CSTOPB
? IUU_TWO_STOP_BITS
: IUU_ONE_STOP_BIT
);
978 status
= iuu_uart_baud(port
,
979 (clockmode
== 2) ? 16457 : 9600 * boost
/ 100,
982 /* set the termios value to the real one, so the user now what has
983 * changed. We support few fields so its easies to copy the old hw
984 * settings back over and then adjust them
987 tty_termios_copy_hw(tty
->termios
, old_termios
);
988 if (status
!= 0) /* Set failed - return old bits */
990 /* Re-encode speed, parity and csize */
991 tty_encode_baud_rate(tty
, baud
, baud
);
992 tty
->termios
->c_cflag
&= ~(supported_mask
|CSIZE
);
993 tty
->termios
->c_cflag
|= newval
| csize
;
996 static void iuu_close(struct usb_serial_port
*port
)
998 /* iuu_led (port,255,0,0,0); */
999 struct usb_serial
*serial
;
1001 serial
= port
->serial
;
1005 dbg("%s - port %d", __func__
, port
->number
);
1010 /* shutdown our urbs */
1011 dbg("%s - shutting down urbs", __func__
);
1012 usb_kill_urb(port
->write_urb
);
1013 usb_kill_urb(port
->read_urb
);
1014 usb_kill_urb(port
->interrupt_in_urb
);
1015 iuu_led(port
, 0, 0, 0xF000, 0xFF);
1019 static void iuu_init_termios(struct tty_struct
*tty
)
1021 *(tty
->termios
) = tty_std_termios
;
1022 tty
->termios
->c_cflag
= CLOCAL
| CREAD
| CS8
| B9600
1023 | TIOCM_CTS
| CSTOPB
| PARENB
;
1024 tty
->termios
->c_ispeed
= 9600;
1025 tty
->termios
->c_ospeed
= 9600;
1026 tty
->termios
->c_lflag
= 0;
1027 tty
->termios
->c_oflag
= 0;
1028 tty
->termios
->c_iflag
= 0;
1031 static int iuu_open(struct tty_struct
*tty
, struct usb_serial_port
*port
)
1033 struct usb_serial
*serial
= port
->serial
;
1037 struct iuu_private
*priv
= usb_get_serial_port_data(port
);
1039 dbg("%s - port %d", __func__
, port
->number
);
1040 usb_clear_halt(serial
->dev
, port
->write_urb
->pipe
);
1041 usb_clear_halt(serial
->dev
, port
->read_urb
->pipe
);
1043 buf
= kmalloc(10, GFP_KERNEL
);
1049 /* initialize writebuf */
1050 #define FISH(a, b, c, d) do { \
1051 result = usb_control_msg(port->serial->dev, \
1052 usb_rcvctrlpipe(port->serial->dev, 0), \
1053 b, a, c, d, buf, 1, 1000); \
1054 dbg("0x%x:0x%x:0x%x:0x%x %d - %x", a, b, c, d, result, \
1055 buf[0]); } while (0);
1057 #define SOUP(a, b, c, d) do { \
1058 result = usb_control_msg(port->serial->dev, \
1059 usb_sndctrlpipe(port->serial->dev, 0), \
1060 b, a, c, d, NULL, 0, 1000); \
1061 dbg("0x%x:0x%x:0x%x:0x%x %d", a, b, c, d, result); } while (0)
1063 /* This is not UART related but IUU USB driver related or something */
1064 /* like that. Basically no IUU will accept any commands from the USB */
1065 /* host unless it has received the following message */
1066 /* sprintf(buf ,"%c%c%c%c",0x03,0x02,0x02,0x0); */
1068 SOUP(0x03, 0x02, 0x02, 0x0);
1070 iuu_led(port
, 0xF000, 0xF000, 0, 0xFF);
1074 switch (clockmode
) {
1075 case 2: /* 3.680 Mhz */
1076 iuu_clk(port
, IUU_CLK_3680000
* boost
/ 100);
1078 iuu_uart_baud(port
, 9600 * boost
/ 100, &actual
,
1081 case 3: /* 6.00 Mhz */
1082 iuu_clk(port
, IUU_CLK_6000000
* boost
/ 100);
1084 iuu_uart_baud(port
, 16457 * boost
/ 100, &actual
,
1087 default: /* 3.579 Mhz */
1088 iuu_clk(port
, IUU_CLK_3579000
* boost
/ 100);
1090 iuu_uart_baud(port
, 9600 * boost
/ 100, &actual
,
1094 /* set the cardin cardout signals */
1101 iuu_cardin
= TIOCM_CD
;
1106 iuu_cardout
= TIOCM_CD
;
1109 iuu_cardin
= TIOCM_DSR
;
1114 iuu_cardout
= TIOCM_DSR
;
1117 iuu_cardin
= TIOCM_CTS
;
1122 iuu_cardout
= TIOCM_CTS
;
1125 iuu_cardin
= TIOCM_RNG
;
1130 iuu_cardout
= TIOCM_RNG
;
1133 iuu_uart_flush(port
);
1135 dbg("%s - initialization done", __func__
);
1137 memset(port
->write_urb
->transfer_buffer
, IUU_UART_RX
, 1);
1138 usb_fill_bulk_urb(port
->write_urb
, port
->serial
->dev
,
1139 usb_sndbulkpipe(port
->serial
->dev
,
1140 port
->bulk_out_endpointAddress
),
1141 port
->write_urb
->transfer_buffer
, 1,
1142 read_rxcmd_callback
, port
);
1143 result
= usb_submit_urb(port
->write_urb
, GFP_KERNEL
);
1146 dev_err(&port
->dev
, "%s - failed submitting read urb,"
1147 " error %d\n", __func__
, result
);
1151 dbg("%s - rxcmd OK", __func__
);
1156 /* how to change VCC */
1157 static int iuu_vcc_set(struct usb_serial_port
*port
, unsigned int vcc
)
1162 buf
= kmalloc(5, GFP_KERNEL
);
1166 dbg("%s - enter", __func__
);
1168 buf
[0] = IUU_SET_VCC
;
1169 buf
[1] = vcc
& 0xFF;
1170 buf
[2] = (vcc
>> 8) & 0xFF;
1171 buf
[3] = (vcc
>> 16) & 0xFF;
1172 buf
[4] = (vcc
>> 24) & 0xFF;
1174 status
= bulk_immediate(port
, buf
, 5);
1177 if (status
!= IUU_OPERATION_OK
)
1178 dbg("%s - vcc error status = %2x", __func__
, status
);
1180 dbg("%s - vcc OK !", __func__
);
1189 static ssize_t
show_vcc_mode(struct device
*dev
,
1190 struct device_attribute
*attr
, char *buf
)
1192 struct usb_serial_port
*port
= to_usb_serial_port(dev
);
1193 struct iuu_private
*priv
= usb_get_serial_port_data(port
);
1195 return sprintf(buf
, "%d\n", priv
->vcc
);
1198 static ssize_t
store_vcc_mode(struct device
*dev
,
1199 struct device_attribute
*attr
, const char *buf
, size_t count
)
1201 struct usb_serial_port
*port
= to_usb_serial_port(dev
);
1202 struct iuu_private
*priv
= usb_get_serial_port_data(port
);
1205 if (strict_strtoul(buf
, 10, &v
)) {
1206 dev_err(dev
, "%s - vcc_mode: %s is not a unsigned long\n",
1208 goto fail_store_vcc_mode
;
1211 dbg("%s: setting vcc_mode = %ld", __func__
, v
);
1213 if ((v
!= 3) && (v
!= 5)) {
1214 dev_err(dev
, "%s - vcc_mode %ld is invalid\n", __func__
, v
);
1216 iuu_vcc_set(port
, v
);
1219 fail_store_vcc_mode
:
1223 static DEVICE_ATTR(vcc_mode
, S_IRUSR
| S_IWUSR
, show_vcc_mode
,
1226 static int iuu_create_sysfs_attrs(struct usb_serial_port
*port
)
1228 dbg("%s", __func__
);
1230 return device_create_file(&port
->dev
, &dev_attr_vcc_mode
);
1233 static int iuu_remove_sysfs_attrs(struct usb_serial_port
*port
)
1235 dbg("%s", __func__
);
1237 device_remove_file(&port
->dev
, &dev_attr_vcc_mode
);
1242 * End Sysfs Attributes
1245 static struct usb_serial_driver iuu_device
= {
1247 .owner
= THIS_MODULE
,
1248 .name
= "iuu_phoenix",
1250 .id_table
= id_table
,
1252 .bulk_in_size
= 512,
1253 .bulk_out_size
= 512,
1254 .port_probe
= iuu_create_sysfs_attrs
,
1255 .port_remove
= iuu_remove_sysfs_attrs
,
1258 .write
= iuu_uart_write
,
1259 .read_bulk_callback
= iuu_uart_read_callback
,
1260 .tiocmget
= iuu_tiocmget
,
1261 .tiocmset
= iuu_tiocmset
,
1262 .set_termios
= iuu_set_termios
,
1263 .init_termios
= iuu_init_termios
,
1264 .attach
= iuu_startup
,
1265 .release
= iuu_release
,
1268 static int __init
iuu_init(void)
1271 retval
= usb_serial_register(&iuu_device
);
1273 goto failed_usb_serial_register
;
1274 retval
= usb_register(&iuu_driver
);
1276 goto failed_usb_register
;
1277 printk(KERN_INFO KBUILD_MODNAME
": " DRIVER_VERSION
":"
1280 failed_usb_register
:
1281 usb_serial_deregister(&iuu_device
);
1282 failed_usb_serial_register
:
1286 static void __exit
iuu_exit(void)
1288 usb_deregister(&iuu_driver
);
1289 usb_serial_deregister(&iuu_device
);
1292 module_init(iuu_init
);
1293 module_exit(iuu_exit
);
1295 MODULE_AUTHOR("Alain Degreffe eczema@ecze.com");
1297 MODULE_DESCRIPTION(DRIVER_DESC
);
1298 MODULE_LICENSE("GPL");
1300 MODULE_VERSION(DRIVER_VERSION
);
1301 module_param(debug
, bool, S_IRUGO
| S_IWUSR
);
1302 MODULE_PARM_DESC(debug
, "Debug enabled or not");
1304 module_param(xmas
, bool, S_IRUGO
| S_IWUSR
);
1305 MODULE_PARM_DESC(xmas
, "Xmas colors enabled or not");
1307 module_param(boost
, int, S_IRUGO
| S_IWUSR
);
1308 MODULE_PARM_DESC(boost
, "Card overclock boost (in percent 100-500)");
1310 module_param(clockmode
, int, S_IRUGO
| S_IWUSR
);
1311 MODULE_PARM_DESC(clockmode
, "Card clock mode (1=3.579 MHz, 2=3.680 MHz, "
1314 module_param(cdmode
, int, S_IRUGO
| S_IWUSR
);
1315 MODULE_PARM_DESC(cdmode
, "Card detect mode (0=none, 1=CD, 2=!CD, 3=DSR, "
1316 "4=!DSR, 5=CTS, 6=!CTS, 7=RING, 8=!RING)");
1318 module_param(vcc_default
, int, S_IRUGO
| S_IWUSR
);
1319 MODULE_PARM_DESC(vcc_default
, "Set default VCC (either 3 for 3.3V or 5 "
1320 "for 5V). Default to 5.");