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>
50 /* Default debug printout level */
51 #define NOZOMI_DEBUG_LEVEL 0x00
52 static int debug
= NOZOMI_DEBUG_LEVEL
;
53 module_param(debug
, int, S_IRUGO
| S_IWUSR
);
55 /* Macros definitions */
56 #define DBG_(lvl, fmt, args...) \
59 pr_debug("[%d] %s(): " fmt "\n", \
60 __LINE__, __func__, ##args); \
63 #define DBG1(args...) DBG_(0x01, ##args)
64 #define DBG2(args...) DBG_(0x02, ##args)
65 #define DBG3(args...) DBG_(0x04, ##args)
66 #define DBG4(args...) DBG_(0x08, ##args)
68 #define TMP_BUF_MAX 256
71 #define NOZOMI_NAME "nozomi"
72 #define NOZOMI_NAME_TTY "nozomi_tty"
74 #define NTTY_TTY_MAXMINORS 256
75 #define NTTY_FIFO_BUFFER_SIZE 8192
77 /* Must be power of 2 */
78 #define FIFO_BUFFER_SIZE_UL 8192
80 /* Size of tmp send buffer to card */
81 #define SEND_BUF_MAX 1024
82 #define RECEIVE_BUF_MAX 4
85 #define R_IIR 0x0000 /* Interrupt Identity Register */
86 #define R_FCR 0x0000 /* Flow Control Register */
87 #define R_IER 0x0004 /* Interrupt Enable Register */
89 #define NOZOMI_CONFIG_MAGIC 0xEFEFFEFE
90 #define TOGGLE_VALID 0x0000
92 /* Definition of interrupt tokens */
93 #define MDM_DL1 0x0001
94 #define MDM_UL1 0x0002
95 #define MDM_DL2 0x0004
96 #define MDM_UL2 0x0008
97 #define DIAG_DL1 0x0010
98 #define DIAG_DL2 0x0020
99 #define DIAG_UL 0x0040
100 #define APP1_DL 0x0080
101 #define APP1_UL 0x0100
102 #define APP2_DL 0x0200
103 #define APP2_UL 0x0400
104 #define CTRL_DL 0x0800
105 #define CTRL_UL 0x1000
108 #define MDM_DL (MDM_DL1 | MDM_DL2)
109 #define MDM_UL (MDM_UL1 | MDM_UL2)
110 #define DIAG_DL (DIAG_DL1 | DIAG_DL2)
112 /* modem signal definition */
113 #define CTRL_DSR 0x0001
114 #define CTRL_DCD 0x0002
115 #define CTRL_RI 0x0004
116 #define CTRL_CTS 0x0008
118 #define CTRL_DTR 0x0001
119 #define CTRL_RTS 0x0002
122 #define NOZOMI_MAX_PORTS 5
123 #define NOZOMI_MAX_CARDS (NTTY_TTY_MAXMINORS / MAX_PORT)
125 /* Type definitions */
128 * There are two types of nozomi cards,
129 * one with 2048 memory and with 8192 memory
132 F32_2
= 2048, /* 512 bytes downlink + uplink * 2 -> 2048 */
133 F32_8
= 8192, /* 3072 bytes downl. + 1024 bytes uplink * 2 -> 8192 */
136 /* Initialization states a card can be in */
138 NOZOMI_STATE_UNKNOWN
= 0,
139 NOZOMI_STATE_ENABLED
= 1, /* pci device enabled */
140 NOZOMI_STATE_ALLOCATED
= 2, /* config setup done */
141 NOZOMI_STATE_READY
= 3, /* flowcontrols received */
144 /* Two different toggle channels exist */
150 /* Port definition for the card regarding flow control */
151 enum ctrl_port_type
{
160 /* Ports that the nozomi has */
174 unsigned int enabled
:5; /*
175 * Toggle fields are valid if enabled is 0,
176 * else A-channels must always be used.
178 unsigned int diag_dl
:1;
179 unsigned int mdm_dl
:1;
180 unsigned int mdm_ul
:1;
181 } __attribute__ ((packed
));
183 /* Configuration table to read at startup of card */
184 /* Is for now only needed during initialization phase */
185 struct config_table
{
187 u16 product_information
;
190 struct toggles toggle
;
193 * If this is 64, it can hold
194 * 60 bytes + 4 that is length field
200 * If this is 64, it can hold
201 * 60 bytes + 4 that is length field
216 } __attribute__ ((packed
));
218 /* This stores all control downlink flags */
221 unsigned int reserved
:4;
226 } __attribute__ ((packed
));
228 /* This stores all control uplink flags */
231 unsigned int reserved
:6;
234 } __attribute__ ((packed
));
239 /* This represents the toggle information */
241 unsigned int mdm_ul
:1;
242 unsigned int mdm_dl
:1;
243 unsigned int diag_dl
:1;
244 unsigned int enabled
:5; /*
245 * Toggle fields are valid if enabled is 0,
246 * else A-channels must always be used.
248 } __attribute__ ((packed
));
250 /* Configuration table to read at startup of card */
251 struct config_table
{
254 u16 product_information
;
255 struct toggles toggle
;
259 * If this is 64, it can hold
260 * 60 bytes + 4 that is length field
276 } __attribute__ ((packed
));
278 /* This stores all control downlink flags */
284 unsigned int reserved
:4;
286 } __attribute__ ((packed
));
288 /* This stores all control uplink flags */
292 unsigned int reserved
:6;
294 } __attribute__ ((packed
));
297 /* This holds all information that is needed regarding a port */
299 struct tty_port port
;
300 u8 update_flow_control
;
301 struct ctrl_ul ctrl_ul
;
302 struct ctrl_dl ctrl_dl
;
303 struct kfifo fifo_ul
;
304 void __iomem
*dl_addr
[2];
307 void __iomem
*ul_addr
[2];
312 wait_queue_head_t tty_wait
;
313 struct async_icount tty_icount
;
318 /* Private data one for each card in the system */
320 void __iomem
*base_addr
;
323 /* Pointers to registers */
324 void __iomem
*reg_iir
;
325 void __iomem
*reg_fcr
;
326 void __iomem
*reg_ier
;
329 enum card_type card_type
;
330 struct config_table config_table
; /* Configuration table */
331 struct pci_dev
*pdev
;
332 struct port port
[NOZOMI_MAX_PORTS
];
335 spinlock_t spin_mutex
; /* secures access to registers and tty */
337 unsigned int index_start
;
338 enum card_state state
;
342 /* Global variables */
343 static const struct pci_device_id nozomi_pci_tbl
[] = {
344 {PCI_DEVICE(0x1931, 0x000c)}, /* Nozomi HSDPA */
348 MODULE_DEVICE_TABLE(pci
, nozomi_pci_tbl
);
350 static struct nozomi
*ndevs
[NOZOMI_MAX_CARDS
];
351 static struct tty_driver
*ntty_driver
;
353 static const struct tty_port_operations noz_tty_port_ops
;
356 * find card by tty_index
358 static inline struct nozomi
*get_dc_by_tty(const struct tty_struct
*tty
)
360 return tty
? ndevs
[tty
->index
/ MAX_PORT
] : NULL
;
363 static inline struct port
*get_port_by_tty(const struct tty_struct
*tty
)
365 struct nozomi
*ndev
= get_dc_by_tty(tty
);
366 return ndev
? &ndev
->port
[tty
->index
% MAX_PORT
] : NULL
;
375 static void read_mem32(u32
*buf
, const void __iomem
*mem_addr_start
,
379 const u32 __iomem
*ptr
= mem_addr_start
;
382 if (unlikely(!ptr
|| !buf
))
385 /* shortcut for extremely often used cases */
386 switch (size_bytes
) {
387 case 2: /* 2 bytes */
389 *buf16
= __le16_to_cpu(readw(ptr
));
391 case 4: /* 4 bytes */
392 *(buf
) = __le32_to_cpu(readl(ptr
));
396 while (i
< size_bytes
) {
397 if (size_bytes
- i
== 2) {
398 /* Handle 2 bytes in the end */
400 *(buf16
) = __le16_to_cpu(readw(ptr
));
404 *(buf
) = __le32_to_cpu(readl(ptr
));
419 static u32
write_mem32(void __iomem
*mem_addr_start
, const u32
*buf
,
423 u32 __iomem
*ptr
= mem_addr_start
;
426 if (unlikely(!ptr
|| !buf
))
429 /* shortcut for extremely often used cases */
430 switch (size_bytes
) {
431 case 2: /* 2 bytes */
432 buf16
= (const u16
*)buf
;
433 writew(__cpu_to_le16(*buf16
), ptr
);
436 * also needs to write 4 bytes in this case
437 * so falling through..
440 case 4: /* 4 bytes */
441 writel(__cpu_to_le32(*buf
), ptr
);
445 while (i
< size_bytes
) {
446 if (size_bytes
- i
== 2) {
448 buf16
= (const u16
*)buf
;
449 writew(__cpu_to_le16(*buf16
), ptr
);
453 writel(__cpu_to_le32(*buf
), ptr
);
462 /* Setup pointers to different channels and also setup buffer sizes. */
463 static void nozomi_setup_memory(struct nozomi
*dc
)
465 void __iomem
*offset
= dc
->base_addr
+ dc
->config_table
.dl_start
;
466 /* The length reported is including the length field of 4 bytes,
467 * hence subtract with 4.
469 const u16 buff_offset
= 4;
471 /* Modem port dl configuration */
472 dc
->port
[PORT_MDM
].dl_addr
[CH_A
] = offset
;
473 dc
->port
[PORT_MDM
].dl_addr
[CH_B
] =
474 (offset
+= dc
->config_table
.dl_mdm_len1
);
475 dc
->port
[PORT_MDM
].dl_size
[CH_A
] =
476 dc
->config_table
.dl_mdm_len1
- buff_offset
;
477 dc
->port
[PORT_MDM
].dl_size
[CH_B
] =
478 dc
->config_table
.dl_mdm_len2
- buff_offset
;
480 /* Diag port dl configuration */
481 dc
->port
[PORT_DIAG
].dl_addr
[CH_A
] =
482 (offset
+= dc
->config_table
.dl_mdm_len2
);
483 dc
->port
[PORT_DIAG
].dl_size
[CH_A
] =
484 dc
->config_table
.dl_diag_len1
- buff_offset
;
485 dc
->port
[PORT_DIAG
].dl_addr
[CH_B
] =
486 (offset
+= dc
->config_table
.dl_diag_len1
);
487 dc
->port
[PORT_DIAG
].dl_size
[CH_B
] =
488 dc
->config_table
.dl_diag_len2
- buff_offset
;
490 /* App1 port dl configuration */
491 dc
->port
[PORT_APP1
].dl_addr
[CH_A
] =
492 (offset
+= dc
->config_table
.dl_diag_len2
);
493 dc
->port
[PORT_APP1
].dl_size
[CH_A
] =
494 dc
->config_table
.dl_app1_len
- buff_offset
;
496 /* App2 port dl configuration */
497 dc
->port
[PORT_APP2
].dl_addr
[CH_A
] =
498 (offset
+= dc
->config_table
.dl_app1_len
);
499 dc
->port
[PORT_APP2
].dl_size
[CH_A
] =
500 dc
->config_table
.dl_app2_len
- buff_offset
;
502 /* Ctrl dl configuration */
503 dc
->port
[PORT_CTRL
].dl_addr
[CH_A
] =
504 (offset
+= dc
->config_table
.dl_app2_len
);
505 dc
->port
[PORT_CTRL
].dl_size
[CH_A
] =
506 dc
->config_table
.dl_ctrl_len
- buff_offset
;
508 offset
= dc
->base_addr
+ dc
->config_table
.ul_start
;
510 /* Modem Port ul configuration */
511 dc
->port
[PORT_MDM
].ul_addr
[CH_A
] = offset
;
512 dc
->port
[PORT_MDM
].ul_size
[CH_A
] =
513 dc
->config_table
.ul_mdm_len1
- buff_offset
;
514 dc
->port
[PORT_MDM
].ul_addr
[CH_B
] =
515 (offset
+= dc
->config_table
.ul_mdm_len1
);
516 dc
->port
[PORT_MDM
].ul_size
[CH_B
] =
517 dc
->config_table
.ul_mdm_len2
- buff_offset
;
519 /* Diag port ul configuration */
520 dc
->port
[PORT_DIAG
].ul_addr
[CH_A
] =
521 (offset
+= dc
->config_table
.ul_mdm_len2
);
522 dc
->port
[PORT_DIAG
].ul_size
[CH_A
] =
523 dc
->config_table
.ul_diag_len
- buff_offset
;
525 /* App1 port ul configuration */
526 dc
->port
[PORT_APP1
].ul_addr
[CH_A
] =
527 (offset
+= dc
->config_table
.ul_diag_len
);
528 dc
->port
[PORT_APP1
].ul_size
[CH_A
] =
529 dc
->config_table
.ul_app1_len
- buff_offset
;
531 /* App2 port ul configuration */
532 dc
->port
[PORT_APP2
].ul_addr
[CH_A
] =
533 (offset
+= dc
->config_table
.ul_app1_len
);
534 dc
->port
[PORT_APP2
].ul_size
[CH_A
] =
535 dc
->config_table
.ul_app2_len
- buff_offset
;
537 /* Ctrl ul configuration */
538 dc
->port
[PORT_CTRL
].ul_addr
[CH_A
] =
539 (offset
+= dc
->config_table
.ul_app2_len
);
540 dc
->port
[PORT_CTRL
].ul_size
[CH_A
] =
541 dc
->config_table
.ul_ctrl_len
- buff_offset
;
544 /* Dump config table under initalization phase */
546 static void dump_table(const struct nozomi
*dc
)
548 DBG3("signature: 0x%08X", dc
->config_table
.signature
);
549 DBG3("version: 0x%04X", dc
->config_table
.version
);
550 DBG3("product_information: 0x%04X", \
551 dc
->config_table
.product_information
);
552 DBG3("toggle enabled: %d", dc
->config_table
.toggle
.enabled
);
553 DBG3("toggle up_mdm: %d", dc
->config_table
.toggle
.mdm_ul
);
554 DBG3("toggle dl_mdm: %d", dc
->config_table
.toggle
.mdm_dl
);
555 DBG3("toggle dl_dbg: %d", dc
->config_table
.toggle
.diag_dl
);
557 DBG3("dl_start: 0x%04X", dc
->config_table
.dl_start
);
558 DBG3("dl_mdm_len0: 0x%04X, %d", dc
->config_table
.dl_mdm_len1
,
559 dc
->config_table
.dl_mdm_len1
);
560 DBG3("dl_mdm_len1: 0x%04X, %d", dc
->config_table
.dl_mdm_len2
,
561 dc
->config_table
.dl_mdm_len2
);
562 DBG3("dl_diag_len0: 0x%04X, %d", dc
->config_table
.dl_diag_len1
,
563 dc
->config_table
.dl_diag_len1
);
564 DBG3("dl_diag_len1: 0x%04X, %d", dc
->config_table
.dl_diag_len2
,
565 dc
->config_table
.dl_diag_len2
);
566 DBG3("dl_app1_len: 0x%04X, %d", dc
->config_table
.dl_app1_len
,
567 dc
->config_table
.dl_app1_len
);
568 DBG3("dl_app2_len: 0x%04X, %d", dc
->config_table
.dl_app2_len
,
569 dc
->config_table
.dl_app2_len
);
570 DBG3("dl_ctrl_len: 0x%04X, %d", dc
->config_table
.dl_ctrl_len
,
571 dc
->config_table
.dl_ctrl_len
);
572 DBG3("ul_start: 0x%04X, %d", dc
->config_table
.ul_start
,
573 dc
->config_table
.ul_start
);
574 DBG3("ul_mdm_len[0]: 0x%04X, %d", dc
->config_table
.ul_mdm_len1
,
575 dc
->config_table
.ul_mdm_len1
);
576 DBG3("ul_mdm_len[1]: 0x%04X, %d", dc
->config_table
.ul_mdm_len2
,
577 dc
->config_table
.ul_mdm_len2
);
578 DBG3("ul_diag_len: 0x%04X, %d", dc
->config_table
.ul_diag_len
,
579 dc
->config_table
.ul_diag_len
);
580 DBG3("ul_app1_len: 0x%04X, %d", dc
->config_table
.ul_app1_len
,
581 dc
->config_table
.ul_app1_len
);
582 DBG3("ul_app2_len: 0x%04X, %d", dc
->config_table
.ul_app2_len
,
583 dc
->config_table
.ul_app2_len
);
584 DBG3("ul_ctrl_len: 0x%04X, %d", dc
->config_table
.ul_ctrl_len
,
585 dc
->config_table
.ul_ctrl_len
);
588 static inline void dump_table(const struct nozomi
*dc
) { }
592 * Read configuration table from card under intalization phase
593 * Returns 1 if ok, else 0
595 static int nozomi_read_config_table(struct nozomi
*dc
)
597 read_mem32((u32
*) &dc
->config_table
, dc
->base_addr
+ 0,
598 sizeof(struct config_table
));
600 if (dc
->config_table
.signature
!= NOZOMI_CONFIG_MAGIC
) {
601 dev_err(&dc
->pdev
->dev
, "ConfigTable Bad! 0x%08X != 0x%08X\n",
602 dc
->config_table
.signature
, NOZOMI_CONFIG_MAGIC
);
606 if ((dc
->config_table
.version
== 0)
607 || (dc
->config_table
.toggle
.enabled
== TOGGLE_VALID
)) {
609 DBG1("Second phase, configuring card");
611 nozomi_setup_memory(dc
);
613 dc
->port
[PORT_MDM
].toggle_ul
= dc
->config_table
.toggle
.mdm_ul
;
614 dc
->port
[PORT_MDM
].toggle_dl
= dc
->config_table
.toggle
.mdm_dl
;
615 dc
->port
[PORT_DIAG
].toggle_dl
= dc
->config_table
.toggle
.diag_dl
;
616 DBG1("toggle ports: MDM UL:%d MDM DL:%d, DIAG DL:%d",
617 dc
->port
[PORT_MDM
].toggle_ul
,
618 dc
->port
[PORT_MDM
].toggle_dl
, dc
->port
[PORT_DIAG
].toggle_dl
);
622 for (i
= PORT_MDM
; i
< MAX_PORT
; i
++) {
623 memset(&dc
->port
[i
].ctrl_dl
, 0, sizeof(struct ctrl_dl
));
624 memset(&dc
->port
[i
].ctrl_ul
, 0, sizeof(struct ctrl_ul
));
627 /* Enable control channel */
628 dc
->last_ier
= dc
->last_ier
| CTRL_DL
;
629 writew(dc
->last_ier
, dc
->reg_ier
);
631 dc
->state
= NOZOMI_STATE_ALLOCATED
;
632 dev_info(&dc
->pdev
->dev
, "Initialization OK!\n");
636 if ((dc
->config_table
.version
> 0)
637 && (dc
->config_table
.toggle
.enabled
!= TOGGLE_VALID
)) {
639 DBG1("First phase: pushing upload buffers, clearing download");
641 dev_info(&dc
->pdev
->dev
, "Version of card: %d\n",
642 dc
->config_table
.version
);
644 /* Here we should disable all I/O over F32. */
645 nozomi_setup_memory(dc
);
648 * We should send ALL channel pair tokens back along
652 /* push upload modem buffers */
653 write_mem32(dc
->port
[PORT_MDM
].ul_addr
[CH_A
],
655 write_mem32(dc
->port
[PORT_MDM
].ul_addr
[CH_B
],
658 writew(MDM_UL
| DIAG_DL
| MDM_DL
, dc
->reg_fcr
);
660 DBG1("First phase done");
666 /* Enable uplink interrupts */
667 static void enable_transmit_ul(enum port_type port
, struct nozomi
*dc
)
669 static const u16 mask
[] = {MDM_UL
, DIAG_UL
, APP1_UL
, APP2_UL
, CTRL_UL
};
671 if (port
< NOZOMI_MAX_PORTS
) {
672 dc
->last_ier
|= mask
[port
];
673 writew(dc
->last_ier
, dc
->reg_ier
);
675 dev_err(&dc
->pdev
->dev
, "Called with wrong port?\n");
679 /* Disable uplink interrupts */
680 static void disable_transmit_ul(enum port_type port
, struct nozomi
*dc
)
682 static const u16 mask
[] =
683 {~MDM_UL
, ~DIAG_UL
, ~APP1_UL
, ~APP2_UL
, ~CTRL_UL
};
685 if (port
< NOZOMI_MAX_PORTS
) {
686 dc
->last_ier
&= mask
[port
];
687 writew(dc
->last_ier
, dc
->reg_ier
);
689 dev_err(&dc
->pdev
->dev
, "Called with wrong port?\n");
693 /* Enable downlink interrupts */
694 static void enable_transmit_dl(enum port_type port
, struct nozomi
*dc
)
696 static const u16 mask
[] = {MDM_DL
, DIAG_DL
, APP1_DL
, APP2_DL
, CTRL_DL
};
698 if (port
< NOZOMI_MAX_PORTS
) {
699 dc
->last_ier
|= mask
[port
];
700 writew(dc
->last_ier
, dc
->reg_ier
);
702 dev_err(&dc
->pdev
->dev
, "Called with wrong port?\n");
706 /* Disable downlink interrupts */
707 static void disable_transmit_dl(enum port_type port
, struct nozomi
*dc
)
709 static const u16 mask
[] =
710 {~MDM_DL
, ~DIAG_DL
, ~APP1_DL
, ~APP2_DL
, ~CTRL_DL
};
712 if (port
< NOZOMI_MAX_PORTS
) {
713 dc
->last_ier
&= mask
[port
];
714 writew(dc
->last_ier
, dc
->reg_ier
);
716 dev_err(&dc
->pdev
->dev
, "Called with wrong port?\n");
721 * Return 1 - send buffer to card and ack.
722 * Return 0 - don't ack, don't send buffer to card.
724 static int send_data(enum port_type index
, struct nozomi
*dc
)
727 struct port
*port
= &dc
->port
[index
];
728 const u8 toggle
= port
->toggle_ul
;
729 void __iomem
*addr
= port
->ul_addr
[toggle
];
730 const u32 ul_size
= port
->ul_size
[toggle
];
732 /* Get data from tty and place in buf for now */
733 size
= kfifo_out(&port
->fifo_ul
, dc
->send_buf
,
734 ul_size
< SEND_BUF_MAX
? ul_size
: SEND_BUF_MAX
);
737 DBG4("No more data to send, disable link:");
741 /* Write length + data */
742 write_mem32(addr
, (u32
*) &size
, 4);
743 write_mem32(addr
+ 4, (u32
*) dc
->send_buf
, size
);
745 tty_port_tty_wakeup(&port
->port
);
750 /* If all data has been read, return 1, else 0 */
751 static int receive_data(enum port_type index
, struct nozomi
*dc
)
753 u8 buf
[RECEIVE_BUF_MAX
] = { 0 };
756 struct port
*port
= &dc
->port
[index
];
757 void __iomem
*addr
= port
->dl_addr
[port
->toggle_dl
];
758 struct tty_struct
*tty
= tty_port_tty_get(&port
->port
);
761 size
= __le32_to_cpu(readl(addr
));
763 if (tty
&& tty_throttled(tty
)) {
764 DBG1("No room in tty, don't read data, don't ack interrupt, "
765 "disable interrupt");
767 /* disable interrupt in downlink... */
768 disable_transmit_dl(index
, dc
);
773 if (unlikely(size
== 0)) {
774 dev_err(&dc
->pdev
->dev
, "size == 0?\n");
780 read_mem32((u32
*) buf
, addr
+ offset
, RECEIVE_BUF_MAX
);
783 tty_insert_flip_char(&port
->port
, buf
[0], TTY_NORMAL
);
785 } else if (size
< RECEIVE_BUF_MAX
) {
786 size
-= tty_insert_flip_string(&port
->port
, buf
, size
);
788 i
= tty_insert_flip_string(&port
->port
, buf
,
795 set_bit(index
, &dc
->flip
);
802 /* Debug for interrupts */
804 static char *interrupt2str(u16 interrupt
)
806 static char buf
[TMP_BUF_MAX
];
809 if (interrupt
& MDM_DL1
)
810 p
+= scnprintf(p
, TMP_BUF_MAX
, "MDM_DL1 ");
811 if (interrupt
& MDM_DL2
)
812 p
+= scnprintf(p
, TMP_BUF_MAX
- (p
- buf
), "MDM_DL2 ");
813 if (interrupt
& MDM_UL1
)
814 p
+= scnprintf(p
, TMP_BUF_MAX
- (p
- buf
), "MDM_UL1 ");
815 if (interrupt
& MDM_UL2
)
816 p
+= scnprintf(p
, TMP_BUF_MAX
- (p
- buf
), "MDM_UL2 ");
817 if (interrupt
& DIAG_DL1
)
818 p
+= scnprintf(p
, TMP_BUF_MAX
- (p
- buf
), "DIAG_DL1 ");
819 if (interrupt
& DIAG_DL2
)
820 p
+= scnprintf(p
, TMP_BUF_MAX
- (p
- buf
), "DIAG_DL2 ");
822 if (interrupt
& DIAG_UL
)
823 p
+= scnprintf(p
, TMP_BUF_MAX
- (p
- buf
), "DIAG_UL ");
825 if (interrupt
& APP1_DL
)
826 p
+= scnprintf(p
, TMP_BUF_MAX
- (p
- buf
), "APP1_DL ");
827 if (interrupt
& APP2_DL
)
828 p
+= scnprintf(p
, TMP_BUF_MAX
- (p
- buf
), "APP2_DL ");
830 if (interrupt
& APP1_UL
)
831 p
+= scnprintf(p
, TMP_BUF_MAX
- (p
- buf
), "APP1_UL ");
832 if (interrupt
& APP2_UL
)
833 p
+= scnprintf(p
, TMP_BUF_MAX
- (p
- buf
), "APP2_UL ");
835 if (interrupt
& CTRL_DL
)
836 p
+= scnprintf(p
, TMP_BUF_MAX
- (p
- buf
), "CTRL_DL ");
837 if (interrupt
& CTRL_UL
)
838 p
+= scnprintf(p
, TMP_BUF_MAX
- (p
- buf
), "CTRL_UL ");
840 if (interrupt
& RESET
)
841 p
+= scnprintf(p
, TMP_BUF_MAX
- (p
- buf
), "RESET ");
848 * Receive flow control
849 * Return 1 - If ok, else 0
851 static int receive_flow_control(struct nozomi
*dc
)
853 enum port_type port
= PORT_MDM
;
854 struct ctrl_dl ctrl_dl
;
855 struct ctrl_dl old_ctrl
;
858 read_mem32((u32
*) &ctrl_dl
, dc
->port
[PORT_CTRL
].dl_addr
[CH_A
], 2);
860 switch (ctrl_dl
.port
) {
862 DBG1("The Base Band sends this value as a response to a "
863 "request for IMSI detach sent over the control "
864 "channel uplink (see section 7.6.1).");
872 enable_ier
= DIAG_DL
;
876 enable_ier
= APP1_DL
;
880 enable_ier
= APP2_DL
;
881 if (dc
->state
== NOZOMI_STATE_ALLOCATED
) {
883 * After card initialization the flow control
884 * received for APP2 is always the last
886 dc
->state
= NOZOMI_STATE_READY
;
887 dev_info(&dc
->pdev
->dev
, "Device READY!\n");
891 dev_err(&dc
->pdev
->dev
,
892 "ERROR: flow control received for non-existing port\n");
896 DBG1("0x%04X->0x%04X", *((u16
*)&dc
->port
[port
].ctrl_dl
),
899 old_ctrl
= dc
->port
[port
].ctrl_dl
;
900 dc
->port
[port
].ctrl_dl
= ctrl_dl
;
902 if (old_ctrl
.CTS
== 1 && ctrl_dl
.CTS
== 0) {
903 DBG1("Disable interrupt (0x%04X) on port: %d",
905 disable_transmit_ul(port
, dc
);
907 } else if (old_ctrl
.CTS
== 0 && ctrl_dl
.CTS
== 1) {
909 if (kfifo_len(&dc
->port
[port
].fifo_ul
)) {
910 DBG1("Enable interrupt (0x%04X) on port: %d",
912 DBG1("Data in buffer [%d], enable transmit! ",
913 kfifo_len(&dc
->port
[port
].fifo_ul
));
914 enable_transmit_ul(port
, dc
);
916 DBG1("No data in buffer...");
920 if (*(u16
*)&old_ctrl
== *(u16
*)&ctrl_dl
) {
921 DBG1(" No change in mctrl");
924 /* Update statistics */
925 if (old_ctrl
.CTS
!= ctrl_dl
.CTS
)
926 dc
->port
[port
].tty_icount
.cts
++;
927 if (old_ctrl
.DSR
!= ctrl_dl
.DSR
)
928 dc
->port
[port
].tty_icount
.dsr
++;
929 if (old_ctrl
.RI
!= ctrl_dl
.RI
)
930 dc
->port
[port
].tty_icount
.rng
++;
931 if (old_ctrl
.DCD
!= ctrl_dl
.DCD
)
932 dc
->port
[port
].tty_icount
.dcd
++;
934 wake_up_interruptible(&dc
->port
[port
].tty_wait
);
936 DBG1("port: %d DCD(%d), CTS(%d), RI(%d), DSR(%d)",
938 dc
->port
[port
].tty_icount
.dcd
, dc
->port
[port
].tty_icount
.cts
,
939 dc
->port
[port
].tty_icount
.rng
, dc
->port
[port
].tty_icount
.dsr
);
944 static enum ctrl_port_type
port2ctrl(enum port_type port
,
945 const struct nozomi
*dc
)
957 dev_err(&dc
->pdev
->dev
,
958 "ERROR: send flow control " \
959 "received for non-existing port\n");
965 * Send flow control, can only update one channel at a time
966 * Return 0 - If we have updated all flow control
967 * Return 1 - If we need to update more flow control, ack current enable more
969 static int send_flow_control(struct nozomi
*dc
)
971 u32 i
, more_flow_control_to_be_updated
= 0;
974 for (i
= PORT_MDM
; i
< MAX_PORT
; i
++) {
975 if (dc
->port
[i
].update_flow_control
) {
976 if (more_flow_control_to_be_updated
) {
977 /* We have more flow control to be updated */
980 dc
->port
[i
].ctrl_ul
.port
= port2ctrl(i
, dc
);
981 ctrl
= (u16
*)&dc
->port
[i
].ctrl_ul
;
982 write_mem32(dc
->port
[PORT_CTRL
].ul_addr
[0], \
984 dc
->port
[i
].update_flow_control
= 0;
985 more_flow_control_to_be_updated
= 1;
992 * Handle downlink data, ports that are handled are modem and diagnostics
994 * Return 0 - toggle fields are out of sync
996 static int handle_data_dl(struct nozomi
*dc
, enum port_type port
, u8
*toggle
,
997 u16 read_iir
, u16 mask1
, u16 mask2
)
999 if (*toggle
== 0 && read_iir
& mask1
) {
1000 if (receive_data(port
, dc
)) {
1001 writew(mask1
, dc
->reg_fcr
);
1002 *toggle
= !(*toggle
);
1005 if (read_iir
& mask2
) {
1006 if (receive_data(port
, dc
)) {
1007 writew(mask2
, dc
->reg_fcr
);
1008 *toggle
= !(*toggle
);
1011 } else if (*toggle
== 1 && read_iir
& mask2
) {
1012 if (receive_data(port
, dc
)) {
1013 writew(mask2
, dc
->reg_fcr
);
1014 *toggle
= !(*toggle
);
1017 if (read_iir
& mask1
) {
1018 if (receive_data(port
, dc
)) {
1019 writew(mask1
, dc
->reg_fcr
);
1020 *toggle
= !(*toggle
);
1024 dev_err(&dc
->pdev
->dev
, "port out of sync!, toggle:%d\n",
1032 * Handle uplink data, this is currently for the modem port
1034 * Return 0 - toggle field are out of sync
1036 static int handle_data_ul(struct nozomi
*dc
, enum port_type port
, u16 read_iir
)
1038 u8
*toggle
= &(dc
->port
[port
].toggle_ul
);
1040 if (*toggle
== 0 && read_iir
& MDM_UL1
) {
1041 dc
->last_ier
&= ~MDM_UL
;
1042 writew(dc
->last_ier
, dc
->reg_ier
);
1043 if (send_data(port
, dc
)) {
1044 writew(MDM_UL1
, dc
->reg_fcr
);
1045 dc
->last_ier
= dc
->last_ier
| MDM_UL
;
1046 writew(dc
->last_ier
, dc
->reg_ier
);
1050 if (read_iir
& MDM_UL2
) {
1051 dc
->last_ier
&= ~MDM_UL
;
1052 writew(dc
->last_ier
, dc
->reg_ier
);
1053 if (send_data(port
, dc
)) {
1054 writew(MDM_UL2
, dc
->reg_fcr
);
1055 dc
->last_ier
= dc
->last_ier
| MDM_UL
;
1056 writew(dc
->last_ier
, dc
->reg_ier
);
1061 } else if (*toggle
== 1 && read_iir
& MDM_UL2
) {
1062 dc
->last_ier
&= ~MDM_UL
;
1063 writew(dc
->last_ier
, dc
->reg_ier
);
1064 if (send_data(port
, dc
)) {
1065 writew(MDM_UL2
, dc
->reg_fcr
);
1066 dc
->last_ier
= dc
->last_ier
| MDM_UL
;
1067 writew(dc
->last_ier
, dc
->reg_ier
);
1071 if (read_iir
& MDM_UL1
) {
1072 dc
->last_ier
&= ~MDM_UL
;
1073 writew(dc
->last_ier
, dc
->reg_ier
);
1074 if (send_data(port
, dc
)) {
1075 writew(MDM_UL1
, dc
->reg_fcr
);
1076 dc
->last_ier
= dc
->last_ier
| MDM_UL
;
1077 writew(dc
->last_ier
, dc
->reg_ier
);
1082 writew(read_iir
& MDM_UL
, dc
->reg_fcr
);
1083 dev_err(&dc
->pdev
->dev
, "port out of sync!\n");
1089 static irqreturn_t
interrupt_handler(int irq
, void *dev_id
)
1091 struct nozomi
*dc
= dev_id
;
1098 spin_lock(&dc
->spin_mutex
);
1099 read_iir
= readw(dc
->reg_iir
);
1102 if (read_iir
== (u16
)-1)
1105 * Just handle interrupt enabled in IER
1106 * (by masking with dc->last_ier)
1108 read_iir
&= dc
->last_ier
;
1114 DBG4("%s irq:0x%04X, prev:0x%04X", interrupt2str(read_iir
), read_iir
,
1117 if (read_iir
& RESET
) {
1118 if (unlikely(!nozomi_read_config_table(dc
))) {
1120 writew(dc
->last_ier
, dc
->reg_ier
);
1121 dev_err(&dc
->pdev
->dev
, "Could not read status from "
1122 "card, we should disable interface\n");
1124 writew(RESET
, dc
->reg_fcr
);
1126 /* No more useful info if this was the reset interrupt. */
1129 if (read_iir
& CTRL_UL
) {
1131 dc
->last_ier
&= ~CTRL_UL
;
1132 writew(dc
->last_ier
, dc
->reg_ier
);
1133 if (send_flow_control(dc
)) {
1134 writew(CTRL_UL
, dc
->reg_fcr
);
1135 dc
->last_ier
= dc
->last_ier
| CTRL_UL
;
1136 writew(dc
->last_ier
, dc
->reg_ier
);
1139 if (read_iir
& CTRL_DL
) {
1140 receive_flow_control(dc
);
1141 writew(CTRL_DL
, dc
->reg_fcr
);
1143 if (read_iir
& MDM_DL
) {
1144 if (!handle_data_dl(dc
, PORT_MDM
,
1145 &(dc
->port
[PORT_MDM
].toggle_dl
), read_iir
,
1146 MDM_DL1
, MDM_DL2
)) {
1147 dev_err(&dc
->pdev
->dev
, "MDM_DL out of sync!\n");
1151 if (read_iir
& MDM_UL
) {
1152 if (!handle_data_ul(dc
, PORT_MDM
, read_iir
)) {
1153 dev_err(&dc
->pdev
->dev
, "MDM_UL out of sync!\n");
1157 if (read_iir
& DIAG_DL
) {
1158 if (!handle_data_dl(dc
, PORT_DIAG
,
1159 &(dc
->port
[PORT_DIAG
].toggle_dl
), read_iir
,
1160 DIAG_DL1
, DIAG_DL2
)) {
1161 dev_err(&dc
->pdev
->dev
, "DIAG_DL out of sync!\n");
1165 if (read_iir
& DIAG_UL
) {
1166 dc
->last_ier
&= ~DIAG_UL
;
1167 writew(dc
->last_ier
, dc
->reg_ier
);
1168 if (send_data(PORT_DIAG
, dc
)) {
1169 writew(DIAG_UL
, dc
->reg_fcr
);
1170 dc
->last_ier
= dc
->last_ier
| DIAG_UL
;
1171 writew(dc
->last_ier
, dc
->reg_ier
);
1174 if (read_iir
& APP1_DL
) {
1175 if (receive_data(PORT_APP1
, dc
))
1176 writew(APP1_DL
, dc
->reg_fcr
);
1178 if (read_iir
& APP1_UL
) {
1179 dc
->last_ier
&= ~APP1_UL
;
1180 writew(dc
->last_ier
, dc
->reg_ier
);
1181 if (send_data(PORT_APP1
, dc
)) {
1182 writew(APP1_UL
, dc
->reg_fcr
);
1183 dc
->last_ier
= dc
->last_ier
| APP1_UL
;
1184 writew(dc
->last_ier
, dc
->reg_ier
);
1187 if (read_iir
& APP2_DL
) {
1188 if (receive_data(PORT_APP2
, dc
))
1189 writew(APP2_DL
, dc
->reg_fcr
);
1191 if (read_iir
& APP2_UL
) {
1192 dc
->last_ier
&= ~APP2_UL
;
1193 writew(dc
->last_ier
, dc
->reg_ier
);
1194 if (send_data(PORT_APP2
, dc
)) {
1195 writew(APP2_UL
, dc
->reg_fcr
);
1196 dc
->last_ier
= dc
->last_ier
| APP2_UL
;
1197 writew(dc
->last_ier
, dc
->reg_ier
);
1202 spin_unlock(&dc
->spin_mutex
);
1204 for (a
= 0; a
< NOZOMI_MAX_PORTS
; a
++)
1205 if (test_and_clear_bit(a
, &dc
->flip
))
1206 tty_flip_buffer_push(&dc
->port
[a
].port
);
1210 spin_unlock(&dc
->spin_mutex
);
1214 static void nozomi_get_card_type(struct nozomi
*dc
)
1219 for (i
= 0; i
< 6; i
++)
1220 size
+= pci_resource_len(dc
->pdev
, i
);
1222 /* Assume card type F32_8 if no match */
1223 dc
->card_type
= size
== 2048 ? F32_2
: F32_8
;
1225 dev_info(&dc
->pdev
->dev
, "Card type is: %d\n", dc
->card_type
);
1228 static void nozomi_setup_private_data(struct nozomi
*dc
)
1230 void __iomem
*offset
= dc
->base_addr
+ dc
->card_type
/ 2;
1233 dc
->reg_fcr
= (void __iomem
*)(offset
+ R_FCR
);
1234 dc
->reg_iir
= (void __iomem
*)(offset
+ R_IIR
);
1235 dc
->reg_ier
= (void __iomem
*)(offset
+ R_IER
);
1239 dc
->port
[PORT_MDM
].token_dl
= MDM_DL
;
1240 dc
->port
[PORT_DIAG
].token_dl
= DIAG_DL
;
1241 dc
->port
[PORT_APP1
].token_dl
= APP1_DL
;
1242 dc
->port
[PORT_APP2
].token_dl
= APP2_DL
;
1244 for (i
= 0; i
< MAX_PORT
; i
++)
1245 init_waitqueue_head(&dc
->port
[i
].tty_wait
);
1248 static ssize_t
card_type_show(struct device
*dev
, struct device_attribute
*attr
,
1251 const struct nozomi
*dc
= dev_get_drvdata(dev
);
1253 return sprintf(buf
, "%d\n", dc
->card_type
);
1255 static DEVICE_ATTR_RO(card_type
);
1257 static ssize_t
open_ttys_show(struct device
*dev
, struct device_attribute
*attr
,
1260 const struct nozomi
*dc
= dev_get_drvdata(dev
);
1262 return sprintf(buf
, "%u\n", dc
->open_ttys
);
1264 static DEVICE_ATTR_RO(open_ttys
);
1266 static void make_sysfs_files(struct nozomi
*dc
)
1268 if (device_create_file(&dc
->pdev
->dev
, &dev_attr_card_type
))
1269 dev_err(&dc
->pdev
->dev
,
1270 "Could not create sysfs file for card_type\n");
1271 if (device_create_file(&dc
->pdev
->dev
, &dev_attr_open_ttys
))
1272 dev_err(&dc
->pdev
->dev
,
1273 "Could not create sysfs file for open_ttys\n");
1276 static void remove_sysfs_files(struct nozomi
*dc
)
1278 device_remove_file(&dc
->pdev
->dev
, &dev_attr_card_type
);
1279 device_remove_file(&dc
->pdev
->dev
, &dev_attr_open_ttys
);
1282 /* Allocate memory for one device */
1283 static int nozomi_card_init(struct pci_dev
*pdev
,
1284 const struct pci_device_id
*ent
)
1287 struct nozomi
*dc
= NULL
;
1291 for (ndev_idx
= 0; ndev_idx
< ARRAY_SIZE(ndevs
); ndev_idx
++)
1292 if (!ndevs
[ndev_idx
])
1295 if (ndev_idx
>= ARRAY_SIZE(ndevs
)) {
1296 dev_err(&pdev
->dev
, "no free tty range for this card left\n");
1301 dc
= kzalloc(sizeof(struct nozomi
), GFP_KERNEL
);
1302 if (unlikely(!dc
)) {
1303 dev_err(&pdev
->dev
, "Could not allocate memory\n");
1310 ret
= pci_enable_device(dc
->pdev
);
1312 dev_err(&pdev
->dev
, "Failed to enable PCI Device\n");
1316 ret
= pci_request_regions(dc
->pdev
, NOZOMI_NAME
);
1318 dev_err(&pdev
->dev
, "I/O address 0x%04x already in use\n",
1319 (int) /* nozomi_private.io_addr */ 0);
1320 goto err_disable_device
;
1323 /* Find out what card type it is */
1324 nozomi_get_card_type(dc
);
1326 dc
->base_addr
= pci_iomap(dc
->pdev
, 0, dc
->card_type
);
1327 if (!dc
->base_addr
) {
1328 dev_err(&pdev
->dev
, "Unable to map card MMIO\n");
1333 dc
->send_buf
= kmalloc(SEND_BUF_MAX
, GFP_KERNEL
);
1334 if (!dc
->send_buf
) {
1335 dev_err(&pdev
->dev
, "Could not allocate send buffer?\n");
1340 for (i
= PORT_MDM
; i
< MAX_PORT
; i
++) {
1341 if (kfifo_alloc(&dc
->port
[i
].fifo_ul
, FIFO_BUFFER_SIZE_UL
,
1344 "Could not allocate kfifo buffer\n");
1346 goto err_free_kfifo
;
1350 spin_lock_init(&dc
->spin_mutex
);
1352 nozomi_setup_private_data(dc
);
1354 /* Disable all interrupts */
1356 writew(dc
->last_ier
, dc
->reg_ier
);
1358 ret
= request_irq(pdev
->irq
, &interrupt_handler
, IRQF_SHARED
,
1360 if (unlikely(ret
)) {
1361 dev_err(&pdev
->dev
, "can't request irq %d\n", pdev
->irq
);
1362 goto err_free_all_kfifo
;
1365 DBG1("base_addr: %p", dc
->base_addr
);
1367 make_sysfs_files(dc
);
1369 dc
->index_start
= ndev_idx
* MAX_PORT
;
1370 ndevs
[ndev_idx
] = dc
;
1372 pci_set_drvdata(pdev
, dc
);
1374 /* Enable RESET interrupt */
1375 dc
->last_ier
= RESET
;
1376 iowrite16(dc
->last_ier
, dc
->reg_ier
);
1378 dc
->state
= NOZOMI_STATE_ENABLED
;
1380 for (i
= 0; i
< MAX_PORT
; i
++) {
1381 struct device
*tty_dev
;
1382 struct port
*port
= &dc
->port
[i
];
1384 tty_port_init(&port
->port
);
1385 port
->port
.ops
= &noz_tty_port_ops
;
1386 tty_dev
= tty_port_register_device(&port
->port
, ntty_driver
,
1387 dc
->index_start
+ i
, &pdev
->dev
);
1389 if (IS_ERR(tty_dev
)) {
1390 ret
= PTR_ERR(tty_dev
);
1391 dev_err(&pdev
->dev
, "Could not allocate tty?\n");
1392 tty_port_destroy(&port
->port
);
1400 for (i
--; i
>= 0; i
--) {
1401 tty_unregister_device(ntty_driver
, dc
->index_start
+ i
);
1402 tty_port_destroy(&dc
->port
[i
].port
);
1404 free_irq(pdev
->irq
, dc
);
1408 for (i
--; i
>= PORT_MDM
; i
--)
1409 kfifo_free(&dc
->port
[i
].fifo_ul
);
1411 kfree(dc
->send_buf
);
1412 iounmap(dc
->base_addr
);
1414 pci_release_regions(pdev
);
1416 pci_disable_device(pdev
);
1423 static void tty_exit(struct nozomi
*dc
)
1427 for (i
= 0; i
< MAX_PORT
; ++i
)
1428 tty_port_tty_hangup(&dc
->port
[i
].port
, false);
1430 /* Racy below - surely should wait for scheduled work to be done or
1431 complete off a hangup method ? */
1432 while (dc
->open_ttys
)
1434 for (i
= 0; i
< MAX_PORT
; ++i
) {
1435 tty_unregister_device(ntty_driver
, dc
->index_start
+ i
);
1436 tty_port_destroy(&dc
->port
[i
].port
);
1440 /* Deallocate memory for one device */
1441 static void nozomi_card_exit(struct pci_dev
*pdev
)
1444 struct ctrl_ul ctrl
;
1445 struct nozomi
*dc
= pci_get_drvdata(pdev
);
1447 /* Disable all interrupts */
1449 writew(dc
->last_ier
, dc
->reg_ier
);
1453 /* Send 0x0001, command card to resend the reset token. */
1454 /* This is to get the reset when the module is reloaded. */
1459 DBG1("sending flow control 0x%04X", *((u16
*)&ctrl
));
1461 /* Setup dc->reg addresses to we can use defines here */
1462 write_mem32(dc
->port
[PORT_CTRL
].ul_addr
[0], (u32
*)&ctrl
, 2);
1463 writew(CTRL_UL
, dc
->reg_fcr
); /* push the token to the card. */
1465 remove_sysfs_files(dc
);
1467 free_irq(pdev
->irq
, dc
);
1469 for (i
= 0; i
< MAX_PORT
; i
++)
1470 kfifo_free(&dc
->port
[i
].fifo_ul
);
1472 kfree(dc
->send_buf
);
1474 iounmap(dc
->base_addr
);
1476 pci_release_regions(pdev
);
1478 pci_disable_device(pdev
);
1480 ndevs
[dc
->index_start
/ MAX_PORT
] = NULL
;
1485 static void set_rts(const struct tty_struct
*tty
, int rts
)
1487 struct port
*port
= get_port_by_tty(tty
);
1489 port
->ctrl_ul
.RTS
= rts
;
1490 port
->update_flow_control
= 1;
1491 enable_transmit_ul(PORT_CTRL
, get_dc_by_tty(tty
));
1494 static void set_dtr(const struct tty_struct
*tty
, int dtr
)
1496 struct port
*port
= get_port_by_tty(tty
);
1498 DBG1("SETTING DTR index: %d, dtr: %d", tty
->index
, dtr
);
1500 port
->ctrl_ul
.DTR
= dtr
;
1501 port
->update_flow_control
= 1;
1502 enable_transmit_ul(PORT_CTRL
, get_dc_by_tty(tty
));
1506 * ----------------------------------------------------------------------------
1508 * ----------------------------------------------------------------------------
1511 static int ntty_install(struct tty_driver
*driver
, struct tty_struct
*tty
)
1513 struct port
*port
= get_port_by_tty(tty
);
1514 struct nozomi
*dc
= get_dc_by_tty(tty
);
1516 if (!port
|| !dc
|| dc
->state
!= NOZOMI_STATE_READY
)
1518 ret
= tty_standard_install(driver
, tty
);
1520 tty
->driver_data
= port
;
1524 static void ntty_cleanup(struct tty_struct
*tty
)
1526 tty
->driver_data
= NULL
;
1529 static int ntty_activate(struct tty_port
*tport
, struct tty_struct
*tty
)
1531 struct port
*port
= container_of(tport
, struct port
, port
);
1532 struct nozomi
*dc
= port
->dc
;
1533 unsigned long flags
;
1535 DBG1("open: %d", port
->token_dl
);
1536 spin_lock_irqsave(&dc
->spin_mutex
, flags
);
1537 dc
->last_ier
= dc
->last_ier
| port
->token_dl
;
1538 writew(dc
->last_ier
, dc
->reg_ier
);
1540 spin_unlock_irqrestore(&dc
->spin_mutex
, flags
);
1541 printk("noz: activated %d: %p\n", tty
->index
, tport
);
1545 static int ntty_open(struct tty_struct
*tty
, struct file
*filp
)
1547 struct port
*port
= tty
->driver_data
;
1548 return tty_port_open(&port
->port
, tty
, filp
);
1551 static void ntty_shutdown(struct tty_port
*tport
)
1553 struct port
*port
= container_of(tport
, struct port
, port
);
1554 struct nozomi
*dc
= port
->dc
;
1555 unsigned long flags
;
1557 DBG1("close: %d", port
->token_dl
);
1558 spin_lock_irqsave(&dc
->spin_mutex
, flags
);
1559 dc
->last_ier
&= ~(port
->token_dl
);
1560 writew(dc
->last_ier
, dc
->reg_ier
);
1562 spin_unlock_irqrestore(&dc
->spin_mutex
, flags
);
1563 printk("noz: shutdown %p\n", tport
);
1566 static void ntty_close(struct tty_struct
*tty
, struct file
*filp
)
1568 struct port
*port
= tty
->driver_data
;
1570 tty_port_close(&port
->port
, tty
, filp
);
1573 static void ntty_hangup(struct tty_struct
*tty
)
1575 struct port
*port
= tty
->driver_data
;
1576 tty_port_hangup(&port
->port
);
1580 * called when the userspace process writes to the tty (/dev/noz*).
1581 * Data is inserted into a fifo, which is then read and transferred to the modem.
1583 static ssize_t
ntty_write(struct tty_struct
*tty
, const u8
*buffer
,
1586 struct nozomi
*dc
= get_dc_by_tty(tty
);
1587 struct port
*port
= tty
->driver_data
;
1588 unsigned long flags
;
1594 rval
= kfifo_in(&port
->fifo_ul
, buffer
, count
);
1596 spin_lock_irqsave(&dc
->spin_mutex
, flags
);
1597 /* CTS is only valid on the modem channel */
1598 if (port
== &(dc
->port
[PORT_MDM
])) {
1599 if (port
->ctrl_dl
.CTS
) {
1600 DBG4("Enable interrupt");
1601 enable_transmit_ul(tty
->index
% MAX_PORT
, dc
);
1603 dev_err(&dc
->pdev
->dev
,
1604 "CTS not active on modem port?\n");
1607 enable_transmit_ul(tty
->index
% MAX_PORT
, dc
);
1609 spin_unlock_irqrestore(&dc
->spin_mutex
, flags
);
1615 * Calculate how much is left in device
1616 * This method is called by the upper tty layer.
1617 * #according to sources N_TTY.c it expects a value >= 0 and
1618 * does not check for negative values.
1620 * If the port is unplugged report lots of room and let the bits
1621 * dribble away so we don't block anything.
1623 static unsigned int ntty_write_room(struct tty_struct
*tty
)
1625 struct port
*port
= tty
->driver_data
;
1626 unsigned int room
= 4096;
1627 const struct nozomi
*dc
= get_dc_by_tty(tty
);
1630 room
= kfifo_avail(&port
->fifo_ul
);
1635 /* Gets io control parameters */
1636 static int ntty_tiocmget(struct tty_struct
*tty
)
1638 const struct port
*port
= tty
->driver_data
;
1639 const struct ctrl_dl
*ctrl_dl
= &port
->ctrl_dl
;
1640 const struct ctrl_ul
*ctrl_ul
= &port
->ctrl_ul
;
1642 /* Note: these could change under us but it is not clear this
1644 return (ctrl_ul
->RTS
? TIOCM_RTS
: 0)
1645 | (ctrl_ul
->DTR
? TIOCM_DTR
: 0)
1646 | (ctrl_dl
->DCD
? TIOCM_CAR
: 0)
1647 | (ctrl_dl
->RI
? TIOCM_RNG
: 0)
1648 | (ctrl_dl
->DSR
? TIOCM_DSR
: 0)
1649 | (ctrl_dl
->CTS
? TIOCM_CTS
: 0);
1652 /* Sets io controls parameters */
1653 static int ntty_tiocmset(struct tty_struct
*tty
,
1654 unsigned int set
, unsigned int clear
)
1656 struct nozomi
*dc
= get_dc_by_tty(tty
);
1657 unsigned long flags
;
1659 spin_lock_irqsave(&dc
->spin_mutex
, flags
);
1660 if (set
& TIOCM_RTS
)
1662 else if (clear
& TIOCM_RTS
)
1665 if (set
& TIOCM_DTR
)
1667 else if (clear
& TIOCM_DTR
)
1669 spin_unlock_irqrestore(&dc
->spin_mutex
, flags
);
1674 static int ntty_cflags_changed(struct port
*port
, unsigned long flags
,
1675 struct async_icount
*cprev
)
1677 const struct async_icount cnow
= port
->tty_icount
;
1680 ret
= ((flags
& TIOCM_RNG
) && (cnow
.rng
!= cprev
->rng
))
1681 || ((flags
& TIOCM_DSR
) && (cnow
.dsr
!= cprev
->dsr
))
1682 || ((flags
& TIOCM_CD
) && (cnow
.dcd
!= cprev
->dcd
))
1683 || ((flags
& TIOCM_CTS
) && (cnow
.cts
!= cprev
->cts
));
1690 static int ntty_tiocgicount(struct tty_struct
*tty
,
1691 struct serial_icounter_struct
*icount
)
1693 struct port
*port
= tty
->driver_data
;
1694 const struct async_icount cnow
= port
->tty_icount
;
1696 icount
->cts
= cnow
.cts
;
1697 icount
->dsr
= cnow
.dsr
;
1698 icount
->rng
= cnow
.rng
;
1699 icount
->dcd
= cnow
.dcd
;
1700 icount
->rx
= cnow
.rx
;
1701 icount
->tx
= cnow
.tx
;
1702 icount
->frame
= cnow
.frame
;
1703 icount
->overrun
= cnow
.overrun
;
1704 icount
->parity
= cnow
.parity
;
1705 icount
->brk
= cnow
.brk
;
1706 icount
->buf_overrun
= cnow
.buf_overrun
;
1710 static int ntty_ioctl(struct tty_struct
*tty
,
1711 unsigned int cmd
, unsigned long arg
)
1713 struct port
*port
= tty
->driver_data
;
1714 int rval
= -ENOIOCTLCMD
;
1718 struct async_icount cprev
= port
->tty_icount
;
1720 rval
= wait_event_interruptible(port
->tty_wait
,
1721 ntty_cflags_changed(port
, arg
, &cprev
));
1725 DBG1("ERR: 0x%08X, %d", cmd
, cmd
);
1733 * Called by the upper tty layer when tty buffers are ready
1734 * to receive data again after a call to throttle.
1736 static void ntty_unthrottle(struct tty_struct
*tty
)
1738 struct nozomi
*dc
= get_dc_by_tty(tty
);
1739 unsigned long flags
;
1741 spin_lock_irqsave(&dc
->spin_mutex
, flags
);
1742 enable_transmit_dl(tty
->index
% MAX_PORT
, dc
);
1745 spin_unlock_irqrestore(&dc
->spin_mutex
, flags
);
1749 * Called by the upper tty layer when the tty buffers are almost full.
1750 * The driver should stop send more data.
1752 static void ntty_throttle(struct tty_struct
*tty
)
1754 struct nozomi
*dc
= get_dc_by_tty(tty
);
1755 unsigned long flags
;
1757 spin_lock_irqsave(&dc
->spin_mutex
, flags
);
1759 spin_unlock_irqrestore(&dc
->spin_mutex
, flags
);
1762 /* Returns number of chars in buffer, called by tty layer */
1763 static unsigned int ntty_chars_in_buffer(struct tty_struct
*tty
)
1765 struct port
*port
= tty
->driver_data
;
1766 struct nozomi
*dc
= get_dc_by_tty(tty
);
1768 if (unlikely(!dc
|| !port
))
1771 return kfifo_len(&port
->fifo_ul
);
1774 static const struct tty_port_operations noz_tty_port_ops
= {
1775 .activate
= ntty_activate
,
1776 .shutdown
= ntty_shutdown
,
1779 static const struct tty_operations tty_ops
= {
1780 .ioctl
= ntty_ioctl
,
1782 .close
= ntty_close
,
1783 .hangup
= ntty_hangup
,
1784 .write
= ntty_write
,
1785 .write_room
= ntty_write_room
,
1786 .unthrottle
= ntty_unthrottle
,
1787 .throttle
= ntty_throttle
,
1788 .chars_in_buffer
= ntty_chars_in_buffer
,
1789 .tiocmget
= ntty_tiocmget
,
1790 .tiocmset
= ntty_tiocmset
,
1791 .get_icount
= ntty_tiocgicount
,
1792 .install
= ntty_install
,
1793 .cleanup
= ntty_cleanup
,
1796 /* Module initialization */
1797 static struct pci_driver nozomi_driver
= {
1798 .name
= NOZOMI_NAME
,
1799 .id_table
= nozomi_pci_tbl
,
1800 .probe
= nozomi_card_init
,
1801 .remove
= nozomi_card_exit
,
1804 static __init
int nozomi_init(void)
1808 ntty_driver
= tty_alloc_driver(NTTY_TTY_MAXMINORS
, TTY_DRIVER_REAL_RAW
|
1809 TTY_DRIVER_DYNAMIC_DEV
);
1810 if (IS_ERR(ntty_driver
))
1811 return PTR_ERR(ntty_driver
);
1813 ntty_driver
->driver_name
= NOZOMI_NAME_TTY
;
1814 ntty_driver
->name
= "noz";
1815 ntty_driver
->major
= 0;
1816 ntty_driver
->type
= TTY_DRIVER_TYPE_SERIAL
;
1817 ntty_driver
->subtype
= SERIAL_TYPE_NORMAL
;
1818 ntty_driver
->init_termios
= tty_std_termios
;
1819 ntty_driver
->init_termios
.c_cflag
= B115200
| CS8
| CREAD
| \
1821 ntty_driver
->init_termios
.c_ispeed
= 115200;
1822 ntty_driver
->init_termios
.c_ospeed
= 115200;
1823 tty_set_operations(ntty_driver
, &tty_ops
);
1825 ret
= tty_register_driver(ntty_driver
);
1827 printk(KERN_ERR
"Nozomi: failed to register ntty driver\n");
1831 ret
= pci_register_driver(&nozomi_driver
);
1833 printk(KERN_ERR
"Nozomi: can't register pci driver\n");
1839 tty_unregister_driver(ntty_driver
);
1841 tty_driver_kref_put(ntty_driver
);
1845 static __exit
void nozomi_exit(void)
1847 pci_unregister_driver(&nozomi_driver
);
1848 tty_unregister_driver(ntty_driver
);
1849 tty_driver_kref_put(ntty_driver
);
1852 module_init(nozomi_init
);
1853 module_exit(nozomi_exit
);
1855 MODULE_LICENSE("Dual BSD/GPL");
1856 MODULE_DESCRIPTION("Nozomi driver");