nozomi driver update
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / char / nozomi.c
CommitLineData
20fd1e3b
FS
1/*
2 * nozomi.c -- HSDPA driver Broadband Wireless Data Card - Globe Trotter
3 *
4 * Written by: Ulf Jakobsson,
e9ed537a 5 * Jan Ã…kerfeldt,
20fd1e3b
FS
6 * Stefan Thomasson,
7 *
8 * Maintained by: Paul Hardwick (p.hardwick@option.com)
9 *
10 * Patches:
11 * Locking code changes for Vodafone by Sphere Systems Ltd,
12 * Andrew Bird (ajb@spheresystems.co.uk )
13 * & Phil Sanderson
14 *
15 * Source has been ported from an implementation made by Filip Aben @ Option
16 *
17 * --------------------------------------------------------------------------
18 *
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.
23 *
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.
28 *
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.
33 *
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
37 *
38 * --------------------------------------------------------------------------
39 */
40
20fd1e3b
FS
41/* Enable this to have a lot of debug printouts */
42#define DEBUG
43
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>
58
59#include <linux/delay.h>
60
61
62#define VERSION_STRING DRIVER_DESC " 2.1d (build date: " \
63 __DATE__ " " __TIME__ ")"
64
65/* Macros definitions */
66
67/* Default debug printout level */
68#define NOZOMI_DEBUG_LEVEL 0x00
69
70#define P_BUF_SIZE 128
71#define NFO(_err_flag_, args...) \
72do { \
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)
78
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)
87
88#ifdef DEBUG
89/* Do we need this settable at runtime? */
90static int debug = NOZOMI_DEBUG_LEVEL;
91
e9ed537a
FS
92#define D(lvl, args...) do \
93 {if (lvl & debug) NFO(KERN_DEBUG, ##args); } \
94 while (0)
20fd1e3b
FS
95#define D_(lvl, args...) D(lvl, ##args)
96
97/* These printouts are always printed */
98
99#else
100static int debug;
101#define D_(lvl, args...)
102#endif
103
104/* TODO: rewrite to optimize macros... */
105
106#define TMP_BUF_MAX 256
107
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';\
115 } \
116 DBG1("SENDING: '%s' (%d+n)", tbuf, len__);\
117 } else {\
118 DBG1("SENDING: '%s' (%d)", tbuf, len__);\
119 } \
120} while (0)
121
122/* Defines */
123#define NOZOMI_NAME "nozomi"
124#define NOZOMI_NAME_TTY "nozomi_tty"
125#define DRIVER_DESC "Nozomi driver"
126
127#define NTTY_TTY_MAXMINORS 256
128#define NTTY_FIFO_BUFFER_SIZE 8192
129
130/* Must be power of 2 */
131#define FIFO_BUFFER_SIZE_UL 8192
132
133/* Size of tmp send buffer to card */
134#define SEND_BUF_MAX 1024
135#define RECEIVE_BUF_MAX 4
136
137
138/* Define all types of vendors and devices to support */
139#define VENDOR1 0x1931 /* Vendor Option */
140#define DEVICE1 0x000c /* HSDPA card */
141
142#define R_IIR 0x0000 /* Interrupt Identity Register */
143#define R_FCR 0x0000 /* Flow Control Register */
144#define R_IER 0x0004 /* Interrupt Enable Register */
145
146#define CONFIG_MAGIC 0xEFEFFEFE
147#define TOGGLE_VALID 0x0000
148
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
164
165#define MDM_DL (MDM_DL1 | MDM_DL2)
166#define MDM_UL (MDM_UL1 | MDM_UL2)
167#define DIAG_DL (DIAG_DL1 | DIAG_DL2)
168
169/* modem signal definition */
170#define CTRL_DSR 0x0001
171#define CTRL_DCD 0x0002
172#define CTRL_RI 0x0004
173#define CTRL_CTS 0x0008
174
175#define CTRL_DTR 0x0001
176#define CTRL_RTS 0x0002
177
178#define MAX_PORT 4
179#define NOZOMI_MAX_PORTS 5
180#define NOZOMI_MAX_CARDS (NTTY_TTY_MAXMINORS / MAX_PORT)
181
182/* Type definitions */
183
184/*
185 * There are two types of nozomi cards,
186 * one with 2048 memory and with 8192 memory
187 */
188enum card_type {
189 F32_2 = 2048, /* 512 bytes downlink + uplink * 2 -> 2048 */
190 F32_8 = 8192, /* 3072 bytes downl. + 1024 bytes uplink * 2 -> 8192 */
191};
192
193/* Two different toggle channels exist */
194enum channel_type {
195 CH_A = 0,
196 CH_B = 1,
197};
198
199/* Port definition for the card regarding flow control */
200enum ctrl_port_type {
201 CTRL_CMD = 0,
202 CTRL_MDM = 1,
203 CTRL_DIAG = 2,
204 CTRL_APP1 = 3,
205 CTRL_APP2 = 4,
206 CTRL_ERROR = -1,
207};
208
209/* Ports that the nozomi has */
210enum port_type {
211 PORT_MDM = 0,
212 PORT_DIAG = 1,
213 PORT_APP1 = 2,
214 PORT_APP2 = 3,
215 PORT_CTRL = 4,
216 PORT_ERROR = -1,
217};
218
219#ifdef __BIG_ENDIAN
220/* Big endian */
221
222struct toggles {
e9ed537a 223 unsigned int enabled:5; /*
20fd1e3b
FS
224 * Toggle fields are valid if enabled is 0,
225 * else A-channels must always be used.
226 */
e9ed537a
FS
227 unsigned int diag_dl:1;
228 unsigned int mdm_dl:1;
229 unsigned int mdm_ul:1;
20fd1e3b
FS
230} __attribute__ ((packed));
231
232/* Configuration table to read at startup of card */
233/* Is for now only needed during initialization phase */
234struct config_table {
235 u32 signature;
236 u16 product_information;
237 u16 version;
238 u8 pad3[3];
239 struct toggles toggle;
240 u8 pad1[4];
241 u16 dl_mdm_len1; /*
242 * If this is 64, it can hold
243 * 60 bytes + 4 that is length field
244 */
245 u16 dl_start;
246
247 u16 dl_diag_len1;
248 u16 dl_mdm_len2; /*
249 * If this is 64, it can hold
250 * 60 bytes + 4 that is length field
251 */
252 u16 dl_app1_len;
253
254 u16 dl_diag_len2;
255 u16 dl_ctrl_len;
256 u16 dl_app2_len;
257 u8 pad2[16];
258 u16 ul_mdm_len1;
259 u16 ul_start;
260 u16 ul_diag_len;
261 u16 ul_mdm_len2;
262 u16 ul_app1_len;
263 u16 ul_app2_len;
264 u16 ul_ctrl_len;
265} __attribute__ ((packed));
266
267/* This stores all control downlink flags */
268struct ctrl_dl {
269 u8 port;
e9ed537a
FS
270 unsigned int reserved:4;
271 unsigned int CTS:1;
272 unsigned int RI:1;
273 unsigned int DCD:1;
274 unsigned int DSR:1;
20fd1e3b
FS
275} __attribute__ ((packed));
276
277/* This stores all control uplink flags */
278struct ctrl_ul {
279 u8 port;
e9ed537a
FS
280 unsigned int reserved:6;
281 unsigned int RTS:1;
282 unsigned int DTR:1;
20fd1e3b
FS
283} __attribute__ ((packed));
284
285#else
286/* Little endian */
287
288/* This represents the toggle information */
289struct toggles {
e9ed537a
FS
290 unsigned int mdm_ul:1;
291 unsigned int mdm_dl:1;
292 unsigned int diag_dl:1;
293 unsigned int enabled:5; /*
20fd1e3b
FS
294 * Toggle fields are valid if enabled is 0,
295 * else A-channels must always be used.
296 */
297} __attribute__ ((packed));
298
299/* Configuration table to read at startup of card */
300struct config_table {
301 u32 signature;
302 u16 version;
303 u16 product_information;
304 struct toggles toggle;
305 u8 pad1[7];
306 u16 dl_start;
307 u16 dl_mdm_len1; /*
308 * If this is 64, it can hold
309 * 60 bytes + 4 that is length field
310 */
311 u16 dl_mdm_len2;
312 u16 dl_diag_len1;
313 u16 dl_diag_len2;
314 u16 dl_app1_len;
315 u16 dl_app2_len;
316 u16 dl_ctrl_len;
317 u8 pad2[16];
318 u16 ul_start;
319 u16 ul_mdm_len2;
320 u16 ul_mdm_len1;
321 u16 ul_diag_len;
322 u16 ul_app1_len;
323 u16 ul_app2_len;
324 u16 ul_ctrl_len;
325} __attribute__ ((packed));
326
327/* This stores all control downlink flags */
328struct ctrl_dl {
e9ed537a
FS
329 unsigned int DSR:1;
330 unsigned int DCD:1;
331 unsigned int RI:1;
332 unsigned int CTS:1;
333 unsigned int reserverd:4;
20fd1e3b
FS
334 u8 port;
335} __attribute__ ((packed));
336
337/* This stores all control uplink flags */
338struct ctrl_ul {
e9ed537a
FS
339 unsigned int DTR:1;
340 unsigned int RTS:1;
341 unsigned int reserved:6;
20fd1e3b
FS
342 u8 port;
343} __attribute__ ((packed));
344#endif
345
346/* This holds all information that is needed regarding a port */
347struct port {
348 u8 update_flow_control;
349 struct ctrl_ul ctrl_ul;
350 struct ctrl_dl ctrl_dl;
351 struct kfifo *fifo_ul;
352 void __iomem *dl_addr[2];
353 u32 dl_size[2];
354 u8 toggle_dl;
355 void __iomem *ul_addr[2];
356 u32 ul_size[2];
357 u8 toggle_ul;
358 u16 token_dl;
359
360 struct tty_struct *tty;
361 int tty_open_count;
362 /* mutex to ensure one access patch to this port */
363 struct mutex tty_sem;
364 wait_queue_head_t tty_wait;
365 struct async_icount tty_icount;
366};
367
368/* Private data one for each card in the system */
369struct nozomi {
370 void __iomem *base_addr;
371 unsigned long flip;
372
373 /* Pointers to registers */
374 void __iomem *reg_iir;
375 void __iomem *reg_fcr;
376 void __iomem *reg_ier;
377
378 u16 last_ier;
379 enum card_type card_type;
380 struct config_table config_table; /* Configuration table */
381 struct pci_dev *pdev;
382 struct port port[NOZOMI_MAX_PORTS];
383 u8 *send_buf;
384
385 spinlock_t spin_mutex; /* secures access to registers and tty */
386
387 unsigned int index_start;
388 u32 open_ttys;
389};
390
391/* This is a data packet that is read or written to/from card */
392struct buffer {
393 u32 size; /* size is the length of the data buffer */
394 u8 *data;
395} __attribute__ ((packed));
396
397/* Global variables */
398static struct pci_device_id nozomi_pci_tbl[] = {
399 {PCI_DEVICE(VENDOR1, DEVICE1)},
400 {},
401};
402
403MODULE_DEVICE_TABLE(pci, nozomi_pci_tbl);
404
405static struct nozomi *ndevs[NOZOMI_MAX_CARDS];
406static struct tty_driver *ntty_driver;
407
408/*
409 * find card by tty_index
410 */
411static inline struct nozomi *get_dc_by_tty(const struct tty_struct *tty)
412{
413 return tty ? ndevs[tty->index / MAX_PORT] : NULL;
414}
415
416static inline struct port *get_port_by_tty(const struct tty_struct *tty)
417{
418 struct nozomi *ndev = get_dc_by_tty(tty);
419 return ndev ? &ndev->port[tty->index % MAX_PORT] : NULL;
420}
421
422/*
423 * TODO:
424 * -Optimize
425 * -Rewrite cleaner
426 */
427
428static void read_mem32(u32 *buf, const void __iomem *mem_addr_start,
429 u32 size_bytes)
430{
431 u32 i = 0;
432 const u32 *ptr = (__force u32 *) mem_addr_start;
433 u16 *buf16;
434
435 if (unlikely(!ptr || !buf))
436 goto out;
437
438 /* shortcut for extremely often used cases */
439 switch (size_bytes) {
440 case 2: /* 2 bytes */
441 buf16 = (u16 *) buf;
442 *buf16 = __le16_to_cpu(readw((void __iomem *)ptr));
443 goto out;
444 break;
445 case 4: /* 4 bytes */
446 *(buf) = __le32_to_cpu(readl((void __iomem *)ptr));
447 goto out;
448 break;
449 }
450
451 while (i < size_bytes) {
452 if (size_bytes - i == 2) {
453 /* Handle 2 bytes in the end */
454 buf16 = (u16 *) buf;
455 *(buf16) = __le16_to_cpu(readw((void __iomem *)ptr));
456 i += 2;
457 } else {
458 /* Read 4 bytes */
459 *(buf) = __le32_to_cpu(readl((void __iomem *)ptr));
460 i += 4;
461 }
462 buf++;
463 ptr++;
464 }
465out:
466 return;
467}
468
469/*
470 * TODO:
471 * -Optimize
472 * -Rewrite cleaner
473 */
474static u32 write_mem32(void __iomem *mem_addr_start, u32 *buf,
475 u32 size_bytes)
476{
477 u32 i = 0;
478 u32 *ptr = (__force u32 *) mem_addr_start;
479 u16 *buf16;
480
481 if (unlikely(!ptr || !buf))
482 return 0;
483
484 /* shortcut for extremely often used cases */
485 switch (size_bytes) {
486 case 2: /* 2 bytes */
487 buf16 = (u16 *) buf;
488 writew(__cpu_to_le16(*buf16), (void __iomem *)ptr);
489 return 2;
490 break;
491 case 1: /*
492 * also needs to write 4 bytes in this case
493 * so falling through..
494 */
495 case 4: /* 4 bytes */
496 writel(__cpu_to_le32(*buf), (void __iomem *)ptr);
497 return 4;
498 break;
499 }
500
501 while (i < size_bytes) {
502 if (size_bytes - i == 2) {
503 /* 2 bytes */
504 buf16 = (u16 *) buf;
505 writew(__cpu_to_le16(*buf16), (void __iomem *)ptr);
506 i += 2;
507 } else {
508 /* 4 bytes */
509 writel(__cpu_to_le32(*buf), (void __iomem *)ptr);
510 i += 4;
511 }
512 buf++;
513 ptr++;
514 }
515 return i;
516}
517
518/* Setup pointers to different channels and also setup buffer sizes. */
519static void setup_memory(struct nozomi *dc)
520{
521 void __iomem *offset = dc->base_addr + dc->config_table.dl_start;
522 /* The length reported is including the length field of 4 bytes,
523 * hence subtract with 4.
524 */
525 const u16 buff_offset = 4;
526
527 /* Modem port dl configuration */
528 dc->port[PORT_MDM].dl_addr[CH_A] = offset;
529 dc->port[PORT_MDM].dl_addr[CH_B] =
530 (offset += dc->config_table.dl_mdm_len1);
531 dc->port[PORT_MDM].dl_size[CH_A] =
532 dc->config_table.dl_mdm_len1 - buff_offset;
533 dc->port[PORT_MDM].dl_size[CH_B] =
534 dc->config_table.dl_mdm_len2 - buff_offset;
535
536 /* Diag port dl configuration */
537 dc->port[PORT_DIAG].dl_addr[CH_A] =
538 (offset += dc->config_table.dl_mdm_len2);
539 dc->port[PORT_DIAG].dl_size[CH_A] =
540 dc->config_table.dl_diag_len1 - buff_offset;
541 dc->port[PORT_DIAG].dl_addr[CH_B] =
542 (offset += dc->config_table.dl_diag_len1);
543 dc->port[PORT_DIAG].dl_size[CH_B] =
544 dc->config_table.dl_diag_len2 - buff_offset;
545
546 /* App1 port dl configuration */
547 dc->port[PORT_APP1].dl_addr[CH_A] =
548 (offset += dc->config_table.dl_diag_len2);
549 dc->port[PORT_APP1].dl_size[CH_A] =
550 dc->config_table.dl_app1_len - buff_offset;
551
552 /* App2 port dl configuration */
553 dc->port[PORT_APP2].dl_addr[CH_A] =
554 (offset += dc->config_table.dl_app1_len);
555 dc->port[PORT_APP2].dl_size[CH_A] =
556 dc->config_table.dl_app2_len - buff_offset;
557
558 /* Ctrl dl configuration */
559 dc->port[PORT_CTRL].dl_addr[CH_A] =
560 (offset += dc->config_table.dl_app2_len);
561 dc->port[PORT_CTRL].dl_size[CH_A] =
562 dc->config_table.dl_ctrl_len - buff_offset;
563
564 offset = dc->base_addr + dc->config_table.ul_start;
565
566 /* Modem Port ul configuration */
567 dc->port[PORT_MDM].ul_addr[CH_A] = offset;
568 dc->port[PORT_MDM].ul_size[CH_A] =
569 dc->config_table.ul_mdm_len1 - buff_offset;
570 dc->port[PORT_MDM].ul_addr[CH_B] =
571 (offset += dc->config_table.ul_mdm_len1);
572 dc->port[PORT_MDM].ul_size[CH_B] =
573 dc->config_table.ul_mdm_len2 - buff_offset;
574
575 /* Diag port ul configuration */
576 dc->port[PORT_DIAG].ul_addr[CH_A] =
577 (offset += dc->config_table.ul_mdm_len2);
578 dc->port[PORT_DIAG].ul_size[CH_A] =
579 dc->config_table.ul_diag_len - buff_offset;
580
581 /* App1 port ul configuration */
582 dc->port[PORT_APP1].ul_addr[CH_A] =
583 (offset += dc->config_table.ul_diag_len);
584 dc->port[PORT_APP1].ul_size[CH_A] =
585 dc->config_table.ul_app1_len - buff_offset;
586
587 /* App2 port ul configuration */
588 dc->port[PORT_APP2].ul_addr[CH_A] =
589 (offset += dc->config_table.ul_app1_len);
590 dc->port[PORT_APP2].ul_size[CH_A] =
591 dc->config_table.ul_app2_len - buff_offset;
592
593 /* Ctrl ul configuration */
594 dc->port[PORT_CTRL].ul_addr[CH_A] =
595 (offset += dc->config_table.ul_app2_len);
596 dc->port[PORT_CTRL].ul_size[CH_A] =
597 dc->config_table.ul_ctrl_len - buff_offset;
598}
599
600/* Dump config table under initalization phase */
601#ifdef DEBUG
602static void dump_table(const struct nozomi *dc)
603{
604 DBG3("signature: 0x%08X", dc->config_table.signature);
605 DBG3("version: 0x%04X", dc->config_table.version);
606 DBG3("product_information: 0x%04X", \
607 dc->config_table.product_information);
608 DBG3("toggle enabled: %d", dc->config_table.toggle.enabled);
609 DBG3("toggle up_mdm: %d", dc->config_table.toggle.mdm_ul);
610 DBG3("toggle dl_mdm: %d", dc->config_table.toggle.mdm_dl);
611 DBG3("toggle dl_dbg: %d", dc->config_table.toggle.diag_dl);
612
613 DBG3("dl_start: 0x%04X", dc->config_table.dl_start);
614 DBG3("dl_mdm_len0: 0x%04X, %d", dc->config_table.dl_mdm_len1,
615 dc->config_table.dl_mdm_len1);
616 DBG3("dl_mdm_len1: 0x%04X, %d", dc->config_table.dl_mdm_len2,
617 dc->config_table.dl_mdm_len2);
618 DBG3("dl_diag_len0: 0x%04X, %d", dc->config_table.dl_diag_len1,
619 dc->config_table.dl_diag_len1);
620 DBG3("dl_diag_len1: 0x%04X, %d", dc->config_table.dl_diag_len2,
621 dc->config_table.dl_diag_len2);
622 DBG3("dl_app1_len: 0x%04X, %d", dc->config_table.dl_app1_len,
623 dc->config_table.dl_app1_len);
624 DBG3("dl_app2_len: 0x%04X, %d", dc->config_table.dl_app2_len,
625 dc->config_table.dl_app2_len);
626 DBG3("dl_ctrl_len: 0x%04X, %d", dc->config_table.dl_ctrl_len,
627 dc->config_table.dl_ctrl_len);
628 DBG3("ul_start: 0x%04X, %d", dc->config_table.ul_start,
629 dc->config_table.ul_start);
630 DBG3("ul_mdm_len[0]: 0x%04X, %d", dc->config_table.ul_mdm_len1,
631 dc->config_table.ul_mdm_len1);
632 DBG3("ul_mdm_len[1]: 0x%04X, %d", dc->config_table.ul_mdm_len2,
633 dc->config_table.ul_mdm_len2);
634 DBG3("ul_diag_len: 0x%04X, %d", dc->config_table.ul_diag_len,
635 dc->config_table.ul_diag_len);
636 DBG3("ul_app1_len: 0x%04X, %d", dc->config_table.ul_app1_len,
637 dc->config_table.ul_app1_len);
638 DBG3("ul_app2_len: 0x%04X, %d", dc->config_table.ul_app2_len,
639 dc->config_table.ul_app2_len);
640 DBG3("ul_ctrl_len: 0x%04X, %d", dc->config_table.ul_ctrl_len,
641 dc->config_table.ul_ctrl_len);
642}
643#else
e9ed537a 644static inline void dump_table(const struct nozomi *dc) { }
20fd1e3b
FS
645#endif
646
647/*
648 * Read configuration table from card under intalization phase
649 * Returns 1 if ok, else 0
650 */
651static int nozomi_read_config_table(struct nozomi *dc)
652{
653 read_mem32((u32 *) &dc->config_table, dc->base_addr + 0,
654 sizeof(struct config_table));
655
656 if (dc->config_table.signature != CONFIG_MAGIC) {
657 dev_err(&dc->pdev->dev, "ConfigTable Bad! 0x%08X != 0x%08X\n",
658 dc->config_table.signature, CONFIG_MAGIC);
659 return 0;
660 }
661
662 if ((dc->config_table.version == 0)
663 || (dc->config_table.toggle.enabled == TOGGLE_VALID)) {
664 int i;
665 DBG1("Second phase, configuring card");
666
667 setup_memory(dc);
668
669 dc->port[PORT_MDM].toggle_ul = dc->config_table.toggle.mdm_ul;
670 dc->port[PORT_MDM].toggle_dl = dc->config_table.toggle.mdm_dl;
671 dc->port[PORT_DIAG].toggle_dl = dc->config_table.toggle.diag_dl;
672 DBG1("toggle ports: MDM UL:%d MDM DL:%d, DIAG DL:%d",
673 dc->port[PORT_MDM].toggle_ul,
674 dc->port[PORT_MDM].toggle_dl, dc->port[PORT_DIAG].toggle_dl);
675
676 dump_table(dc);
677
678 for (i = PORT_MDM; i < MAX_PORT; i++) {
679 dc->port[i].fifo_ul =
680 kfifo_alloc(FIFO_BUFFER_SIZE_UL, GFP_ATOMIC, NULL);
681 memset(&dc->port[i].ctrl_dl, 0, sizeof(struct ctrl_dl));
682 memset(&dc->port[i].ctrl_ul, 0, sizeof(struct ctrl_ul));
683 }
684
685 /* Enable control channel */
686 dc->last_ier = dc->last_ier | CTRL_DL;
687 writew(dc->last_ier, dc->reg_ier);
688
689 dev_info(&dc->pdev->dev, "Initialization OK!\n");
690 return 1;
691 }
692
693 if ((dc->config_table.version > 0)
694 && (dc->config_table.toggle.enabled != TOGGLE_VALID)) {
695 u32 offset = 0;
696 DBG1("First phase: pushing upload buffers, clearing download");
697
698 dev_info(&dc->pdev->dev, "Version of card: %d\n",
699 dc->config_table.version);
700
701 /* Here we should disable all I/O over F32. */
702 setup_memory(dc);
703
704 /*
705 * We should send ALL channel pair tokens back along
706 * with reset token
707 */
708
709 /* push upload modem buffers */
710 write_mem32(dc->port[PORT_MDM].ul_addr[CH_A],
711 (u32 *) &offset, 4);
712 write_mem32(dc->port[PORT_MDM].ul_addr[CH_B],
713 (u32 *) &offset, 4);
714
715 writew(MDM_UL | DIAG_DL | MDM_DL, dc->reg_fcr);
716
717 DBG1("First phase done");
718 }
719
720 return 1;
721}
722
723/* Enable uplink interrupts */
724static void enable_transmit_ul(enum port_type port, struct nozomi *dc)
725{
726 u16 mask[NOZOMI_MAX_PORTS] = \
727 {MDM_UL, DIAG_UL, APP1_UL, APP2_UL, CTRL_UL};
728
729 if (port < NOZOMI_MAX_PORTS) {
730 dc->last_ier |= mask[port];
731 writew(dc->last_ier, dc->reg_ier);
732 } else {
733 dev_err(&dc->pdev->dev, "Called with wrong port?\n");
734 }
735}
736
737/* Disable uplink interrupts */
738static void disable_transmit_ul(enum port_type port, struct nozomi *dc)
739{
740 u16 mask[NOZOMI_MAX_PORTS] = \
741 {~MDM_UL, ~DIAG_UL, ~APP1_UL, ~APP2_UL, ~CTRL_UL};
742
743 if (port < NOZOMI_MAX_PORTS) {
744 dc->last_ier &= mask[port];
745 writew(dc->last_ier, dc->reg_ier);
746 } else {
747 dev_err(&dc->pdev->dev, "Called with wrong port?\n");
748 }
749}
750
751/* Enable downlink interrupts */
752static void enable_transmit_dl(enum port_type port, struct nozomi *dc)
753{
754 u16 mask[NOZOMI_MAX_PORTS] = \
755 {MDM_DL, DIAG_DL, APP1_DL, APP2_DL, CTRL_DL};
756
757 if (port < NOZOMI_MAX_PORTS) {
758 dc->last_ier |= mask[port];
759 writew(dc->last_ier, dc->reg_ier);
760 } else {
761 dev_err(&dc->pdev->dev, "Called with wrong port?\n");
762 }
763}
764
765/* Disable downlink interrupts */
766static void disable_transmit_dl(enum port_type port, struct nozomi *dc)
767{
768 u16 mask[NOZOMI_MAX_PORTS] = \
769 {~MDM_DL, ~DIAG_DL, ~APP1_DL, ~APP2_DL, ~CTRL_DL};
770
771 if (port < NOZOMI_MAX_PORTS) {
772 dc->last_ier &= mask[port];
773 writew(dc->last_ier, dc->reg_ier);
774 } else {
775 dev_err(&dc->pdev->dev, "Called with wrong port?\n");
776 }
777}
778
779/*
780 * Return 1 - send buffer to card and ack.
781 * Return 0 - don't ack, don't send buffer to card.
782 */
783static int send_data(enum port_type index, struct nozomi *dc)
784{
785 u32 size = 0;
786 struct port *port = &dc->port[index];
787 u8 toggle = port->toggle_ul;
788 void __iomem *addr = port->ul_addr[toggle];
789 u32 ul_size = port->ul_size[toggle];
790 struct tty_struct *tty = port->tty;
791
792 /* Get data from tty and place in buf for now */
793 size = __kfifo_get(port->fifo_ul, dc->send_buf,
794 ul_size < SEND_BUF_MAX ? ul_size : SEND_BUF_MAX);
795
796 if (size == 0) {
797 DBG4("No more data to send, disable link:");
798 return 0;
799 }
800
801 /* DUMP(buf, size); */
802
803 /* Write length + data */
804 write_mem32(addr, (u32 *) &size, 4);
805 write_mem32(addr + 4, (u32 *) dc->send_buf, size);
806
807 if (tty)
808 tty_wakeup(tty);
809
810 return 1;
811}
812
813/* If all data has been read, return 1, else 0 */
814static int receive_data(enum port_type index, struct nozomi *dc)
815{
816 u8 buf[RECEIVE_BUF_MAX] = { 0 };
817 int size;
818 u32 offset = 4;
819 struct port *port = &dc->port[index];
820 void __iomem *addr = port->dl_addr[port->toggle_dl];
821 struct tty_struct *tty = port->tty;
822 int i;
823
824 if (unlikely(!tty)) {
825 DBG1("tty not open for port: %d?", index);
826 return 1;
827 }
828
829 read_mem32((u32 *) &size, addr, 4);
830 /* DBG1( "%d bytes port: %d", size, index); */
831
832 if (test_bit(TTY_THROTTLED, &tty->flags)) {
833 DBG1("No room in tty, don't read data, don't ack interrupt, "
834 "disable interrupt");
835
836 /* disable interrupt in downlink... */
837 disable_transmit_dl(index, dc);
838 return 0;
839 }
840
841 if (unlikely(size == 0)) {
842 dev_err(&dc->pdev->dev, "size == 0?\n");
843 return 1;
844 }
845
846 tty_buffer_request_room(tty, size);
847
848 while (size > 0) {
849 read_mem32((u32 *) buf, addr + offset, RECEIVE_BUF_MAX);
850
851 if (size == 1) {
852 tty_insert_flip_char(tty, buf[0], TTY_NORMAL);
853 size = 0;
854 } else if (size < RECEIVE_BUF_MAX) {
855 size -= tty_insert_flip_string(tty, (char *) buf, size);
856 } else {
857 i = tty_insert_flip_string(tty, \
858 (char *) buf, RECEIVE_BUF_MAX);
859 size -= i;
860 offset += i;
861 }
862 }
863
864 set_bit(index, &dc->flip);
865
866 return 1;
867}
868
869/* Debug for interrupts */
870#ifdef DEBUG
871static char *interrupt2str(u16 interrupt)
872{
873 static char buf[TMP_BUF_MAX];
874 char *p = buf;
875
876 interrupt & MDM_DL1 ? p += snprintf(p, TMP_BUF_MAX, "MDM_DL1 ") : NULL;
877 interrupt & MDM_DL2 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
878 "MDM_DL2 ") : NULL;
879
880 interrupt & MDM_UL1 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
881 "MDM_UL1 ") : NULL;
882 interrupt & MDM_UL2 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
883 "MDM_UL2 ") : NULL;
884
885 interrupt & DIAG_DL1 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
886 "DIAG_DL1 ") : NULL;
887 interrupt & DIAG_DL2 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
888 "DIAG_DL2 ") : NULL;
889
890 interrupt & DIAG_UL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
891 "DIAG_UL ") : NULL;
892
893 interrupt & APP1_DL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
894 "APP1_DL ") : NULL;
895 interrupt & APP2_DL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
896 "APP2_DL ") : NULL;
897
898 interrupt & APP1_UL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
899 "APP1_UL ") : NULL;
900 interrupt & APP2_UL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
901 "APP2_UL ") : NULL;
902
903 interrupt & CTRL_DL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
904 "CTRL_DL ") : NULL;
905 interrupt & CTRL_UL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
906 "CTRL_UL ") : NULL;
907
908 interrupt & RESET ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
909 "RESET ") : NULL;
910
911 return buf;
912}
913#endif
914
915/*
916 * Receive flow control
917 * Return 1 - If ok, else 0
918 */
919static int receive_flow_control(struct nozomi *dc)
920{
921 enum port_type port = PORT_MDM;
922 struct ctrl_dl ctrl_dl;
923 struct ctrl_dl old_ctrl;
924 u16 enable_ier = 0;
925
926 read_mem32((u32 *) &ctrl_dl, dc->port[PORT_CTRL].dl_addr[CH_A], 2);
927
928 switch (ctrl_dl.port) {
929 case CTRL_CMD:
930 DBG1("The Base Band sends this value as a response to a "
931 "request for IMSI detach sent over the control "
932 "channel uplink (see section 7.6.1).");
933 break;
934 case CTRL_MDM:
935 port = PORT_MDM;
936 enable_ier = MDM_DL;
937 break;
938 case CTRL_DIAG:
939 port = PORT_DIAG;
940 enable_ier = DIAG_DL;
941 break;
942 case CTRL_APP1:
943 port = PORT_APP1;
944 enable_ier = APP1_DL;
945 break;
946 case CTRL_APP2:
947 port = PORT_APP2;
948 enable_ier = APP2_DL;
949 break;
950 default:
951 dev_err(&dc->pdev->dev,
952 "ERROR: flow control received for non-existing port\n");
953 return 0;
954 };
955
956 DBG1("0x%04X->0x%04X", *((u16 *)&dc->port[port].ctrl_dl),
957 *((u16 *)&ctrl_dl));
958
959 old_ctrl = dc->port[port].ctrl_dl;
960 dc->port[port].ctrl_dl = ctrl_dl;
961
962 if (old_ctrl.CTS == 1 && ctrl_dl.CTS == 0) {
963 DBG1("Disable interrupt (0x%04X) on port: %d",
964 enable_ier, port);
965 disable_transmit_ul(port, dc);
966
967 } else if (old_ctrl.CTS == 0 && ctrl_dl.CTS == 1) {
968
969 if (__kfifo_len(dc->port[port].fifo_ul)) {
970 DBG1("Enable interrupt (0x%04X) on port: %d",
971 enable_ier, port);
972 DBG1("Data in buffer [%d], enable transmit! ",
973 __kfifo_len(dc->port[port].fifo_ul));
974 enable_transmit_ul(port, dc);
975 } else {
976 DBG1("No data in buffer...");
977 }
978 }
979
980 if (*(u16 *)&old_ctrl == *(u16 *)&ctrl_dl) {
981 DBG1(" No change in mctrl");
982 return 1;
983 }
984 /* Update statistics */
985 if (old_ctrl.CTS != ctrl_dl.CTS)
986 dc->port[port].tty_icount.cts++;
987 if (old_ctrl.DSR != ctrl_dl.DSR)
988 dc->port[port].tty_icount.dsr++;
989 if (old_ctrl.RI != ctrl_dl.RI)
990 dc->port[port].tty_icount.rng++;
991 if (old_ctrl.DCD != ctrl_dl.DCD)
992 dc->port[port].tty_icount.dcd++;
993
994 wake_up_interruptible(&dc->port[port].tty_wait);
995
996 DBG1("port: %d DCD(%d), CTS(%d), RI(%d), DSR(%d)",
997 port,
998 dc->port[port].tty_icount.dcd, dc->port[port].tty_icount.cts,
999 dc->port[port].tty_icount.rng, dc->port[port].tty_icount.dsr);
1000
1001 return 1;
1002}
1003
1004static enum ctrl_port_type port2ctrl(enum port_type port,
1005 const struct nozomi *dc)
1006{
1007 switch (port) {
1008 case PORT_MDM:
1009 return CTRL_MDM;
1010 case PORT_DIAG:
1011 return CTRL_DIAG;
1012 case PORT_APP1:
1013 return CTRL_APP1;
1014 case PORT_APP2:
1015 return CTRL_APP2;
1016 default:
1017 dev_err(&dc->pdev->dev,
1018 "ERROR: send flow control " \
1019 "received for non-existing port\n");
1020 };
1021 return CTRL_ERROR;
1022}
1023
1024/*
1025 * Send flow control, can only update one channel at a time
1026 * Return 0 - If we have updated all flow control
1027 * Return 1 - If we need to update more flow control, ack current enable more
1028 */
1029static int send_flow_control(struct nozomi *dc)
1030{
1031 u32 i, more_flow_control_to_be_updated = 0;
1032 u16 *ctrl;
1033
1034 for (i = PORT_MDM; i < MAX_PORT; i++) {
1035 if (dc->port[i].update_flow_control) {
1036 if (more_flow_control_to_be_updated) {
1037 /* We have more flow control to be updated */
1038 return 1;
1039 }
1040 dc->port[i].ctrl_ul.port = port2ctrl(i, dc);
1041 ctrl = (u16 *)&dc->port[i].ctrl_ul;
1042 write_mem32(dc->port[PORT_CTRL].ul_addr[0], \
1043 (u32 *) ctrl, 2);
1044 dc->port[i].update_flow_control = 0;
1045 more_flow_control_to_be_updated = 1;
1046 }
1047 }
1048 return 0;
1049}
1050
1051/*
e9ed537a 1052 * Handle downlink data, ports that are handled are modem and diagnostics
20fd1e3b
FS
1053 * Return 1 - ok
1054 * Return 0 - toggle fields are out of sync
1055 */
1056static int handle_data_dl(struct nozomi *dc, enum port_type port, u8 *toggle,
1057 u16 read_iir, u16 mask1, u16 mask2)
1058{
1059 if (*toggle == 0 && read_iir & mask1) {
1060 if (receive_data(port, dc)) {
1061 writew(mask1, dc->reg_fcr);
1062 *toggle = !(*toggle);
1063 }
1064
1065 if (read_iir & mask2) {
1066 if (receive_data(port, dc)) {
1067 writew(mask2, dc->reg_fcr);
1068 *toggle = !(*toggle);
1069 }
1070 }
1071 } else if (*toggle == 1 && read_iir & mask2) {
1072 if (receive_data(port, dc)) {
1073 writew(mask2, dc->reg_fcr);
1074 *toggle = !(*toggle);
1075 }
1076
1077 if (read_iir & mask1) {
1078 if (receive_data(port, dc)) {
1079 writew(mask1, dc->reg_fcr);
1080 *toggle = !(*toggle);
1081 }
1082 }
1083 } else {
1084 dev_err(&dc->pdev->dev, "port out of sync!, toggle:%d\n",
1085 *toggle);
1086 return 0;
1087 }
1088 return 1;
1089}
1090
1091/*
1092 * Handle uplink data, this is currently for the modem port
1093 * Return 1 - ok
1094 * Return 0 - toggle field are out of sync
1095 */
1096static int handle_data_ul(struct nozomi *dc, enum port_type port, u16 read_iir)
1097{
1098 u8 *toggle = &(dc->port[port].toggle_ul);
1099
1100 if (*toggle == 0 && read_iir & MDM_UL1) {
1101 dc->last_ier &= ~MDM_UL;
1102 writew(dc->last_ier, dc->reg_ier);
1103 if (send_data(port, dc)) {
1104 writew(MDM_UL1, dc->reg_fcr);
1105 dc->last_ier = dc->last_ier | MDM_UL;
1106 writew(dc->last_ier, dc->reg_ier);
1107 *toggle = !*toggle;
1108 }
1109
1110 if (read_iir & MDM_UL2) {
1111 dc->last_ier &= ~MDM_UL;
1112 writew(dc->last_ier, dc->reg_ier);
1113 if (send_data(port, dc)) {
1114 writew(MDM_UL2, dc->reg_fcr);
1115 dc->last_ier = dc->last_ier | MDM_UL;
1116 writew(dc->last_ier, dc->reg_ier);
1117 *toggle = !*toggle;
1118 }
1119 }
1120
1121 } else if (*toggle == 1 && read_iir & MDM_UL2) {
1122 dc->last_ier &= ~MDM_UL;
1123 writew(dc->last_ier, dc->reg_ier);
1124 if (send_data(port, dc)) {
1125 writew(MDM_UL2, dc->reg_fcr);
1126 dc->last_ier = dc->last_ier | MDM_UL;
1127 writew(dc->last_ier, dc->reg_ier);
1128 *toggle = !*toggle;
1129 }
1130
1131 if (read_iir & MDM_UL1) {
1132 dc->last_ier &= ~MDM_UL;
1133 writew(dc->last_ier, dc->reg_ier);
1134 if (send_data(port, dc)) {
1135 writew(MDM_UL1, dc->reg_fcr);
1136 dc->last_ier = dc->last_ier | MDM_UL;
1137 writew(dc->last_ier, dc->reg_ier);
1138 *toggle = !*toggle;
1139 }
1140 }
1141 } else {
1142 writew(read_iir & MDM_UL, dc->reg_fcr);
1143 dev_err(&dc->pdev->dev, "port out of sync!\n");
1144 return 0;
1145 }
1146 return 1;
1147}
1148
1149static irqreturn_t interrupt_handler(int irq, void *dev_id)
1150{
1151 struct nozomi *dc = dev_id;
1152 unsigned int a;
1153 u16 read_iir;
1154
1155 if (!dc)
1156 return IRQ_NONE;
1157
1158 spin_lock(&dc->spin_mutex);
1159 read_iir = readw(dc->reg_iir);
1160
1161 /* Card removed */
1162 if (read_iir == (u16)-1)
1163 goto none;
1164 /*
1165 * Just handle interrupt enabled in IER
1166 * (by masking with dc->last_ier)
1167 */
1168 read_iir &= dc->last_ier;
1169
1170 if (read_iir == 0)
1171 goto none;
1172
1173
1174 DBG4("%s irq:0x%04X, prev:0x%04X", interrupt2str(read_iir), read_iir,
1175 dc->last_ier);
1176
1177 if (read_iir & RESET) {
1178 if (unlikely(!nozomi_read_config_table(dc))) {
1179 dc->last_ier = 0x0;
1180 writew(dc->last_ier, dc->reg_ier);
1181 dev_err(&dc->pdev->dev, "Could not read status from "
1182 "card, we should disable interface\n");
1183 } else {
1184 writew(RESET, dc->reg_fcr);
1185 }
1186 /* No more useful info if this was the reset interrupt. */
1187 goto exit_handler;
1188 }
1189 if (read_iir & CTRL_UL) {
1190 DBG1("CTRL_UL");
1191 dc->last_ier &= ~CTRL_UL;
1192 writew(dc->last_ier, dc->reg_ier);
1193 if (send_flow_control(dc)) {
1194 writew(CTRL_UL, dc->reg_fcr);
1195 dc->last_ier = dc->last_ier | CTRL_UL;
1196 writew(dc->last_ier, dc->reg_ier);
1197 }
1198 }
1199 if (read_iir & CTRL_DL) {
1200 receive_flow_control(dc);
1201 writew(CTRL_DL, dc->reg_fcr);
1202 }
1203 if (read_iir & MDM_DL) {
1204 if (!handle_data_dl(dc, PORT_MDM,
1205 &(dc->port[PORT_MDM].toggle_dl), read_iir,
1206 MDM_DL1, MDM_DL2)) {
1207 dev_err(&dc->pdev->dev, "MDM_DL out of sync!\n");
1208 goto exit_handler;
1209 }
1210 }
1211 if (read_iir & MDM_UL) {
1212 if (!handle_data_ul(dc, PORT_MDM, read_iir)) {
1213 dev_err(&dc->pdev->dev, "MDM_UL out of sync!\n");
1214 goto exit_handler;
1215 }
1216 }
1217 if (read_iir & DIAG_DL) {
1218 if (!handle_data_dl(dc, PORT_DIAG,
1219 &(dc->port[PORT_DIAG].toggle_dl), read_iir,
1220 DIAG_DL1, DIAG_DL2)) {
1221 dev_err(&dc->pdev->dev, "DIAG_DL out of sync!\n");
1222 goto exit_handler;
1223 }
1224 }
1225 if (read_iir & DIAG_UL) {
1226 dc->last_ier &= ~DIAG_UL;
1227 writew(dc->last_ier, dc->reg_ier);
1228 if (send_data(PORT_DIAG, dc)) {
1229 writew(DIAG_UL, dc->reg_fcr);
1230 dc->last_ier = dc->last_ier | DIAG_UL;
1231 writew(dc->last_ier, dc->reg_ier);
1232 }
1233 }
1234 if (read_iir & APP1_DL) {
1235 if (receive_data(PORT_APP1, dc))
1236 writew(APP1_DL, dc->reg_fcr);
1237 }
1238 if (read_iir & APP1_UL) {
1239 dc->last_ier &= ~APP1_UL;
1240 writew(dc->last_ier, dc->reg_ier);
1241 if (send_data(PORT_APP1, dc)) {
1242 writew(APP1_UL, dc->reg_fcr);
1243 dc->last_ier = dc->last_ier | APP1_UL;
1244 writew(dc->last_ier, dc->reg_ier);
1245 }
1246 }
1247 if (read_iir & APP2_DL) {
1248 if (receive_data(PORT_APP2, dc))
1249 writew(APP2_DL, dc->reg_fcr);
1250 }
1251 if (read_iir & APP2_UL) {
1252 dc->last_ier &= ~APP2_UL;
1253 writew(dc->last_ier, dc->reg_ier);
1254 if (send_data(PORT_APP2, dc)) {
1255 writew(APP2_UL, dc->reg_fcr);
1256 dc->last_ier = dc->last_ier | APP2_UL;
1257 writew(dc->last_ier, dc->reg_ier);
1258 }
1259 }
1260
1261exit_handler:
1262 spin_unlock(&dc->spin_mutex);
1263 for (a = 0; a < NOZOMI_MAX_PORTS; a++)
1264 if (test_and_clear_bit(a, &dc->flip))
1265 tty_flip_buffer_push(dc->port[a].tty);
1266 return IRQ_HANDLED;
1267none:
1268 spin_unlock(&dc->spin_mutex);
1269 return IRQ_NONE;
1270}
1271
1272static void nozomi_get_card_type(struct nozomi *dc)
1273{
1274 int i;
1275 u32 size = 0;
1276
1277 for (i = 0; i < 6; i++)
1278 size += pci_resource_len(dc->pdev, i);
1279
1280 /* Assume card type F32_8 if no match */
1281 dc->card_type = size == 2048 ? F32_2 : F32_8;
1282
1283 dev_info(&dc->pdev->dev, "Card type is: %d\n", dc->card_type);
1284}
1285
1286static void nozomi_setup_private_data(struct nozomi *dc)
1287{
1288 void __iomem *offset = dc->base_addr + dc->card_type / 2;
1289 unsigned int i;
1290
1291 dc->reg_fcr = (void __iomem *)(offset + R_FCR);
1292 dc->reg_iir = (void __iomem *)(offset + R_IIR);
1293 dc->reg_ier = (void __iomem *)(offset + R_IER);
1294 dc->last_ier = 0;
1295 dc->flip = 0;
1296
1297 dc->port[PORT_MDM].token_dl = MDM_DL;
1298 dc->port[PORT_DIAG].token_dl = DIAG_DL;
1299 dc->port[PORT_APP1].token_dl = APP1_DL;
1300 dc->port[PORT_APP2].token_dl = APP2_DL;
1301
1302 for (i = 0; i < MAX_PORT; i++)
1303 init_waitqueue_head(&dc->port[i].tty_wait);
1304}
1305
1306static ssize_t card_type_show(struct device *dev, struct device_attribute *attr,
1307 char *buf)
1308{
1309 struct nozomi *dc = pci_get_drvdata(to_pci_dev(dev));
1310
1311 return sprintf(buf, "%d\n", dc->card_type);
1312}
e9ed537a 1313static DEVICE_ATTR(card_type, S_IRUGO, card_type_show, NULL);
20fd1e3b
FS
1314
1315static ssize_t open_ttys_show(struct device *dev, struct device_attribute *attr,
1316 char *buf)
1317{
1318 struct nozomi *dc = pci_get_drvdata(to_pci_dev(dev));
1319
1320 return sprintf(buf, "%u\n", dc->open_ttys);
1321}
e9ed537a 1322static DEVICE_ATTR(open_ttys, S_IRUGO, open_ttys_show, NULL);
20fd1e3b
FS
1323
1324static void make_sysfs_files(struct nozomi *dc)
1325{
1326 if (device_create_file(&dc->pdev->dev, &dev_attr_card_type))
1327 dev_err(&dc->pdev->dev,
1328 "Could not create sysfs file for card_type\n");
1329 if (device_create_file(&dc->pdev->dev, &dev_attr_open_ttys))
1330 dev_err(&dc->pdev->dev,
1331 "Could not create sysfs file for open_ttys\n");
1332}
1333
1334static void remove_sysfs_files(struct nozomi *dc)
1335{
1336 device_remove_file(&dc->pdev->dev, &dev_attr_card_type);
1337 device_remove_file(&dc->pdev->dev, &dev_attr_open_ttys);
1338}
1339
1340/* Allocate memory for one device */
1341static int __devinit nozomi_card_init(struct pci_dev *pdev,
1342 const struct pci_device_id *ent)
1343{
1344 resource_size_t start;
1345 int ret;
1346 struct nozomi *dc = NULL;
1347 int ndev_idx;
1348 int i;
1349
1350 dev_dbg(&pdev->dev, "Init, new card found\n");
1351
1352 for (ndev_idx = 0; ndev_idx < ARRAY_SIZE(ndevs); ndev_idx++)
1353 if (!ndevs[ndev_idx])
1354 break;
1355
1356 if (ndev_idx >= ARRAY_SIZE(ndevs)) {
1357 dev_err(&pdev->dev, "no free tty range for this card left\n");
1358 ret = -EIO;
1359 goto err;
1360 }
1361
1362 dc = kzalloc(sizeof(struct nozomi), GFP_KERNEL);
1363 if (unlikely(!dc)) {
1364 dev_err(&pdev->dev, "Could not allocate memory\n");
1365 ret = -ENOMEM;
1366 goto err_free;
1367 }
1368
1369 dc->pdev = pdev;
1370
1371 /* Find out what card type it is */
1372 nozomi_get_card_type(dc);
1373
1374 ret = pci_enable_device(dc->pdev);
1375 if (ret) {
1376 dev_err(&pdev->dev, "Failed to enable PCI Device\n");
1377 goto err_free;
1378 }
1379
1380 start = pci_resource_start(dc->pdev, 0);
1381 if (start == 0) {
1382 dev_err(&pdev->dev, "No I/O address for card detected\n");
1383 ret = -ENODEV;
1384 goto err_disable_device;
1385 }
1386
1387 ret = pci_request_regions(dc->pdev, NOZOMI_NAME);
1388 if (ret) {
1389 dev_err(&pdev->dev, "I/O address 0x%04x already in use\n",
1390 (int) /* nozomi_private.io_addr */ 0);
1391 goto err_disable_device;
1392 }
1393
1394 dc->base_addr = ioremap(start, dc->card_type);
1395 if (!dc->base_addr) {
1396 dev_err(&pdev->dev, "Unable to map card MMIO\n");
1397 ret = -ENODEV;
1398 goto err_rel_regs;
1399 }
1400
1401 dc->send_buf = kmalloc(SEND_BUF_MAX, GFP_KERNEL);
1402 if (!dc->send_buf) {
1403 dev_err(&pdev->dev, "Could not allocate send buffer?\n");
1404 ret = -ENOMEM;
1405 goto err_free_sbuf;
1406 }
1407
1408 spin_lock_init(&dc->spin_mutex);
1409
1410 nozomi_setup_private_data(dc);
1411
1412 /* Disable all interrupts */
1413 dc->last_ier = 0;
1414 writew(dc->last_ier, dc->reg_ier);
1415
1416 ret = request_irq(pdev->irq, &interrupt_handler, IRQF_SHARED,
1417 NOZOMI_NAME, dc);
1418 if (unlikely(ret)) {
1419 dev_err(&pdev->dev, "can't request irq %d\n", pdev->irq);
1420 goto err_free_sbuf;
1421 }
1422
1423 DBG1("base_addr: %p", dc->base_addr);
1424
1425 make_sysfs_files(dc);
1426
1427 dc->index_start = ndev_idx * MAX_PORT;
1428 ndevs[ndev_idx] = dc;
1429
1430 for (i = 0; i < MAX_PORT; i++) {
1431 mutex_init(&dc->port[i].tty_sem);
1432 dc->port[i].tty_open_count = 0;
1433 dc->port[i].tty = NULL;
1434 tty_register_device(ntty_driver, dc->index_start + i,
1435 &pdev->dev);
1436 }
1437
1438 /* Enable RESET interrupt. */
1439 dc->last_ier = RESET;
1440 writew(dc->last_ier, dc->reg_ier);
1441
1442 pci_set_drvdata(pdev, dc);
1443
1444 return 0;
1445
1446err_free_sbuf:
1447 kfree(dc->send_buf);
1448 iounmap(dc->base_addr);
1449err_rel_regs:
1450 pci_release_regions(pdev);
1451err_disable_device:
1452 pci_disable_device(pdev);
1453err_free:
1454 kfree(dc);
1455err:
1456 return ret;
1457}
1458
1459static void __devexit tty_exit(struct nozomi *dc)
1460{
1461 unsigned int i;
1462
1463 DBG1(" ");
1464
1465 flush_scheduled_work();
1466
1467 for (i = 0; i < MAX_PORT; ++i)
1468 if (dc->port[i].tty && \
1469 list_empty(&dc->port[i].tty->hangup_work.entry))
1470 tty_hangup(dc->port[i].tty);
1471
1472 while (dc->open_ttys)
1473 msleep(1);
1474
1475 for (i = dc->index_start; i < dc->index_start + MAX_PORT; ++i)
1476 tty_unregister_device(ntty_driver, i);
1477}
1478
1479/* Deallocate memory for one device */
1480static void __devexit nozomi_card_exit(struct pci_dev *pdev)
1481{
1482 int i;
1483 struct ctrl_ul ctrl;
1484 struct nozomi *dc = pci_get_drvdata(pdev);
1485
1486 /* Disable all interrupts */
1487 dc->last_ier = 0;
1488 writew(dc->last_ier, dc->reg_ier);
1489
1490 tty_exit(dc);
1491
1492 /* Send 0x0001, command card to resend the reset token. */
1493 /* This is to get the reset when the module is reloaded. */
1494 ctrl.port = 0x00;
1495 ctrl.reserved = 0;
1496 ctrl.RTS = 0;
1497 ctrl.DTR = 1;
1498 DBG1("sending flow control 0x%04X", *((u16 *)&ctrl));
1499
1500 /* Setup dc->reg addresses to we can use defines here */
1501 write_mem32(dc->port[PORT_CTRL].ul_addr[0], (u32 *)&ctrl, 2);
1502 writew(CTRL_UL, dc->reg_fcr); /* push the token to the card. */
1503
1504 remove_sysfs_files(dc);
1505
1506 free_irq(pdev->irq, dc);
1507
1508 for (i = 0; i < MAX_PORT; i++)
1509 if (dc->port[i].fifo_ul)
1510 kfifo_free(dc->port[i].fifo_ul);
1511
1512 kfree(dc->send_buf);
1513
1514 iounmap(dc->base_addr);
1515
1516 pci_release_regions(pdev);
1517
1518 pci_disable_device(pdev);
1519
1520 ndevs[dc->index_start / MAX_PORT] = NULL;
1521
1522 kfree(dc);
1523}
1524
1525static void set_rts(const struct tty_struct *tty, int rts)
1526{
1527 struct port *port = get_port_by_tty(tty);
1528
1529 port->ctrl_ul.RTS = rts;
1530 port->update_flow_control = 1;
1531 enable_transmit_ul(PORT_CTRL, get_dc_by_tty(tty));
1532}
1533
1534static void set_dtr(const struct tty_struct *tty, int dtr)
1535{
1536 struct port *port = get_port_by_tty(tty);
1537
1538 DBG1("SETTING DTR index: %d, dtr: %d", tty->index, dtr);
1539
1540 port->ctrl_ul.DTR = dtr;
1541 port->update_flow_control = 1;
1542 enable_transmit_ul(PORT_CTRL, get_dc_by_tty(tty));
1543}
1544
1545/*
1546 * ----------------------------------------------------------------------------
1547 * TTY code
1548 * ----------------------------------------------------------------------------
1549 */
1550
1551/* Called when the userspace process opens the tty, /dev/noz*. */
1552static int ntty_open(struct tty_struct *tty, struct file *file)
1553{
1554 struct port *port = get_port_by_tty(tty);
1555 struct nozomi *dc = get_dc_by_tty(tty);
1556 unsigned long flags;
1557
1558 if (!port || !dc)
1559 return -ENODEV;
1560
1561 if (mutex_lock_interruptible(&port->tty_sem))
1562 return -ERESTARTSYS;
1563
1564 port->tty_open_count++;
1565 dc->open_ttys++;
1566
1567 /* Enable interrupt downlink for channel */
1568 if (port->tty_open_count == 1) {
1569 tty->low_latency = 1;
1570 tty->driver_data = port;
1571 port->tty = tty;
1572 DBG1("open: %d", port->token_dl);
1573 spin_lock_irqsave(&dc->spin_mutex, flags);
1574 dc->last_ier = dc->last_ier | port->token_dl;
1575 writew(dc->last_ier, dc->reg_ier);
1576 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1577 }
1578
1579 mutex_unlock(&port->tty_sem);
1580
1581 return 0;
1582}
1583
1584/* Called when the userspace process close the tty, /dev/noz*. */
1585static void ntty_close(struct tty_struct *tty, struct file *file)
1586{
1587 struct nozomi *dc = get_dc_by_tty(tty);
1588 struct port *port = tty->driver_data;
1589 unsigned long flags;
1590
1591 if (!dc || !port)
1592 return;
1593
1594 if (mutex_lock_interruptible(&port->tty_sem))
1595 return;
1596
1597 if (!port->tty_open_count)
1598 goto exit;
1599
1600 dc->open_ttys--;
1601 port->tty_open_count--;
1602
1603 if (port->tty_open_count == 0) {
1604 DBG1("close: %d", port->token_dl);
1605 spin_lock_irqsave(&dc->spin_mutex, flags);
1606 dc->last_ier &= ~(port->token_dl);
1607 writew(dc->last_ier, dc->reg_ier);
1608 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1609 }
1610
1611exit:
1612 mutex_unlock(&port->tty_sem);
1613}
1614
1615/*
1616 * called when the userspace process writes to the tty (/dev/noz*).
1617 * Data is inserted into a fifo, which is then read and transfered to the modem.
1618 */
1619static int ntty_write(struct tty_struct *tty, const unsigned char *buffer,
1620 int count)
1621{
1622 int rval = -EINVAL;
1623 struct nozomi *dc = get_dc_by_tty(tty);
1624 struct port *port = tty->driver_data;
1625 unsigned long flags;
1626
1627 /* DBG1( "WRITEx: %d, index = %d", count, index); */
1628
1629 if (!dc || !port)
1630 return -ENODEV;
1631
1632 if (unlikely(!mutex_trylock(&port->tty_sem))) {
1633 /*
1634 * must test lock as tty layer wraps calls
1635 * to this function with BKL
1636 */
1637 dev_err(&dc->pdev->dev, "Would have deadlocked - "
1638 "return EAGAIN\n");
1639 return -EAGAIN;
1640 }
1641
1642 if (unlikely(!port->tty_open_count)) {
1643 DBG1(" ");
1644 goto exit;
1645 }
1646
1647 rval = __kfifo_put(port->fifo_ul, (unsigned char *)buffer, count);
1648
1649 /* notify card */
1650 if (unlikely(dc == NULL)) {
1651 DBG1("No device context?");
1652 goto exit;
1653 }
1654
1655 spin_lock_irqsave(&dc->spin_mutex, flags);
1656 /* CTS is only valid on the modem channel */
1657 if (port == &(dc->port[PORT_MDM])) {
1658 if (port->ctrl_dl.CTS) {
1659 DBG4("Enable interrupt");
1660 enable_transmit_ul(tty->index % MAX_PORT, dc);
1661 } else {
1662 dev_err(&dc->pdev->dev,
1663 "CTS not active on modem port?\n");
1664 }
1665 } else {
1666 enable_transmit_ul(tty->index % MAX_PORT, dc);
1667 }
1668 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1669
1670exit:
1671 mutex_unlock(&port->tty_sem);
1672 return rval;
1673}
1674
1675/*
1676 * Calculate how much is left in device
1677 * This method is called by the upper tty layer.
1678 * #according to sources N_TTY.c it expects a value >= 0 and
1679 * does not check for negative values.
1680 */
1681static int ntty_write_room(struct tty_struct *tty)
1682{
1683 struct port *port = tty->driver_data;
1684 int room = 0;
1685 struct nozomi *dc = get_dc_by_tty(tty);
1686
1687 if (!dc || !port)
1688 return 0;
1689 if (!mutex_trylock(&port->tty_sem))
1690 return 0;
1691
1692 if (!port->tty_open_count)
1693 goto exit;
1694
1695 room = port->fifo_ul->size - __kfifo_len(port->fifo_ul);
1696
1697exit:
1698 mutex_unlock(&port->tty_sem);
1699 return room;
1700}
1701
1702/* Gets io control parameters */
1703static int ntty_tiocmget(struct tty_struct *tty, struct file *file)
1704{
1705 struct port *port = tty->driver_data;
1706 struct ctrl_dl *ctrl_dl = &port->ctrl_dl;
1707 struct ctrl_ul *ctrl_ul = &port->ctrl_ul;
1708
1709 return (ctrl_ul->RTS ? TIOCM_RTS : 0) |
1710 (ctrl_ul->DTR ? TIOCM_DTR : 0) |
1711 (ctrl_dl->DCD ? TIOCM_CAR : 0) |
1712 (ctrl_dl->RI ? TIOCM_RNG : 0) |
1713 (ctrl_dl->DSR ? TIOCM_DSR : 0) |
1714 (ctrl_dl->CTS ? TIOCM_CTS : 0);
1715}
1716
1717/* Sets io controls parameters */
1718static int ntty_tiocmset(struct tty_struct *tty, struct file *file,
1719 unsigned int set, unsigned int clear)
1720{
1721 if (set & TIOCM_RTS)
1722 set_rts(tty, 1);
1723 else if (clear & TIOCM_RTS)
1724 set_rts(tty, 0);
1725
1726 if (set & TIOCM_DTR)
1727 set_dtr(tty, 1);
1728 else if (clear & TIOCM_DTR)
1729 set_dtr(tty, 0);
1730
1731 return 0;
1732}
1733
1734static int ntty_cflags_changed(struct port *port, unsigned long flags,
1735 struct async_icount *cprev)
1736{
1737 struct async_icount cnow = port->tty_icount;
1738 int ret;
1739
1740 ret = ((flags & TIOCM_RNG) && (cnow.rng != cprev->rng)) ||
1741 ((flags & TIOCM_DSR) && (cnow.dsr != cprev->dsr)) ||
1742 ((flags & TIOCM_CD) && (cnow.dcd != cprev->dcd)) ||
1743 ((flags & TIOCM_CTS) && (cnow.cts != cprev->cts));
1744
1745 *cprev = cnow;
1746
1747 return ret;
1748}
1749
1750static int ntty_ioctl_tiocgicount(struct port *port, void __user *argp)
1751{
1752 struct async_icount cnow = port->tty_icount;
1753 struct serial_icounter_struct icount;
1754
1755 icount.cts = cnow.cts;
1756 icount.dsr = cnow.dsr;
1757 icount.rng = cnow.rng;
1758 icount.dcd = cnow.dcd;
1759 icount.rx = cnow.rx;
1760 icount.tx = cnow.tx;
1761 icount.frame = cnow.frame;
1762 icount.overrun = cnow.overrun;
1763 icount.parity = cnow.parity;
1764 icount.brk = cnow.brk;
1765 icount.buf_overrun = cnow.buf_overrun;
1766
1767 return copy_to_user(argp, &icount, sizeof(icount));
1768}
1769
1770static int ntty_ioctl(struct tty_struct *tty, struct file *file,
1771 unsigned int cmd, unsigned long arg)
1772{
1773 struct port *port = tty->driver_data;
1774 void __user *argp = (void __user *)arg;
1775 int rval = -ENOIOCTLCMD;
1776
1777 DBG1("******** IOCTL, cmd: %d", cmd);
1778
1779 switch (cmd) {
1780 case TIOCMIWAIT: {
1781 struct async_icount cprev = port->tty_icount;
1782
1783 rval = wait_event_interruptible(port->tty_wait,
1784 ntty_cflags_changed(port, arg, &cprev));
1785 break;
1786 } case TIOCGICOUNT:
1787 rval = ntty_ioctl_tiocgicount(port, argp);
1788 break;
1789 default:
1790 DBG1("ERR: 0x%08X, %d", cmd, cmd);
1791 break;
1792 };
1793
1794 return rval;
1795}
1796
1797/*
1798 * Called by the upper tty layer when tty buffers are ready
1799 * to receive data again after a call to throttle.
1800 */
1801static void ntty_unthrottle(struct tty_struct *tty)
1802{
1803 struct nozomi *dc = get_dc_by_tty(tty);
1804 unsigned long flags;
1805
1806 DBG1("UNTHROTTLE");
1807 spin_lock_irqsave(&dc->spin_mutex, flags);
1808 enable_transmit_dl(tty->index % MAX_PORT, dc);
1809 set_rts(tty, 1);
1810
1811 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1812}
1813
1814/*
1815 * Called by the upper tty layer when the tty buffers are almost full.
1816 * The driver should stop send more data.
1817 */
1818static void ntty_throttle(struct tty_struct *tty)
1819{
1820 struct nozomi *dc = get_dc_by_tty(tty);
1821 unsigned long flags;
1822
1823 DBG1("THROTTLE");
1824 spin_lock_irqsave(&dc->spin_mutex, flags);
1825 set_rts(tty, 0);
1826 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1827}
1828
1829/* just to discard single character writes */
1830static void ntty_put_char(struct tty_struct *tty, unsigned char c)
1831{
e9ed537a
FS
1832 /*
1833 * card does not react correct when we write single chars
1834 * to the card, so we discard them
1835 */
20fd1e3b
FS
1836 DBG2("PUT CHAR Function: %c", c);
1837}
1838
1839/* Returns number of chars in buffer, called by tty layer */
1840static s32 ntty_chars_in_buffer(struct tty_struct *tty)
1841{
1842 struct port *port = tty->driver_data;
1843 struct nozomi *dc = get_dc_by_tty(tty);
1844 s32 rval;
1845
1846 if (unlikely(!dc || !port)) {
1847 rval = -ENODEV;
1848 goto exit_in_buffer;
1849 }
1850
1851 if (unlikely(!port->tty_open_count)) {
1852 dev_err(&dc->pdev->dev, "No tty open?\n");
1853 rval = -ENODEV;
1854 goto exit_in_buffer;
1855 }
1856
1857 rval = __kfifo_len(port->fifo_ul);
1858
1859exit_in_buffer:
1860 return rval;
1861}
1862
1863static struct tty_operations tty_ops = {
1864 .ioctl = ntty_ioctl,
1865 .open = ntty_open,
1866 .close = ntty_close,
1867 .write = ntty_write,
1868 .write_room = ntty_write_room,
1869 .unthrottle = ntty_unthrottle,
1870 .throttle = ntty_throttle,
1871 .chars_in_buffer = ntty_chars_in_buffer,
1872 .put_char = ntty_put_char,
1873 .tiocmget = ntty_tiocmget,
1874 .tiocmset = ntty_tiocmset,
1875};
1876
1877/* Module initialization */
1878static struct pci_driver nozomi_driver = {
1879 .name = NOZOMI_NAME,
1880 .id_table = nozomi_pci_tbl,
1881 .probe = nozomi_card_init,
1882 .remove = __devexit_p(nozomi_card_exit),
1883};
1884
1885static __init int nozomi_init(void)
1886{
1887 int ret;
1888
1889 printk(KERN_INFO "Initializing %s\n", VERSION_STRING);
1890
1891 ntty_driver = alloc_tty_driver(NTTY_TTY_MAXMINORS);
1892 if (!ntty_driver)
1893 return -ENOMEM;
1894
1895 ntty_driver->owner = THIS_MODULE;
1896 ntty_driver->driver_name = NOZOMI_NAME_TTY;
1897 ntty_driver->name = "noz";
1898 ntty_driver->major = 0;
1899 ntty_driver->type = TTY_DRIVER_TYPE_SERIAL;
1900 ntty_driver->subtype = SERIAL_TYPE_NORMAL;
1901 ntty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
1902 ntty_driver->init_termios = tty_std_termios;
1903 ntty_driver->init_termios.c_cflag = B115200 | CS8 | CREAD | \
1904 HUPCL | CLOCAL;
1905 ntty_driver->init_termios.c_ispeed = 115200;
1906 ntty_driver->init_termios.c_ospeed = 115200;
1907 tty_set_operations(ntty_driver, &tty_ops);
1908
1909 ret = tty_register_driver(ntty_driver);
1910 if (ret) {
1911 printk(KERN_ERR "Nozomi: failed to register ntty driver\n");
1912 goto free_tty;
1913 }
1914
1915 ret = pci_register_driver(&nozomi_driver);
1916 if (ret) {
1917 printk(KERN_ERR "Nozomi: can't register pci driver\n");
1918 goto unr_tty;
1919 }
1920
1921 return 0;
1922unr_tty:
1923 tty_unregister_driver(ntty_driver);
1924free_tty:
1925 put_tty_driver(ntty_driver);
1926 return ret;
1927}
1928
1929static __exit void nozomi_exit(void)
1930{
1931 printk(KERN_INFO "Unloading %s\n", DRIVER_DESC);
1932 pci_unregister_driver(&nozomi_driver);
1933 tty_unregister_driver(ntty_driver);
1934 put_tty_driver(ntty_driver);
1935}
1936
1937module_init(nozomi_init);
1938module_exit(nozomi_exit);
1939
1940module_param(debug, int, S_IRUGO | S_IWUSR);
1941
1942MODULE_LICENSE("Dual BSD/GPL");
1943MODULE_DESCRIPTION(DRIVER_DESC);