Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
View | Details | Raw Unified | Return to bug 144913 | Differences between
and this patch

Collapse All | Expand All

(-)file_not_specified_in_diff (-4 / +2362 lines)
Line  Link Here
--
1
drivers/char/Kconfig  |   10 
drivers/char/Kconfig  |   10 
2
drivers/char/Makefile |    1 
1
drivers/char/Makefile |    1 
3
drivers/char/nozomi.c | 2348 ++++++++++++++++++++++++++++++++++++++++++++++++++
2
drivers/char/nozomi.c | 2348 ++++++++++++++++++++++++++++++++++++++++++++++++++
4
3 files changed, 2359 insertions(+)
3
3 files changed, 2359 insertions(+)
5
-- a/drivers/char/Kconfig
4
++ b/drivers/char/Kconfig
Lines 373-378 config ISTALLION Link Here
373
	  To compile this driver as a module, choose M here: the
373
	  To compile this driver as a module, choose M here: the
374
	  module will be called istallion.
374
	  module will be called istallion.
375
375
376
config NOZOMI
377
	tristate "HSDPA Broadband Wireless Data Card - Globe Trotter"
378
	depends on PCI && EXPERIMENTAL
379
	help
380
	  If you have a HSDPA driver Broadband Wireless Data Card -
381
	  Globe Trotter PCMCIA card, say Y here.
382
383
	  To compile this driver as a module, choose M here, the module
384
	  will be called nozomi.
385
376
config A2232
386
config A2232
377
	tristate "Commodore A2232 serial support (EXPERIMENTAL)"
387
	tristate "Commodore A2232 serial support (EXPERIMENTAL)"
378
	depends on EXPERIMENTAL && ZORRO && BROKEN_ON_SMP
388
	depends on EXPERIMENTAL && ZORRO && BROKEN_ON_SMP
379
-- a/drivers/char/Makefile
389
++ b/drivers/char/Makefile
Lines 26-31 obj-$(CONFIG_SERIAL167) += serial167.o Link Here
26
obj-$(CONFIG_CYCLADES)		+= cyclades.o
26
obj-$(CONFIG_CYCLADES)		+= cyclades.o
27
obj-$(CONFIG_STALLION)		+= stallion.o
27
obj-$(CONFIG_STALLION)		+= stallion.o
28
obj-$(CONFIG_ISTALLION)		+= istallion.o
28
obj-$(CONFIG_ISTALLION)		+= istallion.o
29
obj-$(CONFIG_NOZOMI)		+= nozomi.o
29
obj-$(CONFIG_DIGIEPCA)		+= epca.o
30
obj-$(CONFIG_DIGIEPCA)		+= epca.o
30
obj-$(CONFIG_SPECIALIX)		+= specialix.o
31
obj-$(CONFIG_SPECIALIX)		+= specialix.o
31
obj-$(CONFIG_MOXA_INTELLIO)	+= moxa.o
32
obj-$(CONFIG_MOXA_INTELLIO)	+= moxa.o
32
-- /dev/null
33
++ b/drivers/char/nozomi.c
Line 0 Link Here
1
/*
2
 * nozomi.c  -- HSDPA driver Broadband Wireless Data Card - Globe Trotter
3
 *
4
 * Written by: Ulf Jakobsson,
5
 *             Jan �erfeldt,
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
41
/*
42
 * CHANGELOG
43
 * Version 2.1b
44
 * 07-August-2007 Frank Seidel
45
 * - Minor cleanups
46
 * - theoretical multicard support
47
 *
48
 * Version 2.1
49
 * 03-July-2006 Paul Hardwick
50
 *
51
 * - Stability Improvements. Incorporated spinlock wraps patch.
52
 * - Updated for newer 2.6.14+ kernels (tty_buffer_request_room)
53
 * - using __devexit macro for tty
54
 *
55
 *
56
 * Version 2.0
57
 * 08-feb-2006 15:34:10:Ulf
58
 *
59
 * -Fixed issue when not waking up line disipine layer, could probably result
60
 *  in better uplink performance for 2.4.
61
 *
62
 * -Fixed issue with big endian during initalization, now proper toggle flags
63
 *  are handled between preloader and maincode.
64
 *
65
 * -Fixed flow control issue.
66
 *
67
 * -Added support for setting DTR.
68
 *
69
 * -For 2.4 kernels, removing temporary buffer that's not needed.
70
 *
71
 * -Reading CTS only for modem port (only port that supports it).
72
 *
73
 * -Return 0 in write_room instead of netative value, it's not handled in
74
 *  upper layer.
75
 *
76
 * --------------------------------------------------------------------------
77
 * Version 1.0
78
 *
79
 * First version of driver, only tested with card of type F32_2.
80
 * Works fine with 2.4 and 2.6 kernels.
81
 * Driver also support big endian architecture.
82
 */
83
84
/* Enable this to have a lot of debug printouts */
85
#define DEBUG
86
87
88
#include <linux/module.h>
89
#include <linux/pci.h>
90
#include <linux/ioport.h>
91
#include <linux/tty.h>
92
#include <linux/tty_driver.h>
93
#include <linux/tty_flip.h>
94
#include <linux/serial.h>
95
#include <linux/interrupt.h>
96
#include <linux/kmod.h>
97
#include <linux/init.h>
98
#include <linux/kfifo.h>
99
#include <linux/list.h>
100
#include <linux/uaccess.h>
101
#include <asm/atomic.h>
102
103
#include <linux/delay.h>
104
105
106
#define VERSION_STRING DRIVER_DESC " 2.1b (build date: " \
107
					__DATE__ " " __TIME__ ")"
