1 /* nozomi.c -- HSDPA driver Broadband Wireless Data Card - Globe Trotter
3 * Written by: Ulf Jakobsson,
7 * Maintained by: Paul Hardwick (p.hardwick@option.com)
10 * Locking code changes for Vodafone by Sphere Systems Ltd,
11 * Andrew Bird (ajb@spheresystems.co.uk )
14 * Source has been ported from an implementation made by Filip Aben @ Option
16 * --------------------------------------------------------------------------
18 Copyright (c) 2005,2006 Option Wireless Sweden AB
19 Copyright (c) 2006 Sphere Systems Ltd
20 Copyright (c) 2006 Option Wireless n/v
23 This program is free software; you can redistribute it and/or modify
24 it under the terms of the GNU General Public License as published by
25 the Free Software Foundation; either version 2 of the License, or
26 (at your option) any later version.
28 This program is distributed in the hope that it will be useful,
29 but WITHOUT ANY WARRANTY; without even the implied warranty of
30 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 GNU General Public License for more details.
33 You should have received a copy of the GNU General Public License
34 along with this program; if not, write to the Free Software
35 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
37 * --------------------------------------------------------------------------
42 * 17-Sept-2006 paul Hardwick
43 * -Fixed unseen! bug in termio settings
44 * -Removed more compile warnings
46 * 10-Sept-2006 Paul Hardwick
48 * - A 2.6 kernel support version incorporating patches by GregKH
50 * - module reference counting
51 * - improved device exit code
52 * - greater compliance to kernel guidelines
54 * - 2.4 kernels untested but included
55 * - Still supports 2.6.14 for Ubunto users.
56 * - Finally fixed compiler warnings
59 * 03-July-2006 Paul Hardwick
61 * - Stability Improvements. Incorporated spinlock wraps patch.
62 * - Updated for newer 2.6.14+ kernels (tty_buffer_request_room)
63 * - using __devexit macro for tty
67 * 08-feb-2006 15:34:10:Ulf
69 * -Fixed issue when not waking up line disipine layer, could probably result
70 * in better uplink performance for 2.4.
72 * -Fixed issue with big endian during initalization, now proper toggle flags
73 * are handled between preloader and maincode.
75 * -Fixed flow control issue.
77 * -Added support for setting DTR.
79 * -For 2.4 kernels, removing temporary buffer that's not needed.
81 * -Reading CTS only for modem port (only port that supports it).
83 * -Return 0 in write_room instead of netative value, it's not handled in
86 * --------------------------------------------------------------------------
89 * First version of driver, only tested with card of type F32_2.
90 * Works fine with 2.4 and 2.6 kernels.
91 * Driver also support big endian architecture.
96 * See TODO file in this package
99 #include <linux/version.h>
101 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,14)
102 #define KERNEL_2_6_14
104 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17)
105 #define KERNEL_2_6_18
109 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0)
111 #elif LINUX_VERSION_CODE > KERNEL_VERSION(2,4,0)
115 #include <linux/module.h>
116 #include <linux/pci.h>
117 #include <linux/ioport.h>
118 #include <linux/tty.h>
119 #include <linux/tty_driver.h>
120 #include <linux/tty_flip.h>
121 #include <linux/serial.h>
122 #include <linux/interrupt.h>
123 #include <linux/kmod.h>
124 #include <linux/proc_fs.h>
125 #include <linux/init.h>
127 #include <linux/kfifo.h>
131 #include <asm/uaccess.h>
141 typedef void irqreturn_t;
147 #error "This driver needs PCI support to be available"
150 #define VERSION_STRING DRIVER_DESC " 2.21alpha (build date: " __DATE__ " " __TIME__ ")"
152 /* Macros definitions */
154 /* Enable this to have a lot of debug printouts */
155 /* #define NOZOMI_DEBUG */
157 /* Default debug printout level */
158 #define NOZOMI_DEBUG_LEVEL 0x1
160 #define P_BUF_SIZE 128
161 #define NFO( _err_flag_, args...) \
163 char t_m_p_[P_BUF_SIZE]; \
164 snprintf(t_m_p_, sizeof(t_m_p_), ##args); \
165 printk( _err_flag_ "[%d] %s(): %s\n", __LINE__, __FUNCTION__, t_m_p_); \
168 #define ERR(args...) NFO( KERN_ERR, ##args)
170 #define D1(args...) D_(0x01, ##args)
171 #define D2(args...) D_(0x02, ##args)
172 #define D3(args...) D_(0x04, ##args)
173 #define D4(args...) D_(0x08, ##args)
174 #define D5(args...) D_(0x10, ##args)
175 #define D6(args...) D_(0x20, ##args)
176 #define D7(args...) D_(0x40, ##args)
177 #define D8(args...) D_(0x80, ##args)
180 #define D_(lvl, args...) D(lvl, ##args)
181 /* Do we need this settable at runtime? */
182 static int nzdebug = NOZOMI_DEBUG_LEVEL;
184 #define D(lvl, args...) do{if(lvl & nzdebug) NFO(KERN_DEBUG, ##args );}while(0)
185 #define D_(lvl, args...) D(lvl, ##args)
187 /* These printouts are always printed */
190 static const int nzdebug = 0;
191 #define D_(lvl, args...)
194 /* TODO: rewrite to optimize macros... */
195 #define SET_FCR(value__) \
197 writew((value__), (dc->REG_FCR )); \
200 #define SET_IER(value__, mask__) \
202 dc->ier_last_written = (dc->ier_last_written & ~mask__) | (value__ & mask__ );\
203 writew( dc->ier_last_written, (dc->REG_IER));\
206 #define GET_IER(read_val__) \
208 (read_val__) = readw((dc->REG_IER));\
211 #define GET_IIR(read_val__) \
213 (read_val__) = readw( (dc->REG_IIR));\
216 #define GET_MEM(value__, addr__, length__) \
218 /* read_mem32( (u32*) (value__), (u32) (addr__), (length__)); */\
219 read_mem32( (u32*) (value__), (addr__), (length__));\
222 #define GET_MEM_BUF(value__, addr__, length__) \
224 /* read_mem32_buf( (u32*) (value__), (u32) (addr__), (length__)); */\
225 read_mem32_buf( (u32*) (value__), (addr__), (length__));\
228 #define SET_MEM(addr__, value__, length__) \
230 write_mem32( (addr__), (u32*) (value__), (length__));\
233 #define SET_MEM_BUF(addr__, value__, length__) \
235 write_mem32_buf( (addr__), (u32*) (value__), (length__));\
238 #define TMP_BUF_MAX 256
240 #define DUMP(buf__,len__) \
242 char tbuf[TMP_BUF_MAX]={0};\
244 snprintf(tbuf, len__ > TMP_BUF_MAX ? TMP_BUF_MAX : len__, "%s",buf__);\
245 if(tbuf[len__-2] == '\r') {\
246 tbuf[len__-2] = 'r';\
248 D1( "SENDING: '%s' (%d+n)", tbuf, len__);\
250 D1( "SENDING: '%s' (%d)", tbuf, len__);\
254 #define RELEVANT_IFLAG(iflag) ((iflag) & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
257 #define NOZOMI_NAME "nozomi"
258 #define NOZOMI_NAME_TTY "nozomi_tty"
259 #define DRIVER_DESC "Nozomi driver"
261 #define NTTY_TTY_MAJOR 241
262 #define NTTY_TTY_MINORS MAX_PORT
263 #define NTTY_FIFO_BUFFER_SIZE 8192
265 /* Must be power of 2 */
266 #define FIFO_BUFFER_SIZE_UL 8192
268 /* Size of tmp send buffer to card */
269 #define SEND_BUF_MAX 1024
270 #define RECEIVE_BUF_MAX 4
272 /* Our fake UART values */
275 #define MCR_LOOP 0x04
281 /* Define all types of vendors and devices to support */
282 #define VENDOR1 0x1931 /* Vendor Option */
283 #define DEVICE1 0x000c /* HSDPA card */
285 #define R_IIR 0x0000 /* Interrupt Identity Register */
286 #define R_FCR 0x0000 /* Flow Control Register */
287 #define R_IER 0x0004 /* Interrupt Enable Register */
289 #define CONFIG_MAGIC 0xEFEFFEFE
290 #define TOGGLE_VALID 0x0000
292 /* Definition of interrupt tokens */
293 #define MDM_DL1 0x0001
294 #define MDM_UL1 0x0002
295 #define MDM_DL2 0x0004
296 #define MDM_UL2 0x0008
297 #define DIAG_DL1 0x0010
298 #define DIAG_DL2 0x0020
299 #define DIAG_UL 0x0040
300 #define APP1_DL 0x0080
301 #define APP1_UL 0x0100
302 #define APP2_DL 0x0200
303 #define APP2_UL 0x0400
304 #define CTRL_DL 0x0800
305 #define CTRL_UL 0x1000
308 #define MDM_DL (MDM_DL1 | MDM_DL2)
309 #define MDM_UL (MDM_UL1 | MDM_UL2)
310 #define DIAG_DL (DIAG_DL1 | DIAG_DL2)
312 /* modem signal definition */
313 #define CTRL_DSR 0x0001
314 #define CTRL_DCD 0x0002
315 #define CTRL_RI 0x0004
316 #define CTRL_CTS 0x0008
318 #define CTRL_DTR 0x0001
319 #define CTRL_RTS 0x0002
322 #define NOZOMI_MAX_PORTS 5
324 /* Type definitions */
326 /* There are two types of nozomi cards, one with 2048 memory and with 8192 memory */
328 F32_2 = 2048, /* Has 512 bytes downlink and uplink * 2 -> 2048 */
329 F32_8 = 8192, /* Has 3072 bytes downlink and 1024 bytes uplink * 2 -> 8192 */
332 /* Two different toggle channels exist */
338 /* Port definition for the card regarding flow control */
339 enum ctrl_port_type {
348 /* Ports that the nozomi has */
362 unsigned enabled:5; /* Toggle fields are valid if enabled is 0, else A-channels
363 must always be used. */
367 } __attribute__ ((packed)) toggles_t;
369 /* Configuration table to read at startup of card */
370 /* Is for now only needed during initialization phase */
373 u16 product_information;
378 u16 dl_mdm_len1; /* If this is 64, it can hold 60 bytes + 4 that is length field */
382 u16 dl_mdm_len2; /* If this is 64, it can hold 60 bytes + 4 that is length field */
396 } __attribute__ ((packed)) config_table_t;
398 /* This stores all control downlink flags */
406 } __attribute__ ((packed)) ctrl_dl_t;
408 /* This stores all control uplink flags */
414 } __attribute__ ((packed)) ctrl_ul_t;
419 /* This represents the toggle information */
424 unsigned enabled:5; /* Toggle fields are valid if enabled is 0, else A-channels
425 must always be used. */
426 } __attribute__ ((packed)) toggles_t;
428 /* Configuration table to read at startup of card */
432 u16 product_information;
436 u16 dl_mdm_len1; /* If this is 64, it can hold 60 bytes + 4 that is length field */
451 } __attribute__ ((packed)) config_table_t;
453 /* This stores all control downlink flags */
459 unsigned reserverd:4;
461 } __attribute__ ((packed)) ctrl_dl_t;
463 /* This stores all control uplink flags */
469 } __attribute__ ((packed)) ctrl_ul_t;
472 /* This holds all information that is needed regarding a port */
474 u8 update_flow_control;
477 struct kfifo *fifo_ul;
478 void __iomem *dl_addr[2];
481 void __iomem *ul_addr[2];
486 struct tty_struct *tty;
488 struct semaphore tty_sem;
489 wait_queue_head_t tty_wait;
490 struct async_icount tty_icount;
492 u32 rx_data, tx_data;
497 /* Private data one for each card in the system */
499 void __iomem *base_addr;
502 /* Pointers to registers */
503 void __iomem *REG_IIR;
504 void __iomem *REG_FCR;
505 void __iomem *REG_IER;
507 u16 ier_last_written;
508 enum card_type card_type;
509 config_table_t config_table; /* Configuration table */
510 struct pci_dev *pdev;
511 struct port port[NOZOMI_MAX_PORTS];
514 struct tty_driver *tty_driver;
517 struct tty_struct *tty_table[NTTY_TTY_MINORS];
518 struct tq_struct tty_flip_queue;
522 struct workqueue_struct *tty_flip_wq;
523 struct work_struct tty_flip_wq_struct;
526 struct termios *tty_termios[NTTY_TTY_MINORS];
527 struct termios *tty_termios_locked[NTTY_TTY_MINORS];
528 spinlock_t spin_mutex;
531 struct proc_dir_entry *proc_entry;
535 /* This is a data packet that is read or written to/from card */
537 u32 size; /* size is the length of the data buffer */
539 } __attribute__ ((packed)) buf_t;
541 /* Function declarations */
542 static int ntty_tty_init(dc_t * dc);
543 static void tty_flip_queue_function(void *tmp_dc);
545 /* Global variables */
546 static struct pci_device_id nozomi_pci_tbl[] = {
551 /* Used to store interrupt variables */
553 u16 read_iir; /* Holds current interrupt tokens */
556 MODULE_DEVICE_TABLE(pci, nozomi_pci_tbl);
558 /* Representing the pci device of interest */
559 static int cards_found;
560 static dc_t *my_dev = NULL;
563 static inline dc_t *get_dc_by_pdev(struct pci_dev *pdev)
568 static inline dc_t *get_dc_by_index(s32 index)
573 static inline s32 get_index(struct tty_struct *tty)
578 return MINOR(tty->device) - tty->driver.minor_start;
583 static inline struct port *get_port_by_tty(struct tty_struct *tty)
585 return &my_dev->port[get_index(tty)];
588 static inline dc_t *get_dc_by_tty(struct tty_struct *tty)
595 /* -Rewrite cleaner */
596 //static void read_mem32(u32 *buf, u32 mem_addr_start, u32 size_bytes) {
597 static void read_mem32(u32 * buf, void __iomem * mem_addr_start, u32 size_bytes)
600 u32 *ptr = (__force u32 *) mem_addr_start;
604 if (size_bytes == 2) {
606 *buf16 = readw((void __iomem *)ptr);
610 while (i < size_bytes) {
611 if (size_bytes - i == 2) {
612 /* Handle 2 bytes in the end */
614 *(buf16) = readw((void __iomem *)ptr);
618 *(buf) = readl((void __iomem *)ptr);
627 /* - Rewrite cleaner */
628 /* - merge with read_mem32() */
629 //static void read_mem32_buf(u32 *buf, u32 mem_addr_start, u32 size_bytes) {
630 static void read_mem32_buf(u32 * buf, void __iomem * mem_addr_start,
635 u32 *ptr = (u32 *) mem_addr_start;
639 if (size_bytes == 2) {
641 *buf16 = __le16_to_cpu(readw(ptr));
645 while (i < size_bytes) {
646 if (size_bytes - i == 2) {
647 /* Handle 2 bytes in the end */
649 *(buf16) = __le16_to_cpu(readw(ptr));
653 *(buf) = __le32_to_cpu(readl(ptr));
660 read_mem32(buf, mem_addr_start, size_bytes);
666 /* -Rewrite cleaner */
667 //static u32 write_mem32(u32 mem_addr_start, u32 *buf, u32 size_bytes) {
668 static u32 write_mem32(void __iomem * mem_addr_start, u32 * buf, u32 size_bytes)
671 u32 *ptr = (__force u32 *) mem_addr_start;
675 if (size_bytes == 2) {
677 writew(*buf16, (void __iomem *)ptr);
681 while (i < size_bytes) {
682 if (size_bytes - i == 2) {
685 writew(*buf16, (void __iomem *)ptr);
689 writel(*buf, (void __iomem *)ptr);
699 /* - Merge with write_mem32() */
700 //static u32 write_mem32_buf(u32 mem_addr_start, u32 *buf, u32 size_bytes) {
701 static u32 write_mem32_buf(void __iomem * mem_addr_start, u32 * buf,
706 u32 *ptr = (u32 *) mem_addr_start;
710 if (size_bytes == 2) {
712 writew(__le16_to_cpu(*buf16), ptr);
716 while (i < size_bytes) {
717 if (size_bytes - i == 2) {
720 writew(__le16_to_cpu(*buf16), ptr);
724 writel(__cpu_to_le32(*buf), ptr);
732 return write_mem32(mem_addr_start, buf, size_bytes);
736 /* Setup pointers to different channels and also setup buffer sizes. */
737 static void setup_memory(dc_t * dc)
739 void __iomem *offset = dc->base_addr + dc->config_table.dl_start;
740 /* The length reported is including the length field of 4 bytes, hence subtract with 4. */
743 /* Modem port dl configuration */
744 dc->port[PORT_MDM].dl_addr[CH_A] = offset;
745 dc->port[PORT_MDM].dl_addr[CH_B] = (offset +=
746 dc->config_table.dl_mdm_len1);
747 dc->port[PORT_MDM].dl_size[CH_A] =
748 dc->config_table.dl_mdm_len1 - buff_offset;
749 dc->port[PORT_MDM].dl_size[CH_B] =
750 dc->config_table.dl_mdm_len2 - buff_offset;
752 /* Diag port dl configuration */
753 dc->port[PORT_DIAG].dl_addr[CH_A] = (offset +=
754 dc->config_table.dl_mdm_len2);
755 dc->port[PORT_DIAG].dl_size[CH_A] =
756 dc->config_table.dl_diag_len1 - buff_offset;
757 dc->port[PORT_DIAG].dl_addr[CH_B] = (offset +=
758 dc->config_table.dl_diag_len1);
759 dc->port[PORT_DIAG].dl_size[CH_B] =
760 dc->config_table.dl_diag_len2 - buff_offset;
762 /* App1 port dl configuration */
763 dc->port[PORT_APP1].dl_addr[CH_A] = (offset +=
764 dc->config_table.dl_diag_len2);
765 dc->port[PORT_APP1].dl_size[CH_A] =
766 dc->config_table.dl_app1_len - buff_offset;
768 /* App2 port dl configuration */
769 dc->port[PORT_APP2].dl_addr[CH_A] = (offset +=
770 dc->config_table.dl_app1_len);
771 dc->port[PORT_APP2].dl_size[CH_A] =
772 dc->config_table.dl_app2_len - buff_offset;
774 /* Ctrl dl configuration */
775 dc->port[PORT_CTRL].dl_addr[CH_A] = (offset +=
776 dc->config_table.dl_app2_len);
777 dc->port[PORT_CTRL].dl_size[CH_A] =
778 dc->config_table.dl_ctrl_len - buff_offset;
780 /* Modem Port ul configuration */
781 dc->port[PORT_MDM].ul_addr[CH_A] = (offset =
783 dc->config_table.ul_start);
784 dc->port[PORT_MDM].ul_size[CH_A] =
785 dc->config_table.ul_mdm_len1 - buff_offset;
786 dc->port[PORT_MDM].ul_addr[CH_B] = (offset +=
787 dc->config_table.ul_mdm_len1);
788 dc->port[PORT_MDM].ul_size[CH_B] =
789 dc->config_table.ul_mdm_len2 - buff_offset;
791 /* Diag port ul configuration */
792 dc->port[PORT_DIAG].ul_addr[CH_A] = (offset +=
793 dc->config_table.ul_mdm_len2);
794 dc->port[PORT_DIAG].ul_size[CH_A] =
795 dc->config_table.ul_diag_len - buff_offset;
797 /* App1 port ul configuration */
798 dc->port[PORT_APP1].ul_addr[CH_A] = (offset +=
799 dc->config_table.ul_diag_len);
800 dc->port[PORT_APP1].ul_size[CH_A] =
801 dc->config_table.ul_app1_len - buff_offset;
803 /* App2 port ul configuration */
804 dc->port[PORT_APP2].ul_addr[CH_A] = (offset +=
805 dc->config_table.ul_app1_len);
806 dc->port[PORT_APP2].ul_size[CH_A] =
807 dc->config_table.ul_app2_len - buff_offset;
809 /* Ctrl ul configuration */
810 dc->port[PORT_CTRL].ul_addr[CH_A] = (offset +=
811 dc->config_table.ul_app2_len);
812 dc->port[PORT_CTRL].ul_size[CH_A] =
813 dc->config_table.ul_ctrl_len - buff_offset;
814 // offset = dc->config_table.ul_start;
817 /* Dump config table under initalization phase */
819 static void dump_table(dc_t * dc)
821 D3("signature: 0x%08X", dc->config_table.signature);
822 D3("version: 0x%04X", dc->config_table.version);
823 D3("product_information: 0x%04X", dc->config_table.product_information);
824 D3("toggle enabled: %d", dc->config_table.toggle.enabled);
825 D3("toggle up_mdm: %d", dc->config_table.toggle.mdm_ul);
826 D3("toggle dl_mdm: %d", dc->config_table.toggle.mdm_dl);
827 D3("toggle dl_dbg: %d", dc->config_table.toggle.diag_dl);
829 D3("dl_start: 0x%04X", dc->config_table.dl_start);
830 D3("dl_mdm_len0: 0x%04X, %d", dc->config_table.dl_mdm_len1,
831 dc->config_table.dl_mdm_len1);
832 D3("dl_mdm_len1: 0x%04X, %d", dc->config_table.dl_mdm_len2,
833 dc->config_table.dl_mdm_len2);
834 D3("dl_diag_len0: 0x%04X, %d", dc->config_table.dl_diag_len1,
835 dc->config_table.dl_diag_len1);
836 D3("dl_diag_len1: 0x%04X, %d", dc->config_table.dl_diag_len2,
837 dc->config_table.dl_diag_len2);
838 D3("dl_app1_len: 0x%04X, %d", dc->config_table.dl_app1_len,
839 dc->config_table.dl_app1_len);
840 D3("dl_app2_len: 0x%04X, %d", dc->config_table.dl_app2_len,
841 dc->config_table.dl_app2_len);
842 D3("dl_ctrl_len: 0x%04X, %d", dc->config_table.dl_ctrl_len,
843 dc->config_table.dl_ctrl_len);
844 D3("ul_start: 0x%04X, %d", dc->config_table.ul_start,
845 dc->config_table.ul_start);
846 D3("ul_mdm_len[0]: 0x%04X, %d", dc->config_table.ul_mdm_len1,
847 dc->config_table.ul_mdm_len1);
848 D3("ul_mdm_len[1]: 0x%04X, %d", dc->config_table.ul_mdm_len2,
849 dc->config_table.ul_mdm_len2);
850 D3("ul_diag_len: 0x%04X, %d", dc->config_table.ul_diag_len,
851 dc->config_table.ul_diag_len);
852 D3("ul_app1_len: 0x%04X, %d", dc->config_table.ul_app1_len,
853 dc->config_table.ul_app1_len);
854 D3("ul_app2_len: 0x%04X, %d", dc->config_table.ul_app2_len,
855 dc->config_table.ul_app2_len);
856 D3("ul_ctrl_len: 0x%04X, %d", dc->config_table.ul_ctrl_len,
857 dc->config_table.ul_ctrl_len);
861 /* Read configuration table from card under intalization phase */
862 /* Returns 1 if ok, else 0 */
863 static int nozomi_read_config_table(dc_t * dc)
866 GET_MEM(&dc->config_table, dc->base_addr + 0, sizeof(config_table_t));
868 /* D1( "0x%08X == 0x%08X ", dc->config_table.signature, CONFIG_MAGIC); */
870 if (dc->config_table.signature != CONFIG_MAGIC) {
871 dev_err(&dc->pdev->dev, "ConfigTable Bad! 0x%08X != 0x%08X\n",
872 dc->config_table.signature, CONFIG_MAGIC);
876 if ((dc->config_table.version == 0)
877 || (dc->config_table.toggle.enabled == TOGGLE_VALID)) {
879 D1("Second phase, configuring card");
883 dc->port[PORT_MDM].toggle_ul = dc->config_table.toggle.mdm_ul;
884 dc->port[PORT_MDM].toggle_dl = dc->config_table.toggle.mdm_dl;
885 dc->port[PORT_DIAG].toggle_dl = dc->config_table.toggle.diag_dl;
886 D1("toggle ports: MDM UL:%d MDM DL:%d, DIAG DL:%d",
887 dc->port[PORT_MDM].toggle_ul,
888 dc->port[PORT_MDM].toggle_dl, dc->port[PORT_DIAG].toggle_dl);
893 for (i = PORT_MDM; i < MAX_PORT; i++) {
894 dc->port[i].fifo_ul =
895 kfifo_alloc(FIFO_BUFFER_SIZE_UL, GFP_ATOMIC, NULL);
896 memset(&dc->port[i].ctrl_dl, 0, sizeof(ctrl_dl_t));
897 memset(&dc->port[i].ctrl_ul, 0, sizeof(ctrl_ul_t));
900 /* Enable control channel */
901 SET_IER(CTRL_DL, CTRL_DL);
903 dev_info(&dc->pdev->dev, "Initialization OK!\n");
907 if ((dc->config_table.version > 0)
908 && (dc->config_table.toggle.enabled != TOGGLE_VALID)) {
910 D1("First phase: pushing upload buffers, clearing download");
912 dev_info(&dc->pdev->dev, "Version of card: %d\n",
913 dc->config_table.version);
915 /* Here we should disable all I/O over F32. */
918 /* We should send ALL channel pair tokens back along with reset token */
920 /* push upload modem buffers */
921 SET_MEM(dc->port[PORT_MDM].ul_addr[CH_A], &offset, 4);
922 SET_MEM(dc->port[PORT_MDM].ul_addr[CH_B], &offset, 4);
924 SET_FCR(MDM_UL | DIAG_DL | MDM_DL);
926 D1("First phase done");
932 /* Enable uplink interrupts */
933 static void enable_transmit_ul(enum port_type port, dc_t * dc)
938 SET_IER(MDM_UL, MDM_UL);
941 SET_IER(DIAG_UL, DIAG_UL);
944 SET_IER(APP1_UL, APP1_UL);
947 SET_IER(APP2_UL, APP2_UL);
950 SET_IER(CTRL_UL, CTRL_UL);
953 dev_err(&dc->pdev->dev, "Called with wrong port?\n");
958 /* Disable uplink interrupts */
959 static void disable_transmit_ul(enum port_type port, dc_t * dc)
978 dev_err(&dc->pdev->dev, "Called with wrong port?\n");
983 /* Enable downlink interrupts */
984 static void enable_transmit_dl(enum port_type port, dc_t * dc)
988 SET_IER(MDM_DL, MDM_DL);
991 SET_IER(DIAG_DL, DIAG_DL);
994 SET_IER(APP1_DL, APP1_DL);
997 SET_IER(APP2_DL, APP2_DL);
1000 SET_IER(CTRL_DL, CTRL_DL);
1003 dev_err(&dc->pdev->dev, "Called with wrong port?\n");
1008 /* Disable downlink interrupts */
1009 static void disable_transmit_dl(enum port_type port, dc_t * dc)
1016 SET_IER(0, DIAG_DL);
1019 SET_IER(0, APP1_DL);
1022 SET_IER(0, APP2_DL);
1025 SET_IER(0, CTRL_DL);
1028 dev_err(&dc->pdev->dev, "Called with wrong port?\n");
1033 /* Return 1 - send buffer to card and ack. */
1034 /* Return 0 - don't ack, don't send buffer to card. */
1035 static int send_data(enum port_type index, dc_t * dc)
1038 struct port *port = &dc->port[index];
1039 u8 toggle = port->toggle_ul;
1040 void __iomem *addr = port->ul_addr[toggle];
1041 u32 ul_size = port->ul_size[toggle];
1042 struct tty_struct *tty = port->tty;
1044 if (index >= NTTY_TTY_MINORS) {
1045 dev_err(&dc->pdev->dev, "Called with wrong index?\n");
1049 /* Get data from tty and place in buf for now */
1051 __kfifo_get(port->fifo_ul, dc->send_buf,
1052 ul_size < SEND_BUF_MAX ? ul_size : SEND_BUF_MAX);
1055 D4("No more data to send, disable link:");
1059 port->tx_data += size;
1061 /* DUMP(buf, size); */
1063 /* Write length + data */
1064 SET_MEM(addr, &size, 4);
1065 SET_MEM_BUF(addr + 4, dc->send_buf, size);
1068 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP))
1069 && tty->ldisc.write_wakeup) {
1070 tty->ldisc.write_wakeup(tty);
1072 wake_up_interruptible(&tty->write_wait);
1078 /* If all data has been read, return 1, else 0 */
1079 static int receive_data(enum port_type index, dc_t * dc)
1081 u8 buf[RECEIVE_BUF_MAX] = { 0 };
1084 struct port *port = &dc->port[index];
1085 u8 toggle = port->toggle_dl;
1086 void __iomem *addr = port->dl_addr[toggle];
1087 struct tty_struct *tty = port->tty;
1091 D1("tty not open for port: %d?", index);
1095 GET_MEM(&size, addr, 4);
1096 /* D1( "%d bytes port: %d", size, index); */
1098 if (test_bit(TTY_THROTTLED, &tty->flags)) {
1099 D1("No room in tty, don't read data, don't ack interrupt, disable interrupt");
1101 /* disable interrupt in downlink... */
1102 disable_transmit_dl(index, dc);
1104 schedule_task(&dc->tty_flip_queue);
1110 dev_err(&dc->pdev->dev, "size == 0?\n");
1115 GET_MEM_BUF(buf, addr + offset, 4);
1118 while (i < 4 && size > 0) {
1119 #ifdef KERNEL_2_6_14
1120 if (tty_buffer_request_room(tty, 1) < 1)
1122 if (tty->flip.count >= TTY_FLIPBUF_SIZE) //pre 2.6.14 kernels*/
1125 tty_flip_buffer_push(tty);
1127 tty_insert_flip_char(tty, buf[i], TTY_NORMAL);
1136 tty_flip_buffer_push(tty);
1141 /* Debug for interrupts */
1143 static char *interrupt2str(u16 interrupt)
1145 static char buf[TMP_BUF_MAX];
1148 interrupt & MDM_DL1 ? p += snprintf(p, TMP_BUF_MAX, "MDM_DL1 ") : 0;
1149 interrupt & MDM_DL2 ? p += snprintf(p, TMP_BUF_MAX, "MDM_DL2 ") : 0;
1151 interrupt & MDM_UL1 ? p += snprintf(p, TMP_BUF_MAX, "MDM_UL1 ") : 0;
1152 interrupt & MDM_UL2 ? p += snprintf(p, TMP_BUF_MAX, "MDM_UL2 ") : 0;
1154 interrupt & DIAG_DL1 ? p += snprintf(p, TMP_BUF_MAX, "DIAG_DL1 ") : 0;
1155 interrupt & DIAG_DL2 ? p += snprintf(p, TMP_BUF_MAX, "DIAG_DL2 ") : 0;
1157 interrupt & DIAG_UL ? p += snprintf(p, TMP_BUF_MAX, "DIAG_UL ") : 0;
1159 interrupt & APP1_DL ? p += snprintf(p, TMP_BUF_MAX, "APP1_DL ") : 0;
1160 interrupt & APP2_DL ? p += snprintf(p, TMP_BUF_MAX, "APP2_DL ") : 0;
1162 interrupt & APP1_UL ? p += snprintf(p, TMP_BUF_MAX, "APP1_UL ") : 0;
1163 interrupt & APP2_UL ? p += snprintf(p, TMP_BUF_MAX, "APP2_UL ") : 0;
1165 interrupt & CTRL_DL ? p += snprintf(p, TMP_BUF_MAX, "CTRL_DL ") : 0;
1166 interrupt & CTRL_UL ? p += snprintf(p, TMP_BUF_MAX, "CTRL_UL ") : 0;
1168 interrupt & RESET ? p += snprintf(p, TMP_BUF_MAX, "RESET ") : 0;
1174 /* Receive flow control */
1175 /* Return 1 - If ok, else 0 */
1176 static int receive_flow_control(dc_t * dc, irq_t * m)
1178 enum port_type port = PORT_MDM;
1183 GET_MEM(&ctrl_dl, dc->port[PORT_CTRL].dl_addr[CH_A], 2);
1185 switch (ctrl_dl.port) {
1187 D1("The Base Band sends this value as a response to a request for IMSI detach sent" " over the control channel uplink (see section 7.6.1).");
1191 enable_ier = MDM_DL;
1195 enable_ier = DIAG_DL;
1199 enable_ier = APP1_DL;
1203 enable_ier = APP2_DL;
1206 dev_err(&dc->pdev->dev,
1207 "ERROR: flow control received for non-existing port\n");
1211 D1("0x%04X->0x%04X", *((u16 *) & dc->port[port].ctrl_dl),
1212 *((u16 *) & ctrl_dl));
1214 old_ctrl = dc->port[port].ctrl_dl;
1215 dc->port[port].ctrl_dl = ctrl_dl;
1217 if (old_ctrl.CTS == 1 && ctrl_dl.CTS == 0) {
1218 D1("Disable interrupt (0x%04X) on port: %d", enable_ier, port);
1219 disable_transmit_ul(port, dc);
1221 } else if (old_ctrl.CTS == 0 && ctrl_dl.CTS == 1) {
1223 if (__kfifo_len(dc->port[port].fifo_ul)) {
1224 D1("Enable interrupt (0x%04X) on port: %d", enable_ier,
1226 D1("Data in buffer [%d], enable transmit! ",
1227 __kfifo_len(dc->port[port].fifo_ul));
1228 enable_transmit_ul(port, dc);
1230 D1("No data in buffer...");
1234 if (*(u16 *) & old_ctrl == *(u16 *) & ctrl_dl) {
1235 D1(" No change in mctrl");
1238 /* Update statistics */
1239 if (old_ctrl.CTS != ctrl_dl.CTS) {
1240 dc->port[port].tty_icount.cts++;
1242 if (old_ctrl.DSR != ctrl_dl.DSR) {
1243 dc->port[port].tty_icount.dsr++;
1245 if (old_ctrl.RI != ctrl_dl.RI) {
1246 dc->port[port].tty_icount.rng++;
1248 if (old_ctrl.DCD != ctrl_dl.DCD) {
1249 dc->port[port].tty_icount.dcd++;
1251 D1("port: %d DCD(%d), CTS(%d), RI(%d), DSR(%d)",
1253 dc->port[port].tty_icount.dcd, dc->port[port].tty_icount.cts,
1254 dc->port[port].tty_icount.rng, dc->port[port].tty_icount.dsr);
1260 /* - return enum ctrl_port_type */
1261 static u8 port2ctrl(enum port_type port, dc_t * dc)
1273 dev_err(&dc->pdev->dev,
1274 "ERROR: send flow control received for non-existing port\n");
1279 /* Send flow control, can only update one channel at a time */
1280 /* Return 0 - If we have updated all flow control */
1281 /* Return 1 - If we need to update more flow control, ack current enable more */
1282 static int send_flow_control(dc_t * dc)
1284 u32 i, more_flow_control_to_be_updated = 0;
1287 for (i = PORT_MDM; i < MAX_PORT; i++) {
1288 if (dc->port[i].update_flow_control) {
1289 if (more_flow_control_to_be_updated) {
1290 /* We have more flow control to be updated */
1293 dc->port[i].ctrl_ul.port = port2ctrl(i, dc);
1294 ctrl = (u16 *) & dc->port[i].ctrl_ul;
1295 /* D1( "sending flow control 0x%04X for port %d, %d", (u16) *ctrl, i, dc->port[i].ctrl_ul.port ); */
1296 SET_MEM(dc->port[PORT_CTRL].ul_addr[0], (u32 *) ctrl,
1298 dc->port[i].update_flow_control = 0;
1299 more_flow_control_to_be_updated = 1;
1305 /* Handle donlink data, ports that are handled are modem and diagnostics */
1307 /* Return 0 - toggle fields are out of sync */
1308 static int handle_data_dl(dc_t * dc, irq_t * m, enum port_type port,
1309 u8 * toggle, u16 mask1, u16 mask2)
1312 if (*toggle == 0 && m->read_iir & mask1) {
1313 if (receive_data(port, dc)) {
1315 *toggle = !(*toggle);
1318 if (m->read_iir & mask2) {
1319 if (receive_data(port, dc)) {
1321 *toggle = !(*toggle);
1324 } else if (*toggle == 1 && m->read_iir & mask2) {
1325 if (receive_data(port, dc)) {
1327 *toggle = !(*toggle);
1330 if (m->read_iir & mask1) {
1331 if (receive_data(port, dc)) {
1333 *toggle = !(*toggle);
1337 ERR("port out of sync!, toggle:%d", *toggle);
1343 /* Handle uplink data, this is currently for the modem port */
1345 /* Return 0 - toggle field are out of sync */
1346 static int handle_data_ul(dc_t * dc, irq_t * m, enum port_type port)
1349 u8 *toggle = &(dc->port[port].toggle_ul);
1351 if (*toggle == 0 && m->read_iir & MDM_UL1) {
1353 if (send_data(port, dc)) {
1355 SET_IER(MDM_UL, MDM_UL);
1359 if (m->read_iir & MDM_UL2) {
1361 if (send_data(port, dc)) {
1363 SET_IER(MDM_UL, MDM_UL);
1368 } else if (*toggle == 1 && m->read_iir & MDM_UL2) {
1370 if (send_data(port, dc)) {
1372 SET_IER(MDM_UL, MDM_UL);
1376 if (m->read_iir & MDM_UL1) {
1378 if (send_data(port, dc)) {
1380 SET_IER(MDM_UL, MDM_UL);
1385 SET_FCR(m->read_iir & MDM_UL);
1386 ERR("port out of sync!");
1392 static irqreturn_t interrupt_handler(int irq, void *dev_id,
1393 struct pt_regs *regs)
1398 if (my_dev && my_dev->pdev != dev_id) {
1402 if (!(dc = get_dc_by_pdev(dev_id))) {
1403 ERR("Could not find device context from pci_dev: %Xu",
1408 GET_IIR(m->read_iir);
1410 /* Just handle interrupt enabled in IER (by masking with dc->ier_last_written) */
1411 m->read_iir &= dc->ier_last_written;
1413 if (m->read_iir == 0) {
1422 spin_lock(&dc->spin_mutex);
1424 D4("%s irq:0x%04X, prev:0x%04X", interrupt2str(m->read_iir),
1425 m->read_iir, dc->ier_last_written);
1427 if (m->read_iir & RESET) {
1428 if (!nozomi_read_config_table(dc)) {
1430 ERR("ERR: Could not read status from card, we should disable interface");
1434 goto exit_handler; /* No more useful info if this was the reset interrupt. */
1436 if (m->read_iir & CTRL_UL) {
1438 SET_IER(0, CTRL_UL);
1439 if (send_flow_control(dc)) {
1441 SET_IER(CTRL_UL, CTRL_UL);
1444 if (m->read_iir & CTRL_DL) {
1445 receive_flow_control(dc, m);
1448 if (m->read_iir & MDM_DL) {
1451 (dc, m, PORT_MDM, &(dc->port[PORT_MDM].toggle_dl), MDM_DL1,
1453 ERR("MDM_DL out of sync!");
1457 if (m->read_iir & MDM_UL) {
1458 if (!handle_data_ul(dc, m, PORT_MDM)) {
1459 ERR("MDM_UL out of sync!");
1463 if (m->read_iir & DIAG_DL) {
1466 (dc, m, PORT_DIAG, &(dc->port[PORT_DIAG].toggle_dl),
1467 DIAG_DL1, DIAG_DL2))) {
1468 ERR("DIAG_DL out of sync!");
1472 if (m->read_iir & DIAG_UL) {
1473 SET_IER(0, DIAG_UL);
1474 if (send_data(PORT_DIAG, dc)) {
1476 SET_IER(DIAG_UL, DIAG_UL);
1479 if (m->read_iir & APP1_DL) {
1480 if (receive_data(PORT_APP1, dc)) {
1484 if (m->read_iir & APP1_UL) {
1485 SET_IER(0, APP1_UL);
1486 if (send_data(PORT_APP1, dc)) {
1488 SET_IER(APP1_UL, APP1_UL);
1491 if (m->read_iir & APP2_DL) {
1492 if (receive_data(PORT_APP2, dc)) {
1496 if (m->read_iir & APP2_UL) {
1497 SET_IER(0, APP2_UL);
1498 if (send_data(PORT_APP2, dc)) {
1500 SET_IER(APP2_UL, APP2_UL);
1505 spin_unlock(&dc->spin_mutex);
1509 /* Request a shared IRQ from system */
1510 static int nozomi_setup_interrupt(struct pci_dev *pdev)
1516 request_irq(pdev->irq, &interrupt_handler, SA_SHIRQ, NOZOMI_NAME,
1518 ERR("Cannot open because IRQ %d is already in use.", pdev->irq);
1525 static void nozomi_get_card_type(dc_t * dc)
1530 for (i = 0; i < 6; i++)
1531 size += pci_resource_len(dc->pdev, i);
1532 /* Assume card type F32_8 if no match */
1533 dc->card_type = size == 2048 ? F32_2 : F32_8;
1534 dev_info(&dc->pdev->dev, "Card type is: %d\n", dc->card_type);
1537 static void nozomi_setup_private_data(dc_t * dc)
1539 void __iomem *offset = dc->base_addr + dc->card_type / 2;
1542 dc->REG_FCR = (void __iomem *)(offset + R_FCR);
1543 dc->REG_IIR = (void __iomem *)(offset + R_IIR);
1544 dc->REG_IER = (void __iomem *)(offset + R_IER);
1545 dc->ier_last_written = 0;
1547 dc->port[PORT_MDM].token_dl = MDM_DL;
1548 dc->port[PORT_DIAG].token_dl = DIAG_DL;
1549 dc->port[PORT_APP1].token_dl = APP1_DL;
1550 dc->port[PORT_APP2].token_dl = APP2_DL;
1552 for (i = PORT_MDM; i < MAX_PORT; i++) {
1553 dc->port[i].rx_data = dc->port[i].tx_data = 0;
1554 dc->port[i].tty_dont_flip = 0;
1558 static void tty_flip_queue_function(void *tmp_dc)
1560 dc_t *dc = (dc_t *) tmp_dc;
1562 unsigned long flags;
1564 /* Enable interrupt for that port */
1565 for (i = 0; i < MAX_PORT; i++) {
1566 if (dc->port[i].tty_dont_flip) {
1567 D6("Enable for port: %d", i);
1568 dc->port[i].tty_dont_flip = 0;
1569 spin_lock_irqsave(&dc->spin_mutex, flags);
1570 enable_transmit_dl(dc->port[i].tty_index, dc);
1571 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1576 static int read_proc_card_type(char *buf, char **start, off_t offset, int len)
1578 dc_t *dc = get_dc_by_index(0);
1581 len += sprintf(buf + len, "%d\n", dc->card_type);
1585 static int read_proc_open_ttys(char *buf, char **start, off_t offset, int len)
1587 dc_t *dc = get_dc_by_index(0);
1590 len += sprintf(buf + len, "%d\n", dc->open_ttys);
1594 static int read_proc_version(char *buf, char **start, off_t offset, int len)
1598 len += sprintf(buf + len, "%s\n", VERSION_STRING);
1602 static int read_proc_rtx(char *buf, char **start, off_t offset, int len)
1604 dc_t *dc = get_dc_by_index(0);
1609 for (i = PORT_MDM; i < MAX_PORT; i++) {
1611 sprintf(buf + len, "noz%d rx: %d, tx: %d\n", i,
1612 dc->port[i].rx_data, dc->port[i].tx_data);
1617 static void make_proc_dirs(void)
1619 dc_t *dc = get_dc_by_index(0);
1620 dc->proc_entry = proc_mkdir("nozomi", &proc_root);
1622 /* Register the read_proc */
1623 if (!create_proc_info_entry
1624 ("card_type", 0, dc->proc_entry, read_proc_card_type)) {
1625 ERR("ERROR: failed to register read_procmem");
1627 if (!create_proc_info_entry
1628 ("open_ttys", 0, dc->proc_entry, read_proc_open_ttys)) {
1629 ERR("ERROR: failed to register read_procmem");
1631 if (!create_proc_info_entry("rtx", 0, dc->proc_entry, read_proc_rtx)) {
1632 ERR("ERROR: failed to register read_procmem");
1634 if (!create_proc_info_entry
1635 ("version", 0, dc->proc_entry, read_proc_version)) {
1636 ERR("ERROR: failed to register read_procmem");
1640 static void remove_proc_dirs(void)
1642 dc_t *dc = get_dc_by_index(0);
1644 remove_proc_entry("card_type", dc->proc_entry);
1645 remove_proc_entry("open_ttys", dc->proc_entry);
1646 remove_proc_entry("rtx", dc->proc_entry);
1647 remove_proc_entry("version", dc->proc_entry);
1648 remove_proc_entry("nozomi", &proc_root);
1651 /* Allocate memory for one device */
1652 static int __devinit nozomi_card_init(struct pci_dev *pdev,
1653 const struct pci_device_id *ent)
1659 dev_info(&pdev->dev, "Init, cards_found: %d\n", cards_found);
1661 if (!(my_dev = kmalloc(sizeof(dc_t), GFP_KERNEL))) {
1662 D1("Could not allocate memory");
1666 memset(my_dev, 0, sizeof(dc_t));
1668 if (cards_found > 1) {
1669 dev_err(&pdev->dev, "This driver only supports 1 device\n");
1673 my_dev->pdev = pdev;
1676 /* Find out what card type it is */
1677 nozomi_get_card_type(dc);
1679 if (pci_enable_device(dc->pdev)) {
1680 dev_err(&pdev->dev, "Not possible to enable PCI Device\n");
1683 dc->base_addr = (void *)pci_resource_start(dc->pdev, 0);
1684 if (dc->base_addr == 0x0000) {
1685 dev_err(&pdev->dev, "No I/O-Address for card detected\n");
1687 goto err_disable_device;
1689 dc->base_addr = (void *)ioremap((int)dc->base_addr, dc->card_type);
1690 if (!dc->base_addr) {
1691 dev_err(&pdev->dev, "No I/O-Address for card detected\n");
1693 goto err_disable_device;
1698 nozomi_setup_private_data(dc);
1700 if (pci_request_regions(dc->pdev, NOZOMI_NAME)) {
1701 dev_err(&pdev->dev, "I/O address 0x%04x already in use\n",
1702 (int) /* nozomi_private.io_addr */ 0);
1704 goto err_disable_regions;
1707 dc->send_buf = kmalloc(SEND_BUF_MAX, GFP_KERNEL);
1708 if (!dc->send_buf) {
1709 dev_err(&pdev->dev, "Could not allocate send buffer?\n");
1710 goto err_disable_regions;
1713 /* Disable all interrupts */
1716 /* Setup interrupt handler */
1717 if (nozomi_setup_interrupt(dc->pdev)) {
1719 goto err_disable_regions;
1722 D1("base_addr: 0x%08X", dc->base_addr);
1724 dc->tty_flip_queue.list.next = NULL;
1725 dc->tty_flip_queue.list.prev = NULL;
1726 dc->tty_flip_queue.sync = 0;
1727 dc->tty_flip_queue.data = dc;
1728 dc->tty_flip_queue.routine = tty_flip_queue_function;
1731 if (!(dc->tty_flip_wq = create_singlethread_workqueue(NOZOMI_NAME))) {
1732 ERR("Could not create workqueue?");
1733 BUG_ON(!dc->tty_flip_wq);
1736 INIT_WORK(&dc->tty_flip_wq_struct, tty_flip_queue_function, dc);
1738 spin_lock_init(&dc->spin_mutex);
1741 /* Enable RESET interrupt. */
1742 SET_IER(RESET, 0xFFFF);
1745 err_disable_regions:
1746 pci_release_regions(pdev);
1747 iounmap(dc->base_addr);
1748 dc->base_addr = NULL;
1751 pci_disable_device(pdev);
1756 static void tty_do_close(dc_t * dc, struct port *port)
1759 unsigned long flags;
1761 if (down_interruptible(&port->tty_sem)) {
1764 if (port->tty_open_count) {
1766 port->tty_open_count--;
1770 if (port->tty_open_count == 0) {
1771 D1("close: %d", port->token_dl);
1772 spin_lock_irqsave(&dc->spin_mutex, flags);
1773 SET_IER(0, port->token_dl);
1774 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1780 static void __devexit tty_exit(void)
1786 for (i = 0; i < NTTY_TTY_MINORS; i++) {
1787 while (dc->port[i].tty_open_count)
1788 tty_do_close(dc, &dc->port[i]);
1789 dc->port[i].tty = NULL;
1792 for (i = 0; i < NTTY_TTY_MINORS; ++i){
1793 D1("Unregistering device");
1794 tty_unregister_device(dc->tty_driver, i);
1797 D1("Unregistering driver");
1798 tty_unregister_driver(dc->tty_driver);
1799 put_tty_driver(dc->tty_driver);
1802 /* Deallocate memory for one device */
1803 static void __devexit nozomi_card_exit(struct pci_dev *pdev)
1807 dc_t *dc = get_dc_by_pdev(pdev);
1809 /* Disable all interrupts */
1812 /* Send 0x0001, command card to resend the reset token. */
1813 /* This is to get the reset when the module is reloaded. */
1818 D1("sending flow control 0x%04X", *((u16 *) & ctrl));
1820 /* Setup dc->reg addresses to we can use defines here */
1821 nozomi_setup_private_data(dc);
1822 SET_MEM(dc->port[PORT_CTRL].ul_addr[0], (u32 *) & ctrl, 2);
1823 SET_FCR(CTRL_UL); /* push the token to the card. */
1825 D1("pci_release_regions");
1826 pci_release_regions(pdev);
1829 iounmap(dc->base_addr);
1831 D1("pci_disable_device");
1832 pci_disable_device(pdev);
1834 free_irq(pdev->irq, pdev);
1836 for (i = PORT_MDM; i < MAX_PORT; i++) {
1837 kfree(dc->port[i].fifo_ul);
1840 kfree(dc->send_buf);
1847 destroy_workqueue(dc->tty_flip_wq);
1856 static void set_rts(int index, int rts)
1858 dc_t *dc = get_dc_by_index(index);
1859 dc->port[index].ctrl_ul.RTS = rts;
1860 dc->port[index].update_flow_control = 1;
1861 enable_transmit_ul(PORT_CTRL, dc);
1864 static void set_dtr(int index, int dtr)
1866 dc_t *dc = get_dc_by_index(index);
1867 D1("SETTING DTR index: %d, dtr: %d", index, dtr);
1868 dc->port[index].ctrl_ul.DTR = dtr;
1869 dc->port[index].update_flow_control = 1;
1870 enable_transmit_ul(PORT_CTRL, dc);
1873 /* ---------------------------------------------------------------------------------------------------
1875 ---------------------------------------------------------------------------------------------------*/
1877 /* Called when the userspace process opens the tty, /dev/noz*. */
1878 static int ntty_open(struct tty_struct *tty, struct file *file)
1881 s32 index = get_index(tty);
1882 struct port *port = get_port_by_tty(tty);
1883 dc_t *dc = get_dc_by_tty(tty);
1884 unsigned long flags;
1886 if (down_interruptible(&port->tty_sem)) {
1887 return -ERESTARTSYS;
1890 tty->low_latency = 1;
1891 tty->driver_data = port;
1893 port->tty_index = index;
1894 port->tty_open_count++;
1899 /* Enable interrupt downlink for channel */
1900 if (port->tty_open_count == 1) {
1901 port->rx_data = port->tx_data = 0;
1902 D1("open: %d", port->token_dl);
1903 spin_lock_irqsave(&dc->spin_mutex, flags);
1904 SET_IER(port->token_dl, port->token_dl);
1905 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1912 /* Called when the userspace process close the tty, /dev/noz*. */
1913 static void ntty_close(struct tty_struct *tty, struct file *file)
1915 dc_t *dc = get_dc_by_tty(tty);
1916 tty_do_close(dc, (struct port *)tty->driver_data);
1919 /* called when the userspace process writes to the tty (/dev/noz*). */
1920 /* Data is inserted into a fifo, which is then read and transfered to the modem. */
1922 static int ntty_write(struct tty_struct *tty, const u8 *buffer,
1926 static s32 ntty_write(struct tty_struct *tty, s32 from_user, const u8 * buffer,
1931 dc_t *dc = get_dc_by_tty(tty);
1932 struct port *port = (struct port *)tty->driver_data;
1933 unsigned long flags;
1935 /* D1( "WRITEx: %d, index = %d", count, index); */
1941 if (down_trylock(&port->tty_sem)) { // must test lock as tty layer wraps calls to this function with BKL
1942 ERR("Would have deadlocked - return ERESTARTSYS");
1943 return -ERESTARTSYS;
1946 if (!port->tty_open_count) {
1953 __kfifo_put_user(port->fifo_ul, (unsigned char *)buffer,
1957 __kfifo_put(port->fifo_ul, (unsigned char *)buffer, count);
1960 rval = __kfifo_put(port->fifo_ul, (unsigned char *)buffer, count);
1965 D1("No device context?");
1969 spin_lock_irqsave(&dc->spin_mutex, flags);
1970 // CTS is only valid on the modem channel
1971 if (port == &(dc->port[PORT_MDM])) {
1972 if (port->ctrl_dl.CTS) {
1973 D4("Enable interrupt");
1974 enable_transmit_ul(port->tty_index, dc);
1976 ERR("CTS not active on modem port?");
1979 enable_transmit_ul(port->tty_index, dc);
1981 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1988 /* Calculate how much is left in device */
1989 /* This method is called by the upper tty layer. */
1990 /* #according to sources N_TTY.c it expects a value >= 0 and does not check for negative values. */
1991 static int ntty_write_room(struct tty_struct *tty)
1993 struct port *port = (struct port *)tty->driver_data;
1998 if (down_trylock(&port->tty_sem)) {
2001 if (port->tty_open_count) {
2002 room = port->fifo_ul->size - __kfifo_len(port->fifo_ul);
2008 /* Sets termios flags, called by the tty layer. */
2009 static void ntty_set_termios(struct tty_struct *tty,
2010 struct termios *old_termios)
2014 cflag = tty->termios->c_cflag;
2017 if ((cflag == old_termios->c_cflag) &&
2018 (RELEVANT_IFLAG(tty->termios->c_iflag) ==
2019 RELEVANT_IFLAG(old_termios->c_iflag))) {
2020 D1(" - nothing to change...");
2025 /* get the byte size */
2026 switch (cflag & CSIZE) {
2028 D1(" - data bits = 5");
2031 D1(" - data bits = 6");
2034 D1(" - data bits = 7");
2038 D1(" - data bits = 8");
2042 /* determine the parity */
2043 if (cflag & PARENB) {
2044 if (cflag & PARODD) {
2045 D1(" - parity = odd");
2047 D1(" - parity = even");
2050 D1(" - parity = none");
2053 /* figure out the stop bits requested */
2054 if (cflag & CSTOPB) {
2055 D1(" - stop bits = 2");
2057 D1(" - stop bits = 1");
2060 /* figure out the hardware flow control settings */
2061 if (cflag & CRTSCTS) {
2062 D1(" - RTS/CTS is enabled");
2064 D1(" - RTS/CTS is disabled");
2067 /* determine software flow control */
2068 /* if we are implementing XON/XOFF, set the start and
2069 * stop character in the device */
2070 if (I_IXOFF(tty) || I_IXON(tty)) {
2072 unsigned char stop_char = STOP_CHAR(tty);
2073 unsigned char start_char = START_CHAR(tty);
2075 /* if we are implementing INBOUND XON/XOFF */
2077 D1(" - INBOUND XON/XOFF is enabled, "
2078 "XON = %2x, XOFF = %2x", start_char, stop_char);
2080 D1(" - INBOUND XON/XOFF is disabled");
2083 /* if we are implementing OUTBOUND XON/XOFF */
2085 D1(" - OUTBOUND XON/XOFF is enabled, "
2086 "XON = %2x, XOFF = %2x", start_char, stop_char);
2088 D1(" - OUTBOUND XON/XOFF is disabled");
2096 /* Gets io control parameters */
2097 static int ntty_tiocmget(struct tty_struct *tty, struct file *file)
2099 struct port *port = tty->driver_data;
2100 ctrl_dl_t *ctrl_dl = &port->ctrl_dl;
2101 ctrl_ul_t *ctrl_ul = &port->ctrl_ul;
2103 return 0 | (ctrl_ul->RTS ? TIOCM_RTS : 0)
2104 | (ctrl_ul->DTR ? TIOCM_DTR : 0)
2105 | (ctrl_dl->DCD ? TIOCM_CAR : 0)
2106 | (ctrl_dl->RI ? TIOCM_RNG : 0)
2107 | (ctrl_dl->DSR ? TIOCM_DSR : 0)
2108 | (ctrl_dl->CTS ? TIOCM_CTS : 0);
2111 /* Sets io controls parameters */
2112 static int ntty_tiocmset(struct tty_struct *tty, struct file *file, u32 arg)
2114 struct port *port = (struct port *)tty->driver_data;
2116 set_rts(port->tty_index, (arg & TIOCM_RTS) ? 1 : 0);
2117 set_dtr(port->tty_index, (arg & TIOCM_DTR) ? 1 : 0);
2122 static int ntty_ioctl_tiocmiwait(struct tty_struct *tty, struct file *file,
2123 unsigned int cmd, unsigned long arg)
2125 struct port *port = (struct port *)tty->driver_data;
2127 if (cmd == TIOCMIWAIT) {
2128 DECLARE_WAITQUEUE(wait, current);
2129 struct async_icount cnow;
2130 struct async_icount cprev;
2132 cprev = port->tty_icount;
2134 add_wait_queue(&port->tty_wait, &wait);
2135 set_current_state(TASK_INTERRUPTIBLE);
2137 remove_wait_queue(&port->tty_wait, &wait);
2139 /* see if a signal woke us up */
2140 if (signal_pending(current))
2141 return -ERESTARTSYS;
2143 cnow = port->tty_icount;
2144 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
2145 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
2146 return -EIO; /* no change => error */
2147 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
2148 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
2149 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
2150 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
2157 return -ENOIOCTLCMD;
2160 static int ntty_ioctl_tiocgicount(struct tty_struct *tty, struct file *file,
2161 unsigned int cmd, void __user * arg)
2163 struct port *port = (struct port *)tty->driver_data;
2165 if (cmd == TIOCGICOUNT) {
2166 struct async_icount cnow = port->tty_icount;
2167 struct serial_icounter_struct icount;
2169 icount.cts = cnow.cts;
2170 icount.dsr = cnow.dsr;
2171 icount.rng = cnow.rng;
2172 icount.dcd = cnow.dcd;
2173 icount.rx = cnow.rx;
2174 icount.tx = cnow.tx;
2175 icount.frame = cnow.frame;
2176 icount.overrun = cnow.overrun;
2177 icount.parity = cnow.parity;
2178 icount.brk = cnow.brk;
2179 icount.buf_overrun = cnow.buf_overrun;
2181 if (copy_to_user(arg, &icount, sizeof(icount)))
2185 return -ENOIOCTLCMD;
2188 static int ntty_ioctl(struct tty_struct *tty, struct file *file,
2189 unsigned int cmd, unsigned long arg)
2191 struct port *port = tty->driver_data;
2192 dc_t *dc = get_dc_by_tty(tty);
2193 unsigned long flags;
2195 int rval = -ENOIOCTLCMD;
2197 D1("******** IOCTL, cmd: %d", cmd);
2201 D1("IOCTL TCGETS ...");
2202 rval = -ENOIOCTLCMD;
2205 D1("IOCTL TCSETS ...");
2206 rval = -ENOIOCTLCMD;
2209 rval = ntty_ioctl_tiocmiwait(tty, file, cmd, arg);
2213 ntty_ioctl_tiocgicount(tty, file, cmd, (void __user *)arg);
2216 spin_lock_irqsave(&dc->spin_mutex, flags);
2217 rval = ntty_tiocmget(tty, file);
2218 spin_unlock_irqrestore(&dc->spin_mutex, flags);
2221 rval = ntty_tiocmset(tty, file, arg);
2224 if (get_user(mask, (unsigned long __user *)arg))
2227 spin_lock_irqsave(&dc->spin_mutex, flags);
2228 if (mask & TIOCM_RTS)
2229 set_rts(port->tty_index, 0);
2230 if (mask & TIOCM_DTR)
2231 set_dtr(port->tty_index, 0);
2232 spin_unlock_irqrestore(&dc->spin_mutex, flags);
2236 if (get_user(mask, (unsigned long __user *)arg))
2239 spin_lock_irqsave(&dc->spin_mutex, flags);
2240 if (mask & TIOCM_RTS)
2241 set_rts(port->tty_index, 1);
2242 if (mask & TIOCM_DTR)
2243 set_dtr(port->tty_index, 1);
2244 spin_unlock_irqrestore(&dc->spin_mutex, flags);
2248 D1("IOCTL TCFLSH ...");
2249 rval = -ENOIOCTLCMD;
2253 D1("ERR: 0x%08X, %d", cmd, cmd);
2260 /* Called by the upper tty layer when tty buffers are ready */
2261 /* to receive data again after a call to throttle. */
2262 static void ntty_unthrottle(struct tty_struct *tty)
2264 struct port *port = (struct port *)tty->driver_data;
2265 dc_t *dc = get_dc_by_tty(tty);
2266 unsigned long flags;
2269 spin_lock_irqsave(&dc->spin_mutex, flags);
2270 enable_transmit_dl(port->tty_index, dc);
2271 set_rts(port->tty_index, 1);
2273 spin_unlock_irqrestore(&dc->spin_mutex, flags);
2276 /* Called by the upper tty layer when the tty buffers are almost full. */
2277 /* The driver should stop send more data. */
2278 static void ntty_throttle(struct tty_struct *tty)
2280 struct port *port = (struct port *)tty->driver_data;
2281 dc_t *dc = get_dc_by_tty(tty);
2282 unsigned long flags;
2285 spin_lock_irqsave(&dc->spin_mutex, flags);
2286 set_rts(port->tty_index, 0);
2287 spin_unlock_irqrestore(&dc->spin_mutex, flags);
2290 static void ntty_put_char(struct tty_struct *tty, unsigned char c)
2292 D2("PUT CHAR Function: %c", c);
2295 /* Returns number of chars in buffer, called by tty layer */
2296 static s32 ntty_chars_in_buffer(struct tty_struct *tty)
2298 struct port *port = (struct port *)tty->driver_data;
2303 goto exit_in_buffer;
2306 if (!port->tty_open_count) {
2307 ERR("No tty open?");
2309 goto exit_in_buffer;
2312 rval = __kfifo_len(port->fifo_ul);
2318 static struct tty_operations tty_ops = {
2319 .ioctl = ntty_ioctl,
2321 .close = ntty_close,
2322 .write = ntty_write,
2323 .write_room = ntty_write_room,
2324 .unthrottle = ntty_unthrottle,
2325 .throttle = ntty_throttle,
2326 .set_termios = ntty_set_termios,
2327 .chars_in_buffer = ntty_chars_in_buffer,
2328 .put_char = ntty_put_char,
2331 /* Initializes the tty */
2332 static int ntty_tty_init(dc_t * dc)
2334 struct tty_driver *td;
2338 dc->tty_driver = alloc_tty_driver(NTTY_TTY_MINORS);
2339 if (!dc->tty_driver)
2341 td = dc->tty_driver;
2342 td->owner = THIS_MODULE;
2343 td->driver_name = NOZOMI_NAME_TTY;
2345 td->major = NTTY_TTY_MAJOR;
2346 td->type = TTY_DRIVER_TYPE_SERIAL;
2347 td->subtype = SERIAL_TYPE_NORMAL;
2348 #ifdef KERNEL_2_6_18
2349 td->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
2351 td->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS;
2353 td->init_termios = tty_std_termios;
2354 td->init_termios.c_cflag = B115200 | CS8 | CREAD | HUPCL | CLOCAL;
2357 td->table = dc->tty_table;
2358 td->refcount = &dc->tty_refcount;
2361 td->termios = dc->tty_termios;
2362 td->termios_locked = dc->tty_termios_locked;
2363 tty_set_operations(dc->tty_driver, &tty_ops);
2365 rval = tty_register_driver(td);
2367 dev_err(&dc->pdev->dev, "failed to register ntty tty driver\n");
2371 for (i = 0; i < NTTY_TTY_MINORS; i++) {
2372 init_MUTEX(&dc->port[i].tty_sem);
2373 dc->port[i].tty_open_count = 0;
2374 dc->port[i].tty = NULL;
2376 tty_register_device(td, i, &dc->pdev->dev);
2380 dev_info(&dc->pdev->dev, DRIVER_DESC " " NOZOMI_NAME_TTY "\n");
2384 /* Module initialization */
2385 static struct pci_driver nozomi_driver = {
2386 .name = NOZOMI_NAME,
2387 .id_table = nozomi_pci_tbl,
2388 .probe = nozomi_card_init,
2389 .remove = __devexit_p(nozomi_card_exit),
2392 static __init int nozomi_init(void)
2396 rval = pci_module_init(&nozomi_driver);
2397 printk(KERN_INFO "Initializing %s\n", VERSION_STRING);
2401 static __exit void nozomi_exit(void)
2403 printk(KERN_INFO "Unloading %s", DRIVER_DESC);
2404 pci_unregister_driver(&nozomi_driver);
2407 module_init(nozomi_init);
2408 module_exit(nozomi_exit);
2411 MODULE_PARM(nzdebug, "i");
2414 MODULE_LICENSE("Dual BSD/GPL");
2415 MODULE_DESCRIPTION(DRIVER_DESC);