1 // SPDX-License-Identifier: GPL-2.0+
3 * Infinity Unlimited USB Phoenix driver
5 * Copyright (C) 2010 James Courtier-Dutton (James@superbug.co.uk)
7 * Copyright (C) 2007 Alain Degreffe (eczema@ecze.com)
9 * Original code taken from iuutool (Copyright (C) 2006 Juan Carlos Borrás)
11 * And tested with help of WB Electronics
13 #include <linux/kernel.h>
14 #include <linux/errno.h>
15 #include <linux/slab.h>
16 #include <linux/tty.h>
17 #include <linux/tty_driver.h>
18 #include <linux/tty_flip.h>
19 #include <linux/serial.h>
20 #include <linux/module.h>
21 #include <linux/moduleparam.h>
22 #include <linux/spinlock.h>
23 #include <linux/uaccess.h>
24 #include <linux/usb.h>
25 #include <linux/usb/serial.h>
26 #include "iuu_phoenix.h"
27 #include <linux/random.h>
29 #define DRIVER_DESC "Infinity USB Unlimited Phoenix driver"
31 static const struct usb_device_id id_table
[] = {
32 {USB_DEVICE(IUU_USB_VENDOR_ID
, IUU_USB_PRODUCT_ID
)},
33 {} /* Terminating entry */
35 MODULE_DEVICE_TABLE(usb
, id_table
);
38 static int boost
= 100;
39 static int clockmode
= 1;
40 static int cdmode
= 1;
41 static int iuu_cardin
;
42 static int iuu_cardout
;
44 static int vcc_default
= 5;
46 static int iuu_create_sysfs_attrs(struct usb_serial_port
*port
);
47 static int iuu_remove_sysfs_attrs(struct usb_serial_port
*port
);
48 static void read_rxcmd_callback(struct urb
*urb
);
51 spinlock_t lock
; /* store irq state */
53 int tiostatus
; /* store IUART SIGNAL for tiocmget call */
54 u8 reset
; /* if 1 reset is needed */
55 int poll
; /* number of poll */
56 u8
*writebuf
; /* buffer for writing to device */
57 int writelen
; /* num of byte to write to device */
58 u8
*buf
; /* used for initialize speed */
60 int vcc
; /* vcc (either 3 or 5 V) */
65 static int iuu_port_probe(struct usb_serial_port
*port
)
67 struct iuu_private
*priv
;
70 priv
= kzalloc(sizeof(struct iuu_private
), GFP_KERNEL
);
74 priv
->buf
= kzalloc(256, GFP_KERNEL
);
80 priv
->writebuf
= kzalloc(256, GFP_KERNEL
);
81 if (!priv
->writebuf
) {
87 priv
->vcc
= vcc_default
;
88 spin_lock_init(&priv
->lock
);
90 usb_set_serial_port_data(port
, priv
);
92 ret
= iuu_create_sysfs_attrs(port
);
94 kfree(priv
->writebuf
);
103 static int iuu_port_remove(struct usb_serial_port
*port
)
105 struct iuu_private
*priv
= usb_get_serial_port_data(port
);
107 iuu_remove_sysfs_attrs(port
);
108 kfree(priv
->writebuf
);
115 static int iuu_tiocmset(struct tty_struct
*tty
,
116 unsigned int set
, unsigned int clear
)
118 struct usb_serial_port
*port
= tty
->driver_data
;
119 struct iuu_private
*priv
= usb_get_serial_port_data(port
);
122 /* FIXME: locking on tiomstatus */
123 dev_dbg(&port
->dev
, "%s msg : SET = 0x%04x, CLEAR = 0x%04x\n",
124 __func__
, set
, clear
);
126 spin_lock_irqsave(&priv
->lock
, flags
);
128 if ((set
& TIOCM_RTS
) && !(priv
->tiostatus
== TIOCM_RTS
)) {
129 dev_dbg(&port
->dev
, "%s TIOCMSET RESET called !!!\n", __func__
);
133 priv
->tiostatus
= TIOCM_RTS
;
135 spin_unlock_irqrestore(&priv
->lock
, flags
);
139 /* This is used to provide a carrier detect mechanism
140 * When a card is present, the response is 0x00
141 * When no card , the reader respond with TIOCM_CD
142 * This is known as CD autodetect mechanism
144 static int iuu_tiocmget(struct tty_struct
*tty
)
146 struct usb_serial_port
*port
= tty
->driver_data
;
147 struct iuu_private
*priv
= usb_get_serial_port_data(port
);
151 spin_lock_irqsave(&priv
->lock
, flags
);
152 rc
= priv
->tiostatus
;
153 spin_unlock_irqrestore(&priv
->lock
, flags
);
158 static void iuu_rxcmd(struct urb
*urb
)
160 struct usb_serial_port
*port
= urb
->context
;
161 int status
= urb
->status
;
164 dev_dbg(&port
->dev
, "%s - status = %d\n", __func__
, status
);
170 memset(port
->write_urb
->transfer_buffer
, IUU_UART_RX
, 1);
171 usb_fill_bulk_urb(port
->write_urb
, port
->serial
->dev
,
172 usb_sndbulkpipe(port
->serial
->dev
,
173 port
->bulk_out_endpointAddress
),
174 port
->write_urb
->transfer_buffer
, 1,
175 read_rxcmd_callback
, port
);
176 usb_submit_urb(port
->write_urb
, GFP_ATOMIC
);
179 static int iuu_reset(struct usb_serial_port
*port
, u8 wt
)
181 struct iuu_private
*priv
= usb_get_serial_port_data(port
);
183 char *buf_ptr
= port
->write_urb
->transfer_buffer
;
185 /* Prepare the reset sequence */
187 *buf_ptr
++ = IUU_RST_SET
;
188 *buf_ptr
++ = IUU_DELAY_MS
;
190 *buf_ptr
= IUU_RST_CLEAR
;
192 /* send the sequence */
194 usb_fill_bulk_urb(port
->write_urb
,
196 usb_sndbulkpipe(port
->serial
->dev
,
197 port
->bulk_out_endpointAddress
),
198 port
->write_urb
->transfer_buffer
, 4, iuu_rxcmd
, port
);
199 result
= usb_submit_urb(port
->write_urb
, GFP_ATOMIC
);
210 static void iuu_update_status_callback(struct urb
*urb
)
212 struct usb_serial_port
*port
= urb
->context
;
213 struct iuu_private
*priv
= usb_get_serial_port_data(port
);
215 int status
= urb
->status
;
218 dev_dbg(&port
->dev
, "%s - status = %d\n", __func__
, status
);
223 st
= urb
->transfer_buffer
;
224 dev_dbg(&port
->dev
, "%s - enter\n", __func__
);
225 if (urb
->actual_length
== 1) {
228 priv
->tiostatus
= iuu_cardout
;
231 priv
->tiostatus
= iuu_cardin
;
234 priv
->tiostatus
= iuu_cardin
;
240 static void iuu_status_callback(struct urb
*urb
)
242 struct usb_serial_port
*port
= urb
->context
;
243 int status
= urb
->status
;
245 dev_dbg(&port
->dev
, "%s - status = %d\n", __func__
, status
);
246 usb_fill_bulk_urb(port
->read_urb
, port
->serial
->dev
,
247 usb_rcvbulkpipe(port
->serial
->dev
,
248 port
->bulk_in_endpointAddress
),
249 port
->read_urb
->transfer_buffer
, 256,
250 iuu_update_status_callback
, port
);
251 usb_submit_urb(port
->read_urb
, GFP_ATOMIC
);
254 static int iuu_status(struct usb_serial_port
*port
)
258 memset(port
->write_urb
->transfer_buffer
, IUU_GET_STATE_REGISTER
, 1);
259 usb_fill_bulk_urb(port
->write_urb
, port
->serial
->dev
,
260 usb_sndbulkpipe(port
->serial
->dev
,
261 port
->bulk_out_endpointAddress
),
262 port
->write_urb
->transfer_buffer
, 1,
263 iuu_status_callback
, port
);
264 result
= usb_submit_urb(port
->write_urb
, GFP_ATOMIC
);
269 static int bulk_immediate(struct usb_serial_port
*port
, u8
*buf
, u8 count
)
272 struct usb_serial
*serial
= port
->serial
;
275 /* send the data out the bulk port */
278 usb_bulk_msg(serial
->dev
,
279 usb_sndbulkpipe(serial
->dev
,
280 port
->bulk_out_endpointAddress
), buf
,
281 count
, &actual
, 1000);
283 if (status
!= IUU_OPERATION_OK
)
284 dev_dbg(&port
->dev
, "%s - error = %2x\n", __func__
, status
);
286 dev_dbg(&port
->dev
, "%s - write OK !\n", __func__
);
290 static int read_immediate(struct usb_serial_port
*port
, u8
*buf
, u8 count
)
293 struct usb_serial
*serial
= port
->serial
;
296 /* send the data out the bulk port */
298 usb_bulk_msg(serial
->dev
,
299 usb_rcvbulkpipe(serial
->dev
,
300 port
->bulk_in_endpointAddress
), buf
,
301 count
, &actual
, 1000);
303 if (status
!= IUU_OPERATION_OK
)
304 dev_dbg(&port
->dev
, "%s - error = %2x\n", __func__
, status
);
306 dev_dbg(&port
->dev
, "%s - read OK !\n", __func__
);
310 static int iuu_led(struct usb_serial_port
*port
, unsigned int R
,
311 unsigned int G
, unsigned int B
, u8 f
)
315 buf
= kmalloc(8, GFP_KERNEL
);
319 buf
[0] = IUU_SET_LED
;
321 buf
[2] = (R
>> 8) & 0xFF;
323 buf
[4] = (G
>> 8) & 0xFF;
325 buf
[6] = (B
>> 8) & 0xFF;
327 status
= bulk_immediate(port
, buf
, 8);
329 if (status
!= IUU_OPERATION_OK
)
330 dev_dbg(&port
->dev
, "%s - led error status = %2x\n", __func__
, status
);
332 dev_dbg(&port
->dev
, "%s - led OK !\n", __func__
);
333 return IUU_OPERATION_OK
;
336 static void iuu_rgbf_fill_buffer(u8
*buf
, u8 r1
, u8 r2
, u8 g1
, u8 g2
, u8 b1
,
339 *buf
++ = IUU_SET_LED
;
349 static void iuu_led_activity_on(struct urb
*urb
)
351 struct usb_serial_port
*port
= urb
->context
;
352 char *buf_ptr
= port
->write_urb
->transfer_buffer
;
355 buf_ptr
[0] = IUU_SET_LED
;
356 get_random_bytes(buf_ptr
+ 1, 6);
359 iuu_rgbf_fill_buffer(buf_ptr
, 255, 255, 0, 0, 0, 0, 255);
362 usb_fill_bulk_urb(port
->write_urb
, port
->serial
->dev
,
363 usb_sndbulkpipe(port
->serial
->dev
,
364 port
->bulk_out_endpointAddress
),
365 port
->write_urb
->transfer_buffer
, 8 ,
367 usb_submit_urb(port
->write_urb
, GFP_ATOMIC
);
370 static void iuu_led_activity_off(struct urb
*urb
)
372 struct usb_serial_port
*port
= urb
->context
;
373 char *buf_ptr
= port
->write_urb
->transfer_buffer
;
380 iuu_rgbf_fill_buffer(buf_ptr
, 0, 0, 255, 255, 0, 0, 255);
382 usb_fill_bulk_urb(port
->write_urb
, port
->serial
->dev
,
383 usb_sndbulkpipe(port
->serial
->dev
,
384 port
->bulk_out_endpointAddress
),
385 port
->write_urb
->transfer_buffer
, 8 ,
387 usb_submit_urb(port
->write_urb
, GFP_ATOMIC
);
392 static int iuu_clk(struct usb_serial_port
*port
, int dwFrq
)
395 struct iuu_private
*priv
= usb_get_serial_port_data(port
);
398 u8 DIV
= 0; /* 8bit */
399 u8 XDRV
= 0; /* 8bit */
400 u8 PUMP
= 0; /* 3bit */
401 u8 PBmsb
= 0; /* 2bit */
402 u8 PBlsb
= 0; /* 8bit */
403 u8 PO
= 0; /* 1bit */
408 int frq
= (int)dwFrq
;
411 priv
->buf
[Count
++] = IUU_UART_WRITE_I2C
;
412 priv
->buf
[Count
++] = FrqGenAdr
<< 1;
413 priv
->buf
[Count
++] = 0x09;
414 priv
->buf
[Count
++] = 0x00;
416 status
= bulk_immediate(port
, (u8
*) priv
->buf
, Count
);
418 dev_dbg(&port
->dev
, "%s - write error\n", __func__
);
421 } else if (frq
== 3579000) {
426 } else if (frq
== 3680000) {
431 } else if (frq
== 6000000) {
437 unsigned int result
= 0;
438 unsigned int tmp
= 0;
443 unsigned int lP
= 2055;
444 unsigned int lDiv
= 4;
446 for (lQ
= 2; lQ
<= 47 && !found
; lQ
++)
447 for (lP
= 2055; lP
>= 8 && !found
; lP
--)
448 for (lDiv
= 4; lDiv
<= 127 && !found
; lDiv
++) {
449 tmp
= (12000000 / lDiv
) * (lP
/ lQ
);
450 if (abs((int)(tmp
- frq
)) <
451 abs((int)(frq
- result
))) {
452 check2
= (12000000 / lQ
);
455 check
= (12000000 / lQ
) * lP
;
456 if (check
> 400000000)
458 if (check
< 100000000)
460 if (lDiv
< 4 || lDiv
> 127)
471 P2
= ((P
- PO
) / 2) - 4;
473 PBmsb
= (P2
>> 8 & 0x03);
475 PO
= (P
>> 10) & 0x01;
478 priv
->buf
[Count
++] = IUU_UART_WRITE_I2C
; /* 0x4C */
479 priv
->buf
[Count
++] = FrqGenAdr
<< 1;
480 priv
->buf
[Count
++] = 0x09;
481 priv
->buf
[Count
++] = 0x20; /* Adr = 0x09 */
482 priv
->buf
[Count
++] = IUU_UART_WRITE_I2C
; /* 0x4C */
483 priv
->buf
[Count
++] = FrqGenAdr
<< 1;
484 priv
->buf
[Count
++] = 0x0C;
485 priv
->buf
[Count
++] = DIV
; /* Adr = 0x0C */
486 priv
->buf
[Count
++] = IUU_UART_WRITE_I2C
; /* 0x4C */
487 priv
->buf
[Count
++] = FrqGenAdr
<< 1;
488 priv
->buf
[Count
++] = 0x12;
489 priv
->buf
[Count
++] = XDRV
; /* Adr = 0x12 */
490 priv
->buf
[Count
++] = IUU_UART_WRITE_I2C
; /* 0x4C */
491 priv
->buf
[Count
++] = FrqGenAdr
<< 1;
492 priv
->buf
[Count
++] = 0x13;
493 priv
->buf
[Count
++] = 0x6B; /* Adr = 0x13 */
494 priv
->buf
[Count
++] = IUU_UART_WRITE_I2C
; /* 0x4C */
495 priv
->buf
[Count
++] = FrqGenAdr
<< 1;
496 priv
->buf
[Count
++] = 0x40;
497 priv
->buf
[Count
++] = (0xC0 | ((PUMP
& 0x07) << 2)) |
498 (PBmsb
& 0x03); /* Adr = 0x40 */
499 priv
->buf
[Count
++] = IUU_UART_WRITE_I2C
; /* 0x4C */
500 priv
->buf
[Count
++] = FrqGenAdr
<< 1;
501 priv
->buf
[Count
++] = 0x41;
502 priv
->buf
[Count
++] = PBlsb
; /* Adr = 0x41 */
503 priv
->buf
[Count
++] = IUU_UART_WRITE_I2C
; /* 0x4C */
504 priv
->buf
[Count
++] = FrqGenAdr
<< 1;
505 priv
->buf
[Count
++] = 0x42;
506 priv
->buf
[Count
++] = Q
| (((PO
& 0x01) << 7)); /* Adr = 0x42 */
507 priv
->buf
[Count
++] = IUU_UART_WRITE_I2C
; /* 0x4C */
508 priv
->buf
[Count
++] = FrqGenAdr
<< 1;
509 priv
->buf
[Count
++] = 0x44;
510 priv
->buf
[Count
++] = (char)0xFF; /* Adr = 0x44 */
511 priv
->buf
[Count
++] = IUU_UART_WRITE_I2C
; /* 0x4C */
512 priv
->buf
[Count
++] = FrqGenAdr
<< 1;
513 priv
->buf
[Count
++] = 0x45;
514 priv
->buf
[Count
++] = (char)0xFE; /* Adr = 0x45 */
515 priv
->buf
[Count
++] = IUU_UART_WRITE_I2C
; /* 0x4C */
516 priv
->buf
[Count
++] = FrqGenAdr
<< 1;
517 priv
->buf
[Count
++] = 0x46;
518 priv
->buf
[Count
++] = 0x7F; /* Adr = 0x46 */
519 priv
->buf
[Count
++] = IUU_UART_WRITE_I2C
; /* 0x4C */
520 priv
->buf
[Count
++] = FrqGenAdr
<< 1;
521 priv
->buf
[Count
++] = 0x47;
522 priv
->buf
[Count
++] = (char)0x84; /* Adr = 0x47 */
524 status
= bulk_immediate(port
, (u8
*) priv
->buf
, Count
);
525 if (status
!= IUU_OPERATION_OK
)
526 dev_dbg(&port
->dev
, "%s - write error\n", __func__
);
530 static int iuu_uart_flush(struct usb_serial_port
*port
)
532 struct device
*dev
= &port
->dev
;
535 u8 rxcmd
= IUU_UART_RX
;
536 struct iuu_private
*priv
= usb_get_serial_port_data(port
);
538 if (iuu_led(port
, 0xF000, 0, 0, 0xFF) < 0)
541 for (i
= 0; i
< 2; i
++) {
542 status
= bulk_immediate(port
, &rxcmd
, 1);
543 if (status
!= IUU_OPERATION_OK
) {
544 dev_dbg(dev
, "%s - uart_flush_write error\n", __func__
);
548 status
= read_immediate(port
, &priv
->len
, 1);
549 if (status
!= IUU_OPERATION_OK
) {
550 dev_dbg(dev
, "%s - uart_flush_read error\n", __func__
);
555 dev_dbg(dev
, "%s - uart_flush datalen is : %i\n", __func__
, priv
->len
);
556 status
= read_immediate(port
, priv
->buf
, priv
->len
);
557 if (status
!= IUU_OPERATION_OK
) {
558 dev_dbg(dev
, "%s - uart_flush_read error\n", __func__
);
563 dev_dbg(dev
, "%s - uart_flush_read OK!\n", __func__
);
564 iuu_led(port
, 0, 0xF000, 0, 0xFF);
568 static void read_buf_callback(struct urb
*urb
)
570 struct usb_serial_port
*port
= urb
->context
;
571 unsigned char *data
= urb
->transfer_buffer
;
572 int status
= urb
->status
;
575 if (status
== -EPROTO
) {
576 /* reschedule needed */
581 dev_dbg(&port
->dev
, "%s - %i chars to write\n", __func__
, urb
->actual_length
);
583 if (urb
->actual_length
) {
584 tty_insert_flip_string(&port
->port
, data
, urb
->actual_length
);
585 tty_flip_buffer_push(&port
->port
);
587 iuu_led_activity_on(urb
);
590 static int iuu_bulk_write(struct usb_serial_port
*port
)
592 struct iuu_private
*priv
= usb_get_serial_port_data(port
);
596 char *buf_ptr
= port
->write_urb
->transfer_buffer
;
598 spin_lock_irqsave(&priv
->lock
, flags
);
599 *buf_ptr
++ = IUU_UART_ESC
;
600 *buf_ptr
++ = IUU_UART_TX
;
601 *buf_ptr
++ = priv
->writelen
;
603 memcpy(buf_ptr
, priv
->writebuf
, priv
->writelen
);
604 buf_len
= priv
->writelen
;
606 spin_unlock_irqrestore(&priv
->lock
, flags
);
607 dev_dbg(&port
->dev
, "%s - writing %i chars : %*ph\n", __func__
,
608 buf_len
, buf_len
, buf_ptr
);
609 usb_fill_bulk_urb(port
->write_urb
, port
->serial
->dev
,
610 usb_sndbulkpipe(port
->serial
->dev
,
611 port
->bulk_out_endpointAddress
),
612 port
->write_urb
->transfer_buffer
, buf_len
+ 3,
614 result
= usb_submit_urb(port
->write_urb
, GFP_ATOMIC
);
615 usb_serial_port_softint(port
);
619 static int iuu_read_buf(struct usb_serial_port
*port
, int len
)
623 usb_fill_bulk_urb(port
->read_urb
, port
->serial
->dev
,
624 usb_rcvbulkpipe(port
->serial
->dev
,
625 port
->bulk_in_endpointAddress
),
626 port
->read_urb
->transfer_buffer
, len
,
627 read_buf_callback
, port
);
628 result
= usb_submit_urb(port
->read_urb
, GFP_ATOMIC
);
632 static void iuu_uart_read_callback(struct urb
*urb
)
634 struct usb_serial_port
*port
= urb
->context
;
635 struct iuu_private
*priv
= usb_get_serial_port_data(port
);
637 int status
= urb
->status
;
640 unsigned char *data
= urb
->transfer_buffer
;
644 dev_dbg(&port
->dev
, "%s - status = %d\n", __func__
, status
);
649 if (urb
->actual_length
== 1)
652 if (urb
->actual_length
> 1) {
653 dev_dbg(&port
->dev
, "%s - urb->actual_length = %i\n", __func__
,
658 /* if len > 0 call readbuf */
660 if (len
> 0 && error
== 0) {
661 dev_dbg(&port
->dev
, "%s - call read buf - len to read is %i\n",
663 status
= iuu_read_buf(port
, len
);
666 /* need to update status ? */
667 if (priv
->poll
> 99) {
668 status
= iuu_status(port
);
673 /* reset waiting ? */
675 if (priv
->reset
== 1) {
676 status
= iuu_reset(port
, 0xC);
679 /* Writebuf is waiting */
680 spin_lock_irqsave(&priv
->lock
, flags
);
681 if (priv
->writelen
> 0) {
682 spin_unlock_irqrestore(&priv
->lock
, flags
);
683 status
= iuu_bulk_write(port
);
686 spin_unlock_irqrestore(&priv
->lock
, flags
);
687 /* if nothing to write call again rxcmd */
688 dev_dbg(&port
->dev
, "%s - rxcmd recall\n", __func__
);
689 iuu_led_activity_off(urb
);
692 static int iuu_uart_write(struct tty_struct
*tty
, struct usb_serial_port
*port
,
693 const u8
*buf
, int count
)
695 struct iuu_private
*priv
= usb_get_serial_port_data(port
);
698 spin_lock_irqsave(&priv
->lock
, flags
);
700 count
= min(count
, 256 - priv
->writelen
);
704 /* fill the buffer */
705 memcpy(priv
->writebuf
+ priv
->writelen
, buf
, count
);
706 priv
->writelen
+= count
;
708 spin_unlock_irqrestore(&priv
->lock
, flags
);
713 static void read_rxcmd_callback(struct urb
*urb
)
715 struct usb_serial_port
*port
= urb
->context
;
717 int status
= urb
->status
;
724 usb_fill_bulk_urb(port
->read_urb
, port
->serial
->dev
,
725 usb_rcvbulkpipe(port
->serial
->dev
,
726 port
->bulk_in_endpointAddress
),
727 port
->read_urb
->transfer_buffer
, 256,
728 iuu_uart_read_callback
, port
);
729 result
= usb_submit_urb(port
->read_urb
, GFP_ATOMIC
);
730 dev_dbg(&port
->dev
, "%s - submit result = %d\n", __func__
, result
);
733 static int iuu_uart_on(struct usb_serial_port
*port
)
738 buf
= kmalloc(4, GFP_KERNEL
);
743 buf
[0] = IUU_UART_ENABLE
;
744 buf
[1] = (u8
) ((IUU_BAUD_9600
>> 8) & 0x00FF);
745 buf
[2] = (u8
) (0x00FF & IUU_BAUD_9600
);
746 buf
[3] = (u8
) (0x0F0 & IUU_ONE_STOP_BIT
) | (0x07 & IUU_PARITY_EVEN
);
748 status
= bulk_immediate(port
, buf
, 4);
749 if (status
!= IUU_OPERATION_OK
) {
750 dev_dbg(&port
->dev
, "%s - uart_on error\n", __func__
);
751 goto uart_enable_failed
;
753 /* iuu_reset() the card after iuu_uart_on() */
754 status
= iuu_uart_flush(port
);
755 if (status
!= IUU_OPERATION_OK
)
756 dev_dbg(&port
->dev
, "%s - uart_flush error\n", __func__
);
762 /* Disables the IUU UART (a.k.a. the Phoenix voiderface) */
763 static int iuu_uart_off(struct usb_serial_port
*port
)
767 buf
= kmalloc(1, GFP_KERNEL
);
770 buf
[0] = IUU_UART_DISABLE
;
772 status
= bulk_immediate(port
, buf
, 1);
773 if (status
!= IUU_OPERATION_OK
)
774 dev_dbg(&port
->dev
, "%s - uart_off error\n", __func__
);
780 static int iuu_uart_baud(struct usb_serial_port
*port
, u32 baud_base
,
781 u32
*actual
, u8 parity
)
789 unsigned int T1FrekvensHZ
= 0;
791 dev_dbg(&port
->dev
, "%s - enter baud_base=%d\n", __func__
, baud_base
);
792 dataout
= kmalloc(5, GFP_KERNEL
);
796 /*baud = (((priv->clk / 35) * baud_base) / 100000); */
799 if (baud
< 1200 || baud
> 230400) {
801 return IUU_INVALID_PARAMETER
;
805 T1FrekvensHZ
= 500000;
810 T1FrekvensHZ
= 2000000;
815 T1FrekvensHZ
= 6000000;
820 T1FrekvensHZ
= 24000000;
823 T1reload
= 256 - (u8
) (T1FrekvensHZ
/ (baud
* 2));
825 /* magic number here: ENTER_FIRMWARE_UPDATE; */
826 dataout
[DataCount
++] = IUU_UART_ESC
;
827 /* magic number here: CHANGE_BAUD; */
828 dataout
[DataCount
++] = IUU_UART_CHANGE
;
829 dataout
[DataCount
++] = T1Frekvens
;
830 dataout
[DataCount
++] = T1reload
;
832 *actual
= (T1FrekvensHZ
/ (256 - T1reload
)) / 2;
834 switch (parity
& 0x0F) {
835 case IUU_PARITY_NONE
:
836 dataout
[DataCount
++] = 0x00;
838 case IUU_PARITY_EVEN
:
839 dataout
[DataCount
++] = 0x01;
842 dataout
[DataCount
++] = 0x02;
844 case IUU_PARITY_MARK
:
845 dataout
[DataCount
++] = 0x03;
847 case IUU_PARITY_SPACE
:
848 dataout
[DataCount
++] = 0x04;
852 return IUU_INVALID_PARAMETER
;
855 switch (parity
& 0xF0) {
856 case IUU_ONE_STOP_BIT
:
857 dataout
[DataCount
- 1] |= IUU_ONE_STOP_BIT
;
860 case IUU_TWO_STOP_BITS
:
861 dataout
[DataCount
- 1] |= IUU_TWO_STOP_BITS
;
865 return IUU_INVALID_PARAMETER
;
868 status
= bulk_immediate(port
, dataout
, DataCount
);
869 if (status
!= IUU_OPERATION_OK
)
870 dev_dbg(&port
->dev
, "%s - uart_off error\n", __func__
);
875 static void iuu_set_termios(struct tty_struct
*tty
,
876 struct usb_serial_port
*port
, struct ktermios
*old_termios
)
878 const u32 supported_mask
= CMSPAR
|PARENB
|PARODD
;
879 struct iuu_private
*priv
= usb_get_serial_port_data(port
);
880 unsigned int cflag
= tty
->termios
.c_cflag
;
886 u32 newval
= cflag
& supported_mask
;
888 /* Just use the ospeed. ispeed should be the same. */
889 baud
= tty
->termios
.c_ospeed
;
891 dev_dbg(&port
->dev
, "%s - enter c_ospeed or baud=%d\n", __func__
, baud
);
893 /* compute the parity parameter */
895 if (cflag
& CMSPAR
) { /* Using mark space */
897 parity
|= IUU_PARITY_SPACE
;
899 parity
|= IUU_PARITY_MARK
;
900 } else if (!(cflag
& PARENB
)) {
901 parity
|= IUU_PARITY_NONE
;
903 } else if (cflag
& PARODD
)
904 parity
|= IUU_PARITY_ODD
;
906 parity
|= IUU_PARITY_EVEN
;
908 parity
|= (cflag
& CSTOPB
? IUU_TWO_STOP_BITS
: IUU_ONE_STOP_BIT
);
911 status
= iuu_uart_baud(port
,
912 baud
* priv
->boost
/ 100,
915 /* set the termios value to the real one, so the user now what has
916 * changed. We support few fields so its easies to copy the old hw
917 * settings back over and then adjust them
920 tty_termios_copy_hw(&tty
->termios
, old_termios
);
921 if (status
!= 0) /* Set failed - return old bits */
923 /* Re-encode speed, parity and csize */
924 tty_encode_baud_rate(tty
, baud
, baud
);
925 tty
->termios
.c_cflag
&= ~(supported_mask
|CSIZE
);
926 tty
->termios
.c_cflag
|= newval
| csize
;
929 static void iuu_close(struct usb_serial_port
*port
)
931 /* iuu_led (port,255,0,0,0); */
935 usb_kill_urb(port
->write_urb
);
936 usb_kill_urb(port
->read_urb
);
938 iuu_led(port
, 0, 0, 0xF000, 0xFF);
941 static void iuu_init_termios(struct tty_struct
*tty
)
943 tty
->termios
.c_cflag
= B9600
| CS8
| CSTOPB
| CREAD
| PARENB
| CLOCAL
;
944 tty
->termios
.c_ispeed
= 9600;
945 tty
->termios
.c_ospeed
= 9600;
946 tty
->termios
.c_lflag
= 0;
947 tty
->termios
.c_oflag
= 0;
948 tty
->termios
.c_iflag
= 0;
951 static int iuu_open(struct tty_struct
*tty
, struct usb_serial_port
*port
)
953 struct usb_serial
*serial
= port
->serial
;
954 struct device
*dev
= &port
->dev
;
958 struct iuu_private
*priv
= usb_get_serial_port_data(port
);
960 baud
= tty
->termios
.c_ospeed
;
962 dev_dbg(dev
, "%s - baud %d\n", __func__
, baud
);
963 usb_clear_halt(serial
->dev
, port
->write_urb
->pipe
);
964 usb_clear_halt(serial
->dev
, port
->read_urb
->pipe
);
968 #define SOUP(a, b, c, d) do { \
969 result = usb_control_msg(port->serial->dev, \
970 usb_sndctrlpipe(port->serial->dev, 0), \
971 b, a, c, d, NULL, 0, 1000); \
972 dev_dbg(dev, "0x%x:0x%x:0x%x:0x%x %d\n", a, b, c, d, result); } while (0)
974 /* This is not UART related but IUU USB driver related or something */
975 /* like that. Basically no IUU will accept any commands from the USB */
976 /* host unless it has received the following message */
977 /* sprintf(buf ,"%c%c%c%c",0x03,0x02,0x02,0x0); */
979 SOUP(0x03, 0x02, 0x02, 0x0);
981 iuu_led(port
, 0xF000, 0xF000, 0, 0xFF);
987 case 2: /* 3.680 Mhz */
988 priv
->clk
= IUU_CLK_3680000
;
989 iuu_clk(port
, IUU_CLK_3680000
* boost
/ 100);
991 iuu_uart_baud(port
, baud
* boost
/ 100, &actual
,
994 case 3: /* 6.00 Mhz */
995 iuu_clk(port
, IUU_CLK_6000000
* boost
/ 100);
996 priv
->clk
= IUU_CLK_6000000
;
997 /* Ratio of 6000000 to 3500000 for baud 9600 */
999 iuu_uart_baud(port
, 16457 * boost
/ 100, &actual
,
1002 default: /* 3.579 Mhz */
1003 iuu_clk(port
, IUU_CLK_3579000
* boost
/ 100);
1004 priv
->clk
= IUU_CLK_3579000
;
1006 iuu_uart_baud(port
, baud
* boost
/ 100, &actual
,
1010 /* set the cardin cardout signals */
1017 iuu_cardin
= TIOCM_CD
;
1022 iuu_cardout
= TIOCM_CD
;
1025 iuu_cardin
= TIOCM_DSR
;
1030 iuu_cardout
= TIOCM_DSR
;
1033 iuu_cardin
= TIOCM_CTS
;
1038 iuu_cardout
= TIOCM_CTS
;
1041 iuu_cardin
= TIOCM_RNG
;
1046 iuu_cardout
= TIOCM_RNG
;
1049 iuu_uart_flush(port
);
1051 dev_dbg(dev
, "%s - initialization done\n", __func__
);
1053 memset(port
->write_urb
->transfer_buffer
, IUU_UART_RX
, 1);
1054 usb_fill_bulk_urb(port
->write_urb
, port
->serial
->dev
,
1055 usb_sndbulkpipe(port
->serial
->dev
,
1056 port
->bulk_out_endpointAddress
),
1057 port
->write_urb
->transfer_buffer
, 1,
1058 read_rxcmd_callback
, port
);
1059 result
= usb_submit_urb(port
->write_urb
, GFP_KERNEL
);
1061 dev_err(dev
, "%s - failed submitting read urb, error %d\n", __func__
, result
);
1064 dev_dbg(dev
, "%s - rxcmd OK\n", __func__
);
1070 /* how to change VCC */
1071 static int iuu_vcc_set(struct usb_serial_port
*port
, unsigned int vcc
)
1076 buf
= kmalloc(5, GFP_KERNEL
);
1080 buf
[0] = IUU_SET_VCC
;
1081 buf
[1] = vcc
& 0xFF;
1082 buf
[2] = (vcc
>> 8) & 0xFF;
1083 buf
[3] = (vcc
>> 16) & 0xFF;
1084 buf
[4] = (vcc
>> 24) & 0xFF;
1086 status
= bulk_immediate(port
, buf
, 5);
1089 if (status
!= IUU_OPERATION_OK
)
1090 dev_dbg(&port
->dev
, "%s - vcc error status = %2x\n", __func__
, status
);
1092 dev_dbg(&port
->dev
, "%s - vcc OK !\n", __func__
);
1101 static ssize_t
vcc_mode_show(struct device
*dev
,
1102 struct device_attribute
*attr
, char *buf
)
1104 struct usb_serial_port
*port
= to_usb_serial_port(dev
);
1105 struct iuu_private
*priv
= usb_get_serial_port_data(port
);
1107 return sprintf(buf
, "%d\n", priv
->vcc
);
1110 static ssize_t
vcc_mode_store(struct device
*dev
,
1111 struct device_attribute
*attr
, const char *buf
, size_t count
)
1113 struct usb_serial_port
*port
= to_usb_serial_port(dev
);
1114 struct iuu_private
*priv
= usb_get_serial_port_data(port
);
1117 if (kstrtoul(buf
, 10, &v
)) {
1118 dev_err(dev
, "%s - vcc_mode: %s is not a unsigned long\n",
1120 goto fail_store_vcc_mode
;
1123 dev_dbg(dev
, "%s: setting vcc_mode = %ld\n", __func__
, v
);
1125 if ((v
!= 3) && (v
!= 5)) {
1126 dev_err(dev
, "%s - vcc_mode %ld is invalid\n", __func__
, v
);
1128 iuu_vcc_set(port
, v
);
1131 fail_store_vcc_mode
:
1134 static DEVICE_ATTR_RW(vcc_mode
);
1136 static int iuu_create_sysfs_attrs(struct usb_serial_port
*port
)
1138 return device_create_file(&port
->dev
, &dev_attr_vcc_mode
);
1141 static int iuu_remove_sysfs_attrs(struct usb_serial_port
*port
)
1143 device_remove_file(&port
->dev
, &dev_attr_vcc_mode
);
1148 * End Sysfs Attributes
1151 static struct usb_serial_driver iuu_device
= {
1153 .owner
= THIS_MODULE
,
1154 .name
= "iuu_phoenix",
1156 .id_table
= id_table
,
1160 .bulk_in_size
= 512,
1161 .bulk_out_size
= 512,
1164 .write
= iuu_uart_write
,
1165 .read_bulk_callback
= iuu_uart_read_callback
,
1166 .tiocmget
= iuu_tiocmget
,
1167 .tiocmset
= iuu_tiocmset
,
1168 .set_termios
= iuu_set_termios
,
1169 .init_termios
= iuu_init_termios
,
1170 .port_probe
= iuu_port_probe
,
1171 .port_remove
= iuu_port_remove
,
1174 static struct usb_serial_driver
* const serial_drivers
[] = {
1178 module_usb_serial_driver(serial_drivers
, id_table
);
1180 MODULE_AUTHOR("Alain Degreffe eczema@ecze.com");
1182 MODULE_DESCRIPTION(DRIVER_DESC
);
1183 MODULE_LICENSE("GPL");
1185 module_param(xmas
, bool, S_IRUGO
| S_IWUSR
);
1186 MODULE_PARM_DESC(xmas
, "Xmas colors enabled or not");
1188 module_param(boost
, int, S_IRUGO
| S_IWUSR
);
1189 MODULE_PARM_DESC(boost
, "Card overclock boost (in percent 100-500)");
1191 module_param(clockmode
, int, S_IRUGO
| S_IWUSR
);
1192 MODULE_PARM_DESC(clockmode
, "Card clock mode (1=3.579 MHz, 2=3.680 MHz, "
1195 module_param(cdmode
, int, S_IRUGO
| S_IWUSR
);
1196 MODULE_PARM_DESC(cdmode
, "Card detect mode (0=none, 1=CD, 2=!CD, 3=DSR, "
1197 "4=!DSR, 5=CTS, 6=!CTS, 7=RING, 8=!RING)");
1199 module_param(vcc_default
, int, S_IRUGO
| S_IWUSR
);
1200 MODULE_PARM_DESC(vcc_default
, "Set default VCC (either 3 for 3.3V or 5 "
1201 "for 5V). Default to 5.");