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 void 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
);
113 static int iuu_tiocmset(struct tty_struct
*tty
,
114 unsigned int set
, unsigned int clear
)
116 struct usb_serial_port
*port
= tty
->driver_data
;
117 struct iuu_private
*priv
= usb_get_serial_port_data(port
);
120 /* FIXME: locking on tiomstatus */
121 dev_dbg(&port
->dev
, "%s msg : SET = 0x%04x, CLEAR = 0x%04x\n",
122 __func__
, set
, clear
);
124 spin_lock_irqsave(&priv
->lock
, flags
);
126 if ((set
& TIOCM_RTS
) && !(priv
->tiostatus
== TIOCM_RTS
)) {
127 dev_dbg(&port
->dev
, "%s TIOCMSET RESET called !!!\n", __func__
);
131 priv
->tiostatus
= TIOCM_RTS
;
133 spin_unlock_irqrestore(&priv
->lock
, flags
);
137 /* This is used to provide a carrier detect mechanism
138 * When a card is present, the response is 0x00
139 * When no card , the reader respond with TIOCM_CD
140 * This is known as CD autodetect mechanism
142 static int iuu_tiocmget(struct tty_struct
*tty
)
144 struct usb_serial_port
*port
= tty
->driver_data
;
145 struct iuu_private
*priv
= usb_get_serial_port_data(port
);
149 spin_lock_irqsave(&priv
->lock
, flags
);
150 rc
= priv
->tiostatus
;
151 spin_unlock_irqrestore(&priv
->lock
, flags
);
156 static void iuu_rxcmd(struct urb
*urb
)
158 struct usb_serial_port
*port
= urb
->context
;
159 int status
= urb
->status
;
162 dev_dbg(&port
->dev
, "%s - status = %d\n", __func__
, status
);
168 memset(port
->write_urb
->transfer_buffer
, IUU_UART_RX
, 1);
169 usb_fill_bulk_urb(port
->write_urb
, port
->serial
->dev
,
170 usb_sndbulkpipe(port
->serial
->dev
,
171 port
->bulk_out_endpointAddress
),
172 port
->write_urb
->transfer_buffer
, 1,
173 read_rxcmd_callback
, port
);
174 usb_submit_urb(port
->write_urb
, GFP_ATOMIC
);
177 static int iuu_reset(struct usb_serial_port
*port
, u8 wt
)
179 struct iuu_private
*priv
= usb_get_serial_port_data(port
);
181 char *buf_ptr
= port
->write_urb
->transfer_buffer
;
183 /* Prepare the reset sequence */
185 *buf_ptr
++ = IUU_RST_SET
;
186 *buf_ptr
++ = IUU_DELAY_MS
;
188 *buf_ptr
= IUU_RST_CLEAR
;
190 /* send the sequence */
192 usb_fill_bulk_urb(port
->write_urb
,
194 usb_sndbulkpipe(port
->serial
->dev
,
195 port
->bulk_out_endpointAddress
),
196 port
->write_urb
->transfer_buffer
, 4, iuu_rxcmd
, port
);
197 result
= usb_submit_urb(port
->write_urb
, GFP_ATOMIC
);
208 static void iuu_update_status_callback(struct urb
*urb
)
210 struct usb_serial_port
*port
= urb
->context
;
211 struct iuu_private
*priv
= usb_get_serial_port_data(port
);
213 int status
= urb
->status
;
216 dev_dbg(&port
->dev
, "%s - status = %d\n", __func__
, status
);
221 st
= urb
->transfer_buffer
;
222 dev_dbg(&port
->dev
, "%s - enter\n", __func__
);
223 if (urb
->actual_length
== 1) {
226 priv
->tiostatus
= iuu_cardout
;
229 priv
->tiostatus
= iuu_cardin
;
232 priv
->tiostatus
= iuu_cardin
;
238 static void iuu_status_callback(struct urb
*urb
)
240 struct usb_serial_port
*port
= urb
->context
;
241 int status
= urb
->status
;
243 dev_dbg(&port
->dev
, "%s - status = %d\n", __func__
, status
);
244 usb_fill_bulk_urb(port
->read_urb
, port
->serial
->dev
,
245 usb_rcvbulkpipe(port
->serial
->dev
,
246 port
->bulk_in_endpointAddress
),
247 port
->read_urb
->transfer_buffer
, 256,
248 iuu_update_status_callback
, port
);
249 usb_submit_urb(port
->read_urb
, GFP_ATOMIC
);
252 static int iuu_status(struct usb_serial_port
*port
)
256 memset(port
->write_urb
->transfer_buffer
, IUU_GET_STATE_REGISTER
, 1);
257 usb_fill_bulk_urb(port
->write_urb
, port
->serial
->dev
,
258 usb_sndbulkpipe(port
->serial
->dev
,
259 port
->bulk_out_endpointAddress
),
260 port
->write_urb
->transfer_buffer
, 1,
261 iuu_status_callback
, port
);
262 result
= usb_submit_urb(port
->write_urb
, GFP_ATOMIC
);
267 static int bulk_immediate(struct usb_serial_port
*port
, u8
*buf
, u8 count
)
270 struct usb_serial
*serial
= port
->serial
;
273 /* send the data out the bulk port */
276 usb_bulk_msg(serial
->dev
,
277 usb_sndbulkpipe(serial
->dev
,
278 port
->bulk_out_endpointAddress
), buf
,
279 count
, &actual
, 1000);
281 if (status
!= IUU_OPERATION_OK
)
282 dev_dbg(&port
->dev
, "%s - error = %2x\n", __func__
, status
);
284 dev_dbg(&port
->dev
, "%s - write OK !\n", __func__
);
288 static int read_immediate(struct usb_serial_port
*port
, u8
*buf
, u8 count
)
291 struct usb_serial
*serial
= port
->serial
;
294 /* send the data out the bulk port */
296 usb_bulk_msg(serial
->dev
,
297 usb_rcvbulkpipe(serial
->dev
,
298 port
->bulk_in_endpointAddress
), buf
,
299 count
, &actual
, 1000);
301 if (status
!= IUU_OPERATION_OK
)
302 dev_dbg(&port
->dev
, "%s - error = %2x\n", __func__
, status
);
304 dev_dbg(&port
->dev
, "%s - read OK !\n", __func__
);
308 static int iuu_led(struct usb_serial_port
*port
, unsigned int R
,
309 unsigned int G
, unsigned int B
, u8 f
)
313 buf
= kmalloc(8, GFP_KERNEL
);
317 buf
[0] = IUU_SET_LED
;
319 buf
[2] = (R
>> 8) & 0xFF;
321 buf
[4] = (G
>> 8) & 0xFF;
323 buf
[6] = (B
>> 8) & 0xFF;
325 status
= bulk_immediate(port
, buf
, 8);
327 if (status
!= IUU_OPERATION_OK
)
328 dev_dbg(&port
->dev
, "%s - led error status = %2x\n", __func__
, status
);
330 dev_dbg(&port
->dev
, "%s - led OK !\n", __func__
);
331 return IUU_OPERATION_OK
;
334 static void iuu_rgbf_fill_buffer(u8
*buf
, u8 r1
, u8 r2
, u8 g1
, u8 g2
, u8 b1
,
337 *buf
++ = IUU_SET_LED
;
347 static void iuu_led_activity_on(struct urb
*urb
)
349 struct usb_serial_port
*port
= urb
->context
;
350 char *buf_ptr
= port
->write_urb
->transfer_buffer
;
353 buf_ptr
[0] = IUU_SET_LED
;
354 get_random_bytes(buf_ptr
+ 1, 6);
357 iuu_rgbf_fill_buffer(buf_ptr
, 255, 255, 0, 0, 0, 0, 255);
360 usb_fill_bulk_urb(port
->write_urb
, port
->serial
->dev
,
361 usb_sndbulkpipe(port
->serial
->dev
,
362 port
->bulk_out_endpointAddress
),
363 port
->write_urb
->transfer_buffer
, 8 ,
365 usb_submit_urb(port
->write_urb
, GFP_ATOMIC
);
368 static void iuu_led_activity_off(struct urb
*urb
)
370 struct usb_serial_port
*port
= urb
->context
;
371 char *buf_ptr
= port
->write_urb
->transfer_buffer
;
378 iuu_rgbf_fill_buffer(buf_ptr
, 0, 0, 255, 255, 0, 0, 255);
380 usb_fill_bulk_urb(port
->write_urb
, port
->serial
->dev
,
381 usb_sndbulkpipe(port
->serial
->dev
,
382 port
->bulk_out_endpointAddress
),
383 port
->write_urb
->transfer_buffer
, 8 ,
385 usb_submit_urb(port
->write_urb
, GFP_ATOMIC
);
390 static int iuu_clk(struct usb_serial_port
*port
, int dwFrq
)
393 struct iuu_private
*priv
= usb_get_serial_port_data(port
);
396 u8 DIV
= 0; /* 8bit */
397 u8 XDRV
= 0; /* 8bit */
398 u8 PUMP
= 0; /* 3bit */
399 u8 PBmsb
= 0; /* 2bit */
400 u8 PBlsb
= 0; /* 8bit */
401 u8 PO
= 0; /* 1bit */
406 int frq
= (int)dwFrq
;
409 priv
->buf
[Count
++] = IUU_UART_WRITE_I2C
;
410 priv
->buf
[Count
++] = FrqGenAdr
<< 1;
411 priv
->buf
[Count
++] = 0x09;
412 priv
->buf
[Count
++] = 0x00;
414 status
= bulk_immediate(port
, (u8
*) priv
->buf
, Count
);
416 dev_dbg(&port
->dev
, "%s - write error\n", __func__
);
419 } else if (frq
== 3579000) {
424 } else if (frq
== 3680000) {
429 } else if (frq
== 6000000) {
435 unsigned int result
= 0;
436 unsigned int tmp
= 0;
441 unsigned int lP
= 2055;
442 unsigned int lDiv
= 4;
444 for (lQ
= 2; lQ
<= 47 && !found
; lQ
++)
445 for (lP
= 2055; lP
>= 8 && !found
; lP
--)
446 for (lDiv
= 4; lDiv
<= 127 && !found
; lDiv
++) {
447 tmp
= (12000000 / lDiv
) * (lP
/ lQ
);
448 if (abs((int)(tmp
- frq
)) <
449 abs((int)(frq
- result
))) {
450 check2
= (12000000 / lQ
);
453 check
= (12000000 / lQ
) * lP
;
454 if (check
> 400000000)
456 if (check
< 100000000)
458 if (lDiv
< 4 || lDiv
> 127)
469 P2
= ((P
- PO
) / 2) - 4;
471 PBmsb
= (P2
>> 8 & 0x03);
473 PO
= (P
>> 10) & 0x01;
476 priv
->buf
[Count
++] = IUU_UART_WRITE_I2C
; /* 0x4C */
477 priv
->buf
[Count
++] = FrqGenAdr
<< 1;
478 priv
->buf
[Count
++] = 0x09;
479 priv
->buf
[Count
++] = 0x20; /* Adr = 0x09 */
480 priv
->buf
[Count
++] = IUU_UART_WRITE_I2C
; /* 0x4C */
481 priv
->buf
[Count
++] = FrqGenAdr
<< 1;
482 priv
->buf
[Count
++] = 0x0C;
483 priv
->buf
[Count
++] = DIV
; /* Adr = 0x0C */
484 priv
->buf
[Count
++] = IUU_UART_WRITE_I2C
; /* 0x4C */
485 priv
->buf
[Count
++] = FrqGenAdr
<< 1;
486 priv
->buf
[Count
++] = 0x12;
487 priv
->buf
[Count
++] = XDRV
; /* Adr = 0x12 */
488 priv
->buf
[Count
++] = IUU_UART_WRITE_I2C
; /* 0x4C */
489 priv
->buf
[Count
++] = FrqGenAdr
<< 1;
490 priv
->buf
[Count
++] = 0x13;
491 priv
->buf
[Count
++] = 0x6B; /* Adr = 0x13 */
492 priv
->buf
[Count
++] = IUU_UART_WRITE_I2C
; /* 0x4C */
493 priv
->buf
[Count
++] = FrqGenAdr
<< 1;
494 priv
->buf
[Count
++] = 0x40;
495 priv
->buf
[Count
++] = (0xC0 | ((PUMP
& 0x07) << 2)) |
496 (PBmsb
& 0x03); /* Adr = 0x40 */
497 priv
->buf
[Count
++] = IUU_UART_WRITE_I2C
; /* 0x4C */
498 priv
->buf
[Count
++] = FrqGenAdr
<< 1;
499 priv
->buf
[Count
++] = 0x41;
500 priv
->buf
[Count
++] = PBlsb
; /* Adr = 0x41 */
501 priv
->buf
[Count
++] = IUU_UART_WRITE_I2C
; /* 0x4C */
502 priv
->buf
[Count
++] = FrqGenAdr
<< 1;
503 priv
->buf
[Count
++] = 0x42;
504 priv
->buf
[Count
++] = Q
| (((PO
& 0x01) << 7)); /* Adr = 0x42 */
505 priv
->buf
[Count
++] = IUU_UART_WRITE_I2C
; /* 0x4C */
506 priv
->buf
[Count
++] = FrqGenAdr
<< 1;
507 priv
->buf
[Count
++] = 0x44;
508 priv
->buf
[Count
++] = (char)0xFF; /* Adr = 0x44 */
509 priv
->buf
[Count
++] = IUU_UART_WRITE_I2C
; /* 0x4C */
510 priv
->buf
[Count
++] = FrqGenAdr
<< 1;
511 priv
->buf
[Count
++] = 0x45;
512 priv
->buf
[Count
++] = (char)0xFE; /* Adr = 0x45 */
513 priv
->buf
[Count
++] = IUU_UART_WRITE_I2C
; /* 0x4C */
514 priv
->buf
[Count
++] = FrqGenAdr
<< 1;
515 priv
->buf
[Count
++] = 0x46;
516 priv
->buf
[Count
++] = 0x7F; /* Adr = 0x46 */
517 priv
->buf
[Count
++] = IUU_UART_WRITE_I2C
; /* 0x4C */
518 priv
->buf
[Count
++] = FrqGenAdr
<< 1;
519 priv
->buf
[Count
++] = 0x47;
520 priv
->buf
[Count
++] = (char)0x84; /* Adr = 0x47 */
522 status
= bulk_immediate(port
, (u8
*) priv
->buf
, Count
);
523 if (status
!= IUU_OPERATION_OK
)
524 dev_dbg(&port
->dev
, "%s - write error\n", __func__
);
528 static int iuu_uart_flush(struct usb_serial_port
*port
)
530 struct device
*dev
= &port
->dev
;
534 struct iuu_private
*priv
= usb_get_serial_port_data(port
);
536 if (iuu_led(port
, 0xF000, 0, 0, 0xFF) < 0)
539 rxcmd
= kmalloc(1, GFP_KERNEL
);
543 rxcmd
[0] = IUU_UART_RX
;
545 for (i
= 0; i
< 2; i
++) {
546 status
= bulk_immediate(port
, rxcmd
, 1);
547 if (status
!= IUU_OPERATION_OK
) {
548 dev_dbg(dev
, "%s - uart_flush_write error\n", __func__
);
552 status
= read_immediate(port
, &priv
->len
, 1);
553 if (status
!= IUU_OPERATION_OK
) {
554 dev_dbg(dev
, "%s - uart_flush_read error\n", __func__
);
559 dev_dbg(dev
, "%s - uart_flush datalen is : %i\n", __func__
, priv
->len
);
560 status
= read_immediate(port
, priv
->buf
, priv
->len
);
561 if (status
!= IUU_OPERATION_OK
) {
562 dev_dbg(dev
, "%s - uart_flush_read error\n", __func__
);
567 dev_dbg(dev
, "%s - uart_flush_read OK!\n", __func__
);
568 iuu_led(port
, 0, 0xF000, 0, 0xFF);
576 static void read_buf_callback(struct urb
*urb
)
578 struct usb_serial_port
*port
= urb
->context
;
579 unsigned char *data
= urb
->transfer_buffer
;
580 int status
= urb
->status
;
583 if (status
== -EPROTO
) {
584 /* reschedule needed */
589 dev_dbg(&port
->dev
, "%s - %i chars to write\n", __func__
, urb
->actual_length
);
591 if (urb
->actual_length
) {
592 tty_insert_flip_string(&port
->port
, data
, urb
->actual_length
);
593 tty_flip_buffer_push(&port
->port
);
595 iuu_led_activity_on(urb
);
598 static int iuu_bulk_write(struct usb_serial_port
*port
)
600 struct iuu_private
*priv
= usb_get_serial_port_data(port
);
604 char *buf_ptr
= port
->write_urb
->transfer_buffer
;
606 spin_lock_irqsave(&priv
->lock
, flags
);
607 *buf_ptr
++ = IUU_UART_ESC
;
608 *buf_ptr
++ = IUU_UART_TX
;
609 *buf_ptr
++ = priv
->writelen
;
611 memcpy(buf_ptr
, priv
->writebuf
, priv
->writelen
);
612 buf_len
= priv
->writelen
;
614 spin_unlock_irqrestore(&priv
->lock
, flags
);
615 dev_dbg(&port
->dev
, "%s - writing %i chars : %*ph\n", __func__
,
616 buf_len
, buf_len
, buf_ptr
);
617 usb_fill_bulk_urb(port
->write_urb
, port
->serial
->dev
,
618 usb_sndbulkpipe(port
->serial
->dev
,
619 port
->bulk_out_endpointAddress
),
620 port
->write_urb
->transfer_buffer
, buf_len
+ 3,
622 result
= usb_submit_urb(port
->write_urb
, GFP_ATOMIC
);
623 usb_serial_port_softint(port
);
627 static int iuu_read_buf(struct usb_serial_port
*port
, int len
)
631 usb_fill_bulk_urb(port
->read_urb
, port
->serial
->dev
,
632 usb_rcvbulkpipe(port
->serial
->dev
,
633 port
->bulk_in_endpointAddress
),
634 port
->read_urb
->transfer_buffer
, len
,
635 read_buf_callback
, port
);
636 result
= usb_submit_urb(port
->read_urb
, GFP_ATOMIC
);
640 static void iuu_uart_read_callback(struct urb
*urb
)
642 struct usb_serial_port
*port
= urb
->context
;
643 struct iuu_private
*priv
= usb_get_serial_port_data(port
);
645 int status
= urb
->status
;
647 unsigned char *data
= urb
->transfer_buffer
;
651 dev_dbg(&port
->dev
, "%s - status = %d\n", __func__
, status
);
656 if (urb
->actual_length
== 1)
659 if (urb
->actual_length
> 1) {
660 dev_dbg(&port
->dev
, "%s - urb->actual_length = %i\n", __func__
,
664 /* if len > 0 call readbuf */
667 dev_dbg(&port
->dev
, "%s - call read buf - len to read is %i\n",
669 status
= iuu_read_buf(port
, len
);
672 /* need to update status ? */
673 if (priv
->poll
> 99) {
674 status
= iuu_status(port
);
679 /* reset waiting ? */
681 if (priv
->reset
== 1) {
682 status
= iuu_reset(port
, 0xC);
685 /* Writebuf is waiting */
686 spin_lock_irqsave(&priv
->lock
, flags
);
687 if (priv
->writelen
> 0) {
688 spin_unlock_irqrestore(&priv
->lock
, flags
);
689 status
= iuu_bulk_write(port
);
692 spin_unlock_irqrestore(&priv
->lock
, flags
);
693 /* if nothing to write call again rxcmd */
694 dev_dbg(&port
->dev
, "%s - rxcmd recall\n", __func__
);
695 iuu_led_activity_off(urb
);
698 static int iuu_uart_write(struct tty_struct
*tty
, struct usb_serial_port
*port
,
699 const u8
*buf
, int count
)
701 struct iuu_private
*priv
= usb_get_serial_port_data(port
);
704 spin_lock_irqsave(&priv
->lock
, flags
);
706 count
= min(count
, 256 - priv
->writelen
);
710 /* fill the buffer */
711 memcpy(priv
->writebuf
+ priv
->writelen
, buf
, count
);
712 priv
->writelen
+= count
;
714 spin_unlock_irqrestore(&priv
->lock
, flags
);
719 static void read_rxcmd_callback(struct urb
*urb
)
721 struct usb_serial_port
*port
= urb
->context
;
723 int status
= urb
->status
;
730 usb_fill_bulk_urb(port
->read_urb
, port
->serial
->dev
,
731 usb_rcvbulkpipe(port
->serial
->dev
,
732 port
->bulk_in_endpointAddress
),
733 port
->read_urb
->transfer_buffer
, 256,
734 iuu_uart_read_callback
, port
);
735 result
= usb_submit_urb(port
->read_urb
, GFP_ATOMIC
);
736 dev_dbg(&port
->dev
, "%s - submit result = %d\n", __func__
, result
);
739 static int iuu_uart_on(struct usb_serial_port
*port
)
744 buf
= kmalloc(4, GFP_KERNEL
);
749 buf
[0] = IUU_UART_ENABLE
;
750 buf
[1] = (u8
) ((IUU_BAUD_9600
>> 8) & 0x00FF);
751 buf
[2] = (u8
) (0x00FF & IUU_BAUD_9600
);
752 buf
[3] = (u8
) (0x0F0 & IUU_ONE_STOP_BIT
) | (0x07 & IUU_PARITY_EVEN
);
754 status
= bulk_immediate(port
, buf
, 4);
755 if (status
!= IUU_OPERATION_OK
) {
756 dev_dbg(&port
->dev
, "%s - uart_on error\n", __func__
);
757 goto uart_enable_failed
;
759 /* iuu_reset() the card after iuu_uart_on() */
760 status
= iuu_uart_flush(port
);
761 if (status
!= IUU_OPERATION_OK
)
762 dev_dbg(&port
->dev
, "%s - uart_flush error\n", __func__
);
768 /* Disables the IUU UART (a.k.a. the Phoenix voiderface) */
769 static int iuu_uart_off(struct usb_serial_port
*port
)
773 buf
= kmalloc(1, GFP_KERNEL
);
776 buf
[0] = IUU_UART_DISABLE
;
778 status
= bulk_immediate(port
, buf
, 1);
779 if (status
!= IUU_OPERATION_OK
)
780 dev_dbg(&port
->dev
, "%s - uart_off error\n", __func__
);
786 static int iuu_uart_baud(struct usb_serial_port
*port
, u32 baud_base
,
787 u32
*actual
, u8 parity
)
795 unsigned int T1FrekvensHZ
= 0;
797 dev_dbg(&port
->dev
, "%s - enter baud_base=%d\n", __func__
, baud_base
);
798 dataout
= kmalloc(5, GFP_KERNEL
);
802 /*baud = (((priv->clk / 35) * baud_base) / 100000); */
805 if (baud
< 1200 || baud
> 230400) {
807 return IUU_INVALID_PARAMETER
;
811 T1FrekvensHZ
= 500000;
816 T1FrekvensHZ
= 2000000;
821 T1FrekvensHZ
= 6000000;
826 T1FrekvensHZ
= 24000000;
829 T1reload
= 256 - (u8
) (T1FrekvensHZ
/ (baud
* 2));
831 /* magic number here: ENTER_FIRMWARE_UPDATE; */
832 dataout
[DataCount
++] = IUU_UART_ESC
;
833 /* magic number here: CHANGE_BAUD; */
834 dataout
[DataCount
++] = IUU_UART_CHANGE
;
835 dataout
[DataCount
++] = T1Frekvens
;
836 dataout
[DataCount
++] = T1reload
;
838 *actual
= (T1FrekvensHZ
/ (256 - T1reload
)) / 2;
840 switch (parity
& 0x0F) {
841 case IUU_PARITY_NONE
:
842 dataout
[DataCount
++] = 0x00;
844 case IUU_PARITY_EVEN
:
845 dataout
[DataCount
++] = 0x01;
848 dataout
[DataCount
++] = 0x02;
850 case IUU_PARITY_MARK
:
851 dataout
[DataCount
++] = 0x03;
853 case IUU_PARITY_SPACE
:
854 dataout
[DataCount
++] = 0x04;
858 return IUU_INVALID_PARAMETER
;
861 switch (parity
& 0xF0) {
862 case IUU_ONE_STOP_BIT
:
863 dataout
[DataCount
- 1] |= IUU_ONE_STOP_BIT
;
866 case IUU_TWO_STOP_BITS
:
867 dataout
[DataCount
- 1] |= IUU_TWO_STOP_BITS
;
871 return IUU_INVALID_PARAMETER
;
874 status
= bulk_immediate(port
, dataout
, DataCount
);
875 if (status
!= IUU_OPERATION_OK
)
876 dev_dbg(&port
->dev
, "%s - uart_off error\n", __func__
);
881 static void iuu_set_termios(struct tty_struct
*tty
,
882 struct usb_serial_port
*port
,
883 const struct ktermios
*old_termios
)
885 const u32 supported_mask
= CMSPAR
|PARENB
|PARODD
;
886 struct iuu_private
*priv
= usb_get_serial_port_data(port
);
887 unsigned int cflag
= tty
->termios
.c_cflag
;
893 u32 newval
= cflag
& supported_mask
;
895 /* Just use the ospeed. ispeed should be the same. */
896 baud
= tty
->termios
.c_ospeed
;
898 dev_dbg(&port
->dev
, "%s - enter c_ospeed or baud=%d\n", __func__
, baud
);
900 /* compute the parity parameter */
902 if (cflag
& CMSPAR
) { /* Using mark space */
904 parity
|= IUU_PARITY_SPACE
;
906 parity
|= IUU_PARITY_MARK
;
907 } else if (!(cflag
& PARENB
)) {
908 parity
|= IUU_PARITY_NONE
;
910 } else if (cflag
& PARODD
)
911 parity
|= IUU_PARITY_ODD
;
913 parity
|= IUU_PARITY_EVEN
;
915 parity
|= (cflag
& CSTOPB
? IUU_TWO_STOP_BITS
: IUU_ONE_STOP_BIT
);
918 status
= iuu_uart_baud(port
,
919 baud
* priv
->boost
/ 100,
922 /* set the termios value to the real one, so the user now what has
923 * changed. We support few fields so its easies to copy the old hw
924 * settings back over and then adjust them
927 tty_termios_copy_hw(&tty
->termios
, old_termios
);
928 if (status
!= 0) /* Set failed - return old bits */
930 /* Re-encode speed, parity and csize */
931 tty_encode_baud_rate(tty
, baud
, baud
);
932 tty
->termios
.c_cflag
&= ~(supported_mask
|CSIZE
);
933 tty
->termios
.c_cflag
|= newval
| csize
;
936 static void iuu_close(struct usb_serial_port
*port
)
938 /* iuu_led (port,255,0,0,0); */
942 usb_kill_urb(port
->write_urb
);
943 usb_kill_urb(port
->read_urb
);
945 iuu_led(port
, 0, 0, 0xF000, 0xFF);
948 static void iuu_init_termios(struct tty_struct
*tty
)
950 tty
->termios
.c_cflag
= B9600
| CS8
| CSTOPB
| CREAD
| PARENB
| CLOCAL
;
951 tty
->termios
.c_ispeed
= 9600;
952 tty
->termios
.c_ospeed
= 9600;
953 tty
->termios
.c_lflag
= 0;
954 tty
->termios
.c_oflag
= 0;
955 tty
->termios
.c_iflag
= 0;
958 static int iuu_open(struct tty_struct
*tty
, struct usb_serial_port
*port
)
960 struct usb_serial
*serial
= port
->serial
;
961 struct device
*dev
= &port
->dev
;
965 struct iuu_private
*priv
= usb_get_serial_port_data(port
);
967 baud
= tty
->termios
.c_ospeed
;
969 dev_dbg(dev
, "%s - baud %d\n", __func__
, baud
);
970 usb_clear_halt(serial
->dev
, port
->write_urb
->pipe
);
971 usb_clear_halt(serial
->dev
, port
->read_urb
->pipe
);
975 #define SOUP(a, b, c, d) do { \
976 result = usb_control_msg(port->serial->dev, \
977 usb_sndctrlpipe(port->serial->dev, 0), \
978 b, a, c, d, NULL, 0, 1000); \
979 dev_dbg(dev, "0x%x:0x%x:0x%x:0x%x %d\n", a, b, c, d, result); } while (0)
981 /* This is not UART related but IUU USB driver related or something */
982 /* like that. Basically no IUU will accept any commands from the USB */
983 /* host unless it has received the following message */
984 /* sprintf(buf ,"%c%c%c%c",0x03,0x02,0x02,0x0); */
986 SOUP(0x03, 0x02, 0x02, 0x0);
988 iuu_led(port
, 0xF000, 0xF000, 0, 0xFF);
994 case 2: /* 3.680 Mhz */
995 priv
->clk
= IUU_CLK_3680000
;
996 iuu_clk(port
, IUU_CLK_3680000
* boost
/ 100);
998 iuu_uart_baud(port
, baud
* boost
/ 100, &actual
,
1001 case 3: /* 6.00 Mhz */
1002 iuu_clk(port
, IUU_CLK_6000000
* boost
/ 100);
1003 priv
->clk
= IUU_CLK_6000000
;
1004 /* Ratio of 6000000 to 3500000 for baud 9600 */
1006 iuu_uart_baud(port
, 16457 * boost
/ 100, &actual
,
1009 default: /* 3.579 Mhz */
1010 iuu_clk(port
, IUU_CLK_3579000
* boost
/ 100);
1011 priv
->clk
= IUU_CLK_3579000
;
1013 iuu_uart_baud(port
, baud
* boost
/ 100, &actual
,
1017 /* set the cardin cardout signals */
1024 iuu_cardin
= TIOCM_CD
;
1029 iuu_cardout
= TIOCM_CD
;
1032 iuu_cardin
= TIOCM_DSR
;
1037 iuu_cardout
= TIOCM_DSR
;
1040 iuu_cardin
= TIOCM_CTS
;
1045 iuu_cardout
= TIOCM_CTS
;
1048 iuu_cardin
= TIOCM_RNG
;
1053 iuu_cardout
= TIOCM_RNG
;
1056 iuu_uart_flush(port
);
1058 dev_dbg(dev
, "%s - initialization done\n", __func__
);
1060 memset(port
->write_urb
->transfer_buffer
, IUU_UART_RX
, 1);
1061 usb_fill_bulk_urb(port
->write_urb
, port
->serial
->dev
,
1062 usb_sndbulkpipe(port
->serial
->dev
,
1063 port
->bulk_out_endpointAddress
),
1064 port
->write_urb
->transfer_buffer
, 1,
1065 read_rxcmd_callback
, port
);
1066 result
= usb_submit_urb(port
->write_urb
, GFP_KERNEL
);
1068 dev_err(dev
, "%s - failed submitting read urb, error %d\n", __func__
, result
);
1071 dev_dbg(dev
, "%s - rxcmd OK\n", __func__
);
1077 /* how to change VCC */
1078 static int iuu_vcc_set(struct usb_serial_port
*port
, unsigned int vcc
)
1083 buf
= kmalloc(5, GFP_KERNEL
);
1087 buf
[0] = IUU_SET_VCC
;
1088 buf
[1] = vcc
& 0xFF;
1089 buf
[2] = (vcc
>> 8) & 0xFF;
1090 buf
[3] = (vcc
>> 16) & 0xFF;
1091 buf
[4] = (vcc
>> 24) & 0xFF;
1093 status
= bulk_immediate(port
, buf
, 5);
1096 if (status
!= IUU_OPERATION_OK
)
1097 dev_dbg(&port
->dev
, "%s - vcc error status = %2x\n", __func__
, status
);
1099 dev_dbg(&port
->dev
, "%s - vcc OK !\n", __func__
);
1108 static ssize_t
vcc_mode_show(struct device
*dev
,
1109 struct device_attribute
*attr
, char *buf
)
1111 struct usb_serial_port
*port
= to_usb_serial_port(dev
);
1112 struct iuu_private
*priv
= usb_get_serial_port_data(port
);
1114 return sprintf(buf
, "%d\n", priv
->vcc
);
1117 static ssize_t
vcc_mode_store(struct device
*dev
,
1118 struct device_attribute
*attr
, const char *buf
, size_t count
)
1120 struct usb_serial_port
*port
= to_usb_serial_port(dev
);
1121 struct iuu_private
*priv
= usb_get_serial_port_data(port
);
1124 if (kstrtoul(buf
, 10, &v
)) {
1125 dev_err(dev
, "%s - vcc_mode: %s is not a unsigned long\n",
1127 goto fail_store_vcc_mode
;
1130 dev_dbg(dev
, "%s: setting vcc_mode = %ld\n", __func__
, v
);
1132 if ((v
!= 3) && (v
!= 5)) {
1133 dev_err(dev
, "%s - vcc_mode %ld is invalid\n", __func__
, v
);
1135 iuu_vcc_set(port
, v
);
1138 fail_store_vcc_mode
:
1141 static DEVICE_ATTR_RW(vcc_mode
);
1143 static int iuu_create_sysfs_attrs(struct usb_serial_port
*port
)
1145 return device_create_file(&port
->dev
, &dev_attr_vcc_mode
);
1148 static int iuu_remove_sysfs_attrs(struct usb_serial_port
*port
)
1150 device_remove_file(&port
->dev
, &dev_attr_vcc_mode
);
1155 * End Sysfs Attributes
1158 static struct usb_serial_driver iuu_device
= {
1160 .name
= "iuu_phoenix",
1162 .id_table
= id_table
,
1166 .bulk_in_size
= 512,
1167 .bulk_out_size
= 512,
1170 .write
= iuu_uart_write
,
1171 .read_bulk_callback
= iuu_uart_read_callback
,
1172 .tiocmget
= iuu_tiocmget
,
1173 .tiocmset
= iuu_tiocmset
,
1174 .set_termios
= iuu_set_termios
,
1175 .init_termios
= iuu_init_termios
,
1176 .port_probe
= iuu_port_probe
,
1177 .port_remove
= iuu_port_remove
,
1180 static struct usb_serial_driver
* const serial_drivers
[] = {
1184 module_usb_serial_driver(serial_drivers
, id_table
);
1186 MODULE_AUTHOR("Alain Degreffe eczema@ecze.com");
1188 MODULE_DESCRIPTION(DRIVER_DESC
);
1189 MODULE_LICENSE("GPL");
1191 module_param(xmas
, bool, 0644);
1192 MODULE_PARM_DESC(xmas
, "Xmas colors enabled or not");
1194 module_param(boost
, int, 0644);
1195 MODULE_PARM_DESC(boost
, "Card overclock boost (in percent 100-500)");
1197 module_param(clockmode
, int, 0644);
1198 MODULE_PARM_DESC(clockmode
, "Card clock mode (1=3.579 MHz, 2=3.680 MHz, "
1201 module_param(cdmode
, int, 0644);
1202 MODULE_PARM_DESC(cdmode
, "Card detect mode (0=none, 1=CD, 2=!CD, 3=DSR, "
1203 "4=!DSR, 5=CTS, 6=!CTS, 7=RING, 8=!RING)");
1205 module_param(vcc_default
, int, 0644);
1206 MODULE_PARM_DESC(vcc_default
, "Set default VCC (either 3 for 3.3V or 5 "
1207 "for 5V). Default to 5.");