108
109
/*    Macros definitions */
110
111
/* Default debug printout level */
112
#define NOZOMI_DEBUG_LEVEL 0x00
113
114
#define P_BUF_SIZE 128
115
#define NFO(_err_flag_, args...)				\
116
do {								\
117
	char tmp[P_BUF_SIZE];					\
118
	snprintf(tmp, sizeof(tmp), ##args);			\
119
	printk(_err_flag_ "[%d] %s(): %s\n", __LINE__,		\
120
		__FUNCTION__, tmp);				\
121
} while (0)
122
123
#define DBG1(args...) D_(0x01, ##args)
124
#define DBG2(args...) D_(0x02, ##args)
125
#define DBG3(args...) D_(0x04, ##args)
126
#define DBG4(args...) D_(0x08, ##args)
127
#define DBG5(args...) D_(0x10, ##args)
128
#define DBG6(args...) D_(0x20, ##args)
129
#define DBG7(args...) D_(0x40, ##args)
130
#define DBG8(args...) D_(0x80, ##args)
131
132
#ifdef DEBUG
133
/* Do we need this settable at runtime? */
134
static int debug = NOZOMI_DEBUG_LEVEL;
135
136
#define D(lvl, args...)  do {if (lvl & debug) NFO(KERN_DEBUG, ##args); } \
137
				while (0)
138
#define D_(lvl, args...) D(lvl, ##args)
139
140
/* These printouts are always printed */
141
142
#else
143
static int debug;
144
#define D_(lvl, args...)
145
#endif
146
147
/* TODO: rewrite to optimize macros... */
148
149
#define TMP_BUF_MAX 256
150
151
#define DUMP(buf__,len__) \
152
  do {  \
153
    char tbuf[TMP_BUF_MAX] = {0};\
154
    if (len__ > 1) {\
155
	snprintf(tbuf, len__ > TMP_BUF_MAX ? TMP_BUF_MAX : len__, "%s", buf__);\
156
	if (tbuf[len__-2] == '\r') {\
157
		tbuf[len__-2] = 'r';\
158
	} \
159
	DBG1("SENDING: '%s' (%d+n)", tbuf, len__);\
160
    } else {\
161
	DBG1("SENDING: '%s' (%d)", tbuf, len__);\
162
    } \
163
} while (0)
164
165
/*    Defines */
166
#define NOZOMI_NAME		"nozomi"
167
#define NOZOMI_NAME_TTY		"nozomi_tty"
168
#define DRIVER_DESC		"Nozomi driver"
169
170
#define NTTY_TTY_MAJOR		241
171
#define NTTY_TTY_MINORS		MAX_PORT
172
#define NTTY_FIFO_BUFFER_SIZE	8192
173
174
/* Must be power of 2 */
175
#define FIFO_BUFFER_SIZE_UL	8192
176
177
/* Size of tmp send buffer to card */
178
#define SEND_BUF_MAX		1024
179
#define RECEIVE_BUF_MAX		4
180
181
182
/* Define all types of vendors and devices to support */
183
#define VENDOR1		0x1931	/* Vendor Option */
184
#define DEVICE1		0x000c	/* HSDPA card */
185
186
#define R_IIR		0x0000	/* Interrupt Identity Register */
187
#define R_FCR		0x0000	/* Flow Control Register */
188
#define R_IER		0x0004	/* Interrupt Enable Register */
189
190
#define CONFIG_MAGIC	0xEFEFFEFE
191
#define TOGGLE_VALID	0x0000
192
193
/* Definition of interrupt tokens */
194
#define MDM_DL1		0x0001
195
#define MDM_UL1		0x0002
196
#define MDM_DL2		0x0004
197
#define MDM_UL2		0x0008
198
#define DIAG_DL1	0x0010
199
#define DIAG_DL2	0x0020
200
#define DIAG_UL		0x0040
201
#define APP1_DL		0x0080
202
#define APP1_UL		0x0100
203
#define APP2_DL		0x0200
204
#define APP2_UL		0x0400
205
#define CTRL_DL		0x0800
206
#define CTRL_UL		0x1000
207
#define RESET		0x8000
208
209
#define MDM_DL		(MDM_DL1  | MDM_DL2)
210
#define MDM_UL		(MDM_UL1  | MDM_UL2)
211
#define DIAG_DL		(DIAG_DL1 | DIAG_DL2)
212
213
/* modem signal definition */
214
#define CTRL_DSR	0x0001
215
#define CTRL_DCD	0x0002
216
#define CTRL_RI		0x0004
217
#define CTRL_CTS	0x0008
218
219
#define CTRL_DTR	0x0001
220
#define CTRL_RTS	0x0002
221
222
#define MAX_PORT		4
223
#define NOZOMI_MAX_PORTS	5
224
225
/*    Type definitions */
226
227
/*
228
 * There are two types of nozomi cards,
229
 * one with 2048 memory and with 8192 memory
230
 */
231
enum card_type {
232
	F32_2 = 2048,	/* 512 bytes downlink + uplink * 2 -> 2048 */
233
	F32_8 = 8192,	/* 3072 bytes downl. + 1024 bytes uplink * 2 -> 8192 */
234
};
235
236
/* Two different toggle channels exist */
237
enum channel_type {
238
	CH_A = 0,
239
	CH_B = 1,
240
};
241
242
/* Port definition for the card regarding flow control */
243
enum ctrl_port_type {
244
	CTRL_CMD	= 0,
245
	CTRL_MDM	= 1,
246
	CTRL_DIAG	= 2,
247
	CTRL_APP1	= 3,
248
	CTRL_APP2	= 4,
249
	CTRL_ERROR	= -1,
250
};
251
252
/* Ports that the nozomi has */
253
enum port_type {
254
	PORT_MDM	= 0,
255
	PORT_DIAG	= 1,
256
	PORT_APP1	= 2,
257
	PORT_APP2	= 3,
258
	PORT_CTRL	= 4,
259
	PORT_ERROR	= -1,
260
};
261
262
#ifdef __ARMEB__
263
/* Big endian */
264
265
struct toggles {
266
	unsigned enabled:5;	/*
267
				 * Toggle fields are valid if enabled is 0,
268
				 * else A-channels must always be used.
269
				 */
270
	unsigned diag_dl:1;
271
	unsigned mdm_dl:1;
272
	unsigned mdm_ul:1;
273
} __attribute__ ((packed));
274
275
/* Configuration table to read at startup of card */
276
/* Is for now only needed during initialization phase */
277
struct config_table {
278
	u32 signature;
279
	u16 product_information;
280
	u16 version;
281
	u8 pad3[3];
282
	struct toggles toggle;
283
	u8 pad1[4];
284
	u16 dl_mdm_len1;	/*
285
				 * If this is 64, it can hold
286
				 * 60 bytes + 4 that is length field
287
				 */
288
	u16 dl_start;
289
290
	u16 dl_diag_len1;
291
	u16 dl_mdm_len2;	/*
292
				 * If this is 64, it can hold
293
				 * 60 bytes + 4 that is length field
294
				 */
295
	u16 dl_app1_len;
296
297
	u16 dl_diag_len2;
298
	u16 dl_ctrl_len;
299
	u16 dl_app2_len;
300
	u8 pad2[16];
301
	u16 ul_mdm_len1;
302
	u16 ul_start;
303
	u16 ul_diag_len;
304
	u16 ul_mdm_len2;
305
	u16 ul_app1_len;
306
	u16 ul_app2_len;
307
	u16 ul_ctrl_len;
308
} __attribute__ ((packed));
309
310
/* This stores all control downlink flags */
311
struct ctrl_dl {
312
	u8 port;
313
	unsigned reserved:4;
314
	unsigned CTS:1;
315
	unsigned RI:1;
316
	unsigned DCD:1;
317
	unsigned DSR:1;
318
} __attribute__ ((packed));
319
320
/* This stores all control uplink flags */
321
struct ctrl_ul {
322
	u8 port;
323
	unsigned reserved:6;
324
	unsigned RTS:1;
325
	unsigned DTR:1;
326
} __attribute__ ((packed));
327
328
#else
329
/* Little endian */
330
331
/* This represents the toggle information */
332
struct toggles {
333
	unsigned mdm_ul:1;
334
	unsigned mdm_dl:1;
335
	unsigned diag_dl:1;
336
	unsigned enabled:5;	/*
337
				 * Toggle fields are valid if enabled is 0,
338
				 * else A-channels must always be used.
339
				 */
340
} __attribute__ ((packed));
341
342
/* Configuration table to read at startup of card */
343
struct config_table {
344
	u32 signature;
345
	u16 version;
346
	u16 product_information;
347
	struct toggles toggle;
348
	u8 pad1[7];
349
	u16 dl_start;
350
	u16 dl_mdm_len1;	/*
351
				 * If this is 64, it can hold
352
				 * 60 bytes + 4 that is length field
353
				 */
354
	u16 dl_mdm_len2;
355
	u16 dl_diag_len1;
356
	u16 dl_diag_len2;
357
	u16 dl_app1_len;
358
	u16 dl_app2_len;
359
	u16 dl_ctrl_len;
360
	u8 pad2[16];
361
	u16 ul_start;
362
	u16 ul_mdm_len2;
363
	u16 ul_mdm_len1;
364
	u16 ul_diag_len;
365
	u16 ul_app1_len;
366
	u16 ul_app2_len;
367
	u16 ul_ctrl_len;
368
} __attribute__ ((packed));
369
370
/* This stores all control downlink flags */
371
struct ctrl_dl {
372
	unsigned DSR:1;
373
	unsigned DCD:1;
374
	unsigned RI:1;
375
	unsigned CTS:1;
376
	unsigned reserverd:4;
377
	u8 port;
378
} __attribute__ ((packed));
379
380
/* This stores all control uplink flags */
381
struct ctrl_ul {
382
	unsigned DTR:1;
383
	unsigned RTS:1;
384
	unsigned reserved:6;
385
	u8 port;
386
} __attribute__ ((packed));
387
#endif
388
389
/* This holds all information that is needed regarding a port */
390
struct port {
391
	u8 update_flow_control;
392
	struct ctrl_ul ctrl_ul;
393
	struct ctrl_dl ctrl_dl;
394
	struct kfifo *fifo_ul;
395
	void __iomem *dl_addr[2];
396
	u32 dl_size[2];
397
	u8 toggle_dl;
398
	void __iomem *ul_addr[2];
399
	u32 ul_size[2];
400
	u8 toggle_ul;
401
	u16 token_dl;
402
403
	struct tty_struct *tty;
404
	int tty_open_count;
405
	struct semaphore tty_sem;
406
	wait_queue_head_t tty_wait;
407
	struct async_icount tty_icount;
408
	int tty_index;
409
	u32 rx_data, tx_data;
410
	u8 tty_dont_flip;
411
412
};
413
414
/* Private data one for each card in the system */
415
struct nozomi {
416
	void __iomem *base_addr;
417
	u8 closing;
418
419
	/* Pointers to registers */
420
	void __iomem *reg_iir;
421
	void __iomem *reg_fcr;
422
	void __iomem *reg_ier;
423
424
	u16 last_ier;
425
	enum card_type card_type;
426
	struct config_table config_table;	/* Configuration table */
427
	struct pci_dev *pdev;
428
	struct port port[NOZOMI_MAX_PORTS];
429
	u8 *send_buf;
430
431
	struct tty_driver *tty_driver;
432
433
	struct workqueue_struct *tty_flip_wq;
434
	struct work_struct tty_flip_wq_struct;
435
436
	struct ktermios *tty_termios[NTTY_TTY_MINORS];
437
	struct ktermios *tty_termios_locked[NTTY_TTY_MINORS];
438
	spinlock_t spin_mutex;	/* secures access to registers and tty */
439
440
	u32 open_ttys;
441
};
442
443
/* This is a data packet that is read or written to/from card */
444
struct buffer {
445
	u32 size;		/* size is the length of the data buffer */
446
	u8 *data;
447
} __attribute__ ((packed));
448
449
/*    Function declarations */
450
static int ntty_tty_init(struct nozomi *dc);
451
452
/*    Global variables */
453
static struct pci_device_id nozomi_pci_tbl[] = {
454
	{PCI_DEVICE(VENDOR1, DEVICE1)},
455
	{},
456
};
457
458
MODULE_DEVICE_TABLE(pci, nozomi_pci_tbl);
459
460
/* Used to store interrupt variables */
461
struct irq {
462
	u16 read_iir;		/* Holds current interrupt tokens */
463
};
464
465
/* Representing the pci device of interest */
466
struct nozomi_devices {
467
	struct list_head list;
468
	struct nozomi *my_dev;
469
	struct irq my_irq;
470
	int index_start;
471
};
472
static atomic_t cards_found = ATOMIC_INIT(0);
473
static LIST_HEAD(my_devices);
474
475
/*
476
 * find card by tty_index
477
 */
478
static struct nozomi *get_dc_by_index(s32 index)
479
{
480
	struct list_head *p;
481
	struct nozomi_devices *curdev;
482
	int devidx;
483
484
	if (likely(atomic_read(&cards_found) == 1)) {
485
		curdev = list_first_entry(&my_devices,
486
					struct nozomi_devices, list);
487
		return curdev->my_dev;
488
	} else {
489
		devidx = index - (index % NTTY_TTY_MINORS);
490
491
		list_for_each(p, &my_devices) {
492
			curdev = list_entry(p, struct nozomi_devices, list);
493
			if (curdev->index_start == devidx)
494
				return curdev->my_dev;
495
		}
496
	}
497
498
	printk(KERN_ALERT "Fatal error: could not find device" \
499
		" for tty-index %d\n", index);
500
501
	return NULL;
502
}
503
504
static struct port *get_port_by_tty(const struct tty_struct *tty)
505
{
506
	struct nozomi *ndev = get_dc_by_index(tty->index);
507
	return ndev ? &ndev->port[tty->index] : NULL;
508
}
509
510
static struct nozomi *get_dc_by_tty(const struct tty_struct *tty)
511
{
512
	return get_dc_by_index(tty->index);
513
}
514
515
/*
516
 * TODO:
517
 * -Optimize
518
 * -Rewrite cleaner
519
 */
520
static void read_mem32(u32 *buf, const void __iomem *mem_addr_start,
521
			u32 size_bytes)
522
{
523
	u32 i = 0;
524
#ifdef __ARMEB__
525
	const u32 *ptr = (u32 *) mem_addr_start;
526
#else
527
	const u32 *ptr = (__force u32 *) mem_addr_start;
528
#endif
529
	u16 *buf16;
530
531
	if (unlikely(!ptr || !buf))
532
		goto out;
533
534
	/* shortcut for extremely often used cases */
535
	switch (size_bytes) {
536
	case 2:	/* 2 bytes */
537
		buf16 = (u16 *) buf;
538
#ifdef __ARMEB__
539
		*buf16 = __le16_to_cpu(readw(ptr));
540
#else
541
		*buf16 = readw((void __iomem *)ptr);
542
#endif
543
		goto out;
544
		break;
545
	case 4:	/* 4 bytes */
546
#ifdef __ARMEB__
547
		*(buf) = __le32_to_cpu(readl(ptr));
548
#else
549
		*(buf) = readl((void __iomem *)ptr);
550
#endif
551
		goto out;
552
		break;
553
	}
554
555
	while (i < size_bytes) {
556
		if (size_bytes - i == 2) {
557
			/* Handle 2 bytes in the end */
558
			buf16 = (u16 *) buf;
559
#ifdef __ARMEB__
560
			*(buf16) = __le16_to_cpu(readw(ptr));
561
#else
562
			*(buf16) = readw((void __iomem *)ptr);
563
#endif
564
			i += 2;
565
		} else {
566
			/* Read 4 bytes */
567
#ifdef __ARMEB__
568
			*(buf) = __le32_to_cpu(readl(ptr));
569
#else
570
			*(buf) = readl((void __iomem *)ptr);
571
#endif
572
			i += 4;
573
		}
574
		buf++;
575
		ptr++;
576
	}
577
out:
578
	return;
579
}
580
581
/*
582
 * TODO:
583
 * -Optimize
584
 * -Rewrite cleaner
585
 */
586
static u32 write_mem32(void __iomem *mem_addr_start, u32 *buf,
587
			u32 size_bytes)
588
{
589
	u32 i = 0;
590
#ifdef __ARMEB__
591
	u32 *ptr = (u32 *) mem_addr_start;
592
#else
593
	u32 *ptr = (__force u32 *) mem_addr_start;
594
#endif
595
	u16 *buf16;
596
597
	if (unlikely(!ptr || !buf))
598
		return 0;
599
600
	/* shortcut for extremely often used cases */
601
	switch (size_bytes) {
602
	case 2:	/* 2 bytes */
603
		buf16 = (u16 *) buf;
604
#ifdef __ARMEB__
605
		writew(__le16_to_cpu(*buf16), ptr);
606
#else
607
		writew(*buf16, (void __iomem *)ptr);
608
#endif
609
		return 2;
610
		break;
611
	case 4: /* 4 bytes */
612
#ifdef __ARMEB__
613
		writel(__cpu_to_le32(*buf), ptr);
614
#else
615
		writel(*buf, (void __iomem *)ptr);
616
#endif
617
		return 4;
618
		break;
619
	}
620
621
	while (i < size_bytes) {
622
		if (size_bytes - i == 2) {
623
			/* 2 bytes */
624
			buf16 = (u16 *) buf;
625
#ifdef __ARMEB__
626
			writew(__le16_to_cpu(*buf16), ptr);
627
#else
628
			writew(*buf16, (void __iomem *)ptr);
629
#endif
630
			i += 2;
631
		} else {
632
			/* 4 bytes */
633
#ifdef __ARMEB__
634
			writel(__cpu_to_le32(*buf), ptr);
635
#else
636
			writel(*buf, (void __iomem *)ptr);
637
#endif
638
			i += 4;
639
		}
640
		buf++;
641
		ptr++;
642
	}
643
	return i;
644
}
645
646
/* Setup pointers to different channels and also setup buffer sizes. */
647
static void setup_memory(struct nozomi *dc)
648
{
649
	void __iomem *offset = dc->base_addr + dc->config_table.dl_start;
650
	/* The length reported is including the length field of 4 bytes,
651
	 * hence subtract with 4.
652
	 */
653
	const u16 buff_offset = 4;
654
655
	/* Modem port dl configuration */
656
	dc->port[PORT_MDM].dl_addr[CH_A] = offset;
657
	dc->port[PORT_MDM].dl_addr[CH_B] =
658
				(offset += dc->config_table.dl_mdm_len1);
659
	dc->port[PORT_MDM].dl_size[CH_A] =
660
				dc->config_table.dl_mdm_len1 - buff_offset;
661
	dc->port[PORT_MDM].dl_size[CH_B] =
662
				dc->config_table.dl_mdm_len2 - buff_offset;
663
664
	/* Diag port dl configuration */
665
	dc->port[PORT_DIAG].dl_addr[CH_A] =
666
				(offset += dc->config_table.dl_mdm_len2);
667
	dc->port[PORT_DIAG].dl_size[CH_A] =
668
				dc->config_table.dl_diag_len1 - buff_offset;
669
	dc->port[PORT_DIAG].dl_addr[CH_B] =
670
				(offset += dc->config_table.dl_diag_len1);
671
	dc->port[PORT_DIAG].dl_size[CH_B] =
672
				dc->config_table.dl_diag_len2 - buff_offset;
673
674
	/* App1 port dl configuration */
675
	dc->port[PORT_APP1].dl_addr[CH_A] =
676
				(offset += dc->config_table.dl_diag_len2);
677
	dc->port[PORT_APP1].dl_size[CH_A] =
678
				dc->config_table.dl_app1_len - buff_offset;
679
680
	/* App2 port dl configuration */
681
	dc->port[PORT_APP2].dl_addr[CH_A] =
682
				(offset += dc->config_table.dl_app1_len);
683
	dc->port[PORT_APP2].dl_size[CH_A] =
684
				dc->config_table.dl_app2_len - buff_offset;
685
686
	/* Ctrl dl configuration */
687
	dc->port[PORT_CTRL].dl_addr[CH_A] =
688
				(offset += dc->config_table.dl_app2_len);
689
	dc->port[PORT_CTRL].dl_size[CH_A] =
690
				dc->config_table.dl_ctrl_len - buff_offset;
691
692
	offset = dc->base_addr + dc->config_table.ul_start;
693
694
	/* Modem Port ul configuration */
695
	dc->port[PORT_MDM].ul_addr[CH_A] = offset;
696
	dc->port[PORT_MDM].ul_size[CH_A] =
697
				dc->config_table.ul_mdm_len1 - buff_offset;
698
	dc->port[PORT_MDM].ul_addr[CH_B] =
699
				(offset += dc->config_table.ul_mdm_len1);
700
	dc->port[PORT_MDM].ul_size[CH_B] =
701
				dc->config_table.ul_mdm_len2 - buff_offset;
702
703
	/* Diag port ul configuration */
704
	dc->port[PORT_DIAG].ul_addr[CH_A] =
705
				(offset += dc->config_table.ul_mdm_len2);
706
	dc->port[PORT_DIAG].ul_size[CH_A] =
707
				dc->config_table.ul_diag_len - buff_offset;
708
709
	/* App1 port ul configuration */
710
	dc->port[PORT_APP1].ul_addr[CH_A] =
711
				(offset += dc->config_table.ul_diag_len);
712
	dc->port[PORT_APP1].ul_size[CH_A] =
713
				dc->config_table.ul_app1_len - buff_offset;
714
715
	/* App2 port ul configuration */
716
	dc->port[PORT_APP2].ul_addr[CH_A] =
717
				(offset += dc->config_table.ul_app1_len);
718
	dc->port[PORT_APP2].ul_size[CH_A] =
719
				dc->config_table.ul_app2_len - buff_offset;
720
721
	/* Ctrl ul configuration */
722
	dc->port[PORT_CTRL].ul_addr[CH_A] =
723
				(offset += dc->config_table.ul_app2_len);
724
	dc->port[PORT_CTRL].ul_size[CH_A] =
725
				dc->config_table.ul_ctrl_len - buff_offset;
726
}
727
728
/* Dump config table under initalization phase */
729
#ifdef DEBUG
730
static void dump_table(const struct nozomi *dc)
731
{
732
	DBG3("signature: 0x%08X", dc->config_table.signature);
733
	DBG3("version: 0x%04X", dc->config_table.version);
734
	DBG3("product_information: 0x%04X", \
735
				dc->config_table.product_information);
736
	DBG3("toggle enabled: %d", dc->config_table.toggle.enabled);
737
	DBG3("toggle up_mdm: %d", dc->config_table.toggle.mdm_ul);
738
	DBG3("toggle dl_mdm: %d", dc->config_table.toggle.mdm_dl);
739
	DBG3("toggle dl_dbg: %d", dc->config_table.toggle.diag_dl);
740
741
	DBG3("dl_start: 0x%04X", dc->config_table.dl_start);
742
	DBG3("dl_mdm_len0: 0x%04X, %d", dc->config_table.dl_mdm_len1,
743
	   dc->config_table.dl_mdm_len1);
744
	DBG3("dl_mdm_len1: 0x%04X, %d", dc->config_table.dl_mdm_len2,
745
	   dc->config_table.dl_mdm_len2);
746
	DBG3("dl_diag_len0: 0x%04X, %d", dc->config_table.dl_diag_len1,
747
	   dc->config_table.dl_diag_len1);
748
	DBG3("dl_diag_len1: 0x%04X, %d", dc->config_table.dl_diag_len2,
749
	   dc->config_table.dl_diag_len2);
750
	DBG3("dl_app1_len: 0x%04X, %d", dc->config_table.dl_app1_len,
751
	   dc->config_table.dl_app1_len);
752
	DBG3("dl_app2_len: 0x%04X, %d", dc->config_table.dl_app2_len,
753
	   dc->config_table.dl_app2_len);
754
	DBG3("dl_ctrl_len: 0x%04X, %d", dc->config_table.dl_ctrl_len,
755
	   dc->config_table.dl_ctrl_len);
756
	DBG3("ul_start: 0x%04X, %d", dc->config_table.ul_start,
757
	   dc->config_table.ul_start);
758
	DBG3("ul_mdm_len[0]: 0x%04X, %d", dc->config_table.ul_mdm_len1,
759
	   dc->config_table.ul_mdm_len1);
760
	DBG3("ul_mdm_len[1]: 0x%04X, %d", dc->config_table.ul_mdm_len2,
761
	   dc->config_table.ul_mdm_len2);
762
	DBG3("ul_diag_len: 0x%04X, %d", dc->config_table.ul_diag_len,
763
	   dc->config_table.ul_diag_len);
764
	DBG3("ul_app1_len: 0x%04X, %d", dc->config_table.ul_app1_len,
765
	   dc->config_table.ul_app1_len);
766
	DBG3("ul_app2_len: 0x%04X, %d", dc->config_table.ul_app2_len,
767
	   dc->config_table.ul_app2_len);
768
	DBG3("ul_ctrl_len: 0x%04X, %d", dc->config_table.ul_ctrl_len,
769
	   dc->config_table.ul_ctrl_len);
770
}
771
#else
772
static __inline__ void dump_table(const struct nozomi *dc) { }
773
#endif
774
775
/*
776
 * Read configuration table from card under intalization phase
777
 * Returns 1 if ok, else 0
778
 */
779
static int nozomi_read_config_table(struct nozomi *dc)
780
{
781
	read_mem32((u32 *) &dc->config_table, dc->base_addr + 0,
782
						sizeof(struct config_table));
783
784
	if (dc->config_table.signature != CONFIG_MAGIC) {
785
		dev_err(&dc->pdev->dev, "ConfigTable Bad! 0x%08X != 0x%08X\n",
786
			dc->config_table.signature, CONFIG_MAGIC);
787
		return 0;
788
	}
789
790
	if ((dc->config_table.version == 0)
791
	    || (dc->config_table.toggle.enabled == TOGGLE_VALID)) {
792
		int i;
793
		DBG1("Second phase, configuring card");
794
795
		setup_memory(dc);
796
797
		dc->port[PORT_MDM].toggle_ul = dc->config_table.toggle.mdm_ul;
798
		dc->port[PORT_MDM].toggle_dl = dc->config_table.toggle.mdm_dl;
799
		dc->port[PORT_DIAG].toggle_dl = dc->config_table.toggle.diag_dl;
800
		DBG1("toggle ports: MDM UL:%d MDM DL:%d, DIAG DL:%d",
801
		   dc->port[PORT_MDM].toggle_ul,
802
		   dc->port[PORT_MDM].toggle_dl, dc->port[PORT_DIAG].toggle_dl);
803
804
		dump_table(dc);
805
806
		for (i = PORT_MDM; i < MAX_PORT; i++) {
807
			dc->port[i].fifo_ul =
808
			    kfifo_alloc(FIFO_BUFFER_SIZE_UL, GFP_ATOMIC, NULL);
809
			memset(&dc->port[i].ctrl_dl, 0, sizeof(struct ctrl_dl));
810
			memset(&dc->port[i].ctrl_ul, 0, sizeof(struct ctrl_ul));
811
		}
812
813
		/* Enable control channel */
814
		dc->last_ier = (dc->last_ier & ~CTRL_DL) | CTRL_DL;
815
		writew(dc->last_ier, dc->reg_ier);
816
817
		dev_info(&dc->pdev->dev, "Initialization OK!\n");
818
		return 1;
819
	}
820
821
	if ((dc->config_table.version > 0)
822
	    && (dc->config_table.toggle.enabled != TOGGLE_VALID)) {
823
		u32 offset = 0;
824
		DBG1("First phase: pushing upload buffers, clearing download");
825
826
		dev_info(&dc->pdev->dev, "Version of card: %d\n",
827
			 dc->config_table.version);
828
829
		/* Here we should disable all I/O over F32. */
830
		setup_memory(dc);
831
832
		/*
833
		 * We should send ALL channel pair tokens back along
834
		 * with reset token
835
		 */
836
837
		/* push upload modem buffers */
838
		write_mem32(dc->port[PORT_MDM].ul_addr[CH_A],
839
			(u32 *) &offset, 4);
840
		write_mem32(dc->port[PORT_MDM].ul_addr[CH_B],
841
			(u32 *) &offset, 4);
842
843
		writew(MDM_UL | DIAG_DL | MDM_DL, dc->reg_fcr);
844
845
		DBG1("First phase done");
846
	}
847
848
	return 1;
849
}
850
851
/* Enable uplink interrupts  */
852
static void enable_transmit_ul(enum port_type port, struct nozomi *dc)
853
{
854
	switch (port) {
855
	case PORT_MDM:
856
		dc->last_ier = (dc->last_ier & ~MDM_UL) | MDM_UL;
857
		break;
858
	case PORT_DIAG:
859
		dc->last_ier = (dc->last_ier & ~DIAG_UL) | DIAG_UL;
860
		break;
861
	case PORT_APP1:
862
		dc->last_ier = (dc->last_ier & ~APP1_UL) | APP1_UL;
863
		break;
864
	case PORT_APP2:
865
		dc->last_ier = (dc->last_ier & ~APP2_UL) | APP2_UL;
866
		break;
867
	case PORT_CTRL:
868
		dc->last_ier = (dc->last_ier & ~CTRL_UL) | CTRL_UL;
869
		break;
870
	default:
871
		dev_err(&dc->pdev->dev, "Called with wrong port?\n");
872
		return;
873
		break;
874
	};
875
876
	writew(dc->last_ier, dc->reg_ier);
877
}
878
879
/* Disable uplink interrupts  */
880
static void disable_transmit_ul(enum port_type port, struct nozomi *dc)
881
{
882
	switch (port) {
883
	case PORT_MDM:
884
		dc->last_ier &= ~MDM_UL;
885
		break;
886
	case PORT_DIAG:
887
		dc->last_ier &= ~DIAG_UL;
888
		break;
889
	case PORT_APP1:
890
		dc->last_ier &= ~APP1_UL;
891
		break;
892
	case PORT_APP2:
893
		dc->last_ier &= ~APP2_UL;
894
		break;
895
	case PORT_CTRL:
896
		dc->last_ier &= ~CTRL_UL;
897
		break;
898
	default:
899
		dev_err(&dc->pdev->dev, "Called with wrong port?\n");
900
		return;
901
		break;
902
	};
903
904
	writew(dc->last_ier, dc->reg_ier);
905
}
906
907
/* Enable downlink interrupts */
908
static void enable_transmit_dl(enum port_type port, struct nozomi *dc)
909
{
910
	switch (port) {
911
	case PORT_MDM:
912
		dc->last_ier = (dc->last_ier & ~MDM_DL) | MDM_DL;
913
		break;
914
	case PORT_DIAG:
915
		dc->last_ier = (dc->last_ier & ~DIAG_DL) | DIAG_DL;
916
		break;
917
	case PORT_APP1:
918
		dc->last_ier = (dc->last_ier & ~APP1_DL) | APP1_DL;
919
		break;
920
	case PORT_APP2:
921
		dc->last_ier = (dc->last_ier & ~APP2_DL) | APP2_DL;
922
		break;
923
	case PORT_CTRL:
924
		dc->last_ier = (dc->last_ier & ~CTRL_DL) | CTRL_DL;
925
		break;
926
	default:
927
		dev_err(&dc->pdev->dev, "Called with wrong port?\n");
928
		return;
929
		break;
930
	};
931
932
	writew(dc->last_ier, dc->reg_ier);
933
}
934
935
/* Disable downlink interrupts */
936
static void disable_transmit_dl(enum port_type port, struct nozomi *dc)
937
{
938
	switch (port) {
939
	case PORT_MDM:
940
		dc->last_ier &= ~MDM_DL;
941
		break;
942
	case PORT_DIAG:
943
		dc->last_ier &= ~DIAG_DL;
944
		break;
945
	case PORT_APP1:
946
		dc->last_ier &= ~APP1_DL;
947
		break;
948
	case PORT_APP2:
949
		dc->last_ier &= ~APP2_DL;
950
		break;
951
	case PORT_CTRL:
952
		dc->last_ier &= ~CTRL_DL;
953
		break;
954
	default:
955
		dev_err(&dc->pdev->dev, "Called with wrong port?\n");
956
		return;
957
		break;
958
	};
959
960
	writew(dc->last_ier, dc->reg_ier);
961
}
962
963
/*
964
 * Return 1 - send buffer to card and ack.
965
 * Return 0 - don't ack, don't send buffer to card.
966
 */
967
static int send_data(enum port_type index, struct nozomi *dc)
968
{
969
	u32 size = 0;
970
	struct port *port = &dc->port[index];
971
	u8 toggle = port->toggle_ul;
972
	void __iomem *addr = port->ul_addr[toggle];
973
	u32 ul_size = port->ul_size[toggle];
974
	struct tty_struct *tty = port->tty;
975
976
	/* Get data from tty and place in buf for now */
977
	size = __kfifo_get(port->fifo_ul, dc->send_buf,
978
			   ul_size < SEND_BUF_MAX ? ul_size : SEND_BUF_MAX);
979
980
	if (size == 0) {
981
		DBG4("No more data to send, disable link:");
982
		return 0;
983
	}
984
985
	port->tx_data += size;
986
987
	/* DUMP(buf, size); */
988
989
	/* Write length + data */
990
	write_mem32(addr, (u32 *) &size, 4);
991
	write_mem32(addr + 4, (u32 *) dc->send_buf, size);
992
993
	if (tty)
994
		tty_wakeup(tty);
995
996
	return 1;
997
}
998
999
/* If all data has been read, return 1, else 0 */
1000
static int receive_data(enum port_type index, struct nozomi *dc)
1001
{
1002
	u8 buf[RECEIVE_BUF_MAX] = { 0 };
1003
	int size;
1004
	u32 offset = 4;
1005
	struct port *port = &dc->port[index];
1006
	void __iomem *addr = port->dl_addr[port->toggle_dl];
1007
	struct tty_struct *tty = port->tty;
1008
	int i;
1009
1010
	if (unlikely(!tty)) {
1011
		DBG1("tty not open for port: %d?", index);
1012
		return 1;
1013
	}
1014
1015
	read_mem32((u32 *) &size, addr, 4);
1016
	/*  DBG1( "%d bytes port: %d", size, index); */
1017
1018
	if (test_bit(TTY_THROTTLED, &tty->flags)) {
1019
		DBG1("No room in tty, don't read data, don't ack interrupt, "
1020
			"disable interrupt");
1021
1022
		/* disable interrupt in downlink... */
1023
		disable_transmit_dl(index, dc);
1024
		return 0;
1025
	}
1026
1027
	if (unlikely(size == 0)) {
1028
		dev_err(&dc->pdev->dev, "size == 0?\n");
1029
		return 1;
1030
	}
1031
1032
	port->rx_data += size;
1033
1034
	tty_buffer_request_room(tty, size);
1035
1036
	while (size > 0) {
1037
		read_mem32((u32 *) buf, addr + offset, RECEIVE_BUF_MAX);
1038
1039
		if (size == 1) {
1040
			tty_insert_flip_char(tty, buf[0], TTY_NORMAL);
1041
			size = 0;
1042
		} else if (size < RECEIVE_BUF_MAX) {
1043
			size -= tty_insert_flip_string(tty, (char *) buf, size);
1044
		} else {
1045
			i = tty_insert_flip_string(tty, \
1046
						(char *) buf, RECEIVE_BUF_MAX);
1047
			size -= i;
1048
			offset += i;
1049
		}
1050
	}
1051
1052
	tty_flip_buffer_push(tty);
1053
1054
	return 1;
1055
}
1056
1057
/* Debug for interrupts */
1058
#ifdef DEBUG
1059
static char *interrupt2str(u16 interrupt)
1060
{
1061
	static char buf[TMP_BUF_MAX];
1062
	char *p = buf;
1063
1064
	interrupt & MDM_DL1 ? p += snprintf(p, TMP_BUF_MAX, "MDM_DL1 ") : NULL;
1065
	interrupt & MDM_DL2 ? p += snprintf(p, TMP_BUF_MAX, "MDM_DL2 ") : NULL;
1066
1067
	interrupt & MDM_UL1 ? p += snprintf(p, TMP_BUF_MAX, "MDM_UL1 ") : NULL;
1068
	interrupt & MDM_UL2 ? p += snprintf(p, TMP_BUF_MAX, "MDM_UL2 ") : NULL;
1069
1070
	interrupt & DIAG_DL1 ? p += snprintf(p, \
1071
					TMP_BUF_MAX, "DIAG_DL1 ") : NULL;
1072
	interrupt & DIAG_DL2 ? p += snprintf(p, \
1073
					TMP_BUF_MAX, "DIAG_DL2 ") : NULL;
1074
1075
	interrupt & DIAG_UL ? p += snprintf(p, TMP_BUF_MAX, "DIAG_UL ") : NULL;
1076
1077
	interrupt & APP1_DL ? p += snprintf(p, TMP_BUF_MAX, "APP1_DL ") : NULL;
1078
	interrupt & APP2_DL ? p += snprintf(p, TMP_BUF_MAX, "APP2_DL ") : NULL;
1079
1080
	interrupt & APP1_UL ? p += snprintf(p, TMP_BUF_MAX, "APP1_UL ") : NULL;
1081
	interrupt & APP2_UL ? p += snprintf(p, TMP_BUF_MAX, "APP2_UL ") : NULL;
1082
1083
	interrupt & CTRL_DL ? p += snprintf(p, TMP_BUF_MAX, "CTRL_DL ") : NULL;
1084
	interrupt & CTRL_UL ? p += snprintf(p, TMP_BUF_MAX, "CTRL_UL ") : NULL;
1085
1086
	interrupt & RESET ? p += snprintf(p, TMP_BUF_MAX, "RESET ") : NULL;
1087
1088
	return buf;
1089
}
1090
#endif
1091
1092
/*
1093
 * Receive flow control
1094
 * Return 1 - If ok, else 0
1095
 */
1096
static int receive_flow_control(struct nozomi *dc, struct irq *m)
1097
{
1098
	enum port_type port = PORT_MDM;
1099
	struct ctrl_dl ctrl_dl;
1100
	struct ctrl_dl old_ctrl;
1101
	u16 enable_ier = 0;
1102
1103
	read_mem32((u32 *) &ctrl_dl, dc->port[PORT_CTRL].dl_addr[CH_A], 2);
1104
1105
	switch (ctrl_dl.port) {
1106
	case CTRL_CMD:
1107
		DBG1("The Base Band sends this value as a response to a "
1108
			"request for IMSI detach sent over the control "
1109
			"channel uplink (see section 7.6.1).");
1110
		break;
1111
	case CTRL_MDM:
1112
		port = PORT_MDM;
1113
		enable_ier = MDM_DL;
1114
		break;
1115
	case CTRL_DIAG:
1116
		port = PORT_DIAG;
1117
		enable_ier = DIAG_DL;
1118
		break;
1119
	case CTRL_APP1:
1120
		port = PORT_APP1;
1121
		enable_ier = APP1_DL;
1122
		break;
1123
	case CTRL_APP2:
1124
		port = PORT_APP2;
1125
		enable_ier = APP2_DL;
1126
		break;
1127
	default:
1128
		dev_err(&dc->pdev->dev,
1129
			"ERROR: flow control received for non-existing port\n");
1130
		return 0;
1131
	};
1132
1133
	DBG1("0x%04X->0x%04X", *((u16 *) & dc->port[port].ctrl_dl),
1134
	   *((u16 *) & ctrl_dl));
1135
1136
	old_ctrl = dc->port[port].ctrl_dl;
1137
	dc->port[port].ctrl_dl = ctrl_dl;
1138
1139
	if (old_ctrl.CTS == 1 && ctrl_dl.CTS == 0) {
1140
		DBG1("Disable interrupt (0x%04X) on port: %d",
1141
			enable_ier, port);
1142
		disable_transmit_ul(port, dc);
1143
1144
	} else if (old_ctrl.CTS == 0 && ctrl_dl.CTS == 1) {
1145
1146
		if (__kfifo_len(dc->port[port].fifo_ul)) {
1147
			DBG1("Enable interrupt (0x%04X) on port: %d",
1148
				enable_ier, port);
1149
			DBG1("Data in buffer [%d], enable transmit! ",
1150
				__kfifo_len(dc->port[port].fifo_ul));
1151
			enable_transmit_ul(port, dc);
1152
		} else {
1153
			DBG1("No data in buffer...");
1154
		}
1155
	}
1156
1157
	if (*(u16 *) & old_ctrl == *(u16 *) & ctrl_dl) {
1158
		DBG1(" No change in mctrl");
1159
		return 1;
1160
	}
1161
	/* Update statistics */
1162
	if (old_ctrl.CTS != ctrl_dl.CTS)
1163
		dc->port[port].tty_icount.cts++;
1164
	if (old_ctrl.DSR != ctrl_dl.DSR)
1165
		dc->port[port].tty_icount.dsr++;
1166
	if (old_ctrl.RI != ctrl_dl.RI)
1167
		dc->port[port].tty_icount.rng++;
1168
	if (old_ctrl.DCD != ctrl_dl.DCD)
1169
		dc->port[port].tty_icount.dcd++;
1170
	DBG1("port: %d DCD(%d), CTS(%d), RI(%d), DSR(%d)",
1171
	   port,
1172
	   dc->port[port].tty_icount.dcd, dc->port[port].tty_icount.cts,
1173
	   dc->port[port].tty_icount.rng, dc->port[port].tty_icount.dsr);
1174
1175
	return 1;
1176
}
1177
1178
static enum ctrl_port_type port2ctrl(enum port_type port,
1179
					const struct nozomi *dc)
1180
{
1181
	switch (port) {
1182
	case PORT_MDM:
1183
		return CTRL_MDM;
1184
	case PORT_DIAG:
1185
		return CTRL_DIAG;
1186
	case PORT_APP1:
1187
		return CTRL_APP1;
1188
	case PORT_APP2:
1189
		return CTRL_APP2;
1190
	default:
1191
		dev_err(&dc->pdev->dev,
1192
			"ERROR: send flow control " \
1193
			"received for non-existing port\n");
1194
	};
1195
	return CTRL_ERROR;
1196
}
1197
1198
/*
1199
 * Send flow control, can only update one channel at a time
1200
 * Return 0 - If we have updated all flow control
1201
 * Return 1 - If we need to update more flow control, ack current enable more
1202
 */
1203
static int send_flow_control(struct nozomi *dc)
1204
{
1205
	u32 i, more_flow_control_to_be_updated = 0;
1206
	u16 *ctrl;
1207
1208
	for (i = PORT_MDM; i < MAX_PORT; i++) {
1209
		if (dc->port[i].update_flow_control) {
1210
			if (more_flow_control_to_be_updated) {
1211
				/* We have more flow control to be updated */
1212
				return 1;
1213
			}
1214
			dc->port[i].ctrl_ul.port = port2ctrl(i, dc);
1215
			ctrl = (u16 *) & dc->port[i].ctrl_ul;
1216
			write_mem32(dc->port[PORT_CTRL].ul_addr[0], \
1217
				(u32 *) ctrl, 2);
1218
			dc->port[i].update_flow_control = 0;
1219
			more_flow_control_to_be_updated = 1;
1220
		}
1221
	}
1222
	return 0;
1223
}
1224
1225
/*
1226
 * Handle donlink data, ports that are handled are modem and diagnostics
1227
 * Return 1 - ok
1228
 * Return 0 - toggle fields are out of sync
1229
 */
1230
static int handle_data_dl(struct nozomi *dc, struct irq *m, enum port_type port,
1231
			  u8 *toggle, u16 mask1, u16 mask2)
1232
{
1233
	if (*toggle == 0 && m->read_iir & mask1) {
1234
		if (receive_data(port, dc)) {
1235
			writew(mask1, dc->reg_fcr);
1236
			*toggle = !(*toggle);
1237
		}
1238
1239
		if (m->read_iir & mask2) {
1240
			if (receive_data(port, dc)) {
1241
				writew(mask2, dc->reg_fcr);
1242
				*toggle = !(*toggle);
1243
			}
1244
		}
1245
	} else if (*toggle == 1 && m->read_iir & mask2) {
1246
		if (receive_data(port, dc)) {
1247
			writew(mask2, dc->reg_fcr);
1248
			*toggle = !(*toggle);
1249
		}
1250
1251
		if (m->read_iir & mask1) {
1252
			if (receive_data(port, dc)) {
1253
				writew(mask1, dc->reg_fcr);
1254
				*toggle = !(*toggle);
1255
			}
1256
		}
1257
	} else {
1258
		dev_err(&dc->pdev->dev, "port out of sync!, toggle:%d\n",
1259
			*toggle);
1260
		return 0;
1261
	}
1262
	return 1;
1263
}
1264
1265
/*
1266
 * Handle uplink data, this is currently for the modem port
1267
 * Return 1 - ok
1268
 * Return 0 - toggle field are out of sync
1269
 */
1270
static int handle_data_ul(struct nozomi *dc, struct irq *m, enum port_type port)
1271
{
1272
	u8 *toggle = &(dc->port[port].toggle_ul);
1273
1274
	if (*toggle == 0 && m->read_iir & MDM_UL1) {
1275
		dc->last_ier &= ~MDM_UL;
1276
		writew(dc->last_ier, dc->reg_ier);
1277
		if (send_data(port, dc)) {
1278
			writew(MDM_UL1, dc->reg_fcr);
1279
			dc->last_ier = (dc->last_ier & ~MDM_UL) | MDM_UL;
1280
			writew(dc->last_ier, dc->reg_ier);
1281
			*toggle = !*toggle;
1282
		}
1283
1284
		if (m->read_iir & MDM_UL2) {
1285
			dc->last_ier &= ~MDM_UL;
1286
			writew(dc->last_ier, dc->reg_ier);
1287
			if (send_data(port, dc)) {
1288
				writew(MDM_UL2, dc->reg_fcr);
1289
				dc->last_ier = \
1290
					(dc->last_ier & ~MDM_UL) | MDM_UL;
1291
				writew(dc->last_ier, dc->reg_ier);
1292
				*toggle = !*toggle;
1293
			}
1294
		}
1295
1296
	} else if (*toggle == 1 && m->read_iir & MDM_UL2) {
1297
		dc->last_ier &= ~MDM_UL;
1298
		writew(dc->last_ier, dc->reg_ier);
1299
		if (send_data(port, dc)) {
1300
			writew(MDM_UL2, dc->reg_fcr);
1301
			dc->last_ier = (dc->last_ier & ~MDM_UL) | MDM_UL;
1302
			writew(dc->last_ier, dc->reg_ier);
1303
			*toggle = !*toggle;
1304
		}
1305
1306
		if (m->read_iir & MDM_UL1) {
1307
			dc->last_ier &= ~MDM_UL;
1308
			writew(dc->last_ier, dc->reg_ier);
1309
			if (send_data(port, dc)) {
1310
				writew(MDM_UL1, dc->reg_fcr);
1311
				dc->last_ier = \
1312
					(dc->last_ier & ~MDM_UL) | MDM_UL;
1313
				writew(dc->last_ier, dc->reg_ier);
1314
				*toggle = !*toggle;
1315
			}
1316
		}
1317
	} else {
1318
		writew(m->read_iir & MDM_UL, dc->reg_fcr);
1319
		dev_err(&dc->pdev->dev, "port out of sync!\n");
1320
		return 0;
1321
	}
1322
	return 1;
1323
}
1324
1325
static irqreturn_t interrupt_handler(int irq, void *dev_id)
1326
{
1327
	struct nozomi_devices *ndev = dev_id;
1328
	struct nozomi *dc;
1329
	struct irq *m;
1330
1331
	if (!ndev)
1332
		return IRQ_NONE;
1333
1334
	dc = ndev->my_dev;
1335
	m = &ndev->my_irq;
1336
1337
	spin_lock(&dc->spin_mutex);
1338
	m->read_iir = readw(dc->reg_iir);
1339
1340
	/* Card removed */
1341
	if (m->read_iir == (u16)-1)
1342
		goto none;
1343
	/*
1344
	 * Just handle interrupt enabled in IER
1345
	 * (by masking with dc->last_ier)
1346
	 */
1347
	m->read_iir &= dc->last_ier;
1348
1349
	if (m->read_iir == 0)
1350
		goto none;
1351
1352
1353
	DBG4("%s irq:0x%04X, prev:0x%04X", interrupt2str(m->read_iir),
1354
	   m->read_iir, dc->last_ier);
1355
1356
	if (m->read_iir & RESET) {
1357
		if (unlikely(!nozomi_read_config_table(dc))) {
1358
			dc->last_ier = 0x0;
1359
			writew(dc->last_ier, dc->reg_ier);
1360
			dev_err(&dc->pdev->dev, "Could not read status from "
1361
				"card, we should disable interface\n");
1362
		} else {
1363
			writew(RESET, dc->reg_fcr);
1364
		}
1365
		/* No more useful info if this was the reset interrupt. */
1366
		goto exit_handler;
1367
	}
1368
	if (m->read_iir & CTRL_UL) {
1369
		DBG1("CTRL_UL");
1370
		dc->last_ier &= ~CTRL_UL;
1371
		writew(dc->last_ier, dc->reg_ier);
1372
		if (send_flow_control(dc)) {
1373
			writew(CTRL_UL, dc->reg_fcr);
1374
			dc->last_ier = (dc->last_ier & ~CTRL_UL) | CTRL_UL;
1375
			writew(dc->last_ier, dc->reg_ier);
1376
		}
1377
	}
1378
	if (m->read_iir & CTRL_DL) {
1379
		receive_flow_control(dc, m);
1380
		writew(CTRL_DL, dc->reg_fcr);
1381
	}
1382
	if (m->read_iir & MDM_DL) {
1383
		if (!handle_data_dl(dc, m, PORT_MDM,
1384
				&(dc->port[PORT_MDM].toggle_dl), MDM_DL1,
1385
				MDM_DL2)) {
1386
			dev_err(&dc->pdev->dev, "MDM_DL out of sync!\n");
1387
			goto exit_handler;
1388
		}
1389
	}
1390
	if (m->read_iir & MDM_UL) {
1391
		if (!handle_data_ul(dc, m, PORT_MDM)) {
1392
			dev_err(&dc->pdev->dev, "MDM_UL out of sync!\n");
1393
			goto exit_handler;
1394
		}
1395
	}
1396
	if (m->read_iir & DIAG_DL) {
1397
		if (!handle_data_dl(dc, m, PORT_DIAG,
1398
				&(dc->port[PORT_DIAG].toggle_dl), DIAG_DL1,
1399
				DIAG_DL2)) {
1400
			dev_err(&dc->pdev->dev, "DIAG_DL out of sync!\n");
1401
			goto exit_handler;
1402
		}
1403
	}
1404
	if (m->read_iir & DIAG_UL) {
1405
		dc->last_ier &= ~DIAG_UL;
1406
		writew(dc->last_ier, dc->reg_ier);
1407
		if (send_data(PORT_DIAG, dc)) {
1408
			writew(DIAG_UL, dc->reg_fcr);
1409
			dc->last_ier = (dc->last_ier & ~DIAG_UL) | DIAG_UL;
1410
			writew(dc->last_ier, dc->reg_ier);
1411
		}
1412
	}
1413
	if (m->read_iir & APP1_DL) {
1414
		if (receive_data(PORT_APP1, dc))
1415
			writew(APP1_DL, dc->reg_fcr);
1416
	}
1417
	if (m->read_iir & APP1_UL) {
1418
		dc->last_ier &= ~APP1_UL;
1419
		writew(dc->last_ier, dc->reg_ier);
1420
		if (send_data(PORT_APP1, dc)) {
1421
			writew(APP1_UL, dc->reg_fcr);
1422
			dc->last_ier = (dc->last_ier & ~APP1_UL) | APP1_UL;
1423
			writew(dc->last_ier, dc->reg_ier);
1424
		}
1425
	}
1426
	if (m->read_iir & APP2_DL) {
1427
		if (receive_data(PORT_APP2, dc))
1428
			writew(APP2_DL, dc->reg_fcr);
1429
	}
1430
	if (m->read_iir & APP2_UL) {
1431
		dc->last_ier &= ~APP2_UL;
1432
		writew(dc->last_ier, dc->reg_ier);
1433
		if (send_data(PORT_APP2, dc)) {
1434
			writew(APP2_UL, dc->reg_fcr);
1435
			dc->last_ier = (dc->last_ier & ~APP2_UL) | APP2_UL;
1436
			writew(dc->last_ier, dc->reg_ier);
1437
		}
1438
	}
1439
1440
exit_handler:
1441
	spin_unlock(&dc->spin_mutex);
1442
	return IRQ_HANDLED;
1443
none:
1444
	spin_unlock(&dc->spin_mutex);
1445
	return IRQ_NONE;
1446
}
1447
1448
/* Request a shared IRQ from system */
1449
static int nozomi_setup_interrupt(struct nozomi_devices *ndev)
1450
{
1451
	int rval;
1452
1453
	rval = request_irq(ndev->my_dev->pdev->irq, &interrupt_handler,
1454
				IRQF_SHARED, NOZOMI_NAME, ndev);
1455
	if (unlikely(rval))
1456
		dev_err(&ndev->my_dev->pdev->dev, "Cannot open because IRQ %d "
1457
			"is already in use.\n", ndev->my_dev->pdev->irq);
1458
1459
	return rval;
1460
}
1461
1462
static void nozomi_get_card_type(struct nozomi *dc)
1463
{
1464
	int i;
1465
	u32 size = 0;
1466
1467
	for (i = 0; i < 6; i++)
1468
		size += pci_resource_len(dc->pdev, i);
1469
1470
	/* Assume card type F32_8 if no match */
1471
	dc->card_type = size == 2048 ? F32_2 : F32_8;
1472
1473
	dev_info(&dc->pdev->dev, "Card type is: %d\n", dc->card_type);
1474
}
1475
1476
static void nozomi_setup_private_data(struct nozomi *dc)
1477
{
1478
	void __iomem *offset = dc->base_addr + dc->card_type / 2;
1479
	int i;
1480
1481
	dc->reg_fcr = (void __iomem *)(offset + R_FCR);
1482
	dc->reg_iir = (void __iomem *)(offset + R_IIR);
1483
	dc->reg_ier = (void __iomem *)(offset + R_IER);
1484
	dc->last_ier = 0;
1485
	dc->closing = 0;
1486
1487
	dc->port[PORT_MDM].token_dl = MDM_DL;
1488
	dc->port[PORT_DIAG].token_dl = DIAG_DL;
1489
	dc->port[PORT_APP1].token_dl = APP1_DL;
1490
	dc->port[PORT_APP2].token_dl = APP2_DL;
1491
1492
	for (i = PORT_MDM; i < MAX_PORT; ++i) {
1493
		dc->port[i].rx_data = 0;
1494
		dc->port[i].tx_data = 0;
1495
		dc->port[i].tty_dont_flip = 0;
1496
	}
1497
}
1498
1499
static void tty_flip_queue_function(struct work_struct *work)
1500
{
1501
	struct nozomi *dc = container_of(work, struct nozomi,
1502
						tty_flip_wq_struct);
1503
	int i;
1504
	unsigned long flags;
1505
1506
	/* Enable interrupt for that port */
1507
	for (i = 0; i < MAX_PORT; i++) {
1508
		if (dc->port[i].tty_dont_flip) {
1509
			DBG6("Enable for port: %d", i);
1510
			dc->port[i].tty_dont_flip = 0;
1511
			spin_lock_irqsave(&dc->spin_mutex, flags);
1512
			enable_transmit_dl(dc->port[i].tty_index, dc);
1513
			spin_unlock_irqrestore(&dc->spin_mutex, flags);
1514
		}
1515
	}
1516
}
1517
1518
static ssize_t card_type_show(struct device *dev, struct device_attribute *attr,
1519
			  char *buf)
1520
{
1521
	struct pci_dev *pdev = to_pci_dev(dev);
1522
	struct nozomi_devices *deventry = pci_get_drvdata(pdev);
1523
1524
	return sprintf(buf, "%d\n", deventry->my_dev->card_type);
1525
}
1526
static DEVICE_ATTR(card_type, 0444, card_type_show, NULL);
1527
1528
static ssize_t open_ttys_show(struct device *dev, struct device_attribute *attr,
1529
			  char *buf)
1530
{
1531
	struct pci_dev *pdev = to_pci_dev(dev);
1532
	struct nozomi_devices *deventry = pci_get_drvdata(pdev);
1533
1534
	return sprintf(buf, "%u\n", deventry->my_dev->open_ttys);
1535
}
1536
static DEVICE_ATTR(open_ttys, 0444, open_ttys_show, NULL);
1537
1538
static void make_sysfs_files(struct nozomi *dc)
1539
{
1540
	if (device_create_file(&dc->pdev->dev, &dev_attr_card_type))
1541
		dev_err(&dc->pdev->dev,
1542
			"Could not create sysfs file for card_type\n");
1543
	if (device_create_file(&dc->pdev->dev, &dev_attr_open_ttys))
1544
		dev_err(&dc->pdev->dev,
1545
			"Could not create sysfs file for open_ttys\n");
1546
}
1547
1548
static void remove_sysfs_files(struct nozomi *dc)
1549
{
1550
	device_remove_file(&dc->pdev->dev, &dev_attr_card_type);
1551
	device_remove_file(&dc->pdev->dev, &dev_attr_open_ttys);
1552
}
1553
1554
/* Allocate memory for one device */
1555
static int __devinit nozomi_card_init(struct pci_dev *pdev,
1556
				      const struct pci_device_id *ent)
1557
{
1558
	resource_size_t start;
1559
	int ret = -EIO;
1560
	struct nozomi *dc = NULL;
1561
	struct nozomi_devices *newdev = NULL;
1562
	int i;
1563
1564
	atomic_inc(&cards_found);
1565
	dev_dbg(&pdev->dev, "Init, cards_found: %d\n",
1566
		atomic_read(&cards_found));
1567
1568
	dc = kzalloc(sizeof(struct nozomi), GFP_KERNEL);
1569
	if (unlikely(!dc)) {
1570
		dev_err(&pdev->dev, "Could not allocate memory\n");
1571
		return -ENOMEM;
1572
	}
1573
	newdev = kzalloc(sizeof(struct nozomi_devices), GFP_KERNEL);
1574
	if (unlikely(!newdev)) {
1575
		dev_err(&pdev->dev, "Could not allocate memory\n");
1576
		kfree(dc);
1577
		return -ENOMEM;
1578
	}
1579
	dc->pdev = pdev;
1580
	pci_set_drvdata(pdev, dc);
1581
1582
	newdev->my_dev = dc;
1583
1584
	/* Find out what card type it is */
1585
	nozomi_get_card_type(dc);
1586
1587
	if (pci_enable_device(dc->pdev)) {
1588
		dev_err(&pdev->dev, "Not possible to enable PCI Device\n");
1589
		kfree(dc);
1590
		kfree(newdev);
1591
		return -ENODEV;
1592
	}
1593
1594
	start = pci_resource_start(dc->pdev, 0);
1595
	if (start == 0x0000) {
1596
		dev_err(&pdev->dev, "No I/O-Address for card detected\n");
1597
		ret = -ENODEV;
1598
		goto err_disable_device;
1599
	}
1600
1601
	dc->base_addr = ioremap(start, dc->card_type);
1602
	if (!dc->base_addr) {
1603
		dev_err(&pdev->dev, "No I/O-Address for card detected\n");
1604
		ret = -ENODEV;
1605
		goto err_disable_device;
1606
	}
1607
1608
	dc->open_ttys = 0;
1609
1610
	nozomi_setup_private_data(dc);
1611
1612
	if (pci_request_regions(dc->pdev, NOZOMI_NAME)) {
1613
		dev_err(&pdev->dev, "I/O address 0x%04x already in use\n",
1614
			(int) /* nozomi_private.io_addr */ 0);
1615
		ret = -EIO;
1616
		goto err_disable_regions;
1617
	}
1618
1619
	dc->send_buf = kmalloc(SEND_BUF_MAX, GFP_KERNEL);
1620
	if (!dc->send_buf) {
1621
		dev_err(&pdev->dev, "Could not allocate send buffer?\n");
1622
		goto err_disable_regions;
1623
	}
1624
1625
	/* Disable all interrupts */
1626
	dc->last_ier = 0;
1627
	writew(dc->last_ier, dc->reg_ier);
1628
1629
	/* Setup interrupt handler */
1630
	if (nozomi_setup_interrupt(newdev)) {
1631
		ret = -EIO;
1632
		goto err_disable_regions;
1633
	}
1634
1635
	DBG1("base_addr: %p", dc->base_addr);
1636
1637
	dc->tty_flip_wq = create_singlethread_workqueue(NOZOMI_NAME);
1638
	if (!dc->tty_flip_wq) {
1639
		dev_err(&dc->pdev->dev, "Could not create workqueue?\n");
1640
		return -ENOMEM;
1641
	}
1642
	INIT_WORK(&dc->tty_flip_wq_struct, tty_flip_queue_function);
1643
1644
	spin_lock_init(&dc->spin_mutex);
1645
1646
	make_sysfs_files(dc);
1647
1648
	if (atomic_read(&cards_found) == 1) {
1649
		ntty_tty_init(dc);
1650
		newdev->index_start = 0;
1651
	} else {
1652
		dc->tty_driver->num += NTTY_TTY_MINORS;
1653
		for (i = 0; i < NTTY_TTY_MINORS; i++) {
1654
			init_MUTEX(&dc->port[i].tty_sem);
1655
			dc->port[i].tty_open_count = 0;
1656
			dc->port[i].tty = NULL;
1657
			tty_register_device(dc->tty_driver,
1658
				i + (atomic_read(&cards_found)
1659
							* NTTY_TTY_MINORS),
1660
				&dc->pdev->dev);
1661
		}
1662
		newdev->index_start =
1663
				atomic_read(&cards_found) * NTTY_TTY_MINORS;
1664
	}
1665
1666
	/* Enable  RESET interrupt. */
1667
	dc->last_ier = RESET;
1668
	writew(dc->last_ier, dc->reg_ier);
1669
1670
	INIT_LIST_HEAD(&newdev->list);
1671
	list_add_tail(&newdev->list, &my_devices);
1672
1673
	pci_set_drvdata(pdev, newdev);
1674
1675
	return 0;
1676
1677
err_disable_regions:
1678
	pci_release_regions(pdev);
1679
	iounmap(dc->base_addr);
1680
	dc->base_addr = NULL;
1681
1682
err_disable_device:
1683
	pci_disable_device(pdev);
1684
	kfree(dc);
1685
	kfree(newdev);
1686
	return ret;
1687
}
1688
1689
static void tty_do_close(struct nozomi *dc, struct port *port)
1690
{
1691
	unsigned long flags;
1692
1693
	if (!dc || !port)
1694
		return;
1695
1696
	if (down_interruptible(&port->tty_sem))
1697
		return;
1698
1699
	if (!port->tty_open_count)
1700
		goto exit;
1701
1702
	dc->open_ttys--;
1703
	port->tty_open_count--;
1704
1705
	if (port->tty_open_count == 0) {
1706
		DBG1("close: %d", port->token_dl);
1707
		spin_lock_irqsave(&dc->spin_mutex, flags);
1708
		dc->last_ier &= ~(port->token_dl);
1709
		writew(dc->last_ier, dc->reg_ier);
1710
		spin_unlock_irqrestore(&dc->spin_mutex, flags);
1711
	}
1712
1713
exit:
1714
	up(&port->tty_sem);
1715
}
1716
1717
static void __devexit tty_exit(struct nozomi_devices *ndev)
1718
{
1719
	struct nozomi *dc = ndev->my_dev;
1720
	int i, ret;
1721
1722
	DBG1(" ");
1723
1724
	flush_scheduled_work();
1725
1726
	for (i = ndev->index_start; i < ndev->index_start + NTTY_TTY_MINORS; \
1727
									++i)
1728
		if (dc->port[i].tty && \
1729
				list_empty(&dc->port[i].tty->hangup_work.entry))
1730
			tty_hangup(dc->port[i].tty);
1731
1732
	while (dc->open_ttys)
1733
		msleep_interruptible(1);
1734
1735
	for (i = ndev->index_start; i < ndev->index_start + NTTY_TTY_MINORS; \
1736
									++i)
1737
		tty_unregister_device(dc->tty_driver, i);
1738
1739
	ret = tty_unregister_driver(dc->tty_driver);
1740
	if (ret)
1741
		printk(KERN_ERR "Unable to unregister the tty driver !"
1742
							" (%d)\n", ret);
1743
	put_tty_driver(dc->tty_driver);
1744
}
1745
1746
/* Deallocate memory for one device */
1747
static void __devexit nozomi_card_exit(struct pci_dev *pdev)
1748
{
1749
	int i;
1750
	struct ctrl_ul ctrl;
1751
	struct nozomi_devices *deventry = pci_get_drvdata(pdev);
1752
	struct nozomi *dc = deventry->my_dev;
1753
1754
	/* Disable all interrupts */
1755
	dc->last_ier = 0;
1756
	writew(dc->last_ier, dc->reg_ier);
1757
1758
	tty_exit(deventry);
1759
1760
	/* Send 0x0001, command card to resend the reset token.  */
1761
	/* This is to get the reset when the module is reloaded. */
1762
	ctrl.port = 0x00;
1763
	ctrl.reserved = 0;
1764
	ctrl.RTS = 0;
1765
	ctrl.DTR = 1;
1766
	DBG1("sending flow control 0x%04X", *((u16 *) & ctrl));
1767
1768
	/* Setup dc->reg addresses to we can use defines here */
1769
	nozomi_setup_private_data(dc);
1770
	write_mem32(dc->port[PORT_CTRL].ul_addr[0], (u32 *) & ctrl, 2);
1771
	writew(CTRL_UL, dc->reg_fcr);	/* push the token to the card. */
1772
1773
	DBG1("pci_release_regions");
1774
	pci_release_regions(pdev);
1775
1776
	if (dc->base_addr)
1777
		iounmap(dc->base_addr);
1778
1779
	DBG1("pci_disable_device");
1780
	pci_disable_device(pdev);
1781
1782
	free_irq(pdev->irq, deventry);
1783
1784
	for (i = PORT_MDM; i < MAX_PORT; i++)
1785
		kfree(dc->port[i].fifo_ul);
1786
1787
	kfree(dc->send_buf);
1788
1789
	remove_sysfs_files(dc);
1790
1791
	destroy_workqueue(dc->tty_flip_wq);
1792
1793
	kfree(dc);
1794
	deventry->my_dev = 0;
1795
1796
	list_del(&deventry->list);
1797
	kfree(deventry);
1798
1799
	atomic_dec(&cards_found);
1800
}
1801
1802
static void set_rts(int index, int rts)
1803
{
1804
	struct nozomi *dc = get_dc_by_index(index);
1805
1806
	dc->port[index].ctrl_ul.RTS = rts;
1807
	dc->port[index].update_flow_control = 1;
1808
	enable_transmit_ul(PORT_CTRL, dc);
1809
}
1810
1811
static void set_dtr(int index, int dtr)
1812
{
1813
	struct nozomi *dc = get_dc_by_index(index);
1814
1815
	DBG1("SETTING DTR index: %d, dtr: %d", index, dtr);
1816
1817
	dc->port[index].ctrl_ul.DTR = dtr;
1818
	dc->port[index].update_flow_control = 1;
1819
	enable_transmit_ul(PORT_CTRL, dc);
1820
}
1821
1822
/*
1823
 * ----------------------------------------------------------------------------
1824
 * TTY code
1825
 * ----------------------------------------------------------------------------
1826
 */
1827
1828
/* Called when the userspace process opens the tty, /dev/noz*.  */
1829
static int ntty_open(struct tty_struct *tty, struct file *file)
1830
{
1831
	struct port *port = get_port_by_tty(tty);
1832
	struct nozomi *dc = get_dc_by_tty(tty);
1833
	unsigned long flags;
1834
1835
	if (down_interruptible(&port->tty_sem))
1836
		return -ERESTARTSYS;
1837
1838
	port->tty_open_count++;
1839
	dc->open_ttys++;
1840
1841
	/* Enable interrupt downlink for channel */
1842
	if (port->tty_open_count == 1) {
1843
		tty->low_latency = 1;
1844
		tty->driver_data = port;
1845
		port->tty = tty;
1846
		port->tty_index = tty->index;
1847
		port->rx_data = 0;
1848
		port->tx_data = 0;
1849
		DBG1("open: %d", port->token_dl);
1850
		spin_lock_irqsave(&dc->spin_mutex, flags);
1851
		dc->last_ier =
1852
			(dc->last_ier & ~(port->token_dl)) | port->token_dl;
1853
		writew(dc->last_ier, dc->reg_ier);
1854
		spin_unlock_irqrestore(&dc->spin_mutex, flags);
1855
	}
1856
1857
	up(&port->tty_sem);
1858
1859
	return 0;
1860
}
1861
1862
/* Called when the userspace process close the tty, /dev/noz*. */
1863
static void ntty_close(struct tty_struct *tty, struct file *file)
1864
{
1865
	struct nozomi *dc = get_dc_by_tty(tty);
1866
	tty_do_close(dc, (struct port *)tty->driver_data);
1867
}
1868
1869
/*
1870
 * called when the userspace process writes to the tty (/dev/noz*).
1871
 * Data is inserted into a fifo, which is then read and transfered to the modem.
1872
 */
1873
static int ntty_write(struct tty_struct *tty, const unsigned char *buffer,
1874
		      int count)
1875
{
1876
	int rval = -EINVAL;
1877
	struct nozomi *dc = get_dc_by_tty(tty);
1878
	struct port *port = (struct port *)tty->driver_data;
1879
	unsigned long flags;
1880
1881
	/* DBG1( "WRITEx: %d, index = %d", count, index); */
1882
1883
	if (!dc || !port)
1884
		return -ENODEV;
1885
1886
	if (unlikely(down_trylock(&port->tty_sem))) {
1887
		/*
1888
		 * must test lock as tty layer wraps calls
1889
		 * to this function with BKL
1890
		 */
1891
		dev_err(&dc->pdev->dev, "Would have deadlocked - "
1892
			"return ERESTARTSYS\n");
1893
		return -ERESTARTSYS;
1894
	}
1895
1896
	if (unlikely(!port->tty_open_count)) {
1897
		DBG1(" ");
1898
		goto exit;
1899
	}
1900
1901
	rval = __kfifo_put(port->fifo_ul, (unsigned char *)buffer, count);
1902
1903
	/* notify card */
1904
	if (unlikely(dc == NULL)) {
1905
		DBG1("No device context?");
1906
		goto exit;
1907
	}
1908
1909
	spin_lock_irqsave(&dc->spin_mutex, flags);
1910
	/* CTS is only valid on the modem channel */
1911
	if (port == &(dc->port[PORT_MDM])) {
1912
		if (port->ctrl_dl.CTS) {
1913
			DBG4("Enable interrupt");
1914
			enable_transmit_ul(port->tty_index, dc);
1915
		} else {
1916
			dev_err(&dc->pdev->dev,
1917
				"CTS not active on modem port?\n");
1918
		}
1919
	} else {
1920
		enable_transmit_ul(port->tty_index, dc);
1921
	}
1922
	spin_unlock_irqrestore(&dc->spin_mutex, flags);
1923
1924
exit:
1925
	up(&port->tty_sem);
1926
	return rval;
1927
}
1928
1929
/*
1930
 * Calculate how much is left in device
1931
 * This method is called by the upper tty layer.
1932
 *   #according to sources N_TTY.c it expects a value >= 0 and
1933
 *    does not check for negative values.
1934
 */
1935
static int ntty_write_room(struct tty_struct *tty)
1936
{
1937
	struct port *port = (struct port *)tty->driver_data;
1938
	int room = 0;
1939
	struct nozomi *dc = get_dc_by_tty(tty);
1940
1941
	if (!dc || !port)
1942
		return 0;
1943
	if (down_trylock(&port->tty_sem))
1944
		return 0;
1945
1946
	if (!port->tty_open_count)
1947
		goto exit;
1948
1949
	room = port->fifo_ul->size - __kfifo_len(port->fifo_ul);
1950
1951
exit:
1952
	up(&port->tty_sem);
1953
	return room;
1954
}
1955
1956
/* Sets termios flags, called by the tty layer. */
1957
static void ntty_set_termios(struct tty_struct *tty,
1958
			     struct ktermios *old_termios)
1959
{
1960
	unsigned int cflag;
1961
1962
	cflag = tty->termios->c_cflag;
1963
1964
	/* get the byte size */
1965
	switch (cflag & CSIZE) {
1966
	case CS5:
1967
		DBG1(" - data bits = 5");
1968
		break;
1969
	case CS6:
1970
		DBG1(" - data bits = 6");
1971
		break;
1972
	case CS7:
1973
		DBG1(" - data bits = 7");
1974
		break;
1975
	default:
1976
	case CS8:
1977
		DBG1(" - data bits = 8");
1978
		break;
1979
	}
1980
1981
	/* determine the parity */
1982
	if (cflag & PARENB) {
1983
		if (cflag & PARODD) {
1984
			DBG1(" - parity = odd");
1985
		} else {
1986
			DBG1(" - parity = even");
1987
		}
1988
	} else {
1989
		DBG1(" - parity = none");
1990
	}
1991
1992
	/* figure out the stop bits requested */
1993
	if (cflag & CSTOPB) {
1994
		DBG1(" - stop bits = 2");
1995
	} else {
1996
		DBG1(" - stop bits = 1");
1997
	}
1998
1999
	/* figure out the hardware flow control settings */
2000
	if (cflag & CRTSCTS) {
2001
		DBG1(" - RTS/CTS is enabled");
2002
	} else {
2003
		DBG1(" - RTS/CTS is disabled");
2004
	}
2005
2006
	/* determine software flow control */
2007
	/* if we are implementing XON/XOFF, set the start and
2008
	 * stop character in the device */
2009
	if (I_IXOFF(tty) || I_IXON(tty)) {
2010
		/* if we are implementing INBOUND XON/XOFF */
2011
		if (I_IXOFF(tty))
2012
			DBG1(" - INBOUND XON/XOFF is enabled, "
2013
			   "XON = %2x, XOFF = %2x",
2014
			   START_CHAR(tty), STOP_CHAR(tty));
2015
		else
2016
			DBG1(" - INBOUND XON/XOFF is disabled");
2017
2018
		/* if we are implementing OUTBOUND XON/XOFF */
2019
		if (I_IXON(tty))
2020
			DBG1(" - OUTBOUND XON/XOFF is enabled, "
2021
			   "XON = %2x, XOFF = %2x",
2022
			   START_CHAR(tty), STOP_CHAR(tty));
2023
		else
2024
			DBG1(" - OUTBOUND XON/XOFF is disabled");
2025
	}
2026
2027
	return;
2028
}
2029
2030
/* Gets io control parameters */
2031
static int ntty_tiocmget(struct tty_struct *tty, struct file *file)
2032
{
2033
	struct port *port = tty->driver_data;
2034
	struct ctrl_dl *ctrl_dl = &port->ctrl_dl;
2035
	struct ctrl_ul *ctrl_ul = &port->ctrl_ul;
2036
2037
	return 0 | (ctrl_ul->RTS ? TIOCM_RTS : 0)
2038
	    | (ctrl_ul->DTR ? TIOCM_DTR : 0)
2039
	    | (ctrl_dl->DCD ? TIOCM_CAR : 0)
2040
	    | (ctrl_dl->RI ? TIOCM_RNG : 0)
2041
	    | (ctrl_dl->DSR ? TIOCM_DSR : 0)
2042
	    | (ctrl_dl->CTS ? TIOCM_CTS : 0);
2043
}
2044
2045
/* Sets io controls parameters */
2046
static int ntty_tiocmset(struct tty_struct *tty, struct file *file,
2047
	unsigned int set, unsigned int clear)
2048
{
2049
	struct port *port = (struct port *)tty->driver_data;
2050
2051
	if (set & TIOCM_RTS)
2052
		set_rts(port->tty_index, 1);
2053
	else if (clear & TIOCM_RTS)
2054
		set_rts(port->tty_index, 0);
2055
2056
	if (set & TIOCM_DTR)
2057
		set_dtr(port->tty_index, 1);
2058
	else if (clear & TIOCM_DTR)
2059
		set_dtr(port->tty_index, 0);
2060
2061
	return 0;
2062
}
2063
2064
static int ntty_ioctl_tiocmiwait(struct tty_struct *tty, struct file *file,
2065
				 unsigned int cmd, unsigned long arg)
2066
{
2067
	struct port *port = (struct port *)tty->driver_data;
2068
2069
	if (cmd == TIOCMIWAIT) {
2070
		DECLARE_WAITQUEUE(wait, current);
2071
		struct async_icount cnow;
2072
		struct async_icount cprev;
2073
2074
		cprev = port->tty_icount;
2075
		while (1) {
2076
			add_wait_queue(&port->tty_wait, &wait);
2077
			set_current_state(TASK_INTERRUPTIBLE);
2078
			schedule();
2079
			remove_wait_queue(&port->tty_wait, &wait);
2080
2081
			/* see if a signal woke us up */
2082
			if (signal_pending(current))
2083
				return -ERESTARTSYS;
2084
2085
			cnow = port->tty_icount;
2086
			if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
2087
			    cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
2088
				return -EIO;	/* no change => error */
2089
			if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
2090
			    ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
2091
			    ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
2092
			    ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)))
2093
				return 0;
2094
2095
			cprev = cnow;
2096
		}
