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)
11 * Locking code changes for Vodafone by Sphere Systems Ltd,
12 * Andrew Bird (ajb@spheresystems.co.uk )
15 * Source has been ported from an implementation made by Filip Aben @ Option
17 * --------------------------------------------------------------------------
19 * Copyright (c) 2005,2006 Option Wireless Sweden AB
20 * Copyright (c) 2006 Sphere Systems Ltd
21 * Copyright (c) 2006 Option Wireless n/v
22 * All rights Reserved.
24 * This program is free software; you can redistribute it and/or modify
25 * it under the terms of the GNU General Public License as published by
26 * the Free Software Foundation; either version 2 of the License, or
27 * (at your option) any later version.
29 * This program is distributed in the hope that it will be useful,
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 * GNU General Public License for more details.
34 * You should have received a copy of the GNU General Public License
35 * along with this program; if not, write to the Free Software
36 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
38 * --------------------------------------------------------------------------
44 * 11-November-2007 Jiri Slaby, Frank Seidel
45 * - Big rework of multicard support by Jiri
46 * - Major cleanups (semaphore to mutex, endianess, no major reservation)
50 * 30-October-2007 Frank Seidel
51 * - Completed multicard support
55 * 07-August-2007 Frank Seidel
57 * - theoretical multicard support
60 * 03-July-2006 Paul Hardwick
62 * - Stability Improvements. Incorporated spinlock wraps patch.
63 * - Updated for newer 2.6.14+ kernels (tty_buffer_request_room)
64 * - using __devexit macro for tty
68 * 08-feb-2006 15:34:10:Ulf
70 * -Fixed issue when not waking up line disipine layer, could probably result
71 * in better uplink performance for 2.4.
73 * -Fixed issue with big endian during initalization, now proper toggle flags
74 * are handled between preloader and maincode.
76 * -Fixed flow control issue.
78 * -Added support for setting DTR.
80 * -For 2.4 kernels, removing temporary buffer that's not needed.
82 * -Reading CTS only for modem port (only port that supports it).
84 * -Return 0 in write_room instead of netative value, it's not handled in
87 * --------------------------------------------------------------------------
90 * First version of driver, only tested with card of type F32_2.
91 * Works fine with 2.4 and 2.6 kernels.
92 * Driver also support big endian architecture.
95 /* Enable this to have a lot of debug printouts */
98 #include <linux/kernel.h>
99 #include <linux/module.h>
100 #include <linux/pci.h>
101 #include <linux/ioport.h>
102 #include <linux/tty.h>
103 #include <linux/tty_driver.h>
104 #include <linux/tty_flip.h>
105 #include <linux/serial.h>
106 #include <linux/interrupt.h>
107 #include <linux/kmod.h>
108 #include <linux/init.h>
109 #include <linux/kfifo.h>
110 #include <linux/uaccess.h>
111 #include <asm/byteorder.h>
113 #include <linux/delay.h>
116 #define VERSION_STRING DRIVER_DESC " 2.1d (build date: " \
117 __DATE__ " " __TIME__ ")"
119 /* Macros definitions */
121 /* Default debug printout level */
122 #define NOZOMI_DEBUG_LEVEL 0x00
124 #define P_BUF_SIZE 128
125 #define NFO(_err_flag_, args...) \
127 char tmp[P_BUF_SIZE]; \
128 snprintf(tmp, sizeof(tmp), ##args); \
129 printk(_err_flag_ "[%d] %s(): %s\n", __LINE__, \
130 __FUNCTION__, tmp); \
133 #define DBG1(args...) D_(0x01, ##args)
134 #define DBG2(args...) D_(0x02, ##args)
135 #define DBG3(args...) D_(0x04, ##args)
136 #define DBG4(args...) D_(0x08, ##args)
137 #define DBG5(args...) D_(0x10, ##args)
138 #define DBG6(args...) D_(0x20, ##args)
139 #define DBG7(args...) D_(0x40, ##args)
140 #define DBG8(args...) D_(0x80, ##args)
143 /* Do we need this settable at runtime? */
144 static int debug
= NOZOMI_DEBUG_LEVEL
;
146 #define D(lvl, args...) do {if (lvl & debug) NFO(KERN_DEBUG, ##args); } \
148 #define D_(lvl, args...) D(lvl, ##args)
150 /* These printouts are always printed */
154 #define D_(lvl, args...)
157 /* TODO: rewrite to optimize macros... */
159 #define TMP_BUF_MAX 256
161 #define DUMP(buf__,len__) \
163 char tbuf[TMP_BUF_MAX] = {0};\
165 snprintf(tbuf, len__ > TMP_BUF_MAX ? TMP_BUF_MAX : len__, "%s", buf__);\
166 if (tbuf[len__-2] == '\r') {\
167 tbuf[len__-2] = 'r';\
169 DBG1("SENDING: '%s' (%d+n)", tbuf, len__);\
171 DBG1("SENDING: '%s' (%d)", tbuf, len__);\
176 #define NOZOMI_NAME "nozomi"
177 #define NOZOMI_NAME_TTY "nozomi_tty"
178 #define DRIVER_DESC "Nozomi driver"
180 #define NTTY_TTY_MAXMINORS 256
181 #define NTTY_FIFO_BUFFER_SIZE 8192
183 /* Must be power of 2 */
184 #define FIFO_BUFFER_SIZE_UL 8192
186 /* Size of tmp send buffer to card */
187 #define SEND_BUF_MAX 1024
188 #define RECEIVE_BUF_MAX 4
191 /* Define all types of vendors and devices to support */
192 #define VENDOR1 0x1931 /* Vendor Option */
193 #define DEVICE1 0x000c /* HSDPA card */
195 #define R_IIR 0x0000 /* Interrupt Identity Register */
196 #define R_FCR 0x0000 /* Flow Control Register */
197 #define R_IER 0x0004 /* Interrupt Enable Register */
199 #define CONFIG_MAGIC 0xEFEFFEFE
200 #define TOGGLE_VALID 0x0000
202 /* Definition of interrupt tokens */
203 #define MDM_DL1 0x0001
204 #define MDM_UL1 0x0002
205 #define MDM_DL2 0x0004
206 #define MDM_UL2 0x0008
207 #define DIAG_DL1 0x0010
208 #define DIAG_DL2 0x0020
209 #define DIAG_UL 0x0040
210 #define APP1_DL 0x0080
211 #define APP1_UL 0x0100
212 #define APP2_DL 0x0200
213 #define APP2_UL 0x0400
214 #define CTRL_DL 0x0800
215 #define CTRL_UL 0x1000
218 #define MDM_DL (MDM_DL1 | MDM_DL2)
219 #define MDM_UL (MDM_UL1 | MDM_UL2)
220 #define DIAG_DL (DIAG_DL1 | DIAG_DL2)
222 /* modem signal definition */
223 #define CTRL_DSR 0x0001
224 #define CTRL_DCD 0x0002
225 #define CTRL_RI 0x0004
226 #define CTRL_CTS 0x0008
228 #define CTRL_DTR 0x0001
229 #define CTRL_RTS 0x0002
232 #define NOZOMI_MAX_PORTS 5
233 #define NOZOMI_MAX_CARDS (NTTY_TTY_MAXMINORS / MAX_PORT)
235 /* Type definitions */
238 * There are two types of nozomi cards,
239 * one with 2048 memory and with 8192 memory
242 F32_2
= 2048, /* 512 bytes downlink + uplink * 2 -> 2048 */
243 F32_8
= 8192, /* 3072 bytes downl. + 1024 bytes uplink * 2 -> 8192 */
246 /* Two different toggle channels exist */
252 /* Port definition for the card regarding flow control */
253 enum ctrl_port_type
{
262 /* Ports that the nozomi has */
276 unsigned enabled
:5; /*
277 * Toggle fields are valid if enabled is 0,
278 * else A-channels must always be used.
283 } __attribute__ ((packed
));
285 /* Configuration table to read at startup of card */
286 /* Is for now only needed during initialization phase */
287 struct config_table
{
289 u16 product_information
;
292 struct toggles toggle
;
295 * If this is 64, it can hold
296 * 60 bytes + 4 that is length field
302 * If this is 64, it can hold
303 * 60 bytes + 4 that is length field
318 } __attribute__ ((packed
));
320 /* This stores all control downlink flags */
328 } __attribute__ ((packed
));
330 /* This stores all control uplink flags */
336 } __attribute__ ((packed
));
341 /* This represents the toggle information */
346 unsigned enabled
:5; /*
347 * Toggle fields are valid if enabled is 0,
348 * else A-channels must always be used.
350 } __attribute__ ((packed
));
352 /* Configuration table to read at startup of card */
353 struct config_table
{
356 u16 product_information
;
357 struct toggles toggle
;
361 * If this is 64, it can hold
362 * 60 bytes + 4 that is length field
378 } __attribute__ ((packed
));
380 /* This stores all control downlink flags */
386 unsigned reserverd
:4;
388 } __attribute__ ((packed
));
390 /* This stores all control uplink flags */
396 } __attribute__ ((packed
));
399 /* This holds all information that is needed regarding a port */
401 u8 update_flow_control
;
402 struct ctrl_ul ctrl_ul
;
403 struct ctrl_dl ctrl_dl
;
404 struct kfifo
*fifo_ul
;
405 void __iomem
*dl_addr
[2];
408 void __iomem
*ul_addr
[2];
413 struct tty_struct
*tty
;
415 /* mutex to ensure one access patch to this port */
416 struct mutex tty_sem
;
417 wait_queue_head_t tty_wait
;
418 struct async_icount tty_icount
;
421 /* Private data one for each card in the system */
423 void __iomem
*base_addr
;
426 /* Pointers to registers */
427 void __iomem
*reg_iir
;
428 void __iomem
*reg_fcr
;
429 void __iomem
*reg_ier
;
432 enum card_type card_type
;
433 struct config_table config_table
; /* Configuration table */
434 struct pci_dev
*pdev
;
435 struct port port
[NOZOMI_MAX_PORTS
];
438 spinlock_t spin_mutex
; /* secures access to registers and tty */
440 unsigned int index_start
;
444 /* This is a data packet that is read or written to/from card */
446 u32 size
; /* size is the length of the data buffer */
448 } __attribute__ ((packed
));
450 /* Global variables */
451 static struct pci_device_id nozomi_pci_tbl
[] = {
452 {PCI_DEVICE(VENDOR1
, DEVICE1
)},
456 MODULE_DEVICE_TABLE(pci
, nozomi_pci_tbl
);
458 static struct nozomi
*ndevs
[NOZOMI_MAX_CARDS
];
459 static struct tty_driver
*ntty_driver
;
462 * find card by tty_index
464 static inline struct nozomi
*get_dc_by_tty(const struct tty_struct
*tty
)
466 return tty
? ndevs
[tty
->index
/ MAX_PORT
] : NULL
;
469 static inline struct port
*get_port_by_tty(const struct tty_struct
*tty
)
471 struct nozomi
*ndev
= get_dc_by_tty(tty
);
472 return ndev
? &ndev
->port
[tty
->index
% MAX_PORT
] : NULL
;
481 static void read_mem32(u32
*buf
, const void __iomem
*mem_addr_start
,
485 const u32
*ptr
= (__force u32
*) mem_addr_start
;
488 if (unlikely(!ptr
|| !buf
))
491 /* shortcut for extremely often used cases */
492 switch (size_bytes
) {
493 case 2: /* 2 bytes */
495 *buf16
= __le16_to_cpu(readw((void __iomem
*)ptr
));
498 case 4: /* 4 bytes */
499 *(buf
) = __le32_to_cpu(readl((void __iomem
*)ptr
));
504 while (i
< size_bytes
) {
505 if (size_bytes
- i
== 2) {
506 /* Handle 2 bytes in the end */
508 *(buf16
) = __le16_to_cpu(readw((void __iomem
*)ptr
));
512 *(buf
) = __le32_to_cpu(readl((void __iomem
*)ptr
));
527 static u32
write_mem32(void __iomem
*mem_addr_start
, u32
*buf
,
531 u32
*ptr
= (__force u32
*) mem_addr_start
;
534 if (unlikely(!ptr
|| !buf
))
537 /* shortcut for extremely often used cases */
538 switch (size_bytes
) {
539 case 2: /* 2 bytes */
541 writew(__cpu_to_le16(*buf16
), (void __iomem
*)ptr
);
545 * also needs to write 4 bytes in this case
546 * so falling through..
548 case 4: /* 4 bytes */
549 writel(__cpu_to_le32(*buf
), (void __iomem
*)ptr
);
554 while (i
< size_bytes
) {
555 if (size_bytes
- i
== 2) {
558 writew(__cpu_to_le16(*buf16
), (void __iomem
*)ptr
);
562 writel(__cpu_to_le32(*buf
), (void __iomem
*)ptr
);
571 /* Setup pointers to different channels and also setup buffer sizes. */
572 static void setup_memory(struct nozomi
*dc
)
574 void __iomem
*offset
= dc
->base_addr
+ dc
->config_table
.dl_start
;
575 /* The length reported is including the length field of 4 bytes,
576 * hence subtract with 4.
578 const u16 buff_offset
= 4;
580 /* Modem port dl configuration */
581 dc
->port
[PORT_MDM
].dl_addr
[CH_A
] = offset
;
582 dc
->port
[PORT_MDM
].dl_addr
[CH_B
] =
583 (offset
+= dc
->config_table
.dl_mdm_len1
);
584 dc
->port
[PORT_MDM
].dl_size
[CH_A
] =
585 dc
->config_table
.dl_mdm_len1
- buff_offset
;
586 dc
->port
[PORT_MDM
].dl_size
[CH_B
] =
587 dc
->config_table
.dl_mdm_len2
- buff_offset
;
589 /* Diag port dl configuration */
590 dc
->port
[PORT_DIAG
].dl_addr
[CH_A
] =
591 (offset
+= dc
->config_table
.dl_mdm_len2
);
592 dc
->port
[PORT_DIAG
].dl_size
[CH_A
] =
593 dc
->config_table
.dl_diag_len1
- buff_offset
;
594 dc
->port
[PORT_DIAG
].dl_addr
[CH_B
] =
595 (offset
+= dc
->config_table
.dl_diag_len1
);
596 dc
->port
[PORT_DIAG
].dl_size
[CH_B
] =
597 dc
->config_table
.dl_diag_len2
- buff_offset
;
599 /* App1 port dl configuration */
600 dc
->port
[PORT_APP1
].dl_addr
[CH_A
] =
601 (offset
+= dc
->config_table
.dl_diag_len2
);
602 dc
->port
[PORT_APP1
].dl_size
[CH_A
] =
603 dc
->config_table
.dl_app1_len
- buff_offset
;
605 /* App2 port dl configuration */
606 dc
->port
[PORT_APP2
].dl_addr
[CH_A
] =
607 (offset
+= dc
->config_table
.dl_app1_len
);
608 dc
->port
[PORT_APP2
].dl_size
[CH_A
] =
609 dc
->config_table
.dl_app2_len
- buff_offset
;
611 /* Ctrl dl configuration */
612 dc
->port
[PORT_CTRL
].dl_addr
[CH_A
] =
613 (offset
+= dc
->config_table
.dl_app2_len
);
614 dc
->port
[PORT_CTRL
].dl_size
[CH_A
] =
615 dc
->config_table
.dl_ctrl_len
- buff_offset
;
617 offset
= dc
->base_addr
+ dc
->config_table
.ul_start
;
619 /* Modem Port ul configuration */
620 dc
->port
[PORT_MDM
].ul_addr
[CH_A
] = offset
;
621 dc
->port
[PORT_MDM
].ul_size
[CH_A
] =
622 dc
->config_table
.ul_mdm_len1
- buff_offset
;
623 dc
->port
[PORT_MDM
].ul_addr
[CH_B
] =
624 (offset
+= dc
->config_table
.ul_mdm_len1
);
625 dc
->port
[PORT_MDM
].ul_size
[CH_B
] =
626 dc
->config_table
.ul_mdm_len2
- buff_offset
;
628 /* Diag port ul configuration */
629 dc
->port
[PORT_DIAG
].ul_addr
[CH_A
] =
630 (offset
+= dc
->config_table
.ul_mdm_len2
);
631 dc
->port
[PORT_DIAG
].ul_size
[CH_A
] =
632 dc
->config_table
.ul_diag_len
- buff_offset
;
634 /* App1 port ul configuration */
635 dc
->port
[PORT_APP1
].ul_addr
[CH_A
] =
636 (offset
+= dc
->config_table
.ul_diag_len
);
637 dc
->port
[PORT_APP1
].ul_size
[CH_A
] =
638 dc
->config_table
.ul_app1_len
- buff_offset
;
640 /* App2 port ul configuration */
641 dc
->port
[PORT_APP2
].ul_addr
[CH_A
] =
642 (offset
+= dc
->config_table
.ul_app1_len
);
643 dc
->port
[PORT_APP2
].ul_size
[CH_A
] =
644 dc
->config_table
.ul_app2_len
- buff_offset
;
646 /* Ctrl ul configuration */
647 dc
->port
[PORT_CTRL
].ul_addr
[CH_A
] =
648 (offset
+= dc
->config_table
.ul_app2_len
);
649 dc
->port
[PORT_CTRL
].ul_size
[CH_A
] =
650 dc
->config_table
.ul_ctrl_len
- buff_offset
;
653 /* Dump config table under initalization phase */
655 static void dump_table(const struct nozomi
*dc
)
657 DBG3("signature: 0x%08X", dc
->config_table
.signature
);
658 DBG3("version: 0x%04X", dc
->config_table
.version
);
659 DBG3("product_information: 0x%04X", \
660 dc
->config_table
.product_information
);
661 DBG3("toggle enabled: %d", dc
->config_table
.toggle
.enabled
);
662 DBG3("toggle up_mdm: %d", dc
->config_table
.toggle
.mdm_ul
);
663 DBG3("toggle dl_mdm: %d", dc
->config_table
.toggle
.mdm_dl
);
664 DBG3("toggle dl_dbg: %d", dc
->config_table
.toggle
.diag_dl
);
666 DBG3("dl_start: 0x%04X", dc
->config_table
.dl_start
);
667 DBG3("dl_mdm_len0: 0x%04X, %d", dc
->config_table
.dl_mdm_len1
,
668 dc
->config_table
.dl_mdm_len1
);
669 DBG3("dl_mdm_len1: 0x%04X, %d", dc
->config_table
.dl_mdm_len2
,
670 dc
->config_table
.dl_mdm_len2
);
671 DBG3("dl_diag_len0: 0x%04X, %d", dc
->config_table
.dl_diag_len1
,
672 dc
->config_table
.dl_diag_len1
);
673 DBG3("dl_diag_len1: 0x%04X, %d", dc
->config_table
.dl_diag_len2
,
674 dc
->config_table
.dl_diag_len2
);
675 DBG3("dl_app1_len: 0x%04X, %d", dc
->config_table
.dl_app1_len
,
676 dc
->config_table
.dl_app1_len
);
677 DBG3("dl_app2_len: 0x%04X, %d", dc
->config_table
.dl_app2_len
,
678 dc
->config_table
.dl_app2_len
);
679 DBG3("dl_ctrl_len: 0x%04X, %d", dc
->config_table
.dl_ctrl_len
,
680 dc
->config_table
.dl_ctrl_len
);
681 DBG3("ul_start: 0x%04X, %d", dc
->config_table
.ul_start
,
682 dc
->config_table
.ul_start
);
683 DBG3("ul_mdm_len[0]: 0x%04X, %d", dc
->config_table
.ul_mdm_len1
,
684 dc
->config_table
.ul_mdm_len1
);
685 DBG3("ul_mdm_len[1]: 0x%04X, %d", dc
->config_table
.ul_mdm_len2
,
686 dc
->config_table
.ul_mdm_len2
);
687 DBG3("ul_diag_len: 0x%04X, %d", dc
->config_table
.ul_diag_len
,
688 dc
->config_table
.ul_diag_len
);
689 DBG3("ul_app1_len: 0x%04X, %d", dc
->config_table
.ul_app1_len
,
690 dc
->config_table
.ul_app1_len
);
691 DBG3("ul_app2_len: 0x%04X, %d", dc
->config_table
.ul_app2_len
,
692 dc
->config_table
.ul_app2_len
);
693 DBG3("ul_ctrl_len: 0x%04X, %d", dc
->config_table
.ul_ctrl_len
,
694 dc
->config_table
.ul_ctrl_len
);
697 static __inline__
void dump_table(const struct nozomi
*dc
) { }
701 * Read configuration table from card under intalization phase
702 * Returns 1 if ok, else 0
704 static int nozomi_read_config_table(struct nozomi
*dc
)
706 read_mem32((u32
*) &dc
->config_table
, dc
->base_addr
+ 0,
707 sizeof(struct config_table
));
709 if (dc
->config_table
.signature
!= CONFIG_MAGIC
) {
710 dev_err(&dc
->pdev
->dev
, "ConfigTable Bad! 0x%08X != 0x%08X\n",
711 dc
->config_table
.signature
, CONFIG_MAGIC
);
715 if ((dc
->config_table
.version
== 0)
716 || (dc
->config_table
.toggle
.enabled
== TOGGLE_VALID
)) {
718 DBG1("Second phase, configuring card");
722 dc
->port
[PORT_MDM
].toggle_ul
= dc
->config_table
.toggle
.mdm_ul
;
723 dc
->port
[PORT_MDM
].toggle_dl
= dc
->config_table
.toggle
.mdm_dl
;
724 dc
->port
[PORT_DIAG
].toggle_dl
= dc
->config_table
.toggle
.diag_dl
;
725 DBG1("toggle ports: MDM UL:%d MDM DL:%d, DIAG DL:%d",
726 dc
->port
[PORT_MDM
].toggle_ul
,
727 dc
->port
[PORT_MDM
].toggle_dl
, dc
->port
[PORT_DIAG
].toggle_dl
);
731 for (i
= PORT_MDM
; i
< MAX_PORT
; i
++) {
732 dc
->port
[i
].fifo_ul
=
733 kfifo_alloc(FIFO_BUFFER_SIZE_UL
, GFP_ATOMIC
, NULL
);
734 memset(&dc
->port
[i
].ctrl_dl
, 0, sizeof(struct ctrl_dl
));
735 memset(&dc
->port
[i
].ctrl_ul
, 0, sizeof(struct ctrl_ul
));
738 /* Enable control channel */
739 dc
->last_ier
= dc
->last_ier
| CTRL_DL
;
740 writew(dc
->last_ier
, dc
->reg_ier
);
742 dev_info(&dc
->pdev
->dev
, "Initialization OK!\n");
746 if ((dc
->config_table
.version
> 0)
747 && (dc
->config_table
.toggle
.enabled
!= TOGGLE_VALID
)) {
749 DBG1("First phase: pushing upload buffers, clearing download");
751 dev_info(&dc
->pdev
->dev
, "Version of card: %d\n",
752 dc
->config_table
.version
);
754 /* Here we should disable all I/O over F32. */
758 * We should send ALL channel pair tokens back along
762 /* push upload modem buffers */
763 write_mem32(dc
->port
[PORT_MDM
].ul_addr
[CH_A
],
765 write_mem32(dc
->port
[PORT_MDM
].ul_addr
[CH_B
],
768 writew(MDM_UL
| DIAG_DL
| MDM_DL
, dc
->reg_fcr
);
770 DBG1("First phase done");
776 /* Enable uplink interrupts */
777 static void enable_transmit_ul(enum port_type port
, struct nozomi
*dc
)
779 u16 mask
[NOZOMI_MAX_PORTS
] = \
780 {MDM_UL
, DIAG_UL
, APP1_UL
, APP2_UL
, CTRL_UL
};
782 if (port
< NOZOMI_MAX_PORTS
) {
783 dc
->last_ier
|= mask
[port
];
784 writew(dc
->last_ier
, dc
->reg_ier
);
786 dev_err(&dc
->pdev
->dev
, "Called with wrong port?\n");
790 /* Disable uplink interrupts */
791 static void disable_transmit_ul(enum port_type port
, struct nozomi
*dc
)
793 u16 mask
[NOZOMI_MAX_PORTS
] = \
794 {~MDM_UL
, ~DIAG_UL
, ~APP1_UL
, ~APP2_UL
, ~CTRL_UL
};
796 if (port
< NOZOMI_MAX_PORTS
) {
797 dc
->last_ier
&= mask
[port
];
798 writew(dc
->last_ier
, dc
->reg_ier
);
800 dev_err(&dc
->pdev
->dev
, "Called with wrong port?\n");
804 /* Enable downlink interrupts */
805 static void enable_transmit_dl(enum port_type port
, struct nozomi
*dc
)
807 u16 mask
[NOZOMI_MAX_PORTS
] = \
808 {MDM_DL
, DIAG_DL
, APP1_DL
, APP2_DL
, CTRL_DL
};
810 if (port
< NOZOMI_MAX_PORTS
) {
811 dc
->last_ier
|= mask
[port
];
812 writew(dc
->last_ier
, dc
->reg_ier
);
814 dev_err(&dc
->pdev
->dev
, "Called with wrong port?\n");
818 /* Disable downlink interrupts */
819 static void disable_transmit_dl(enum port_type port
, struct nozomi
*dc
)
821 u16 mask
[NOZOMI_MAX_PORTS
] = \
822 {~MDM_DL
, ~DIAG_DL
, ~APP1_DL
, ~APP2_DL
, ~CTRL_DL
};
824 if (port
< NOZOMI_MAX_PORTS
) {
825 dc
->last_ier
&= mask
[port
];
826 writew(dc
->last_ier
, dc
->reg_ier
);
828 dev_err(&dc
->pdev
->dev
, "Called with wrong port?\n");
833 * Return 1 - send buffer to card and ack.
834 * Return 0 - don't ack, don't send buffer to card.
836 static int send_data(enum port_type index
, struct nozomi
*dc
)
839 struct port
*port
= &dc
->port
[index
];
840 u8 toggle
= port
->toggle_ul
;
841 void __iomem
*addr
= port
->ul_addr
[toggle
];
842 u32 ul_size
= port
->ul_size
[toggle
];
843 struct tty_struct
*tty
= port
->tty
;
845 /* Get data from tty and place in buf for now */
846 size
= __kfifo_get(port
->fifo_ul
, dc
->send_buf
,
847 ul_size
< SEND_BUF_MAX
? ul_size
: SEND_BUF_MAX
);
850 DBG4("No more data to send, disable link:");
854 /* DUMP(buf, size); */
856 /* Write length + data */
857 write_mem32(addr
, (u32
*) &size
, 4);
858 write_mem32(addr
+ 4, (u32
*) dc
->send_buf
, size
);
866 /* If all data has been read, return 1, else 0 */
867 static int receive_data(enum port_type index
, struct nozomi
*dc
)
869 u8 buf
[RECEIVE_BUF_MAX
] = { 0 };
872 struct port
*port
= &dc
->port
[index
];
873 void __iomem
*addr
= port
->dl_addr
[port
->toggle_dl
];
874 struct tty_struct
*tty
= port
->tty
;
877 if (unlikely(!tty
)) {
878 DBG1("tty not open for port: %d?", index
);
882 read_mem32((u32
*) &size
, addr
, 4);
883 /* DBG1( "%d bytes port: %d", size, index); */
885 if (test_bit(TTY_THROTTLED
, &tty
->flags
)) {
886 DBG1("No room in tty, don't read data, don't ack interrupt, "
887 "disable interrupt");
889 /* disable interrupt in downlink... */
890 disable_transmit_dl(index
, dc
);
894 if (unlikely(size
== 0)) {
895 dev_err(&dc
->pdev
->dev
, "size == 0?\n");
899 tty_buffer_request_room(tty
, size
);
902 read_mem32((u32
*) buf
, addr
+ offset
, RECEIVE_BUF_MAX
);
905 tty_insert_flip_char(tty
, buf
[0], TTY_NORMAL
);
907 } else if (size
< RECEIVE_BUF_MAX
) {
908 size
-= tty_insert_flip_string(tty
, (char *) buf
, size
);
910 i
= tty_insert_flip_string(tty
, \
911 (char *) buf
, RECEIVE_BUF_MAX
);
917 set_bit(index
, &dc
->flip
);
922 /* Debug for interrupts */
924 static char *interrupt2str(u16 interrupt
)
926 static char buf
[TMP_BUF_MAX
];
929 interrupt
& MDM_DL1
? p
+= snprintf(p
, TMP_BUF_MAX
, "MDM_DL1 ") : NULL
;
930 interrupt
& MDM_DL2
? p
+= snprintf(p
, TMP_BUF_MAX
- (p
- buf
),
933 interrupt
& MDM_UL1
? p
+= snprintf(p
, TMP_BUF_MAX
- (p
- buf
),
935 interrupt
& MDM_UL2
? p
+= snprintf(p
, TMP_BUF_MAX
- (p
- buf
),
938 interrupt
& DIAG_DL1
? p
+= snprintf(p
, TMP_BUF_MAX
- (p
- buf
),
940 interrupt
& DIAG_DL2
? p
+= snprintf(p
, TMP_BUF_MAX
- (p
- buf
),
943 interrupt
& DIAG_UL
? p
+= snprintf(p
, TMP_BUF_MAX
- (p
- buf
),
946 interrupt
& APP1_DL
? p
+= snprintf(p
, TMP_BUF_MAX
- (p
- buf
),
948 interrupt
& APP2_DL
? p
+= snprintf(p
, TMP_BUF_MAX
- (p
- buf
),
951 interrupt
& APP1_UL
? p
+= snprintf(p
, TMP_BUF_MAX
- (p
- buf
),
953 interrupt
& APP2_UL
? p
+= snprintf(p
, TMP_BUF_MAX
- (p
- buf
),
956 interrupt
& CTRL_DL
? p
+= snprintf(p
, TMP_BUF_MAX
- (p
- buf
),
958 interrupt
& CTRL_UL
? p
+= snprintf(p
, TMP_BUF_MAX
- (p
- buf
),
961 interrupt
& RESET
? p
+= snprintf(p
, TMP_BUF_MAX
- (p
- buf
),
969 * Receive flow control
970 * Return 1 - If ok, else 0
972 static int receive_flow_control(struct nozomi
*dc
)
974 enum port_type port
= PORT_MDM
;
975 struct ctrl_dl ctrl_dl
;
976 struct ctrl_dl old_ctrl
;
979 read_mem32((u32
*) &ctrl_dl
, dc
->port
[PORT_CTRL
].dl_addr
[CH_A
], 2);
981 switch (ctrl_dl
.port
) {
983 DBG1("The Base Band sends this value as a response to a "
984 "request for IMSI detach sent over the control "
985 "channel uplink (see section 7.6.1).");
993 enable_ier
= DIAG_DL
;
997 enable_ier
= APP1_DL
;
1001 enable_ier
= APP2_DL
;
1004 dev_err(&dc
->pdev
->dev
,
1005 "ERROR: flow control received for non-existing port\n");
1009 DBG1("0x%04X->0x%04X", *((u16
*)&dc
->port
[port
].ctrl_dl
),
1010 *((u16
*)&ctrl_dl
));
1012 old_ctrl
= dc
->port
[port
].ctrl_dl
;
1013 dc
->port
[port
].ctrl_dl
= ctrl_dl
;
1015 if (old_ctrl
.CTS
== 1 && ctrl_dl
.CTS
== 0) {
1016 DBG1("Disable interrupt (0x%04X) on port: %d",
1018 disable_transmit_ul(port
, dc
);
1020 } else if (old_ctrl
.CTS
== 0 && ctrl_dl
.CTS
== 1) {
1022 if (__kfifo_len(dc
->port
[port
].fifo_ul
)) {
1023 DBG1("Enable interrupt (0x%04X) on port: %d",
1025 DBG1("Data in buffer [%d], enable transmit! ",
1026 __kfifo_len(dc
->port
[port
].fifo_ul
));
1027 enable_transmit_ul(port
, dc
);
1029 DBG1("No data in buffer...");
1033 if (*(u16
*)&old_ctrl
== *(u16
*)&ctrl_dl
) {
1034 DBG1(" No change in mctrl");
1037 /* Update statistics */
1038 if (old_ctrl
.CTS
!= ctrl_dl
.CTS
)
1039 dc
->port
[port
].tty_icount
.cts
++;
1040 if (old_ctrl
.DSR
!= ctrl_dl
.DSR
)
1041 dc
->port
[port
].tty_icount
.dsr
++;
1042 if (old_ctrl
.RI
!= ctrl_dl
.RI
)
1043 dc
->port
[port
].tty_icount
.rng
++;
1044 if (old_ctrl
.DCD
!= ctrl_dl
.DCD
)
1045 dc
->port
[port
].tty_icount
.dcd
++;
1047 wake_up_interruptible(&dc
->port
[port
].tty_wait
);
1049 DBG1("port: %d DCD(%d), CTS(%d), RI(%d), DSR(%d)",
1051 dc
->port
[port
].tty_icount
.dcd
, dc
->port
[port
].tty_icount
.cts
,
1052 dc
->port
[port
].tty_icount
.rng
, dc
->port
[port
].tty_icount
.dsr
);
1057 static enum ctrl_port_type
port2ctrl(enum port_type port
,
1058 const struct nozomi
*dc
)
1070 dev_err(&dc
->pdev
->dev
,
1071 "ERROR: send flow control " \
1072 "received for non-existing port\n");
1078 * Send flow control, can only update one channel at a time
1079 * Return 0 - If we have updated all flow control
1080 * Return 1 - If we need to update more flow control, ack current enable more
1082 static int send_flow_control(struct nozomi
*dc
)
1084 u32 i
, more_flow_control_to_be_updated
= 0;
1087 for (i
= PORT_MDM
; i
< MAX_PORT
; i
++) {
1088 if (dc
->port
[i
].update_flow_control
) {
1089 if (more_flow_control_to_be_updated
) {
1090 /* We have more flow control to be updated */
1093 dc
->port
[i
].ctrl_ul
.port
= port2ctrl(i
, dc
);
1094 ctrl
= (u16
*)&dc
->port
[i
].ctrl_ul
;
1095 write_mem32(dc
->port
[PORT_CTRL
].ul_addr
[0], \
1097 dc
->port
[i
].update_flow_control
= 0;
1098 more_flow_control_to_be_updated
= 1;
1105 * Handle donlink data, ports that are handled are modem and diagnostics
1107 * Return 0 - toggle fields are out of sync
1109 static int handle_data_dl(struct nozomi
*dc
, enum port_type port
, u8
*toggle
,
1110 u16 read_iir
, u16 mask1
, u16 mask2
)
1112 if (*toggle
== 0 && read_iir
& mask1
) {
1113 if (receive_data(port
, dc
)) {
1114 writew(mask1
, dc
->reg_fcr
);
1115 *toggle
= !(*toggle
);
1118 if (read_iir
& mask2
) {
1119 if (receive_data(port
, dc
)) {
1120 writew(mask2
, dc
->reg_fcr
);
1121 *toggle
= !(*toggle
);
1124 } else if (*toggle
== 1 && read_iir
& mask2
) {
1125 if (receive_data(port
, dc
)) {
1126 writew(mask2
, dc
->reg_fcr
);
1127 *toggle
= !(*toggle
);
1130 if (read_iir
& mask1
) {
1131 if (receive_data(port
, dc
)) {
1132 writew(mask1
, dc
->reg_fcr
);
1133 *toggle
= !(*toggle
);
1137 dev_err(&dc
->pdev
->dev
, "port out of sync!, toggle:%d\n",
1145 * Handle uplink data, this is currently for the modem port
1147 * Return 0 - toggle field are out of sync
1149 static int handle_data_ul(struct nozomi
*dc
, enum port_type port
, u16 read_iir
)
1151 u8
*toggle
= &(dc
->port
[port
].toggle_ul
);
1153 if (*toggle
== 0 && read_iir
& MDM_UL1
) {
1154 dc
->last_ier
&= ~MDM_UL
;
1155 writew(dc
->last_ier
, dc
->reg_ier
);
1156 if (send_data(port
, dc
)) {
1157 writew(MDM_UL1
, dc
->reg_fcr
);
1158 dc
->last_ier
= dc
->last_ier
| MDM_UL
;
1159 writew(dc
->last_ier
, dc
->reg_ier
);
1163 if (read_iir
& MDM_UL2
) {
1164 dc
->last_ier
&= ~MDM_UL
;
1165 writew(dc
->last_ier
, dc
->reg_ier
);
1166 if (send_data(port
, dc
)) {
1167 writew(MDM_UL2
, dc
->reg_fcr
);
1168 dc
->last_ier
= dc
->last_ier
| MDM_UL
;
1169 writew(dc
->last_ier
, dc
->reg_ier
);
1174 } else if (*toggle
== 1 && read_iir
& MDM_UL2
) {
1175 dc
->last_ier
&= ~MDM_UL
;
1176 writew(dc
->last_ier
, dc
->reg_ier
);
1177 if (send_data(port
, dc
)) {
1178 writew(MDM_UL2
, dc
->reg_fcr
);
1179 dc
->last_ier
= dc
->last_ier
| MDM_UL
;
1180 writew(dc
->last_ier
, dc
->reg_ier
);
1184 if (read_iir
& MDM_UL1
) {
1185 dc
->last_ier
&= ~MDM_UL
;
1186 writew(dc
->last_ier
, dc
->reg_ier
);
1187 if (send_data(port
, dc
)) {
1188 writew(MDM_UL1
, dc
->reg_fcr
);
1189 dc
->last_ier
= dc
->last_ier
| MDM_UL
;
1190 writew(dc
->last_ier
, dc
->reg_ier
);
1195 writew(read_iir
& MDM_UL
, dc
->reg_fcr
);
1196 dev_err(&dc
->pdev
->dev
, "port out of sync!\n");
1202 static irqreturn_t
interrupt_handler(int irq
, void *dev_id
)
1204 struct nozomi
*dc
= dev_id
;
1211 spin_lock(&dc
->spin_mutex
);
1212 read_iir
= readw(dc
->reg_iir
);
1215 if (read_iir
== (u16
)-1)
1218 * Just handle interrupt enabled in IER
1219 * (by masking with dc->last_ier)
1221 read_iir
&= dc
->last_ier
;
1227 DBG4("%s irq:0x%04X, prev:0x%04X", interrupt2str(read_iir
), read_iir
,
1230 if (read_iir
& RESET
) {
1231 if (unlikely(!nozomi_read_config_table(dc
))) {
1233 writew(dc
->last_ier
, dc
->reg_ier
);
1234 dev_err(&dc
->pdev
->dev
, "Could not read status from "
1235 "card, we should disable interface\n");
1237 writew(RESET
, dc
->reg_fcr
);
1239 /* No more useful info if this was the reset interrupt. */
1242 if (read_iir
& CTRL_UL
) {
1244 dc
->last_ier
&= ~CTRL_UL
;
1245 writew(dc
->last_ier
, dc
->reg_ier
);
1246 if (send_flow_control(dc
)) {
1247 writew(CTRL_UL
, dc
->reg_fcr
);
1248 dc
->last_ier
= dc
->last_ier
| CTRL_UL
;
1249 writew(dc
->last_ier
, dc
->reg_ier
);
1252 if (read_iir
& CTRL_DL
) {
1253 receive_flow_control(dc
);
1254 writew(CTRL_DL
, dc
->reg_fcr
);
1256 if (read_iir
& MDM_DL
) {
1257 if (!handle_data_dl(dc
, PORT_MDM
,
1258 &(dc
->port
[PORT_MDM
].toggle_dl
), read_iir
,
1259 MDM_DL1
, MDM_DL2
)) {
1260 dev_err(&dc
->pdev
->dev
, "MDM_DL out of sync!\n");
1264 if (read_iir
& MDM_UL
) {
1265 if (!handle_data_ul(dc
, PORT_MDM
, read_iir
)) {
1266 dev_err(&dc
->pdev
->dev
, "MDM_UL out of sync!\n");
1270 if (read_iir
& DIAG_DL
) {
1271 if (!handle_data_dl(dc
, PORT_DIAG
,
1272 &(dc
->port
[PORT_DIAG
].toggle_dl
), read_iir
,
1273 DIAG_DL1
, DIAG_DL2
)) {
1274 dev_err(&dc
->pdev
->dev
, "DIAG_DL out of sync!\n");
1278 if (read_iir
& DIAG_UL
) {
1279 dc
->last_ier
&= ~DIAG_UL
;
1280 writew(dc
->last_ier
, dc
->reg_ier
);
1281 if (send_data(PORT_DIAG
, dc
)) {
1282 writew(DIAG_UL
, dc
->reg_fcr
);
1283 dc
->last_ier
= dc
->last_ier
| DIAG_UL
;
1284 writew(dc
->last_ier
, dc
->reg_ier
);
1287 if (read_iir
& APP1_DL
) {
1288 if (receive_data(PORT_APP1
, dc
))
1289 writew(APP1_DL
, dc
->reg_fcr
);
1291 if (read_iir
& APP1_UL
) {
1292 dc
->last_ier
&= ~APP1_UL
;
1293 writew(dc
->last_ier
, dc
->reg_ier
);
1294 if (send_data(PORT_APP1
, dc
)) {
1295 writew(APP1_UL
, dc
->reg_fcr
);
1296 dc
->last_ier
= dc
->last_ier
| APP1_UL
;
1297 writew(dc
->last_ier
, dc
->reg_ier
);
1300 if (read_iir
& APP2_DL
) {
1301 if (receive_data(PORT_APP2
, dc
))
1302 writew(APP2_DL
, dc
->reg_fcr
);
1304 if (read_iir
& APP2_UL
) {
1305 dc
->last_ier
&= ~APP2_UL
;
1306 writew(dc
->last_ier
, dc
->reg_ier
);
1307 if (send_data(PORT_APP2
, dc
)) {
1308 writew(APP2_UL
, dc
->reg_fcr
);
1309 dc
->last_ier
= dc
->last_ier
| APP2_UL
;
1310 writew(dc
->last_ier
, dc
->reg_ier
);
1315 spin_unlock(&dc
->spin_mutex
);
1316 for (a
= 0; a
< NOZOMI_MAX_PORTS
; a
++)
1317 if (test_and_clear_bit(a
, &dc
->flip
))
1318 tty_flip_buffer_push(dc
->port
[a
].tty
);
1321 spin_unlock(&dc
->spin_mutex
);
1325 static void nozomi_get_card_type(struct nozomi
*dc
)
1330 for (i
= 0; i
< 6; i
++)
1331 size
+= pci_resource_len(dc
->pdev
, i
);
1333 /* Assume card type F32_8 if no match */
1334 dc
->card_type
= size
== 2048 ? F32_2
: F32_8
;
1336 dev_info(&dc
->pdev
->dev
, "Card type is: %d\n", dc
->card_type
);
1339 static void nozomi_setup_private_data(struct nozomi
*dc
)
1341 void __iomem
*offset
= dc
->base_addr
+ dc
->card_type
/ 2;
1344 dc
->reg_fcr
= (void __iomem
*)(offset
+ R_FCR
);
1345 dc
->reg_iir
= (void __iomem
*)(offset
+ R_IIR
);
1346 dc
->reg_ier
= (void __iomem
*)(offset
+ R_IER
);
1350 dc
->port
[PORT_MDM
].token_dl
= MDM_DL
;
1351 dc
->port
[PORT_DIAG
].token_dl
= DIAG_DL
;
1352 dc
->port
[PORT_APP1
].token_dl
= APP1_DL
;
1353 dc
->port
[PORT_APP2
].token_dl
= APP2_DL
;
1355 for (i
= 0; i
< MAX_PORT
; i
++)
1356 init_waitqueue_head(&dc
->port
[i
].tty_wait
);
1359 static ssize_t
card_type_show(struct device
*dev
, struct device_attribute
*attr
,
1362 struct nozomi
*dc
= pci_get_drvdata(to_pci_dev(dev
));
1364 return sprintf(buf
, "%d\n", dc
->card_type
);
1366 static DEVICE_ATTR(card_type
, 0444, card_type_show
, NULL
);
1368 static ssize_t
open_ttys_show(struct device
*dev
, struct device_attribute
*attr
,
1371 struct nozomi
*dc
= pci_get_drvdata(to_pci_dev(dev
));
1373 return sprintf(buf
, "%u\n", dc
->open_ttys
);
1375 static DEVICE_ATTR(open_ttys
, 0444, open_ttys_show
, NULL
);
1377 static void make_sysfs_files(struct nozomi
*dc
)
1379 if (device_create_file(&dc
->pdev
->dev
, &dev_attr_card_type
))
1380 dev_err(&dc
->pdev
->dev
,
1381 "Could not create sysfs file for card_type\n");
1382 if (device_create_file(&dc
->pdev
->dev
, &dev_attr_open_ttys
))
1383 dev_err(&dc
->pdev
->dev
,
1384 "Could not create sysfs file for open_ttys\n");
1387 static void remove_sysfs_files(struct nozomi
*dc
)
1389 device_remove_file(&dc
->pdev
->dev
, &dev_attr_card_type
);
1390 device_remove_file(&dc
->pdev
->dev
, &dev_attr_open_ttys
);
1393 /* Allocate memory for one device */
1394 static int __devinit
nozomi_card_init(struct pci_dev
*pdev
,
1395 const struct pci_device_id
*ent
)
1397 resource_size_t start
;
1399 struct nozomi
*dc
= NULL
;
1403 dev_dbg(&pdev
->dev
, "Init, new card found\n");
1405 for (ndev_idx
= 0; ndev_idx
< ARRAY_SIZE(ndevs
); ndev_idx
++)
1406 if (!ndevs
[ndev_idx
])
1409 if (ndev_idx
>= ARRAY_SIZE(ndevs
)) {
1410 dev_err(&pdev
->dev
, "no free tty range for this card left\n");
1415 dc
= kzalloc(sizeof(struct nozomi
), GFP_KERNEL
);
1416 if (unlikely(!dc
)) {
1417 dev_err(&pdev
->dev
, "Could not allocate memory\n");
1424 /* Find out what card type it is */
1425 nozomi_get_card_type(dc
);
1427 ret
= pci_enable_device(dc
->pdev
);
1429 dev_err(&pdev
->dev
, "Failed to enable PCI Device\n");
1433 start
= pci_resource_start(dc
->pdev
, 0);
1435 dev_err(&pdev
->dev
, "No I/O address for card detected\n");
1437 goto err_disable_device
;
1440 ret
= pci_request_regions(dc
->pdev
, NOZOMI_NAME
);
1442 dev_err(&pdev
->dev
, "I/O address 0x%04x already in use\n",
1443 (int) /* nozomi_private.io_addr */ 0);
1444 goto err_disable_device
;
1447 dc
->base_addr
= ioremap(start
, dc
->card_type
);
1448 if (!dc
->base_addr
) {
1449 dev_err(&pdev
->dev
, "Unable to map card MMIO\n");
1454 dc
->send_buf
= kmalloc(SEND_BUF_MAX
, GFP_KERNEL
);
1455 if (!dc
->send_buf
) {
1456 dev_err(&pdev
->dev
, "Could not allocate send buffer?\n");
1461 spin_lock_init(&dc
->spin_mutex
);
1463 nozomi_setup_private_data(dc
);
1465 /* Disable all interrupts */
1467 writew(dc
->last_ier
, dc
->reg_ier
);
1469 ret
= request_irq(pdev
->irq
, &interrupt_handler
, IRQF_SHARED
,
1471 if (unlikely(ret
)) {
1472 dev_err(&pdev
->dev
, "can't request irq %d\n", pdev
->irq
);
1476 DBG1("base_addr: %p", dc
->base_addr
);
1478 make_sysfs_files(dc
);
1480 dc
->index_start
= ndev_idx
* MAX_PORT
;
1481 ndevs
[ndev_idx
] = dc
;
1483 for (i
= 0; i
< MAX_PORT
; i
++) {
1484 mutex_init(&dc
->port
[i
].tty_sem
);
1485 dc
->port
[i
].tty_open_count
= 0;
1486 dc
->port
[i
].tty
= NULL
;
1487 tty_register_device(ntty_driver
, dc
->index_start
+ i
,
1491 /* Enable RESET interrupt. */
1492 dc
->last_ier
= RESET
;
1493 writew(dc
->last_ier
, dc
->reg_ier
);
1495 pci_set_drvdata(pdev
, dc
);
1500 kfree(dc
->send_buf
);
1501 iounmap(dc
->base_addr
);
1503 pci_release_regions(pdev
);
1505 pci_disable_device(pdev
);
1512 static void __devexit
tty_exit(struct nozomi
*dc
)
1518 flush_scheduled_work();
1520 for (i
= 0; i
< MAX_PORT
; ++i
)
1521 if (dc
->port
[i
].tty
&& \
1522 list_empty(&dc
->port
[i
].tty
->hangup_work
.entry
))
1523 tty_hangup(dc
->port
[i
].tty
);
1525 while (dc
->open_ttys
)
1528 for (i
= dc
->index_start
; i
< dc
->index_start
+ MAX_PORT
; ++i
)
1529 tty_unregister_device(ntty_driver
, i
);
1532 /* Deallocate memory for one device */
1533 static void __devexit
nozomi_card_exit(struct pci_dev
*pdev
)
1536 struct ctrl_ul ctrl
;
1537 struct nozomi
*dc
= pci_get_drvdata(pdev
);
1539 /* Disable all interrupts */
1541 writew(dc
->last_ier
, dc
->reg_ier
);
1545 /* Send 0x0001, command card to resend the reset token. */
1546 /* This is to get the reset when the module is reloaded. */
1551 DBG1("sending flow control 0x%04X", *((u16
*)&ctrl
));
1553 /* Setup dc->reg addresses to we can use defines here */
1554 write_mem32(dc
->port
[PORT_CTRL
].ul_addr
[0], (u32
*)&ctrl
, 2);
1555 writew(CTRL_UL
, dc
->reg_fcr
); /* push the token to the card. */
1557 remove_sysfs_files(dc
);
1559 free_irq(pdev
->irq
, dc
);
1561 for (i
= 0; i
< MAX_PORT
; i
++)
1562 if (dc
->port
[i
].fifo_ul
)
1563 kfifo_free(dc
->port
[i
].fifo_ul
);
1565 kfree(dc
->send_buf
);
1567 iounmap(dc
->base_addr
);
1569 pci_release_regions(pdev
);
1571 pci_disable_device(pdev
);
1573 ndevs
[dc
->index_start
/ MAX_PORT
] = NULL
;
1578 static void set_rts(const struct tty_struct
*tty
, int rts
)
1580 struct port
*port
= get_port_by_tty(tty
);
1582 port
->ctrl_ul
.RTS
= rts
;
1583 port
->update_flow_control
= 1;
1584 enable_transmit_ul(PORT_CTRL
, get_dc_by_tty(tty
));
1587 static void set_dtr(const struct tty_struct
*tty
, int dtr
)
1589 struct port
*port
= get_port_by_tty(tty
);
1591 DBG1("SETTING DTR index: %d, dtr: %d", tty
->index
, dtr
);
1593 port
->ctrl_ul
.DTR
= dtr
;
1594 port
->update_flow_control
= 1;
1595 enable_transmit_ul(PORT_CTRL
, get_dc_by_tty(tty
));
1599 * ----------------------------------------------------------------------------
1601 * ----------------------------------------------------------------------------
1604 /* Called when the userspace process opens the tty, /dev/noz*. */
1605 static int ntty_open(struct tty_struct
*tty
, struct file
*file
)
1607 struct port
*port
= get_port_by_tty(tty
);
1608 struct nozomi
*dc
= get_dc_by_tty(tty
);
1609 unsigned long flags
;
1614 if (mutex_lock_interruptible(&port
->tty_sem
))
1615 return -ERESTARTSYS
;
1617 port
->tty_open_count
++;
1620 /* Enable interrupt downlink for channel */
1621 if (port
->tty_open_count
== 1) {
1622 tty
->low_latency
= 1;
1623 tty
->driver_data
= port
;
1625 DBG1("open: %d", port
->token_dl
);
1626 spin_lock_irqsave(&dc
->spin_mutex
, flags
);
1627 dc
->last_ier
= dc
->last_ier
| port
->token_dl
;
1628 writew(dc
->last_ier
, dc
->reg_ier
);
1629 spin_unlock_irqrestore(&dc
->spin_mutex
, flags
);
1632 mutex_unlock(&port
->tty_sem
);
1637 /* Called when the userspace process close the tty, /dev/noz*. */
1638 static void ntty_close(struct tty_struct
*tty
, struct file
*file
)
1640 struct nozomi
*dc
= get_dc_by_tty(tty
);
1641 struct port
*port
= tty
->driver_data
;
1642 unsigned long flags
;
1647 if (mutex_lock_interruptible(&port
->tty_sem
))
1650 if (!port
->tty_open_count
)
1654 port
->tty_open_count
--;
1656 if (port
->tty_open_count
== 0) {
1657 DBG1("close: %d", port
->token_dl
);
1658 spin_lock_irqsave(&dc
->spin_mutex
, flags
);
1659 dc
->last_ier
&= ~(port
->token_dl
);
1660 writew(dc
->last_ier
, dc
->reg_ier
);
1661 spin_unlock_irqrestore(&dc
->spin_mutex
, flags
);
1665 mutex_unlock(&port
->tty_sem
);
1669 * called when the userspace process writes to the tty (/dev/noz*).
1670 * Data is inserted into a fifo, which is then read and transfered to the modem.
1672 static int ntty_write(struct tty_struct
*tty
, const unsigned char *buffer
,
1676 struct nozomi
*dc
= get_dc_by_tty(tty
);
1677 struct port
*port
= tty
->driver_data
;
1678 unsigned long flags
;
1680 /* DBG1( "WRITEx: %d, index = %d", count, index); */
1685 if (unlikely(!mutex_trylock(&port
->tty_sem
))) {
1687 * must test lock as tty layer wraps calls
1688 * to this function with BKL
1690 dev_err(&dc
->pdev
->dev
, "Would have deadlocked - "
1695 if (unlikely(!port
->tty_open_count
)) {
1700 rval
= __kfifo_put(port
->fifo_ul
, (unsigned char *)buffer
, count
);
1703 if (unlikely(dc
== NULL
)) {
1704 DBG1("No device context?");
1708 spin_lock_irqsave(&dc
->spin_mutex
, flags
);
1709 /* CTS is only valid on the modem channel */
1710 if (port
== &(dc
->port
[PORT_MDM
])) {
1711 if (port
->ctrl_dl
.CTS
) {
1712 DBG4("Enable interrupt");
1713 enable_transmit_ul(tty
->index
% MAX_PORT
, dc
);
1715 dev_err(&dc
->pdev
->dev
,
1716 "CTS not active on modem port?\n");
1719 enable_transmit_ul(tty
->index
% MAX_PORT
, dc
);
1721 spin_unlock_irqrestore(&dc
->spin_mutex
, flags
);
1724 mutex_unlock(&port
->tty_sem
);
1729 * Calculate how much is left in device
1730 * This method is called by the upper tty layer.
1731 * #according to sources N_TTY.c it expects a value >= 0 and
1732 * does not check for negative values.
1734 static int ntty_write_room(struct tty_struct
*tty
)
1736 struct port
*port
= tty
->driver_data
;
1738 struct nozomi
*dc
= get_dc_by_tty(tty
);
1742 if (!mutex_trylock(&port
->tty_sem
))
1745 if (!port
->tty_open_count
)
1748 room
= port
->fifo_ul
->size
- __kfifo_len(port
->fifo_ul
);
1751 mutex_unlock(&port
->tty_sem
);
1755 /* Gets io control parameters */
1756 static int ntty_tiocmget(struct tty_struct
*tty
, struct file
*file
)
1758 struct port
*port
= tty
->driver_data
;
1759 struct ctrl_dl
*ctrl_dl
= &port
->ctrl_dl
;
1760 struct ctrl_ul
*ctrl_ul
= &port
->ctrl_ul
;
1762 return (ctrl_ul
->RTS
? TIOCM_RTS
: 0) |
1763 (ctrl_ul
->DTR
? TIOCM_DTR
: 0) |
1764 (ctrl_dl
->DCD
? TIOCM_CAR
: 0) |
1765 (ctrl_dl
->RI
? TIOCM_RNG
: 0) |
1766 (ctrl_dl
->DSR
? TIOCM_DSR
: 0) |
1767 (ctrl_dl
->CTS
? TIOCM_CTS
: 0);
1770 /* Sets io controls parameters */
1771 static int ntty_tiocmset(struct tty_struct
*tty
, struct file
*file
,
1772 unsigned int set
, unsigned int clear
)
1774 if (set
& TIOCM_RTS
)
1776 else if (clear
& TIOCM_RTS
)
1779 if (set
& TIOCM_DTR
)
1781 else if (clear
& TIOCM_DTR
)
1787 static int ntty_cflags_changed(struct port
*port
, unsigned long flags
,
1788 struct async_icount
*cprev
)
1790 struct async_icount cnow
= port
->tty_icount
;
1793 ret
= ((flags
& TIOCM_RNG
) && (cnow
.rng
!= cprev
->rng
)) ||
1794 ((flags
& TIOCM_DSR
) && (cnow
.dsr
!= cprev
->dsr
)) ||
1795 ((flags
& TIOCM_CD
) && (cnow
.dcd
!= cprev
->dcd
)) ||
1796 ((flags
& TIOCM_CTS
) && (cnow
.cts
!= cprev
->cts
));
1803 static int ntty_ioctl_tiocgicount(struct port
*port
, void __user
*argp
)
1805 struct async_icount cnow
= port
->tty_icount
;
1806 struct serial_icounter_struct icount
;
1808 icount
.cts
= cnow
.cts
;
1809 icount
.dsr
= cnow
.dsr
;
1810 icount
.rng
= cnow
.rng
;
1811 icount
.dcd
= cnow
.dcd
;
1812 icount
.rx
= cnow
.rx
;
1813 icount
.tx
= cnow
.tx
;
1814 icount
.frame
= cnow
.frame
;
1815 icount
.overrun
= cnow
.overrun
;
1816 icount
.parity
= cnow
.parity
;
1817 icount
.brk
= cnow
.brk
;
1818 icount
.buf_overrun
= cnow
.buf_overrun
;
1820 return copy_to_user(argp
, &icount
, sizeof(icount
));
1823 static int ntty_ioctl(struct tty_struct
*tty
, struct file
*file
,
1824 unsigned int cmd
, unsigned long arg
)
1826 struct port
*port
= tty
->driver_data
;
1827 void __user
*argp
= (void __user
*)arg
;
1828 int rval
= -ENOIOCTLCMD
;
1830 DBG1("******** IOCTL, cmd: %d", cmd
);
1834 struct async_icount cprev
= port
->tty_icount
;
1836 rval
= wait_event_interruptible(port
->tty_wait
,
1837 ntty_cflags_changed(port
, arg
, &cprev
));
1840 rval
= ntty_ioctl_tiocgicount(port
, argp
);
1843 DBG1("ERR: 0x%08X, %d", cmd
, cmd
);
1851 * Called by the upper tty layer when tty buffers are ready
1852 * to receive data again after a call to throttle.
1854 static void ntty_unthrottle(struct tty_struct
*tty
)
1856 struct nozomi
*dc
= get_dc_by_tty(tty
);
1857 unsigned long flags
;
1860 spin_lock_irqsave(&dc
->spin_mutex
, flags
);
1861 enable_transmit_dl(tty
->index
% MAX_PORT
, dc
);
1864 spin_unlock_irqrestore(&dc
->spin_mutex
, flags
);
1868 * Called by the upper tty layer when the tty buffers are almost full.
1869 * The driver should stop send more data.
1871 static void ntty_throttle(struct tty_struct
*tty
)
1873 struct nozomi
*dc
= get_dc_by_tty(tty
);
1874 unsigned long flags
;
1877 spin_lock_irqsave(&dc
->spin_mutex
, flags
);
1879 spin_unlock_irqrestore(&dc
->spin_mutex
, flags
);
1882 /* just to discard single character writes */
1883 static void ntty_put_char(struct tty_struct
*tty
, unsigned char c
)
1886 DBG2("PUT CHAR Function: %c", c
);
1889 /* Returns number of chars in buffer, called by tty layer */
1890 static s32
ntty_chars_in_buffer(struct tty_struct
*tty
)
1892 struct port
*port
= tty
->driver_data
;
1893 struct nozomi
*dc
= get_dc_by_tty(tty
);
1896 if (unlikely(!dc
|| !port
)) {
1898 goto exit_in_buffer
;
1901 if (unlikely(!port
->tty_open_count
)) {
1902 dev_err(&dc
->pdev
->dev
, "No tty open?\n");
1904 goto exit_in_buffer
;
1907 rval
= __kfifo_len(port
->fifo_ul
);
1913 static struct tty_operations tty_ops
= {
1914 .ioctl
= ntty_ioctl
,
1916 .close
= ntty_close
,
1917 .write
= ntty_write
,
1918 .write_room
= ntty_write_room
,
1919 .unthrottle
= ntty_unthrottle
,
1920 .throttle
= ntty_throttle
,
1921 .chars_in_buffer
= ntty_chars_in_buffer
,
1922 .put_char
= ntty_put_char
,
1923 .tiocmget
= ntty_tiocmget
,
1924 .tiocmset
= ntty_tiocmset
,
1927 /* Module initialization */
1928 static struct pci_driver nozomi_driver
= {
1929 .name
= NOZOMI_NAME
,
1930 .id_table
= nozomi_pci_tbl
,
1931 .probe
= nozomi_card_init
,
1932 .remove
= __devexit_p(nozomi_card_exit
),
1935 static __init
int nozomi_init(void)
1939 printk(KERN_INFO
"Initializing %s\n", VERSION_STRING
);
1941 ntty_driver
= alloc_tty_driver(NTTY_TTY_MAXMINORS
);
1945 ntty_driver
->owner
= THIS_MODULE
;
1946 ntty_driver
->driver_name
= NOZOMI_NAME_TTY
;
1947 ntty_driver
->name
= "noz";
1948 ntty_driver
->major
= 0;
1949 ntty_driver
->type
= TTY_DRIVER_TYPE_SERIAL
;
1950 ntty_driver
->subtype
= SERIAL_TYPE_NORMAL
;
1951 ntty_driver
->flags
= TTY_DRIVER_REAL_RAW
| TTY_DRIVER_DYNAMIC_DEV
;
1952 ntty_driver
->init_termios
= tty_std_termios
;
1953 ntty_driver
->init_termios
.c_cflag
= B115200
| CS8
| CREAD
| \
1955 ntty_driver
->init_termios
.c_ispeed
= 115200;
1956 ntty_driver
->init_termios
.c_ospeed
= 115200;
1957 tty_set_operations(ntty_driver
, &tty_ops
);
1959 ret
= tty_register_driver(ntty_driver
);
1961 printk(KERN_ERR
"Nozomi: failed to register ntty driver\n");
1965 ret
= pci_register_driver(&nozomi_driver
);
1967 printk(KERN_ERR
"Nozomi: can't register pci driver\n");
1973 tty_unregister_driver(ntty_driver
);
1975 put_tty_driver(ntty_driver
);
1979 static __exit
void nozomi_exit(void)
1981 printk(KERN_INFO
"Unloading %s\n", DRIVER_DESC
);
1982 pci_unregister_driver(&nozomi_driver
);
1983 tty_unregister_driver(ntty_driver
);
1984 put_tty_driver(ntty_driver
);
1987 module_init(nozomi_init
);
1988 module_exit(nozomi_exit
);
1990 module_param(debug
, int, S_IRUGO
| S_IWUSR
);
1992 MODULE_LICENSE("Dual BSD/GPL");
1993 MODULE_DESCRIPTION(DRIVER_DESC
);