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