First Support on Ginger and OMAP TI
[linux-ginger.git] / drivers / char / nozomi.c
blobd3400b20444f4f726b7de49e6ff88a5aa78cc938
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 <asm/byteorder.h>
60 #include <linux/delay.h>
63 #define VERSION_STRING DRIVER_DESC " 2.1d (build date: " \
64 __DATE__ " " __TIME__ ")"
66 /* Macros definitions */
68 /* Default debug printout level */
69 #define NOZOMI_DEBUG_LEVEL 0x00
71 #define P_BUF_SIZE 128
72 #define NFO(_err_flag_, args...) \
73 do { \
74 char tmp[P_BUF_SIZE]; \
75 snprintf(tmp, sizeof(tmp), ##args); \
76 printk(_err_flag_ "[%d] %s(): %s\n", __LINE__, \
77 __func__, tmp); \
78 } while (0)
80 #define DBG1(args...) D_(0x01, ##args)
81 #define DBG2(args...) D_(0x02, ##args)
82 #define DBG3(args...) D_(0x04, ##args)
83 #define DBG4(args...) D_(0x08, ##args)
84 #define DBG5(args...) D_(0x10, ##args)
85 #define DBG6(args...) D_(0x20, ##args)
86 #define DBG7(args...) D_(0x40, ##args)
87 #define DBG8(args...) D_(0x80, ##args)
89 #ifdef DEBUG
90 /* Do we need this settable at runtime? */
91 static int debug = NOZOMI_DEBUG_LEVEL;
93 #define D(lvl, args...) do \
94 {if (lvl & debug) NFO(KERN_DEBUG, ##args); } \
95 while (0)
96 #define D_(lvl, args...) D(lvl, ##args)
98 /* These printouts are always printed */
100 #else
101 static int debug;
102 #define D_(lvl, args...)
103 #endif
105 /* TODO: rewrite to optimize macros... */
107 #define TMP_BUF_MAX 256
109 #define DUMP(buf__,len__) \
110 do { \
111 char tbuf[TMP_BUF_MAX] = {0};\
112 if (len__ > 1) {\
113 snprintf(tbuf, len__ > TMP_BUF_MAX ? TMP_BUF_MAX : len__, "%s", buf__);\
114 if (tbuf[len__-2] == '\r') {\
115 tbuf[len__-2] = 'r';\
117 DBG1("SENDING: '%s' (%d+n)", tbuf, len__);\
118 } else {\
119 DBG1("SENDING: '%s' (%d)", tbuf, len__);\
121 } while (0)
123 /* Defines */
124 #define NOZOMI_NAME "nozomi"
125 #define NOZOMI_NAME_TTY "nozomi_tty"
126 #define DRIVER_DESC "Nozomi driver"
128 #define NTTY_TTY_MAXMINORS 256
129 #define NTTY_FIFO_BUFFER_SIZE 8192
131 /* Must be power of 2 */
132 #define FIFO_BUFFER_SIZE_UL 8192
134 /* Size of tmp send buffer to card */
135 #define SEND_BUF_MAX 1024
136 #define RECEIVE_BUF_MAX 4
139 /* Define all types of vendors and devices to support */
140 #define VENDOR1 0x1931 /* Vendor Option */
141 #define DEVICE1 0x000c /* HSDPA card */
143 #define R_IIR 0x0000 /* Interrupt Identity Register */
144 #define R_FCR 0x0000 /* Flow Control Register */
145 #define R_IER 0x0004 /* Interrupt Enable Register */
147 #define CONFIG_MAGIC 0xEFEFFEFE
148 #define TOGGLE_VALID 0x0000
150 /* Definition of interrupt tokens */
151 #define MDM_DL1 0x0001
152 #define MDM_UL1 0x0002
153 #define MDM_DL2 0x0004
154 #define MDM_UL2 0x0008
155 #define DIAG_DL1 0x0010
156 #define DIAG_DL2 0x0020
157 #define DIAG_UL 0x0040
158 #define APP1_DL 0x0080
159 #define APP1_UL 0x0100
160 #define APP2_DL 0x0200
161 #define APP2_UL 0x0400
162 #define CTRL_DL 0x0800
163 #define CTRL_UL 0x1000
164 #define RESET 0x8000
166 #define MDM_DL (MDM_DL1 | MDM_DL2)
167 #define MDM_UL (MDM_UL1 | MDM_UL2)
168 #define DIAG_DL (DIAG_DL1 | DIAG_DL2)
170 /* modem signal definition */
171 #define CTRL_DSR 0x0001
172 #define CTRL_DCD 0x0002
173 #define CTRL_RI 0x0004
174 #define CTRL_CTS 0x0008
176 #define CTRL_DTR 0x0001
177 #define CTRL_RTS 0x0002
179 #define MAX_PORT 4
180 #define NOZOMI_MAX_PORTS 5
181 #define NOZOMI_MAX_CARDS (NTTY_TTY_MAXMINORS / MAX_PORT)
183 /* Type definitions */
186 * There are two types of nozomi cards,
187 * one with 2048 memory and with 8192 memory
189 enum card_type {
190 F32_2 = 2048, /* 512 bytes downlink + uplink * 2 -> 2048 */
191 F32_8 = 8192, /* 3072 bytes downl. + 1024 bytes uplink * 2 -> 8192 */
194 /* Initialization states a card can be in */
195 enum card_state {
196 NOZOMI_STATE_UKNOWN = 0,
197 NOZOMI_STATE_ENABLED = 1, /* pci device enabled */
198 NOZOMI_STATE_ALLOCATED = 2, /* config setup done */
199 NOZOMI_STATE_READY = 3, /* flowcontrols received */
202 /* Two different toggle channels exist */
203 enum channel_type {
204 CH_A = 0,
205 CH_B = 1,
208 /* Port definition for the card regarding flow control */
209 enum ctrl_port_type {
210 CTRL_CMD = 0,
211 CTRL_MDM = 1,
212 CTRL_DIAG = 2,
213 CTRL_APP1 = 3,
214 CTRL_APP2 = 4,
215 CTRL_ERROR = -1,
218 /* Ports that the nozomi has */
219 enum port_type {
220 PORT_MDM = 0,
221 PORT_DIAG = 1,
222 PORT_APP1 = 2,
223 PORT_APP2 = 3,
224 PORT_CTRL = 4,
225 PORT_ERROR = -1,
228 #ifdef __BIG_ENDIAN
229 /* Big endian */
231 struct toggles {
232 unsigned int enabled:5; /*
233 * Toggle fields are valid if enabled is 0,
234 * else A-channels must always be used.
236 unsigned int diag_dl:1;
237 unsigned int mdm_dl:1;
238 unsigned int mdm_ul:1;
239 } __attribute__ ((packed));
241 /* Configuration table to read at startup of card */
242 /* Is for now only needed during initialization phase */
243 struct config_table {
244 u32 signature;
245 u16 product_information;
246 u16 version;
247 u8 pad3[3];
248 struct toggles toggle;
249 u8 pad1[4];
250 u16 dl_mdm_len1; /*
251 * If this is 64, it can hold
252 * 60 bytes + 4 that is length field
254 u16 dl_start;
256 u16 dl_diag_len1;
257 u16 dl_mdm_len2; /*
258 * If this is 64, it can hold
259 * 60 bytes + 4 that is length field
261 u16 dl_app1_len;
263 u16 dl_diag_len2;
264 u16 dl_ctrl_len;
265 u16 dl_app2_len;
266 u8 pad2[16];
267 u16 ul_mdm_len1;
268 u16 ul_start;
269 u16 ul_diag_len;
270 u16 ul_mdm_len2;
271 u16 ul_app1_len;
272 u16 ul_app2_len;
273 u16 ul_ctrl_len;
274 } __attribute__ ((packed));
276 /* This stores all control downlink flags */
277 struct ctrl_dl {
278 u8 port;
279 unsigned int reserved:4;
280 unsigned int CTS:1;
281 unsigned int RI:1;
282 unsigned int DCD:1;
283 unsigned int DSR:1;
284 } __attribute__ ((packed));
286 /* This stores all control uplink flags */
287 struct ctrl_ul {
288 u8 port;
289 unsigned int reserved:6;
290 unsigned int RTS:1;
291 unsigned int DTR:1;
292 } __attribute__ ((packed));
294 #else
295 /* Little endian */
297 /* This represents the toggle information */
298 struct toggles {
299 unsigned int mdm_ul:1;
300 unsigned int mdm_dl:1;
301 unsigned int diag_dl:1;
302 unsigned int enabled:5; /*
303 * Toggle fields are valid if enabled is 0,
304 * else A-channels must always be used.
306 } __attribute__ ((packed));
308 /* Configuration table to read at startup of card */
309 struct config_table {
310 u32 signature;
311 u16 version;
312 u16 product_information;
313 struct toggles toggle;
314 u8 pad1[7];
315 u16 dl_start;
316 u16 dl_mdm_len1; /*
317 * If this is 64, it can hold
318 * 60 bytes + 4 that is length field
320 u16 dl_mdm_len2;
321 u16 dl_diag_len1;
322 u16 dl_diag_len2;
323 u16 dl_app1_len;
324 u16 dl_app2_len;
325 u16 dl_ctrl_len;
326 u8 pad2[16];
327 u16 ul_start;
328 u16 ul_mdm_len2;
329 u16 ul_mdm_len1;
330 u16 ul_diag_len;
331 u16 ul_app1_len;
332 u16 ul_app2_len;
333 u16 ul_ctrl_len;
334 } __attribute__ ((packed));
336 /* This stores all control downlink flags */
337 struct ctrl_dl {
338 unsigned int DSR:1;
339 unsigned int DCD:1;
340 unsigned int RI:1;
341 unsigned int CTS:1;
342 unsigned int reserverd:4;
343 u8 port;
344 } __attribute__ ((packed));
346 /* This stores all control uplink flags */
347 struct ctrl_ul {
348 unsigned int DTR:1;
349 unsigned int RTS:1;
350 unsigned int reserved:6;
351 u8 port;
352 } __attribute__ ((packed));
353 #endif
355 /* This holds all information that is needed regarding a port */
356 struct port {
357 struct tty_port port;
358 u8 update_flow_control;
359 struct ctrl_ul ctrl_ul;
360 struct ctrl_dl ctrl_dl;
361 struct kfifo *fifo_ul;
362 void __iomem *dl_addr[2];
363 u32 dl_size[2];
364 u8 toggle_dl;
365 void __iomem *ul_addr[2];
366 u32 ul_size[2];
367 u8 toggle_ul;
368 u16 token_dl;
370 /* mutex to ensure one access patch to this port */
371 struct mutex tty_sem;
372 wait_queue_head_t tty_wait;
373 struct async_icount tty_icount;
376 /* Private data one for each card in the system */
377 struct nozomi {
378 void __iomem *base_addr;
379 unsigned long flip;
381 /* Pointers to registers */
382 void __iomem *reg_iir;
383 void __iomem *reg_fcr;
384 void __iomem *reg_ier;
386 u16 last_ier;
387 enum card_type card_type;
388 struct config_table config_table; /* Configuration table */
389 struct pci_dev *pdev;
390 struct port port[NOZOMI_MAX_PORTS];
391 u8 *send_buf;
393 spinlock_t spin_mutex; /* secures access to registers and tty */
395 unsigned int index_start;
396 enum card_state state;
397 u32 open_ttys;
400 /* This is a data packet that is read or written to/from card */
401 struct buffer {
402 u32 size; /* size is the length of the data buffer */
403 u8 *data;
404 } __attribute__ ((packed));
406 /* Global variables */
407 static const struct pci_device_id nozomi_pci_tbl[] __devinitconst = {
408 {PCI_DEVICE(VENDOR1, DEVICE1)},
412 MODULE_DEVICE_TABLE(pci, nozomi_pci_tbl);
414 static struct nozomi *ndevs[NOZOMI_MAX_CARDS];
415 static struct tty_driver *ntty_driver;
418 * find card by tty_index
420 static inline struct nozomi *get_dc_by_tty(const struct tty_struct *tty)
422 return tty ? ndevs[tty->index / MAX_PORT] : NULL;
425 static inline struct port *get_port_by_tty(const struct tty_struct *tty)
427 struct nozomi *ndev = get_dc_by_tty(tty);
428 return ndev ? &ndev->port[tty->index % MAX_PORT] : NULL;
432 * TODO:
433 * -Optimize
434 * -Rewrite cleaner
437 static void read_mem32(u32 *buf, const void __iomem *mem_addr_start,
438 u32 size_bytes)
440 u32 i = 0;
441 const u32 __iomem *ptr = mem_addr_start;
442 u16 *buf16;
444 if (unlikely(!ptr || !buf))
445 goto out;
447 /* shortcut for extremely often used cases */
448 switch (size_bytes) {
449 case 2: /* 2 bytes */
450 buf16 = (u16 *) buf;
451 *buf16 = __le16_to_cpu(readw(ptr));
452 goto out;
453 break;
454 case 4: /* 4 bytes */
455 *(buf) = __le32_to_cpu(readl(ptr));
456 goto out;
457 break;
460 while (i < size_bytes) {
461 if (size_bytes - i == 2) {
462 /* Handle 2 bytes in the end */
463 buf16 = (u16 *) buf;
464 *(buf16) = __le16_to_cpu(readw(ptr));
465 i += 2;
466 } else {
467 /* Read 4 bytes */
468 *(buf) = __le32_to_cpu(readl(ptr));
469 i += 4;
471 buf++;
472 ptr++;
474 out:
475 return;
479 * TODO:
480 * -Optimize
481 * -Rewrite cleaner
483 static u32 write_mem32(void __iomem *mem_addr_start, const u32 *buf,
484 u32 size_bytes)
486 u32 i = 0;
487 u32 __iomem *ptr = mem_addr_start;
488 const u16 *buf16;
490 if (unlikely(!ptr || !buf))
491 return 0;
493 /* shortcut for extremely often used cases */
494 switch (size_bytes) {
495 case 2: /* 2 bytes */
496 buf16 = (const u16 *)buf;
497 writew(__cpu_to_le16(*buf16), ptr);
498 return 2;
499 break;
500 case 1: /*
501 * also needs to write 4 bytes in this case
502 * so falling through..
504 case 4: /* 4 bytes */
505 writel(__cpu_to_le32(*buf), ptr);
506 return 4;
507 break;
510 while (i < size_bytes) {
511 if (size_bytes - i == 2) {
512 /* 2 bytes */
513 buf16 = (const u16 *)buf;
514 writew(__cpu_to_le16(*buf16), ptr);
515 i += 2;
516 } else {
517 /* 4 bytes */
518 writel(__cpu_to_le32(*buf), ptr);
519 i += 4;
521 buf++;
522 ptr++;
524 return i;
527 /* Setup pointers to different channels and also setup buffer sizes. */
528 static void setup_memory(struct nozomi *dc)
530 void __iomem *offset = dc->base_addr + dc->config_table.dl_start;
531 /* The length reported is including the length field of 4 bytes,
532 * hence subtract with 4.
534 const u16 buff_offset = 4;
536 /* Modem port dl configuration */
537 dc->port[PORT_MDM].dl_addr[CH_A] = offset;
538 dc->port[PORT_MDM].dl_addr[CH_B] =
539 (offset += dc->config_table.dl_mdm_len1);
540 dc->port[PORT_MDM].dl_size[CH_A] =
541 dc->config_table.dl_mdm_len1 - buff_offset;
542 dc->port[PORT_MDM].dl_size[CH_B] =
543 dc->config_table.dl_mdm_len2 - buff_offset;
545 /* Diag port dl configuration */
546 dc->port[PORT_DIAG].dl_addr[CH_A] =
547 (offset += dc->config_table.dl_mdm_len2);
548 dc->port[PORT_DIAG].dl_size[CH_A] =
549 dc->config_table.dl_diag_len1 - buff_offset;
550 dc->port[PORT_DIAG].dl_addr[CH_B] =
551 (offset += dc->config_table.dl_diag_len1);
552 dc->port[PORT_DIAG].dl_size[CH_B] =
553 dc->config_table.dl_diag_len2 - buff_offset;
555 /* App1 port dl configuration */
556 dc->port[PORT_APP1].dl_addr[CH_A] =
557 (offset += dc->config_table.dl_diag_len2);
558 dc->port[PORT_APP1].dl_size[CH_A] =
559 dc->config_table.dl_app1_len - buff_offset;
561 /* App2 port dl configuration */
562 dc->port[PORT_APP2].dl_addr[CH_A] =
563 (offset += dc->config_table.dl_app1_len);
564 dc->port[PORT_APP2].dl_size[CH_A] =
565 dc->config_table.dl_app2_len - buff_offset;
567 /* Ctrl dl configuration */
568 dc->port[PORT_CTRL].dl_addr[CH_A] =
569 (offset += dc->config_table.dl_app2_len);
570 dc->port[PORT_CTRL].dl_size[CH_A] =
571 dc->config_table.dl_ctrl_len - buff_offset;
573 offset = dc->base_addr + dc->config_table.ul_start;
575 /* Modem Port ul configuration */
576 dc->port[PORT_MDM].ul_addr[CH_A] = offset;
577 dc->port[PORT_MDM].ul_size[CH_A] =
578 dc->config_table.ul_mdm_len1 - buff_offset;
579 dc->port[PORT_MDM].ul_addr[CH_B] =
580 (offset += dc->config_table.ul_mdm_len1);
581 dc->port[PORT_MDM].ul_size[CH_B] =
582 dc->config_table.ul_mdm_len2 - buff_offset;
584 /* Diag port ul configuration */
585 dc->port[PORT_DIAG].ul_addr[CH_A] =
586 (offset += dc->config_table.ul_mdm_len2);
587 dc->port[PORT_DIAG].ul_size[CH_A] =
588 dc->config_table.ul_diag_len - buff_offset;
590 /* App1 port ul configuration */
591 dc->port[PORT_APP1].ul_addr[CH_A] =
592 (offset += dc->config_table.ul_diag_len);
593 dc->port[PORT_APP1].ul_size[CH_A] =
594 dc->config_table.ul_app1_len - buff_offset;
596 /* App2 port ul configuration */
597 dc->port[PORT_APP2].ul_addr[CH_A] =
598 (offset += dc->config_table.ul_app1_len);
599 dc->port[PORT_APP2].ul_size[CH_A] =
600 dc->config_table.ul_app2_len - buff_offset;
602 /* Ctrl ul configuration */
603 dc->port[PORT_CTRL].ul_addr[CH_A] =
604 (offset += dc->config_table.ul_app2_len);
605 dc->port[PORT_CTRL].ul_size[CH_A] =
606 dc->config_table.ul_ctrl_len - buff_offset;
609 /* Dump config table under initalization phase */
610 #ifdef DEBUG
611 static void dump_table(const struct nozomi *dc)
613 DBG3("signature: 0x%08X", dc->config_table.signature);
614 DBG3("version: 0x%04X", dc->config_table.version);
615 DBG3("product_information: 0x%04X", \
616 dc->config_table.product_information);
617 DBG3("toggle enabled: %d", dc->config_table.toggle.enabled);
618 DBG3("toggle up_mdm: %d", dc->config_table.toggle.mdm_ul);
619 DBG3("toggle dl_mdm: %d", dc->config_table.toggle.mdm_dl);
620 DBG3("toggle dl_dbg: %d", dc->config_table.toggle.diag_dl);
622 DBG3("dl_start: 0x%04X", dc->config_table.dl_start);
623 DBG3("dl_mdm_len0: 0x%04X, %d", dc->config_table.dl_mdm_len1,
624 dc->config_table.dl_mdm_len1);
625 DBG3("dl_mdm_len1: 0x%04X, %d", dc->config_table.dl_mdm_len2,
626 dc->config_table.dl_mdm_len2);
627 DBG3("dl_diag_len0: 0x%04X, %d", dc->config_table.dl_diag_len1,
628 dc->config_table.dl_diag_len1);
629 DBG3("dl_diag_len1: 0x%04X, %d", dc->config_table.dl_diag_len2,
630 dc->config_table.dl_diag_len2);
631 DBG3("dl_app1_len: 0x%04X, %d", dc->config_table.dl_app1_len,
632 dc->config_table.dl_app1_len);
633 DBG3("dl_app2_len: 0x%04X, %d", dc->config_table.dl_app2_len,
634 dc->config_table.dl_app2_len);
635 DBG3("dl_ctrl_len: 0x%04X, %d", dc->config_table.dl_ctrl_len,
636 dc->config_table.dl_ctrl_len);
637 DBG3("ul_start: 0x%04X, %d", dc->config_table.ul_start,
638 dc->config_table.ul_start);
639 DBG3("ul_mdm_len[0]: 0x%04X, %d", dc->config_table.ul_mdm_len1,
640 dc->config_table.ul_mdm_len1);
641 DBG3("ul_mdm_len[1]: 0x%04X, %d", dc->config_table.ul_mdm_len2,
642 dc->config_table.ul_mdm_len2);
643 DBG3("ul_diag_len: 0x%04X, %d", dc->config_table.ul_diag_len,
644 dc->config_table.ul_diag_len);
645 DBG3("ul_app1_len: 0x%04X, %d", dc->config_table.ul_app1_len,
646 dc->config_table.ul_app1_len);
647 DBG3("ul_app2_len: 0x%04X, %d", dc->config_table.ul_app2_len,
648 dc->config_table.ul_app2_len);
649 DBG3("ul_ctrl_len: 0x%04X, %d", dc->config_table.ul_ctrl_len,
650 dc->config_table.ul_ctrl_len);
652 #else
653 static inline void dump_table(const struct nozomi *dc) { }
654 #endif
657 * Read configuration table from card under intalization phase
658 * Returns 1 if ok, else 0
660 static int nozomi_read_config_table(struct nozomi *dc)
662 read_mem32((u32 *) &dc->config_table, dc->base_addr + 0,
663 sizeof(struct config_table));
665 if (dc->config_table.signature != CONFIG_MAGIC) {
666 dev_err(&dc->pdev->dev, "ConfigTable Bad! 0x%08X != 0x%08X\n",
667 dc->config_table.signature, CONFIG_MAGIC);
668 return 0;
671 if ((dc->config_table.version == 0)
672 || (dc->config_table.toggle.enabled == TOGGLE_VALID)) {
673 int i;
674 DBG1("Second phase, configuring card");
676 setup_memory(dc);
678 dc->port[PORT_MDM].toggle_ul = dc->config_table.toggle.mdm_ul;
679 dc->port[PORT_MDM].toggle_dl = dc->config_table.toggle.mdm_dl;
680 dc->port[PORT_DIAG].toggle_dl = dc->config_table.toggle.diag_dl;
681 DBG1("toggle ports: MDM UL:%d MDM DL:%d, DIAG DL:%d",
682 dc->port[PORT_MDM].toggle_ul,
683 dc->port[PORT_MDM].toggle_dl, dc->port[PORT_DIAG].toggle_dl);
685 dump_table(dc);
687 for (i = PORT_MDM; i < MAX_PORT; i++) {
688 dc->port[i].fifo_ul =
689 kfifo_alloc(FIFO_BUFFER_SIZE_UL, GFP_ATOMIC, NULL);
690 memset(&dc->port[i].ctrl_dl, 0, sizeof(struct ctrl_dl));
691 memset(&dc->port[i].ctrl_ul, 0, sizeof(struct ctrl_ul));
694 /* Enable control channel */
695 dc->last_ier = dc->last_ier | CTRL_DL;
696 writew(dc->last_ier, dc->reg_ier);
698 dc->state = NOZOMI_STATE_ALLOCATED;
699 dev_info(&dc->pdev->dev, "Initialization OK!\n");
700 return 1;
703 if ((dc->config_table.version > 0)
704 && (dc->config_table.toggle.enabled != TOGGLE_VALID)) {
705 u32 offset = 0;
706 DBG1("First phase: pushing upload buffers, clearing download");
708 dev_info(&dc->pdev->dev, "Version of card: %d\n",
709 dc->config_table.version);
711 /* Here we should disable all I/O over F32. */
712 setup_memory(dc);
715 * We should send ALL channel pair tokens back along
716 * with reset token
719 /* push upload modem buffers */
720 write_mem32(dc->port[PORT_MDM].ul_addr[CH_A],
721 (u32 *) &offset, 4);
722 write_mem32(dc->port[PORT_MDM].ul_addr[CH_B],
723 (u32 *) &offset, 4);
725 writew(MDM_UL | DIAG_DL | MDM_DL, dc->reg_fcr);
727 DBG1("First phase done");
730 return 1;
733 /* Enable uplink interrupts */
734 static void enable_transmit_ul(enum port_type port, struct nozomi *dc)
736 static const u16 mask[] = {MDM_UL, DIAG_UL, APP1_UL, APP2_UL, CTRL_UL};
738 if (port < NOZOMI_MAX_PORTS) {
739 dc->last_ier |= mask[port];
740 writew(dc->last_ier, dc->reg_ier);
741 } else {
742 dev_err(&dc->pdev->dev, "Called with wrong port?\n");
746 /* Disable uplink interrupts */
747 static void disable_transmit_ul(enum port_type port, struct nozomi *dc)
749 static const u16 mask[] =
750 {~MDM_UL, ~DIAG_UL, ~APP1_UL, ~APP2_UL, ~CTRL_UL};
752 if (port < NOZOMI_MAX_PORTS) {
753 dc->last_ier &= mask[port];
754 writew(dc->last_ier, dc->reg_ier);
755 } else {
756 dev_err(&dc->pdev->dev, "Called with wrong port?\n");
760 /* Enable downlink interrupts */
761 static void enable_transmit_dl(enum port_type port, struct nozomi *dc)
763 static const u16 mask[] = {MDM_DL, DIAG_DL, APP1_DL, APP2_DL, CTRL_DL};
765 if (port < NOZOMI_MAX_PORTS) {
766 dc->last_ier |= mask[port];
767 writew(dc->last_ier, dc->reg_ier);
768 } else {
769 dev_err(&dc->pdev->dev, "Called with wrong port?\n");
773 /* Disable downlink interrupts */
774 static void disable_transmit_dl(enum port_type port, struct nozomi *dc)
776 static const u16 mask[] =
777 {~MDM_DL, ~DIAG_DL, ~APP1_DL, ~APP2_DL, ~CTRL_DL};
779 if (port < NOZOMI_MAX_PORTS) {
780 dc->last_ier &= mask[port];
781 writew(dc->last_ier, dc->reg_ier);
782 } else {
783 dev_err(&dc->pdev->dev, "Called with wrong port?\n");
788 * Return 1 - send buffer to card and ack.
789 * Return 0 - don't ack, don't send buffer to card.
791 static int send_data(enum port_type index, struct nozomi *dc)
793 u32 size = 0;
794 struct port *port = &dc->port[index];
795 const u8 toggle = port->toggle_ul;
796 void __iomem *addr = port->ul_addr[toggle];
797 const u32 ul_size = port->ul_size[toggle];
798 struct tty_struct *tty = tty_port_tty_get(&port->port);
800 /* Get data from tty and place in buf for now */
801 size = __kfifo_get(port->fifo_ul, dc->send_buf,
802 ul_size < SEND_BUF_MAX ? ul_size : SEND_BUF_MAX);
804 if (size == 0) {
805 DBG4("No more data to send, disable link:");
806 tty_kref_put(tty);
807 return 0;
810 /* DUMP(buf, size); */
812 /* Write length + data */
813 write_mem32(addr, (u32 *) &size, 4);
814 write_mem32(addr + 4, (u32 *) dc->send_buf, size);
816 if (tty)
817 tty_wakeup(tty);
819 tty_kref_put(tty);
820 return 1;
823 /* If all data has been read, return 1, else 0 */
824 static int receive_data(enum port_type index, struct nozomi *dc)
826 u8 buf[RECEIVE_BUF_MAX] = { 0 };
827 int size;
828 u32 offset = 4;
829 struct port *port = &dc->port[index];
830 void __iomem *addr = port->dl_addr[port->toggle_dl];
831 struct tty_struct *tty = tty_port_tty_get(&port->port);
832 int i, ret;
834 if (unlikely(!tty)) {
835 DBG1("tty not open for port: %d?", index);
836 return 1;
839 read_mem32((u32 *) &size, addr, 4);
840 /* DBG1( "%d bytes port: %d", size, index); */
842 if (test_bit(TTY_THROTTLED, &tty->flags)) {
843 DBG1("No room in tty, don't read data, don't ack interrupt, "
844 "disable interrupt");
846 /* disable interrupt in downlink... */
847 disable_transmit_dl(index, dc);
848 ret = 0;
849 goto put;
852 if (unlikely(size == 0)) {
853 dev_err(&dc->pdev->dev, "size == 0?\n");
854 ret = 1;
855 goto put;
858 tty_buffer_request_room(tty, size);
860 while (size > 0) {
861 read_mem32((u32 *) buf, addr + offset, RECEIVE_BUF_MAX);
863 if (size == 1) {
864 tty_insert_flip_char(tty, buf[0], TTY_NORMAL);
865 size = 0;
866 } else if (size < RECEIVE_BUF_MAX) {
867 size -= tty_insert_flip_string(tty, (char *) buf, size);
868 } else {
869 i = tty_insert_flip_string(tty, \
870 (char *) buf, RECEIVE_BUF_MAX);
871 size -= i;
872 offset += i;
876 set_bit(index, &dc->flip);
877 ret = 1;
878 put:
879 tty_kref_put(tty);
880 return ret;
883 /* Debug for interrupts */
884 #ifdef DEBUG
885 static char *interrupt2str(u16 interrupt)
887 static char buf[TMP_BUF_MAX];
888 char *p = buf;
890 interrupt & MDM_DL1 ? p += snprintf(p, TMP_BUF_MAX, "MDM_DL1 ") : NULL;
891 interrupt & MDM_DL2 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
892 "MDM_DL2 ") : NULL;
894 interrupt & MDM_UL1 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
895 "MDM_UL1 ") : NULL;
896 interrupt & MDM_UL2 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
897 "MDM_UL2 ") : NULL;
899 interrupt & DIAG_DL1 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
900 "DIAG_DL1 ") : NULL;
901 interrupt & DIAG_DL2 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
902 "DIAG_DL2 ") : NULL;
904 interrupt & DIAG_UL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
905 "DIAG_UL ") : NULL;
907 interrupt & APP1_DL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
908 "APP1_DL ") : NULL;
909 interrupt & APP2_DL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
910 "APP2_DL ") : NULL;
912 interrupt & APP1_UL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
913 "APP1_UL ") : NULL;
914 interrupt & APP2_UL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
915 "APP2_UL ") : NULL;
917 interrupt & CTRL_DL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
918 "CTRL_DL ") : NULL;
919 interrupt & CTRL_UL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
920 "CTRL_UL ") : NULL;
922 interrupt & RESET ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
923 "RESET ") : NULL;
925 return buf;
927 #endif
930 * Receive flow control
931 * Return 1 - If ok, else 0
933 static int receive_flow_control(struct nozomi *dc)
935 enum port_type port = PORT_MDM;
936 struct ctrl_dl ctrl_dl;
937 struct ctrl_dl old_ctrl;
938 u16 enable_ier = 0;
940 read_mem32((u32 *) &ctrl_dl, dc->port[PORT_CTRL].dl_addr[CH_A], 2);
942 switch (ctrl_dl.port) {
943 case CTRL_CMD:
944 DBG1("The Base Band sends this value as a response to a "
945 "request for IMSI detach sent over the control "
946 "channel uplink (see section 7.6.1).");
947 break;
948 case CTRL_MDM:
949 port = PORT_MDM;
950 enable_ier = MDM_DL;
951 break;
952 case CTRL_DIAG:
953 port = PORT_DIAG;
954 enable_ier = DIAG_DL;
955 break;
956 case CTRL_APP1:
957 port = PORT_APP1;
958 enable_ier = APP1_DL;
959 break;
960 case CTRL_APP2:
961 port = PORT_APP2;
962 enable_ier = APP2_DL;
963 if (dc->state == NOZOMI_STATE_ALLOCATED) {
965 * After card initialization the flow control
966 * received for APP2 is always the last
968 dc->state = NOZOMI_STATE_READY;
969 dev_info(&dc->pdev->dev, "Device READY!\n");
971 break;
972 default:
973 dev_err(&dc->pdev->dev,
974 "ERROR: flow control received for non-existing port\n");
975 return 0;
978 DBG1("0x%04X->0x%04X", *((u16 *)&dc->port[port].ctrl_dl),
979 *((u16 *)&ctrl_dl));
981 old_ctrl = dc->port[port].ctrl_dl;
982 dc->port[port].ctrl_dl = ctrl_dl;
984 if (old_ctrl.CTS == 1 && ctrl_dl.CTS == 0) {
985 DBG1("Disable interrupt (0x%04X) on port: %d",
986 enable_ier, port);
987 disable_transmit_ul(port, dc);
989 } else if (old_ctrl.CTS == 0 && ctrl_dl.CTS == 1) {
991 if (__kfifo_len(dc->port[port].fifo_ul)) {
992 DBG1("Enable interrupt (0x%04X) on port: %d",
993 enable_ier, port);
994 DBG1("Data in buffer [%d], enable transmit! ",
995 __kfifo_len(dc->port[port].fifo_ul));
996 enable_transmit_ul(port, dc);
997 } else {
998 DBG1("No data in buffer...");
1002 if (*(u16 *)&old_ctrl == *(u16 *)&ctrl_dl) {
1003 DBG1(" No change in mctrl");
1004 return 1;
1006 /* Update statistics */
1007 if (old_ctrl.CTS != ctrl_dl.CTS)
1008 dc->port[port].tty_icount.cts++;
1009 if (old_ctrl.DSR != ctrl_dl.DSR)
1010 dc->port[port].tty_icount.dsr++;
1011 if (old_ctrl.RI != ctrl_dl.RI)
1012 dc->port[port].tty_icount.rng++;
1013 if (old_ctrl.DCD != ctrl_dl.DCD)
1014 dc->port[port].tty_icount.dcd++;
1016 wake_up_interruptible(&dc->port[port].tty_wait);
1018 DBG1("port: %d DCD(%d), CTS(%d), RI(%d), DSR(%d)",
1019 port,
1020 dc->port[port].tty_icount.dcd, dc->port[port].tty_icount.cts,
1021 dc->port[port].tty_icount.rng, dc->port[port].tty_icount.dsr);
1023 return 1;
1026 static enum ctrl_port_type port2ctrl(enum port_type port,
1027 const struct nozomi *dc)
1029 switch (port) {
1030 case PORT_MDM:
1031 return CTRL_MDM;
1032 case PORT_DIAG:
1033 return CTRL_DIAG;
1034 case PORT_APP1:
1035 return CTRL_APP1;
1036 case PORT_APP2:
1037 return CTRL_APP2;
1038 default:
1039 dev_err(&dc->pdev->dev,
1040 "ERROR: send flow control " \
1041 "received for non-existing port\n");
1043 return CTRL_ERROR;
1047 * Send flow control, can only update one channel at a time
1048 * Return 0 - If we have updated all flow control
1049 * Return 1 - If we need to update more flow control, ack current enable more
1051 static int send_flow_control(struct nozomi *dc)
1053 u32 i, more_flow_control_to_be_updated = 0;
1054 u16 *ctrl;
1056 for (i = PORT_MDM; i < MAX_PORT; i++) {
1057 if (dc->port[i].update_flow_control) {
1058 if (more_flow_control_to_be_updated) {
1059 /* We have more flow control to be updated */
1060 return 1;
1062 dc->port[i].ctrl_ul.port = port2ctrl(i, dc);
1063 ctrl = (u16 *)&dc->port[i].ctrl_ul;
1064 write_mem32(dc->port[PORT_CTRL].ul_addr[0], \
1065 (u32 *) ctrl, 2);
1066 dc->port[i].update_flow_control = 0;
1067 more_flow_control_to_be_updated = 1;
1070 return 0;
1074 * Handle downlink data, ports that are handled are modem and diagnostics
1075 * Return 1 - ok
1076 * Return 0 - toggle fields are out of sync
1078 static int handle_data_dl(struct nozomi *dc, enum port_type port, u8 *toggle,
1079 u16 read_iir, u16 mask1, u16 mask2)
1081 if (*toggle == 0 && read_iir & mask1) {
1082 if (receive_data(port, dc)) {
1083 writew(mask1, dc->reg_fcr);
1084 *toggle = !(*toggle);
1087 if (read_iir & mask2) {
1088 if (receive_data(port, dc)) {
1089 writew(mask2, dc->reg_fcr);
1090 *toggle = !(*toggle);
1093 } else if (*toggle == 1 && read_iir & mask2) {
1094 if (receive_data(port, dc)) {
1095 writew(mask2, dc->reg_fcr);
1096 *toggle = !(*toggle);
1099 if (read_iir & mask1) {
1100 if (receive_data(port, dc)) {
1101 writew(mask1, dc->reg_fcr);
1102 *toggle = !(*toggle);
1105 } else {
1106 dev_err(&dc->pdev->dev, "port out of sync!, toggle:%d\n",
1107 *toggle);
1108 return 0;
1110 return 1;
1114 * Handle uplink data, this is currently for the modem port
1115 * Return 1 - ok
1116 * Return 0 - toggle field are out of sync
1118 static int handle_data_ul(struct nozomi *dc, enum port_type port, u16 read_iir)
1120 u8 *toggle = &(dc->port[port].toggle_ul);
1122 if (*toggle == 0 && read_iir & MDM_UL1) {
1123 dc->last_ier &= ~MDM_UL;
1124 writew(dc->last_ier, dc->reg_ier);
1125 if (send_data(port, dc)) {
1126 writew(MDM_UL1, dc->reg_fcr);
1127 dc->last_ier = dc->last_ier | MDM_UL;
1128 writew(dc->last_ier, dc->reg_ier);
1129 *toggle = !*toggle;
1132 if (read_iir & MDM_UL2) {
1133 dc->last_ier &= ~MDM_UL;
1134 writew(dc->last_ier, dc->reg_ier);
1135 if (send_data(port, dc)) {
1136 writew(MDM_UL2, dc->reg_fcr);
1137 dc->last_ier = dc->last_ier | MDM_UL;
1138 writew(dc->last_ier, dc->reg_ier);
1139 *toggle = !*toggle;
1143 } else if (*toggle == 1 && read_iir & MDM_UL2) {
1144 dc->last_ier &= ~MDM_UL;
1145 writew(dc->last_ier, dc->reg_ier);
1146 if (send_data(port, dc)) {
1147 writew(MDM_UL2, dc->reg_fcr);
1148 dc->last_ier = dc->last_ier | MDM_UL;
1149 writew(dc->last_ier, dc->reg_ier);
1150 *toggle = !*toggle;
1153 if (read_iir & MDM_UL1) {
1154 dc->last_ier &= ~MDM_UL;
1155 writew(dc->last_ier, dc->reg_ier);
1156 if (send_data(port, dc)) {
1157 writew(MDM_UL1, dc->reg_fcr);
1158 dc->last_ier = dc->last_ier | MDM_UL;
1159 writew(dc->last_ier, dc->reg_ier);
1160 *toggle = !*toggle;
1163 } else {
1164 writew(read_iir & MDM_UL, dc->reg_fcr);
1165 dev_err(&dc->pdev->dev, "port out of sync!\n");
1166 return 0;
1168 return 1;
1171 static irqreturn_t interrupt_handler(int irq, void *dev_id)
1173 struct nozomi *dc = dev_id;
1174 unsigned int a;
1175 u16 read_iir;
1177 if (!dc)
1178 return IRQ_NONE;
1180 spin_lock(&dc->spin_mutex);
1181 read_iir = readw(dc->reg_iir);
1183 /* Card removed */
1184 if (read_iir == (u16)-1)
1185 goto none;
1187 * Just handle interrupt enabled in IER
1188 * (by masking with dc->last_ier)
1190 read_iir &= dc->last_ier;
1192 if (read_iir == 0)
1193 goto none;
1196 DBG4("%s irq:0x%04X, prev:0x%04X", interrupt2str(read_iir), read_iir,
1197 dc->last_ier);
1199 if (read_iir & RESET) {
1200 if (unlikely(!nozomi_read_config_table(dc))) {
1201 dc->last_ier = 0x0;
1202 writew(dc->last_ier, dc->reg_ier);
1203 dev_err(&dc->pdev->dev, "Could not read status from "
1204 "card, we should disable interface\n");
1205 } else {
1206 writew(RESET, dc->reg_fcr);
1208 /* No more useful info if this was the reset interrupt. */
1209 goto exit_handler;
1211 if (read_iir & CTRL_UL) {
1212 DBG1("CTRL_UL");
1213 dc->last_ier &= ~CTRL_UL;
1214 writew(dc->last_ier, dc->reg_ier);
1215 if (send_flow_control(dc)) {
1216 writew(CTRL_UL, dc->reg_fcr);
1217 dc->last_ier = dc->last_ier | CTRL_UL;
1218 writew(dc->last_ier, dc->reg_ier);
1221 if (read_iir & CTRL_DL) {
1222 receive_flow_control(dc);
1223 writew(CTRL_DL, dc->reg_fcr);
1225 if (read_iir & MDM_DL) {
1226 if (!handle_data_dl(dc, PORT_MDM,
1227 &(dc->port[PORT_MDM].toggle_dl), read_iir,
1228 MDM_DL1, MDM_DL2)) {
1229 dev_err(&dc->pdev->dev, "MDM_DL out of sync!\n");
1230 goto exit_handler;
1233 if (read_iir & MDM_UL) {
1234 if (!handle_data_ul(dc, PORT_MDM, read_iir)) {
1235 dev_err(&dc->pdev->dev, "MDM_UL out of sync!\n");
1236 goto exit_handler;
1239 if (read_iir & DIAG_DL) {
1240 if (!handle_data_dl(dc, PORT_DIAG,
1241 &(dc->port[PORT_DIAG].toggle_dl), read_iir,
1242 DIAG_DL1, DIAG_DL2)) {
1243 dev_err(&dc->pdev->dev, "DIAG_DL out of sync!\n");
1244 goto exit_handler;
1247 if (read_iir & DIAG_UL) {
1248 dc->last_ier &= ~DIAG_UL;
1249 writew(dc->last_ier, dc->reg_ier);
1250 if (send_data(PORT_DIAG, dc)) {
1251 writew(DIAG_UL, dc->reg_fcr);
1252 dc->last_ier = dc->last_ier | DIAG_UL;
1253 writew(dc->last_ier, dc->reg_ier);
1256 if (read_iir & APP1_DL) {
1257 if (receive_data(PORT_APP1, dc))
1258 writew(APP1_DL, dc->reg_fcr);
1260 if (read_iir & APP1_UL) {
1261 dc->last_ier &= ~APP1_UL;
1262 writew(dc->last_ier, dc->reg_ier);
1263 if (send_data(PORT_APP1, dc)) {
1264 writew(APP1_UL, dc->reg_fcr);
1265 dc->last_ier = dc->last_ier | APP1_UL;
1266 writew(dc->last_ier, dc->reg_ier);
1269 if (read_iir & APP2_DL) {
1270 if (receive_data(PORT_APP2, dc))
1271 writew(APP2_DL, dc->reg_fcr);
1273 if (read_iir & APP2_UL) {
1274 dc->last_ier &= ~APP2_UL;
1275 writew(dc->last_ier, dc->reg_ier);
1276 if (send_data(PORT_APP2, dc)) {
1277 writew(APP2_UL, dc->reg_fcr);
1278 dc->last_ier = dc->last_ier | APP2_UL;
1279 writew(dc->last_ier, dc->reg_ier);
1283 exit_handler:
1284 spin_unlock(&dc->spin_mutex);
1285 for (a = 0; a < NOZOMI_MAX_PORTS; a++) {
1286 struct tty_struct *tty;
1287 if (test_and_clear_bit(a, &dc->flip)) {
1288 tty = tty_port_tty_get(&dc->port[a].port);
1289 if (tty)
1290 tty_flip_buffer_push(tty);
1291 tty_kref_put(tty);
1294 return IRQ_HANDLED;
1295 none:
1296 spin_unlock(&dc->spin_mutex);
1297 return IRQ_NONE;
1300 static void nozomi_get_card_type(struct nozomi *dc)
1302 int i;
1303 u32 size = 0;
1305 for (i = 0; i < 6; i++)
1306 size += pci_resource_len(dc->pdev, i);
1308 /* Assume card type F32_8 if no match */
1309 dc->card_type = size == 2048 ? F32_2 : F32_8;
1311 dev_info(&dc->pdev->dev, "Card type is: %d\n", dc->card_type);
1314 static void nozomi_setup_private_data(struct nozomi *dc)
1316 void __iomem *offset = dc->base_addr + dc->card_type / 2;
1317 unsigned int i;
1319 dc->reg_fcr = (void __iomem *)(offset + R_FCR);
1320 dc->reg_iir = (void __iomem *)(offset + R_IIR);
1321 dc->reg_ier = (void __iomem *)(offset + R_IER);
1322 dc->last_ier = 0;
1323 dc->flip = 0;
1325 dc->port[PORT_MDM].token_dl = MDM_DL;
1326 dc->port[PORT_DIAG].token_dl = DIAG_DL;
1327 dc->port[PORT_APP1].token_dl = APP1_DL;
1328 dc->port[PORT_APP2].token_dl = APP2_DL;
1330 for (i = 0; i < MAX_PORT; i++)
1331 init_waitqueue_head(&dc->port[i].tty_wait);
1334 static ssize_t card_type_show(struct device *dev, struct device_attribute *attr,
1335 char *buf)
1337 const struct nozomi *dc = pci_get_drvdata(to_pci_dev(dev));
1339 return sprintf(buf, "%d\n", dc->card_type);
1341 static DEVICE_ATTR(card_type, S_IRUGO, card_type_show, NULL);
1343 static ssize_t open_ttys_show(struct device *dev, struct device_attribute *attr,
1344 char *buf)
1346 const struct nozomi *dc = pci_get_drvdata(to_pci_dev(dev));
1348 return sprintf(buf, "%u\n", dc->open_ttys);
1350 static DEVICE_ATTR(open_ttys, S_IRUGO, open_ttys_show, NULL);
1352 static void make_sysfs_files(struct nozomi *dc)
1354 if (device_create_file(&dc->pdev->dev, &dev_attr_card_type))
1355 dev_err(&dc->pdev->dev,
1356 "Could not create sysfs file for card_type\n");
1357 if (device_create_file(&dc->pdev->dev, &dev_attr_open_ttys))
1358 dev_err(&dc->pdev->dev,
1359 "Could not create sysfs file for open_ttys\n");
1362 static void remove_sysfs_files(struct nozomi *dc)
1364 device_remove_file(&dc->pdev->dev, &dev_attr_card_type);
1365 device_remove_file(&dc->pdev->dev, &dev_attr_open_ttys);
1368 /* Allocate memory for one device */
1369 static int __devinit nozomi_card_init(struct pci_dev *pdev,
1370 const struct pci_device_id *ent)
1372 resource_size_t start;
1373 int ret;
1374 struct nozomi *dc = NULL;
1375 int ndev_idx;
1376 int i;
1378 dev_dbg(&pdev->dev, "Init, new card found\n");
1380 for (ndev_idx = 0; ndev_idx < ARRAY_SIZE(ndevs); ndev_idx++)
1381 if (!ndevs[ndev_idx])
1382 break;
1384 if (ndev_idx >= ARRAY_SIZE(ndevs)) {
1385 dev_err(&pdev->dev, "no free tty range for this card left\n");
1386 ret = -EIO;
1387 goto err;
1390 dc = kzalloc(sizeof(struct nozomi), GFP_KERNEL);
1391 if (unlikely(!dc)) {
1392 dev_err(&pdev->dev, "Could not allocate memory\n");
1393 ret = -ENOMEM;
1394 goto err_free;
1397 dc->pdev = pdev;
1399 ret = pci_enable_device(dc->pdev);
1400 if (ret) {
1401 dev_err(&pdev->dev, "Failed to enable PCI Device\n");
1402 goto err_free;
1405 ret = pci_request_regions(dc->pdev, NOZOMI_NAME);
1406 if (ret) {
1407 dev_err(&pdev->dev, "I/O address 0x%04x already in use\n",
1408 (int) /* nozomi_private.io_addr */ 0);
1409 goto err_disable_device;
1412 start = pci_resource_start(dc->pdev, 0);
1413 if (start == 0) {
1414 dev_err(&pdev->dev, "No I/O address for card detected\n");
1415 ret = -ENODEV;
1416 goto err_rel_regs;
1419 /* Find out what card type it is */
1420 nozomi_get_card_type(dc);
1422 dc->base_addr = ioremap_nocache(start, dc->card_type);
1423 if (!dc->base_addr) {
1424 dev_err(&pdev->dev, "Unable to map card MMIO\n");
1425 ret = -ENODEV;
1426 goto err_rel_regs;
1429 dc->send_buf = kmalloc(SEND_BUF_MAX, GFP_KERNEL);
1430 if (!dc->send_buf) {
1431 dev_err(&pdev->dev, "Could not allocate send buffer?\n");
1432 ret = -ENOMEM;
1433 goto err_free_sbuf;
1436 spin_lock_init(&dc->spin_mutex);
1438 nozomi_setup_private_data(dc);
1440 /* Disable all interrupts */
1441 dc->last_ier = 0;
1442 writew(dc->last_ier, dc->reg_ier);
1444 ret = request_irq(pdev->irq, &interrupt_handler, IRQF_SHARED,
1445 NOZOMI_NAME, dc);
1446 if (unlikely(ret)) {
1447 dev_err(&pdev->dev, "can't request irq %d\n", pdev->irq);
1448 goto err_free_sbuf;
1451 DBG1("base_addr: %p", dc->base_addr);
1453 make_sysfs_files(dc);
1455 dc->index_start = ndev_idx * MAX_PORT;
1456 ndevs[ndev_idx] = dc;
1458 pci_set_drvdata(pdev, dc);
1460 /* Enable RESET interrupt */
1461 dc->last_ier = RESET;
1462 iowrite16(dc->last_ier, dc->reg_ier);
1464 dc->state = NOZOMI_STATE_ENABLED;
1466 for (i = 0; i < MAX_PORT; i++) {
1467 mutex_init(&dc->port[i].tty_sem);
1468 tty_port_init(&dc->port[i].port);
1469 tty_register_device(ntty_driver, dc->index_start + i,
1470 &pdev->dev);
1472 return 0;
1474 err_free_sbuf:
1475 kfree(dc->send_buf);
1476 iounmap(dc->base_addr);
1477 err_rel_regs:
1478 pci_release_regions(pdev);
1479 err_disable_device:
1480 pci_disable_device(pdev);
1481 err_free:
1482 kfree(dc);
1483 err:
1484 return ret;
1487 static void __devexit tty_exit(struct nozomi *dc)
1489 unsigned int i;
1491 DBG1(" ");
1493 flush_scheduled_work();
1495 for (i = 0; i < MAX_PORT; ++i) {
1496 struct tty_struct *tty = tty_port_tty_get(&dc->port[i].port);
1497 if (tty && list_empty(&tty->hangup_work.entry))
1498 tty_hangup(tty);
1499 tty_kref_put(tty);
1501 /* Racy below - surely should wait for scheduled work to be done or
1502 complete off a hangup method ? */
1503 while (dc->open_ttys)
1504 msleep(1);
1505 for (i = dc->index_start; i < dc->index_start + MAX_PORT; ++i)
1506 tty_unregister_device(ntty_driver, i);
1509 /* Deallocate memory for one device */
1510 static void __devexit nozomi_card_exit(struct pci_dev *pdev)
1512 int i;
1513 struct ctrl_ul ctrl;
1514 struct nozomi *dc = pci_get_drvdata(pdev);
1516 /* Disable all interrupts */
1517 dc->last_ier = 0;
1518 writew(dc->last_ier, dc->reg_ier);
1520 tty_exit(dc);
1522 /* Send 0x0001, command card to resend the reset token. */
1523 /* This is to get the reset when the module is reloaded. */
1524 ctrl.port = 0x00;
1525 ctrl.reserved = 0;
1526 ctrl.RTS = 0;
1527 ctrl.DTR = 1;
1528 DBG1("sending flow control 0x%04X", *((u16 *)&ctrl));
1530 /* Setup dc->reg addresses to we can use defines here */
1531 write_mem32(dc->port[PORT_CTRL].ul_addr[0], (u32 *)&ctrl, 2);
1532 writew(CTRL_UL, dc->reg_fcr); /* push the token to the card. */
1534 remove_sysfs_files(dc);
1536 free_irq(pdev->irq, dc);
1538 for (i = 0; i < MAX_PORT; i++)
1539 if (dc->port[i].fifo_ul)
1540 kfifo_free(dc->port[i].fifo_ul);
1542 kfree(dc->send_buf);
1544 iounmap(dc->base_addr);
1546 pci_release_regions(pdev);
1548 pci_disable_device(pdev);
1550 ndevs[dc->index_start / MAX_PORT] = NULL;
1552 kfree(dc);
1555 static void set_rts(const struct tty_struct *tty, int rts)
1557 struct port *port = get_port_by_tty(tty);
1559 port->ctrl_ul.RTS = rts;
1560 port->update_flow_control = 1;
1561 enable_transmit_ul(PORT_CTRL, get_dc_by_tty(tty));
1564 static void set_dtr(const struct tty_struct *tty, int dtr)
1566 struct port *port = get_port_by_tty(tty);
1568 DBG1("SETTING DTR index: %d, dtr: %d", tty->index, dtr);
1570 port->ctrl_ul.DTR = dtr;
1571 port->update_flow_control = 1;
1572 enable_transmit_ul(PORT_CTRL, get_dc_by_tty(tty));
1576 * ----------------------------------------------------------------------------
1577 * TTY code
1578 * ----------------------------------------------------------------------------
1581 /* Called when the userspace process opens the tty, /dev/noz*. */
1582 static int ntty_open(struct tty_struct *tty, struct file *file)
1584 struct port *port = get_port_by_tty(tty);
1585 struct nozomi *dc = get_dc_by_tty(tty);
1586 unsigned long flags;
1588 if (!port || !dc || dc->state != NOZOMI_STATE_READY)
1589 return -ENODEV;
1591 if (mutex_lock_interruptible(&port->tty_sem))
1592 return -ERESTARTSYS;
1594 port->port.count++;
1595 dc->open_ttys++;
1597 /* Enable interrupt downlink for channel */
1598 if (port->port.count == 1) {
1599 tty->driver_data = port;
1600 tty_port_tty_set(&port->port, tty);
1601 DBG1("open: %d", port->token_dl);
1602 spin_lock_irqsave(&dc->spin_mutex, flags);
1603 dc->last_ier = dc->last_ier | port->token_dl;
1604 writew(dc->last_ier, dc->reg_ier);
1605 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1607 mutex_unlock(&port->tty_sem);
1608 return 0;
1611 /* Called when the userspace process close the tty, /dev/noz*. Also
1612 called immediately if ntty_open fails in which case tty->driver_data
1613 will be NULL an we exit by the first return */
1615 static void ntty_close(struct tty_struct *tty, struct file *file)
1617 struct nozomi *dc = get_dc_by_tty(tty);
1618 struct port *nport = tty->driver_data;
1619 struct tty_port *port = &nport->port;
1620 unsigned long flags;
1622 if (!dc || !nport)
1623 return;
1625 /* Users cannot interrupt a close */
1626 mutex_lock(&nport->tty_sem);
1628 WARN_ON(!port->count);
1630 dc->open_ttys--;
1631 port->count--;
1632 tty_port_tty_set(port, NULL);
1634 if (port->count == 0) {
1635 DBG1("close: %d", nport->token_dl);
1636 spin_lock_irqsave(&dc->spin_mutex, flags);
1637 dc->last_ier &= ~(nport->token_dl);
1638 writew(dc->last_ier, dc->reg_ier);
1639 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1641 mutex_unlock(&nport->tty_sem);
1645 * called when the userspace process writes to the tty (/dev/noz*).
1646 * Data is inserted into a fifo, which is then read and transfered to the modem.
1648 static int ntty_write(struct tty_struct *tty, const unsigned char *buffer,
1649 int count)
1651 int rval = -EINVAL;
1652 struct nozomi *dc = get_dc_by_tty(tty);
1653 struct port *port = tty->driver_data;
1654 unsigned long flags;
1656 /* DBG1( "WRITEx: %d, index = %d", count, index); */
1658 if (!dc || !port)
1659 return -ENODEV;
1661 if (unlikely(!mutex_trylock(&port->tty_sem))) {
1663 * must test lock as tty layer wraps calls
1664 * to this function with BKL
1666 dev_err(&dc->pdev->dev, "Would have deadlocked - "
1667 "return EAGAIN\n");
1668 return -EAGAIN;
1671 if (unlikely(!port->port.count)) {
1672 DBG1(" ");
1673 goto exit;
1676 rval = __kfifo_put(port->fifo_ul, (unsigned char *)buffer, count);
1678 /* notify card */
1679 if (unlikely(dc == NULL)) {
1680 DBG1("No device context?");
1681 goto exit;
1684 spin_lock_irqsave(&dc->spin_mutex, flags);
1685 /* CTS is only valid on the modem channel */
1686 if (port == &(dc->port[PORT_MDM])) {
1687 if (port->ctrl_dl.CTS) {
1688 DBG4("Enable interrupt");
1689 enable_transmit_ul(tty->index % MAX_PORT, dc);
1690 } else {
1691 dev_err(&dc->pdev->dev,
1692 "CTS not active on modem port?\n");
1694 } else {
1695 enable_transmit_ul(tty->index % MAX_PORT, dc);
1697 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1699 exit:
1700 mutex_unlock(&port->tty_sem);
1701 return rval;
1705 * Calculate how much is left in device
1706 * This method is called by the upper tty layer.
1707 * #according to sources N_TTY.c it expects a value >= 0 and
1708 * does not check for negative values.
1710 static int ntty_write_room(struct tty_struct *tty)
1712 struct port *port = tty->driver_data;
1713 int room = 0;
1714 const struct nozomi *dc = get_dc_by_tty(tty);
1716 if (!dc || !port)
1717 return 0;
1718 if (!mutex_trylock(&port->tty_sem))
1719 return 0;
1721 if (!port->port.count)
1722 goto exit;
1724 room = port->fifo_ul->size - __kfifo_len(port->fifo_ul);
1726 exit:
1727 mutex_unlock(&port->tty_sem);
1728 return room;
1731 /* Gets io control parameters */
1732 static int ntty_tiocmget(struct tty_struct *tty, struct file *file)
1734 const struct port *port = tty->driver_data;
1735 const struct ctrl_dl *ctrl_dl = &port->ctrl_dl;
1736 const struct ctrl_ul *ctrl_ul = &port->ctrl_ul;
1738 /* Note: these could change under us but it is not clear this
1739 matters if so */
1740 return (ctrl_ul->RTS ? TIOCM_RTS : 0) |
1741 (ctrl_ul->DTR ? TIOCM_DTR : 0) |
1742 (ctrl_dl->DCD ? TIOCM_CAR : 0) |
1743 (ctrl_dl->RI ? TIOCM_RNG : 0) |
1744 (ctrl_dl->DSR ? TIOCM_DSR : 0) |
1745 (ctrl_dl->CTS ? TIOCM_CTS : 0);
1748 /* Sets io controls parameters */
1749 static int ntty_tiocmset(struct tty_struct *tty, struct file *file,
1750 unsigned int set, unsigned int clear)
1752 struct nozomi *dc = get_dc_by_tty(tty);
1753 unsigned long flags;
1755 spin_lock_irqsave(&dc->spin_mutex, flags);
1756 if (set & TIOCM_RTS)
1757 set_rts(tty, 1);
1758 else if (clear & TIOCM_RTS)
1759 set_rts(tty, 0);
1761 if (set & TIOCM_DTR)
1762 set_dtr(tty, 1);
1763 else if (clear & TIOCM_DTR)
1764 set_dtr(tty, 0);
1765 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1767 return 0;
1770 static int ntty_cflags_changed(struct port *port, unsigned long flags,
1771 struct async_icount *cprev)
1773 const struct async_icount cnow = port->tty_icount;
1774 int ret;
1776 ret = ((flags & TIOCM_RNG) && (cnow.rng != cprev->rng)) ||
1777 ((flags & TIOCM_DSR) && (cnow.dsr != cprev->dsr)) ||
1778 ((flags & TIOCM_CD) && (cnow.dcd != cprev->dcd)) ||
1779 ((flags & TIOCM_CTS) && (cnow.cts != cprev->cts));
1781 *cprev = cnow;
1783 return ret;
1786 static int ntty_ioctl_tiocgicount(struct port *port, void __user *argp)
1788 const struct async_icount cnow = port->tty_icount;
1789 struct serial_icounter_struct icount;
1791 icount.cts = cnow.cts;
1792 icount.dsr = cnow.dsr;
1793 icount.rng = cnow.rng;
1794 icount.dcd = cnow.dcd;
1795 icount.rx = cnow.rx;
1796 icount.tx = cnow.tx;
1797 icount.frame = cnow.frame;
1798 icount.overrun = cnow.overrun;
1799 icount.parity = cnow.parity;
1800 icount.brk = cnow.brk;
1801 icount.buf_overrun = cnow.buf_overrun;
1803 return copy_to_user(argp, &icount, sizeof(icount)) ? -EFAULT : 0;
1806 static int ntty_ioctl(struct tty_struct *tty, struct file *file,
1807 unsigned int cmd, unsigned long arg)
1809 struct port *port = tty->driver_data;
1810 void __user *argp = (void __user *)arg;
1811 int rval = -ENOIOCTLCMD;
1813 DBG1("******** IOCTL, cmd: %d", cmd);
1815 switch (cmd) {
1816 case TIOCMIWAIT: {
1817 struct async_icount cprev = port->tty_icount;
1819 rval = wait_event_interruptible(port->tty_wait,
1820 ntty_cflags_changed(port, arg, &cprev));
1821 break;
1822 } case TIOCGICOUNT:
1823 rval = ntty_ioctl_tiocgicount(port, argp);
1824 break;
1825 default:
1826 DBG1("ERR: 0x%08X, %d", cmd, cmd);
1827 break;
1830 return rval;
1834 * Called by the upper tty layer when tty buffers are ready
1835 * to receive data again after a call to throttle.
1837 static void ntty_unthrottle(struct tty_struct *tty)
1839 struct nozomi *dc = get_dc_by_tty(tty);
1840 unsigned long flags;
1842 DBG1("UNTHROTTLE");
1843 spin_lock_irqsave(&dc->spin_mutex, flags);
1844 enable_transmit_dl(tty->index % MAX_PORT, dc);
1845 set_rts(tty, 1);
1847 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1851 * Called by the upper tty layer when the tty buffers are almost full.
1852 * The driver should stop send more data.
1854 static void ntty_throttle(struct tty_struct *tty)
1856 struct nozomi *dc = get_dc_by_tty(tty);
1857 unsigned long flags;
1859 DBG1("THROTTLE");
1860 spin_lock_irqsave(&dc->spin_mutex, flags);
1861 set_rts(tty, 0);
1862 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1865 /* Returns number of chars in buffer, called by tty layer */
1866 static s32 ntty_chars_in_buffer(struct tty_struct *tty)
1868 struct port *port = tty->driver_data;
1869 struct nozomi *dc = get_dc_by_tty(tty);
1870 s32 rval = 0;
1872 if (unlikely(!dc || !port)) {
1873 goto exit_in_buffer;
1876 if (unlikely(!port->port.count)) {
1877 dev_err(&dc->pdev->dev, "No tty open?\n");
1878 goto exit_in_buffer;
1881 rval = __kfifo_len(port->fifo_ul);
1883 exit_in_buffer:
1884 return rval;
1887 static const struct tty_operations tty_ops = {
1888 .ioctl = ntty_ioctl,
1889 .open = ntty_open,
1890 .close = ntty_close,
1891 .write = ntty_write,
1892 .write_room = ntty_write_room,
1893 .unthrottle = ntty_unthrottle,
1894 .throttle = ntty_throttle,
1895 .chars_in_buffer = ntty_chars_in_buffer,
1896 .tiocmget = ntty_tiocmget,
1897 .tiocmset = ntty_tiocmset,
1900 /* Module initialization */
1901 static struct pci_driver nozomi_driver = {
1902 .name = NOZOMI_NAME,
1903 .id_table = nozomi_pci_tbl,
1904 .probe = nozomi_card_init,
1905 .remove = __devexit_p(nozomi_card_exit),
1908 static __init int nozomi_init(void)
1910 int ret;
1912 printk(KERN_INFO "Initializing %s\n", VERSION_STRING);
1914 ntty_driver = alloc_tty_driver(NTTY_TTY_MAXMINORS);
1915 if (!ntty_driver)
1916 return -ENOMEM;
1918 ntty_driver->owner = THIS_MODULE;
1919 ntty_driver->driver_name = NOZOMI_NAME_TTY;
1920 ntty_driver->name = "noz";
1921 ntty_driver->major = 0;
1922 ntty_driver->type = TTY_DRIVER_TYPE_SERIAL;
1923 ntty_driver->subtype = SERIAL_TYPE_NORMAL;
1924 ntty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
1925 ntty_driver->init_termios = tty_std_termios;
1926 ntty_driver->init_termios.c_cflag = B115200 | CS8 | CREAD | \
1927 HUPCL | CLOCAL;
1928 ntty_driver->init_termios.c_ispeed = 115200;
1929 ntty_driver->init_termios.c_ospeed = 115200;
1930 tty_set_operations(ntty_driver, &tty_ops);
1932 ret = tty_register_driver(ntty_driver);
1933 if (ret) {
1934 printk(KERN_ERR "Nozomi: failed to register ntty driver\n");
1935 goto free_tty;
1938 ret = pci_register_driver(&nozomi_driver);
1939 if (ret) {
1940 printk(KERN_ERR "Nozomi: can't register pci driver\n");
1941 goto unr_tty;
1944 return 0;
1945 unr_tty:
1946 tty_unregister_driver(ntty_driver);
1947 free_tty:
1948 put_tty_driver(ntty_driver);
1949 return ret;
1952 static __exit void nozomi_exit(void)
1954 printk(KERN_INFO "Unloading %s\n", DRIVER_DESC);
1955 pci_unregister_driver(&nozomi_driver);
1956 tty_unregister_driver(ntty_driver);
1957 put_tty_driver(ntty_driver);
1960 module_init(nozomi_init);
1961 module_exit(nozomi_exit);
1963 module_param(debug, int, S_IRUGO | S_IWUSR);
1965 MODULE_LICENSE("Dual BSD/GPL");
1966 MODULE_DESCRIPTION(DRIVER_DESC);