44 #include <linux/kernel.h>
45 #include <linux/module.h>
46 #include <linux/pci.h>
48 #include <linux/tty.h>
51 #include <linux/sched.h>
52 #include <linux/serial.h>
58 #include <linux/slab.h>
59 #include <asm/byteorder.h>
64 #define VERSION_STRING DRIVER_DESC " 2.1d"
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)
93 #define D(lvl, args...) do \
94 {if (lvl & debug) NFO(KERN_DEBUG, ##args); } \
96 #define D_(lvl, args...) D(lvl, ##args)
102 #define D_(lvl, args...)
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
132 #define FIFO_BUFFER_SIZE_UL 8192
135 #define SEND_BUF_MAX 1024
136 #define RECEIVE_BUF_MAX 4
143 #define CONFIG_MAGIC 0xEFEFFEFE
144 #define TOGGLE_VALID 0x0000
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)
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)
423 static inline struct port *get_port_by_tty(
const struct tty_struct *tty)
435 static void read_mem32(
u32 *
buf,
const void __iomem *mem_addr_start,
446 switch (size_bytes) {
458 while (i < size_bytes) {
459 if (size_bytes - i == 2) {
481 static u32 write_mem32(
void __iomem *mem_addr_start,
const u32 *buf,
492 switch (size_bytes) {
494 buf16 = (
const u16 *)buf;
508 while (i < size_bytes) {
509 if (size_bytes - i == 2) {
511 buf16 = (
const u16 *)buf;
532 const u16 buff_offset = 4;
609 static void dump_table(
const struct nozomi *
dc)
613 DBG3(
"product_information: 0x%04X", \
651 static inline void dump_table(
const struct nozomi *
dc) { }
658 static int nozomi_read_config_table(
struct nozomi *
dc)
664 dev_err(&dc->
pdev->dev,
"ConfigTable Bad! 0x%08X != 0x%08X\n",
672 DBG1(
"Second phase, configuring card");
679 DBG1(
"toggle ports: MDM UL:%d MDM DL:%d, DIAG DL:%d",
702 DBG1(
"First phase: pushing upload buffers, clearing download");
723 DBG1(
"First phase done");
738 dev_err(&dc->
pdev->dev,
"Called with wrong port?\n");
743 static void disable_transmit_ul(
enum port_type port,
struct nozomi *dc)
745 static const u16 mask[] =
752 dev_err(&dc->
pdev->dev,
"Called with wrong port?\n");
765 dev_err(&dc->
pdev->dev,
"Called with wrong port?\n");
770 static void disable_transmit_dl(
enum port_type port,
struct nozomi *dc)
772 static const u16 mask[] =
779 dev_err(&dc->
pdev->dev,
"Called with wrong port?\n");
801 DBG4(
"No more data to send, disable link:");
809 write_mem32(addr, (
u32 *) &size, 4);
831 DBG1(
"tty not open for port: %d?", index);
835 read_mem32((
u32 *) &size, addr, 4);
839 DBG1(
"No room in tty, don't read data, don't ack interrupt, "
840 "disable interrupt");
843 disable_transmit_dl(index, dc);
858 tty_insert_flip_char(tty, buf[0],
TTY_NORMAL);
861 size -= tty_insert_flip_string(tty, (
char *) buf, size);
863 i = tty_insert_flip_string(tty, \
927 static int receive_flow_control(
struct nozomi *dc)
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).");
968 "ERROR: flow control received for non-existing port\n");
972 DBG1(
"0x%04X->0x%04X", *((
u16 *)&dc->
port[port].ctrl_dl),
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) {
986 DBG1(
"Enable interrupt (0x%04X) on port: %d",
988 DBG1(
"Data in buffer [%d], enable transmit! ",
990 enable_transmit_ul(port, dc);
992 DBG1(
"No data in buffer...");
997 DBG1(
" No change in mctrl");
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);
1034 "ERROR: send flow control " \
1035 "received for non-existing port\n");
1045 static int send_flow_control(
struct nozomi *dc)
1047 u32 i, more_flow_control_to_be_updated = 0;
1051 if (dc->
port[i].update_flow_control) {
1052 if (more_flow_control_to_be_updated) {
1056 dc->
port[
i].ctrl_ul.port = port2ctrl(i, dc);
1057 ctrl = (
u16 *)&dc->
port[i].ctrl_ul;
1060 dc->
port[
i].update_flow_control = 0;
1061 more_flow_control_to_be_updated = 1;
1075 if (*toggle == 0 && read_iir & mask1) {
1076 if (receive_data(port, dc)) {
1078 *toggle = !(*toggle);
1081 if (read_iir & mask2) {
1082 if (receive_data(port, dc)) {
1084 *toggle = !(*toggle);
1087 }
else if (*toggle == 1 && read_iir & mask2) {
1088 if (receive_data(port, dc)) {
1090 *toggle = !(*toggle);
1093 if (read_iir & mask1) {
1094 if (receive_data(port, dc)) {
1096 *toggle = !(*toggle);
1100 dev_err(&dc->
pdev->dev,
"port out of sync!, toggle:%d\n",
1116 if (*toggle == 0 && read_iir &
MDM_UL1) {
1119 if (send_data(port, dc)) {
1129 if (send_data(port, dc)) {
1137 }
else if (*toggle == 1 && read_iir & MDM_UL2) {
1140 if (send_data(port, dc)) {
1147 if (read_iir & MDM_UL1) {
1150 if (send_data(port, dc)) {
1178 if (read_iir == (
u16)-1)
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))) {
1197 dev_err(&dc->
pdev->dev,
"Could not read status from "
1198 "card, we should disable interface\n");
1209 if (send_flow_control(dc)) {
1216 receive_flow_control(dc);
1228 if (!handle_data_ul(dc,
PORT_MDM, read_iir)) {
1237 dev_err(&dc->
pdev->dev,
"DIAG_DL out of sync!\n");
1294 static void nozomi_get_card_type(
struct nozomi *dc)
1299 for (i = 0; i < 6; i++)
1308 static void nozomi_setup_private_data(
struct nozomi *dc)
1346 static void make_sysfs_files(
struct nozomi *dc)
1350 "Could not create sysfs file for card_type\n");
1353 "Could not create sysfs file for open_ttys\n");
1356 static void remove_sysfs_files(
struct nozomi *dc)
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])
1379 dev_err(&pdev->
dev,
"no free tty range for this card left\n");
1386 dev_err(&pdev->
dev,
"Could not allocate memory\n");
1395 dev_err(&pdev->
dev,
"Failed to enable PCI Device\n");
1401 dev_err(&pdev->
dev,
"I/O address 0x%04x already in use\n",
1403 goto err_disable_device;
1408 dev_err(&pdev->
dev,
"No I/O address for card detected\n");
1414 nozomi_get_card_type(dc);
1418 dev_err(&pdev->
dev,
"Unable to map card MMIO\n");
1425 dev_err(&pdev->
dev,
"Could not allocate send buffer?\n");
1434 "Could not allocate kfifo buffer\n");
1436 goto err_free_kfifo;
1442 nozomi_setup_private_data(dc);
1452 goto err_free_kfifo;
1457 make_sysfs_files(dc);
1460 ndevs[ndev_idx] =
dc;
1462 pci_set_drvdata(pdev, dc);
1472 struct port *port = &dc->
port[
i];
1475 port->
port.ops = &noz_tty_port_ops;
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)
1523 for (i = dc->
index_start; i < dc->index_start + MAX_PORT; ++i)
1532 struct nozomi *dc = pci_get_drvdata(pdev);
1546 DBG1(
"sending flow control 0x%04X", *((
u16 *)&ctrl));
1552 remove_sysfs_files(dc);
1574 struct port *port = get_port_by_tty(tty);
1578 enable_transmit_ul(
PORT_CTRL, get_dc_by_tty(tty));
1583 struct port *port = get_port_by_tty(tty);
1585 DBG1(
"SETTING DTR index: %d, dtr: %d", tty->
index, dtr);
1589 enable_transmit_ul(
PORT_CTRL, get_dc_by_tty(tty));
1600 struct port *port = get_port_by_tty(tty);
1601 struct nozomi *dc = get_dc_by_tty(tty);
1611 static void ntty_cleanup(
struct tty_struct *tty)
1618 struct port *port =
container_of(tport,
struct port, port);
1620 unsigned long flags;
1627 spin_unlock_irqrestore(&dc->
spin_mutex, flags);
1628 printk(
"noz: activated %d: %p\n", tty->
index, tport);
1638 static void ntty_shutdown(
struct tty_port *tport)
1640 struct port *port =
container_of(tport,
struct port, port);
1642 unsigned long flags;
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)
1660 static void ntty_hangup(
struct tty_struct *tty)
1674 struct nozomi *dc = get_dc_by_tty(tty);
1676 unsigned long flags;
1687 DBG1(
"No device context?");
1695 DBG4(
"Enable interrupt");
1696 enable_transmit_ul(tty->
index % MAX_PORT, dc);
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);
1719 static int ntty_write_room(
struct tty_struct *tty)
1723 const struct nozomi *dc = get_dc_by_tty(tty);
1732 static int ntty_tiocmget(
struct tty_struct *tty)
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;
1765 spin_unlock_irqrestore(&dc->
spin_mutex, flags);
1770 static int ntty_cflags_changed(
struct port *port,
unsigned long flags,
1786 static int ntty_tiocgicount(
struct tty_struct *tty,
1796 icount->
rx = cnow.
rx;
1797 icount->
tx = cnow.
tx;
1806 static int ntty_ioctl(
struct tty_struct *tty,
1807 unsigned int cmd,
unsigned long arg)
1812 DBG1(
"******** IOCTL, cmd: %d", cmd);
1819 ntty_cflags_changed(port, arg, &cprev));
1823 DBG1(
"ERR: 0x%08X, %d", cmd, cmd);
1834 static void ntty_unthrottle(
struct tty_struct *tty)
1836 struct nozomi *dc = get_dc_by_tty(tty);
1837 unsigned long flags;
1841 enable_transmit_dl(tty->
index % MAX_PORT, dc);
1844 spin_unlock_irqrestore(&dc->
spin_mutex, flags);
1851 static void ntty_throttle(
struct tty_struct *tty)
1853 struct nozomi *dc = get_dc_by_tty(tty);
1854 unsigned long flags;
1859 spin_unlock_irqrestore(&dc->
spin_mutex, flags);
1866 struct nozomi *dc = get_dc_by_tty(tty);
1870 goto exit_in_buffer;
1880 .activate = ntty_activate,
1881 .shutdown = ntty_shutdown,
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,
1904 .id_table = nozomi_pci_tbl,
1905 .probe = nozomi_card_init,
1909 static __init int nozomi_init(
void)
1920 ntty_driver->
name =
"noz";
1921 ntty_driver->
major = 0;
1938 ret = pci_register_driver(&nozomi_driver);
1952 static __exit void nozomi_exit(
void)