x86: introduce page pool in cpa
[linux-2.6/mini2440.git] / drivers / char / nozomi.c
blobdfaab2322de32c09a0adeab709cf5613ca9a0d5f
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/serial.h>
52 #include <linux/interrupt.h>
53 #include <linux/kmod.h>
54 #include <linux/init.h>
55 #include <linux/kfifo.h>
56 #include <linux/uaccess.h>
57 #include <asm/byteorder.h>
59 #include <linux/delay.h>
62 #define VERSION_STRING DRIVER_DESC " 2.1d (build date: " \
63 __DATE__ " " __TIME__ ")"
65 /* Macros definitions */
67 /* Default debug printout level */
68 #define NOZOMI_DEBUG_LEVEL 0x00
70 #define P_BUF_SIZE 128
71 #define NFO(_err_flag_, args...) \
72 do { \
73 char tmp[P_BUF_SIZE]; \
74 snprintf(tmp, sizeof(tmp), ##args); \
75 printk(_err_flag_ "[%d] %s(): %s\n", __LINE__, \
76 __FUNCTION__, tmp); \
77 } while (0)
79 #define DBG1(args...) D_(0x01, ##args)
80 #define DBG2(args...) D_(0x02, ##args)
81 #define DBG3(args...) D_(0x04, ##args)
82 #define DBG4(args...) D_(0x08, ##args)
83 #define DBG5(args...) D_(0x10, ##args)
84 #define DBG6(args...) D_(0x20, ##args)
85 #define DBG7(args...) D_(0x40, ##args)
86 #define DBG8(args...) D_(0x80, ##args)
88 #ifdef DEBUG
89 /* Do we need this settable at runtime? */
90 static int debug = NOZOMI_DEBUG_LEVEL;
92 #define D(lvl, args...) do \
93 {if (lvl & debug) NFO(KERN_DEBUG, ##args); } \
94 while (0)
95 #define D_(lvl, args...) D(lvl, ##args)
97 /* These printouts are always printed */
99 #else
100 static int debug;
101 #define D_(lvl, args...)
102 #endif
104 /* TODO: rewrite to optimize macros... */
106 #define TMP_BUF_MAX 256
108 #define DUMP(buf__,len__) \
109 do { \
110 char tbuf[TMP_BUF_MAX] = {0};\
111 if (len__ > 1) {\
112 snprintf(tbuf, len__ > TMP_BUF_MAX ? TMP_BUF_MAX : len__, "%s", buf__);\
113 if (tbuf[len__-2] == '\r') {\
114 tbuf[len__-2] = 'r';\
116 DBG1("SENDING: '%s' (%d+n)", tbuf, len__);\
117 } else {\
118 DBG1("SENDING: '%s' (%d)", tbuf, len__);\
120 } while (0)
122 /* Defines */
123 #define NOZOMI_NAME "nozomi"
124 #define NOZOMI_NAME_TTY "nozomi_tty"
125 #define DRIVER_DESC "Nozomi driver"
127 #define NTTY_TTY_MAXMINORS 256
128 #define NTTY_FIFO_BUFFER_SIZE 8192
130 /* Must be power of 2 */
131 #define FIFO_BUFFER_SIZE_UL 8192
133 /* Size of tmp send buffer to card */
134 #define SEND_BUF_MAX 1024
135 #define RECEIVE_BUF_MAX 4
138 /* Define all types of vendors and devices to support */
139 #define VENDOR1 0x1931 /* Vendor Option */
140 #define DEVICE1 0x000c /* HSDPA card */
142 #define R_IIR 0x0000 /* Interrupt Identity Register */
143 #define R_FCR 0x0000 /* Flow Control Register */
144 #define R_IER 0x0004 /* Interrupt Enable Register */
146 #define CONFIG_MAGIC 0xEFEFFEFE
147 #define TOGGLE_VALID 0x0000
149 /* Definition of interrupt tokens */
150 #define MDM_DL1 0x0001
151 #define MDM_UL1 0x0002
152 #define MDM_DL2 0x0004
153 #define MDM_UL2 0x0008
154 #define DIAG_DL1 0x0010
155 #define DIAG_DL2 0x0020
156 #define DIAG_UL 0x0040
157 #define APP1_DL 0x0080
158 #define APP1_UL 0x0100
159 #define APP2_DL 0x0200
160 #define APP2_UL 0x0400
161 #define CTRL_DL 0x0800
162 #define CTRL_UL 0x1000
163 #define RESET 0x8000
165 #define MDM_DL (MDM_DL1 | MDM_DL2)
166 #define MDM_UL (MDM_UL1 | MDM_UL2)
167 #define DIAG_DL (DIAG_DL1 | DIAG_DL2)
169 /* modem signal definition */
170 #define CTRL_DSR 0x0001
171 #define CTRL_DCD 0x0002
172 #define CTRL_RI 0x0004
173 #define CTRL_CTS 0x0008
175 #define CTRL_DTR 0x0001
176 #define CTRL_RTS 0x0002
178 #define MAX_PORT 4
179 #define NOZOMI_MAX_PORTS 5
180 #define NOZOMI_MAX_CARDS (NTTY_TTY_MAXMINORS / MAX_PORT)
182 /* Type definitions */
185 * There are two types of nozomi cards,
186 * one with 2048 memory and with 8192 memory
188 enum card_type {
189 F32_2 = 2048, /* 512 bytes downlink + uplink * 2 -> 2048 */
190 F32_8 = 8192, /* 3072 bytes downl. + 1024 bytes uplink * 2 -> 8192 */
193 /* Two different toggle channels exist */
194 enum channel_type {
195 CH_A = 0,
196 CH_B = 1,
199 /* Port definition for the card regarding flow control */
200 enum ctrl_port_type {
201 CTRL_CMD = 0,
202 CTRL_MDM = 1,
203 CTRL_DIAG = 2,
204 CTRL_APP1 = 3,
205 CTRL_APP2 = 4,
206 CTRL_ERROR = -1,
209 /* Ports that the nozomi has */
210 enum port_type {
211 PORT_MDM = 0,
212 PORT_DIAG = 1,
213 PORT_APP1 = 2,
214 PORT_APP2 = 3,
215 PORT_CTRL = 4,
216 PORT_ERROR = -1,
219 #ifdef __BIG_ENDIAN
220 /* Big endian */
222 struct toggles {
223 unsigned int enabled:5; /*
224 * Toggle fields are valid if enabled is 0,
225 * else A-channels must always be used.
227 unsigned int diag_dl:1;
228 unsigned int mdm_dl:1;
229 unsigned int mdm_ul:1;
230 } __attribute__ ((packed));
232 /* Configuration table to read at startup of card */
233 /* Is for now only needed during initialization phase */
234 struct config_table {
235 u32 signature;
236 u16 product_information;
237 u16 version;
238 u8 pad3[3];
239 struct toggles toggle;
240 u8 pad1[4];
241 u16 dl_mdm_len1; /*
242 * If this is 64, it can hold
243 * 60 bytes + 4 that is length field
245 u16 dl_start;
247 u16 dl_diag_len1;
248 u16 dl_mdm_len2; /*
249 * If this is 64, it can hold
250 * 60 bytes + 4 that is length field
252 u16 dl_app1_len;
254 u16 dl_diag_len2;
255 u16 dl_ctrl_len;
256 u16 dl_app2_len;
257 u8 pad2[16];
258 u16 ul_mdm_len1;
259 u16 ul_start;
260 u16 ul_diag_len;
261 u16 ul_mdm_len2;
262 u16 ul_app1_len;
263 u16 ul_app2_len;
264 u16 ul_ctrl_len;
265 } __attribute__ ((packed));
267 /* This stores all control downlink flags */
268 struct ctrl_dl {
269 u8 port;
270 unsigned int reserved:4;
271 unsigned int CTS:1;
272 unsigned int RI:1;
273 unsigned int DCD:1;
274 unsigned int DSR:1;
275 } __attribute__ ((packed));
277 /* This stores all control uplink flags */
278 struct ctrl_ul {
279 u8 port;
280 unsigned int reserved:6;
281 unsigned int RTS:1;
282 unsigned int DTR:1;
283 } __attribute__ ((packed));
285 #else
286 /* Little endian */
288 /* This represents the toggle information */
289 struct toggles {
290 unsigned int mdm_ul:1;
291 unsigned int mdm_dl:1;
292 unsigned int diag_dl:1;
293 unsigned int enabled:5; /*
294 * Toggle fields are valid if enabled is 0,
295 * else A-channels must always be used.
297 } __attribute__ ((packed));
299 /* Configuration table to read at startup of card */
300 struct config_table {
301 u32 signature;
302 u16 version;
303 u16 product_information;
304 struct toggles toggle;
305 u8 pad1[7];
306 u16 dl_start;
307 u16 dl_mdm_len1; /*
308 * If this is 64, it can hold
309 * 60 bytes + 4 that is length field
311 u16 dl_mdm_len2;
312 u16 dl_diag_len1;
313 u16 dl_diag_len2;
314 u16 dl_app1_len;
315 u16 dl_app2_len;
316 u16 dl_ctrl_len;
317 u8 pad2[16];
318 u16 ul_start;
319 u16 ul_mdm_len2;
320 u16 ul_mdm_len1;
321 u16 ul_diag_len;
322 u16 ul_app1_len;
323 u16 ul_app2_len;
324 u16 ul_ctrl_len;
325 } __attribute__ ((packed));
327 /* This stores all control downlink flags */
328 struct ctrl_dl {
329 unsigned int DSR:1;
330 unsigned int DCD:1;
331 unsigned int RI:1;
332 unsigned int CTS:1;
333 unsigned int reserverd:4;
334 u8 port;
335 } __attribute__ ((packed));
337 /* This stores all control uplink flags */
338 struct ctrl_ul {
339 unsigned int DTR:1;
340 unsigned int RTS:1;
341 unsigned int reserved:6;
342 u8 port;
343 } __attribute__ ((packed));
344 #endif
346 /* This holds all information that is needed regarding a port */
347 struct port {
348 u8 update_flow_control;
349 struct ctrl_ul ctrl_ul;
350 struct ctrl_dl ctrl_dl;
351 struct kfifo *fifo_ul;
352 void __iomem *dl_addr[2];
353 u32 dl_size[2];
354 u8 toggle_dl;
355 void __iomem *ul_addr[2];
356 u32 ul_size[2];
357 u8 toggle_ul;
358 u16 token_dl;
360 struct tty_struct *tty;
361 int tty_open_count;
362 /* mutex to ensure one access patch to this port */
363 struct mutex tty_sem;
364 wait_queue_head_t tty_wait;
365 struct async_icount tty_icount;
368 /* Private data one for each card in the system */
369 struct nozomi {
370 void __iomem *base_addr;
371 unsigned long flip;
373 /* Pointers to registers */
374 void __iomem *reg_iir;
375 void __iomem *reg_fcr;
376 void __iomem *reg_ier;
378 u16 last_ier;
379 enum card_type card_type;
380 struct config_table config_table; /* Configuration table */
381 struct pci_dev *pdev;
382 struct port port[NOZOMI_MAX_PORTS];
383 u8 *send_buf;
385 spinlock_t spin_mutex; /* secures access to registers and tty */
387 unsigned int index_start;
388 u32 open_ttys;
391 /* This is a data packet that is read or written to/from card */
392 struct buffer {
393 u32 size; /* size is the length of the data buffer */
394 u8 *data;
395 } __attribute__ ((packed));
397 /* Global variables */
398 static const struct pci_device_id nozomi_pci_tbl[] __devinitconst = {
399 {PCI_DEVICE(VENDOR1, DEVICE1)},
403 MODULE_DEVICE_TABLE(pci, nozomi_pci_tbl);
405 static struct nozomi *ndevs[NOZOMI_MAX_CARDS];
406 static struct tty_driver *ntty_driver;
409 * find card by tty_index
411 static inline struct nozomi *get_dc_by_tty(const struct tty_struct *tty)
413 return tty ? ndevs[tty->index / MAX_PORT] : NULL;
416 static inline struct port *get_port_by_tty(const struct tty_struct *tty)
418 struct nozomi *ndev = get_dc_by_tty(tty);
419 return ndev ? &ndev->port[tty->index % MAX_PORT] : NULL;
423 * TODO:
424 * -Optimize
425 * -Rewrite cleaner
428 static void read_mem32(u32 *buf, const void __iomem *mem_addr_start,
429 u32 size_bytes)
431 u32 i = 0;
432 const u32 *ptr = (__force u32 *) mem_addr_start;
433 u16 *buf16;
435 if (unlikely(!ptr || !buf))
436 goto out;
438 /* shortcut for extremely often used cases */
439 switch (size_bytes) {
440 case 2: /* 2 bytes */
441 buf16 = (u16 *) buf;
442 *buf16 = __le16_to_cpu(readw((void __iomem *)ptr));
443 goto out;
444 break;
445 case 4: /* 4 bytes */
446 *(buf) = __le32_to_cpu(readl((void __iomem *)ptr));
447 goto out;
448 break;
451 while (i < size_bytes) {
452 if (size_bytes - i == 2) {
453 /* Handle 2 bytes in the end */
454 buf16 = (u16 *) buf;
455 *(buf16) = __le16_to_cpu(readw((void __iomem *)ptr));
456 i += 2;
457 } else {
458 /* Read 4 bytes */
459 *(buf) = __le32_to_cpu(readl((void __iomem *)ptr));
460 i += 4;
462 buf++;
463 ptr++;
465 out:
466 return;
470 * TODO:
471 * -Optimize
472 * -Rewrite cleaner
474 static u32 write_mem32(void __iomem *mem_addr_start, const u32 *buf,
475 u32 size_bytes)
477 u32 i = 0;
478 u32 *ptr = (__force u32 *) mem_addr_start;
479 const u16 *buf16;
481 if (unlikely(!ptr || !buf))
482 return 0;
484 /* shortcut for extremely often used cases */
485 switch (size_bytes) {
486 case 2: /* 2 bytes */
487 buf16 = (const u16 *)buf;
488 writew(__cpu_to_le16(*buf16), (void __iomem *)ptr);
489 return 2;
490 break;
491 case 1: /*
492 * also needs to write 4 bytes in this case
493 * so falling through..
495 case 4: /* 4 bytes */
496 writel(__cpu_to_le32(*buf), (void __iomem *)ptr);
497 return 4;
498 break;
501 while (i < size_bytes) {
502 if (size_bytes - i == 2) {
503 /* 2 bytes */
504 buf16 = (const u16 *)buf;
505 writew(__cpu_to_le16(*buf16), (void __iomem *)ptr);
506 i += 2;
507 } else {
508 /* 4 bytes */
509 writel(__cpu_to_le32(*buf), (void __iomem *)ptr);
510 i += 4;
512 buf++;
513 ptr++;
515 return i;
518 /* Setup pointers to different channels and also setup buffer sizes. */
519 static void setup_memory(struct nozomi *dc)
521 void __iomem *offset = dc->base_addr + dc->config_table.dl_start;
522 /* The length reported is including the length field of 4 bytes,
523 * hence subtract with 4.
525 const u16 buff_offset = 4;
527 /* Modem port dl configuration */
528 dc->port[PORT_MDM].dl_addr[CH_A] = offset;
529 dc->port[PORT_MDM].dl_addr[CH_B] =
530 (offset += dc->config_table.dl_mdm_len1);
531 dc->port[PORT_MDM].dl_size[CH_A] =
532 dc->config_table.dl_mdm_len1 - buff_offset;
533 dc->port[PORT_MDM].dl_size[CH_B] =
534 dc->config_table.dl_mdm_len2 - buff_offset;
536 /* Diag port dl configuration */
537 dc->port[PORT_DIAG].dl_addr[CH_A] =
538 (offset += dc->config_table.dl_mdm_len2);
539 dc->port[PORT_DIAG].dl_size[CH_A] =
540 dc->config_table.dl_diag_len1 - buff_offset;
541 dc->port[PORT_DIAG].dl_addr[CH_B] =
542 (offset += dc->config_table.dl_diag_len1);
543 dc->port[PORT_DIAG].dl_size[CH_B] =
544 dc->config_table.dl_diag_len2 - buff_offset;
546 /* App1 port dl configuration */
547 dc->port[PORT_APP1].dl_addr[CH_A] =
548 (offset += dc->config_table.dl_diag_len2);
549 dc->port[PORT_APP1].dl_size[CH_A] =
550 dc->config_table.dl_app1_len - buff_offset;
552 /* App2 port dl configuration */
553 dc->port[PORT_APP2].dl_addr[CH_A] =
554 (offset += dc->config_table.dl_app1_len);
555 dc->port[PORT_APP2].dl_size[CH_A] =
556 dc->config_table.dl_app2_len - buff_offset;
558 /* Ctrl dl configuration */
559 dc->port[PORT_CTRL].dl_addr[CH_A] =
560 (offset += dc->config_table.dl_app2_len);
561 dc->port[PORT_CTRL].dl_size[CH_A] =
562 dc->config_table.dl_ctrl_len - buff_offset;
564 offset = dc->base_addr + dc->config_table.ul_start;
566 /* Modem Port ul configuration */
567 dc->port[PORT_MDM].ul_addr[CH_A] = offset;
568 dc->port[PORT_MDM].ul_size[CH_A] =
569 dc->config_table.ul_mdm_len1 - buff_offset;
570 dc->port[PORT_MDM].ul_addr[CH_B] =
571 (offset += dc->config_table.ul_mdm_len1);
572 dc->port[PORT_MDM].ul_size[CH_B] =
573 dc->config_table.ul_mdm_len2 - buff_offset;
575 /* Diag port ul configuration */
576 dc->port[PORT_DIAG].ul_addr[CH_A] =
577 (offset += dc->config_table.ul_mdm_len2);
578 dc->port[PORT_DIAG].ul_size[CH_A] =
579 dc->config_table.ul_diag_len - buff_offset;
581 /* App1 port ul configuration */
582 dc->port[PORT_APP1].ul_addr[CH_A] =
583 (offset += dc->config_table.ul_diag_len);
584 dc->port[PORT_APP1].ul_size[CH_A] =
585 dc->config_table.ul_app1_len - buff_offset;
587 /* App2 port ul configuration */
588 dc->port[PORT_APP2].ul_addr[CH_A] =
589 (offset += dc->config_table.ul_app1_len);
590 dc->port[PORT_APP2].ul_size[CH_A] =
591 dc->config_table.ul_app2_len - buff_offset;
593 /* Ctrl ul configuration */
594 dc->port[PORT_CTRL].ul_addr[CH_A] =
595 (offset += dc->config_table.ul_app2_len);
596 dc->port[PORT_CTRL].ul_size[CH_A] =
597 dc->config_table.ul_ctrl_len - buff_offset;
600 /* Dump config table under initalization phase */
601 #ifdef DEBUG
602 static void dump_table(const struct nozomi *dc)
604 DBG3("signature: 0x%08X", dc->config_table.signature);
605 DBG3("version: 0x%04X", dc->config_table.version);
606 DBG3("product_information: 0x%04X", \
607 dc->config_table.product_information);
608 DBG3("toggle enabled: %d", dc->config_table.toggle.enabled);
609 DBG3("toggle up_mdm: %d", dc->config_table.toggle.mdm_ul);
610 DBG3("toggle dl_mdm: %d", dc->config_table.toggle.mdm_dl);
611 DBG3("toggle dl_dbg: %d", dc->config_table.toggle.diag_dl);
613 DBG3("dl_start: 0x%04X", dc->config_table.dl_start);
614 DBG3("dl_mdm_len0: 0x%04X, %d", dc->config_table.dl_mdm_len1,
615 dc->config_table.dl_mdm_len1);
616 DBG3("dl_mdm_len1: 0x%04X, %d", dc->config_table.dl_mdm_len2,
617 dc->config_table.dl_mdm_len2);
618 DBG3("dl_diag_len0: 0x%04X, %d", dc->config_table.dl_diag_len1,
619 dc->config_table.dl_diag_len1);
620 DBG3("dl_diag_len1: 0x%04X, %d", dc->config_table.dl_diag_len2,
621 dc->config_table.dl_diag_len2);
622 DBG3("dl_app1_len: 0x%04X, %d", dc->config_table.dl_app1_len,
623 dc->config_table.dl_app1_len);
624 DBG3("dl_app2_len: 0x%04X, %d", dc->config_table.dl_app2_len,
625 dc->config_table.dl_app2_len);
626 DBG3("dl_ctrl_len: 0x%04X, %d", dc->config_table.dl_ctrl_len,
627 dc->config_table.dl_ctrl_len);
628 DBG3("ul_start: 0x%04X, %d", dc->config_table.ul_start,
629 dc->config_table.ul_start);
630 DBG3("ul_mdm_len[0]: 0x%04X, %d", dc->config_table.ul_mdm_len1,
631 dc->config_table.ul_mdm_len1);
632 DBG3("ul_mdm_len[1]: 0x%04X, %d", dc->config_table.ul_mdm_len2,
633 dc->config_table.ul_mdm_len2);
634 DBG3("ul_diag_len: 0x%04X, %d", dc->config_table.ul_diag_len,
635 dc->config_table.ul_diag_len);
636 DBG3("ul_app1_len: 0x%04X, %d", dc->config_table.ul_app1_len,
637 dc->config_table.ul_app1_len);
638 DBG3("ul_app2_len: 0x%04X, %d", dc->config_table.ul_app2_len,
639 dc->config_table.ul_app2_len);
640 DBG3("ul_ctrl_len: 0x%04X, %d", dc->config_table.ul_ctrl_len,
641 dc->config_table.ul_ctrl_len);
643 #else
644 static inline void dump_table(const struct nozomi *dc) { }
645 #endif
648 * Read configuration table from card under intalization phase
649 * Returns 1 if ok, else 0
651 static int nozomi_read_config_table(struct nozomi *dc)
653 read_mem32((u32 *) &dc->config_table, dc->base_addr + 0,
654 sizeof(struct config_table));
656 if (dc->config_table.signature != CONFIG_MAGIC) {
657 dev_err(&dc->pdev->dev, "ConfigTable Bad! 0x%08X != 0x%08X\n",
658 dc->config_table.signature, CONFIG_MAGIC);
659 return 0;
662 if ((dc->config_table.version == 0)
663 || (dc->config_table.toggle.enabled == TOGGLE_VALID)) {
664 int i;
665 DBG1("Second phase, configuring card");
667 setup_memory(dc);
669 dc->port[PORT_MDM].toggle_ul = dc->config_table.toggle.mdm_ul;
670 dc->port[PORT_MDM].toggle_dl = dc->config_table.toggle.mdm_dl;
671 dc->port[PORT_DIAG].toggle_dl = dc->config_table.toggle.diag_dl;
672 DBG1("toggle ports: MDM UL:%d MDM DL:%d, DIAG DL:%d",
673 dc->port[PORT_MDM].toggle_ul,
674 dc->port[PORT_MDM].toggle_dl, dc->port[PORT_DIAG].toggle_dl);
676 dump_table(dc);
678 for (i = PORT_MDM; i < MAX_PORT; i++) {
679 dc->port[i].fifo_ul =
680 kfifo_alloc(FIFO_BUFFER_SIZE_UL, GFP_ATOMIC, NULL);
681 memset(&dc->port[i].ctrl_dl, 0, sizeof(struct ctrl_dl));
682 memset(&dc->port[i].ctrl_ul, 0, sizeof(struct ctrl_ul));
685 /* Enable control channel */
686 dc->last_ier = dc->last_ier | CTRL_DL;
687 writew(dc->last_ier, dc->reg_ier);
689 dev_info(&dc->pdev->dev, "Initialization OK!\n");
690 return 1;
693 if ((dc->config_table.version > 0)
694 && (dc->config_table.toggle.enabled != TOGGLE_VALID)) {
695 u32 offset = 0;
696 DBG1("First phase: pushing upload buffers, clearing download");
698 dev_info(&dc->pdev->dev, "Version of card: %d\n",
699 dc->config_table.version);
701 /* Here we should disable all I/O over F32. */
702 setup_memory(dc);
705 * We should send ALL channel pair tokens back along
706 * with reset token
709 /* push upload modem buffers */
710 write_mem32(dc->port[PORT_MDM].ul_addr[CH_A],
711 (u32 *) &offset, 4);
712 write_mem32(dc->port[PORT_MDM].ul_addr[CH_B],
713 (u32 *) &offset, 4);
715 writew(MDM_UL | DIAG_DL | MDM_DL, dc->reg_fcr);
717 DBG1("First phase done");
720 return 1;
723 /* Enable uplink interrupts */
724 static void enable_transmit_ul(enum port_type port, struct nozomi *dc)
726 static const u16 mask[] = {MDM_UL, DIAG_UL, APP1_UL, APP2_UL, CTRL_UL};
728 if (port < NOZOMI_MAX_PORTS) {
729 dc->last_ier |= mask[port];
730 writew(dc->last_ier, dc->reg_ier);
731 } else {
732 dev_err(&dc->pdev->dev, "Called with wrong port?\n");
736 /* Disable uplink interrupts */
737 static void disable_transmit_ul(enum port_type port, struct nozomi *dc)
739 static const u16 mask[] =
740 {~MDM_UL, ~DIAG_UL, ~APP1_UL, ~APP2_UL, ~CTRL_UL};
742 if (port < NOZOMI_MAX_PORTS) {
743 dc->last_ier &= mask[port];
744 writew(dc->last_ier, dc->reg_ier);
745 } else {
746 dev_err(&dc->pdev->dev, "Called with wrong port?\n");
750 /* Enable downlink interrupts */
751 static void enable_transmit_dl(enum port_type port, struct nozomi *dc)
753 static const u16 mask[] = {MDM_DL, DIAG_DL, APP1_DL, APP2_DL, CTRL_DL};
755 if (port < NOZOMI_MAX_PORTS) {
756 dc->last_ier |= mask[port];
757 writew(dc->last_ier, dc->reg_ier);
758 } else {
759 dev_err(&dc->pdev->dev, "Called with wrong port?\n");
763 /* Disable downlink interrupts */
764 static void disable_transmit_dl(enum port_type port, struct nozomi *dc)
766 static const u16 mask[] =
767 {~MDM_DL, ~DIAG_DL, ~APP1_DL, ~APP2_DL, ~CTRL_DL};
769 if (port < NOZOMI_MAX_PORTS) {
770 dc->last_ier &= mask[port];
771 writew(dc->last_ier, dc->reg_ier);
772 } else {
773 dev_err(&dc->pdev->dev, "Called with wrong port?\n");
778 * Return 1 - send buffer to card and ack.
779 * Return 0 - don't ack, don't send buffer to card.
781 static int send_data(enum port_type index, const struct nozomi *dc)
783 u32 size = 0;
784 const struct port *port = &dc->port[index];
785 const u8 toggle = port->toggle_ul;
786 void __iomem *addr = port->ul_addr[toggle];
787 const u32 ul_size = port->ul_size[toggle];
788 struct tty_struct *tty = port->tty;
790 /* Get data from tty and place in buf for now */
791 size = __kfifo_get(port->fifo_ul, dc->send_buf,
792 ul_size < SEND_BUF_MAX ? ul_size : SEND_BUF_MAX);
794 if (size == 0) {
795 DBG4("No more data to send, disable link:");
796 return 0;
799 /* DUMP(buf, size); */
801 /* Write length + data */
802 write_mem32(addr, (u32 *) &size, 4);
803 write_mem32(addr + 4, (u32 *) dc->send_buf, size);
805 if (tty)
806 tty_wakeup(tty);
808 return 1;
811 /* If all data has been read, return 1, else 0 */
812 static int receive_data(enum port_type index, struct nozomi *dc)
814 u8 buf[RECEIVE_BUF_MAX] = { 0 };
815 int size;
816 u32 offset = 4;
817 struct port *port = &dc->port[index];
818 void __iomem *addr = port->dl_addr[port->toggle_dl];
819 struct tty_struct *tty = port->tty;
820 int i;
822 if (unlikely(!tty)) {
823 DBG1("tty not open for port: %d?", index);
824 return 1;
827 read_mem32((u32 *) &size, addr, 4);
828 /* DBG1( "%d bytes port: %d", size, index); */
830 if (test_bit(TTY_THROTTLED, &tty->flags)) {
831 DBG1("No room in tty, don't read data, don't ack interrupt, "
832 "disable interrupt");
834 /* disable interrupt in downlink... */
835 disable_transmit_dl(index, dc);
836 return 0;
839 if (unlikely(size == 0)) {
840 dev_err(&dc->pdev->dev, "size == 0?\n");
841 return 1;
844 tty_buffer_request_room(tty, size);
846 while (size > 0) {
847 read_mem32((u32 *) buf, addr + offset, RECEIVE_BUF_MAX);
849 if (size == 1) {
850 tty_insert_flip_char(tty, buf[0], TTY_NORMAL);
851 size = 0;
852 } else if (size < RECEIVE_BUF_MAX) {
853 size -= tty_insert_flip_string(tty, (char *) buf, size);
854 } else {
855 i = tty_insert_flip_string(tty, \
856 (char *) buf, RECEIVE_BUF_MAX);
857 size -= i;
858 offset += i;
862 set_bit(index, &dc->flip);
864 return 1;
867 /* Debug for interrupts */
868 #ifdef DEBUG
869 static char *interrupt2str(u16 interrupt)
871 static char buf[TMP_BUF_MAX];
872 char *p = buf;
874 interrupt & MDM_DL1 ? p += snprintf(p, TMP_BUF_MAX, "MDM_DL1 ") : NULL;
875 interrupt & MDM_DL2 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
876 "MDM_DL2 ") : NULL;
878 interrupt & MDM_UL1 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
879 "MDM_UL1 ") : NULL;
880 interrupt & MDM_UL2 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
881 "MDM_UL2 ") : NULL;
883 interrupt & DIAG_DL1 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
884 "DIAG_DL1 ") : NULL;
885 interrupt & DIAG_DL2 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
886 "DIAG_DL2 ") : NULL;
888 interrupt & DIAG_UL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
889 "DIAG_UL ") : NULL;
891 interrupt & APP1_DL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
892 "APP1_DL ") : NULL;
893 interrupt & APP2_DL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
894 "APP2_DL ") : NULL;
896 interrupt & APP1_UL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
897 "APP1_UL ") : NULL;
898 interrupt & APP2_UL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
899 "APP2_UL ") : NULL;
901 interrupt & CTRL_DL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
902 "CTRL_DL ") : NULL;
903 interrupt & CTRL_UL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
904 "CTRL_UL ") : NULL;
906 interrupt & RESET ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
907 "RESET ") : NULL;
909 return buf;
911 #endif
914 * Receive flow control
915 * Return 1 - If ok, else 0
917 static int receive_flow_control(struct nozomi *dc)
919 enum port_type port = PORT_MDM;
920 struct ctrl_dl ctrl_dl;
921 struct ctrl_dl old_ctrl;
922 u16 enable_ier = 0;
924 read_mem32((u32 *) &ctrl_dl, dc->port[PORT_CTRL].dl_addr[CH_A], 2);
926 switch (ctrl_dl.port) {
927 case CTRL_CMD:
928 DBG1("The Base Band sends this value as a response to a "
929 "request for IMSI detach sent over the control "
930 "channel uplink (see section 7.6.1).");
931 break;
932 case CTRL_MDM:
933 port = PORT_MDM;
934 enable_ier = MDM_DL;
935 break;
936 case CTRL_DIAG:
937 port = PORT_DIAG;
938 enable_ier = DIAG_DL;
939 break;
940 case CTRL_APP1:
941 port = PORT_APP1;
942 enable_ier = APP1_DL;
943 break;
944 case CTRL_APP2:
945 port = PORT_APP2;
946 enable_ier = APP2_DL;
947 break;
948 default:
949 dev_err(&dc->pdev->dev,
950 "ERROR: flow control received for non-existing port\n");
951 return 0;
954 DBG1("0x%04X->0x%04X", *((u16 *)&dc->port[port].ctrl_dl),
955 *((u16 *)&ctrl_dl));
957 old_ctrl = dc->port[port].ctrl_dl;
958 dc->port[port].ctrl_dl = ctrl_dl;
960 if (old_ctrl.CTS == 1 && ctrl_dl.CTS == 0) {
961 DBG1("Disable interrupt (0x%04X) on port: %d",
962 enable_ier, port);
963 disable_transmit_ul(port, dc);
965 } else if (old_ctrl.CTS == 0 && ctrl_dl.CTS == 1) {
967 if (__kfifo_len(dc->port[port].fifo_ul)) {
968 DBG1("Enable interrupt (0x%04X) on port: %d",
969 enable_ier, port);
970 DBG1("Data in buffer [%d], enable transmit! ",
971 __kfifo_len(dc->port[port].fifo_ul));
972 enable_transmit_ul(port, dc);
973 } else {
974 DBG1("No data in buffer...");
978 if (*(u16 *)&old_ctrl == *(u16 *)&ctrl_dl) {
979 DBG1(" No change in mctrl");
980 return 1;
982 /* Update statistics */
983 if (old_ctrl.CTS != ctrl_dl.CTS)
984 dc->port[port].tty_icount.cts++;
985 if (old_ctrl.DSR != ctrl_dl.DSR)
986 dc->port[port].tty_icount.dsr++;
987 if (old_ctrl.RI != ctrl_dl.RI)
988 dc->port[port].tty_icount.rng++;
989 if (old_ctrl.DCD != ctrl_dl.DCD)
990 dc->port[port].tty_icount.dcd++;
992 wake_up_interruptible(&dc->port[port].tty_wait);
994 DBG1("port: %d DCD(%d), CTS(%d), RI(%d), DSR(%d)",
995 port,
996 dc->port[port].tty_icount.dcd, dc->port[port].tty_icount.cts,
997 dc->port[port].tty_icount.rng, dc->port[port].tty_icount.dsr);
999 return 1;
1002 static enum ctrl_port_type port2ctrl(enum port_type port,
1003 const struct nozomi *dc)
1005 switch (port) {
1006 case PORT_MDM:
1007 return CTRL_MDM;
1008 case PORT_DIAG:
1009 return CTRL_DIAG;
1010 case PORT_APP1:
1011 return CTRL_APP1;
1012 case PORT_APP2:
1013 return CTRL_APP2;
1014 default:
1015 dev_err(&dc->pdev->dev,
1016 "ERROR: send flow control " \
1017 "received for non-existing port\n");
1019 return CTRL_ERROR;
1023 * Send flow control, can only update one channel at a time
1024 * Return 0 - If we have updated all flow control
1025 * Return 1 - If we need to update more flow control, ack current enable more
1027 static int send_flow_control(struct nozomi *dc)
1029 u32 i, more_flow_control_to_be_updated = 0;
1030 u16 *ctrl;
1032 for (i = PORT_MDM; i < MAX_PORT; i++) {
1033 if (dc->port[i].update_flow_control) {
1034 if (more_flow_control_to_be_updated) {
1035 /* We have more flow control to be updated */
1036 return 1;
1038 dc->port[i].ctrl_ul.port = port2ctrl(i, dc);
1039 ctrl = (u16 *)&dc->port[i].ctrl_ul;
1040 write_mem32(dc->port[PORT_CTRL].ul_addr[0], \
1041 (u32 *) ctrl, 2);
1042 dc->port[i].update_flow_control = 0;
1043 more_flow_control_to_be_updated = 1;
1046 return 0;
1050 * Handle downlink data, ports that are handled are modem and diagnostics
1051 * Return 1 - ok
1052 * Return 0 - toggle fields are out of sync
1054 static int handle_data_dl(struct nozomi *dc, enum port_type port, u8 *toggle,
1055 u16 read_iir, u16 mask1, u16 mask2)
1057 if (*toggle == 0 && read_iir & mask1) {
1058 if (receive_data(port, dc)) {
1059 writew(mask1, dc->reg_fcr);
1060 *toggle = !(*toggle);
1063 if (read_iir & mask2) {
1064 if (receive_data(port, dc)) {
1065 writew(mask2, dc->reg_fcr);
1066 *toggle = !(*toggle);
1069 } else if (*toggle == 1 && read_iir & mask2) {
1070 if (receive_data(port, dc)) {
1071 writew(mask2, dc->reg_fcr);
1072 *toggle = !(*toggle);
1075 if (read_iir & mask1) {
1076 if (receive_data(port, dc)) {
1077 writew(mask1, dc->reg_fcr);
1078 *toggle = !(*toggle);
1081 } else {
1082 dev_err(&dc->pdev->dev, "port out of sync!, toggle:%d\n",
1083 *toggle);
1084 return 0;
1086 return 1;
1090 * Handle uplink data, this is currently for the modem port
1091 * Return 1 - ok
1092 * Return 0 - toggle field are out of sync
1094 static int handle_data_ul(struct nozomi *dc, enum port_type port, u16 read_iir)
1096 u8 *toggle = &(dc->port[port].toggle_ul);
1098 if (*toggle == 0 && read_iir & MDM_UL1) {
1099 dc->last_ier &= ~MDM_UL;
1100 writew(dc->last_ier, dc->reg_ier);
1101 if (send_data(port, dc)) {
1102 writew(MDM_UL1, dc->reg_fcr);
1103 dc->last_ier = dc->last_ier | MDM_UL;
1104 writew(dc->last_ier, dc->reg_ier);
1105 *toggle = !*toggle;
1108 if (read_iir & MDM_UL2) {
1109 dc->last_ier &= ~MDM_UL;
1110 writew(dc->last_ier, dc->reg_ier);
1111 if (send_data(port, dc)) {
1112 writew(MDM_UL2, dc->reg_fcr);
1113 dc->last_ier = dc->last_ier | MDM_UL;
1114 writew(dc->last_ier, dc->reg_ier);
1115 *toggle = !*toggle;
1119 } else if (*toggle == 1 && read_iir & MDM_UL2) {
1120 dc->last_ier &= ~MDM_UL;
1121 writew(dc->last_ier, dc->reg_ier);
1122 if (send_data(port, dc)) {
1123 writew(MDM_UL2, 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_UL1) {
1130 dc->last_ier &= ~MDM_UL;
1131 writew(dc->last_ier, dc->reg_ier);
1132 if (send_data(port, dc)) {
1133 writew(MDM_UL1, dc->reg_fcr);
1134 dc->last_ier = dc->last_ier | MDM_UL;
1135 writew(dc->last_ier, dc->reg_ier);
1136 *toggle = !*toggle;
1139 } else {
1140 writew(read_iir & MDM_UL, dc->reg_fcr);
1141 dev_err(&dc->pdev->dev, "port out of sync!\n");
1142 return 0;
1144 return 1;
1147 static irqreturn_t interrupt_handler(int irq, void *dev_id)
1149 struct nozomi *dc = dev_id;
1150 unsigned int a;
1151 u16 read_iir;
1153 if (!dc)
1154 return IRQ_NONE;
1156 spin_lock(&dc->spin_mutex);
1157 read_iir = readw(dc->reg_iir);
1159 /* Card removed */
1160 if (read_iir == (u16)-1)
1161 goto none;
1163 * Just handle interrupt enabled in IER
1164 * (by masking with dc->last_ier)
1166 read_iir &= dc->last_ier;
1168 if (read_iir == 0)
1169 goto none;
1172 DBG4("%s irq:0x%04X, prev:0x%04X", interrupt2str(read_iir), read_iir,
1173 dc->last_ier);
1175 if (read_iir & RESET) {
1176 if (unlikely(!nozomi_read_config_table(dc))) {
1177 dc->last_ier = 0x0;
1178 writew(dc->last_ier, dc->reg_ier);
1179 dev_err(&dc->pdev->dev, "Could not read status from "
1180 "card, we should disable interface\n");
1181 } else {
1182 writew(RESET, dc->reg_fcr);
1184 /* No more useful info if this was the reset interrupt. */
1185 goto exit_handler;
1187 if (read_iir & CTRL_UL) {
1188 DBG1("CTRL_UL");
1189 dc->last_ier &= ~CTRL_UL;
1190 writew(dc->last_ier, dc->reg_ier);
1191 if (send_flow_control(dc)) {
1192 writew(CTRL_UL, dc->reg_fcr);
1193 dc->last_ier = dc->last_ier | CTRL_UL;
1194 writew(dc->last_ier, dc->reg_ier);
1197 if (read_iir & CTRL_DL) {
1198 receive_flow_control(dc);
1199 writew(CTRL_DL, dc->reg_fcr);
1201 if (read_iir & MDM_DL) {
1202 if (!handle_data_dl(dc, PORT_MDM,
1203 &(dc->port[PORT_MDM].toggle_dl), read_iir,
1204 MDM_DL1, MDM_DL2)) {
1205 dev_err(&dc->pdev->dev, "MDM_DL out of sync!\n");
1206 goto exit_handler;
1209 if (read_iir & MDM_UL) {
1210 if (!handle_data_ul(dc, PORT_MDM, read_iir)) {
1211 dev_err(&dc->pdev->dev, "MDM_UL out of sync!\n");
1212 goto exit_handler;
1215 if (read_iir & DIAG_DL) {
1216 if (!handle_data_dl(dc, PORT_DIAG,
1217 &(dc->port[PORT_DIAG].toggle_dl), read_iir,
1218 DIAG_DL1, DIAG_DL2)) {
1219 dev_err(&dc->pdev->dev, "DIAG_DL out of sync!\n");
1220 goto exit_handler;
1223 if (read_iir & DIAG_UL) {
1224 dc->last_ier &= ~DIAG_UL;
1225 writew(dc->last_ier, dc->reg_ier);
1226 if (send_data(PORT_DIAG, dc)) {
1227 writew(DIAG_UL, dc->reg_fcr);
1228 dc->last_ier = dc->last_ier | DIAG_UL;
1229 writew(dc->last_ier, dc->reg_ier);
1232 if (read_iir & APP1_DL) {
1233 if (receive_data(PORT_APP1, dc))
1234 writew(APP1_DL, dc->reg_fcr);
1236 if (read_iir & APP1_UL) {
1237 dc->last_ier &= ~APP1_UL;
1238 writew(dc->last_ier, dc->reg_ier);
1239 if (send_data(PORT_APP1, dc)) {
1240 writew(APP1_UL, dc->reg_fcr);
1241 dc->last_ier = dc->last_ier | APP1_UL;
1242 writew(dc->last_ier, dc->reg_ier);
1245 if (read_iir & APP2_DL) {
1246 if (receive_data(PORT_APP2, dc))
1247 writew(APP2_DL, dc->reg_fcr);
1249 if (read_iir & APP2_UL) {
1250 dc->last_ier &= ~APP2_UL;
1251 writew(dc->last_ier, dc->reg_ier);
1252 if (send_data(PORT_APP2, dc)) {
1253 writew(APP2_UL, dc->reg_fcr);
1254 dc->last_ier = dc->last_ier | APP2_UL;
1255 writew(dc->last_ier, dc->reg_ier);
1259 exit_handler:
1260 spin_unlock(&dc->spin_mutex);
1261 for (a = 0; a < NOZOMI_MAX_PORTS; a++)
1262 if (test_and_clear_bit(a, &dc->flip))
1263 tty_flip_buffer_push(dc->port[a].tty);
1264 return IRQ_HANDLED;
1265 none:
1266 spin_unlock(&dc->spin_mutex);
1267 return IRQ_NONE;
1270 static void nozomi_get_card_type(struct nozomi *dc)
1272 int i;
1273 u32 size = 0;
1275 for (i = 0; i < 6; i++)
1276 size += pci_resource_len(dc->pdev, i);
1278 /* Assume card type F32_8 if no match */
1279 dc->card_type = size == 2048 ? F32_2 : F32_8;
1281 dev_info(&dc->pdev->dev, "Card type is: %d\n", dc->card_type);
1284 static void nozomi_setup_private_data(struct nozomi *dc)
1286 void __iomem *offset = dc->base_addr + dc->card_type / 2;
1287 unsigned int i;
1289 dc->reg_fcr = (void __iomem *)(offset + R_FCR);
1290 dc->reg_iir = (void __iomem *)(offset + R_IIR);
1291 dc->reg_ier = (void __iomem *)(offset + R_IER);
1292 dc->last_ier = 0;
1293 dc->flip = 0;
1295 dc->port[PORT_MDM].token_dl = MDM_DL;
1296 dc->port[PORT_DIAG].token_dl = DIAG_DL;
1297 dc->port[PORT_APP1].token_dl = APP1_DL;
1298 dc->port[PORT_APP2].token_dl = APP2_DL;
1300 for (i = 0; i < MAX_PORT; i++)
1301 init_waitqueue_head(&dc->port[i].tty_wait);
1304 static ssize_t card_type_show(struct device *dev, struct device_attribute *attr,
1305 char *buf)
1307 const struct nozomi *dc = pci_get_drvdata(to_pci_dev(dev));
1309 return sprintf(buf, "%d\n", dc->card_type);
1311 static DEVICE_ATTR(card_type, S_IRUGO, card_type_show, NULL);
1313 static ssize_t open_ttys_show(struct device *dev, struct device_attribute *attr,
1314 char *buf)
1316 const struct nozomi *dc = pci_get_drvdata(to_pci_dev(dev));
1318 return sprintf(buf, "%u\n", dc->open_ttys);
1320 static DEVICE_ATTR(open_ttys, S_IRUGO, open_ttys_show, NULL);
1322 static void make_sysfs_files(struct nozomi *dc)
1324 if (device_create_file(&dc->pdev->dev, &dev_attr_card_type))
1325 dev_err(&dc->pdev->dev,
1326 "Could not create sysfs file for card_type\n");
1327 if (device_create_file(&dc->pdev->dev, &dev_attr_open_ttys))
1328 dev_err(&dc->pdev->dev,
1329 "Could not create sysfs file for open_ttys\n");
1332 static void remove_sysfs_files(struct nozomi *dc)
1334 device_remove_file(&dc->pdev->dev, &dev_attr_card_type);
1335 device_remove_file(&dc->pdev->dev, &dev_attr_open_ttys);
1338 /* Allocate memory for one device */
1339 static int __devinit nozomi_card_init(struct pci_dev *pdev,
1340 const struct pci_device_id *ent)
1342 resource_size_t start;
1343 int ret;
1344 struct nozomi *dc = NULL;
1345 int ndev_idx;
1346 int i;
1348 dev_dbg(&pdev->dev, "Init, new card found\n");
1350 for (ndev_idx = 0; ndev_idx < ARRAY_SIZE(ndevs); ndev_idx++)
1351 if (!ndevs[ndev_idx])
1352 break;
1354 if (ndev_idx >= ARRAY_SIZE(ndevs)) {
1355 dev_err(&pdev->dev, "no free tty range for this card left\n");
1356 ret = -EIO;
1357 goto err;
1360 dc = kzalloc(sizeof(struct nozomi), GFP_KERNEL);
1361 if (unlikely(!dc)) {
1362 dev_err(&pdev->dev, "Could not allocate memory\n");
1363 ret = -ENOMEM;
1364 goto err_free;
1367 dc->pdev = pdev;
1369 /* Find out what card type it is */
1370 nozomi_get_card_type(dc);
1372 ret = pci_enable_device(dc->pdev);
1373 if (ret) {
1374 dev_err(&pdev->dev, "Failed to enable PCI Device\n");
1375 goto err_free;
1378 start = pci_resource_start(dc->pdev, 0);
1379 if (start == 0) {
1380 dev_err(&pdev->dev, "No I/O address for card detected\n");
1381 ret = -ENODEV;
1382 goto err_disable_device;
1385 ret = pci_request_regions(dc->pdev, NOZOMI_NAME);
1386 if (ret) {
1387 dev_err(&pdev->dev, "I/O address 0x%04x already in use\n",
1388 (int) /* nozomi_private.io_addr */ 0);
1389 goto err_disable_device;
1392 dc->base_addr = ioremap(start, dc->card_type);
1393 if (!dc->base_addr) {
1394 dev_err(&pdev->dev, "Unable to map card MMIO\n");
1395 ret = -ENODEV;
1396 goto err_rel_regs;
1399 dc->send_buf = kmalloc(SEND_BUF_MAX, GFP_KERNEL);
1400 if (!dc->send_buf) {
1401 dev_err(&pdev->dev, "Could not allocate send buffer?\n");
1402 ret = -ENOMEM;
1403 goto err_free_sbuf;
1406 spin_lock_init(&dc->spin_mutex);
1408 nozomi_setup_private_data(dc);
1410 /* Disable all interrupts */
1411 dc->last_ier = 0;
1412 writew(dc->last_ier, dc->reg_ier);
1414 ret = request_irq(pdev->irq, &interrupt_handler, IRQF_SHARED,
1415 NOZOMI_NAME, dc);
1416 if (unlikely(ret)) {
1417 dev_err(&pdev->dev, "can't request irq %d\n", pdev->irq);
1418 goto err_free_sbuf;
1421 DBG1("base_addr: %p", dc->base_addr);
1423 make_sysfs_files(dc);
1425 dc->index_start = ndev_idx * MAX_PORT;
1426 ndevs[ndev_idx] = dc;
1428 for (i = 0; i < MAX_PORT; i++) {
1429 mutex_init(&dc->port[i].tty_sem);
1430 dc->port[i].tty_open_count = 0;
1431 dc->port[i].tty = NULL;
1432 tty_register_device(ntty_driver, dc->index_start + i,
1433 &pdev->dev);
1436 /* Enable RESET interrupt. */
1437 dc->last_ier = RESET;
1438 writew(dc->last_ier, dc->reg_ier);
1440 pci_set_drvdata(pdev, dc);
1442 return 0;
1444 err_free_sbuf:
1445 kfree(dc->send_buf);
1446 iounmap(dc->base_addr);
1447 err_rel_regs:
1448 pci_release_regions(pdev);
1449 err_disable_device:
1450 pci_disable_device(pdev);
1451 err_free:
1452 kfree(dc);
1453 err:
1454 return ret;
1457 static void __devexit tty_exit(struct nozomi *dc)
1459 unsigned int i;
1461 DBG1(" ");
1463 flush_scheduled_work();
1465 for (i = 0; i < MAX_PORT; ++i)
1466 if (dc->port[i].tty && \
1467 list_empty(&dc->port[i].tty->hangup_work.entry))
1468 tty_hangup(dc->port[i].tty);
1470 while (dc->open_ttys)
1471 msleep(1);
1473 for (i = dc->index_start; i < dc->index_start + MAX_PORT; ++i)
1474 tty_unregister_device(ntty_driver, i);
1477 /* Deallocate memory for one device */
1478 static void __devexit nozomi_card_exit(struct pci_dev *pdev)
1480 int i;
1481 struct ctrl_ul ctrl;
1482 struct nozomi *dc = pci_get_drvdata(pdev);
1484 /* Disable all interrupts */
1485 dc->last_ier = 0;
1486 writew(dc->last_ier, dc->reg_ier);
1488 tty_exit(dc);
1490 /* Send 0x0001, command card to resend the reset token. */
1491 /* This is to get the reset when the module is reloaded. */
1492 ctrl.port = 0x00;
1493 ctrl.reserved = 0;
1494 ctrl.RTS = 0;
1495 ctrl.DTR = 1;
1496 DBG1("sending flow control 0x%04X", *((u16 *)&ctrl));
1498 /* Setup dc->reg addresses to we can use defines here */
1499 write_mem32(dc->port[PORT_CTRL].ul_addr[0], (u32 *)&ctrl, 2);
1500 writew(CTRL_UL, dc->reg_fcr); /* push the token to the card. */
1502 remove_sysfs_files(dc);
1504 free_irq(pdev->irq, dc);
1506 for (i = 0; i < MAX_PORT; i++)
1507 if (dc->port[i].fifo_ul)
1508 kfifo_free(dc->port[i].fifo_ul);
1510 kfree(dc->send_buf);
1512 iounmap(dc->base_addr);
1514 pci_release_regions(pdev);
1516 pci_disable_device(pdev);
1518 ndevs[dc->index_start / MAX_PORT] = NULL;
1520 kfree(dc);
1523 static void set_rts(const struct tty_struct *tty, int rts)
1525 struct port *port = get_port_by_tty(tty);
1527 port->ctrl_ul.RTS = rts;
1528 port->update_flow_control = 1;
1529 enable_transmit_ul(PORT_CTRL, get_dc_by_tty(tty));
1532 static void set_dtr(const struct tty_struct *tty, int dtr)
1534 struct port *port = get_port_by_tty(tty);
1536 DBG1("SETTING DTR index: %d, dtr: %d", tty->index, dtr);
1538 port->ctrl_ul.DTR = dtr;
1539 port->update_flow_control = 1;
1540 enable_transmit_ul(PORT_CTRL, get_dc_by_tty(tty));
1544 * ----------------------------------------------------------------------------
1545 * TTY code
1546 * ----------------------------------------------------------------------------
1549 /* Called when the userspace process opens the tty, /dev/noz*. */
1550 static int ntty_open(struct tty_struct *tty, struct file *file)
1552 struct port *port = get_port_by_tty(tty);
1553 struct nozomi *dc = get_dc_by_tty(tty);
1554 unsigned long flags;
1556 if (!port || !dc)
1557 return -ENODEV;
1559 if (mutex_lock_interruptible(&port->tty_sem))
1560 return -ERESTARTSYS;
1562 port->tty_open_count++;
1563 dc->open_ttys++;
1565 /* Enable interrupt downlink for channel */
1566 if (port->tty_open_count == 1) {
1567 tty->low_latency = 1;
1568 tty->driver_data = port;
1569 port->tty = tty;
1570 DBG1("open: %d", port->token_dl);
1571 spin_lock_irqsave(&dc->spin_mutex, flags);
1572 dc->last_ier = dc->last_ier | port->token_dl;
1573 writew(dc->last_ier, dc->reg_ier);
1574 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1577 mutex_unlock(&port->tty_sem);
1579 return 0;
1582 /* Called when the userspace process close the tty, /dev/noz*. */
1583 static void ntty_close(struct tty_struct *tty, struct file *file)
1585 struct nozomi *dc = get_dc_by_tty(tty);
1586 struct port *port = tty->driver_data;
1587 unsigned long flags;
1589 if (!dc || !port)
1590 return;
1592 if (mutex_lock_interruptible(&port->tty_sem))
1593 return;
1595 if (!port->tty_open_count)
1596 goto exit;
1598 dc->open_ttys--;
1599 port->tty_open_count--;
1601 if (port->tty_open_count == 0) {
1602 DBG1("close: %d", port->token_dl);
1603 spin_lock_irqsave(&dc->spin_mutex, flags);
1604 dc->last_ier &= ~(port->token_dl);
1605 writew(dc->last_ier, dc->reg_ier);
1606 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1609 exit:
1610 mutex_unlock(&port->tty_sem);
1614 * called when the userspace process writes to the tty (/dev/noz*).
1615 * Data is inserted into a fifo, which is then read and transfered to the modem.
1617 static int ntty_write(struct tty_struct *tty, const unsigned char *buffer,
1618 int count)
1620 int rval = -EINVAL;
1621 struct nozomi *dc = get_dc_by_tty(tty);
1622 struct port *port = tty->driver_data;
1623 unsigned long flags;
1625 /* DBG1( "WRITEx: %d, index = %d", count, index); */
1627 if (!dc || !port)
1628 return -ENODEV;
1630 if (unlikely(!mutex_trylock(&port->tty_sem))) {
1632 * must test lock as tty layer wraps calls
1633 * to this function with BKL
1635 dev_err(&dc->pdev->dev, "Would have deadlocked - "
1636 "return EAGAIN\n");
1637 return -EAGAIN;
1640 if (unlikely(!port->tty_open_count)) {
1641 DBG1(" ");
1642 goto exit;
1645 rval = __kfifo_put(port->fifo_ul, (unsigned char *)buffer, count);
1647 /* notify card */
1648 if (unlikely(dc == NULL)) {
1649 DBG1("No device context?");
1650 goto exit;
1653 spin_lock_irqsave(&dc->spin_mutex, flags);
1654 /* CTS is only valid on the modem channel */
1655 if (port == &(dc->port[PORT_MDM])) {
1656 if (port->ctrl_dl.CTS) {
1657 DBG4("Enable interrupt");
1658 enable_transmit_ul(tty->index % MAX_PORT, dc);
1659 } else {
1660 dev_err(&dc->pdev->dev,
1661 "CTS not active on modem port?\n");
1663 } else {
1664 enable_transmit_ul(tty->index % MAX_PORT, dc);
1666 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1668 exit:
1669 mutex_unlock(&port->tty_sem);
1670 return rval;
1674 * Calculate how much is left in device
1675 * This method is called by the upper tty layer.
1676 * #according to sources N_TTY.c it expects a value >= 0 and
1677 * does not check for negative values.
1679 static int ntty_write_room(struct tty_struct *tty)
1681 struct port *port = tty->driver_data;
1682 int room = 0;
1683 const struct nozomi *dc = get_dc_by_tty(tty);
1685 if (!dc || !port)
1686 return 0;
1687 if (!mutex_trylock(&port->tty_sem))
1688 return 0;
1690 if (!port->tty_open_count)
1691 goto exit;
1693 room = port->fifo_ul->size - __kfifo_len(port->fifo_ul);
1695 exit:
1696 mutex_unlock(&port->tty_sem);
1697 return room;
1700 /* Gets io control parameters */
1701 static int ntty_tiocmget(struct tty_struct *tty, struct file *file)
1703 const struct port *port = tty->driver_data;
1704 const struct ctrl_dl *ctrl_dl = &port->ctrl_dl;
1705 const struct ctrl_ul *ctrl_ul = &port->ctrl_ul;
1707 return (ctrl_ul->RTS ? TIOCM_RTS : 0) |
1708 (ctrl_ul->DTR ? TIOCM_DTR : 0) |
1709 (ctrl_dl->DCD ? TIOCM_CAR : 0) |
1710 (ctrl_dl->RI ? TIOCM_RNG : 0) |
1711 (ctrl_dl->DSR ? TIOCM_DSR : 0) |
1712 (ctrl_dl->CTS ? TIOCM_CTS : 0);
1715 /* Sets io controls parameters */
1716 static int ntty_tiocmset(struct tty_struct *tty, struct file *file,
1717 unsigned int set, unsigned int clear)
1719 if (set & TIOCM_RTS)
1720 set_rts(tty, 1);
1721 else if (clear & TIOCM_RTS)
1722 set_rts(tty, 0);
1724 if (set & TIOCM_DTR)
1725 set_dtr(tty, 1);
1726 else if (clear & TIOCM_DTR)
1727 set_dtr(tty, 0);
1729 return 0;
1732 static int ntty_cflags_changed(struct port *port, unsigned long flags,
1733 struct async_icount *cprev)
1735 const struct async_icount cnow = port->tty_icount;
1736 int ret;
1738 ret = ((flags & TIOCM_RNG) && (cnow.rng != cprev->rng)) ||
1739 ((flags & TIOCM_DSR) && (cnow.dsr != cprev->dsr)) ||
1740 ((flags & TIOCM_CD) && (cnow.dcd != cprev->dcd)) ||
1741 ((flags & TIOCM_CTS) && (cnow.cts != cprev->cts));
1743 *cprev = cnow;
1745 return ret;
1748 static int ntty_ioctl_tiocgicount(struct port *port, void __user *argp)
1750 const struct async_icount cnow = port->tty_icount;
1751 struct serial_icounter_struct icount;
1753 icount.cts = cnow.cts;
1754 icount.dsr = cnow.dsr;
1755 icount.rng = cnow.rng;
1756 icount.dcd = cnow.dcd;
1757 icount.rx = cnow.rx;
1758 icount.tx = cnow.tx;
1759 icount.frame = cnow.frame;
1760 icount.overrun = cnow.overrun;
1761 icount.parity = cnow.parity;
1762 icount.brk = cnow.brk;
1763 icount.buf_overrun = cnow.buf_overrun;
1765 return copy_to_user(argp, &icount, sizeof(icount));
1768 static int ntty_ioctl(struct tty_struct *tty, struct file *file,
1769 unsigned int cmd, unsigned long arg)
1771 struct port *port = tty->driver_data;
1772 void __user *argp = (void __user *)arg;
1773 int rval = -ENOIOCTLCMD;
1775 DBG1("******** IOCTL, cmd: %d", cmd);
1777 switch (cmd) {
1778 case TIOCMIWAIT: {
1779 struct async_icount cprev = port->tty_icount;
1781 rval = wait_event_interruptible(port->tty_wait,
1782 ntty_cflags_changed(port, arg, &cprev));
1783 break;
1784 } case TIOCGICOUNT:
1785 rval = ntty_ioctl_tiocgicount(port, argp);
1786 break;
1787 default:
1788 DBG1("ERR: 0x%08X, %d", cmd, cmd);
1789 break;
1792 return rval;
1796 * Called by the upper tty layer when tty buffers are ready
1797 * to receive data again after a call to throttle.
1799 static void ntty_unthrottle(struct tty_struct *tty)
1801 struct nozomi *dc = get_dc_by_tty(tty);
1802 unsigned long flags;
1804 DBG1("UNTHROTTLE");
1805 spin_lock_irqsave(&dc->spin_mutex, flags);
1806 enable_transmit_dl(tty->index % MAX_PORT, dc);
1807 set_rts(tty, 1);
1809 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1813 * Called by the upper tty layer when the tty buffers are almost full.
1814 * The driver should stop send more data.
1816 static void ntty_throttle(struct tty_struct *tty)
1818 struct nozomi *dc = get_dc_by_tty(tty);
1819 unsigned long flags;
1821 DBG1("THROTTLE");
1822 spin_lock_irqsave(&dc->spin_mutex, flags);
1823 set_rts(tty, 0);
1824 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1827 /* just to discard single character writes */
1828 static void ntty_put_char(struct tty_struct *tty, unsigned char c)
1831 * card does not react correct when we write single chars
1832 * to the card, so we discard them
1834 DBG2("PUT CHAR Function: %c", c);
1837 /* Returns number of chars in buffer, called by tty layer */
1838 static s32 ntty_chars_in_buffer(struct tty_struct *tty)
1840 struct port *port = tty->driver_data;
1841 struct nozomi *dc = get_dc_by_tty(tty);
1842 s32 rval;
1844 if (unlikely(!dc || !port)) {
1845 rval = -ENODEV;
1846 goto exit_in_buffer;
1849 if (unlikely(!port->tty_open_count)) {
1850 dev_err(&dc->pdev->dev, "No tty open?\n");
1851 rval = -ENODEV;
1852 goto exit_in_buffer;
1855 rval = __kfifo_len(port->fifo_ul);
1857 exit_in_buffer:
1858 return rval;
1861 static const struct tty_operations tty_ops = {
1862 .ioctl = ntty_ioctl,
1863 .open = ntty_open,
1864 .close = ntty_close,
1865 .write = ntty_write,
1866 .write_room = ntty_write_room,
1867 .unthrottle = ntty_unthrottle,
1868 .throttle = ntty_throttle,
1869 .chars_in_buffer = ntty_chars_in_buffer,
1870 .put_char = ntty_put_char,
1871 .tiocmget = ntty_tiocmget,
1872 .tiocmset = ntty_tiocmset,
1875 /* Module initialization */
1876 static struct pci_driver nozomi_driver = {
1877 .name = NOZOMI_NAME,
1878 .id_table = nozomi_pci_tbl,
1879 .probe = nozomi_card_init,
1880 .remove = __devexit_p(nozomi_card_exit),
1883 static __init int nozomi_init(void)
1885 int ret;
1887 printk(KERN_INFO "Initializing %s\n", VERSION_STRING);
1889 ntty_driver = alloc_tty_driver(NTTY_TTY_MAXMINORS);
1890 if (!ntty_driver)
1891 return -ENOMEM;
1893 ntty_driver->owner = THIS_MODULE;
1894 ntty_driver->driver_name = NOZOMI_NAME_TTY;
1895 ntty_driver->name = "noz";
1896 ntty_driver->major = 0;
1897 ntty_driver->type = TTY_DRIVER_TYPE_SERIAL;
1898 ntty_driver->subtype = SERIAL_TYPE_NORMAL;
1899 ntty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
1900 ntty_driver->init_termios = tty_std_termios;
1901 ntty_driver->init_termios.c_cflag = B115200 | CS8 | CREAD | \
1902 HUPCL | CLOCAL;
1903 ntty_driver->init_termios.c_ispeed = 115200;
1904 ntty_driver->init_termios.c_ospeed = 115200;
1905 tty_set_operations(ntty_driver, &tty_ops);
1907 ret = tty_register_driver(ntty_driver);
1908 if (ret) {
1909 printk(KERN_ERR "Nozomi: failed to register ntty driver\n");
1910 goto free_tty;
1913 ret = pci_register_driver(&nozomi_driver);
1914 if (ret) {
1915 printk(KERN_ERR "Nozomi: can't register pci driver\n");
1916 goto unr_tty;
1919 return 0;
1920 unr_tty:
1921 tty_unregister_driver(ntty_driver);
1922 free_tty:
1923 put_tty_driver(ntty_driver);
1924 return ret;
1927 static __exit void nozomi_exit(void)
1929 printk(KERN_INFO "Unloading %s\n", DRIVER_DESC);
1930 pci_unregister_driver(&nozomi_driver);
1931 tty_unregister_driver(ntty_driver);
1932 put_tty_driver(ntty_driver);
1935 module_init(nozomi_init);
1936 module_exit(nozomi_exit);
1938 module_param(debug, int, S_IRUGO | S_IWUSR);
1940 MODULE_LICENSE("Dual BSD/GPL");
1941 MODULE_DESCRIPTION(DRIVER_DESC);