2097
2098
	}
2099
	return -ENOIOCTLCMD;
2100
}
2101
2102
static int ntty_ioctl_tiocgicount(struct tty_struct *tty, struct file *file,
2103
				  unsigned int cmd, void __user *arg)
2104
{
2105
	struct port *port = (struct port *)tty->driver_data;
2106
2107
	if (cmd == TIOCGICOUNT) {
2108
		struct async_icount cnow = port->tty_icount;
2109
		struct serial_icounter_struct icount;
2110
2111
		icount.cts = cnow.cts;
2112
		icount.dsr = cnow.dsr;
2113
		icount.rng = cnow.rng;
2114
		icount.dcd = cnow.dcd;
2115
		icount.rx = cnow.rx;
2116
		icount.tx = cnow.tx;
2117
		icount.frame = cnow.frame;
2118
		icount.overrun = cnow.overrun;
2119
		icount.parity = cnow.parity;
2120
		icount.brk = cnow.brk;
2121
		icount.buf_overrun = cnow.buf_overrun;
2122
2123
		if (copy_to_user(arg, &icount, sizeof(icount)))
2124
			return -EFAULT;
2125
		return 0;
2126
	}
2127
	return -ENOIOCTLCMD;
2128
}
2129
2130
static int ntty_ioctl(struct tty_struct *tty, struct file *file,
2131
		      unsigned int cmd, unsigned long arg)
