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