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
;
162 int status
= urb
->status
;
165 dev_dbg(&port
->dev
, "%s - status = %d\n", __func__
, status
);
171 memset(port
->write_urb
->transfer_buffer
, IUU_UART_RX
, 1);
172 usb_fill_bulk_urb(port
->write_urb
, port
->serial
->dev
,
173 usb_sndbulkpipe(port
->serial
->dev
,
174 port
->bulk_out_endpointAddress
),
175 port
->write_urb
->transfer_buffer
, 1,
176 read_rxcmd_callback
, port
);
177 result
= usb_submit_urb(port
->write_urb
, GFP_ATOMIC
);
180 static int iuu_reset(struct usb_serial_port
*port
, u8 wt
)
182 struct iuu_private
*priv
= usb_get_serial_port_data(port
);
184 char *buf_ptr
= port
->write_urb
->transfer_buffer
;
186 /* Prepare the reset sequence */
188 *buf_ptr
++ = IUU_RST_SET
;
189 *buf_ptr
++ = IUU_DELAY_MS
;
191 *buf_ptr
= IUU_RST_CLEAR
;
193 /* send the sequence */
195 usb_fill_bulk_urb(port
->write_urb
,
197 usb_sndbulkpipe(port
->serial
->dev
,
198 port
->bulk_out_endpointAddress
),
199 port
->write_urb
->transfer_buffer
, 4, iuu_rxcmd
, port
);
200 result
= usb_submit_urb(port
->write_urb
, GFP_ATOMIC
);
211 static void iuu_update_status_callback(struct urb
*urb
)
213 struct usb_serial_port
*port
= urb
->context
;
214 struct iuu_private
*priv
= usb_get_serial_port_data(port
);
216 int status
= urb
->status
;
219 dev_dbg(&port
->dev
, "%s - status = %d\n", __func__
, status
);
224 st
= urb
->transfer_buffer
;
225 dev_dbg(&port
->dev
, "%s - enter\n", __func__
);
226 if (urb
->actual_length
== 1) {
229 priv
->tiostatus
= iuu_cardout
;
232 priv
->tiostatus
= iuu_cardin
;
235 priv
->tiostatus
= iuu_cardin
;
241 static void iuu_status_callback(struct urb
*urb
)
243 struct usb_serial_port
*port
= urb
->context
;
245 int status
= urb
->status
;
247 dev_dbg(&port
->dev
, "%s - status = %d\n", __func__
, status
);
248 usb_fill_bulk_urb(port
->read_urb
, port
->serial
->dev
,
249 usb_rcvbulkpipe(port
->serial
->dev
,
250 port
->bulk_in_endpointAddress
),
251 port
->read_urb
->transfer_buffer
, 256,
252 iuu_update_status_callback
, port
);
253 result
= usb_submit_urb(port
->read_urb
, GFP_ATOMIC
);
256 static int iuu_status(struct usb_serial_port
*port
)
260 memset(port
->write_urb
->transfer_buffer
, IUU_GET_STATE_REGISTER
, 1);
261 usb_fill_bulk_urb(port
->write_urb
, port
->serial
->dev
,
262 usb_sndbulkpipe(port
->serial
->dev
,
263 port
->bulk_out_endpointAddress
),
264 port
->write_urb
->transfer_buffer
, 1,
265 iuu_status_callback
, port
);
266 result
= usb_submit_urb(port
->write_urb
, GFP_ATOMIC
);
271 static int bulk_immediate(struct usb_serial_port
*port
, u8
*buf
, u8 count
)
274 struct usb_serial
*serial
= port
->serial
;
277 /* send the data out the bulk port */
280 usb_bulk_msg(serial
->dev
,
281 usb_sndbulkpipe(serial
->dev
,
282 port
->bulk_out_endpointAddress
), buf
,
283 count
, &actual
, 1000);
285 if (status
!= IUU_OPERATION_OK
)
286 dev_dbg(&port
->dev
, "%s - error = %2x\n", __func__
, status
);
288 dev_dbg(&port
->dev
, "%s - write OK !\n", __func__
);
292 static int read_immediate(struct usb_serial_port
*port
, u8
*buf
, u8 count
)
295 struct usb_serial
*serial
= port
->serial
;
298 /* send the data out the bulk port */
300 usb_bulk_msg(serial
->dev
,
301 usb_rcvbulkpipe(serial
->dev
,
302 port
->bulk_in_endpointAddress
), buf
,
303 count
, &actual
, 1000);
305 if (status
!= IUU_OPERATION_OK
)
306 dev_dbg(&port
->dev
, "%s - error = %2x\n", __func__
, status
);
308 dev_dbg(&port
->dev
, "%s - read OK !\n", __func__
);
312 static int iuu_led(struct usb_serial_port
*port
, unsigned int R
,
313 unsigned int G
, unsigned int B
, u8 f
)
317 buf
= kmalloc(8, GFP_KERNEL
);
321 buf
[0] = IUU_SET_LED
;
323 buf
[2] = (R
>> 8) & 0xFF;
325 buf
[4] = (G
>> 8) & 0xFF;
327 buf
[6] = (B
>> 8) & 0xFF;
329 status
= bulk_immediate(port
, buf
, 8);
331 if (status
!= IUU_OPERATION_OK
)
332 dev_dbg(&port
->dev
, "%s - led error status = %2x\n", __func__
, status
);
334 dev_dbg(&port
->dev
, "%s - led OK !\n", __func__
);
335 return IUU_OPERATION_OK
;
338 static void iuu_rgbf_fill_buffer(u8
*buf
, u8 r1
, u8 r2
, u8 g1
, u8 g2
, u8 b1
,
341 *buf
++ = IUU_SET_LED
;
351 static void iuu_led_activity_on(struct urb
*urb
)
353 struct usb_serial_port
*port
= urb
->context
;
355 char *buf_ptr
= port
->write_urb
->transfer_buffer
;
356 *buf_ptr
++ = IUU_SET_LED
;
358 get_random_bytes(buf_ptr
, 6);
361 iuu_rgbf_fill_buffer(buf_ptr
, 255, 255, 0, 0, 0, 0, 255);
364 usb_fill_bulk_urb(port
->write_urb
, port
->serial
->dev
,
365 usb_sndbulkpipe(port
->serial
->dev
,
366 port
->bulk_out_endpointAddress
),
367 port
->write_urb
->transfer_buffer
, 8 ,
369 result
= usb_submit_urb(port
->write_urb
, GFP_ATOMIC
);
372 static void iuu_led_activity_off(struct urb
*urb
)
374 struct usb_serial_port
*port
= urb
->context
;
376 char *buf_ptr
= port
->write_urb
->transfer_buffer
;
381 *buf_ptr
++ = IUU_SET_LED
;
382 iuu_rgbf_fill_buffer(buf_ptr
, 0, 0, 255, 255, 0, 0, 255);
384 usb_fill_bulk_urb(port
->write_urb
, port
->serial
->dev
,
385 usb_sndbulkpipe(port
->serial
->dev
,
386 port
->bulk_out_endpointAddress
),
387 port
->write_urb
->transfer_buffer
, 8 ,
389 result
= usb_submit_urb(port
->write_urb
, GFP_ATOMIC
);
394 static int iuu_clk(struct usb_serial_port
*port
, int dwFrq
)
397 struct iuu_private
*priv
= usb_get_serial_port_data(port
);
400 u8 DIV
= 0; /* 8bit */
401 u8 XDRV
= 0; /* 8bit */
402 u8 PUMP
= 0; /* 3bit */
403 u8 PBmsb
= 0; /* 2bit */
404 u8 PBlsb
= 0; /* 8bit */
405 u8 PO
= 0; /* 1bit */
410 int frq
= (int)dwFrq
;
413 priv
->buf
[Count
++] = IUU_UART_WRITE_I2C
;
414 priv
->buf
[Count
++] = FrqGenAdr
<< 1;
415 priv
->buf
[Count
++] = 0x09;
416 priv
->buf
[Count
++] = 0x00;
418 status
= bulk_immediate(port
, (u8
*) priv
->buf
, Count
);
420 dev_dbg(&port
->dev
, "%s - write error\n", __func__
);
423 } else if (frq
== 3579000) {
428 } else if (frq
== 3680000) {
433 } else if (frq
== 6000000) {
439 unsigned int result
= 0;
440 unsigned int tmp
= 0;
445 unsigned int lP
= 2055;
446 unsigned int lDiv
= 4;
448 for (lQ
= 2; lQ
<= 47 && !found
; lQ
++)
449 for (lP
= 2055; lP
>= 8 && !found
; lP
--)
450 for (lDiv
= 4; lDiv
<= 127 && !found
; lDiv
++) {
451 tmp
= (12000000 / lDiv
) * (lP
/ lQ
);
452 if (abs((int)(tmp
- frq
)) <
453 abs((int)(frq
- result
))) {
454 check2
= (12000000 / lQ
);
457 check
= (12000000 / lQ
) * lP
;
458 if (check
> 400000000)
460 if (check
< 100000000)
462 if (lDiv
< 4 || lDiv
> 127)
473 P2
= ((P
- PO
) / 2) - 4;
475 PBmsb
= (P2
>> 8 & 0x03);
477 PO
= (P
>> 10) & 0x01;
480 priv
->buf
[Count
++] = IUU_UART_WRITE_I2C
; /* 0x4C */
481 priv
->buf
[Count
++] = FrqGenAdr
<< 1;
482 priv
->buf
[Count
++] = 0x09;
483 priv
->buf
[Count
++] = 0x20; /* Adr = 0x09 */
484 priv
->buf
[Count
++] = IUU_UART_WRITE_I2C
; /* 0x4C */
485 priv
->buf
[Count
++] = FrqGenAdr
<< 1;
486 priv
->buf
[Count
++] = 0x0C;
487 priv
->buf
[Count
++] = DIV
; /* Adr = 0x0C */
488 priv
->buf
[Count
++] = IUU_UART_WRITE_I2C
; /* 0x4C */
489 priv
->buf
[Count
++] = FrqGenAdr
<< 1;
490 priv
->buf
[Count
++] = 0x12;
491 priv
->buf
[Count
++] = XDRV
; /* Adr = 0x12 */
492 priv
->buf
[Count
++] = IUU_UART_WRITE_I2C
; /* 0x4C */
493 priv
->buf
[Count
++] = FrqGenAdr
<< 1;
494 priv
->buf
[Count
++] = 0x13;
495 priv
->buf
[Count
++] = 0x6B; /* Adr = 0x13 */
496 priv
->buf
[Count
++] = IUU_UART_WRITE_I2C
; /* 0x4C */
497 priv
->buf
[Count
++] = FrqGenAdr
<< 1;
498 priv
->buf
[Count
++] = 0x40;
499 priv
->buf
[Count
++] = (0xC0 | ((PUMP
& 0x07) << 2)) |
500 (PBmsb
& 0x03); /* Adr = 0x40 */
501 priv
->buf
[Count
++] = IUU_UART_WRITE_I2C
; /* 0x4C */
502 priv
->buf
[Count
++] = FrqGenAdr
<< 1;
503 priv
->buf
[Count
++] = 0x41;
504 priv
->buf
[Count
++] = PBlsb
; /* Adr = 0x41 */
505 priv
->buf
[Count
++] = IUU_UART_WRITE_I2C
; /* 0x4C */
506 priv
->buf
[Count
++] = FrqGenAdr
<< 1;
507 priv
->buf
[Count
++] = 0x42;
508 priv
->buf
[Count
++] = Q
| (((PO
& 0x01) << 7)); /* Adr = 0x42 */
509 priv
->buf
[Count
++] = IUU_UART_WRITE_I2C
; /* 0x4C */
510 priv
->buf
[Count
++] = FrqGenAdr
<< 1;
511 priv
->buf
[Count
++] = 0x44;
512 priv
->buf
[Count
++] = (char)0xFF; /* Adr = 0x44 */
513 priv
->buf
[Count
++] = IUU_UART_WRITE_I2C
; /* 0x4C */
514 priv
->buf
[Count
++] = FrqGenAdr
<< 1;
515 priv
->buf
[Count
++] = 0x45;
516 priv
->buf
[Count
++] = (char)0xFE; /* Adr = 0x45 */
517 priv
->buf
[Count
++] = IUU_UART_WRITE_I2C
; /* 0x4C */
518 priv
->buf
[Count
++] = FrqGenAdr
<< 1;
519 priv
->buf
[Count
++] = 0x46;
520 priv
->buf
[Count
++] = 0x7F; /* Adr = 0x46 */
521 priv
->buf
[Count
++] = IUU_UART_WRITE_I2C
; /* 0x4C */
522 priv
->buf
[Count
++] = FrqGenAdr
<< 1;
523 priv
->buf
[Count
++] = 0x47;
524 priv
->buf
[Count
++] = (char)0x84; /* Adr = 0x47 */
526 status
= bulk_immediate(port
, (u8
*) priv
->buf
, Count
);
527 if (status
!= IUU_OPERATION_OK
)
528 dev_dbg(&port
->dev
, "%s - write error\n", __func__
);
532 static int iuu_uart_flush(struct usb_serial_port
*port
)
534 struct device
*dev
= &port
->dev
;
537 u8 rxcmd
= IUU_UART_RX
;
538 struct iuu_private
*priv
= usb_get_serial_port_data(port
);
540 if (iuu_led(port
, 0xF000, 0, 0, 0xFF) < 0)
543 for (i
= 0; i
< 2; i
++) {
544 status
= bulk_immediate(port
, &rxcmd
, 1);
545 if (status
!= IUU_OPERATION_OK
) {
546 dev_dbg(dev
, "%s - uart_flush_write error\n", __func__
);
550 status
= read_immediate(port
, &priv
->len
, 1);
551 if (status
!= IUU_OPERATION_OK
) {
552 dev_dbg(dev
, "%s - uart_flush_read error\n", __func__
);
557 dev_dbg(dev
, "%s - uart_flush datalen is : %i\n", __func__
, priv
->len
);
558 status
= read_immediate(port
, priv
->buf
, priv
->len
);
559 if (status
!= IUU_OPERATION_OK
) {
560 dev_dbg(dev
, "%s - uart_flush_read error\n", __func__
);
565 dev_dbg(dev
, "%s - uart_flush_read OK!\n", __func__
);
566 iuu_led(port
, 0, 0xF000, 0, 0xFF);
570 static void read_buf_callback(struct urb
*urb
)
572 struct usb_serial_port
*port
= urb
->context
;
573 unsigned char *data
= urb
->transfer_buffer
;
574 int status
= urb
->status
;
577 if (status
== -EPROTO
) {
578 /* reschedule needed */
583 dev_dbg(&port
->dev
, "%s - %i chars to write\n", __func__
, urb
->actual_length
);
585 if (urb
->actual_length
) {
586 tty_insert_flip_string(&port
->port
, data
, urb
->actual_length
);
587 tty_flip_buffer_push(&port
->port
);
589 iuu_led_activity_on(urb
);
592 static int iuu_bulk_write(struct usb_serial_port
*port
)
594 struct iuu_private
*priv
= usb_get_serial_port_data(port
);
598 char *buf_ptr
= port
->write_urb
->transfer_buffer
;
600 spin_lock_irqsave(&priv
->lock
, flags
);
601 *buf_ptr
++ = IUU_UART_ESC
;
602 *buf_ptr
++ = IUU_UART_TX
;
603 *buf_ptr
++ = priv
->writelen
;
605 memcpy(buf_ptr
, priv
->writebuf
, priv
->writelen
);
606 buf_len
= priv
->writelen
;
608 spin_unlock_irqrestore(&priv
->lock
, flags
);
609 dev_dbg(&port
->dev
, "%s - writing %i chars : %*ph\n", __func__
,
610 buf_len
, buf_len
, buf_ptr
);
611 usb_fill_bulk_urb(port
->write_urb
, port
->serial
->dev
,
612 usb_sndbulkpipe(port
->serial
->dev
,
613 port
->bulk_out_endpointAddress
),
614 port
->write_urb
->transfer_buffer
, buf_len
+ 3,
616 result
= usb_submit_urb(port
->write_urb
, GFP_ATOMIC
);
617 usb_serial_port_softint(port
);
621 static int iuu_read_buf(struct usb_serial_port
*port
, int len
)
625 usb_fill_bulk_urb(port
->read_urb
, port
->serial
->dev
,
626 usb_rcvbulkpipe(port
->serial
->dev
,
627 port
->bulk_in_endpointAddress
),
628 port
->read_urb
->transfer_buffer
, len
,
629 read_buf_callback
, port
);
630 result
= usb_submit_urb(port
->read_urb
, GFP_ATOMIC
);
634 static void iuu_uart_read_callback(struct urb
*urb
)
636 struct usb_serial_port
*port
= urb
->context
;
637 struct iuu_private
*priv
= usb_get_serial_port_data(port
);
639 int status
= urb
->status
;
642 unsigned char *data
= urb
->transfer_buffer
;
646 dev_dbg(&port
->dev
, "%s - status = %d\n", __func__
, status
);
651 if (urb
->actual_length
== 1)
654 if (urb
->actual_length
> 1) {
655 dev_dbg(&port
->dev
, "%s - urb->actual_length = %i\n", __func__
,
660 /* if len > 0 call readbuf */
662 if (len
> 0 && error
== 0) {
663 dev_dbg(&port
->dev
, "%s - call read buf - len to read is %i\n",
665 status
= iuu_read_buf(port
, len
);
668 /* need to update status ? */
669 if (priv
->poll
> 99) {
670 status
= iuu_status(port
);
675 /* reset waiting ? */
677 if (priv
->reset
== 1) {
678 status
= iuu_reset(port
, 0xC);
681 /* Writebuf is waiting */
682 spin_lock_irqsave(&priv
->lock
, flags
);
683 if (priv
->writelen
> 0) {
684 spin_unlock_irqrestore(&priv
->lock
, flags
);
685 status
= iuu_bulk_write(port
);
688 spin_unlock_irqrestore(&priv
->lock
, flags
);
689 /* if nothing to write call again rxcmd */
690 dev_dbg(&port
->dev
, "%s - rxcmd recall\n", __func__
);
691 iuu_led_activity_off(urb
);
694 static int iuu_uart_write(struct tty_struct
*tty
, struct usb_serial_port
*port
,
695 const u8
*buf
, int count
)
697 struct iuu_private
*priv
= usb_get_serial_port_data(port
);
703 spin_lock_irqsave(&priv
->lock
, flags
);
705 /* fill the buffer */
706 memcpy(priv
->writebuf
+ priv
->writelen
, buf
, count
);
707 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
;
856 switch (parity
& 0xF0) {
857 case IUU_ONE_STOP_BIT
:
858 dataout
[DataCount
- 1] |= IUU_ONE_STOP_BIT
;
861 case IUU_TWO_STOP_BITS
:
862 dataout
[DataCount
- 1] |= IUU_TWO_STOP_BITS
;
866 return IUU_INVALID_PARAMETER
;
870 status
= bulk_immediate(port
, dataout
, DataCount
);
871 if (status
!= IUU_OPERATION_OK
)
872 dev_dbg(&port
->dev
, "%s - uart_off error\n", __func__
);
877 static void iuu_set_termios(struct tty_struct
*tty
,
878 struct usb_serial_port
*port
, struct ktermios
*old_termios
)
880 const u32 supported_mask
= CMSPAR
|PARENB
|PARODD
;
881 struct iuu_private
*priv
= usb_get_serial_port_data(port
);
882 unsigned int cflag
= tty
->termios
.c_cflag
;
888 u32 newval
= cflag
& supported_mask
;
890 /* Just use the ospeed. ispeed should be the same. */
891 baud
= tty
->termios
.c_ospeed
;
893 dev_dbg(&port
->dev
, "%s - enter c_ospeed or baud=%d\n", __func__
, baud
);
895 /* compute the parity parameter */
897 if (cflag
& CMSPAR
) { /* Using mark space */
899 parity
|= IUU_PARITY_SPACE
;
901 parity
|= IUU_PARITY_MARK
;
902 } else if (!(cflag
& PARENB
)) {
903 parity
|= IUU_PARITY_NONE
;
905 } else if (cflag
& PARODD
)
906 parity
|= IUU_PARITY_ODD
;
908 parity
|= IUU_PARITY_EVEN
;
910 parity
|= (cflag
& CSTOPB
? IUU_TWO_STOP_BITS
: IUU_ONE_STOP_BIT
);
913 status
= iuu_uart_baud(port
,
914 baud
* priv
->boost
/ 100,
917 /* set the termios value to the real one, so the user now what has
918 * changed. We support few fields so its easies to copy the old hw
919 * settings back over and then adjust them
922 tty_termios_copy_hw(&tty
->termios
, old_termios
);
923 if (status
!= 0) /* Set failed - return old bits */
925 /* Re-encode speed, parity and csize */
926 tty_encode_baud_rate(tty
, baud
, baud
);
927 tty
->termios
.c_cflag
&= ~(supported_mask
|CSIZE
);
928 tty
->termios
.c_cflag
|= newval
| csize
;
931 static void iuu_close(struct usb_serial_port
*port
)
933 /* iuu_led (port,255,0,0,0); */
937 usb_kill_urb(port
->write_urb
);
938 usb_kill_urb(port
->read_urb
);
940 iuu_led(port
, 0, 0, 0xF000, 0xFF);
943 static void iuu_init_termios(struct tty_struct
*tty
)
945 tty
->termios
= tty_std_termios
;
946 tty
->termios
.c_cflag
= CLOCAL
| CREAD
| CS8
| B9600
947 | TIOCM_CTS
| CSTOPB
| PARENB
;
948 tty
->termios
.c_ispeed
= 9600;
949 tty
->termios
.c_ospeed
= 9600;
950 tty
->termios
.c_lflag
= 0;
951 tty
->termios
.c_oflag
= 0;
952 tty
->termios
.c_iflag
= 0;
955 static int iuu_open(struct tty_struct
*tty
, struct usb_serial_port
*port
)
957 struct usb_serial
*serial
= port
->serial
;
958 struct device
*dev
= &port
->dev
;
962 struct iuu_private
*priv
= usb_get_serial_port_data(port
);
964 baud
= tty
->termios
.c_ospeed
;
966 dev_dbg(dev
, "%s - baud %d\n", __func__
, baud
);
967 usb_clear_halt(serial
->dev
, port
->write_urb
->pipe
);
968 usb_clear_halt(serial
->dev
, port
->read_urb
->pipe
);
972 #define SOUP(a, b, c, d) do { \
973 result = usb_control_msg(port->serial->dev, \
974 usb_sndctrlpipe(port->serial->dev, 0), \
975 b, a, c, d, NULL, 0, 1000); \
976 dev_dbg(dev, "0x%x:0x%x:0x%x:0x%x %d\n", a, b, c, d, result); } while (0)
978 /* This is not UART related but IUU USB driver related or something */
979 /* like that. Basically no IUU will accept any commands from the USB */
980 /* host unless it has received the following message */
981 /* sprintf(buf ,"%c%c%c%c",0x03,0x02,0x02,0x0); */
983 SOUP(0x03, 0x02, 0x02, 0x0);
985 iuu_led(port
, 0xF000, 0xF000, 0, 0xFF);
991 case 2: /* 3.680 Mhz */
992 priv
->clk
= IUU_CLK_3680000
;
993 iuu_clk(port
, IUU_CLK_3680000
* boost
/ 100);
995 iuu_uart_baud(port
, baud
* boost
/ 100, &actual
,
998 case 3: /* 6.00 Mhz */
999 iuu_clk(port
, IUU_CLK_6000000
* boost
/ 100);
1000 priv
->clk
= IUU_CLK_6000000
;
1001 /* Ratio of 6000000 to 3500000 for baud 9600 */
1003 iuu_uart_baud(port
, 16457 * boost
/ 100, &actual
,
1006 default: /* 3.579 Mhz */
1007 iuu_clk(port
, IUU_CLK_3579000
* boost
/ 100);
1008 priv
->clk
= IUU_CLK_3579000
;
1010 iuu_uart_baud(port
, baud
* boost
/ 100, &actual
,
1014 /* set the cardin cardout signals */
1021 iuu_cardin
= TIOCM_CD
;
1026 iuu_cardout
= TIOCM_CD
;
1029 iuu_cardin
= TIOCM_DSR
;
1034 iuu_cardout
= TIOCM_DSR
;
1037 iuu_cardin
= TIOCM_CTS
;
1042 iuu_cardout
= TIOCM_CTS
;
1045 iuu_cardin
= TIOCM_RNG
;
1050 iuu_cardout
= TIOCM_RNG
;
1053 iuu_uart_flush(port
);
1055 dev_dbg(dev
, "%s - initialization done\n", __func__
);
1057 memset(port
->write_urb
->transfer_buffer
, IUU_UART_RX
, 1);
1058 usb_fill_bulk_urb(port
->write_urb
, port
->serial
->dev
,
1059 usb_sndbulkpipe(port
->serial
->dev
,
1060 port
->bulk_out_endpointAddress
),
1061 port
->write_urb
->transfer_buffer
, 1,
1062 read_rxcmd_callback
, port
);
1063 result
= usb_submit_urb(port
->write_urb
, GFP_KERNEL
);
1065 dev_err(dev
, "%s - failed submitting read urb, error %d\n", __func__
, result
);
1068 dev_dbg(dev
, "%s - rxcmd OK\n", __func__
);
1074 /* how to change VCC */
1075 static int iuu_vcc_set(struct usb_serial_port
*port
, unsigned int vcc
)
1080 buf
= kmalloc(5, GFP_KERNEL
);
1084 buf
[0] = IUU_SET_VCC
;
1085 buf
[1] = vcc
& 0xFF;
1086 buf
[2] = (vcc
>> 8) & 0xFF;
1087 buf
[3] = (vcc
>> 16) & 0xFF;
1088 buf
[4] = (vcc
>> 24) & 0xFF;
1090 status
= bulk_immediate(port
, buf
, 5);
1093 if (status
!= IUU_OPERATION_OK
)
1094 dev_dbg(&port
->dev
, "%s - vcc error status = %2x\n", __func__
, status
);
1096 dev_dbg(&port
->dev
, "%s - vcc OK !\n", __func__
);
1105 static ssize_t
vcc_mode_show(struct device
*dev
,
1106 struct device_attribute
*attr
, char *buf
)
1108 struct usb_serial_port
*port
= to_usb_serial_port(dev
);
1109 struct iuu_private
*priv
= usb_get_serial_port_data(port
);
1111 return sprintf(buf
, "%d\n", priv
->vcc
);
1114 static ssize_t
vcc_mode_store(struct device
*dev
,
1115 struct device_attribute
*attr
, const char *buf
, size_t count
)
1117 struct usb_serial_port
*port
= to_usb_serial_port(dev
);
1118 struct iuu_private
*priv
= usb_get_serial_port_data(port
);
1121 if (kstrtoul(buf
, 10, &v
)) {
1122 dev_err(dev
, "%s - vcc_mode: %s is not a unsigned long\n",
1124 goto fail_store_vcc_mode
;
1127 dev_dbg(dev
, "%s: setting vcc_mode = %ld\n", __func__
, v
);
1129 if ((v
!= 3) && (v
!= 5)) {
1130 dev_err(dev
, "%s - vcc_mode %ld is invalid\n", __func__
, v
);
1132 iuu_vcc_set(port
, v
);
1135 fail_store_vcc_mode
:
1138 static DEVICE_ATTR_RW(vcc_mode
);
1140 static int iuu_create_sysfs_attrs(struct usb_serial_port
*port
)
1142 return device_create_file(&port
->dev
, &dev_attr_vcc_mode
);
1145 static int iuu_remove_sysfs_attrs(struct usb_serial_port
*port
)
1147 device_remove_file(&port
->dev
, &dev_attr_vcc_mode
);
1152 * End Sysfs Attributes
1155 static struct usb_serial_driver iuu_device
= {
1157 .owner
= THIS_MODULE
,
1158 .name
= "iuu_phoenix",
1160 .id_table
= id_table
,
1164 .bulk_in_size
= 512,
1165 .bulk_out_size
= 512,
1168 .write
= iuu_uart_write
,
1169 .read_bulk_callback
= iuu_uart_read_callback
,
1170 .tiocmget
= iuu_tiocmget
,
1171 .tiocmset
= iuu_tiocmset
,
1172 .set_termios
= iuu_set_termios
,
1173 .init_termios
= iuu_init_termios
,
1174 .port_probe
= iuu_port_probe
,
1175 .port_remove
= iuu_port_remove
,
1178 static struct usb_serial_driver
* const serial_drivers
[] = {
1182 module_usb_serial_driver(serial_drivers
, id_table
);
1184 MODULE_AUTHOR("Alain Degreffe eczema@ecze.com");
1186 MODULE_DESCRIPTION(DRIVER_DESC
);
1187 MODULE_LICENSE("GPL");
1189 module_param(xmas
, bool, S_IRUGO
| S_IWUSR
);
1190 MODULE_PARM_DESC(xmas
, "Xmas colors enabled or not");
1192 module_param(boost
, int, S_IRUGO
| S_IWUSR
);
1193 MODULE_PARM_DESC(boost
, "Card overclock boost (in percent 100-500)");
1195 module_param(clockmode
, int, S_IRUGO
| S_IWUSR
);
1196 MODULE_PARM_DESC(clockmode
, "Card clock mode (1=3.579 MHz, 2=3.680 MHz, "
1199 module_param(cdmode
, int, S_IRUGO
| S_IWUSR
);
1200 MODULE_PARM_DESC(cdmode
, "Card detect mode (0=none, 1=CD, 2=!CD, 3=DSR, "
1201 "4=!DSR, 5=CTS, 6=!CTS, 7=RING, 8=!RING)");
1203 module_param(vcc_default
, int, S_IRUGO
| S_IWUSR
);
1204 MODULE_PARM_DESC(vcc_default
, "Set default VCC (either 3 for 3.3V or 5 "
1205 "for 5V). Default to 5.");