patch for 2.6.22 - sent by Roberto
[nozomi.git] / nozomi.c
1 /* nozomi.c  -- HSDPA driver Broadband Wireless Data Card - Globe Trotter
2 *
3 * Written by: Ulf Jakobsson,
4 *             Jan �erfeldt,
5 *             Stefan Thomasson,
6 *
7 * Maintained by: Paul Hardwick (p.hardwick@option.com)
8 *
9 * Patches:
10 *          Locking code changes for Vodafone by Sphere Systems Ltd,
11 *                              Andrew Bird (ajb@spheresystems.co.uk )
12 *                              & Phil Sanderson
13 *
14 * Source has been ported from an implementation made by Filip Aben @ Option
15 *
16 * --------------------------------------------------------------------------
17
18 Copyright (c) 2005,2006 Option Wireless Sweden AB
19 Copyright (c) 2006 Sphere Systems Ltd
20 Copyright (c) 2006 Option Wireless n/v
21 All rights Reserved.
22
23 This program is free software; you can redistribute it and/or modify
24 it under the terms of the GNU General Public License as published by
25 the Free Software Foundation; either version 2 of the License, or
26 (at your option) any later version.
27
28 This program is distributed in the hope that it will be useful,
29 but WITHOUT ANY WARRANTY; without even the implied warranty of
30 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31 GNU General Public License for more details.
32
33 You should have received a copy of the GNU General Public License
34 along with this program; if not, write to the Free Software
35 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
36
37 * --------------------------------------------------------------------------
38 */
39
40 /* CHANGELOG
41 * Version 2.21alpha
42 * 17-Sept-2006 paul Hardwick
43 *       -Fixed unseen! bug in termio settings
44 *       -Removed more compile warnings
45 * Version 2.2alpha
46 * 10-Sept-2006 Paul Hardwick
47 *
48 * - A 2.6 kernel support version incorporating patches by GregKH
49 * - changes include:
50 * - module reference counting
51 * - improved device exit code
52 * - greater compliance to kernel guidelines
53 *
54 * - 2.4 kernels untested but included
55 * - Still supports 2.6.14 for Ubunto users.
56 * - Finally fixed compiler warnings
57
58 * Version 2.1
59 * 03-July-2006 Paul Hardwick
60 *
61 * - Stability Improvements. Incorporated spinlock wraps patch.
62 * - Updated for newer 2.6.14+ kernels (tty_buffer_request_room)
63 * - using __devexit macro for tty
64 *
65 *
66 * Version 2.0
67 * 08-feb-2006 15:34:10:Ulf
68 *
69 * -Fixed issue when not waking up line disipine layer, could probably result
70 * in better uplink performance for 2.4.
71 *
72 * -Fixed issue with big endian during initalization, now proper toggle flags
73 * are handled between preloader and maincode.
74 *
75 * -Fixed flow control issue.
76 *
77 * -Added support for setting DTR.
78 *
79 * -For 2.4 kernels, removing temporary buffer that's not needed.
80 *
81 * -Reading CTS only for modem port (only port that supports it).
82 *
83 * -Return 0 in write_room instead of netative value, it's not handled in
84 * upper layer.
85 *
86 * --------------------------------------------------------------------------
87 * Version 1.0
88 *
89 * First version of driver, only tested with card of type F32_2.
90 * Works fine with 2.4 and 2.6 kernels.
91 * Driver also support big endian architecture.
92 */
93
94 /* TODO
95 *
96 * See TODO file in this package
97 */
98
99 #include <linux/version.h>
100
101 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,14)
102 #define KERNEL_2_6_14
103 #endif
104 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17)
105 #define KERNEL_2_6_18
106 #endif
107
108
109 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0)
110 #define KERNEL_2_6
111 #elif LINUX_VERSION_CODE > KERNEL_VERSION(2,4,0)
112 #define KERNEL_2_4
113 #endif
114
115 #include <linux/module.h>
116 #include <linux/pci.h>
117 #include <linux/ioport.h>
118 #include <linux/tty.h>
119 #include <linux/tty_driver.h>
120 #include <linux/tty_flip.h>
121 #include <linux/serial.h>
122 #include <linux/interrupt.h>
123 #include <linux/kmod.h>
124 #include <linux/proc_fs.h>
125 #include <linux/init.h>
126 #ifdef KERNEL_2_6
127 #include <linux/kfifo.h>
128 #else
129 #include "kfifo.h"
130 #endif
131 #include <asm/uaccess.h>
132
133 #ifdef KERNEL_2_4
134
135 #ifndef IRQ_NONE
136 #define IRQ_NONE
137 #endif
138
139 #ifndef IRQ_HANDLED
140 #define IRQ_HANDLED
141 typedef void irqreturn_t;
142 #endif
143
144 #endif
145
146 #ifndef CONFIG_PCI
147 #error "This driver needs PCI support to be available"
148 #endif
149
150 #define VERSION_STRING DRIVER_DESC " 2.21alpha (build date: " __DATE__ " " __TIME__ ")"
151
152 /*    Macros definitions */
153
154 /* Enable this to have a lot of debug printouts */
155 /* #define NOZOMI_DEBUG */
156
157 /* Default debug printout level */
158 #define NOZOMI_DEBUG_LEVEL 0x1
159
160 #define P_BUF_SIZE 128
161 #define NFO( _err_flag_, args...)                                            \
162   do{                                                                        \
163     char t_m_p_[P_BUF_SIZE];                                                 \
164       snprintf(t_m_p_, sizeof(t_m_p_), ##args);                              \
165       printk( _err_flag_ "[%d] %s(): %s\n", __LINE__, __FUNCTION__, t_m_p_); \
166 } while(0)
167
168 #define ERR(args...)  NFO( KERN_ERR, ##args)
169
170 #define D1(args...) D_(0x01, ##args)
171 #define D2(args...) D_(0x02, ##args)
172 #define D3(args...) D_(0x04, ##args)
173 #define D4(args...) D_(0x08, ##args)
174 #define D5(args...) D_(0x10, ##args)
175 #define D6(args...) D_(0x20, ##args)
176 #define D7(args...) D_(0x40, ##args)
177 #define D8(args...) D_(0x80, ##args)
178
179 #ifdef NOZOMI_DEBUG
180 #define D_(lvl, args...) D(lvl, ##args)
181   /* Do we need this settable at runtime? */
182 static int nzdebug = NOZOMI_DEBUG_LEVEL;
183
184 #define D(lvl, args...)  do{if(lvl & nzdebug) NFO(KERN_DEBUG, ##args );}while(0)
185 #define D_(lvl, args...) D(lvl, ##args)
186
187 /* These printouts are always printed */
188
189 #else
190 static const int nzdebug = 0;
191 #define D_(lvl, args...)
192 #endif
193
194 /* TODO: rewrite to optimize macros... */
195 #define SET_FCR(value__) \
196   do {  \
197     writew((value__), (dc->REG_FCR )); \
198 } while(0)
199
200 #define SET_IER(value__, mask__) \
201   do {  \
202     dc->ier_last_written = (dc->ier_last_written & ~mask__) | (value__ & mask__ );\
203     writew( dc->ier_last_written, (dc->REG_IER));\
204 } while(0)
205
206 #define GET_IER(read_val__) \
207   do {  \
208     (read_val__) = readw((dc->REG_IER));\
209 } while(0)
210
211 #define GET_IIR(read_val__) \
212   do {  \
213     (read_val__) = readw( (dc->REG_IIR));\
214 } while(0)
215
216 #define GET_MEM(value__, addr__, length__) \
217   do {  \
218 /*    read_mem32( (u32*) (value__), (u32) (addr__), (length__)); */\
219     read_mem32( (u32*) (value__), (addr__), (length__));\
220 } while(0)
221
222 #define GET_MEM_BUF(value__, addr__, length__) \
223   do {  \
224 /*    read_mem32_buf( (u32*) (value__), (u32) (addr__), (length__)); */\
225     read_mem32_buf( (u32*) (value__), (addr__), (length__));\
226 } while(0)
227
228 #define SET_MEM(addr__, value__, length__) \
229   do {  \
230   write_mem32( (addr__),  (u32*) (value__), (length__));\
231 } while(0)
232
233 #define SET_MEM_BUF(addr__, value__, length__) \
234   do {  \
235   write_mem32_buf( (addr__),  (u32*) (value__), (length__));\
236 } while(0)
237
238 #define TMP_BUF_MAX 256
239
240 #define DUMP(buf__,len__) \
241   do {  \
242     char tbuf[TMP_BUF_MAX]={0};\
243     if (len__>1) {\
244         snprintf(tbuf, len__ > TMP_BUF_MAX ? TMP_BUF_MAX : len__, "%s",buf__);\
245         if(tbuf[len__-2] == '\r') {\
246             tbuf[len__-2] = 'r';\
247         }\
248         D1( "SENDING: '%s' (%d+n)", tbuf, len__);\
249     } else {\
250         D1( "SENDING: '%s' (%d)", tbuf, len__);\
251     }\
252 } while(0)
253
254 #define RELEVANT_IFLAG(iflag) ((iflag) & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
255
256 /*    Defines */
257 #define NOZOMI_NAME     "nozomi"
258 #define NOZOMI_NAME_TTY "nozomi_tty"
259 #define DRIVER_DESC     "Nozomi driver"
260
261 #define NTTY_TTY_MAJOR                  241
262 #define NTTY_TTY_MINORS                 MAX_PORT
263 #define NTTY_FIFO_BUFFER_SIZE   8192
264
265 /* Must be power of 2 */
266 #define FIFO_BUFFER_SIZE_UL     8192
267
268 /* Size of tmp send buffer to card */
269 #define SEND_BUF_MAX    1024
270 #define RECEIVE_BUF_MAX 4
271
272 /* Our fake UART values */
273 #define MCR_DTR         0x01
274 #define MCR_RTS         0x02
275 #define MCR_LOOP        0x04
276 #define MSR_CTS         0x08
277 #define MSR_CD          0x10
278 #define MSR_RI          0x20
279 #define MSR_DSR         0x40
280
281 /* Define all types of vendors and devices to support */
282 #define VENDOR1          0x1931 /* Vendor Option */
283 #define DEVICE1          0x000c /* HSDPA card */
284
285 #define R_IIR            0x0000 /* Interrupt Identity Register */
286 #define R_FCR            0x0000 /* Flow Control Register */
287 #define R_IER            0x0004 /* Interrupt Enable Register */
288
289 #define CONFIG_MAGIC 0xEFEFFEFE
290 #define TOGGLE_VALID 0x0000
291
292 /* Definition of interrupt tokens */
293 #define MDM_DL1  0x0001
294 #define MDM_UL1  0x0002
295 #define MDM_DL2  0x0004
296 #define MDM_UL2  0x0008
297 #define DIAG_DL1 0x0010
298 #define DIAG_DL2 0x0020
299 #define DIAG_UL  0x0040
300 #define APP1_DL  0x0080
301 #define APP1_UL  0x0100
302 #define APP2_DL  0x0200
303 #define APP2_UL  0x0400
304 #define CTRL_DL  0x0800
305 #define CTRL_UL  0x1000
306 #define RESET    0x8000
307
308 #define MDM_DL  (MDM_DL1  | MDM_DL2)
309 #define MDM_UL  (MDM_UL1  | MDM_UL2)
310 #define DIAG_DL (DIAG_DL1 | DIAG_DL2)
311
312 /* modem signal definition */
313 #define CTRL_DSR 0x0001
314 #define CTRL_DCD 0x0002
315 #define CTRL_RI  0x0004
316 #define CTRL_CTS 0x0008
317
318 #define CTRL_DTR 0x0001
319 #define CTRL_RTS 0x0002
320
321 #define MAX_PORT 4
322 #define NOZOMI_MAX_PORTS 5
323
324 /*    Type definitions */
325
326 /* There are two types of nozomi cards, one with 2048 memory and with 8192 memory */
327 enum card_type {
328         F32_2 = 2048,           /* Has 512 bytes downlink and uplink * 2             -> 2048 */
329         F32_8 = 8192,           /* Has 3072 bytes downlink and 1024 bytes uplink * 2 -> 8192 */
330 };
331
332 /* Two different toggle channels exist */
333 enum channel_type {
334         CH_A = 0,
335         CH_B = 1,
336 };
337
338 /* Port definition for the card regarding flow control */
339 enum ctrl_port_type {
340         CTRL_CMD = 0x00,
341         CTRL_MDM = 0x01,
342         CTRL_DIAG = 0x02,
343         CTRL_APP1 = 0x03,
344         CTRL_APP2 = 0x04,
345         CTRL_ERROR = -1,
346 };
347
348 /* Ports that the nozomi has */
349 enum port_type {
350         PORT_MDM = 0,
351         PORT_DIAG = 1,
352         PORT_APP1 = 2,
353         PORT_APP2 = 3,
354         PORT_CTRL = 4,
355         PORT_ERROR = -1,
356 };
357
358 #ifdef __ARMEB__
359 /* Big endian */
360
361 typedef struct {
362         unsigned enabled:5;     /* Toggle fields are valid if enabled is 0, else A-channels
363                                    must always be used. */
364         unsigned diag_dl:1;
365         unsigned mdm_dl:1;
366         unsigned mdm_ul:1;
367 } __attribute__ ((packed)) toggles_t;
368
369 /* Configuration table to read at startup of card */
370 /* Is for now only needed during initialization phase */
371 typedef struct {
372         u32 signature;
373         u16 product_information;
374         u16 version;
375         u8 pad3[3];
376         toggles_t toggle;
377         u8 pad1[4];
378         u16 dl_mdm_len1;        /* If this is 64, it can hold 60 bytes + 4 that is length field */
379         u16 dl_start;
380
381         u16 dl_diag_len1;
382         u16 dl_mdm_len2;        /* If this is 64, it can hold 60 bytes + 4 that is length field */
383         u16 dl_app1_len;
384
385         u16 dl_diag_len2;
386         u16 dl_ctrl_len;
387         u16 dl_app2_len;
388         u8 pad2[16];
389         u16 ul_mdm_len1;
390         u16 ul_start;
391         u16 ul_diag_len;
392         u16 ul_mdm_len2;
393         u16 ul_app1_len;
394         u16 ul_app2_len;
395         u16 ul_ctrl_len;
396 } __attribute__ ((packed)) config_table_t;
397
398 /* This stores all control downlink flags */
399 typedef struct {
400         u8 port;
401         unsigned reserved:4;
402         unsigned CTS:1;
403         unsigned RI:1;
404         unsigned DCD:1;
405         unsigned DSR:1;
406 } __attribute__ ((packed)) ctrl_dl_t;
407
408 /* This stores all control uplink flags */
409 typedef struct {
410         u8 port;
411         unsigned reserved:6;
412         unsigned RTS:1;
413         unsigned DTR:1;
414 } __attribute__ ((packed)) ctrl_ul_t;
415
416 #else
417 /* Little endian */
418
419 /* This represents the toggle information */
420 typedef struct {
421         unsigned mdm_ul:1;
422         unsigned mdm_dl:1;
423         unsigned diag_dl:1;
424         unsigned enabled:5;     /* Toggle fields are valid if enabled is 0, else A-channels
425                                    must always be used. */
426 } __attribute__ ((packed)) toggles_t;
427
428 /* Configuration table to read at startup of card */
429 typedef struct {
430         u32 signature;
431         u16 version;
432         u16 product_information;
433         toggles_t toggle;
434         u8 pad1[7];
435         u16 dl_start;
436         u16 dl_mdm_len1;        /* If this is 64, it can hold 60 bytes + 4 that is length field */
437         u16 dl_mdm_len2;
438         u16 dl_diag_len1;
439         u16 dl_diag_len2;
440         u16 dl_app1_len;
441         u16 dl_app2_len;
442         u16 dl_ctrl_len;
443         u8 pad2[16];
444         u16 ul_start;
445         u16 ul_mdm_len2;
446         u16 ul_mdm_len1;
447         u16 ul_diag_len;
448         u16 ul_app1_len;
449         u16 ul_app2_len;
450         u16 ul_ctrl_len;
451 } __attribute__ ((packed)) config_table_t;
452
453 /* This stores all control downlink flags */
454 typedef struct {
455         unsigned DSR:1;
456         unsigned DCD:1;
457         unsigned RI:1;
458         unsigned CTS:1;
459         unsigned reserverd:4;
460         u8 port;
461 } __attribute__ ((packed)) ctrl_dl_t;
462
463 /* This stores all control uplink flags */
464 typedef struct {
465         unsigned DTR:1;
466         unsigned RTS:1;
467         unsigned reserved:6;
468         u8 port;
469 } __attribute__ ((packed)) ctrl_ul_t;
470 #endif
471
472 /* This holds all information that is needed regarding a port */
473 struct port {
474         u8 update_flow_control;
475         ctrl_ul_t ctrl_ul;
476         ctrl_dl_t ctrl_dl;
477         struct kfifo *fifo_ul;
478         void __iomem *dl_addr[2];
479         u32 dl_size[2];
480         u8 toggle_dl;
481         void __iomem *ul_addr[2];
482         u32 ul_size[2];
483         u8 toggle_ul;
484         u16 token_dl;
485
486         struct tty_struct *tty;
487         int tty_open_count;
488         struct semaphore tty_sem;
489         wait_queue_head_t tty_wait;
490         struct async_icount tty_icount;
491         int tty_index;
492         u32 rx_data, tx_data;
493         u8 tty_dont_flip;
494
495 };
496
497 /* Private data one for each card in the system */
498 typedef struct {
499         void __iomem *base_addr;
500         u8 closing;
501
502         /* Pointers to registers */
503         void __iomem *REG_IIR;
504         void __iomem *REG_FCR;
505         void __iomem *REG_IER;
506
507         u16 ier_last_written;
508         enum card_type card_type;
509         config_table_t config_table;    /* Configuration table */
510         struct pci_dev *pdev;
511         struct port port[NOZOMI_MAX_PORTS];
512         u8 *send_buf;
513
514         struct tty_driver *tty_driver;
515
516 #ifdef KERNEL_2_4
517         struct tty_struct *tty_table[NTTY_TTY_MINORS];
518         struct tq_struct tty_flip_queue;
519         s32 tty_refcount;
520 #endif
521 #ifdef KERNEL_2_6
522         struct workqueue_struct *tty_flip_wq;
523         struct work_struct tty_flip_wq_struct;
524 #endif
525
526         struct termios *tty_termios[NTTY_TTY_MINORS];
527         struct termios *tty_termios_locked[NTTY_TTY_MINORS];
528         spinlock_t spin_mutex;
529
530         u32 open_ttys;
531         struct proc_dir_entry *proc_entry;
532
533 } dc_t;
534
535 /* This is a data packet that is read or written to/from card */
536 typedef struct {
537         u32 size;               /* size is the length of the data buffer */
538         u8 *data;
539 } __attribute__ ((packed)) buf_t;
540
541 /*    Function declarations */
542 static int ntty_tty_init(dc_t * dc);
543 static void tty_flip_queue_function(void *tmp_dc);
544
545 /*    Global variables */
546 static struct pci_device_id nozomi_pci_tbl[] = {
547         {VENDOR1, DEVICE1},
548         {0,}
549 };
550
551 /* Used to store interrupt variables */
552 typedef struct {
553         u16 read_iir;           /* Holds current interrupt tokens */
554 } irq_t;
555
556 MODULE_DEVICE_TABLE(pci, nozomi_pci_tbl);
557
558 /* Representing the pci device of interest */
559 static int cards_found;
560 static dc_t *my_dev = NULL;
561 static irq_t my_irq;
562
563 static inline dc_t *get_dc_by_pdev(struct pci_dev *pdev)
564 {
565         return my_dev;
566 }
567
568 static inline dc_t *get_dc_by_index(s32 index)
569 {
570         return my_dev;
571 }
572
573 static inline s32 get_index(struct tty_struct *tty)
574 {
575 #ifdef KERNEL_2_6
576         return tty->index;
577 #else
578         return MINOR(tty->device) - tty->driver.minor_start;
579 #endif
580
581 }
582
583 static inline struct port *get_port_by_tty(struct tty_struct *tty)
584 {
585         return &my_dev->port[get_index(tty)];
586 }
587
588 static inline dc_t *get_dc_by_tty(struct tty_struct *tty)
589 {
590         return my_dev;
591 }
592
593 /* TODO: */
594 /* -Optimize */
595 /* -Rewrite cleaner */
596 //static void read_mem32(u32 *buf, u32 mem_addr_start, u32 size_bytes) {
597 static void read_mem32(u32 * buf, void __iomem * mem_addr_start, u32 size_bytes)
598 {
599         u32 i = 0;
600         u32 *ptr = (__force u32 *) mem_addr_start;
601         u16 *buf16;
602
603         /* 2 bytes */
604         if (size_bytes == 2) {
605                 buf16 = (u16 *) buf;
606                 *buf16 = readw((void __iomem *)ptr);
607                 return;
608         }
609
610         while (i < size_bytes) {
611                 if (size_bytes - i == 2) {
612                         /* Handle 2 bytes in the end */
613                         buf16 = (u16 *) buf;
614                         *(buf16) = readw((void __iomem *)ptr);
615                         i += 2;
616                 } else {
617                         /* Read 4 bytes */
618                         *(buf) = readl((void __iomem *)ptr);
619                         i += 4;
620                 }
621                 buf++;
622                 ptr++;
623         }
624 }
625
626 /* TODO: */
627 /* - Rewrite cleaner */
628 /* - merge with read_mem32() */
629 //static void read_mem32_buf(u32 *buf, u32 mem_addr_start, u32 size_bytes) {
630 static void read_mem32_buf(u32 * buf, void __iomem * mem_addr_start,
631                            u32 size_bytes)
632 {
633 #ifdef __ARMEB__
634         u32 i = 0;
635         u32 *ptr = (u32 *) mem_addr_start;
636         u16 *buf16;
637
638         /* 2 bytes */
639         if (size_bytes == 2) {
640                 buf16 = (u16 *) buf;
641                 *buf16 = __le16_to_cpu(readw(ptr));
642                 return;
643         }
644
645         while (i < size_bytes) {
646                 if (size_bytes - i == 2) {
647                         /* Handle 2 bytes in the end */
648                         buf16 = (u16 *) buf;
649                         *(buf16) = __le16_to_cpu(readw(ptr));
650                         i += 2;
651                 } else {
652                         /* Read 4 bytes */
653                         *(buf) = __le32_to_cpu(readl(ptr));
654                         i += 4;
655                 }
656                 buf++;
657                 ptr++;
658         }
659 #else
660         read_mem32(buf, mem_addr_start, size_bytes);
661 #endif
662 }
663
664 /* TODO: */
665 /* -Optimize */
666 /* -Rewrite cleaner */
667 //static u32 write_mem32(u32 mem_addr_start, u32 *buf, u32 size_bytes) {
668 static u32 write_mem32(void __iomem * mem_addr_start, u32 * buf, u32 size_bytes)
669 {
670         u32 i = 0;
671         u32 *ptr = (__force u32 *) mem_addr_start;
672         u16 *buf16;
673
674         /* 2 bytes */
675         if (size_bytes == 2) {
676                 buf16 = (u16 *) buf;
677                 writew(*buf16, (void __iomem *)ptr);
678                 return 2;
679         }
680
681         while (i < size_bytes) {
682                 if (size_bytes - i == 2) {
683                         /* 2 bytes */
684                         buf16 = (u16 *) buf;
685                         writew(*buf16, (void __iomem *)ptr);
686                         i += 2;
687                 } else {
688                         /* 4 bytes */
689                         writel(*buf, (void __iomem *)ptr);
690                         i += 4;
691                 }
692                 buf++;
693                 ptr++;
694         }
695         return size_bytes;
696 }
697
698 /* Todo: */
699 /* - Merge with write_mem32() */
700 //static u32 write_mem32_buf(u32 mem_addr_start, u32 *buf, u32 size_bytes) {
701 static u32 write_mem32_buf(void __iomem * mem_addr_start, u32 * buf,
702                            u32 size_bytes)
703 {
704 #ifdef __ARMEB__
705         u32 i = 0;
706         u32 *ptr = (u32 *) mem_addr_start;
707         u16 *buf16;
708
709         /* 2 bytes */
710         if (size_bytes == 2) {
711                 buf16 = (u16 *) buf;
712                 writew(__le16_to_cpu(*buf16), ptr);
713                 return 2;
714         }
715
716         while (i < size_bytes) {
717                 if (size_bytes - i == 2) {
718                         /* 2 bytes */
719                         buf16 = (u16 *) buf;
720                         writew(__le16_to_cpu(*buf16), ptr);
721                         i += 2;
722                 } else {
723                         /* 4 bytes */
724                         writel(__cpu_to_le32(*buf), ptr);
725                         i += 4;
726                 }
727                 buf++;
728                 ptr++;
729         }
730         return size_bytes;
731 #else
732         return write_mem32(mem_addr_start, buf, size_bytes);
733 #endif
734 }
735
736 /* Setup pointers to different channels and also setup buffer sizes. */
737 static void setup_memory(dc_t * dc)
738 {
739         void __iomem *offset = dc->base_addr + dc->config_table.dl_start;
740         /* The length reported is including the length field of 4 bytes, hence subtract with 4. */
741         u16 buff_offset = 4;
742
743         /* Modem port dl configuration */
744         dc->port[PORT_MDM].dl_addr[CH_A] = offset;
745         dc->port[PORT_MDM].dl_addr[CH_B] = (offset +=
746                                             dc->config_table.dl_mdm_len1);
747         dc->port[PORT_MDM].dl_size[CH_A] =
748             dc->config_table.dl_mdm_len1 - buff_offset;
749         dc->port[PORT_MDM].dl_size[CH_B] =
750             dc->config_table.dl_mdm_len2 - buff_offset;
751
752         /* Diag port dl configuration */
753         dc->port[PORT_DIAG].dl_addr[CH_A] = (offset +=
754                                              dc->config_table.dl_mdm_len2);
755         dc->port[PORT_DIAG].dl_size[CH_A] =
756             dc->config_table.dl_diag_len1 - buff_offset;
757         dc->port[PORT_DIAG].dl_addr[CH_B] = (offset +=
758                                              dc->config_table.dl_diag_len1);
759         dc->port[PORT_DIAG].dl_size[CH_B] =
760             dc->config_table.dl_diag_len2 - buff_offset;
761
762         /* App1 port dl configuration */
763         dc->port[PORT_APP1].dl_addr[CH_A] = (offset +=
764                                              dc->config_table.dl_diag_len2);
765         dc->port[PORT_APP1].dl_size[CH_A] =
766             dc->config_table.dl_app1_len - buff_offset;
767
768         /* App2 port dl configuration */
769         dc->port[PORT_APP2].dl_addr[CH_A] = (offset +=
770                                              dc->config_table.dl_app1_len);
771         dc->port[PORT_APP2].dl_size[CH_A] =
772             dc->config_table.dl_app2_len - buff_offset;
773
774         /* Ctrl dl configuration */
775         dc->port[PORT_CTRL].dl_addr[CH_A] = (offset +=
776                                              dc->config_table.dl_app2_len);
777         dc->port[PORT_CTRL].dl_size[CH_A] =
778             dc->config_table.dl_ctrl_len - buff_offset;
779
780         /* Modem Port ul configuration */
781         dc->port[PORT_MDM].ul_addr[CH_A] = (offset =
782                                             dc->base_addr +
783                                             dc->config_table.ul_start);
784         dc->port[PORT_MDM].ul_size[CH_A] =
785             dc->config_table.ul_mdm_len1 - buff_offset;
786         dc->port[PORT_MDM].ul_addr[CH_B] = (offset +=
787                                             dc->config_table.ul_mdm_len1);
788         dc->port[PORT_MDM].ul_size[CH_B] =
789             dc->config_table.ul_mdm_len2 - buff_offset;
790
791         /* Diag port ul configuration */
792         dc->port[PORT_DIAG].ul_addr[CH_A] = (offset +=
793                                              dc->config_table.ul_mdm_len2);
794         dc->port[PORT_DIAG].ul_size[CH_A] =
795             dc->config_table.ul_diag_len - buff_offset;
796
797         /* App1 port ul configuration */
798         dc->port[PORT_APP1].ul_addr[CH_A] = (offset +=
799                                              dc->config_table.ul_diag_len);
800         dc->port[PORT_APP1].ul_size[CH_A] =
801             dc->config_table.ul_app1_len - buff_offset;
802
803         /* App2 port ul configuration */
804         dc->port[PORT_APP2].ul_addr[CH_A] = (offset +=
805                                              dc->config_table.ul_app1_len);
806         dc->port[PORT_APP2].ul_size[CH_A] =
807             dc->config_table.ul_app2_len - buff_offset;
808
809         /* Ctrl ul configuration */
810         dc->port[PORT_CTRL].ul_addr[CH_A] = (offset +=
811                                              dc->config_table.ul_app2_len);
812         dc->port[PORT_CTRL].ul_size[CH_A] =
813             dc->config_table.ul_ctrl_len - buff_offset;
814 //    offset = dc->config_table.ul_start;
815 }
816
817 /* Dump config table under initalization phase */
818 #ifdef NOZOMI_DEBUG
819 static void dump_table(dc_t * dc)
820 {
821         D3("signature: 0x%08X", dc->config_table.signature);
822         D3("version: 0x%04X", dc->config_table.version);
823         D3("product_information: 0x%04X", dc->config_table.product_information);
824         D3("toggle enabled: %d", dc->config_table.toggle.enabled);
825         D3("toggle up_mdm: %d", dc->config_table.toggle.mdm_ul);
826         D3("toggle dl_mdm: %d", dc->config_table.toggle.mdm_dl);
827         D3("toggle dl_dbg: %d", dc->config_table.toggle.diag_dl);
828
829         D3("dl_start: 0x%04X", dc->config_table.dl_start);
830         D3("dl_mdm_len0: 0x%04X, %d", dc->config_table.dl_mdm_len1,
831            dc->config_table.dl_mdm_len1);
832         D3("dl_mdm_len1: 0x%04X, %d", dc->config_table.dl_mdm_len2,
833            dc->config_table.dl_mdm_len2);
834         D3("dl_diag_len0: 0x%04X, %d", dc->config_table.dl_diag_len1,
835            dc->config_table.dl_diag_len1);
836         D3("dl_diag_len1: 0x%04X, %d", dc->config_table.dl_diag_len2,
837            dc->config_table.dl_diag_len2);
838         D3("dl_app1_len: 0x%04X, %d", dc->config_table.dl_app1_len,
839            dc->config_table.dl_app1_len);
840         D3("dl_app2_len: 0x%04X, %d", dc->config_table.dl_app2_len,
841            dc->config_table.dl_app2_len);
842         D3("dl_ctrl_len: 0x%04X, %d", dc->config_table.dl_ctrl_len,
843            dc->config_table.dl_ctrl_len);
844         D3("ul_start: 0x%04X, %d", dc->config_table.ul_start,
845            dc->config_table.ul_start);
846         D3("ul_mdm_len[0]: 0x%04X, %d", dc->config_table.ul_mdm_len1,
847            dc->config_table.ul_mdm_len1);
848         D3("ul_mdm_len[1]: 0x%04X, %d", dc->config_table.ul_mdm_len2,
849            dc->config_table.ul_mdm_len2);
850         D3("ul_diag_len: 0x%04X, %d", dc->config_table.ul_diag_len,
851            dc->config_table.ul_diag_len);
852         D3("ul_app1_len: 0x%04X, %d", dc->config_table.ul_app1_len,
853            dc->config_table.ul_app1_len);
854         D3("ul_app2_len: 0x%04X, %d", dc->config_table.ul_app2_len,
855            dc->config_table.ul_app2_len);
856         D3("ul_ctrl_len: 0x%04X, %d", dc->config_table.ul_ctrl_len,
857            dc->config_table.ul_ctrl_len);
858 }
859 #endif
860
861 /* Read configuration table from card under intalization phase */
862 /* Returns 1 if ok, else 0 */
863 static int nozomi_read_config_table(dc_t * dc)
864 {
865
866         GET_MEM(&dc->config_table, dc->base_addr + 0, sizeof(config_table_t));
867
868         /* D1( "0x%08X == 0x%08X ", dc->config_table.signature, CONFIG_MAGIC); */
869
870         if (dc->config_table.signature != CONFIG_MAGIC) {
871                 dev_err(&dc->pdev->dev, "ConfigTable Bad! 0x%08X != 0x%08X\n",
872                         dc->config_table.signature, CONFIG_MAGIC);
873                 return 0;
874         }
875
876         if ((dc->config_table.version == 0)
877             || (dc->config_table.toggle.enabled == TOGGLE_VALID)) {
878                 int i;
879                 D1("Second phase, configuring card");
880
881                 setup_memory(dc);
882
883                 dc->port[PORT_MDM].toggle_ul = dc->config_table.toggle.mdm_ul;
884                 dc->port[PORT_MDM].toggle_dl = dc->config_table.toggle.mdm_dl;
885                 dc->port[PORT_DIAG].toggle_dl = dc->config_table.toggle.diag_dl;
886                 D1("toggle ports: MDM UL:%d MDM DL:%d, DIAG DL:%d",
887                    dc->port[PORT_MDM].toggle_ul,
888                    dc->port[PORT_MDM].toggle_dl, dc->port[PORT_DIAG].toggle_dl);
889
890 #ifdef NOZOMI_DEBUG
891                 dump_table(dc);
892 #endif
893                 for (i = PORT_MDM; i < MAX_PORT; i++) {
894                         dc->port[i].fifo_ul =
895                             kfifo_alloc(FIFO_BUFFER_SIZE_UL, GFP_ATOMIC, NULL);
896                         memset(&dc->port[i].ctrl_dl, 0, sizeof(ctrl_dl_t));
897                         memset(&dc->port[i].ctrl_ul, 0, sizeof(ctrl_ul_t));
898                 }
899
900                 /* Enable control channel */
901                 SET_IER(CTRL_DL, CTRL_DL);
902
903                 dev_info(&dc->pdev->dev, "Initialization OK!\n");
904                 return 1;
905         }
906
907         if ((dc->config_table.version > 0)
908             && (dc->config_table.toggle.enabled != TOGGLE_VALID)) {
909                 u32 offset = 0;
910                 D1("First phase: pushing upload buffers, clearing download");
911
912                 dev_info(&dc->pdev->dev, "Version of card: %d\n",
913                          dc->config_table.version);
914
915                 /* Here we should disable all I/O over F32. */
916                 setup_memory(dc);
917
918                 /* We should send ALL channel pair tokens back along with reset token */
919
920                 /* push upload modem buffers */
921                 SET_MEM(dc->port[PORT_MDM].ul_addr[CH_A], &offset, 4);
922                 SET_MEM(dc->port[PORT_MDM].ul_addr[CH_B], &offset, 4);
923
924                 SET_FCR(MDM_UL | DIAG_DL | MDM_DL);
925
926                 D1("First phase done");
927         }
928
929         return 1;
930 }
931
932 /* Enable uplink interrupts  */
933 static void enable_transmit_ul(enum port_type port, dc_t * dc)
934 {
935
936         switch (port) {
937         case PORT_MDM:
938                 SET_IER(MDM_UL, MDM_UL);
939                 break;
940         case PORT_DIAG:
941                 SET_IER(DIAG_UL, DIAG_UL);
942                 break;
943         case PORT_APP1:
944                 SET_IER(APP1_UL, APP1_UL);
945                 break;
946         case PORT_APP2:
947                 SET_IER(APP2_UL, APP2_UL);
948                 break;
949         case PORT_CTRL:
950                 SET_IER(CTRL_UL, CTRL_UL);
951                 break;
952         default:
953                 dev_err(&dc->pdev->dev, "Called with wrong port?\n");
954                 break;
955         };
956 }
957
958 /* Disable uplink interrupts  */
959 static void disable_transmit_ul(enum port_type port, dc_t * dc)
960 {
961         switch (port) {
962         case PORT_MDM:
963                 SET_IER(0, MDM_UL);
964                 break;
965         case PORT_DIAG:
966                 SET_IER(0, DIAG_UL);
967                 break;
968         case PORT_APP1:
969                 SET_IER(0, APP1_UL);
970                 break;
971         case PORT_APP2:
972                 SET_IER(0, APP2_UL);
973                 break;
974         case PORT_CTRL:
975                 SET_IER(0, CTRL_UL);
976                 break;
977         default:
978                 dev_err(&dc->pdev->dev, "Called with wrong port?\n");
979                 break;
980         };
981 }
982
983 /* Enable downlink interrupts */
984 static void enable_transmit_dl(enum port_type port, dc_t * dc)
985 {
986         switch (port) {
987         case PORT_MDM:
988                 SET_IER(MDM_DL, MDM_DL);
989                 break;
990         case PORT_DIAG:
991                 SET_IER(DIAG_DL, DIAG_DL);
992                 break;
993         case PORT_APP1:
994                 SET_IER(APP1_DL, APP1_DL);
995                 break;
996         case PORT_APP2:
997                 SET_IER(APP2_DL, APP2_DL);
998                 break;
999         case PORT_CTRL:
1000                 SET_IER(CTRL_DL, CTRL_DL);
1001                 break;
1002         default:
1003                 dev_err(&dc->pdev->dev, "Called with wrong port?\n");
1004                 break;
1005         };
1006 }
1007
1008 /* Disable downlink interrupts */
1009 static void disable_transmit_dl(enum port_type port, dc_t * dc)
1010 {
1011         switch (port) {
1012         case PORT_MDM:
1013                 SET_IER(0, MDM_DL);
1014                 break;
1015         case PORT_DIAG:
1016                 SET_IER(0, DIAG_DL);
1017                 break;
1018         case PORT_APP1:
1019                 SET_IER(0, APP1_DL);
1020                 break;
1021         case PORT_APP2:
1022                 SET_IER(0, APP2_DL);
1023                 break;
1024         case PORT_CTRL:
1025                 SET_IER(0, CTRL_DL);
1026                 break;
1027         default:
1028                 dev_err(&dc->pdev->dev, "Called with wrong port?\n");
1029                 break;
1030         };
1031 }
1032
1033 /* Return 1 - send buffer to card and ack. */
1034 /* Return 0 - don't ack, don't send buffer to card. */
1035 static int send_data(enum port_type index, dc_t * dc)
1036 {
1037         u32 size = 0;
1038         struct port *port = &dc->port[index];
1039         u8 toggle = port->toggle_ul;
1040         void __iomem *addr = port->ul_addr[toggle];
1041         u32 ul_size = port->ul_size[toggle];
1042         struct tty_struct *tty = port->tty;
1043
1044         if (index >= NTTY_TTY_MINORS) {
1045                 dev_err(&dc->pdev->dev, "Called with wrong index?\n");
1046                 return 0;
1047         }
1048
1049         /* Get data from tty and place in buf for now */
1050         size =
1051             __kfifo_get(port->fifo_ul, dc->send_buf,
1052                         ul_size < SEND_BUF_MAX ? ul_size : SEND_BUF_MAX);
1053
1054         if (size == 0) {
1055                 D4("No more data to send, disable link:");
1056                 return 0;
1057         }
1058
1059         port->tx_data += size;
1060
1061         /* DUMP(buf, size); */
1062
1063         /* Write length + data */
1064         SET_MEM(addr, &size, 4);
1065         SET_MEM_BUF(addr + 4, dc->send_buf, size);
1066
1067         if (port->tty) {
1068                 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP))
1069                     && tty->ldisc.write_wakeup) {
1070                         tty->ldisc.write_wakeup(tty);
1071                 }
1072                 wake_up_interruptible(&tty->write_wait);
1073         }
1074
1075         return 1;
1076 }
1077
1078 /* If all data has been read, return 1, else 0 */
1079 static int receive_data(enum port_type index, dc_t * dc)
1080 {
1081         u8 buf[RECEIVE_BUF_MAX] = { 0 };
1082         int size;
1083         u32 offset = 4;
1084         struct port *port = &dc->port[index];
1085         u8 toggle = port->toggle_dl;
1086         void __iomem *addr = port->dl_addr[toggle];
1087         struct tty_struct *tty = port->tty;
1088         int i;
1089
1090         if (!tty) {
1091                 D1("tty not open for port: %d?", index);
1092                 return 1;
1093         }
1094
1095         GET_MEM(&size, addr, 4);
1096         /*  D1( "%d bytes port: %d", size, index); */
1097
1098         if (test_bit(TTY_THROTTLED, &tty->flags)) {
1099                 D1("No room in tty, don't read data, don't ack interrupt, disable interrupt");
1100
1101                 /* disable interrupt in downlink... */
1102                 disable_transmit_dl(index, dc);
1103 #ifdef KERNEL_2_4
1104                 schedule_task(&dc->tty_flip_queue);
1105 #endif
1106                 return 0;
1107         }
1108
1109         if (size == 0) {
1110                 dev_err(&dc->pdev->dev, "size == 0?\n");
1111                 return 1;
1112         }
1113
1114         while (size > 0) {
1115                 GET_MEM_BUF(buf, addr + offset, 4);
1116
1117                 i = 0;
1118                 while (i < 4 && size > 0) {
1119 #ifdef KERNEL_2_6_14
1120                         if (tty_buffer_request_room(tty, 1) < 1)
1121 #else
1122                         if (tty->flip.count >= TTY_FLIPBUF_SIZE)        //pre 2.6.14 kernels*/
1123 #endif
1124                         {
1125                                 tty_flip_buffer_push(tty);
1126                         }
1127                         tty_insert_flip_char(tty, buf[i], TTY_NORMAL);
1128                         port->rx_data++;
1129                         i++;
1130                         size--;
1131                 }
1132
1133                 offset += 4;
1134         }
1135
1136         tty_flip_buffer_push(tty);
1137
1138         return 1;
1139 }
1140
1141 /* Debug for interrupts */
1142 #ifdef NOZOMI_DEBUG
1143 static char *interrupt2str(u16 interrupt)
1144 {
1145         static char buf[TMP_BUF_MAX];
1146         char *p = buf;
1147
1148         interrupt & MDM_DL1 ? p += snprintf(p, TMP_BUF_MAX, "MDM_DL1 ") : 0;
1149         interrupt & MDM_DL2 ? p += snprintf(p, TMP_BUF_MAX, "MDM_DL2 ") : 0;
1150
1151         interrupt & MDM_UL1 ? p += snprintf(p, TMP_BUF_MAX, "MDM_UL1 ") : 0;
1152         interrupt & MDM_UL2 ? p += snprintf(p, TMP_BUF_MAX, "MDM_UL2 ") : 0;
1153
1154         interrupt & DIAG_DL1 ? p += snprintf(p, TMP_BUF_MAX, "DIAG_DL1 ") : 0;
1155         interrupt & DIAG_DL2 ? p += snprintf(p, TMP_BUF_MAX, "DIAG_DL2 ") : 0;
1156
1157         interrupt & DIAG_UL ? p += snprintf(p, TMP_BUF_MAX, "DIAG_UL ") : 0;
1158
1159         interrupt & APP1_DL ? p += snprintf(p, TMP_BUF_MAX, "APP1_DL ") : 0;
1160         interrupt & APP2_DL ? p += snprintf(p, TMP_BUF_MAX, "APP2_DL ") : 0;
1161
1162         interrupt & APP1_UL ? p += snprintf(p, TMP_BUF_MAX, "APP1_UL ") : 0;
1163         interrupt & APP2_UL ? p += snprintf(p, TMP_BUF_MAX, "APP2_UL ") : 0;
1164
1165         interrupt & CTRL_DL ? p += snprintf(p, TMP_BUF_MAX, "CTRL_DL ") : 0;
1166         interrupt & CTRL_UL ? p += snprintf(p, TMP_BUF_MAX, "CTRL_UL ") : 0;
1167
1168         interrupt & RESET ? p += snprintf(p, TMP_BUF_MAX, "RESET ") : 0;
1169
1170         return buf;
1171 }
1172 #endif
1173
1174 /* Receive flow control */
1175 /* Return 1 - If ok, else 0 */
1176 static int receive_flow_control(dc_t * dc, irq_t * m)
1177 {
1178         enum port_type port = PORT_MDM;
1179         ctrl_dl_t ctrl_dl;
1180         ctrl_dl_t old_ctrl;
1181         u16 enable_ier = 0;
1182
1183         GET_MEM(&ctrl_dl, dc->port[PORT_CTRL].dl_addr[CH_A], 2);
1184
1185         switch (ctrl_dl.port) {
1186         case CTRL_CMD:
1187                 D1("The Base Band sends this value as a response to a request for IMSI detach sent" " over the control channel uplink (see section 7.6.1).");
1188                 break;
1189         case CTRL_MDM:
1190                 port = PORT_MDM;
1191                 enable_ier = MDM_DL;
1192                 break;
1193         case CTRL_DIAG:
1194                 port = PORT_DIAG;
1195                 enable_ier = DIAG_DL;
1196                 break;
1197         case CTRL_APP1:
1198                 port = PORT_APP1;
1199                 enable_ier = APP1_DL;
1200                 break;
1201         case CTRL_APP2:
1202                 port = PORT_APP2;
1203                 enable_ier = APP2_DL;
1204                 break;
1205         default:
1206                 dev_err(&dc->pdev->dev,
1207                         "ERROR: flow control received for non-existing port\n");
1208                 return 0;
1209         };
1210
1211         D1("0x%04X->0x%04X", *((u16 *) & dc->port[port].ctrl_dl),
1212            *((u16 *) & ctrl_dl));
1213
1214         old_ctrl = dc->port[port].ctrl_dl;
1215         dc->port[port].ctrl_dl = ctrl_dl;
1216
1217         if (old_ctrl.CTS == 1 && ctrl_dl.CTS == 0) {
1218                 D1("Disable interrupt (0x%04X) on port: %d", enable_ier, port);
1219                 disable_transmit_ul(port, dc);
1220
1221         } else if (old_ctrl.CTS == 0 && ctrl_dl.CTS == 1) {
1222
1223                 if (__kfifo_len(dc->port[port].fifo_ul)) {
1224                         D1("Enable interrupt (0x%04X) on port: %d", enable_ier,
1225                            port);
1226                         D1("Data in buffer [%d], enable transmit! ",
1227                            __kfifo_len(dc->port[port].fifo_ul));
1228                         enable_transmit_ul(port, dc);
1229                 } else {
1230                         D1("No data in buffer...");
1231                 }
1232         }
1233
1234         if (*(u16 *) & old_ctrl == *(u16 *) & ctrl_dl) {
1235                 D1(" No change in mctrl");
1236                 return 1;
1237         }
1238         /* Update statistics */
1239         if (old_ctrl.CTS != ctrl_dl.CTS) {
1240                 dc->port[port].tty_icount.cts++;
1241         }
1242         if (old_ctrl.DSR != ctrl_dl.DSR) {
1243                 dc->port[port].tty_icount.dsr++;
1244         }
1245         if (old_ctrl.RI != ctrl_dl.RI) {
1246                 dc->port[port].tty_icount.rng++;
1247         }
1248         if (old_ctrl.DCD != ctrl_dl.DCD) {
1249                 dc->port[port].tty_icount.dcd++;
1250         }
1251         D1("port: %d DCD(%d), CTS(%d), RI(%d), DSR(%d)",
1252            port,
1253            dc->port[port].tty_icount.dcd, dc->port[port].tty_icount.cts,
1254            dc->port[port].tty_icount.rng, dc->port[port].tty_icount.dsr);
1255
1256         return 1;
1257 }
1258
1259 /* TODO:  */
1260 /* - return enum ctrl_port_type */
1261 static u8 port2ctrl(enum port_type port, dc_t * dc)
1262 {
1263         switch (port) {
1264         case PORT_MDM:
1265                 return CTRL_MDM;
1266         case PORT_DIAG:
1267                 return CTRL_DIAG;
1268         case PORT_APP1:
1269                 return CTRL_APP1;
1270         case PORT_APP2:
1271                 return CTRL_APP2;
1272         default:
1273                 dev_err(&dc->pdev->dev,
1274                         "ERROR: send flow control received for non-existing port\n");
1275         };
1276         return -1;
1277 }
1278
1279 /* Send flow control, can only update one channel at a time */
1280 /* Return 0 - If we have updated all flow control */
1281 /* Return 1 - If we need to update more flow control, ack current enable more */
1282 static int send_flow_control(dc_t * dc)
1283 {
1284         u32 i, more_flow_control_to_be_updated = 0;
1285         u16 *ctrl;
1286
1287         for (i = PORT_MDM; i < MAX_PORT; i++) {
1288                 if (dc->port[i].update_flow_control) {
1289                         if (more_flow_control_to_be_updated) {
1290                                 /* We have more flow control to be updated */
1291                                 return 1;
1292                         }
1293                         dc->port[i].ctrl_ul.port = port2ctrl(i, dc);
1294                         ctrl = (u16 *) & dc->port[i].ctrl_ul;
1295                         /* D1( "sending flow control 0x%04X for port %d, %d", (u16) *ctrl, i, dc->port[i].ctrl_ul.port ); */
1296                         SET_MEM(dc->port[PORT_CTRL].ul_addr[0], (u32 *) ctrl,
1297                                 2);
1298                         dc->port[i].update_flow_control = 0;
1299                         more_flow_control_to_be_updated = 1;
1300                 }
1301         }
1302         return 0;
1303 }
1304
1305 /* Handle donlink data, ports that are handled are modem and diagnostics */
1306 /* Return 1 - ok */
1307 /* Return 0 - toggle fields are out of sync */
1308 static int handle_data_dl(dc_t * dc, irq_t * m, enum port_type port,
1309                           u8 * toggle, u16 mask1, u16 mask2)
1310 {
1311
1312         if (*toggle == 0 && m->read_iir & mask1) {
1313                 if (receive_data(port, dc)) {
1314                         SET_FCR(mask1);
1315                         *toggle = !(*toggle);
1316                 }
1317
1318                 if (m->read_iir & mask2) {
1319                         if (receive_data(port, dc)) {
1320                                 SET_FCR(mask2);
1321                                 *toggle = !(*toggle);
1322                         }
1323                 }
1324         } else if (*toggle == 1 && m->read_iir & mask2) {
1325                 if (receive_data(port, dc)) {
1326                         SET_FCR(mask2);
1327                         *toggle = !(*toggle);
1328                 }
1329
1330                 if (m->read_iir & mask1) {
1331                         if (receive_data(port, dc)) {
1332                                 SET_FCR(mask1);
1333                                 *toggle = !(*toggle);
1334                         }
1335                 }
1336         } else {
1337                 ERR("port out of sync!, toggle:%d", *toggle);
1338                 return 0;
1339         }
1340         return 1;
1341 }
1342
1343 /* Handle uplink data, this is currently for the modem port */
1344 /* Return 1 - ok */
1345 /* Return 0 - toggle field are out of sync */
1346 static int handle_data_ul(dc_t * dc, irq_t * m, enum port_type port)
1347 {
1348
1349         u8 *toggle = &(dc->port[port].toggle_ul);
1350
1351         if (*toggle == 0 && m->read_iir & MDM_UL1) {
1352                 SET_IER(0, MDM_UL);
1353                 if (send_data(port, dc)) {
1354                         SET_FCR(MDM_UL1);
1355                         SET_IER(MDM_UL, MDM_UL);
1356                         *toggle = !*toggle;
1357                 }
1358
1359                 if (m->read_iir & MDM_UL2) {
1360                         SET_IER(0, MDM_UL);
1361                         if (send_data(port, dc)) {
1362                                 SET_FCR(MDM_UL2);
1363                                 SET_IER(MDM_UL, MDM_UL);
1364                                 *toggle = !*toggle;
1365                         }
1366                 }
1367
1368         } else if (*toggle == 1 && m->read_iir & MDM_UL2) {
1369                 SET_IER(0, MDM_UL);
1370                 if (send_data(port, dc)) {
1371                         SET_FCR(MDM_UL2);
1372                         SET_IER(MDM_UL, MDM_UL);
1373                         *toggle = !*toggle;
1374                 }
1375
1376                 if (m->read_iir & MDM_UL1) {
1377                         SET_IER(0, MDM_UL);
1378                         if (send_data(port, dc)) {
1379                                 SET_FCR(MDM_UL1);
1380                                 SET_IER(MDM_UL, MDM_UL);
1381                                 *toggle = !*toggle;
1382                         }
1383                 }
1384         } else {
1385                 SET_FCR(m->read_iir & MDM_UL);
1386                 ERR("port out of sync!");
1387                 return 0;
1388         }
1389         return 1;
1390 }
1391
1392 static irqreturn_t interrupt_handler(int irq, void *dev_id,
1393                                      struct pt_regs *regs)
1394 {
1395         dc_t *dc = NULL;
1396         irq_t *m = &my_irq;
1397
1398         if (my_dev && my_dev->pdev != dev_id) {
1399                 return IRQ_NONE;
1400         }
1401
1402         if (!(dc = get_dc_by_pdev(dev_id))) {
1403                 ERR("Could not find device context from pci_dev: %Xu",
1404                     (u32) dev_id);
1405                 return IRQ_NONE;
1406         }
1407
1408         GET_IIR(m->read_iir);
1409
1410         /* Just handle interrupt enabled in IER (by masking with dc->ier_last_written) */
1411         m->read_iir &= dc->ier_last_written;
1412
1413         if (m->read_iir == 0) {
1414                 return IRQ_NONE;
1415         }
1416
1417         if (dc == NULL) {
1418                 ERR("ERROR!!");
1419                 return IRQ_NONE;
1420         }
1421
1422         spin_lock(&dc->spin_mutex);
1423
1424         D4("%s irq:0x%04X, prev:0x%04X", interrupt2str(m->read_iir),
1425            m->read_iir, dc->ier_last_written);
1426
1427         if (m->read_iir & RESET) {
1428                 if (!nozomi_read_config_table(dc)) {
1429                         SET_IER(0, 0xFFFF);
1430                         ERR("ERR: Could not read status from card, we should disable interface");
1431                 } else {
1432                         SET_FCR(RESET);
1433                 }
1434                 goto exit_handler;      /* No more useful info if this was the reset interrupt. */
1435         }
1436         if (m->read_iir & CTRL_UL) {
1437                 D1("CTRL_UL");
1438                 SET_IER(0, CTRL_UL);
1439                 if (send_flow_control(dc)) {
1440                         SET_FCR(CTRL_UL);
1441                         SET_IER(CTRL_UL, CTRL_UL);
1442                 }
1443         }
1444         if (m->read_iir & CTRL_DL) {
1445                 receive_flow_control(dc, m);
1446                 SET_FCR(CTRL_DL);
1447         }
1448         if (m->read_iir & MDM_DL) {
1449                 if (!
1450                     (handle_data_dl
1451                      (dc, m, PORT_MDM, &(dc->port[PORT_MDM].toggle_dl), MDM_DL1,
1452                       MDM_DL2))) {
1453                         ERR("MDM_DL out of sync!");
1454                         goto exit_handler;
1455                 }
1456         }
1457         if (m->read_iir & MDM_UL) {
1458                 if (!handle_data_ul(dc, m, PORT_MDM)) {
1459                         ERR("MDM_UL out of sync!");
1460                         goto exit_handler;
1461                 }
1462         }
1463         if (m->read_iir & DIAG_DL) {
1464                 if (!
1465                     (handle_data_dl
1466                      (dc, m, PORT_DIAG, &(dc->port[PORT_DIAG].toggle_dl),
1467                       DIAG_DL1, DIAG_DL2))) {
1468                         ERR("DIAG_DL out of sync!");
1469                         goto exit_handler;
1470                 }
1471         }
1472         if (m->read_iir & DIAG_UL) {
1473                 SET_IER(0, DIAG_UL);
1474                 if (send_data(PORT_DIAG, dc)) {
1475                         SET_FCR(DIAG_UL);
1476                         SET_IER(DIAG_UL, DIAG_UL);
1477                 }
1478         }
1479         if (m->read_iir & APP1_DL) {
1480                 if (receive_data(PORT_APP1, dc)) {
1481                         SET_FCR(APP1_DL);
1482                 }
1483         }
1484         if (m->read_iir & APP1_UL) {
1485                 SET_IER(0, APP1_UL);
1486                 if (send_data(PORT_APP1, dc)) {
1487                         SET_FCR(APP1_UL);
1488                         SET_IER(APP1_UL, APP1_UL);
1489                 }
1490         }
1491         if (m->read_iir & APP2_DL) {
1492                 if (receive_data(PORT_APP2, dc)) {
1493                         SET_FCR(APP2_DL);
1494                 }
1495         }
1496         if (m->read_iir & APP2_UL) {
1497                 SET_IER(0, APP2_UL);
1498                 if (send_data(PORT_APP2, dc)) {
1499                         SET_FCR(APP2_UL);
1500                         SET_IER(APP2_UL, APP2_UL);
1501                 }
1502         }
1503
1504       exit_handler:
1505         spin_unlock(&dc->spin_mutex);
1506         return IRQ_HANDLED;
1507 }
1508
1509 /* Request a shared IRQ from system */
1510 static int nozomi_setup_interrupt(struct pci_dev *pdev)
1511 {
1512
1513         int rval = 0;
1514
1515         if ((rval =
1516              request_irq(pdev->irq, &interrupt_handler, SA_SHIRQ, NOZOMI_NAME,
1517                          pdev))) {
1518                 ERR("Cannot open because IRQ %d is already in use.", pdev->irq);
1519                 return rval;
1520         }
1521
1522         return rval;
1523 }
1524
1525 static void nozomi_get_card_type(dc_t * dc)
1526 {
1527         int i;
1528         u32 size = 0;
1529
1530         for (i = 0; i < 6; i++)
1531                 size += pci_resource_len(dc->pdev, i);
1532         /* Assume card type F32_8 if no match */
1533         dc->card_type = size == 2048 ? F32_2 : F32_8;
1534         dev_info(&dc->pdev->dev, "Card type is: %d\n", dc->card_type);
1535 }
1536
1537 static void nozomi_setup_private_data(dc_t * dc)
1538 {
1539         void __iomem *offset = dc->base_addr + dc->card_type / 2;
1540         int i;
1541
1542         dc->REG_FCR = (void __iomem *)(offset + R_FCR);
1543         dc->REG_IIR = (void __iomem *)(offset + R_IIR);
1544         dc->REG_IER = (void __iomem *)(offset + R_IER);
1545         dc->ier_last_written = 0;
1546         dc->closing = 0;
1547         dc->port[PORT_MDM].token_dl = MDM_DL;
1548         dc->port[PORT_DIAG].token_dl = DIAG_DL;
1549         dc->port[PORT_APP1].token_dl = APP1_DL;
1550         dc->port[PORT_APP2].token_dl = APP2_DL;
1551
1552         for (i = PORT_MDM; i < MAX_PORT; i++) {
1553                 dc->port[i].rx_data = dc->port[i].tx_data = 0;
1554                 dc->port[i].tty_dont_flip = 0;
1555         }
1556 }
1557
1558 static void tty_flip_queue_function(void *tmp_dc)
1559 {
1560         dc_t *dc = (dc_t *) tmp_dc;
1561         int i;
1562         unsigned long flags;
1563
1564         /* Enable interrupt for that port */
1565         for (i = 0; i < MAX_PORT; i++) {
1566                 if (dc->port[i].tty_dont_flip) {
1567                         D6("Enable for port: %d", i);
1568                         dc->port[i].tty_dont_flip = 0;
1569                         spin_lock_irqsave(&dc->spin_mutex, flags);
1570                         enable_transmit_dl(dc->port[i].tty_index, dc);
1571                         spin_unlock_irqrestore(&dc->spin_mutex, flags);
1572                 }
1573         }
1574 }
1575
1576 static int read_proc_card_type(char *buf, char **start, off_t offset, int len)
1577 {
1578         dc_t *dc = get_dc_by_index(0);
1579         len = 0;
1580
1581         len += sprintf(buf + len, "%d\n", dc->card_type);
1582         return len;
1583 }
1584
1585 static int read_proc_open_ttys(char *buf, char **start, off_t offset, int len)
1586 {
1587         dc_t *dc = get_dc_by_index(0);
1588         len = 0;
1589
1590         len += sprintf(buf + len, "%d\n", dc->open_ttys);
1591         return len;
1592 }
1593
1594 static int read_proc_version(char *buf, char **start, off_t offset, int len)
1595 {
1596         len = 0;
1597
1598         len += sprintf(buf + len, "%s\n", VERSION_STRING);
1599         return len;
1600 }
1601
1602 static int read_proc_rtx(char *buf, char **start, off_t offset, int len)
1603 {
1604         dc_t *dc = get_dc_by_index(0);
1605         int i;
1606
1607         len = 0;
1608
1609         for (i = PORT_MDM; i < MAX_PORT; i++) {
1610                 len +=
1611                     sprintf(buf + len, "noz%d rx: %d, tx: %d\n", i,
1612                             dc->port[i].rx_data, dc->port[i].tx_data);
1613         }
1614         return len;
1615 }
1616
1617 static void make_proc_dirs(void)
1618 {
1619         dc_t *dc = get_dc_by_index(0);
1620         dc->proc_entry = proc_mkdir("nozomi", &proc_root);
1621
1622         /* Register the read_proc */
1623         if (!create_proc_info_entry
1624             ("card_type", 0, dc->proc_entry, read_proc_card_type)) {
1625                 ERR("ERROR: failed to register read_procmem");
1626         }
1627         if (!create_proc_info_entry
1628             ("open_ttys", 0, dc->proc_entry, read_proc_open_ttys)) {
1629                 ERR("ERROR: failed to register read_procmem");
1630         }
1631         if (!create_proc_info_entry("rtx", 0, dc->proc_entry, read_proc_rtx)) {
1632                 ERR("ERROR: failed to register read_procmem");
1633         }
1634         if (!create_proc_info_entry
1635             ("version", 0, dc->proc_entry, read_proc_version)) {
1636                 ERR("ERROR: failed to register read_procmem");
1637         }
1638 }
1639
1640 static void remove_proc_dirs(void)
1641 {
1642         dc_t *dc = get_dc_by_index(0);
1643
1644         remove_proc_entry("card_type", dc->proc_entry);
1645         remove_proc_entry("open_ttys", dc->proc_entry);
1646         remove_proc_entry("rtx", dc->proc_entry);
1647         remove_proc_entry("version", dc->proc_entry);
1648         remove_proc_entry("nozomi", &proc_root);
1649 }
1650
1651 /* Allocate memory for one device */
1652 static int __devinit nozomi_card_init(struct pci_dev *pdev,
1653                                       const struct pci_device_id *ent)
1654 {
1655         int ret = -EIO;
1656         dc_t *dc = NULL;
1657
1658         cards_found++;
1659         dev_info(&pdev->dev, "Init, cards_found: %d\n", cards_found);
1660
1661         if (!(my_dev = kmalloc(sizeof(dc_t), GFP_KERNEL))) {
1662                 D1("Could not allocate memory");
1663                 return -EIO;
1664         }
1665
1666         memset(my_dev, 0, sizeof(dc_t));
1667
1668         if (cards_found > 1) {
1669                 dev_err(&pdev->dev, "This driver only supports 1 device\n");
1670                 return -ENODEV;
1671         }
1672
1673         my_dev->pdev = pdev;
1674         dc = my_dev;
1675
1676         /* Find out what card type it is */
1677         nozomi_get_card_type(dc);
1678
1679         if (pci_enable_device(dc->pdev)) {
1680                 dev_err(&pdev->dev, "Not possible to enable PCI Device\n");
1681                 return -ENODEV;
1682         }
1683         dc->base_addr = (void *)pci_resource_start(dc->pdev, 0);
1684         if (dc->base_addr == 0x0000) {
1685                 dev_err(&pdev->dev, "No I/O-Address for card detected\n");
1686                 ret = -ENODEV;
1687                 goto err_disable_device;
1688         }
1689         dc->base_addr = (void *)ioremap((int)dc->base_addr, dc->card_type);
1690         if (!dc->base_addr) {
1691                 dev_err(&pdev->dev, "No I/O-Address for card detected\n");
1692                 ret = -ENODEV;
1693                 goto err_disable_device;
1694         }
1695
1696         dc->open_ttys = 0;
1697
1698         nozomi_setup_private_data(dc);
1699
1700         if (pci_request_regions(dc->pdev, NOZOMI_NAME)) {
1701                 dev_err(&pdev->dev, "I/O address 0x%04x already in use\n",
1702                         (int) /* nozomi_private.io_addr */ 0);
1703                 ret = -EIO;
1704                 goto err_disable_regions;
1705         }
1706
1707         dc->send_buf = kmalloc(SEND_BUF_MAX, GFP_KERNEL);
1708         if (!dc->send_buf) {
1709                 dev_err(&pdev->dev, "Could not allocate send buffer?\n");
1710                 goto err_disable_regions;
1711         }
1712
1713         /* Disable all interrupts */
1714         SET_IER(0, 0xFFFF);
1715
1716         /* Setup interrupt handler */
1717         if (nozomi_setup_interrupt(dc->pdev)) {
1718                 ret = -EIO;
1719                 goto err_disable_regions;
1720         }
1721
1722         D1("base_addr: 0x%08X", dc->base_addr);
1723 #ifdef KERNEL_2_4
1724         dc->tty_flip_queue.list.next = NULL;
1725         dc->tty_flip_queue.list.prev = NULL;
1726         dc->tty_flip_queue.sync = 0;
1727         dc->tty_flip_queue.data = dc;
1728         dc->tty_flip_queue.routine = tty_flip_queue_function;
1729 #endif
1730 #ifdef KERNEL_2_6
1731         if (!(dc->tty_flip_wq = create_singlethread_workqueue(NOZOMI_NAME))) {
1732                 ERR("Could not create workqueue?");
1733                 BUG_ON(!dc->tty_flip_wq);
1734                 return -ENOMEM;
1735         }
1736 //      INIT_WORK(&dc->tty_flip_wq_struct, tty_flip_queue_function, dc);
1737         INIT_WORK(&dc->tty_flip_wq_struct, tty_flip_queue_function);
1738 #endif
1739         spin_lock_init(&dc->spin_mutex);
1740         make_proc_dirs();
1741         ntty_tty_init(dc);
1742         /* Enable  RESET interrupt. */
1743         SET_IER(RESET, 0xFFFF);
1744         return 0;
1745
1746       err_disable_regions:
1747         pci_release_regions(pdev);
1748         iounmap(dc->base_addr);
1749         dc->base_addr = NULL;
1750
1751       err_disable_device:
1752         pci_disable_device(pdev);
1753         kfree(my_dev);
1754         return ret;
1755 }
1756
1757 static void tty_do_close(dc_t * dc, struct port *port)
1758 {
1759
1760         unsigned long flags;
1761
1762         if (down_interruptible(&port->tty_sem)) {
1763                 return;
1764         }
1765         if (port->tty_open_count) {
1766                 dc->open_ttys--;
1767                 port->tty_open_count--;
1768         #ifdef KERNEL_2_4
1769                 MOD_DEC_USE_COUNT;
1770         #endif
1771                 if (port->tty_open_count == 0) {
1772                         D1("close: %d", port->token_dl);
1773                         spin_lock_irqsave(&dc->spin_mutex, flags);
1774                         SET_IER(0, port->token_dl);
1775                         spin_unlock_irqrestore(&dc->spin_mutex, flags);
1776                 }
1777         }
1778         up(&port->tty_sem);
1779 }
1780
1781 static void __devexit tty_exit(void)
1782 {
1783         dc_t *dc = my_dev;
1784         int i;
1785
1786         D1(" ");
1787         for (i = 0; i < NTTY_TTY_MINORS; i++) {
1788                 while (dc->port[i].tty_open_count)
1789                         tty_do_close(dc, &dc->port[i]);
1790                 dc->port[i].tty = NULL;
1791         }
1792 #ifdef KERNEL_2_6
1793         for (i = 0; i < NTTY_TTY_MINORS; ++i){
1794                 D1("Unregistering device");
1795                 tty_unregister_device(dc->tty_driver, i);
1796         }
1797 #endif
1798         D1("Unregistering driver");
1799         tty_unregister_driver(dc->tty_driver);
1800         put_tty_driver(dc->tty_driver);
1801 }
1802
1803 /* Deallocate memory for one device */
1804 static void __devexit nozomi_card_exit(struct pci_dev *pdev)
1805 {
1806         int i;
1807         ctrl_ul_t ctrl;
1808         dc_t *dc = get_dc_by_pdev(pdev);
1809
1810         /* Disable all interrupts */
1811         SET_IER(0, 0xFFFF);
1812
1813         /* Send 0x0001, command card to resend the reset token. */
1814         /* This is to get the reset when the module is reloaded. */
1815         ctrl.port = 0x00;
1816         ctrl.reserved = 0;
1817         ctrl.RTS = 0;
1818         ctrl.DTR = 1;
1819         D1("sending flow control 0x%04X", *((u16 *) & ctrl));
1820
1821         /* Setup dc->reg addresses to we can use defines here */
1822         nozomi_setup_private_data(dc);
1823         SET_MEM(dc->port[PORT_CTRL].ul_addr[0], (u32 *) & ctrl, 2);
1824         SET_FCR(CTRL_UL);       /* push the token to the card. */
1825
1826         D1("pci_release_regions");
1827         pci_release_regions(pdev);
1828
1829         if (dc->base_addr)
1830                 iounmap(dc->base_addr);
1831
1832         D1("pci_disable_device");
1833         pci_disable_device(pdev);
1834
1835         free_irq(pdev->irq, pdev);
1836
1837         for (i = PORT_MDM; i < MAX_PORT; i++) {
1838                 kfree(dc->port[i].fifo_ul);
1839         }
1840
1841         kfree(dc->send_buf);
1842
1843         tty_exit();
1844
1845         remove_proc_dirs();
1846
1847 #ifdef KERNEL_2_6
1848         destroy_workqueue(dc->tty_flip_wq);
1849 #endif
1850         if (my_dev) {
1851                 kfree(my_dev);
1852         }
1853
1854         cards_found--;
1855 }
1856
1857 static void set_rts(int index, int rts)
1858 {
1859         dc_t *dc = get_dc_by_index(index);
1860         dc->port[index].ctrl_ul.RTS = rts;
1861         dc->port[index].update_flow_control = 1;
1862         enable_transmit_ul(PORT_CTRL, dc);
1863 }
1864
1865 static void set_dtr(int index, int dtr)
1866 {
1867         dc_t *dc = get_dc_by_index(index);
1868         D1("SETTING DTR index: %d, dtr: %d", index, dtr);
1869         dc->port[index].ctrl_ul.DTR = dtr;
1870         dc->port[index].update_flow_control = 1;
1871         enable_transmit_ul(PORT_CTRL, dc);
1872 }
1873
1874 /* ---------------------------------------------------------------------------------------------------
1875   TTY code
1876   ---------------------------------------------------------------------------------------------------*/
1877
1878 /* Called when the userspace process opens the tty, /dev/noz*. */
1879 static int ntty_open(struct tty_struct *tty, struct file *file)
1880 {
1881
1882         s32 index = get_index(tty);
1883         struct port *port = get_port_by_tty(tty);
1884         dc_t *dc = get_dc_by_tty(tty);
1885         unsigned long flags;
1886
1887         if (down_interruptible(&port->tty_sem)) {
1888                 return -ERESTARTSYS;
1889         }
1890
1891         tty->low_latency = 1;
1892         tty->driver_data = port;
1893         port->tty = tty;
1894         port->tty_index = index;
1895         port->tty_open_count++;
1896         dc->open_ttys++;
1897 #ifdef KERNEL_2_4
1898         MOD_INC_USE_COUNT;
1899 #endif
1900         /* Enable interrupt downlink for channel */
1901         if (port->tty_open_count == 1) {
1902                 port->rx_data = port->tx_data = 0;
1903                 D1("open: %d", port->token_dl);
1904                 spin_lock_irqsave(&dc->spin_mutex, flags);
1905                 SET_IER(port->token_dl, port->token_dl);
1906                 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1907         }
1908
1909         up(&port->tty_sem);
1910         return 0;
1911 }
1912
1913 /* Called when the userspace process close the tty, /dev/noz*. */
1914 static void ntty_close(struct tty_struct *tty, struct file *file)
1915 {
1916         dc_t *dc = get_dc_by_tty(tty);
1917         tty_do_close(dc, (struct port *)tty->driver_data);
1918 }
1919
1920 /* called when the userspace process writes to the tty (/dev/noz*).  */
1921 /* Data is inserted into a fifo, which is then read and transfered to the modem. */
1922 #ifdef KERNEL_2_6
1923 static int ntty_write(struct tty_struct *tty, const u8 *buffer,
1924                       int count)
1925 {
1926 #else
1927 static s32 ntty_write(struct tty_struct *tty, s32 from_user, const u8 * buffer,
1928                       s32 count)
1929 {
1930 #endif
1931         int rval = -EINVAL;
1932         dc_t *dc = get_dc_by_tty(tty);
1933         struct port *port = (struct port *)tty->driver_data;
1934         unsigned long flags;
1935
1936         /* D1( "WRITEx: %d, index = %d", count, index); */
1937
1938         if (!port) {
1939                 return -ENODEV;
1940         }
1941
1942         if (down_trylock(&port->tty_sem)) {     // must test lock as tty layer wraps calls to this function with BKL
1943                 ERR("Would have deadlocked - return ERESTARTSYS");
1944                 return -ERESTARTSYS;
1945         }
1946
1947         if (!port->tty_open_count) {
1948                 D1(" ");
1949                 goto exit;
1950         }
1951 #ifdef KERNEL_2_4
1952         if (from_user) {
1953                 rval =
1954                     __kfifo_put_user(port->fifo_ul, (unsigned char *)buffer,
1955                                      count);
1956         } else {
1957                 rval =
1958                     __kfifo_put(port->fifo_ul, (unsigned char *)buffer, count);
1959         }
1960 #else
1961         rval = __kfifo_put(port->fifo_ul, (unsigned char *)buffer, count);
1962 #endif
1963
1964         /* notify card */
1965         if (dc == NULL) {
1966                 D1("No device context?");
1967                 goto exit;
1968         }
1969
1970         spin_lock_irqsave(&dc->spin_mutex, flags);
1971         // CTS is only valid on the modem channel
1972         if (port == &(dc->port[PORT_MDM])) {
1973                 if (port->ctrl_dl.CTS) {
1974                         D4("Enable interrupt");
1975                         enable_transmit_ul(port->tty_index, dc);
1976                 } else {
1977                         ERR("CTS not active on modem port?");
1978                 }
1979         } else {
1980                 enable_transmit_ul(port->tty_index, dc);
1981         }
1982         spin_unlock_irqrestore(&dc->spin_mutex, flags);
1983
1984       exit:
1985         up(&port->tty_sem);
1986         return rval;
1987 }
1988
1989 /* Calculate how much is left in device */
1990 /* This method is called by the upper tty layer. */
1991 /*   #according to sources N_TTY.c it expects a value >= 0 and does not check for negative values. */
1992 static int ntty_write_room(struct tty_struct *tty)
1993 {
1994         struct port *port = (struct port *)tty->driver_data;
1995         int room = 0;
1996         if (!port) {
1997                 return 0;
1998         }
1999         if (down_trylock(&port->tty_sem)) {
2000                 return 0;
2001         }
2002         if (port->tty_open_count) {
2003                 room = port->fifo_ul->size - __kfifo_len(port->fifo_ul);
2004         }
2005         up(&port->tty_sem);
2006         return room;
2007 }
2008
2009 /* Sets termios flags, called by the tty layer. */
2010 static void ntty_set_termios(struct tty_struct *tty,
2011                              struct termios *old_termios)
2012 {
2013         unsigned int cflag;
2014
2015         cflag = tty->termios->c_cflag;
2016
2017         if (old_termios) {
2018                 if ((cflag == old_termios->c_cflag) &&
2019                     (RELEVANT_IFLAG(tty->termios->c_iflag) ==
2020                      RELEVANT_IFLAG(old_termios->c_iflag))) {
2021                         D1(" - nothing to change...");
2022                         goto exit_termios;
2023                 }
2024         }
2025
2026         /* get the byte size */
2027         switch (cflag & CSIZE) {
2028         case CS5:
2029                 D1(" - data bits = 5");
2030                 break;
2031         case CS6:
2032                 D1(" - data bits = 6");
2033                 break;
2034         case CS7:
2035                 D1(" - data bits = 7");
2036                 break;
2037         default:
2038         case CS8:
2039                 D1(" - data bits = 8");
2040                 break;
2041         }
2042
2043         /* determine the parity */
2044         if (cflag & PARENB) {
2045                 if (cflag & PARODD) {
2046                         D1(" - parity = odd");
2047                 } else {
2048                         D1(" - parity = even");
2049                 }
2050         } else {
2051                 D1(" - parity = none");
2052         }
2053
2054         /* figure out the stop bits requested */
2055         if (cflag & CSTOPB) {
2056                 D1(" - stop bits = 2");
2057         } else {
2058                 D1(" - stop bits = 1");
2059         }
2060
2061         /* figure out the hardware flow control settings */
2062         if (cflag & CRTSCTS) {
2063                 D1(" - RTS/CTS is enabled");
2064         } else {
2065                 D1(" - RTS/CTS is disabled");
2066         }
2067
2068         /* determine software flow control */
2069         /* if we are implementing XON/XOFF, set the start and
2070          * stop character in the device */
2071         if (I_IXOFF(tty) || I_IXON(tty)) {
2072 #ifdef NOZOMI_DEBUG
2073                 unsigned char stop_char = STOP_CHAR(tty);
2074                 unsigned char start_char = START_CHAR(tty);
2075 #endif
2076                 /* if we are implementing INBOUND XON/XOFF */
2077                 if (I_IXOFF(tty)) {
2078                         D1(" - INBOUND XON/XOFF is enabled, "
2079                            "XON = %2x, XOFF = %2x", start_char, stop_char);
2080                 } else {
2081                         D1(" - INBOUND XON/XOFF is disabled");
2082                 }
2083
2084                 /* if we are implementing OUTBOUND XON/XOFF */
2085                 if (I_IXON(tty)) {
2086                         D1(" - OUTBOUND XON/XOFF is enabled, "
2087                            "XON = %2x, XOFF = %2x", start_char, stop_char);
2088                 } else {
2089                         D1(" - OUTBOUND XON/XOFF is disabled");
2090                 }
2091         }
2092
2093       exit_termios:
2094         return;
2095 }
2096
2097 /* Gets io control parameters */
2098 static int ntty_tiocmget(struct tty_struct *tty, struct file *file)
2099 {
2100         struct port *port = tty->driver_data;
2101         ctrl_dl_t *ctrl_dl = &port->ctrl_dl;
2102         ctrl_ul_t *ctrl_ul = &port->ctrl_ul;
2103
2104         return 0 | (ctrl_ul->RTS ? TIOCM_RTS : 0)
2105             | (ctrl_ul->DTR ? TIOCM_DTR : 0)
2106             | (ctrl_dl->DCD ? TIOCM_CAR : 0)
2107             | (ctrl_dl->RI ? TIOCM_RNG : 0)
2108             | (ctrl_dl->DSR ? TIOCM_DSR : 0)
2109             | (ctrl_dl->CTS ? TIOCM_CTS : 0);
2110 }
2111
2112 /* Sets io controls parameters */
2113 static int ntty_tiocmset(struct tty_struct *tty, struct file *file, u32 arg)
2114 {
2115         struct port *port = (struct port *)tty->driver_data;
2116
2117         set_rts(port->tty_index, (arg & TIOCM_RTS) ? 1 : 0);
2118         set_dtr(port->tty_index, (arg & TIOCM_DTR) ? 1 : 0);
2119
2120         return 0;
2121 }
2122
2123 static int ntty_ioctl_tiocmiwait(struct tty_struct *tty, struct file *file,
2124                                  unsigned int cmd, unsigned long arg)
2125 {
2126         struct port *port = (struct port *)tty->driver_data;
2127
2128         if (cmd == TIOCMIWAIT) {
2129                 DECLARE_WAITQUEUE(wait, current);
2130                 struct async_icount cnow;
2131                 struct async_icount cprev;
2132
2133                 cprev = port->tty_icount;
2134                 while (1) {
2135                         add_wait_queue(&port->tty_wait, &wait);
2136                         set_current_state(TASK_INTERRUPTIBLE);
2137                         schedule();
2138                         remove_wait_queue(&port->tty_wait, &wait);
2139
2140                         /* see if a signal woke us up */
2141                         if (signal_pending(current))
2142                                 return -ERESTARTSYS;
2143
2144                         cnow = port->tty_icount;
2145                         if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
2146                             cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
2147                                 return -EIO;    /* no change => error */
2148                         if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
2149                             ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
2150                             ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
2151                             ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
2152                                 return 0;
2153                         }
2154                         cprev = cnow;
2155                 }
2156
2157         }
2158         return -ENOIOCTLCMD;
2159 }
2160
2161 static int ntty_ioctl_tiocgicount(struct tty_struct *tty, struct file *file,
2162                                   unsigned int cmd, void __user * arg)
2163 {
2164         struct port *port = (struct port *)tty->driver_data;
2165
2166         if (cmd == TIOCGICOUNT) {
2167                 struct async_icount cnow = port->tty_icount;
2168                 struct serial_icounter_struct icount;
2169
2170                 icount.cts = cnow.cts;
2171                 icount.dsr = cnow.dsr;
2172                 icount.rng = cnow.rng;
2173                 icount.dcd = cnow.dcd;
2174                 icount.rx = cnow.rx;
2175                 icount.tx = cnow.tx;
2176                 icount.frame = cnow.frame;
2177                 icount.overrun = cnow.overrun;
2178                 icount.parity = cnow.parity;
2179                 icount.brk = cnow.brk;
2180                 icount.buf_overrun = cnow.buf_overrun;
2181
2182                 if (copy_to_user(arg, &icount, sizeof(icount)))
2183                         return -EFAULT;
2184                 return 0;
2185         }
2186         return -ENOIOCTLCMD;
2187 }
2188
2189 static int ntty_ioctl(struct tty_struct *tty, struct file *file,
2190                       unsigned int cmd, unsigned long arg)
2191 {
2192         struct port *port = tty->driver_data;
2193         dc_t *dc = get_dc_by_tty(tty);
2194         unsigned long flags;
2195         int mask;
2196         int rval = -ENOIOCTLCMD;
2197
2198         D1("******** IOCTL, cmd: %d", cmd);
2199
2200         switch (cmd) {
2201         case TCGETS:
2202                 D1("IOCTL TCGETS ...");
2203                 rval = -ENOIOCTLCMD;
2204                 break;
2205         case TCSETS:
2206                 D1("IOCTL TCSETS ...");
2207                 rval = -ENOIOCTLCMD;
2208                 break;
2209         case TIOCMIWAIT:
2210                 rval = ntty_ioctl_tiocmiwait(tty, file, cmd, arg);
2211                 break;
2212         case TIOCGICOUNT:
2213                 rval =
2214                     ntty_ioctl_tiocgicount(tty, file, cmd, (void __user *)arg);
2215                 break;
2216         case TIOCMGET:
2217                 spin_lock_irqsave(&dc->spin_mutex, flags);
2218                 rval = ntty_tiocmget(tty, file);
2219                 spin_unlock_irqrestore(&dc->spin_mutex, flags);
2220                 break;
2221         case TIOCMSET:
2222                 rval = ntty_tiocmset(tty, file, arg);
2223                 break;
2224         case TIOCMBIC:
2225                 if (get_user(mask, (unsigned long __user *)arg))
2226                         return -EFAULT;
2227
2228                 spin_lock_irqsave(&dc->spin_mutex, flags);
2229                 if (mask & TIOCM_RTS)
2230                         set_rts(port->tty_index, 0);
2231                 if (mask & TIOCM_DTR)
2232                         set_dtr(port->tty_index, 0);
2233                 spin_unlock_irqrestore(&dc->spin_mutex, flags);
2234                 rval = 0;
2235                 break;
2236         case TIOCMBIS:
2237                 if (get_user(mask, (unsigned long __user *)arg))
2238                         return -EFAULT;
2239
2240                 spin_lock_irqsave(&dc->spin_mutex, flags);
2241                 if (mask & TIOCM_RTS)
2242                         set_rts(port->tty_index, 1);
2243                 if (mask & TIOCM_DTR)
2244                         set_dtr(port->tty_index, 1);
2245                 spin_unlock_irqrestore(&dc->spin_mutex, flags);
2246                 rval = 0;
2247                 break;
2248         case TCFLSH:
2249                 D1("IOCTL TCFLSH ...");
2250                 rval = -ENOIOCTLCMD;
2251                 break;
2252
2253         default:
2254                 D1("ERR: 0x%08X, %d", cmd, cmd);
2255                 break;
2256         };
2257
2258         return rval;
2259 }
2260
2261 /* Called by the upper tty layer when tty buffers are ready */
2262 /* to receive data again after a call to throttle. */
2263 static void ntty_unthrottle(struct tty_struct *tty)
2264 {
2265         struct port *port = (struct port *)tty->driver_data;
2266         dc_t *dc = get_dc_by_tty(tty);
2267         unsigned long flags;
2268
2269         D1("UNTHROTTLE");
2270         spin_lock_irqsave(&dc->spin_mutex, flags);
2271         enable_transmit_dl(port->tty_index, dc);
2272         set_rts(port->tty_index, 1);
2273
2274         spin_unlock_irqrestore(&dc->spin_mutex, flags);
2275 }
2276
2277 /* Called by the upper tty layer when the tty buffers are almost full. */
2278 /* The driver should stop send more data. */
2279 static void ntty_throttle(struct tty_struct *tty)
2280 {
2281         struct port *port = (struct port *)tty->driver_data;
2282         dc_t *dc = get_dc_by_tty(tty);
2283         unsigned long flags;
2284
2285         D1("THROTTLE");
2286         spin_lock_irqsave(&dc->spin_mutex, flags);
2287         set_rts(port->tty_index, 0);
2288         spin_unlock_irqrestore(&dc->spin_mutex, flags);
2289 }
2290
2291 static void ntty_put_char(struct tty_struct *tty, unsigned char c)
2292 {
2293         D2("PUT CHAR Function: %c", c);
2294 }
2295
2296 /* Returns number of chars in buffer, called by tty layer */
2297 static s32 ntty_chars_in_buffer(struct tty_struct *tty)
2298 {
2299         struct port *port = (struct port *)tty->driver_data;
2300         s32 rval;
2301
2302         if (!port) {
2303                 rval = -ENODEV;
2304                 goto exit_in_buffer;
2305         }
2306
2307         if (!port->tty_open_count) {
2308                 ERR("No tty open?");
2309                 rval = -ENODEV;
2310                 goto exit_in_buffer;
2311         }
2312
2313         rval = __kfifo_len(port->fifo_ul);
2314
2315       exit_in_buffer:
2316         return rval;
2317 }
2318
2319 static struct tty_operations tty_ops = {
2320         .ioctl = ntty_ioctl,
2321         .open = ntty_open,
2322         .close = ntty_close,
2323         .write = ntty_write,
2324         .write_room = ntty_write_room,
2325         .unthrottle = ntty_unthrottle,
2326         .throttle = ntty_throttle,
2327         .set_termios = ntty_set_termios,
2328         .chars_in_buffer = ntty_chars_in_buffer,
2329         .put_char = ntty_put_char,
2330 };
2331
2332 /* Initializes the tty */
2333 static int ntty_tty_init(dc_t * dc)
2334 {
2335         struct tty_driver *td;
2336         int rval;
2337         int i;
2338
2339         dc->tty_driver = alloc_tty_driver(NTTY_TTY_MINORS);
2340         if (!dc->tty_driver)
2341                 return -ENOMEM;
2342         td = dc->tty_driver;
2343         td->owner = THIS_MODULE;
2344         td->driver_name = NOZOMI_NAME_TTY;
2345         td->name = "noz";
2346         td->major = NTTY_TTY_MAJOR;
2347         td->type = TTY_DRIVER_TYPE_SERIAL;
2348         td->subtype = SERIAL_TYPE_NORMAL;
2349 #ifdef KERNEL_2_6_18
2350         td->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
2351 #else
2352         td->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS;
2353 #endif
2354         td->init_termios = tty_std_termios;
2355         td->init_termios.c_cflag = B115200 | CS8 | CREAD | HUPCL | CLOCAL;
2356
2357 #ifdef KERNEL_2_4
2358         td->table = dc->tty_table;
2359         td->refcount = &dc->tty_refcount;
2360 #endif
2361
2362         td->termios = dc->tty_termios;
2363         td->termios_locked = dc->tty_termios_locked;
2364         tty_set_operations(dc->tty_driver, &tty_ops);
2365
2366         rval = tty_register_driver(td);
2367         if (rval) {
2368                 dev_err(&dc->pdev->dev, "failed to register ntty tty driver\n");
2369                 return rval;
2370         }
2371
2372         for (i = 0; i < NTTY_TTY_MINORS; i++) {
2373                 init_MUTEX(&dc->port[i].tty_sem);
2374                 dc->port[i].tty_open_count = 0;
2375                 dc->port[i].tty = NULL;
2376 #ifdef KERNEL_2_6
2377                 tty_register_device(td, i, &dc->pdev->dev);
2378 #endif
2379         }
2380
2381         dev_info(&dc->pdev->dev, DRIVER_DESC " " NOZOMI_NAME_TTY "\n");
2382         return rval;
2383 }
2384
2385 /* Module initialization */
2386 static struct pci_driver nozomi_driver = {
2387         .name = NOZOMI_NAME,
2388         .id_table = nozomi_pci_tbl,
2389         .probe = nozomi_card_init,
2390         .remove = __devexit_p(nozomi_card_exit),
2391 };
2392
2393 static __init int nozomi_init(void)
2394 {
2395         int rval = 0;
2396
2397 //      rval = pci_module_init(&nozomi_driver);
2398         rval = pci_register_driver(&nozomi_driver);
2399         printk(KERN_INFO "Initializing %s\n", VERSION_STRING);
2400         return rval;
2401 }
2402
2403 static __exit void nozomi_exit(void)
2404 {
2405         printk(KERN_INFO "Unloading %s", DRIVER_DESC);
2406         pci_unregister_driver(&nozomi_driver);
2407 }
2408
2409 module_init(nozomi_init);
2410 module_exit(nozomi_exit);
2411
2412 #ifdef NOZOMI_DEBUG
2413 MODULE_PARM(nzdebug, "i");
2414 #endif
2415
2416 MODULE_LICENSE("Dual BSD/GPL");
2417 MODULE_DESCRIPTION(DRIVER_DESC);