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.5"
44 #define DRIVER_DESC "Infinity USB Unlimited Phoenix driver"
46 static 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
;
68 static void read_rxcmd_callback(struct urb
*urb
);
71 spinlock_t lock
; /* store irq state */
72 wait_queue_head_t delta_msr_wait
;
75 u8 termios_initialized
;
76 int tiostatus
; /* store IUART SIGNAL for tiocmget call */
77 u8 reset
; /* if 1 reset is needed */
78 int poll
; /* number of poll */
79 u8
*writebuf
; /* buffer for writing to device */
80 int writelen
; /* num of byte to write to device */
81 u8
*buf
; /* used for initialize speed */
82 u8
*dbgbuf
; /* debug buffer */
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", __FUNCTION__
);
104 dbg("%s - Privates buffers allocation success", __FUNCTION__
);
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", __FUNCTION__
);
115 if (iuu_alloc_buf(priv
)) {
119 spin_lock_init(&priv
->lock
);
120 init_waitqueue_head(&priv
->delta_msr_wait
);
121 usb_set_serial_port_data(serial
->port
[0], priv
);
125 /* Shutdown function */
126 static void iuu_shutdown(struct usb_serial
*serial
)
128 struct usb_serial_port
*port
= serial
->port
[0];
129 struct iuu_private
*priv
= usb_get_serial_port_data(port
);
133 dbg("%s", __FUNCTION__
);
137 dbg("%s - I will free all", __FUNCTION__
);
138 usb_set_serial_port_data(port
, NULL
);
140 dbg("%s - priv is not anymore in port structure", __FUNCTION__
);
143 dbg("%s priv is now kfree", __FUNCTION__
);
147 static int iuu_tiocmset(struct usb_serial_port
*port
, struct file
*file
,
148 unsigned int set
, unsigned int clear
)
150 struct iuu_private
*priv
= usb_get_serial_port_data(port
);
151 struct tty_struct
*tty
;
154 dbg("%s (%d) msg : SET = 0x%04x, CLEAR = 0x%04x ", __FUNCTION__
,
155 port
->number
, set
, clear
);
157 priv
->tiostatus
= TIOCM_RTS
;
159 if (!(set
& TIOCM_RTS
) && priv
->tiostatus
== TIOCM_RTS
) {
160 dbg("%s TIOCMSET RESET called !!!", __FUNCTION__
);
168 /* This is used to provide a carrier detect mechanism
169 * When a card is present, the response is 0x00
170 * When no card , the reader respond with TIOCM_CD
171 * This is known as CD autodetect mechanism
173 static int iuu_tiocmget(struct usb_serial_port
*port
, struct file
*file
)
175 struct iuu_private
*priv
= usb_get_serial_port_data(port
);
176 return priv
->tiostatus
;
179 static void iuu_rxcmd(struct urb
*urb
)
181 struct usb_serial_port
*port
= (struct usb_serial_port
*)urb
->context
;
183 dbg("%s - enter", __FUNCTION__
);
186 dbg("%s - urb->status = %d", __FUNCTION__
, urb
->status
);
192 memset(port
->write_urb
->transfer_buffer
, IUU_UART_RX
, 1);
193 usb_fill_bulk_urb(port
->write_urb
, port
->serial
->dev
,
194 usb_sndbulkpipe(port
->serial
->dev
,
195 port
->bulk_out_endpointAddress
),
196 port
->write_urb
->transfer_buffer
, 1,
197 read_rxcmd_callback
, port
);
198 result
= usb_submit_urb(port
->write_urb
, GFP_ATOMIC
);
201 static int iuu_reset(struct usb_serial_port
*port
, u8 wt
)
203 struct iuu_private
*priv
= usb_get_serial_port_data(port
);
205 char *buf_ptr
= port
->write_urb
->transfer_buffer
;
206 dbg("%s - enter", __FUNCTION__
);
208 /* Prepare the reset sequence */
210 *buf_ptr
++ = IUU_RST_SET
;
211 *buf_ptr
++ = IUU_DELAY_MS
;
213 *buf_ptr
= IUU_RST_CLEAR
;
215 /* send the sequence */
217 usb_fill_bulk_urb(port
->write_urb
,
219 usb_sndbulkpipe(port
->serial
->dev
,
220 port
->bulk_out_endpointAddress
),
221 port
->write_urb
->transfer_buffer
, 4, iuu_rxcmd
, port
);
222 result
= usb_submit_urb(port
->write_urb
, GFP_ATOMIC
);
233 static void iuu_update_status_callback(struct urb
*urb
)
235 struct usb_serial_port
*port
= (struct usb_serial_port
*)urb
->context
;
236 struct iuu_private
*priv
= usb_get_serial_port_data(port
);
238 dbg("%s - enter", __FUNCTION__
);
241 dbg("%s - urb->status = %d", __FUNCTION__
, urb
->status
);
246 st
= urb
->transfer_buffer
;
247 dbg("%s - enter", __FUNCTION__
);
248 if (urb
->actual_length
== 1) {
251 priv
->tiostatus
= iuu_cardout
;
254 priv
->tiostatus
= iuu_cardin
;
257 priv
->tiostatus
= iuu_cardin
;
263 static void iuu_status_callback(struct urb
*urb
)
265 struct usb_serial_port
*port
= (struct usb_serial_port
*)urb
->context
;
267 dbg("%s - enter", __FUNCTION__
);
269 dbg("%s - urb->status = %d", __FUNCTION__
, urb
->status
);
270 usb_fill_bulk_urb(port
->read_urb
, port
->serial
->dev
,
271 usb_rcvbulkpipe(port
->serial
->dev
,
272 port
->bulk_in_endpointAddress
),
273 port
->read_urb
->transfer_buffer
, 256,
274 iuu_update_status_callback
, port
);
275 result
= usb_submit_urb(port
->read_urb
, GFP_ATOMIC
);
278 static int iuu_status(struct usb_serial_port
*port
)
282 dbg("%s - enter", __FUNCTION__
);
284 memset(port
->write_urb
->transfer_buffer
, IUU_GET_STATE_REGISTER
, 1);
285 usb_fill_bulk_urb(port
->write_urb
, port
->serial
->dev
,
286 usb_sndbulkpipe(port
->serial
->dev
,
287 port
->bulk_out_endpointAddress
),
288 port
->write_urb
->transfer_buffer
, 1,
289 iuu_status_callback
, port
);
290 result
= usb_submit_urb(port
->write_urb
, GFP_ATOMIC
);
295 static int bulk_immediate(struct usb_serial_port
*port
, u8
*buf
, u8 count
)
298 struct usb_serial
*serial
= port
->serial
;
301 dbg("%s - enter", __FUNCTION__
);
303 /* send the data out the bulk port */
306 usb_bulk_msg(serial
->dev
,
307 usb_sndbulkpipe(serial
->dev
,
308 port
->bulk_out_endpointAddress
), buf
,
309 count
, &actual
, HZ
* 1);
311 if (status
!= IUU_OPERATION_OK
) {
312 dbg("%s - error = %2x", __FUNCTION__
, status
);
314 dbg("%s - write OK !", __FUNCTION__
);
319 static int read_immediate(struct usb_serial_port
*port
, u8
*buf
, u8 count
)
322 struct usb_serial
*serial
= port
->serial
;
325 dbg("%s - enter", __FUNCTION__
);
327 /* send the data out the bulk port */
330 usb_bulk_msg(serial
->dev
,
331 usb_rcvbulkpipe(serial
->dev
,
332 port
->bulk_in_endpointAddress
), buf
,
333 count
, &actual
, HZ
* 1);
335 if (status
!= IUU_OPERATION_OK
) {
336 dbg("%s - error = %2x", __FUNCTION__
, status
);
338 dbg("%s - read OK !", __FUNCTION__
);
344 static int iuu_led(struct usb_serial_port
*port
, unsigned int R
,
345 unsigned int G
, unsigned int B
, u8 f
)
349 buf
= kmalloc(8, GFP_KERNEL
);
353 dbg("%s - enter", __FUNCTION__
);
355 buf
[0] = IUU_SET_LED
;
357 buf
[2] = (R
>> 8) & 0xFF;
359 buf
[4] = (G
>> 8) & 0xFF;
361 buf
[6] = (B
>> 8) & 0xFF;
363 status
= bulk_immediate(port
, buf
, 8);
365 if (status
!= IUU_OPERATION_OK
)
366 dbg("%s - led error status = %2x", __FUNCTION__
, status
);
368 dbg("%s - led OK !", __FUNCTION__
);
369 return IUU_OPERATION_OK
;
372 static void iuu_rgbf_fill_buffer(u8
*buf
, u8 r1
, u8 r2
, u8 g1
, u8 g2
, u8 b1
,
375 *buf
++ = IUU_SET_LED
;
385 static void iuu_led_activity_on(struct urb
*urb
)
387 struct usb_serial_port
*port
= (struct usb_serial_port
*)urb
->context
;
389 char *buf_ptr
= port
->write_urb
->transfer_buffer
;
390 *buf_ptr
++ = IUU_SET_LED
;
392 get_random_bytes(buf_ptr
, 6);
395 iuu_rgbf_fill_buffer(buf_ptr
, 255, 255, 0, 0, 0, 0, 255);
398 usb_fill_bulk_urb(port
->write_urb
, port
->serial
->dev
,
399 usb_sndbulkpipe(port
->serial
->dev
,
400 port
->bulk_out_endpointAddress
),
401 port
->write_urb
->transfer_buffer
, 8 ,
403 result
= usb_submit_urb(port
->write_urb
, GFP_ATOMIC
);
406 static void iuu_led_activity_off(struct urb
*urb
)
408 struct usb_serial_port
*port
= (struct usb_serial_port
*)urb
->context
;
410 char *buf_ptr
= port
->write_urb
->transfer_buffer
;
415 *buf_ptr
++ = IUU_SET_LED
;
416 iuu_rgbf_fill_buffer(buf_ptr
, 0, 0, 255, 255, 0, 0, 255);
418 usb_fill_bulk_urb(port
->write_urb
, port
->serial
->dev
,
419 usb_sndbulkpipe(port
->serial
->dev
,
420 port
->bulk_out_endpointAddress
),
421 port
->write_urb
->transfer_buffer
, 8 ,
423 result
= usb_submit_urb(port
->write_urb
, GFP_ATOMIC
);
428 static int iuu_clk(struct usb_serial_port
*port
, int dwFrq
)
431 struct iuu_private
*priv
= usb_get_serial_port_data(port
);
434 u8 DIV
= 0; /* 8bit */
435 u8 XDRV
= 0; /* 8bit */
436 u8 PUMP
= 0; /* 3bit */
437 u8 PBmsb
= 0; /* 2bit */
438 u8 PBlsb
= 0; /* 8bit */
439 u8 PO
= 0; /* 1bit */
444 int frq
= (int)dwFrq
;
446 dbg("%s - enter", __FUNCTION__
);
449 priv
->buf
[Count
++] = IUU_UART_WRITE_I2C
;
450 priv
->buf
[Count
++] = FrqGenAdr
<< 1;
451 priv
->buf
[Count
++] = 0x09;
452 priv
->buf
[Count
++] = 0x00;
454 status
= bulk_immediate(port
, (u8
*) priv
->buf
, Count
);
456 dbg("%s - write error ", __FUNCTION__
);
459 } else if (frq
== 3579000) {
464 } else if (frq
== 3680000) {
469 } else if (frq
== 6000000) {
475 unsigned int result
= 0;
476 unsigned int tmp
= 0;
481 unsigned int lP
= 2055;
482 unsigned int lDiv
= 4;
484 for (lQ
= 2; lQ
<= 47 && !found
; lQ
++)
485 for (lP
= 2055; lP
>= 8 && !found
; lP
--)
486 for (lDiv
= 4; lDiv
<= 127 && !found
; lDiv
++) {
487 tmp
= (12000000 / lDiv
) * (lP
/ lQ
);
488 if (abs((int)(tmp
- frq
)) <
489 abs((int)(frq
- result
))) {
490 check2
= (12000000 / lQ
);
493 check
= (12000000 / lQ
) * lP
;
494 if (check
> 400000000)
496 if (check
< 100000000)
498 if (lDiv
< 4 || lDiv
> 127)
509 P2
= ((P
- PO
) / 2) - 4;
512 PBmsb
= (P2
>> 8 & 0x03);
514 PO
= (P
>> 10) & 0x01;
517 priv
->buf
[Count
++] = IUU_UART_WRITE_I2C
; /* 0x4C */
518 priv
->buf
[Count
++] = FrqGenAdr
<< 1;
519 priv
->buf
[Count
++] = 0x09;
520 priv
->buf
[Count
++] = 0x20; /* Adr = 0x09 */
521 priv
->buf
[Count
++] = IUU_UART_WRITE_I2C
; /* 0x4C */
522 priv
->buf
[Count
++] = FrqGenAdr
<< 1;
523 priv
->buf
[Count
++] = 0x0C;
524 priv
->buf
[Count
++] = DIV
; /* Adr = 0x0C */
525 priv
->buf
[Count
++] = IUU_UART_WRITE_I2C
; /* 0x4C */
526 priv
->buf
[Count
++] = FrqGenAdr
<< 1;
527 priv
->buf
[Count
++] = 0x12;
528 priv
->buf
[Count
++] = XDRV
; /* Adr = 0x12 */
529 priv
->buf
[Count
++] = IUU_UART_WRITE_I2C
; /* 0x4C */
530 priv
->buf
[Count
++] = FrqGenAdr
<< 1;
531 priv
->buf
[Count
++] = 0x13;
532 priv
->buf
[Count
++] = 0x6B; /* Adr = 0x13 */
533 priv
->buf
[Count
++] = IUU_UART_WRITE_I2C
; /* 0x4C */
534 priv
->buf
[Count
++] = FrqGenAdr
<< 1;
535 priv
->buf
[Count
++] = 0x40;
536 priv
->buf
[Count
++] = (0xC0 | ((PUMP
& 0x07) << 2)) |
537 (PBmsb
& 0x03); /* Adr = 0x40 */
538 priv
->buf
[Count
++] = IUU_UART_WRITE_I2C
; /* 0x4C */
539 priv
->buf
[Count
++] = FrqGenAdr
<< 1;
540 priv
->buf
[Count
++] = 0x41;
541 priv
->buf
[Count
++] = PBlsb
; /* Adr = 0x41 */
542 priv
->buf
[Count
++] = IUU_UART_WRITE_I2C
; /* 0x4C */
543 priv
->buf
[Count
++] = FrqGenAdr
<< 1;
544 priv
->buf
[Count
++] = 0x42;
545 priv
->buf
[Count
++] = Q
| (((PO
& 0x01) << 7)); /* Adr = 0x42 */
546 priv
->buf
[Count
++] = IUU_UART_WRITE_I2C
; /* 0x4C */
547 priv
->buf
[Count
++] = FrqGenAdr
<< 1;
548 priv
->buf
[Count
++] = 0x44;
549 priv
->buf
[Count
++] = (char)0xFF; /* Adr = 0x44 */
550 priv
->buf
[Count
++] = IUU_UART_WRITE_I2C
; /* 0x4C */
551 priv
->buf
[Count
++] = FrqGenAdr
<< 1;
552 priv
->buf
[Count
++] = 0x45;
553 priv
->buf
[Count
++] = (char)0xFE; /* Adr = 0x45 */
554 priv
->buf
[Count
++] = IUU_UART_WRITE_I2C
; /* 0x4C */
555 priv
->buf
[Count
++] = FrqGenAdr
<< 1;
556 priv
->buf
[Count
++] = 0x46;
557 priv
->buf
[Count
++] = 0x7F; /* Adr = 0x46 */
558 priv
->buf
[Count
++] = IUU_UART_WRITE_I2C
; /* 0x4C */
559 priv
->buf
[Count
++] = FrqGenAdr
<< 1;
560 priv
->buf
[Count
++] = 0x47;
561 priv
->buf
[Count
++] = (char)0x84; /* Adr = 0x47 */
563 status
= bulk_immediate(port
, (u8
*) priv
->buf
, Count
);
564 if (status
!= IUU_OPERATION_OK
)
565 dbg("%s - write error ", __FUNCTION__
);
569 static int iuu_uart_flush(struct usb_serial_port
*port
)
573 u8 rxcmd
= IUU_UART_RX
;
574 struct iuu_private
*priv
= usb_get_serial_port_data(port
);
576 dbg("%s - enter", __FUNCTION__
);
578 if (iuu_led(port
, 0xF000, 0, 0, 0xFF) < 0)
581 for (i
= 0; i
< 2; i
++) {
582 status
= bulk_immediate(port
, &rxcmd
, 1);
583 if (status
!= IUU_OPERATION_OK
) {
584 dbg("%s - uart_flush_write error", __FUNCTION__
);
588 status
= read_immediate(port
, &priv
->len
, 1);
589 if (status
!= IUU_OPERATION_OK
) {
590 dbg("%s - uart_flush_read error", __FUNCTION__
);
595 dbg("%s - uart_flush datalen is : %i ", __FUNCTION__
,
597 status
= read_immediate(port
, priv
->buf
, priv
->len
);
598 if (status
!= IUU_OPERATION_OK
) {
599 dbg("%s - uart_flush_read error", __FUNCTION__
);
604 dbg("%s - uart_flush_read OK!", __FUNCTION__
);
605 iuu_led(port
, 0, 0xF000, 0, 0xFF);
609 static void read_buf_callback(struct urb
*urb
)
611 struct usb_serial_port
*port
= (struct usb_serial_port
*)urb
->context
;
612 unsigned char *data
= urb
->transfer_buffer
;
613 struct tty_struct
*tty
;
614 dbg("%s - urb->status = %d", __FUNCTION__
, urb
->status
);
617 dbg("%s - urb->status = %d", __FUNCTION__
, urb
->status
);
618 if (urb
->status
== -EPROTO
) {
619 /* reschedule needed */
624 dbg("%s - %i chars to write", __FUNCTION__
, urb
->actual_length
);
627 dbg("%s - data is NULL !!!", __FUNCTION__
);
628 if (tty
&& urb
->actual_length
&& data
) {
629 tty_insert_flip_string(tty
, data
, urb
->actual_length
);
630 tty_flip_buffer_push(tty
);
632 iuu_led_activity_on(urb
);
635 static int iuu_bulk_write(struct usb_serial_port
*port
)
637 struct iuu_private
*priv
= usb_get_serial_port_data(port
);
641 char *buf_ptr
= port
->write_urb
->transfer_buffer
;
642 dbg("%s - enter", __FUNCTION__
);
644 *buf_ptr
++ = IUU_UART_ESC
;
645 *buf_ptr
++ = IUU_UART_TX
;
646 *buf_ptr
++ = priv
->writelen
;
648 memcpy(buf_ptr
, priv
->writebuf
,
651 for (i
= 0; i
< priv
->writelen
; i
++)
652 sprintf(priv
->dbgbuf
+ i
*2 ,
653 "%02X", priv
->writebuf
[i
]);
654 priv
->dbgbuf
[priv
->writelen
+i
*2] = 0;
655 dbg("%s - writing %i chars : %s", __FUNCTION__
,
656 priv
->writelen
, priv
->dbgbuf
);
658 usb_fill_bulk_urb(port
->write_urb
, port
->serial
->dev
,
659 usb_sndbulkpipe(port
->serial
->dev
,
660 port
->bulk_out_endpointAddress
),
661 port
->write_urb
->transfer_buffer
, priv
->writelen
+ 3,
663 result
= usb_submit_urb(port
->write_urb
, GFP_ATOMIC
);
664 spin_lock_irqsave(&priv
->lock
, flags
);
666 spin_unlock_irqrestore(&priv
->lock
, flags
);
667 usb_serial_port_softint(port
);
671 static int iuu_read_buf(struct usb_serial_port
*port
, int len
)
674 dbg("%s - enter", __FUNCTION__
);
676 usb_fill_bulk_urb(port
->read_urb
, port
->serial
->dev
,
677 usb_rcvbulkpipe(port
->serial
->dev
,
678 port
->bulk_in_endpointAddress
),
679 port
->read_urb
->transfer_buffer
, len
,
680 read_buf_callback
, port
);
681 result
= usb_submit_urb(port
->read_urb
, GFP_ATOMIC
);
685 static void iuu_uart_read_callback(struct urb
*urb
)
687 struct usb_serial_port
*port
= (struct usb_serial_port
*)urb
->context
;
688 struct iuu_private
*priv
= usb_get_serial_port_data(port
);
693 unsigned char *data
= urb
->transfer_buffer
;
696 dbg("%s - enter", __FUNCTION__
);
699 dbg("%s - urb->status = %d", __FUNCTION__
, urb
->status
);
704 dbg("%s - data is NULL !!!", __FUNCTION__
);
706 if (urb
->actual_length
== 1 && data
!= NULL
)
709 if (urb
->actual_length
> 1) {
710 dbg("%s - urb->actual_length = %i", __FUNCTION__
,
715 /* if len > 0 call readbuf */
717 if (len
> 0 && error
== 0) {
718 dbg("%s - call read buf - len to read is %i ",
720 status
= iuu_read_buf(port
, len
);
723 /* need to update status ? */
724 if (priv
->poll
> 99) {
725 status
= iuu_status(port
);
730 /* reset waiting ? */
732 if (priv
->reset
== 1) {
733 status
= iuu_reset(port
, 0xC);
736 /* Writebuf is waiting */
737 spin_lock_irqsave(&priv
->lock
, flags
);
738 if (priv
->writelen
> 0) {
739 spin_unlock_irqrestore(&priv
->lock
, flags
);
740 status
= iuu_bulk_write(port
);
743 spin_unlock_irqrestore(&priv
->lock
, flags
);
744 /* if nothing to write call again rxcmd */
745 dbg("%s - rxcmd recall", __FUNCTION__
);
746 iuu_led_activity_off(urb
);
750 static int iuu_uart_write(struct usb_serial_port
*port
, const u8
*buf
,
753 struct iuu_private
*priv
= usb_get_serial_port_data(port
);
755 dbg("%s - enter", __FUNCTION__
);
760 spin_lock_irqsave(&priv
->lock
, flags
);
761 if (priv
->writelen
> 0) {
762 /* buffer already filled but not commited */
763 spin_unlock_irqrestore(&priv
->lock
, flags
);
766 /* fill the buffer */
767 memcpy(priv
->writebuf
, buf
, count
);
768 priv
->writelen
= count
;
769 spin_unlock_irqrestore(&priv
->lock
, flags
);
774 static void read_rxcmd_callback(struct urb
*urb
)
776 struct usb_serial_port
*port
= (struct usb_serial_port
*)urb
->context
;
778 dbg("%s - enter", __FUNCTION__
);
780 dbg("%s - urb->status = %d", __FUNCTION__
, urb
->status
);
783 dbg("%s - urb->status = %d", __FUNCTION__
, urb
->status
);
788 usb_fill_bulk_urb(port
->read_urb
, port
->serial
->dev
,
789 usb_rcvbulkpipe(port
->serial
->dev
,
790 port
->bulk_in_endpointAddress
),
791 port
->read_urb
->transfer_buffer
, 256,
792 iuu_uart_read_callback
, port
);
793 result
= usb_submit_urb(port
->read_urb
, GFP_ATOMIC
);
794 dbg("%s - submit result = %d", __FUNCTION__
, result
);
798 static int iuu_uart_on(struct usb_serial_port
*port
)
803 buf
= kmalloc(sizeof(u8
) * 4, GFP_KERNEL
);
808 buf
[0] = IUU_UART_ENABLE
;
809 buf
[1] = (u8
) ((IUU_BAUD_9600
>> 8) & 0x00FF);
810 buf
[2] = (u8
) (0x00FF & IUU_BAUD_9600
);
811 buf
[3] = (u8
) (0x0F0 & IUU_TWO_STOP_BITS
) | (0x07 & IUU_PARITY_EVEN
);
813 status
= bulk_immediate(port
, buf
, 4);
814 if (status
!= IUU_OPERATION_OK
) {
815 dbg("%s - uart_on error", __FUNCTION__
);
816 goto uart_enable_failed
;
818 /* iuu_reset() the card after iuu_uart_on() */
819 status
= iuu_uart_flush(port
);
820 if (status
!= IUU_OPERATION_OK
)
821 dbg("%s - uart_flush error", __FUNCTION__
);
827 /* Diables the IUU UART (a.k.a. the Phoenix voiderface) */
828 static int iuu_uart_off(struct usb_serial_port
*port
)
832 buf
= kmalloc(1, GFP_KERNEL
);
835 buf
[0] = IUU_UART_DISABLE
;
837 status
= bulk_immediate(port
, buf
, 1);
838 if (status
!= IUU_OPERATION_OK
)
839 dbg("%s - uart_off error", __FUNCTION__
);
845 static int iuu_uart_baud(struct usb_serial_port
*port
, u32 baud
,
846 u32
*actual
, u8 parity
)
853 unsigned int T1FrekvensHZ
= 0;
855 dataout
= kmalloc(sizeof(u8
) * 5, GFP_KERNEL
);
860 if (baud
< 1200 || baud
> 230400) {
862 return IUU_INVALID_PARAMETER
;
866 T1FrekvensHZ
= 500000;
871 T1FrekvensHZ
= 2000000;
876 T1FrekvensHZ
= 6000000;
881 T1FrekvensHZ
= 24000000;
884 T1reload
= 256 - (u8
) (T1FrekvensHZ
/ (baud
* 2));
886 /* magic number here: ENTER_FIRMWARE_UPDATE; */
887 dataout
[DataCount
++] = IUU_UART_ESC
;
888 /* magic number here: CHANGE_BAUD; */
889 dataout
[DataCount
++] = IUU_UART_CHANGE
;
890 dataout
[DataCount
++] = T1Frekvens
;
891 dataout
[DataCount
++] = T1reload
;
893 *actual
= (T1FrekvensHZ
/ (256 - T1reload
)) / 2;
895 switch (parity
& 0x0F) {
896 case IUU_PARITY_NONE
:
897 dataout
[DataCount
++] = 0x00;
899 case IUU_PARITY_EVEN
:
900 dataout
[DataCount
++] = 0x01;
903 dataout
[DataCount
++] = 0x02;
905 case IUU_PARITY_MARK
:
906 dataout
[DataCount
++] = 0x03;
908 case IUU_PARITY_SPACE
:
909 dataout
[DataCount
++] = 0x04;
913 return IUU_INVALID_PARAMETER
;
917 switch (parity
& 0xF0) {
918 case IUU_ONE_STOP_BIT
:
919 dataout
[DataCount
- 1] |= IUU_ONE_STOP_BIT
;
922 case IUU_TWO_STOP_BITS
:
923 dataout
[DataCount
- 1] |= IUU_TWO_STOP_BITS
;
927 return IUU_INVALID_PARAMETER
;
931 status
= bulk_immediate(port
, dataout
, DataCount
);
932 if (status
!= IUU_OPERATION_OK
)
933 dbg("%s - uart_off error", __FUNCTION__
);
938 static int set_control_lines(struct usb_device
*dev
, u8 value
)
943 static void iuu_close(struct usb_serial_port
*port
, struct file
*filp
)
945 /* iuu_led (port,255,0,0,0); */
946 struct usb_serial
*serial
;
947 struct iuu_private
*priv
= usb_get_serial_port_data(port
);
949 unsigned int c_cflag
;
951 serial
= port
->serial
;
955 dbg("%s - port %d", __FUNCTION__
, port
->number
);
960 c_cflag
= port
->tty
->termios
->c_cflag
;
961 if (c_cflag
& HUPCL
) {
962 /* drop DTR and RTS */
963 priv
= usb_get_serial_port_data(port
);
964 spin_lock_irqsave(&priv
->lock
, flags
);
965 priv
->line_control
= 0;
966 spin_unlock_irqrestore(&priv
->lock
, flags
);
967 set_control_lines(port
->serial
->dev
, 0);
971 /* shutdown our urbs */
972 dbg("%s - shutting down urbs", __FUNCTION__
);
973 usb_kill_urb(port
->write_urb
);
974 usb_kill_urb(port
->read_urb
);
975 usb_kill_urb(port
->interrupt_in_urb
);
977 /* wait one second to free all buffers */
978 iuu_led(port
, 0, 0, 0xF000, 0xFF);
980 usb_reset_device(port
->serial
->dev
);
984 static int iuu_open(struct usb_serial_port
*port
, struct file
*filp
)
986 struct usb_serial
*serial
= port
->serial
;
991 struct iuu_private
*priv
= usb_get_serial_port_data(port
);
993 dbg("%s - port %d", __FUNCTION__
, port
->number
);
994 usb_clear_halt(serial
->dev
, port
->write_urb
->pipe
);
995 usb_clear_halt(serial
->dev
, port
->read_urb
->pipe
);
997 buf
= kmalloc(10, GFP_KERNEL
);
1001 /* fixup the endpoint buffer size */
1002 kfree(port
->bulk_out_buffer
);
1003 port
->bulk_out_buffer
= kmalloc(512, GFP_KERNEL
);
1004 port
->bulk_out_size
= 512;
1005 kfree(port
->bulk_in_buffer
);
1006 port
->bulk_in_buffer
= kmalloc(512, GFP_KERNEL
);
1007 port
->bulk_in_size
= 512;
1009 if (!port
->bulk_out_buffer
|| !port
->bulk_in_buffer
) {
1010 kfree(port
->bulk_out_buffer
);
1011 kfree(port
->bulk_in_buffer
);
1016 usb_fill_bulk_urb(port
->write_urb
, port
->serial
->dev
,
1017 usb_sndbulkpipe(port
->serial
->dev
,
1018 port
->bulk_out_endpointAddress
),
1019 port
->bulk_out_buffer
, 512,
1023 usb_fill_bulk_urb(port
->read_urb
, port
->serial
->dev
,
1024 usb_rcvbulkpipe(port
->serial
->dev
,
1025 port
->bulk_in_endpointAddress
),
1026 port
->bulk_in_buffer
, 512,
1029 /* set the termios structure */
1030 spin_lock_irqsave(&priv
->lock
, flags
);
1031 if (!priv
->termios_initialized
) {
1032 *(port
->tty
->termios
) = tty_std_termios
;
1033 port
->tty
->termios
->c_cflag
= CLOCAL
| CREAD
| CS8
| B9600
1034 | TIOCM_CTS
| CSTOPB
| PARENB
;
1035 port
->tty
->termios
->c_lflag
= 0;
1036 port
->tty
->termios
->c_oflag
= 0;
1037 port
->tty
->termios
->c_iflag
= 0;
1038 priv
->termios_initialized
= 1;
1039 port
->tty
->low_latency
= 1;
1042 spin_unlock_irqrestore(&priv
->lock
, flags
);
1044 /* initialize writebuf */
1045 #define FISH(a, b, c, d) do { \
1046 result = usb_control_msg(port->serial->dev, \
1047 usb_rcvctrlpipe(port->serial->dev, 0), \
1048 b, a, c, d, buf, 1, 1000); \
1049 dbg("0x%x:0x%x:0x%x:0x%x %d - %x", a, b, c, d, result, \
1050 buf[0]); } while (0);
1052 #define SOUP(a, b, c, d) do { \
1053 result = usb_control_msg(port->serial->dev, \
1054 usb_sndctrlpipe(port->serial->dev, 0), \
1055 b, a, c, d, NULL, 0, 1000); \
1056 dbg("0x%x:0x%x:0x%x:0x%x %d", a, b, c, d, result); } while (0)
1058 /* This is not UART related but IUU USB driver related or something */
1059 /* like that. Basically no IUU will accept any commands from the USB */
1060 /* host unless it has received the following message */
1061 /* sprintf(buf ,"%c%c%c%c",0x03,0x02,0x02,0x0); */
1063 SOUP(0x03, 0x02, 0x02, 0x0);
1065 iuu_led(port
, 0xF000, 0xF000, 0, 0xFF);
1069 switch (clockmode
) {
1070 case 2: /* 3.680 Mhz */
1071 iuu_clk(port
, IUU_CLK_3680000
* boost
/ 100);
1073 iuu_uart_baud(port
, 9600 * boost
/ 100, &actual
,
1076 case 3: /* 6.00 Mhz */
1077 iuu_clk(port
, IUU_CLK_6000000
* boost
/ 100);
1079 iuu_uart_baud(port
, 16457 * boost
/ 100, &actual
,
1082 default: /* 3.579 Mhz */
1083 iuu_clk(port
, IUU_CLK_3579000
* boost
/ 100);
1085 iuu_uart_baud(port
, 9600 * boost
/ 100, &actual
,
1089 /* set the cardin cardout signals */
1096 iuu_cardin
= TIOCM_CD
;
1101 iuu_cardout
= TIOCM_CD
;
1104 iuu_cardin
= TIOCM_DSR
;
1109 iuu_cardout
= TIOCM_DSR
;
1112 iuu_cardin
= TIOCM_CTS
;
1117 iuu_cardout
= TIOCM_CTS
;
1120 iuu_cardin
= TIOCM_RNG
;
1125 iuu_cardout
= TIOCM_RNG
;
1128 iuu_uart_flush(port
);
1130 dbg("%s - initialization done", __FUNCTION__
);
1132 memset(port
->write_urb
->transfer_buffer
, IUU_UART_RX
, 1);
1133 usb_fill_bulk_urb(port
->write_urb
, port
->serial
->dev
,
1134 usb_sndbulkpipe(port
->serial
->dev
,
1135 port
->bulk_out_endpointAddress
),
1136 port
->write_urb
->transfer_buffer
, 1,
1137 read_rxcmd_callback
, port
);
1138 result
= usb_submit_urb(port
->write_urb
, GFP_KERNEL
);
1141 dev_err(&port
->dev
, "%s - failed submitting read urb,"
1142 " error %d\n", __FUNCTION__
, result
);
1143 iuu_close(port
, NULL
);
1146 dbg("%s - rxcmd OK", __FUNCTION__
);
1151 static struct usb_serial_driver iuu_device
= {
1153 .owner
= THIS_MODULE
,
1154 .name
= "iuu_phoenix",
1156 .id_table
= id_table
,
1157 .num_interrupt_in
= NUM_DONT_CARE
,
1163 .write
= iuu_uart_write
,
1164 .read_bulk_callback
= iuu_uart_read_callback
,
1165 .tiocmget
= iuu_tiocmget
,
1166 .tiocmset
= iuu_tiocmset
,
1167 .attach
= iuu_startup
,
1168 .shutdown
= iuu_shutdown
,
1171 static int __init
iuu_init(void)
1174 retval
= usb_serial_register(&iuu_device
);
1176 goto failed_usb_serial_register
;
1177 retval
= usb_register(&iuu_driver
);
1179 goto failed_usb_register
;
1180 info(DRIVER_DESC
" " DRIVER_VERSION
);
1182 failed_usb_register
:
1183 usb_serial_deregister(&iuu_device
);
1184 failed_usb_serial_register
:
1188 static void __exit
iuu_exit(void)
1190 usb_deregister(&iuu_driver
);
1191 usb_serial_deregister(&iuu_device
);
1194 module_init(iuu_init
);
1195 module_exit(iuu_exit
);
1197 MODULE_AUTHOR("Alain Degreffe eczema@ecze.com");
1199 MODULE_DESCRIPTION(DRIVER_DESC
);
1200 MODULE_LICENSE("GPL");
1202 MODULE_VERSION(DRIVER_VERSION
);
1203 module_param(debug
, bool, S_IRUGO
| S_IWUSR
);
1204 MODULE_PARM_DESC(debug
, "Debug enabled or not");
1206 module_param(xmas
, bool, S_IRUGO
| S_IWUSR
);
1207 MODULE_PARM_DESC(xmas
, "xmas color enabled or not");
1209 module_param(boost
, int, S_IRUGO
| S_IWUSR
);
1210 MODULE_PARM_DESC(boost
, "overclock boost percent 100 to 500");
1212 module_param(clockmode
, int, S_IRUGO
| S_IWUSR
);
1213 MODULE_PARM_DESC(clockmode
, "1=3Mhz579,2=3Mhz680,3=6Mhz");
1215 module_param(cdmode
, int, S_IRUGO
| S_IWUSR
);
1216 MODULE_PARM_DESC(cdmode
, "Card detect mode 0=none, 1=CD, 2=!CD, 3=DSR, "
1217 "4=!DSR, 5=CTS, 6=!CTS, 7=RING, 8=!RING");