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
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
);
54 static struct usb_driver iuu_driver
= {
55 .name
= "iuu_phoenix",
56 .probe
= usb_serial_probe
,
57 .disconnect
= usb_serial_disconnect
,
63 static int boost
= 100;
64 static int clockmode
= 1;
65 static int cdmode
= 1;
66 static int iuu_cardin
;
67 static int iuu_cardout
;
69 static int vcc_default
= 5;
71 static void read_rxcmd_callback(struct urb
*urb
);
74 spinlock_t lock
; /* store irq state */
75 wait_queue_head_t delta_msr_wait
;
77 int tiostatus
; /* store IUART SIGNAL for tiocmget call */
78 u8 reset
; /* if 1 reset is needed */
79 int poll
; /* number of poll */
80 u8
*writebuf
; /* buffer for writing to device */
81 int writelen
; /* num of byte to write to device */
82 u8
*buf
; /* used for initialize speed */
83 u8
*dbgbuf
; /* debug buffer */
85 int vcc
; /* vcc (either 3 or 5 V) */
92 static void iuu_free_buf(struct iuu_private
*priv
)
96 kfree(priv
->writebuf
);
99 static int iuu_alloc_buf(struct iuu_private
*priv
)
101 priv
->buf
= kzalloc(256, GFP_KERNEL
);
102 priv
->dbgbuf
= kzalloc(256, GFP_KERNEL
);
103 priv
->writebuf
= kzalloc(256, GFP_KERNEL
);
104 if (!priv
->buf
|| !priv
->dbgbuf
|| !priv
->writebuf
) {
106 dbg("%s problem allocation buffer", __func__
);
109 dbg("%s - Privates buffers allocation success", __func__
);
113 static int iuu_startup(struct usb_serial
*serial
)
115 struct iuu_private
*priv
;
116 priv
= kzalloc(sizeof(struct iuu_private
), GFP_KERNEL
);
117 dbg("%s- priv allocation success", __func__
);
120 if (iuu_alloc_buf(priv
)) {
124 priv
->vcc
= vcc_default
;
125 spin_lock_init(&priv
->lock
);
126 init_waitqueue_head(&priv
->delta_msr_wait
);
127 usb_set_serial_port_data(serial
->port
[0], priv
);
131 /* Release function */
132 static void iuu_release(struct usb_serial
*serial
)
134 struct usb_serial_port
*port
= serial
->port
[0];
135 struct iuu_private
*priv
= usb_get_serial_port_data(port
);
143 dbg("%s - I will free all", __func__
);
144 usb_set_serial_port_data(port
, NULL
);
146 dbg("%s - priv is not anymore in port structure", __func__
);
149 dbg("%s priv is now kfree", __func__
);
153 static int iuu_tiocmset(struct tty_struct
*tty
, struct file
*file
,
154 unsigned int set
, unsigned int clear
)
156 struct usb_serial_port
*port
= tty
->driver_data
;
157 struct iuu_private
*priv
= usb_get_serial_port_data(port
);
160 dbg("%s (%d) msg : SET = 0x%04x, CLEAR = 0x%04x ", __func__
,
161 port
->number
, set
, clear
);
163 spin_lock_irqsave(&priv
->lock
, flags
);
165 if ((set
& TIOCM_RTS
) && !(priv
->tiostatus
== TIOCM_RTS
)) {
166 dbg("%s TIOCMSET RESET called !!!", __func__
);
170 priv
->tiostatus
= TIOCM_RTS
;
172 spin_unlock_irqrestore(&priv
->lock
, flags
);
176 /* This is used to provide a carrier detect mechanism
177 * When a card is present, the response is 0x00
178 * When no card , the reader respond with TIOCM_CD
179 * This is known as CD autodetect mechanism
181 static int iuu_tiocmget(struct tty_struct
*tty
, struct file
*file
)
183 struct usb_serial_port
*port
= tty
->driver_data
;
184 struct iuu_private
*priv
= usb_get_serial_port_data(port
);
188 spin_lock_irqsave(&priv
->lock
, flags
);
189 rc
= priv
->tiostatus
;
190 spin_unlock_irqrestore(&priv
->lock
, flags
);
195 static void iuu_rxcmd(struct urb
*urb
)
197 struct usb_serial_port
*port
= urb
->context
;
199 int status
= urb
->status
;
201 dbg("%s - enter", __func__
);
204 dbg("%s - status = %d", __func__
, status
);
210 memset(port
->write_urb
->transfer_buffer
, IUU_UART_RX
, 1);
211 usb_fill_bulk_urb(port
->write_urb
, port
->serial
->dev
,
212 usb_sndbulkpipe(port
->serial
->dev
,
213 port
->bulk_out_endpointAddress
),
214 port
->write_urb
->transfer_buffer
, 1,
215 read_rxcmd_callback
, port
);
216 result
= usb_submit_urb(port
->write_urb
, GFP_ATOMIC
);
219 static int iuu_reset(struct usb_serial_port
*port
, u8 wt
)
221 struct iuu_private
*priv
= usb_get_serial_port_data(port
);
223 char *buf_ptr
= port
->write_urb
->transfer_buffer
;
224 dbg("%s - enter", __func__
);
226 /* Prepare the reset sequence */
228 *buf_ptr
++ = IUU_RST_SET
;
229 *buf_ptr
++ = IUU_DELAY_MS
;
231 *buf_ptr
= IUU_RST_CLEAR
;
233 /* send the sequence */
235 usb_fill_bulk_urb(port
->write_urb
,
237 usb_sndbulkpipe(port
->serial
->dev
,
238 port
->bulk_out_endpointAddress
),
239 port
->write_urb
->transfer_buffer
, 4, iuu_rxcmd
, port
);
240 result
= usb_submit_urb(port
->write_urb
, GFP_ATOMIC
);
251 static void iuu_update_status_callback(struct urb
*urb
)
253 struct usb_serial_port
*port
= urb
->context
;
254 struct iuu_private
*priv
= usb_get_serial_port_data(port
);
256 int status
= urb
->status
;
258 dbg("%s - enter", __func__
);
261 dbg("%s - status = %d", __func__
, status
);
266 st
= urb
->transfer_buffer
;
267 dbg("%s - enter", __func__
);
268 if (urb
->actual_length
== 1) {
271 priv
->tiostatus
= iuu_cardout
;
274 priv
->tiostatus
= iuu_cardin
;
277 priv
->tiostatus
= iuu_cardin
;
283 static void iuu_status_callback(struct urb
*urb
)
285 struct usb_serial_port
*port
= urb
->context
;
287 int status
= urb
->status
;
289 dbg("%s - status = %d", __func__
, status
);
290 usb_fill_bulk_urb(port
->read_urb
, port
->serial
->dev
,
291 usb_rcvbulkpipe(port
->serial
->dev
,
292 port
->bulk_in_endpointAddress
),
293 port
->read_urb
->transfer_buffer
, 256,
294 iuu_update_status_callback
, port
);
295 result
= usb_submit_urb(port
->read_urb
, GFP_ATOMIC
);
298 static int iuu_status(struct usb_serial_port
*port
)
302 dbg("%s - enter", __func__
);
304 memset(port
->write_urb
->transfer_buffer
, IUU_GET_STATE_REGISTER
, 1);
305 usb_fill_bulk_urb(port
->write_urb
, port
->serial
->dev
,
306 usb_sndbulkpipe(port
->serial
->dev
,
307 port
->bulk_out_endpointAddress
),
308 port
->write_urb
->transfer_buffer
, 1,
309 iuu_status_callback
, port
);
310 result
= usb_submit_urb(port
->write_urb
, GFP_ATOMIC
);
315 static int bulk_immediate(struct usb_serial_port
*port
, u8
*buf
, u8 count
)
318 struct usb_serial
*serial
= port
->serial
;
321 dbg("%s - enter", __func__
);
323 /* send the data out the bulk port */
326 usb_bulk_msg(serial
->dev
,
327 usb_sndbulkpipe(serial
->dev
,
328 port
->bulk_out_endpointAddress
), buf
,
329 count
, &actual
, HZ
* 1);
331 if (status
!= IUU_OPERATION_OK
)
332 dbg("%s - error = %2x", __func__
, status
);
334 dbg("%s - write OK !", __func__
);
338 static int read_immediate(struct usb_serial_port
*port
, u8
*buf
, u8 count
)
341 struct usb_serial
*serial
= port
->serial
;
344 dbg("%s - enter", __func__
);
346 /* send the data out the bulk port */
349 usb_bulk_msg(serial
->dev
,
350 usb_rcvbulkpipe(serial
->dev
,
351 port
->bulk_in_endpointAddress
), buf
,
352 count
, &actual
, HZ
* 1);
354 if (status
!= IUU_OPERATION_OK
)
355 dbg("%s - error = %2x", __func__
, status
);
357 dbg("%s - read OK !", __func__
);
361 static int iuu_led(struct usb_serial_port
*port
, unsigned int R
,
362 unsigned int G
, unsigned int B
, u8 f
)
366 buf
= kmalloc(8, GFP_KERNEL
);
370 dbg("%s - enter", __func__
);
372 buf
[0] = IUU_SET_LED
;
374 buf
[2] = (R
>> 8) & 0xFF;
376 buf
[4] = (G
>> 8) & 0xFF;
378 buf
[6] = (B
>> 8) & 0xFF;
380 status
= bulk_immediate(port
, buf
, 8);
382 if (status
!= IUU_OPERATION_OK
)
383 dbg("%s - led error status = %2x", __func__
, status
);
385 dbg("%s - led OK !", __func__
);
386 return IUU_OPERATION_OK
;
389 static void iuu_rgbf_fill_buffer(u8
*buf
, u8 r1
, u8 r2
, u8 g1
, u8 g2
, u8 b1
,
392 *buf
++ = IUU_SET_LED
;
402 static void iuu_led_activity_on(struct urb
*urb
)
404 struct usb_serial_port
*port
= urb
->context
;
406 char *buf_ptr
= port
->write_urb
->transfer_buffer
;
407 *buf_ptr
++ = IUU_SET_LED
;
409 get_random_bytes(buf_ptr
, 6);
412 iuu_rgbf_fill_buffer(buf_ptr
, 255, 255, 0, 0, 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
);
423 static void iuu_led_activity_off(struct urb
*urb
)
425 struct usb_serial_port
*port
= urb
->context
;
427 char *buf_ptr
= port
->write_urb
->transfer_buffer
;
432 *buf_ptr
++ = IUU_SET_LED
;
433 iuu_rgbf_fill_buffer(buf_ptr
, 0, 0, 255, 255, 0, 0, 255);
435 usb_fill_bulk_urb(port
->write_urb
, port
->serial
->dev
,
436 usb_sndbulkpipe(port
->serial
->dev
,
437 port
->bulk_out_endpointAddress
),
438 port
->write_urb
->transfer_buffer
, 8 ,
440 result
= usb_submit_urb(port
->write_urb
, GFP_ATOMIC
);
445 static int iuu_clk(struct usb_serial_port
*port
, int dwFrq
)
448 struct iuu_private
*priv
= usb_get_serial_port_data(port
);
451 u8 DIV
= 0; /* 8bit */
452 u8 XDRV
= 0; /* 8bit */
453 u8 PUMP
= 0; /* 3bit */
454 u8 PBmsb
= 0; /* 2bit */
455 u8 PBlsb
= 0; /* 8bit */
456 u8 PO
= 0; /* 1bit */
461 int frq
= (int)dwFrq
;
463 dbg("%s - enter", __func__
);
466 priv
->buf
[Count
++] = IUU_UART_WRITE_I2C
;
467 priv
->buf
[Count
++] = FrqGenAdr
<< 1;
468 priv
->buf
[Count
++] = 0x09;
469 priv
->buf
[Count
++] = 0x00;
471 status
= bulk_immediate(port
, (u8
*) priv
->buf
, Count
);
473 dbg("%s - write error ", __func__
);
476 } else if (frq
== 3579000) {
481 } else if (frq
== 3680000) {
486 } else if (frq
== 6000000) {
492 unsigned int result
= 0;
493 unsigned int tmp
= 0;
498 unsigned int lP
= 2055;
499 unsigned int lDiv
= 4;
501 for (lQ
= 2; lQ
<= 47 && !found
; lQ
++)
502 for (lP
= 2055; lP
>= 8 && !found
; lP
--)
503 for (lDiv
= 4; lDiv
<= 127 && !found
; lDiv
++) {
504 tmp
= (12000000 / lDiv
) * (lP
/ lQ
);
505 if (abs((int)(tmp
- frq
)) <
506 abs((int)(frq
- result
))) {
507 check2
= (12000000 / lQ
);
510 check
= (12000000 / lQ
) * lP
;
511 if (check
> 400000000)
513 if (check
< 100000000)
515 if (lDiv
< 4 || lDiv
> 127)
526 P2
= ((P
- PO
) / 2) - 4;
529 PBmsb
= (P2
>> 8 & 0x03);
531 PO
= (P
>> 10) & 0x01;
534 priv
->buf
[Count
++] = IUU_UART_WRITE_I2C
; /* 0x4C */
535 priv
->buf
[Count
++] = FrqGenAdr
<< 1;
536 priv
->buf
[Count
++] = 0x09;
537 priv
->buf
[Count
++] = 0x20; /* Adr = 0x09 */
538 priv
->buf
[Count
++] = IUU_UART_WRITE_I2C
; /* 0x4C */
539 priv
->buf
[Count
++] = FrqGenAdr
<< 1;
540 priv
->buf
[Count
++] = 0x0C;
541 priv
->buf
[Count
++] = DIV
; /* Adr = 0x0C */
542 priv
->buf
[Count
++] = IUU_UART_WRITE_I2C
; /* 0x4C */
543 priv
->buf
[Count
++] = FrqGenAdr
<< 1;
544 priv
->buf
[Count
++] = 0x12;
545 priv
->buf
[Count
++] = XDRV
; /* Adr = 0x12 */
546 priv
->buf
[Count
++] = IUU_UART_WRITE_I2C
; /* 0x4C */
547 priv
->buf
[Count
++] = FrqGenAdr
<< 1;
548 priv
->buf
[Count
++] = 0x13;
549 priv
->buf
[Count
++] = 0x6B; /* Adr = 0x13 */
550 priv
->buf
[Count
++] = IUU_UART_WRITE_I2C
; /* 0x4C */
551 priv
->buf
[Count
++] = FrqGenAdr
<< 1;
552 priv
->buf
[Count
++] = 0x40;
553 priv
->buf
[Count
++] = (0xC0 | ((PUMP
& 0x07) << 2)) |
554 (PBmsb
& 0x03); /* Adr = 0x40 */
555 priv
->buf
[Count
++] = IUU_UART_WRITE_I2C
; /* 0x4C */
556 priv
->buf
[Count
++] = FrqGenAdr
<< 1;
557 priv
->buf
[Count
++] = 0x41;
558 priv
->buf
[Count
++] = PBlsb
; /* Adr = 0x41 */
559 priv
->buf
[Count
++] = IUU_UART_WRITE_I2C
; /* 0x4C */
560 priv
->buf
[Count
++] = FrqGenAdr
<< 1;
561 priv
->buf
[Count
++] = 0x42;
562 priv
->buf
[Count
++] = Q
| (((PO
& 0x01) << 7)); /* Adr = 0x42 */
563 priv
->buf
[Count
++] = IUU_UART_WRITE_I2C
; /* 0x4C */
564 priv
->buf
[Count
++] = FrqGenAdr
<< 1;
565 priv
->buf
[Count
++] = 0x44;
566 priv
->buf
[Count
++] = (char)0xFF; /* Adr = 0x44 */
567 priv
->buf
[Count
++] = IUU_UART_WRITE_I2C
; /* 0x4C */
568 priv
->buf
[Count
++] = FrqGenAdr
<< 1;
569 priv
->buf
[Count
++] = 0x45;
570 priv
->buf
[Count
++] = (char)0xFE; /* Adr = 0x45 */
571 priv
->buf
[Count
++] = IUU_UART_WRITE_I2C
; /* 0x4C */
572 priv
->buf
[Count
++] = FrqGenAdr
<< 1;
573 priv
->buf
[Count
++] = 0x46;
574 priv
->buf
[Count
++] = 0x7F; /* Adr = 0x46 */
575 priv
->buf
[Count
++] = IUU_UART_WRITE_I2C
; /* 0x4C */
576 priv
->buf
[Count
++] = FrqGenAdr
<< 1;
577 priv
->buf
[Count
++] = 0x47;
578 priv
->buf
[Count
++] = (char)0x84; /* Adr = 0x47 */
580 status
= bulk_immediate(port
, (u8
*) priv
->buf
, Count
);
581 if (status
!= IUU_OPERATION_OK
)
582 dbg("%s - write error ", __func__
);
586 static int iuu_uart_flush(struct usb_serial_port
*port
)
590 u8 rxcmd
= IUU_UART_RX
;
591 struct iuu_private
*priv
= usb_get_serial_port_data(port
);
593 dbg("%s - enter", __func__
);
595 if (iuu_led(port
, 0xF000, 0, 0, 0xFF) < 0)
598 for (i
= 0; i
< 2; i
++) {
599 status
= bulk_immediate(port
, &rxcmd
, 1);
600 if (status
!= IUU_OPERATION_OK
) {
601 dbg("%s - uart_flush_write error", __func__
);
605 status
= read_immediate(port
, &priv
->len
, 1);
606 if (status
!= IUU_OPERATION_OK
) {
607 dbg("%s - uart_flush_read error", __func__
);
612 dbg("%s - uart_flush datalen is : %i ", __func__
,
614 status
= read_immediate(port
, priv
->buf
, priv
->len
);
615 if (status
!= IUU_OPERATION_OK
) {
616 dbg("%s - uart_flush_read error", __func__
);
621 dbg("%s - uart_flush_read OK!", __func__
);
622 iuu_led(port
, 0, 0xF000, 0, 0xFF);
626 static void read_buf_callback(struct urb
*urb
)
628 struct usb_serial_port
*port
= urb
->context
;
629 unsigned char *data
= urb
->transfer_buffer
;
630 struct tty_struct
*tty
;
631 int status
= urb
->status
;
633 dbg("%s - status = %d", __func__
, status
);
636 if (status
== -EPROTO
) {
637 /* reschedule needed */
642 dbg("%s - %i chars to write", __func__
, urb
->actual_length
);
643 tty
= tty_port_tty_get(&port
->port
);
645 dbg("%s - data is NULL !!!", __func__
);
646 if (tty
&& urb
->actual_length
&& data
) {
647 tty_insert_flip_string(tty
, data
, urb
->actual_length
);
648 tty_flip_buffer_push(tty
);
651 iuu_led_activity_on(urb
);
654 static int iuu_bulk_write(struct usb_serial_port
*port
)
656 struct iuu_private
*priv
= usb_get_serial_port_data(port
);
661 char *buf_ptr
= port
->write_urb
->transfer_buffer
;
662 dbg("%s - enter", __func__
);
664 spin_lock_irqsave(&priv
->lock
, flags
);
665 *buf_ptr
++ = IUU_UART_ESC
;
666 *buf_ptr
++ = IUU_UART_TX
;
667 *buf_ptr
++ = priv
->writelen
;
669 memcpy(buf_ptr
, priv
->writebuf
, priv
->writelen
);
670 buf_len
= priv
->writelen
;
672 spin_unlock_irqrestore(&priv
->lock
, flags
);
674 for (i
= 0; i
< buf_len
; i
++)
675 sprintf(priv
->dbgbuf
+ i
*2 ,
676 "%02X", priv
->writebuf
[i
]);
677 priv
->dbgbuf
[buf_len
+i
*2] = 0;
678 dbg("%s - writing %i chars : %s", __func__
,
679 buf_len
, priv
->dbgbuf
);
681 usb_fill_bulk_urb(port
->write_urb
, port
->serial
->dev
,
682 usb_sndbulkpipe(port
->serial
->dev
,
683 port
->bulk_out_endpointAddress
),
684 port
->write_urb
->transfer_buffer
, buf_len
+ 3,
686 result
= usb_submit_urb(port
->write_urb
, GFP_ATOMIC
);
687 usb_serial_port_softint(port
);
691 static int iuu_read_buf(struct usb_serial_port
*port
, int len
)
694 dbg("%s - enter", __func__
);
696 usb_fill_bulk_urb(port
->read_urb
, port
->serial
->dev
,
697 usb_rcvbulkpipe(port
->serial
->dev
,
698 port
->bulk_in_endpointAddress
),
699 port
->read_urb
->transfer_buffer
, len
,
700 read_buf_callback
, port
);
701 result
= usb_submit_urb(port
->read_urb
, GFP_ATOMIC
);
705 static void iuu_uart_read_callback(struct urb
*urb
)
707 struct usb_serial_port
*port
= urb
->context
;
708 struct iuu_private
*priv
= usb_get_serial_port_data(port
);
710 int status
= urb
->status
;
713 unsigned char *data
= urb
->transfer_buffer
;
716 dbg("%s - enter", __func__
);
719 dbg("%s - status = %d", __func__
, status
);
724 dbg("%s - data is NULL !!!", __func__
);
726 if (urb
->actual_length
== 1 && data
!= NULL
)
729 if (urb
->actual_length
> 1) {
730 dbg("%s - urb->actual_length = %i", __func__
,
735 /* if len > 0 call readbuf */
737 if (len
> 0 && error
== 0) {
738 dbg("%s - call read buf - len to read is %i ",
740 status
= iuu_read_buf(port
, len
);
743 /* need to update status ? */
744 if (priv
->poll
> 99) {
745 status
= iuu_status(port
);
750 /* reset waiting ? */
752 if (priv
->reset
== 1) {
753 status
= iuu_reset(port
, 0xC);
756 /* Writebuf is waiting */
757 spin_lock_irqsave(&priv
->lock
, flags
);
758 if (priv
->writelen
> 0) {
759 spin_unlock_irqrestore(&priv
->lock
, flags
);
760 status
= iuu_bulk_write(port
);
763 spin_unlock_irqrestore(&priv
->lock
, flags
);
764 /* if nothing to write call again rxcmd */
765 dbg("%s - rxcmd recall", __func__
);
766 iuu_led_activity_off(urb
);
769 static int iuu_uart_write(struct tty_struct
*tty
, struct usb_serial_port
*port
,
770 const u8
*buf
, int count
)
772 struct iuu_private
*priv
= usb_get_serial_port_data(port
);
774 dbg("%s - enter", __func__
);
779 spin_lock_irqsave(&priv
->lock
, flags
);
781 /* fill the buffer */
782 memcpy(priv
->writebuf
+ priv
->writelen
, buf
, count
);
783 priv
->writelen
+= count
;
784 spin_unlock_irqrestore(&priv
->lock
, flags
);
789 static void read_rxcmd_callback(struct urb
*urb
)
791 struct usb_serial_port
*port
= urb
->context
;
793 int status
= urb
->status
;
795 dbg("%s - status = %d", __func__
, status
);
802 usb_fill_bulk_urb(port
->read_urb
, port
->serial
->dev
,
803 usb_rcvbulkpipe(port
->serial
->dev
,
804 port
->bulk_in_endpointAddress
),
805 port
->read_urb
->transfer_buffer
, 256,
806 iuu_uart_read_callback
, port
);
807 result
= usb_submit_urb(port
->read_urb
, GFP_ATOMIC
);
808 dbg("%s - submit result = %d", __func__
, result
);
812 static int iuu_uart_on(struct usb_serial_port
*port
)
817 buf
= kmalloc(sizeof(u8
) * 4, GFP_KERNEL
);
822 buf
[0] = IUU_UART_ENABLE
;
823 buf
[1] = (u8
) ((IUU_BAUD_9600
>> 8) & 0x00FF);
824 buf
[2] = (u8
) (0x00FF & IUU_BAUD_9600
);
825 buf
[3] = (u8
) (0x0F0 & IUU_ONE_STOP_BIT
) | (0x07 & IUU_PARITY_EVEN
);
827 status
= bulk_immediate(port
, buf
, 4);
828 if (status
!= IUU_OPERATION_OK
) {
829 dbg("%s - uart_on error", __func__
);
830 goto uart_enable_failed
;
832 /* iuu_reset() the card after iuu_uart_on() */
833 status
= iuu_uart_flush(port
);
834 if (status
!= IUU_OPERATION_OK
)
835 dbg("%s - uart_flush error", __func__
);
841 /* Diables the IUU UART (a.k.a. the Phoenix voiderface) */
842 static int iuu_uart_off(struct usb_serial_port
*port
)
846 buf
= kmalloc(1, GFP_KERNEL
);
849 buf
[0] = IUU_UART_DISABLE
;
851 status
= bulk_immediate(port
, buf
, 1);
852 if (status
!= IUU_OPERATION_OK
)
853 dbg("%s - uart_off error", __func__
);
859 static int iuu_uart_baud(struct usb_serial_port
*port
, u32 baud_base
,
860 u32
*actual
, u8 parity
)
868 unsigned int T1FrekvensHZ
= 0;
870 dbg("%s - enter baud_base=%d", __func__
, baud_base
);
871 dataout
= kmalloc(sizeof(u8
) * 5, GFP_KERNEL
);
875 /*baud = (((priv->clk / 35) * baud_base) / 100000); */
878 if (baud
< 1200 || baud
> 230400) {
880 return IUU_INVALID_PARAMETER
;
884 T1FrekvensHZ
= 500000;
889 T1FrekvensHZ
= 2000000;
894 T1FrekvensHZ
= 6000000;
899 T1FrekvensHZ
= 24000000;
902 T1reload
= 256 - (u8
) (T1FrekvensHZ
/ (baud
* 2));
904 /* magic number here: ENTER_FIRMWARE_UPDATE; */
905 dataout
[DataCount
++] = IUU_UART_ESC
;
906 /* magic number here: CHANGE_BAUD; */
907 dataout
[DataCount
++] = IUU_UART_CHANGE
;
908 dataout
[DataCount
++] = T1Frekvens
;
909 dataout
[DataCount
++] = T1reload
;
911 *actual
= (T1FrekvensHZ
/ (256 - T1reload
)) / 2;
913 switch (parity
& 0x0F) {
914 case IUU_PARITY_NONE
:
915 dataout
[DataCount
++] = 0x00;
917 case IUU_PARITY_EVEN
:
918 dataout
[DataCount
++] = 0x01;
921 dataout
[DataCount
++] = 0x02;
923 case IUU_PARITY_MARK
:
924 dataout
[DataCount
++] = 0x03;
926 case IUU_PARITY_SPACE
:
927 dataout
[DataCount
++] = 0x04;
931 return IUU_INVALID_PARAMETER
;
935 switch (parity
& 0xF0) {
936 case IUU_ONE_STOP_BIT
:
937 dataout
[DataCount
- 1] |= IUU_ONE_STOP_BIT
;
940 case IUU_TWO_STOP_BITS
:
941 dataout
[DataCount
- 1] |= IUU_TWO_STOP_BITS
;
945 return IUU_INVALID_PARAMETER
;
949 status
= bulk_immediate(port
, dataout
, DataCount
);
950 if (status
!= IUU_OPERATION_OK
)
951 dbg("%s - uart_off error", __func__
);
956 static void iuu_set_termios(struct tty_struct
*tty
,
957 struct usb_serial_port
*port
, struct ktermios
*old_termios
)
959 const u32 supported_mask
= CMSPAR
|PARENB
|PARODD
;
960 struct iuu_private
*priv
= usb_get_serial_port_data(port
);
961 unsigned int cflag
= tty
->termios
->c_cflag
;
967 u32 newval
= cflag
& supported_mask
;
969 /* Just use the ospeed. ispeed should be the same. */
970 baud
= tty
->termios
->c_ospeed
;
972 dbg("%s - enter c_ospeed or baud=%d", __func__
, baud
);
974 /* compute the parity parameter */
976 if (cflag
& CMSPAR
) { /* Using mark space */
978 parity
|= IUU_PARITY_SPACE
;
980 parity
|= IUU_PARITY_MARK
;
981 } else if (!(cflag
& PARENB
)) {
982 parity
|= IUU_PARITY_NONE
;
984 } else if (cflag
& PARODD
)
985 parity
|= IUU_PARITY_ODD
;
987 parity
|= IUU_PARITY_EVEN
;
989 parity
|= (cflag
& CSTOPB
? IUU_TWO_STOP_BITS
: IUU_ONE_STOP_BIT
);
992 status
= iuu_uart_baud(port
,
993 baud
* priv
->boost
/ 100,
996 /* set the termios value to the real one, so the user now what has
997 * changed. We support few fields so its easies to copy the old hw
998 * settings back over and then adjust them
1001 tty_termios_copy_hw(tty
->termios
, old_termios
);
1002 if (status
!= 0) /* Set failed - return old bits */
1004 /* Re-encode speed, parity and csize */
1005 tty_encode_baud_rate(tty
, baud
, baud
);
1006 tty
->termios
->c_cflag
&= ~(supported_mask
|CSIZE
);
1007 tty
->termios
->c_cflag
|= newval
| csize
;
1010 static void iuu_close(struct usb_serial_port
*port
)
1012 /* iuu_led (port,255,0,0,0); */
1013 struct usb_serial
*serial
;
1015 serial
= port
->serial
;
1019 dbg("%s - port %d", __func__
, port
->number
);
1024 /* shutdown our urbs */
1025 dbg("%s - shutting down urbs", __func__
);
1026 usb_kill_urb(port
->write_urb
);
1027 usb_kill_urb(port
->read_urb
);
1028 usb_kill_urb(port
->interrupt_in_urb
);
1029 iuu_led(port
, 0, 0, 0xF000, 0xFF);
1033 static void iuu_init_termios(struct tty_struct
*tty
)
1035 dbg("%s - enter", __func__
);
1036 *(tty
->termios
) = tty_std_termios
;
1037 tty
->termios
->c_cflag
= CLOCAL
| CREAD
| CS8
| B9600
1038 | TIOCM_CTS
| CSTOPB
| PARENB
;
1039 tty
->termios
->c_ispeed
= 9600;
1040 tty
->termios
->c_ospeed
= 9600;
1041 tty
->termios
->c_lflag
= 0;
1042 tty
->termios
->c_oflag
= 0;
1043 tty
->termios
->c_iflag
= 0;
1046 static int iuu_open(struct tty_struct
*tty
, struct usb_serial_port
*port
)
1048 struct usb_serial
*serial
= port
->serial
;
1053 struct iuu_private
*priv
= usb_get_serial_port_data(port
);
1055 baud
= tty
->termios
->c_ospeed
;
1056 tty
->termios
->c_ispeed
= baud
;
1057 /* Re-encode speed */
1058 tty_encode_baud_rate(tty
, baud
, baud
);
1060 dbg("%s - port %d, baud %d", __func__
, port
->number
, baud
);
1061 usb_clear_halt(serial
->dev
, port
->write_urb
->pipe
);
1062 usb_clear_halt(serial
->dev
, port
->read_urb
->pipe
);
1064 buf
= kmalloc(10, GFP_KERNEL
);
1070 /* initialize writebuf */
1071 #define FISH(a, b, c, d) do { \
1072 result = usb_control_msg(port->serial->dev, \
1073 usb_rcvctrlpipe(port->serial->dev, 0), \
1074 b, a, c, d, buf, 1, 1000); \
1075 dbg("0x%x:0x%x:0x%x:0x%x %d - %x", a, b, c, d, result, \
1076 buf[0]); } while (0);
1078 #define SOUP(a, b, c, d) do { \
1079 result = usb_control_msg(port->serial->dev, \
1080 usb_sndctrlpipe(port->serial->dev, 0), \
1081 b, a, c, d, NULL, 0, 1000); \
1082 dbg("0x%x:0x%x:0x%x:0x%x %d", a, b, c, d, result); } while (0)
1084 /* This is not UART related but IUU USB driver related or something */
1085 /* like that. Basically no IUU will accept any commands from the USB */
1086 /* host unless it has received the following message */
1087 /* sprintf(buf ,"%c%c%c%c",0x03,0x02,0x02,0x0); */
1089 SOUP(0x03, 0x02, 0x02, 0x0);
1091 iuu_led(port
, 0xF000, 0xF000, 0, 0xFF);
1095 priv
->boost
= boost
;
1097 switch (clockmode
) {
1098 case 2: /* 3.680 Mhz */
1099 priv
->clk
= IUU_CLK_3680000
;
1100 iuu_clk(port
, IUU_CLK_3680000
* boost
/ 100);
1102 iuu_uart_baud(port
, baud
* boost
/ 100, &actual
,
1105 case 3: /* 6.00 Mhz */
1106 iuu_clk(port
, IUU_CLK_6000000
* boost
/ 100);
1107 priv
->clk
= IUU_CLK_6000000
;
1108 /* Ratio of 6000000 to 3500000 for baud 9600 */
1110 iuu_uart_baud(port
, 16457 * boost
/ 100, &actual
,
1113 default: /* 3.579 Mhz */
1114 iuu_clk(port
, IUU_CLK_3579000
* boost
/ 100);
1115 priv
->clk
= IUU_CLK_3579000
;
1117 iuu_uart_baud(port
, baud
* boost
/ 100, &actual
,
1121 /* set the cardin cardout signals */
1128 iuu_cardin
= TIOCM_CD
;
1133 iuu_cardout
= TIOCM_CD
;
1136 iuu_cardin
= TIOCM_DSR
;
1141 iuu_cardout
= TIOCM_DSR
;
1144 iuu_cardin
= TIOCM_CTS
;
1149 iuu_cardout
= TIOCM_CTS
;
1152 iuu_cardin
= TIOCM_RNG
;
1157 iuu_cardout
= TIOCM_RNG
;
1160 iuu_uart_flush(port
);
1162 dbg("%s - initialization done", __func__
);
1164 memset(port
->write_urb
->transfer_buffer
, IUU_UART_RX
, 1);
1165 usb_fill_bulk_urb(port
->write_urb
, port
->serial
->dev
,
1166 usb_sndbulkpipe(port
->serial
->dev
,
1167 port
->bulk_out_endpointAddress
),
1168 port
->write_urb
->transfer_buffer
, 1,
1169 read_rxcmd_callback
, port
);
1170 result
= usb_submit_urb(port
->write_urb
, GFP_KERNEL
);
1173 dev_err(&port
->dev
, "%s - failed submitting read urb,"
1174 " error %d\n", __func__
, result
);
1178 dbg("%s - rxcmd OK", __func__
);
1183 /* how to change VCC */
1184 static int iuu_vcc_set(struct usb_serial_port
*port
, unsigned int vcc
)
1189 buf
= kmalloc(5, GFP_KERNEL
);
1193 dbg("%s - enter", __func__
);
1195 buf
[0] = IUU_SET_VCC
;
1196 buf
[1] = vcc
& 0xFF;
1197 buf
[2] = (vcc
>> 8) & 0xFF;
1198 buf
[3] = (vcc
>> 16) & 0xFF;
1199 buf
[4] = (vcc
>> 24) & 0xFF;
1201 status
= bulk_immediate(port
, buf
, 5);
1204 if (status
!= IUU_OPERATION_OK
)
1205 dbg("%s - vcc error status = %2x", __func__
, status
);
1207 dbg("%s - vcc OK !", __func__
);
1216 static ssize_t
show_vcc_mode(struct device
*dev
,
1217 struct device_attribute
*attr
, char *buf
)
1219 struct usb_serial_port
*port
= to_usb_serial_port(dev
);
1220 struct iuu_private
*priv
= usb_get_serial_port_data(port
);
1222 return sprintf(buf
, "%d\n", priv
->vcc
);
1225 static ssize_t
store_vcc_mode(struct device
*dev
,
1226 struct device_attribute
*attr
, const char *buf
, size_t count
)
1228 struct usb_serial_port
*port
= to_usb_serial_port(dev
);
1229 struct iuu_private
*priv
= usb_get_serial_port_data(port
);
1232 if (strict_strtoul(buf
, 10, &v
)) {
1233 dev_err(dev
, "%s - vcc_mode: %s is not a unsigned long\n",
1235 goto fail_store_vcc_mode
;
1238 dbg("%s: setting vcc_mode = %ld", __func__
, v
);
1240 if ((v
!= 3) && (v
!= 5)) {
1241 dev_err(dev
, "%s - vcc_mode %ld is invalid\n", __func__
, v
);
1243 iuu_vcc_set(port
, v
);
1246 fail_store_vcc_mode
:
1250 static DEVICE_ATTR(vcc_mode
, S_IRUSR
| S_IWUSR
, show_vcc_mode
,
1253 static int iuu_create_sysfs_attrs(struct usb_serial_port
*port
)
1255 dbg("%s", __func__
);
1257 return device_create_file(&port
->dev
, &dev_attr_vcc_mode
);
1260 static int iuu_remove_sysfs_attrs(struct usb_serial_port
*port
)
1262 dbg("%s", __func__
);
1264 device_remove_file(&port
->dev
, &dev_attr_vcc_mode
);
1269 * End Sysfs Attributes
1272 static struct usb_serial_driver iuu_device
= {
1274 .owner
= THIS_MODULE
,
1275 .name
= "iuu_phoenix",
1277 .id_table
= id_table
,
1278 .usb_driver
= &iuu_driver
,
1280 .bulk_in_size
= 512,
1281 .bulk_out_size
= 512,
1282 .port_probe
= iuu_create_sysfs_attrs
,
1283 .port_remove
= iuu_remove_sysfs_attrs
,
1286 .write
= iuu_uart_write
,
1287 .read_bulk_callback
= iuu_uart_read_callback
,
1288 .tiocmget
= iuu_tiocmget
,
1289 .tiocmset
= iuu_tiocmset
,
1290 .set_termios
= iuu_set_termios
,
1291 .init_termios
= iuu_init_termios
,
1292 .attach
= iuu_startup
,
1293 .release
= iuu_release
,
1296 static int __init
iuu_init(void)
1299 retval
= usb_serial_register(&iuu_device
);
1301 goto failed_usb_serial_register
;
1302 retval
= usb_register(&iuu_driver
);
1304 goto failed_usb_register
;
1305 printk(KERN_INFO KBUILD_MODNAME
": " DRIVER_VERSION
":"
1308 failed_usb_register
:
1309 usb_serial_deregister(&iuu_device
);
1310 failed_usb_serial_register
:
1314 static void __exit
iuu_exit(void)
1316 usb_deregister(&iuu_driver
);
1317 usb_serial_deregister(&iuu_device
);
1320 module_init(iuu_init
);
1321 module_exit(iuu_exit
);
1323 MODULE_AUTHOR("Alain Degreffe eczema@ecze.com");
1325 MODULE_DESCRIPTION(DRIVER_DESC
);
1326 MODULE_LICENSE("GPL");
1328 MODULE_VERSION(DRIVER_VERSION
);
1329 module_param(debug
, bool, S_IRUGO
| S_IWUSR
);
1330 MODULE_PARM_DESC(debug
, "Debug enabled or not");
1332 module_param(xmas
, bool, S_IRUGO
| S_IWUSR
);
1333 MODULE_PARM_DESC(xmas
, "Xmas colors enabled or not");
1335 module_param(boost
, int, S_IRUGO
| S_IWUSR
);
1336 MODULE_PARM_DESC(boost
, "Card overclock boost (in percent 100-500)");
1338 module_param(clockmode
, int, S_IRUGO
| S_IWUSR
);
1339 MODULE_PARM_DESC(clockmode
, "Card clock mode (1=3.579 MHz, 2=3.680 MHz, "
1342 module_param(cdmode
, int, S_IRUGO
| S_IWUSR
);
1343 MODULE_PARM_DESC(cdmode
, "Card detect mode (0=none, 1=CD, 2=!CD, 3=DSR, "
1344 "4=!DSR, 5=CTS, 6=!CTS, 7=RING, 8=!RING)");
1346 module_param(vcc_default
, int, S_IRUGO
| S_IWUSR
);
1347 MODULE_PARM_DESC(vcc_default
, "Set default VCC (either 3 for 3.3V or 5 "
1348 "for 5V). Default to 5.");