MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / drivers / char / nozomi / nozomi.c
blob19f41ece9acf75c557ca0f9cc11d868e3568beb9
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,14)
49 #define KERNEL_2_6_14
50 #endif
52 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0)
53 #define KERNEL_2_6
54 #elif LINUX_VERSION_CODE > KERNEL_VERSION(2,4,0)
55 #define KERNEL_2_4
56 #endif
58 #include <linux/module.h>
59 #include <linux/pci.h>
60 #include <linux/ioport.h>
61 #include <linux/tty.h>
62 #include <linux/tty_driver.h>
63 #include <linux/tty_flip.h>
64 #include <linux/serial.h>
65 #include <linux/interrupt.h>
66 #include <linux/kmod.h>
67 #include <linux/proc_fs.h>
68 #include <linux/spinlock.h>
69 #include <asm/uaccess.h>
72 #define VERSION_STRING DRIVER_DESC " (build date: " __DATE__ " " __TIME__ ")"
74 //#ifdef KERNEL_2_6
75 //#include <linux/kfifo.h>
76 //#else
77 #include "kfifo.h"
78 #include <linux/workqueue.h>
80 //#endif
82 #ifdef KERNEL_2_4
83 #ifndef IRQ_NONE
84 #define IRQ_NONE
85 #endif
86 #ifndef IRQ_HANDLED
87 #define IRQ_HANDLED
88 typedef void irqreturn_t;
89 #endif
90 #endif
92 #ifndef CONFIG_PCI
93 #error "This driver needs PCI support to be available"
94 #endif
96 /* Macros definitions */
98 /* Enable this to have a lot of debug printouts */
99 /* #define NOZOMI_DEBUG */
101 /* Default debug printout level */
102 #define NOZOMI_DEBUG_LEVEL 0x1
104 #define P_BUF_SIZE 128
105 #define NFO( _err_flag_, args...) \
106 do{ \
107 char t_m_p_[P_BUF_SIZE]; \
108 snprintf(t_m_p_, sizeof(t_m_p_), ##args); \
109 printk( _err_flag_ "[%d] %s(): %s\n", __LINE__, __FUNCTION__, t_m_p_); \
110 } while(0)
112 #define INFO(args...) NFO(KERN_INFO, ##args)
113 #define ERR(args...) NFO( KERN_ERR, ##args)
115 #define D1(args...) D_(0x01, ##args)
116 #define D2(args...) D_(0x02, ##args)
117 #define D3(args...) D_(0x04, ##args)
118 #define D4(args...) D_(0x08, ##args)
119 #define D5(args...) D_(0x10, ##args)
120 #define D6(args...) D_(0x20, ##args)
121 #define D7(args...) D_(0x40, ##args)
122 #define D8(args...) D_(0x80, ##args)
125 #ifdef NOZOMI_DEBUG
126 #define D_(lvl, args...) D(lvl, ##args)
127 /* Do we need this settable at runtime? */
128 static int nzdebug = NOZOMI_DEBUG_LEVEL;
130 #define D(lvl, args...) do{if(lvl & nzdebug) NFO(KERN_DEBUG, ##args );}while(0)
131 #define D_(lvl, args...) D(lvl, ##args)
133 /* These printouts are always printed */
135 #else
136 static const int nzdebug = 0;
137 #define D_(lvl, args...)
138 #endif
140 /* TODO: rewrite to optimize macros... */
141 #define SET_FCR(value__) \
142 do { \
143 writew((value__), (void*) (dc->REG_FCR )); \
144 } while(0)
146 #define SET_IER(value__, mask__) \
147 do { \
148 dc->ier_last_written = (dc->ier_last_written & ~mask__) | (value__ & mask__ );\
149 writew( dc->ier_last_written, (void*) (dc->REG_IER));\
150 } while(0)
152 #define GET_IER(read_val__) \
153 do { \
154 (read_val__) = readw((void*) (dc->REG_IER));\
155 } while(0)
157 #define GET_IIR(read_val__) \
158 do { \
159 (read_val__) = readw((void*) (dc->REG_IIR));\
160 } while(0)
162 #define GET_MEM(value__, addr__, length__) \
163 do { \
164 read_mem32( (u32*) (value__), (u32) (addr__), (length__));\
165 } while(0)
167 #define GET_MEM_BUF(value__, addr__, length__) \
168 do { \
169 read_mem32_buf( (u32*) (value__), (u32) (addr__), (length__));\
170 } while(0)
172 #define SET_MEM(addr__, value__, length__) \
173 do { \
174 write_mem32( (addr__), (u32*) (value__), (length__));\
175 } while(0)
177 #define SET_MEM_BUF(addr__, value__, length__) \
178 do { \
179 write_mem32_buf( (addr__), (u32*) (value__), (length__));\
180 } while(0)
183 #define TMP_BUF_MAX 256
185 #define DUMP(buf__,len__) \
186 do { \
187 char tbuf[TMP_BUF_MAX]={0};\
188 if (len__>1) {\
189 snprintf(tbuf, len__ > TMP_BUF_MAX ? TMP_BUF_MAX : len__, "%s",buf__);\
190 if(tbuf[len__-2] == '\r') {\
191 tbuf[len__-2] = 'r';\
193 D1( "SENDING: '%s' (%d+n)", tbuf, len__);\
194 } else {\
195 D1( "SENDING: '%s' (%d)", tbuf, len__);\
197 } while(0)
199 #define RELEVANT_IFLAG(iflag) ((iflag) & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
202 /* Defines */
203 #define NOZOMI_NAME "nozomi"
204 #define NOZOMI_NAME_TTY "nozomi_tty"
205 #define DRIVER_DESC "Nozomi driver"
207 #define NTTY_TTY_MAJOR 241
208 #define NTTY_TTY_MINORS MAX_PORT
209 #define NTTY_FIFO_BUFFER_SIZE 8192
211 /* Must be power of 2 */
212 #define FIFO_BUFFER_SIZE_UL 8192
214 /* Size of tmp send buffer to card */
215 #define SEND_BUF_MAX 1024
216 #define RECEIVE_BUF_MAX 4
218 /* Our fake UART values */
219 #define MCR_DTR 0x01
220 #define MCR_RTS 0x02
221 #define MCR_LOOP 0x04
222 #define MSR_CTS 0x08
223 #define MSR_CD 0x10
224 #define MSR_RI 0x20
225 #define MSR_DSR 0x40
227 /* Define all types of vendors and devices to support */
228 #define VENDOR1 0x1931 /* Vendor Option */
229 #define DEVICE1 0x000c /* HSDPA card */
231 #define R_IIR 0x0000 /* Interrupt Identity Register */
232 #define R_FCR 0x0000 /* Flow Control Register */
233 #define R_IER 0x0004 /* Interrupt Enable Register */
235 #define CONFIG_MAGIC 0xEFEFFEFE
236 #define TOGGLE_VALID 0x0000
238 /* Definition of interrupt tokens */
239 #define MDM_DL1 0x0001
240 #define MDM_UL1 0x0002
241 #define MDM_DL2 0x0004
242 #define MDM_UL2 0x0008
243 #define DIAG_DL1 0x0010
244 #define DIAG_DL2 0x0020
245 #define DIAG_UL 0x0040
246 #define APP1_DL 0x0080
247 #define APP1_UL 0x0100
248 #define APP2_DL 0x0200
249 #define APP2_UL 0x0400
250 #define CTRL_DL 0x0800
251 #define CTRL_UL 0x1000
252 #define RESET 0x8000
254 #define MDM_DL (MDM_DL1 | MDM_DL2)
255 #define MDM_UL (MDM_UL1 | MDM_UL2)
256 #define DIAG_DL (DIAG_DL1 | DIAG_DL2)
258 /* modem signal definition */
259 #define CTRL_DSR 0x0001
260 #define CTRL_DCD 0x0002
261 #define CTRL_RI 0x0004
262 #define CTRL_CTS 0x0008
264 #define CTRL_DTR 0x0001
265 #define CTRL_RTS 0x0002
267 #define MAX_PORT 4
268 #define NOZOMI_MAX_PORTS 5
270 /* Type definitions */
272 /* There are two types of nozomi cards, one with 2048 memory and with 8192 memory */
273 typedef enum {
274 F32_2 = 2048, /* Has 512 bytes downlink and uplink * 2 -> 2048 */
275 F32_8 = 9192, /* Has 3072 bytes downlink and 1024 bytes uplink * 2 -> 8192 */
276 } card_type_t;
278 /* Two different toggle channels exist */
279 typedef enum {
280 CH_A=0,
281 CH_B=1,
282 } channel_t;
284 /* Port definition for the card regarding flow control */
285 typedef enum {
286 CTRL_CMD = 0x00,
287 CTRL_MDM = 0x01,
288 CTRL_DIAG = 0x02,
289 CTRL_APP1 = 0x03,
290 CTRL_APP2 = 0x04,
291 CTRL_ERROR = -1,
292 } ctrl_port_t;
294 /* Ports that the nozomi has */
295 typedef enum {
296 PORT_MDM = 0,
297 PORT_DIAG= 1,
298 PORT_APP1= 2,
299 PORT_APP2= 3,
300 PORT_CTRL= 4,
301 PORT_ERROR=-1,
302 } port_type_t;
304 #ifdef __ARMEB__
305 /* Big endian */
307 typedef struct {
308 unsigned enabled : 5; /* Toggle fields are valid if enabled is 0, else A-channels
309 must always be used. */
310 unsigned diag_dl : 1;
311 unsigned mdm_dl : 1;
312 unsigned mdm_ul : 1;
313 } __attribute__ ((packed)) toggles_t;
315 /* Configuration table to read at startup of card */
316 /* Is for now only needed during initialization phase */
317 typedef struct {
318 u32 signature;
319 u16 product_information;
320 u16 version;
321 u8 pad3[3];
322 toggles_t toggle;
323 u8 pad1[4];
324 u16 dl_mdm_len1; /* If this is 64, it can hold 60 bytes + 4 that is length field */
325 u16 dl_start;
327 u16 dl_diag_len1;
328 u16 dl_mdm_len2; /* If this is 64, it can hold 60 bytes + 4 that is length field */
329 u16 dl_app1_len;
331 u16 dl_diag_len2;
332 u16 dl_ctrl_len;
333 u16 dl_app2_len;
334 u8 pad2[16];
335 u16 ul_mdm_len1;
336 u16 ul_start;
337 u16 ul_diag_len;
338 u16 ul_mdm_len2;
339 u16 ul_app1_len;
340 u16 ul_app2_len;
341 u16 ul_ctrl_len;
342 } __attribute__((packed)) config_table_t;
344 /* This stores all control downlink flags */
345 typedef struct {
346 u8 port;
347 unsigned reserved : 4;
348 unsigned CTS : 1;
349 unsigned RI : 1;
350 unsigned DCD : 1;
351 unsigned DSR : 1;
352 } __attribute__ ((packed)) ctrl_dl_t;
354 /* This stores all control uplink flags */
355 typedef struct {
356 u8 port;
357 unsigned reserved : 6;
358 unsigned RTS : 1;
359 unsigned DTR : 1;
360 } __attribute__ ((packed)) ctrl_ul_t;
362 #else
363 /* Little endian */
365 /* This represents the toggle information */
366 typedef struct {
367 unsigned mdm_ul : 1;
368 unsigned mdm_dl : 1;
369 unsigned diag_dl : 1;
370 unsigned enabled : 5; /* Toggle fields are valid if enabled is 0, else A-channels
371 must always be used. */
372 } __attribute__ ((packed)) toggles_t;
374 /* Configuration table to read at startup of card */
375 typedef struct {
376 u32 signature;
377 u16 version;
378 u16 product_information;
379 toggles_t toggle;
380 u8 pad1[7];
381 u16 dl_start;
382 u16 dl_mdm_len1; /* If this is 64, it can hold 60 bytes + 4 that is length field */
383 u16 dl_mdm_len2;
384 u16 dl_diag_len1;
385 u16 dl_diag_len2;
386 u16 dl_app1_len;
387 u16 dl_app2_len;
388 u16 dl_ctrl_len;
389 u8 pad2[16];
390 u16 ul_start;
391 u16 ul_mdm_len2;
392 u16 ul_mdm_len1;
393 u16 ul_diag_len;
394 u16 ul_app1_len;
395 u16 ul_app2_len;
396 u16 ul_ctrl_len;
397 } __attribute__((packed)) config_table_t;
399 /* This stores all control downlink flags */
400 typedef struct {
401 unsigned DSR : 1;
402 unsigned DCD : 1;
403 unsigned RI : 1;
404 unsigned CTS : 1;
405 unsigned reserverd : 4;
406 u8 port;
407 } __attribute__ ((packed)) ctrl_dl_t;
409 /* This stores all control uplink flags */
410 typedef struct {
411 unsigned DTR : 1;
412 unsigned RTS : 1;
413 unsigned reserved : 6;
414 u8 port;
415 } __attribute__ ((packed)) ctrl_ul_t;
416 #endif
418 /* This holds all information that is needed regarding a port */
419 typedef struct {
420 u8 update_flow_control;
421 ctrl_ul_t ctrl_ul;
422 ctrl_dl_t ctrl_dl;
423 struct kfifo *fifo_ul;
424 u32 dl_addr[2];
425 u32 dl_size[2];
426 u8 toggle_dl;
427 u32 ul_addr[2];
428 u32 ul_size[2];
429 u8 toggle_ul;
430 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;
439 } port_t;
441 /* Private data one for each card in the system */
442 typedef struct {
443 u32 base_addr;
444 u8 closing;
445 /* Register addresses */
446 u32 REG_IIR;
447 u32 REG_FCR;
448 u32 REG_IER;
449 volatile u16 ier_last_written;
450 card_type_t card_type;
451 config_table_t config_table; /* Configuration table */
452 struct pci_dev *pdev;
453 port_t port[NOZOMI_MAX_PORTS];
454 u8 *send_buf;
455 struct tty_driver tty_driver;
457 #ifdef KERNEL_2_4
458 struct tty_struct *tty_table[NTTY_TTY_MINORS];
459 struct tq_struct tty_flip_queue;
460 s32 tty_refcount;
461 #endif
462 #ifdef KERNEL_2_6
463 struct workqueue_struct *tty_flip_wq;
464 struct work_struct tty_flip_wq_struct;
465 #endif
467 struct termios *tty_termios[NTTY_TTY_MINORS];
468 struct termios *tty_termios_locked[NTTY_TTY_MINORS];
469 spinlock_t spin_mutex;
471 u32 open_ttys;
472 struct proc_dir_entry *proc_entry;
473 } dc_t;
475 /* This is a data packet that is read or written to/from card */
476 typedef struct {
477 u32 size; /* size is the length of the data buffer */
478 u8 *data;
479 } __attribute__ ((packed)) buf_t;
481 /* Function declarations */
482 static int ntty_tty_init(void);
484 static void tty_flip_queue_function(void *nouse);
486 /* Global variables */
487 static struct pci_device_id nozomi_pci_tbl[] __devinitdata = {
488 {VENDOR1, DEVICE1}, // Vodafone mobile connect HSDPA/UMTS/GPRS card
489 {0x11ab, DEVICE1}, // swisscom 5 in 1
490 {0, }
493 /* Used to store interrupt variables */
494 typedef struct {
495 volatile u16 read_iir; /* Holds current interrupt tokens */
496 } irq_t;
498 MODULE_DEVICE_TABLE(pci, nozomi_pci_tbl);
500 /* Representing the pci device of interest */
501 static int cards_found;
502 dc_t *dc = NULL;
503 irq_t my_irq;
505 /* TODO: */
506 /* -Optimize */
507 /* -Rewrite cleaner */
508 static void read_mem32(u32 *buf, u32 mem_addr_start, u32 size_bytes)
510 u32 i = 0;
511 u32 *ptr = (u32*) mem_addr_start;
512 u16 *buf16;
514 /* 2 bytes */
515 if ( size_bytes == 2 ) {
516 buf16 = (u16*) buf;
517 *buf16 = readw( ptr );
518 return;
521 while ( i < size_bytes ) {
522 if ( size_bytes - i == 2) {
523 /* Handle 2 bytes in the end */
524 buf16 = (u16*) buf;
525 *(buf16) = readw( ptr );
526 i+=2;
527 } else {
528 /* Read 4 bytes */
529 *(buf) = readl( ptr );
530 i+=4;
532 buf++; ptr++;
536 /* TODO: */
537 /* - Rewrite cleaner */
538 /* - merge with read_mem32() */
539 static void read_mem32_buf(u32 *buf, u32 mem_addr_start, u32 size_bytes)
541 #ifdef __ARMEB__
542 u32 i = 0;
543 u32 *ptr = (u32*) mem_addr_start;
544 u16 *buf16;
546 /* 2 bytes */
547 if ( size_bytes == 2 ) {
548 buf16 = (u16*) buf;
549 *buf16 = __le16_to_cpu( readw( ptr ));
550 return;
553 while ( i < size_bytes ) {
554 if ( size_bytes - i == 2) {
555 /* Handle 2 bytes in the end */
556 buf16 = (u16*) buf;
557 *(buf16) = __le16_to_cpu( readw( ptr ));
558 i+=2;
559 } else {
560 /* Read 4 bytes */
561 *(buf) = __le32_to_cpu( readl( ptr ));
562 i+=4;
564 buf++; ptr++;
566 #else
567 read_mem32(buf, mem_addr_start, size_bytes);
568 #endif
571 /* TODO: */
572 /* -Optimize */
573 /* -Rewrite cleaner */
574 static u32 write_mem32(u32 mem_addr_start, u32 *buf, u32 size_bytes)
576 u32 i = 0;
577 u32 *ptr = (u32*) mem_addr_start;
578 u16 *buf16;
580 /* 2 bytes */
581 if (size_bytes == 2) {
582 buf16 = (u16*) buf;
583 writew( *buf16, ptr);
584 return 2;
587 while (i < size_bytes) {
588 if ( size_bytes - i == 2) {
589 /* 2 bytes */
590 buf16 = (u16*) buf;
591 writew( *buf16, ptr);
592 i+=2;
593 } else {
594 /* 4 bytes */
595 writel( *buf, ptr );
596 i += 4;
598 buf++; ptr++;
600 return size_bytes;
603 /* Todo: */
604 /* - Merge with write_mem32() */
605 static u32 write_mem32_buf(u32 mem_addr_start, u32 *buf, u32 size_bytes)
607 #ifdef __ARMEB__
608 u32 i = 0;
609 u32 *ptr = (u32*) mem_addr_start;
610 u16 *buf16;
612 /* 2 bytes */
613 if (size_bytes == 2) {
614 buf16 = (u16*) buf;
615 writew( __le16_to_cpu(*buf16), ptr);
616 return 2;
619 while (i < size_bytes) {
620 if ( size_bytes - i == 2) {
621 /* 2 bytes */
622 buf16 = (u16*) buf;
623 writew( __le16_to_cpu(*buf16), ptr);
624 i+=2;
625 } else {
626 /* 4 bytes */
627 writel( __cpu_to_le32( *buf ), ptr );
628 i += 4;
630 buf++; ptr++;
632 return size_bytes;
633 #else
634 return write_mem32(mem_addr_start, buf, size_bytes);
635 #endif
638 /* Setup pointers to different channels and also setup buffer sizes. */
639 static void setup_memory(void)
641 /* The length reported is including the length field of 4 bytes, hence subtract with 4. */
642 u32 offset = dc->base_addr + dc->config_table.dl_start;
643 u16 buff_offset = 4;
645 /* Modem port dl configuration */
646 dc->port[PORT_MDM].dl_addr[CH_A] = offset;
647 dc->port[PORT_MDM].dl_addr[CH_B] = (offset += dc->config_table.dl_mdm_len1);
648 dc->port[PORT_MDM].dl_size[CH_A] = dc->config_table.dl_mdm_len1 - buff_offset;
649 dc->port[PORT_MDM].dl_size[CH_B] = dc->config_table.dl_mdm_len2 - buff_offset;
651 /* Diag port dl configuration */
652 dc->port[PORT_DIAG].dl_addr[CH_A] = (offset += dc->config_table.dl_mdm_len2);
653 dc->port[PORT_DIAG].dl_size[CH_A] = dc->config_table.dl_diag_len1 - buff_offset;
654 dc->port[PORT_DIAG].dl_addr[CH_B] = (offset += dc->config_table.dl_diag_len1);
655 dc->port[PORT_DIAG].dl_size[CH_B] = dc->config_table.dl_diag_len2 - buff_offset;
657 /* App1 port dl configuration */
658 dc->port[PORT_APP1].dl_addr[CH_A] = (offset += dc->config_table.dl_diag_len2);
659 dc->port[PORT_APP1].dl_size[CH_A] = dc->config_table.dl_app1_len - buff_offset;
661 /* App2 port dl configuration */
662 dc->port[PORT_APP2].dl_addr[CH_A] = (offset += dc->config_table.dl_app1_len);
663 dc->port[PORT_APP2].dl_size[CH_A] = dc->config_table.dl_app2_len - buff_offset;
665 /* Ctrl dl configuration */
666 dc->port[PORT_CTRL].dl_addr[CH_A] = (offset += dc->config_table.dl_app2_len);
667 dc->port[PORT_CTRL].dl_size[CH_A] = dc->config_table.dl_ctrl_len - buff_offset;
670 /* Modem Port ul configuration */
671 dc->port[PORT_MDM].ul_addr[CH_A] = (offset = dc->base_addr + dc->config_table.ul_start);
672 dc->port[PORT_MDM].ul_size[CH_A] = dc->config_table.ul_mdm_len1 - buff_offset;
673 dc->port[PORT_MDM].ul_addr[CH_B] = (offset += dc->config_table.ul_mdm_len1);
674 dc->port[PORT_MDM].ul_size[CH_B] = dc->config_table.ul_mdm_len2 - buff_offset;
676 /* Diag port ul configuration */
677 dc->port[PORT_DIAG].ul_addr[CH_A] = (offset += dc->config_table.ul_mdm_len2);
678 dc->port[PORT_DIAG].ul_size[CH_A] = dc->config_table.ul_diag_len - buff_offset;
680 /* App1 port ul configuration */
681 dc->port[PORT_APP1].ul_addr[CH_A] = (offset += dc->config_table.ul_diag_len);
682 dc->port[PORT_APP1].ul_size[CH_A] = dc->config_table.ul_app1_len - buff_offset;
684 /* App2 port ul configuration */
685 dc->port[PORT_APP2].ul_addr[CH_A] = (offset += dc->config_table.ul_app1_len);
686 dc->port[PORT_APP2].ul_size[CH_A] = dc->config_table.ul_app2_len - buff_offset;
688 /* Ctrl ul configuration */
689 dc->port[PORT_CTRL].ul_addr[CH_A] = (offset += dc->config_table.ul_app2_len);
690 dc->port[PORT_CTRL].ul_size[CH_A] = dc->config_table.ul_ctrl_len - buff_offset;
691 offset = dc->config_table.ul_start;
694 /* Dump config table under initalization phase */
695 #ifdef NOZOMI_DEBUG
696 static void dump_table(void)
698 D3("signature: 0x%08X", dc->config_table.signature);
699 D3("version: 0x%04X", dc->config_table.version);
700 D3("product_information: 0x%04X", dc->config_table.product_information);
701 D3("toggle enabled: %d", dc->config_table.toggle.enabled);
702 D3("toggle up_mdm: %d", dc->config_table.toggle.mdm_ul);
703 D3("toggle dl_mdm: %d", dc->config_table.toggle.mdm_dl);
704 D3("toggle dl_dbg: %d", dc->config_table.toggle.diag_dl);
706 D3("dl_start: 0x%04X", dc->config_table.dl_start);
707 D3("dl_mdm_len0: 0x%04X, %d", dc->config_table.dl_mdm_len1, dc->config_table.dl_mdm_len1);
708 D3("dl_mdm_len1: 0x%04X, %d", dc->config_table.dl_mdm_len2, dc->config_table.dl_mdm_len2);
709 D3("dl_diag_len0: 0x%04X, %d", dc->config_table.dl_diag_len1, dc->config_table.dl_diag_len1);
710 D3("dl_diag_len1: 0x%04X, %d", dc->config_table.dl_diag_len2, dc->config_table.dl_diag_len2);
711 D3("dl_app1_len: 0x%04X, %d", dc->config_table.dl_app1_len, dc->config_table.dl_app1_len);
712 D3("dl_app2_len: 0x%04X, %d", dc->config_table.dl_app2_len, dc->config_table.dl_app2_len);
713 D3("dl_ctrl_len: 0x%04X, %d", dc->config_table.dl_ctrl_len, dc->config_table.dl_ctrl_len);
714 D3("ul_start: 0x%04X, %d", dc->config_table.ul_start, dc->config_table.ul_start);
715 D3("ul_mdm_len[0]: 0x%04X, %d", dc->config_table.ul_mdm_len1, dc->config_table.ul_mdm_len1);
716 D3("ul_mdm_len[1]: 0x%04X, %d", dc->config_table.ul_mdm_len2, dc->config_table.ul_mdm_len2);
717 D3("ul_diag_len: 0x%04X, %d", dc->config_table.ul_diag_len, dc->config_table.ul_diag_len);
718 D3("ul_app1_len: 0x%04X, %d", dc->config_table.ul_app1_len, dc->config_table.ul_app1_len);
719 D3("ul_app2_len: 0x%04X, %d", dc->config_table.ul_app2_len, dc->config_table.ul_app2_len);
720 D3("ul_ctrl_len: 0x%04X, %d", dc->config_table.ul_ctrl_len, dc->config_table.ul_ctrl_len);
722 #endif
724 /* Read configuration table from card under intalization phase */
725 /* Returns 1 if ok, else 0 */
726 static int nozomi_read_config_table(void)
728 GET_MEM( &dc->config_table, dc->base_addr + 0, sizeof(config_table_t));
729 /* D1( "0x%08X == 0x%08X ", dc->config_table.signature, CONFIG_MAGIC); */
730 if ( dc->config_table.signature != CONFIG_MAGIC ) {
731 ERR("ConfigTable Bad! 0x%08X != 0x%08X", dc->config_table.signature, CONFIG_MAGIC);
732 return 0;
735 if ( (dc->config_table.version == 0) || (dc->config_table.toggle.enabled == TOGGLE_VALID) ) {
736 int i;
738 D1( "Second phase, configuring card");
739 setup_memory();
740 dc->port[PORT_MDM].toggle_ul = dc->config_table.toggle.mdm_ul;
741 dc->port[PORT_MDM].toggle_dl = dc->config_table.toggle.mdm_dl;
742 dc->port[PORT_DIAG].toggle_dl = dc->config_table.toggle.diag_dl;
743 D1( "toggle ports: MDM UL:%d MDM DL:%d, DIAG DL:%d",
744 dc->port[PORT_MDM].toggle_ul,
745 dc->port[PORT_MDM].toggle_dl,
746 dc->port[PORT_DIAG].toggle_dl);
748 #ifdef NOZOMI_DEBUG
749 dump_table();
750 #endif
751 for (i=PORT_MDM; i< MAX_PORT;i++) {
752 dc->port[i].fifo_ul = kfifo_alloc( FIFO_BUFFER_SIZE_UL, GFP_ATOMIC , NULL);
753 memset( &dc->port[i].ctrl_dl, 0, sizeof (ctrl_dl_t));
754 memset( &dc->port[i].ctrl_ul, 0, sizeof (ctrl_ul_t));
757 /* Enable control channel */
758 SET_IER( CTRL_DL, CTRL_DL );
760 INFO("Initialization OK!");
761 return 1;
764 if( (dc->config_table.version > 0) && (dc->config_table.toggle.enabled != TOGGLE_VALID ) ) {
765 u32 offset = 0;
766 D1( "First phase: pushing upload buffers, clearing download");
767 INFO("Version of card: %d", dc->config_table.version);
769 /* Here we should disable all I/O over F32. */
770 setup_memory();
772 /* We should send ALL channel pair tokens back along with reset token */
774 /* push upload modem buffers */
775 SET_MEM( dc->port[PORT_MDM].ul_addr[CH_A], &offset, 4);
776 SET_MEM( dc->port[PORT_MDM].ul_addr[CH_B], &offset, 4);
778 SET_FCR( MDM_UL | DIAG_DL | MDM_DL );
780 D1( "First phase done");
783 return 1;
786 /* Enable uplink interrupts */
787 static void enable_transmit_ul(port_type_t port)
789 switch( port ) {
790 case PORT_MDM: SET_IER( MDM_UL , MDM_UL ); break;
791 case PORT_DIAG: SET_IER( DIAG_UL, DIAG_UL ); break;
792 case PORT_APP1: SET_IER( APP1_UL, APP1_UL ); break;
793 case PORT_APP2: SET_IER( APP2_UL, APP2_UL ); break;
794 case PORT_CTRL: SET_IER( CTRL_UL, CTRL_UL ); break;
795 default:
796 ERR("Called with wrong port?");
797 break;
801 /* Disable uplink interrupts */
802 static void disable_transmit_ul(port_type_t port)
804 switch( port ) {
805 case PORT_MDM: SET_IER( 0 ,MDM_UL ); break;
806 case PORT_DIAG: SET_IER( 0, DIAG_UL ); break;
807 case PORT_APP1: SET_IER( 0, APP1_UL ); break;
808 case PORT_APP2: SET_IER( 0, APP2_UL ); break;
809 case PORT_CTRL: SET_IER( 0, CTRL_UL ); break;
810 default:
811 ERR("Called with wrong port?");
812 break;
816 /* Enable downlink interrupts */
817 static void enable_transmit_dl(port_type_t port)
819 switch( port ) {
820 case PORT_MDM: SET_IER( MDM_DL , MDM_DL ); break;
821 case PORT_DIAG: SET_IER( DIAG_DL, DIAG_DL ); break;
822 case PORT_APP1: SET_IER( APP1_DL, APP1_DL ); break;
823 case PORT_APP2: SET_IER( APP2_DL, APP2_DL ); break;
824 case PORT_CTRL: SET_IER( CTRL_DL, CTRL_DL ); break;
825 default:
826 ERR("Called with wrong port?");
827 break;
831 /* Disable downlink interrupts */
832 static void disable_transmit_dl(port_type_t port)
834 switch( port ) {
835 case PORT_MDM: SET_IER( 0 ,MDM_DL ); break;
836 case PORT_DIAG: SET_IER( 0, DIAG_DL ); break;
837 case PORT_APP1: SET_IER( 0, APP1_DL ); break;
838 case PORT_APP2: SET_IER( 0, APP2_DL ); break;
839 case PORT_CTRL: SET_IER( 0, CTRL_DL ); break;
840 default:
841 ERR("Called with wrong port?");
842 break;
846 /* Return 1 - send buffer to card and ack. */
847 /* Return 0 - don't ack, don't send buffer to card. */
848 int send_data(port_type_t index)
850 u32 size = 0;
851 port_t *port = &dc->port[index];
852 u8 toggle = port->toggle_ul;
853 u32 addr = port->ul_addr[toggle];
854 u32 ul_size = port->ul_size[toggle];
855 struct tty_struct *tty = port->tty;
857 if (index >= NTTY_TTY_MINORS) {
858 ERR("Called with wrong index?");
859 return 0;
862 /* Get data from tty and place in buf for now */
863 size = __kfifo_get( port->fifo_ul,dc->send_buf, ul_size < SEND_BUF_MAX ? ul_size : SEND_BUF_MAX );
865 if (size == 0) {
866 D4("No more data to send, disable link:");
867 return 0;
870 port->tx_data += size;
872 /* DUMP(buf, size); */
874 /* Write length + data */
875 SET_MEM( addr, &size, 4 );
876 SET_MEM_BUF( addr + 4, dc->send_buf, size);
878 if (port->tty) {
879 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) && tty->ldisc.write_wakeup) {
880 tty->ldisc.write_wakeup(tty);
882 wake_up_interruptible(&tty->write_wait);
885 return 1;
888 /* If all data has been read, return 1, else 0 */
889 static int receive_data(port_type_t index)
891 u8 buf[RECEIVE_BUF_MAX];
892 int size;
893 u32 offset=4;
894 port_t *port=&dc->port[index];
895 u8 toggle=port->toggle_dl;
896 u32 addr=port->dl_addr[toggle];
897 int i;
898 struct tty_struct *tty=port->tty;
900 if ( !tty ) {
901 D1("tty not open for port: %d?", index);
902 return 1;
905 if (test_bit(TTY_DONT_FLIP, &tty->flags)) {
906 D6("TTY_DONT_FLIP set!! %d", index);
907 /* Here we disable interrupt for that port and schedule */
908 /* task. Task wakes up a little bit later and enables interrupt.. */
909 port->tty_dont_flip = 1;
910 disable_transmit_dl(index);
911 #ifdef KERNEL_2_4
912 schedule_task(&dc->tty_flip_queue);
913 #endif
914 #ifdef KERNEL_2_6
915 if (!queue_work(dc->tty_flip_wq, &dc->tty_flip_wq_struct)) {
916 ERR("Call to queue_work() failed.");
918 #endif
919 return 0;
922 GET_MEM( &size, addr, 4 );
923 /* D1( "%d bytes port: %d", size, index); */
925 if ( test_bit( TTY_THROTTLED, & tty->flags) ) {
926 D1("No room in tty, don't read data, don't ack interrupt, disable interrupt");
927 /* disable interrupt in downlink... */
928 disable_transmit_dl(index);
929 return 0;
932 if (size == 0) {
933 ERR("size == 0?");
934 return 1;
937 while( size > 0 ) {
938 GET_MEM_BUF(buf, addr + offset, 4);
939 i = 0;
940 while (i < 4 && size > 0) {
941 #ifdef KERNEL_2_6_14
942 if (tty_buffer_request_room(tty, 1) < 1)
943 #else
944 if (tty->flip.count >= TTY_FLIPBUF_SIZE)
945 #endif
947 tty_flip_buffer_push(tty);
949 tty_insert_flip_char(tty, buf[i], TTY_NORMAL);
950 port->rx_data++;
951 i++;
952 size--;
955 offset += 4;
958 tty_flip_buffer_push(tty);
959 return 1;
962 /* Debug for interrupts */
963 #ifdef NOZOMI_DEBUG
964 static char* interrupt2str( u16 interrupt)
966 static char buf[TMP_BUF_MAX];
967 char *p = buf;
969 interrupt & MDM_DL1 ? p += snprintf(p, TMP_BUF_MAX, "MDM_DL1 "):0;
970 interrupt & MDM_DL2 ? p += snprintf(p, TMP_BUF_MAX, "MDM_DL2 "):0;
972 interrupt & MDM_UL1 ? p += snprintf(p, TMP_BUF_MAX, "MDM_UL1 "):0;
973 interrupt & MDM_UL2 ? p += snprintf(p, TMP_BUF_MAX, "MDM_UL2 "):0;
975 interrupt & DIAG_DL1 ? p += snprintf(p, TMP_BUF_MAX, "DIAG_DL1 "):0;
976 interrupt & DIAG_DL2 ? p += snprintf(p, TMP_BUF_MAX, "DIAG_DL2 "):0;
978 interrupt & DIAG_UL ? p += snprintf(p, TMP_BUF_MAX, "DIAG_UL "):0;
980 interrupt & APP1_DL ? p += snprintf(p, TMP_BUF_MAX, "APP1_DL "):0;
981 interrupt & APP2_DL ? p += snprintf(p, TMP_BUF_MAX, "APP2_DL "):0;
983 interrupt & APP1_UL ? p += snprintf(p, TMP_BUF_MAX, "APP1_UL "):0;
984 interrupt & APP2_UL ? p += snprintf(p, TMP_BUF_MAX, "APP2_UL "):0;
986 interrupt & CTRL_DL ? p += snprintf(p, TMP_BUF_MAX, "CTRL_DL "):0;
987 interrupt & CTRL_UL ? p += snprintf(p, TMP_BUF_MAX, "CTRL_UL "):0;
989 interrupt & RESET ? p += snprintf(p, TMP_BUF_MAX, "RESET "):0;
991 return buf;
993 #endif
995 /* Receive flow control */
996 /* Return 1 - If ok, else 0 */
997 static int receive_flow_control(irq_t *m)
999 port_type_t port = PORT_MDM;
1000 ctrl_dl_t ctrl_dl;
1001 ctrl_dl_t old_ctrl;
1002 u16 enable_ier = 0;
1004 GET_MEM( &ctrl_dl, dc->port[PORT_CTRL].dl_addr[CH_A], 2);
1006 switch( ctrl_dl.port ) {
1007 case CTRL_CMD:
1008 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).");
1009 break;
1010 case CTRL_MDM: port = PORT_MDM; enable_ier = MDM_DL; break;
1011 case CTRL_DIAG: port = PORT_DIAG; enable_ier = DIAG_DL; break;
1012 case CTRL_APP1: port = PORT_APP1; enable_ier = APP1_DL; break;
1013 case CTRL_APP2: port = PORT_APP2; enable_ier = APP2_DL; break;
1014 default:
1015 ERR("ERROR: flow control received for non-existing port");
1016 return 0;
1019 D1( "0x%04X->0x%04X", *((u16*) &dc->port[port].ctrl_dl), *((u16*)&ctrl_dl));
1021 old_ctrl = dc->port[port].ctrl_dl;
1022 dc->port[port].ctrl_dl = ctrl_dl;
1024 if ( old_ctrl.CTS == 1 && ctrl_dl.CTS == 0 ) {
1025 D1( "Disable interrupt (0x%04X) on port: %d", enable_ier, port);
1026 disable_transmit_ul(port);
1027 } else if ( old_ctrl.CTS == 0 && ctrl_dl.CTS == 1 ) {
1028 if ( __kfifo_len(dc->port[port].fifo_ul) ) {
1029 D1( "Enable interrupt (0x%04X) on port: %d", enable_ier, port);
1030 D1( "Data in buffer [%d], enable transmit! ", __kfifo_len(dc->port[port].fifo_ul) );
1031 enable_transmit_ul(port);
1032 } else {
1033 D1( "No data in buffer...");
1037 if(*(u16*)&old_ctrl == *(u16*)&ctrl_dl) {
1038 D1( " No change in mctrl");
1039 return 1;
1041 /* Update statistics */
1042 if(old_ctrl.CTS != ctrl_dl.CTS) {
1043 dc->port[port].tty_icount.cts++;
1045 if(old_ctrl.DSR != ctrl_dl.DSR) {
1046 dc->port[port].tty_icount.dsr++;
1048 if(old_ctrl.RI != ctrl_dl.RI) {
1049 dc->port[port].tty_icount.rng++;
1051 if(old_ctrl.DCD != ctrl_dl.DCD) {
1052 dc->port[port].tty_icount.dcd++;
1054 D1("port: %d DCD(%d), CTS(%d), RI(%d), DSR(%d)", port,
1055 dc->port[port].tty_icount.dcd, dc->port[port].tty_icount.cts,
1056 dc->port[port].tty_icount.rng, dc->port[port].tty_icount.dsr);
1058 return 1;
1061 /* TODO: */
1062 /* - return ctrl_port_t */
1063 static u8 port2ctrl(port_type_t port)
1065 switch( port ) {
1066 case PORT_MDM:
1067 return CTRL_MDM;
1068 case PORT_DIAG:
1069 return CTRL_DIAG;
1070 case PORT_APP1:
1071 return CTRL_APP1;
1072 case PORT_APP2:
1073 return CTRL_APP2;
1074 default:
1075 ERR("ERROR: send flow control received for non-existing port");
1076 return -1;
1078 return -1;
1081 /* Send flow control, can only update one channel at a time */
1082 /* Return 0 - If we have updated all flow control */
1083 /* Return 1 - If we need to update more flow control, ack current enable more */
1084 static int send_flow_control(void)
1086 u32 i, more_flow_control_to_be_updated = 0;
1087 u16 *ctrl;
1089 for( i=PORT_MDM; i<MAX_PORT ; i++ ) {
1090 if( dc->port[i].update_flow_control ) {
1091 if ( more_flow_control_to_be_updated ) {
1092 /* We have more flow control to be updated */
1093 return 1;
1095 dc->port[i].ctrl_ul.port = port2ctrl(i);
1096 ctrl = (u16*) &dc->port[i].ctrl_ul;
1097 /* D1( "sending flow control 0x%04X for port %d, %d", (u16) *ctrl, i, dc->port[i].ctrl_ul.port ); */
1098 SET_MEM( dc->port[PORT_CTRL].ul_addr[0], (u32*) ctrl, 2 );
1099 dc->port[i].update_flow_control = 0;
1100 more_flow_control_to_be_updated = 1;
1103 return 0;
1106 /* Handle donlink data, ports that are handled are modem and diagnostics */
1107 /* Return 1 - ok */
1108 /* Return 0 - toggle fields are out of sync */
1109 static int handle_data_dl(irq_t *m, port_type_t port, u8 *toggle, u16 mask1, u16 mask2)
1111 if ( *toggle == 0 && m->read_iir & mask1 ) {
1112 if (receive_data(port)) {
1113 SET_FCR( mask1 );
1114 *toggle = !(*toggle);
1117 if ( m->read_iir & mask2 ) {
1118 if (receive_data(port)) {
1119 SET_FCR( mask2 );
1120 *toggle = !(*toggle);
1123 } else if ( *toggle == 1 && m->read_iir & mask2 ) {
1124 if (receive_data(port)) {
1125 SET_FCR( mask2 );
1126 *toggle = !(*toggle);
1129 if ( m->read_iir & mask1 ) {
1130 if (receive_data(port)) {
1131 SET_FCR( mask1 );
1132 *toggle = !(*toggle);
1135 } else {
1136 ERR("port out of sync!, toggle:%d", *toggle);
1137 return 0;
1139 return 1;
1142 /* Handle uplink data, this is currently for the modem port */
1143 /* Return 1 - ok */
1144 /* Return 0 - toggle field are out of sync */
1145 static int handle_data_ul(irq_t *m, port_type_t port)
1147 u8 *toggle = &(dc->port[port].toggle_ul);
1149 if ( *toggle==0 && m->read_iir & MDM_UL1 ) {
1150 SET_IER( 0, MDM_UL );
1151 if (send_data(port)) {
1152 SET_FCR( MDM_UL1 );
1153 SET_IER( MDM_UL, MDM_UL);
1154 *toggle = !*toggle;
1157 if ( m->read_iir & MDM_UL2 ) {
1158 SET_IER( 0, MDM_UL );
1159 if (send_data(port)) {
1160 SET_FCR( MDM_UL2 );
1161 SET_IER( MDM_UL, MDM_UL);
1162 *toggle = !*toggle;
1166 } else if ( *toggle==1 && m->read_iir & MDM_UL2 ) {
1167 SET_IER( 0, MDM_UL );
1168 if (send_data(port)) {
1169 SET_FCR( MDM_UL2 );
1170 SET_IER( MDM_UL, MDM_UL);
1171 *toggle = !*toggle;
1174 if ( m->read_iir & MDM_UL1 ) {
1175 SET_IER( 0, MDM_UL );
1176 if (send_data(port)) {
1177 SET_FCR( MDM_UL1 );
1178 SET_IER( MDM_UL, MDM_UL);
1179 *toggle = !*toggle;
1182 } else {
1183 SET_FCR( m->read_iir & MDM_UL );
1184 ERR("port out of sync!");
1185 return 0;
1187 return 1;
1190 static irqreturn_t interrupt_handler(int irq, void *dev_id, struct pt_regs *regs)
1192 irq_t *m = &my_irq;
1194 if (dc && dc->pdev != dev_id) {
1195 return IRQ_NONE;
1198 GET_IIR( m->read_iir );
1200 #if 1 // add by Victor Yu. 05-24-2006
1201 spin_lock(&dc->spin_mutex);
1202 #endif
1203 /* Just handle interrupt enabled in IER (by masking with dc->ier_last_written) */
1204 m->read_iir &= dc->ier_last_written;
1206 if (m->read_iir == 0) {
1207 #if 1 // add by Victor Yu. 05-24-2006
1208 spin_unlock(&dc->spin_mutex);
1209 #endif
1210 return IRQ_NONE;
1213 D4( "%s irq:0x%04X, prev:0x%04X", interrupt2str(m->read_iir), m->read_iir, dc->ier_last_written);
1215 if ( m->read_iir & RESET) {
1216 if( !nozomi_read_config_table() ) {
1217 SET_IER( 0, 0xFFFF);
1218 ERR("ERR: Could not read status from card, we should disable interface");
1219 } else {
1220 SET_FCR( RESET );
1222 goto exit_handler; /* No more useful info if this was the reset interrupt. */
1225 if ( m->read_iir & CTRL_UL ) {
1226 D1( "CTRL_UL");
1227 SET_IER( 0, CTRL_UL );
1228 if ( send_flow_control() ) {
1229 SET_FCR( CTRL_UL );
1230 SET_IER( CTRL_UL, CTRL_UL );
1233 if ( m->read_iir & CTRL_DL ) {
1234 receive_flow_control(m);
1235 SET_FCR( CTRL_DL );
1237 if ( m->read_iir & MDM_DL ) {
1238 if ( !(handle_data_dl(m, PORT_MDM, &(dc->port[PORT_MDM].toggle_dl), MDM_DL1, MDM_DL2)) ) {
1239 ERR("MDM_DL out of sync!");
1240 goto exit_handler;
1243 if ( m->read_iir & MDM_UL ) {
1244 if ( !handle_data_ul(m, PORT_MDM ) ) {
1245 ERR("MDM_UL out of sync!");
1246 goto exit_handler;
1249 if ( m->read_iir & DIAG_DL ) {
1250 if ( !(handle_data_dl(m, PORT_DIAG, &(dc->port[PORT_DIAG].toggle_dl), DIAG_DL1, DIAG_DL2)) ) {
1251 ERR("DIAG_DL out of sync!");
1252 goto exit_handler;
1255 if ( m->read_iir & DIAG_UL ) {
1256 SET_IER( 0, DIAG_UL );
1257 if( send_data(PORT_DIAG) ) {
1258 SET_FCR( DIAG_UL );
1259 SET_IER( DIAG_UL, DIAG_UL );
1262 if ( m->read_iir & APP1_DL ) {
1263 if (receive_data(PORT_APP1) ) {
1264 SET_FCR( APP1_DL );
1267 if ( m->read_iir & APP1_UL ) {
1268 SET_IER( 0, APP1_UL );
1269 if(send_data(PORT_APP1)) {
1270 SET_FCR( APP1_UL );
1271 SET_IER( APP1_UL, APP1_UL );
1274 if ( m->read_iir & APP2_DL ) {
1275 if (receive_data(PORT_APP2)) {
1276 SET_FCR( APP2_DL );
1279 if ( m->read_iir & APP2_UL ) {
1280 SET_IER( 0, APP2_UL );
1281 if(send_data(PORT_APP2)) {
1282 SET_FCR( APP2_UL );
1283 SET_IER( APP2_UL, APP2_UL );
1287 exit_handler:
1288 spin_unlock(&dc->spin_mutex);
1289 return IRQ_HANDLED;
1292 /* Request a shared IRQ from system */
1293 static int nozomi_setup_interrupt(struct pci_dev *pdev)
1295 int rval;
1297 if ( (rval = request_irq( pdev->irq, &interrupt_handler, SA_SHIRQ, NOZOMI_NAME, pdev )) ) {
1298 ERR("Cannot open because IRQ %d is already in use.", pdev->irq );
1301 return rval;
1304 static void nozomi_get_card_type(void)
1306 u32 i, size=0;
1308 for( i=0; i<6; i++ ) {
1309 size += pci_resource_len(dc->pdev, i);
1312 /* Assume card type F32_8 if no match */
1313 dc->card_type = size == 2048 ? F32_2 : F32_8;
1315 INFO("Card type is: %d", dc->card_type );
1318 void nozomi_setup_private_data(void)
1320 u32 offset = dc->base_addr + dc->card_type/2;
1321 int i;
1323 dc->REG_FCR = offset + R_FCR;
1324 dc->REG_IIR = offset + R_IIR;
1325 dc->REG_IER = offset + R_IER;
1326 dc->ier_last_written = 0;
1327 dc->closing = 0;
1329 dc->port[PORT_MDM ].token_dl = MDM_DL;
1330 dc->port[PORT_DIAG].token_dl = DIAG_DL;
1331 dc->port[PORT_APP1].token_dl = APP1_DL;
1332 dc->port[PORT_APP2].token_dl = APP2_DL;
1334 for( i=PORT_MDM; i<MAX_PORT; i++ ) {
1335 dc->port[i].rx_data = dc->port[i].tx_data = 0;
1336 dc->port[i].tty_dont_flip = 0;
1341 static void tty_flip_queue_function(void *nouse)
1343 int i;
1344 #if 1 // add by Victor Yu. 05-24-2006
1345 unsigned long flags;
1346 #endif
1348 /* Enable interrupt for that port */
1349 for(i=0;i<MAX_PORT;i++) {
1350 if (dc->port[i].tty_dont_flip) {
1351 D6("Enable for port: %d", i);
1352 dc->port[i].tty_dont_flip = 0;
1353 #if 1 // add by Victor Yu. 05-24-2006
1354 spin_lock_irqsave(&dc->spin_mutex, flags);
1355 #endif
1356 enable_transmit_dl(dc->port[i].tty_index);
1357 #if 1 // add by Victor Yu. 05-24-2006
1358 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1359 #endif
1364 static int read_proc_card_type(char *buf, char **start, off_t offset, int len)
1366 len = 0;
1367 len += sprintf(buf+len,"%d\n", dc->card_type);
1368 return len;
1371 static int read_proc_open_ttys(char *buf, char **start, off_t offset, int len)
1373 len = 0;
1374 len += sprintf(buf+len,"%d\n", dc->open_ttys);
1375 return len;
1378 static int read_proc_version(char *buf, char **start, off_t offset, int len)
1380 len = 0;
1381 len += sprintf(buf+len, "%s\n", VERSION_STRING);
1382 return len;
1385 static int read_proc_rtx(char *buf, char **start, off_t offset, int len)
1387 int i;
1389 len = 0;
1390 for(i=PORT_MDM;i<MAX_PORT;i++) {
1391 len += sprintf(buf+len,"noz%d rx: %d, tx: %d\n", i, dc->port[i].rx_data, dc->port[i].tx_data);
1393 return len;
1396 static void make_proc_dirs(void)
1398 dc->proc_entry = proc_mkdir("nozomi", &proc_root);
1399 /* Register the read_proc */
1400 if ( !create_proc_info_entry("card_type",0,dc->proc_entry,read_proc_card_type) ) {
1401 ERR("ERROR: failed to register read_procmem");
1403 if ( !create_proc_info_entry("open_ttys",0,dc->proc_entry,read_proc_open_ttys) ) {
1404 ERR("ERROR: failed to register read_procmem");
1406 if ( !create_proc_info_entry("rtx",0,dc->proc_entry,read_proc_rtx) ) {
1407 ERR("ERROR: failed to register read_procmem");
1409 if ( !create_proc_info_entry("version",0,dc->proc_entry,read_proc_version) ) {
1410 ERR("ERROR: failed to register read_procmem");
1414 static void remove_proc_dirs(void)
1416 remove_proc_entry("card_type", dc->proc_entry);
1417 remove_proc_entry("open_ttys", dc->proc_entry);
1418 remove_proc_entry("rtx", dc->proc_entry);
1419 remove_proc_entry("version", dc->proc_entry);
1420 remove_proc_entry("nozomi", &proc_root);
1423 /* Allocate memory for one device */
1424 static int __devinit nozomi_card_init(struct pci_dev *pdev, const struct pci_device_id *ent)
1426 int ret = -EIO;
1428 cards_found++;
1429 INFO("Init, cards_found: %d", cards_found);
1431 if (!(dc = kmalloc(sizeof(dc_t), GFP_KERNEL))) {
1432 D1( "Could not allocate memory");
1433 return -EIO;
1436 memset(dc, 0, sizeof( dc_t ));
1438 if (cards_found > 1) {
1439 ERR("This driver only supports 1 device");
1440 return -ENODEV;
1443 dc->pdev = pdev;
1445 /* Find out what card type it is */
1446 nozomi_get_card_type();
1448 if (pci_enable_device(dc->pdev)) {
1449 ERR("Not possible to enable PCI Device");
1450 return -ENODEV;
1453 if ( (dc->base_addr = pci_resource_start(dc->pdev, 0)) == 0x0000) {
1454 ERR("No I/O-Address for card detected");
1455 ret = -ENODEV;
1456 goto err_disable_device;
1459 if (!(dc->base_addr = (u32) ioremap(dc->base_addr, dc->card_type))) {
1460 ERR("No I/O-Address for card detected");
1461 ret = -ENODEV;
1462 goto err_disable_device;
1465 dc->open_ttys=0;
1467 nozomi_setup_private_data();
1469 if (pci_request_regions(dc->pdev, NOZOMI_NAME)) {
1470 ERR("I/O address 0x%04x already in use",
1471 (int) /* nozomi_private.io_addr */ 0);
1472 ret = -EIO;
1473 goto err_disable_regions;
1476 if ( !(dc->send_buf = kmalloc(SEND_BUF_MAX, GFP_KERNEL))) {
1477 ERR("Could not allocate send buffer?");
1478 goto err_disable_regions;
1481 /* Disable all interrupts */
1482 SET_IER( 0 , 0xFFFF );
1484 /* Setup interrupt handler */
1485 if (nozomi_setup_interrupt(dc->pdev)) {
1486 ret = -EIO;
1487 goto err_disable_regions;
1490 D1( "base_addr: 0x%08X", dc->base_addr);
1492 #ifdef KERNEL_2_4
1493 dc->tty_flip_queue.list.next = NULL;
1494 dc->tty_flip_queue.list.prev = NULL;
1495 dc->tty_flip_queue.sync = 0;
1496 dc->tty_flip_queue.data = dc;
1497 dc->tty_flip_queue.routine = tty_flip_queue_function;
1498 #endif
1499 #ifdef KERNEL_2_6
1500 if ( !(dc->tty_flip_wq = create_singlethread_workqueue(NOZOMI_NAME )) ) {
1501 ERR("Could not create workqueue?");
1502 BUG_ON(!dc->tty_flip_wq);
1503 return -ENOMEM;
1505 INIT_WORK( &dc->tty_flip_wq_struct, tty_flip_queue_function, NULL);
1506 #endif
1508 spin_lock_init(&dc->spin_mutex);
1509 make_proc_dirs();
1510 ntty_tty_init();
1512 /* Enable RESET interrupt. */
1513 SET_IER( RESET, 0xFFFF );
1514 return 0;
1516 err_disable_regions:
1517 pci_release_regions(pdev);
1518 iounmap((void *)dc->base_addr );
1519 dc->base_addr = 0;
1521 err_disable_device:
1522 pci_disable_device(pdev);
1523 if( dc ) {
1524 kfree(dc);
1525 dc = NULL;
1527 return ret;
1530 static void tty_do_close(port_t *port)
1532 #if 1 // add by Victor Yu. 05-24-2006
1533 unsigned long flags;
1534 #endif
1535 #if 0 // Add by Jared Wu. 11-15-2006. Now it can be waked up by signal.
1536 down(&port->tty_sem);
1537 #esle
1538 if(down_interruptible(&port->tty_sem)) {
1539 return ;
1541 #endif
1543 if ( !port->tty_open_count ) {
1544 goto exit;
1547 dc->open_ttys--;
1548 port->tty_open_count--;
1549 #ifdef KERNEL_2_4
1550 MOD_DEC_USE_COUNT;
1551 #endif
1553 if ( port->tty_open_count == 0) {
1554 D1("close: %d", port->token_dl );
1555 #if 1 // add by Victor Yu. 05-24-2006
1556 spin_lock_irqsave(&dc->spin_mutex, flags);
1557 #endif
1558 SET_IER( 0, port->token_dl );
1559 #if 1 // add by Victor Yu. 05-24-2006
1560 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1561 #endif
1564 exit:
1565 up(&port->tty_sem);
1567 #if 0 // Add by Jared 11-15-2006. Compiling error.
1568 static void __exit tty_exit(void) {
1569 #else
1570 static void __devexit tty_exit(void) {
1571 #endif
1572 int i;
1574 D1( " ");
1575 #ifdef KERNEL_2_6
1576 for (i = 0; i < NTTY_TTY_MINORS; ++i)
1577 tty_unregister_device(&dc->tty_driver, i);
1578 #endif
1580 tty_unregister_driver(&dc->tty_driver);
1582 for (i = 0; i < NTTY_TTY_MINORS; i++) {
1583 while (dc->port[i].tty_open_count) {
1584 tty_do_close(&dc->port[i]);
1586 dc->port[i].tty = NULL;
1590 /* Deallocate memory for one device */
1591 static void __devexit nozomi_card_exit(struct pci_dev *pdev)
1593 int i;
1594 //ctrl_ul_t ctrl;
1595 unsigned long flags;
1597 #if 1 // Add by Jared 10-12-2006. Kill the pppd process in user space
1598 char *argv[2], **envp, *scratch;
1600 if ( !(envp=(char **)kmalloc(10*sizeof(char *), GFP_KERNEL)) )
1601 return;
1602 argv[0] = "/etc/init.d/umts.sh";
1603 argv[1] = "stop";
1604 argv[2] = 0;
1605 envp[i++] = "HOME=/";
1606 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
1607 envp[i++] = 0;
1608 call_usermodehelper(argv[0], argv, envp,0);
1609 kfree(envp);
1610 #endif
1613 free_irq( pdev->irq, pdev );
1614 spin_lock_irqsave(&dc->spin_mutex, flags);
1615 /* Disable all interrupts */
1616 SET_IER( 0, 0xFFFF);
1617 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1619 #if 0 // mask by Victor Yu. 05-25-2006
1620 /* Send 0x0001, command card to resend the reset token. */
1621 /* This is to get the reset when the module is reloaded. */
1622 ctrl.port = 0x00; ctrl.reserved = 0; ctrl.RTS=0; ctrl.DTR=1;
1623 D1( "sending flow control 0x%04X", * ((u16*) &ctrl) );
1625 /* Setup dc->reg addresses to we can use defines here */
1626 nozomi_setup_private_data();
1627 SET_MEM( dc->port[PORT_CTRL].ul_addr[0], (u32*) &ctrl, 2 );
1628 SET_FCR( CTRL_UL ); /* push the token to the card. */
1629 #endif
1631 D1( "pci_release_regions");
1632 pci_release_regions(pdev);
1634 if(dc->base_addr)
1635 iounmap((void *)dc->base_addr);
1637 D1( "pci_disable_device");
1638 pci_disable_device(pdev);
1640 for (i=PORT_MDM; i< MAX_PORT;i++) {
1641 kfree ( dc->port[i].fifo_ul );
1644 kfree( dc->send_buf );
1645 tty_exit();
1647 remove_proc_dirs();
1649 #ifdef KERNEL_2_6
1650 destroy_workqueue(dc->tty_flip_wq);
1651 #endif
1653 if (dc) {
1654 kfree( dc );
1657 cards_found--;
1660 static void set_rts(int index, int rts)
1662 dc->port[index].ctrl_ul.RTS = rts;
1663 dc->port[index].update_flow_control = 1;
1664 enable_transmit_ul(PORT_CTRL);
1667 static void set_dtr(int index, int dtr)
1669 D1("SETTING DTR index: %d, dtr: %d", index, dtr);
1670 dc->port[index].ctrl_ul.DTR = dtr;
1671 dc->port[index].update_flow_control = 1;
1672 enable_transmit_ul(PORT_CTRL);
1676 /*------------------------------------------------------------------------------------------
1677 TTY code
1678 -----------------------------------------------------------------------------------------*/
1681 /* Called when the userspace process opens the tty, /dev/noz*. */
1682 static int ntty_open(struct tty_struct *tty, struct file *file)
1684 #if 1 // add by Victor Yu. 05-24-2006
1685 unsigned long flags;
1686 #endif
1687 #ifdef KERNEL_2_6
1688 s32 index = tty->index;
1689 #else
1690 s32 index = MINOR(tty->device) - tty->driver.minor_start;
1691 #endif
1692 port_t *port=&dc->port[index];
1694 #if 0 // Add by Jared, 11-15-2006. It can be waked up by signal.
1695 down(&port->tty_sem);
1696 #else
1697 if(down_interruptible(&port->tty_sem)){
1698 return -ERESTARTSYS;
1700 #endif
1701 tty->low_latency = 1;
1702 tty->driver_data = port;
1703 port->tty = tty;
1704 port->tty_index = index;
1706 port->tty_open_count++;
1707 dc->open_ttys++;
1709 #ifdef KERNEL_2_4
1710 MOD_INC_USE_COUNT;
1711 #endif
1713 /* Enable interrupt downlink for channel */
1714 if ( port->tty_open_count == 1) {
1715 port->rx_data = port->tx_data = 0;
1716 D1("open: %d", port->token_dl );
1717 #if 1 // add by Victor Yu. 05-24-2006
1718 spin_lock_irqsave(&dc->spin_mutex, flags);
1719 #endif
1720 SET_IER( port->token_dl, port->token_dl );
1721 #if 1 // add by Victor Yu. 05-24-2006
1722 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1723 #endif
1726 up(&port->tty_sem);
1728 return 0;
1731 /* Called when the userspace process close the tty, /dev/noz*. */
1732 static void ntty_close(struct tty_struct *tty, struct file *file)
1734 tty_do_close((port_t *) tty->driver_data);
1737 /* called when the userspace process writes to the tty (/dev/noz*). */
1738 /* Data is inserted into a fifo, which is then read and transfered to the modem. */
1739 #ifdef KERNEL_2_6
1740 static int ntty_write(struct tty_struct *tty, int from_user, const unsigned char *buffer, int count)
1741 #else
1742 static s32 ntty_write(struct tty_struct *tty, s32 from_user, const u8 *buffer, s32 count)
1743 #endif
1745 #if 1 // add by Victor Yu. 05-24-2006
1746 unsigned long flags;
1747 #endif
1748 int rval = -EINVAL;
1749 port_t *port = (port_t *) tty->driver_data;
1751 /* D1( "WRITEx: %d, index = %d", count, index); */
1753 if (! port) {
1754 return -ENODEV;
1757 #if 0 // Add by Jared 11-15-2006. It can be waked up by signal now.
1758 down(&port->tty_sem);
1759 #else
1760 if(down_trylock(&port->tty_sem) ) { // must test lock as tty layer wraps calls to this function with BKL
1761 ERR("Would have deadlocked - return ERESTARTSYS");
1762 return -ERESTARTSYS;
1764 #endif
1765 if (! port->tty_open_count) {
1766 D1( " ");
1767 goto exit;
1770 #ifdef KERNEL_2_4
1771 if (from_user) {
1772 rval = __kfifo_put_user(port->fifo_ul, (unsigned char *) buffer, count);
1773 } else {
1774 rval = __kfifo_put(port->fifo_ul, (unsigned char *) buffer, count);
1776 #else
1777 rval = __kfifo_put(port->fifo_ul, (unsigned char *) buffer, count);
1778 #endif
1780 /* notify card */
1781 if ( dc == NULL) {
1782 D1( "No device context?");
1783 goto exit;
1786 // CTS is only valid on the modem channel
1787 if ( port == &(dc->port[PORT_MDM]) ) {
1788 if ( port->ctrl_dl.CTS ) {
1789 D4( "Enable interrupt");
1790 #if 1 // add by Victor Yu. 05-24-2006
1791 spin_lock_irqsave(&dc->spin_mutex, flags);
1792 #endif
1793 enable_transmit_ul(port->tty_index);
1794 #if 1 // add by Victor Yu. 05-24-2006
1795 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1796 #endif
1797 } else {
1798 ERR("CTS not active on modem port?");
1800 } else {
1801 #if 1 // add by Victor Yu. 05-24-2006
1802 spin_lock_irqsave(&dc->spin_mutex, flags);
1803 #endif
1804 enable_transmit_ul(port->tty_index);
1805 #if 1 // add by Victor Yu. 05-24-2006
1806 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1807 #endif
1810 exit:
1811 up(&port->tty_sem);
1812 return rval;
1815 /* Calculate how much is left in device */
1816 /* This method is called by the upper tty layer. */
1817 /* #according to sources N_TTY.c it expects a value >= 0 and does not check for negative values. */
1818 static int ntty_write_room(struct tty_struct *tty)
1820 port_t *port = (port_t *) tty->driver_data;
1821 int room = 0;
1823 if (! port) {
1824 return 0;
1827 #if 0 // Add by Jared Wu. 11-15-2006. Now it can be waked up by signal.
1828 down(&port->tty_sem);
1829 #else
1830 if(down_trylock(&port->tty_sem)){
1831 return 0;
1833 #endif
1835 if (! port->tty_open_count) {
1836 goto exit;
1839 room = port->fifo_ul->size - __kfifo_len(port->fifo_ul);
1841 exit:
1842 up(&port->tty_sem);
1843 return room;
1846 /* Sets termios flags, called by the tty layer. */
1847 static void ntty_set_termios(struct tty_struct *tty, struct termios *old_termios)
1849 unsigned int cflag;
1851 cflag = tty->termios->c_cflag;
1852 if (old_termios) {
1853 if ((cflag == old_termios->c_cflag) &&
1854 (RELEVANT_IFLAG(tty->termios->c_iflag) == RELEVANT_IFLAG(old_termios->c_iflag))) {
1855 D1( " - nothing to change...");
1856 goto exit_termios;
1860 /* get the byte size */
1861 switch (cflag & CSIZE) {
1862 case CS5:
1863 D1( " - data bits = 5");
1864 break;
1865 case CS6:
1866 D1( " - data bits = 6");
1867 break;
1868 case CS7:
1869 D1( " - data bits = 7");
1870 break;
1871 case CS8:
1872 default:
1873 D1( " - data bits = 8");
1874 break;
1877 /* determine the parity */
1878 if (cflag & PARENB) {
1879 if (cflag & PARODD) {
1880 D1( " - parity = odd");
1881 } else {
1882 D1( " - parity = even");
1884 } else {
1885 D1( " - parity = none");
1888 /* figure out the stop bits requested */
1889 if (cflag & CSTOPB) {
1890 D1( " - stop bits = 2");
1891 } else {
1892 D1( " - stop bits = 1");
1895 /* figure out the hardware flow control settings */
1896 if (cflag & CRTSCTS) {
1897 D1( " - RTS/CTS is enabled");
1898 } else {
1899 D1( " - RTS/CTS is disabled");
1902 /* determine software flow control */
1903 /* if we are implementing XON/XOFF, set the start and
1904 * stop character in the device */
1905 if (I_IXOFF(tty) || I_IXON(tty)) {
1906 #ifdef NOZOMI_DEBUG
1907 unsigned char stop_char = STOP_CHAR(tty);
1908 unsigned char start_char = START_CHAR(tty);
1909 #endif
1910 /* if we are implementing INBOUND XON/XOFF */
1911 if (I_IXOFF(tty)) {
1912 D1( " - INBOUND XON/XOFF is enabled, "
1913 "XON = %2x, XOFF = %2x", start_char, stop_char);
1914 } else {
1915 D1( " - INBOUND XON/XOFF is disabled");
1918 /* if we are implementing OUTBOUND XON/XOFF */
1919 if (I_IXON(tty)) {
1920 D1( " - OUTBOUND XON/XOFF is enabled, "
1921 "XON = %2x, XOFF = %2x", start_char, stop_char);
1922 } else {
1923 D1( " - OUTBOUND XON/XOFF is disabled");
1927 exit_termios:
1928 return;
1931 /* Gets io control parameters */
1932 static int ntty_tiocmget(struct tty_struct *tty, struct file *file)
1934 port_t *port = (port_t *) tty->driver_data;
1935 ctrl_dl_t *ctrl_dl = &port->ctrl_dl;
1936 ctrl_ul_t *ctrl_ul = &port->ctrl_ul;
1938 return 0
1939 | (ctrl_ul->RTS ? TIOCM_RTS : 0)
1940 | (ctrl_ul->DTR ? TIOCM_DTR : 0)
1941 | (ctrl_dl->DCD ? TIOCM_CAR : 0)
1942 | (ctrl_dl->RI ? TIOCM_RNG : 0)
1943 | (ctrl_dl->DSR ? TIOCM_DSR : 0)
1944 | (ctrl_dl->CTS ? TIOCM_CTS : 0);
1947 /* Sets io controls parameters */
1948 static int ntty_tiocmset(struct tty_struct *tty, struct file *file, u32 arg)
1950 #if 1 // add by Victor Yu. 05-24-2006
1951 unsigned long flags;
1952 #endif
1953 port_t *port = (port_t *) tty->driver_data;
1955 #if 1 // add by Victor Yu. 05-24-2006
1956 spin_lock_irqsave(&dc->spin_mutex, flags);
1957 #endif
1958 set_rts(port->tty_index, (arg & TIOCM_RTS) ? 1 : 0);
1959 set_dtr(port->tty_index, (arg & TIOCM_DTR) ? 1 : 0);
1960 #if 1 // add by Victor Yu. 05-24-2006
1961 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1962 #endif
1964 return 0;
1967 static int ntty_ioctl_tiocmiwait(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
1969 port_t *port = (port_t *) tty->driver_data;
1971 if (cmd == TIOCMIWAIT) {
1972 DECLARE_WAITQUEUE(wait, current);
1973 struct async_icount cnow;
1974 struct async_icount cprev;
1976 cprev = port->tty_icount;
1977 while (1) {
1978 add_wait_queue(&port->tty_wait, &wait);
1979 set_current_state(TASK_INTERRUPTIBLE);
1980 schedule();
1981 remove_wait_queue(&port->tty_wait, &wait);
1983 /* see if a signal woke us up */
1984 if (signal_pending(current))
1985 return -ERESTARTSYS;
1987 cnow = port->tty_icount;
1988 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
1989 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1990 return -EIO; /* no change => error */
1991 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1992 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1993 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1994 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
1995 return 0;
1997 cprev = cnow;
2001 return -ENOIOCTLCMD;
2004 static int ntty_ioctl_tiocgicount(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
2006 port_t *port = (port_t *) tty->driver_data;
2008 if (cmd == TIOCGICOUNT) {
2009 struct async_icount cnow = port->tty_icount;
2010 struct serial_icounter_struct icount;
2012 icount.cts = cnow.cts;
2013 icount.dsr = cnow.dsr;
2014 icount.rng = cnow.rng;
2015 icount.dcd = cnow.dcd;
2016 icount.rx = cnow.rx;
2017 icount.tx = cnow.tx;
2018 icount.frame = cnow.frame;
2019 icount.overrun = cnow.overrun;
2020 icount.parity = cnow.parity;
2021 icount.brk = cnow.brk;
2022 icount.buf_overrun = cnow.buf_overrun;
2024 if (copy_to_user((void *)arg, &icount, sizeof(icount)))
2025 return -EFAULT;
2026 return 0;
2028 return -ENOIOCTLCMD;
2031 static int ntty_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
2033 #if 1 // add by Victor Yu. 05-24-2006
2034 unsigned long flags;
2035 #endif
2036 port_t *port = (port_t *) tty->driver_data;
2037 int mask;
2038 int rval = -ENOIOCTLCMD;
2040 D1("******** IOCTL, cmd: %d", cmd);
2041 switch (cmd) {
2042 #if 0 // mask by Victor Yu. 05-25-2006
2043 case TCGETS:
2044 D1( "IOCTL TCGETS ...");
2045 rval = -ENOIOCTLCMD;
2046 break;
2047 case TCSETS:
2048 D1( "IOCTL TCSETS ...");
2049 rval = -ENOIOCTLCMD;
2050 break;
2051 #endif
2052 case TIOCMIWAIT:
2053 rval = ntty_ioctl_tiocmiwait(tty, file, cmd, arg);
2054 break;
2055 case TIOCGICOUNT:
2056 rval = ntty_ioctl_tiocgicount(tty, file, cmd, arg);
2057 break;
2058 case TIOCMGET:
2059 rval = ntty_tiocmget(tty, file);
2060 break;
2061 case TIOCMSET:
2062 rval = ntty_tiocmset(tty, file, arg);
2063 break;
2064 case TIOCMBIC:
2065 if (get_user(mask, (unsigned long *) arg))
2066 return -EFAULT;
2068 #if 1 // add by Victor Yu. 05-24-2006
2069 spin_lock_irqsave(&dc->spin_mutex, flags);
2070 #endif
2071 if (mask & TIOCM_RTS)
2072 set_rts(port->tty_index, 0);
2073 if (mask & TIOCM_DTR)
2074 set_dtr(port->tty_index, 0);
2075 #if 1 // add by Victor Yu. 05-24-2006
2076 spin_unlock_irqrestore(&dc->spin_mutex, flags);
2077 #endif
2078 rval = 0;
2079 break;
2080 case TIOCMBIS:
2081 if (get_user(mask, (unsigned long *) arg))
2082 return -EFAULT;
2084 #if 1 // add by Victor Yu. 05-24-2006
2085 spin_lock_irqsave(&dc->spin_mutex, flags);
2086 #endif
2087 if (mask & TIOCM_RTS)
2088 set_rts(port->tty_index, 1);
2089 if (mask & TIOCM_DTR)
2090 set_dtr(port->tty_index, 1);
2091 #if 1 // add by Victor Yu. 05-24-2006
2092 spin_unlock_irqrestore(&dc->spin_mutex, flags);
2093 #endif
2094 rval = 0;
2095 break;
2096 #if 0 // mask by Victor Yu.
2097 case TCFLSH:
2098 D1( "IOCTL TCFLSH ...");
2099 rval = -ENOIOCTLCMD;
2100 break;
2102 default:
2103 D1( "ERR: 0x%08X, %d", cmd, cmd);
2104 break;
2105 #endif
2108 return rval;
2111 /* Called by the upper tty layer when tty buffers are ready */
2112 /* to receive data again after a call to throttle. */
2113 static void ntty_unthrottle(struct tty_struct *tty)
2115 port_t *port = (port_t *) tty->driver_data;
2116 unsigned long flags;
2118 D1( "UNTHROTTLE");
2119 spin_lock_irqsave(&dc->spin_mutex, flags);
2120 enable_transmit_dl(port->tty_index);
2121 set_rts(port->tty_index, 1);
2123 spin_unlock_irqrestore(&dc->spin_mutex, flags);
2126 /* Called by the upper tty layer when the tty buffers are almost full. */
2127 /* The driver should stop send more data. */
2128 static void ntty_throttle(struct tty_struct *tty)
2130 port_t *port = (port_t *) tty->driver_data;
2131 unsigned long flags;
2133 D1( "THROTTLE");
2134 spin_lock_irqsave(&dc->spin_mutex, flags);
2135 set_rts(port->tty_index, 0);
2136 spin_unlock_irqrestore(&dc->spin_mutex, flags);
2139 static void ntty_put_char(struct tty_struct *tty, unsigned char c)
2141 D2("PUT CHAR Function: %c", c);
2144 /* Returns number of chars in buffer, called by tty layer */
2145 static s32 ntty_chars_in_buffer(struct tty_struct *tty)
2147 port_t *port = (port_t *) tty->driver_data;
2148 s32 rval;
2150 if (! port) {
2151 rval = -ENODEV;
2152 goto exit_in_buffer;
2155 if (! port->tty_open_count) {
2156 ERR("No tty open?");
2157 rval = -ENODEV;
2158 goto exit_in_buffer;
2161 rval = __kfifo_len(port->fifo_ul);
2163 exit_in_buffer:
2164 return rval;
2167 /* Initializes the tty */
2168 static int ntty_tty_init(void)
2170 struct tty_driver *td = &dc->tty_driver;
2171 int rval;
2172 int i;
2174 memset(td, 0, sizeof(struct tty_driver));
2176 td->magic = TTY_DRIVER_MAGIC;
2177 td->driver_name = NOZOMI_NAME_TTY;
2178 td->name = "noz";
2179 td->major = NTTY_TTY_MAJOR,
2180 td->type = TTY_DRIVER_TYPE_SERIAL,
2181 td->subtype = SERIAL_TYPE_NORMAL,
2182 td->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS,
2183 td->init_termios = tty_std_termios;
2184 td->init_termios.c_cflag = B115200 | CS8 | CREAD | HUPCL | CLOCAL;
2186 td->num = MAX_PORT;
2187 td->name_base = 0;
2188 td->minor_start = 0;
2190 #ifdef KERNEL_2_4
2191 td->table = dc->tty_table;
2192 td->refcount = &dc->tty_refcount;
2193 #endif
2195 td->termios = dc->tty_termios;
2196 td->termios_locked = dc->tty_termios_locked;
2198 td->ioctl = ntty_ioctl;
2199 td->open = ntty_open;
2200 td->close = ntty_close;
2201 td->write = ntty_write;
2202 td->write_room = ntty_write_room;
2203 td->unthrottle = ntty_unthrottle;
2204 td->throttle = ntty_throttle;
2205 td->set_termios = ntty_set_termios;
2206 td->chars_in_buffer = ntty_chars_in_buffer;
2207 td->put_char = ntty_put_char;
2209 rval = tty_register_driver(td);
2211 if (rval) {
2212 printk(KERN_ERR "failed to register ntty tty driver");
2213 D1( "failed to register ntty tty driver");
2214 return rval;
2217 for (i = 0; i < NTTY_TTY_MINORS; i++) {
2218 init_MUTEX(&dc->port[i].tty_sem);
2219 dc->port[i].tty_open_count = 0;
2220 dc->port[i].tty = NULL;
2222 #ifdef KERNEL_2_6
2223 tty_register_device(td, i, NULL);
2224 #endif
2227 printk(KERN_INFO DRIVER_DESC " " NOZOMI_NAME_TTY);
2228 D1( " ");
2229 return rval;
2232 /* Module initialization */
2233 static struct pci_driver nozomi_driver = {
2234 .name = NOZOMI_NAME,
2235 .id_table = nozomi_pci_tbl,
2236 .probe = nozomi_card_init,
2237 .remove = __devexit_p(nozomi_card_exit),
2240 static int __init nozomi_init(void)
2242 int rval;
2244 rval = pci_module_init(&nozomi_driver);
2245 printk(KERN_INFO "Initializing %s\n", VERSION_STRING);
2246 return rval;
2249 static void nozomi_exit(void)
2251 printk(KERN_INFO "Unloading %s", DRIVER_DESC);
2252 pci_unregister_driver(&nozomi_driver);
2255 module_init(nozomi_init);
2256 module_exit(nozomi_exit);
2258 #ifdef NOZOMI_DEBUG
2259 MODULE_PARM(nzdebug, "i");
2260 #endif
2262 MODULE_LICENSE("Dual BSD/GPL");
2263 MODULE_DESCRIPTION( DRIVER_DESC );