MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / drivers / char / nozomi / nozomi.jared.c
blobe03e80009e949c43f5b5fe44060392bb2de52eb9
2 /* nozomi.c -- HSDPA driver Broadband Wireless Data Card - Globe Trotter
4 * Written by: Ulf Jakobsson,
5 * Jan Ã…kerfeldt,
6 * Stefan Thomasson,
8 * Maintained by: Paul Hardwick, p.hardwick@option.com
10 * Source has been ported from an implementation made by Filip Aben, f.aben@option.com
12 * --------------------------------------------------------------------------
14 Copyright (c) 2005 Option Wireless Sweden AB
15 All rights Reserved.
17 This program is free software; you can redistribute it and/or modify
18 it under the terms of the GNU General Public License as published by
19 the Free Software Foundation; either version 2 of the License, or
20 (at your option) any later version.
22 This program is distributed in the hope that it will be useful,
23 but WITHOUT ANY WARRANTY; without even the implied warranty of
24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 GNU General Public License for more details.
27 You should have received a copy of the GNU General Public License
28 along with this program; if not, write to the Free Software
29 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
31 * --------------------------------------------------------------------------
34 /* CHANGELOG
36 * See CHANGELOG in this package
39 /* TODO
41 * See TODO file in this package
46 #include <linux/version.h>
48 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0)
49 #define KERNEL_2_6
50 #elif LINUX_VERSION_CODE > KERNEL_VERSION(2,4,0)
51 #define KERNEL_2_4
52 #endif
54 #include <linux/module.h>
55 #include <linux/init.h>
56 #include <linux/pci.h>
57 #include <linux/ioport.h>
58 #include <linux/tty.h>
59 #include <linux/tty_driver.h>
60 #include <linux/tty_flip.h>
61 #include <linux/serial.h>
62 #include <linux/interrupt.h>
63 #include <linux/kmod.h>
64 #include <linux/proc_fs.h>
65 #include <asm/uaccess.h>
68 #define VERSION_STRING DRIVER_DESC " (build date: " __DATE__ " " __TIME__ ")"
70 #ifdef KERNEL_2_6
71 #if 0
72 #include <linux/kfifo.h>
73 #endif
74 #include "kfifo.h"
75 #include <linux/workqueue.h>
76 #else
77 #include "kfifo.h"
78 #include <linux/tqueue.h>
79 #endif
81 #ifdef KERNEL_2_4
82 #ifndef IRQ_NONE
83 #define IRQ_NONE
84 #endif
85 #ifndef IRQ_HANDLED
86 #define IRQ_HANDLED
87 typedef void irqreturn_t;
88 #endif
89 #endif
91 #ifndef CONFIG_PCI
92 #error "This driver needs PCI support to be available"
93 #endif
95 /* Macros definitions */
97 /* Enable this to have a lot of debug printouts */
98 //#define NOZOMI_DEBUG
100 /* Default debug printout level */
101 #define NOZOMI_DEBUG_LEVEL 0xff
103 #define P_BUF_SIZE 128
104 #define NFO( _err_flag_, args...) \
105 do{ \
106 char t_m_p_[P_BUF_SIZE]; \
107 snprintf(t_m_p_, sizeof(t_m_p_), ##args); \
108 printk( _err_flag_ "[%d] %s(): %s\n", __LINE__, __FUNCTION__, t_m_p_); \
109 } while(0)
111 #define INFO(args...) NFO(KERN_INFO, ##args)
112 #define ERR(args...) NFO( KERN_ERR, ##args)
114 #define D1(args...) D_(0x01, ##args)
115 #define D2(args...) D_(0x02, ##args)
116 #define D3(args...) D_(0x04, ##args)
117 #define D4(args...) D_(0x08, ##args)
118 #define D5(args...) D_(0x10, ##args)
119 #define D6(args...) D_(0x20, ##args)
120 #define D7(args...) D_(0x40, ##args)
121 #define D8(args...) D_(0x80, ##args)
124 #ifdef NOZOMI_DEBUG
125 #define D_(lvl, args...) D(lvl, ##args)
126 /* Do we need this settable at runtime? */
127 static int nzdebug = NOZOMI_DEBUG_LEVEL;
129 #define D(lvl, args...) do{if(lvl & nzdebug) NFO(KERN_ALERT, ##args );}while(0)
130 #define D_(lvl, args...) D(lvl, ##args)
132 /* These printouts are always printed */
134 #else
135 static const int nzdebug = 0;
136 #define D_(lvl, args...)
137 #endif
139 /* TODO: rewrite to optimize macros... */
140 #define SET_FCR(value__) \
141 do { \
142 writew((value__), (u32) (dc->REG_FCR )); \
143 } while(0)
145 #define SET_IER(value__, mask__) \
146 do { \
147 dc->ier_last_written = (dc->ier_last_written & ~mask__) | (value__ & mask__ );\
148 writew( dc->ier_last_written, (u32)(dc->REG_IER));\
149 } while(0)
151 #define GET_IER(read_val__) \
152 do { \
153 (read_val__) = readw((u32) (dc->REG_IER));\
154 } while(0)
156 #define GET_IIR(read_val__) \
157 do { \
158 (read_val__) = readw((u32) (dc->REG_IIR));\
159 } while(0)
161 #define GET_MEM(value__, addr__, length__) \
162 do { \
163 read_mem32( (u32*) (value__), (u32) (addr__), (length__));\
164 } while(0)
166 #define GET_MEM_BUF(value__, addr__, length__) \
167 do { \
168 read_mem32_buf( (u32*) (value__), (u32) (addr__), (length__));\
169 } while(0)
171 #define SET_MEM(addr__, value__, length__) \
172 do { \
173 write_mem32( (addr__), (u32*) (value__), (length__));\
174 } while(0)
176 #define SET_MEM_BUF(addr__, value__, length__) \
177 do { \
178 write_mem32_buf( (addr__), (u32*) (value__), (length__));\
179 } while(0)
182 #define TMP_BUF_MAX 256
184 #define DUMP(buf__,len__) \
185 do { \
186 char tbuf[TMP_BUF_MAX]={0};\
187 if (len__>1) {\
188 snprintf(tbuf, len__ > TMP_BUF_MAX ? TMP_BUF_MAX : len__, "%s",buf__);\
189 if(tbuf[len__-2] == '\r') {\
190 tbuf[len__-2] = 'r';\
192 D1( "SENDING: '%s' (%d+n)", tbuf, len__);\
193 } else {\
194 D1( "SENDING: '%s' (%d)", tbuf, len__);\
196 } while(0)
198 #define RELEVANT_IFLAG(iflag) ((iflag) & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
201 /* Defines */
202 #define NOZOMI_NAME "nozomi"
203 #define NOZOMI_NAME_TTY "nozomi_tty"
204 #define DRIVER_DESC "Nozomi driver"
206 #define NTTY_TTY_MAJOR 242
207 #define NTTY_TTY_MINORS MAX_PORT
208 #define NTTY_FIFO_BUFFER_SIZE 8192
210 /* Must be power of 2 */
211 #define FIFO_BUFFER_SIZE_UL 8192
213 /* Size of tmp send buffer to card */
214 #define SEND_BUF_MAX 1024
215 #define RECEIVE_BUF_MAX 4
217 /* Our fake UART values */
218 #define MCR_DTR 0x01
219 #define MCR_RTS 0x02
220 #define MCR_LOOP 0x04
221 #define MSR_CTS 0x08
222 #define MSR_CD 0x10
223 #define MSR_RI 0x20
224 #define MSR_DSR 0x40
226 /* Define all types of vendors and devices to support */
227 #define VENDOR1 0x1931 /* Vendor Option */
228 #define DEVICE1 0x000c /* HSDPA card */
230 #define R_IIR 0x0000 /* Interrupt Identity Register */
231 #define R_FCR 0x0000 /* Flow Control Register */
232 #define R_IER 0x0004 /* Interrupt Enable Register */
234 #define CONFIG_MAGIC 0xEFEFFEFE
235 #define TOGGLE_VALID 0x0000
237 /* Definition of interrupt tokens */
238 #define MDM_DL1 0x0001
239 #define MDM_UL1 0x0002
240 #define MDM_DL2 0x0004
241 #define MDM_UL2 0x0008
242 #define DIAG_DL1 0x0010
243 #define DIAG_DL2 0x0020
244 #define DIAG_UL 0x0040
245 #define APP1_DL 0x0080
246 #define APP1_UL 0x0100
247 #define APP2_DL 0x0200
248 #define APP2_UL 0x0400
249 #define CTRL_DL 0x0800
250 #define CTRL_UL 0x1000
251 #define RESET 0x8000
253 #define MDM_DL (MDM_DL1 | MDM_DL2)
254 #define MDM_UL (MDM_UL1 | MDM_UL2)
255 #define DIAG_DL (DIAG_DL1 | DIAG_DL2)
257 /* modem signal definition */
258 #define CTRL_DSR 0x0001
259 #define CTRL_DCD 0x0002
260 #define CTRL_RI 0x0004
261 #define CTRL_CTS 0x0008
263 #define CTRL_DTR 0x0001
264 #define CTRL_RTS 0x0002
266 #define MAX_PORT 4
267 #define NOZOMI_MAX_PORTS 5
269 /* Type definitions */
271 /* There are two types of nozomi cards, one with 2048 memory and with 8192 memory */
272 typedef enum {
273 F32_2 = 2048, /* Has 512 bytes downlink and uplink * 2 -> 2048 */
274 F32_8 = 8192, /* Has 3072 bytes downlink and 1024 bytes uplink * 2 -> 8192 */
275 } card_type_t;
277 /* Two different toggle channels exist */
278 typedef enum {
279 CH_A=0,
280 CH_B=1,
281 } channel_t;
283 /* Port definition for the card regarding flow control */
284 typedef enum {
285 CTRL_CMD = 0x00,
286 CTRL_MDM = 0x01,
287 CTRL_DIAG = 0x02,
288 CTRL_APP1 = 0x03,
289 CTRL_APP2 = 0x04,
290 CTRL_ERROR = -1,
291 } ctrl_port_t;
293 /* Ports that the nozomi has */
294 typedef enum {
295 PORT_MDM = 0,
296 PORT_DIAG= 1,
297 PORT_APP1= 2,
298 PORT_APP2= 3,
299 PORT_CTRL= 4,
300 PORT_ERROR=-1,
301 } port_type_t;
303 #ifdef __ARMEB__
304 /* Big endian */
306 typedef struct {
307 unsigned enabled : 5; /* Toggle fields are valid if enabled is 0, else A-channels
308 must always be used. */
309 unsigned diag_dl : 1;
310 unsigned mdm_dl : 1;
311 unsigned mdm_ul : 1;
312 } __attribute__ ((packed)) toggles_t;
314 /* Configuration table to read at startup of card */
315 /* Is for now only needed during initialization phase */
316 typedef struct {
317 u32 signature;
318 u16 product_information;
319 u16 version;
320 u8 pad3[3];
321 toggles_t toggle;
322 u8 pad1[4];
323 u16 dl_mdm_len1; /* If this is 64, it can hold 60 bytes + 4 that is length field */
324 u16 dl_start;
326 u16 dl_diag_len1;
327 u16 dl_mdm_len2; /* If this is 64, it can hold 60 bytes + 4 that is length field */
328 u16 dl_app1_len;
330 u16 dl_diag_len2;
331 u16 dl_ctrl_len;
332 u16 dl_app2_len;
333 u8 pad2[16];
334 u16 ul_mdm_len1;
335 u16 ul_start;
336 u16 ul_diag_len;
337 u16 ul_mdm_len2;
338 u16 ul_app1_len;
339 u16 ul_app2_len;
340 u16 ul_ctrl_len;
341 } __attribute__((packed)) config_table_t;
343 /* This stores all control downlink flags */
344 typedef struct {
345 u8 port;
346 unsigned reserved : 4;
347 unsigned CTS : 1;
348 unsigned RI : 1;
349 unsigned DCD : 1;
350 unsigned DSR : 1;
351 } __attribute__ ((packed)) ctrl_dl_t;
353 /* This stores all control uplink flags */
354 typedef struct {
355 u8 port;
356 unsigned reserved : 6;
357 unsigned RTS : 1;
358 unsigned DTR : 1;
359 } __attribute__ ((packed)) ctrl_ul_t;
361 #else
362 /* Little endian */
364 /* This represents the toggle information */
365 typedef struct {
366 unsigned mdm_ul : 1;
367 unsigned mdm_dl : 1;
368 unsigned diag_dl : 1;
369 unsigned enabled : 5; /* Toggle fields are valid if enabled is 0, else A-channels
370 must always be used. */
371 } __attribute__ ((packed)) toggles_t;
373 /* Configuration table to read at startup of card */
374 typedef struct {
375 u32 signature;
376 u16 version;
377 u16 product_information;
378 toggles_t toggle;
379 u8 pad1[7];
380 u16 dl_start;
381 u16 dl_mdm_len1; /* If this is 64, it can hold 60 bytes + 4 that is length field */
382 u16 dl_mdm_len2;
383 u16 dl_diag_len1;
384 u16 dl_diag_len2;
385 u16 dl_app1_len;
386 u16 dl_app2_len;
387 u16 dl_ctrl_len;
388 u8 pad2[16];
389 u16 ul_start;
390 u16 ul_mdm_len2;
391 u16 ul_mdm_len1;
392 u16 ul_diag_len;
393 u16 ul_app1_len;
394 u16 ul_app2_len;
395 u16 ul_ctrl_len;
396 } __attribute__((packed)) config_table_t;
398 /* This stores all control downlink flags */
399 typedef struct {
400 unsigned DSR : 1;
401 unsigned DCD : 1;
402 unsigned RI : 1;
403 unsigned CTS : 1;
404 unsigned reserverd : 4;
405 u8 port;
406 } __attribute__ ((packed)) ctrl_dl_t;
408 /* This stores all control uplink flags */
409 typedef struct {
410 unsigned DTR : 1;
411 unsigned RTS : 1;
412 unsigned reserved : 6;
413 u8 port;
414 } __attribute__ ((packed)) ctrl_ul_t;
415 #endif
417 /* This holds all information that is needed regarding a port */
418 typedef struct {
419 u8 update_flow_control;
420 ctrl_ul_t ctrl_ul;
421 ctrl_dl_t ctrl_dl;
422 struct kfifo *fifo_ul;
423 u32 dl_addr[2];
424 u32 dl_size[2];
425 u8 toggle_dl;
426 u32 ul_addr[2];
427 u32 ul_size[2];
428 u8 toggle_ul;
429 u16 token_dl;
431 struct tty_struct *tty;
432 int tty_open_count;
433 struct semaphore tty_sem;
434 wait_queue_head_t tty_wait;
435 struct async_icount tty_icount;
436 int tty_index;
437 u32 rx_data, tx_data;
438 u8 tty_dont_flip;
440 } port_t;
442 /* Private data one for each card in the system */
443 typedef struct {
444 u32 base_addr;
445 u8 closing;
447 /* Register addresses */
448 u32 REG_IIR;
449 u32 REG_FCR;
450 u32 REG_IER;
452 volatile u16 ier_last_written;
453 card_type_t card_type;
454 config_table_t config_table; /* Configuration table */
455 struct pci_dev *pdev;
457 // Add by Jared 05-29-2006. Because we need to know which port we would wakeup in task queue.
458 port_type_t index;
459 #ifdef KERNEL_2_6
460 struct work_struct nozomi_wq;
461 #elif defined KERNEL_2_4
462 struct tq_struct tqueue; /* task queue for line discipline waking up */
463 #endif
465 port_t port[NOZOMI_MAX_PORTS];
466 u8 *send_buf;
467 struct tty_driver tty_driver;
469 #ifdef KERNEL_2_4
470 struct tty_struct *tty_table[NTTY_TTY_MINORS];
471 struct tq_struct tty_flip_queue;
472 s32 tty_refcount;
473 #endif
474 #ifdef KERNEL_2_6
475 struct workqueue_struct *tty_flip_wq;
476 struct work_struct tty_flip_wq_struct;
477 #endif
479 struct termios *tty_termios[NTTY_TTY_MINORS];
480 struct termios *tty_termios_locked[NTTY_TTY_MINORS];
481 spinlock_t spin_mutex;
483 u32 open_ttys;
484 struct proc_dir_entry *proc_entry;
486 } dc_t;
488 /* This is a data packet that is read or written to/from card */
489 typedef struct {
490 u32 size; /* size is the length of the data buffer */
491 u8 *data;
492 } __attribute__ ((packed)) buf_t;
494 /* Function declarations */
495 static int ntty_tty_init(dc_t *dc);
497 static void tty_flip_queue_function(void *tmp_dc);
499 /* Global variables */
500 static struct pci_device_id nozomi_pci_tbl[] __devinitdata = {
501 {VENDOR1, DEVICE1, 0, 0, 0, 0, 0},
502 {0, }
505 /* Used to store interrupt variables */
506 typedef struct {
507 volatile u16 read_iir; /* Holds current interrupt tokens */
508 } irq_t;
510 MODULE_DEVICE_TABLE(pci, nozomi_pci_tbl);
512 /* Representing the pci device of interest */
513 static int cards_found;
514 dc_t* my_dev = NULL;
515 struct pci_dev *my_pdev = NULL;
516 irq_t my_irq;
520 static inline dc_t* get_dc_by_pdev(struct pci_dev* pdev) {
521 return my_dev;
524 static inline dc_t* get_dc_by_index(s32 index ) {
525 return my_dev;
528 static inline s32 get_index(struct tty_struct *tty) {
529 #ifdef KERNEL_2_6
530 return tty->index;
531 #else
532 return MINOR(tty->device) - tty->driver.minor_start;
533 #endif
536 static inline port_t* get_port_by_tty(struct tty_struct *tty) {
537 return &my_dev->port[ get_index(tty) ];
540 static inline dc_t* get_dc_by_tty(struct tty_struct *tty ) {
541 return my_dev;
545 /* TODO: */
546 /* -Optimize */
547 /* -Rewrite cleaner */
548 static void read_mem32(u32 *buf, u32 mem_addr_start, u32 size_bytes) {
549 u32 i = 0;
550 u32* ptr = (u32*) mem_addr_start;
551 u16* buf16;
553 /* 2 bytes */
554 if (size_bytes == 2) {
555 buf16 = (u16*) buf;
556 *buf16 = readw( (u32)ptr );
557 return;
560 while (i < size_bytes) {
561 if ( size_bytes - i == 2) {
562 /* Handle 2 bytes in the end */
563 buf16 = (u16*) buf;
564 *(buf16) = readw( (u32)ptr );
565 i+=2;
566 } else {
567 /* Read 4 bytes */
568 *(buf) = readl( (u32)ptr );
569 i+=4;
571 buf++; ptr++;
575 /* TODO: */
576 /* - Rewrite cleaner */
577 /* - merge with read_mem32() */
578 static void read_mem32_buf(u32 *buf, u32 mem_addr_start, u32 size_bytes) {
579 #ifdef __ARMEB__
580 u32 i = 0;
581 u32* ptr = (u32*) mem_addr_start;
582 u16* buf16;
584 /* 2 bytes */
585 if (size_bytes == 2) {
586 buf16 = (u16*) buf;
587 *buf16 = __le16_to_cpu( readw( (u32)ptr ));
588 return;
591 while (i < size_bytes) {
592 if ( size_bytes - i == 2) {
593 /* Handle 2 bytes in the end */
594 buf16 = (u16*) buf;
595 *(buf16) = __le16_to_cpu( readw( (u32)ptr ));
596 i+=2;
597 } else {
598 /* Read 4 bytes */
599 *(buf) = __le32_to_cpu( readl( (u32)ptr ));
600 i+=4;
602 buf++; ptr++;
604 #else
605 read_mem32(buf, mem_addr_start, size_bytes);
606 #endif
609 /* TODO: */
610 /* -Optimize */
611 /* -Rewrite cleaner */
612 static u32 write_mem32(u32 mem_addr_start, u32 *buf, u32 size_bytes) {
613 u32 i = 0;
614 u32* ptr = (u32*) mem_addr_start;
615 u16* buf16;
617 /* 2 bytes */
618 if (size_bytes == 2) {
619 buf16 = (u16*) buf;
620 writew( (u16)*buf16, (u32)ptr);
621 return 2;
624 while (i < size_bytes) {
625 if ( size_bytes - i == 2) {
626 /* 2 bytes */
627 buf16 = (u16*) buf;
628 writew( (u16)*buf16, (u32)ptr);
629 i+=2;
630 } else {
631 /* 4 bytes */
632 writel( (u32)*buf, (u32)ptr );
633 i += 4;
635 buf++; ptr++;
637 return size_bytes;
640 /* Todo: */
641 /* - Merge with write_mem32() */
642 static u32 write_mem32_buf(u32 mem_addr_start, u32 *buf, u32 size_bytes) {
643 #ifdef __ARMEB__
644 u32 i = 0;
645 u32* ptr = (u32*) mem_addr_start;
646 u16* buf16;
648 /* 2 bytes */
649 if (size_bytes == 2) {
650 buf16 = (u16*) buf;
651 writew( __le16_to_cpu(*buf16), (u32)ptr);
652 return 2;
655 while (i < size_bytes) {
656 if ( size_bytes - i == 2) {
657 /* 2 bytes */
658 buf16 = (u16*) buf;
659 writew( __le16_to_cpu(*buf16), (u32)ptr);
660 i+=2;
661 } else {
662 /* 4 bytes */
663 writel( __cpu_to_le32( *buf ), (u32)ptr );
664 i += 4;
666 buf++; ptr++;
668 return size_bytes;
669 #else
670 return write_mem32(mem_addr_start, buf, size_bytes);
671 #endif
674 /* Setup pointers to different channels and also setup buffer sizes. */
675 static void setup_memory(dc_t *dc) {
677 u32 offset = dc->base_addr + dc->config_table.dl_start;
678 /* The length reported is including the length field of 4 bytes, hence subtract with 4. */
679 u16 buff_offset = 4;
681 /* Modem port dl configuration */
682 dc->port[PORT_MDM].dl_addr[CH_A] = offset;
683 dc->port[PORT_MDM].dl_addr[CH_B] = (offset += dc->config_table.dl_mdm_len1);
684 dc->port[PORT_MDM].dl_size[CH_A] = dc->config_table.dl_mdm_len1 - buff_offset;
685 dc->port[PORT_MDM].dl_size[CH_B] = dc->config_table.dl_mdm_len2 - buff_offset;
687 /* Diag port dl configuration */
688 dc->port[PORT_DIAG].dl_addr[CH_A] = (offset += dc->config_table.dl_mdm_len2);
689 dc->port[PORT_DIAG].dl_size[CH_A] = dc->config_table.dl_diag_len1 - buff_offset;
690 dc->port[PORT_DIAG].dl_addr[CH_B] = (offset += dc->config_table.dl_diag_len1);
691 dc->port[PORT_DIAG].dl_size[CH_B] = dc->config_table.dl_diag_len2 - buff_offset;
693 /* App1 port dl configuration */
694 dc->port[PORT_APP1].dl_addr[CH_A] = (offset += dc->config_table.dl_diag_len2);
695 dc->port[PORT_APP1].dl_size[CH_A] = dc->config_table.dl_app1_len - buff_offset;
697 /* App2 port dl configuration */
698 dc->port[PORT_APP2].dl_addr[CH_A] = (offset += dc->config_table.dl_app1_len);
699 dc->port[PORT_APP2].dl_size[CH_A] = dc->config_table.dl_app2_len - buff_offset;
701 /* Ctrl dl configuration */
702 dc->port[PORT_CTRL].dl_addr[CH_A] = (offset += dc->config_table.dl_app2_len);
703 dc->port[PORT_CTRL].dl_size[CH_A] = dc->config_table.dl_ctrl_len - buff_offset;
706 /* Modem Port ul configuration */
707 dc->port[PORT_MDM].ul_addr[CH_A] = (offset = dc->base_addr + dc->config_table.ul_start);
708 dc->port[PORT_MDM].ul_size[CH_A] = dc->config_table.ul_mdm_len1 - buff_offset;
709 dc->port[PORT_MDM].ul_addr[CH_B] = (offset += dc->config_table.ul_mdm_len1);
710 dc->port[PORT_MDM].ul_size[CH_B] = dc->config_table.ul_mdm_len2 - buff_offset;
712 /* Diag port ul configuration */
713 dc->port[PORT_DIAG].ul_addr[CH_A] = (offset += dc->config_table.ul_mdm_len2);
714 dc->port[PORT_DIAG].ul_size[CH_A] = dc->config_table.ul_diag_len - buff_offset;
716 /* App1 port ul configuration */
717 dc->port[PORT_APP1].ul_addr[CH_A] = (offset += dc->config_table.ul_diag_len);
718 dc->port[PORT_APP1].ul_size[CH_A] = dc->config_table.ul_app1_len - buff_offset;
720 /* App2 port ul configuration */
721 dc->port[PORT_APP2].ul_addr[CH_A] = (offset += dc->config_table.ul_app1_len);
722 dc->port[PORT_APP2].ul_size[CH_A] = dc->config_table.ul_app2_len - buff_offset;
724 /* Ctrl ul configuration */
725 dc->port[PORT_CTRL].ul_addr[CH_A] = (offset += dc->config_table.ul_app2_len);
726 dc->port[PORT_CTRL].ul_size[CH_A] = dc->config_table.ul_ctrl_len - buff_offset;
727 offset = dc->config_table.ul_start;
730 /* Dump config table under initalization phase */
731 #ifdef NOZOMI_DEBUG
732 static void dump_table(dc_t *dc) {
733 D3("signature: 0x%08X", dc->config_table.signature);
734 D3("version: 0x%04X", dc->config_table.version);
735 D3("product_information: 0x%04X", dc->config_table.product_information);
736 D3("toggle enabled: %d", dc->config_table.toggle.enabled);
737 D3("toggle up_mdm: %d", dc->config_table.toggle.mdm_ul);
738 D3("toggle dl_mdm: %d", dc->config_table.toggle.mdm_dl);
739 D3("toggle dl_dbg: %d", dc->config_table.toggle.diag_dl);
741 D3("dl_start: 0x%04X", dc->config_table.dl_start);
742 D3("dl_mdm_len0: 0x%04X, %d", dc->config_table.dl_mdm_len1, dc->config_table.dl_mdm_len1);
743 D3("dl_mdm_len1: 0x%04X, %d", dc->config_table.dl_mdm_len2, dc->config_table.dl_mdm_len2);
744 D3("dl_diag_len0: 0x%04X, %d", dc->config_table.dl_diag_len1, dc->config_table.dl_diag_len1);
745 D3("dl_diag_len1: 0x%04X, %d", dc->config_table.dl_diag_len2, dc->config_table.dl_diag_len2);
746 D3("dl_app1_len: 0x%04X, %d", dc->config_table.dl_app1_len, dc->config_table.dl_app1_len);
747 D3("dl_app2_len: 0x%04X, %d", dc->config_table.dl_app2_len, dc->config_table.dl_app2_len);
748 D3("dl_ctrl_len: 0x%04X, %d", dc->config_table.dl_ctrl_len, dc->config_table.dl_ctrl_len);
749 D3("ul_start: 0x%04X, %d", dc->config_table.ul_start, dc->config_table.ul_start);
750 D3("ul_mdm_len[0]: 0x%04X, %d", dc->config_table.ul_mdm_len1, dc->config_table.ul_mdm_len1);
751 D3("ul_mdm_len[1]: 0x%04X, %d", dc->config_table.ul_mdm_len2, dc->config_table.ul_mdm_len2);
752 D3("ul_diag_len: 0x%04X, %d", dc->config_table.ul_diag_len, dc->config_table.ul_diag_len);
753 D3("ul_app1_len: 0x%04X, %d", dc->config_table.ul_app1_len, dc->config_table.ul_app1_len);
754 D3("ul_app2_len: 0x%04X, %d", dc->config_table.ul_app2_len, dc->config_table.ul_app2_len);
755 D3("ul_ctrl_len: 0x%04X, %d", dc->config_table.ul_ctrl_len, dc->config_table.ul_ctrl_len);
757 #endif
759 /* Read configuration table from card under intalization phase */
760 /* Returns 1 if ok, else 0 */
761 static int nozomi_read_config_table(dc_t *dc) {
763 GET_MEM( &dc->config_table, dc->base_addr + 0, sizeof(config_table_t));
765 /* D1( "0x%08X == 0x%08X ", dc->config_table.signature, CONFIG_MAGIC); */
767 if( dc->config_table.signature != CONFIG_MAGIC ) {
768 ERR("ConfigTable Bad! 0x%08X != 0x%08X", dc->config_table.signature, CONFIG_MAGIC);
769 return 0;
772 if( (dc->config_table.version == 0) || (dc->config_table.toggle.enabled == TOGGLE_VALID) ) {
773 int i;
774 D1( "Second phase, configuring card");
776 setup_memory(dc);
778 dc->port[PORT_MDM].toggle_ul = dc->config_table.toggle.mdm_ul;
779 dc->port[PORT_MDM].toggle_dl = dc->config_table.toggle.mdm_dl;
780 dc->port[PORT_DIAG].toggle_dl = dc->config_table.toggle.diag_dl;
781 D1( "toggle ports: MDM UL:%d MDM DL:%d, DIAG DL:%d",
782 dc->port[PORT_MDM].toggle_ul,
783 dc->port[PORT_MDM].toggle_dl,
784 dc->port[PORT_DIAG].toggle_dl);
786 #ifdef NOZOMI_DEBUG
787 dump_table(dc);
788 #endif
789 for (i=PORT_MDM; i< MAX_PORT;i++) {
790 dc->port[i].fifo_ul = kfifo_alloc( FIFO_BUFFER_SIZE_UL, GFP_ATOMIC , NULL);
791 memset( &dc->port[i].ctrl_dl, 0, sizeof (ctrl_dl_t));
792 memset( &dc->port[i].ctrl_ul, 0, sizeof (ctrl_ul_t));
795 /* Enable control channel */
796 SET_IER( CTRL_DL, CTRL_DL );
798 INFO("Initialization OK!");
799 return 1;
802 if( (dc->config_table.version > 0) && (dc->config_table.toggle.enabled != TOGGLE_VALID ) ) {
803 u32 offset = 0;
804 D1( "First phase: pushing upload buffers, clearing download");
806 INFO("Version of card: %d", dc->config_table.version);
808 /* Here we should disable all I/O over F32. */
809 setup_memory(dc);
811 /* We should send ALL channel pair tokens back along with reset token */
813 /* push upload modem buffers */
814 SET_MEM( dc->port[PORT_MDM].ul_addr[CH_A], &offset, 4);
815 SET_MEM( dc->port[PORT_MDM].ul_addr[CH_B], &offset, 4);
817 SET_FCR( MDM_UL | DIAG_DL | MDM_DL );
819 D1( "First phase done");
822 return 1;
825 /* Enable uplink interrupts */
826 static void enable_transmit_ul( port_type_t port , dc_t *dc ) {
828 switch( port ) {
829 case PORT_MDM: SET_IER( MDM_UL , MDM_UL ); break;
830 case PORT_DIAG: SET_IER( DIAG_UL, DIAG_UL ); break;
831 case PORT_APP1: SET_IER( APP1_UL, APP1_UL ); break;
832 case PORT_APP2: SET_IER( APP2_UL, APP2_UL ); break;
833 case PORT_CTRL: SET_IER( CTRL_UL, CTRL_UL ); break;
834 default:
835 ERR("Called with wrong port?");
836 break;
840 /* Disable uplink interrupts */
841 static void disable_transmit_ul( port_type_t port , dc_t *dc ) {
843 switch( port ) {
844 case PORT_MDM: SET_IER( 0 ,MDM_UL ); break;
845 case PORT_DIAG: SET_IER( 0, DIAG_UL ); break;
846 case PORT_APP1: SET_IER( 0, APP1_UL ); break;
847 case PORT_APP2: SET_IER( 0, APP2_UL ); break;
848 case PORT_CTRL: SET_IER( 0, CTRL_UL ); break;
849 default:
850 ERR("Called with wrong port?");
851 break;
855 /* Enable downlink interrupts */
856 static void enable_transmit_dl( port_type_t port , dc_t *dc ) {
857 #if 1 // Written by Jared. 05-24-2006. For portect the global variable ier_last_written
858 u32 flags;
860 spin_lock_irqsave(&dc->spin_mutex, flags);
861 #endif
862 switch( port ) {
863 case PORT_MDM: SET_IER( MDM_DL , MDM_DL ); break;
864 case PORT_DIAG: SET_IER( DIAG_DL, DIAG_DL ); break;
865 case PORT_APP1: SET_IER( APP1_DL, APP1_DL ); break;
866 case PORT_APP2: SET_IER( APP2_DL, APP2_DL ); break;
867 case PORT_CTRL: SET_IER( CTRL_DL, CTRL_DL ); break;
869 default:
870 ERR("Called with wrong port?");
871 break;
873 #if 1 // Written by Jared. 05-24-2006. For portect the global variable ier_last_written
874 spin_unlock_irqrestore(&dc->spin_mutex, flags);
875 #endif
878 /* Disable downlink interrupts */
879 static void disable_transmit_dl( port_type_t port , dc_t *dc ) {
881 switch( port ) {
882 case PORT_MDM: SET_IER( 0 ,MDM_DL ); break;
883 case PORT_DIAG: SET_IER( 0, DIAG_DL ); break;
884 case PORT_APP1: SET_IER( 0, APP1_DL ); break;
885 case PORT_APP2: SET_IER( 0, APP2_DL ); break;
886 case PORT_CTRL: SET_IER( 0, CTRL_DL ); break;
887 default:
888 ERR("Called with wrong port?");
889 break;
893 static void nozomi_softint(void *private)
895 dc_t *dc = private;
896 port_t *port = &dc->port[dc->index];
897 struct tty_struct *tty = port->tty;
899 if (port->tty) {
900 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) && tty->ldisc.write_wakeup) {
901 tty->ldisc.write_wakeup(tty);
903 wake_up_interruptible(&tty->write_wait);
907 /* Return 1 - send buffer to card and ack. */
908 /* Return 0 - don't ack, don't send buffer to card. */
909 int send_data( port_type_t index, dc_t *dc ) {
910 u32 size = 0;
911 port_t *port = &dc->port[index];
912 u8 toggle = port->toggle_ul;
913 u32 addr = port->ul_addr[toggle];
914 u32 ul_size = port->ul_size[toggle];
915 //struct tty_struct *tty = port->tty;
917 if (index >= NTTY_TTY_MINORS) {
918 ERR("Called with wrong index?");
919 return 0;
922 /* Get data from tty and place in buf for now */
923 size = __kfifo_get( port->fifo_ul, dc->send_buf, ul_size < SEND_BUF_MAX ? ul_size : SEND_BUF_MAX );
925 if (size == 0) {
926 D4("No more data to send, disable link:");
927 return 0;
930 port->tx_data += size;
932 /* DUMP(buf, size); */
934 /* Write length + data */
935 SET_MEM( addr, &size, 4 );
936 SET_MEM_BUF( addr + 4, dc->send_buf, size);
937 #if 0 // Mask by Jared 05-29-2006. Should not wakeup line displine in interrupt.
938 if (port->tty) {
939 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) && tty->ldisc.write_wakeup) {
940 tty->ldisc.write_wakeup(tty);
942 wake_up_interruptible(&tty->write_wait);
944 #else
945 // Modify by Jared 05-29-2006. Wake up the line displine in the task queue.
946 dc->index = index;
947 # ifdef KERNEL_2_6
948 schedule_work(&dc->nozomi_wq);
949 # else
950 queue_task(&dc->tqueue, &tq_immediate);
951 mark_bh(IMMEDIATE_BH);
952 # endif
953 #endif
954 return 1;
957 /* If all data has been read, return 1, else 0 */
958 static int receive_data( port_type_t index, dc_t* dc ) {
959 u8 buf[RECEIVE_BUF_MAX] = {0};
960 int size;
961 u32 offset = 4;
962 port_t *port = &dc->port[index];
963 u8 toggle = port->toggle_dl;
964 u32 addr = port->dl_addr[toggle];
965 struct tty_struct *tty = port->tty;
966 int i;
968 if ( !tty ) {
969 D1("tty not open for port: %d?", index);
970 return 1;
973 if (test_bit(TTY_DONT_FLIP, &tty->flags)) {
974 D6("TTY_DONT_FLIP set!! %d", index);
975 /* Here we disable interrupt for that port and schedule */
976 /* task. Task wakes up a little bit later and enables interrupt.. */
977 port->tty_dont_flip = 1;
978 disable_transmit_dl(index, dc);
979 #ifdef KERNEL_2_4
980 schedule_task(&dc->tty_flip_queue);
981 #endif
982 #ifdef KERNEL_2_6
983 if (!queue_work(dc->tty_flip_wq, &dc->tty_flip_wq_struct)) {
984 ERR("Call to queue_work() failed.");
986 #endif
987 return 0;
990 GET_MEM( &size, addr, 4 );
991 /* D1( "%d bytes port: %d", size, index); */
993 if ( test_bit( TTY_THROTTLED, & tty->flags) ) {
994 D1("No room in tty, don't read data, don't ack interrupt, disable interrupt");
996 /* disable interrupt in downlink... */
997 disable_transmit_dl(index, dc);
998 return 0;
1001 if (size == 0) {
1002 ERR("size == 0?");
1003 return 1;
1006 while( size > 0 ) {
1007 GET_MEM_BUF(buf, addr + offset, 4);
1009 i = 0;
1010 while (i < 4 && size > 0) {
1011 if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
1012 tty_flip_buffer_push(tty);
1014 tty_insert_flip_char(tty, buf[i], TTY_NORMAL);
1015 port->rx_data++;
1016 i++;
1017 size--;
1020 offset += 4;
1023 tty_flip_buffer_push(tty);
1025 return 1;
1028 /* Debug for interrupts */
1029 #ifdef NOZOMI_DEBUG
1030 static char* interrupt2str( u16 interrupt) {
1031 static char buf[TMP_BUF_MAX];
1032 char *p = buf;
1034 interrupt & MDM_DL1 ? p += snprintf(p, TMP_BUF_MAX, "MDM_DL1 "):0;
1035 interrupt & MDM_DL2 ? p += snprintf(p, TMP_BUF_MAX, "MDM_DL2 "):0;
1037 interrupt & MDM_UL1 ? p += snprintf(p, TMP_BUF_MAX, "MDM_UL1 "):0;
1038 interrupt & MDM_UL2 ? p += snprintf(p, TMP_BUF_MAX, "MDM_UL2 "):0;
1040 interrupt & DIAG_DL1 ? p += snprintf(p, TMP_BUF_MAX, "DIAG_DL1 "):0;
1041 interrupt & DIAG_DL2 ? p += snprintf(p, TMP_BUF_MAX, "DIAG_DL2 "):0;
1043 interrupt & DIAG_UL ? p += snprintf(p, TMP_BUF_MAX, "DIAG_UL "):0;
1045 interrupt & APP1_DL ? p += snprintf(p, TMP_BUF_MAX, "APP1_DL "):0;
1046 interrupt & APP2_DL ? p += snprintf(p, TMP_BUF_MAX, "APP2_DL "):0;
1048 interrupt & APP1_UL ? p += snprintf(p, TMP_BUF_MAX, "APP1_UL "):0;
1049 interrupt & APP2_UL ? p += snprintf(p, TMP_BUF_MAX, "APP2_UL "):0;
1051 interrupt & CTRL_DL ? p += snprintf(p, TMP_BUF_MAX, "CTRL_DL "):0;
1052 interrupt & CTRL_UL ? p += snprintf(p, TMP_BUF_MAX, "CTRL_UL "):0;
1054 interrupt & RESET ? p += snprintf(p, TMP_BUF_MAX, "RESET "):0;
1056 return buf;
1058 #endif
1060 /* Receive flow control */
1061 /* Return 1 - If ok, else 0 */
1062 static int receive_flow_control( dc_t *dc, irq_t *m) {
1063 port_type_t port = PORT_MDM;
1064 ctrl_dl_t ctrl_dl;
1065 ctrl_dl_t old_ctrl;
1066 u16 enable_ier = 0;
1068 GET_MEM( &ctrl_dl, dc->port[PORT_CTRL].dl_addr[CH_A], 2);
1070 switch( ctrl_dl.port ) {
1071 case CTRL_CMD:
1072 D1( "The Base Band sends this value as a response to a request for IMSI detach sent"
1073 " over the control channel uplink (see section 7.6.1).");
1074 break;
1075 case CTRL_MDM: port = PORT_MDM; enable_ier = MDM_DL; break;
1076 case CTRL_DIAG: port = PORT_DIAG; enable_ier = DIAG_DL; break;
1077 case CTRL_APP1: port = PORT_APP1; enable_ier = APP1_DL; break;
1078 case CTRL_APP2: port = PORT_APP2; enable_ier = APP2_DL; break;
1079 default:
1080 ERR("ERROR: flow control received for non-existing port");
1081 return 0;
1084 D1( "0x%04X->0x%04X", *((u16*) &dc->port[port].ctrl_dl), *((u16*)&ctrl_dl));
1086 old_ctrl = dc->port[port].ctrl_dl;
1087 dc->port[port].ctrl_dl = ctrl_dl;
1089 if ( old_ctrl.CTS == 1 && ctrl_dl.CTS == 0 ) {
1090 D1( "Disable interrupt (0x%04X) on port: %d", enable_ier, port);
1091 disable_transmit_ul(port, dc);
1093 } else if ( old_ctrl.CTS == 0 && ctrl_dl.CTS == 1 ) {
1095 if ( __kfifo_len(dc->port[port].fifo_ul) ) {
1096 D1( "Enable interrupt (0x%04X) on port: %d", enable_ier, port);
1097 D1( "Data in buffer [%d], enable transmit! ", __kfifo_len(dc->port[port].fifo_ul) );
1098 enable_transmit_ul( port, dc );
1099 } else {
1100 D1( "No data in buffer...");
1104 if(*(u16*)&old_ctrl == *(u16*)&ctrl_dl)
1106 D1( " No change in mctrl");
1107 return 1;
1109 /* Update statistics */
1110 if(old_ctrl.CTS != ctrl_dl.CTS) {
1111 dc->port[port].tty_icount.cts++;
1113 if(old_ctrl.DSR != ctrl_dl.DSR) {
1114 dc->port[port].tty_icount.dsr++;
1116 if(old_ctrl.RI != ctrl_dl.RI) {
1117 dc->port[port].tty_icount.rng++;
1119 if(old_ctrl.DCD != ctrl_dl.DCD) {
1120 dc->port[port].tty_icount.dcd++;
1122 D1("port: %d DCD(%d), CTS(%d), RI(%d), DSR(%d)",
1123 port,
1124 dc->port[port].tty_icount.dcd, dc->port[port].tty_icount.cts,
1125 dc->port[port].tty_icount.rng, dc->port[port].tty_icount.dsr);
1127 return 1;
1130 /* TODO: */
1131 /* - return ctrl_port_t */
1132 static u8 port2ctrl(port_type_t port) {
1133 switch( port ) {
1134 case PORT_MDM:
1135 return CTRL_MDM;
1136 case PORT_DIAG:
1137 return CTRL_DIAG;
1138 case PORT_APP1:
1139 return CTRL_APP1;
1140 case PORT_APP2:
1141 return CTRL_APP2;
1142 default:
1143 ERR("ERROR: send flow control received for non-existing port");
1144 return -1;
1146 return -1;
1149 /* Send flow control, can only update one channel at a time */
1150 /* Return 0 - If we have updated all flow control */
1151 /* Return 1 - If we need to update more flow control, ack current enable more */
1152 static int send_flow_control( dc_t *dc ) {
1153 u32 i, more_flow_control_to_be_updated = 0;
1154 u16* ctrl;
1156 for( i=PORT_MDM; i<MAX_PORT ; i++ ) {
1157 if( dc->port[i].update_flow_control ) {
1158 if ( more_flow_control_to_be_updated ) {
1159 /* We have more flow control to be updated */
1160 return 1;
1162 dc->port[i].ctrl_ul.port = port2ctrl(i);
1163 ctrl = (u16*) &dc->port[i].ctrl_ul;
1164 /* D1( "sending flow control 0x%04X for port %d, %d", (u16) *ctrl, i, dc->port[i].ctrl_ul.port ); */
1165 SET_MEM( dc->port[PORT_CTRL].ul_addr[0], (u32*) ctrl, 2 );
1166 dc->port[i].update_flow_control = 0;
1167 more_flow_control_to_be_updated = 1;
1170 return 0;
1173 /* Handle donlink data, ports that are handled are modem and diagnostics */
1174 /* Return 1 - ok */
1175 /* Return 0 - toggle fields are out of sync */
1176 static int handle_data_dl(dc_t *dc, irq_t *m, port_type_t port, u8 *toggle, u16 mask1, u16 mask2) {
1178 if ( *toggle == 0 && m->read_iir & mask1 ) {
1179 if (receive_data( port, dc )) {
1180 SET_FCR( mask1 );
1181 *toggle = !(*toggle);
1184 if ( m->read_iir & mask2 ) {
1185 if (receive_data( port, dc )) {
1186 SET_FCR( mask2 );
1187 *toggle = !(*toggle);
1190 } else if ( *toggle == 1 && m->read_iir & mask2 ) {
1191 if (receive_data( port, dc )) {
1192 SET_FCR( mask2 );
1193 *toggle = !(*toggle);
1196 if ( m->read_iir & mask1 ) {
1197 if (receive_data( port, dc )) {
1198 SET_FCR( mask1 );
1199 *toggle = !(*toggle);
1202 } else {
1203 ERR("port out of sync!, toggle:%d", *toggle);
1204 return 0;
1206 return 1;
1209 /* Handle uplink data, this is currently for the modem port */
1210 /* Return 1 - ok */
1211 /* Return 0 - toggle field are out of sync */
1212 static int handle_data_ul(dc_t *dc, irq_t *m, port_type_t port) {
1214 u8 *toggle = &(dc->port[port].toggle_ul);
1216 if ( *toggle==0 && m->read_iir & MDM_UL1 ) {
1217 SET_IER( 0, MDM_UL );
1218 if (send_data(port, dc)) {
1219 SET_FCR( MDM_UL1 );
1220 SET_IER( MDM_UL, MDM_UL);
1221 *toggle = !*toggle;
1224 if ( m->read_iir & MDM_UL2 ) {
1225 SET_IER( 0, MDM_UL );
1226 if (send_data(port, dc)) {
1227 SET_FCR( MDM_UL2 );
1228 SET_IER( MDM_UL, MDM_UL);
1229 *toggle = !*toggle;
1233 } else if ( *toggle==1 && m->read_iir & MDM_UL2 ) {
1234 SET_IER( 0, MDM_UL );
1235 if (send_data(port, dc)) {
1236 SET_FCR( MDM_UL2 );
1237 SET_IER( MDM_UL, MDM_UL);
1238 *toggle = !*toggle;
1241 if ( m->read_iir & MDM_UL1 ) {
1242 SET_IER( 0, MDM_UL );
1243 if (send_data(port, dc)) {
1244 SET_FCR( MDM_UL1 );
1245 SET_IER( MDM_UL, MDM_UL);
1246 *toggle = !*toggle;
1249 } else {
1250 SET_FCR( m->read_iir & MDM_UL );
1251 ERR("port out of sync!");
1252 return 0;
1254 return 1;
1257 static irqreturn_t interrupt_handler(int irq, void *dev_id, struct pt_regs *regs) {
1258 dc_t *dc = NULL;
1259 irq_t* m = &my_irq;
1261 if (my_dev && my_dev->pdev != dev_id) {
1262 return IRQ_NONE;
1265 if ( !(dc = get_dc_by_pdev(dev_id)) ) {
1266 ERR("Could not find device context from pci_dev: %d", (u32) dev_id);
1267 return IRQ_NONE;
1270 #if 1 // add by Victor Yu. 05-24-2006
1271 spin_lock(&dc->spin_mutex);
1272 #endif
1273 GET_IIR( m->read_iir );
1275 /* Just handle interrupt enabled in IER (by masking with dc->ier_last_written) */
1276 m->read_iir &= dc->ier_last_written;
1278 if (m->read_iir == 0) {
1279 #if 1 // add by Victor Yu. 05-24-2006
1280 spin_unlock(&dc->spin_mutex);
1281 #endif
1282 return IRQ_NONE;
1285 #if 0 // mask by Victor Yu. 05-24-2006
1286 if (dc == NULL ) {
1287 ERR("ERROR!!");
1288 return IRQ_NONE;
1290 spin_lock(&dc->spin_mutex);
1291 #endif
1293 D4( "%s irq:0x%04X, prev:0x%04X", interrupt2str(m->read_iir), m->read_iir, dc->ier_last_written);
1295 if ( m->read_iir & RESET) {
1296 if( !nozomi_read_config_table(dc) ) {
1297 SET_IER( 0, 0xFFFF);
1298 ERR("ERR: Could not read status from card, we should disable interface");
1299 } else {
1300 SET_FCR( RESET );
1302 goto exit_handler; /* No more useful info if this was the reset interrupt. */
1304 if ( m->read_iir & CTRL_UL ) {
1305 D1( "CTRL_UL");
1306 SET_IER( 0, CTRL_UL );
1307 if ( send_flow_control(dc) ) {
1308 SET_FCR( CTRL_UL );
1309 SET_IER( CTRL_UL, CTRL_UL );
1312 if ( m->read_iir & CTRL_DL ) {
1313 receive_flow_control(dc, m);
1314 SET_FCR( CTRL_DL );
1316 if ( m->read_iir & MDM_DL ) {
1317 if ( !(handle_data_dl(dc, m, PORT_MDM, &(dc->port[PORT_MDM].toggle_dl), MDM_DL1, MDM_DL2)) ) {
1318 ERR("MDM_DL out of sync!");
1319 goto exit_handler;
1322 if ( m->read_iir & MDM_UL ) {
1323 if ( !handle_data_ul(dc, m, PORT_MDM ) ) {
1324 ERR("MDM_UL out of sync!");
1325 goto exit_handler;
1328 if ( m->read_iir & DIAG_DL ) {
1329 if ( !(handle_data_dl(dc, m, PORT_DIAG, &(dc->port[PORT_DIAG].toggle_dl), DIAG_DL1, DIAG_DL2)) ) {
1330 ERR("DIAG_DL out of sync!");
1331 goto exit_handler;
1334 if ( m->read_iir & DIAG_UL ) {
1335 SET_IER( 0, DIAG_UL );
1336 if( send_data( PORT_DIAG, dc ) ) {
1337 SET_FCR( DIAG_UL );
1338 SET_IER( DIAG_UL, DIAG_UL );
1341 if ( m->read_iir & APP1_DL ) {
1342 if (receive_data( PORT_APP1, dc ) ) {
1343 SET_FCR( APP1_DL );
1346 if ( m->read_iir & APP1_UL ) {
1347 SET_IER( 0, APP1_UL );
1348 if(send_data( PORT_APP1, dc )) {
1349 SET_FCR( APP1_UL );
1350 SET_IER( APP1_UL, APP1_UL );
1353 if ( m->read_iir & APP2_DL ) {
1354 if (receive_data( PORT_APP2, dc )) {
1355 SET_FCR( APP2_DL );
1358 if ( m->read_iir & APP2_UL ) {
1359 SET_IER( 0, APP2_UL );
1360 if(send_data( PORT_APP2, dc )) {
1361 SET_FCR( APP2_UL );
1362 SET_IER( APP2_UL, APP2_UL );
1366 exit_handler:
1367 spin_unlock(&dc->spin_mutex);
1368 return IRQ_HANDLED;
1371 /* Request a shared IRQ from system */
1372 static int nozomi_setup_interrupt(struct pci_dev *pdev) {
1374 int rval = 0;
1376 if ( (rval = request_irq( pdev->irq, &interrupt_handler, SA_SHIRQ, NOZOMI_NAME, pdev )) ) {
1377 ERR("Cannot open because IRQ %d is already in use.", pdev->irq );
1378 return rval;
1381 return rval;
1384 static void nozomi_get_card_type(dc_t *dc) {
1385 u32 i, size=0;
1386 for(i=0;i<6;i++) {
1387 size += pci_resource_len(dc->pdev, i);
1390 /* Assume card type F32_8 if no match */
1391 dc->card_type = size == 2048 ? F32_2 : F32_8;
1393 INFO("Card type is: %d", dc->card_type );
1396 void nozomi_setup_private_data(dc_t *dc) {
1397 u32 offset = dc->base_addr + dc->card_type/2;
1398 int i;
1400 dc->REG_FCR = offset + R_FCR;
1401 dc->REG_IIR = offset + R_IIR;
1402 dc->REG_IER = offset + R_IER;
1403 dc->ier_last_written = 0;
1404 dc->closing = 0;
1406 dc->port[PORT_MDM ].token_dl = MDM_DL;
1407 dc->port[PORT_DIAG].token_dl = DIAG_DL;
1408 dc->port[PORT_APP1].token_dl = APP1_DL;
1409 dc->port[PORT_APP2].token_dl = APP2_DL;
1411 for(i=PORT_MDM;i<MAX_PORT;i++) {
1412 dc->port[i].rx_data = dc->port[i].tx_data = 0;
1413 dc->port[i].tty_dont_flip = 0;
1418 static void tty_flip_queue_function(void *tmp_dc) {
1419 dc_t *dc = (dc_t*) tmp_dc;
1420 int i;
1422 /* Enable interrupt for that port */
1423 for(i=0;i<MAX_PORT;i++) {
1424 if (dc->port[i].tty_dont_flip) {
1425 D6("Enable for port: %d", i);
1426 dc->port[i].tty_dont_flip = 0;
1427 enable_transmit_dl(dc->port[i].tty_index, dc);
1432 static int read_proc_card_type(char *buf, char **start, off_t offset, int len) {
1433 dc_t *dc = get_dc_by_index(0);
1434 len = 0;
1436 len += sprintf(buf+len,"%d\n", dc->card_type);
1437 return len;
1440 static int read_proc_open_ttys(char *buf, char **start, off_t offset, int len) {
1441 dc_t *dc = get_dc_by_index(0);
1442 len = 0;
1444 len += sprintf(buf+len,"%d\n", dc->open_ttys);
1445 return len;
1448 static int read_proc_version(char *buf, char **start, off_t offset, int len) {
1449 len = 0;
1451 len += sprintf(buf+len, "%s\n", VERSION_STRING);
1452 return len;
1455 static int read_proc_rtx(char *buf, char **start, off_t offset, int len) {
1456 dc_t *dc = get_dc_by_index(0);
1457 int i;
1459 len = 0;
1462 for(i=PORT_MDM;i<MAX_PORT;i++) {
1463 len += sprintf(buf+len,"noz%d rx: %d, tx: %d\n", i, dc->port[i].rx_data, dc->port[i].tx_data);
1465 return len;
1468 static void make_proc_dirs(void) {
1469 dc_t *dc = get_dc_by_index(0);
1471 dc->proc_entry = proc_mkdir("nozomi", &proc_root);
1473 /* Register the read_proc */
1474 if ( !create_proc_info_entry("card_type",0,dc->proc_entry,read_proc_card_type) ) {
1475 ERR("ERROR: failed to register read_procmem");
1477 if ( !create_proc_info_entry("open_ttys",0,dc->proc_entry,read_proc_open_ttys) ) {
1478 ERR("ERROR: failed to register read_procmem");
1480 if ( !create_proc_info_entry("rtx",0,dc->proc_entry,read_proc_rtx) ) {
1481 ERR("ERROR: failed to register read_procmem");
1483 if ( !create_proc_info_entry("version",0,dc->proc_entry,read_proc_version) ) {
1484 ERR("ERROR: failed to register read_procmem");
1488 static void remove_proc_dirs(void) {
1489 dc_t *dc = get_dc_by_index(0);
1491 remove_proc_entry("card_type", dc->proc_entry);
1492 remove_proc_entry("open_ttys", dc->proc_entry);
1493 remove_proc_entry("rtx", dc->proc_entry);
1494 remove_proc_entry("version", dc->proc_entry);
1495 remove_proc_entry("nozomi", &proc_root);
1498 /* Allocate memory for one device */
1499 #if 0 // Mask by Jared 05-24-2006
1500 static int __devinit nozomi_card_init(struct pci_dev *pdev, const struct pci_device_id *ent) {
1501 #else
1502 static int nozomi_card_init(struct pci_dev *pdev, const struct pci_device_id *ent) {
1503 #endif
1504 int ret = -EIO;
1505 dc_t *dc=NULL;
1507 cards_found++;
1508 INFO("Init, cards_found: %d", cards_found);
1510 if (!(my_dev = kmalloc(sizeof(dc_t), GFP_KERNEL))) {
1511 D1( "Could not allocate memory");
1512 return -EIO;
1515 memset(my_dev, 0, sizeof( dc_t ));
1517 if (cards_found > 1) {
1518 ERR("This driver only supports 1 device");
1519 return -ENODEV;
1522 my_dev->pdev = pdev;
1523 dc = my_dev;
1525 /* Find out what card type it is */
1526 nozomi_get_card_type(dc);
1528 if (pci_enable_device(dc->pdev)) {
1529 ERR("Not possible to enable PCI Device");
1530 return -ENODEV;
1533 if ( (dc->base_addr = pci_resource_start(dc->pdev, 0)) == 0x0000) {
1534 ERR("No I/O-Address for card detected");
1535 ret = -ENODEV;
1536 goto err_disable_device;
1539 if (!(dc->base_addr = (u32) ioremap(dc->base_addr, dc->card_type))) {
1540 ERR("No I/O-Address for card detected");
1541 ret = -ENODEV;
1542 goto err_disable_device;
1545 dc->open_ttys=0;
1547 // Add by Jared 05-29-2006. Wake up the line displine in the task queue.
1548 #ifdef KERNEL_2_6
1549 INIT_WORK(&dc->nozomi_wq, nozomi_softint, my_dev);
1550 #else
1551 dc->tqueue.routine = nozomi_softint;
1552 dc->tqueue.data = my_dev;
1553 #endif
1555 nozomi_setup_private_data(dc);
1557 if (pci_request_regions(dc->pdev, NOZOMI_NAME)) {
1558 ERR("I/O address 0x%04x already in use",
1559 (int) /* nozomi_private.io_addr */ 0);
1560 ret = -EIO;
1561 goto err_disable_regions;
1564 if ( !(dc->send_buf = kmalloc(SEND_BUF_MAX, GFP_KERNEL))) {
1565 ERR("Could not allocate send buffer?");
1566 goto err_disable_regions;
1569 /* Disable all interrupts */
1570 SET_IER( 0 , 0xFFFF );
1572 /* Setup interrupt handler */
1573 if (nozomi_setup_interrupt(dc->pdev)) {
1574 ret = -EIO;
1575 goto err_disable_regions;
1578 D1( "base_addr: 0x%08X", dc->base_addr);
1580 #ifdef KERNEL_2_4
1581 dc->tty_flip_queue.list.next = NULL;
1582 dc->tty_flip_queue.list.prev = NULL;
1583 dc->tty_flip_queue.sync = 0;
1584 dc->tty_flip_queue.data = dc;
1585 dc->tty_flip_queue.routine = tty_flip_queue_function;
1586 #endif
1587 #ifdef KERNEL_2_6
1588 if ( !(dc->tty_flip_wq = create_singlethread_workqueue(NOZOMI_NAME )) ) {
1589 ERR("Could not create workqueue?");
1590 BUG_ON(!dc->tty_flip_wq);
1591 return -ENOMEM;
1593 INIT_WORK( &dc->tty_flip_wq_struct, tty_flip_queue_function, (void*) dc);
1594 #endif
1596 spin_lock_init(&dc->spin_mutex);
1598 make_proc_dirs();
1600 ntty_tty_init(dc);
1602 /* Enable RESET interrupt. */
1603 SET_IER( RESET, 0xFFFF );
1605 return 0;
1607 err_disable_regions:
1608 pci_release_regions(pdev);
1609 iounmap((void *)dc->base_addr );
1610 dc->base_addr = 0;
1612 err_disable_device:
1613 pci_disable_device(pdev);
1614 if(my_dev) {
1615 kfree( my_dev );
1617 return ret;
1620 static void tty_do_close(dc_t *dc, port_t *port) {
1621 u32 flags;
1623 down(&port->tty_sem);
1625 if ( !port->tty_open_count ) {
1626 goto exit;
1629 dc->open_ttys--;
1630 port->tty_open_count--;
1631 #ifdef KERNEL_2_4
1632 MOD_DEC_USE_COUNT;
1633 #endif
1635 if ( port->tty_open_count == 0) {
1636 D1("close: %d", port->token_dl );
1638 #if 1 // Written by Jared. 05-24-2006. For portect the global variable ier_last_written
1639 spin_lock_irqsave(&dc->spin_mutex, flags);
1640 SET_IER( 0, port->token_dl );
1641 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1642 #else
1643 SET_IER( 0, port->token_dl );
1644 #endif
1647 exit:
1648 up(&port->tty_sem);
1651 #ifdef MODULE
1652 static void __exit tty_exit(void) {
1653 #else
1654 static void tty_exit(void) {
1655 #endif
1656 int i;
1657 dc_t *dc = my_dev;
1659 D1( " ");
1661 #ifdef KERNEL_2_6
1662 for (i = 0; i < NTTY_TTY_MINORS; ++i)
1663 tty_unregister_device(&dc->tty_driver, i);
1664 #endif
1666 tty_unregister_driver(&dc->tty_driver);
1668 for (i = 0; i < NTTY_TTY_MINORS; i++) {
1669 while (dc->port[i].tty_open_count) {
1670 tty_do_close(dc, &dc->port[i]);
1673 dc->port[i].tty = NULL;
1677 /* Deallocate memory for one device */
1678 #if 0 // Mask by Jared 05-24-2006
1679 static void __devexit nozomi_card_exit(struct pci_dev *pdev) {
1680 #else
1681 static void nozomi_card_exit(struct pci_dev *pdev) {
1682 #endif
1683 int i;
1684 ctrl_ul_t ctrl;
1685 dc_t *dc = get_dc_by_pdev(pdev);
1686 u32 flags;
1688 /* Disable all interrupts */
1689 #if 1 // Written by Jared. 05-24-2006. For portect the global variable ier_last_written
1690 spin_lock_irqsave(&dc->spin_mutex, flags);
1691 SET_IER( 0, 0xFFFF);
1692 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1693 #else
1694 SET_IER( 0, 0xFFFF);
1695 #endif
1696 /* Send 0x0001, command card to resend the reset token. */
1697 /* This is to get the reset when the module is reloaded. */
1698 ctrl.port = 0x00; ctrl.reserved = 0; ctrl.RTS=0; ctrl.DTR=1;
1699 D1( "sending flow control 0x%04X", * ((u16*) &ctrl) );
1701 /* Setup dc->reg addresses to we can use defines here */
1702 nozomi_setup_private_data(dc);
1703 SET_MEM( dc->port[PORT_CTRL].ul_addr[0], (u32*) &ctrl, 2 );
1704 SET_FCR( CTRL_UL ); /* push the token to the card. */
1706 D1( "pci_release_regions");
1707 pci_release_regions(pdev);
1709 if(dc->base_addr)
1710 iounmap((void *)dc->base_addr);
1712 D1( "pci_disable_device");
1713 pci_disable_device(pdev);
1715 free_irq( pdev->irq, pdev );
1717 for (i=PORT_MDM; i< MAX_PORT;i++) {
1718 kfree ( dc->port[i].fifo_ul );
1721 kfree( dc->send_buf );
1723 tty_exit();
1725 remove_proc_dirs();
1727 #ifdef KERNEL_2_6
1728 destroy_workqueue(dc->tty_flip_wq);
1729 #endif
1731 if (my_dev) {
1732 kfree( my_dev );
1735 cards_found--;
1738 static void set_rts(int index, int rts) {
1739 dc_t *dc = get_dc_by_index(index);
1740 #if 1 // Written by Jared. 05-24-2006. For portect the global variable ier_last_written
1741 u32 flags;
1743 spin_lock_irqsave(&dc->spin_mutex, flags);
1744 #endif
1745 dc->port[index].ctrl_ul.RTS = rts;
1746 dc->port[index].update_flow_control = 1;
1747 enable_transmit_ul(PORT_CTRL, dc);
1748 #if 1 // Written by Jared. 05-24-2006. For portect the global variable ier_last_written
1749 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1750 #endif
1753 static void set_dtr(int index, int dtr) {
1754 dc_t *dc = get_dc_by_index(index);
1756 D1("SETTING DTR index: %d, dtr: %d", index, dtr);
1758 dc->port[index].ctrl_ul.DTR = dtr;
1759 dc->port[index].update_flow_control = 1;
1760 enable_transmit_ul(PORT_CTRL, dc);
1764 /* ---------------------------------------------------------------------------------------------------
1765 TTY code
1766 ---------------------------------------------------------------------------------------------------*/
1769 /* Called when the userspace process opens the tty, /dev/noz*. */
1770 static int ntty_open(struct tty_struct *tty, struct file *file) {
1772 s32 index = get_index(tty);
1773 port_t *port = get_port_by_tty(tty);
1774 dc_t *dc = get_dc_by_tty(tty);
1775 u32 flags;
1777 down(&port->tty_sem);
1779 tty->low_latency = 1;
1780 tty->driver_data = port;
1781 port->tty = tty;
1782 port->tty_index = index;
1784 port->tty_open_count++;
1785 dc->open_ttys++;
1787 #ifdef KERNEL_2_4
1788 MOD_INC_USE_COUNT;
1789 #endif
1791 /* Enable interrupt downlink for channel */
1792 if ( port->tty_open_count == 1) {
1793 port->rx_data = port->tx_data = 0;
1794 D1("open: %d", port->token_dl );
1795 #if 1 // Written by Jared. 05-24-2006. For portect the global variable ier_last_written
1796 spin_lock_irqsave(&dc->spin_mutex, flags);
1797 SET_IER( port->token_dl, port->token_dl );
1798 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1799 #else
1800 SET_IER( port->token_dl, port->token_dl );
1801 #endif
1805 up(&port->tty_sem);
1807 return 0;
1810 /* Called when the userspace process close the tty, /dev/noz*. */
1811 static void ntty_close(struct tty_struct *tty, struct file *file) {
1812 dc_t *dc = get_dc_by_tty(tty);
1813 tty_do_close(dc, (port_t *) tty->driver_data);
1816 /* called when the userspace process writes to the tty (/dev/noz*). */
1817 /* Data is inserted into a fifo, which is then read and transfered to the modem. */
1818 #ifdef KERNEL_2_6
1819 static int ntty_write(struct tty_struct *tty, const unsigned char *buffer, int count) {
1820 #else
1821 static s32 ntty_write(struct tty_struct *tty, s32 from_user, const u8 *buffer, s32 count) {
1822 #endif
1823 int rval = -EINVAL;
1824 dc_t *dc = get_dc_by_tty(tty);
1825 port_t *port = (port_t *) tty->driver_data;
1826 u32 flags;
1828 /* D1( "WRITEx: %d, index = %d", count, index); */
1830 if (! port) {
1831 return -ENODEV;
1834 down(&port->tty_sem);
1836 if (! port->tty_open_count) {
1837 D1( " ");
1838 goto exit;
1840 #if 0 // Written by Jared. 05-24-2006. For portect the global variable ier_last_written
1841 spin_lock_irqsave(&dc->spin_mutex, flags);
1842 #endif
1844 #ifdef KERNEL_2_4
1845 if (from_user) {
1846 rval = __kfifo_put_user(port->fifo_ul, (unsigned char *) buffer, count);
1847 } else {
1848 rval = __kfifo_put(port->fifo_ul, (unsigned char *) buffer, count);
1850 #else
1851 rval = __kfifo_put(port->fifo_ul, (unsigned char *) buffer, count);
1852 #endif
1854 #if 0 // Written by Jared. 05-24-2006. For portect the global variable ier_last_written
1855 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1856 #endif
1858 /* notify card */
1859 if ( dc == NULL) {
1860 D1( "No device context?");
1861 goto exit;
1864 #if 1 // Written by Jared. 05-24-2006. For portect the global variable ier_last_written
1865 spin_lock_irqsave(&dc->spin_mutex, flags);
1866 #endif
1867 // CTS is only valid on the modem channel
1868 if ( port == &(dc->port[PORT_MDM]) ) {
1869 if ( port->ctrl_dl.CTS ) {
1870 D4( "Enable interrupt");
1871 enable_transmit_ul(port->tty_index, dc );
1872 } else {
1873 ERR("CTS not active on modem port?");
1875 } else {
1876 enable_transmit_ul(port->tty_index, dc );
1878 #if 1 // Written by Jared. 05-24-2006. For portect the global variable ier_last_written
1879 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1880 #endif
1881 exit:
1882 up(&port->tty_sem);
1883 return rval;
1886 /* Calculate how much is left in device */
1887 /* This method is called by the upper tty layer. */
1888 /* #according to sources N_TTY.c it expects a value >= 0 and does not check for negative values. */
1889 static int ntty_write_room(struct tty_struct *tty) {
1890 port_t *port = (port_t *) tty->driver_data;
1891 int room = 0;
1892 // u32 flags = 0;
1893 // dc_t *dc = get_dc_by_tty(tty);
1895 if (! port) {
1896 return 0;
1899 down(&port->tty_sem);
1901 if (! port->tty_open_count) {
1902 goto exit;
1905 room = port->fifo_ul->size - __kfifo_len(port->fifo_ul);
1907 exit:
1908 up(&port->tty_sem);
1909 return room;
1912 /* Sets termios flags, called by the tty layer. */
1913 static void ntty_set_termios(struct tty_struct *tty, struct termios *old_termios) {
1914 unsigned int cflag;
1916 cflag = tty->termios->c_cflag;
1918 if (old_termios) {
1919 if ((cflag == old_termios->c_cflag) &&
1920 (RELEVANT_IFLAG(tty->termios->c_iflag) == RELEVANT_IFLAG(old_termios->c_iflag))) {
1921 D1( " - nothing to change...");
1922 goto exit_termios;
1926 /* get the byte size */
1927 switch (cflag & CSIZE) {
1928 case CS5:
1929 D1( " - data bits = 5");
1930 break;
1931 case CS6:
1932 D1( " - data bits = 6");
1933 break;
1934 case CS7:
1935 D1( " - data bits = 7");
1936 break;
1937 default:
1938 case CS8:
1939 D1( " - data bits = 8");
1940 break;
1943 /* determine the parity */
1944 if (cflag & PARENB) {
1945 if (cflag & PARODD) {
1946 D1( " - parity = odd");
1947 } else {
1948 D1( " - parity = even");
1950 } else {
1951 D1( " - parity = none");
1954 /* figure out the stop bits requested */
1955 if (cflag & CSTOPB) {
1956 D1( " - stop bits = 2");
1957 } else {
1958 D1( " - stop bits = 1");
1961 /* figure out the hardware flow control settings */
1962 if (cflag & CRTSCTS) {
1963 D1( " - RTS/CTS is enabled");
1964 } else {
1965 D1( " - RTS/CTS is disabled");
1968 /* determine software flow control */
1969 /* if we are implementing XON/XOFF, set the start and
1970 * stop character in the device */
1971 if (I_IXOFF(tty) || I_IXON(tty)) {
1972 #ifdef NOZOMI_DEBUG
1973 unsigned char stop_char = STOP_CHAR(tty);
1974 unsigned char start_char = START_CHAR(tty);
1975 #endif
1976 /* if we are implementing INBOUND XON/XOFF */
1977 if (I_IXOFF(tty)) {
1978 D1( " - INBOUND XON/XOFF is enabled, "
1979 "XON = %2x, XOFF = %2x", start_char, stop_char);
1980 } else {
1981 D1( " - INBOUND XON/XOFF is disabled");
1984 /* if we are implementing OUTBOUND XON/XOFF */
1985 if (I_IXON(tty)) {
1986 D1( " - OUTBOUND XON/XOFF is enabled, "
1987 "XON = %2x, XOFF = %2x", start_char, stop_char);
1988 } else {
1989 D1( " - OUTBOUND XON/XOFF is disabled");
1993 exit_termios:
1994 return;
1997 /* Gets io control parameters */
1998 static int ntty_tiocmget(struct tty_struct *tty, struct file *file) {
1999 port_t *port = (port_t *) tty->driver_data;
2000 ctrl_dl_t *ctrl_dl = &port->ctrl_dl;
2001 ctrl_ul_t *ctrl_ul = &port->ctrl_ul;
2003 return 0
2004 | (ctrl_ul->RTS ? TIOCM_RTS : 0)
2005 | (ctrl_ul->DTR ? TIOCM_DTR : 0)
2006 | (ctrl_dl->DCD ? TIOCM_CAR : 0)
2007 | (ctrl_dl->RI ? TIOCM_RNG : 0)
2008 | (ctrl_dl->DSR ? TIOCM_DSR : 0)
2009 | (ctrl_dl->CTS ? TIOCM_CTS : 0);
2012 /* Sets io controls parameters */
2013 static int ntty_tiocmset(struct tty_struct *tty, struct file *file, u32 arg) {
2014 port_t *port = (port_t *) tty->driver_data;
2016 set_rts(port->tty_index, (arg & TIOCM_RTS) ? 1 : 0);
2017 set_dtr(port->tty_index, (arg & TIOCM_DTR) ? 1 : 0);
2019 return 0;
2022 static int ntty_ioctl_tiocmiwait(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg) {
2023 port_t *port = (port_t *) tty->driver_data;
2025 if (cmd == TIOCMIWAIT) {
2026 DECLARE_WAITQUEUE(wait, current);
2027 struct async_icount cnow;
2028 struct async_icount cprev;
2030 cprev = port->tty_icount;
2031 while (1) {
2032 add_wait_queue(&port->tty_wait, &wait);
2033 set_current_state(TASK_INTERRUPTIBLE);
2034 schedule();
2035 remove_wait_queue(&port->tty_wait, &wait);
2037 /* see if a signal woke us up */
2038 if (signal_pending(current))
2039 return -ERESTARTSYS;
2041 cnow = port->tty_icount;
2042 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
2043 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
2044 return -EIO; /* no change => error */
2045 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
2046 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
2047 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
2048 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
2049 return 0;
2051 cprev = cnow;
2055 return -ENOIOCTLCMD;
2058 static int ntty_ioctl_tiocgicount(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg) {
2059 port_t *port = (port_t *) tty->driver_data;
2061 if (cmd == TIOCGICOUNT) {
2062 struct async_icount cnow = port->tty_icount;
2063 struct serial_icounter_struct icount;
2065 icount.cts = cnow.cts;
2066 icount.dsr = cnow.dsr;
2067 icount.rng = cnow.rng;
2068 icount.dcd = cnow.dcd;
2069 icount.rx = cnow.rx;
2070 icount.tx = cnow.tx;
2071 icount.frame = cnow.frame;
2072 icount.overrun = cnow.overrun;
2073 icount.parity = cnow.parity;
2074 icount.brk = cnow.brk;
2075 icount.buf_overrun = cnow.buf_overrun;
2077 if (copy_to_user((void *)arg, &icount, sizeof(icount)))
2078 return -EFAULT;
2079 return 0;
2081 return -ENOIOCTLCMD;
2084 static int ntty_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg) {
2085 port_t *port = (port_t *) tty->driver_data;
2086 int mask;
2087 int rval = -ENOIOCTLCMD;
2089 D1("******** IOCTL, cmd: %d", cmd);
2092 switch (cmd) {
2093 case TCGETS:
2094 D1( "IOCTL TCGETS ...");
2095 rval = -ENOIOCTLCMD;
2096 break;
2097 case TCSETS:
2098 D1( "IOCTL TCSETS ...");
2099 rval = -ENOIOCTLCMD;
2100 break;
2101 case TIOCMIWAIT:
2102 rval = ntty_ioctl_tiocmiwait(tty, file, cmd, arg);
2103 break;
2104 case TIOCGICOUNT:
2105 rval = ntty_ioctl_tiocgicount(tty, file, cmd, arg);
2106 break;
2107 case TIOCMGET:
2108 rval = ntty_tiocmget(tty, file);
2109 break;
2110 case TIOCMSET:
2111 rval = ntty_tiocmset(tty, file, arg);
2112 break;
2113 case TIOCMBIC:
2114 if (get_user(mask, (unsigned long *) arg))
2115 return -EFAULT;
2117 if (mask & TIOCM_RTS)
2118 set_rts(port->tty_index, 0);
2119 if (mask & TIOCM_DTR)
2120 set_dtr(port->tty_index, 0);
2121 rval = 0;
2122 break;
2123 case TIOCMBIS:
2124 if (get_user(mask, (unsigned long *) arg))
2125 return -EFAULT;
2127 if (mask & TIOCM_RTS)
2128 set_rts(port->tty_index, 1);
2129 if (mask & TIOCM_DTR)
2130 set_dtr(port->tty_index, 1);
2131 rval = 0;
2132 break;
2133 case TCFLSH:
2134 D1( "IOCTL TCFLSH ...");
2135 rval = -ENOIOCTLCMD;
2136 break;
2138 default:
2139 D1( "ERR: 0x%08X, %d", cmd, cmd);
2140 break;
2143 return rval;
2146 /* Called by the upper tty layer when tty buffers are ready */
2147 /* to receive data again after a call to throttle. */
2148 static void ntty_unthrottle(struct tty_struct *tty) {
2149 port_t *port = (port_t *) tty->driver_data;
2150 dc_t *dc = get_dc_by_tty(tty);
2152 #if 0 // Masked by Jared. 05-24-2006. Move into set_rts(), enable_transmit_dl().
2153 u32 flags;
2155 spin_lock_irqsave(&dc->spin_mutex, flags);
2156 #endif
2157 D1( "UNTHROTTLE");
2159 enable_transmit_dl(port->tty_index, dc);
2160 set_rts(port->tty_index, 1);
2161 #if 0 // Masked by Jared. 05-24-2006. Move into set_rts(), enable_transmit_dl().
2162 spin_unlock_irqrestore(&dc->spin_mutex, flags);
2163 #endif
2167 /* Called by the upper tty layer when the tty buffers are almost full. */
2168 /* The driver should stop send more data. */
2169 static void ntty_throttle(struct tty_struct *tty) {
2170 port_t *port = (port_t *) tty->driver_data;
2171 #if 0 // Masked by Jared. 05-24-2006. Move into set_rts(), enable_transmit_dl().
2172 dc_t *dc = get_dc_by_tty(tty);
2173 u32 flags;
2174 spin_lock_irqsave(&dc->spin_mutex, flags);
2175 #endif
2176 D1( "THROTTLE");
2177 set_rts(port->tty_index, 0);
2178 #if 0 // Masked by Jared. 05-24-2006. Move into set_rts(), enable_transmit_dl().
2179 spin_unlock_irqrestore(&dc->spin_mutex, flags);
2180 #endif
2184 static void ntty_put_char(struct tty_struct *tty, unsigned char c) {
2185 D2("PUT CHAR Function: %c", c);
2188 /* Returns number of chars in buffer, called by tty layer */
2189 static s32 ntty_chars_in_buffer(struct tty_struct *tty) {
2190 port_t *port = (port_t *) tty->driver_data;
2191 s32 rval;
2193 if (! port) {
2194 rval = -ENODEV;
2195 goto exit_in_buffer;
2198 if (! port->tty_open_count) {
2199 ERR("No tty open?");
2200 rval = -ENODEV;
2201 goto exit_in_buffer;
2204 rval = __kfifo_len(port->fifo_ul);
2206 exit_in_buffer:
2207 return rval;
2210 /* Initializes the tty */
2211 static int ntty_tty_init(dc_t *dc) {
2212 struct tty_driver *td = &dc->tty_driver;
2213 int rval;
2214 int i;
2216 memset(td, 0, sizeof(struct tty_driver));
2218 td->magic = TTY_DRIVER_MAGIC;
2219 td->driver_name = NOZOMI_NAME_TTY;
2220 td->name = "noz";
2221 td->major = NTTY_TTY_MAJOR,
2222 td->type = TTY_DRIVER_TYPE_SERIAL,
2223 td->subtype = SERIAL_TYPE_NORMAL,
2224 td->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS,
2225 td->init_termios = tty_std_termios;
2226 td->init_termios.c_cflag = B115200 | CS8 | CREAD | HUPCL | CLOCAL;
2228 td->num = MAX_PORT;
2229 td->name_base = 0;
2230 td->minor_start = 0;
2232 #ifdef KERNEL_2_4
2233 td->table = dc->tty_table;
2234 td->refcount = &dc->tty_refcount;
2235 #endif
2237 td->termios = dc->tty_termios;
2238 td->termios_locked = dc->tty_termios_locked;
2240 td->ioctl = ntty_ioctl;
2241 td->open = ntty_open;
2242 td->close = ntty_close;
2243 td->write = ntty_write;
2244 td->write_room = ntty_write_room;
2245 td->unthrottle = ntty_unthrottle;
2246 td->throttle = ntty_throttle;
2247 td->set_termios = ntty_set_termios;
2248 td->chars_in_buffer = ntty_chars_in_buffer;
2249 td->put_char = ntty_put_char;
2251 rval = tty_register_driver(td);
2253 if (rval) {
2254 printk(KERN_ERR "failed to register ntty tty driver");
2255 D1( "failed to register ntty tty driver");
2256 return rval;
2259 for (i = 0; i < NTTY_TTY_MINORS; i++) {
2260 init_MUTEX(&dc->port[i].tty_sem);
2261 dc->port[i].tty_open_count = 0;
2262 dc->port[i].tty = NULL;
2264 #ifdef KERNEL_2_6
2265 tty_register_device(td, i, NULL);
2266 #endif
2269 printk(KERN_INFO DRIVER_DESC " " NOZOMI_NAME_TTY);
2270 D1( " ");
2271 return rval;
2274 /* Module initialization */
2275 static struct pci_driver nozomi_driver = {
2276 .name = NOZOMI_NAME,
2277 .id_table = nozomi_pci_tbl,
2278 .probe = nozomi_card_init,
2279 #if 0 // Mask by Jared 05-24-2006
2280 .remove = __devexit_p(nozomi_card_exit),
2281 #else
2282 .remove = (nozomi_card_exit),
2283 #endif
2286 static int __init nozomi_init(void) {
2287 int rval = 0;
2288 #if 0 // Mask by Jared 05-24-2006
2289 rval = pci_module_init(&nozomi_driver);
2290 #else
2291 rval = pci_register_driver(&nozomi_driver);
2292 #endif
2293 printk(KERN_INFO "Initializing %s\n", VERSION_STRING);
2294 return rval;
2297 static void __exit nozomi_exit(void) {
2298 printk(KERN_INFO "Unloading %s", DRIVER_DESC);
2300 pci_unregister_driver(&nozomi_driver);
2303 module_init(nozomi_init);
2304 module_exit(nozomi_exit);
2306 #ifdef NOZOMI_DEBUG
2307 MODULE_PARM(nzdebug, "i");
2308 #endif
2310 MODULE_LICENSE("Dual BSD/GPL");
2311 MODULE_DESCRIPTION( DRIVER_DESC );