2132
{
2133
	struct port *port = tty->driver_data;
2134
	struct nozomi *dc = get_dc_by_tty(tty);
2135
	unsigned long flags;
2136
	int mask;
2137
	int rval = -ENOIOCTLCMD;
2138
2139
	DBG1("******** IOCTL, cmd: %d", cmd);
2140
2141
	switch (cmd) {
2142
	case TCGETS:
2143
		DBG1("IOCTL TCGETS ...");
2144
		rval = -ENOIOCTLCMD;
2145
		break;
2146
	case TCSETS:
2147
		DBG1("IOCTL TCSETS ...");
2148
		rval = -ENOIOCTLCMD;
2149
		break;
2150
	case TIOCMIWAIT:
2151
		rval = ntty_ioctl_tiocmiwait(tty, file, cmd, arg);
2152
		break;
2153
	case TIOCGICOUNT:
2154
		rval =
2155
		    ntty_ioctl_tiocgicount(tty, file, cmd, (void __user *)arg);
2156
		break;
2157
	case TIOCMGET:
2158
		spin_lock_irqsave(&dc->spin_mutex, flags);
2159
		rval = ntty_tiocmget(tty, file);
2160
		spin_unlock_irqrestore(&dc->spin_mutex, flags);
2161
		break;
2162
	case TIOCMSET:
2163
		rval = ntty_tiocmset(tty, file, arg, ~arg);
2164
		break;
2165
	case TIOCMBIC:
2166
		if (get_user(mask, (unsigned long __user *)arg))
2167
			return -EFAULT;
2168
2169
		spin_lock_irqsave(&dc->spin_mutex, flags);
2170
		if (mask & TIOCM_RTS)
2171
			set_rts(port->tty_index, 0);
2172
		if (mask & TIOCM_DTR)
2173
			set_dtr(port->tty_index, 0);
2174
		spin_unlock_irqrestore(&dc->spin_mutex, flags);
2175
		rval = 0;
2176
		break;
2177
	case TIOCMBIS:
2178
		if (get_user(mask, (unsigned long __user *)arg))
2179
			return -EFAULT;
2180
2181
		spin_lock_irqsave(&dc->spin_mutex, flags);
2182
		if (mask & TIOCM_RTS)
2183
			set_rts(port->tty_index, 1);
2184
		if (mask & TIOCM_DTR)
2185
			set_dtr(port->tty_index, 1);
2186
		spin_unlock_irqrestore(&dc->spin_mutex, flags);
2187
		rval = 0;
2188
		break;
2189
	case TCFLSH:
2190
		DBG1("IOCTL TCFLSH ...");
2191
		rval = -ENOIOCTLCMD;
2192
		break;
2193
2194
	default:
2195
		DBG1("ERR: 0x%08X, %d", cmd, cmd);
2196
		break;
2197
	};
2198
2199
	return rval;
2200
}
2201
2202
/*
2203
 * Called by the upper tty layer when tty buffers are ready
2204
 * to receive data again after a call to throttle.
2205
 */
