2 /* nozomi.c -- HSDPA driver Broadband Wireless Data Card - Globe Trotter
4 * Written by: Ulf Jakobsson,
8 * Maintained by: Paul Hardwick, p.hardwick@option.com
10 * Source has been ported from an implementation made by Filip Aben, f.aben@option.com
12 * --------------------------------------------------------------------------
14 Copyright (c) 2005 Option Wireless Sweden AB
17 This program is free software; you can redistribute it and/or modify
18 it under the terms of the GNU General Public License as published by
19 the Free Software Foundation; either version 2 of the License, or
20 (at your option) any later version.
22 This program is distributed in the hope that it will be useful,
23 but WITHOUT ANY WARRANTY; without even the implied warranty of
24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 GNU General Public License for more details.
27 You should have received a copy of the GNU General Public License
28 along with this program; if not, write to the Free Software
29 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
31 * --------------------------------------------------------------------------
36 * See CHANGELOG in this package
41 * See TODO file in this package
46 #include <linux/version.h>
48 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,14)
52 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0)
54 #elif LINUX_VERSION_CODE > KERNEL_VERSION(2,4,0)
58 #include <linux/module.h>
59 #include <linux/pci.h>
60 #include <linux/ioport.h>
61 #include <linux/tty.h>
62 #include <linux/tty_driver.h>
63 #include <linux/tty_flip.h>
64 #include <linux/serial.h>
65 #include <linux/interrupt.h>
66 #include <linux/kmod.h>
67 #include <linux/proc_fs.h>
68 #include <linux/spinlock.h>
69 #include <asm/uaccess.h>
72 #define VERSION_STRING DRIVER_DESC " (build date: " __DATE__ " " __TIME__ ")"
75 //#include <linux/kfifo.h>
78 #include <linux/workqueue.h>
88 typedef void irqreturn_t
;
93 #error "This driver needs PCI support to be available"
96 /* Macros definitions */
98 /* Enable this to have a lot of debug printouts */
99 /* #define NOZOMI_DEBUG */
101 /* Default debug printout level */
102 #define NOZOMI_DEBUG_LEVEL 0x1
104 #define P_BUF_SIZE 128
105 #define NFO( _err_flag_, args...) \
107 char t_m_p_[P_BUF_SIZE]; \
108 snprintf(t_m_p_, sizeof(t_m_p_), ##args); \
109 printk( _err_flag_ "[%d] %s(): %s\n", __LINE__, __FUNCTION__, t_m_p_); \
112 #define INFO(args...) NFO(KERN_INFO, ##args)
113 #define ERR(args...) NFO( KERN_ERR, ##args)
115 #define D1(args...) D_(0x01, ##args)
116 #define D2(args...) D_(0x02, ##args)
117 #define D3(args...) D_(0x04, ##args)
118 #define D4(args...) D_(0x08, ##args)
119 #define D5(args...) D_(0x10, ##args)
120 #define D6(args...) D_(0x20, ##args)
121 #define D7(args...) D_(0x40, ##args)
122 #define D8(args...) D_(0x80, ##args)
126 #define D_(lvl, args...) D(lvl, ##args)
127 /* Do we need this settable at runtime? */
128 static int nzdebug
= NOZOMI_DEBUG_LEVEL
;
130 #define D(lvl, args...) do{if(lvl & nzdebug) NFO(KERN_DEBUG, ##args );}while(0)
131 #define D_(lvl, args...) D(lvl, ##args)
133 /* These printouts are always printed */
136 static const int nzdebug
= 0;
137 #define D_(lvl, args...)
140 /* TODO: rewrite to optimize macros... */
141 #define SET_FCR(value__) \
143 writew((value__), (void*) (dc->REG_FCR )); \
146 #define SET_IER(value__, mask__) \
148 dc->ier_last_written = (dc->ier_last_written & ~mask__) | (value__ & mask__ );\
149 writew( dc->ier_last_written, (void*) (dc->REG_IER));\
152 #define GET_IER(read_val__) \
154 (read_val__) = readw((void*) (dc->REG_IER));\
157 #define GET_IIR(read_val__) \
159 (read_val__) = readw((void*) (dc->REG_IIR));\
162 #define GET_MEM(value__, addr__, length__) \
164 read_mem32( (u32*) (value__), (u32) (addr__), (length__));\
167 #define GET_MEM_BUF(value__, addr__, length__) \
169 read_mem32_buf( (u32*) (value__), (u32) (addr__), (length__));\
172 #define SET_MEM(addr__, value__, length__) \
174 write_mem32( (addr__), (u32*) (value__), (length__));\
177 #define SET_MEM_BUF(addr__, value__, length__) \
179 write_mem32_buf( (addr__), (u32*) (value__), (length__));\
183 #define TMP_BUF_MAX 256
185 #define DUMP(buf__,len__) \
187 char tbuf[TMP_BUF_MAX]={0};\
189 snprintf(tbuf, len__ > TMP_BUF_MAX ? TMP_BUF_MAX : len__, "%s",buf__);\
190 if(tbuf[len__-2] == '\r') {\
191 tbuf[len__-2] = 'r';\
193 D1( "SENDING: '%s' (%d+n)", tbuf, len__);\
195 D1( "SENDING: '%s' (%d)", tbuf, len__);\
199 #define RELEVANT_IFLAG(iflag) ((iflag) & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
203 #define NOZOMI_NAME "nozomi"
204 #define NOZOMI_NAME_TTY "nozomi_tty"
205 #define DRIVER_DESC "Nozomi driver"
207 #define NTTY_TTY_MAJOR 241
208 #define NTTY_TTY_MINORS MAX_PORT
209 #define NTTY_FIFO_BUFFER_SIZE 8192
211 /* Must be power of 2 */
212 #define FIFO_BUFFER_SIZE_UL 8192
214 /* Size of tmp send buffer to card */
215 #define SEND_BUF_MAX 1024
216 #define RECEIVE_BUF_MAX 4
218 /* Our fake UART values */
221 #define MCR_LOOP 0x04
227 /* Define all types of vendors and devices to support */
228 #define VENDOR1 0x1931 /* Vendor Option */
229 #define DEVICE1 0x000c /* HSDPA card */
231 #define R_IIR 0x0000 /* Interrupt Identity Register */
232 #define R_FCR 0x0000 /* Flow Control Register */
233 #define R_IER 0x0004 /* Interrupt Enable Register */
235 #define CONFIG_MAGIC 0xEFEFFEFE
236 #define TOGGLE_VALID 0x0000
238 /* Definition of interrupt tokens */
239 #define MDM_DL1 0x0001
240 #define MDM_UL1 0x0002
241 #define MDM_DL2 0x0004
242 #define MDM_UL2 0x0008
243 #define DIAG_DL1 0x0010
244 #define DIAG_DL2 0x0020
245 #define DIAG_UL 0x0040
246 #define APP1_DL 0x0080
247 #define APP1_UL 0x0100
248 #define APP2_DL 0x0200
249 #define APP2_UL 0x0400
250 #define CTRL_DL 0x0800
251 #define CTRL_UL 0x1000
254 #define MDM_DL (MDM_DL1 | MDM_DL2)
255 #define MDM_UL (MDM_UL1 | MDM_UL2)
256 #define DIAG_DL (DIAG_DL1 | DIAG_DL2)
258 /* modem signal definition */
259 #define CTRL_DSR 0x0001
260 #define CTRL_DCD 0x0002
261 #define CTRL_RI 0x0004
262 #define CTRL_CTS 0x0008
264 #define CTRL_DTR 0x0001
265 #define CTRL_RTS 0x0002
268 #define NOZOMI_MAX_PORTS 5
270 /* Type definitions */
272 /* There are two types of nozomi cards, one with 2048 memory and with 8192 memory */
274 F32_2
= 2048, /* Has 512 bytes downlink and uplink * 2 -> 2048 */
275 F32_8
= 9192, /* Has 3072 bytes downlink and 1024 bytes uplink * 2 -> 8192 */
278 /* Two different toggle channels exist */
284 /* Port definition for the card regarding flow control */
294 /* Ports that the nozomi has */
308 unsigned enabled
: 5; /* Toggle fields are valid if enabled is 0, else A-channels
309 must always be used. */
310 unsigned diag_dl
: 1;
313 } __attribute__ ((packed
)) toggles_t
;
315 /* Configuration table to read at startup of card */
316 /* Is for now only needed during initialization phase */
319 u16 product_information
;
324 u16 dl_mdm_len1
; /* If this is 64, it can hold 60 bytes + 4 that is length field */
328 u16 dl_mdm_len2
; /* If this is 64, it can hold 60 bytes + 4 that is length field */
342 } __attribute__((packed
)) config_table_t
;
344 /* This stores all control downlink flags */
347 unsigned reserved
: 4;
352 } __attribute__ ((packed
)) ctrl_dl_t
;
354 /* This stores all control uplink flags */
357 unsigned reserved
: 6;
360 } __attribute__ ((packed
)) ctrl_ul_t
;
365 /* This represents the toggle information */
369 unsigned diag_dl
: 1;
370 unsigned enabled
: 5; /* Toggle fields are valid if enabled is 0, else A-channels
371 must always be used. */
372 } __attribute__ ((packed
)) toggles_t
;
374 /* Configuration table to read at startup of card */
378 u16 product_information
;
382 u16 dl_mdm_len1
; /* If this is 64, it can hold 60 bytes + 4 that is length field */
397 } __attribute__((packed
)) config_table_t
;
399 /* This stores all control downlink flags */
405 unsigned reserverd
: 4;
407 } __attribute__ ((packed
)) ctrl_dl_t
;
409 /* This stores all control uplink flags */
413 unsigned reserved
: 6;
415 } __attribute__ ((packed
)) ctrl_ul_t
;
418 /* This holds all information that is needed regarding a port */
420 u8 update_flow_control
;
423 struct kfifo
*fifo_ul
;
431 struct tty_struct
*tty
;
433 struct semaphore tty_sem
;
434 wait_queue_head_t tty_wait
;
435 struct async_icount tty_icount
;
437 u32 rx_data
, tx_data
;
441 /* Private data one for each card in the system */
445 /* Register addresses */
449 volatile u16 ier_last_written
;
450 card_type_t card_type
;
451 config_table_t config_table
; /* Configuration table */
452 struct pci_dev
*pdev
;
453 port_t port
[NOZOMI_MAX_PORTS
];
455 struct tty_driver tty_driver
;
458 struct tty_struct
*tty_table
[NTTY_TTY_MINORS
];
459 struct tq_struct tty_flip_queue
;
463 struct workqueue_struct
*tty_flip_wq
;
464 struct work_struct tty_flip_wq_struct
;
467 struct termios
*tty_termios
[NTTY_TTY_MINORS
];
468 struct termios
*tty_termios_locked
[NTTY_TTY_MINORS
];
469 spinlock_t spin_mutex
;
472 struct proc_dir_entry
*proc_entry
;
475 /* This is a data packet that is read or written to/from card */
477 u32 size
; /* size is the length of the data buffer */
479 } __attribute__ ((packed
)) buf_t
;
481 /* Function declarations */
482 static int ntty_tty_init(void);
484 static void tty_flip_queue_function(void *nouse
);
486 /* Global variables */
487 static struct pci_device_id nozomi_pci_tbl
[] __devinitdata
= {
488 {VENDOR1
, DEVICE1
}, // Vodafone mobile connect HSDPA/UMTS/GPRS card
489 {0x11ab, DEVICE1
}, // swisscom 5 in 1
493 /* Used to store interrupt variables */
495 volatile u16 read_iir
; /* Holds current interrupt tokens */
498 MODULE_DEVICE_TABLE(pci
, nozomi_pci_tbl
);
500 /* Representing the pci device of interest */
501 static int cards_found
;
507 /* -Rewrite cleaner */
508 static void read_mem32(u32
*buf
, u32 mem_addr_start
, u32 size_bytes
)
511 u32
*ptr
= (u32
*) mem_addr_start
;
515 if ( size_bytes
== 2 ) {
517 *buf16
= readw( ptr
);
521 while ( i
< size_bytes
) {
522 if ( size_bytes
- i
== 2) {
523 /* Handle 2 bytes in the end */
525 *(buf16
) = readw( ptr
);
529 *(buf
) = readl( ptr
);
537 /* - Rewrite cleaner */
538 /* - merge with read_mem32() */
539 static void read_mem32_buf(u32
*buf
, u32 mem_addr_start
, u32 size_bytes
)
543 u32
*ptr
= (u32
*) mem_addr_start
;
547 if ( size_bytes
== 2 ) {
549 *buf16
= __le16_to_cpu( readw( ptr
));
553 while ( i
< size_bytes
) {
554 if ( size_bytes
- i
== 2) {
555 /* Handle 2 bytes in the end */
557 *(buf16
) = __le16_to_cpu( readw( ptr
));
561 *(buf
) = __le32_to_cpu( readl( ptr
));
567 read_mem32(buf
, mem_addr_start
, size_bytes
);
573 /* -Rewrite cleaner */
574 static u32
write_mem32(u32 mem_addr_start
, u32
*buf
, u32 size_bytes
)
577 u32
*ptr
= (u32
*) mem_addr_start
;
581 if (size_bytes
== 2) {
583 writew( *buf16
, ptr
);
587 while (i
< size_bytes
) {
588 if ( size_bytes
- i
== 2) {
591 writew( *buf16
, ptr
);
604 /* - Merge with write_mem32() */
605 static u32
write_mem32_buf(u32 mem_addr_start
, u32
*buf
, u32 size_bytes
)
609 u32
*ptr
= (u32
*) mem_addr_start
;
613 if (size_bytes
== 2) {
615 writew( __le16_to_cpu(*buf16
), ptr
);
619 while (i
< size_bytes
) {
620 if ( size_bytes
- i
== 2) {
623 writew( __le16_to_cpu(*buf16
), ptr
);
627 writel( __cpu_to_le32( *buf
), ptr
);
634 return write_mem32(mem_addr_start
, buf
, size_bytes
);
638 /* Setup pointers to different channels and also setup buffer sizes. */
639 static void setup_memory(void)
641 /* The length reported is including the length field of 4 bytes, hence subtract with 4. */
642 u32 offset
= dc
->base_addr
+ dc
->config_table
.dl_start
;
645 /* Modem port dl configuration */
646 dc
->port
[PORT_MDM
].dl_addr
[CH_A
] = offset
;
647 dc
->port
[PORT_MDM
].dl_addr
[CH_B
] = (offset
+= dc
->config_table
.dl_mdm_len1
);
648 dc
->port
[PORT_MDM
].dl_size
[CH_A
] = dc
->config_table
.dl_mdm_len1
- buff_offset
;
649 dc
->port
[PORT_MDM
].dl_size
[CH_B
] = dc
->config_table
.dl_mdm_len2
- buff_offset
;
651 /* Diag port dl configuration */
652 dc
->port
[PORT_DIAG
].dl_addr
[CH_A
] = (offset
+= dc
->config_table
.dl_mdm_len2
);
653 dc
->port
[PORT_DIAG
].dl_size
[CH_A
] = dc
->config_table
.dl_diag_len1
- buff_offset
;
654 dc
->port
[PORT_DIAG
].dl_addr
[CH_B
] = (offset
+= dc
->config_table
.dl_diag_len1
);
655 dc
->port
[PORT_DIAG
].dl_size
[CH_B
] = dc
->config_table
.dl_diag_len2
- buff_offset
;
657 /* App1 port dl configuration */
658 dc
->port
[PORT_APP1
].dl_addr
[CH_A
] = (offset
+= dc
->config_table
.dl_diag_len2
);
659 dc
->port
[PORT_APP1
].dl_size
[CH_A
] = dc
->config_table
.dl_app1_len
- buff_offset
;
661 /* App2 port dl configuration */
662 dc
->port
[PORT_APP2
].dl_addr
[CH_A
] = (offset
+= dc
->config_table
.dl_app1_len
);
663 dc
->port
[PORT_APP2
].dl_size
[CH_A
] = dc
->config_table
.dl_app2_len
- buff_offset
;
665 /* Ctrl dl configuration */
666 dc
->port
[PORT_CTRL
].dl_addr
[CH_A
] = (offset
+= dc
->config_table
.dl_app2_len
);
667 dc
->port
[PORT_CTRL
].dl_size
[CH_A
] = dc
->config_table
.dl_ctrl_len
- buff_offset
;
670 /* Modem Port ul configuration */
671 dc
->port
[PORT_MDM
].ul_addr
[CH_A
] = (offset
= dc
->base_addr
+ dc
->config_table
.ul_start
);
672 dc
->port
[PORT_MDM
].ul_size
[CH_A
] = dc
->config_table
.ul_mdm_len1
- buff_offset
;
673 dc
->port
[PORT_MDM
].ul_addr
[CH_B
] = (offset
+= dc
->config_table
.ul_mdm_len1
);
674 dc
->port
[PORT_MDM
].ul_size
[CH_B
] = dc
->config_table
.ul_mdm_len2
- buff_offset
;
676 /* Diag port ul configuration */
677 dc
->port
[PORT_DIAG
].ul_addr
[CH_A
] = (offset
+= dc
->config_table
.ul_mdm_len2
);
678 dc
->port
[PORT_DIAG
].ul_size
[CH_A
] = dc
->config_table
.ul_diag_len
- buff_offset
;
680 /* App1 port ul configuration */
681 dc
->port
[PORT_APP1
].ul_addr
[CH_A
] = (offset
+= dc
->config_table
.ul_diag_len
);
682 dc
->port
[PORT_APP1
].ul_size
[CH_A
] = dc
->config_table
.ul_app1_len
- buff_offset
;
684 /* App2 port ul configuration */
685 dc
->port
[PORT_APP2
].ul_addr
[CH_A
] = (offset
+= dc
->config_table
.ul_app1_len
);
686 dc
->port
[PORT_APP2
].ul_size
[CH_A
] = dc
->config_table
.ul_app2_len
- buff_offset
;
688 /* Ctrl ul configuration */
689 dc
->port
[PORT_CTRL
].ul_addr
[CH_A
] = (offset
+= dc
->config_table
.ul_app2_len
);
690 dc
->port
[PORT_CTRL
].ul_size
[CH_A
] = dc
->config_table
.ul_ctrl_len
- buff_offset
;
691 offset
= dc
->config_table
.ul_start
;
694 /* Dump config table under initalization phase */
696 static void dump_table(void)
698 D3("signature: 0x%08X", dc
->config_table
.signature
);
699 D3("version: 0x%04X", dc
->config_table
.version
);
700 D3("product_information: 0x%04X", dc
->config_table
.product_information
);
701 D3("toggle enabled: %d", dc
->config_table
.toggle
.enabled
);
702 D3("toggle up_mdm: %d", dc
->config_table
.toggle
.mdm_ul
);
703 D3("toggle dl_mdm: %d", dc
->config_table
.toggle
.mdm_dl
);
704 D3("toggle dl_dbg: %d", dc
->config_table
.toggle
.diag_dl
);
706 D3("dl_start: 0x%04X", dc
->config_table
.dl_start
);
707 D3("dl_mdm_len0: 0x%04X, %d", dc
->config_table
.dl_mdm_len1
, dc
->config_table
.dl_mdm_len1
);
708 D3("dl_mdm_len1: 0x%04X, %d", dc
->config_table
.dl_mdm_len2
, dc
->config_table
.dl_mdm_len2
);
709 D3("dl_diag_len0: 0x%04X, %d", dc
->config_table
.dl_diag_len1
, dc
->config_table
.dl_diag_len1
);
710 D3("dl_diag_len1: 0x%04X, %d", dc
->config_table
.dl_diag_len2
, dc
->config_table
.dl_diag_len2
);
711 D3("dl_app1_len: 0x%04X, %d", dc
->config_table
.dl_app1_len
, dc
->config_table
.dl_app1_len
);
712 D3("dl_app2_len: 0x%04X, %d", dc
->config_table
.dl_app2_len
, dc
->config_table
.dl_app2_len
);
713 D3("dl_ctrl_len: 0x%04X, %d", dc
->config_table
.dl_ctrl_len
, dc
->config_table
.dl_ctrl_len
);
714 D3("ul_start: 0x%04X, %d", dc
->config_table
.ul_start
, dc
->config_table
.ul_start
);
715 D3("ul_mdm_len[0]: 0x%04X, %d", dc
->config_table
.ul_mdm_len1
, dc
->config_table
.ul_mdm_len1
);
716 D3("ul_mdm_len[1]: 0x%04X, %d", dc
->config_table
.ul_mdm_len2
, dc
->config_table
.ul_mdm_len2
);
717 D3("ul_diag_len: 0x%04X, %d", dc
->config_table
.ul_diag_len
, dc
->config_table
.ul_diag_len
);
718 D3("ul_app1_len: 0x%04X, %d", dc
->config_table
.ul_app1_len
, dc
->config_table
.ul_app1_len
);
719 D3("ul_app2_len: 0x%04X, %d", dc
->config_table
.ul_app2_len
, dc
->config_table
.ul_app2_len
);
720 D3("ul_ctrl_len: 0x%04X, %d", dc
->config_table
.ul_ctrl_len
, dc
->config_table
.ul_ctrl_len
);
724 /* Read configuration table from card under intalization phase */
725 /* Returns 1 if ok, else 0 */
726 static int nozomi_read_config_table(void)
728 GET_MEM( &dc
->config_table
, dc
->base_addr
+ 0, sizeof(config_table_t
));
729 /* D1( "0x%08X == 0x%08X ", dc->config_table.signature, CONFIG_MAGIC); */
730 if ( dc
->config_table
.signature
!= CONFIG_MAGIC
) {
731 ERR("ConfigTable Bad! 0x%08X != 0x%08X", dc
->config_table
.signature
, CONFIG_MAGIC
);
735 if ( (dc
->config_table
.version
== 0) || (dc
->config_table
.toggle
.enabled
== TOGGLE_VALID
) ) {
738 D1( "Second phase, configuring card");
740 dc
->port
[PORT_MDM
].toggle_ul
= dc
->config_table
.toggle
.mdm_ul
;
741 dc
->port
[PORT_MDM
].toggle_dl
= dc
->config_table
.toggle
.mdm_dl
;
742 dc
->port
[PORT_DIAG
].toggle_dl
= dc
->config_table
.toggle
.diag_dl
;
743 D1( "toggle ports: MDM UL:%d MDM DL:%d, DIAG DL:%d",
744 dc
->port
[PORT_MDM
].toggle_ul
,
745 dc
->port
[PORT_MDM
].toggle_dl
,
746 dc
->port
[PORT_DIAG
].toggle_dl
);
751 for (i
=PORT_MDM
; i
< MAX_PORT
;i
++) {
752 dc
->port
[i
].fifo_ul
= kfifo_alloc( FIFO_BUFFER_SIZE_UL
, GFP_ATOMIC
, NULL
);
753 memset( &dc
->port
[i
].ctrl_dl
, 0, sizeof (ctrl_dl_t
));
754 memset( &dc
->port
[i
].ctrl_ul
, 0, sizeof (ctrl_ul_t
));
757 /* Enable control channel */
758 SET_IER( CTRL_DL
, CTRL_DL
);
760 INFO("Initialization OK!");
764 if( (dc
->config_table
.version
> 0) && (dc
->config_table
.toggle
.enabled
!= TOGGLE_VALID
) ) {
766 D1( "First phase: pushing upload buffers, clearing download");
767 INFO("Version of card: %d", dc
->config_table
.version
);
769 /* Here we should disable all I/O over F32. */
772 /* We should send ALL channel pair tokens back along with reset token */
774 /* push upload modem buffers */
775 SET_MEM( dc
->port
[PORT_MDM
].ul_addr
[CH_A
], &offset
, 4);
776 SET_MEM( dc
->port
[PORT_MDM
].ul_addr
[CH_B
], &offset
, 4);
778 SET_FCR( MDM_UL
| DIAG_DL
| MDM_DL
);
780 D1( "First phase done");
786 /* Enable uplink interrupts */
787 static void enable_transmit_ul(port_type_t port
)
790 case PORT_MDM
: SET_IER( MDM_UL
, MDM_UL
); break;
791 case PORT_DIAG
: SET_IER( DIAG_UL
, DIAG_UL
); break;
792 case PORT_APP1
: SET_IER( APP1_UL
, APP1_UL
); break;
793 case PORT_APP2
: SET_IER( APP2_UL
, APP2_UL
); break;
794 case PORT_CTRL
: SET_IER( CTRL_UL
, CTRL_UL
); break;
796 ERR("Called with wrong port?");
801 /* Disable uplink interrupts */
802 static void disable_transmit_ul(port_type_t port
)
805 case PORT_MDM
: SET_IER( 0 ,MDM_UL
); break;
806 case PORT_DIAG
: SET_IER( 0, DIAG_UL
); break;
807 case PORT_APP1
: SET_IER( 0, APP1_UL
); break;
808 case PORT_APP2
: SET_IER( 0, APP2_UL
); break;
809 case PORT_CTRL
: SET_IER( 0, CTRL_UL
); break;
811 ERR("Called with wrong port?");
816 /* Enable downlink interrupts */
817 static void enable_transmit_dl(port_type_t port
)
820 case PORT_MDM
: SET_IER( MDM_DL
, MDM_DL
); break;
821 case PORT_DIAG
: SET_IER( DIAG_DL
, DIAG_DL
); break;
822 case PORT_APP1
: SET_IER( APP1_DL
, APP1_DL
); break;
823 case PORT_APP2
: SET_IER( APP2_DL
, APP2_DL
); break;
824 case PORT_CTRL
: SET_IER( CTRL_DL
, CTRL_DL
); break;
826 ERR("Called with wrong port?");
831 /* Disable downlink interrupts */
832 static void disable_transmit_dl(port_type_t port
)
835 case PORT_MDM
: SET_IER( 0 ,MDM_DL
); break;
836 case PORT_DIAG
: SET_IER( 0, DIAG_DL
); break;
837 case PORT_APP1
: SET_IER( 0, APP1_DL
); break;
838 case PORT_APP2
: SET_IER( 0, APP2_DL
); break;
839 case PORT_CTRL
: SET_IER( 0, CTRL_DL
); break;
841 ERR("Called with wrong port?");
846 /* Return 1 - send buffer to card and ack. */
847 /* Return 0 - don't ack, don't send buffer to card. */
848 int send_data(port_type_t index
)
851 port_t
*port
= &dc
->port
[index
];
852 u8 toggle
= port
->toggle_ul
;
853 u32 addr
= port
->ul_addr
[toggle
];
854 u32 ul_size
= port
->ul_size
[toggle
];
855 struct tty_struct
*tty
= port
->tty
;
857 if (index
>= NTTY_TTY_MINORS
) {
858 ERR("Called with wrong index?");
862 /* Get data from tty and place in buf for now */
863 size
= __kfifo_get( port
->fifo_ul
,dc
->send_buf
, ul_size
< SEND_BUF_MAX
? ul_size
: SEND_BUF_MAX
);
866 D4("No more data to send, disable link:");
870 port
->tx_data
+= size
;
872 /* DUMP(buf, size); */
874 /* Write length + data */
875 SET_MEM( addr
, &size
, 4 );
876 SET_MEM_BUF( addr
+ 4, dc
->send_buf
, size
);
879 if ((tty
->flags
& (1 << TTY_DO_WRITE_WAKEUP
)) && tty
->ldisc
.write_wakeup
) {
880 tty
->ldisc
.write_wakeup(tty
);
882 wake_up_interruptible(&tty
->write_wait
);
888 /* If all data has been read, return 1, else 0 */
889 static int receive_data(port_type_t index
)
891 u8 buf
[RECEIVE_BUF_MAX
];
894 port_t
*port
=&dc
->port
[index
];
895 u8 toggle
=port
->toggle_dl
;
896 u32 addr
=port
->dl_addr
[toggle
];
898 struct tty_struct
*tty
=port
->tty
;
901 D1("tty not open for port: %d?", index
);
905 if (test_bit(TTY_DONT_FLIP
, &tty
->flags
)) {
906 D6("TTY_DONT_FLIP set!! %d", index
);
907 /* Here we disable interrupt for that port and schedule */
908 /* task. Task wakes up a little bit later and enables interrupt.. */
909 port
->tty_dont_flip
= 1;
910 disable_transmit_dl(index
);
912 schedule_task(&dc
->tty_flip_queue
);
915 if (!queue_work(dc
->tty_flip_wq
, &dc
->tty_flip_wq_struct
)) {
916 ERR("Call to queue_work() failed.");
922 GET_MEM( &size
, addr
, 4 );
923 /* D1( "%d bytes port: %d", size, index); */
925 if ( test_bit( TTY_THROTTLED
, & tty
->flags
) ) {
926 D1("No room in tty, don't read data, don't ack interrupt, disable interrupt");
927 /* disable interrupt in downlink... */
928 disable_transmit_dl(index
);
938 GET_MEM_BUF(buf
, addr
+ offset
, 4);
940 while (i
< 4 && size
> 0) {
942 if (tty_buffer_request_room(tty
, 1) < 1)
944 if (tty
->flip
.count
>= TTY_FLIPBUF_SIZE
)
947 tty_flip_buffer_push(tty
);
949 tty_insert_flip_char(tty
, buf
[i
], TTY_NORMAL
);
958 tty_flip_buffer_push(tty
);
962 /* Debug for interrupts */
964 static char* interrupt2str( u16 interrupt
)
966 static char buf
[TMP_BUF_MAX
];
969 interrupt
& MDM_DL1
? p
+= snprintf(p
, TMP_BUF_MAX
, "MDM_DL1 "):0;
970 interrupt
& MDM_DL2
? p
+= snprintf(p
, TMP_BUF_MAX
, "MDM_DL2 "):0;
972 interrupt
& MDM_UL1
? p
+= snprintf(p
, TMP_BUF_MAX
, "MDM_UL1 "):0;
973 interrupt
& MDM_UL2
? p
+= snprintf(p
, TMP_BUF_MAX
, "MDM_UL2 "):0;
975 interrupt
& DIAG_DL1
? p
+= snprintf(p
, TMP_BUF_MAX
, "DIAG_DL1 "):0;
976 interrupt
& DIAG_DL2
? p
+= snprintf(p
, TMP_BUF_MAX
, "DIAG_DL2 "):0;
978 interrupt
& DIAG_UL
? p
+= snprintf(p
, TMP_BUF_MAX
, "DIAG_UL "):0;
980 interrupt
& APP1_DL
? p
+= snprintf(p
, TMP_BUF_MAX
, "APP1_DL "):0;
981 interrupt
& APP2_DL
? p
+= snprintf(p
, TMP_BUF_MAX
, "APP2_DL "):0;
983 interrupt
& APP1_UL
? p
+= snprintf(p
, TMP_BUF_MAX
, "APP1_UL "):0;
984 interrupt
& APP2_UL
? p
+= snprintf(p
, TMP_BUF_MAX
, "APP2_UL "):0;
986 interrupt
& CTRL_DL
? p
+= snprintf(p
, TMP_BUF_MAX
, "CTRL_DL "):0;
987 interrupt
& CTRL_UL
? p
+= snprintf(p
, TMP_BUF_MAX
, "CTRL_UL "):0;
989 interrupt
& RESET
? p
+= snprintf(p
, TMP_BUF_MAX
, "RESET "):0;
995 /* Receive flow control */
996 /* Return 1 - If ok, else 0 */
997 static int receive_flow_control(irq_t
*m
)
999 port_type_t port
= PORT_MDM
;
1004 GET_MEM( &ctrl_dl
, dc
->port
[PORT_CTRL
].dl_addr
[CH_A
], 2);
1006 switch( ctrl_dl
.port
) {
1008 D1( "The Base Band sends this value as a response to a request for IMSI detach sent over the control channel uplink (see section 7.6.1).");
1010 case CTRL_MDM
: port
= PORT_MDM
; enable_ier
= MDM_DL
; break;
1011 case CTRL_DIAG
: port
= PORT_DIAG
; enable_ier
= DIAG_DL
; break;
1012 case CTRL_APP1
: port
= PORT_APP1
; enable_ier
= APP1_DL
; break;
1013 case CTRL_APP2
: port
= PORT_APP2
; enable_ier
= APP2_DL
; break;
1015 ERR("ERROR: flow control received for non-existing port");
1019 D1( "0x%04X->0x%04X", *((u16
*) &dc
->port
[port
].ctrl_dl
), *((u16
*)&ctrl_dl
));
1021 old_ctrl
= dc
->port
[port
].ctrl_dl
;
1022 dc
->port
[port
].ctrl_dl
= ctrl_dl
;
1024 if ( old_ctrl
.CTS
== 1 && ctrl_dl
.CTS
== 0 ) {
1025 D1( "Disable interrupt (0x%04X) on port: %d", enable_ier
, port
);
1026 disable_transmit_ul(port
);
1027 } else if ( old_ctrl
.CTS
== 0 && ctrl_dl
.CTS
== 1 ) {
1028 if ( __kfifo_len(dc
->port
[port
].fifo_ul
) ) {
1029 D1( "Enable interrupt (0x%04X) on port: %d", enable_ier
, port
);
1030 D1( "Data in buffer [%d], enable transmit! ", __kfifo_len(dc
->port
[port
].fifo_ul
) );
1031 enable_transmit_ul(port
);
1033 D1( "No data in buffer...");
1037 if(*(u16
*)&old_ctrl
== *(u16
*)&ctrl_dl
) {
1038 D1( " No change in mctrl");
1041 /* Update statistics */
1042 if(old_ctrl
.CTS
!= ctrl_dl
.CTS
) {
1043 dc
->port
[port
].tty_icount
.cts
++;
1045 if(old_ctrl
.DSR
!= ctrl_dl
.DSR
) {
1046 dc
->port
[port
].tty_icount
.dsr
++;
1048 if(old_ctrl
.RI
!= ctrl_dl
.RI
) {
1049 dc
->port
[port
].tty_icount
.rng
++;
1051 if(old_ctrl
.DCD
!= ctrl_dl
.DCD
) {
1052 dc
->port
[port
].tty_icount
.dcd
++;
1054 D1("port: %d DCD(%d), CTS(%d), RI(%d), DSR(%d)", port
,
1055 dc
->port
[port
].tty_icount
.dcd
, dc
->port
[port
].tty_icount
.cts
,
1056 dc
->port
[port
].tty_icount
.rng
, dc
->port
[port
].tty_icount
.dsr
);
1062 /* - return ctrl_port_t */
1063 static u8
port2ctrl(port_type_t port
)
1075 ERR("ERROR: send flow control received for non-existing port");
1081 /* Send flow control, can only update one channel at a time */
1082 /* Return 0 - If we have updated all flow control */
1083 /* Return 1 - If we need to update more flow control, ack current enable more */
1084 static int send_flow_control(void)
1086 u32 i
, more_flow_control_to_be_updated
= 0;
1089 for( i
=PORT_MDM
; i
<MAX_PORT
; i
++ ) {
1090 if( dc
->port
[i
].update_flow_control
) {
1091 if ( more_flow_control_to_be_updated
) {
1092 /* We have more flow control to be updated */
1095 dc
->port
[i
].ctrl_ul
.port
= port2ctrl(i
);
1096 ctrl
= (u16
*) &dc
->port
[i
].ctrl_ul
;
1097 /* D1( "sending flow control 0x%04X for port %d, %d", (u16) *ctrl, i, dc->port[i].ctrl_ul.port ); */
1098 SET_MEM( dc
->port
[PORT_CTRL
].ul_addr
[0], (u32
*) ctrl
, 2 );
1099 dc
->port
[i
].update_flow_control
= 0;
1100 more_flow_control_to_be_updated
= 1;
1106 /* Handle donlink data, ports that are handled are modem and diagnostics */
1108 /* Return 0 - toggle fields are out of sync */
1109 static int handle_data_dl(irq_t
*m
, port_type_t port
, u8
*toggle
, u16 mask1
, u16 mask2
)
1111 if ( *toggle
== 0 && m
->read_iir
& mask1
) {
1112 if (receive_data(port
)) {
1114 *toggle
= !(*toggle
);
1117 if ( m
->read_iir
& mask2
) {
1118 if (receive_data(port
)) {
1120 *toggle
= !(*toggle
);
1123 } else if ( *toggle
== 1 && m
->read_iir
& mask2
) {
1124 if (receive_data(port
)) {
1126 *toggle
= !(*toggle
);
1129 if ( m
->read_iir
& mask1
) {
1130 if (receive_data(port
)) {
1132 *toggle
= !(*toggle
);
1136 ERR("port out of sync!, toggle:%d", *toggle
);
1142 /* Handle uplink data, this is currently for the modem port */
1144 /* Return 0 - toggle field are out of sync */
1145 static int handle_data_ul(irq_t
*m
, port_type_t port
)
1147 u8
*toggle
= &(dc
->port
[port
].toggle_ul
);
1149 if ( *toggle
==0 && m
->read_iir
& MDM_UL1
) {
1150 SET_IER( 0, MDM_UL
);
1151 if (send_data(port
)) {
1153 SET_IER( MDM_UL
, MDM_UL
);
1157 if ( m
->read_iir
& MDM_UL2
) {
1158 SET_IER( 0, MDM_UL
);
1159 if (send_data(port
)) {
1161 SET_IER( MDM_UL
, MDM_UL
);
1166 } else if ( *toggle
==1 && m
->read_iir
& MDM_UL2
) {
1167 SET_IER( 0, MDM_UL
);
1168 if (send_data(port
)) {
1170 SET_IER( MDM_UL
, MDM_UL
);
1174 if ( m
->read_iir
& MDM_UL1
) {
1175 SET_IER( 0, MDM_UL
);
1176 if (send_data(port
)) {
1178 SET_IER( MDM_UL
, MDM_UL
);
1183 SET_FCR( m
->read_iir
& MDM_UL
);
1184 ERR("port out of sync!");
1190 static irqreturn_t
interrupt_handler(int irq
, void *dev_id
, struct pt_regs
*regs
)
1194 if (dc
&& dc
->pdev
!= dev_id
) {
1198 GET_IIR( m
->read_iir
);
1200 #if 1 // add by Victor Yu. 05-24-2006
1201 spin_lock(&dc
->spin_mutex
);
1203 /* Just handle interrupt enabled in IER (by masking with dc->ier_last_written) */
1204 m
->read_iir
&= dc
->ier_last_written
;
1206 if (m
->read_iir
== 0) {
1207 #if 1 // add by Victor Yu. 05-24-2006
1208 spin_unlock(&dc
->spin_mutex
);
1213 D4( "%s irq:0x%04X, prev:0x%04X", interrupt2str(m
->read_iir
), m
->read_iir
, dc
->ier_last_written
);
1215 if ( m
->read_iir
& RESET
) {
1216 if( !nozomi_read_config_table() ) {
1217 SET_IER( 0, 0xFFFF);
1218 ERR("ERR: Could not read status from card, we should disable interface");
1222 goto exit_handler
; /* No more useful info if this was the reset interrupt. */
1225 if ( m
->read_iir
& CTRL_UL
) {
1227 SET_IER( 0, CTRL_UL
);
1228 if ( send_flow_control() ) {
1230 SET_IER( CTRL_UL
, CTRL_UL
);
1233 if ( m
->read_iir
& CTRL_DL
) {
1234 receive_flow_control(m
);
1237 if ( m
->read_iir
& MDM_DL
) {
1238 if ( !(handle_data_dl(m
, PORT_MDM
, &(dc
->port
[PORT_MDM
].toggle_dl
), MDM_DL1
, MDM_DL2
)) ) {
1239 ERR("MDM_DL out of sync!");
1243 if ( m
->read_iir
& MDM_UL
) {
1244 if ( !handle_data_ul(m
, PORT_MDM
) ) {
1245 ERR("MDM_UL out of sync!");
1249 if ( m
->read_iir
& DIAG_DL
) {
1250 if ( !(handle_data_dl(m
, PORT_DIAG
, &(dc
->port
[PORT_DIAG
].toggle_dl
), DIAG_DL1
, DIAG_DL2
)) ) {
1251 ERR("DIAG_DL out of sync!");
1255 if ( m
->read_iir
& DIAG_UL
) {
1256 SET_IER( 0, DIAG_UL
);
1257 if( send_data(PORT_DIAG
) ) {
1259 SET_IER( DIAG_UL
, DIAG_UL
);
1262 if ( m
->read_iir
& APP1_DL
) {
1263 if (receive_data(PORT_APP1
) ) {
1267 if ( m
->read_iir
& APP1_UL
) {
1268 SET_IER( 0, APP1_UL
);
1269 if(send_data(PORT_APP1
)) {
1271 SET_IER( APP1_UL
, APP1_UL
);
1274 if ( m
->read_iir
& APP2_DL
) {
1275 if (receive_data(PORT_APP2
)) {
1279 if ( m
->read_iir
& APP2_UL
) {
1280 SET_IER( 0, APP2_UL
);
1281 if(send_data(PORT_APP2
)) {
1283 SET_IER( APP2_UL
, APP2_UL
);
1288 spin_unlock(&dc
->spin_mutex
);
1292 /* Request a shared IRQ from system */
1293 static int nozomi_setup_interrupt(struct pci_dev
*pdev
)
1297 if ( (rval
= request_irq( pdev
->irq
, &interrupt_handler
, SA_SHIRQ
, NOZOMI_NAME
, pdev
)) ) {
1298 ERR("Cannot open because IRQ %d is already in use.", pdev
->irq
);
1304 static void nozomi_get_card_type(void)
1308 for( i
=0; i
<6; i
++ ) {
1309 size
+= pci_resource_len(dc
->pdev
, i
);
1312 /* Assume card type F32_8 if no match */
1313 dc
->card_type
= size
== 2048 ? F32_2
: F32_8
;
1315 INFO("Card type is: %d", dc
->card_type
);
1318 void nozomi_setup_private_data(void)
1320 u32 offset
= dc
->base_addr
+ dc
->card_type
/2;
1323 dc
->REG_FCR
= offset
+ R_FCR
;
1324 dc
->REG_IIR
= offset
+ R_IIR
;
1325 dc
->REG_IER
= offset
+ R_IER
;
1326 dc
->ier_last_written
= 0;
1329 dc
->port
[PORT_MDM
].token_dl
= MDM_DL
;
1330 dc
->port
[PORT_DIAG
].token_dl
= DIAG_DL
;
1331 dc
->port
[PORT_APP1
].token_dl
= APP1_DL
;
1332 dc
->port
[PORT_APP2
].token_dl
= APP2_DL
;
1334 for( i
=PORT_MDM
; i
<MAX_PORT
; i
++ ) {
1335 dc
->port
[i
].rx_data
= dc
->port
[i
].tx_data
= 0;
1336 dc
->port
[i
].tty_dont_flip
= 0;
1341 static void tty_flip_queue_function(void *nouse
)
1344 #if 1 // add by Victor Yu. 05-24-2006
1345 unsigned long flags
;
1348 /* Enable interrupt for that port */
1349 for(i
=0;i
<MAX_PORT
;i
++) {
1350 if (dc
->port
[i
].tty_dont_flip
) {
1351 D6("Enable for port: %d", i
);
1352 dc
->port
[i
].tty_dont_flip
= 0;
1353 #if 1 // add by Victor Yu. 05-24-2006
1354 spin_lock_irqsave(&dc
->spin_mutex
, flags
);
1356 enable_transmit_dl(dc
->port
[i
].tty_index
);
1357 #if 1 // add by Victor Yu. 05-24-2006
1358 spin_unlock_irqrestore(&dc
->spin_mutex
, flags
);
1364 static int read_proc_card_type(char *buf
, char **start
, off_t offset
, int len
)
1367 len
+= sprintf(buf
+len
,"%d\n", dc
->card_type
);
1371 static int read_proc_open_ttys(char *buf
, char **start
, off_t offset
, int len
)
1374 len
+= sprintf(buf
+len
,"%d\n", dc
->open_ttys
);
1378 static int read_proc_version(char *buf
, char **start
, off_t offset
, int len
)
1381 len
+= sprintf(buf
+len
, "%s\n", VERSION_STRING
);
1385 static int read_proc_rtx(char *buf
, char **start
, off_t offset
, int len
)
1390 for(i
=PORT_MDM
;i
<MAX_PORT
;i
++) {
1391 len
+= sprintf(buf
+len
,"noz%d rx: %d, tx: %d\n", i
, dc
->port
[i
].rx_data
, dc
->port
[i
].tx_data
);
1396 static void make_proc_dirs(void)
1398 dc
->proc_entry
= proc_mkdir("nozomi", &proc_root
);
1399 /* Register the read_proc */
1400 if ( !create_proc_info_entry("card_type",0,dc
->proc_entry
,read_proc_card_type
) ) {
1401 ERR("ERROR: failed to register read_procmem");
1403 if ( !create_proc_info_entry("open_ttys",0,dc
->proc_entry
,read_proc_open_ttys
) ) {
1404 ERR("ERROR: failed to register read_procmem");
1406 if ( !create_proc_info_entry("rtx",0,dc
->proc_entry
,read_proc_rtx
) ) {
1407 ERR("ERROR: failed to register read_procmem");
1409 if ( !create_proc_info_entry("version",0,dc
->proc_entry
,read_proc_version
) ) {
1410 ERR("ERROR: failed to register read_procmem");
1414 static void remove_proc_dirs(void)
1416 remove_proc_entry("card_type", dc
->proc_entry
);
1417 remove_proc_entry("open_ttys", dc
->proc_entry
);
1418 remove_proc_entry("rtx", dc
->proc_entry
);
1419 remove_proc_entry("version", dc
->proc_entry
);
1420 remove_proc_entry("nozomi", &proc_root
);
1423 /* Allocate memory for one device */
1424 static int __devinit
nozomi_card_init(struct pci_dev
*pdev
, const struct pci_device_id
*ent
)
1429 INFO("Init, cards_found: %d", cards_found
);
1431 if (!(dc
= kmalloc(sizeof(dc_t
), GFP_KERNEL
))) {
1432 D1( "Could not allocate memory");
1436 memset(dc
, 0, sizeof( dc_t
));
1438 if (cards_found
> 1) {
1439 ERR("This driver only supports 1 device");
1445 /* Find out what card type it is */
1446 nozomi_get_card_type();
1448 if (pci_enable_device(dc
->pdev
)) {
1449 ERR("Not possible to enable PCI Device");
1453 if ( (dc
->base_addr
= pci_resource_start(dc
->pdev
, 0)) == 0x0000) {
1454 ERR("No I/O-Address for card detected");
1456 goto err_disable_device
;
1459 if (!(dc
->base_addr
= (u32
) ioremap(dc
->base_addr
, dc
->card_type
))) {
1460 ERR("No I/O-Address for card detected");
1462 goto err_disable_device
;
1467 nozomi_setup_private_data();
1469 if (pci_request_regions(dc
->pdev
, NOZOMI_NAME
)) {
1470 ERR("I/O address 0x%04x already in use",
1471 (int) /* nozomi_private.io_addr */ 0);
1473 goto err_disable_regions
;
1476 if ( !(dc
->send_buf
= kmalloc(SEND_BUF_MAX
, GFP_KERNEL
))) {
1477 ERR("Could not allocate send buffer?");
1478 goto err_disable_regions
;
1481 /* Disable all interrupts */
1482 SET_IER( 0 , 0xFFFF );
1484 /* Setup interrupt handler */
1485 if (nozomi_setup_interrupt(dc
->pdev
)) {
1487 goto err_disable_regions
;
1490 D1( "base_addr: 0x%08X", dc
->base_addr
);
1493 dc
->tty_flip_queue
.list
.next
= NULL
;
1494 dc
->tty_flip_queue
.list
.prev
= NULL
;
1495 dc
->tty_flip_queue
.sync
= 0;
1496 dc
->tty_flip_queue
.data
= dc
;
1497 dc
->tty_flip_queue
.routine
= tty_flip_queue_function
;
1500 if ( !(dc
->tty_flip_wq
= create_singlethread_workqueue(NOZOMI_NAME
)) ) {
1501 ERR("Could not create workqueue?");
1502 BUG_ON(!dc
->tty_flip_wq
);
1505 INIT_WORK( &dc
->tty_flip_wq_struct
, tty_flip_queue_function
, NULL
);
1508 spin_lock_init(&dc
->spin_mutex
);
1512 /* Enable RESET interrupt. */
1513 SET_IER( RESET
, 0xFFFF );
1516 err_disable_regions
:
1517 pci_release_regions(pdev
);
1518 iounmap((void *)dc
->base_addr
);
1522 pci_disable_device(pdev
);
1530 static void tty_do_close(port_t
*port
)
1532 #if 1 // add by Victor Yu. 05-24-2006
1533 unsigned long flags
;
1535 #if 0 // Add by Jared Wu. 11-15-2006. Now it can be waked up by signal.
1536 down(&port
->tty_sem
);
1538 if(down_interruptible(&port
->tty_sem
)) {
1543 if ( !port
->tty_open_count
) {
1548 port
->tty_open_count
--;
1553 if ( port
->tty_open_count
== 0) {
1554 D1("close: %d", port
->token_dl
);
1555 #if 1 // add by Victor Yu. 05-24-2006
1556 spin_lock_irqsave(&dc
->spin_mutex
, flags
);
1558 SET_IER( 0, port
->token_dl
);
1559 #if 1 // add by Victor Yu. 05-24-2006
1560 spin_unlock_irqrestore(&dc
->spin_mutex
, flags
);
1567 #if 0 // Add by Jared 11-15-2006. Compiling error.
1568 static void __exit
tty_exit(void) {
1570 static void __devexit
tty_exit(void) {
1576 for (i
= 0; i
< NTTY_TTY_MINORS
; ++i
)
1577 tty_unregister_device(&dc
->tty_driver
, i
);
1580 tty_unregister_driver(&dc
->tty_driver
);
1582 for (i
= 0; i
< NTTY_TTY_MINORS
; i
++) {
1583 while (dc
->port
[i
].tty_open_count
) {
1584 tty_do_close(&dc
->port
[i
]);
1586 dc
->port
[i
].tty
= NULL
;
1590 /* Deallocate memory for one device */
1591 static void __devexit
nozomi_card_exit(struct pci_dev
*pdev
)
1595 unsigned long flags
;
1597 #if 1 // Add by Jared 10-12-2006. Kill the pppd process in user space
1598 char *argv
[2], **envp
, *scratch
;
1600 if ( !(envp
=(char **)kmalloc(10*sizeof(char *), GFP_KERNEL
)) )
1602 argv
[0] = "/etc/init.d/umts.sh";
1605 envp
[i
++] = "HOME=/";
1606 envp
[i
++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
1608 call_usermodehelper(argv
[0], argv
, envp
,0);
1613 free_irq( pdev
->irq
, pdev
);
1614 spin_lock_irqsave(&dc
->spin_mutex
, flags
);
1615 /* Disable all interrupts */
1616 SET_IER( 0, 0xFFFF);
1617 spin_unlock_irqrestore(&dc
->spin_mutex
, flags
);
1619 #if 0 // mask by Victor Yu. 05-25-2006
1620 /* Send 0x0001, command card to resend the reset token. */
1621 /* This is to get the reset when the module is reloaded. */
1622 ctrl
.port
= 0x00; ctrl
.reserved
= 0; ctrl
.RTS
=0; ctrl
.DTR
=1;
1623 D1( "sending flow control 0x%04X", * ((u16
*) &ctrl
) );
1625 /* Setup dc->reg addresses to we can use defines here */
1626 nozomi_setup_private_data();
1627 SET_MEM( dc
->port
[PORT_CTRL
].ul_addr
[0], (u32
*) &ctrl
, 2 );
1628 SET_FCR( CTRL_UL
); /* push the token to the card. */
1631 D1( "pci_release_regions");
1632 pci_release_regions(pdev
);
1635 iounmap((void *)dc
->base_addr
);
1637 D1( "pci_disable_device");
1638 pci_disable_device(pdev
);
1640 for (i
=PORT_MDM
; i
< MAX_PORT
;i
++) {
1641 kfree ( dc
->port
[i
].fifo_ul
);
1644 kfree( dc
->send_buf
);
1650 destroy_workqueue(dc
->tty_flip_wq
);
1660 static void set_rts(int index
, int rts
)
1662 dc
->port
[index
].ctrl_ul
.RTS
= rts
;
1663 dc
->port
[index
].update_flow_control
= 1;
1664 enable_transmit_ul(PORT_CTRL
);
1667 static void set_dtr(int index
, int dtr
)
1669 D1("SETTING DTR index: %d, dtr: %d", index
, dtr
);
1670 dc
->port
[index
].ctrl_ul
.DTR
= dtr
;
1671 dc
->port
[index
].update_flow_control
= 1;
1672 enable_transmit_ul(PORT_CTRL
);
1676 /*------------------------------------------------------------------------------------------
1678 -----------------------------------------------------------------------------------------*/
1681 /* Called when the userspace process opens the tty, /dev/noz*. */
1682 static int ntty_open(struct tty_struct
*tty
, struct file
*file
)
1684 #if 1 // add by Victor Yu. 05-24-2006
1685 unsigned long flags
;
1688 s32 index
= tty
->index
;
1690 s32 index
= MINOR(tty
->device
) - tty
->driver
.minor_start
;
1692 port_t
*port
=&dc
->port
[index
];
1694 #if 0 // Add by Jared, 11-15-2006. It can be waked up by signal.
1695 down(&port
->tty_sem
);
1697 if(down_interruptible(&port
->tty_sem
)){
1698 return -ERESTARTSYS
;
1701 tty
->low_latency
= 1;
1702 tty
->driver_data
= port
;
1704 port
->tty_index
= index
;
1706 port
->tty_open_count
++;
1713 /* Enable interrupt downlink for channel */
1714 if ( port
->tty_open_count
== 1) {
1715 port
->rx_data
= port
->tx_data
= 0;
1716 D1("open: %d", port
->token_dl
);
1717 #if 1 // add by Victor Yu. 05-24-2006
1718 spin_lock_irqsave(&dc
->spin_mutex
, flags
);
1720 SET_IER( port
->token_dl
, port
->token_dl
);
1721 #if 1 // add by Victor Yu. 05-24-2006
1722 spin_unlock_irqrestore(&dc
->spin_mutex
, flags
);
1731 /* Called when the userspace process close the tty, /dev/noz*. */
1732 static void ntty_close(struct tty_struct
*tty
, struct file
*file
)
1734 tty_do_close((port_t
*) tty
->driver_data
);
1737 /* called when the userspace process writes to the tty (/dev/noz*). */
1738 /* Data is inserted into a fifo, which is then read and transfered to the modem. */
1740 static int ntty_write(struct tty_struct
*tty
, int from_user
, const unsigned char *buffer
, int count
)
1742 static s32
ntty_write(struct tty_struct
*tty
, s32 from_user
, const u8
*buffer
, s32 count
)
1745 #if 1 // add by Victor Yu. 05-24-2006
1746 unsigned long flags
;
1749 port_t
*port
= (port_t
*) tty
->driver_data
;
1751 /* D1( "WRITEx: %d, index = %d", count, index); */
1757 #if 0 // Add by Jared 11-15-2006. It can be waked up by signal now.
1758 down(&port
->tty_sem
);
1760 if(down_trylock(&port
->tty_sem
) ) { // must test lock as tty layer wraps calls to this function with BKL
1761 ERR("Would have deadlocked - return ERESTARTSYS");
1762 return -ERESTARTSYS
;
1765 if (! port
->tty_open_count
) {
1772 rval
= __kfifo_put_user(port
->fifo_ul
, (unsigned char *) buffer
, count
);
1774 rval
= __kfifo_put(port
->fifo_ul
, (unsigned char *) buffer
, count
);
1777 rval
= __kfifo_put(port
->fifo_ul
, (unsigned char *) buffer
, count
);
1782 D1( "No device context?");
1786 // CTS is only valid on the modem channel
1787 if ( port
== &(dc
->port
[PORT_MDM
]) ) {
1788 if ( port
->ctrl_dl
.CTS
) {
1789 D4( "Enable interrupt");
1790 #if 1 // add by Victor Yu. 05-24-2006
1791 spin_lock_irqsave(&dc
->spin_mutex
, flags
);
1793 enable_transmit_ul(port
->tty_index
);
1794 #if 1 // add by Victor Yu. 05-24-2006
1795 spin_unlock_irqrestore(&dc
->spin_mutex
, flags
);
1798 ERR("CTS not active on modem port?");
1801 #if 1 // add by Victor Yu. 05-24-2006
1802 spin_lock_irqsave(&dc
->spin_mutex
, flags
);
1804 enable_transmit_ul(port
->tty_index
);
1805 #if 1 // add by Victor Yu. 05-24-2006
1806 spin_unlock_irqrestore(&dc
->spin_mutex
, flags
);
1815 /* Calculate how much is left in device */
1816 /* This method is called by the upper tty layer. */
1817 /* #according to sources N_TTY.c it expects a value >= 0 and does not check for negative values. */
1818 static int ntty_write_room(struct tty_struct
*tty
)
1820 port_t
*port
= (port_t
*) tty
->driver_data
;
1827 #if 0 // Add by Jared Wu. 11-15-2006. Now it can be waked up by signal.
1828 down(&port
->tty_sem
);
1830 if(down_trylock(&port
->tty_sem
)){
1835 if (! port
->tty_open_count
) {
1839 room
= port
->fifo_ul
->size
- __kfifo_len(port
->fifo_ul
);
1846 /* Sets termios flags, called by the tty layer. */
1847 static void ntty_set_termios(struct tty_struct
*tty
, struct termios
*old_termios
)
1851 cflag
= tty
->termios
->c_cflag
;
1853 if ((cflag
== old_termios
->c_cflag
) &&
1854 (RELEVANT_IFLAG(tty
->termios
->c_iflag
) == RELEVANT_IFLAG(old_termios
->c_iflag
))) {
1855 D1( " - nothing to change...");
1860 /* get the byte size */
1861 switch (cflag
& CSIZE
) {
1863 D1( " - data bits = 5");
1866 D1( " - data bits = 6");
1869 D1( " - data bits = 7");
1873 D1( " - data bits = 8");
1877 /* determine the parity */
1878 if (cflag
& PARENB
) {
1879 if (cflag
& PARODD
) {
1880 D1( " - parity = odd");
1882 D1( " - parity = even");
1885 D1( " - parity = none");
1888 /* figure out the stop bits requested */
1889 if (cflag
& CSTOPB
) {
1890 D1( " - stop bits = 2");
1892 D1( " - stop bits = 1");
1895 /* figure out the hardware flow control settings */
1896 if (cflag
& CRTSCTS
) {
1897 D1( " - RTS/CTS is enabled");
1899 D1( " - RTS/CTS is disabled");
1902 /* determine software flow control */
1903 /* if we are implementing XON/XOFF, set the start and
1904 * stop character in the device */
1905 if (I_IXOFF(tty
) || I_IXON(tty
)) {
1907 unsigned char stop_char
= STOP_CHAR(tty
);
1908 unsigned char start_char
= START_CHAR(tty
);
1910 /* if we are implementing INBOUND XON/XOFF */
1912 D1( " - INBOUND XON/XOFF is enabled, "
1913 "XON = %2x, XOFF = %2x", start_char
, stop_char
);
1915 D1( " - INBOUND XON/XOFF is disabled");
1918 /* if we are implementing OUTBOUND XON/XOFF */
1920 D1( " - OUTBOUND XON/XOFF is enabled, "
1921 "XON = %2x, XOFF = %2x", start_char
, stop_char
);
1923 D1( " - OUTBOUND XON/XOFF is disabled");
1931 /* Gets io control parameters */
1932 static int ntty_tiocmget(struct tty_struct
*tty
, struct file
*file
)
1934 port_t
*port
= (port_t
*) tty
->driver_data
;
1935 ctrl_dl_t
*ctrl_dl
= &port
->ctrl_dl
;
1936 ctrl_ul_t
*ctrl_ul
= &port
->ctrl_ul
;
1939 | (ctrl_ul
->RTS
? TIOCM_RTS
: 0)
1940 | (ctrl_ul
->DTR
? TIOCM_DTR
: 0)
1941 | (ctrl_dl
->DCD
? TIOCM_CAR
: 0)
1942 | (ctrl_dl
->RI
? TIOCM_RNG
: 0)
1943 | (ctrl_dl
->DSR
? TIOCM_DSR
: 0)
1944 | (ctrl_dl
->CTS
? TIOCM_CTS
: 0);
1947 /* Sets io controls parameters */
1948 static int ntty_tiocmset(struct tty_struct
*tty
, struct file
*file
, u32 arg
)
1950 #if 1 // add by Victor Yu. 05-24-2006
1951 unsigned long flags
;
1953 port_t
*port
= (port_t
*) tty
->driver_data
;
1955 #if 1 // add by Victor Yu. 05-24-2006
1956 spin_lock_irqsave(&dc
->spin_mutex
, flags
);
1958 set_rts(port
->tty_index
, (arg
& TIOCM_RTS
) ? 1 : 0);
1959 set_dtr(port
->tty_index
, (arg
& TIOCM_DTR
) ? 1 : 0);
1960 #if 1 // add by Victor Yu. 05-24-2006
1961 spin_unlock_irqrestore(&dc
->spin_mutex
, flags
);
1967 static int ntty_ioctl_tiocmiwait(struct tty_struct
*tty
, struct file
*file
, unsigned int cmd
, unsigned long arg
)
1969 port_t
*port
= (port_t
*) tty
->driver_data
;
1971 if (cmd
== TIOCMIWAIT
) {
1972 DECLARE_WAITQUEUE(wait
, current
);
1973 struct async_icount cnow
;
1974 struct async_icount cprev
;
1976 cprev
= port
->tty_icount
;
1978 add_wait_queue(&port
->tty_wait
, &wait
);
1979 set_current_state(TASK_INTERRUPTIBLE
);
1981 remove_wait_queue(&port
->tty_wait
, &wait
);
1983 /* see if a signal woke us up */
1984 if (signal_pending(current
))
1985 return -ERESTARTSYS
;
1987 cnow
= port
->tty_icount
;
1988 if (cnow
.rng
== cprev
.rng
&& cnow
.dsr
== cprev
.dsr
&&
1989 cnow
.dcd
== cprev
.dcd
&& cnow
.cts
== cprev
.cts
)
1990 return -EIO
; /* no change => error */
1991 if (((arg
& TIOCM_RNG
) && (cnow
.rng
!= cprev
.rng
)) ||
1992 ((arg
& TIOCM_DSR
) && (cnow
.dsr
!= cprev
.dsr
)) ||
1993 ((arg
& TIOCM_CD
) && (cnow
.dcd
!= cprev
.dcd
)) ||
1994 ((arg
& TIOCM_CTS
) && (cnow
.cts
!= cprev
.cts
)) ) {
2001 return -ENOIOCTLCMD
;
2004 static int ntty_ioctl_tiocgicount(struct tty_struct
*tty
, struct file
*file
, unsigned int cmd
, unsigned long arg
)
2006 port_t
*port
= (port_t
*) tty
->driver_data
;
2008 if (cmd
== TIOCGICOUNT
) {
2009 struct async_icount cnow
= port
->tty_icount
;
2010 struct serial_icounter_struct icount
;
2012 icount
.cts
= cnow
.cts
;
2013 icount
.dsr
= cnow
.dsr
;
2014 icount
.rng
= cnow
.rng
;
2015 icount
.dcd
= cnow
.dcd
;
2016 icount
.rx
= cnow
.rx
;
2017 icount
.tx
= cnow
.tx
;
2018 icount
.frame
= cnow
.frame
;
2019 icount
.overrun
= cnow
.overrun
;
2020 icount
.parity
= cnow
.parity
;
2021 icount
.brk
= cnow
.brk
;
2022 icount
.buf_overrun
= cnow
.buf_overrun
;
2024 if (copy_to_user((void *)arg
, &icount
, sizeof(icount
)))
2028 return -ENOIOCTLCMD
;
2031 static int ntty_ioctl(struct tty_struct
*tty
, struct file
*file
, unsigned int cmd
, unsigned long arg
)
2033 #if 1 // add by Victor Yu. 05-24-2006
2034 unsigned long flags
;
2036 port_t
*port
= (port_t
*) tty
->driver_data
;
2038 int rval
= -ENOIOCTLCMD
;
2040 D1("******** IOCTL, cmd: %d", cmd
);
2042 #if 0 // mask by Victor Yu. 05-25-2006
2044 D1( "IOCTL TCGETS ...");
2045 rval
= -ENOIOCTLCMD
;
2048 D1( "IOCTL TCSETS ...");
2049 rval
= -ENOIOCTLCMD
;
2053 rval
= ntty_ioctl_tiocmiwait(tty
, file
, cmd
, arg
);
2056 rval
= ntty_ioctl_tiocgicount(tty
, file
, cmd
, arg
);
2059 rval
= ntty_tiocmget(tty
, file
);
2062 rval
= ntty_tiocmset(tty
, file
, arg
);
2065 if (get_user(mask
, (unsigned long *) arg
))
2068 #if 1 // add by Victor Yu. 05-24-2006
2069 spin_lock_irqsave(&dc
->spin_mutex
, flags
);
2071 if (mask
& TIOCM_RTS
)
2072 set_rts(port
->tty_index
, 0);
2073 if (mask
& TIOCM_DTR
)
2074 set_dtr(port
->tty_index
, 0);
2075 #if 1 // add by Victor Yu. 05-24-2006
2076 spin_unlock_irqrestore(&dc
->spin_mutex
, flags
);
2081 if (get_user(mask
, (unsigned long *) arg
))
2084 #if 1 // add by Victor Yu. 05-24-2006
2085 spin_lock_irqsave(&dc
->spin_mutex
, flags
);
2087 if (mask
& TIOCM_RTS
)
2088 set_rts(port
->tty_index
, 1);
2089 if (mask
& TIOCM_DTR
)
2090 set_dtr(port
->tty_index
, 1);
2091 #if 1 // add by Victor Yu. 05-24-2006
2092 spin_unlock_irqrestore(&dc
->spin_mutex
, flags
);
2096 #if 0 // mask by Victor Yu.
2098 D1( "IOCTL TCFLSH ...");
2099 rval
= -ENOIOCTLCMD
;
2103 D1( "ERR: 0x%08X, %d", cmd
, cmd
);
2111 /* Called by the upper tty layer when tty buffers are ready */
2112 /* to receive data again after a call to throttle. */
2113 static void ntty_unthrottle(struct tty_struct
*tty
)
2115 port_t
*port
= (port_t
*) tty
->driver_data
;
2116 unsigned long flags
;
2119 spin_lock_irqsave(&dc
->spin_mutex
, flags
);
2120 enable_transmit_dl(port
->tty_index
);
2121 set_rts(port
->tty_index
, 1);
2123 spin_unlock_irqrestore(&dc
->spin_mutex
, flags
);
2126 /* Called by the upper tty layer when the tty buffers are almost full. */
2127 /* The driver should stop send more data. */
2128 static void ntty_throttle(struct tty_struct
*tty
)
2130 port_t
*port
= (port_t
*) tty
->driver_data
;
2131 unsigned long flags
;
2134 spin_lock_irqsave(&dc
->spin_mutex
, flags
);
2135 set_rts(port
->tty_index
, 0);
2136 spin_unlock_irqrestore(&dc
->spin_mutex
, flags
);
2139 static void ntty_put_char(struct tty_struct
*tty
, unsigned char c
)
2141 D2("PUT CHAR Function: %c", c
);
2144 /* Returns number of chars in buffer, called by tty layer */
2145 static s32
ntty_chars_in_buffer(struct tty_struct
*tty
)
2147 port_t
*port
= (port_t
*) tty
->driver_data
;
2152 goto exit_in_buffer
;
2155 if (! port
->tty_open_count
) {
2156 ERR("No tty open?");
2158 goto exit_in_buffer
;
2161 rval
= __kfifo_len(port
->fifo_ul
);
2167 /* Initializes the tty */
2168 static int ntty_tty_init(void)
2170 struct tty_driver
*td
= &dc
->tty_driver
;
2174 memset(td
, 0, sizeof(struct tty_driver
));
2176 td
->magic
= TTY_DRIVER_MAGIC
;
2177 td
->driver_name
= NOZOMI_NAME_TTY
;
2179 td
->major
= NTTY_TTY_MAJOR
,
2180 td
->type
= TTY_DRIVER_TYPE_SERIAL
,
2181 td
->subtype
= SERIAL_TYPE_NORMAL
,
2182 td
->flags
= TTY_DRIVER_REAL_RAW
| TTY_DRIVER_NO_DEVFS
,
2183 td
->init_termios
= tty_std_termios
;
2184 td
->init_termios
.c_cflag
= B115200
| CS8
| CREAD
| HUPCL
| CLOCAL
;
2188 td
->minor_start
= 0;
2191 td
->table
= dc
->tty_table
;
2192 td
->refcount
= &dc
->tty_refcount
;
2195 td
->termios
= dc
->tty_termios
;
2196 td
->termios_locked
= dc
->tty_termios_locked
;
2198 td
->ioctl
= ntty_ioctl
;
2199 td
->open
= ntty_open
;
2200 td
->close
= ntty_close
;
2201 td
->write
= ntty_write
;
2202 td
->write_room
= ntty_write_room
;
2203 td
->unthrottle
= ntty_unthrottle
;
2204 td
->throttle
= ntty_throttle
;
2205 td
->set_termios
= ntty_set_termios
;
2206 td
->chars_in_buffer
= ntty_chars_in_buffer
;
2207 td
->put_char
= ntty_put_char
;
2209 rval
= tty_register_driver(td
);
2212 printk(KERN_ERR
"failed to register ntty tty driver");
2213 D1( "failed to register ntty tty driver");
2217 for (i
= 0; i
< NTTY_TTY_MINORS
; i
++) {
2218 init_MUTEX(&dc
->port
[i
].tty_sem
);
2219 dc
->port
[i
].tty_open_count
= 0;
2220 dc
->port
[i
].tty
= NULL
;
2223 tty_register_device(td
, i
, NULL
);
2227 printk(KERN_INFO DRIVER_DESC
" " NOZOMI_NAME_TTY
);
2232 /* Module initialization */
2233 static struct pci_driver nozomi_driver
= {
2234 .name
= NOZOMI_NAME
,
2235 .id_table
= nozomi_pci_tbl
,
2236 .probe
= nozomi_card_init
,
2237 .remove
= __devexit_p(nozomi_card_exit
),
2240 static int __init
nozomi_init(void)
2244 rval
= pci_module_init(&nozomi_driver
);
2245 printk(KERN_INFO
"Initializing %s\n", VERSION_STRING
);
2249 static void nozomi_exit(void)
2251 printk(KERN_INFO
"Unloading %s", DRIVER_DESC
);
2252 pci_unregister_driver(&nozomi_driver
);
2255 module_init(nozomi_init
);
2256 module_exit(nozomi_exit
);
2259 MODULE_PARM(nzdebug
, "i");
2262 MODULE_LICENSE("Dual BSD/GPL");
2263 MODULE_DESCRIPTION( DRIVER_DESC
);