Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / drivers / char / nozomi.c
blobbb8345a93b6aac5a46bf50895e7ea4b4c2ad6311
1 /*
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 * Patches:
11 * Locking code changes for Vodafone by Sphere Systems Ltd,
12 * Andrew Bird (ajb@spheresystems.co.uk )
13 * & Phil Sanderson
15 * Source has been ported from an implementation made by Filip Aben @ Option
17 * --------------------------------------------------------------------------
19 * Copyright (c) 2005,2006 Option Wireless Sweden AB
20 * Copyright (c) 2006 Sphere Systems Ltd
21 * Copyright (c) 2006 Option Wireless n/v
22 * All rights Reserved.
24 * This program is free software; you can redistribute it and/or modify
25 * it under the terms of the GNU General Public License as published by
26 * the Free Software Foundation; either version 2 of the License, or
27 * (at your option) any later version.
29 * This program is distributed in the hope that it will be useful,
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 * GNU General Public License for more details.
34 * You should have received a copy of the GNU General Public License
35 * along with this program; if not, write to the Free Software
36 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
38 * --------------------------------------------------------------------------
41 /* Enable this to have a lot of debug printouts */
42 #define DEBUG
44 #include <linux/kernel.h>
45 #include <linux/module.h>
46 #include <linux/pci.h>
47 #include <linux/ioport.h>
48 #include <linux/tty.h>
49 #include <linux/tty_driver.h>
50 #include <linux/tty_flip.h>
51 #include <linux/serial.h>
52 #include <linux/interrupt.h>
53 #include <linux/kmod.h>
54 #include <linux/init.h>
55 #include <linux/kfifo.h>
56 #include <linux/uaccess.h>
57 #include <asm/byteorder.h>
59 #include <linux/delay.h>
62 #define VERSION_STRING DRIVER_DESC " 2.1d (build date: " \
63 __DATE__ " " __TIME__ ")"
65 /* Macros definitions */
67 /* Default debug printout level */
68 #define NOZOMI_DEBUG_LEVEL 0x00
70 #define P_BUF_SIZE 128
71 #define NFO(_err_flag_, args...) \
72 do { \
73 char tmp[P_BUF_SIZE]; \
74 snprintf(tmp, sizeof(tmp), ##args); \
75 printk(_err_flag_ "[%d] %s(): %s\n", __LINE__, \
76 __FUNCTION__, tmp); \
77 } while (0)
79 #define DBG1(args...) D_(0x01, ##args)
80 #define DBG2(args...) D_(0x02, ##args)
81 #define DBG3(args...) D_(0x04, ##args)
82 #define DBG4(args...) D_(0x08, ##args)
83 #define DBG5(args...) D_(0x10, ##args)
84 #define DBG6(args...) D_(0x20, ##args)
85 #define DBG7(args...) D_(0x40, ##args)
86 #define DBG8(args...) D_(0x80, ##args)
88 #ifdef DEBUG
89 /* Do we need this settable at runtime? */
90 static int debug = NOZOMI_DEBUG_LEVEL;
92 #define D(lvl, args...) do \
93 {if (lvl & debug) NFO(KERN_DEBUG, ##args); } \
94 while (0)
95 #define D_(lvl, args...) D(lvl, ##args)
97 /* These printouts are always printed */
99 #else
100 static int debug;
101 #define D_(lvl, args...)
102 #endif
104 /* TODO: rewrite to optimize macros... */
106 #define TMP_BUF_MAX 256
108 #define DUMP(buf__,len__) \
109 do { \
110 char tbuf[TMP_BUF_MAX] = {0};\
111 if (len__ > 1) {\
112 snprintf(tbuf, len__ > TMP_BUF_MAX ? TMP_BUF_MAX : len__, "%s", buf__);\
113 if (tbuf[len__-2] == '\r') {\
114 tbuf[len__-2] = 'r';\
116 DBG1("SENDING: '%s' (%d+n)", tbuf, len__);\
117 } else {\
118 DBG1("SENDING: '%s' (%d)", tbuf, len__);\
120 } while (0)
122 /* Defines */
123 #define NOZOMI_NAME "nozomi"
124 #define NOZOMI_NAME_TTY "nozomi_tty"
125 #define DRIVER_DESC "Nozomi driver"
127 #define NTTY_TTY_MAXMINORS 256
128 #define NTTY_FIFO_BUFFER_SIZE 8192
130 /* Must be power of 2 */
131 #define FIFO_BUFFER_SIZE_UL 8192
133 /* Size of tmp send buffer to card */
134 #define SEND_BUF_MAX 1024
135 #define RECEIVE_BUF_MAX 4
138 /* Define all types of vendors and devices to support */
139 #define VENDOR1 0x1931 /* Vendor Option */
140 #define DEVICE1 0x000c /* HSDPA card */
142 #define R_IIR 0x0000 /* Interrupt Identity Register */
143 #define R_FCR 0x0000 /* Flow Control Register */
144 #define R_IER 0x0004 /* Interrupt Enable Register */
146 #define CONFIG_MAGIC 0xEFEFFEFE
147 #define TOGGLE_VALID 0x0000
149 /* Definition of interrupt tokens */
150 #define MDM_DL1 0x0001
151 #define MDM_UL1 0x0002
152 #define MDM_DL2 0x0004
153 #define MDM_UL2 0x0008
154 #define DIAG_DL1 0x0010
155 #define DIAG_DL2 0x0020
156 #define DIAG_UL 0x0040
157 #define APP1_DL 0x0080
158 #define APP1_UL 0x0100
159 #define APP2_DL 0x0200
160 #define APP2_UL 0x0400
161 #define CTRL_DL 0x0800
162 #define CTRL_UL 0x1000
163 #define RESET 0x8000
165 #define MDM_DL (MDM_DL1 | MDM_DL2)
166 #define MDM_UL (MDM_UL1 | MDM_UL2)
167 #define DIAG_DL (DIAG_DL1 | DIAG_DL2)
169 /* modem signal definition */
170 #define CTRL_DSR 0x0001
171 #define CTRL_DCD 0x0002
172 #define CTRL_RI 0x0004
173 #define CTRL_CTS 0x0008
175 #define CTRL_DTR 0x0001
176 #define CTRL_RTS 0x0002
178 #define MAX_PORT 4
179 #define NOZOMI_MAX_PORTS 5
180 #define NOZOMI_MAX_CARDS (NTTY_TTY_MAXMINORS / MAX_PORT)
182 /* Type definitions */
185 * There are two types of nozomi cards,
186 * one with 2048 memory and with 8192 memory
188 enum card_type {
189 F32_2 = 2048, /* 512 bytes downlink + uplink * 2 -> 2048 */
190 F32_8 = 8192, /* 3072 bytes downl. + 1024 bytes uplink * 2 -> 8192 */
193 <<<<<<< HEAD:drivers/char/nozomi.c
194 =======
195 /* Initialization states a card can be in */
196 enum card_state {
197 NOZOMI_STATE_UKNOWN = 0,
198 NOZOMI_STATE_ENABLED = 1, /* pci device enabled */
199 NOZOMI_STATE_ALLOCATED = 2, /* config setup done */
200 NOZOMI_STATE_READY = 3, /* flowcontrols received */
203 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/char/nozomi.c
204 /* Two different toggle channels exist */
205 enum channel_type {
206 CH_A = 0,
207 CH_B = 1,
210 /* Port definition for the card regarding flow control */
211 enum ctrl_port_type {
212 CTRL_CMD = 0,
213 CTRL_MDM = 1,
214 CTRL_DIAG = 2,
215 CTRL_APP1 = 3,
216 CTRL_APP2 = 4,
217 CTRL_ERROR = -1,
220 /* Ports that the nozomi has */
221 enum port_type {
222 PORT_MDM = 0,
223 PORT_DIAG = 1,
224 PORT_APP1 = 2,
225 PORT_APP2 = 3,
226 PORT_CTRL = 4,
227 PORT_ERROR = -1,
230 #ifdef __BIG_ENDIAN
231 /* Big endian */
233 struct toggles {
234 unsigned int enabled:5; /*
235 * Toggle fields are valid if enabled is 0,
236 * else A-channels must always be used.
238 unsigned int diag_dl:1;
239 unsigned int mdm_dl:1;
240 unsigned int mdm_ul:1;
241 } __attribute__ ((packed));
243 /* Configuration table to read at startup of card */
244 /* Is for now only needed during initialization phase */
245 struct config_table {
246 u32 signature;
247 u16 product_information;
248 u16 version;
249 u8 pad3[3];
250 struct toggles toggle;
251 u8 pad1[4];
252 u16 dl_mdm_len1; /*
253 * If this is 64, it can hold
254 * 60 bytes + 4 that is length field
256 u16 dl_start;
258 u16 dl_diag_len1;
259 u16 dl_mdm_len2; /*
260 * If this is 64, it can hold
261 * 60 bytes + 4 that is length field
263 u16 dl_app1_len;
265 u16 dl_diag_len2;
266 u16 dl_ctrl_len;
267 u16 dl_app2_len;
268 u8 pad2[16];
269 u16 ul_mdm_len1;
270 u16 ul_start;
271 u16 ul_diag_len;
272 u16 ul_mdm_len2;
273 u16 ul_app1_len;
274 u16 ul_app2_len;
275 u16 ul_ctrl_len;
276 } __attribute__ ((packed));
278 /* This stores all control downlink flags */
279 struct ctrl_dl {
280 u8 port;
281 unsigned int reserved:4;
282 unsigned int CTS:1;
283 unsigned int RI:1;
284 unsigned int DCD:1;
285 unsigned int DSR:1;
286 } __attribute__ ((packed));
288 /* This stores all control uplink flags */
289 struct ctrl_ul {
290 u8 port;
291 unsigned int reserved:6;
292 unsigned int RTS:1;
293 unsigned int DTR:1;
294 } __attribute__ ((packed));
296 #else
297 /* Little endian */
299 /* This represents the toggle information */
300 struct toggles {
301 unsigned int mdm_ul:1;
302 unsigned int mdm_dl:1;
303 unsigned int diag_dl:1;
304 unsigned int enabled:5; /*
305 * Toggle fields are valid if enabled is 0,
306 * else A-channels must always be used.
308 } __attribute__ ((packed));
310 /* Configuration table to read at startup of card */
311 struct config_table {
312 u32 signature;
313 u16 version;
314 u16 product_information;
315 struct toggles toggle;
316 u8 pad1[7];
317 u16 dl_start;
318 u16 dl_mdm_len1; /*
319 * If this is 64, it can hold
320 * 60 bytes + 4 that is length field
322 u16 dl_mdm_len2;
323 u16 dl_diag_len1;
324 u16 dl_diag_len2;
325 u16 dl_app1_len;
326 u16 dl_app2_len;
327 u16 dl_ctrl_len;
328 u8 pad2[16];
329 u16 ul_start;
330 u16 ul_mdm_len2;
331 u16 ul_mdm_len1;
332 u16 ul_diag_len;
333 u16 ul_app1_len;
334 u16 ul_app2_len;
335 u16 ul_ctrl_len;
336 } __attribute__ ((packed));
338 /* This stores all control downlink flags */
339 struct ctrl_dl {
340 unsigned int DSR:1;
341 unsigned int DCD:1;
342 unsigned int RI:1;
343 unsigned int CTS:1;
344 unsigned int reserverd:4;
345 u8 port;
346 } __attribute__ ((packed));
348 /* This stores all control uplink flags */
349 struct ctrl_ul {
350 unsigned int DTR:1;
351 unsigned int RTS:1;
352 unsigned int reserved:6;
353 u8 port;
354 } __attribute__ ((packed));
355 #endif
357 /* This holds all information that is needed regarding a port */
358 struct port {
359 u8 update_flow_control;
360 struct ctrl_ul ctrl_ul;
361 struct ctrl_dl ctrl_dl;
362 struct kfifo *fifo_ul;
363 void __iomem *dl_addr[2];
364 u32 dl_size[2];
365 u8 toggle_dl;
366 void __iomem *ul_addr[2];
367 u32 ul_size[2];
368 u8 toggle_ul;
369 u16 token_dl;
371 struct tty_struct *tty;
372 int tty_open_count;
373 /* mutex to ensure one access patch to this port */
374 struct mutex tty_sem;
375 wait_queue_head_t tty_wait;
376 struct async_icount tty_icount;
379 /* Private data one for each card in the system */
380 struct nozomi {
381 void __iomem *base_addr;
382 unsigned long flip;
384 /* Pointers to registers */
385 void __iomem *reg_iir;
386 void __iomem *reg_fcr;
387 void __iomem *reg_ier;
389 u16 last_ier;
390 enum card_type card_type;
391 struct config_table config_table; /* Configuration table */
392 struct pci_dev *pdev;
393 struct port port[NOZOMI_MAX_PORTS];
394 u8 *send_buf;
396 spinlock_t spin_mutex; /* secures access to registers and tty */
398 unsigned int index_start;
399 <<<<<<< HEAD:drivers/char/nozomi.c
400 =======
401 enum card_state state;
402 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/char/nozomi.c
403 u32 open_ttys;
406 /* This is a data packet that is read or written to/from card */
407 struct buffer {
408 u32 size; /* size is the length of the data buffer */
409 u8 *data;
410 } __attribute__ ((packed));
412 /* Global variables */
413 static const struct pci_device_id nozomi_pci_tbl[] __devinitconst = {
414 {PCI_DEVICE(VENDOR1, DEVICE1)},
418 MODULE_DEVICE_TABLE(pci, nozomi_pci_tbl);
420 static struct nozomi *ndevs[NOZOMI_MAX_CARDS];
421 static struct tty_driver *ntty_driver;
424 * find card by tty_index
426 static inline struct nozomi *get_dc_by_tty(const struct tty_struct *tty)
428 return tty ? ndevs[tty->index / MAX_PORT] : NULL;
431 static inline struct port *get_port_by_tty(const struct tty_struct *tty)
433 struct nozomi *ndev = get_dc_by_tty(tty);
434 return ndev ? &ndev->port[tty->index % MAX_PORT] : NULL;
438 * TODO:
439 * -Optimize
440 * -Rewrite cleaner
443 static void read_mem32(u32 *buf, const void __iomem *mem_addr_start,
444 u32 size_bytes)
446 u32 i = 0;
447 const u32 *ptr = (__force u32 *) mem_addr_start;
448 u16 *buf16;
450 if (unlikely(!ptr || !buf))
451 goto out;
453 /* shortcut for extremely often used cases */
454 switch (size_bytes) {
455 case 2: /* 2 bytes */
456 buf16 = (u16 *) buf;
457 *buf16 = __le16_to_cpu(readw((void __iomem *)ptr));
458 goto out;
459 break;
460 case 4: /* 4 bytes */
461 *(buf) = __le32_to_cpu(readl((void __iomem *)ptr));
462 goto out;
463 break;
466 while (i < size_bytes) {
467 if (size_bytes - i == 2) {
468 /* Handle 2 bytes in the end */
469 buf16 = (u16 *) buf;
470 *(buf16) = __le16_to_cpu(readw((void __iomem *)ptr));
471 i += 2;
472 } else {
473 /* Read 4 bytes */
474 *(buf) = __le32_to_cpu(readl((void __iomem *)ptr));
475 i += 4;
477 buf++;
478 ptr++;
480 out:
481 return;
485 * TODO:
486 * -Optimize
487 * -Rewrite cleaner
489 static u32 write_mem32(void __iomem *mem_addr_start, const u32 *buf,
490 u32 size_bytes)
492 u32 i = 0;
493 u32 *ptr = (__force u32 *) mem_addr_start;
494 const u16 *buf16;
496 if (unlikely(!ptr || !buf))
497 return 0;
499 /* shortcut for extremely often used cases */
500 switch (size_bytes) {
501 case 2: /* 2 bytes */
502 buf16 = (const u16 *)buf;
503 writew(__cpu_to_le16(*buf16), (void __iomem *)ptr);
504 return 2;
505 break;
506 case 1: /*
507 * also needs to write 4 bytes in this case
508 * so falling through..
510 case 4: /* 4 bytes */
511 writel(__cpu_to_le32(*buf), (void __iomem *)ptr);
512 return 4;
513 break;
516 while (i < size_bytes) {
517 if (size_bytes - i == 2) {
518 /* 2 bytes */
519 buf16 = (const u16 *)buf;
520 writew(__cpu_to_le16(*buf16), (void __iomem *)ptr);
521 i += 2;
522 } else {
523 /* 4 bytes */
524 writel(__cpu_to_le32(*buf), (void __iomem *)ptr);
525 i += 4;
527 buf++;
528 ptr++;
530 return i;
533 /* Setup pointers to different channels and also setup buffer sizes. */
534 static void setup_memory(struct nozomi *dc)
536 void __iomem *offset = dc->base_addr + dc->config_table.dl_start;
537 /* The length reported is including the length field of 4 bytes,
538 * hence subtract with 4.
540 const u16 buff_offset = 4;
542 /* Modem port dl configuration */
543 dc->port[PORT_MDM].dl_addr[CH_A] = offset;
544 dc->port[PORT_MDM].dl_addr[CH_B] =
545 (offset += dc->config_table.dl_mdm_len1);
546 dc->port[PORT_MDM].dl_size[CH_A] =
547 dc->config_table.dl_mdm_len1 - buff_offset;
548 dc->port[PORT_MDM].dl_size[CH_B] =
549 dc->config_table.dl_mdm_len2 - buff_offset;
551 /* Diag port dl configuration */
552 dc->port[PORT_DIAG].dl_addr[CH_A] =
553 (offset += dc->config_table.dl_mdm_len2);
554 dc->port[PORT_DIAG].dl_size[CH_A] =
555 dc->config_table.dl_diag_len1 - buff_offset;
556 dc->port[PORT_DIAG].dl_addr[CH_B] =
557 (offset += dc->config_table.dl_diag_len1);
558 dc->port[PORT_DIAG].dl_size[CH_B] =
559 dc->config_table.dl_diag_len2 - buff_offset;
561 /* App1 port dl configuration */
562 dc->port[PORT_APP1].dl_addr[CH_A] =
563 (offset += dc->config_table.dl_diag_len2);
564 dc->port[PORT_APP1].dl_size[CH_A] =
565 dc->config_table.dl_app1_len - buff_offset;
567 /* App2 port dl configuration */
568 dc->port[PORT_APP2].dl_addr[CH_A] =
569 (offset += dc->config_table.dl_app1_len);
570 dc->port[PORT_APP2].dl_size[CH_A] =
571 dc->config_table.dl_app2_len - buff_offset;
573 /* Ctrl dl configuration */
574 dc->port[PORT_CTRL].dl_addr[CH_A] =
575 (offset += dc->config_table.dl_app2_len);
576 dc->port[PORT_CTRL].dl_size[CH_A] =
577 dc->config_table.dl_ctrl_len - buff_offset;
579 offset = dc->base_addr + dc->config_table.ul_start;
581 /* Modem Port ul configuration */
582 dc->port[PORT_MDM].ul_addr[CH_A] = offset;
583 dc->port[PORT_MDM].ul_size[CH_A] =
584 dc->config_table.ul_mdm_len1 - buff_offset;
585 dc->port[PORT_MDM].ul_addr[CH_B] =
586 (offset += dc->config_table.ul_mdm_len1);
587 dc->port[PORT_MDM].ul_size[CH_B] =
588 dc->config_table.ul_mdm_len2 - buff_offset;
590 /* Diag port ul configuration */
591 dc->port[PORT_DIAG].ul_addr[CH_A] =
592 (offset += dc->config_table.ul_mdm_len2);
593 dc->port[PORT_DIAG].ul_size[CH_A] =
594 dc->config_table.ul_diag_len - buff_offset;
596 /* App1 port ul configuration */
597 dc->port[PORT_APP1].ul_addr[CH_A] =
598 (offset += dc->config_table.ul_diag_len);
599 dc->port[PORT_APP1].ul_size[CH_A] =
600 dc->config_table.ul_app1_len - buff_offset;
602 /* App2 port ul configuration */
603 dc->port[PORT_APP2].ul_addr[CH_A] =
604 (offset += dc->config_table.ul_app1_len);
605 dc->port[PORT_APP2].ul_size[CH_A] =
606 dc->config_table.ul_app2_len - buff_offset;
608 /* Ctrl ul configuration */
609 dc->port[PORT_CTRL].ul_addr[CH_A] =
610 (offset += dc->config_table.ul_app2_len);
611 dc->port[PORT_CTRL].ul_size[CH_A] =
612 dc->config_table.ul_ctrl_len - buff_offset;
615 /* Dump config table under initalization phase */
616 #ifdef DEBUG
617 static void dump_table(const struct nozomi *dc)
619 DBG3("signature: 0x%08X", dc->config_table.signature);
620 DBG3("version: 0x%04X", dc->config_table.version);
621 DBG3("product_information: 0x%04X", \
622 dc->config_table.product_information);
623 DBG3("toggle enabled: %d", dc->config_table.toggle.enabled);
624 DBG3("toggle up_mdm: %d", dc->config_table.toggle.mdm_ul);
625 DBG3("toggle dl_mdm: %d", dc->config_table.toggle.mdm_dl);
626 DBG3("toggle dl_dbg: %d", dc->config_table.toggle.diag_dl);
628 DBG3("dl_start: 0x%04X", dc->config_table.dl_start);
629 DBG3("dl_mdm_len0: 0x%04X, %d", dc->config_table.dl_mdm_len1,
630 dc->config_table.dl_mdm_len1);
631 DBG3("dl_mdm_len1: 0x%04X, %d", dc->config_table.dl_mdm_len2,
632 dc->config_table.dl_mdm_len2);
633 DBG3("dl_diag_len0: 0x%04X, %d", dc->config_table.dl_diag_len1,
634 dc->config_table.dl_diag_len1);
635 DBG3("dl_diag_len1: 0x%04X, %d", dc->config_table.dl_diag_len2,
636 dc->config_table.dl_diag_len2);
637 DBG3("dl_app1_len: 0x%04X, %d", dc->config_table.dl_app1_len,
638 dc->config_table.dl_app1_len);
639 DBG3("dl_app2_len: 0x%04X, %d", dc->config_table.dl_app2_len,
640 dc->config_table.dl_app2_len);
641 DBG3("dl_ctrl_len: 0x%04X, %d", dc->config_table.dl_ctrl_len,
642 dc->config_table.dl_ctrl_len);
643 DBG3("ul_start: 0x%04X, %d", dc->config_table.ul_start,
644 dc->config_table.ul_start);
645 DBG3("ul_mdm_len[0]: 0x%04X, %d", dc->config_table.ul_mdm_len1,
646 dc->config_table.ul_mdm_len1);
647 DBG3("ul_mdm_len[1]: 0x%04X, %d", dc->config_table.ul_mdm_len2,
648 dc->config_table.ul_mdm_len2);
649 DBG3("ul_diag_len: 0x%04X, %d", dc->config_table.ul_diag_len,
650 dc->config_table.ul_diag_len);
651 DBG3("ul_app1_len: 0x%04X, %d", dc->config_table.ul_app1_len,
652 dc->config_table.ul_app1_len);
653 DBG3("ul_app2_len: 0x%04X, %d", dc->config_table.ul_app2_len,
654 dc->config_table.ul_app2_len);
655 DBG3("ul_ctrl_len: 0x%04X, %d", dc->config_table.ul_ctrl_len,
656 dc->config_table.ul_ctrl_len);
658 #else
659 static inline void dump_table(const struct nozomi *dc) { }
660 #endif
663 * Read configuration table from card under intalization phase
664 * Returns 1 if ok, else 0
666 static int nozomi_read_config_table(struct nozomi *dc)
668 read_mem32((u32 *) &dc->config_table, dc->base_addr + 0,
669 sizeof(struct config_table));
671 if (dc->config_table.signature != CONFIG_MAGIC) {
672 dev_err(&dc->pdev->dev, "ConfigTable Bad! 0x%08X != 0x%08X\n",
673 dc->config_table.signature, CONFIG_MAGIC);
674 return 0;
677 if ((dc->config_table.version == 0)
678 || (dc->config_table.toggle.enabled == TOGGLE_VALID)) {
679 int i;
680 DBG1("Second phase, configuring card");
682 setup_memory(dc);
684 dc->port[PORT_MDM].toggle_ul = dc->config_table.toggle.mdm_ul;
685 dc->port[PORT_MDM].toggle_dl = dc->config_table.toggle.mdm_dl;
686 dc->port[PORT_DIAG].toggle_dl = dc->config_table.toggle.diag_dl;
687 DBG1("toggle ports: MDM UL:%d MDM DL:%d, DIAG DL:%d",
688 dc->port[PORT_MDM].toggle_ul,
689 dc->port[PORT_MDM].toggle_dl, dc->port[PORT_DIAG].toggle_dl);
691 dump_table(dc);
693 for (i = PORT_MDM; i < MAX_PORT; i++) {
694 dc->port[i].fifo_ul =
695 kfifo_alloc(FIFO_BUFFER_SIZE_UL, GFP_ATOMIC, NULL);
696 memset(&dc->port[i].ctrl_dl, 0, sizeof(struct ctrl_dl));
697 memset(&dc->port[i].ctrl_ul, 0, sizeof(struct ctrl_ul));
700 /* Enable control channel */
701 dc->last_ier = dc->last_ier | CTRL_DL;
702 writew(dc->last_ier, dc->reg_ier);
704 <<<<<<< HEAD:drivers/char/nozomi.c
705 =======
706 dc->state = NOZOMI_STATE_ALLOCATED;
707 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/char/nozomi.c
708 dev_info(&dc->pdev->dev, "Initialization OK!\n");
709 return 1;
712 if ((dc->config_table.version > 0)
713 && (dc->config_table.toggle.enabled != TOGGLE_VALID)) {
714 u32 offset = 0;
715 DBG1("First phase: pushing upload buffers, clearing download");
717 dev_info(&dc->pdev->dev, "Version of card: %d\n",
718 dc->config_table.version);
720 /* Here we should disable all I/O over F32. */
721 setup_memory(dc);
724 * We should send ALL channel pair tokens back along
725 * with reset token
728 /* push upload modem buffers */
729 write_mem32(dc->port[PORT_MDM].ul_addr[CH_A],
730 (u32 *) &offset, 4);
731 write_mem32(dc->port[PORT_MDM].ul_addr[CH_B],
732 (u32 *) &offset, 4);
734 writew(MDM_UL | DIAG_DL | MDM_DL, dc->reg_fcr);
736 DBG1("First phase done");
739 return 1;
742 /* Enable uplink interrupts */
743 static void enable_transmit_ul(enum port_type port, struct nozomi *dc)
745 static const u16 mask[] = {MDM_UL, DIAG_UL, APP1_UL, APP2_UL, CTRL_UL};
747 if (port < NOZOMI_MAX_PORTS) {
748 dc->last_ier |= mask[port];
749 writew(dc->last_ier, dc->reg_ier);
750 } else {
751 dev_err(&dc->pdev->dev, "Called with wrong port?\n");
755 /* Disable uplink interrupts */
756 static void disable_transmit_ul(enum port_type port, struct nozomi *dc)
758 static const u16 mask[] =
759 {~MDM_UL, ~DIAG_UL, ~APP1_UL, ~APP2_UL, ~CTRL_UL};
761 if (port < NOZOMI_MAX_PORTS) {
762 dc->last_ier &= mask[port];
763 writew(dc->last_ier, dc->reg_ier);
764 } else {
765 dev_err(&dc->pdev->dev, "Called with wrong port?\n");
769 /* Enable downlink interrupts */
770 static void enable_transmit_dl(enum port_type port, struct nozomi *dc)
772 static const u16 mask[] = {MDM_DL, DIAG_DL, APP1_DL, APP2_DL, CTRL_DL};
774 if (port < NOZOMI_MAX_PORTS) {
775 dc->last_ier |= mask[port];
776 writew(dc->last_ier, dc->reg_ier);
777 } else {
778 dev_err(&dc->pdev->dev, "Called with wrong port?\n");
782 /* Disable downlink interrupts */
783 static void disable_transmit_dl(enum port_type port, struct nozomi *dc)
785 static const u16 mask[] =
786 {~MDM_DL, ~DIAG_DL, ~APP1_DL, ~APP2_DL, ~CTRL_DL};
788 if (port < NOZOMI_MAX_PORTS) {
789 dc->last_ier &= mask[port];
790 writew(dc->last_ier, dc->reg_ier);
791 } else {
792 dev_err(&dc->pdev->dev, "Called with wrong port?\n");
797 * Return 1 - send buffer to card and ack.
798 * Return 0 - don't ack, don't send buffer to card.
800 static int send_data(enum port_type index, const struct nozomi *dc)
802 u32 size = 0;
803 const struct port *port = &dc->port[index];
804 const u8 toggle = port->toggle_ul;
805 void __iomem *addr = port->ul_addr[toggle];
806 const u32 ul_size = port->ul_size[toggle];
807 struct tty_struct *tty = port->tty;
809 /* Get data from tty and place in buf for now */
810 size = __kfifo_get(port->fifo_ul, dc->send_buf,
811 ul_size < SEND_BUF_MAX ? ul_size : SEND_BUF_MAX);
813 if (size == 0) {
814 DBG4("No more data to send, disable link:");
815 return 0;
818 /* DUMP(buf, size); */
820 /* Write length + data */
821 write_mem32(addr, (u32 *) &size, 4);
822 write_mem32(addr + 4, (u32 *) dc->send_buf, size);
824 if (tty)
825 tty_wakeup(tty);
827 return 1;
830 /* If all data has been read, return 1, else 0 */
831 static int receive_data(enum port_type index, struct nozomi *dc)
833 u8 buf[RECEIVE_BUF_MAX] = { 0 };
834 int size;
835 u32 offset = 4;
836 struct port *port = &dc->port[index];
837 void __iomem *addr = port->dl_addr[port->toggle_dl];
838 struct tty_struct *tty = port->tty;
839 int i;
841 if (unlikely(!tty)) {
842 DBG1("tty not open for port: %d?", index);
843 return 1;
846 read_mem32((u32 *) &size, addr, 4);
847 /* DBG1( "%d bytes port: %d", size, index); */
849 if (test_bit(TTY_THROTTLED, &tty->flags)) {
850 DBG1("No room in tty, don't read data, don't ack interrupt, "
851 "disable interrupt");
853 /* disable interrupt in downlink... */
854 disable_transmit_dl(index, dc);
855 return 0;
858 if (unlikely(size == 0)) {
859 dev_err(&dc->pdev->dev, "size == 0?\n");
860 return 1;
863 tty_buffer_request_room(tty, size);
865 while (size > 0) {
866 read_mem32((u32 *) buf, addr + offset, RECEIVE_BUF_MAX);
868 if (size == 1) {
869 tty_insert_flip_char(tty, buf[0], TTY_NORMAL);
870 size = 0;
871 } else if (size < RECEIVE_BUF_MAX) {
872 size -= tty_insert_flip_string(tty, (char *) buf, size);
873 } else {
874 i = tty_insert_flip_string(tty, \
875 (char *) buf, RECEIVE_BUF_MAX);
876 size -= i;
877 offset += i;
881 set_bit(index, &dc->flip);
883 return 1;
886 /* Debug for interrupts */
887 #ifdef DEBUG
888 static char *interrupt2str(u16 interrupt)
890 static char buf[TMP_BUF_MAX];
891 char *p = buf;
893 interrupt & MDM_DL1 ? p += snprintf(p, TMP_BUF_MAX, "MDM_DL1 ") : NULL;
894 interrupt & MDM_DL2 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
895 "MDM_DL2 ") : NULL;
897 interrupt & MDM_UL1 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
898 "MDM_UL1 ") : NULL;
899 interrupt & MDM_UL2 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
900 "MDM_UL2 ") : NULL;
902 interrupt & DIAG_DL1 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
903 "DIAG_DL1 ") : NULL;
904 interrupt & DIAG_DL2 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
905 "DIAG_DL2 ") : NULL;
907 interrupt & DIAG_UL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
908 "DIAG_UL ") : NULL;
910 interrupt & APP1_DL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
911 "APP1_DL ") : NULL;
912 interrupt & APP2_DL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
913 "APP2_DL ") : NULL;
915 interrupt & APP1_UL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
916 "APP1_UL ") : NULL;
917 interrupt & APP2_UL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
918 "APP2_UL ") : NULL;
920 interrupt & CTRL_DL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
921 "CTRL_DL ") : NULL;
922 interrupt & CTRL_UL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
923 "CTRL_UL ") : NULL;
925 interrupt & RESET ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
926 "RESET ") : NULL;
928 return buf;
930 #endif
933 * Receive flow control
934 * Return 1 - If ok, else 0
936 static int receive_flow_control(struct nozomi *dc)
938 enum port_type port = PORT_MDM;
939 struct ctrl_dl ctrl_dl;
940 struct ctrl_dl old_ctrl;
941 u16 enable_ier = 0;
943 read_mem32((u32 *) &ctrl_dl, dc->port[PORT_CTRL].dl_addr[CH_A], 2);
945 switch (ctrl_dl.port) {
946 case CTRL_CMD:
947 DBG1("The Base Band sends this value as a response to a "
948 "request for IMSI detach sent over the control "
949 "channel uplink (see section 7.6.1).");
950 break;
951 case CTRL_MDM:
952 port = PORT_MDM;
953 enable_ier = MDM_DL;
954 break;
955 case CTRL_DIAG:
956 port = PORT_DIAG;
957 enable_ier = DIAG_DL;
958 break;
959 case CTRL_APP1:
960 port = PORT_APP1;
961 enable_ier = APP1_DL;
962 break;
963 case CTRL_APP2:
964 port = PORT_APP2;
965 enable_ier = APP2_DL;
966 <<<<<<< HEAD:drivers/char/nozomi.c
967 =======
968 if (dc->state == NOZOMI_STATE_ALLOCATED) {
970 * After card initialization the flow control
971 * received for APP2 is always the last
973 dc->state = NOZOMI_STATE_READY;
974 dev_info(&dc->pdev->dev, "Device READY!\n");
976 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/char/nozomi.c
977 break;
978 default:
979 dev_err(&dc->pdev->dev,
980 "ERROR: flow control received for non-existing port\n");
981 return 0;
984 DBG1("0x%04X->0x%04X", *((u16 *)&dc->port[port].ctrl_dl),
985 *((u16 *)&ctrl_dl));
987 old_ctrl = dc->port[port].ctrl_dl;
988 dc->port[port].ctrl_dl = ctrl_dl;
990 if (old_ctrl.CTS == 1 && ctrl_dl.CTS == 0) {
991 DBG1("Disable interrupt (0x%04X) on port: %d",
992 enable_ier, port);
993 disable_transmit_ul(port, dc);
995 } else if (old_ctrl.CTS == 0 && ctrl_dl.CTS == 1) {
997 if (__kfifo_len(dc->port[port].fifo_ul)) {
998 DBG1("Enable interrupt (0x%04X) on port: %d",
999 enable_ier, port);
1000 DBG1("Data in buffer [%d], enable transmit! ",
1001 __kfifo_len(dc->port[port].fifo_ul));
1002 enable_transmit_ul(port, dc);
1003 } else {
1004 DBG1("No data in buffer...");
1008 if (*(u16 *)&old_ctrl == *(u16 *)&ctrl_dl) {
1009 DBG1(" No change in mctrl");
1010 return 1;
1012 /* Update statistics */
1013 if (old_ctrl.CTS != ctrl_dl.CTS)
1014 dc->port[port].tty_icount.cts++;
1015 if (old_ctrl.DSR != ctrl_dl.DSR)
1016 dc->port[port].tty_icount.dsr++;
1017 if (old_ctrl.RI != ctrl_dl.RI)
1018 dc->port[port].tty_icount.rng++;
1019 if (old_ctrl.DCD != ctrl_dl.DCD)
1020 dc->port[port].tty_icount.dcd++;
1022 wake_up_interruptible(&dc->port[port].tty_wait);
1024 DBG1("port: %d DCD(%d), CTS(%d), RI(%d), DSR(%d)",
1025 port,
1026 dc->port[port].tty_icount.dcd, dc->port[port].tty_icount.cts,
1027 dc->port[port].tty_icount.rng, dc->port[port].tty_icount.dsr);
1029 return 1;
1032 static enum ctrl_port_type port2ctrl(enum port_type port,
1033 const struct nozomi *dc)
1035 switch (port) {
1036 case PORT_MDM:
1037 return CTRL_MDM;
1038 case PORT_DIAG:
1039 return CTRL_DIAG;
1040 case PORT_APP1:
1041 return CTRL_APP1;
1042 case PORT_APP2:
1043 return CTRL_APP2;
1044 default:
1045 dev_err(&dc->pdev->dev,
1046 "ERROR: send flow control " \
1047 "received for non-existing port\n");
1049 return CTRL_ERROR;
1053 * Send flow control, can only update one channel at a time
1054 * Return 0 - If we have updated all flow control
1055 * Return 1 - If we need to update more flow control, ack current enable more
1057 static int send_flow_control(struct nozomi *dc)
1059 u32 i, more_flow_control_to_be_updated = 0;
1060 u16 *ctrl;
1062 for (i = PORT_MDM; i < MAX_PORT; i++) {
1063 if (dc->port[i].update_flow_control) {
1064 if (more_flow_control_to_be_updated) {
1065 /* We have more flow control to be updated */
1066 return 1;
1068 dc->port[i].ctrl_ul.port = port2ctrl(i, dc);
1069 ctrl = (u16 *)&dc->port[i].ctrl_ul;
1070 write_mem32(dc->port[PORT_CTRL].ul_addr[0], \
1071 (u32 *) ctrl, 2);
1072 dc->port[i].update_flow_control = 0;
1073 more_flow_control_to_be_updated = 1;
1076 return 0;
1080 * Handle downlink data, ports that are handled are modem and diagnostics
1081 * Return 1 - ok
1082 * Return 0 - toggle fields are out of sync
1084 static int handle_data_dl(struct nozomi *dc, enum port_type port, u8 *toggle,
1085 u16 read_iir, u16 mask1, u16 mask2)
1087 if (*toggle == 0 && read_iir & mask1) {
1088 if (receive_data(port, dc)) {
1089 writew(mask1, dc->reg_fcr);
1090 *toggle = !(*toggle);
1093 if (read_iir & mask2) {
1094 if (receive_data(port, dc)) {
1095 writew(mask2, dc->reg_fcr);
1096 *toggle = !(*toggle);
1099 } else if (*toggle == 1 && read_iir & mask2) {
1100 if (receive_data(port, dc)) {
1101 writew(mask2, dc->reg_fcr);
1102 *toggle = !(*toggle);
1105 if (read_iir & mask1) {
1106 if (receive_data(port, dc)) {
1107 writew(mask1, dc->reg_fcr);
1108 *toggle = !(*toggle);
1111 } else {
1112 dev_err(&dc->pdev->dev, "port out of sync!, toggle:%d\n",
1113 *toggle);
1114 return 0;
1116 return 1;
1120 * Handle uplink data, this is currently for the modem port
1121 * Return 1 - ok
1122 * Return 0 - toggle field are out of sync
1124 static int handle_data_ul(struct nozomi *dc, enum port_type port, u16 read_iir)
1126 u8 *toggle = &(dc->port[port].toggle_ul);
1128 if (*toggle == 0 && read_iir & MDM_UL1) {
1129 dc->last_ier &= ~MDM_UL;
1130 writew(dc->last_ier, dc->reg_ier);
1131 if (send_data(port, dc)) {
1132 writew(MDM_UL1, dc->reg_fcr);
1133 dc->last_ier = dc->last_ier | MDM_UL;
1134 writew(dc->last_ier, dc->reg_ier);
1135 *toggle = !*toggle;
1138 if (read_iir & MDM_UL2) {
1139 dc->last_ier &= ~MDM_UL;
1140 writew(dc->last_ier, dc->reg_ier);
1141 if (send_data(port, dc)) {
1142 writew(MDM_UL2, dc->reg_fcr);
1143 dc->last_ier = dc->last_ier | MDM_UL;
1144 writew(dc->last_ier, dc->reg_ier);
1145 *toggle = !*toggle;
1149 } else if (*toggle == 1 && read_iir & MDM_UL2) {
1150 dc->last_ier &= ~MDM_UL;
1151 writew(dc->last_ier, dc->reg_ier);
1152 if (send_data(port, dc)) {
1153 writew(MDM_UL2, dc->reg_fcr);
1154 dc->last_ier = dc->last_ier | MDM_UL;
1155 writew(dc->last_ier, dc->reg_ier);
1156 *toggle = !*toggle;
1159 if (read_iir & MDM_UL1) {
1160 dc->last_ier &= ~MDM_UL;
1161 writew(dc->last_ier, dc->reg_ier);
1162 if (send_data(port, dc)) {
1163 writew(MDM_UL1, dc->reg_fcr);
1164 dc->last_ier = dc->last_ier | MDM_UL;
1165 writew(dc->last_ier, dc->reg_ier);
1166 *toggle = !*toggle;
1169 } else {
1170 writew(read_iir & MDM_UL, dc->reg_fcr);
1171 dev_err(&dc->pdev->dev, "port out of sync!\n");
1172 return 0;
1174 return 1;
1177 static irqreturn_t interrupt_handler(int irq, void *dev_id)
1179 struct nozomi *dc = dev_id;
1180 unsigned int a;
1181 u16 read_iir;
1183 if (!dc)
1184 return IRQ_NONE;
1186 spin_lock(&dc->spin_mutex);
1187 read_iir = readw(dc->reg_iir);
1189 /* Card removed */
1190 if (read_iir == (u16)-1)
1191 goto none;
1193 * Just handle interrupt enabled in IER
1194 * (by masking with dc->last_ier)
1196 read_iir &= dc->last_ier;
1198 if (read_iir == 0)
1199 goto none;
1202 DBG4("%s irq:0x%04X, prev:0x%04X", interrupt2str(read_iir), read_iir,
1203 dc->last_ier);
1205 if (read_iir & RESET) {
1206 if (unlikely(!nozomi_read_config_table(dc))) {
1207 dc->last_ier = 0x0;
1208 writew(dc->last_ier, dc->reg_ier);
1209 dev_err(&dc->pdev->dev, "Could not read status from "
1210 "card, we should disable interface\n");
1211 } else {
1212 writew(RESET, dc->reg_fcr);
1214 /* No more useful info if this was the reset interrupt. */
1215 goto exit_handler;
1217 if (read_iir & CTRL_UL) {
1218 DBG1("CTRL_UL");
1219 dc->last_ier &= ~CTRL_UL;
1220 writew(dc->last_ier, dc->reg_ier);
1221 if (send_flow_control(dc)) {
1222 writew(CTRL_UL, dc->reg_fcr);
1223 dc->last_ier = dc->last_ier | CTRL_UL;
1224 writew(dc->last_ier, dc->reg_ier);
1227 if (read_iir & CTRL_DL) {
1228 receive_flow_control(dc);
1229 writew(CTRL_DL, dc->reg_fcr);
1231 if (read_iir & MDM_DL) {
1232 if (!handle_data_dl(dc, PORT_MDM,
1233 &(dc->port[PORT_MDM].toggle_dl), read_iir,
1234 MDM_DL1, MDM_DL2)) {
1235 dev_err(&dc->pdev->dev, "MDM_DL out of sync!\n");
1236 goto exit_handler;
1239 if (read_iir & MDM_UL) {
1240 if (!handle_data_ul(dc, PORT_MDM, read_iir)) {
1241 dev_err(&dc->pdev->dev, "MDM_UL out of sync!\n");
1242 goto exit_handler;
1245 if (read_iir & DIAG_DL) {
1246 if (!handle_data_dl(dc, PORT_DIAG,
1247 &(dc->port[PORT_DIAG].toggle_dl), read_iir,
1248 DIAG_DL1, DIAG_DL2)) {
1249 dev_err(&dc->pdev->dev, "DIAG_DL out of sync!\n");
1250 goto exit_handler;
1253 if (read_iir & DIAG_UL) {
1254 dc->last_ier &= ~DIAG_UL;
1255 writew(dc->last_ier, dc->reg_ier);
1256 if (send_data(PORT_DIAG, dc)) {
1257 writew(DIAG_UL, dc->reg_fcr);
1258 dc->last_ier = dc->last_ier | DIAG_UL;
1259 writew(dc->last_ier, dc->reg_ier);
1262 if (read_iir & APP1_DL) {
1263 if (receive_data(PORT_APP1, dc))
1264 writew(APP1_DL, dc->reg_fcr);
1266 if (read_iir & APP1_UL) {
1267 dc->last_ier &= ~APP1_UL;
1268 writew(dc->last_ier, dc->reg_ier);
1269 if (send_data(PORT_APP1, dc)) {
1270 writew(APP1_UL, dc->reg_fcr);
1271 dc->last_ier = dc->last_ier | APP1_UL;
1272 writew(dc->last_ier, dc->reg_ier);
1275 if (read_iir & APP2_DL) {
1276 if (receive_data(PORT_APP2, dc))
1277 writew(APP2_DL, dc->reg_fcr);
1279 if (read_iir & APP2_UL) {
1280 dc->last_ier &= ~APP2_UL;
1281 writew(dc->last_ier, dc->reg_ier);
1282 if (send_data(PORT_APP2, dc)) {
1283 writew(APP2_UL, dc->reg_fcr);
1284 dc->last_ier = dc->last_ier | APP2_UL;
1285 writew(dc->last_ier, dc->reg_ier);
1289 exit_handler:
1290 spin_unlock(&dc->spin_mutex);
1291 for (a = 0; a < NOZOMI_MAX_PORTS; a++)
1292 if (test_and_clear_bit(a, &dc->flip))
1293 tty_flip_buffer_push(dc->port[a].tty);
1294 return IRQ_HANDLED;
1295 none:
1296 spin_unlock(&dc->spin_mutex);
1297 return IRQ_NONE;
1300 static void nozomi_get_card_type(struct nozomi *dc)
1302 int i;
1303 u32 size = 0;
1305 for (i = 0; i < 6; i++)
1306 size += pci_resource_len(dc->pdev, i);
1308 /* Assume card type F32_8 if no match */
1309 dc->card_type = size == 2048 ? F32_2 : F32_8;
1311 dev_info(&dc->pdev->dev, "Card type is: %d\n", dc->card_type);
1314 static void nozomi_setup_private_data(struct nozomi *dc)
1316 void __iomem *offset = dc->base_addr + dc->card_type / 2;
1317 unsigned int i;
1319 dc->reg_fcr = (void __iomem *)(offset + R_FCR);
1320 dc->reg_iir = (void __iomem *)(offset + R_IIR);
1321 dc->reg_ier = (void __iomem *)(offset + R_IER);
1322 dc->last_ier = 0;
1323 dc->flip = 0;
1325 dc->port[PORT_MDM].token_dl = MDM_DL;
1326 dc->port[PORT_DIAG].token_dl = DIAG_DL;
1327 dc->port[PORT_APP1].token_dl = APP1_DL;
1328 dc->port[PORT_APP2].token_dl = APP2_DL;
1330 for (i = 0; i < MAX_PORT; i++)
1331 init_waitqueue_head(&dc->port[i].tty_wait);
1334 static ssize_t card_type_show(struct device *dev, struct device_attribute *attr,
1335 char *buf)
1337 const struct nozomi *dc = pci_get_drvdata(to_pci_dev(dev));
1339 return sprintf(buf, "%d\n", dc->card_type);
1341 static DEVICE_ATTR(card_type, S_IRUGO, card_type_show, NULL);
1343 static ssize_t open_ttys_show(struct device *dev, struct device_attribute *attr,
1344 char *buf)
1346 const struct nozomi *dc = pci_get_drvdata(to_pci_dev(dev));
1348 return sprintf(buf, "%u\n", dc->open_ttys);
1350 static DEVICE_ATTR(open_ttys, S_IRUGO, open_ttys_show, NULL);
1352 static void make_sysfs_files(struct nozomi *dc)
1354 if (device_create_file(&dc->pdev->dev, &dev_attr_card_type))
1355 dev_err(&dc->pdev->dev,
1356 "Could not create sysfs file for card_type\n");
1357 if (device_create_file(&dc->pdev->dev, &dev_attr_open_ttys))
1358 dev_err(&dc->pdev->dev,
1359 "Could not create sysfs file for open_ttys\n");
1362 static void remove_sysfs_files(struct nozomi *dc)
1364 device_remove_file(&dc->pdev->dev, &dev_attr_card_type);
1365 device_remove_file(&dc->pdev->dev, &dev_attr_open_ttys);
1368 /* Allocate memory for one device */
1369 static int __devinit nozomi_card_init(struct pci_dev *pdev,
1370 const struct pci_device_id *ent)
1372 resource_size_t start;
1373 int ret;
1374 struct nozomi *dc = NULL;
1375 int ndev_idx;
1376 int i;
1378 dev_dbg(&pdev->dev, "Init, new card found\n");
1380 for (ndev_idx = 0; ndev_idx < ARRAY_SIZE(ndevs); ndev_idx++)
1381 if (!ndevs[ndev_idx])
1382 break;
1384 if (ndev_idx >= ARRAY_SIZE(ndevs)) {
1385 dev_err(&pdev->dev, "no free tty range for this card left\n");
1386 ret = -EIO;
1387 goto err;
1390 dc = kzalloc(sizeof(struct nozomi), GFP_KERNEL);
1391 if (unlikely(!dc)) {
1392 dev_err(&pdev->dev, "Could not allocate memory\n");
1393 ret = -ENOMEM;
1394 goto err_free;
1397 dc->pdev = pdev;
1399 <<<<<<< HEAD:drivers/char/nozomi.c
1400 /* Find out what card type it is */
1401 nozomi_get_card_type(dc);
1403 =======
1404 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/char/nozomi.c
1405 ret = pci_enable_device(dc->pdev);
1406 if (ret) {
1407 dev_err(&pdev->dev, "Failed to enable PCI Device\n");
1408 goto err_free;
1411 <<<<<<< HEAD:drivers/char/nozomi.c
1412 start = pci_resource_start(dc->pdev, 0);
1413 if (start == 0) {
1414 dev_err(&pdev->dev, "No I/O address for card detected\n");
1415 ret = -ENODEV;
1416 goto err_disable_device;
1419 =======
1420 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/char/nozomi.c
1421 ret = pci_request_regions(dc->pdev, NOZOMI_NAME);
1422 if (ret) {
1423 dev_err(&pdev->dev, "I/O address 0x%04x already in use\n",
1424 (int) /* nozomi_private.io_addr */ 0);
1425 goto err_disable_device;
1428 <<<<<<< HEAD:drivers/char/nozomi.c
1429 =======
1430 start = pci_resource_start(dc->pdev, 0);
1431 if (start == 0) {
1432 dev_err(&pdev->dev, "No I/O address for card detected\n");
1433 ret = -ENODEV;
1434 goto err_rel_regs;
1437 /* Find out what card type it is */
1438 nozomi_get_card_type(dc);
1440 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/char/nozomi.c
1441 dc->base_addr = ioremap(start, dc->card_type);
1442 if (!dc->base_addr) {
1443 dev_err(&pdev->dev, "Unable to map card MMIO\n");
1444 ret = -ENODEV;
1445 goto err_rel_regs;
1448 dc->send_buf = kmalloc(SEND_BUF_MAX, GFP_KERNEL);
1449 if (!dc->send_buf) {
1450 dev_err(&pdev->dev, "Could not allocate send buffer?\n");
1451 ret = -ENOMEM;
1452 goto err_free_sbuf;
1455 spin_lock_init(&dc->spin_mutex);
1457 nozomi_setup_private_data(dc);
1459 /* Disable all interrupts */
1460 dc->last_ier = 0;
1461 writew(dc->last_ier, dc->reg_ier);
1463 ret = request_irq(pdev->irq, &interrupt_handler, IRQF_SHARED,
1464 NOZOMI_NAME, dc);
1465 if (unlikely(ret)) {
1466 dev_err(&pdev->dev, "can't request irq %d\n", pdev->irq);
1467 goto err_free_sbuf;
1470 DBG1("base_addr: %p", dc->base_addr);
1472 make_sysfs_files(dc);
1474 dc->index_start = ndev_idx * MAX_PORT;
1475 ndevs[ndev_idx] = dc;
1477 <<<<<<< HEAD:drivers/char/nozomi.c
1478 =======
1479 pci_set_drvdata(pdev, dc);
1481 /* Enable RESET interrupt */
1482 dc->last_ier = RESET;
1483 iowrite16(dc->last_ier, dc->reg_ier);
1485 dc->state = NOZOMI_STATE_ENABLED;
1487 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/char/nozomi.c
1488 for (i = 0; i < MAX_PORT; i++) {
1489 mutex_init(&dc->port[i].tty_sem);
1490 dc->port[i].tty_open_count = 0;
1491 dc->port[i].tty = NULL;
1492 tty_register_device(ntty_driver, dc->index_start + i,
1493 &pdev->dev);
1496 <<<<<<< HEAD:drivers/char/nozomi.c
1497 /* Enable RESET interrupt. */
1498 dc->last_ier = RESET;
1499 writew(dc->last_ier, dc->reg_ier);
1501 pci_set_drvdata(pdev, dc);
1503 =======
1504 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/char/nozomi.c
1505 return 0;
1507 err_free_sbuf:
1508 kfree(dc->send_buf);
1509 iounmap(dc->base_addr);
1510 err_rel_regs:
1511 pci_release_regions(pdev);
1512 err_disable_device:
1513 pci_disable_device(pdev);
1514 err_free:
1515 kfree(dc);
1516 err:
1517 return ret;
1520 static void __devexit tty_exit(struct nozomi *dc)
1522 unsigned int i;
1524 DBG1(" ");
1526 flush_scheduled_work();
1528 for (i = 0; i < MAX_PORT; ++i)
1529 if (dc->port[i].tty && \
1530 list_empty(&dc->port[i].tty->hangup_work.entry))
1531 tty_hangup(dc->port[i].tty);
1533 while (dc->open_ttys)
1534 msleep(1);
1536 for (i = dc->index_start; i < dc->index_start + MAX_PORT; ++i)
1537 tty_unregister_device(ntty_driver, i);
1540 /* Deallocate memory for one device */
1541 static void __devexit nozomi_card_exit(struct pci_dev *pdev)
1543 int i;
1544 struct ctrl_ul ctrl;
1545 struct nozomi *dc = pci_get_drvdata(pdev);
1547 /* Disable all interrupts */
1548 dc->last_ier = 0;
1549 writew(dc->last_ier, dc->reg_ier);
1551 tty_exit(dc);
1553 /* Send 0x0001, command card to resend the reset token. */
1554 /* This is to get the reset when the module is reloaded. */
1555 ctrl.port = 0x00;
1556 ctrl.reserved = 0;
1557 ctrl.RTS = 0;
1558 ctrl.DTR = 1;
1559 DBG1("sending flow control 0x%04X", *((u16 *)&ctrl));
1561 /* Setup dc->reg addresses to we can use defines here */
1562 write_mem32(dc->port[PORT_CTRL].ul_addr[0], (u32 *)&ctrl, 2);
1563 writew(CTRL_UL, dc->reg_fcr); /* push the token to the card. */
1565 remove_sysfs_files(dc);
1567 free_irq(pdev->irq, dc);
1569 for (i = 0; i < MAX_PORT; i++)
1570 if (dc->port[i].fifo_ul)
1571 kfifo_free(dc->port[i].fifo_ul);
1573 kfree(dc->send_buf);
1575 iounmap(dc->base_addr);
1577 pci_release_regions(pdev);
1579 pci_disable_device(pdev);
1581 ndevs[dc->index_start / MAX_PORT] = NULL;
1583 kfree(dc);
1586 static void set_rts(const struct tty_struct *tty, int rts)
1588 struct port *port = get_port_by_tty(tty);
1590 port->ctrl_ul.RTS = rts;
1591 port->update_flow_control = 1;
1592 enable_transmit_ul(PORT_CTRL, get_dc_by_tty(tty));
1595 static void set_dtr(const struct tty_struct *tty, int dtr)
1597 struct port *port = get_port_by_tty(tty);
1599 DBG1("SETTING DTR index: %d, dtr: %d", tty->index, dtr);
1601 port->ctrl_ul.DTR = dtr;
1602 port->update_flow_control = 1;
1603 enable_transmit_ul(PORT_CTRL, get_dc_by_tty(tty));
1607 * ----------------------------------------------------------------------------
1608 * TTY code
1609 * ----------------------------------------------------------------------------
1612 /* Called when the userspace process opens the tty, /dev/noz*. */
1613 static int ntty_open(struct tty_struct *tty, struct file *file)
1615 struct port *port = get_port_by_tty(tty);
1616 struct nozomi *dc = get_dc_by_tty(tty);
1617 unsigned long flags;
1619 <<<<<<< HEAD:drivers/char/nozomi.c
1620 if (!port || !dc)
1621 =======
1622 if (!port || !dc || dc->state != NOZOMI_STATE_READY)
1623 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/char/nozomi.c
1624 return -ENODEV;
1626 if (mutex_lock_interruptible(&port->tty_sem))
1627 return -ERESTARTSYS;
1629 port->tty_open_count++;
1630 dc->open_ttys++;
1632 /* Enable interrupt downlink for channel */
1633 if (port->tty_open_count == 1) {
1634 tty->low_latency = 1;
1635 tty->driver_data = port;
1636 port->tty = tty;
1637 DBG1("open: %d", port->token_dl);
1638 spin_lock_irqsave(&dc->spin_mutex, flags);
1639 dc->last_ier = dc->last_ier | port->token_dl;
1640 writew(dc->last_ier, dc->reg_ier);
1641 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1644 mutex_unlock(&port->tty_sem);
1646 return 0;
1649 /* Called when the userspace process close the tty, /dev/noz*. */
1650 static void ntty_close(struct tty_struct *tty, struct file *file)
1652 struct nozomi *dc = get_dc_by_tty(tty);
1653 struct port *port = tty->driver_data;
1654 unsigned long flags;
1656 if (!dc || !port)
1657 return;
1659 if (mutex_lock_interruptible(&port->tty_sem))
1660 return;
1662 if (!port->tty_open_count)
1663 goto exit;
1665 dc->open_ttys--;
1666 port->tty_open_count--;
1668 if (port->tty_open_count == 0) {
1669 DBG1("close: %d", port->token_dl);
1670 spin_lock_irqsave(&dc->spin_mutex, flags);
1671 dc->last_ier &= ~(port->token_dl);
1672 writew(dc->last_ier, dc->reg_ier);
1673 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1676 exit:
1677 mutex_unlock(&port->tty_sem);
1681 * called when the userspace process writes to the tty (/dev/noz*).
1682 * Data is inserted into a fifo, which is then read and transfered to the modem.
1684 static int ntty_write(struct tty_struct *tty, const unsigned char *buffer,
1685 int count)
1687 int rval = -EINVAL;
1688 struct nozomi *dc = get_dc_by_tty(tty);
1689 struct port *port = tty->driver_data;
1690 unsigned long flags;
1692 /* DBG1( "WRITEx: %d, index = %d", count, index); */
1694 if (!dc || !port)
1695 return -ENODEV;
1697 if (unlikely(!mutex_trylock(&port->tty_sem))) {
1699 * must test lock as tty layer wraps calls
1700 * to this function with BKL
1702 dev_err(&dc->pdev->dev, "Would have deadlocked - "
1703 "return EAGAIN\n");
1704 return -EAGAIN;
1707 if (unlikely(!port->tty_open_count)) {
1708 DBG1(" ");
1709 goto exit;
1712 rval = __kfifo_put(port->fifo_ul, (unsigned char *)buffer, count);
1714 /* notify card */
1715 if (unlikely(dc == NULL)) {
1716 DBG1("No device context?");
1717 goto exit;
1720 spin_lock_irqsave(&dc->spin_mutex, flags);
1721 /* CTS is only valid on the modem channel */
1722 if (port == &(dc->port[PORT_MDM])) {
1723 if (port->ctrl_dl.CTS) {
1724 DBG4("Enable interrupt");
1725 enable_transmit_ul(tty->index % MAX_PORT, dc);
1726 } else {
1727 dev_err(&dc->pdev->dev,
1728 "CTS not active on modem port?\n");
1730 } else {
1731 enable_transmit_ul(tty->index % MAX_PORT, dc);
1733 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1735 exit:
1736 mutex_unlock(&port->tty_sem);
1737 return rval;
1741 * Calculate how much is left in device
1742 * This method is called by the upper tty layer.
1743 * #according to sources N_TTY.c it expects a value >= 0 and
1744 * does not check for negative values.
1746 static int ntty_write_room(struct tty_struct *tty)
1748 struct port *port = tty->driver_data;
1749 int room = 0;
1750 const struct nozomi *dc = get_dc_by_tty(tty);
1752 if (!dc || !port)
1753 return 0;
1754 if (!mutex_trylock(&port->tty_sem))
1755 return 0;
1757 if (!port->tty_open_count)
1758 goto exit;
1760 room = port->fifo_ul->size - __kfifo_len(port->fifo_ul);
1762 exit:
1763 mutex_unlock(&port->tty_sem);
1764 return room;
1767 /* Gets io control parameters */
1768 static int ntty_tiocmget(struct tty_struct *tty, struct file *file)
1770 const struct port *port = tty->driver_data;
1771 const struct ctrl_dl *ctrl_dl = &port->ctrl_dl;
1772 const struct ctrl_ul *ctrl_ul = &port->ctrl_ul;
1774 return (ctrl_ul->RTS ? TIOCM_RTS : 0) |
1775 (ctrl_ul->DTR ? TIOCM_DTR : 0) |
1776 (ctrl_dl->DCD ? TIOCM_CAR : 0) |
1777 (ctrl_dl->RI ? TIOCM_RNG : 0) |
1778 (ctrl_dl->DSR ? TIOCM_DSR : 0) |
1779 (ctrl_dl->CTS ? TIOCM_CTS : 0);
1782 /* Sets io controls parameters */
1783 static int ntty_tiocmset(struct tty_struct *tty, struct file *file,
1784 unsigned int set, unsigned int clear)
1786 <<<<<<< HEAD:drivers/char/nozomi.c
1787 =======
1788 struct nozomi *dc = get_dc_by_tty(tty);
1789 unsigned long flags;
1791 spin_lock_irqsave(&dc->spin_mutex, flags);
1792 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/char/nozomi.c
1793 if (set & TIOCM_RTS)
1794 set_rts(tty, 1);
1795 else if (clear & TIOCM_RTS)
1796 set_rts(tty, 0);
1798 if (set & TIOCM_DTR)
1799 set_dtr(tty, 1);
1800 else if (clear & TIOCM_DTR)
1801 set_dtr(tty, 0);
1802 <<<<<<< HEAD:drivers/char/nozomi.c
1803 =======
1804 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1805 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/char/nozomi.c
1807 return 0;
1810 static int ntty_cflags_changed(struct port *port, unsigned long flags,
1811 struct async_icount *cprev)
1813 const struct async_icount cnow = port->tty_icount;
1814 int ret;
1816 ret = ((flags & TIOCM_RNG) && (cnow.rng != cprev->rng)) ||
1817 ((flags & TIOCM_DSR) && (cnow.dsr != cprev->dsr)) ||
1818 ((flags & TIOCM_CD) && (cnow.dcd != cprev->dcd)) ||
1819 ((flags & TIOCM_CTS) && (cnow.cts != cprev->cts));
1821 *cprev = cnow;
1823 return ret;
1826 static int ntty_ioctl_tiocgicount(struct port *port, void __user *argp)
1828 const struct async_icount cnow = port->tty_icount;
1829 struct serial_icounter_struct icount;
1831 icount.cts = cnow.cts;
1832 icount.dsr = cnow.dsr;
1833 icount.rng = cnow.rng;
1834 icount.dcd = cnow.dcd;
1835 icount.rx = cnow.rx;
1836 icount.tx = cnow.tx;
1837 icount.frame = cnow.frame;
1838 icount.overrun = cnow.overrun;
1839 icount.parity = cnow.parity;
1840 icount.brk = cnow.brk;
1841 icount.buf_overrun = cnow.buf_overrun;
1843 <<<<<<< HEAD:drivers/char/nozomi.c
1844 return copy_to_user(argp, &icount, sizeof(icount));
1845 =======
1846 return copy_to_user(argp, &icount, sizeof(icount)) ? -EFAULT : 0;
1847 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/char/nozomi.c
1850 static int ntty_ioctl(struct tty_struct *tty, struct file *file,
1851 unsigned int cmd, unsigned long arg)
1853 struct port *port = tty->driver_data;
1854 void __user *argp = (void __user *)arg;
1855 int rval = -ENOIOCTLCMD;
1857 DBG1("******** IOCTL, cmd: %d", cmd);
1859 switch (cmd) {
1860 case TIOCMIWAIT: {
1861 struct async_icount cprev = port->tty_icount;
1863 rval = wait_event_interruptible(port->tty_wait,
1864 ntty_cflags_changed(port, arg, &cprev));
1865 break;
1866 } case TIOCGICOUNT:
1867 rval = ntty_ioctl_tiocgicount(port, argp);
1868 break;
1869 default:
1870 DBG1("ERR: 0x%08X, %d", cmd, cmd);
1871 break;
1874 return rval;
1878 * Called by the upper tty layer when tty buffers are ready
1879 * to receive data again after a call to throttle.
1881 static void ntty_unthrottle(struct tty_struct *tty)
1883 struct nozomi *dc = get_dc_by_tty(tty);
1884 unsigned long flags;
1886 DBG1("UNTHROTTLE");
1887 spin_lock_irqsave(&dc->spin_mutex, flags);
1888 enable_transmit_dl(tty->index % MAX_PORT, dc);
1889 set_rts(tty, 1);
1891 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1895 * Called by the upper tty layer when the tty buffers are almost full.
1896 * The driver should stop send more data.
1898 static void ntty_throttle(struct tty_struct *tty)
1900 struct nozomi *dc = get_dc_by_tty(tty);
1901 unsigned long flags;
1903 DBG1("THROTTLE");
1904 spin_lock_irqsave(&dc->spin_mutex, flags);
1905 set_rts(tty, 0);
1906 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1909 /* just to discard single character writes */
1910 static void ntty_put_char(struct tty_struct *tty, unsigned char c)
1913 * card does not react correct when we write single chars
1914 * to the card, so we discard them
1916 DBG2("PUT CHAR Function: %c", c);
1919 /* Returns number of chars in buffer, called by tty layer */
1920 static s32 ntty_chars_in_buffer(struct tty_struct *tty)
1922 struct port *port = tty->driver_data;
1923 struct nozomi *dc = get_dc_by_tty(tty);
1924 s32 rval;
1926 if (unlikely(!dc || !port)) {
1927 rval = -ENODEV;
1928 goto exit_in_buffer;
1931 if (unlikely(!port->tty_open_count)) {
1932 dev_err(&dc->pdev->dev, "No tty open?\n");
1933 rval = -ENODEV;
1934 goto exit_in_buffer;
1937 rval = __kfifo_len(port->fifo_ul);
1939 exit_in_buffer:
1940 return rval;
1943 static const struct tty_operations tty_ops = {
1944 .ioctl = ntty_ioctl,
1945 .open = ntty_open,
1946 .close = ntty_close,
1947 .write = ntty_write,
1948 .write_room = ntty_write_room,
1949 .unthrottle = ntty_unthrottle,
1950 .throttle = ntty_throttle,
1951 .chars_in_buffer = ntty_chars_in_buffer,
1952 .put_char = ntty_put_char,
1953 .tiocmget = ntty_tiocmget,
1954 .tiocmset = ntty_tiocmset,
1957 /* Module initialization */
1958 static struct pci_driver nozomi_driver = {
1959 .name = NOZOMI_NAME,
1960 .id_table = nozomi_pci_tbl,
1961 .probe = nozomi_card_init,
1962 .remove = __devexit_p(nozomi_card_exit),
1965 static __init int nozomi_init(void)
1967 int ret;
1969 printk(KERN_INFO "Initializing %s\n", VERSION_STRING);
1971 ntty_driver = alloc_tty_driver(NTTY_TTY_MAXMINORS);
1972 if (!ntty_driver)
1973 return -ENOMEM;
1975 ntty_driver->owner = THIS_MODULE;
1976 ntty_driver->driver_name = NOZOMI_NAME_TTY;
1977 ntty_driver->name = "noz";
1978 ntty_driver->major = 0;
1979 ntty_driver->type = TTY_DRIVER_TYPE_SERIAL;
1980 ntty_driver->subtype = SERIAL_TYPE_NORMAL;
1981 ntty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
1982 ntty_driver->init_termios = tty_std_termios;
1983 ntty_driver->init_termios.c_cflag = B115200 | CS8 | CREAD | \
1984 HUPCL | CLOCAL;
1985 ntty_driver->init_termios.c_ispeed = 115200;
1986 ntty_driver->init_termios.c_ospeed = 115200;
1987 tty_set_operations(ntty_driver, &tty_ops);
1989 ret = tty_register_driver(ntty_driver);
1990 if (ret) {
1991 printk(KERN_ERR "Nozomi: failed to register ntty driver\n");
1992 goto free_tty;
1995 ret = pci_register_driver(&nozomi_driver);
1996 if (ret) {
1997 printk(KERN_ERR "Nozomi: can't register pci driver\n");
1998 goto unr_tty;
2001 return 0;
2002 unr_tty:
2003 tty_unregister_driver(ntty_driver);
2004 free_tty:
2005 put_tty_driver(ntty_driver);
2006 return ret;
2009 static __exit void nozomi_exit(void)
2011 printk(KERN_INFO "Unloading %s\n", DRIVER_DESC);
2012 pci_unregister_driver(&nozomi_driver);
2013 tty_unregister_driver(ntty_driver);
2014 put_tty_driver(ntty_driver);
2017 module_init(nozomi_init);
2018 module_exit(nozomi_exit);
2020 module_param(debug, int, S_IRUGO | S_IWUSR);
2022 MODULE_LICENSE("Dual BSD/GPL");
2023 MODULE_DESCRIPTION(DRIVER_DESC);