2206
static void ntty_unthrottle(struct tty_struct *tty)
2207
{
2208
	struct port *port = (struct port *)tty->driver_data;
2209
	struct nozomi *dc = get_dc_by_tty(tty);
2210
	unsigned long flags;
2211
2212
	DBG1("UNTHROTTLE");
2213
	spin_lock_irqsave(&dc->spin_mutex, flags);
2214
	enable_transmit_dl(port->tty_index, dc);
2215
	set_rts(port->tty_index, 1);
2216
2217
	spin_unlock_irqrestore(&dc->spin_mutex, flags);
2218
}
2219
2220
/*
2221
 * Called by the upper tty layer when the tty buffers are almost full.
2222
 * The driver should stop send more data.
2223
 */
2224
static void ntty_throttle(struct tty_struct *tty)
2225
{
2226
	struct port *port = (struct port *)tty->driver_data;
2227
	struct nozomi *dc = get_dc_by_tty(tty);
2228
	unsigned long flags;
2229
2230
	DBG1("THROTTLE");
2231
	spin_lock_irqsave(&dc->spin_mutex, flags);
2232
	set_rts(port->tty_index, 0);
2233
	spin_unlock_irqrestore(&dc->spin_mutex, flags);
2234
}
2235
2236
/* just to discard single character writes */
2237
static void ntty_put_char(struct tty_struct *tty, unsigned char c)
2238
{
2239
	DBG2("PUT CHAR Function: %c", c);
2240
}
2241
2242
/* Returns number of chars in buffer, called by tty layer */
2243
static s32 ntty_chars_in_buffer(struct tty_struct *tty)
2244
{
2245
	struct port *port = (struct port *)tty->driver_data;
2246
	struct nozomi *dc = get_dc_by_tty(tty);
2247
	s32 rval;
2248
2249
	if (unlikely(!dc || !port)) {
2250
		rval = -ENODEV;
2251
		goto exit_in_buffer;
2252
	}
2253
2254
	if (unlikely(!port->tty_open_count)) {
2255
		dev_err(&dc->pdev->dev, "No tty open?\n");
2256
		rval = -ENODEV;
2257
		goto exit_in_buffer;
2258
	}
2259
2260
	rval = __kfifo_len(port->fifo_ul);
2261
2262
exit_in_buffer:
2263
	return rval;
2264
}
2265
2266
static struct tty_operations tty_ops = {
2267
	.ioctl = ntty_ioctl,
2268
	.open = ntty_open,
2269
	.close = ntty_close,
2270
	.write = ntty_write,
2271
	.write_room = ntty_write_room,
2272
	.unthrottle = ntty_unthrottle,
2273
	.throttle = ntty_throttle,
2274
	.set_termios = ntty_set_termios,
2275
	.chars_in_buffer = ntty_chars_in_buffer,
2276
	.put_char = ntty_put_char,
2277
	.tiocmset = ntty_tiocmset,
2278
};
2279
2280
/* Initializes the tty */
2281
static int ntty_tty_init(struct nozomi *dc)
2282
{
2283
	struct tty_driver *td;
2284
	int rval;
2285
	int i;
2286
2287
	dc->tty_driver = alloc_tty_driver(NTTY_TTY_MINORS);
2288
	if (!dc->tty_driver)
2289
		return -ENOMEM;
2290
	td = dc->tty_driver;
2291
	td->owner = THIS_MODULE;
2292
	td->driver_name = NOZOMI_NAME_TTY;
2293
	td->name = "noz";
2294
	td->major = NTTY_TTY_MAJOR;
2295
	td->type = TTY_DRIVER_TYPE_SERIAL;
2296
	td->subtype = SERIAL_TYPE_NORMAL;
2297
	td->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
2298
	td->init_termios = tty_std_termios;
2299
	td->init_termios.c_cflag = B115200 | CS8 | CREAD | HUPCL | CLOCAL;
2300
2301
	td->termios = dc->tty_termios;
2302
	td->termios_locked = dc->tty_termios_locked;
2303
	tty_set_operations(dc->tty_driver, &tty_ops);
2304
2305
	rval = tty_register_driver(td);
2306
	if (rval) {
2307
		dev_err(&dc->pdev->dev, "failed to register ntty tty driver\n");
2308
		return rval;
2309
	}
2310
2311
	for (i = 0; i < NTTY_TTY_MINORS; i++) {
2312
		init_MUTEX(&dc->port[i].tty_sem);
2313
		dc->port[i].tty_open_count = 0;
2314
		dc->port[i].tty = NULL;
2315
		tty_register_device(td, i, &dc->pdev->dev);
2316
	}
2317
2318
	dev_info(&dc->pdev->dev, DRIVER_DESC " " NOZOMI_NAME_TTY "\n");
2319
	return rval;
2320
}
2321
2322
/* Module initialization */
2323
static struct pci_driver nozomi_driver = {
2324
	.name = NOZOMI_NAME,
2325
	.id_table = nozomi_pci_tbl,
2326
	.probe = nozomi_card_init,
2327
	.remove = __devexit_p(nozomi_card_exit),
2328
};
2329
2330
static __init int nozomi_init(void)
2331
{
2332
	printk(KERN_INFO "Initializing %s\n", VERSION_STRING);
2333
	return pci_register_driver(&nozomi_driver);
2334
}
2335
2336
static __exit void nozomi_exit(void)
2337
{
2338
	printk(KERN_INFO "Unloading %s\n", DRIVER_DESC);
2339
	pci_unregister_driver(&nozomi_driver);
2340
}
2341
2342
module_init(nozomi_init);
2343
module_exit(nozomi_exit);
2344
2345
module_param(debug, int, S_IRUGO | S_IWUSR);
2346
2347
MODULE_LICENSE("Dual BSD/GPL");
2348
MODULE_DESCRIPTION(DRIVER_DESC);

Return to bug 144913