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 * --------------------------------------------------------------------------
41 /* Enable this to have a lot of debug printouts */
44 #include <linux/kernel.h>
45 #include <linux/module.h>
46 #include <linux/pci.h>
47 #include <linux/ioport.h>
48 #include <linux/tty.h>
49 #include <linux/tty_driver.h>
50 #include <linux/tty_flip.h>
51 #include <linux/sched.h>
52 #include <linux/serial.h>
53 #include <linux/interrupt.h>
54 #include <linux/kmod.h>
55 #include <linux/init.h>
56 #include <linux/kfifo.h>
57 #include <linux/uaccess.h>
58 #include <linux/slab.h>
59 #include <asm/byteorder.h>
61 #include <linux/delay.h>
64 #define VERSION_STRING DRIVER_DESC " 2.1d"
66 /* Macros definitions */
68 /* Default debug printout level */
69 #define NOZOMI_DEBUG_LEVEL 0x00
71 #define P_BUF_SIZE 128
72 #define NFO(_err_flag_, args...) \
74 char tmp[P_BUF_SIZE]; \
75 snprintf(tmp, sizeof(tmp), ##args); \
76 printk(_err_flag_ "[%d] %s(): %s\n", __LINE__, \
80 #define DBG1(args...) D_(0x01, ##args)
81 #define DBG2(args...) D_(0x02, ##args)
82 #define DBG3(args...) D_(0x04, ##args)
83 #define DBG4(args...) D_(0x08, ##args)
84 #define DBG5(args...) D_(0x10, ##args)
85 #define DBG6(args...) D_(0x20, ##args)
86 #define DBG7(args...) D_(0x40, ##args)
87 #define DBG8(args...) D_(0x80, ##args)
90 /* Do we need this settable at runtime? */
91 static int debug
= NOZOMI_DEBUG_LEVEL
;
93 #define D(lvl, args...) do \
94 {if (lvl & debug) NFO(KERN_DEBUG, ##args); } \
96 #define D_(lvl, args...) D(lvl, ##args)
98 /* These printouts are always printed */
102 #define D_(lvl, args...)
105 /* TODO: rewrite to optimize macros... */
107 #define TMP_BUF_MAX 256
109 #define DUMP(buf__,len__) \
111 char tbuf[TMP_BUF_MAX] = {0};\
113 snprintf(tbuf, len__ > TMP_BUF_MAX ? TMP_BUF_MAX : len__, "%s", buf__);\
114 if (tbuf[len__-2] == '\r') {\
115 tbuf[len__-2] = 'r';\
117 DBG1("SENDING: '%s' (%d+n)", tbuf, len__);\
119 DBG1("SENDING: '%s' (%d)", tbuf, len__);\
124 #define NOZOMI_NAME "nozomi"
125 #define NOZOMI_NAME_TTY "nozomi_tty"
126 #define DRIVER_DESC "Nozomi driver"
128 #define NTTY_TTY_MAXMINORS 256
129 #define NTTY_FIFO_BUFFER_SIZE 8192
131 /* Must be power of 2 */
132 #define FIFO_BUFFER_SIZE_UL 8192
134 /* Size of tmp send buffer to card */
135 #define SEND_BUF_MAX 1024
136 #define RECEIVE_BUF_MAX 4
139 #define R_IIR 0x0000 /* Interrupt Identity Register */
140 #define R_FCR 0x0000 /* Flow Control Register */
141 #define R_IER 0x0004 /* Interrupt Enable Register */
143 #define CONFIG_MAGIC 0xEFEFFEFE
144 #define TOGGLE_VALID 0x0000
146 /* Definition of interrupt tokens */
147 #define MDM_DL1 0x0001
148 #define MDM_UL1 0x0002
149 #define MDM_DL2 0x0004
150 #define MDM_UL2 0x0008
151 #define DIAG_DL1 0x0010
152 #define DIAG_DL2 0x0020
153 #define DIAG_UL 0x0040
154 #define APP1_DL 0x0080
155 #define APP1_UL 0x0100
156 #define APP2_DL 0x0200
157 #define APP2_UL 0x0400
158 #define CTRL_DL 0x0800
159 #define CTRL_UL 0x1000
162 #define MDM_DL (MDM_DL1 | MDM_DL2)
163 #define MDM_UL (MDM_UL1 | MDM_UL2)
164 #define DIAG_DL (DIAG_DL1 | DIAG_DL2)
166 /* modem signal definition */
167 #define CTRL_DSR 0x0001
168 #define CTRL_DCD 0x0002
169 #define CTRL_RI 0x0004
170 #define CTRL_CTS 0x0008
172 #define CTRL_DTR 0x0001
173 #define CTRL_RTS 0x0002
176 #define NOZOMI_MAX_PORTS 5
177 #define NOZOMI_MAX_CARDS (NTTY_TTY_MAXMINORS / MAX_PORT)
179 /* Type definitions */
182 * There are two types of nozomi cards,
183 * one with 2048 memory and with 8192 memory
186 F32_2
= 2048, /* 512 bytes downlink + uplink * 2 -> 2048 */
187 F32_8
= 8192, /* 3072 bytes downl. + 1024 bytes uplink * 2 -> 8192 */
190 /* Initialization states a card can be in */
192 NOZOMI_STATE_UKNOWN
= 0,
193 NOZOMI_STATE_ENABLED
= 1, /* pci device enabled */
194 NOZOMI_STATE_ALLOCATED
= 2, /* config setup done */
195 NOZOMI_STATE_READY
= 3, /* flowcontrols received */
198 /* Two different toggle channels exist */
204 /* Port definition for the card regarding flow control */
205 enum ctrl_port_type
{
214 /* Ports that the nozomi has */
228 unsigned int enabled
:5; /*
229 * Toggle fields are valid if enabled is 0,
230 * else A-channels must always be used.
232 unsigned int diag_dl
:1;
233 unsigned int mdm_dl
:1;
234 unsigned int mdm_ul
:1;
235 } __attribute__ ((packed
));
237 /* Configuration table to read at startup of card */
238 /* Is for now only needed during initialization phase */
239 struct config_table
{
241 u16 product_information
;
244 struct toggles toggle
;
247 * If this is 64, it can hold
248 * 60 bytes + 4 that is length field
254 * If this is 64, it can hold
255 * 60 bytes + 4 that is length field
270 } __attribute__ ((packed
));
272 /* This stores all control downlink flags */
275 unsigned int reserved
:4;
280 } __attribute__ ((packed
));
282 /* This stores all control uplink flags */
285 unsigned int reserved
:6;
288 } __attribute__ ((packed
));
293 /* This represents the toggle information */
295 unsigned int mdm_ul
:1;
296 unsigned int mdm_dl
:1;
297 unsigned int diag_dl
:1;
298 unsigned int enabled
:5; /*
299 * Toggle fields are valid if enabled is 0,
300 * else A-channels must always be used.
302 } __attribute__ ((packed
));
304 /* Configuration table to read at startup of card */
305 struct config_table
{
308 u16 product_information
;
309 struct toggles toggle
;
313 * If this is 64, it can hold
314 * 60 bytes + 4 that is length field
330 } __attribute__ ((packed
));
332 /* This stores all control downlink flags */
338 unsigned int reserverd
:4;
340 } __attribute__ ((packed
));
342 /* This stores all control uplink flags */
346 unsigned int reserved
:6;
348 } __attribute__ ((packed
));
351 /* This holds all information that is needed regarding a port */
353 struct tty_port port
;
354 u8 update_flow_control
;
355 struct ctrl_ul ctrl_ul
;
356 struct ctrl_dl ctrl_dl
;
357 struct kfifo fifo_ul
;
358 void __iomem
*dl_addr
[2];
361 void __iomem
*ul_addr
[2];
366 wait_queue_head_t tty_wait
;
367 struct async_icount tty_icount
;
372 /* Private data one for each card in the system */
374 void __iomem
*base_addr
;
377 /* Pointers to registers */
378 void __iomem
*reg_iir
;
379 void __iomem
*reg_fcr
;
380 void __iomem
*reg_ier
;
383 enum card_type card_type
;
384 struct config_table config_table
; /* Configuration table */
385 struct pci_dev
*pdev
;
386 struct port port
[NOZOMI_MAX_PORTS
];
389 spinlock_t spin_mutex
; /* secures access to registers and tty */
391 unsigned int index_start
;
392 enum card_state state
;
396 /* This is a data packet that is read or written to/from card */
398 u32 size
; /* size is the length of the data buffer */
400 } __attribute__ ((packed
));
402 /* Global variables */
403 static const struct pci_device_id nozomi_pci_tbl
[] __devinitconst
= {
404 {PCI_DEVICE(0x1931, 0x000c)}, /* Nozomi HSDPA */
408 MODULE_DEVICE_TABLE(pci
, nozomi_pci_tbl
);
410 static struct nozomi
*ndevs
[NOZOMI_MAX_CARDS
];
411 static struct tty_driver
*ntty_driver
;
413 static const struct tty_port_operations noz_tty_port_ops
;
416 * find card by tty_index
418 static inline struct nozomi
*get_dc_by_tty(const struct tty_struct
*tty
)
420 return tty
? ndevs
[tty
->index
/ MAX_PORT
] : NULL
;
423 static inline struct port
*get_port_by_tty(const struct tty_struct
*tty
)
425 struct nozomi
*ndev
= get_dc_by_tty(tty
);
426 return ndev
? &ndev
->port
[tty
->index
% MAX_PORT
] : NULL
;
435 static void read_mem32(u32
*buf
, const void __iomem
*mem_addr_start
,
439 const u32 __iomem
*ptr
= mem_addr_start
;
442 if (unlikely(!ptr
|| !buf
))
445 /* shortcut for extremely often used cases */
446 switch (size_bytes
) {
447 case 2: /* 2 bytes */
449 *buf16
= __le16_to_cpu(readw(ptr
));
452 case 4: /* 4 bytes */
453 *(buf
) = __le32_to_cpu(readl(ptr
));
458 while (i
< size_bytes
) {
459 if (size_bytes
- i
== 2) {
460 /* Handle 2 bytes in the end */
462 *(buf16
) = __le16_to_cpu(readw(ptr
));
466 *(buf
) = __le32_to_cpu(readl(ptr
));
481 static u32
write_mem32(void __iomem
*mem_addr_start
, const u32
*buf
,
485 u32 __iomem
*ptr
= mem_addr_start
;
488 if (unlikely(!ptr
|| !buf
))
491 /* shortcut for extremely often used cases */
492 switch (size_bytes
) {
493 case 2: /* 2 bytes */
494 buf16
= (const u16
*)buf
;
495 writew(__cpu_to_le16(*buf16
), ptr
);
499 * also needs to write 4 bytes in this case
500 * so falling through..
502 case 4: /* 4 bytes */
503 writel(__cpu_to_le32(*buf
), ptr
);
508 while (i
< size_bytes
) {
509 if (size_bytes
- i
== 2) {
511 buf16
= (const u16
*)buf
;
512 writew(__cpu_to_le16(*buf16
), ptr
);
516 writel(__cpu_to_le32(*buf
), ptr
);
525 /* Setup pointers to different channels and also setup buffer sizes. */
526 static void setup_memory(struct nozomi
*dc
)
528 void __iomem
*offset
= dc
->base_addr
+ dc
->config_table
.dl_start
;
529 /* The length reported is including the length field of 4 bytes,
530 * hence subtract with 4.
532 const u16 buff_offset
= 4;
534 /* Modem port dl configuration */
535 dc
->port
[PORT_MDM
].dl_addr
[CH_A
] = offset
;
536 dc
->port
[PORT_MDM
].dl_addr
[CH_B
] =
537 (offset
+= dc
->config_table
.dl_mdm_len1
);
538 dc
->port
[PORT_MDM
].dl_size
[CH_A
] =
539 dc
->config_table
.dl_mdm_len1
- buff_offset
;
540 dc
->port
[PORT_MDM
].dl_size
[CH_B
] =
541 dc
->config_table
.dl_mdm_len2
- buff_offset
;
543 /* Diag port dl configuration */
544 dc
->port
[PORT_DIAG
].dl_addr
[CH_A
] =
545 (offset
+= dc
->config_table
.dl_mdm_len2
);
546 dc
->port
[PORT_DIAG
].dl_size
[CH_A
] =
547 dc
->config_table
.dl_diag_len1
- buff_offset
;
548 dc
->port
[PORT_DIAG
].dl_addr
[CH_B
] =
549 (offset
+= dc
->config_table
.dl_diag_len1
);
550 dc
->port
[PORT_DIAG
].dl_size
[CH_B
] =
551 dc
->config_table
.dl_diag_len2
- buff_offset
;
553 /* App1 port dl configuration */
554 dc
->port
[PORT_APP1
].dl_addr
[CH_A
] =
555 (offset
+= dc
->config_table
.dl_diag_len2
);
556 dc
->port
[PORT_APP1
].dl_size
[CH_A
] =
557 dc
->config_table
.dl_app1_len
- buff_offset
;
559 /* App2 port dl configuration */
560 dc
->port
[PORT_APP2
].dl_addr
[CH_A
] =
561 (offset
+= dc
->config_table
.dl_app1_len
);
562 dc
->port
[PORT_APP2
].dl_size
[CH_A
] =
563 dc
->config_table
.dl_app2_len
- buff_offset
;
565 /* Ctrl dl configuration */
566 dc
->port
[PORT_CTRL
].dl_addr
[CH_A
] =
567 (offset
+= dc
->config_table
.dl_app2_len
);
568 dc
->port
[PORT_CTRL
].dl_size
[CH_A
] =
569 dc
->config_table
.dl_ctrl_len
- buff_offset
;
571 offset
= dc
->base_addr
+ dc
->config_table
.ul_start
;
573 /* Modem Port ul configuration */
574 dc
->port
[PORT_MDM
].ul_addr
[CH_A
] = offset
;
575 dc
->port
[PORT_MDM
].ul_size
[CH_A
] =
576 dc
->config_table
.ul_mdm_len1
- buff_offset
;
577 dc
->port
[PORT_MDM
].ul_addr
[CH_B
] =
578 (offset
+= dc
->config_table
.ul_mdm_len1
);
579 dc
->port
[PORT_MDM
].ul_size
[CH_B
] =
580 dc
->config_table
.ul_mdm_len2
- buff_offset
;
582 /* Diag port ul configuration */
583 dc
->port
[PORT_DIAG
].ul_addr
[CH_A
] =
584 (offset
+= dc
->config_table
.ul_mdm_len2
);
585 dc
->port
[PORT_DIAG
].ul_size
[CH_A
] =
586 dc
->config_table
.ul_diag_len
- buff_offset
;
588 /* App1 port ul configuration */
589 dc
->port
[PORT_APP1
].ul_addr
[CH_A
] =
590 (offset
+= dc
->config_table
.ul_diag_len
);
591 dc
->port
[PORT_APP1
].ul_size
[CH_A
] =
592 dc
->config_table
.ul_app1_len
- buff_offset
;
594 /* App2 port ul configuration */
595 dc
->port
[PORT_APP2
].ul_addr
[CH_A
] =
596 (offset
+= dc
->config_table
.ul_app1_len
);
597 dc
->port
[PORT_APP2
].ul_size
[CH_A
] =
598 dc
->config_table
.ul_app2_len
- buff_offset
;
600 /* Ctrl ul configuration */
601 dc
->port
[PORT_CTRL
].ul_addr
[CH_A
] =
602 (offset
+= dc
->config_table
.ul_app2_len
);
603 dc
->port
[PORT_CTRL
].ul_size
[CH_A
] =
604 dc
->config_table
.ul_ctrl_len
- buff_offset
;
607 /* Dump config table under initalization phase */
609 static void dump_table(const struct nozomi
*dc
)
611 DBG3("signature: 0x%08X", dc
->config_table
.signature
);
612 DBG3("version: 0x%04X", dc
->config_table
.version
);
613 DBG3("product_information: 0x%04X", \
614 dc
->config_table
.product_information
);
615 DBG3("toggle enabled: %d", dc
->config_table
.toggle
.enabled
);
616 DBG3("toggle up_mdm: %d", dc
->config_table
.toggle
.mdm_ul
);
617 DBG3("toggle dl_mdm: %d", dc
->config_table
.toggle
.mdm_dl
);
618 DBG3("toggle dl_dbg: %d", dc
->config_table
.toggle
.diag_dl
);
620 DBG3("dl_start: 0x%04X", dc
->config_table
.dl_start
);
621 DBG3("dl_mdm_len0: 0x%04X, %d", dc
->config_table
.dl_mdm_len1
,
622 dc
->config_table
.dl_mdm_len1
);
623 DBG3("dl_mdm_len1: 0x%04X, %d", dc
->config_table
.dl_mdm_len2
,
624 dc
->config_table
.dl_mdm_len2
);
625 DBG3("dl_diag_len0: 0x%04X, %d", dc
->config_table
.dl_diag_len1
,
626 dc
->config_table
.dl_diag_len1
);
627 DBG3("dl_diag_len1: 0x%04X, %d", dc
->config_table
.dl_diag_len2
,
628 dc
->config_table
.dl_diag_len2
);
629 DBG3("dl_app1_len: 0x%04X, %d", dc
->config_table
.dl_app1_len
,
630 dc
->config_table
.dl_app1_len
);
631 DBG3("dl_app2_len: 0x%04X, %d", dc
->config_table
.dl_app2_len
,
632 dc
->config_table
.dl_app2_len
);
633 DBG3("dl_ctrl_len: 0x%04X, %d", dc
->config_table
.dl_ctrl_len
,
634 dc
->config_table
.dl_ctrl_len
);
635 DBG3("ul_start: 0x%04X, %d", dc
->config_table
.ul_start
,
636 dc
->config_table
.ul_start
);
637 DBG3("ul_mdm_len[0]: 0x%04X, %d", dc
->config_table
.ul_mdm_len1
,
638 dc
->config_table
.ul_mdm_len1
);
639 DBG3("ul_mdm_len[1]: 0x%04X, %d", dc
->config_table
.ul_mdm_len2
,
640 dc
->config_table
.ul_mdm_len2
);
641 DBG3("ul_diag_len: 0x%04X, %d", dc
->config_table
.ul_diag_len
,
642 dc
->config_table
.ul_diag_len
);
643 DBG3("ul_app1_len: 0x%04X, %d", dc
->config_table
.ul_app1_len
,
644 dc
->config_table
.ul_app1_len
);
645 DBG3("ul_app2_len: 0x%04X, %d", dc
->config_table
.ul_app2_len
,
646 dc
->config_table
.ul_app2_len
);
647 DBG3("ul_ctrl_len: 0x%04X, %d", dc
->config_table
.ul_ctrl_len
,
648 dc
->config_table
.ul_ctrl_len
);
651 static inline void dump_table(const struct nozomi
*dc
) { }
655 * Read configuration table from card under intalization phase
656 * Returns 1 if ok, else 0
658 static int nozomi_read_config_table(struct nozomi
*dc
)
660 read_mem32((u32
*) &dc
->config_table
, dc
->base_addr
+ 0,
661 sizeof(struct config_table
));
663 if (dc
->config_table
.signature
!= CONFIG_MAGIC
) {
664 dev_err(&dc
->pdev
->dev
, "ConfigTable Bad! 0x%08X != 0x%08X\n",
665 dc
->config_table
.signature
, CONFIG_MAGIC
);
669 if ((dc
->config_table
.version
== 0)
670 || (dc
->config_table
.toggle
.enabled
== TOGGLE_VALID
)) {
672 DBG1("Second phase, configuring card");
676 dc
->port
[PORT_MDM
].toggle_ul
= dc
->config_table
.toggle
.mdm_ul
;
677 dc
->port
[PORT_MDM
].toggle_dl
= dc
->config_table
.toggle
.mdm_dl
;
678 dc
->port
[PORT_DIAG
].toggle_dl
= dc
->config_table
.toggle
.diag_dl
;
679 DBG1("toggle ports: MDM UL:%d MDM DL:%d, DIAG DL:%d",
680 dc
->port
[PORT_MDM
].toggle_ul
,
681 dc
->port
[PORT_MDM
].toggle_dl
, dc
->port
[PORT_DIAG
].toggle_dl
);
685 for (i
= PORT_MDM
; i
< MAX_PORT
; i
++) {
686 memset(&dc
->port
[i
].ctrl_dl
, 0, sizeof(struct ctrl_dl
));
687 memset(&dc
->port
[i
].ctrl_ul
, 0, sizeof(struct ctrl_ul
));
690 /* Enable control channel */
691 dc
->last_ier
= dc
->last_ier
| CTRL_DL
;
692 writew(dc
->last_ier
, dc
->reg_ier
);
694 dc
->state
= NOZOMI_STATE_ALLOCATED
;
695 dev_info(&dc
->pdev
->dev
, "Initialization OK!\n");
699 if ((dc
->config_table
.version
> 0)
700 && (dc
->config_table
.toggle
.enabled
!= TOGGLE_VALID
)) {
702 DBG1("First phase: pushing upload buffers, clearing download");
704 dev_info(&dc
->pdev
->dev
, "Version of card: %d\n",
705 dc
->config_table
.version
);
707 /* Here we should disable all I/O over F32. */
711 * We should send ALL channel pair tokens back along
715 /* push upload modem buffers */
716 write_mem32(dc
->port
[PORT_MDM
].ul_addr
[CH_A
],
718 write_mem32(dc
->port
[PORT_MDM
].ul_addr
[CH_B
],
721 writew(MDM_UL
| DIAG_DL
| MDM_DL
, dc
->reg_fcr
);
723 DBG1("First phase done");
729 /* Enable uplink interrupts */
730 static void enable_transmit_ul(enum port_type port
, struct nozomi
*dc
)
732 static const u16 mask
[] = {MDM_UL
, DIAG_UL
, APP1_UL
, APP2_UL
, CTRL_UL
};
734 if (port
< NOZOMI_MAX_PORTS
) {
735 dc
->last_ier
|= mask
[port
];
736 writew(dc
->last_ier
, dc
->reg_ier
);
738 dev_err(&dc
->pdev
->dev
, "Called with wrong port?\n");
742 /* Disable uplink interrupts */
743 static void disable_transmit_ul(enum port_type port
, struct nozomi
*dc
)
745 static const u16 mask
[] =
746 {~MDM_UL
, ~DIAG_UL
, ~APP1_UL
, ~APP2_UL
, ~CTRL_UL
};
748 if (port
< NOZOMI_MAX_PORTS
) {
749 dc
->last_ier
&= mask
[port
];
750 writew(dc
->last_ier
, dc
->reg_ier
);
752 dev_err(&dc
->pdev
->dev
, "Called with wrong port?\n");
756 /* Enable downlink interrupts */
757 static void enable_transmit_dl(enum port_type port
, struct nozomi
*dc
)
759 static const u16 mask
[] = {MDM_DL
, DIAG_DL
, APP1_DL
, APP2_DL
, CTRL_DL
};
761 if (port
< NOZOMI_MAX_PORTS
) {
762 dc
->last_ier
|= mask
[port
];
763 writew(dc
->last_ier
, dc
->reg_ier
);
765 dev_err(&dc
->pdev
->dev
, "Called with wrong port?\n");
769 /* Disable downlink interrupts */
770 static void disable_transmit_dl(enum port_type port
, struct nozomi
*dc
)
772 static const u16 mask
[] =
773 {~MDM_DL
, ~DIAG_DL
, ~APP1_DL
, ~APP2_DL
, ~CTRL_DL
};
775 if (port
< NOZOMI_MAX_PORTS
) {
776 dc
->last_ier
&= mask
[port
];
777 writew(dc
->last_ier
, dc
->reg_ier
);
779 dev_err(&dc
->pdev
->dev
, "Called with wrong port?\n");
784 * Return 1 - send buffer to card and ack.
785 * Return 0 - don't ack, don't send buffer to card.
787 static int send_data(enum port_type index
, struct nozomi
*dc
)
790 struct port
*port
= &dc
->port
[index
];
791 const u8 toggle
= port
->toggle_ul
;
792 void __iomem
*addr
= port
->ul_addr
[toggle
];
793 const u32 ul_size
= port
->ul_size
[toggle
];
794 struct tty_struct
*tty
= tty_port_tty_get(&port
->port
);
796 /* Get data from tty and place in buf for now */
797 size
= kfifo_out(&port
->fifo_ul
, dc
->send_buf
,
798 ul_size
< SEND_BUF_MAX
? ul_size
: SEND_BUF_MAX
);
801 DBG4("No more data to send, disable link:");
806 /* DUMP(buf, size); */
808 /* Write length + data */
809 write_mem32(addr
, (u32
*) &size
, 4);
810 write_mem32(addr
+ 4, (u32
*) dc
->send_buf
, size
);
819 /* If all data has been read, return 1, else 0 */
820 static int receive_data(enum port_type index
, struct nozomi
*dc
)
822 u8 buf
[RECEIVE_BUF_MAX
] = { 0 };
825 struct port
*port
= &dc
->port
[index
];
826 void __iomem
*addr
= port
->dl_addr
[port
->toggle_dl
];
827 struct tty_struct
*tty
= tty_port_tty_get(&port
->port
);
830 if (unlikely(!tty
)) {
831 DBG1("tty not open for port: %d?", index
);
835 read_mem32((u32
*) &size
, addr
, 4);
836 /* DBG1( "%d bytes port: %d", size, index); */
838 if (test_bit(TTY_THROTTLED
, &tty
->flags
)) {
839 DBG1("No room in tty, don't read data, don't ack interrupt, "
840 "disable interrupt");
842 /* disable interrupt in downlink... */
843 disable_transmit_dl(index
, dc
);
848 if (unlikely(size
== 0)) {
849 dev_err(&dc
->pdev
->dev
, "size == 0?\n");
855 read_mem32((u32
*) buf
, addr
+ offset
, RECEIVE_BUF_MAX
);
858 tty_insert_flip_char(tty
, buf
[0], TTY_NORMAL
);
860 } else if (size
< RECEIVE_BUF_MAX
) {
861 size
-= tty_insert_flip_string(tty
, (char *) buf
, size
);
863 i
= tty_insert_flip_string(tty
, \
864 (char *) buf
, RECEIVE_BUF_MAX
);
870 set_bit(index
, &dc
->flip
);
877 /* Debug for interrupts */
879 static char *interrupt2str(u16 interrupt
)
881 static char buf
[TMP_BUF_MAX
];
884 interrupt
& MDM_DL1
? p
+= snprintf(p
, TMP_BUF_MAX
, "MDM_DL1 ") : NULL
;
885 interrupt
& MDM_DL2
? p
+= snprintf(p
, TMP_BUF_MAX
- (p
- buf
),
888 interrupt
& MDM_UL1
? p
+= snprintf(p
, TMP_BUF_MAX
- (p
- buf
),
890 interrupt
& MDM_UL2
? p
+= snprintf(p
, TMP_BUF_MAX
- (p
- buf
),
893 interrupt
& DIAG_DL1
? p
+= snprintf(p
, TMP_BUF_MAX
- (p
- buf
),
895 interrupt
& DIAG_DL2
? p
+= snprintf(p
, TMP_BUF_MAX
- (p
- buf
),
898 interrupt
& DIAG_UL
? p
+= snprintf(p
, TMP_BUF_MAX
- (p
- buf
),
901 interrupt
& APP1_DL
? p
+= snprintf(p
, TMP_BUF_MAX
- (p
- buf
),
903 interrupt
& APP2_DL
? p
+= snprintf(p
, TMP_BUF_MAX
- (p
- buf
),
906 interrupt
& APP1_UL
? p
+= snprintf(p
, TMP_BUF_MAX
- (p
- buf
),
908 interrupt
& APP2_UL
? p
+= snprintf(p
, TMP_BUF_MAX
- (p
- buf
),
911 interrupt
& CTRL_DL
? p
+= snprintf(p
, TMP_BUF_MAX
- (p
- buf
),
913 interrupt
& CTRL_UL
? p
+= snprintf(p
, TMP_BUF_MAX
- (p
- buf
),
916 interrupt
& RESET
? p
+= snprintf(p
, TMP_BUF_MAX
- (p
- buf
),
924 * Receive flow control
925 * Return 1 - If ok, else 0
927 static int receive_flow_control(struct nozomi
*dc
)
929 enum port_type port
= PORT_MDM
;
930 struct ctrl_dl ctrl_dl
;
931 struct ctrl_dl old_ctrl
;
934 read_mem32((u32
*) &ctrl_dl
, dc
->port
[PORT_CTRL
].dl_addr
[CH_A
], 2);
936 switch (ctrl_dl
.port
) {
938 DBG1("The Base Band sends this value as a response to a "
939 "request for IMSI detach sent over the control "
940 "channel uplink (see section 7.6.1).");
948 enable_ier
= DIAG_DL
;
952 enable_ier
= APP1_DL
;
956 enable_ier
= APP2_DL
;
957 if (dc
->state
== NOZOMI_STATE_ALLOCATED
) {
959 * After card initialization the flow control
960 * received for APP2 is always the last
962 dc
->state
= NOZOMI_STATE_READY
;
963 dev_info(&dc
->pdev
->dev
, "Device READY!\n");
967 dev_err(&dc
->pdev
->dev
,
968 "ERROR: flow control received for non-existing port\n");
972 DBG1("0x%04X->0x%04X", *((u16
*)&dc
->port
[port
].ctrl_dl
),
975 old_ctrl
= dc
->port
[port
].ctrl_dl
;
976 dc
->port
[port
].ctrl_dl
= ctrl_dl
;
978 if (old_ctrl
.CTS
== 1 && ctrl_dl
.CTS
== 0) {
979 DBG1("Disable interrupt (0x%04X) on port: %d",
981 disable_transmit_ul(port
, dc
);
983 } else if (old_ctrl
.CTS
== 0 && ctrl_dl
.CTS
== 1) {
985 if (kfifo_len(&dc
->port
[port
].fifo_ul
)) {
986 DBG1("Enable interrupt (0x%04X) on port: %d",
988 DBG1("Data in buffer [%d], enable transmit! ",
989 kfifo_len(&dc
->port
[port
].fifo_ul
));
990 enable_transmit_ul(port
, dc
);
992 DBG1("No data in buffer...");
996 if (*(u16
*)&old_ctrl
== *(u16
*)&ctrl_dl
) {
997 DBG1(" No change in mctrl");
1000 /* Update statistics */
1001 if (old_ctrl
.CTS
!= ctrl_dl
.CTS
)
1002 dc
->port
[port
].tty_icount
.cts
++;
1003 if (old_ctrl
.DSR
!= ctrl_dl
.DSR
)
1004 dc
->port
[port
].tty_icount
.dsr
++;
1005 if (old_ctrl
.RI
!= ctrl_dl
.RI
)
1006 dc
->port
[port
].tty_icount
.rng
++;
1007 if (old_ctrl
.DCD
!= ctrl_dl
.DCD
)
1008 dc
->port
[port
].tty_icount
.dcd
++;
1010 wake_up_interruptible(&dc
->port
[port
].tty_wait
);
1012 DBG1("port: %d DCD(%d), CTS(%d), RI(%d), DSR(%d)",
1014 dc
->port
[port
].tty_icount
.dcd
, dc
->port
[port
].tty_icount
.cts
,
1015 dc
->port
[port
].tty_icount
.rng
, dc
->port
[port
].tty_icount
.dsr
);
1020 static enum ctrl_port_type
port2ctrl(enum port_type port
,
1021 const struct nozomi
*dc
)
1033 dev_err(&dc
->pdev
->dev
,
1034 "ERROR: send flow control " \
1035 "received for non-existing port\n");
1041 * Send flow control, can only update one channel at a time
1042 * Return 0 - If we have updated all flow control
1043 * Return 1 - If we need to update more flow control, ack current enable more
1045 static int send_flow_control(struct nozomi
*dc
)
1047 u32 i
, more_flow_control_to_be_updated
= 0;
1050 for (i
= PORT_MDM
; i
< MAX_PORT
; i
++) {
1051 if (dc
->port
[i
].update_flow_control
) {
1052 if (more_flow_control_to_be_updated
) {
1053 /* We have more flow control to be updated */
1056 dc
->port
[i
].ctrl_ul
.port
= port2ctrl(i
, dc
);
1057 ctrl
= (u16
*)&dc
->port
[i
].ctrl_ul
;
1058 write_mem32(dc
->port
[PORT_CTRL
].ul_addr
[0], \
1060 dc
->port
[i
].update_flow_control
= 0;
1061 more_flow_control_to_be_updated
= 1;
1068 * Handle downlink data, ports that are handled are modem and diagnostics
1070 * Return 0 - toggle fields are out of sync
1072 static int handle_data_dl(struct nozomi
*dc
, enum port_type port
, u8
*toggle
,
1073 u16 read_iir
, u16 mask1
, u16 mask2
)
1075 if (*toggle
== 0 && read_iir
& mask1
) {
1076 if (receive_data(port
, dc
)) {
1077 writew(mask1
, dc
->reg_fcr
);
1078 *toggle
= !(*toggle
);
1081 if (read_iir
& mask2
) {
1082 if (receive_data(port
, dc
)) {
1083 writew(mask2
, dc
->reg_fcr
);
1084 *toggle
= !(*toggle
);
1087 } else if (*toggle
== 1 && read_iir
& mask2
) {
1088 if (receive_data(port
, dc
)) {
1089 writew(mask2
, dc
->reg_fcr
);
1090 *toggle
= !(*toggle
);
1093 if (read_iir
& mask1
) {
1094 if (receive_data(port
, dc
)) {
1095 writew(mask1
, dc
->reg_fcr
);
1096 *toggle
= !(*toggle
);
1100 dev_err(&dc
->pdev
->dev
, "port out of sync!, toggle:%d\n",
1108 * Handle uplink data, this is currently for the modem port
1110 * Return 0 - toggle field are out of sync
1112 static int handle_data_ul(struct nozomi
*dc
, enum port_type port
, u16 read_iir
)
1114 u8
*toggle
= &(dc
->port
[port
].toggle_ul
);
1116 if (*toggle
== 0 && read_iir
& MDM_UL1
) {
1117 dc
->last_ier
&= ~MDM_UL
;
1118 writew(dc
->last_ier
, dc
->reg_ier
);
1119 if (send_data(port
, dc
)) {
1120 writew(MDM_UL1
, dc
->reg_fcr
);
1121 dc
->last_ier
= dc
->last_ier
| MDM_UL
;
1122 writew(dc
->last_ier
, dc
->reg_ier
);
1126 if (read_iir
& MDM_UL2
) {
1127 dc
->last_ier
&= ~MDM_UL
;
1128 writew(dc
->last_ier
, dc
->reg_ier
);
1129 if (send_data(port
, dc
)) {
1130 writew(MDM_UL2
, dc
->reg_fcr
);
1131 dc
->last_ier
= dc
->last_ier
| MDM_UL
;
1132 writew(dc
->last_ier
, dc
->reg_ier
);
1137 } else if (*toggle
== 1 && read_iir
& MDM_UL2
) {
1138 dc
->last_ier
&= ~MDM_UL
;
1139 writew(dc
->last_ier
, dc
->reg_ier
);
1140 if (send_data(port
, dc
)) {
1141 writew(MDM_UL2
, dc
->reg_fcr
);
1142 dc
->last_ier
= dc
->last_ier
| MDM_UL
;
1143 writew(dc
->last_ier
, dc
->reg_ier
);
1147 if (read_iir
& MDM_UL1
) {
1148 dc
->last_ier
&= ~MDM_UL
;
1149 writew(dc
->last_ier
, dc
->reg_ier
);
1150 if (send_data(port
, dc
)) {
1151 writew(MDM_UL1
, dc
->reg_fcr
);
1152 dc
->last_ier
= dc
->last_ier
| MDM_UL
;
1153 writew(dc
->last_ier
, dc
->reg_ier
);
1158 writew(read_iir
& MDM_UL
, dc
->reg_fcr
);
1159 dev_err(&dc
->pdev
->dev
, "port out of sync!\n");
1165 static irqreturn_t
interrupt_handler(int irq
, void *dev_id
)
1167 struct nozomi
*dc
= dev_id
;
1174 spin_lock(&dc
->spin_mutex
);
1175 read_iir
= readw(dc
->reg_iir
);
1178 if (read_iir
== (u16
)-1)
1181 * Just handle interrupt enabled in IER
1182 * (by masking with dc->last_ier)
1184 read_iir
&= dc
->last_ier
;
1190 DBG4("%s irq:0x%04X, prev:0x%04X", interrupt2str(read_iir
), read_iir
,
1193 if (read_iir
& RESET
) {
1194 if (unlikely(!nozomi_read_config_table(dc
))) {
1196 writew(dc
->last_ier
, dc
->reg_ier
);
1197 dev_err(&dc
->pdev
->dev
, "Could not read status from "
1198 "card, we should disable interface\n");
1200 writew(RESET
, dc
->reg_fcr
);
1202 /* No more useful info if this was the reset interrupt. */
1205 if (read_iir
& CTRL_UL
) {
1207 dc
->last_ier
&= ~CTRL_UL
;
1208 writew(dc
->last_ier
, dc
->reg_ier
);
1209 if (send_flow_control(dc
)) {
1210 writew(CTRL_UL
, dc
->reg_fcr
);
1211 dc
->last_ier
= dc
->last_ier
| CTRL_UL
;
1212 writew(dc
->last_ier
, dc
->reg_ier
);
1215 if (read_iir
& CTRL_DL
) {
1216 receive_flow_control(dc
);
1217 writew(CTRL_DL
, dc
->reg_fcr
);
1219 if (read_iir
& MDM_DL
) {
1220 if (!handle_data_dl(dc
, PORT_MDM
,
1221 &(dc
->port
[PORT_MDM
].toggle_dl
), read_iir
,
1222 MDM_DL1
, MDM_DL2
)) {
1223 dev_err(&dc
->pdev
->dev
, "MDM_DL out of sync!\n");
1227 if (read_iir
& MDM_UL
) {
1228 if (!handle_data_ul(dc
, PORT_MDM
, read_iir
)) {
1229 dev_err(&dc
->pdev
->dev
, "MDM_UL out of sync!\n");
1233 if (read_iir
& DIAG_DL
) {
1234 if (!handle_data_dl(dc
, PORT_DIAG
,
1235 &(dc
->port
[PORT_DIAG
].toggle_dl
), read_iir
,
1236 DIAG_DL1
, DIAG_DL2
)) {
1237 dev_err(&dc
->pdev
->dev
, "DIAG_DL out of sync!\n");
1241 if (read_iir
& DIAG_UL
) {
1242 dc
->last_ier
&= ~DIAG_UL
;
1243 writew(dc
->last_ier
, dc
->reg_ier
);
1244 if (send_data(PORT_DIAG
, dc
)) {
1245 writew(DIAG_UL
, dc
->reg_fcr
);
1246 dc
->last_ier
= dc
->last_ier
| DIAG_UL
;
1247 writew(dc
->last_ier
, dc
->reg_ier
);
1250 if (read_iir
& APP1_DL
) {
1251 if (receive_data(PORT_APP1
, dc
))
1252 writew(APP1_DL
, dc
->reg_fcr
);
1254 if (read_iir
& APP1_UL
) {
1255 dc
->last_ier
&= ~APP1_UL
;
1256 writew(dc
->last_ier
, dc
->reg_ier
);
1257 if (send_data(PORT_APP1
, dc
)) {
1258 writew(APP1_UL
, dc
->reg_fcr
);
1259 dc
->last_ier
= dc
->last_ier
| APP1_UL
;
1260 writew(dc
->last_ier
, dc
->reg_ier
);
1263 if (read_iir
& APP2_DL
) {
1264 if (receive_data(PORT_APP2
, dc
))
1265 writew(APP2_DL
, dc
->reg_fcr
);
1267 if (read_iir
& APP2_UL
) {
1268 dc
->last_ier
&= ~APP2_UL
;
1269 writew(dc
->last_ier
, dc
->reg_ier
);
1270 if (send_data(PORT_APP2
, dc
)) {
1271 writew(APP2_UL
, dc
->reg_fcr
);
1272 dc
->last_ier
= dc
->last_ier
| APP2_UL
;
1273 writew(dc
->last_ier
, dc
->reg_ier
);
1278 spin_unlock(&dc
->spin_mutex
);
1279 for (a
= 0; a
< NOZOMI_MAX_PORTS
; a
++) {
1280 struct tty_struct
*tty
;
1281 if (test_and_clear_bit(a
, &dc
->flip
)) {
1282 tty
= tty_port_tty_get(&dc
->port
[a
].port
);
1284 tty_flip_buffer_push(tty
);
1290 spin_unlock(&dc
->spin_mutex
);
1294 static void nozomi_get_card_type(struct nozomi
*dc
)
1299 for (i
= 0; i
< 6; i
++)
1300 size
+= pci_resource_len(dc
->pdev
, i
);
1302 /* Assume card type F32_8 if no match */
1303 dc
->card_type
= size
== 2048 ? F32_2
: F32_8
;
1305 dev_info(&dc
->pdev
->dev
, "Card type is: %d\n", dc
->card_type
);
1308 static void nozomi_setup_private_data(struct nozomi
*dc
)
1310 void __iomem
*offset
= dc
->base_addr
+ dc
->card_type
/ 2;
1313 dc
->reg_fcr
= (void __iomem
*)(offset
+ R_FCR
);
1314 dc
->reg_iir
= (void __iomem
*)(offset
+ R_IIR
);
1315 dc
->reg_ier
= (void __iomem
*)(offset
+ R_IER
);
1319 dc
->port
[PORT_MDM
].token_dl
= MDM_DL
;
1320 dc
->port
[PORT_DIAG
].token_dl
= DIAG_DL
;
1321 dc
->port
[PORT_APP1
].token_dl
= APP1_DL
;
1322 dc
->port
[PORT_APP2
].token_dl
= APP2_DL
;
1324 for (i
= 0; i
< MAX_PORT
; i
++)
1325 init_waitqueue_head(&dc
->port
[i
].tty_wait
);
1328 static ssize_t
card_type_show(struct device
*dev
, struct device_attribute
*attr
,
1331 const struct nozomi
*dc
= pci_get_drvdata(to_pci_dev(dev
));
1333 return sprintf(buf
, "%d\n", dc
->card_type
);
1335 static DEVICE_ATTR(card_type
, S_IRUGO
, card_type_show
, NULL
);
1337 static ssize_t
open_ttys_show(struct device
*dev
, struct device_attribute
*attr
,
1340 const struct nozomi
*dc
= pci_get_drvdata(to_pci_dev(dev
));
1342 return sprintf(buf
, "%u\n", dc
->open_ttys
);
1344 static DEVICE_ATTR(open_ttys
, S_IRUGO
, open_ttys_show
, NULL
);
1346 static void make_sysfs_files(struct nozomi
*dc
)
1348 if (device_create_file(&dc
->pdev
->dev
, &dev_attr_card_type
))
1349 dev_err(&dc
->pdev
->dev
,
1350 "Could not create sysfs file for card_type\n");
1351 if (device_create_file(&dc
->pdev
->dev
, &dev_attr_open_ttys
))
1352 dev_err(&dc
->pdev
->dev
,
1353 "Could not create sysfs file for open_ttys\n");
1356 static void remove_sysfs_files(struct nozomi
*dc
)
1358 device_remove_file(&dc
->pdev
->dev
, &dev_attr_card_type
);
1359 device_remove_file(&dc
->pdev
->dev
, &dev_attr_open_ttys
);
1362 /* Allocate memory for one device */
1363 static int __devinit
nozomi_card_init(struct pci_dev
*pdev
,
1364 const struct pci_device_id
*ent
)
1366 resource_size_t start
;
1368 struct nozomi
*dc
= NULL
;
1372 dev_dbg(&pdev
->dev
, "Init, new card found\n");
1374 for (ndev_idx
= 0; ndev_idx
< ARRAY_SIZE(ndevs
); ndev_idx
++)
1375 if (!ndevs
[ndev_idx
])
1378 if (ndev_idx
>= ARRAY_SIZE(ndevs
)) {
1379 dev_err(&pdev
->dev
, "no free tty range for this card left\n");
1384 dc
= kzalloc(sizeof(struct nozomi
), GFP_KERNEL
);
1385 if (unlikely(!dc
)) {
1386 dev_err(&pdev
->dev
, "Could not allocate memory\n");
1393 ret
= pci_enable_device(dc
->pdev
);
1395 dev_err(&pdev
->dev
, "Failed to enable PCI Device\n");
1399 ret
= pci_request_regions(dc
->pdev
, NOZOMI_NAME
);
1401 dev_err(&pdev
->dev
, "I/O address 0x%04x already in use\n",
1402 (int) /* nozomi_private.io_addr */ 0);
1403 goto err_disable_device
;
1406 start
= pci_resource_start(dc
->pdev
, 0);
1408 dev_err(&pdev
->dev
, "No I/O address for card detected\n");
1413 /* Find out what card type it is */
1414 nozomi_get_card_type(dc
);
1416 dc
->base_addr
= ioremap_nocache(start
, dc
->card_type
);
1417 if (!dc
->base_addr
) {
1418 dev_err(&pdev
->dev
, "Unable to map card MMIO\n");
1423 dc
->send_buf
= kmalloc(SEND_BUF_MAX
, GFP_KERNEL
);
1424 if (!dc
->send_buf
) {
1425 dev_err(&pdev
->dev
, "Could not allocate send buffer?\n");
1430 for (i
= PORT_MDM
; i
< MAX_PORT
; i
++) {
1431 if (kfifo_alloc(&dc
->port
[i
].fifo_ul
, FIFO_BUFFER_SIZE_UL
,
1434 "Could not allocate kfifo buffer\n");
1436 goto err_free_kfifo
;
1440 spin_lock_init(&dc
->spin_mutex
);
1442 nozomi_setup_private_data(dc
);
1444 /* Disable all interrupts */
1446 writew(dc
->last_ier
, dc
->reg_ier
);
1448 ret
= request_irq(pdev
->irq
, &interrupt_handler
, IRQF_SHARED
,
1450 if (unlikely(ret
)) {
1451 dev_err(&pdev
->dev
, "can't request irq %d\n", pdev
->irq
);
1452 goto err_free_kfifo
;
1455 DBG1("base_addr: %p", dc
->base_addr
);
1457 make_sysfs_files(dc
);
1459 dc
->index_start
= ndev_idx
* MAX_PORT
;
1460 ndevs
[ndev_idx
] = dc
;
1462 pci_set_drvdata(pdev
, dc
);
1464 /* Enable RESET interrupt */
1465 dc
->last_ier
= RESET
;
1466 iowrite16(dc
->last_ier
, dc
->reg_ier
);
1468 dc
->state
= NOZOMI_STATE_ENABLED
;
1470 for (i
= 0; i
< MAX_PORT
; i
++) {
1471 struct device
*tty_dev
;
1472 struct port
*port
= &dc
->port
[i
];
1474 tty_port_init(&port
->port
);
1475 port
->port
.ops
= &noz_tty_port_ops
;
1476 tty_dev
= tty_register_device(ntty_driver
, dc
->index_start
+ i
,
1479 if (IS_ERR(tty_dev
)) {
1480 ret
= PTR_ERR(tty_dev
);
1481 dev_err(&pdev
->dev
, "Could not allocate tty?\n");
1489 for (i
= dc
->index_start
; i
< dc
->index_start
+ MAX_PORT
; ++i
)
1490 tty_unregister_device(ntty_driver
, i
);
1492 for (i
= 0; i
< MAX_PORT
; i
++)
1493 kfifo_free(&dc
->port
[i
].fifo_ul
);
1495 kfree(dc
->send_buf
);
1496 iounmap(dc
->base_addr
);
1498 pci_release_regions(pdev
);
1500 pci_disable_device(pdev
);
1507 static void __devexit
tty_exit(struct nozomi
*dc
)
1513 for (i
= 0; i
< MAX_PORT
; ++i
) {
1514 struct tty_struct
*tty
= tty_port_tty_get(&dc
->port
[i
].port
);
1515 if (tty
&& list_empty(&tty
->hangup_work
.entry
))
1519 /* Racy below - surely should wait for scheduled work to be done or
1520 complete off a hangup method ? */
1521 while (dc
->open_ttys
)
1523 for (i
= dc
->index_start
; i
< dc
->index_start
+ MAX_PORT
; ++i
)
1524 tty_unregister_device(ntty_driver
, i
);
1527 /* Deallocate memory for one device */
1528 static void __devexit
nozomi_card_exit(struct pci_dev
*pdev
)
1531 struct ctrl_ul ctrl
;
1532 struct nozomi
*dc
= pci_get_drvdata(pdev
);
1534 /* Disable all interrupts */
1536 writew(dc
->last_ier
, dc
->reg_ier
);
1540 /* Send 0x0001, command card to resend the reset token. */
1541 /* This is to get the reset when the module is reloaded. */
1546 DBG1("sending flow control 0x%04X", *((u16
*)&ctrl
));
1548 /* Setup dc->reg addresses to we can use defines here */
1549 write_mem32(dc
->port
[PORT_CTRL
].ul_addr
[0], (u32
*)&ctrl
, 2);
1550 writew(CTRL_UL
, dc
->reg_fcr
); /* push the token to the card. */
1552 remove_sysfs_files(dc
);
1554 free_irq(pdev
->irq
, dc
);
1556 for (i
= 0; i
< MAX_PORT
; i
++)
1557 kfifo_free(&dc
->port
[i
].fifo_ul
);
1559 kfree(dc
->send_buf
);
1561 iounmap(dc
->base_addr
);
1563 pci_release_regions(pdev
);
1565 pci_disable_device(pdev
);
1567 ndevs
[dc
->index_start
/ MAX_PORT
] = NULL
;
1572 static void set_rts(const struct tty_struct
*tty
, int rts
)
1574 struct port
*port
= get_port_by_tty(tty
);
1576 port
->ctrl_ul
.RTS
= rts
;
1577 port
->update_flow_control
= 1;
1578 enable_transmit_ul(PORT_CTRL
, get_dc_by_tty(tty
));
1581 static void set_dtr(const struct tty_struct
*tty
, int dtr
)
1583 struct port
*port
= get_port_by_tty(tty
);
1585 DBG1("SETTING DTR index: %d, dtr: %d", tty
->index
, dtr
);
1587 port
->ctrl_ul
.DTR
= dtr
;
1588 port
->update_flow_control
= 1;
1589 enable_transmit_ul(PORT_CTRL
, get_dc_by_tty(tty
));
1593 * ----------------------------------------------------------------------------
1595 * ----------------------------------------------------------------------------
1598 static int ntty_install(struct tty_driver
*driver
, struct tty_struct
*tty
)
1600 struct port
*port
= get_port_by_tty(tty
);
1601 struct nozomi
*dc
= get_dc_by_tty(tty
);
1603 if (!port
|| !dc
|| dc
->state
!= NOZOMI_STATE_READY
)
1605 ret
= tty_standard_install(driver
, tty
);
1607 tty
->driver_data
= port
;
1611 static void ntty_cleanup(struct tty_struct
*tty
)
1613 tty
->driver_data
= NULL
;
1616 static int ntty_activate(struct tty_port
*tport
, struct tty_struct
*tty
)
1618 struct port
*port
= container_of(tport
, struct port
, port
);
1619 struct nozomi
*dc
= port
->dc
;
1620 unsigned long flags
;
1622 DBG1("open: %d", port
->token_dl
);
1623 spin_lock_irqsave(&dc
->spin_mutex
, flags
);
1624 dc
->last_ier
= dc
->last_ier
| port
->token_dl
;
1625 writew(dc
->last_ier
, dc
->reg_ier
);
1627 spin_unlock_irqrestore(&dc
->spin_mutex
, flags
);
1628 printk("noz: activated %d: %p\n", tty
->index
, tport
);
1632 static int ntty_open(struct tty_struct
*tty
, struct file
*filp
)
1634 struct port
*port
= tty
->driver_data
;
1635 return tty_port_open(&port
->port
, tty
, filp
);
1638 static void ntty_shutdown(struct tty_port
*tport
)
1640 struct port
*port
= container_of(tport
, struct port
, port
);
1641 struct nozomi
*dc
= port
->dc
;
1642 unsigned long flags
;
1644 DBG1("close: %d", port
->token_dl
);
1645 spin_lock_irqsave(&dc
->spin_mutex
, flags
);
1646 dc
->last_ier
&= ~(port
->token_dl
);
1647 writew(dc
->last_ier
, dc
->reg_ier
);
1649 spin_unlock_irqrestore(&dc
->spin_mutex
, flags
);
1650 printk("noz: shutdown %p\n", tport
);
1653 static void ntty_close(struct tty_struct
*tty
, struct file
*filp
)
1655 struct port
*port
= tty
->driver_data
;
1657 tty_port_close(&port
->port
, tty
, filp
);
1660 static void ntty_hangup(struct tty_struct
*tty
)
1662 struct port
*port
= tty
->driver_data
;
1663 tty_port_hangup(&port
->port
);
1667 * called when the userspace process writes to the tty (/dev/noz*).
1668 * Data is inserted into a fifo, which is then read and transferred to the modem.
1670 static int ntty_write(struct tty_struct
*tty
, const unsigned char *buffer
,
1674 struct nozomi
*dc
= get_dc_by_tty(tty
);
1675 struct port
*port
= tty
->driver_data
;
1676 unsigned long flags
;
1678 /* DBG1( "WRITEx: %d, index = %d", count, index); */
1683 rval
= kfifo_in(&port
->fifo_ul
, (unsigned char *)buffer
, count
);
1686 if (unlikely(dc
== NULL
)) {
1687 DBG1("No device context?");
1691 spin_lock_irqsave(&dc
->spin_mutex
, flags
);
1692 /* CTS is only valid on the modem channel */
1693 if (port
== &(dc
->port
[PORT_MDM
])) {
1694 if (port
->ctrl_dl
.CTS
) {
1695 DBG4("Enable interrupt");
1696 enable_transmit_ul(tty
->index
% MAX_PORT
, dc
);
1698 dev_err(&dc
->pdev
->dev
,
1699 "CTS not active on modem port?\n");
1702 enable_transmit_ul(tty
->index
% MAX_PORT
, dc
);
1704 spin_unlock_irqrestore(&dc
->spin_mutex
, flags
);
1711 * Calculate how much is left in device
1712 * This method is called by the upper tty layer.
1713 * #according to sources N_TTY.c it expects a value >= 0 and
1714 * does not check for negative values.
1716 * If the port is unplugged report lots of room and let the bits
1717 * dribble away so we don't block anything.
1719 static int ntty_write_room(struct tty_struct
*tty
)
1721 struct port
*port
= tty
->driver_data
;
1723 const struct nozomi
*dc
= get_dc_by_tty(tty
);
1726 room
= kfifo_avail(&port
->fifo_ul
);
1731 /* Gets io control parameters */
1732 static int ntty_tiocmget(struct tty_struct
*tty
)
1734 const struct port
*port
= tty
->driver_data
;
1735 const struct ctrl_dl
*ctrl_dl
= &port
->ctrl_dl
;
1736 const struct ctrl_ul
*ctrl_ul
= &port
->ctrl_ul
;
1738 /* Note: these could change under us but it is not clear this
1740 return (ctrl_ul
->RTS
? TIOCM_RTS
: 0) |
1741 (ctrl_ul
->DTR
? TIOCM_DTR
: 0) |
1742 (ctrl_dl
->DCD
? TIOCM_CAR
: 0) |
1743 (ctrl_dl
->RI
? TIOCM_RNG
: 0) |
1744 (ctrl_dl
->DSR
? TIOCM_DSR
: 0) |
1745 (ctrl_dl
->CTS
? TIOCM_CTS
: 0);
1748 /* Sets io controls parameters */
1749 static int ntty_tiocmset(struct tty_struct
*tty
,
1750 unsigned int set
, unsigned int clear
)
1752 struct nozomi
*dc
= get_dc_by_tty(tty
);
1753 unsigned long flags
;
1755 spin_lock_irqsave(&dc
->spin_mutex
, flags
);
1756 if (set
& TIOCM_RTS
)
1758 else if (clear
& TIOCM_RTS
)
1761 if (set
& TIOCM_DTR
)
1763 else if (clear
& TIOCM_DTR
)
1765 spin_unlock_irqrestore(&dc
->spin_mutex
, flags
);
1770 static int ntty_cflags_changed(struct port
*port
, unsigned long flags
,
1771 struct async_icount
*cprev
)
1773 const struct async_icount cnow
= port
->tty_icount
;
1776 ret
= ((flags
& TIOCM_RNG
) && (cnow
.rng
!= cprev
->rng
)) ||
1777 ((flags
& TIOCM_DSR
) && (cnow
.dsr
!= cprev
->dsr
)) ||
1778 ((flags
& TIOCM_CD
) && (cnow
.dcd
!= cprev
->dcd
)) ||
1779 ((flags
& TIOCM_CTS
) && (cnow
.cts
!= cprev
->cts
));
1786 static int ntty_tiocgicount(struct tty_struct
*tty
,
1787 struct serial_icounter_struct
*icount
)
1789 struct port
*port
= tty
->driver_data
;
1790 const struct async_icount cnow
= port
->tty_icount
;
1792 icount
->cts
= cnow
.cts
;
1793 icount
->dsr
= cnow
.dsr
;
1794 icount
->rng
= cnow
.rng
;
1795 icount
->dcd
= cnow
.dcd
;
1796 icount
->rx
= cnow
.rx
;
1797 icount
->tx
= cnow
.tx
;
1798 icount
->frame
= cnow
.frame
;
1799 icount
->overrun
= cnow
.overrun
;
1800 icount
->parity
= cnow
.parity
;
1801 icount
->brk
= cnow
.brk
;
1802 icount
->buf_overrun
= cnow
.buf_overrun
;
1806 static int ntty_ioctl(struct tty_struct
*tty
,
1807 unsigned int cmd
, unsigned long arg
)
1809 struct port
*port
= tty
->driver_data
;
1810 int rval
= -ENOIOCTLCMD
;
1812 DBG1("******** IOCTL, cmd: %d", cmd
);
1816 struct async_icount cprev
= port
->tty_icount
;
1818 rval
= wait_event_interruptible(port
->tty_wait
,
1819 ntty_cflags_changed(port
, arg
, &cprev
));
1823 DBG1("ERR: 0x%08X, %d", cmd
, cmd
);
1831 * Called by the upper tty layer when tty buffers are ready
1832 * to receive data again after a call to throttle.
1834 static void ntty_unthrottle(struct tty_struct
*tty
)
1836 struct nozomi
*dc
= get_dc_by_tty(tty
);
1837 unsigned long flags
;
1840 spin_lock_irqsave(&dc
->spin_mutex
, flags
);
1841 enable_transmit_dl(tty
->index
% MAX_PORT
, dc
);
1844 spin_unlock_irqrestore(&dc
->spin_mutex
, flags
);
1848 * Called by the upper tty layer when the tty buffers are almost full.
1849 * The driver should stop send more data.
1851 static void ntty_throttle(struct tty_struct
*tty
)
1853 struct nozomi
*dc
= get_dc_by_tty(tty
);
1854 unsigned long flags
;
1857 spin_lock_irqsave(&dc
->spin_mutex
, flags
);
1859 spin_unlock_irqrestore(&dc
->spin_mutex
, flags
);
1862 /* Returns number of chars in buffer, called by tty layer */
1863 static s32
ntty_chars_in_buffer(struct tty_struct
*tty
)
1865 struct port
*port
= tty
->driver_data
;
1866 struct nozomi
*dc
= get_dc_by_tty(tty
);
1869 if (unlikely(!dc
|| !port
)) {
1870 goto exit_in_buffer
;
1873 rval
= kfifo_len(&port
->fifo_ul
);
1879 static const struct tty_port_operations noz_tty_port_ops
= {
1880 .activate
= ntty_activate
,
1881 .shutdown
= ntty_shutdown
,
1884 static const struct tty_operations tty_ops
= {
1885 .ioctl
= ntty_ioctl
,
1887 .close
= ntty_close
,
1888 .hangup
= ntty_hangup
,
1889 .write
= ntty_write
,
1890 .write_room
= ntty_write_room
,
1891 .unthrottle
= ntty_unthrottle
,
1892 .throttle
= ntty_throttle
,
1893 .chars_in_buffer
= ntty_chars_in_buffer
,
1894 .tiocmget
= ntty_tiocmget
,
1895 .tiocmset
= ntty_tiocmset
,
1896 .get_icount
= ntty_tiocgicount
,
1897 .install
= ntty_install
,
1898 .cleanup
= ntty_cleanup
,
1901 /* Module initialization */
1902 static struct pci_driver nozomi_driver
= {
1903 .name
= NOZOMI_NAME
,
1904 .id_table
= nozomi_pci_tbl
,
1905 .probe
= nozomi_card_init
,
1906 .remove
= __devexit_p(nozomi_card_exit
),
1909 static __init
int nozomi_init(void)
1913 printk(KERN_INFO
"Initializing %s\n", VERSION_STRING
);
1915 ntty_driver
= alloc_tty_driver(NTTY_TTY_MAXMINORS
);
1919 ntty_driver
->driver_name
= NOZOMI_NAME_TTY
;
1920 ntty_driver
->name
= "noz";
1921 ntty_driver
->major
= 0;
1922 ntty_driver
->type
= TTY_DRIVER_TYPE_SERIAL
;
1923 ntty_driver
->subtype
= SERIAL_TYPE_NORMAL
;
1924 ntty_driver
->flags
= TTY_DRIVER_REAL_RAW
| TTY_DRIVER_DYNAMIC_DEV
;
1925 ntty_driver
->init_termios
= tty_std_termios
;
1926 ntty_driver
->init_termios
.c_cflag
= B115200
| CS8
| CREAD
| \
1928 ntty_driver
->init_termios
.c_ispeed
= 115200;
1929 ntty_driver
->init_termios
.c_ospeed
= 115200;
1930 tty_set_operations(ntty_driver
, &tty_ops
);
1932 ret
= tty_register_driver(ntty_driver
);
1934 printk(KERN_ERR
"Nozomi: failed to register ntty driver\n");
1938 ret
= pci_register_driver(&nozomi_driver
);
1940 printk(KERN_ERR
"Nozomi: can't register pci driver\n");
1946 tty_unregister_driver(ntty_driver
);
1948 put_tty_driver(ntty_driver
);
1952 static __exit
void nozomi_exit(void)
1954 printk(KERN_INFO
"Unloading %s\n", DRIVER_DESC
);
1955 pci_unregister_driver(&nozomi_driver
);
1956 tty_unregister_driver(ntty_driver
);
1957 put_tty_driver(ntty_driver
);
1960 module_init(nozomi_init
);
1961 module_exit(nozomi_exit
);
1963 module_param(debug
, int, S_IRUGO
| S_IWUSR
);
1965 MODULE_LICENSE("Dual BSD/GPL");
1966 MODULE_DESCRIPTION(DRIVER_DESC
);