1 // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
3 * nozomi.c -- HSDPA driver Broadband Wireless Data Card - Globe Trotter
5 * Written by: Ulf Jakobsson,
9 * Maintained by: Paul Hardwick (p.hardwick@option.com)
12 * Locking code changes for Vodafone by Sphere Systems Ltd,
13 * Andrew Bird (ajb@spheresystems.co.uk )
16 * Source has been ported from an implementation made by Filip Aben @ Option
18 * --------------------------------------------------------------------------
20 * Copyright (c) 2005,2006 Option Wireless Sweden AB
21 * Copyright (c) 2006 Sphere Systems Ltd
22 * Copyright (c) 2006 Option Wireless n/v
23 * All rights Reserved.
25 * --------------------------------------------------------------------------
28 /* Enable this to have a lot of debug printouts */
31 #include <linux/kernel.h>
32 #include <linux/module.h>
33 #include <linux/pci.h>
34 #include <linux/ioport.h>
35 #include <linux/tty.h>
36 #include <linux/tty_driver.h>
37 #include <linux/tty_flip.h>
38 #include <linux/sched.h>
39 #include <linux/serial.h>
40 #include <linux/interrupt.h>
41 #include <linux/kmod.h>
42 #include <linux/init.h>
43 #include <linux/kfifo.h>
44 #include <linux/uaccess.h>
45 #include <linux/slab.h>
46 #include <asm/byteorder.h>
48 #include <linux/delay.h>
51 #define VERSION_STRING DRIVER_DESC " 2.1d"
53 /* Default debug printout level */
54 #define NOZOMI_DEBUG_LEVEL 0x00
55 static int debug
= NOZOMI_DEBUG_LEVEL
;
56 module_param(debug
, int, S_IRUGO
| S_IWUSR
);
58 /* Macros definitions */
59 #define DBG_(lvl, fmt, args...) \
62 pr_debug("[%d] %s(): " fmt "\n", \
63 __LINE__, __func__, ##args); \
66 #define DBG1(args...) DBG_(0x01, ##args)
67 #define DBG2(args...) DBG_(0x02, ##args)
68 #define DBG3(args...) DBG_(0x04, ##args)
69 #define DBG4(args...) DBG_(0x08, ##args)
71 /* TODO: rewrite to optimize macros... */
73 #define TMP_BUF_MAX 256
75 #define DUMP(buf__, len__) \
77 char tbuf[TMP_BUF_MAX] = {0}; \
79 u32 data_len = min_t(u32, len__, TMP_BUF_MAX); \
80 strscpy(tbuf, buf__, data_len); \
81 if (tbuf[data_len - 2] == '\r') \
82 tbuf[data_len - 2] = 'r'; \
83 DBG1("SENDING: '%s' (%d+n)", tbuf, len__); \
85 DBG1("SENDING: '%s' (%d)", tbuf, len__); \
90 #define NOZOMI_NAME "nozomi"
91 #define NOZOMI_NAME_TTY "nozomi_tty"
92 #define DRIVER_DESC "Nozomi driver"
94 #define NTTY_TTY_MAXMINORS 256
95 #define NTTY_FIFO_BUFFER_SIZE 8192
97 /* Must be power of 2 */
98 #define FIFO_BUFFER_SIZE_UL 8192
100 /* Size of tmp send buffer to card */
101 #define SEND_BUF_MAX 1024
102 #define RECEIVE_BUF_MAX 4
105 #define R_IIR 0x0000 /* Interrupt Identity Register */
106 #define R_FCR 0x0000 /* Flow Control Register */
107 #define R_IER 0x0004 /* Interrupt Enable Register */
109 #define NOZOMI_CONFIG_MAGIC 0xEFEFFEFE
110 #define TOGGLE_VALID 0x0000
112 /* Definition of interrupt tokens */
113 #define MDM_DL1 0x0001
114 #define MDM_UL1 0x0002
115 #define MDM_DL2 0x0004
116 #define MDM_UL2 0x0008
117 #define DIAG_DL1 0x0010
118 #define DIAG_DL2 0x0020
119 #define DIAG_UL 0x0040
120 #define APP1_DL 0x0080
121 #define APP1_UL 0x0100
122 #define APP2_DL 0x0200
123 #define APP2_UL 0x0400
124 #define CTRL_DL 0x0800
125 #define CTRL_UL 0x1000
128 #define MDM_DL (MDM_DL1 | MDM_DL2)
129 #define MDM_UL (MDM_UL1 | MDM_UL2)
130 #define DIAG_DL (DIAG_DL1 | DIAG_DL2)
132 /* modem signal definition */
133 #define CTRL_DSR 0x0001
134 #define CTRL_DCD 0x0002
135 #define CTRL_RI 0x0004
136 #define CTRL_CTS 0x0008
138 #define CTRL_DTR 0x0001
139 #define CTRL_RTS 0x0002
142 #define NOZOMI_MAX_PORTS 5
143 #define NOZOMI_MAX_CARDS (NTTY_TTY_MAXMINORS / MAX_PORT)
145 /* Type definitions */
148 * There are two types of nozomi cards,
149 * one with 2048 memory and with 8192 memory
152 F32_2
= 2048, /* 512 bytes downlink + uplink * 2 -> 2048 */
153 F32_8
= 8192, /* 3072 bytes downl. + 1024 bytes uplink * 2 -> 8192 */
156 /* Initialization states a card can be in */
158 NOZOMI_STATE_UNKNOWN
= 0,
159 NOZOMI_STATE_ENABLED
= 1, /* pci device enabled */
160 NOZOMI_STATE_ALLOCATED
= 2, /* config setup done */
161 NOZOMI_STATE_READY
= 3, /* flowcontrols received */
164 /* Two different toggle channels exist */
170 /* Port definition for the card regarding flow control */
171 enum ctrl_port_type
{
180 /* Ports that the nozomi has */
194 unsigned int enabled
:5; /*
195 * Toggle fields are valid if enabled is 0,
196 * else A-channels must always be used.
198 unsigned int diag_dl
:1;
199 unsigned int mdm_dl
:1;
200 unsigned int mdm_ul
:1;
201 } __attribute__ ((packed
));
203 /* Configuration table to read at startup of card */
204 /* Is for now only needed during initialization phase */
205 struct config_table
{
207 u16 product_information
;
210 struct toggles toggle
;
213 * If this is 64, it can hold
214 * 60 bytes + 4 that is length field
220 * If this is 64, it can hold
221 * 60 bytes + 4 that is length field
236 } __attribute__ ((packed
));
238 /* This stores all control downlink flags */
241 unsigned int reserved
:4;
246 } __attribute__ ((packed
));
248 /* This stores all control uplink flags */
251 unsigned int reserved
:6;
254 } __attribute__ ((packed
));
259 /* This represents the toggle information */
261 unsigned int mdm_ul
:1;
262 unsigned int mdm_dl
:1;
263 unsigned int diag_dl
:1;
264 unsigned int enabled
:5; /*
265 * Toggle fields are valid if enabled is 0,
266 * else A-channels must always be used.
268 } __attribute__ ((packed
));
270 /* Configuration table to read at startup of card */
271 struct config_table
{
274 u16 product_information
;
275 struct toggles toggle
;
279 * If this is 64, it can hold
280 * 60 bytes + 4 that is length field
296 } __attribute__ ((packed
));
298 /* This stores all control downlink flags */
304 unsigned int reserved
:4;
306 } __attribute__ ((packed
));
308 /* This stores all control uplink flags */
312 unsigned int reserved
:6;
314 } __attribute__ ((packed
));
317 /* This holds all information that is needed regarding a port */
319 struct tty_port port
;
320 u8 update_flow_control
;
321 struct ctrl_ul ctrl_ul
;
322 struct ctrl_dl ctrl_dl
;
323 struct kfifo fifo_ul
;
324 void __iomem
*dl_addr
[2];
327 void __iomem
*ul_addr
[2];
332 wait_queue_head_t tty_wait
;
333 struct async_icount tty_icount
;
338 /* Private data one for each card in the system */
340 void __iomem
*base_addr
;
343 /* Pointers to registers */
344 void __iomem
*reg_iir
;
345 void __iomem
*reg_fcr
;
346 void __iomem
*reg_ier
;
349 enum card_type card_type
;
350 struct config_table config_table
; /* Configuration table */
351 struct pci_dev
*pdev
;
352 struct port port
[NOZOMI_MAX_PORTS
];
355 spinlock_t spin_mutex
; /* secures access to registers and tty */
357 unsigned int index_start
;
358 enum card_state state
;
362 /* This is a data packet that is read or written to/from card */
364 u32 size
; /* size is the length of the data buffer */
366 } __attribute__ ((packed
));
368 /* Global variables */
369 static const struct pci_device_id nozomi_pci_tbl
[] = {
370 {PCI_DEVICE(0x1931, 0x000c)}, /* Nozomi HSDPA */
374 MODULE_DEVICE_TABLE(pci
, nozomi_pci_tbl
);
376 static struct nozomi
*ndevs
[NOZOMI_MAX_CARDS
];
377 static struct tty_driver
*ntty_driver
;
379 static const struct tty_port_operations noz_tty_port_ops
;
382 * find card by tty_index
384 static inline struct nozomi
*get_dc_by_tty(const struct tty_struct
*tty
)
386 return tty
? ndevs
[tty
->index
/ MAX_PORT
] : NULL
;
389 static inline struct port
*get_port_by_tty(const struct tty_struct
*tty
)
391 struct nozomi
*ndev
= get_dc_by_tty(tty
);
392 return ndev
? &ndev
->port
[tty
->index
% MAX_PORT
] : NULL
;
401 static void read_mem32(u32
*buf
, const void __iomem
*mem_addr_start
,
405 const u32 __iomem
*ptr
= mem_addr_start
;
408 if (unlikely(!ptr
|| !buf
))
411 /* shortcut for extremely often used cases */
412 switch (size_bytes
) {
413 case 2: /* 2 bytes */
415 *buf16
= __le16_to_cpu(readw(ptr
));
418 case 4: /* 4 bytes */
419 *(buf
) = __le32_to_cpu(readl(ptr
));
424 while (i
< size_bytes
) {
425 if (size_bytes
- i
== 2) {
426 /* Handle 2 bytes in the end */
428 *(buf16
) = __le16_to_cpu(readw(ptr
));
432 *(buf
) = __le32_to_cpu(readl(ptr
));
447 static u32
write_mem32(void __iomem
*mem_addr_start
, const u32
*buf
,
451 u32 __iomem
*ptr
= mem_addr_start
;
454 if (unlikely(!ptr
|| !buf
))
457 /* shortcut for extremely often used cases */
458 switch (size_bytes
) {
459 case 2: /* 2 bytes */
460 buf16
= (const u16
*)buf
;
461 writew(__cpu_to_le16(*buf16
), ptr
);
465 * also needs to write 4 bytes in this case
466 * so falling through..
468 case 4: /* 4 bytes */
469 writel(__cpu_to_le32(*buf
), ptr
);
474 while (i
< size_bytes
) {
475 if (size_bytes
- i
== 2) {
477 buf16
= (const u16
*)buf
;
478 writew(__cpu_to_le16(*buf16
), ptr
);
482 writel(__cpu_to_le32(*buf
), ptr
);
491 /* Setup pointers to different channels and also setup buffer sizes. */
492 static void nozomi_setup_memory(struct nozomi
*dc
)
494 void __iomem
*offset
= dc
->base_addr
+ dc
->config_table
.dl_start
;
495 /* The length reported is including the length field of 4 bytes,
496 * hence subtract with 4.
498 const u16 buff_offset
= 4;
500 /* Modem port dl configuration */
501 dc
->port
[PORT_MDM
].dl_addr
[CH_A
] = offset
;
502 dc
->port
[PORT_MDM
].dl_addr
[CH_B
] =
503 (offset
+= dc
->config_table
.dl_mdm_len1
);
504 dc
->port
[PORT_MDM
].dl_size
[CH_A
] =
505 dc
->config_table
.dl_mdm_len1
- buff_offset
;
506 dc
->port
[PORT_MDM
].dl_size
[CH_B
] =
507 dc
->config_table
.dl_mdm_len2
- buff_offset
;
509 /* Diag port dl configuration */
510 dc
->port
[PORT_DIAG
].dl_addr
[CH_A
] =
511 (offset
+= dc
->config_table
.dl_mdm_len2
);
512 dc
->port
[PORT_DIAG
].dl_size
[CH_A
] =
513 dc
->config_table
.dl_diag_len1
- buff_offset
;
514 dc
->port
[PORT_DIAG
].dl_addr
[CH_B
] =
515 (offset
+= dc
->config_table
.dl_diag_len1
);
516 dc
->port
[PORT_DIAG
].dl_size
[CH_B
] =
517 dc
->config_table
.dl_diag_len2
- buff_offset
;
519 /* App1 port dl configuration */
520 dc
->port
[PORT_APP1
].dl_addr
[CH_A
] =
521 (offset
+= dc
->config_table
.dl_diag_len2
);
522 dc
->port
[PORT_APP1
].dl_size
[CH_A
] =
523 dc
->config_table
.dl_app1_len
- buff_offset
;
525 /* App2 port dl configuration */
526 dc
->port
[PORT_APP2
].dl_addr
[CH_A
] =
527 (offset
+= dc
->config_table
.dl_app1_len
);
528 dc
->port
[PORT_APP2
].dl_size
[CH_A
] =
529 dc
->config_table
.dl_app2_len
- buff_offset
;
531 /* Ctrl dl configuration */
532 dc
->port
[PORT_CTRL
].dl_addr
[CH_A
] =
533 (offset
+= dc
->config_table
.dl_app2_len
);
534 dc
->port
[PORT_CTRL
].dl_size
[CH_A
] =
535 dc
->config_table
.dl_ctrl_len
- buff_offset
;
537 offset
= dc
->base_addr
+ dc
->config_table
.ul_start
;
539 /* Modem Port ul configuration */
540 dc
->port
[PORT_MDM
].ul_addr
[CH_A
] = offset
;
541 dc
->port
[PORT_MDM
].ul_size
[CH_A
] =
542 dc
->config_table
.ul_mdm_len1
- buff_offset
;
543 dc
->port
[PORT_MDM
].ul_addr
[CH_B
] =
544 (offset
+= dc
->config_table
.ul_mdm_len1
);
545 dc
->port
[PORT_MDM
].ul_size
[CH_B
] =
546 dc
->config_table
.ul_mdm_len2
- buff_offset
;
548 /* Diag port ul configuration */
549 dc
->port
[PORT_DIAG
].ul_addr
[CH_A
] =
550 (offset
+= dc
->config_table
.ul_mdm_len2
);
551 dc
->port
[PORT_DIAG
].ul_size
[CH_A
] =
552 dc
->config_table
.ul_diag_len
- buff_offset
;
554 /* App1 port ul configuration */
555 dc
->port
[PORT_APP1
].ul_addr
[CH_A
] =
556 (offset
+= dc
->config_table
.ul_diag_len
);
557 dc
->port
[PORT_APP1
].ul_size
[CH_A
] =
558 dc
->config_table
.ul_app1_len
- buff_offset
;
560 /* App2 port ul configuration */
561 dc
->port
[PORT_APP2
].ul_addr
[CH_A
] =
562 (offset
+= dc
->config_table
.ul_app1_len
);
563 dc
->port
[PORT_APP2
].ul_size
[CH_A
] =
564 dc
->config_table
.ul_app2_len
- buff_offset
;
566 /* Ctrl ul configuration */
567 dc
->port
[PORT_CTRL
].ul_addr
[CH_A
] =
568 (offset
+= dc
->config_table
.ul_app2_len
);
569 dc
->port
[PORT_CTRL
].ul_size
[CH_A
] =
570 dc
->config_table
.ul_ctrl_len
- buff_offset
;
573 /* Dump config table under initalization phase */
575 static void dump_table(const struct nozomi
*dc
)
577 DBG3("signature: 0x%08X", dc
->config_table
.signature
);
578 DBG3("version: 0x%04X", dc
->config_table
.version
);
579 DBG3("product_information: 0x%04X", \
580 dc
->config_table
.product_information
);
581 DBG3("toggle enabled: %d", dc
->config_table
.toggle
.enabled
);
582 DBG3("toggle up_mdm: %d", dc
->config_table
.toggle
.mdm_ul
);
583 DBG3("toggle dl_mdm: %d", dc
->config_table
.toggle
.mdm_dl
);
584 DBG3("toggle dl_dbg: %d", dc
->config_table
.toggle
.diag_dl
);
586 DBG3("dl_start: 0x%04X", dc
->config_table
.dl_start
);
587 DBG3("dl_mdm_len0: 0x%04X, %d", dc
->config_table
.dl_mdm_len1
,
588 dc
->config_table
.dl_mdm_len1
);
589 DBG3("dl_mdm_len1: 0x%04X, %d", dc
->config_table
.dl_mdm_len2
,
590 dc
->config_table
.dl_mdm_len2
);
591 DBG3("dl_diag_len0: 0x%04X, %d", dc
->config_table
.dl_diag_len1
,
592 dc
->config_table
.dl_diag_len1
);
593 DBG3("dl_diag_len1: 0x%04X, %d", dc
->config_table
.dl_diag_len2
,
594 dc
->config_table
.dl_diag_len2
);
595 DBG3("dl_app1_len: 0x%04X, %d", dc
->config_table
.dl_app1_len
,
596 dc
->config_table
.dl_app1_len
);
597 DBG3("dl_app2_len: 0x%04X, %d", dc
->config_table
.dl_app2_len
,
598 dc
->config_table
.dl_app2_len
);
599 DBG3("dl_ctrl_len: 0x%04X, %d", dc
->config_table
.dl_ctrl_len
,
600 dc
->config_table
.dl_ctrl_len
);
601 DBG3("ul_start: 0x%04X, %d", dc
->config_table
.ul_start
,
602 dc
->config_table
.ul_start
);
603 DBG3("ul_mdm_len[0]: 0x%04X, %d", dc
->config_table
.ul_mdm_len1
,
604 dc
->config_table
.ul_mdm_len1
);
605 DBG3("ul_mdm_len[1]: 0x%04X, %d", dc
->config_table
.ul_mdm_len2
,
606 dc
->config_table
.ul_mdm_len2
);
607 DBG3("ul_diag_len: 0x%04X, %d", dc
->config_table
.ul_diag_len
,
608 dc
->config_table
.ul_diag_len
);
609 DBG3("ul_app1_len: 0x%04X, %d", dc
->config_table
.ul_app1_len
,
610 dc
->config_table
.ul_app1_len
);
611 DBG3("ul_app2_len: 0x%04X, %d", dc
->config_table
.ul_app2_len
,
612 dc
->config_table
.ul_app2_len
);
613 DBG3("ul_ctrl_len: 0x%04X, %d", dc
->config_table
.ul_ctrl_len
,
614 dc
->config_table
.ul_ctrl_len
);
617 static inline void dump_table(const struct nozomi
*dc
) { }
621 * Read configuration table from card under intalization phase
622 * Returns 1 if ok, else 0
624 static int nozomi_read_config_table(struct nozomi
*dc
)
626 read_mem32((u32
*) &dc
->config_table
, dc
->base_addr
+ 0,
627 sizeof(struct config_table
));
629 if (dc
->config_table
.signature
!= NOZOMI_CONFIG_MAGIC
) {
630 dev_err(&dc
->pdev
->dev
, "ConfigTable Bad! 0x%08X != 0x%08X\n",
631 dc
->config_table
.signature
, NOZOMI_CONFIG_MAGIC
);
635 if ((dc
->config_table
.version
== 0)
636 || (dc
->config_table
.toggle
.enabled
== TOGGLE_VALID
)) {
638 DBG1("Second phase, configuring card");
640 nozomi_setup_memory(dc
);
642 dc
->port
[PORT_MDM
].toggle_ul
= dc
->config_table
.toggle
.mdm_ul
;
643 dc
->port
[PORT_MDM
].toggle_dl
= dc
->config_table
.toggle
.mdm_dl
;
644 dc
->port
[PORT_DIAG
].toggle_dl
= dc
->config_table
.toggle
.diag_dl
;
645 DBG1("toggle ports: MDM UL:%d MDM DL:%d, DIAG DL:%d",
646 dc
->port
[PORT_MDM
].toggle_ul
,
647 dc
->port
[PORT_MDM
].toggle_dl
, dc
->port
[PORT_DIAG
].toggle_dl
);
651 for (i
= PORT_MDM
; i
< MAX_PORT
; i
++) {
652 memset(&dc
->port
[i
].ctrl_dl
, 0, sizeof(struct ctrl_dl
));
653 memset(&dc
->port
[i
].ctrl_ul
, 0, sizeof(struct ctrl_ul
));
656 /* Enable control channel */
657 dc
->last_ier
= dc
->last_ier
| CTRL_DL
;
658 writew(dc
->last_ier
, dc
->reg_ier
);
660 dc
->state
= NOZOMI_STATE_ALLOCATED
;
661 dev_info(&dc
->pdev
->dev
, "Initialization OK!\n");
665 if ((dc
->config_table
.version
> 0)
666 && (dc
->config_table
.toggle
.enabled
!= TOGGLE_VALID
)) {
668 DBG1("First phase: pushing upload buffers, clearing download");
670 dev_info(&dc
->pdev
->dev
, "Version of card: %d\n",
671 dc
->config_table
.version
);
673 /* Here we should disable all I/O over F32. */
674 nozomi_setup_memory(dc
);
677 * We should send ALL channel pair tokens back along
681 /* push upload modem buffers */
682 write_mem32(dc
->port
[PORT_MDM
].ul_addr
[CH_A
],
684 write_mem32(dc
->port
[PORT_MDM
].ul_addr
[CH_B
],
687 writew(MDM_UL
| DIAG_DL
| MDM_DL
, dc
->reg_fcr
);
689 DBG1("First phase done");
695 /* Enable uplink interrupts */
696 static void enable_transmit_ul(enum port_type port
, struct nozomi
*dc
)
698 static const u16 mask
[] = {MDM_UL
, DIAG_UL
, APP1_UL
, APP2_UL
, CTRL_UL
};
700 if (port
< NOZOMI_MAX_PORTS
) {
701 dc
->last_ier
|= mask
[port
];
702 writew(dc
->last_ier
, dc
->reg_ier
);
704 dev_err(&dc
->pdev
->dev
, "Called with wrong port?\n");
708 /* Disable uplink interrupts */
709 static void disable_transmit_ul(enum port_type port
, struct nozomi
*dc
)
711 static const u16 mask
[] =
712 {~MDM_UL
, ~DIAG_UL
, ~APP1_UL
, ~APP2_UL
, ~CTRL_UL
};
714 if (port
< NOZOMI_MAX_PORTS
) {
715 dc
->last_ier
&= mask
[port
];
716 writew(dc
->last_ier
, dc
->reg_ier
);
718 dev_err(&dc
->pdev
->dev
, "Called with wrong port?\n");
722 /* Enable downlink interrupts */
723 static void enable_transmit_dl(enum port_type port
, struct nozomi
*dc
)
725 static const u16 mask
[] = {MDM_DL
, DIAG_DL
, APP1_DL
, APP2_DL
, CTRL_DL
};
727 if (port
< NOZOMI_MAX_PORTS
) {
728 dc
->last_ier
|= mask
[port
];
729 writew(dc
->last_ier
, dc
->reg_ier
);
731 dev_err(&dc
->pdev
->dev
, "Called with wrong port?\n");
735 /* Disable downlink interrupts */
736 static void disable_transmit_dl(enum port_type port
, struct nozomi
*dc
)
738 static const u16 mask
[] =
739 {~MDM_DL
, ~DIAG_DL
, ~APP1_DL
, ~APP2_DL
, ~CTRL_DL
};
741 if (port
< NOZOMI_MAX_PORTS
) {
742 dc
->last_ier
&= mask
[port
];
743 writew(dc
->last_ier
, dc
->reg_ier
);
745 dev_err(&dc
->pdev
->dev
, "Called with wrong port?\n");
750 * Return 1 - send buffer to card and ack.
751 * Return 0 - don't ack, don't send buffer to card.
753 static int send_data(enum port_type index
, struct nozomi
*dc
)
756 struct port
*port
= &dc
->port
[index
];
757 const u8 toggle
= port
->toggle_ul
;
758 void __iomem
*addr
= port
->ul_addr
[toggle
];
759 const u32 ul_size
= port
->ul_size
[toggle
];
761 /* Get data from tty and place in buf for now */
762 size
= kfifo_out(&port
->fifo_ul
, dc
->send_buf
,
763 ul_size
< SEND_BUF_MAX
? ul_size
: SEND_BUF_MAX
);
766 DBG4("No more data to send, disable link:");
770 /* DUMP(buf, size); */
772 /* Write length + data */
773 write_mem32(addr
, (u32
*) &size
, 4);
774 write_mem32(addr
+ 4, (u32
*) dc
->send_buf
, size
);
776 tty_port_tty_wakeup(&port
->port
);
781 /* If all data has been read, return 1, else 0 */
782 static int receive_data(enum port_type index
, struct nozomi
*dc
)
784 u8 buf
[RECEIVE_BUF_MAX
] = { 0 };
787 struct port
*port
= &dc
->port
[index
];
788 void __iomem
*addr
= port
->dl_addr
[port
->toggle_dl
];
789 struct tty_struct
*tty
= tty_port_tty_get(&port
->port
);
792 size
= __le32_to_cpu(readl(addr
));
793 /* DBG1( "%d bytes port: %d", size, index); */
795 if (tty
&& tty_throttled(tty
)) {
796 DBG1("No room in tty, don't read data, don't ack interrupt, "
797 "disable interrupt");
799 /* disable interrupt in downlink... */
800 disable_transmit_dl(index
, dc
);
805 if (unlikely(size
== 0)) {
806 dev_err(&dc
->pdev
->dev
, "size == 0?\n");
812 read_mem32((u32
*) buf
, addr
+ offset
, RECEIVE_BUF_MAX
);
815 tty_insert_flip_char(&port
->port
, buf
[0], TTY_NORMAL
);
817 } else if (size
< RECEIVE_BUF_MAX
) {
818 size
-= tty_insert_flip_string(&port
->port
,
821 i
= tty_insert_flip_string(&port
->port
,
822 (char *)buf
, RECEIVE_BUF_MAX
);
828 set_bit(index
, &dc
->flip
);
835 /* Debug for interrupts */
837 static char *interrupt2str(u16 interrupt
)
839 static char buf
[TMP_BUF_MAX
];
842 if (interrupt
& MDM_DL1
)
843 p
+= scnprintf(p
, TMP_BUF_MAX
, "MDM_DL1 ");
844 if (interrupt
& MDM_DL2
)
845 p
+= scnprintf(p
, TMP_BUF_MAX
- (p
- buf
), "MDM_DL2 ");
846 if (interrupt
& MDM_UL1
)
847 p
+= scnprintf(p
, TMP_BUF_MAX
- (p
- buf
), "MDM_UL1 ");
848 if (interrupt
& MDM_UL2
)
849 p
+= scnprintf(p
, TMP_BUF_MAX
- (p
- buf
), "MDM_UL2 ");
850 if (interrupt
& DIAG_DL1
)
851 p
+= scnprintf(p
, TMP_BUF_MAX
- (p
- buf
), "DIAG_DL1 ");
852 if (interrupt
& DIAG_DL2
)
853 p
+= scnprintf(p
, TMP_BUF_MAX
- (p
- buf
), "DIAG_DL2 ");
855 if (interrupt
& DIAG_UL
)
856 p
+= scnprintf(p
, TMP_BUF_MAX
- (p
- buf
), "DIAG_UL ");
858 if (interrupt
& APP1_DL
)
859 p
+= scnprintf(p
, TMP_BUF_MAX
- (p
- buf
), "APP1_DL ");
860 if (interrupt
& APP2_DL
)
861 p
+= scnprintf(p
, TMP_BUF_MAX
- (p
- buf
), "APP2_DL ");
863 if (interrupt
& APP1_UL
)
864 p
+= scnprintf(p
, TMP_BUF_MAX
- (p
- buf
), "APP1_UL ");
865 if (interrupt
& APP2_UL
)
866 p
+= scnprintf(p
, TMP_BUF_MAX
- (p
- buf
), "APP2_UL ");
868 if (interrupt
& CTRL_DL
)
869 p
+= scnprintf(p
, TMP_BUF_MAX
- (p
- buf
), "CTRL_DL ");
870 if (interrupt
& CTRL_UL
)
871 p
+= scnprintf(p
, TMP_BUF_MAX
- (p
- buf
), "CTRL_UL ");
873 if (interrupt
& RESET
)
874 p
+= scnprintf(p
, TMP_BUF_MAX
- (p
- buf
), "RESET ");
881 * Receive flow control
882 * Return 1 - If ok, else 0
884 static int receive_flow_control(struct nozomi
*dc
)
886 enum port_type port
= PORT_MDM
;
887 struct ctrl_dl ctrl_dl
;
888 struct ctrl_dl old_ctrl
;
891 read_mem32((u32
*) &ctrl_dl
, dc
->port
[PORT_CTRL
].dl_addr
[CH_A
], 2);
893 switch (ctrl_dl
.port
) {
895 DBG1("The Base Band sends this value as a response to a "
896 "request for IMSI detach sent over the control "
897 "channel uplink (see section 7.6.1).");
905 enable_ier
= DIAG_DL
;
909 enable_ier
= APP1_DL
;
913 enable_ier
= APP2_DL
;
914 if (dc
->state
== NOZOMI_STATE_ALLOCATED
) {
916 * After card initialization the flow control
917 * received for APP2 is always the last
919 dc
->state
= NOZOMI_STATE_READY
;
920 dev_info(&dc
->pdev
->dev
, "Device READY!\n");
924 dev_err(&dc
->pdev
->dev
,
925 "ERROR: flow control received for non-existing port\n");
929 DBG1("0x%04X->0x%04X", *((u16
*)&dc
->port
[port
].ctrl_dl
),
932 old_ctrl
= dc
->port
[port
].ctrl_dl
;
933 dc
->port
[port
].ctrl_dl
= ctrl_dl
;
935 if (old_ctrl
.CTS
== 1 && ctrl_dl
.CTS
== 0) {
936 DBG1("Disable interrupt (0x%04X) on port: %d",
938 disable_transmit_ul(port
, dc
);
940 } else if (old_ctrl
.CTS
== 0 && ctrl_dl
.CTS
== 1) {
942 if (kfifo_len(&dc
->port
[port
].fifo_ul
)) {
943 DBG1("Enable interrupt (0x%04X) on port: %d",
945 DBG1("Data in buffer [%d], enable transmit! ",
946 kfifo_len(&dc
->port
[port
].fifo_ul
));
947 enable_transmit_ul(port
, dc
);
949 DBG1("No data in buffer...");
953 if (*(u16
*)&old_ctrl
== *(u16
*)&ctrl_dl
) {
954 DBG1(" No change in mctrl");
957 /* Update statistics */
958 if (old_ctrl
.CTS
!= ctrl_dl
.CTS
)
959 dc
->port
[port
].tty_icount
.cts
++;
960 if (old_ctrl
.DSR
!= ctrl_dl
.DSR
)
961 dc
->port
[port
].tty_icount
.dsr
++;
962 if (old_ctrl
.RI
!= ctrl_dl
.RI
)
963 dc
->port
[port
].tty_icount
.rng
++;
964 if (old_ctrl
.DCD
!= ctrl_dl
.DCD
)
965 dc
->port
[port
].tty_icount
.dcd
++;
967 wake_up_interruptible(&dc
->port
[port
].tty_wait
);
969 DBG1("port: %d DCD(%d), CTS(%d), RI(%d), DSR(%d)",
971 dc
->port
[port
].tty_icount
.dcd
, dc
->port
[port
].tty_icount
.cts
,
972 dc
->port
[port
].tty_icount
.rng
, dc
->port
[port
].tty_icount
.dsr
);
977 static enum ctrl_port_type
port2ctrl(enum port_type port
,
978 const struct nozomi
*dc
)
990 dev_err(&dc
->pdev
->dev
,
991 "ERROR: send flow control " \
992 "received for non-existing port\n");
998 * Send flow control, can only update one channel at a time
999 * Return 0 - If we have updated all flow control
1000 * Return 1 - If we need to update more flow control, ack current enable more
1002 static int send_flow_control(struct nozomi
*dc
)
1004 u32 i
, more_flow_control_to_be_updated
= 0;
1007 for (i
= PORT_MDM
; i
< MAX_PORT
; i
++) {
1008 if (dc
->port
[i
].update_flow_control
) {
1009 if (more_flow_control_to_be_updated
) {
1010 /* We have more flow control to be updated */
1013 dc
->port
[i
].ctrl_ul
.port
= port2ctrl(i
, dc
);
1014 ctrl
= (u16
*)&dc
->port
[i
].ctrl_ul
;
1015 write_mem32(dc
->port
[PORT_CTRL
].ul_addr
[0], \
1017 dc
->port
[i
].update_flow_control
= 0;
1018 more_flow_control_to_be_updated
= 1;
1025 * Handle downlink data, ports that are handled are modem and diagnostics
1027 * Return 0 - toggle fields are out of sync
1029 static int handle_data_dl(struct nozomi
*dc
, enum port_type port
, u8
*toggle
,
1030 u16 read_iir
, u16 mask1
, u16 mask2
)
1032 if (*toggle
== 0 && read_iir
& mask1
) {
1033 if (receive_data(port
, dc
)) {
1034 writew(mask1
, dc
->reg_fcr
);
1035 *toggle
= !(*toggle
);
1038 if (read_iir
& mask2
) {
1039 if (receive_data(port
, dc
)) {
1040 writew(mask2
, dc
->reg_fcr
);
1041 *toggle
= !(*toggle
);
1044 } else if (*toggle
== 1 && read_iir
& mask2
) {
1045 if (receive_data(port
, dc
)) {
1046 writew(mask2
, dc
->reg_fcr
);
1047 *toggle
= !(*toggle
);
1050 if (read_iir
& mask1
) {
1051 if (receive_data(port
, dc
)) {
1052 writew(mask1
, dc
->reg_fcr
);
1053 *toggle
= !(*toggle
);
1057 dev_err(&dc
->pdev
->dev
, "port out of sync!, toggle:%d\n",
1065 * Handle uplink data, this is currently for the modem port
1067 * Return 0 - toggle field are out of sync
1069 static int handle_data_ul(struct nozomi
*dc
, enum port_type port
, u16 read_iir
)
1071 u8
*toggle
= &(dc
->port
[port
].toggle_ul
);
1073 if (*toggle
== 0 && read_iir
& MDM_UL1
) {
1074 dc
->last_ier
&= ~MDM_UL
;
1075 writew(dc
->last_ier
, dc
->reg_ier
);
1076 if (send_data(port
, dc
)) {
1077 writew(MDM_UL1
, dc
->reg_fcr
);
1078 dc
->last_ier
= dc
->last_ier
| MDM_UL
;
1079 writew(dc
->last_ier
, dc
->reg_ier
);
1083 if (read_iir
& MDM_UL2
) {
1084 dc
->last_ier
&= ~MDM_UL
;
1085 writew(dc
->last_ier
, dc
->reg_ier
);
1086 if (send_data(port
, dc
)) {
1087 writew(MDM_UL2
, dc
->reg_fcr
);
1088 dc
->last_ier
= dc
->last_ier
| MDM_UL
;
1089 writew(dc
->last_ier
, dc
->reg_ier
);
1094 } else if (*toggle
== 1 && read_iir
& MDM_UL2
) {
1095 dc
->last_ier
&= ~MDM_UL
;
1096 writew(dc
->last_ier
, dc
->reg_ier
);
1097 if (send_data(port
, dc
)) {
1098 writew(MDM_UL2
, dc
->reg_fcr
);
1099 dc
->last_ier
= dc
->last_ier
| MDM_UL
;
1100 writew(dc
->last_ier
, dc
->reg_ier
);
1104 if (read_iir
& MDM_UL1
) {
1105 dc
->last_ier
&= ~MDM_UL
;
1106 writew(dc
->last_ier
, dc
->reg_ier
);
1107 if (send_data(port
, dc
)) {
1108 writew(MDM_UL1
, dc
->reg_fcr
);
1109 dc
->last_ier
= dc
->last_ier
| MDM_UL
;
1110 writew(dc
->last_ier
, dc
->reg_ier
);
1115 writew(read_iir
& MDM_UL
, dc
->reg_fcr
);
1116 dev_err(&dc
->pdev
->dev
, "port out of sync!\n");
1122 static irqreturn_t
interrupt_handler(int irq
, void *dev_id
)
1124 struct nozomi
*dc
= dev_id
;
1131 spin_lock(&dc
->spin_mutex
);
1132 read_iir
= readw(dc
->reg_iir
);
1135 if (read_iir
== (u16
)-1)
1138 * Just handle interrupt enabled in IER
1139 * (by masking with dc->last_ier)
1141 read_iir
&= dc
->last_ier
;
1147 DBG4("%s irq:0x%04X, prev:0x%04X", interrupt2str(read_iir
), read_iir
,
1150 if (read_iir
& RESET
) {
1151 if (unlikely(!nozomi_read_config_table(dc
))) {
1153 writew(dc
->last_ier
, dc
->reg_ier
);
1154 dev_err(&dc
->pdev
->dev
, "Could not read status from "
1155 "card, we should disable interface\n");
1157 writew(RESET
, dc
->reg_fcr
);
1159 /* No more useful info if this was the reset interrupt. */
1162 if (read_iir
& CTRL_UL
) {
1164 dc
->last_ier
&= ~CTRL_UL
;
1165 writew(dc
->last_ier
, dc
->reg_ier
);
1166 if (send_flow_control(dc
)) {
1167 writew(CTRL_UL
, dc
->reg_fcr
);
1168 dc
->last_ier
= dc
->last_ier
| CTRL_UL
;
1169 writew(dc
->last_ier
, dc
->reg_ier
);
1172 if (read_iir
& CTRL_DL
) {
1173 receive_flow_control(dc
);
1174 writew(CTRL_DL
, dc
->reg_fcr
);
1176 if (read_iir
& MDM_DL
) {
1177 if (!handle_data_dl(dc
, PORT_MDM
,
1178 &(dc
->port
[PORT_MDM
].toggle_dl
), read_iir
,
1179 MDM_DL1
, MDM_DL2
)) {
1180 dev_err(&dc
->pdev
->dev
, "MDM_DL out of sync!\n");
1184 if (read_iir
& MDM_UL
) {
1185 if (!handle_data_ul(dc
, PORT_MDM
, read_iir
)) {
1186 dev_err(&dc
->pdev
->dev
, "MDM_UL out of sync!\n");
1190 if (read_iir
& DIAG_DL
) {
1191 if (!handle_data_dl(dc
, PORT_DIAG
,
1192 &(dc
->port
[PORT_DIAG
].toggle_dl
), read_iir
,
1193 DIAG_DL1
, DIAG_DL2
)) {
1194 dev_err(&dc
->pdev
->dev
, "DIAG_DL out of sync!\n");
1198 if (read_iir
& DIAG_UL
) {
1199 dc
->last_ier
&= ~DIAG_UL
;
1200 writew(dc
->last_ier
, dc
->reg_ier
);
1201 if (send_data(PORT_DIAG
, dc
)) {
1202 writew(DIAG_UL
, dc
->reg_fcr
);
1203 dc
->last_ier
= dc
->last_ier
| DIAG_UL
;
1204 writew(dc
->last_ier
, dc
->reg_ier
);
1207 if (read_iir
& APP1_DL
) {
1208 if (receive_data(PORT_APP1
, dc
))
1209 writew(APP1_DL
, dc
->reg_fcr
);
1211 if (read_iir
& APP1_UL
) {
1212 dc
->last_ier
&= ~APP1_UL
;
1213 writew(dc
->last_ier
, dc
->reg_ier
);
1214 if (send_data(PORT_APP1
, dc
)) {
1215 writew(APP1_UL
, dc
->reg_fcr
);
1216 dc
->last_ier
= dc
->last_ier
| APP1_UL
;
1217 writew(dc
->last_ier
, dc
->reg_ier
);
1220 if (read_iir
& APP2_DL
) {
1221 if (receive_data(PORT_APP2
, dc
))
1222 writew(APP2_DL
, dc
->reg_fcr
);
1224 if (read_iir
& APP2_UL
) {
1225 dc
->last_ier
&= ~APP2_UL
;
1226 writew(dc
->last_ier
, dc
->reg_ier
);
1227 if (send_data(PORT_APP2
, dc
)) {
1228 writew(APP2_UL
, dc
->reg_fcr
);
1229 dc
->last_ier
= dc
->last_ier
| APP2_UL
;
1230 writew(dc
->last_ier
, dc
->reg_ier
);
1235 spin_unlock(&dc
->spin_mutex
);
1237 for (a
= 0; a
< NOZOMI_MAX_PORTS
; a
++)
1238 if (test_and_clear_bit(a
, &dc
->flip
))
1239 tty_flip_buffer_push(&dc
->port
[a
].port
);
1243 spin_unlock(&dc
->spin_mutex
);
1247 static void nozomi_get_card_type(struct nozomi
*dc
)
1252 for (i
= 0; i
< 6; i
++)
1253 size
+= pci_resource_len(dc
->pdev
, i
);
1255 /* Assume card type F32_8 if no match */
1256 dc
->card_type
= size
== 2048 ? F32_2
: F32_8
;
1258 dev_info(&dc
->pdev
->dev
, "Card type is: %d\n", dc
->card_type
);
1261 static void nozomi_setup_private_data(struct nozomi
*dc
)
1263 void __iomem
*offset
= dc
->base_addr
+ dc
->card_type
/ 2;
1266 dc
->reg_fcr
= (void __iomem
*)(offset
+ R_FCR
);
1267 dc
->reg_iir
= (void __iomem
*)(offset
+ R_IIR
);
1268 dc
->reg_ier
= (void __iomem
*)(offset
+ R_IER
);
1272 dc
->port
[PORT_MDM
].token_dl
= MDM_DL
;
1273 dc
->port
[PORT_DIAG
].token_dl
= DIAG_DL
;
1274 dc
->port
[PORT_APP1
].token_dl
= APP1_DL
;
1275 dc
->port
[PORT_APP2
].token_dl
= APP2_DL
;
1277 for (i
= 0; i
< MAX_PORT
; i
++)
1278 init_waitqueue_head(&dc
->port
[i
].tty_wait
);
1281 static ssize_t
card_type_show(struct device
*dev
, struct device_attribute
*attr
,
1284 const struct nozomi
*dc
= dev_get_drvdata(dev
);
1286 return sprintf(buf
, "%d\n", dc
->card_type
);
1288 static DEVICE_ATTR_RO(card_type
);
1290 static ssize_t
open_ttys_show(struct device
*dev
, struct device_attribute
*attr
,
1293 const struct nozomi
*dc
= dev_get_drvdata(dev
);
1295 return sprintf(buf
, "%u\n", dc
->open_ttys
);
1297 static DEVICE_ATTR_RO(open_ttys
);
1299 static void make_sysfs_files(struct nozomi
*dc
)
1301 if (device_create_file(&dc
->pdev
->dev
, &dev_attr_card_type
))
1302 dev_err(&dc
->pdev
->dev
,
1303 "Could not create sysfs file for card_type\n");
1304 if (device_create_file(&dc
->pdev
->dev
, &dev_attr_open_ttys
))
1305 dev_err(&dc
->pdev
->dev
,
1306 "Could not create sysfs file for open_ttys\n");
1309 static void remove_sysfs_files(struct nozomi
*dc
)
1311 device_remove_file(&dc
->pdev
->dev
, &dev_attr_card_type
);
1312 device_remove_file(&dc
->pdev
->dev
, &dev_attr_open_ttys
);
1315 /* Allocate memory for one device */
1316 static int nozomi_card_init(struct pci_dev
*pdev
,
1317 const struct pci_device_id
*ent
)
1320 struct nozomi
*dc
= NULL
;
1324 dev_dbg(&pdev
->dev
, "Init, new card found\n");
1326 for (ndev_idx
= 0; ndev_idx
< ARRAY_SIZE(ndevs
); ndev_idx
++)
1327 if (!ndevs
[ndev_idx
])
1330 if (ndev_idx
>= ARRAY_SIZE(ndevs
)) {
1331 dev_err(&pdev
->dev
, "no free tty range for this card left\n");
1336 dc
= kzalloc(sizeof(struct nozomi
), GFP_KERNEL
);
1337 if (unlikely(!dc
)) {
1338 dev_err(&pdev
->dev
, "Could not allocate memory\n");
1345 ret
= pci_enable_device(dc
->pdev
);
1347 dev_err(&pdev
->dev
, "Failed to enable PCI Device\n");
1351 ret
= pci_request_regions(dc
->pdev
, NOZOMI_NAME
);
1353 dev_err(&pdev
->dev
, "I/O address 0x%04x already in use\n",
1354 (int) /* nozomi_private.io_addr */ 0);
1355 goto err_disable_device
;
1358 /* Find out what card type it is */
1359 nozomi_get_card_type(dc
);
1361 dc
->base_addr
= pci_iomap(dc
->pdev
, 0, dc
->card_type
);
1362 if (!dc
->base_addr
) {
1363 dev_err(&pdev
->dev
, "Unable to map card MMIO\n");
1368 dc
->send_buf
= kmalloc(SEND_BUF_MAX
, GFP_KERNEL
);
1369 if (!dc
->send_buf
) {
1370 dev_err(&pdev
->dev
, "Could not allocate send buffer?\n");
1375 for (i
= PORT_MDM
; i
< MAX_PORT
; i
++) {
1376 if (kfifo_alloc(&dc
->port
[i
].fifo_ul
, FIFO_BUFFER_SIZE_UL
,
1379 "Could not allocate kfifo buffer\n");
1381 goto err_free_kfifo
;
1385 spin_lock_init(&dc
->spin_mutex
);
1387 nozomi_setup_private_data(dc
);
1389 /* Disable all interrupts */
1391 writew(dc
->last_ier
, dc
->reg_ier
);
1393 ret
= request_irq(pdev
->irq
, &interrupt_handler
, IRQF_SHARED
,
1395 if (unlikely(ret
)) {
1396 dev_err(&pdev
->dev
, "can't request irq %d\n", pdev
->irq
);
1397 goto err_free_kfifo
;
1400 DBG1("base_addr: %p", dc
->base_addr
);
1402 make_sysfs_files(dc
);
1404 dc
->index_start
= ndev_idx
* MAX_PORT
;
1405 ndevs
[ndev_idx
] = dc
;
1407 pci_set_drvdata(pdev
, dc
);
1409 /* Enable RESET interrupt */
1410 dc
->last_ier
= RESET
;
1411 iowrite16(dc
->last_ier
, dc
->reg_ier
);
1413 dc
->state
= NOZOMI_STATE_ENABLED
;
1415 for (i
= 0; i
< MAX_PORT
; i
++) {
1416 struct device
*tty_dev
;
1417 struct port
*port
= &dc
->port
[i
];
1419 tty_port_init(&port
->port
);
1420 port
->port
.ops
= &noz_tty_port_ops
;
1421 tty_dev
= tty_port_register_device(&port
->port
, ntty_driver
,
1422 dc
->index_start
+ i
, &pdev
->dev
);
1424 if (IS_ERR(tty_dev
)) {
1425 ret
= PTR_ERR(tty_dev
);
1426 dev_err(&pdev
->dev
, "Could not allocate tty?\n");
1427 tty_port_destroy(&port
->port
);
1435 for (i
= 0; i
< MAX_PORT
; ++i
) {
1436 tty_unregister_device(ntty_driver
, dc
->index_start
+ i
);
1437 tty_port_destroy(&dc
->port
[i
].port
);
1440 for (i
= 0; i
< MAX_PORT
; i
++)
1441 kfifo_free(&dc
->port
[i
].fifo_ul
);
1443 kfree(dc
->send_buf
);
1444 iounmap(dc
->base_addr
);
1446 pci_release_regions(pdev
);
1448 pci_disable_device(pdev
);
1455 static void tty_exit(struct nozomi
*dc
)
1461 for (i
= 0; i
< MAX_PORT
; ++i
)
1462 tty_port_tty_hangup(&dc
->port
[i
].port
, false);
1464 /* Racy below - surely should wait for scheduled work to be done or
1465 complete off a hangup method ? */
1466 while (dc
->open_ttys
)
1468 for (i
= 0; i
< MAX_PORT
; ++i
) {
1469 tty_unregister_device(ntty_driver
, dc
->index_start
+ i
);
1470 tty_port_destroy(&dc
->port
[i
].port
);
1474 /* Deallocate memory for one device */
1475 static void nozomi_card_exit(struct pci_dev
*pdev
)
1478 struct ctrl_ul ctrl
;
1479 struct nozomi
*dc
= pci_get_drvdata(pdev
);
1481 /* Disable all interrupts */
1483 writew(dc
->last_ier
, dc
->reg_ier
);
1487 /* Send 0x0001, command card to resend the reset token. */
1488 /* This is to get the reset when the module is reloaded. */
1493 DBG1("sending flow control 0x%04X", *((u16
*)&ctrl
));
1495 /* Setup dc->reg addresses to we can use defines here */
1496 write_mem32(dc
->port
[PORT_CTRL
].ul_addr
[0], (u32
*)&ctrl
, 2);
1497 writew(CTRL_UL
, dc
->reg_fcr
); /* push the token to the card. */
1499 remove_sysfs_files(dc
);
1501 free_irq(pdev
->irq
, dc
);
1503 for (i
= 0; i
< MAX_PORT
; i
++)
1504 kfifo_free(&dc
->port
[i
].fifo_ul
);
1506 kfree(dc
->send_buf
);
1508 iounmap(dc
->base_addr
);
1510 pci_release_regions(pdev
);
1512 pci_disable_device(pdev
);
1514 ndevs
[dc
->index_start
/ MAX_PORT
] = NULL
;
1519 static void set_rts(const struct tty_struct
*tty
, int rts
)
1521 struct port
*port
= get_port_by_tty(tty
);
1523 port
->ctrl_ul
.RTS
= rts
;
1524 port
->update_flow_control
= 1;
1525 enable_transmit_ul(PORT_CTRL
, get_dc_by_tty(tty
));
1528 static void set_dtr(const struct tty_struct
*tty
, int dtr
)
1530 struct port
*port
= get_port_by_tty(tty
);
1532 DBG1("SETTING DTR index: %d, dtr: %d", tty
->index
, dtr
);
1534 port
->ctrl_ul
.DTR
= dtr
;
1535 port
->update_flow_control
= 1;
1536 enable_transmit_ul(PORT_CTRL
, get_dc_by_tty(tty
));
1540 * ----------------------------------------------------------------------------
1542 * ----------------------------------------------------------------------------
1545 static int ntty_install(struct tty_driver
*driver
, struct tty_struct
*tty
)
1547 struct port
*port
= get_port_by_tty(tty
);
1548 struct nozomi
*dc
= get_dc_by_tty(tty
);
1550 if (!port
|| !dc
|| dc
->state
!= NOZOMI_STATE_READY
)
1552 ret
= tty_standard_install(driver
, tty
);
1554 tty
->driver_data
= port
;
1558 static void ntty_cleanup(struct tty_struct
*tty
)
1560 tty
->driver_data
= NULL
;
1563 static int ntty_activate(struct tty_port
*tport
, struct tty_struct
*tty
)
1565 struct port
*port
= container_of(tport
, struct port
, port
);
1566 struct nozomi
*dc
= port
->dc
;
1567 unsigned long flags
;
1569 DBG1("open: %d", port
->token_dl
);
1570 spin_lock_irqsave(&dc
->spin_mutex
, flags
);
1571 dc
->last_ier
= dc
->last_ier
| port
->token_dl
;
1572 writew(dc
->last_ier
, dc
->reg_ier
);
1574 spin_unlock_irqrestore(&dc
->spin_mutex
, flags
);
1575 printk("noz: activated %d: %p\n", tty
->index
, tport
);
1579 static int ntty_open(struct tty_struct
*tty
, struct file
*filp
)
1581 struct port
*port
= tty
->driver_data
;
1582 return tty_port_open(&port
->port
, tty
, filp
);
1585 static void ntty_shutdown(struct tty_port
*tport
)
1587 struct port
*port
= container_of(tport
, struct port
, port
);
1588 struct nozomi
*dc
= port
->dc
;
1589 unsigned long flags
;
1591 DBG1("close: %d", port
->token_dl
);
1592 spin_lock_irqsave(&dc
->spin_mutex
, flags
);
1593 dc
->last_ier
&= ~(port
->token_dl
);
1594 writew(dc
->last_ier
, dc
->reg_ier
);
1596 spin_unlock_irqrestore(&dc
->spin_mutex
, flags
);
1597 printk("noz: shutdown %p\n", tport
);
1600 static void ntty_close(struct tty_struct
*tty
, struct file
*filp
)
1602 struct port
*port
= tty
->driver_data
;
1604 tty_port_close(&port
->port
, tty
, filp
);
1607 static void ntty_hangup(struct tty_struct
*tty
)
1609 struct port
*port
= tty
->driver_data
;
1610 tty_port_hangup(&port
->port
);
1614 * called when the userspace process writes to the tty (/dev/noz*).
1615 * Data is inserted into a fifo, which is then read and transferred to the modem.
1617 static int ntty_write(struct tty_struct
*tty
, const unsigned char *buffer
,
1621 struct nozomi
*dc
= get_dc_by_tty(tty
);
1622 struct port
*port
= tty
->driver_data
;
1623 unsigned long flags
;
1625 /* DBG1( "WRITEx: %d, index = %d", count, index); */
1630 rval
= kfifo_in(&port
->fifo_ul
, (unsigned char *)buffer
, count
);
1632 spin_lock_irqsave(&dc
->spin_mutex
, flags
);
1633 /* CTS is only valid on the modem channel */
1634 if (port
== &(dc
->port
[PORT_MDM
])) {
1635 if (port
->ctrl_dl
.CTS
) {
1636 DBG4("Enable interrupt");
1637 enable_transmit_ul(tty
->index
% MAX_PORT
, dc
);
1639 dev_err(&dc
->pdev
->dev
,
1640 "CTS not active on modem port?\n");
1643 enable_transmit_ul(tty
->index
% MAX_PORT
, dc
);
1645 spin_unlock_irqrestore(&dc
->spin_mutex
, flags
);
1651 * Calculate how much is left in device
1652 * This method is called by the upper tty layer.
1653 * #according to sources N_TTY.c it expects a value >= 0 and
1654 * does not check for negative values.
1656 * If the port is unplugged report lots of room and let the bits
1657 * dribble away so we don't block anything.
1659 static int ntty_write_room(struct tty_struct
*tty
)
1661 struct port
*port
= tty
->driver_data
;
1663 const struct nozomi
*dc
= get_dc_by_tty(tty
);
1666 room
= kfifo_avail(&port
->fifo_ul
);
1671 /* Gets io control parameters */
1672 static int ntty_tiocmget(struct tty_struct
*tty
)
1674 const struct port
*port
= tty
->driver_data
;
1675 const struct ctrl_dl
*ctrl_dl
= &port
->ctrl_dl
;
1676 const struct ctrl_ul
*ctrl_ul
= &port
->ctrl_ul
;
1678 /* Note: these could change under us but it is not clear this
1680 return (ctrl_ul
->RTS
? TIOCM_RTS
: 0)
1681 | (ctrl_ul
->DTR
? TIOCM_DTR
: 0)
1682 | (ctrl_dl
->DCD
? TIOCM_CAR
: 0)
1683 | (ctrl_dl
->RI
? TIOCM_RNG
: 0)
1684 | (ctrl_dl
->DSR
? TIOCM_DSR
: 0)
1685 | (ctrl_dl
->CTS
? TIOCM_CTS
: 0);
1688 /* Sets io controls parameters */
1689 static int ntty_tiocmset(struct tty_struct
*tty
,
1690 unsigned int set
, unsigned int clear
)
1692 struct nozomi
*dc
= get_dc_by_tty(tty
);
1693 unsigned long flags
;
1695 spin_lock_irqsave(&dc
->spin_mutex
, flags
);
1696 if (set
& TIOCM_RTS
)
1698 else if (clear
& TIOCM_RTS
)
1701 if (set
& TIOCM_DTR
)
1703 else if (clear
& TIOCM_DTR
)
1705 spin_unlock_irqrestore(&dc
->spin_mutex
, flags
);
1710 static int ntty_cflags_changed(struct port
*port
, unsigned long flags
,
1711 struct async_icount
*cprev
)
1713 const struct async_icount cnow
= port
->tty_icount
;
1716 ret
= ((flags
& TIOCM_RNG
) && (cnow
.rng
!= cprev
->rng
))
1717 || ((flags
& TIOCM_DSR
) && (cnow
.dsr
!= cprev
->dsr
))
1718 || ((flags
& TIOCM_CD
) && (cnow
.dcd
!= cprev
->dcd
))
1719 || ((flags
& TIOCM_CTS
) && (cnow
.cts
!= cprev
->cts
));
1726 static int ntty_tiocgicount(struct tty_struct
*tty
,
1727 struct serial_icounter_struct
*icount
)
1729 struct port
*port
= tty
->driver_data
;
1730 const struct async_icount cnow
= port
->tty_icount
;
1732 icount
->cts
= cnow
.cts
;
1733 icount
->dsr
= cnow
.dsr
;
1734 icount
->rng
= cnow
.rng
;
1735 icount
->dcd
= cnow
.dcd
;
1736 icount
->rx
= cnow
.rx
;
1737 icount
->tx
= cnow
.tx
;
1738 icount
->frame
= cnow
.frame
;
1739 icount
->overrun
= cnow
.overrun
;
1740 icount
->parity
= cnow
.parity
;
1741 icount
->brk
= cnow
.brk
;
1742 icount
->buf_overrun
= cnow
.buf_overrun
;
1746 static int ntty_ioctl(struct tty_struct
*tty
,
1747 unsigned int cmd
, unsigned long arg
)
1749 struct port
*port
= tty
->driver_data
;
1750 int rval
= -ENOIOCTLCMD
;
1752 DBG1("******** IOCTL, cmd: %d", cmd
);
1756 struct async_icount cprev
= port
->tty_icount
;
1758 rval
= wait_event_interruptible(port
->tty_wait
,
1759 ntty_cflags_changed(port
, arg
, &cprev
));
1763 DBG1("ERR: 0x%08X, %d", cmd
, cmd
);
1771 * Called by the upper tty layer when tty buffers are ready
1772 * to receive data again after a call to throttle.
1774 static void ntty_unthrottle(struct tty_struct
*tty
)
1776 struct nozomi
*dc
= get_dc_by_tty(tty
);
1777 unsigned long flags
;
1780 spin_lock_irqsave(&dc
->spin_mutex
, flags
);
1781 enable_transmit_dl(tty
->index
% MAX_PORT
, dc
);
1784 spin_unlock_irqrestore(&dc
->spin_mutex
, flags
);
1788 * Called by the upper tty layer when the tty buffers are almost full.
1789 * The driver should stop send more data.
1791 static void ntty_throttle(struct tty_struct
*tty
)
1793 struct nozomi
*dc
= get_dc_by_tty(tty
);
1794 unsigned long flags
;
1797 spin_lock_irqsave(&dc
->spin_mutex
, flags
);
1799 spin_unlock_irqrestore(&dc
->spin_mutex
, flags
);
1802 /* Returns number of chars in buffer, called by tty layer */
1803 static s32
ntty_chars_in_buffer(struct tty_struct
*tty
)
1805 struct port
*port
= tty
->driver_data
;
1806 struct nozomi
*dc
= get_dc_by_tty(tty
);
1809 if (unlikely(!dc
|| !port
)) {
1810 goto exit_in_buffer
;
1813 rval
= kfifo_len(&port
->fifo_ul
);
1819 static const struct tty_port_operations noz_tty_port_ops
= {
1820 .activate
= ntty_activate
,
1821 .shutdown
= ntty_shutdown
,
1824 static const struct tty_operations tty_ops
= {
1825 .ioctl
= ntty_ioctl
,
1827 .close
= ntty_close
,
1828 .hangup
= ntty_hangup
,
1829 .write
= ntty_write
,
1830 .write_room
= ntty_write_room
,
1831 .unthrottle
= ntty_unthrottle
,
1832 .throttle
= ntty_throttle
,
1833 .chars_in_buffer
= ntty_chars_in_buffer
,
1834 .tiocmget
= ntty_tiocmget
,
1835 .tiocmset
= ntty_tiocmset
,
1836 .get_icount
= ntty_tiocgicount
,
1837 .install
= ntty_install
,
1838 .cleanup
= ntty_cleanup
,
1841 /* Module initialization */
1842 static struct pci_driver nozomi_driver
= {
1843 .name
= NOZOMI_NAME
,
1844 .id_table
= nozomi_pci_tbl
,
1845 .probe
= nozomi_card_init
,
1846 .remove
= nozomi_card_exit
,
1849 static __init
int nozomi_init(void)
1853 printk(KERN_INFO
"Initializing %s\n", VERSION_STRING
);
1855 ntty_driver
= alloc_tty_driver(NTTY_TTY_MAXMINORS
);
1859 ntty_driver
->driver_name
= NOZOMI_NAME_TTY
;
1860 ntty_driver
->name
= "noz";
1861 ntty_driver
->major
= 0;
1862 ntty_driver
->type
= TTY_DRIVER_TYPE_SERIAL
;
1863 ntty_driver
->subtype
= SERIAL_TYPE_NORMAL
;
1864 ntty_driver
->flags
= TTY_DRIVER_REAL_RAW
| TTY_DRIVER_DYNAMIC_DEV
;
1865 ntty_driver
->init_termios
= tty_std_termios
;
1866 ntty_driver
->init_termios
.c_cflag
= B115200
| CS8
| CREAD
| \
1868 ntty_driver
->init_termios
.c_ispeed
= 115200;
1869 ntty_driver
->init_termios
.c_ospeed
= 115200;
1870 tty_set_operations(ntty_driver
, &tty_ops
);
1872 ret
= tty_register_driver(ntty_driver
);
1874 printk(KERN_ERR
"Nozomi: failed to register ntty driver\n");
1878 ret
= pci_register_driver(&nozomi_driver
);
1880 printk(KERN_ERR
"Nozomi: can't register pci driver\n");
1886 tty_unregister_driver(ntty_driver
);
1888 put_tty_driver(ntty_driver
);
1892 static __exit
void nozomi_exit(void)
1894 printk(KERN_INFO
"Unloading %s\n", DRIVER_DESC
);
1895 pci_unregister_driver(&nozomi_driver
);
1896 tty_unregister_driver(ntty_driver
);
1897 put_tty_driver(ntty_driver
);
1900 module_init(nozomi_init
);
1901 module_exit(nozomi_exit
);
1903 MODULE_LICENSE("Dual BSD/GPL");
1904 MODULE_DESCRIPTION(DRIVER_DESC
);