]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/char/nozomi.c
6076e662886a43fc3b863ab3bd6022e0e8041f0e
[linux-2.6-omap-h63xx.git] / drivers / char / nozomi.c
1 /*
2  * nozomi.c  -- HSDPA driver Broadband Wireless Data Card - Globe Trotter
3  *
4  * Written by: Ulf Jakobsson,
5  *             Jan �erfeldt,
6  *             Stefan Thomasson,
7  *
8  * Maintained by: Paul Hardwick (p.hardwick@option.com)
9  *
10  * Patches:
11  *          Locking code changes for Vodafone by Sphere Systems Ltd,
12  *                              Andrew Bird (ajb@spheresystems.co.uk )
13  *                              & Phil Sanderson
14  *
15  * Source has been ported from an implementation made by Filip Aben @ Option
16  *
17  * --------------------------------------------------------------------------
18  *
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.
23  *
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.
28  *
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.
33  *
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
37  *
38  * --------------------------------------------------------------------------
39  */
40
41 /*
42  * CHANGELOG
43  * Version 2.1d
44  * 11-November-2007 Jiri Slaby, Frank Seidel
45  * - Big rework of multicard support by Jiri
46  * - Major cleanups (semaphore to mutex, endianess, no major reservation)
47  * - Optimizations
48  *
49  * Version 2.1c
50  * 30-October-2007 Frank Seidel
51  * - Completed multicard support
52  * - Minor cleanups
53  *
54  * Version 2.1b
55  * 07-August-2007 Frank Seidel
56  * - Minor cleanups
57  * - theoretical multicard support
58  *
59  * Version 2.1
60  * 03-July-2006 Paul Hardwick
61  *
62  * - Stability Improvements. Incorporated spinlock wraps patch.
63  * - Updated for newer 2.6.14+ kernels (tty_buffer_request_room)
64  * - using __devexit macro for tty
65  *
66  *
67  * Version 2.0
68  * 08-feb-2006 15:34:10:Ulf
69  *
70  * -Fixed issue when not waking up line disipine layer, could probably result
71  *  in better uplink performance for 2.4.
72  *
73  * -Fixed issue with big endian during initalization, now proper toggle flags
74  *  are handled between preloader and maincode.
75  *
76  * -Fixed flow control issue.
77  *
78  * -Added support for setting DTR.
79  *
80  * -For 2.4 kernels, removing temporary buffer that's not needed.
81  *
82  * -Reading CTS only for modem port (only port that supports it).
83  *
84  * -Return 0 in write_room instead of netative value, it's not handled in
85  *  upper layer.
86  *
87  * --------------------------------------------------------------------------
88  * Version 1.0
89  *
90  * First version of driver, only tested with card of type F32_2.
91  * Works fine with 2.4 and 2.6 kernels.
92  * Driver also support big endian architecture.
93  */
94
95 /* Enable this to have a lot of debug printouts */
96 #define DEBUG
97
98 #include <linux/kernel.h>
99 #include <linux/module.h>
100 #include <linux/pci.h>
101 #include <linux/ioport.h>
102 #include <linux/tty.h>
103 #include <linux/tty_driver.h>
104 #include <linux/tty_flip.h>
105 #include <linux/serial.h>
106 #include <linux/interrupt.h>
107 #include <linux/kmod.h>
108 #include <linux/init.h>
109 #include <linux/kfifo.h>
110 #include <linux/uaccess.h>
111 #include <asm/byteorder.h>
112
113 #include <linux/delay.h>
114
115
116 #define VERSION_STRING DRIVER_DESC " 2.1d (build date: " \
117                                         __DATE__ " " __TIME__ ")"
118
119 /*    Macros definitions */
120
121 /* Default debug printout level */
122 #define NOZOMI_DEBUG_LEVEL 0x00
123
124 #define P_BUF_SIZE 128
125 #define NFO(_err_flag_, args...)                                \
126 do {                                                            \
127         char tmp[P_BUF_SIZE];                                   \
128         snprintf(tmp, sizeof(tmp), ##args);                     \
129         printk(_err_flag_ "[%d] %s(): %s\n", __LINE__,          \
130                 __FUNCTION__, tmp);                             \
131 } while (0)
132
133 #define DBG1(args...) D_(0x01, ##args)
134 #define DBG2(args...) D_(0x02, ##args)
135 #define DBG3(args...) D_(0x04, ##args)
136 #define DBG4(args...) D_(0x08, ##args)
137 #define DBG5(args...) D_(0x10, ##args)
138 #define DBG6(args...) D_(0x20, ##args)
139 #define DBG7(args...) D_(0x40, ##args)
140 #define DBG8(args...) D_(0x80, ##args)
141
142 #ifdef DEBUG
143 /* Do we need this settable at runtime? */
144 static int debug = NOZOMI_DEBUG_LEVEL;
145
146 #define D(lvl, args...)  do {if (lvl & debug) NFO(KERN_DEBUG, ##args); } \
147                                 while (0)
148 #define D_(lvl, args...) D(lvl, ##args)
149
150 /* These printouts are always printed */
151
152 #else
153 static int debug;
154 #define D_(lvl, args...)
155 #endif
156
157 /* TODO: rewrite to optimize macros... */
158
159 #define TMP_BUF_MAX 256
160
161 #define DUMP(buf__,len__) \
162   do {  \
163     char tbuf[TMP_BUF_MAX] = {0};\
164     if (len__ > 1) {\
165         snprintf(tbuf, len__ > TMP_BUF_MAX ? TMP_BUF_MAX : len__, "%s", buf__);\
166         if (tbuf[len__-2] == '\r') {\
167                 tbuf[len__-2] = 'r';\
168         } \
169         DBG1("SENDING: '%s' (%d+n)", tbuf, len__);\
170     } else {\
171         DBG1("SENDING: '%s' (%d)", tbuf, len__);\
172     } \
173 } while (0)
174
175 /*    Defines */
176 #define NOZOMI_NAME             "nozomi"
177 #define NOZOMI_NAME_TTY         "nozomi_tty"
178 #define DRIVER_DESC             "Nozomi driver"
179
180 #define NTTY_TTY_MAXMINORS      256
181 #define NTTY_FIFO_BUFFER_SIZE   8192
182
183 /* Must be power of 2 */
184 #define FIFO_BUFFER_SIZE_UL     8192
185
186 /* Size of tmp send buffer to card */
187 #define SEND_BUF_MAX            1024
188 #define RECEIVE_BUF_MAX         4
189
190
191 /* Define all types of vendors and devices to support */
192 #define VENDOR1         0x1931  /* Vendor Option */
193 #define DEVICE1         0x000c  /* HSDPA card */
194
195 #define R_IIR           0x0000  /* Interrupt Identity Register */
196 #define R_FCR           0x0000  /* Flow Control Register */
197 #define R_IER           0x0004  /* Interrupt Enable Register */
198
199 #define CONFIG_MAGIC    0xEFEFFEFE
200 #define TOGGLE_VALID    0x0000
201
202 /* Definition of interrupt tokens */
203 #define MDM_DL1         0x0001
204 #define MDM_UL1         0x0002
205 #define MDM_DL2         0x0004
206 #define MDM_UL2         0x0008
207 #define DIAG_DL1        0x0010
208 #define DIAG_DL2        0x0020
209 #define DIAG_UL         0x0040
210 #define APP1_DL         0x0080
211 #define APP1_UL         0x0100
212 #define APP2_DL         0x0200
213 #define APP2_UL         0x0400
214 #define CTRL_DL         0x0800
215 #define CTRL_UL         0x1000
216 #define RESET           0x8000
217
218 #define MDM_DL          (MDM_DL1  | MDM_DL2)
219 #define MDM_UL          (MDM_UL1  | MDM_UL2)
220 #define DIAG_DL         (DIAG_DL1 | DIAG_DL2)
221
222 /* modem signal definition */
223 #define CTRL_DSR        0x0001
224 #define CTRL_DCD        0x0002
225 #define CTRL_RI         0x0004
226 #define CTRL_CTS        0x0008
227
228 #define CTRL_DTR        0x0001
229 #define CTRL_RTS        0x0002
230
231 #define MAX_PORT                4
232 #define NOZOMI_MAX_PORTS        5
233 #define NOZOMI_MAX_CARDS        (NTTY_TTY_MAXMINORS / MAX_PORT)
234
235 /*    Type definitions */
236
237 /*
238  * There are two types of nozomi cards,
239  * one with 2048 memory and with 8192 memory
240  */
241 enum card_type {
242         F32_2 = 2048,   /* 512 bytes downlink + uplink * 2 -> 2048 */
243         F32_8 = 8192,   /* 3072 bytes downl. + 1024 bytes uplink * 2 -> 8192 */
244 };
245
246 /* Two different toggle channels exist */
247 enum channel_type {
248         CH_A = 0,
249         CH_B = 1,
250 };
251
252 /* Port definition for the card regarding flow control */
253 enum ctrl_port_type {
254         CTRL_CMD        = 0,
255         CTRL_MDM        = 1,
256         CTRL_DIAG       = 2,
257         CTRL_APP1       = 3,
258         CTRL_APP2       = 4,
259         CTRL_ERROR      = -1,
260 };
261
262 /* Ports that the nozomi has */
263 enum port_type {
264         PORT_MDM        = 0,
265         PORT_DIAG       = 1,
266         PORT_APP1       = 2,
267         PORT_APP2       = 3,
268         PORT_CTRL       = 4,
269         PORT_ERROR      = -1,
270 };
271
272 #ifdef __BIG_ENDIAN
273 /* Big endian */
274
275 struct toggles {
276         unsigned enabled:5;     /*
277                                  * Toggle fields are valid if enabled is 0,
278                                  * else A-channels must always be used.
279                                  */
280         unsigned diag_dl:1;
281         unsigned mdm_dl:1;
282         unsigned mdm_ul:1;
283 } __attribute__ ((packed));
284
285 /* Configuration table to read at startup of card */
286 /* Is for now only needed during initialization phase */
287 struct config_table {
288         u32 signature;
289         u16 product_information;
290         u16 version;
291         u8 pad3[3];
292         struct toggles toggle;
293         u8 pad1[4];
294         u16 dl_mdm_len1;        /*
295                                  * If this is 64, it can hold
296                                  * 60 bytes + 4 that is length field
297                                  */
298         u16 dl_start;
299
300         u16 dl_diag_len1;
301         u16 dl_mdm_len2;        /*
302                                  * If this is 64, it can hold
303                                  * 60 bytes + 4 that is length field
304                                  */
305         u16 dl_app1_len;
306
307         u16 dl_diag_len2;
308         u16 dl_ctrl_len;
309         u16 dl_app2_len;
310         u8 pad2[16];
311         u16 ul_mdm_len1;
312         u16 ul_start;
313         u16 ul_diag_len;
314         u16 ul_mdm_len2;
315         u16 ul_app1_len;
316         u16 ul_app2_len;
317         u16 ul_ctrl_len;
318 } __attribute__ ((packed));
319
320 /* This stores all control downlink flags */
321 struct ctrl_dl {
322         u8 port;
323         unsigned reserved:4;
324         unsigned CTS:1;
325         unsigned RI:1;
326         unsigned DCD:1;
327         unsigned DSR:1;
328 } __attribute__ ((packed));
329
330 /* This stores all control uplink flags */
331 struct ctrl_ul {
332         u8 port;
333         unsigned reserved:6;
334         unsigned RTS:1;
335         unsigned DTR:1;
336 } __attribute__ ((packed));
337
338 #else
339 /* Little endian */
340
341 /* This represents the toggle information */
342 struct toggles {
343         unsigned mdm_ul:1;
344         unsigned mdm_dl:1;
345         unsigned diag_dl:1;
346         unsigned enabled:5;     /*
347                                  * Toggle fields are valid if enabled is 0,
348                                  * else A-channels must always be used.
349                                  */
350 } __attribute__ ((packed));
351
352 /* Configuration table to read at startup of card */
353 struct config_table {
354         u32 signature;
355         u16 version;
356         u16 product_information;
357         struct toggles toggle;
358         u8 pad1[7];
359         u16 dl_start;
360         u16 dl_mdm_len1;        /*
361                                  * If this is 64, it can hold
362                                  * 60 bytes + 4 that is length field
363                                  */
364         u16 dl_mdm_len2;
365         u16 dl_diag_len1;
366         u16 dl_diag_len2;
367         u16 dl_app1_len;
368         u16 dl_app2_len;
369         u16 dl_ctrl_len;
370         u8 pad2[16];
371         u16 ul_start;
372         u16 ul_mdm_len2;
373         u16 ul_mdm_len1;
374         u16 ul_diag_len;
375         u16 ul_app1_len;
376         u16 ul_app2_len;
377         u16 ul_ctrl_len;
378 } __attribute__ ((packed));
379
380 /* This stores all control downlink flags */
381 struct ctrl_dl {
382         unsigned DSR:1;
383         unsigned DCD:1;
384         unsigned RI:1;
385         unsigned CTS:1;
386         unsigned reserverd:4;
387         u8 port;
388 } __attribute__ ((packed));
389
390 /* This stores all control uplink flags */
391 struct ctrl_ul {
392         unsigned DTR:1;
393         unsigned RTS:1;
394         unsigned reserved:6;
395         u8 port;
396 } __attribute__ ((packed));
397 #endif
398
399 /* This holds all information that is needed regarding a port */
400 struct port {
401         u8 update_flow_control;
402         struct ctrl_ul ctrl_ul;
403         struct ctrl_dl ctrl_dl;
404         struct kfifo *fifo_ul;
405         void __iomem *dl_addr[2];
406         u32 dl_size[2];
407         u8 toggle_dl;
408         void __iomem *ul_addr[2];
409         u32 ul_size[2];
410         u8 toggle_ul;
411         u16 token_dl;
412
413         struct tty_struct *tty;
414         int tty_open_count;
415         /* mutex to ensure one access patch to this port */
416         struct mutex tty_sem;
417         wait_queue_head_t tty_wait;
418         struct async_icount tty_icount;
419 };
420
421 /* Private data one for each card in the system */
422 struct nozomi {
423         void __iomem *base_addr;
424         unsigned long flip;
425
426         /* Pointers to registers */
427         void __iomem *reg_iir;
428         void __iomem *reg_fcr;
429         void __iomem *reg_ier;
430
431         u16 last_ier;
432         enum card_type card_type;
433         struct config_table config_table;       /* Configuration table */
434         struct pci_dev *pdev;
435         struct port port[NOZOMI_MAX_PORTS];
436         u8 *send_buf;
437
438         spinlock_t spin_mutex;  /* secures access to registers and tty */
439
440         unsigned int index_start;
441         u32 open_ttys;
442 };
443
444 /* This is a data packet that is read or written to/from card */
445 struct buffer {
446         u32 size;               /* size is the length of the data buffer */
447         u8 *data;
448 } __attribute__ ((packed));
449
450 /*    Global variables */
451 static struct pci_device_id nozomi_pci_tbl[] = {
452         {PCI_DEVICE(VENDOR1, DEVICE1)},
453         {},
454 };
455
456 MODULE_DEVICE_TABLE(pci, nozomi_pci_tbl);
457
458 static struct nozomi *ndevs[NOZOMI_MAX_CARDS];
459 static struct tty_driver *ntty_driver;
460
461 /*
462  * find card by tty_index
463  */
464 static inline struct nozomi *get_dc_by_tty(const struct tty_struct *tty)
465 {
466         return tty ? ndevs[tty->index / MAX_PORT] : NULL;
467 }
468
469 static inline struct port *get_port_by_tty(const struct tty_struct *tty)
470 {
471         struct nozomi *ndev = get_dc_by_tty(tty);
472         return ndev ? &ndev->port[tty->index % MAX_PORT] : NULL;
473 }
474
475 /*
476  * TODO:
477  * -Optimize
478  * -Rewrite cleaner
479  */
480
481 static void read_mem32(u32 *buf, const void __iomem *mem_addr_start,
482                         u32 size_bytes)
483 {
484         u32 i = 0;
485         const u32 *ptr = (__force u32 *) mem_addr_start;
486         u16 *buf16;
487
488         if (unlikely(!ptr || !buf))
489                 goto out;
490
491         /* shortcut for extremely often used cases */
492         switch (size_bytes) {
493         case 2: /* 2 bytes */
494                 buf16 = (u16 *) buf;
495                 *buf16 = __le16_to_cpu(readw((void __iomem *)ptr));
496                 goto out;
497                 break;
498         case 4: /* 4 bytes */
499                 *(buf) = __le32_to_cpu(readl((void __iomem *)ptr));
500                 goto out;
501                 break;
502         }
503
504         while (i < size_bytes) {
505                 if (size_bytes - i == 2) {
506                         /* Handle 2 bytes in the end */
507                         buf16 = (u16 *) buf;
508                         *(buf16) = __le16_to_cpu(readw((void __iomem *)ptr));
509                         i += 2;
510                 } else {
511                         /* Read 4 bytes */
512                         *(buf) = __le32_to_cpu(readl((void __iomem *)ptr));
513                         i += 4;
514                 }
515                 buf++;
516                 ptr++;
517         }
518 out:
519         return;
520 }
521
522 /*
523  * TODO:
524  * -Optimize
525  * -Rewrite cleaner
526  */
527 static u32 write_mem32(void __iomem *mem_addr_start, u32 *buf,
528                         u32 size_bytes)
529 {
530         u32 i = 0;
531         u32 *ptr = (__force u32 *) mem_addr_start;
532         u16 *buf16;
533
534         if (unlikely(!ptr || !buf))
535                 return 0;
536
537         /* shortcut for extremely often used cases */
538         switch (size_bytes) {
539         case 2: /* 2 bytes */
540                 buf16 = (u16 *) buf;
541                 writew(__cpu_to_le16(*buf16), (void __iomem *)ptr);
542                 return 2;
543                 break;
544         case 1: /*
545                  * also needs to write 4 bytes in this case
546                  * so falling through..
547                  */
548         case 4: /* 4 bytes */
549                 writel(__cpu_to_le32(*buf), (void __iomem *)ptr);
550                 return 4;
551                 break;
552         }
553
554         while (i < size_bytes) {
555                 if (size_bytes - i == 2) {
556                         /* 2 bytes */
557                         buf16 = (u16 *) buf;
558                         writew(__cpu_to_le16(*buf16), (void __iomem *)ptr);
559                         i += 2;
560                 } else {
561                         /* 4 bytes */
562                         writel(__cpu_to_le32(*buf), (void __iomem *)ptr);
563                         i += 4;
564                 }
565                 buf++;
566                 ptr++;
567         }
568         return i;
569 }
570
571 /* Setup pointers to different channels and also setup buffer sizes. */
572 static void setup_memory(struct nozomi *dc)
573 {
574         void __iomem *offset = dc->base_addr + dc->config_table.dl_start;
575         /* The length reported is including the length field of 4 bytes,
576          * hence subtract with 4.
577          */
578         const u16 buff_offset = 4;
579
580         /* Modem port dl configuration */
581         dc->port[PORT_MDM].dl_addr[CH_A] = offset;
582         dc->port[PORT_MDM].dl_addr[CH_B] =
583                                 (offset += dc->config_table.dl_mdm_len1);
584         dc->port[PORT_MDM].dl_size[CH_A] =
585                                 dc->config_table.dl_mdm_len1 - buff_offset;
586         dc->port[PORT_MDM].dl_size[CH_B] =
587                                 dc->config_table.dl_mdm_len2 - buff_offset;
588
589         /* Diag port dl configuration */
590         dc->port[PORT_DIAG].dl_addr[CH_A] =
591                                 (offset += dc->config_table.dl_mdm_len2);
592         dc->port[PORT_DIAG].dl_size[CH_A] =
593                                 dc->config_table.dl_diag_len1 - buff_offset;
594         dc->port[PORT_DIAG].dl_addr[CH_B] =
595                                 (offset += dc->config_table.dl_diag_len1);
596         dc->port[PORT_DIAG].dl_size[CH_B] =
597                                 dc->config_table.dl_diag_len2 - buff_offset;
598
599         /* App1 port dl configuration */
600         dc->port[PORT_APP1].dl_addr[CH_A] =
601                                 (offset += dc->config_table.dl_diag_len2);
602         dc->port[PORT_APP1].dl_size[CH_A] =
603                                 dc->config_table.dl_app1_len - buff_offset;
604
605         /* App2 port dl configuration */
606         dc->port[PORT_APP2].dl_addr[CH_A] =
607                                 (offset += dc->config_table.dl_app1_len);
608         dc->port[PORT_APP2].dl_size[CH_A] =
609                                 dc->config_table.dl_app2_len - buff_offset;
610
611         /* Ctrl dl configuration */
612         dc->port[PORT_CTRL].dl_addr[CH_A] =
613                                 (offset += dc->config_table.dl_app2_len);
614         dc->port[PORT_CTRL].dl_size[CH_A] =
615                                 dc->config_table.dl_ctrl_len - buff_offset;
616
617         offset = dc->base_addr + dc->config_table.ul_start;
618
619         /* Modem Port ul configuration */
620         dc->port[PORT_MDM].ul_addr[CH_A] = offset;
621         dc->port[PORT_MDM].ul_size[CH_A] =
622                                 dc->config_table.ul_mdm_len1 - buff_offset;
623         dc->port[PORT_MDM].ul_addr[CH_B] =
624                                 (offset += dc->config_table.ul_mdm_len1);
625         dc->port[PORT_MDM].ul_size[CH_B] =
626                                 dc->config_table.ul_mdm_len2 - buff_offset;
627
628         /* Diag port ul configuration */
629         dc->port[PORT_DIAG].ul_addr[CH_A] =
630                                 (offset += dc->config_table.ul_mdm_len2);
631         dc->port[PORT_DIAG].ul_size[CH_A] =
632                                 dc->config_table.ul_diag_len - buff_offset;
633
634         /* App1 port ul configuration */
635         dc->port[PORT_APP1].ul_addr[CH_A] =
636                                 (offset += dc->config_table.ul_diag_len);
637         dc->port[PORT_APP1].ul_size[CH_A] =
638                                 dc->config_table.ul_app1_len - buff_offset;
639
640         /* App2 port ul configuration */
641         dc->port[PORT_APP2].ul_addr[CH_A] =
642                                 (offset += dc->config_table.ul_app1_len);
643         dc->port[PORT_APP2].ul_size[CH_A] =
644                                 dc->config_table.ul_app2_len - buff_offset;
645
646         /* Ctrl ul configuration */
647         dc->port[PORT_CTRL].ul_addr[CH_A] =
648                                 (offset += dc->config_table.ul_app2_len);
649         dc->port[PORT_CTRL].ul_size[CH_A] =
650                                 dc->config_table.ul_ctrl_len - buff_offset;
651 }
652
653 /* Dump config table under initalization phase */
654 #ifdef DEBUG
655 static void dump_table(const struct nozomi *dc)
656 {
657         DBG3("signature: 0x%08X", dc->config_table.signature);
658         DBG3("version: 0x%04X", dc->config_table.version);
659         DBG3("product_information: 0x%04X", \
660                                 dc->config_table.product_information);
661         DBG3("toggle enabled: %d", dc->config_table.toggle.enabled);
662         DBG3("toggle up_mdm: %d", dc->config_table.toggle.mdm_ul);
663         DBG3("toggle dl_mdm: %d", dc->config_table.toggle.mdm_dl);
664         DBG3("toggle dl_dbg: %d", dc->config_table.toggle.diag_dl);
665
666         DBG3("dl_start: 0x%04X", dc->config_table.dl_start);
667         DBG3("dl_mdm_len0: 0x%04X, %d", dc->config_table.dl_mdm_len1,
668            dc->config_table.dl_mdm_len1);
669         DBG3("dl_mdm_len1: 0x%04X, %d", dc->config_table.dl_mdm_len2,
670            dc->config_table.dl_mdm_len2);
671         DBG3("dl_diag_len0: 0x%04X, %d", dc->config_table.dl_diag_len1,
672            dc->config_table.dl_diag_len1);
673         DBG3("dl_diag_len1: 0x%04X, %d", dc->config_table.dl_diag_len2,
674            dc->config_table.dl_diag_len2);
675         DBG3("dl_app1_len: 0x%04X, %d", dc->config_table.dl_app1_len,
676            dc->config_table.dl_app1_len);
677         DBG3("dl_app2_len: 0x%04X, %d", dc->config_table.dl_app2_len,
678            dc->config_table.dl_app2_len);
679         DBG3("dl_ctrl_len: 0x%04X, %d", dc->config_table.dl_ctrl_len,
680            dc->config_table.dl_ctrl_len);
681         DBG3("ul_start: 0x%04X, %d", dc->config_table.ul_start,
682            dc->config_table.ul_start);
683         DBG3("ul_mdm_len[0]: 0x%04X, %d", dc->config_table.ul_mdm_len1,
684            dc->config_table.ul_mdm_len1);
685         DBG3("ul_mdm_len[1]: 0x%04X, %d", dc->config_table.ul_mdm_len2,
686            dc->config_table.ul_mdm_len2);
687         DBG3("ul_diag_len: 0x%04X, %d", dc->config_table.ul_diag_len,
688            dc->config_table.ul_diag_len);
689         DBG3("ul_app1_len: 0x%04X, %d", dc->config_table.ul_app1_len,
690            dc->config_table.ul_app1_len);
691         DBG3("ul_app2_len: 0x%04X, %d", dc->config_table.ul_app2_len,
692            dc->config_table.ul_app2_len);
693         DBG3("ul_ctrl_len: 0x%04X, %d", dc->config_table.ul_ctrl_len,
694            dc->config_table.ul_ctrl_len);
695 }
696 #else
697 static __inline__ void dump_table(const struct nozomi *dc) { }
698 #endif
699
700 /*
701  * Read configuration table from card under intalization phase
702  * Returns 1 if ok, else 0
703  */
704 static int nozomi_read_config_table(struct nozomi *dc)
705 {
706         read_mem32((u32 *) &dc->config_table, dc->base_addr + 0,
707                                                 sizeof(struct config_table));
708
709         if (dc->config_table.signature != CONFIG_MAGIC) {
710                 dev_err(&dc->pdev->dev, "ConfigTable Bad! 0x%08X != 0x%08X\n",
711                         dc->config_table.signature, CONFIG_MAGIC);
712                 return 0;
713         }
714
715         if ((dc->config_table.version == 0)
716             || (dc->config_table.toggle.enabled == TOGGLE_VALID)) {
717                 int i;
718                 DBG1("Second phase, configuring card");
719
720                 setup_memory(dc);
721
722                 dc->port[PORT_MDM].toggle_ul = dc->config_table.toggle.mdm_ul;
723                 dc->port[PORT_MDM].toggle_dl = dc->config_table.toggle.mdm_dl;
724                 dc->port[PORT_DIAG].toggle_dl = dc->config_table.toggle.diag_dl;
725                 DBG1("toggle ports: MDM UL:%d MDM DL:%d, DIAG DL:%d",
726                    dc->port[PORT_MDM].toggle_ul,
727                    dc->port[PORT_MDM].toggle_dl, dc->port[PORT_DIAG].toggle_dl);
728
729                 dump_table(dc);
730
731                 for (i = PORT_MDM; i < MAX_PORT; i++) {
732                         dc->port[i].fifo_ul =
733                             kfifo_alloc(FIFO_BUFFER_SIZE_UL, GFP_ATOMIC, NULL);
734                         memset(&dc->port[i].ctrl_dl, 0, sizeof(struct ctrl_dl));
735                         memset(&dc->port[i].ctrl_ul, 0, sizeof(struct ctrl_ul));
736                 }
737
738                 /* Enable control channel */
739                 dc->last_ier = dc->last_ier | CTRL_DL;
740                 writew(dc->last_ier, dc->reg_ier);
741
742                 dev_info(&dc->pdev->dev, "Initialization OK!\n");
743                 return 1;
744         }
745
746         if ((dc->config_table.version > 0)
747             && (dc->config_table.toggle.enabled != TOGGLE_VALID)) {
748                 u32 offset = 0;
749                 DBG1("First phase: pushing upload buffers, clearing download");
750
751                 dev_info(&dc->pdev->dev, "Version of card: %d\n",
752                          dc->config_table.version);
753
754                 /* Here we should disable all I/O over F32. */
755                 setup_memory(dc);
756
757                 /*
758                  * We should send ALL channel pair tokens back along
759                  * with reset token
760                  */
761
762                 /* push upload modem buffers */
763                 write_mem32(dc->port[PORT_MDM].ul_addr[CH_A],
764                         (u32 *) &offset, 4);
765                 write_mem32(dc->port[PORT_MDM].ul_addr[CH_B],
766                         (u32 *) &offset, 4);
767
768                 writew(MDM_UL | DIAG_DL | MDM_DL, dc->reg_fcr);
769
770                 DBG1("First phase done");
771         }
772
773         return 1;
774 }
775
776 /* Enable uplink interrupts  */
777 static void enable_transmit_ul(enum port_type port, struct nozomi *dc)
778 {
779         u16 mask[NOZOMI_MAX_PORTS] = \
780                         {MDM_UL, DIAG_UL, APP1_UL, APP2_UL, CTRL_UL};
781
782         if (port < NOZOMI_MAX_PORTS) {
783                 dc->last_ier |= mask[port];
784                 writew(dc->last_ier, dc->reg_ier);
785         } else {
786                 dev_err(&dc->pdev->dev, "Called with wrong port?\n");
787         }
788 }
789
790 /* Disable uplink interrupts  */
791 static void disable_transmit_ul(enum port_type port, struct nozomi *dc)
792 {
793         u16 mask[NOZOMI_MAX_PORTS] = \
794                         {~MDM_UL, ~DIAG_UL, ~APP1_UL, ~APP2_UL, ~CTRL_UL};
795
796         if (port < NOZOMI_MAX_PORTS) {
797                 dc->last_ier &= mask[port];
798                 writew(dc->last_ier, dc->reg_ier);
799         } else {
800                 dev_err(&dc->pdev->dev, "Called with wrong port?\n");
801         }
802 }
803
804 /* Enable downlink interrupts */
805 static void enable_transmit_dl(enum port_type port, struct nozomi *dc)
806 {
807         u16 mask[NOZOMI_MAX_PORTS] = \
808                         {MDM_DL, DIAG_DL, APP1_DL, APP2_DL, CTRL_DL};
809
810         if (port < NOZOMI_MAX_PORTS) {
811                 dc->last_ier |= mask[port];
812                 writew(dc->last_ier, dc->reg_ier);
813         } else {
814                 dev_err(&dc->pdev->dev, "Called with wrong port?\n");
815         }
816 }
817
818 /* Disable downlink interrupts */
819 static void disable_transmit_dl(enum port_type port, struct nozomi *dc)
820 {
821         u16 mask[NOZOMI_MAX_PORTS] = \
822                         {~MDM_DL, ~DIAG_DL, ~APP1_DL, ~APP2_DL, ~CTRL_DL};
823
824         if (port < NOZOMI_MAX_PORTS) {
825                 dc->last_ier &= mask[port];
826                 writew(dc->last_ier, dc->reg_ier);
827         } else {
828                 dev_err(&dc->pdev->dev, "Called with wrong port?\n");
829         }
830 }
831
832 /*
833  * Return 1 - send buffer to card and ack.
834  * Return 0 - don't ack, don't send buffer to card.
835  */
836 static int send_data(enum port_type index, struct nozomi *dc)
837 {
838         u32 size = 0;
839         struct port *port = &dc->port[index];
840         u8 toggle = port->toggle_ul;
841         void __iomem *addr = port->ul_addr[toggle];
842         u32 ul_size = port->ul_size[toggle];
843         struct tty_struct *tty = port->tty;
844
845         /* Get data from tty and place in buf for now */
846         size = __kfifo_get(port->fifo_ul, dc->send_buf,
847                            ul_size < SEND_BUF_MAX ? ul_size : SEND_BUF_MAX);
848
849         if (size == 0) {
850                 DBG4("No more data to send, disable link:");
851                 return 0;
852         }
853
854         /* DUMP(buf, size); */
855
856         /* Write length + data */
857         write_mem32(addr, (u32 *) &size, 4);
858         write_mem32(addr + 4, (u32 *) dc->send_buf, size);
859
860         if (tty)
861                 tty_wakeup(tty);
862
863         return 1;
864 }
865
866 /* If all data has been read, return 1, else 0 */
867 static int receive_data(enum port_type index, struct nozomi *dc)
868 {
869         u8 buf[RECEIVE_BUF_MAX] = { 0 };
870         int size;
871         u32 offset = 4;
872         struct port *port = &dc->port[index];
873         void __iomem *addr = port->dl_addr[port->toggle_dl];
874         struct tty_struct *tty = port->tty;
875         int i;
876
877         if (unlikely(!tty)) {
878                 DBG1("tty not open for port: %d?", index);
879                 return 1;
880         }
881
882         read_mem32((u32 *) &size, addr, 4);
883         /*  DBG1( "%d bytes port: %d", size, index); */
884
885         if (test_bit(TTY_THROTTLED, &tty->flags)) {
886                 DBG1("No room in tty, don't read data, don't ack interrupt, "
887                         "disable interrupt");
888
889                 /* disable interrupt in downlink... */
890                 disable_transmit_dl(index, dc);
891                 return 0;
892         }
893
894         if (unlikely(size == 0)) {
895                 dev_err(&dc->pdev->dev, "size == 0?\n");
896                 return 1;
897         }
898
899         tty_buffer_request_room(tty, size);
900
901         while (size > 0) {
902                 read_mem32((u32 *) buf, addr + offset, RECEIVE_BUF_MAX);
903
904                 if (size == 1) {
905                         tty_insert_flip_char(tty, buf[0], TTY_NORMAL);
906                         size = 0;
907                 } else if (size < RECEIVE_BUF_MAX) {
908                         size -= tty_insert_flip_string(tty, (char *) buf, size);
909                 } else {
910                         i = tty_insert_flip_string(tty, \
911                                                 (char *) buf, RECEIVE_BUF_MAX);
912                         size -= i;
913                         offset += i;
914                 }
915         }
916
917         set_bit(index, &dc->flip);
918
919         return 1;
920 }
921
922 /* Debug for interrupts */
923 #ifdef DEBUG
924 static char *interrupt2str(u16 interrupt)
925 {
926         static char buf[TMP_BUF_MAX];
927         char *p = buf;
928
929         interrupt & MDM_DL1 ? p += snprintf(p, TMP_BUF_MAX, "MDM_DL1 ") : NULL;
930         interrupt & MDM_DL2 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
931                                         "MDM_DL2 ") : NULL;
932
933         interrupt & MDM_UL1 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
934                                         "MDM_UL1 ") : NULL;
935         interrupt & MDM_UL2 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
936                                         "MDM_UL2 ") : NULL;
937
938         interrupt & DIAG_DL1 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
939                                         "DIAG_DL1 ") : NULL;
940         interrupt & DIAG_DL2 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
941                                         "DIAG_DL2 ") : NULL;
942
943         interrupt & DIAG_UL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
944                                         "DIAG_UL ") : NULL;
945
946         interrupt & APP1_DL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
947                                         "APP1_DL ") : NULL;
948         interrupt & APP2_DL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
949                                         "APP2_DL ") : NULL;
950
951         interrupt & APP1_UL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
952                                         "APP1_UL ") : NULL;
953         interrupt & APP2_UL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
954                                         "APP2_UL ") : NULL;
955
956         interrupt & CTRL_DL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
957                                         "CTRL_DL ") : NULL;
958         interrupt & CTRL_UL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
959                                         "CTRL_UL ") : NULL;
960
961         interrupt & RESET ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
962                                         "RESET ") : NULL;
963
964         return buf;
965 }
966 #endif
967
968 /*
969  * Receive flow control
970  * Return 1 - If ok, else 0
971  */
972 static int receive_flow_control(struct nozomi *dc)
973 {
974         enum port_type port = PORT_MDM;
975         struct ctrl_dl ctrl_dl;
976         struct ctrl_dl old_ctrl;
977         u16 enable_ier = 0;
978
979         read_mem32((u32 *) &ctrl_dl, dc->port[PORT_CTRL].dl_addr[CH_A], 2);
980
981         switch (ctrl_dl.port) {
982         case CTRL_CMD:
983                 DBG1("The Base Band sends this value as a response to a "
984                         "request for IMSI detach sent over the control "
985                         "channel uplink (see section 7.6.1).");
986                 break;
987         case CTRL_MDM:
988                 port = PORT_MDM;
989                 enable_ier = MDM_DL;
990                 break;
991         case CTRL_DIAG:
992                 port = PORT_DIAG;
993                 enable_ier = DIAG_DL;
994                 break;
995         case CTRL_APP1:
996                 port = PORT_APP1;
997                 enable_ier = APP1_DL;
998                 break;
999         case CTRL_APP2:
1000                 port = PORT_APP2;
1001                 enable_ier = APP2_DL;
1002                 break;
1003         default:
1004                 dev_err(&dc->pdev->dev,
1005                         "ERROR: flow control received for non-existing port\n");
1006                 return 0;
1007         };
1008
1009         DBG1("0x%04X->0x%04X", *((u16 *)&dc->port[port].ctrl_dl),
1010            *((u16 *)&ctrl_dl));
1011
1012         old_ctrl = dc->port[port].ctrl_dl;
1013         dc->port[port].ctrl_dl = ctrl_dl;
1014
1015         if (old_ctrl.CTS == 1 && ctrl_dl.CTS == 0) {
1016                 DBG1("Disable interrupt (0x%04X) on port: %d",
1017                         enable_ier, port);
1018                 disable_transmit_ul(port, dc);
1019
1020         } else if (old_ctrl.CTS == 0 && ctrl_dl.CTS == 1) {
1021
1022                 if (__kfifo_len(dc->port[port].fifo_ul)) {
1023                         DBG1("Enable interrupt (0x%04X) on port: %d",
1024                                 enable_ier, port);
1025                         DBG1("Data in buffer [%d], enable transmit! ",
1026                                 __kfifo_len(dc->port[port].fifo_ul));
1027                         enable_transmit_ul(port, dc);
1028                 } else {
1029                         DBG1("No data in buffer...");
1030                 }
1031         }
1032
1033         if (*(u16 *)&old_ctrl == *(u16 *)&ctrl_dl) {
1034                 DBG1(" No change in mctrl");
1035                 return 1;
1036         }
1037         /* Update statistics */
1038         if (old_ctrl.CTS != ctrl_dl.CTS)
1039                 dc->port[port].tty_icount.cts++;
1040         if (old_ctrl.DSR != ctrl_dl.DSR)
1041                 dc->port[port].tty_icount.dsr++;
1042         if (old_ctrl.RI != ctrl_dl.RI)
1043                 dc->port[port].tty_icount.rng++;
1044         if (old_ctrl.DCD != ctrl_dl.DCD)
1045                 dc->port[port].tty_icount.dcd++;
1046
1047         wake_up_interruptible(&dc->port[port].tty_wait);
1048
1049         DBG1("port: %d DCD(%d), CTS(%d), RI(%d), DSR(%d)",
1050            port,
1051            dc->port[port].tty_icount.dcd, dc->port[port].tty_icount.cts,
1052            dc->port[port].tty_icount.rng, dc->port[port].tty_icount.dsr);
1053
1054         return 1;
1055 }
1056
1057 static enum ctrl_port_type port2ctrl(enum port_type port,
1058                                         const struct nozomi *dc)
1059 {
1060         switch (port) {
1061         case PORT_MDM:
1062                 return CTRL_MDM;
1063         case PORT_DIAG:
1064                 return CTRL_DIAG;
1065         case PORT_APP1:
1066                 return CTRL_APP1;
1067         case PORT_APP2:
1068                 return CTRL_APP2;
1069         default:
1070                 dev_err(&dc->pdev->dev,
1071                         "ERROR: send flow control " \
1072                         "received for non-existing port\n");
1073         };
1074         return CTRL_ERROR;
1075 }
1076
1077 /*
1078  * Send flow control, can only update one channel at a time
1079  * Return 0 - If we have updated all flow control
1080  * Return 1 - If we need to update more flow control, ack current enable more
1081  */
1082 static int send_flow_control(struct nozomi *dc)
1083 {
1084         u32 i, more_flow_control_to_be_updated = 0;
1085         u16 *ctrl;
1086
1087         for (i = PORT_MDM; i < MAX_PORT; i++) {
1088                 if (dc->port[i].update_flow_control) {
1089                         if (more_flow_control_to_be_updated) {
1090                                 /* We have more flow control to be updated */
1091                                 return 1;
1092                         }
1093                         dc->port[i].ctrl_ul.port = port2ctrl(i, dc);
1094                         ctrl = (u16 *)&dc->port[i].ctrl_ul;
1095                         write_mem32(dc->port[PORT_CTRL].ul_addr[0], \
1096                                 (u32 *) ctrl, 2);
1097                         dc->port[i].update_flow_control = 0;
1098                         more_flow_control_to_be_updated = 1;
1099                 }
1100         }
1101         return 0;
1102 }
1103
1104 /*
1105  * Handle donlink data, ports that are handled are modem and diagnostics
1106  * Return 1 - ok
1107  * Return 0 - toggle fields are out of sync
1108  */
1109 static int handle_data_dl(struct nozomi *dc, enum port_type port, u8 *toggle,
1110                         u16 read_iir, u16 mask1, u16 mask2)
1111 {
1112         if (*toggle == 0 && read_iir & mask1) {
1113                 if (receive_data(port, dc)) {
1114                         writew(mask1, dc->reg_fcr);
1115                         *toggle = !(*toggle);
1116                 }
1117
1118                 if (read_iir & mask2) {
1119                         if (receive_data(port, dc)) {
1120                                 writew(mask2, dc->reg_fcr);
1121                                 *toggle = !(*toggle);
1122                         }
1123                 }
1124         } else if (*toggle == 1 && read_iir & mask2) {
1125                 if (receive_data(port, dc)) {
1126                         writew(mask2, dc->reg_fcr);
1127                         *toggle = !(*toggle);
1128                 }
1129
1130                 if (read_iir & mask1) {
1131                         if (receive_data(port, dc)) {
1132                                 writew(mask1, dc->reg_fcr);
1133                                 *toggle = !(*toggle);
1134                         }
1135                 }
1136         } else {
1137                 dev_err(&dc->pdev->dev, "port out of sync!, toggle:%d\n",
1138                         *toggle);
1139                 return 0;
1140         }
1141         return 1;
1142 }
1143
1144 /*
1145  * Handle uplink data, this is currently for the modem port
1146  * Return 1 - ok
1147  * Return 0 - toggle field are out of sync
1148  */
1149 static int handle_data_ul(struct nozomi *dc, enum port_type port, u16 read_iir)
1150 {
1151         u8 *toggle = &(dc->port[port].toggle_ul);
1152
1153         if (*toggle == 0 && 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;
1161                 }
1162
1163                 if (read_iir & MDM_UL2) {
1164                         dc->last_ier &= ~MDM_UL;
1165                         writew(dc->last_ier, dc->reg_ier);
1166                         if (send_data(port, dc)) {
1167                                 writew(MDM_UL2, dc->reg_fcr);
1168                                 dc->last_ier = dc->last_ier | MDM_UL;
1169                                 writew(dc->last_ier, dc->reg_ier);
1170                                 *toggle = !*toggle;
1171                         }
1172                 }
1173
1174         } else if (*toggle == 1 && read_iir & MDM_UL2) {
1175                 dc->last_ier &= ~MDM_UL;
1176                 writew(dc->last_ier, dc->reg_ier);
1177                 if (send_data(port, dc)) {
1178                         writew(MDM_UL2, dc->reg_fcr);
1179                         dc->last_ier = dc->last_ier | MDM_UL;
1180                         writew(dc->last_ier, dc->reg_ier);
1181                         *toggle = !*toggle;
1182                 }
1183
1184                 if (read_iir & MDM_UL1) {
1185                         dc->last_ier &= ~MDM_UL;
1186                         writew(dc->last_ier, dc->reg_ier);
1187                         if (send_data(port, dc)) {
1188                                 writew(MDM_UL1, dc->reg_fcr);
1189                                 dc->last_ier = dc->last_ier | MDM_UL;
1190                                 writew(dc->last_ier, dc->reg_ier);
1191                                 *toggle = !*toggle;
1192                         }
1193                 }
1194         } else {
1195                 writew(read_iir & MDM_UL, dc->reg_fcr);
1196                 dev_err(&dc->pdev->dev, "port out of sync!\n");
1197                 return 0;
1198         }
1199         return 1;
1200 }
1201
1202 static irqreturn_t interrupt_handler(int irq, void *dev_id)
1203 {
1204         struct nozomi *dc = dev_id;
1205         unsigned int a;
1206         u16 read_iir;
1207
1208         if (!dc)
1209                 return IRQ_NONE;
1210
1211         spin_lock(&dc->spin_mutex);
1212         read_iir = readw(dc->reg_iir);
1213
1214         /* Card removed */
1215         if (read_iir == (u16)-1)
1216                 goto none;
1217         /*
1218          * Just handle interrupt enabled in IER
1219          * (by masking with dc->last_ier)
1220          */
1221         read_iir &= dc->last_ier;
1222
1223         if (read_iir == 0)
1224                 goto none;
1225
1226
1227         DBG4("%s irq:0x%04X, prev:0x%04X", interrupt2str(read_iir), read_iir,
1228                 dc->last_ier);
1229
1230         if (read_iir & RESET) {
1231                 if (unlikely(!nozomi_read_config_table(dc))) {
1232                         dc->last_ier = 0x0;
1233                         writew(dc->last_ier, dc->reg_ier);
1234                         dev_err(&dc->pdev->dev, "Could not read status from "
1235                                 "card, we should disable interface\n");
1236                 } else {
1237                         writew(RESET, dc->reg_fcr);
1238                 }
1239                 /* No more useful info if this was the reset interrupt. */
1240                 goto exit_handler;
1241         }
1242         if (read_iir & CTRL_UL) {
1243                 DBG1("CTRL_UL");
1244                 dc->last_ier &= ~CTRL_UL;
1245                 writew(dc->last_ier, dc->reg_ier);
1246                 if (send_flow_control(dc)) {
1247                         writew(CTRL_UL, dc->reg_fcr);
1248                         dc->last_ier = dc->last_ier | CTRL_UL;
1249                         writew(dc->last_ier, dc->reg_ier);
1250                 }
1251         }
1252         if (read_iir & CTRL_DL) {
1253                 receive_flow_control(dc);
1254                 writew(CTRL_DL, dc->reg_fcr);
1255         }
1256         if (read_iir & MDM_DL) {
1257                 if (!handle_data_dl(dc, PORT_MDM,
1258                                 &(dc->port[PORT_MDM].toggle_dl), read_iir,
1259                                 MDM_DL1, MDM_DL2)) {
1260                         dev_err(&dc->pdev->dev, "MDM_DL out of sync!\n");
1261                         goto exit_handler;
1262                 }
1263         }
1264         if (read_iir & MDM_UL) {
1265                 if (!handle_data_ul(dc, PORT_MDM, read_iir)) {
1266                         dev_err(&dc->pdev->dev, "MDM_UL out of sync!\n");
1267                         goto exit_handler;
1268                 }
1269         }
1270         if (read_iir & DIAG_DL) {
1271                 if (!handle_data_dl(dc, PORT_DIAG,
1272                                 &(dc->port[PORT_DIAG].toggle_dl), read_iir,
1273                                 DIAG_DL1, DIAG_DL2)) {
1274                         dev_err(&dc->pdev->dev, "DIAG_DL out of sync!\n");
1275                         goto exit_handler;
1276                 }
1277         }
1278         if (read_iir & DIAG_UL) {
1279                 dc->last_ier &= ~DIAG_UL;
1280                 writew(dc->last_ier, dc->reg_ier);
1281                 if (send_data(PORT_DIAG, dc)) {
1282                         writew(DIAG_UL, dc->reg_fcr);
1283                         dc->last_ier = dc->last_ier | DIAG_UL;
1284                         writew(dc->last_ier, dc->reg_ier);
1285                 }
1286         }
1287         if (read_iir & APP1_DL) {
1288                 if (receive_data(PORT_APP1, dc))
1289                         writew(APP1_DL, dc->reg_fcr);
1290         }
1291         if (read_iir & APP1_UL) {
1292                 dc->last_ier &= ~APP1_UL;
1293                 writew(dc->last_ier, dc->reg_ier);
1294                 if (send_data(PORT_APP1, dc)) {
1295                         writew(APP1_UL, dc->reg_fcr);
1296                         dc->last_ier = dc->last_ier | APP1_UL;
1297                         writew(dc->last_ier, dc->reg_ier);
1298                 }
1299         }
1300         if (read_iir & APP2_DL) {
1301                 if (receive_data(PORT_APP2, dc))
1302                         writew(APP2_DL, dc->reg_fcr);
1303         }
1304         if (read_iir & APP2_UL) {
1305                 dc->last_ier &= ~APP2_UL;
1306                 writew(dc->last_ier, dc->reg_ier);
1307                 if (send_data(PORT_APP2, dc)) {
1308                         writew(APP2_UL, dc->reg_fcr);
1309                         dc->last_ier = dc->last_ier | APP2_UL;
1310                         writew(dc->last_ier, dc->reg_ier);
1311                 }
1312         }
1313
1314 exit_handler:
1315         spin_unlock(&dc->spin_mutex);
1316         for (a = 0; a < NOZOMI_MAX_PORTS; a++)
1317                 if (test_and_clear_bit(a, &dc->flip))
1318                         tty_flip_buffer_push(dc->port[a].tty);
1319         return IRQ_HANDLED;
1320 none:
1321         spin_unlock(&dc->spin_mutex);
1322         return IRQ_NONE;
1323 }
1324
1325 static void nozomi_get_card_type(struct nozomi *dc)
1326 {
1327         int i;
1328         u32 size = 0;
1329
1330         for (i = 0; i < 6; i++)
1331                 size += pci_resource_len(dc->pdev, i);
1332
1333         /* Assume card type F32_8 if no match */
1334         dc->card_type = size == 2048 ? F32_2 : F32_8;
1335
1336         dev_info(&dc->pdev->dev, "Card type is: %d\n", dc->card_type);
1337 }
1338
1339 static void nozomi_setup_private_data(struct nozomi *dc)
1340 {
1341         void __iomem *offset = dc->base_addr + dc->card_type / 2;
1342         unsigned int i;
1343
1344         dc->reg_fcr = (void __iomem *)(offset + R_FCR);
1345         dc->reg_iir = (void __iomem *)(offset + R_IIR);
1346         dc->reg_ier = (void __iomem *)(offset + R_IER);
1347         dc->last_ier = 0;
1348         dc->flip = 0;
1349
1350         dc->port[PORT_MDM].token_dl = MDM_DL;
1351         dc->port[PORT_DIAG].token_dl = DIAG_DL;
1352         dc->port[PORT_APP1].token_dl = APP1_DL;
1353         dc->port[PORT_APP2].token_dl = APP2_DL;
1354
1355         for (i = 0; i < MAX_PORT; i++)
1356                 init_waitqueue_head(&dc->port[i].tty_wait);
1357 }
1358
1359 static ssize_t card_type_show(struct device *dev, struct device_attribute *attr,
1360                           char *buf)
1361 {
1362         struct nozomi *dc = pci_get_drvdata(to_pci_dev(dev));
1363
1364         return sprintf(buf, "%d\n", dc->card_type);
1365 }
1366 static DEVICE_ATTR(card_type, 0444, card_type_show, NULL);
1367
1368 static ssize_t open_ttys_show(struct device *dev, struct device_attribute *attr,
1369                           char *buf)
1370 {
1371         struct nozomi *dc = pci_get_drvdata(to_pci_dev(dev));
1372
1373         return sprintf(buf, "%u\n", dc->open_ttys);
1374 }
1375 static DEVICE_ATTR(open_ttys, 0444, open_ttys_show, NULL);
1376
1377 static void make_sysfs_files(struct nozomi *dc)
1378 {
1379         if (device_create_file(&dc->pdev->dev, &dev_attr_card_type))
1380                 dev_err(&dc->pdev->dev,
1381                         "Could not create sysfs file for card_type\n");
1382         if (device_create_file(&dc->pdev->dev, &dev_attr_open_ttys))
1383                 dev_err(&dc->pdev->dev,
1384                         "Could not create sysfs file for open_ttys\n");
1385 }
1386
1387 static void remove_sysfs_files(struct nozomi *dc)
1388 {
1389         device_remove_file(&dc->pdev->dev, &dev_attr_card_type);
1390         device_remove_file(&dc->pdev->dev, &dev_attr_open_ttys);
1391 }
1392
1393 /* Allocate memory for one device */
1394 static int __devinit nozomi_card_init(struct pci_dev *pdev,
1395                                       const struct pci_device_id *ent)
1396 {
1397         resource_size_t start;
1398         int ret;
1399         struct nozomi *dc = NULL;
1400         int ndev_idx;
1401         int i;
1402
1403         dev_dbg(&pdev->dev, "Init, new card found\n");
1404
1405         for (ndev_idx = 0; ndev_idx < ARRAY_SIZE(ndevs); ndev_idx++)
1406                 if (!ndevs[ndev_idx])
1407                         break;
1408
1409         if (ndev_idx >= ARRAY_SIZE(ndevs)) {
1410                 dev_err(&pdev->dev, "no free tty range for this card left\n");
1411                 ret = -EIO;
1412                 goto err;
1413         }
1414
1415         dc = kzalloc(sizeof(struct nozomi), GFP_KERNEL);
1416         if (unlikely(!dc)) {
1417                 dev_err(&pdev->dev, "Could not allocate memory\n");
1418                 ret = -ENOMEM;
1419                 goto err_free;
1420         }
1421
1422         dc->pdev = pdev;
1423
1424         /* Find out what card type it is */
1425         nozomi_get_card_type(dc);
1426
1427         ret = pci_enable_device(dc->pdev);
1428         if (ret) {
1429                 dev_err(&pdev->dev, "Failed to enable PCI Device\n");
1430                 goto err_free;
1431         }
1432
1433         start = pci_resource_start(dc->pdev, 0);
1434         if (start == 0) {
1435                 dev_err(&pdev->dev, "No I/O address for card detected\n");
1436                 ret = -ENODEV;
1437                 goto err_disable_device;
1438         }
1439
1440         ret = pci_request_regions(dc->pdev, NOZOMI_NAME);
1441         if (ret) {
1442                 dev_err(&pdev->dev, "I/O address 0x%04x already in use\n",
1443                         (int) /* nozomi_private.io_addr */ 0);
1444                 goto err_disable_device;
1445         }
1446
1447         dc->base_addr = ioremap(start, dc->card_type);
1448         if (!dc->base_addr) {
1449                 dev_err(&pdev->dev, "Unable to map card MMIO\n");
1450                 ret = -ENODEV;
1451                 goto err_rel_regs;
1452         }
1453
1454         dc->send_buf = kmalloc(SEND_BUF_MAX, GFP_KERNEL);
1455         if (!dc->send_buf) {
1456                 dev_err(&pdev->dev, "Could not allocate send buffer?\n");
1457                 ret = -ENOMEM;
1458                 goto err_free_sbuf;
1459         }
1460
1461         spin_lock_init(&dc->spin_mutex);
1462
1463         nozomi_setup_private_data(dc);
1464
1465         /* Disable all interrupts */
1466         dc->last_ier = 0;
1467         writew(dc->last_ier, dc->reg_ier);
1468
1469         ret = request_irq(pdev->irq, &interrupt_handler, IRQF_SHARED,
1470                         NOZOMI_NAME, dc);
1471         if (unlikely(ret)) {
1472                 dev_err(&pdev->dev, "can't request irq %d\n", pdev->irq);
1473                 goto err_free_sbuf;
1474         }
1475
1476         DBG1("base_addr: %p", dc->base_addr);
1477
1478         make_sysfs_files(dc);
1479
1480         dc->index_start = ndev_idx * MAX_PORT;
1481         ndevs[ndev_idx] = dc;
1482
1483         for (i = 0; i < MAX_PORT; i++) {
1484                 mutex_init(&dc->port[i].tty_sem);
1485                 dc->port[i].tty_open_count = 0;
1486                 dc->port[i].tty = NULL;
1487                 tty_register_device(ntty_driver, dc->index_start + i,
1488                                                         &pdev->dev);
1489         }
1490
1491         /* Enable  RESET interrupt. */
1492         dc->last_ier = RESET;
1493         writew(dc->last_ier, dc->reg_ier);
1494
1495         pci_set_drvdata(pdev, dc);
1496
1497         return 0;
1498
1499 err_free_sbuf:
1500         kfree(dc->send_buf);
1501         iounmap(dc->base_addr);
1502 err_rel_regs:
1503         pci_release_regions(pdev);
1504 err_disable_device:
1505         pci_disable_device(pdev);
1506 err_free:
1507         kfree(dc);
1508 err:
1509         return ret;
1510 }
1511
1512 static void __devexit tty_exit(struct nozomi *dc)
1513 {
1514         unsigned int i;
1515
1516         DBG1(" ");
1517
1518         flush_scheduled_work();
1519
1520         for (i = 0; i < MAX_PORT; ++i)
1521                 if (dc->port[i].tty && \
1522                                 list_empty(&dc->port[i].tty->hangup_work.entry))
1523                         tty_hangup(dc->port[i].tty);
1524
1525         while (dc->open_ttys)
1526                 msleep(1);
1527
1528         for (i = dc->index_start; i < dc->index_start + MAX_PORT; ++i)
1529                 tty_unregister_device(ntty_driver, i);
1530 }
1531
1532 /* Deallocate memory for one device */
1533 static void __devexit nozomi_card_exit(struct pci_dev *pdev)
1534 {
1535         int i;
1536         struct ctrl_ul ctrl;
1537         struct nozomi *dc = pci_get_drvdata(pdev);
1538
1539         /* Disable all interrupts */
1540         dc->last_ier = 0;
1541         writew(dc->last_ier, dc->reg_ier);
1542
1543         tty_exit(dc);
1544
1545         /* Send 0x0001, command card to resend the reset token.  */
1546         /* This is to get the reset when the module is reloaded. */
1547         ctrl.port = 0x00;
1548         ctrl.reserved = 0;
1549         ctrl.RTS = 0;
1550         ctrl.DTR = 1;
1551         DBG1("sending flow control 0x%04X", *((u16 *)&ctrl));
1552
1553         /* Setup dc->reg addresses to we can use defines here */
1554         write_mem32(dc->port[PORT_CTRL].ul_addr[0], (u32 *)&ctrl, 2);
1555         writew(CTRL_UL, dc->reg_fcr);   /* push the token to the card. */
1556
1557         remove_sysfs_files(dc);
1558
1559         free_irq(pdev->irq, dc);
1560
1561         for (i = 0; i < MAX_PORT; i++)
1562                 if (dc->port[i].fifo_ul)
1563                         kfifo_free(dc->port[i].fifo_ul);
1564
1565         kfree(dc->send_buf);
1566
1567         iounmap(dc->base_addr);
1568
1569         pci_release_regions(pdev);
1570
1571         pci_disable_device(pdev);
1572
1573         ndevs[dc->index_start / MAX_PORT] = NULL;
1574
1575         kfree(dc);
1576 }
1577
1578 static void set_rts(const struct tty_struct *tty, int rts)
1579 {
1580         struct port *port = get_port_by_tty(tty);
1581
1582         port->ctrl_ul.RTS = rts;
1583         port->update_flow_control = 1;
1584         enable_transmit_ul(PORT_CTRL, get_dc_by_tty(tty));
1585 }
1586
1587 static void set_dtr(const struct tty_struct *tty, int dtr)
1588 {
1589         struct port *port = get_port_by_tty(tty);
1590
1591         DBG1("SETTING DTR index: %d, dtr: %d", tty->index, dtr);
1592
1593         port->ctrl_ul.DTR = dtr;
1594         port->update_flow_control = 1;
1595         enable_transmit_ul(PORT_CTRL, get_dc_by_tty(tty));
1596 }
1597
1598 /*
1599  * ----------------------------------------------------------------------------
1600  * TTY code
1601  * ----------------------------------------------------------------------------
1602  */
1603
1604 /* Called when the userspace process opens the tty, /dev/noz*.  */
1605 static int ntty_open(struct tty_struct *tty, struct file *file)
1606 {
1607         struct port *port = get_port_by_tty(tty);
1608         struct nozomi *dc = get_dc_by_tty(tty);
1609         unsigned long flags;
1610
1611         if (!port || !dc)
1612                 return -ENODEV;
1613
1614         if (mutex_lock_interruptible(&port->tty_sem))
1615                 return -ERESTARTSYS;
1616
1617         port->tty_open_count++;
1618         dc->open_ttys++;
1619
1620         /* Enable interrupt downlink for channel */
1621         if (port->tty_open_count == 1) {
1622                 tty->low_latency = 1;
1623                 tty->driver_data = port;
1624                 port->tty = tty;
1625                 DBG1("open: %d", port->token_dl);
1626                 spin_lock_irqsave(&dc->spin_mutex, flags);
1627                 dc->last_ier = dc->last_ier | port->token_dl;
1628                 writew(dc->last_ier, dc->reg_ier);
1629                 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1630         }
1631
1632         mutex_unlock(&port->tty_sem);
1633
1634         return 0;
1635 }
1636
1637 /* Called when the userspace process close the tty, /dev/noz*. */
1638 static void ntty_close(struct tty_struct *tty, struct file *file)
1639 {
1640         struct nozomi *dc = get_dc_by_tty(tty);
1641         struct port *port = tty->driver_data;
1642         unsigned long flags;
1643
1644         if (!dc || !port)
1645                 return;
1646
1647         if (mutex_lock_interruptible(&port->tty_sem))
1648                 return;
1649
1650         if (!port->tty_open_count)
1651                 goto exit;
1652
1653         dc->open_ttys--;
1654         port->tty_open_count--;
1655
1656         if (port->tty_open_count == 0) {
1657                 DBG1("close: %d", port->token_dl);
1658                 spin_lock_irqsave(&dc->spin_mutex, flags);
1659                 dc->last_ier &= ~(port->token_dl);
1660                 writew(dc->last_ier, dc->reg_ier);
1661                 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1662         }
1663
1664 exit:
1665         mutex_unlock(&port->tty_sem);
1666 }
1667
1668 /*
1669  * called when the userspace process writes to the tty (/dev/noz*).
1670  * Data is inserted into a fifo, which is then read and transfered to the modem.
1671  */
1672 static int ntty_write(struct tty_struct *tty, const unsigned char *buffer,
1673                       int count)
1674 {
1675         int rval = -EINVAL;
1676         struct nozomi *dc = get_dc_by_tty(tty);
1677         struct port *port = tty->driver_data;
1678         unsigned long flags;
1679
1680         /* DBG1( "WRITEx: %d, index = %d", count, index); */
1681
1682         if (!dc || !port)
1683                 return -ENODEV;
1684
1685         if (unlikely(!mutex_trylock(&port->tty_sem))) {
1686                 /*
1687                  * must test lock as tty layer wraps calls
1688                  * to this function with BKL
1689                  */
1690                 dev_err(&dc->pdev->dev, "Would have deadlocked - "
1691                         "return EAGAIN\n");
1692                 return -EAGAIN;
1693         }
1694
1695         if (unlikely(!port->tty_open_count)) {
1696                 DBG1(" ");
1697                 goto exit;
1698         }
1699
1700         rval = __kfifo_put(port->fifo_ul, (unsigned char *)buffer, count);
1701
1702         /* notify card */
1703         if (unlikely(dc == NULL)) {
1704                 DBG1("No device context?");
1705                 goto exit;
1706         }
1707
1708         spin_lock_irqsave(&dc->spin_mutex, flags);
1709         /* CTS is only valid on the modem channel */
1710         if (port == &(dc->port[PORT_MDM])) {
1711                 if (port->ctrl_dl.CTS) {
1712                         DBG4("Enable interrupt");
1713                         enable_transmit_ul(tty->index % MAX_PORT, dc);
1714                 } else {
1715                         dev_err(&dc->pdev->dev,
1716                                 "CTS not active on modem port?\n");
1717                 }
1718         } else {
1719                 enable_transmit_ul(tty->index % MAX_PORT, dc);
1720         }
1721         spin_unlock_irqrestore(&dc->spin_mutex, flags);
1722
1723 exit:
1724         mutex_unlock(&port->tty_sem);
1725         return rval;
1726 }
1727
1728 /*
1729  * Calculate how much is left in device
1730  * This method is called by the upper tty layer.
1731  *   #according to sources N_TTY.c it expects a value >= 0 and
1732  *    does not check for negative values.
1733  */
1734 static int ntty_write_room(struct tty_struct *tty)
1735 {
1736         struct port *port = tty->driver_data;
1737         int room = 0;
1738         struct nozomi *dc = get_dc_by_tty(tty);
1739
1740         if (!dc || !port)
1741                 return 0;
1742         if (!mutex_trylock(&port->tty_sem))
1743                 return 0;
1744
1745         if (!port->tty_open_count)
1746                 goto exit;
1747
1748         room = port->fifo_ul->size - __kfifo_len(port->fifo_ul);
1749
1750 exit:
1751         mutex_unlock(&port->tty_sem);
1752         return room;
1753 }
1754
1755 /* Gets io control parameters */
1756 static int ntty_tiocmget(struct tty_struct *tty, struct file *file)
1757 {
1758         struct port *port = tty->driver_data;
1759         struct ctrl_dl *ctrl_dl = &port->ctrl_dl;
1760         struct ctrl_ul *ctrl_ul = &port->ctrl_ul;
1761
1762         return  (ctrl_ul->RTS ? TIOCM_RTS : 0) |
1763                 (ctrl_ul->DTR ? TIOCM_DTR : 0) |
1764                 (ctrl_dl->DCD ? TIOCM_CAR : 0) |
1765                 (ctrl_dl->RI  ? TIOCM_RNG : 0) |
1766                 (ctrl_dl->DSR ? TIOCM_DSR : 0) |
1767                 (ctrl_dl->CTS ? TIOCM_CTS : 0);
1768 }
1769
1770 /* Sets io controls parameters */
1771 static int ntty_tiocmset(struct tty_struct *tty, struct file *file,
1772         unsigned int set, unsigned int clear)
1773 {
1774         if (set & TIOCM_RTS)
1775                 set_rts(tty, 1);
1776         else if (clear & TIOCM_RTS)
1777                 set_rts(tty, 0);
1778
1779         if (set & TIOCM_DTR)
1780                 set_dtr(tty, 1);
1781         else if (clear & TIOCM_DTR)
1782                 set_dtr(tty, 0);
1783
1784         return 0;
1785 }
1786
1787 static int ntty_cflags_changed(struct port *port, unsigned long flags,
1788                 struct async_icount *cprev)
1789 {
1790         struct async_icount cnow = port->tty_icount;
1791         int ret;
1792
1793         ret =   ((flags & TIOCM_RNG) && (cnow.rng != cprev->rng)) ||
1794                 ((flags & TIOCM_DSR) && (cnow.dsr != cprev->dsr)) ||
1795                 ((flags & TIOCM_CD)  && (cnow.dcd != cprev->dcd)) ||
1796                 ((flags & TIOCM_CTS) && (cnow.cts != cprev->cts));
1797
1798         *cprev = cnow;
1799
1800         return ret;
1801 }
1802
1803 static int ntty_ioctl_tiocgicount(struct port *port, void __user *argp)
1804 {
1805         struct async_icount cnow = port->tty_icount;
1806         struct serial_icounter_struct icount;
1807
1808         icount.cts = cnow.cts;
1809         icount.dsr = cnow.dsr;
1810         icount.rng = cnow.rng;
1811         icount.dcd = cnow.dcd;
1812         icount.rx = cnow.rx;
1813         icount.tx = cnow.tx;
1814         icount.frame = cnow.frame;
1815         icount.overrun = cnow.overrun;
1816         icount.parity = cnow.parity;
1817         icount.brk = cnow.brk;
1818         icount.buf_overrun = cnow.buf_overrun;
1819
1820         return copy_to_user(argp, &icount, sizeof(icount));
1821 }
1822
1823 static int ntty_ioctl(struct tty_struct *tty, struct file *file,
1824                       unsigned int cmd, unsigned long arg)
1825 {
1826         struct port *port = tty->driver_data;
1827         void __user *argp = (void __user *)arg;
1828         int rval = -ENOIOCTLCMD;
1829
1830         DBG1("******** IOCTL, cmd: %d", cmd);
1831
1832         switch (cmd) {
1833         case TIOCMIWAIT: {
1834                 struct async_icount cprev = port->tty_icount;
1835
1836                 rval = wait_event_interruptible(port->tty_wait,
1837                                 ntty_cflags_changed(port, arg, &cprev));
1838                 break;
1839         } case TIOCGICOUNT:
1840                 rval = ntty_ioctl_tiocgicount(port, argp);
1841                 break;
1842         default:
1843                 DBG1("ERR: 0x%08X, %d", cmd, cmd);
1844                 break;
1845         };
1846
1847         return rval;
1848 }
1849
1850 /*
1851  * Called by the upper tty layer when tty buffers are ready
1852  * to receive data again after a call to throttle.
1853  */
1854 static void ntty_unthrottle(struct tty_struct *tty)
1855 {
1856         struct nozomi *dc = get_dc_by_tty(tty);
1857         unsigned long flags;
1858
1859         DBG1("UNTHROTTLE");
1860         spin_lock_irqsave(&dc->spin_mutex, flags);
1861         enable_transmit_dl(tty->index % MAX_PORT, dc);
1862         set_rts(tty, 1);
1863
1864         spin_unlock_irqrestore(&dc->spin_mutex, flags);
1865 }
1866
1867 /*
1868  * Called by the upper tty layer when the tty buffers are almost full.
1869  * The driver should stop send more data.
1870  */
1871 static void ntty_throttle(struct tty_struct *tty)
1872 {
1873         struct nozomi *dc = get_dc_by_tty(tty);
1874         unsigned long flags;
1875
1876         DBG1("THROTTLE");
1877         spin_lock_irqsave(&dc->spin_mutex, flags);
1878         set_rts(tty, 0);
1879         spin_unlock_irqrestore(&dc->spin_mutex, flags);
1880 }
1881
1882 /* just to discard single character writes */
1883 static void ntty_put_char(struct tty_struct *tty, unsigned char c)
1884 {
1885         /* FIXME !!! */
1886         DBG2("PUT CHAR Function: %c", c);
1887 }
1888
1889 /* Returns number of chars in buffer, called by tty layer */
1890 static s32 ntty_chars_in_buffer(struct tty_struct *tty)
1891 {
1892         struct port *port = tty->driver_data;
1893         struct nozomi *dc = get_dc_by_tty(tty);
1894         s32 rval;
1895
1896         if (unlikely(!dc || !port)) {
1897                 rval = -ENODEV;
1898                 goto exit_in_buffer;
1899         }
1900
1901         if (unlikely(!port->tty_open_count)) {
1902                 dev_err(&dc->pdev->dev, "No tty open?\n");
1903                 rval = -ENODEV;
1904                 goto exit_in_buffer;
1905         }
1906
1907         rval = __kfifo_len(port->fifo_ul);
1908
1909 exit_in_buffer:
1910         return rval;
1911 }
1912
1913 static struct tty_operations tty_ops = {
1914         .ioctl = ntty_ioctl,
1915         .open = ntty_open,
1916         .close = ntty_close,
1917         .write = ntty_write,
1918         .write_room = ntty_write_room,
1919         .unthrottle = ntty_unthrottle,
1920         .throttle = ntty_throttle,
1921         .chars_in_buffer = ntty_chars_in_buffer,
1922         .put_char = ntty_put_char,
1923         .tiocmget = ntty_tiocmget,
1924         .tiocmset = ntty_tiocmset,
1925 };
1926
1927 /* Module initialization */
1928 static struct pci_driver nozomi_driver = {
1929         .name = NOZOMI_NAME,
1930         .id_table = nozomi_pci_tbl,
1931         .probe = nozomi_card_init,
1932         .remove = __devexit_p(nozomi_card_exit),
1933 };
1934
1935 static __init int nozomi_init(void)
1936 {
1937         int ret;
1938
1939         printk(KERN_INFO "Initializing %s\n", VERSION_STRING);
1940
1941         ntty_driver = alloc_tty_driver(NTTY_TTY_MAXMINORS);
1942         if (!ntty_driver)
1943                 return -ENOMEM;
1944
1945         ntty_driver->owner = THIS_MODULE;
1946         ntty_driver->driver_name = NOZOMI_NAME_TTY;
1947         ntty_driver->name = "noz";
1948         ntty_driver->major = 0;
1949         ntty_driver->type = TTY_DRIVER_TYPE_SERIAL;
1950         ntty_driver->subtype = SERIAL_TYPE_NORMAL;
1951         ntty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
1952         ntty_driver->init_termios = tty_std_termios;
1953         ntty_driver->init_termios.c_cflag = B115200 | CS8 | CREAD | \
1954                                                 HUPCL | CLOCAL;
1955         ntty_driver->init_termios.c_ispeed = 115200;
1956         ntty_driver->init_termios.c_ospeed = 115200;
1957         tty_set_operations(ntty_driver, &tty_ops);
1958
1959         ret = tty_register_driver(ntty_driver);
1960         if (ret) {
1961                 printk(KERN_ERR "Nozomi: failed to register ntty driver\n");
1962                 goto free_tty;
1963         }
1964
1965         ret = pci_register_driver(&nozomi_driver);
1966         if (ret) {
1967                 printk(KERN_ERR "Nozomi: can't register pci driver\n");
1968                 goto unr_tty;
1969         }
1970
1971         return 0;
1972 unr_tty:
1973         tty_unregister_driver(ntty_driver);
1974 free_tty:
1975         put_tty_driver(ntty_driver);
1976         return ret;
1977 }
1978
1979 static __exit void nozomi_exit(void)
1980 {
1981         printk(KERN_INFO "Unloading %s\n", DRIVER_DESC);
1982         pci_unregister_driver(&nozomi_driver);
1983         tty_unregister_driver(ntty_driver);
1984         put_tty_driver(ntty_driver);
1985 }
1986
1987 module_init(nozomi_init);
1988 module_exit(nozomi_exit);
1989
1990 module_param(debug, int, S_IRUGO | S_IWUSR);
1991
1992 MODULE_LICENSE("Dual BSD/GPL");
1993 MODULE_DESCRIPTION(DRIVER_DESC);