Merge branch 'x86-olpc-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[wandboard.git] / drivers / char / nozomi.c
blob294d03e8c61a6a249b6008878efd08386c86d0a9
1 /*
2 * nozomi.c -- HSDPA driver Broadband Wireless Data Card - Globe Trotter
4 * Written by: Ulf Jakobsson,
5 * Jan Ã…kerfeldt,
6 * Stefan Thomasson,
8 * Maintained by: Paul Hardwick (p.hardwick@option.com)
10 * Patches:
11 * Locking code changes for Vodafone by Sphere Systems Ltd,
12 * Andrew Bird (ajb@spheresystems.co.uk )
13 * & Phil Sanderson
15 * Source has been ported from an implementation made by Filip Aben @ Option
17 * --------------------------------------------------------------------------
19 * Copyright (c) 2005,2006 Option Wireless Sweden AB
20 * Copyright (c) 2006 Sphere Systems Ltd
21 * Copyright (c) 2006 Option Wireless n/v
22 * All rights Reserved.
24 * This program is free software; you can redistribute it and/or modify
25 * it under the terms of the GNU General Public License as published by
26 * the Free Software Foundation; either version 2 of the License, or
27 * (at your option) any later version.
29 * This program is distributed in the hope that it will be useful,
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 * GNU General Public License for more details.
34 * You should have received a copy of the GNU General Public License
35 * along with this program; if not, write to the Free Software
36 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
38 * --------------------------------------------------------------------------
41 /* Enable this to have a lot of debug printouts */
42 #define DEBUG
44 #include <linux/kernel.h>
45 #include <linux/module.h>
46 #include <linux/pci.h>
47 #include <linux/ioport.h>
48 #include <linux/tty.h>
49 #include <linux/tty_driver.h>
50 #include <linux/tty_flip.h>
51 #include <linux/sched.h>
52 #include <linux/serial.h>
53 #include <linux/interrupt.h>
54 #include <linux/kmod.h>
55 #include <linux/init.h>
56 #include <linux/kfifo.h>
57 #include <linux/uaccess.h>
58 #include <linux/slab.h>
59 #include <asm/byteorder.h>
61 #include <linux/delay.h>
64 #define VERSION_STRING DRIVER_DESC " 2.1d (build date: " \
65 __DATE__ " " __TIME__ ")"
67 /* Macros definitions */
69 /* Default debug printout level */
70 #define NOZOMI_DEBUG_LEVEL 0x00
72 #define P_BUF_SIZE 128
73 #define NFO(_err_flag_, args...) \
74 do { \
75 char tmp[P_BUF_SIZE]; \
76 snprintf(tmp, sizeof(tmp), ##args); \
77 printk(_err_flag_ "[%d] %s(): %s\n", __LINE__, \
78 __func__, tmp); \
79 } while (0)
81 #define DBG1(args...) D_(0x01, ##args)
82 #define DBG2(args...) D_(0x02, ##args)
83 #define DBG3(args...) D_(0x04, ##args)
84 #define DBG4(args...) D_(0x08, ##args)
85 #define DBG5(args...) D_(0x10, ##args)
86 #define DBG6(args...) D_(0x20, ##args)
87 #define DBG7(args...) D_(0x40, ##args)
88 #define DBG8(args...) D_(0x80, ##args)
90 #ifdef DEBUG
91 /* Do we need this settable at runtime? */
92 static int debug = NOZOMI_DEBUG_LEVEL;
94 #define D(lvl, args...) do \
95 {if (lvl & debug) NFO(KERN_DEBUG, ##args); } \
96 while (0)
97 #define D_(lvl, args...) D(lvl, ##args)
99 /* These printouts are always printed */
101 #else
102 static int debug;
103 #define D_(lvl, args...)
104 #endif
106 /* TODO: rewrite to optimize macros... */
108 #define TMP_BUF_MAX 256
110 #define DUMP(buf__,len__) \
111 do { \
112 char tbuf[TMP_BUF_MAX] = {0};\
113 if (len__ > 1) {\
114 snprintf(tbuf, len__ > TMP_BUF_MAX ? TMP_BUF_MAX : len__, "%s", buf__);\
115 if (tbuf[len__-2] == '\r') {\
116 tbuf[len__-2] = 'r';\
118 DBG1("SENDING: '%s' (%d+n)", tbuf, len__);\
119 } else {\
120 DBG1("SENDING: '%s' (%d)", tbuf, len__);\
122 } while (0)
124 /* Defines */
125 #define NOZOMI_NAME "nozomi"
126 #define NOZOMI_NAME_TTY "nozomi_tty"
127 #define DRIVER_DESC "Nozomi driver"
129 #define NTTY_TTY_MAXMINORS 256
130 #define NTTY_FIFO_BUFFER_SIZE 8192
132 /* Must be power of 2 */
133 #define FIFO_BUFFER_SIZE_UL 8192
135 /* Size of tmp send buffer to card */
136 #define SEND_BUF_MAX 1024
137 #define RECEIVE_BUF_MAX 4
140 #define R_IIR 0x0000 /* Interrupt Identity Register */
141 #define R_FCR 0x0000 /* Flow Control Register */
142 #define R_IER 0x0004 /* Interrupt Enable Register */
144 #define CONFIG_MAGIC 0xEFEFFEFE
145 #define TOGGLE_VALID 0x0000
147 /* Definition of interrupt tokens */
148 #define MDM_DL1 0x0001
149 #define MDM_UL1 0x0002
150 #define MDM_DL2 0x0004
151 #define MDM_UL2 0x0008
152 #define DIAG_DL1 0x0010
153 #define DIAG_DL2 0x0020
154 #define DIAG_UL 0x0040
155 #define APP1_DL 0x0080
156 #define APP1_UL 0x0100
157 #define APP2_DL 0x0200
158 #define APP2_UL 0x0400
159 #define CTRL_DL 0x0800
160 #define CTRL_UL 0x1000
161 #define RESET 0x8000
163 #define MDM_DL (MDM_DL1 | MDM_DL2)
164 #define MDM_UL (MDM_UL1 | MDM_UL2)
165 #define DIAG_DL (DIAG_DL1 | DIAG_DL2)
167 /* modem signal definition */
168 #define CTRL_DSR 0x0001
169 #define CTRL_DCD 0x0002
170 #define CTRL_RI 0x0004
171 #define CTRL_CTS 0x0008
173 #define CTRL_DTR 0x0001
174 #define CTRL_RTS 0x0002
176 #define MAX_PORT 4
177 #define NOZOMI_MAX_PORTS 5
178 #define NOZOMI_MAX_CARDS (NTTY_TTY_MAXMINORS / MAX_PORT)
180 /* Type definitions */
183 * There are two types of nozomi cards,
184 * one with 2048 memory and with 8192 memory
186 enum card_type {
187 F32_2 = 2048, /* 512 bytes downlink + uplink * 2 -> 2048 */
188 F32_8 = 8192, /* 3072 bytes downl. + 1024 bytes uplink * 2 -> 8192 */
191 /* Initialization states a card can be in */
192 enum card_state {
193 NOZOMI_STATE_UKNOWN = 0,
194 NOZOMI_STATE_ENABLED = 1, /* pci device enabled */
195 NOZOMI_STATE_ALLOCATED = 2, /* config setup done */
196 NOZOMI_STATE_READY = 3, /* flowcontrols received */
199 /* Two different toggle channels exist */
200 enum channel_type {
201 CH_A = 0,
202 CH_B = 1,
205 /* Port definition for the card regarding flow control */
206 enum ctrl_port_type {
207 CTRL_CMD = 0,
208 CTRL_MDM = 1,
209 CTRL_DIAG = 2,
210 CTRL_APP1 = 3,
211 CTRL_APP2 = 4,
212 CTRL_ERROR = -1,
215 /* Ports that the nozomi has */
216 enum port_type {
217 PORT_MDM = 0,
218 PORT_DIAG = 1,
219 PORT_APP1 = 2,
220 PORT_APP2 = 3,
221 PORT_CTRL = 4,
222 PORT_ERROR = -1,
225 #ifdef __BIG_ENDIAN
226 /* Big endian */
228 struct toggles {
229 unsigned int enabled:5; /*
230 * Toggle fields are valid if enabled is 0,
231 * else A-channels must always be used.
233 unsigned int diag_dl:1;
234 unsigned int mdm_dl:1;
235 unsigned int mdm_ul:1;
236 } __attribute__ ((packed));
238 /* Configuration table to read at startup of card */
239 /* Is for now only needed during initialization phase */
240 struct config_table {
241 u32 signature;
242 u16 product_information;
243 u16 version;
244 u8 pad3[3];
245 struct toggles toggle;
246 u8 pad1[4];
247 u16 dl_mdm_len1; /*
248 * If this is 64, it can hold
249 * 60 bytes + 4 that is length field
251 u16 dl_start;
253 u16 dl_diag_len1;
254 u16 dl_mdm_len2; /*
255 * If this is 64, it can hold
256 * 60 bytes + 4 that is length field
258 u16 dl_app1_len;
260 u16 dl_diag_len2;
261 u16 dl_ctrl_len;
262 u16 dl_app2_len;
263 u8 pad2[16];
264 u16 ul_mdm_len1;
265 u16 ul_start;
266 u16 ul_diag_len;
267 u16 ul_mdm_len2;
268 u16 ul_app1_len;
269 u16 ul_app2_len;
270 u16 ul_ctrl_len;
271 } __attribute__ ((packed));
273 /* This stores all control downlink flags */
274 struct ctrl_dl {
275 u8 port;
276 unsigned int reserved:4;
277 unsigned int CTS:1;
278 unsigned int RI:1;
279 unsigned int DCD:1;
280 unsigned int DSR:1;
281 } __attribute__ ((packed));
283 /* This stores all control uplink flags */
284 struct ctrl_ul {
285 u8 port;
286 unsigned int reserved:6;
287 unsigned int RTS:1;
288 unsigned int DTR:1;
289 } __attribute__ ((packed));
291 #else
292 /* Little endian */
294 /* This represents the toggle information */
295 struct toggles {
296 unsigned int mdm_ul:1;
297 unsigned int mdm_dl:1;
298 unsigned int diag_dl:1;
299 unsigned int enabled:5; /*
300 * Toggle fields are valid if enabled is 0,
301 * else A-channels must always be used.
303 } __attribute__ ((packed));
305 /* Configuration table to read at startup of card */
306 struct config_table {
307 u32 signature;
308 u16 version;
309 u16 product_information;
310 struct toggles toggle;
311 u8 pad1[7];
312 u16 dl_start;
313 u16 dl_mdm_len1; /*
314 * If this is 64, it can hold
315 * 60 bytes + 4 that is length field
317 u16 dl_mdm_len2;
318 u16 dl_diag_len1;
319 u16 dl_diag_len2;
320 u16 dl_app1_len;
321 u16 dl_app2_len;
322 u16 dl_ctrl_len;
323 u8 pad2[16];
324 u16 ul_start;
325 u16 ul_mdm_len2;
326 u16 ul_mdm_len1;
327 u16 ul_diag_len;
328 u16 ul_app1_len;
329 u16 ul_app2_len;
330 u16 ul_ctrl_len;
331 } __attribute__ ((packed));
333 /* This stores all control downlink flags */
334 struct ctrl_dl {
335 unsigned int DSR:1;
336 unsigned int DCD:1;
337 unsigned int RI:1;
338 unsigned int CTS:1;
339 unsigned int reserverd:4;
340 u8 port;
341 } __attribute__ ((packed));
343 /* This stores all control uplink flags */
344 struct ctrl_ul {
345 unsigned int DTR:1;
346 unsigned int RTS:1;
347 unsigned int reserved:6;
348 u8 port;
349 } __attribute__ ((packed));
350 #endif
352 /* This holds all information that is needed regarding a port */
353 struct port {
354 struct tty_port port;
355 u8 update_flow_control;
356 struct ctrl_ul ctrl_ul;
357 struct ctrl_dl ctrl_dl;
358 struct kfifo fifo_ul;
359 void __iomem *dl_addr[2];
360 u32 dl_size[2];
361 u8 toggle_dl;
362 void __iomem *ul_addr[2];
363 u32 ul_size[2];
364 u8 toggle_ul;
365 u16 token_dl;
367 /* mutex to ensure one access patch to this port */
368 struct mutex tty_sem;
369 wait_queue_head_t tty_wait;
370 struct async_icount tty_icount;
372 struct nozomi *dc;
375 /* Private data one for each card in the system */
376 struct nozomi {
377 void __iomem *base_addr;
378 unsigned long flip;
380 /* Pointers to registers */
381 void __iomem *reg_iir;
382 void __iomem *reg_fcr;
383 void __iomem *reg_ier;
385 u16 last_ier;
386 enum card_type card_type;
387 struct config_table config_table; /* Configuration table */
388 struct pci_dev *pdev;
389 struct port port[NOZOMI_MAX_PORTS];
390 u8 *send_buf;
392 spinlock_t spin_mutex; /* secures access to registers and tty */
394 unsigned int index_start;
395 enum card_state state;
396 u32 open_ttys;
399 /* This is a data packet that is read or written to/from card */
400 struct buffer {
401 u32 size; /* size is the length of the data buffer */
402 u8 *data;
403 } __attribute__ ((packed));
405 /* Global variables */
406 static const struct pci_device_id nozomi_pci_tbl[] __devinitconst = {
407 {PCI_DEVICE(0x1931, 0x000c)}, /* Nozomi HSDPA */
411 MODULE_DEVICE_TABLE(pci, nozomi_pci_tbl);
413 static struct nozomi *ndevs[NOZOMI_MAX_CARDS];
414 static struct tty_driver *ntty_driver;
416 static const struct tty_port_operations noz_tty_port_ops;
419 * find card by tty_index
421 static inline struct nozomi *get_dc_by_tty(const struct tty_struct *tty)
423 return tty ? ndevs[tty->index / MAX_PORT] : NULL;
426 static inline struct port *get_port_by_tty(const struct tty_struct *tty)
428 struct nozomi *ndev = get_dc_by_tty(tty);
429 return ndev ? &ndev->port[tty->index % MAX_PORT] : NULL;
433 * TODO:
434 * -Optimize
435 * -Rewrite cleaner
438 static void read_mem32(u32 *buf, const void __iomem *mem_addr_start,
439 u32 size_bytes)
441 u32 i = 0;
442 const u32 __iomem *ptr = mem_addr_start;
443 u16 *buf16;
445 if (unlikely(!ptr || !buf))
446 goto out;
448 /* shortcut for extremely often used cases */
449 switch (size_bytes) {
450 case 2: /* 2 bytes */
451 buf16 = (u16 *) buf;
452 *buf16 = __le16_to_cpu(readw(ptr));
453 goto out;
454 break;
455 case 4: /* 4 bytes */
456 *(buf) = __le32_to_cpu(readl(ptr));
457 goto out;
458 break;
461 while (i < size_bytes) {
462 if (size_bytes - i == 2) {
463 /* Handle 2 bytes in the end */
464 buf16 = (u16 *) buf;
465 *(buf16) = __le16_to_cpu(readw(ptr));
466 i += 2;
467 } else {
468 /* Read 4 bytes */
469 *(buf) = __le32_to_cpu(readl(ptr));
470 i += 4;
472 buf++;
473 ptr++;
475 out:
476 return;
480 * TODO:
481 * -Optimize
482 * -Rewrite cleaner
484 static u32 write_mem32(void __iomem *mem_addr_start, const u32 *buf,
485 u32 size_bytes)
487 u32 i = 0;
488 u32 __iomem *ptr = mem_addr_start;
489 const u16 *buf16;
491 if (unlikely(!ptr || !buf))
492 return 0;
494 /* shortcut for extremely often used cases */
495 switch (size_bytes) {
496 case 2: /* 2 bytes */
497 buf16 = (const u16 *)buf;
498 writew(__cpu_to_le16(*buf16), ptr);
499 return 2;
500 break;
501 case 1: /*
502 * also needs to write 4 bytes in this case
503 * so falling through..
505 case 4: /* 4 bytes */
506 writel(__cpu_to_le32(*buf), ptr);
507 return 4;
508 break;
511 while (i < size_bytes) {
512 if (size_bytes - i == 2) {
513 /* 2 bytes */
514 buf16 = (const u16 *)buf;
515 writew(__cpu_to_le16(*buf16), ptr);
516 i += 2;
517 } else {
518 /* 4 bytes */
519 writel(__cpu_to_le32(*buf), ptr);
520 i += 4;
522 buf++;
523 ptr++;
525 return i;
528 /* Setup pointers to different channels and also setup buffer sizes. */
529 static void setup_memory(struct nozomi *dc)
531 void __iomem *offset = dc->base_addr + dc->config_table.dl_start;
532 /* The length reported is including the length field of 4 bytes,
533 * hence subtract with 4.
535 const u16 buff_offset = 4;
537 /* Modem port dl configuration */
538 dc->port[PORT_MDM].dl_addr[CH_A] = offset;
539 dc->port[PORT_MDM].dl_addr[CH_B] =
540 (offset += dc->config_table.dl_mdm_len1);
541 dc->port[PORT_MDM].dl_size[CH_A] =
542 dc->config_table.dl_mdm_len1 - buff_offset;
543 dc->port[PORT_MDM].dl_size[CH_B] =
544 dc->config_table.dl_mdm_len2 - buff_offset;
546 /* Diag port dl configuration */
547 dc->port[PORT_DIAG].dl_addr[CH_A] =
548 (offset += dc->config_table.dl_mdm_len2);
549 dc->port[PORT_DIAG].dl_size[CH_A] =
550 dc->config_table.dl_diag_len1 - buff_offset;
551 dc->port[PORT_DIAG].dl_addr[CH_B] =
552 (offset += dc->config_table.dl_diag_len1);
553 dc->port[PORT_DIAG].dl_size[CH_B] =
554 dc->config_table.dl_diag_len2 - buff_offset;
556 /* App1 port dl configuration */
557 dc->port[PORT_APP1].dl_addr[CH_A] =
558 (offset += dc->config_table.dl_diag_len2);
559 dc->port[PORT_APP1].dl_size[CH_A] =
560 dc->config_table.dl_app1_len - buff_offset;
562 /* App2 port dl configuration */
563 dc->port[PORT_APP2].dl_addr[CH_A] =
564 (offset += dc->config_table.dl_app1_len);
565 dc->port[PORT_APP2].dl_size[CH_A] =
566 dc->config_table.dl_app2_len - buff_offset;
568 /* Ctrl dl configuration */
569 dc->port[PORT_CTRL].dl_addr[CH_A] =
570 (offset += dc->config_table.dl_app2_len);
571 dc->port[PORT_CTRL].dl_size[CH_A] =
572 dc->config_table.dl_ctrl_len - buff_offset;
574 offset = dc->base_addr + dc->config_table.ul_start;
576 /* Modem Port ul configuration */
577 dc->port[PORT_MDM].ul_addr[CH_A] = offset;
578 dc->port[PORT_MDM].ul_size[CH_A] =
579 dc->config_table.ul_mdm_len1 - buff_offset;
580 dc->port[PORT_MDM].ul_addr[CH_B] =
581 (offset += dc->config_table.ul_mdm_len1);
582 dc->port[PORT_MDM].ul_size[CH_B] =
583 dc->config_table.ul_mdm_len2 - buff_offset;
585 /* Diag port ul configuration */
586 dc->port[PORT_DIAG].ul_addr[CH_A] =
587 (offset += dc->config_table.ul_mdm_len2);
588 dc->port[PORT_DIAG].ul_size[CH_A] =
589 dc->config_table.ul_diag_len - buff_offset;
591 /* App1 port ul configuration */
592 dc->port[PORT_APP1].ul_addr[CH_A] =
593 (offset += dc->config_table.ul_diag_len);
594 dc->port[PORT_APP1].ul_size[CH_A] =
595 dc->config_table.ul_app1_len - buff_offset;
597 /* App2 port ul configuration */
598 dc->port[PORT_APP2].ul_addr[CH_A] =
599 (offset += dc->config_table.ul_app1_len);
600 dc->port[PORT_APP2].ul_size[CH_A] =
601 dc->config_table.ul_app2_len - buff_offset;
603 /* Ctrl ul configuration */
604 dc->port[PORT_CTRL].ul_addr[CH_A] =
605 (offset += dc->config_table.ul_app2_len);
606 dc->port[PORT_CTRL].ul_size[CH_A] =
607 dc->config_table.ul_ctrl_len - buff_offset;
610 /* Dump config table under initalization phase */
611 #ifdef DEBUG
612 static void dump_table(const struct nozomi *dc)
614 DBG3("signature: 0x%08X", dc->config_table.signature);
615 DBG3("version: 0x%04X", dc->config_table.version);
616 DBG3("product_information: 0x%04X", \
617 dc->config_table.product_information);
618 DBG3("toggle enabled: %d", dc->config_table.toggle.enabled);
619 DBG3("toggle up_mdm: %d", dc->config_table.toggle.mdm_ul);
620 DBG3("toggle dl_mdm: %d", dc->config_table.toggle.mdm_dl);
621 DBG3("toggle dl_dbg: %d", dc->config_table.toggle.diag_dl);
623 DBG3("dl_start: 0x%04X", dc->config_table.dl_start);
624 DBG3("dl_mdm_len0: 0x%04X, %d", dc->config_table.dl_mdm_len1,
625 dc->config_table.dl_mdm_len1);
626 DBG3("dl_mdm_len1: 0x%04X, %d", dc->config_table.dl_mdm_len2,
627 dc->config_table.dl_mdm_len2);
628 DBG3("dl_diag_len0: 0x%04X, %d", dc->config_table.dl_diag_len1,
629 dc->config_table.dl_diag_len1);
630 DBG3("dl_diag_len1: 0x%04X, %d", dc->config_table.dl_diag_len2,
631 dc->config_table.dl_diag_len2);
632 DBG3("dl_app1_len: 0x%04X, %d", dc->config_table.dl_app1_len,
633 dc->config_table.dl_app1_len);
634 DBG3("dl_app2_len: 0x%04X, %d", dc->config_table.dl_app2_len,
635 dc->config_table.dl_app2_len);
636 DBG3("dl_ctrl_len: 0x%04X, %d", dc->config_table.dl_ctrl_len,
637 dc->config_table.dl_ctrl_len);
638 DBG3("ul_start: 0x%04X, %d", dc->config_table.ul_start,
639 dc->config_table.ul_start);
640 DBG3("ul_mdm_len[0]: 0x%04X, %d", dc->config_table.ul_mdm_len1,
641 dc->config_table.ul_mdm_len1);
642 DBG3("ul_mdm_len[1]: 0x%04X, %d", dc->config_table.ul_mdm_len2,
643 dc->config_table.ul_mdm_len2);
644 DBG3("ul_diag_len: 0x%04X, %d", dc->config_table.ul_diag_len,
645 dc->config_table.ul_diag_len);
646 DBG3("ul_app1_len: 0x%04X, %d", dc->config_table.ul_app1_len,
647 dc->config_table.ul_app1_len);
648 DBG3("ul_app2_len: 0x%04X, %d", dc->config_table.ul_app2_len,
649 dc->config_table.ul_app2_len);
650 DBG3("ul_ctrl_len: 0x%04X, %d", dc->config_table.ul_ctrl_len,
651 dc->config_table.ul_ctrl_len);
653 #else
654 static inline void dump_table(const struct nozomi *dc) { }
655 #endif
658 * Read configuration table from card under intalization phase
659 * Returns 1 if ok, else 0
661 static int nozomi_read_config_table(struct nozomi *dc)
663 read_mem32((u32 *) &dc->config_table, dc->base_addr + 0,
664 sizeof(struct config_table));
666 if (dc->config_table.signature != CONFIG_MAGIC) {
667 dev_err(&dc->pdev->dev, "ConfigTable Bad! 0x%08X != 0x%08X\n",
668 dc->config_table.signature, CONFIG_MAGIC);
669 return 0;
672 if ((dc->config_table.version == 0)
673 || (dc->config_table.toggle.enabled == TOGGLE_VALID)) {
674 int i;
675 DBG1("Second phase, configuring card");
677 setup_memory(dc);
679 dc->port[PORT_MDM].toggle_ul = dc->config_table.toggle.mdm_ul;
680 dc->port[PORT_MDM].toggle_dl = dc->config_table.toggle.mdm_dl;
681 dc->port[PORT_DIAG].toggle_dl = dc->config_table.toggle.diag_dl;
682 DBG1("toggle ports: MDM UL:%d MDM DL:%d, DIAG DL:%d",
683 dc->port[PORT_MDM].toggle_ul,
684 dc->port[PORT_MDM].toggle_dl, dc->port[PORT_DIAG].toggle_dl);
686 dump_table(dc);
688 for (i = PORT_MDM; i < MAX_PORT; i++) {
689 memset(&dc->port[i].ctrl_dl, 0, sizeof(struct ctrl_dl));
690 memset(&dc->port[i].ctrl_ul, 0, sizeof(struct ctrl_ul));
693 /* Enable control channel */
694 dc->last_ier = dc->last_ier | CTRL_DL;
695 writew(dc->last_ier, dc->reg_ier);
697 dc->state = NOZOMI_STATE_ALLOCATED;
698 dev_info(&dc->pdev->dev, "Initialization OK!\n");
699 return 1;
702 if ((dc->config_table.version > 0)
703 && (dc->config_table.toggle.enabled != TOGGLE_VALID)) {
704 u32 offset = 0;
705 DBG1("First phase: pushing upload buffers, clearing download");
707 dev_info(&dc->pdev->dev, "Version of card: %d\n",
708 dc->config_table.version);
710 /* Here we should disable all I/O over F32. */
711 setup_memory(dc);
714 * We should send ALL channel pair tokens back along
715 * with reset token
718 /* push upload modem buffers */
719 write_mem32(dc->port[PORT_MDM].ul_addr[CH_A],
720 (u32 *) &offset, 4);
721 write_mem32(dc->port[PORT_MDM].ul_addr[CH_B],
722 (u32 *) &offset, 4);
724 writew(MDM_UL | DIAG_DL | MDM_DL, dc->reg_fcr);
726 DBG1("First phase done");
729 return 1;
732 /* Enable uplink interrupts */
733 static void enable_transmit_ul(enum port_type port, struct nozomi *dc)
735 static const u16 mask[] = {MDM_UL, DIAG_UL, APP1_UL, APP2_UL, CTRL_UL};
737 if (port < NOZOMI_MAX_PORTS) {
738 dc->last_ier |= mask[port];
739 writew(dc->last_ier, dc->reg_ier);
740 } else {
741 dev_err(&dc->pdev->dev, "Called with wrong port?\n");
745 /* Disable uplink interrupts */
746 static void disable_transmit_ul(enum port_type port, struct nozomi *dc)
748 static const u16 mask[] =
749 {~MDM_UL, ~DIAG_UL, ~APP1_UL, ~APP2_UL, ~CTRL_UL};
751 if (port < NOZOMI_MAX_PORTS) {
752 dc->last_ier &= mask[port];
753 writew(dc->last_ier, dc->reg_ier);
754 } else {
755 dev_err(&dc->pdev->dev, "Called with wrong port?\n");
759 /* Enable downlink interrupts */
760 static void enable_transmit_dl(enum port_type port, struct nozomi *dc)
762 static const u16 mask[] = {MDM_DL, DIAG_DL, APP1_DL, APP2_DL, CTRL_DL};
764 if (port < NOZOMI_MAX_PORTS) {
765 dc->last_ier |= mask[port];
766 writew(dc->last_ier, dc->reg_ier);
767 } else {
768 dev_err(&dc->pdev->dev, "Called with wrong port?\n");
772 /* Disable downlink interrupts */
773 static void disable_transmit_dl(enum port_type port, struct nozomi *dc)
775 static const u16 mask[] =
776 {~MDM_DL, ~DIAG_DL, ~APP1_DL, ~APP2_DL, ~CTRL_DL};
778 if (port < NOZOMI_MAX_PORTS) {
779 dc->last_ier &= mask[port];
780 writew(dc->last_ier, dc->reg_ier);
781 } else {
782 dev_err(&dc->pdev->dev, "Called with wrong port?\n");
787 * Return 1 - send buffer to card and ack.
788 * Return 0 - don't ack, don't send buffer to card.
790 static int send_data(enum port_type index, struct nozomi *dc)
792 u32 size = 0;
793 struct port *port = &dc->port[index];
794 const u8 toggle = port->toggle_ul;
795 void __iomem *addr = port->ul_addr[toggle];
796 const u32 ul_size = port->ul_size[toggle];
797 struct tty_struct *tty = tty_port_tty_get(&port->port);
799 /* Get data from tty and place in buf for now */
800 size = kfifo_out(&port->fifo_ul, dc->send_buf,
801 ul_size < SEND_BUF_MAX ? ul_size : SEND_BUF_MAX);
803 if (size == 0) {
804 DBG4("No more data to send, disable link:");
805 tty_kref_put(tty);
806 return 0;
809 /* DUMP(buf, size); */
811 /* Write length + data */
812 write_mem32(addr, (u32 *) &size, 4);
813 write_mem32(addr + 4, (u32 *) dc->send_buf, size);
815 if (tty)
816 tty_wakeup(tty);
818 tty_kref_put(tty);
819 return 1;
822 /* If all data has been read, return 1, else 0 */
823 static int receive_data(enum port_type index, struct nozomi *dc)
825 u8 buf[RECEIVE_BUF_MAX] = { 0 };
826 int size;
827 u32 offset = 4;
828 struct port *port = &dc->port[index];
829 void __iomem *addr = port->dl_addr[port->toggle_dl];
830 struct tty_struct *tty = tty_port_tty_get(&port->port);
831 int i, ret;
833 if (unlikely(!tty)) {
834 DBG1("tty not open for port: %d?", index);
835 return 1;
838 read_mem32((u32 *) &size, addr, 4);
839 /* DBG1( "%d bytes port: %d", size, index); */
841 if (test_bit(TTY_THROTTLED, &tty->flags)) {
842 DBG1("No room in tty, don't read data, don't ack interrupt, "
843 "disable interrupt");
845 /* disable interrupt in downlink... */
846 disable_transmit_dl(index, dc);
847 ret = 0;
848 goto put;
851 if (unlikely(size == 0)) {
852 dev_err(&dc->pdev->dev, "size == 0?\n");
853 ret = 1;
854 goto put;
857 while (size > 0) {
858 read_mem32((u32 *) buf, addr + offset, RECEIVE_BUF_MAX);
860 if (size == 1) {
861 tty_insert_flip_char(tty, buf[0], TTY_NORMAL);
862 size = 0;
863 } else if (size < RECEIVE_BUF_MAX) {
864 size -= tty_insert_flip_string(tty, (char *) buf, size);
865 } else {
866 i = tty_insert_flip_string(tty, \
867 (char *) buf, RECEIVE_BUF_MAX);
868 size -= i;
869 offset += i;
873 set_bit(index, &dc->flip);
874 ret = 1;
875 put:
876 tty_kref_put(tty);
877 return ret;
880 /* Debug for interrupts */
881 #ifdef DEBUG
882 static char *interrupt2str(u16 interrupt)
884 static char buf[TMP_BUF_MAX];
885 char *p = buf;
887 interrupt & MDM_DL1 ? p += snprintf(p, TMP_BUF_MAX, "MDM_DL1 ") : NULL;
888 interrupt & MDM_DL2 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
889 "MDM_DL2 ") : NULL;
891 interrupt & MDM_UL1 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
892 "MDM_UL1 ") : NULL;
893 interrupt & MDM_UL2 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
894 "MDM_UL2 ") : NULL;
896 interrupt & DIAG_DL1 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
897 "DIAG_DL1 ") : NULL;
898 interrupt & DIAG_DL2 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
899 "DIAG_DL2 ") : NULL;
901 interrupt & DIAG_UL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
902 "DIAG_UL ") : NULL;
904 interrupt & APP1_DL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
905 "APP1_DL ") : NULL;
906 interrupt & APP2_DL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
907 "APP2_DL ") : NULL;
909 interrupt & APP1_UL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
910 "APP1_UL ") : NULL;
911 interrupt & APP2_UL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
912 "APP2_UL ") : NULL;
914 interrupt & CTRL_DL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
915 "CTRL_DL ") : NULL;
916 interrupt & CTRL_UL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
917 "CTRL_UL ") : NULL;
919 interrupt & RESET ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
920 "RESET ") : NULL;
922 return buf;
924 #endif
927 * Receive flow control
928 * Return 1 - If ok, else 0
930 static int receive_flow_control(struct nozomi *dc)
932 enum port_type port = PORT_MDM;
933 struct ctrl_dl ctrl_dl;
934 struct ctrl_dl old_ctrl;
935 u16 enable_ier = 0;
937 read_mem32((u32 *) &ctrl_dl, dc->port[PORT_CTRL].dl_addr[CH_A], 2);
939 switch (ctrl_dl.port) {
940 case CTRL_CMD:
941 DBG1("The Base Band sends this value as a response to a "
942 "request for IMSI detach sent over the control "
943 "channel uplink (see section 7.6.1).");
944 break;
945 case CTRL_MDM:
946 port = PORT_MDM;
947 enable_ier = MDM_DL;
948 break;
949 case CTRL_DIAG:
950 port = PORT_DIAG;
951 enable_ier = DIAG_DL;
952 break;
953 case CTRL_APP1:
954 port = PORT_APP1;
955 enable_ier = APP1_DL;
956 break;
957 case CTRL_APP2:
958 port = PORT_APP2;
959 enable_ier = APP2_DL;
960 if (dc->state == NOZOMI_STATE_ALLOCATED) {
962 * After card initialization the flow control
963 * received for APP2 is always the last
965 dc->state = NOZOMI_STATE_READY;
966 dev_info(&dc->pdev->dev, "Device READY!\n");
968 break;
969 default:
970 dev_err(&dc->pdev->dev,
971 "ERROR: flow control received for non-existing port\n");
972 return 0;
975 DBG1("0x%04X->0x%04X", *((u16 *)&dc->port[port].ctrl_dl),
976 *((u16 *)&ctrl_dl));
978 old_ctrl = dc->port[port].ctrl_dl;
979 dc->port[port].ctrl_dl = ctrl_dl;
981 if (old_ctrl.CTS == 1 && ctrl_dl.CTS == 0) {
982 DBG1("Disable interrupt (0x%04X) on port: %d",
983 enable_ier, port);
984 disable_transmit_ul(port, dc);
986 } else if (old_ctrl.CTS == 0 && ctrl_dl.CTS == 1) {
988 if (kfifo_len(&dc->port[port].fifo_ul)) {
989 DBG1("Enable interrupt (0x%04X) on port: %d",
990 enable_ier, port);
991 DBG1("Data in buffer [%d], enable transmit! ",
992 kfifo_len(&dc->port[port].fifo_ul));
993 enable_transmit_ul(port, dc);
994 } else {
995 DBG1("No data in buffer...");
999 if (*(u16 *)&old_ctrl == *(u16 *)&ctrl_dl) {
1000 DBG1(" No change in mctrl");
1001 return 1;
1003 /* Update statistics */
1004 if (old_ctrl.CTS != ctrl_dl.CTS)
1005 dc->port[port].tty_icount.cts++;
1006 if (old_ctrl.DSR != ctrl_dl.DSR)
1007 dc->port[port].tty_icount.dsr++;
1008 if (old_ctrl.RI != ctrl_dl.RI)
1009 dc->port[port].tty_icount.rng++;
1010 if (old_ctrl.DCD != ctrl_dl.DCD)
1011 dc->port[port].tty_icount.dcd++;
1013 wake_up_interruptible(&dc->port[port].tty_wait);
1015 DBG1("port: %d DCD(%d), CTS(%d), RI(%d), DSR(%d)",
1016 port,
1017 dc->port[port].tty_icount.dcd, dc->port[port].tty_icount.cts,
1018 dc->port[port].tty_icount.rng, dc->port[port].tty_icount.dsr);
1020 return 1;
1023 static enum ctrl_port_type port2ctrl(enum port_type port,
1024 const struct nozomi *dc)
1026 switch (port) {
1027 case PORT_MDM:
1028 return CTRL_MDM;
1029 case PORT_DIAG:
1030 return CTRL_DIAG;
1031 case PORT_APP1:
1032 return CTRL_APP1;
1033 case PORT_APP2:
1034 return CTRL_APP2;
1035 default:
1036 dev_err(&dc->pdev->dev,
1037 "ERROR: send flow control " \
1038 "received for non-existing port\n");
1040 return CTRL_ERROR;
1044 * Send flow control, can only update one channel at a time
1045 * Return 0 - If we have updated all flow control
1046 * Return 1 - If we need to update more flow control, ack current enable more
1048 static int send_flow_control(struct nozomi *dc)
1050 u32 i, more_flow_control_to_be_updated = 0;
1051 u16 *ctrl;
1053 for (i = PORT_MDM; i < MAX_PORT; i++) {
1054 if (dc->port[i].update_flow_control) {
1055 if (more_flow_control_to_be_updated) {
1056 /* We have more flow control to be updated */
1057 return 1;
1059 dc->port[i].ctrl_ul.port = port2ctrl(i, dc);
1060 ctrl = (u16 *)&dc->port[i].ctrl_ul;
1061 write_mem32(dc->port[PORT_CTRL].ul_addr[0], \
1062 (u32 *) ctrl, 2);
1063 dc->port[i].update_flow_control = 0;
1064 more_flow_control_to_be_updated = 1;
1067 return 0;
1071 * Handle downlink data, ports that are handled are modem and diagnostics
1072 * Return 1 - ok
1073 * Return 0 - toggle fields are out of sync
1075 static int handle_data_dl(struct nozomi *dc, enum port_type port, u8 *toggle,
1076 u16 read_iir, u16 mask1, u16 mask2)
1078 if (*toggle == 0 && read_iir & mask1) {
1079 if (receive_data(port, dc)) {
1080 writew(mask1, dc->reg_fcr);
1081 *toggle = !(*toggle);
1084 if (read_iir & mask2) {
1085 if (receive_data(port, dc)) {
1086 writew(mask2, dc->reg_fcr);
1087 *toggle = !(*toggle);
1090 } else if (*toggle == 1 && read_iir & mask2) {
1091 if (receive_data(port, dc)) {
1092 writew(mask2, dc->reg_fcr);
1093 *toggle = !(*toggle);
1096 if (read_iir & mask1) {
1097 if (receive_data(port, dc)) {
1098 writew(mask1, dc->reg_fcr);
1099 *toggle = !(*toggle);
1102 } else {
1103 dev_err(&dc->pdev->dev, "port out of sync!, toggle:%d\n",
1104 *toggle);
1105 return 0;
1107 return 1;
1111 * Handle uplink data, this is currently for the modem port
1112 * Return 1 - ok
1113 * Return 0 - toggle field are out of sync
1115 static int handle_data_ul(struct nozomi *dc, enum port_type port, u16 read_iir)
1117 u8 *toggle = &(dc->port[port].toggle_ul);
1119 if (*toggle == 0 && read_iir & MDM_UL1) {
1120 dc->last_ier &= ~MDM_UL;
1121 writew(dc->last_ier, dc->reg_ier);
1122 if (send_data(port, dc)) {
1123 writew(MDM_UL1, dc->reg_fcr);
1124 dc->last_ier = dc->last_ier | MDM_UL;
1125 writew(dc->last_ier, dc->reg_ier);
1126 *toggle = !*toggle;
1129 if (read_iir & MDM_UL2) {
1130 dc->last_ier &= ~MDM_UL;
1131 writew(dc->last_ier, dc->reg_ier);
1132 if (send_data(port, dc)) {
1133 writew(MDM_UL2, dc->reg_fcr);
1134 dc->last_ier = dc->last_ier | MDM_UL;
1135 writew(dc->last_ier, dc->reg_ier);
1136 *toggle = !*toggle;
1140 } else if (*toggle == 1 && read_iir & MDM_UL2) {
1141 dc->last_ier &= ~MDM_UL;
1142 writew(dc->last_ier, dc->reg_ier);
1143 if (send_data(port, dc)) {
1144 writew(MDM_UL2, dc->reg_fcr);
1145 dc->last_ier = dc->last_ier | MDM_UL;
1146 writew(dc->last_ier, dc->reg_ier);
1147 *toggle = !*toggle;
1150 if (read_iir & MDM_UL1) {
1151 dc->last_ier &= ~MDM_UL;
1152 writew(dc->last_ier, dc->reg_ier);
1153 if (send_data(port, dc)) {
1154 writew(MDM_UL1, dc->reg_fcr);
1155 dc->last_ier = dc->last_ier | MDM_UL;
1156 writew(dc->last_ier, dc->reg_ier);
1157 *toggle = !*toggle;
1160 } else {
1161 writew(read_iir & MDM_UL, dc->reg_fcr);
1162 dev_err(&dc->pdev->dev, "port out of sync!\n");
1163 return 0;
1165 return 1;
1168 static irqreturn_t interrupt_handler(int irq, void *dev_id)
1170 struct nozomi *dc = dev_id;
1171 unsigned int a;
1172 u16 read_iir;
1174 if (!dc)
1175 return IRQ_NONE;
1177 spin_lock(&dc->spin_mutex);
1178 read_iir = readw(dc->reg_iir);
1180 /* Card removed */
1181 if (read_iir == (u16)-1)
1182 goto none;
1184 * Just handle interrupt enabled in IER
1185 * (by masking with dc->last_ier)
1187 read_iir &= dc->last_ier;
1189 if (read_iir == 0)
1190 goto none;
1193 DBG4("%s irq:0x%04X, prev:0x%04X", interrupt2str(read_iir), read_iir,
1194 dc->last_ier);
1196 if (read_iir & RESET) {
1197 if (unlikely(!nozomi_read_config_table(dc))) {
1198 dc->last_ier = 0x0;
1199 writew(dc->last_ier, dc->reg_ier);
1200 dev_err(&dc->pdev->dev, "Could not read status from "
1201 "card, we should disable interface\n");
1202 } else {
1203 writew(RESET, dc->reg_fcr);
1205 /* No more useful info if this was the reset interrupt. */
1206 goto exit_handler;
1208 if (read_iir & CTRL_UL) {
1209 DBG1("CTRL_UL");
1210 dc->last_ier &= ~CTRL_UL;
1211 writew(dc->last_ier, dc->reg_ier);
1212 if (send_flow_control(dc)) {
1213 writew(CTRL_UL, dc->reg_fcr);
1214 dc->last_ier = dc->last_ier | CTRL_UL;
1215 writew(dc->last_ier, dc->reg_ier);
1218 if (read_iir & CTRL_DL) {
1219 receive_flow_control(dc);
1220 writew(CTRL_DL, dc->reg_fcr);
1222 if (read_iir & MDM_DL) {
1223 if (!handle_data_dl(dc, PORT_MDM,
1224 &(dc->port[PORT_MDM].toggle_dl), read_iir,
1225 MDM_DL1, MDM_DL2)) {
1226 dev_err(&dc->pdev->dev, "MDM_DL out of sync!\n");
1227 goto exit_handler;
1230 if (read_iir & MDM_UL) {
1231 if (!handle_data_ul(dc, PORT_MDM, read_iir)) {
1232 dev_err(&dc->pdev->dev, "MDM_UL out of sync!\n");
1233 goto exit_handler;
1236 if (read_iir & DIAG_DL) {
1237 if (!handle_data_dl(dc, PORT_DIAG,
1238 &(dc->port[PORT_DIAG].toggle_dl), read_iir,
1239 DIAG_DL1, DIAG_DL2)) {
1240 dev_err(&dc->pdev->dev, "DIAG_DL out of sync!\n");
1241 goto exit_handler;
1244 if (read_iir & DIAG_UL) {
1245 dc->last_ier &= ~DIAG_UL;
1246 writew(dc->last_ier, dc->reg_ier);
1247 if (send_data(PORT_DIAG, dc)) {
1248 writew(DIAG_UL, dc->reg_fcr);
1249 dc->last_ier = dc->last_ier | DIAG_UL;
1250 writew(dc->last_ier, dc->reg_ier);
1253 if (read_iir & APP1_DL) {
1254 if (receive_data(PORT_APP1, dc))
1255 writew(APP1_DL, dc->reg_fcr);
1257 if (read_iir & APP1_UL) {
1258 dc->last_ier &= ~APP1_UL;
1259 writew(dc->last_ier, dc->reg_ier);
1260 if (send_data(PORT_APP1, dc)) {
1261 writew(APP1_UL, dc->reg_fcr);
1262 dc->last_ier = dc->last_ier | APP1_UL;
1263 writew(dc->last_ier, dc->reg_ier);
1266 if (read_iir & APP2_DL) {
1267 if (receive_data(PORT_APP2, dc))
1268 writew(APP2_DL, dc->reg_fcr);
1270 if (read_iir & APP2_UL) {
1271 dc->last_ier &= ~APP2_UL;
1272 writew(dc->last_ier, dc->reg_ier);
1273 if (send_data(PORT_APP2, dc)) {
1274 writew(APP2_UL, dc->reg_fcr);
1275 dc->last_ier = dc->last_ier | APP2_UL;
1276 writew(dc->last_ier, dc->reg_ier);
1280 exit_handler:
1281 spin_unlock(&dc->spin_mutex);
1282 for (a = 0; a < NOZOMI_MAX_PORTS; a++) {
1283 struct tty_struct *tty;
1284 if (test_and_clear_bit(a, &dc->flip)) {
1285 tty = tty_port_tty_get(&dc->port[a].port);
1286 if (tty)
1287 tty_flip_buffer_push(tty);
1288 tty_kref_put(tty);
1291 return IRQ_HANDLED;
1292 none:
1293 spin_unlock(&dc->spin_mutex);
1294 return IRQ_NONE;
1297 static void nozomi_get_card_type(struct nozomi *dc)
1299 int i;
1300 u32 size = 0;
1302 for (i = 0; i < 6; i++)
1303 size += pci_resource_len(dc->pdev, i);
1305 /* Assume card type F32_8 if no match */
1306 dc->card_type = size == 2048 ? F32_2 : F32_8;
1308 dev_info(&dc->pdev->dev, "Card type is: %d\n", dc->card_type);
1311 static void nozomi_setup_private_data(struct nozomi *dc)
1313 void __iomem *offset = dc->base_addr + dc->card_type / 2;
1314 unsigned int i;
1316 dc->reg_fcr = (void __iomem *)(offset + R_FCR);
1317 dc->reg_iir = (void __iomem *)(offset + R_IIR);
1318 dc->reg_ier = (void __iomem *)(offset + R_IER);
1319 dc->last_ier = 0;
1320 dc->flip = 0;
1322 dc->port[PORT_MDM].token_dl = MDM_DL;
1323 dc->port[PORT_DIAG].token_dl = DIAG_DL;
1324 dc->port[PORT_APP1].token_dl = APP1_DL;
1325 dc->port[PORT_APP2].token_dl = APP2_DL;
1327 for (i = 0; i < MAX_PORT; i++)
1328 init_waitqueue_head(&dc->port[i].tty_wait);
1331 static ssize_t card_type_show(struct device *dev, struct device_attribute *attr,
1332 char *buf)
1334 const struct nozomi *dc = pci_get_drvdata(to_pci_dev(dev));
1336 return sprintf(buf, "%d\n", dc->card_type);
1338 static DEVICE_ATTR(card_type, S_IRUGO, card_type_show, NULL);
1340 static ssize_t open_ttys_show(struct device *dev, struct device_attribute *attr,
1341 char *buf)
1343 const struct nozomi *dc = pci_get_drvdata(to_pci_dev(dev));
1345 return sprintf(buf, "%u\n", dc->open_ttys);
1347 static DEVICE_ATTR(open_ttys, S_IRUGO, open_ttys_show, NULL);
1349 static void make_sysfs_files(struct nozomi *dc)
1351 if (device_create_file(&dc->pdev->dev, &dev_attr_card_type))
1352 dev_err(&dc->pdev->dev,
1353 "Could not create sysfs file for card_type\n");
1354 if (device_create_file(&dc->pdev->dev, &dev_attr_open_ttys))
1355 dev_err(&dc->pdev->dev,
1356 "Could not create sysfs file for open_ttys\n");
1359 static void remove_sysfs_files(struct nozomi *dc)
1361 device_remove_file(&dc->pdev->dev, &dev_attr_card_type);
1362 device_remove_file(&dc->pdev->dev, &dev_attr_open_ttys);
1365 /* Allocate memory for one device */
1366 static int __devinit nozomi_card_init(struct pci_dev *pdev,
1367 const struct pci_device_id *ent)
1369 resource_size_t start;
1370 int ret;
1371 struct nozomi *dc = NULL;
1372 int ndev_idx;
1373 int i;
1375 dev_dbg(&pdev->dev, "Init, new card found\n");
1377 for (ndev_idx = 0; ndev_idx < ARRAY_SIZE(ndevs); ndev_idx++)
1378 if (!ndevs[ndev_idx])
1379 break;
1381 if (ndev_idx >= ARRAY_SIZE(ndevs)) {
1382 dev_err(&pdev->dev, "no free tty range for this card left\n");
1383 ret = -EIO;
1384 goto err;
1387 dc = kzalloc(sizeof(struct nozomi), GFP_KERNEL);
1388 if (unlikely(!dc)) {
1389 dev_err(&pdev->dev, "Could not allocate memory\n");
1390 ret = -ENOMEM;
1391 goto err_free;
1394 dc->pdev = pdev;
1396 ret = pci_enable_device(dc->pdev);
1397 if (ret) {
1398 dev_err(&pdev->dev, "Failed to enable PCI Device\n");
1399 goto err_free;
1402 ret = pci_request_regions(dc->pdev, NOZOMI_NAME);
1403 if (ret) {
1404 dev_err(&pdev->dev, "I/O address 0x%04x already in use\n",
1405 (int) /* nozomi_private.io_addr */ 0);
1406 goto err_disable_device;
1409 start = pci_resource_start(dc->pdev, 0);
1410 if (start == 0) {
1411 dev_err(&pdev->dev, "No I/O address for card detected\n");
1412 ret = -ENODEV;
1413 goto err_rel_regs;
1416 /* Find out what card type it is */
1417 nozomi_get_card_type(dc);
1419 dc->base_addr = ioremap_nocache(start, dc->card_type);
1420 if (!dc->base_addr) {
1421 dev_err(&pdev->dev, "Unable to map card MMIO\n");
1422 ret = -ENODEV;
1423 goto err_rel_regs;
1426 dc->send_buf = kmalloc(SEND_BUF_MAX, GFP_KERNEL);
1427 if (!dc->send_buf) {
1428 dev_err(&pdev->dev, "Could not allocate send buffer?\n");
1429 ret = -ENOMEM;
1430 goto err_free_sbuf;
1433 for (i = PORT_MDM; i < MAX_PORT; i++) {
1434 if (kfifo_alloc(&dc->port[i].fifo_ul,
1435 FIFO_BUFFER_SIZE_UL, GFP_ATOMIC)) {
1436 dev_err(&pdev->dev,
1437 "Could not allocate kfifo buffer\n");
1438 ret = -ENOMEM;
1439 goto err_free_kfifo;
1443 spin_lock_init(&dc->spin_mutex);
1445 nozomi_setup_private_data(dc);
1447 /* Disable all interrupts */
1448 dc->last_ier = 0;
1449 writew(dc->last_ier, dc->reg_ier);
1451 ret = request_irq(pdev->irq, &interrupt_handler, IRQF_SHARED,
1452 NOZOMI_NAME, dc);
1453 if (unlikely(ret)) {
1454 dev_err(&pdev->dev, "can't request irq %d\n", pdev->irq);
1455 goto err_free_kfifo;
1458 DBG1("base_addr: %p", dc->base_addr);
1460 make_sysfs_files(dc);
1462 dc->index_start = ndev_idx * MAX_PORT;
1463 ndevs[ndev_idx] = dc;
1465 pci_set_drvdata(pdev, dc);
1467 /* Enable RESET interrupt */
1468 dc->last_ier = RESET;
1469 iowrite16(dc->last_ier, dc->reg_ier);
1471 dc->state = NOZOMI_STATE_ENABLED;
1473 for (i = 0; i < MAX_PORT; i++) {
1474 struct device *tty_dev;
1475 struct port *port = &dc->port[i];
1476 port->dc = dc;
1477 mutex_init(&port->tty_sem);
1478 tty_port_init(&port->port);
1479 port->port.ops = &noz_tty_port_ops;
1480 tty_dev = tty_register_device(ntty_driver, dc->index_start + i,
1481 &pdev->dev);
1483 if (IS_ERR(tty_dev)) {
1484 ret = PTR_ERR(tty_dev);
1485 dev_err(&pdev->dev, "Could not allocate tty?\n");
1486 goto err_free_tty;
1490 return 0;
1492 err_free_tty:
1493 for (i = dc->index_start; i < dc->index_start + MAX_PORT; ++i)
1494 tty_unregister_device(ntty_driver, i);
1495 err_free_kfifo:
1496 for (i = 0; i < MAX_PORT; i++)
1497 kfifo_free(&dc->port[i].fifo_ul);
1498 err_free_sbuf:
1499 kfree(dc->send_buf);
1500 iounmap(dc->base_addr);
1501 err_rel_regs:
1502 pci_release_regions(pdev);
1503 err_disable_device:
1504 pci_disable_device(pdev);
1505 err_free:
1506 kfree(dc);
1507 err:
1508 return ret;
1511 static void __devexit tty_exit(struct nozomi *dc)
1513 unsigned int i;
1515 DBG1(" ");
1517 flush_scheduled_work();
1519 for (i = 0; i < MAX_PORT; ++i) {
1520 struct tty_struct *tty = tty_port_tty_get(&dc->port[i].port);
1521 if (tty && list_empty(&tty->hangup_work.entry))
1522 tty_hangup(tty);
1523 tty_kref_put(tty);
1525 /* Racy below - surely should wait for scheduled work to be done or
1526 complete off a hangup method ? */
1527 while (dc->open_ttys)
1528 msleep(1);
1529 for (i = dc->index_start; i < dc->index_start + MAX_PORT; ++i)
1530 tty_unregister_device(ntty_driver, i);
1533 /* Deallocate memory for one device */
1534 static void __devexit nozomi_card_exit(struct pci_dev *pdev)
1536 int i;
1537 struct ctrl_ul ctrl;
1538 struct nozomi *dc = pci_get_drvdata(pdev);
1540 /* Disable all interrupts */
1541 dc->last_ier = 0;
1542 writew(dc->last_ier, dc->reg_ier);
1544 tty_exit(dc);
1546 /* Send 0x0001, command card to resend the reset token. */
1547 /* This is to get the reset when the module is reloaded. */
1548 ctrl.port = 0x00;
1549 ctrl.reserved = 0;
1550 ctrl.RTS = 0;
1551 ctrl.DTR = 1;
1552 DBG1("sending flow control 0x%04X", *((u16 *)&ctrl));
1554 /* Setup dc->reg addresses to we can use defines here */
1555 write_mem32(dc->port[PORT_CTRL].ul_addr[0], (u32 *)&ctrl, 2);
1556 writew(CTRL_UL, dc->reg_fcr); /* push the token to the card. */
1558 remove_sysfs_files(dc);
1560 free_irq(pdev->irq, dc);
1562 for (i = 0; i < MAX_PORT; i++)
1563 kfifo_free(&dc->port[i].fifo_ul);
1565 kfree(dc->send_buf);
1567 iounmap(dc->base_addr);
1569 pci_release_regions(pdev);
1571 pci_disable_device(pdev);
1573 ndevs[dc->index_start / MAX_PORT] = NULL;
1575 kfree(dc);
1578 static void set_rts(const struct tty_struct *tty, int rts)
1580 struct port *port = get_port_by_tty(tty);
1582 port->ctrl_ul.RTS = rts;
1583 port->update_flow_control = 1;
1584 enable_transmit_ul(PORT_CTRL, get_dc_by_tty(tty));
1587 static void set_dtr(const struct tty_struct *tty, int dtr)
1589 struct port *port = get_port_by_tty(tty);
1591 DBG1("SETTING DTR index: %d, dtr: %d", tty->index, dtr);
1593 port->ctrl_ul.DTR = dtr;
1594 port->update_flow_control = 1;
1595 enable_transmit_ul(PORT_CTRL, get_dc_by_tty(tty));
1599 * ----------------------------------------------------------------------------
1600 * TTY code
1601 * ----------------------------------------------------------------------------
1604 static int ntty_install(struct tty_driver *driver, struct tty_struct *tty)
1606 struct port *port = get_port_by_tty(tty);
1607 struct nozomi *dc = get_dc_by_tty(tty);
1608 int ret;
1609 if (!port || !dc || dc->state != NOZOMI_STATE_READY)
1610 return -ENODEV;
1611 ret = tty_init_termios(tty);
1612 if (ret == 0) {
1613 tty_driver_kref_get(driver);
1614 tty->count++;
1615 tty->driver_data = port;
1616 driver->ttys[tty->index] = tty;
1618 return ret;
1621 static void ntty_cleanup(struct tty_struct *tty)
1623 tty->driver_data = NULL;
1626 static int ntty_activate(struct tty_port *tport, struct tty_struct *tty)
1628 struct port *port = container_of(tport, struct port, port);
1629 struct nozomi *dc = port->dc;
1630 unsigned long flags;
1632 DBG1("open: %d", port->token_dl);
1633 spin_lock_irqsave(&dc->spin_mutex, flags);
1634 dc->last_ier = dc->last_ier | port->token_dl;
1635 writew(dc->last_ier, dc->reg_ier);
1636 dc->open_ttys++;
1637 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1638 printk("noz: activated %d: %p\n", tty->index, tport);
1639 return 0;
1642 static int ntty_open(struct tty_struct *tty, struct file *filp)
1644 struct port *port = tty->driver_data;
1645 return tty_port_open(&port->port, tty, filp);
1648 static void ntty_shutdown(struct tty_port *tport)
1650 struct port *port = container_of(tport, struct port, port);
1651 struct nozomi *dc = port->dc;
1652 unsigned long flags;
1654 DBG1("close: %d", port->token_dl);
1655 spin_lock_irqsave(&dc->spin_mutex, flags);
1656 dc->last_ier &= ~(port->token_dl);
1657 writew(dc->last_ier, dc->reg_ier);
1658 dc->open_ttys--;
1659 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1660 printk("noz: shutdown %p\n", tport);
1663 static void ntty_close(struct tty_struct *tty, struct file *filp)
1665 struct port *port = tty->driver_data;
1666 if (port)
1667 tty_port_close(&port->port, tty, filp);
1670 static void ntty_hangup(struct tty_struct *tty)
1672 struct port *port = tty->driver_data;
1673 tty_port_hangup(&port->port);
1677 * called when the userspace process writes to the tty (/dev/noz*).
1678 * Data is inserted into a fifo, which is then read and transfered to the modem.
1680 static int ntty_write(struct tty_struct *tty, const unsigned char *buffer,
1681 int count)
1683 int rval = -EINVAL;
1684 struct nozomi *dc = get_dc_by_tty(tty);
1685 struct port *port = tty->driver_data;
1686 unsigned long flags;
1688 /* DBG1( "WRITEx: %d, index = %d", count, index); */
1690 if (!dc || !port)
1691 return -ENODEV;
1693 mutex_lock(&port->tty_sem);
1695 if (unlikely(!port->port.count)) {
1696 DBG1(" ");
1697 goto exit;
1700 rval = kfifo_in(&port->fifo_ul, (unsigned char *)buffer, count);
1702 /* notify card */
1703 if (unlikely(dc == NULL)) {
1704 DBG1("No device context?");
1705 goto exit;
1708 spin_lock_irqsave(&dc->spin_mutex, flags);
1709 /* CTS is only valid on the modem channel */
1710 if (port == &(dc->port[PORT_MDM])) {
1711 if (port->ctrl_dl.CTS) {
1712 DBG4("Enable interrupt");
1713 enable_transmit_ul(tty->index % MAX_PORT, dc);
1714 } else {
1715 dev_err(&dc->pdev->dev,
1716 "CTS not active on modem port?\n");
1718 } else {
1719 enable_transmit_ul(tty->index % MAX_PORT, dc);
1721 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1723 exit:
1724 mutex_unlock(&port->tty_sem);
1725 return rval;
1729 * Calculate how much is left in device
1730 * This method is called by the upper tty layer.
1731 * #according to sources N_TTY.c it expects a value >= 0 and
1732 * does not check for negative values.
1734 * If the port is unplugged report lots of room and let the bits
1735 * dribble away so we don't block anything.
1737 static int ntty_write_room(struct tty_struct *tty)
1739 struct port *port = tty->driver_data;
1740 int room = 4096;
1741 const struct nozomi *dc = get_dc_by_tty(tty);
1743 if (dc) {
1744 mutex_lock(&port->tty_sem);
1745 if (port->port.count)
1746 room = kfifo_avail(&port->fifo_ul);
1747 mutex_unlock(&port->tty_sem);
1749 return room;
1752 /* Gets io control parameters */
1753 static int ntty_tiocmget(struct tty_struct *tty, struct file *file)
1755 const struct port *port = tty->driver_data;
1756 const struct ctrl_dl *ctrl_dl = &port->ctrl_dl;
1757 const struct ctrl_ul *ctrl_ul = &port->ctrl_ul;
1759 /* Note: these could change under us but it is not clear this
1760 matters if so */
1761 return (ctrl_ul->RTS ? TIOCM_RTS : 0) |
1762 (ctrl_ul->DTR ? TIOCM_DTR : 0) |
1763 (ctrl_dl->DCD ? TIOCM_CAR : 0) |
1764 (ctrl_dl->RI ? TIOCM_RNG : 0) |
1765 (ctrl_dl->DSR ? TIOCM_DSR : 0) |
1766 (ctrl_dl->CTS ? TIOCM_CTS : 0);
1769 /* Sets io controls parameters */
1770 static int ntty_tiocmset(struct tty_struct *tty, struct file *file,
1771 unsigned int set, unsigned int clear)
1773 struct nozomi *dc = get_dc_by_tty(tty);
1774 unsigned long flags;
1776 spin_lock_irqsave(&dc->spin_mutex, flags);
1777 if (set & TIOCM_RTS)
1778 set_rts(tty, 1);
1779 else if (clear & TIOCM_RTS)
1780 set_rts(tty, 0);
1782 if (set & TIOCM_DTR)
1783 set_dtr(tty, 1);
1784 else if (clear & TIOCM_DTR)
1785 set_dtr(tty, 0);
1786 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1788 return 0;
1791 static int ntty_cflags_changed(struct port *port, unsigned long flags,
1792 struct async_icount *cprev)
1794 const struct async_icount cnow = port->tty_icount;
1795 int ret;
1797 ret = ((flags & TIOCM_RNG) && (cnow.rng != cprev->rng)) ||
1798 ((flags & TIOCM_DSR) && (cnow.dsr != cprev->dsr)) ||
1799 ((flags & TIOCM_CD) && (cnow.dcd != cprev->dcd)) ||
1800 ((flags & TIOCM_CTS) && (cnow.cts != cprev->cts));
1802 *cprev = cnow;
1804 return ret;
1807 static int ntty_tiocgicount(struct tty_struct *tty,
1808 struct serial_icounter_struct *icount)
1810 struct port *port = tty->driver_data;
1811 const struct async_icount cnow = port->tty_icount;
1813 icount->cts = cnow.cts;
1814 icount->dsr = cnow.dsr;
1815 icount->rng = cnow.rng;
1816 icount->dcd = cnow.dcd;
1817 icount->rx = cnow.rx;
1818 icount->tx = cnow.tx;
1819 icount->frame = cnow.frame;
1820 icount->overrun = cnow.overrun;
1821 icount->parity = cnow.parity;
1822 icount->brk = cnow.brk;
1823 icount->buf_overrun = cnow.buf_overrun;
1824 return 0;
1827 static int ntty_ioctl(struct tty_struct *tty, struct file *file,
1828 unsigned int cmd, unsigned long arg)
1830 struct port *port = tty->driver_data;
1831 int rval = -ENOIOCTLCMD;
1833 DBG1("******** IOCTL, cmd: %d", cmd);
1835 switch (cmd) {
1836 case TIOCMIWAIT: {
1837 struct async_icount cprev = port->tty_icount;
1839 rval = wait_event_interruptible(port->tty_wait,
1840 ntty_cflags_changed(port, arg, &cprev));
1841 break;
1843 default:
1844 DBG1("ERR: 0x%08X, %d", cmd, cmd);
1845 break;
1848 return rval;
1852 * Called by the upper tty layer when tty buffers are ready
1853 * to receive data again after a call to throttle.
1855 static void ntty_unthrottle(struct tty_struct *tty)
1857 struct nozomi *dc = get_dc_by_tty(tty);
1858 unsigned long flags;
1860 DBG1("UNTHROTTLE");
1861 spin_lock_irqsave(&dc->spin_mutex, flags);
1862 enable_transmit_dl(tty->index % MAX_PORT, dc);
1863 set_rts(tty, 1);
1865 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1869 * Called by the upper tty layer when the tty buffers are almost full.
1870 * The driver should stop send more data.
1872 static void ntty_throttle(struct tty_struct *tty)
1874 struct nozomi *dc = get_dc_by_tty(tty);
1875 unsigned long flags;
1877 DBG1("THROTTLE");
1878 spin_lock_irqsave(&dc->spin_mutex, flags);
1879 set_rts(tty, 0);
1880 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1883 /* Returns number of chars in buffer, called by tty layer */
1884 static s32 ntty_chars_in_buffer(struct tty_struct *tty)
1886 struct port *port = tty->driver_data;
1887 struct nozomi *dc = get_dc_by_tty(tty);
1888 s32 rval = 0;
1890 if (unlikely(!dc || !port)) {
1891 goto exit_in_buffer;
1894 if (unlikely(!port->port.count)) {
1895 dev_err(&dc->pdev->dev, "No tty open?\n");
1896 goto exit_in_buffer;
1899 rval = kfifo_len(&port->fifo_ul);
1901 exit_in_buffer:
1902 return rval;
1905 static const struct tty_port_operations noz_tty_port_ops = {
1906 .activate = ntty_activate,
1907 .shutdown = ntty_shutdown,
1910 static const struct tty_operations tty_ops = {
1911 .ioctl = ntty_ioctl,
1912 .open = ntty_open,
1913 .close = ntty_close,
1914 .hangup = ntty_hangup,
1915 .write = ntty_write,
1916 .write_room = ntty_write_room,
1917 .unthrottle = ntty_unthrottle,
1918 .throttle = ntty_throttle,
1919 .chars_in_buffer = ntty_chars_in_buffer,
1920 .tiocmget = ntty_tiocmget,
1921 .tiocmset = ntty_tiocmset,
1922 .get_icount = ntty_tiocgicount,
1923 .install = ntty_install,
1924 .cleanup = ntty_cleanup,
1927 /* Module initialization */
1928 static struct pci_driver nozomi_driver = {
1929 .name = NOZOMI_NAME,
1930 .id_table = nozomi_pci_tbl,
1931 .probe = nozomi_card_init,
1932 .remove = __devexit_p(nozomi_card_exit),
1935 static __init int nozomi_init(void)
1937 int ret;
1939 printk(KERN_INFO "Initializing %s\n", VERSION_STRING);
1941 ntty_driver = alloc_tty_driver(NTTY_TTY_MAXMINORS);
1942 if (!ntty_driver)
1943 return -ENOMEM;
1945 ntty_driver->owner = THIS_MODULE;
1946 ntty_driver->driver_name = NOZOMI_NAME_TTY;
1947 ntty_driver->name = "noz";
1948 ntty_driver->major = 0;
1949 ntty_driver->type = TTY_DRIVER_TYPE_SERIAL;
1950 ntty_driver->subtype = SERIAL_TYPE_NORMAL;
1951 ntty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
1952 ntty_driver->init_termios = tty_std_termios;
1953 ntty_driver->init_termios.c_cflag = B115200 | CS8 | CREAD | \
1954 HUPCL | CLOCAL;
1955 ntty_driver->init_termios.c_ispeed = 115200;
1956 ntty_driver->init_termios.c_ospeed = 115200;
1957 tty_set_operations(ntty_driver, &tty_ops);
1959 ret = tty_register_driver(ntty_driver);
1960 if (ret) {
1961 printk(KERN_ERR "Nozomi: failed to register ntty driver\n");
1962 goto free_tty;
1965 ret = pci_register_driver(&nozomi_driver);
1966 if (ret) {
1967 printk(KERN_ERR "Nozomi: can't register pci driver\n");
1968 goto unr_tty;
1971 return 0;
1972 unr_tty:
1973 tty_unregister_driver(ntty_driver);
1974 free_tty:
1975 put_tty_driver(ntty_driver);
1976 return ret;
1979 static __exit void nozomi_exit(void)
1981 printk(KERN_INFO "Unloading %s\n", DRIVER_DESC);
1982 pci_unregister_driver(&nozomi_driver);
1983 tty_unregister_driver(ntty_driver);
1984 put_tty_driver(ntty_driver);
1987 module_init(nozomi_init);
1988 module_exit(nozomi_exit);
1990 module_param(debug, int, S_IRUGO | S_IWUSR);
1992 MODULE_LICENSE("Dual BSD/GPL");
1993 MODULE_DESCRIPTION(DRIVER_DESC);