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,0)
50 #elif LINUX_VERSION_CODE > KERNEL_VERSION(2,4,0)
54 #include <linux/module.h>
55 #include <linux/pci.h>
56 #include <linux/ioport.h>
57 #include <linux/tty.h>
58 #include <linux/tty_driver.h>
59 #include <linux/tty_flip.h>
60 #include <linux/serial.h>
61 #include <linux/interrupt.h>
62 #include <linux/kmod.h>
63 #include <linux/proc_fs.h>
64 #include <linux/spinlock.h>
65 #include <asm/uaccess.h>
68 #define VERSION_STRING DRIVER_DESC " (build date: " __DATE__ " " __TIME__ ")"
71 //#include <linux/kfifo.h>
82 typedef void irqreturn_t;
87 #error "This driver needs PCI support to be available"
90 /* Macros definitions */
92 /* Enable this to have a lot of debug printouts */
93 /* #define NOZOMI_DEBUG */
95 /* Default debug printout level */
96 #define NOZOMI_DEBUG_LEVEL 0x1
98 #define P_BUF_SIZE 128
99 #define NFO( _err_flag_, args...) \
101 char t_m_p_[P_BUF_SIZE]; \
102 snprintf(t_m_p_, sizeof(t_m_p_), ##args); \
103 printk( _err_flag_ "[%d] %s(): %s\n", __LINE__, __FUNCTION__, t_m_p_); \
106 #define INFO(args...) NFO(KERN_INFO, ##args)
107 #define ERR(args...) NFO( KERN_ERR, ##args)
109 #define D1(args...) D_(0x01, ##args)
110 #define D2(args...) D_(0x02, ##args)
111 #define D3(args...) D_(0x04, ##args)
112 #define D4(args...) D_(0x08, ##args)
113 #define D5(args...) D_(0x10, ##args)
114 #define D6(args...) D_(0x20, ##args)
115 #define D7(args...) D_(0x40, ##args)
116 #define D8(args...) D_(0x80, ##args)
120 #define D_(lvl, args...) D(lvl, ##args)
121 /* Do we need this settable at runtime? */
122 static int nzdebug = NOZOMI_DEBUG_LEVEL;
124 #define D(lvl, args...) do{if(lvl & nzdebug) NFO(KERN_DEBUG, ##args );}while(0)
125 #define D_(lvl, args...) D(lvl, ##args)
127 /* These printouts are always printed */
130 static const int nzdebug = 0;
131 #define D_(lvl, args...)
134 /* TODO: rewrite to optimize macros... */
135 #define SET_FCR(value__) \
137 writew((value__), (void*) (dc->REG_FCR )); \
140 #define SET_IER(value__, mask__) \
142 dc->ier_last_written = (dc->ier_last_written & ~mask__) | (value__ & mask__ );\
143 writew( dc->ier_last_written, (void*) (dc->REG_IER));\
146 #define GET_IER(read_val__) \
148 (read_val__) = readw((void*) (dc->REG_IER));\
151 #define GET_IIR(read_val__) \
153 (read_val__) = readw((void*) (dc->REG_IIR));\
156 #define GET_MEM(value__, addr__, length__) \
158 read_mem32( (u32*) (value__), (u32) (addr__), (length__));\
161 #define GET_MEM_BUF(value__, addr__, length__) \
163 read_mem32_buf( (u32*) (value__), (u32) (addr__), (length__));\
166 #define SET_MEM(addr__, value__, length__) \
168 write_mem32( (addr__), (u32*) (value__), (length__));\
171 #define SET_MEM_BUF(addr__, value__, length__) \
173 write_mem32_buf( (addr__), (u32*) (value__), (length__));\
177 #define TMP_BUF_MAX 256
179 #define DUMP(buf__,len__) \
181 char tbuf[TMP_BUF_MAX]={0};\
183 snprintf(tbuf, len__ > TMP_BUF_MAX ? TMP_BUF_MAX : len__, "%s",buf__);\
184 if(tbuf[len__-2] == '\r') {\
185 tbuf[len__-2] = 'r';\
187 D1( "SENDING: '%s' (%d+n)", tbuf, len__);\
189 D1( "SENDING: '%s' (%d)", tbuf, len__);\
193 #define RELEVANT_IFLAG(iflag) ((iflag) & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
197 #define NOZOMI_NAME "nozomi"
198 #define NOZOMI_NAME_TTY "nozomi_tty"
199 #define DRIVER_DESC "Nozomi driver"
201 #define NTTY_TTY_MAJOR 241
202 #define NTTY_TTY_MINORS MAX_PORT
203 #define NTTY_FIFO_BUFFER_SIZE 8192
205 /* Must be power of 2 */
206 #define FIFO_BUFFER_SIZE_UL 8192
208 /* Size of tmp send buffer to card */
209 #define SEND_BUF_MAX 1024
210 #define RECEIVE_BUF_MAX 4
212 /* Our fake UART values */
215 #define MCR_LOOP 0x04
221 /* Define all types of vendors and devices to support */
222 #define VENDOR1 0x1931 /* Vendor Option */
223 #define DEVICE1 0x000c /* HSDPA card */
225 #define R_IIR 0x0000 /* Interrupt Identity Register */
226 #define R_FCR 0x0000 /* Flow Control Register */
227 #define R_IER 0x0004 /* Interrupt Enable Register */
229 #define CONFIG_MAGIC 0xEFEFFEFE
230 #define TOGGLE_VALID 0x0000
232 /* Definition of interrupt tokens */
233 #define MDM_DL1 0x0001
234 #define MDM_UL1 0x0002
235 #define MDM_DL2 0x0004
236 #define MDM_UL2 0x0008
237 #define DIAG_DL1 0x0010
238 #define DIAG_DL2 0x0020
239 #define DIAG_UL 0x0040
240 #define APP1_DL 0x0080
241 #define APP1_UL 0x0100
242 #define APP2_DL 0x0200
243 #define APP2_UL 0x0400
244 #define CTRL_DL 0x0800
245 #define CTRL_UL 0x1000
248 #define MDM_DL (MDM_DL1 | MDM_DL2)
249 #define MDM_UL (MDM_UL1 | MDM_UL2)
250 #define DIAG_DL (DIAG_DL1 | DIAG_DL2)
252 /* modem signal definition */
253 #define CTRL_DSR 0x0001
254 #define CTRL_DCD 0x0002
255 #define CTRL_RI 0x0004
256 #define CTRL_CTS 0x0008
258 #define CTRL_DTR 0x0001
259 #define CTRL_RTS 0x0002
262 #define NOZOMI_MAX_PORTS 5
264 /* Type definitions */
266 /* There are two types of nozomi cards, one with 2048 memory and with 8192 memory */
268 F32_2 = 2048, /* Has 512 bytes downlink and uplink * 2 -> 2048 */
269 F32_8 = 9192, /* Has 3072 bytes downlink and 1024 bytes uplink * 2 -> 8192 */
272 /* Two different toggle channels exist */
278 /* Port definition for the card regarding flow control */
288 /* Ports that the nozomi has */
302 unsigned enabled : 5; /* Toggle fields are valid if enabled is 0, else A-channels
303 must always be used. */
304 unsigned diag_dl : 1;
307 } __attribute__ ((packed)) toggles_t;
309 /* Configuration table to read at startup of card */
310 /* Is for now only needed during initialization phase */
313 u16 product_information;
318 u16 dl_mdm_len1; /* If this is 64, it can hold 60 bytes + 4 that is length field */
322 u16 dl_mdm_len2; /* If this is 64, it can hold 60 bytes + 4 that is length field */
336 } __attribute__((packed)) config_table_t;
338 /* This stores all control downlink flags */
341 unsigned reserved : 4;
346 } __attribute__ ((packed)) ctrl_dl_t;
348 /* This stores all control uplink flags */
351 unsigned reserved : 6;
354 } __attribute__ ((packed)) ctrl_ul_t;
359 /* This represents the toggle information */
363 unsigned diag_dl : 1;
364 unsigned enabled : 5; /* Toggle fields are valid if enabled is 0, else A-channels
365 must always be used. */
366 } __attribute__ ((packed)) toggles_t;
368 /* Configuration table to read at startup of card */
372 u16 product_information;
376 u16 dl_mdm_len1; /* If this is 64, it can hold 60 bytes + 4 that is length field */
391 } __attribute__((packed)) config_table_t;
393 /* This stores all control downlink flags */
399 unsigned reserverd : 4;
401 } __attribute__ ((packed)) ctrl_dl_t;
403 /* This stores all control uplink flags */
407 unsigned reserved : 6;
409 } __attribute__ ((packed)) ctrl_ul_t;
412 /* This holds all information that is needed regarding a port */
414 u8 update_flow_control;
417 struct kfifo *fifo_ul;
425 struct tty_struct *tty;
427 struct semaphore tty_sem;
428 wait_queue_head_t tty_wait;
429 struct async_icount tty_icount;
431 u32 rx_data, tx_data;
435 /* Private data one for each card in the system */
439 /* Register addresses */
443 volatile u16 ier_last_written;
444 card_type_t card_type;
445 config_table_t config_table; /* Configuration table */
446 struct pci_dev *pdev;
447 port_t port[NOZOMI_MAX_PORTS];
449 struct tty_driver tty_driver;
452 struct tty_struct *tty_table[NTTY_TTY_MINORS];
453 struct tq_struct tty_flip_queue;
457 struct workqueue_struct *tty_flip_wq;
458 struct work_struct tty_flip_wq_struct;
461 struct termios *tty_termios[NTTY_TTY_MINORS];
462 struct termios *tty_termios_locked[NTTY_TTY_MINORS];
463 spinlock_t spin_mutex;
466 struct proc_dir_entry *proc_entry;
469 /* This is a data packet that is read or written to/from card */
471 u32 size; /* size is the length of the data buffer */
473 } __attribute__ ((packed)) buf_t;
475 /* Function declarations */
476 static int ntty_tty_init(dc_t *dc);
478 static void tty_flip_queue_function(void *tmp_dc);
480 /* Global variables */
481 static struct pci_device_id nozomi_pci_tbl[] __devinitdata = {
486 /* Used to store interrupt variables */
488 volatile u16 read_iir; /* Holds current interrupt tokens */
491 MODULE_DEVICE_TABLE(pci, nozomi_pci_tbl);
493 /* Representing the pci device of interest */
494 static int cards_found;
496 struct pci_dev *my_pdev = NULL;
499 static inline dc_t* get_dc_by_pdev(struct pci_dev* pdev)
504 static inline dc_t* get_dc_by_index(s32 index )
509 static inline s32 get_index(struct tty_struct *tty)
514 return MINOR(tty->device) - tty->driver.minor_start;
518 static inline port_t* get_port_by_tty(struct tty_struct *tty)
520 return &my_dev->port[ get_index(tty) ];
523 static inline dc_t* get_dc_by_tty(struct tty_struct *tty )
531 /* -Rewrite cleaner */
532 static void read_mem32(u32 *buf, u32 mem_addr_start, u32 size_bytes)
535 u32 *ptr = (u32*) mem_addr_start;
539 if ( size_bytes == 2 ) {
541 *buf16 = readw( ptr );
545 while ( i < size_bytes ) {
546 if ( size_bytes - i == 2) {
547 /* Handle 2 bytes in the end */
549 *(buf16) = readw( ptr );
553 *(buf) = readl( ptr );
561 /* - Rewrite cleaner */
562 /* - merge with read_mem32() */
563 static void read_mem32_buf(u32 *buf, u32 mem_addr_start, u32 size_bytes)
567 u32 *ptr = (u32*) mem_addr_start;
571 if ( size_bytes == 2 ) {
573 *buf16 = __le16_to_cpu( readw( ptr ));
577 while ( i < size_bytes ) {
578 if ( size_bytes - i == 2) {
579 /* Handle 2 bytes in the end */
581 *(buf16) = __le16_to_cpu( readw( ptr ));
585 *(buf) = __le32_to_cpu( readl( ptr ));
591 read_mem32(buf, mem_addr_start, size_bytes);
597 /* -Rewrite cleaner */
598 static u32 write_mem32(u32 mem_addr_start, u32 *buf, u32 size_bytes)
601 u32 *ptr = (u32*) mem_addr_start;
605 if (size_bytes == 2) {
607 writew( *buf16, ptr);
611 while (i < size_bytes) {
612 if ( size_bytes - i == 2) {
615 writew( *buf16, ptr);
628 /* - Merge with write_mem32() */
629 static u32 write_mem32_buf(u32 mem_addr_start, u32 *buf, u32 size_bytes)
633 u32 *ptr = (u32*) mem_addr_start;
637 if (size_bytes == 2) {
639 writew( __le16_to_cpu(*buf16), ptr);
643 while (i < size_bytes) {
644 if ( size_bytes - i == 2) {
647 writew( __le16_to_cpu(*buf16), ptr);
651 writel( __cpu_to_le32( *buf ), ptr );
658 return write_mem32(mem_addr_start, buf, size_bytes);
662 /* Setup pointers to different channels and also setup buffer sizes. */
663 static void setup_memory(dc_t *dc)
665 /* The length reported is including the length field of 4 bytes, hence subtract with 4. */
666 u32 offset = dc->base_addr + dc->config_table.dl_start;
669 /* Modem port dl configuration */
670 dc->port[PORT_MDM].dl_addr[CH_A] = offset;
671 dc->port[PORT_MDM].dl_addr[CH_B] = (offset += dc->config_table.dl_mdm_len1);
672 dc->port[PORT_MDM].dl_size[CH_A] = dc->config_table.dl_mdm_len1 - buff_offset;
673 dc->port[PORT_MDM].dl_size[CH_B] = dc->config_table.dl_mdm_len2 - buff_offset;
675 /* Diag port dl configuration */
676 dc->port[PORT_DIAG].dl_addr[CH_A] = (offset += dc->config_table.dl_mdm_len2);
677 dc->port[PORT_DIAG].dl_size[CH_A] = dc->config_table.dl_diag_len1 - buff_offset;
678 dc->port[PORT_DIAG].dl_addr[CH_B] = (offset += dc->config_table.dl_diag_len1);
679 dc->port[PORT_DIAG].dl_size[CH_B] = dc->config_table.dl_diag_len2 - buff_offset;
681 /* App1 port dl configuration */
682 dc->port[PORT_APP1].dl_addr[CH_A] = (offset += dc->config_table.dl_diag_len2);
683 dc->port[PORT_APP1].dl_size[CH_A] = dc->config_table.dl_app1_len - buff_offset;
685 /* App2 port dl configuration */
686 dc->port[PORT_APP2].dl_addr[CH_A] = (offset += dc->config_table.dl_app1_len);
687 dc->port[PORT_APP2].dl_size[CH_A] = dc->config_table.dl_app2_len - buff_offset;
689 /* Ctrl dl configuration */
690 dc->port[PORT_CTRL].dl_addr[CH_A] = (offset += dc->config_table.dl_app2_len);
691 dc->port[PORT_CTRL].dl_size[CH_A] = dc->config_table.dl_ctrl_len - buff_offset;
694 /* Modem Port ul configuration */
695 dc->port[PORT_MDM].ul_addr[CH_A] = (offset = dc->base_addr + dc->config_table.ul_start);
696 dc->port[PORT_MDM].ul_size[CH_A] = dc->config_table.ul_mdm_len1 - buff_offset;
697 dc->port[PORT_MDM].ul_addr[CH_B] = (offset += dc->config_table.ul_mdm_len1);
698 dc->port[PORT_MDM].ul_size[CH_B] = dc->config_table.ul_mdm_len2 - buff_offset;
700 /* Diag port ul configuration */
701 dc->port[PORT_DIAG].ul_addr[CH_A] = (offset += dc->config_table.ul_mdm_len2);
702 dc->port[PORT_DIAG].ul_size[CH_A] = dc->config_table.ul_diag_len - buff_offset;
704 /* App1 port ul configuration */
705 dc->port[PORT_APP1].ul_addr[CH_A] = (offset += dc->config_table.ul_diag_len);
706 dc->port[PORT_APP1].ul_size[CH_A] = dc->config_table.ul_app1_len - buff_offset;
708 /* App2 port ul configuration */
709 dc->port[PORT_APP2].ul_addr[CH_A] = (offset += dc->config_table.ul_app1_len);
710 dc->port[PORT_APP2].ul_size[CH_A] = dc->config_table.ul_app2_len - buff_offset;
712 /* Ctrl ul configuration */
713 dc->port[PORT_CTRL].ul_addr[CH_A] = (offset += dc->config_table.ul_app2_len);
714 dc->port[PORT_CTRL].ul_size[CH_A] = dc->config_table.ul_ctrl_len - buff_offset;
715 offset = dc->config_table.ul_start;
718 /* Dump config table under initalization phase */
720 static void dump_table(dc_t *dc)
722 D3("signature: 0x%08X", dc->config_table.signature);
723 D3("version: 0x%04X", dc->config_table.version);
724 D3("product_information: 0x%04X", dc->config_table.product_information);
725 D3("toggle enabled: %d", dc->config_table.toggle.enabled);
726 D3("toggle up_mdm: %d", dc->config_table.toggle.mdm_ul);
727 D3("toggle dl_mdm: %d", dc->config_table.toggle.mdm_dl);
728 D3("toggle dl_dbg: %d", dc->config_table.toggle.diag_dl);
730 D3("dl_start: 0x%04X", dc->config_table.dl_start);
731 D3("dl_mdm_len0: 0x%04X, %d", dc->config_table.dl_mdm_len1, dc->config_table.dl_mdm_len1);
732 D3("dl_mdm_len1: 0x%04X, %d", dc->config_table.dl_mdm_len2, dc->config_table.dl_mdm_len2);
733 D3("dl_diag_len0: 0x%04X, %d", dc->config_table.dl_diag_len1, dc->config_table.dl_diag_len1);
734 D3("dl_diag_len1: 0x%04X, %d", dc->config_table.dl_diag_len2, dc->config_table.dl_diag_len2);
735 D3("dl_app1_len: 0x%04X, %d", dc->config_table.dl_app1_len, dc->config_table.dl_app1_len);
736 D3("dl_app2_len: 0x%04X, %d", dc->config_table.dl_app2_len, dc->config_table.dl_app2_len);
737 D3("dl_ctrl_len: 0x%04X, %d", dc->config_table.dl_ctrl_len, dc->config_table.dl_ctrl_len);
738 D3("ul_start: 0x%04X, %d", dc->config_table.ul_start, dc->config_table.ul_start);
739 D3("ul_mdm_len[0]: 0x%04X, %d", dc->config_table.ul_mdm_len1, dc->config_table.ul_mdm_len1);
740 D3("ul_mdm_len[1]: 0x%04X, %d", dc->config_table.ul_mdm_len2, dc->config_table.ul_mdm_len2);
741 D3("ul_diag_len: 0x%04X, %d", dc->config_table.ul_diag_len, dc->config_table.ul_diag_len);
742 D3("ul_app1_len: 0x%04X, %d", dc->config_table.ul_app1_len, dc->config_table.ul_app1_len);
743 D3("ul_app2_len: 0x%04X, %d", dc->config_table.ul_app2_len, dc->config_table.ul_app2_len);
744 D3("ul_ctrl_len: 0x%04X, %d", dc->config_table.ul_ctrl_len, dc->config_table.ul_ctrl_len);
748 /* Read configuration table from card under intalization phase */
749 /* Returns 1 if ok, else 0 */
750 static int nozomi_read_config_table(dc_t *dc)
752 GET_MEM( &dc->config_table, dc->base_addr + 0, sizeof(config_table_t));
753 /* D1( "0x%08X == 0x%08X ", dc->config_table.signature, CONFIG_MAGIC); */
754 if ( dc->config_table.signature != CONFIG_MAGIC ) {
755 ERR("ConfigTable Bad! 0x%08X != 0x%08X", dc->config_table.signature, CONFIG_MAGIC);
759 if ( (dc->config_table.version == 0) || (dc->config_table.toggle.enabled == TOGGLE_VALID) ) {
762 D1( "Second phase, configuring card");
764 dc->port[PORT_MDM].toggle_ul = dc->config_table.toggle.mdm_ul;
765 dc->port[PORT_MDM].toggle_dl = dc->config_table.toggle.mdm_dl;
766 dc->port[PORT_DIAG].toggle_dl = dc->config_table.toggle.diag_dl;
767 D1( "toggle ports: MDM UL:%d MDM DL:%d, DIAG DL:%d",
768 dc->port[PORT_MDM].toggle_ul,
769 dc->port[PORT_MDM].toggle_dl,
770 dc->port[PORT_DIAG].toggle_dl);
775 for (i=PORT_MDM; i< MAX_PORT;i++) {
776 dc->port[i].fifo_ul = kfifo_alloc( FIFO_BUFFER_SIZE_UL, GFP_ATOMIC , NULL);
777 memset( &dc->port[i].ctrl_dl, 0, sizeof (ctrl_dl_t));
778 memset( &dc->port[i].ctrl_ul, 0, sizeof (ctrl_ul_t));
781 /* Enable control channel */
782 SET_IER( CTRL_DL, CTRL_DL );
784 INFO("Initialization OK!");
788 if( (dc->config_table.version > 0) && (dc->config_table.toggle.enabled != TOGGLE_VALID ) ) {
790 D1( "First phase: pushing upload buffers, clearing download");
791 INFO("Version of card: %d", dc->config_table.version);
793 /* Here we should disable all I/O over F32. */
796 /* We should send ALL channel pair tokens back along with reset token */
798 /* push upload modem buffers */
799 SET_MEM( dc->port[PORT_MDM].ul_addr[CH_A], &offset, 4);
800 SET_MEM( dc->port[PORT_MDM].ul_addr[CH_B], &offset, 4);
802 SET_FCR( MDM_UL | DIAG_DL | MDM_DL );
804 D1( "First phase done");
810 /* Enable uplink interrupts */
811 static void enable_transmit_ul( port_type_t port , dc_t *dc )
814 case PORT_MDM: SET_IER( MDM_UL , MDM_UL ); break;
815 case PORT_DIAG: SET_IER( DIAG_UL, DIAG_UL ); break;
816 case PORT_APP1: SET_IER( APP1_UL, APP1_UL ); break;
817 case PORT_APP2: SET_IER( APP2_UL, APP2_UL ); break;
818 case PORT_CTRL: SET_IER( CTRL_UL, CTRL_UL ); break;
820 ERR("Called with wrong port?");
825 /* Disable uplink interrupts */
826 static void disable_transmit_ul( port_type_t port , dc_t *dc )
829 case PORT_MDM: SET_IER( 0 ,MDM_UL ); break;
830 case PORT_DIAG: SET_IER( 0, DIAG_UL ); break;
831 case PORT_APP1: SET_IER( 0, APP1_UL ); break;
832 case PORT_APP2: SET_IER( 0, APP2_UL ); break;
833 case PORT_CTRL: SET_IER( 0, CTRL_UL ); break;
835 ERR("Called with wrong port?");
840 /* Enable downlink interrupts */
841 static void enable_transmit_dl( port_type_t port , dc_t *dc )
844 case PORT_MDM: SET_IER( MDM_DL , MDM_DL ); break;
845 case PORT_DIAG: SET_IER( DIAG_DL, DIAG_DL ); break;
846 case PORT_APP1: SET_IER( APP1_DL, APP1_DL ); break;
847 case PORT_APP2: SET_IER( APP2_DL, APP2_DL ); break;
848 case PORT_CTRL: SET_IER( CTRL_DL, CTRL_DL ); break;
850 ERR("Called with wrong port?");
855 /* Disable downlink interrupts */
856 static void disable_transmit_dl( port_type_t port , dc_t *dc )
859 case PORT_MDM: SET_IER( 0 ,MDM_DL ); break;
860 case PORT_DIAG: SET_IER( 0, DIAG_DL ); break;
861 case PORT_APP1: SET_IER( 0, APP1_DL ); break;
862 case PORT_APP2: SET_IER( 0, APP2_DL ); break;
863 case PORT_CTRL: SET_IER( 0, CTRL_DL ); break;
865 ERR("Called with wrong port?");
870 /* Return 1 - send buffer to card and ack. */
871 /* Return 0 - don't ack, don't send buffer to card. */
872 int send_data( port_type_t index, dc_t *dc )
875 port_t *port = &dc->port[index];
876 u8 toggle = port->toggle_ul;
877 u32 addr = port->ul_addr[toggle];
878 u32 ul_size = port->ul_size[toggle];
879 struct tty_struct *tty = port->tty;
881 if (index >= NTTY_TTY_MINORS) {
882 ERR("Called with wrong index?");
886 /* Get data from tty and place in buf for now */
887 size = __kfifo_get( port->fifo_ul, dc->send_buf, ul_size < SEND_BUF_MAX ? ul_size : SEND_BUF_MAX );
890 D4("No more data to send, disable link:");
894 port->tx_data += size;
896 /* DUMP(buf, size); */
898 /* Write length + data */
899 SET_MEM( addr, &size, 4 );
900 SET_MEM_BUF( addr + 4, dc->send_buf, size);
903 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) && tty->ldisc.write_wakeup) {
904 tty->ldisc.write_wakeup(tty);
906 wake_up_interruptible(&tty->write_wait);
912 /* If all data has been read, return 1, else 0 */
913 static int receive_data( port_type_t index, dc_t* dc )
915 u8 buf[RECEIVE_BUF_MAX];
918 port_t *port=&dc->port[index];
919 u8 toggle=port->toggle_dl;
920 u32 addr=port->dl_addr[toggle];
922 struct tty_struct *tty=port->tty;
925 D1("tty not open for port: %d?", index);
929 if (test_bit(TTY_DONT_FLIP, &tty->flags)) {
930 D6("TTY_DONT_FLIP set!! %d", index);
931 /* Here we disable interrupt for that port and schedule */
932 /* task. Task wakes up a little bit later and enables interrupt.. */
933 port->tty_dont_flip = 1;
934 disable_transmit_dl(index, dc);
936 schedule_task(&dc->tty_flip_queue);
939 if (!queue_work(dc->tty_flip_wq, &dc->tty_flip_wq_struct)) {
940 ERR("Call to queue_work() failed.");
946 GET_MEM( &size, addr, 4 );
947 /* D1( "%d bytes port: %d", size, index); */
949 if ( test_bit( TTY_THROTTLED, & tty->flags) ) {
950 D1("No room in tty, don't read data, don't ack interrupt, disable interrupt");
951 /* disable interrupt in downlink... */
952 disable_transmit_dl(index, dc);
962 GET_MEM_BUF(buf, addr + offset, 4);
964 while (i < 4 && size > 0) {
965 if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
966 tty_flip_buffer_push(tty);
968 tty_insert_flip_char(tty, buf[i], TTY_NORMAL);
977 tty_flip_buffer_push(tty);
981 /* Debug for interrupts */
983 static char* interrupt2str( u16 interrupt)
985 static char buf[TMP_BUF_MAX];
988 interrupt & MDM_DL1 ? p += snprintf(p, TMP_BUF_MAX, "MDM_DL1 "):0;
989 interrupt & MDM_DL2 ? p += snprintf(p, TMP_BUF_MAX, "MDM_DL2 "):0;
991 interrupt & MDM_UL1 ? p += snprintf(p, TMP_BUF_MAX, "MDM_UL1 "):0;
992 interrupt & MDM_UL2 ? p += snprintf(p, TMP_BUF_MAX, "MDM_UL2 "):0;
994 interrupt & DIAG_DL1 ? p += snprintf(p, TMP_BUF_MAX, "DIAG_DL1 "):0;
995 interrupt & DIAG_DL2 ? p += snprintf(p, TMP_BUF_MAX, "DIAG_DL2 "):0;
997 interrupt & DIAG_UL ? p += snprintf(p, TMP_BUF_MAX, "DIAG_UL "):0;
999 interrupt & APP1_DL ? p += snprintf(p, TMP_BUF_MAX, "APP1_DL "):0;
1000 interrupt & APP2_DL ? p += snprintf(p, TMP_BUF_MAX, "APP2_DL "):0;
1002 interrupt & APP1_UL ? p += snprintf(p, TMP_BUF_MAX, "APP1_UL "):0;
1003 interrupt & APP2_UL ? p += snprintf(p, TMP_BUF_MAX, "APP2_UL "):0;
1005 interrupt & CTRL_DL ? p += snprintf(p, TMP_BUF_MAX, "CTRL_DL "):0;
1006 interrupt & CTRL_UL ? p += snprintf(p, TMP_BUF_MAX, "CTRL_UL "):0;
1008 interrupt & RESET ? p += snprintf(p, TMP_BUF_MAX, "RESET "):0;
1014 /* Receive flow control */
1015 /* Return 1 - If ok, else 0 */
1016 static int receive_flow_control( dc_t *dc, irq_t *m)
1018 port_type_t port = PORT_MDM;
1023 GET_MEM( &ctrl_dl, dc->port[PORT_CTRL].dl_addr[CH_A], 2);
1025 switch( ctrl_dl.port ) {
1027 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).");
1029 case CTRL_MDM: port = PORT_MDM; enable_ier = MDM_DL; break;
1030 case CTRL_DIAG: port = PORT_DIAG; enable_ier = DIAG_DL; break;
1031 case CTRL_APP1: port = PORT_APP1; enable_ier = APP1_DL; break;
1032 case CTRL_APP2: port = PORT_APP2; enable_ier = APP2_DL; break;
1034 ERR("ERROR: flow control received for non-existing port");
1038 D1( "0x%04X->0x%04X", *((u16*) &dc->port[port].ctrl_dl), *((u16*)&ctrl_dl));
1040 old_ctrl = dc->port[port].ctrl_dl;
1041 dc->port[port].ctrl_dl = ctrl_dl;
1043 if ( old_ctrl.CTS == 1 && ctrl_dl.CTS == 0 ) {
1044 D1( "Disable interrupt (0x%04X) on port: %d", enable_ier, port);
1045 disable_transmit_ul(port, dc);
1046 } else if ( old_ctrl.CTS == 0 && ctrl_dl.CTS == 1 ) {
1047 if ( __kfifo_len(dc->port[port].fifo_ul) ) {
1048 D1( "Enable interrupt (0x%04X) on port: %d", enable_ier, port);
1049 D1( "Data in buffer [%d], enable transmit! ", __kfifo_len(dc->port[port].fifo_ul) );
1050 enable_transmit_ul( port, dc );
1052 D1( "No data in buffer...");
1056 if(*(u16*)&old_ctrl == *(u16*)&ctrl_dl) {
1057 D1( " No change in mctrl");
1060 /* Update statistics */
1061 if(old_ctrl.CTS != ctrl_dl.CTS) {
1062 dc->port[port].tty_icount.cts++;
1064 if(old_ctrl.DSR != ctrl_dl.DSR) {
1065 dc->port[port].tty_icount.dsr++;
1067 if(old_ctrl.RI != ctrl_dl.RI) {
1068 dc->port[port].tty_icount.rng++;
1070 if(old_ctrl.DCD != ctrl_dl.DCD) {
1071 dc->port[port].tty_icount.dcd++;
1073 D1("port: %d DCD(%d), CTS(%d), RI(%d), DSR(%d)", port,
1074 dc->port[port].tty_icount.dcd, dc->port[port].tty_icount.cts,
1075 dc->port[port].tty_icount.rng, dc->port[port].tty_icount.dsr);
1081 /* - return ctrl_port_t */
1082 static u8 port2ctrl(port_type_t port)
1094 ERR("ERROR: send flow control received for non-existing port");
1100 /* Send flow control, can only update one channel at a time */
1101 /* Return 0 - If we have updated all flow control */
1102 /* Return 1 - If we need to update more flow control, ack current enable more */
1103 static int send_flow_control( dc_t *dc )
1105 u32 i, more_flow_control_to_be_updated = 0;
1108 for( i=PORT_MDM; i<MAX_PORT ; i++ ) {
1109 if( dc->port[i].update_flow_control ) {
1110 if ( more_flow_control_to_be_updated ) {
1111 /* We have more flow control to be updated */
1114 dc->port[i].ctrl_ul.port = port2ctrl(i);
1115 ctrl = (u16*) &dc->port[i].ctrl_ul;
1116 /* D1( "sending flow control 0x%04X for port %d, %d", (u16) *ctrl, i, dc->port[i].ctrl_ul.port ); */
1117 SET_MEM( dc->port[PORT_CTRL].ul_addr[0], (u32*) ctrl, 2 );
1118 dc->port[i].update_flow_control = 0;
1119 more_flow_control_to_be_updated = 1;
1125 /* Handle donlink data, ports that are handled are modem and diagnostics */
1127 /* Return 0 - toggle fields are out of sync */
1128 static int handle_data_dl(dc_t *dc, irq_t *m, port_type_t port, u8 *toggle, u16 mask1, u16 mask2)
1130 if ( *toggle == 0 && m->read_iir & mask1 ) {
1131 if (receive_data( port, dc )) {
1133 *toggle = !(*toggle);
1136 if ( m->read_iir & mask2 ) {
1137 if (receive_data( port, dc )) {
1139 *toggle = !(*toggle);
1142 } else if ( *toggle == 1 && m->read_iir & mask2 ) {
1143 if (receive_data( port, dc )) {
1145 *toggle = !(*toggle);
1148 if ( m->read_iir & mask1 ) {
1149 if (receive_data( port, dc )) {
1151 *toggle = !(*toggle);
1155 ERR("port out of sync!, toggle:%d", *toggle);
1161 /* Handle uplink data, this is currently for the modem port */
1163 /* Return 0 - toggle field are out of sync */
1164 static int handle_data_ul(dc_t *dc, irq_t *m, port_type_t port)
1166 u8 *toggle = &(dc->port[port].toggle_ul);
1168 if ( *toggle==0 && m->read_iir & MDM_UL1 ) {
1169 SET_IER( 0, MDM_UL );
1170 if (send_data(port, dc)) {
1172 SET_IER( MDM_UL, MDM_UL);
1176 if ( m->read_iir & MDM_UL2 ) {
1177 SET_IER( 0, MDM_UL );
1178 if (send_data(port, dc)) {
1180 SET_IER( MDM_UL, MDM_UL);
1185 } else if ( *toggle==1 && m->read_iir & MDM_UL2 ) {
1186 SET_IER( 0, MDM_UL );
1187 if (send_data(port, dc)) {
1189 SET_IER( MDM_UL, MDM_UL);
1193 if ( m->read_iir & MDM_UL1 ) {
1194 SET_IER( 0, MDM_UL );
1195 if (send_data(port, dc)) {
1197 SET_IER( MDM_UL, MDM_UL);
1202 SET_FCR( m->read_iir & MDM_UL );
1203 ERR("port out of sync!");
1209 static irqreturn_t interrupt_handler(int irq, void *dev_id, struct pt_regs *regs)
1214 if (my_dev && my_dev->pdev != dev_id) {
1218 if ( !(dc = get_dc_by_pdev(dev_id)) ) {
1219 ERR("Could not find device context from pci_dev: %d", (u32) dev_id);
1223 GET_IIR( m->read_iir );
1225 #if 1 // add by Victor Yu. 05-24-2006
1226 spin_lock(&dc->spin_mutex);
1228 /* Just handle interrupt enabled in IER (by masking with dc->ier_last_written) */
1229 m->read_iir &= dc->ier_last_written;
1231 if (m->read_iir == 0) {
1232 #if 1 // add by Victor Yu. 05-24-2006
1233 spin_unlock(&dc->spin_mutex);
1238 #if 0 // mask by Victor Yu. 05-24-2006
1243 spin_lock(&dc->spin_mutex);
1246 D4( "%s irq:0x%04X, prev:0x%04X", interrupt2str(m->read_iir), m->read_iir, dc->ier_last_written);
1248 if ( m->read_iir & RESET) {
1249 if( !nozomi_read_config_table(dc) ) {
1250 SET_IER( 0, 0xFFFF);
1251 ERR("ERR: Could not read status from card, we should disable interface");
1255 goto exit_handler; /* No more useful info if this was the reset interrupt. */
1258 if ( m->read_iir & CTRL_UL ) {
1260 SET_IER( 0, CTRL_UL );
1261 if ( send_flow_control(dc) ) {
1263 SET_IER( CTRL_UL, CTRL_UL );
1266 if ( m->read_iir & CTRL_DL ) {
1267 receive_flow_control(dc, m);
1270 if ( m->read_iir & MDM_DL ) {
1271 if ( !(handle_data_dl(dc, m, PORT_MDM, &(dc->port[PORT_MDM].toggle_dl), MDM_DL1, MDM_DL2)) ) {
1272 ERR("MDM_DL out of sync!");
1276 if ( m->read_iir & MDM_UL ) {
1277 if ( !handle_data_ul(dc, m, PORT_MDM ) ) {
1278 ERR("MDM_UL out of sync!");
1282 if ( m->read_iir & DIAG_DL ) {
1283 if ( !(handle_data_dl(dc, m, PORT_DIAG, &(dc->port[PORT_DIAG].toggle_dl), DIAG_DL1, DIAG_DL2)) ) {
1284 ERR("DIAG_DL out of sync!");
1288 if ( m->read_iir & DIAG_UL ) {
1289 SET_IER( 0, DIAG_UL );
1290 if( send_data( PORT_DIAG, dc ) ) {
1292 SET_IER( DIAG_UL, DIAG_UL );
1295 if ( m->read_iir & APP1_DL ) {
1296 if (receive_data( PORT_APP1, dc ) ) {
1300 if ( m->read_iir & APP1_UL ) {
1301 SET_IER( 0, APP1_UL );
1302 if(send_data( PORT_APP1, dc )) {
1304 SET_IER( APP1_UL, APP1_UL );
1307 if ( m->read_iir & APP2_DL ) {
1308 if (receive_data( PORT_APP2, dc )) {
1312 if ( m->read_iir & APP2_UL ) {
1313 SET_IER( 0, APP2_UL );
1314 if(send_data( PORT_APP2, dc )) {
1316 SET_IER( APP2_UL, APP2_UL );
1321 spin_unlock(&dc->spin_mutex);
1325 /* Request a shared IRQ from system */
1326 static int nozomi_setup_interrupt(struct pci_dev *pdev)
1330 if ( (rval = request_irq( pdev->irq, &interrupt_handler, SA_SHIRQ, NOZOMI_NAME, pdev )) ) {
1331 ERR("Cannot open because IRQ %d is already in use.", pdev->irq );
1337 static void nozomi_get_card_type(dc_t *dc)
1341 for( i=0; i<6; i++ ) {
1342 size += pci_resource_len(dc->pdev, i);
1345 /* Assume card type F32_8 if no match */
1346 dc->card_type = size == 2048 ? F32_2 : F32_8;
1348 INFO("Card type is: %d", dc->card_type );
1351 void nozomi_setup_private_data(dc_t *dc)
1353 u32 offset = dc->base_addr + dc->card_type/2;
1356 dc->REG_FCR = offset + R_FCR;
1357 dc->REG_IIR = offset + R_IIR;
1358 dc->REG_IER = offset + R_IER;
1359 dc->ier_last_written = 0;
1362 dc->port[PORT_MDM ].token_dl = MDM_DL;
1363 dc->port[PORT_DIAG].token_dl = DIAG_DL;
1364 dc->port[PORT_APP1].token_dl = APP1_DL;
1365 dc->port[PORT_APP2].token_dl = APP2_DL;
1367 for( i=PORT_MDM; i<MAX_PORT; i++ ) {
1368 dc->port[i].rx_data = dc->port[i].tx_data = 0;
1369 dc->port[i].tty_dont_flip = 0;
1374 static void tty_flip_queue_function(void *tmp_dc)
1376 dc_t *dc = (dc_t*) tmp_dc;
1378 #if 1 // add by Victor Yu. 05-24-2006
1382 /* Enable interrupt for that port */
1383 for(i=0;i<MAX_PORT;i++) {
1384 if (dc->port[i].tty_dont_flip) {
1385 D6("Enable for port: %d", i);
1386 dc->port[i].tty_dont_flip = 0;
1387 #if 1 // add by Victor Yu. 05-24-2006
1388 spin_lock_irqsave(&dc->spin_mutex, flags);
1390 enable_transmit_dl(dc->port[i].tty_index, dc);
1391 #if 1 // add by Victor Yu. 05-24-2006
1392 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1398 static int read_proc_card_type(char *buf, char **start, off_t offset, int len)
1400 dc_t *dc = get_dc_by_index(0);
1403 len += sprintf(buf+len,"%d\n", dc->card_type);
1407 static int read_proc_open_ttys(char *buf, char **start, off_t offset, int len)
1409 dc_t *dc = get_dc_by_index(0);
1412 len += sprintf(buf+len,"%d\n", dc->open_ttys);
1416 static int read_proc_version(char *buf, char **start, off_t offset, int len)
1419 len += sprintf(buf+len, "%s\n", VERSION_STRING);
1423 static int read_proc_rtx(char *buf, char **start, off_t offset, int len)
1425 dc_t *dc = get_dc_by_index(0);
1429 for(i=PORT_MDM;i<MAX_PORT;i++) {
1430 len += sprintf(buf+len,"noz%d rx: %d, tx: %d\n", i, dc->port[i].rx_data, dc->port[i].tx_data);
1435 static void make_proc_dirs(void)
1437 dc_t *dc = get_dc_by_index(0);
1439 dc->proc_entry = proc_mkdir("nozomi", &proc_root);
1440 /* Register the read_proc */
1441 if ( !create_proc_info_entry("card_type",0,dc->proc_entry,read_proc_card_type) ) {
1442 ERR("ERROR: failed to register read_procmem");
1444 if ( !create_proc_info_entry("open_ttys",0,dc->proc_entry,read_proc_open_ttys) ) {
1445 ERR("ERROR: failed to register read_procmem");
1447 if ( !create_proc_info_entry("rtx",0,dc->proc_entry,read_proc_rtx) ) {
1448 ERR("ERROR: failed to register read_procmem");
1450 if ( !create_proc_info_entry("version",0,dc->proc_entry,read_proc_version) ) {
1451 ERR("ERROR: failed to register read_procmem");
1455 static void remove_proc_dirs(void)
1457 dc_t *dc = get_dc_by_index(0);
1459 remove_proc_entry("card_type", dc->proc_entry);
1460 remove_proc_entry("open_ttys", dc->proc_entry);
1461 remove_proc_entry("rtx", dc->proc_entry);
1462 remove_proc_entry("version", dc->proc_entry);
1463 remove_proc_entry("nozomi", &proc_root);
1466 /* Allocate memory for one device */
1467 static int __devinit nozomi_card_init(struct pci_dev *pdev, const struct pci_device_id *ent)
1473 INFO("Init, cards_found: %d", cards_found);
1475 if (!(my_dev = kmalloc(sizeof(dc_t), GFP_KERNEL))) {
1476 D1( "Could not allocate memory");
1480 memset(my_dev, 0, sizeof( dc_t ));
1482 if (cards_found > 1) {
1483 ERR("This driver only supports 1 device");
1487 my_dev->pdev = pdev;
1490 /* Find out what card type it is */
1491 nozomi_get_card_type(dc);
1493 if (pci_enable_device(dc->pdev)) {
1494 ERR("Not possible to enable PCI Device");
1498 if ( (dc->base_addr = pci_resource_start(dc->pdev, 0)) == 0x0000) {
1499 ERR("No I/O-Address for card detected");
1501 goto err_disable_device;
1504 if (!(dc->base_addr = (u32) ioremap(dc->base_addr, dc->card_type))) {
1505 ERR("No I/O-Address for card detected");
1507 goto err_disable_device;
1512 nozomi_setup_private_data(dc);
1514 if (pci_request_regions(dc->pdev, NOZOMI_NAME)) {
1515 ERR("I/O address 0x%04x already in use",
1516 (int) /* nozomi_private.io_addr */ 0);
1518 goto err_disable_regions;
1521 if ( !(dc->send_buf = kmalloc(SEND_BUF_MAX, GFP_KERNEL))) {
1522 ERR("Could not allocate send buffer?");
1523 goto err_disable_regions;
1526 /* Disable all interrupts */
1527 SET_IER( 0 , 0xFFFF );
1529 /* Setup interrupt handler */
1530 if (nozomi_setup_interrupt(dc->pdev)) {
1532 goto err_disable_regions;
1535 D1( "base_addr: 0x%08X", dc->base_addr);
1538 dc->tty_flip_queue.list.next = NULL;
1539 dc->tty_flip_queue.list.prev = NULL;
1540 dc->tty_flip_queue.sync = 0;
1541 dc->tty_flip_queue.data = dc;
1542 dc->tty_flip_queue.routine = tty_flip_queue_function;
1545 if ( !(dc->tty_flip_wq = create_singlethread_workqueue(NOZOMI_NAME )) ) {
1546 ERR("Could not create workqueue?");
1547 BUG_ON(!dc->tty_flip_wq);
1550 INIT_WORK( &dc->tty_flip_wq_struct, tty_flip_queue_function, (void*) dc);
1553 spin_lock_init(&dc->spin_mutex);
1557 /* Enable RESET interrupt. */
1558 SET_IER( RESET, 0xFFFF );
1561 err_disable_regions:
1562 pci_release_regions(pdev);
1563 iounmap((void *)dc->base_addr );
1567 pci_disable_device(pdev);
1574 static void tty_do_close(dc_t *dc, port_t *port)
1576 #if 1 // add by Victor Yu. 05-24-2006
1579 down(&port->tty_sem);
1581 if ( !port->tty_open_count ) {
1586 port->tty_open_count--;
1591 if ( port->tty_open_count == 0) {
1592 D1("close: %d", port->token_dl );
1593 #if 1 // add by Victor Yu. 05-24-2006
1594 spin_lock_irqsave(&dc->spin_mutex, flags);
1596 SET_IER( 0, port->token_dl );
1597 #if 1 // add by Victor Yu. 05-24-2006
1598 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1606 static void __exit tty_exit(void) {
1612 for (i = 0; i < NTTY_TTY_MINORS; ++i)
1613 tty_unregister_device(&dc->tty_driver, i);
1616 tty_unregister_driver(&dc->tty_driver);
1618 for (i = 0; i < NTTY_TTY_MINORS; i++) {
1619 while (dc->port[i].tty_open_count) {
1620 tty_do_close(dc, &dc->port[i]);
1622 dc->port[i].tty = NULL;
1626 /* Deallocate memory for one device */
1627 static void __devexit nozomi_card_exit(struct pci_dev *pdev)
1631 dc_t *dc = get_dc_by_pdev(pdev);
1633 /* Disable all interrupts */
1634 SET_IER( 0, 0xFFFF);
1636 /* Send 0x0001, command card to resend the reset token. */
1637 /* This is to get the reset when the module is reloaded. */
1638 ctrl.port = 0x00; ctrl.reserved = 0; ctrl.RTS=0; ctrl.DTR=1;
1639 D1( "sending flow control 0x%04X", * ((u16*) &ctrl) );
1641 /* Setup dc->reg addresses to we can use defines here */
1642 nozomi_setup_private_data(dc);
1643 SET_MEM( dc->port[PORT_CTRL].ul_addr[0], (u32*) &ctrl, 2 );
1644 SET_FCR( CTRL_UL ); /* push the token to the card. */
1646 D1( "pci_release_regions");
1647 pci_release_regions(pdev);
1650 iounmap((void *)dc->base_addr);
1652 D1( "pci_disable_device");
1653 pci_disable_device(pdev);
1655 free_irq( pdev->irq, pdev );
1657 for (i=PORT_MDM; i< MAX_PORT;i++) {
1658 kfree ( dc->port[i].fifo_ul );
1661 kfree( dc->send_buf );
1668 destroy_workqueue(dc->tty_flip_wq);
1678 static void set_rts(int index, int rts)
1680 dc_t *dc = get_dc_by_index(index);
1682 dc->port[index].ctrl_ul.RTS = rts;
1683 dc->port[index].update_flow_control = 1;
1684 enable_transmit_ul(PORT_CTRL, dc);
1687 static void set_dtr(int index, int dtr)
1689 dc_t *dc = get_dc_by_index(index);
1691 D1("SETTING DTR index: %d, dtr: %d", index, dtr);
1692 dc->port[index].ctrl_ul.DTR = dtr;
1693 dc->port[index].update_flow_control = 1;
1694 enable_transmit_ul(PORT_CTRL, dc);
1698 /*------------------------------------------------------------------------------------------
1700 -----------------------------------------------------------------------------------------*/
1703 /* Called when the userspace process opens the tty, /dev/noz*. */
1704 static int ntty_open(struct tty_struct *tty, struct file *file)
1706 #if 1 // add by Victor Yu. 05-24-2006
1709 s32 index = get_index(tty);
1710 port_t *port = get_port_by_tty(tty);
1711 dc_t *dc = get_dc_by_tty(tty);
1713 down(&port->tty_sem);
1715 tty->low_latency = 1;
1716 tty->driver_data = port;
1718 port->tty_index = index;
1720 port->tty_open_count++;
1727 /* Enable interrupt downlink for channel */
1728 if ( port->tty_open_count == 1) {
1729 port->rx_data = port->tx_data = 0;
1730 D1("open: %d", port->token_dl );
1731 #if 1 // add by Victor Yu. 05-24-2006
1732 spin_lock_irqsave(&dc->spin_mutex, flags);
1734 SET_IER( port->token_dl, port->token_dl );
1735 #if 1 // add by Victor Yu. 05-24-2006
1736 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1745 /* Called when the userspace process close the tty, /dev/noz*. */
1746 static void ntty_close(struct tty_struct *tty, struct file *file)
1748 dc_t *dc = get_dc_by_tty(tty);
1750 tty_do_close(dc, (port_t *) tty->driver_data);
1753 /* called when the userspace process writes to the tty (/dev/noz*). */
1754 /* Data is inserted into a fifo, which is then read and transfered to the modem. */
1756 static int ntty_write(struct tty_struct *tty, const unsigned char *buffer, int count)
1758 static s32 ntty_write(struct tty_struct *tty, s32 from_user, const u8 *buffer, s32 count)
1761 #if 1 // add by Victor Yu. 05-24-2006
1765 dc_t *dc = get_dc_by_tty(tty);
1766 port_t *port = (port_t *) tty->driver_data;
1768 /* D1( "WRITEx: %d, index = %d", count, index); */
1774 down(&port->tty_sem);
1776 if (! port->tty_open_count) {
1783 rval = __kfifo_put_user(port->fifo_ul, (unsigned char *) buffer, count);
1785 rval = __kfifo_put(port->fifo_ul, (unsigned char *) buffer, count);
1788 rval = __kfifo_put(port->fifo_ul, (unsigned char *) buffer, count);
1793 D1( "No device context?");
1797 // CTS is only valid on the modem channel
1798 if ( port == &(dc->port[PORT_MDM]) ) {
1799 if ( port->ctrl_dl.CTS ) {
1800 D4( "Enable interrupt");
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, dc );
1805 #if 1 // add by Victor Yu. 05-24-2006
1806 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1809 ERR("CTS not active on modem port?");
1812 #if 1 // add by Victor Yu. 05-24-2006
1813 spin_lock_irqsave(&dc->spin_mutex, flags);
1815 enable_transmit_ul(port->tty_index, dc );
1816 #if 1 // add by Victor Yu. 05-24-2006
1817 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1826 /* Calculate how much is left in device */
1827 /* This method is called by the upper tty layer. */
1828 /* #according to sources N_TTY.c it expects a value >= 0 and does not check for negative values. */
1829 static int ntty_write_room(struct tty_struct *tty)
1831 port_t *port = (port_t *) tty->driver_data;
1838 down(&port->tty_sem);
1840 if (! port->tty_open_count) {
1844 room = port->fifo_ul->size - __kfifo_len(port->fifo_ul);
1851 /* Sets termios flags, called by the tty layer. */
1852 static void ntty_set_termios(struct tty_struct *tty, struct termios *old_termios)
1856 cflag = tty->termios->c_cflag;
1858 if ((cflag == old_termios->c_cflag) &&
1859 (RELEVANT_IFLAG(tty->termios->c_iflag) == RELEVANT_IFLAG(old_termios->c_iflag))) {
1860 D1( " - nothing to change...");
1865 /* get the byte size */
1866 switch (cflag & CSIZE) {
1868 D1( " - data bits = 5");
1871 D1( " - data bits = 6");
1874 D1( " - data bits = 7");
1878 D1( " - data bits = 8");
1882 /* determine the parity */
1883 if (cflag & PARENB) {
1884 if (cflag & PARODD) {
1885 D1( " - parity = odd");
1887 D1( " - parity = even");
1890 D1( " - parity = none");
1893 /* figure out the stop bits requested */
1894 if (cflag & CSTOPB) {
1895 D1( " - stop bits = 2");
1897 D1( " - stop bits = 1");
1900 /* figure out the hardware flow control settings */
1901 if (cflag & CRTSCTS) {
1902 D1( " - RTS/CTS is enabled");
1904 D1( " - RTS/CTS is disabled");
1907 /* determine software flow control */
1908 /* if we are implementing XON/XOFF, set the start and
1909 * stop character in the device */
1910 if (I_IXOFF(tty) || I_IXON(tty)) {
1912 unsigned char stop_char = STOP_CHAR(tty);
1913 unsigned char start_char = START_CHAR(tty);
1915 /* if we are implementing INBOUND XON/XOFF */
1917 D1( " - INBOUND XON/XOFF is enabled, "
1918 "XON = %2x, XOFF = %2x", start_char, stop_char);
1920 D1( " - INBOUND XON/XOFF is disabled");
1923 /* if we are implementing OUTBOUND XON/XOFF */
1925 D1( " - OUTBOUND XON/XOFF is enabled, "
1926 "XON = %2x, XOFF = %2x", start_char, stop_char);
1928 D1( " - OUTBOUND XON/XOFF is disabled");
1936 /* Gets io control parameters */
1937 static int ntty_tiocmget(struct tty_struct *tty, struct file *file)
1939 port_t *port = (port_t *) tty->driver_data;
1940 ctrl_dl_t *ctrl_dl = &port->ctrl_dl;
1941 ctrl_ul_t *ctrl_ul = &port->ctrl_ul;
1944 | (ctrl_ul->RTS ? TIOCM_RTS : 0)
1945 | (ctrl_ul->DTR ? TIOCM_DTR : 0)
1946 | (ctrl_dl->DCD ? TIOCM_CAR : 0)
1947 | (ctrl_dl->RI ? TIOCM_RNG : 0)
1948 | (ctrl_dl->DSR ? TIOCM_DSR : 0)
1949 | (ctrl_dl->CTS ? TIOCM_CTS : 0);
1952 /* Sets io controls parameters */
1953 static int ntty_tiocmset(struct tty_struct *tty, struct file *file, u32 arg)
1955 #if 1 // add by Victor Yu. 05-24-2006
1957 dc_t *dc = get_dc_by_tty(tty);
1959 port_t *port = (port_t *) tty->driver_data;
1961 #if 1 // add by Victor Yu. 05-24-2006
1962 spin_lock_irqsave(&dc->spin_mutex, flags);
1964 set_rts(port->tty_index, (arg & TIOCM_RTS) ? 1 : 0);
1965 set_dtr(port->tty_index, (arg & TIOCM_DTR) ? 1 : 0);
1966 #if 1 // add by Victor Yu. 05-24-2006
1967 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1973 static int ntty_ioctl_tiocmiwait(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
1975 port_t *port = (port_t *) tty->driver_data;
1977 if (cmd == TIOCMIWAIT) {
1978 DECLARE_WAITQUEUE(wait, current);
1979 struct async_icount cnow;
1980 struct async_icount cprev;
1982 cprev = port->tty_icount;
1984 add_wait_queue(&port->tty_wait, &wait);
1985 set_current_state(TASK_INTERRUPTIBLE);
1987 remove_wait_queue(&port->tty_wait, &wait);
1989 /* see if a signal woke us up */
1990 if (signal_pending(current))
1991 return -ERESTARTSYS;
1993 cnow = port->tty_icount;
1994 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
1995 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1996 return -EIO; /* no change => error */
1997 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1998 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1999 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
2000 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
2007 return -ENOIOCTLCMD;
2010 static int ntty_ioctl_tiocgicount(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
2012 port_t *port = (port_t *) tty->driver_data;
2014 if (cmd == TIOCGICOUNT) {
2015 struct async_icount cnow = port->tty_icount;
2016 struct serial_icounter_struct icount;
2018 icount.cts = cnow.cts;
2019 icount.dsr = cnow.dsr;
2020 icount.rng = cnow.rng;
2021 icount.dcd = cnow.dcd;
2022 icount.rx = cnow.rx;
2023 icount.tx = cnow.tx;
2024 icount.frame = cnow.frame;
2025 icount.overrun = cnow.overrun;
2026 icount.parity = cnow.parity;
2027 icount.brk = cnow.brk;
2028 icount.buf_overrun = cnow.buf_overrun;
2030 if (copy_to_user((void *)arg, &icount, sizeof(icount)))
2034 return -ENOIOCTLCMD;
2037 static int ntty_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
2039 #if 1 // add by Victor Yu. 05-24-2006
2041 dc_t *dc = get_dc_by_tty(tty);
2043 port_t *port = (port_t *) tty->driver_data;
2045 int rval = -ENOIOCTLCMD;
2047 D1("******** IOCTL, cmd: %d", cmd);
2049 #if 0 // mask by Victor Yu. 05-25-2006
2051 D1( "IOCTL TCGETS ...");
2052 rval = -ENOIOCTLCMD;
2055 D1( "IOCTL TCSETS ...");
2056 rval = -ENOIOCTLCMD;
2060 rval = ntty_ioctl_tiocmiwait(tty, file, cmd, arg);
2063 rval = ntty_ioctl_tiocgicount(tty, file, cmd, arg);
2066 rval = ntty_tiocmget(tty, file);
2069 rval = ntty_tiocmset(tty, file, arg);
2072 if (get_user(mask, (unsigned long *) arg))
2075 #if 1 // add by Victor Yu. 05-24-2006
2076 spin_lock_irqsave(&dc->spin_mutex, flags);
2078 if (mask & TIOCM_RTS)
2079 set_rts(port->tty_index, 0);
2080 if (mask & TIOCM_DTR)
2081 set_dtr(port->tty_index, 0);
2082 #if 1 // add by Victor Yu. 05-24-2006
2083 spin_unlock_irqrestore(&dc->spin_mutex, flags);
2088 if (get_user(mask, (unsigned long *) arg))
2091 #if 1 // add by Victor Yu. 05-24-2006
2092 spin_lock_irqsave(&dc->spin_mutex, flags);
2094 if (mask & TIOCM_RTS)
2095 set_rts(port->tty_index, 1);
2096 if (mask & TIOCM_DTR)
2097 set_dtr(port->tty_index, 1);
2098 #if 1 // add by Victor Yu. 05-24-2006
2099 spin_unlock_irqrestore(&dc->spin_mutex, flags);
2103 #if 0 // mask by Victor Yu.
2105 D1( "IOCTL TCFLSH ...");
2106 rval = -ENOIOCTLCMD;
2110 D1( "ERR: 0x%08X, %d", cmd, cmd);
2118 /* Called by the upper tty layer when tty buffers are ready */
2119 /* to receive data again after a call to throttle. */
2120 static void ntty_unthrottle(struct tty_struct *tty)
2122 port_t *port = (port_t *) tty->driver_data;
2123 dc_t *dc = get_dc_by_tty(tty);
2127 spin_lock_irqsave(&dc->spin_mutex, flags);
2128 enable_transmit_dl(port->tty_index, dc);
2129 set_rts(port->tty_index, 1);
2131 spin_unlock_irqrestore(&dc->spin_mutex, flags);
2134 /* Called by the upper tty layer when the tty buffers are almost full. */
2135 /* The driver should stop send more data. */
2136 static void ntty_throttle(struct tty_struct *tty)
2138 port_t *port = (port_t *) tty->driver_data;
2139 dc_t *dc = get_dc_by_tty(tty);
2143 spin_lock_irqsave(&dc->spin_mutex, flags);
2144 set_rts(port->tty_index, 0);
2145 spin_unlock_irqrestore(&dc->spin_mutex, flags);
2148 static void ntty_put_char(struct tty_struct *tty, unsigned char c)
2150 D2("PUT CHAR Function: %c", c);
2153 /* Returns number of chars in buffer, called by tty layer */
2154 static s32 ntty_chars_in_buffer(struct tty_struct *tty)
2156 port_t *port = (port_t *) tty->driver_data;
2161 goto exit_in_buffer;
2164 if (! port->tty_open_count) {
2165 ERR("No tty open?");
2167 goto exit_in_buffer;
2170 rval = __kfifo_len(port->fifo_ul);
2176 /* Initializes the tty */
2177 static int ntty_tty_init(dc_t *dc)
2179 struct tty_driver *td = &dc->tty_driver;
2183 memset(td, 0, sizeof(struct tty_driver));
2185 td->magic = TTY_DRIVER_MAGIC;
2186 td->driver_name = NOZOMI_NAME_TTY;
2188 td->major = NTTY_TTY_MAJOR,
2189 td->type = TTY_DRIVER_TYPE_SERIAL,
2190 td->subtype = SERIAL_TYPE_NORMAL,
2191 td->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS,
2192 td->init_termios = tty_std_termios;
2193 td->init_termios.c_cflag = B115200 | CS8 | CREAD | HUPCL | CLOCAL;
2197 td->minor_start = 0;
2200 td->table = dc->tty_table;
2201 td->refcount = &dc->tty_refcount;
2204 td->termios = dc->tty_termios;
2205 td->termios_locked = dc->tty_termios_locked;
2207 td->ioctl = ntty_ioctl;
2208 td->open = ntty_open;
2209 td->close = ntty_close;
2210 td->write = ntty_write;
2211 td->write_room = ntty_write_room;
2212 td->unthrottle = ntty_unthrottle;
2213 td->throttle = ntty_throttle;
2214 td->set_termios = ntty_set_termios;
2215 td->chars_in_buffer = ntty_chars_in_buffer;
2216 td->put_char = ntty_put_char;
2218 rval = tty_register_driver(td);
2221 printk(KERN_ERR "failed to register ntty tty driver");
2222 D1( "failed to register ntty tty driver");
2226 for (i = 0; i < NTTY_TTY_MINORS; i++) {
2227 init_MUTEX(&dc->port[i].tty_sem);
2228 dc->port[i].tty_open_count = 0;
2229 dc->port[i].tty = NULL;
2232 tty_register_device(td, i, NULL);
2236 printk(KERN_INFO DRIVER_DESC " " NOZOMI_NAME_TTY);
2241 /* Module initialization */
2242 static struct pci_driver nozomi_driver = {
2243 .name = NOZOMI_NAME,
2244 .id_table = nozomi_pci_tbl,
2245 .probe = nozomi_card_init,
2246 .remove = __devexit_p(nozomi_card_exit),
2249 static int __init nozomi_init(void)
2253 rval = pci_module_init(&nozomi_driver);
2254 printk(KERN_INFO "Initializing %s\n", VERSION_STRING);
2258 static void nozomi_exit(void)
2260 printk(KERN_INFO "Unloading %s", DRIVER_DESC);
2261 pci_unregister_driver(&nozomi_driver);
2264 module_init(nozomi_init);
2265 module_exit(nozomi_exit);
2268 MODULE_PARM(nzdebug, "i");
2271 MODULE_LICENSE("Dual BSD/GPL");
2272 MODULE_DESCRIPTION( DRIVER_DESC );