Blame view

drivers/tty/nozomi.c 47.9 KB
20fd1e3be   Frank Seidel   nozomi driver
1
2
3
4
  /*
   * nozomi.c  -- HSDPA driver Broadband Wireless Data Card - Globe Trotter
   *
   * Written by: Ulf Jakobsson,
e9ed537a3   Frank Seidel   nozomi driver update
5
   *             Jan Ã…kerfeldt,
20fd1e3be   Frank Seidel   nozomi driver
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
   *             Stefan Thomasson,
   *
   * Maintained by: Paul Hardwick (p.hardwick@option.com)
   *
   * Patches:
   *          Locking code changes for Vodafone by Sphere Systems Ltd,
   *                              Andrew Bird (ajb@spheresystems.co.uk )
   *                              & Phil Sanderson
   *
   * Source has been ported from an implementation made by Filip Aben @ Option
   *
   * --------------------------------------------------------------------------
   *
   * Copyright (c) 2005,2006 Option Wireless Sweden AB
   * Copyright (c) 2006 Sphere Systems Ltd
   * Copyright (c) 2006 Option Wireless n/v
   * All rights Reserved.
   *
   * This program is free software; you can redistribute it and/or modify
   * it under the terms of the GNU General Public License as published by
   * the Free Software Foundation; either version 2 of the License, or
   * (at your option) any later version.
   *
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU General Public License for more details.
   *
   * You should have received a copy of the GNU General Public License
   * along with this program; if not, write to the Free Software
   * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
   *
   * --------------------------------------------------------------------------
   */
20fd1e3be   Frank Seidel   nozomi driver
40
41
42
43
44
45
46
47
48
49
  /* Enable this to have a lot of debug printouts */
  #define DEBUG
  
  #include <linux/kernel.h>
  #include <linux/module.h>
  #include <linux/pci.h>
  #include <linux/ioport.h>
  #include <linux/tty.h>
  #include <linux/tty_driver.h>
  #include <linux/tty_flip.h>
d43c36dc6   Alexey Dobriyan   headers: remove s...
50
  #include <linux/sched.h>
20fd1e3be   Frank Seidel   nozomi driver
51
52
53
54
55
56
  #include <linux/serial.h>
  #include <linux/interrupt.h>
  #include <linux/kmod.h>
  #include <linux/init.h>
  #include <linux/kfifo.h>
  #include <linux/uaccess.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
57
  #include <linux/slab.h>
20fd1e3be   Frank Seidel   nozomi driver
58
59
60
  #include <asm/byteorder.h>
  
  #include <linux/delay.h>
2cae8de7b   Michal Marek   nozomi: Drop __TI...
61
  #define VERSION_STRING DRIVER_DESC " 2.1d"
20fd1e3be   Frank Seidel   nozomi driver
62
63
64
65
66
67
68
69
70
71
72
73
74
  
  /*    Macros definitions */
  
  /* Default debug printout level */
  #define NOZOMI_DEBUG_LEVEL 0x00
  
  #define P_BUF_SIZE 128
  #define NFO(_err_flag_, args...)				\
  do {								\
  	char tmp[P_BUF_SIZE];					\
  	snprintf(tmp, sizeof(tmp), ##args);			\
  	printk(_err_flag_ "[%d] %s(): %s
  ", __LINE__,		\
bf9d89295   Harvey Harrison   drivers/char: rep...
75
  		__func__, tmp);				\
20fd1e3be   Frank Seidel   nozomi driver
76
77
78
79
80
81
82
83
84
85
86
87
88
89
  } while (0)
  
  #define DBG1(args...) D_(0x01, ##args)
  #define DBG2(args...) D_(0x02, ##args)
  #define DBG3(args...) D_(0x04, ##args)
  #define DBG4(args...) D_(0x08, ##args)
  #define DBG5(args...) D_(0x10, ##args)
  #define DBG6(args...) D_(0x20, ##args)
  #define DBG7(args...) D_(0x40, ##args)
  #define DBG8(args...) D_(0x80, ##args)
  
  #ifdef DEBUG
  /* Do we need this settable at runtime? */
  static int debug = NOZOMI_DEBUG_LEVEL;
e9ed537a3   Frank Seidel   nozomi driver update
90
91
92
  #define D(lvl, args...)  do \
  			{if (lvl & debug) NFO(KERN_DEBUG, ##args); } \
  			while (0)
20fd1e3be   Frank Seidel   nozomi driver
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
  #define D_(lvl, args...) D(lvl, ##args)
  
  /* These printouts are always printed */
  
  #else
  static int debug;
  #define D_(lvl, args...)
  #endif
  
  /* TODO: rewrite to optimize macros... */
  
  #define TMP_BUF_MAX 256
  
  #define DUMP(buf__,len__) \
    do {  \
      char tbuf[TMP_BUF_MAX] = {0};\
      if (len__ > 1) {\
  	snprintf(tbuf, len__ > TMP_BUF_MAX ? TMP_BUF_MAX : len__, "%s", buf__);\
  	if (tbuf[len__-2] == '\r') {\
  		tbuf[len__-2] = 'r';\
  	} \
  	DBG1("SENDING: '%s' (%d+n)", tbuf, len__);\
      } else {\
  	DBG1("SENDING: '%s' (%d)", tbuf, len__);\
      } \
  } while (0)
  
  /*    Defines */
  #define NOZOMI_NAME		"nozomi"
  #define NOZOMI_NAME_TTY		"nozomi_tty"
  #define DRIVER_DESC		"Nozomi driver"
  
  #define NTTY_TTY_MAXMINORS	256
  #define NTTY_FIFO_BUFFER_SIZE	8192
  
  /* Must be power of 2 */
  #define FIFO_BUFFER_SIZE_UL	8192
  
  /* Size of tmp send buffer to card */
  #define SEND_BUF_MAX		1024
  #define RECEIVE_BUF_MAX		4
20fd1e3be   Frank Seidel   nozomi driver
134
135
136
  #define R_IIR		0x0000	/* Interrupt Identity Register */
  #define R_FCR		0x0000	/* Flow Control Register */
  #define R_IER		0x0004	/* Interrupt Enable Register */
03ac6b349   Valentin Rothberg   drivers/tty/nozom...
137
138
  #define NOZOMI_CONFIG_MAGIC	0xEFEFFEFE
  #define TOGGLE_VALID		0x0000
20fd1e3be   Frank Seidel   nozomi driver
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
  
  /* Definition of interrupt tokens */
  #define MDM_DL1		0x0001
  #define MDM_UL1		0x0002
  #define MDM_DL2		0x0004
  #define MDM_UL2		0x0008
  #define DIAG_DL1	0x0010
  #define DIAG_DL2	0x0020
  #define DIAG_UL		0x0040
  #define APP1_DL		0x0080
  #define APP1_UL		0x0100
  #define APP2_DL		0x0200
  #define APP2_UL		0x0400
  #define CTRL_DL		0x0800
  #define CTRL_UL		0x1000
  #define RESET		0x8000
  
  #define MDM_DL		(MDM_DL1  | MDM_DL2)
  #define MDM_UL		(MDM_UL1  | MDM_UL2)
  #define DIAG_DL		(DIAG_DL1 | DIAG_DL2)
  
  /* modem signal definition */
  #define CTRL_DSR	0x0001
  #define CTRL_DCD	0x0002
  #define CTRL_RI		0x0004
  #define CTRL_CTS	0x0008
  
  #define CTRL_DTR	0x0001
  #define CTRL_RTS	0x0002
  
  #define MAX_PORT		4
  #define NOZOMI_MAX_PORTS	5
  #define NOZOMI_MAX_CARDS	(NTTY_TTY_MAXMINORS / MAX_PORT)
  
  /*    Type definitions */
  
  /*
   * There are two types of nozomi cards,
   * one with 2048 memory and with 8192 memory
   */
  enum card_type {
  	F32_2 = 2048,	/* 512 bytes downlink + uplink * 2 -> 2048 */
  	F32_8 = 8192,	/* 3072 bytes downl. + 1024 bytes uplink * 2 -> 8192 */
  };
661b4e89d   Frank Seidel   nozomi: fix initi...
183
184
185
186
187
188
189
  /* Initialization states a card can be in */
  enum card_state {
  	NOZOMI_STATE_UKNOWN	= 0,
  	NOZOMI_STATE_ENABLED	= 1,	/* pci device enabled */
  	NOZOMI_STATE_ALLOCATED	= 2,	/* config setup done */
  	NOZOMI_STATE_READY	= 3,	/* flowcontrols received */
  };
20fd1e3be   Frank Seidel   nozomi driver
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
  /* Two different toggle channels exist */
  enum channel_type {
  	CH_A = 0,
  	CH_B = 1,
  };
  
  /* Port definition for the card regarding flow control */
  enum ctrl_port_type {
  	CTRL_CMD	= 0,
  	CTRL_MDM	= 1,
  	CTRL_DIAG	= 2,
  	CTRL_APP1	= 3,
  	CTRL_APP2	= 4,
  	CTRL_ERROR	= -1,
  };
  
  /* Ports that the nozomi has */
  enum port_type {
  	PORT_MDM	= 0,
  	PORT_DIAG	= 1,
  	PORT_APP1	= 2,
  	PORT_APP2	= 3,
  	PORT_CTRL	= 4,
  	PORT_ERROR	= -1,
  };
  
  #ifdef __BIG_ENDIAN
  /* Big endian */
  
  struct toggles {
e9ed537a3   Frank Seidel   nozomi driver update
220
  	unsigned int enabled:5;	/*
20fd1e3be   Frank Seidel   nozomi driver
221
222
223
  				 * Toggle fields are valid if enabled is 0,
  				 * else A-channels must always be used.
  				 */
e9ed537a3   Frank Seidel   nozomi driver update
224
225
226
  	unsigned int diag_dl:1;
  	unsigned int mdm_dl:1;
  	unsigned int mdm_ul:1;
20fd1e3be   Frank Seidel   nozomi driver
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
  } __attribute__ ((packed));
  
  /* Configuration table to read at startup of card */
  /* Is for now only needed during initialization phase */
  struct config_table {
  	u32 signature;
  	u16 product_information;
  	u16 version;
  	u8 pad3[3];
  	struct toggles toggle;
  	u8 pad1[4];
  	u16 dl_mdm_len1;	/*
  				 * If this is 64, it can hold
  				 * 60 bytes + 4 that is length field
  				 */
  	u16 dl_start;
  
  	u16 dl_diag_len1;
  	u16 dl_mdm_len2;	/*
  				 * If this is 64, it can hold
  				 * 60 bytes + 4 that is length field
  				 */
  	u16 dl_app1_len;
  
  	u16 dl_diag_len2;
  	u16 dl_ctrl_len;
  	u16 dl_app2_len;
  	u8 pad2[16];
  	u16 ul_mdm_len1;
  	u16 ul_start;
  	u16 ul_diag_len;
  	u16 ul_mdm_len2;
  	u16 ul_app1_len;
  	u16 ul_app2_len;
  	u16 ul_ctrl_len;
  } __attribute__ ((packed));
  
  /* This stores all control downlink flags */
  struct ctrl_dl {
  	u8 port;
e9ed537a3   Frank Seidel   nozomi driver update
267
268
269
270
271
  	unsigned int reserved:4;
  	unsigned int CTS:1;
  	unsigned int RI:1;
  	unsigned int DCD:1;
  	unsigned int DSR:1;
20fd1e3be   Frank Seidel   nozomi driver
272
273
274
275
276
  } __attribute__ ((packed));
  
  /* This stores all control uplink flags */
  struct ctrl_ul {
  	u8 port;
e9ed537a3   Frank Seidel   nozomi driver update
277
278
279
  	unsigned int reserved:6;
  	unsigned int RTS:1;
  	unsigned int DTR:1;
20fd1e3be   Frank Seidel   nozomi driver
280
281
282
283
284
285
286
  } __attribute__ ((packed));
  
  #else
  /* Little endian */
  
  /* This represents the toggle information */
  struct toggles {
e9ed537a3   Frank Seidel   nozomi driver update
287
288
289
290
  	unsigned int mdm_ul:1;
  	unsigned int mdm_dl:1;
  	unsigned int diag_dl:1;
  	unsigned int enabled:5;	/*
20fd1e3be   Frank Seidel   nozomi driver
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
  				 * Toggle fields are valid if enabled is 0,
  				 * else A-channels must always be used.
  				 */
  } __attribute__ ((packed));
  
  /* Configuration table to read at startup of card */
  struct config_table {
  	u32 signature;
  	u16 version;
  	u16 product_information;
  	struct toggles toggle;
  	u8 pad1[7];
  	u16 dl_start;
  	u16 dl_mdm_len1;	/*
  				 * If this is 64, it can hold
  				 * 60 bytes + 4 that is length field
  				 */
  	u16 dl_mdm_len2;
  	u16 dl_diag_len1;
  	u16 dl_diag_len2;
  	u16 dl_app1_len;
  	u16 dl_app2_len;
  	u16 dl_ctrl_len;
  	u8 pad2[16];
  	u16 ul_start;
  	u16 ul_mdm_len2;
  	u16 ul_mdm_len1;
  	u16 ul_diag_len;
  	u16 ul_app1_len;
  	u16 ul_app2_len;
  	u16 ul_ctrl_len;
  } __attribute__ ((packed));
  
  /* This stores all control downlink flags */
  struct ctrl_dl {
e9ed537a3   Frank Seidel   nozomi driver update
326
327
328
329
330
  	unsigned int DSR:1;
  	unsigned int DCD:1;
  	unsigned int RI:1;
  	unsigned int CTS:1;
  	unsigned int reserverd:4;
20fd1e3be   Frank Seidel   nozomi driver
331
332
333
334
335
  	u8 port;
  } __attribute__ ((packed));
  
  /* This stores all control uplink flags */
  struct ctrl_ul {
e9ed537a3   Frank Seidel   nozomi driver update
336
337
338
  	unsigned int DTR:1;
  	unsigned int RTS:1;
  	unsigned int reserved:6;
20fd1e3be   Frank Seidel   nozomi driver
339
340
341
342
343
344
  	u8 port;
  } __attribute__ ((packed));
  #endif
  
  /* This holds all information that is needed regarding a port */
  struct port {
33dd474ae   Alan Cox   tty: kref nozomi
345
  	struct tty_port port;
20fd1e3be   Frank Seidel   nozomi driver
346
347
348
  	u8 update_flow_control;
  	struct ctrl_ul ctrl_ul;
  	struct ctrl_dl ctrl_dl;
454654878   Stefani Seibold   kfifo: move struc...
349
  	struct kfifo fifo_ul;
20fd1e3be   Frank Seidel   nozomi driver
350
351
352
353
354
355
356
  	void __iomem *dl_addr[2];
  	u32 dl_size[2];
  	u8 toggle_dl;
  	void __iomem *ul_addr[2];
  	u32 ul_size[2];
  	u8 toggle_ul;
  	u16 token_dl;
20fd1e3be   Frank Seidel   nozomi driver
357
358
  	wait_queue_head_t tty_wait;
  	struct async_icount tty_icount;
266794eb7   Alan Cox   nozomi: Add tty_p...
359
360
  
  	struct nozomi *dc;
20fd1e3be   Frank Seidel   nozomi driver
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
  };
  
  /* Private data one for each card in the system */
  struct nozomi {
  	void __iomem *base_addr;
  	unsigned long flip;
  
  	/* Pointers to registers */
  	void __iomem *reg_iir;
  	void __iomem *reg_fcr;
  	void __iomem *reg_ier;
  
  	u16 last_ier;
  	enum card_type card_type;
  	struct config_table config_table;	/* Configuration table */
  	struct pci_dev *pdev;
  	struct port port[NOZOMI_MAX_PORTS];
  	u8 *send_buf;
  
  	spinlock_t spin_mutex;	/* secures access to registers and tty */
  
  	unsigned int index_start;
661b4e89d   Frank Seidel   nozomi: fix initi...
383
  	enum card_state state;
20fd1e3be   Frank Seidel   nozomi driver
384
385
386
387
388
389
390
391
392
393
  	u32 open_ttys;
  };
  
  /* This is a data packet that is read or written to/from card */
  struct buffer {
  	u32 size;		/* size is the length of the data buffer */
  	u8 *data;
  } __attribute__ ((packed));
  
  /*    Global variables */
512f82a06   Bill Pemberton   tty: remove use o...
394
  static const struct pci_device_id nozomi_pci_tbl[] = {
b2a3dbc3e   Alan Cox   nozomi: Tidy up t...
395
  	{PCI_DEVICE(0x1931, 0x000c)},	/* Nozomi HSDPA */
20fd1e3be   Frank Seidel   nozomi driver
396
397
398
399
400
401
402
  	{},
  };
  
  MODULE_DEVICE_TABLE(pci, nozomi_pci_tbl);
  
  static struct nozomi *ndevs[NOZOMI_MAX_CARDS];
  static struct tty_driver *ntty_driver;
266794eb7   Alan Cox   nozomi: Add tty_p...
403
  static const struct tty_port_operations noz_tty_port_ops;
20fd1e3be   Frank Seidel   nozomi driver
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
  /*
   * find card by tty_index
   */
  static inline struct nozomi *get_dc_by_tty(const struct tty_struct *tty)
  {
  	return tty ? ndevs[tty->index / MAX_PORT] : NULL;
  }
  
  static inline struct port *get_port_by_tty(const struct tty_struct *tty)
  {
  	struct nozomi *ndev = get_dc_by_tty(tty);
  	return ndev ? &ndev->port[tty->index % MAX_PORT] : NULL;
  }
  
  /*
   * TODO:
   * -Optimize
   * -Rewrite cleaner
   */
  
  static void read_mem32(u32 *buf, const void __iomem *mem_addr_start,
  			u32 size_bytes)
  {
  	u32 i = 0;
782a6de47   Al Viro   fix iomem misanno...
428
  	const u32 __iomem *ptr = mem_addr_start;
20fd1e3be   Frank Seidel   nozomi driver
429
430
431
432
433
434
435
436
437
  	u16 *buf16;
  
  	if (unlikely(!ptr || !buf))
  		goto out;
  
  	/* shortcut for extremely often used cases */
  	switch (size_bytes) {
  	case 2:	/* 2 bytes */
  		buf16 = (u16 *) buf;
782a6de47   Al Viro   fix iomem misanno...
438
  		*buf16 = __le16_to_cpu(readw(ptr));
20fd1e3be   Frank Seidel   nozomi driver
439
440
441
  		goto out;
  		break;
  	case 4:	/* 4 bytes */
782a6de47   Al Viro   fix iomem misanno...
442
  		*(buf) = __le32_to_cpu(readl(ptr));
20fd1e3be   Frank Seidel   nozomi driver
443
444
445
446
447
448
449
450
  		goto out;
  		break;
  	}
  
  	while (i < size_bytes) {
  		if (size_bytes - i == 2) {
  			/* Handle 2 bytes in the end */
  			buf16 = (u16 *) buf;
782a6de47   Al Viro   fix iomem misanno...
451
  			*(buf16) = __le16_to_cpu(readw(ptr));
20fd1e3be   Frank Seidel   nozomi driver
452
453
454
  			i += 2;
  		} else {
  			/* Read 4 bytes */
782a6de47   Al Viro   fix iomem misanno...
455
  			*(buf) = __le32_to_cpu(readl(ptr));
20fd1e3be   Frank Seidel   nozomi driver
456
457
458
459
460
461
462
463
464
465
466
467
468
469
  			i += 4;
  		}
  		buf++;
  		ptr++;
  	}
  out:
  	return;
  }
  
  /*
   * TODO:
   * -Optimize
   * -Rewrite cleaner
   */
71e1b4abd   Jan Engelhardt   nozomi: constify ...
470
  static u32 write_mem32(void __iomem *mem_addr_start, const u32 *buf,
20fd1e3be   Frank Seidel   nozomi driver
471
472
473
  			u32 size_bytes)
  {
  	u32 i = 0;
782a6de47   Al Viro   fix iomem misanno...
474
  	u32 __iomem *ptr = mem_addr_start;
71e1b4abd   Jan Engelhardt   nozomi: constify ...
475
  	const u16 *buf16;
20fd1e3be   Frank Seidel   nozomi driver
476
477
478
479
480
481
482
  
  	if (unlikely(!ptr || !buf))
  		return 0;
  
  	/* shortcut for extremely often used cases */
  	switch (size_bytes) {
  	case 2:	/* 2 bytes */
71e1b4abd   Jan Engelhardt   nozomi: constify ...
483
  		buf16 = (const u16 *)buf;
782a6de47   Al Viro   fix iomem misanno...
484
  		writew(__cpu_to_le16(*buf16), ptr);
20fd1e3be   Frank Seidel   nozomi driver
485
486
487
488
489
490
491
  		return 2;
  		break;
  	case 1: /*
  		 * also needs to write 4 bytes in this case
  		 * so falling through..
  		 */
  	case 4: /* 4 bytes */
782a6de47   Al Viro   fix iomem misanno...
492
  		writel(__cpu_to_le32(*buf), ptr);
20fd1e3be   Frank Seidel   nozomi driver
493
494
495
496
497
498
499
  		return 4;
  		break;
  	}
  
  	while (i < size_bytes) {
  		if (size_bytes - i == 2) {
  			/* 2 bytes */
71e1b4abd   Jan Engelhardt   nozomi: constify ...
500
  			buf16 = (const u16 *)buf;
782a6de47   Al Viro   fix iomem misanno...
501
  			writew(__cpu_to_le16(*buf16), ptr);
20fd1e3be   Frank Seidel   nozomi driver
502
503
504
  			i += 2;
  		} else {
  			/* 4 bytes */
782a6de47   Al Viro   fix iomem misanno...
505
  			writel(__cpu_to_le32(*buf), ptr);
20fd1e3be   Frank Seidel   nozomi driver
506
507
508
509
510
511
512
513
514
  			i += 4;
  		}
  		buf++;
  		ptr++;
  	}
  	return i;
  }
  
  /* Setup pointers to different channels and also setup buffer sizes. */
7acb88af6   Chen Gang   drivers/tty/nozom...
515
  static void nozomi_setup_memory(struct nozomi *dc)
20fd1e3be   Frank Seidel   nozomi driver
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
  {
  	void __iomem *offset = dc->base_addr + dc->config_table.dl_start;
  	/* The length reported is including the length field of 4 bytes,
  	 * hence subtract with 4.
  	 */
  	const u16 buff_offset = 4;
  
  	/* Modem port dl configuration */
  	dc->port[PORT_MDM].dl_addr[CH_A] = offset;
  	dc->port[PORT_MDM].dl_addr[CH_B] =
  				(offset += dc->config_table.dl_mdm_len1);
  	dc->port[PORT_MDM].dl_size[CH_A] =
  				dc->config_table.dl_mdm_len1 - buff_offset;
  	dc->port[PORT_MDM].dl_size[CH_B] =
  				dc->config_table.dl_mdm_len2 - buff_offset;
  
  	/* Diag port dl configuration */
  	dc->port[PORT_DIAG].dl_addr[CH_A] =
  				(offset += dc->config_table.dl_mdm_len2);
  	dc->port[PORT_DIAG].dl_size[CH_A] =
  				dc->config_table.dl_diag_len1 - buff_offset;
  	dc->port[PORT_DIAG].dl_addr[CH_B] =
  				(offset += dc->config_table.dl_diag_len1);
  	dc->port[PORT_DIAG].dl_size[CH_B] =
  				dc->config_table.dl_diag_len2 - buff_offset;
  
  	/* App1 port dl configuration */
  	dc->port[PORT_APP1].dl_addr[CH_A] =
  				(offset += dc->config_table.dl_diag_len2);
  	dc->port[PORT_APP1].dl_size[CH_A] =
  				dc->config_table.dl_app1_len - buff_offset;
  
  	/* App2 port dl configuration */
  	dc->port[PORT_APP2].dl_addr[CH_A] =
  				(offset += dc->config_table.dl_app1_len);
  	dc->port[PORT_APP2].dl_size[CH_A] =
  				dc->config_table.dl_app2_len - buff_offset;
  
  	/* Ctrl dl configuration */
  	dc->port[PORT_CTRL].dl_addr[CH_A] =
  				(offset += dc->config_table.dl_app2_len);
  	dc->port[PORT_CTRL].dl_size[CH_A] =
  				dc->config_table.dl_ctrl_len - buff_offset;
  
  	offset = dc->base_addr + dc->config_table.ul_start;
  
  	/* Modem Port ul configuration */
  	dc->port[PORT_MDM].ul_addr[CH_A] = offset;
  	dc->port[PORT_MDM].ul_size[CH_A] =
  				dc->config_table.ul_mdm_len1 - buff_offset;
  	dc->port[PORT_MDM].ul_addr[CH_B] =
  				(offset += dc->config_table.ul_mdm_len1);
  	dc->port[PORT_MDM].ul_size[CH_B] =
  				dc->config_table.ul_mdm_len2 - buff_offset;
  
  	/* Diag port ul configuration */
  	dc->port[PORT_DIAG].ul_addr[CH_A] =
  				(offset += dc->config_table.ul_mdm_len2);
  	dc->port[PORT_DIAG].ul_size[CH_A] =
  				dc->config_table.ul_diag_len - buff_offset;
  
  	/* App1 port ul configuration */
  	dc->port[PORT_APP1].ul_addr[CH_A] =
  				(offset += dc->config_table.ul_diag_len);
  	dc->port[PORT_APP1].ul_size[CH_A] =
  				dc->config_table.ul_app1_len - buff_offset;
  
  	/* App2 port ul configuration */
  	dc->port[PORT_APP2].ul_addr[CH_A] =
  				(offset += dc->config_table.ul_app1_len);
  	dc->port[PORT_APP2].ul_size[CH_A] =
  				dc->config_table.ul_app2_len - buff_offset;
  
  	/* Ctrl ul configuration */
  	dc->port[PORT_CTRL].ul_addr[CH_A] =
  				(offset += dc->config_table.ul_app2_len);
  	dc->port[PORT_CTRL].ul_size[CH_A] =
  				dc->config_table.ul_ctrl_len - buff_offset;
  }
  
  /* Dump config table under initalization phase */
  #ifdef DEBUG
  static void dump_table(const struct nozomi *dc)
  {
  	DBG3("signature: 0x%08X", dc->config_table.signature);
  	DBG3("version: 0x%04X", dc->config_table.version);
  	DBG3("product_information: 0x%04X", \
  				dc->config_table.product_information);
  	DBG3("toggle enabled: %d", dc->config_table.toggle.enabled);
  	DBG3("toggle up_mdm: %d", dc->config_table.toggle.mdm_ul);
  	DBG3("toggle dl_mdm: %d", dc->config_table.toggle.mdm_dl);
  	DBG3("toggle dl_dbg: %d", dc->config_table.toggle.diag_dl);
  
  	DBG3("dl_start: 0x%04X", dc->config_table.dl_start);
  	DBG3("dl_mdm_len0: 0x%04X, %d", dc->config_table.dl_mdm_len1,
  	   dc->config_table.dl_mdm_len1);
  	DBG3("dl_mdm_len1: 0x%04X, %d", dc->config_table.dl_mdm_len2,
  	   dc->config_table.dl_mdm_len2);
  	DBG3("dl_diag_len0: 0x%04X, %d", dc->config_table.dl_diag_len1,
  	   dc->config_table.dl_diag_len1);
  	DBG3("dl_diag_len1: 0x%04X, %d", dc->config_table.dl_diag_len2,
  	   dc->config_table.dl_diag_len2);
  	DBG3("dl_app1_len: 0x%04X, %d", dc->config_table.dl_app1_len,
  	   dc->config_table.dl_app1_len);
  	DBG3("dl_app2_len: 0x%04X, %d", dc->config_table.dl_app2_len,
  	   dc->config_table.dl_app2_len);
  	DBG3("dl_ctrl_len: 0x%04X, %d", dc->config_table.dl_ctrl_len,
  	   dc->config_table.dl_ctrl_len);
  	DBG3("ul_start: 0x%04X, %d", dc->config_table.ul_start,
  	   dc->config_table.ul_start);
  	DBG3("ul_mdm_len[0]: 0x%04X, %d", dc->config_table.ul_mdm_len1,
  	   dc->config_table.ul_mdm_len1);
  	DBG3("ul_mdm_len[1]: 0x%04X, %d", dc->config_table.ul_mdm_len2,
  	   dc->config_table.ul_mdm_len2);
  	DBG3("ul_diag_len: 0x%04X, %d", dc->config_table.ul_diag_len,
  	   dc->config_table.ul_diag_len);
  	DBG3("ul_app1_len: 0x%04X, %d", dc->config_table.ul_app1_len,
  	   dc->config_table.ul_app1_len);
  	DBG3("ul_app2_len: 0x%04X, %d", dc->config_table.ul_app2_len,
  	   dc->config_table.ul_app2_len);
  	DBG3("ul_ctrl_len: 0x%04X, %d", dc->config_table.ul_ctrl_len,
  	   dc->config_table.ul_ctrl_len);
  }
  #else
e9ed537a3   Frank Seidel   nozomi driver update
640
  static inline void dump_table(const struct nozomi *dc) { }
20fd1e3be   Frank Seidel   nozomi driver
641
642
643
644
645
646
647
648
649
650
  #endif
  
  /*
   * Read configuration table from card under intalization phase
   * Returns 1 if ok, else 0
   */
  static int nozomi_read_config_table(struct nozomi *dc)
  {
  	read_mem32((u32 *) &dc->config_table, dc->base_addr + 0,
  						sizeof(struct config_table));
03ac6b349   Valentin Rothberg   drivers/tty/nozom...
651
  	if (dc->config_table.signature != NOZOMI_CONFIG_MAGIC) {
20fd1e3be   Frank Seidel   nozomi driver
652
653
  		dev_err(&dc->pdev->dev, "ConfigTable Bad! 0x%08X != 0x%08X
  ",
03ac6b349   Valentin Rothberg   drivers/tty/nozom...
654
  			dc->config_table.signature, NOZOMI_CONFIG_MAGIC);
20fd1e3be   Frank Seidel   nozomi driver
655
656
657
658
659
660
661
  		return 0;
  	}
  
  	if ((dc->config_table.version == 0)
  	    || (dc->config_table.toggle.enabled == TOGGLE_VALID)) {
  		int i;
  		DBG1("Second phase, configuring card");
7acb88af6   Chen Gang   drivers/tty/nozom...
662
  		nozomi_setup_memory(dc);
20fd1e3be   Frank Seidel   nozomi driver
663
664
665
666
667
668
669
670
671
672
673
  
  		dc->port[PORT_MDM].toggle_ul = dc->config_table.toggle.mdm_ul;
  		dc->port[PORT_MDM].toggle_dl = dc->config_table.toggle.mdm_dl;
  		dc->port[PORT_DIAG].toggle_dl = dc->config_table.toggle.diag_dl;
  		DBG1("toggle ports: MDM UL:%d MDM DL:%d, DIAG DL:%d",
  		   dc->port[PORT_MDM].toggle_ul,
  		   dc->port[PORT_MDM].toggle_dl, dc->port[PORT_DIAG].toggle_dl);
  
  		dump_table(dc);
  
  		for (i = PORT_MDM; i < MAX_PORT; i++) {
20fd1e3be   Frank Seidel   nozomi driver
674
675
676
677
678
679
680
  			memset(&dc->port[i].ctrl_dl, 0, sizeof(struct ctrl_dl));
  			memset(&dc->port[i].ctrl_ul, 0, sizeof(struct ctrl_ul));
  		}
  
  		/* Enable control channel */
  		dc->last_ier = dc->last_ier | CTRL_DL;
  		writew(dc->last_ier, dc->reg_ier);
661b4e89d   Frank Seidel   nozomi: fix initi...
681
  		dc->state = NOZOMI_STATE_ALLOCATED;
20fd1e3be   Frank Seidel   nozomi driver
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
  		dev_info(&dc->pdev->dev, "Initialization OK!
  ");
  		return 1;
  	}
  
  	if ((dc->config_table.version > 0)
  	    && (dc->config_table.toggle.enabled != TOGGLE_VALID)) {
  		u32 offset = 0;
  		DBG1("First phase: pushing upload buffers, clearing download");
  
  		dev_info(&dc->pdev->dev, "Version of card: %d
  ",
  			 dc->config_table.version);
  
  		/* Here we should disable all I/O over F32. */
7acb88af6   Chen Gang   drivers/tty/nozom...
697
  		nozomi_setup_memory(dc);
20fd1e3be   Frank Seidel   nozomi driver
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
  
  		/*
  		 * We should send ALL channel pair tokens back along
  		 * with reset token
  		 */
  
  		/* push upload modem buffers */
  		write_mem32(dc->port[PORT_MDM].ul_addr[CH_A],
  			(u32 *) &offset, 4);
  		write_mem32(dc->port[PORT_MDM].ul_addr[CH_B],
  			(u32 *) &offset, 4);
  
  		writew(MDM_UL | DIAG_DL | MDM_DL, dc->reg_fcr);
  
  		DBG1("First phase done");
  	}
  
  	return 1;
  }
  
  /* Enable uplink interrupts  */
  static void enable_transmit_ul(enum port_type port, struct nozomi *dc)
  {
71e1b4abd   Jan Engelhardt   nozomi: constify ...
721
  	static const u16 mask[] = {MDM_UL, DIAG_UL, APP1_UL, APP2_UL, CTRL_UL};
20fd1e3be   Frank Seidel   nozomi driver
722
723
724
725
726
727
728
729
730
731
732
733
734
  
  	if (port < NOZOMI_MAX_PORTS) {
  		dc->last_ier |= mask[port];
  		writew(dc->last_ier, dc->reg_ier);
  	} else {
  		dev_err(&dc->pdev->dev, "Called with wrong port?
  ");
  	}
  }
  
  /* Disable uplink interrupts  */
  static void disable_transmit_ul(enum port_type port, struct nozomi *dc)
  {
71e1b4abd   Jan Engelhardt   nozomi: constify ...
735
736
  	static const u16 mask[] =
  		{~MDM_UL, ~DIAG_UL, ~APP1_UL, ~APP2_UL, ~CTRL_UL};
20fd1e3be   Frank Seidel   nozomi driver
737
738
739
740
741
742
743
744
745
746
747
748
749
  
  	if (port < NOZOMI_MAX_PORTS) {
  		dc->last_ier &= mask[port];
  		writew(dc->last_ier, dc->reg_ier);
  	} else {
  		dev_err(&dc->pdev->dev, "Called with wrong port?
  ");
  	}
  }
  
  /* Enable downlink interrupts */
  static void enable_transmit_dl(enum port_type port, struct nozomi *dc)
  {
71e1b4abd   Jan Engelhardt   nozomi: constify ...
750
  	static const u16 mask[] = {MDM_DL, DIAG_DL, APP1_DL, APP2_DL, CTRL_DL};
20fd1e3be   Frank Seidel   nozomi driver
751
752
753
754
755
756
757
758
759
760
761
762
763
  
  	if (port < NOZOMI_MAX_PORTS) {
  		dc->last_ier |= mask[port];
  		writew(dc->last_ier, dc->reg_ier);
  	} else {
  		dev_err(&dc->pdev->dev, "Called with wrong port?
  ");
  	}
  }
  
  /* Disable downlink interrupts */
  static void disable_transmit_dl(enum port_type port, struct nozomi *dc)
  {
71e1b4abd   Jan Engelhardt   nozomi: constify ...
764
765
  	static const u16 mask[] =
  		{~MDM_DL, ~DIAG_DL, ~APP1_DL, ~APP2_DL, ~CTRL_DL};
20fd1e3be   Frank Seidel   nozomi driver
766
767
768
769
770
771
772
773
774
775
776
777
778
779
  
  	if (port < NOZOMI_MAX_PORTS) {
  		dc->last_ier &= mask[port];
  		writew(dc->last_ier, dc->reg_ier);
  	} else {
  		dev_err(&dc->pdev->dev, "Called with wrong port?
  ");
  	}
  }
  
  /*
   * Return 1 - send buffer to card and ack.
   * Return 0 - don't ack, don't send buffer to card.
   */
33dd474ae   Alan Cox   tty: kref nozomi
780
  static int send_data(enum port_type index, struct nozomi *dc)
20fd1e3be   Frank Seidel   nozomi driver
781
782
  {
  	u32 size = 0;
33dd474ae   Alan Cox   tty: kref nozomi
783
  	struct port *port = &dc->port[index];
18bbe0c26   Frank Seidel   nozomi: finish co...
784
  	const u8 toggle = port->toggle_ul;
20fd1e3be   Frank Seidel   nozomi driver
785
  	void __iomem *addr = port->ul_addr[toggle];
18bbe0c26   Frank Seidel   nozomi: finish co...
786
  	const u32 ul_size = port->ul_size[toggle];
20fd1e3be   Frank Seidel   nozomi driver
787
788
  
  	/* Get data from tty and place in buf for now */
7acd72eb8   Stefani Seibold   kfifo: rename kfi...
789
  	size = kfifo_out(&port->fifo_ul, dc->send_buf,
20fd1e3be   Frank Seidel   nozomi driver
790
791
792
793
794
795
796
797
798
799
800
801
  			   ul_size < SEND_BUF_MAX ? ul_size : SEND_BUF_MAX);
  
  	if (size == 0) {
  		DBG4("No more data to send, disable link:");
  		return 0;
  	}
  
  	/* DUMP(buf, size); */
  
  	/* Write length + data */
  	write_mem32(addr, (u32 *) &size, 4);
  	write_mem32(addr + 4, (u32 *) dc->send_buf, size);
6aad04f21   Jiri Slaby   TTY: add tty_port...
802
  	tty_port_tty_wakeup(&port->port);
20fd1e3be   Frank Seidel   nozomi driver
803
804
805
806
807
808
809
810
811
812
813
814
  
  	return 1;
  }
  
  /* If all data has been read, return 1, else 0 */
  static int receive_data(enum port_type index, struct nozomi *dc)
  {
  	u8 buf[RECEIVE_BUF_MAX] = { 0 };
  	int size;
  	u32 offset = 4;
  	struct port *port = &dc->port[index];
  	void __iomem *addr = port->dl_addr[port->toggle_dl];
33dd474ae   Alan Cox   tty: kref nozomi
815
  	struct tty_struct *tty = tty_port_tty_get(&port->port);
9237a81a1   Jiri Slaby   tty: nozomi, fix ...
816
  	int i, ret;
20fd1e3be   Frank Seidel   nozomi driver
817

a4f642a8a   Arnd Bergmann   tty: nozomi: avoi...
818
  	size = __le32_to_cpu(readl(addr));
20fd1e3be   Frank Seidel   nozomi driver
819
  	/*  DBG1( "%d bytes port: %d", size, index); */
97ef38b82   Peter Hurley   tty: Replace TTY_...
820
  	if (tty && tty_throttled(tty)) {
20fd1e3be   Frank Seidel   nozomi driver
821
822
823
824
825
  		DBG1("No room in tty, don't read data, don't ack interrupt, "
  			"disable interrupt");
  
  		/* disable interrupt in downlink... */
  		disable_transmit_dl(index, dc);
9237a81a1   Jiri Slaby   tty: nozomi, fix ...
826
827
  		ret = 0;
  		goto put;
20fd1e3be   Frank Seidel   nozomi driver
828
829
830
831
832
  	}
  
  	if (unlikely(size == 0)) {
  		dev_err(&dc->pdev->dev, "size == 0?
  ");
9237a81a1   Jiri Slaby   tty: nozomi, fix ...
833
834
  		ret = 1;
  		goto put;
20fd1e3be   Frank Seidel   nozomi driver
835
  	}
20fd1e3be   Frank Seidel   nozomi driver
836
837
838
839
  	while (size > 0) {
  		read_mem32((u32 *) buf, addr + offset, RECEIVE_BUF_MAX);
  
  		if (size == 1) {
92a19f9ce   Jiri Slaby   TTY: switch tty_i...
840
  			tty_insert_flip_char(&port->port, buf[0], TTY_NORMAL);
20fd1e3be   Frank Seidel   nozomi driver
841
842
  			size = 0;
  		} else if (size < RECEIVE_BUF_MAX) {
05c7cd399   Jiri Slaby   TTY: switch tty_i...
843
844
  			size -= tty_insert_flip_string(&port->port,
  					(char *)buf, size);
20fd1e3be   Frank Seidel   nozomi driver
845
  		} else {
05c7cd399   Jiri Slaby   TTY: switch tty_i...
846
847
  			i = tty_insert_flip_string(&port->port,
  					(char *)buf, RECEIVE_BUF_MAX);
20fd1e3be   Frank Seidel   nozomi driver
848
849
850
851
852
853
  			size -= i;
  			offset += i;
  		}
  	}
  
  	set_bit(index, &dc->flip);
9237a81a1   Jiri Slaby   tty: nozomi, fix ...
854
855
  	ret = 1;
  put:
33dd474ae   Alan Cox   tty: kref nozomi
856
  	tty_kref_put(tty);
9237a81a1   Jiri Slaby   tty: nozomi, fix ...
857
  	return ret;
20fd1e3be   Frank Seidel   nozomi driver
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
  }
  
  /* Debug for interrupts */
  #ifdef DEBUG
  static char *interrupt2str(u16 interrupt)
  {
  	static char buf[TMP_BUF_MAX];
  	char *p = buf;
  
  	interrupt & MDM_DL1 ? p += snprintf(p, TMP_BUF_MAX, "MDM_DL1 ") : NULL;
  	interrupt & MDM_DL2 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
  					"MDM_DL2 ") : NULL;
  
  	interrupt & MDM_UL1 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
  					"MDM_UL1 ") : NULL;
  	interrupt & MDM_UL2 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
  					"MDM_UL2 ") : NULL;
  
  	interrupt & DIAG_DL1 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
  					"DIAG_DL1 ") : NULL;
  	interrupt & DIAG_DL2 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
  					"DIAG_DL2 ") : NULL;
  
  	interrupt & DIAG_UL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
  					"DIAG_UL ") : NULL;
  
  	interrupt & APP1_DL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
  					"APP1_DL ") : NULL;
  	interrupt & APP2_DL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
  					"APP2_DL ") : NULL;
  
  	interrupt & APP1_UL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
  					"APP1_UL ") : NULL;
  	interrupt & APP2_UL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
  					"APP2_UL ") : NULL;
  
  	interrupt & CTRL_DL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
  					"CTRL_DL ") : NULL;
  	interrupt & CTRL_UL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
  					"CTRL_UL ") : NULL;
  
  	interrupt & RESET ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
  					"RESET ") : NULL;
  
  	return buf;
  }
  #endif
  
  /*
   * Receive flow control
   * Return 1 - If ok, else 0
   */
  static int receive_flow_control(struct nozomi *dc)
  {
  	enum port_type port = PORT_MDM;
  	struct ctrl_dl ctrl_dl;
  	struct ctrl_dl old_ctrl;
  	u16 enable_ier = 0;
  
  	read_mem32((u32 *) &ctrl_dl, dc->port[PORT_CTRL].dl_addr[CH_A], 2);
  
  	switch (ctrl_dl.port) {
  	case CTRL_CMD:
  		DBG1("The Base Band sends this value as a response to a "
  			"request for IMSI detach sent over the control "
  			"channel uplink (see section 7.6.1).");
  		break;
  	case CTRL_MDM:
  		port = PORT_MDM;
  		enable_ier = MDM_DL;
  		break;
  	case CTRL_DIAG:
  		port = PORT_DIAG;
  		enable_ier = DIAG_DL;
  		break;
  	case CTRL_APP1:
  		port = PORT_APP1;
  		enable_ier = APP1_DL;
  		break;
  	case CTRL_APP2:
  		port = PORT_APP2;
  		enable_ier = APP2_DL;
661b4e89d   Frank Seidel   nozomi: fix initi...
940
941
942
943
944
945
946
947
948
  		if (dc->state == NOZOMI_STATE_ALLOCATED) {
  			/*
  			 * After card initialization the flow control
  			 * received for APP2 is always the last
  			 */
  			dc->state = NOZOMI_STATE_READY;
  			dev_info(&dc->pdev->dev, "Device READY!
  ");
  		}
20fd1e3be   Frank Seidel   nozomi driver
949
950
951
952
953
954
  		break;
  	default:
  		dev_err(&dc->pdev->dev,
  			"ERROR: flow control received for non-existing port
  ");
  		return 0;
fc811472c   Joe Perches   tty: Remove unnec...
955
  	}
20fd1e3be   Frank Seidel   nozomi driver
956
957
958
959
960
961
962
963
964
965
966
967
968
  
  	DBG1("0x%04X->0x%04X", *((u16 *)&dc->port[port].ctrl_dl),
  	   *((u16 *)&ctrl_dl));
  
  	old_ctrl = dc->port[port].ctrl_dl;
  	dc->port[port].ctrl_dl = ctrl_dl;
  
  	if (old_ctrl.CTS == 1 && ctrl_dl.CTS == 0) {
  		DBG1("Disable interrupt (0x%04X) on port: %d",
  			enable_ier, port);
  		disable_transmit_ul(port, dc);
  
  	} else if (old_ctrl.CTS == 0 && ctrl_dl.CTS == 1) {
e64c026dd   Stefani Seibold   kfifo: cleanup na...
969
  		if (kfifo_len(&dc->port[port].fifo_ul)) {
20fd1e3be   Frank Seidel   nozomi driver
970
971
972
  			DBG1("Enable interrupt (0x%04X) on port: %d",
  				enable_ier, port);
  			DBG1("Data in buffer [%d], enable transmit! ",
e64c026dd   Stefani Seibold   kfifo: cleanup na...
973
  				kfifo_len(&dc->port[port].fifo_ul));
20fd1e3be   Frank Seidel   nozomi driver
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
  			enable_transmit_ul(port, dc);
  		} else {
  			DBG1("No data in buffer...");
  		}
  	}
  
  	if (*(u16 *)&old_ctrl == *(u16 *)&ctrl_dl) {
  		DBG1(" No change in mctrl");
  		return 1;
  	}
  	/* Update statistics */
  	if (old_ctrl.CTS != ctrl_dl.CTS)
  		dc->port[port].tty_icount.cts++;
  	if (old_ctrl.DSR != ctrl_dl.DSR)
  		dc->port[port].tty_icount.dsr++;
  	if (old_ctrl.RI != ctrl_dl.RI)
  		dc->port[port].tty_icount.rng++;
  	if (old_ctrl.DCD != ctrl_dl.DCD)
  		dc->port[port].tty_icount.dcd++;
  
  	wake_up_interruptible(&dc->port[port].tty_wait);
  
  	DBG1("port: %d DCD(%d), CTS(%d), RI(%d), DSR(%d)",
  	   port,
  	   dc->port[port].tty_icount.dcd, dc->port[port].tty_icount.cts,
  	   dc->port[port].tty_icount.rng, dc->port[port].tty_icount.dsr);
  
  	return 1;
  }
  
  static enum ctrl_port_type port2ctrl(enum port_type port,
  					const struct nozomi *dc)
  {
  	switch (port) {
  	case PORT_MDM:
  		return CTRL_MDM;
  	case PORT_DIAG:
  		return CTRL_DIAG;
  	case PORT_APP1:
  		return CTRL_APP1;
  	case PORT_APP2:
  		return CTRL_APP2;
  	default:
  		dev_err(&dc->pdev->dev,
  			"ERROR: send flow control " \
  			"received for non-existing port
  ");
fc811472c   Joe Perches   tty: Remove unnec...
1021
  	}
20fd1e3be   Frank Seidel   nozomi driver
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
  	return CTRL_ERROR;
  }
  
  /*
   * Send flow control, can only update one channel at a time
   * Return 0 - If we have updated all flow control
   * Return 1 - If we need to update more flow control, ack current enable more
   */
  static int send_flow_control(struct nozomi *dc)
  {
  	u32 i, more_flow_control_to_be_updated = 0;
  	u16 *ctrl;
  
  	for (i = PORT_MDM; i < MAX_PORT; i++) {
  		if (dc->port[i].update_flow_control) {
  			if (more_flow_control_to_be_updated) {
  				/* We have more flow control to be updated */
  				return 1;
  			}
  			dc->port[i].ctrl_ul.port = port2ctrl(i, dc);
  			ctrl = (u16 *)&dc->port[i].ctrl_ul;
  			write_mem32(dc->port[PORT_CTRL].ul_addr[0], \
  				(u32 *) ctrl, 2);
  			dc->port[i].update_flow_control = 0;
  			more_flow_control_to_be_updated = 1;
  		}
  	}
  	return 0;
  }
  
  /*
e9ed537a3   Frank Seidel   nozomi driver update
1053
   * Handle downlink data, ports that are handled are modem and diagnostics
20fd1e3be   Frank Seidel   nozomi driver
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
   * Return 1 - ok
   * Return 0 - toggle fields are out of sync
   */
  static int handle_data_dl(struct nozomi *dc, enum port_type port, u8 *toggle,
  			u16 read_iir, u16 mask1, u16 mask2)
  {
  	if (*toggle == 0 && read_iir & mask1) {
  		if (receive_data(port, dc)) {
  			writew(mask1, dc->reg_fcr);
  			*toggle = !(*toggle);
  		}
  
  		if (read_iir & mask2) {
  			if (receive_data(port, dc)) {
  				writew(mask2, dc->reg_fcr);
  				*toggle = !(*toggle);
  			}
  		}
  	} else if (*toggle == 1 && read_iir & mask2) {
  		if (receive_data(port, dc)) {
  			writew(mask2, dc->reg_fcr);
  			*toggle = !(*toggle);
  		}
  
  		if (read_iir & mask1) {
  			if (receive_data(port, dc)) {
  				writew(mask1, dc->reg_fcr);
  				*toggle = !(*toggle);
  			}
  		}
  	} else {
  		dev_err(&dc->pdev->dev, "port out of sync!, toggle:%d
  ",
  			*toggle);
  		return 0;
  	}
  	return 1;
  }
  
  /*
   * Handle uplink data, this is currently for the modem port
   * Return 1 - ok
   * Return 0 - toggle field are out of sync
   */
  static int handle_data_ul(struct nozomi *dc, enum port_type port, u16 read_iir)
  {
  	u8 *toggle = &(dc->port[port].toggle_ul);
  
  	if (*toggle == 0 && read_iir & MDM_UL1) {
  		dc->last_ier &= ~MDM_UL;
  		writew(dc->last_ier, dc->reg_ier);
  		if (send_data(port, dc)) {
  			writew(MDM_UL1, dc->reg_fcr);
  			dc->last_ier = dc->last_ier | MDM_UL;
  			writew(dc->last_ier, dc->reg_ier);
  			*toggle = !*toggle;
  		}
  
  		if (read_iir & MDM_UL2) {
  			dc->last_ier &= ~MDM_UL;
  			writew(dc->last_ier, dc->reg_ier);
  			if (send_data(port, dc)) {
  				writew(MDM_UL2, dc->reg_fcr);
  				dc->last_ier = dc->last_ier | MDM_UL;
  				writew(dc->last_ier, dc->reg_ier);
  				*toggle = !*toggle;
  			}
  		}
  
  	} else if (*toggle == 1 && read_iir & MDM_UL2) {
  		dc->last_ier &= ~MDM_UL;
  		writew(dc->last_ier, dc->reg_ier);
  		if (send_data(port, dc)) {
  			writew(MDM_UL2, dc->reg_fcr);
  			dc->last_ier = dc->last_ier | MDM_UL;
  			writew(dc->last_ier, dc->reg_ier);
  			*toggle = !*toggle;
  		}
  
  		if (read_iir & MDM_UL1) {
  			dc->last_ier &= ~MDM_UL;
  			writew(dc->last_ier, dc->reg_ier);
  			if (send_data(port, dc)) {
  				writew(MDM_UL1, dc->reg_fcr);
  				dc->last_ier = dc->last_ier | MDM_UL;
  				writew(dc->last_ier, dc->reg_ier);
  				*toggle = !*toggle;
  			}
  		}
  	} else {
  		writew(read_iir & MDM_UL, dc->reg_fcr);
  		dev_err(&dc->pdev->dev, "port out of sync!
  ");
  		return 0;
  	}
  	return 1;
  }
  
  static irqreturn_t interrupt_handler(int irq, void *dev_id)
  {
  	struct nozomi *dc = dev_id;
  	unsigned int a;
  	u16 read_iir;
  
  	if (!dc)
  		return IRQ_NONE;
  
  	spin_lock(&dc->spin_mutex);
  	read_iir = readw(dc->reg_iir);
  
  	/* Card removed */
  	if (read_iir == (u16)-1)
  		goto none;
  	/*
  	 * Just handle interrupt enabled in IER
  	 * (by masking with dc->last_ier)
  	 */
  	read_iir &= dc->last_ier;
  
  	if (read_iir == 0)
  		goto none;
  
  
  	DBG4("%s irq:0x%04X, prev:0x%04X", interrupt2str(read_iir), read_iir,
  		dc->last_ier);
  
  	if (read_iir & RESET) {
  		if (unlikely(!nozomi_read_config_table(dc))) {
  			dc->last_ier = 0x0;
  			writew(dc->last_ier, dc->reg_ier);
  			dev_err(&dc->pdev->dev, "Could not read status from "
  				"card, we should disable interface
  ");
  		} else {
  			writew(RESET, dc->reg_fcr);
  		}
  		/* No more useful info if this was the reset interrupt. */
  		goto exit_handler;
  	}
  	if (read_iir & CTRL_UL) {
  		DBG1("CTRL_UL");
  		dc->last_ier &= ~CTRL_UL;
  		writew(dc->last_ier, dc->reg_ier);
  		if (send_flow_control(dc)) {
  			writew(CTRL_UL, dc->reg_fcr);
  			dc->last_ier = dc->last_ier | CTRL_UL;
  			writew(dc->last_ier, dc->reg_ier);
  		}
  	}
  	if (read_iir & CTRL_DL) {
  		receive_flow_control(dc);
  		writew(CTRL_DL, dc->reg_fcr);
  	}
  	if (read_iir & MDM_DL) {
  		if (!handle_data_dl(dc, PORT_MDM,
  				&(dc->port[PORT_MDM].toggle_dl), read_iir,
  				MDM_DL1, MDM_DL2)) {
  			dev_err(&dc->pdev->dev, "MDM_DL out of sync!
  ");
  			goto exit_handler;
  		}
  	}
  	if (read_iir & MDM_UL) {
  		if (!handle_data_ul(dc, PORT_MDM, read_iir)) {
  			dev_err(&dc->pdev->dev, "MDM_UL out of sync!
  ");
  			goto exit_handler;
  		}
  	}
  	if (read_iir & DIAG_DL) {
  		if (!handle_data_dl(dc, PORT_DIAG,
  				&(dc->port[PORT_DIAG].toggle_dl), read_iir,
  				DIAG_DL1, DIAG_DL2)) {
  			dev_err(&dc->pdev->dev, "DIAG_DL out of sync!
  ");
  			goto exit_handler;
  		}
  	}
  	if (read_iir & DIAG_UL) {
  		dc->last_ier &= ~DIAG_UL;
  		writew(dc->last_ier, dc->reg_ier);
  		if (send_data(PORT_DIAG, dc)) {
  			writew(DIAG_UL, dc->reg_fcr);
  			dc->last_ier = dc->last_ier | DIAG_UL;
  			writew(dc->last_ier, dc->reg_ier);
  		}
  	}
  	if (read_iir & APP1_DL) {
  		if (receive_data(PORT_APP1, dc))
  			writew(APP1_DL, dc->reg_fcr);
  	}
  	if (read_iir & APP1_UL) {
  		dc->last_ier &= ~APP1_UL;
  		writew(dc->last_ier, dc->reg_ier);
  		if (send_data(PORT_APP1, dc)) {
  			writew(APP1_UL, dc->reg_fcr);
  			dc->last_ier = dc->last_ier | APP1_UL;
  			writew(dc->last_ier, dc->reg_ier);
  		}
  	}
  	if (read_iir & APP2_DL) {
  		if (receive_data(PORT_APP2, dc))
  			writew(APP2_DL, dc->reg_fcr);
  	}
  	if (read_iir & APP2_UL) {
  		dc->last_ier &= ~APP2_UL;
  		writew(dc->last_ier, dc->reg_ier);
  		if (send_data(PORT_APP2, dc)) {
  			writew(APP2_UL, dc->reg_fcr);
  			dc->last_ier = dc->last_ier | APP2_UL;
  			writew(dc->last_ier, dc->reg_ier);
  		}
  	}
  
  exit_handler:
  	spin_unlock(&dc->spin_mutex);
2e124b4a3   Jiri Slaby   TTY: switch tty_f...
1270
1271
1272
1273
  
  	for (a = 0; a < NOZOMI_MAX_PORTS; a++)
  		if (test_and_clear_bit(a, &dc->flip))
  			tty_flip_buffer_push(&dc->port[a].port);
20fd1e3be   Frank Seidel   nozomi driver
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
  	return IRQ_HANDLED;
  none:
  	spin_unlock(&dc->spin_mutex);
  	return IRQ_NONE;
  }
  
  static void nozomi_get_card_type(struct nozomi *dc)
  {
  	int i;
  	u32 size = 0;
  
  	for (i = 0; i < 6; i++)
  		size += pci_resource_len(dc->pdev, i);
  
  	/* Assume card type F32_8 if no match */
  	dc->card_type = size == 2048 ? F32_2 : F32_8;
  
  	dev_info(&dc->pdev->dev, "Card type is: %d
  ", dc->card_type);
  }
  
  static void nozomi_setup_private_data(struct nozomi *dc)
  {
  	void __iomem *offset = dc->base_addr + dc->card_type / 2;
  	unsigned int i;
  
  	dc->reg_fcr = (void __iomem *)(offset + R_FCR);
  	dc->reg_iir = (void __iomem *)(offset + R_IIR);
  	dc->reg_ier = (void __iomem *)(offset + R_IER);
  	dc->last_ier = 0;
  	dc->flip = 0;
  
  	dc->port[PORT_MDM].token_dl = MDM_DL;
  	dc->port[PORT_DIAG].token_dl = DIAG_DL;
  	dc->port[PORT_APP1].token_dl = APP1_DL;
  	dc->port[PORT_APP2].token_dl = APP2_DL;
  
  	for (i = 0; i < MAX_PORT; i++)
  		init_waitqueue_head(&dc->port[i].tty_wait);
  }
  
  static ssize_t card_type_show(struct device *dev, struct device_attribute *attr,
  			  char *buf)
  {
71e1b4abd   Jan Engelhardt   nozomi: constify ...
1318
  	const struct nozomi *dc = pci_get_drvdata(to_pci_dev(dev));
20fd1e3be   Frank Seidel   nozomi driver
1319
1320
1321
1322
  
  	return sprintf(buf, "%d
  ", dc->card_type);
  }
e9ed537a3   Frank Seidel   nozomi driver update
1323
  static DEVICE_ATTR(card_type, S_IRUGO, card_type_show, NULL);
20fd1e3be   Frank Seidel   nozomi driver
1324
1325
1326
1327
  
  static ssize_t open_ttys_show(struct device *dev, struct device_attribute *attr,
  			  char *buf)
  {
71e1b4abd   Jan Engelhardt   nozomi: constify ...
1328
  	const struct nozomi *dc = pci_get_drvdata(to_pci_dev(dev));
20fd1e3be   Frank Seidel   nozomi driver
1329
1330
1331
1332
  
  	return sprintf(buf, "%u
  ", dc->open_ttys);
  }
e9ed537a3   Frank Seidel   nozomi driver update
1333
  static DEVICE_ATTR(open_ttys, S_IRUGO, open_ttys_show, NULL);
20fd1e3be   Frank Seidel   nozomi driver
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
  
  static void make_sysfs_files(struct nozomi *dc)
  {
  	if (device_create_file(&dc->pdev->dev, &dev_attr_card_type))
  		dev_err(&dc->pdev->dev,
  			"Could not create sysfs file for card_type
  ");
  	if (device_create_file(&dc->pdev->dev, &dev_attr_open_ttys))
  		dev_err(&dc->pdev->dev,
  			"Could not create sysfs file for open_ttys
  ");
  }
  
  static void remove_sysfs_files(struct nozomi *dc)
  {
  	device_remove_file(&dc->pdev->dev, &dev_attr_card_type);
  	device_remove_file(&dc->pdev->dev, &dev_attr_open_ttys);
  }
  
  /* Allocate memory for one device */
9671f0992   Bill Pemberton   tty: remove use o...
1354
  static int nozomi_card_init(struct pci_dev *pdev,
20fd1e3be   Frank Seidel   nozomi driver
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
  				      const struct pci_device_id *ent)
  {
  	resource_size_t start;
  	int ret;
  	struct nozomi *dc = NULL;
  	int ndev_idx;
  	int i;
  
  	dev_dbg(&pdev->dev, "Init, new card found
  ");
  
  	for (ndev_idx = 0; ndev_idx < ARRAY_SIZE(ndevs); ndev_idx++)
  		if (!ndevs[ndev_idx])
  			break;
  
  	if (ndev_idx >= ARRAY_SIZE(ndevs)) {
  		dev_err(&pdev->dev, "no free tty range for this card left
  ");
  		ret = -EIO;
  		goto err;
  	}
  
  	dc = kzalloc(sizeof(struct nozomi), GFP_KERNEL);
  	if (unlikely(!dc)) {
  		dev_err(&pdev->dev, "Could not allocate memory
  ");
  		ret = -ENOMEM;
  		goto err_free;
  	}
  
  	dc->pdev = pdev;
20fd1e3be   Frank Seidel   nozomi driver
1386
1387
1388
1389
1390
1391
  	ret = pci_enable_device(dc->pdev);
  	if (ret) {
  		dev_err(&pdev->dev, "Failed to enable PCI Device
  ");
  		goto err_free;
  	}
20fd1e3be   Frank Seidel   nozomi driver
1392
1393
1394
1395
1396
1397
1398
  	ret = pci_request_regions(dc->pdev, NOZOMI_NAME);
  	if (ret) {
  		dev_err(&pdev->dev, "I/O address 0x%04x already in use
  ",
  			(int) /* nozomi_private.io_addr */ 0);
  		goto err_disable_device;
  	}
661b4e89d   Frank Seidel   nozomi: fix initi...
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
  	start = pci_resource_start(dc->pdev, 0);
  	if (start == 0) {
  		dev_err(&pdev->dev, "No I/O address for card detected
  ");
  		ret = -ENODEV;
  		goto err_rel_regs;
  	}
  
  	/* Find out what card type it is */
  	nozomi_get_card_type(dc);
24cb23352   Alan Cox   char serial: swit...
1409
  	dc->base_addr = ioremap_nocache(start, dc->card_type);
20fd1e3be   Frank Seidel   nozomi driver
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
  	if (!dc->base_addr) {
  		dev_err(&pdev->dev, "Unable to map card MMIO
  ");
  		ret = -ENODEV;
  		goto err_rel_regs;
  	}
  
  	dc->send_buf = kmalloc(SEND_BUF_MAX, GFP_KERNEL);
  	if (!dc->send_buf) {
  		dev_err(&pdev->dev, "Could not allocate send buffer?
  ");
  		ret = -ENOMEM;
  		goto err_free_sbuf;
  	}
9842c38e9   Stefani Seibold   kfifo: fix warn_u...
1424
  	for (i = PORT_MDM; i < MAX_PORT; i++) {
c29bd8d89   Jiri Slaby   Char: nozomi, use...
1425
1426
  		if (kfifo_alloc(&dc->port[i].fifo_ul, FIFO_BUFFER_SIZE_UL,
  					GFP_KERNEL)) {
9842c38e9   Stefani Seibold   kfifo: fix warn_u...
1427
1428
1429
1430
1431
1432
1433
  			dev_err(&pdev->dev,
  					"Could not allocate kfifo buffer
  ");
  			ret = -ENOMEM;
  			goto err_free_kfifo;
  		}
  	}
20fd1e3be   Frank Seidel   nozomi driver
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
  	spin_lock_init(&dc->spin_mutex);
  
  	nozomi_setup_private_data(dc);
  
  	/* Disable all interrupts */
  	dc->last_ier = 0;
  	writew(dc->last_ier, dc->reg_ier);
  
  	ret = request_irq(pdev->irq, &interrupt_handler, IRQF_SHARED,
  			NOZOMI_NAME, dc);
  	if (unlikely(ret)) {
  		dev_err(&pdev->dev, "can't request irq %d
  ", pdev->irq);
9842c38e9   Stefani Seibold   kfifo: fix warn_u...
1447
  		goto err_free_kfifo;
20fd1e3be   Frank Seidel   nozomi driver
1448
1449
1450
1451
1452
1453
1454
1455
  	}
  
  	DBG1("base_addr: %p", dc->base_addr);
  
  	make_sysfs_files(dc);
  
  	dc->index_start = ndev_idx * MAX_PORT;
  	ndevs[ndev_idx] = dc;
661b4e89d   Frank Seidel   nozomi: fix initi...
1456
1457
1458
1459
1460
1461
1462
  	pci_set_drvdata(pdev, dc);
  
  	/* Enable RESET interrupt */
  	dc->last_ier = RESET;
  	iowrite16(dc->last_ier, dc->reg_ier);
  
  	dc->state = NOZOMI_STATE_ENABLED;
20fd1e3be   Frank Seidel   nozomi driver
1463
  	for (i = 0; i < MAX_PORT; i++) {
9842c38e9   Stefani Seibold   kfifo: fix warn_u...
1464
  		struct device *tty_dev;
266794eb7   Alan Cox   nozomi: Add tty_p...
1465
1466
  		struct port *port = &dc->port[i];
  		port->dc = dc;
266794eb7   Alan Cox   nozomi: Add tty_p...
1467
1468
  		tty_port_init(&port->port);
  		port->port.ops = &noz_tty_port_ops;
734cc1783   Jiri Slaby   TTY: use tty_port...
1469
1470
  		tty_dev = tty_port_register_device(&port->port, ntty_driver,
  				dc->index_start + i, &pdev->dev);
9842c38e9   Stefani Seibold   kfifo: fix warn_u...
1471
1472
1473
1474
1475
  
  		if (IS_ERR(tty_dev)) {
  			ret = PTR_ERR(tty_dev);
  			dev_err(&pdev->dev, "Could not allocate tty?
  ");
191c5f102   Jiri Slaby   TTY: call tty_por...
1476
  			tty_port_destroy(&port->port);
9842c38e9   Stefani Seibold   kfifo: fix warn_u...
1477
1478
  			goto err_free_tty;
  		}
20fd1e3be   Frank Seidel   nozomi driver
1479
  	}
9842c38e9   Stefani Seibold   kfifo: fix warn_u...
1480

20fd1e3be   Frank Seidel   nozomi driver
1481
  	return 0;
9842c38e9   Stefani Seibold   kfifo: fix warn_u...
1482
  err_free_tty:
191c5f102   Jiri Slaby   TTY: call tty_por...
1483
1484
1485
1486
  	for (i = 0; i < MAX_PORT; ++i) {
  		tty_unregister_device(ntty_driver, dc->index_start + i);
  		tty_port_destroy(&dc->port[i].port);
  	}
9842c38e9   Stefani Seibold   kfifo: fix warn_u...
1487
1488
1489
  err_free_kfifo:
  	for (i = 0; i < MAX_PORT; i++)
  		kfifo_free(&dc->port[i].fifo_ul);
20fd1e3be   Frank Seidel   nozomi driver
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
  err_free_sbuf:
  	kfree(dc->send_buf);
  	iounmap(dc->base_addr);
  err_rel_regs:
  	pci_release_regions(pdev);
  err_disable_device:
  	pci_disable_device(pdev);
  err_free:
  	kfree(dc);
  err:
  	return ret;
  }
ae8d8a146   Bill Pemberton   tty: remove use o...
1502
  static void tty_exit(struct nozomi *dc)
20fd1e3be   Frank Seidel   nozomi driver
1503
1504
1505
1506
  {
  	unsigned int i;
  
  	DBG1(" ");
aa27a094e   Jiri Slaby   TTY: add tty_port...
1507
1508
  	for (i = 0; i < MAX_PORT; ++i)
  		tty_port_tty_hangup(&dc->port[i].port, false);
33dd474ae   Alan Cox   tty: kref nozomi
1509
1510
  	/* Racy below - surely should wait for scheduled work to be done or
  	   complete off a hangup method ? */
20fd1e3be   Frank Seidel   nozomi driver
1511
1512
  	while (dc->open_ttys)
  		msleep(1);
191c5f102   Jiri Slaby   TTY: call tty_por...
1513
1514
1515
1516
  	for (i = 0; i < MAX_PORT; ++i) {
  		tty_unregister_device(ntty_driver, dc->index_start + i);
  		tty_port_destroy(&dc->port[i].port);
  	}
20fd1e3be   Frank Seidel   nozomi driver
1517
1518
1519
  }
  
  /* Deallocate memory for one device */
ae8d8a146   Bill Pemberton   tty: remove use o...
1520
  static void nozomi_card_exit(struct pci_dev *pdev)
20fd1e3be   Frank Seidel   nozomi driver
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
  {
  	int i;
  	struct ctrl_ul ctrl;
  	struct nozomi *dc = pci_get_drvdata(pdev);
  
  	/* Disable all interrupts */
  	dc->last_ier = 0;
  	writew(dc->last_ier, dc->reg_ier);
  
  	tty_exit(dc);
  
  	/* Send 0x0001, command card to resend the reset token.  */
  	/* This is to get the reset when the module is reloaded. */
  	ctrl.port = 0x00;
  	ctrl.reserved = 0;
  	ctrl.RTS = 0;
  	ctrl.DTR = 1;
  	DBG1("sending flow control 0x%04X", *((u16 *)&ctrl));
  
  	/* Setup dc->reg addresses to we can use defines here */
  	write_mem32(dc->port[PORT_CTRL].ul_addr[0], (u32 *)&ctrl, 2);
  	writew(CTRL_UL, dc->reg_fcr);	/* push the token to the card. */
  
  	remove_sysfs_files(dc);
  
  	free_irq(pdev->irq, dc);
  
  	for (i = 0; i < MAX_PORT; i++)
454654878   Stefani Seibold   kfifo: move struc...
1549
  		kfifo_free(&dc->port[i].fifo_ul);
20fd1e3be   Frank Seidel   nozomi driver
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
  
  	kfree(dc->send_buf);
  
  	iounmap(dc->base_addr);
  
  	pci_release_regions(pdev);
  
  	pci_disable_device(pdev);
  
  	ndevs[dc->index_start / MAX_PORT] = NULL;
  
  	kfree(dc);
  }
  
  static void set_rts(const struct tty_struct *tty, int rts)
  {
  	struct port *port = get_port_by_tty(tty);
  
  	port->ctrl_ul.RTS = rts;
  	port->update_flow_control = 1;
  	enable_transmit_ul(PORT_CTRL, get_dc_by_tty(tty));
  }
  
  static void set_dtr(const struct tty_struct *tty, int dtr)
  {
  	struct port *port = get_port_by_tty(tty);
  
  	DBG1("SETTING DTR index: %d, dtr: %d", tty->index, dtr);
  
  	port->ctrl_ul.DTR = dtr;
  	port->update_flow_control = 1;
  	enable_transmit_ul(PORT_CTRL, get_dc_by_tty(tty));
  }
  
  /*
   * ----------------------------------------------------------------------------
   * TTY code
   * ----------------------------------------------------------------------------
   */
266794eb7   Alan Cox   nozomi: Add tty_p...
1589
  static int ntty_install(struct tty_driver *driver, struct tty_struct *tty)
20fd1e3be   Frank Seidel   nozomi driver
1590
1591
1592
  {
  	struct port *port = get_port_by_tty(tty);
  	struct nozomi *dc = get_dc_by_tty(tty);
266794eb7   Alan Cox   nozomi: Add tty_p...
1593
  	int ret;
661b4e89d   Frank Seidel   nozomi: fix initi...
1594
  	if (!port || !dc || dc->state != NOZOMI_STATE_READY)
20fd1e3be   Frank Seidel   nozomi driver
1595
  		return -ENODEV;
81f5835ea   Jiri Slaby   TTY: use tty_stan...
1596
1597
  	ret = tty_standard_install(driver, tty);
  	if (ret == 0)
bf9c1fca9   Jiri Slaby   Char: nozomi, set...
1598
  		tty->driver_data = port;
266794eb7   Alan Cox   nozomi: Add tty_p...
1599
  	return ret;
20fd1e3be   Frank Seidel   nozomi driver
1600
  }
266794eb7   Alan Cox   nozomi: Add tty_p...
1601
1602
1603
1604
  static void ntty_cleanup(struct tty_struct *tty)
  {
  	tty->driver_data = NULL;
  }
716da631a   Alan Cox   nozomi: Fix close...
1605

266794eb7   Alan Cox   nozomi: Add tty_p...
1606
  static int ntty_activate(struct tty_port *tport, struct tty_struct *tty)
20fd1e3be   Frank Seidel   nozomi driver
1607
  {
266794eb7   Alan Cox   nozomi: Add tty_p...
1608
1609
  	struct port *port = container_of(tport, struct port, port);
  	struct nozomi *dc = port->dc;
20fd1e3be   Frank Seidel   nozomi driver
1610
  	unsigned long flags;
266794eb7   Alan Cox   nozomi: Add tty_p...
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
  	DBG1("open: %d", port->token_dl);
  	spin_lock_irqsave(&dc->spin_mutex, flags);
  	dc->last_ier = dc->last_ier | port->token_dl;
  	writew(dc->last_ier, dc->reg_ier);
  	dc->open_ttys++;
  	spin_unlock_irqrestore(&dc->spin_mutex, flags);
  	printk("noz: activated %d: %p
  ", tty->index, tport);
  	return 0;
  }
20fd1e3be   Frank Seidel   nozomi driver
1621

266794eb7   Alan Cox   nozomi: Add tty_p...
1622
1623
  static int ntty_open(struct tty_struct *tty, struct file *filp)
  {
bf9c1fca9   Jiri Slaby   Char: nozomi, set...
1624
  	struct port *port = tty->driver_data;
266794eb7   Alan Cox   nozomi: Add tty_p...
1625
1626
  	return tty_port_open(&port->port, tty, filp);
  }
20fd1e3be   Frank Seidel   nozomi driver
1627

266794eb7   Alan Cox   nozomi: Add tty_p...
1628
1629
1630
1631
1632
  static void ntty_shutdown(struct tty_port *tport)
  {
  	struct port *port = container_of(tport, struct port, port);
  	struct nozomi *dc = port->dc;
  	unsigned long flags;
20fd1e3be   Frank Seidel   nozomi driver
1633

266794eb7   Alan Cox   nozomi: Add tty_p...
1634
1635
1636
1637
  	DBG1("close: %d", port->token_dl);
  	spin_lock_irqsave(&dc->spin_mutex, flags);
  	dc->last_ier &= ~(port->token_dl);
  	writew(dc->last_ier, dc->reg_ier);
20fd1e3be   Frank Seidel   nozomi driver
1638
  	dc->open_ttys--;
266794eb7   Alan Cox   nozomi: Add tty_p...
1639
1640
1641
1642
  	spin_unlock_irqrestore(&dc->spin_mutex, flags);
  	printk("noz: shutdown %p
  ", tport);
  }
20fd1e3be   Frank Seidel   nozomi driver
1643

266794eb7   Alan Cox   nozomi: Add tty_p...
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
  static void ntty_close(struct tty_struct *tty, struct file *filp)
  {
  	struct port *port = tty->driver_data;
  	if (port)
  		tty_port_close(&port->port, tty, filp);
  }
  
  static void ntty_hangup(struct tty_struct *tty)
  {
  	struct port *port = tty->driver_data;
  	tty_port_hangup(&port->port);
20fd1e3be   Frank Seidel   nozomi driver
1655
1656
1657
1658
  }
  
  /*
   * called when the userspace process writes to the tty (/dev/noz*).
25985edce   Lucas De Marchi   Fix common misspe...
1659
   * Data is inserted into a fifo, which is then read and transferred to the modem.
20fd1e3be   Frank Seidel   nozomi driver
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
   */
  static int ntty_write(struct tty_struct *tty, const unsigned char *buffer,
  		      int count)
  {
  	int rval = -EINVAL;
  	struct nozomi *dc = get_dc_by_tty(tty);
  	struct port *port = tty->driver_data;
  	unsigned long flags;
  
  	/* DBG1( "WRITEx: %d, index = %d", count, index); */
  
  	if (!dc || !port)
  		return -ENODEV;
7acd72eb8   Stefani Seibold   kfifo: rename kfi...
1673
  	rval = kfifo_in(&port->fifo_ul, (unsigned char *)buffer, count);
20fd1e3be   Frank Seidel   nozomi driver
1674

20fd1e3be   Frank Seidel   nozomi driver
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
  	spin_lock_irqsave(&dc->spin_mutex, flags);
  	/* CTS is only valid on the modem channel */
  	if (port == &(dc->port[PORT_MDM])) {
  		if (port->ctrl_dl.CTS) {
  			DBG4("Enable interrupt");
  			enable_transmit_ul(tty->index % MAX_PORT, dc);
  		} else {
  			dev_err(&dc->pdev->dev,
  				"CTS not active on modem port?
  ");
  		}
  	} else {
  		enable_transmit_ul(tty->index % MAX_PORT, dc);
  	}
  	spin_unlock_irqrestore(&dc->spin_mutex, flags);
20fd1e3be   Frank Seidel   nozomi driver
1690
1691
1692
1693
1694
1695
1696
1697
  	return rval;
  }
  
  /*
   * Calculate how much is left in device
   * This method is called by the upper tty layer.
   *   #according to sources N_TTY.c it expects a value >= 0 and
   *    does not check for negative values.
e8c65d143   Alan Cox   nozomi: Fix mutex...
1698
1699
1700
   *
   * If the port is unplugged report lots of room and let the bits
   * dribble away so we don't block anything.
20fd1e3be   Frank Seidel   nozomi driver
1701
1702
1703
1704
   */
  static int ntty_write_room(struct tty_struct *tty)
  {
  	struct port *port = tty->driver_data;
e8c65d143   Alan Cox   nozomi: Fix mutex...
1705
  	int room = 4096;
71e1b4abd   Jan Engelhardt   nozomi: constify ...
1706
  	const struct nozomi *dc = get_dc_by_tty(tty);
20fd1e3be   Frank Seidel   nozomi driver
1707

7fdc28931   Jiri Slaby   Char: nozomi, rem...
1708
  	if (dc)
6d742f655   Jiri Slaby   Char: nozomi, rem...
1709
  		room = kfifo_avail(&port->fifo_ul);
7fdc28931   Jiri Slaby   Char: nozomi, rem...
1710

20fd1e3be   Frank Seidel   nozomi driver
1711
1712
1713
1714
  	return room;
  }
  
  /* Gets io control parameters */
60b33c133   Alan Cox   tiocmget: kill of...
1715
  static int ntty_tiocmget(struct tty_struct *tty)
20fd1e3be   Frank Seidel   nozomi driver
1716
  {
71e1b4abd   Jan Engelhardt   nozomi: constify ...
1717
1718
1719
  	const struct port *port = tty->driver_data;
  	const struct ctrl_dl *ctrl_dl = &port->ctrl_dl;
  	const struct ctrl_ul *ctrl_ul = &port->ctrl_ul;
20fd1e3be   Frank Seidel   nozomi driver
1720

978e595f8   Alan Cox   tty/serial: lay t...
1721
1722
  	/* Note: these could change under us but it is not clear this
  	   matters if so */
20fd1e3be   Frank Seidel   nozomi driver
1723
1724
1725
1726
1727
1728
1729
1730
1731
  	return	(ctrl_ul->RTS ? TIOCM_RTS : 0) |
  		(ctrl_ul->DTR ? TIOCM_DTR : 0) |
  		(ctrl_dl->DCD ? TIOCM_CAR : 0) |
  		(ctrl_dl->RI  ? TIOCM_RNG : 0) |
  		(ctrl_dl->DSR ? TIOCM_DSR : 0) |
  		(ctrl_dl->CTS ? TIOCM_CTS : 0);
  }
  
  /* Sets io controls parameters */
20b9d1771   Alan Cox   tiocmset: kill th...
1732
1733
  static int ntty_tiocmset(struct tty_struct *tty,
  					unsigned int set, unsigned int clear)
20fd1e3be   Frank Seidel   nozomi driver
1734
  {
661b4e89d   Frank Seidel   nozomi: fix initi...
1735
1736
1737
1738
  	struct nozomi *dc = get_dc_by_tty(tty);
  	unsigned long flags;
  
  	spin_lock_irqsave(&dc->spin_mutex, flags);
20fd1e3be   Frank Seidel   nozomi driver
1739
1740
1741
1742
1743
1744
1745
1746
1747
  	if (set & TIOCM_RTS)
  		set_rts(tty, 1);
  	else if (clear & TIOCM_RTS)
  		set_rts(tty, 0);
  
  	if (set & TIOCM_DTR)
  		set_dtr(tty, 1);
  	else if (clear & TIOCM_DTR)
  		set_dtr(tty, 0);
661b4e89d   Frank Seidel   nozomi: fix initi...
1748
  	spin_unlock_irqrestore(&dc->spin_mutex, flags);
20fd1e3be   Frank Seidel   nozomi driver
1749
1750
1751
1752
1753
1754
1755
  
  	return 0;
  }
  
  static int ntty_cflags_changed(struct port *port, unsigned long flags,
  		struct async_icount *cprev)
  {
18bbe0c26   Frank Seidel   nozomi: finish co...
1756
  	const struct async_icount cnow = port->tty_icount;
20fd1e3be   Frank Seidel   nozomi driver
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
  	int ret;
  
  	ret =	((flags & TIOCM_RNG) && (cnow.rng != cprev->rng)) ||
  		((flags & TIOCM_DSR) && (cnow.dsr != cprev->dsr)) ||
  		((flags & TIOCM_CD)  && (cnow.dcd != cprev->dcd)) ||
  		((flags & TIOCM_CTS) && (cnow.cts != cprev->cts));
  
  	*cprev = cnow;
  
  	return ret;
  }
0587102cf   Alan Cox   tty: icount chang...
1768
1769
  static int ntty_tiocgicount(struct tty_struct *tty,
  				struct serial_icounter_struct *icount)
20fd1e3be   Frank Seidel   nozomi driver
1770
  {
0587102cf   Alan Cox   tty: icount chang...
1771
  	struct port *port = tty->driver_data;
18bbe0c26   Frank Seidel   nozomi: finish co...
1772
  	const struct async_icount cnow = port->tty_icount;
0587102cf   Alan Cox   tty: icount chang...
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
  
  	icount->cts = cnow.cts;
  	icount->dsr = cnow.dsr;
  	icount->rng = cnow.rng;
  	icount->dcd = cnow.dcd;
  	icount->rx = cnow.rx;
  	icount->tx = cnow.tx;
  	icount->frame = cnow.frame;
  	icount->overrun = cnow.overrun;
  	icount->parity = cnow.parity;
  	icount->brk = cnow.brk;
  	icount->buf_overrun = cnow.buf_overrun;
  	return 0;
20fd1e3be   Frank Seidel   nozomi driver
1786
  }
6caa76b77   Alan Cox   tty: now phase ou...
1787
  static int ntty_ioctl(struct tty_struct *tty,
20fd1e3be   Frank Seidel   nozomi driver
1788
1789
1790
  		      unsigned int cmd, unsigned long arg)
  {
  	struct port *port = tty->driver_data;
20fd1e3be   Frank Seidel   nozomi driver
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
  	int rval = -ENOIOCTLCMD;
  
  	DBG1("******** IOCTL, cmd: %d", cmd);
  
  	switch (cmd) {
  	case TIOCMIWAIT: {
  		struct async_icount cprev = port->tty_icount;
  
  		rval = wait_event_interruptible(port->tty_wait,
  				ntty_cflags_changed(port, arg, &cprev));
  		break;
0587102cf   Alan Cox   tty: icount chang...
1802
  	}
20fd1e3be   Frank Seidel   nozomi driver
1803
1804
1805
  	default:
  		DBG1("ERR: 0x%08X, %d", cmd, cmd);
  		break;
fc811472c   Joe Perches   tty: Remove unnec...
1806
  	}
20fd1e3be   Frank Seidel   nozomi driver
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
  
  	return rval;
  }
  
  /*
   * Called by the upper tty layer when tty buffers are ready
   * to receive data again after a call to throttle.
   */
  static void ntty_unthrottle(struct tty_struct *tty)
  {
  	struct nozomi *dc = get_dc_by_tty(tty);
  	unsigned long flags;
  
  	DBG1("UNTHROTTLE");
  	spin_lock_irqsave(&dc->spin_mutex, flags);
  	enable_transmit_dl(tty->index % MAX_PORT, dc);
  	set_rts(tty, 1);
  
  	spin_unlock_irqrestore(&dc->spin_mutex, flags);
  }
  
  /*
   * Called by the upper tty layer when the tty buffers are almost full.
   * The driver should stop send more data.
   */
  static void ntty_throttle(struct tty_struct *tty)
  {
  	struct nozomi *dc = get_dc_by_tty(tty);
  	unsigned long flags;
  
  	DBG1("THROTTLE");
  	spin_lock_irqsave(&dc->spin_mutex, flags);
  	set_rts(tty, 0);
  	spin_unlock_irqrestore(&dc->spin_mutex, flags);
  }
20fd1e3be   Frank Seidel   nozomi driver
1842
1843
1844
1845
1846
  /* Returns number of chars in buffer, called by tty layer */
  static s32 ntty_chars_in_buffer(struct tty_struct *tty)
  {
  	struct port *port = tty->driver_data;
  	struct nozomi *dc = get_dc_by_tty(tty);
23198fda7   Alan Cox   tty: fix chars_in...
1847
  	s32 rval = 0;
20fd1e3be   Frank Seidel   nozomi driver
1848
1849
  
  	if (unlikely(!dc || !port)) {
20fd1e3be   Frank Seidel   nozomi driver
1850
1851
  		goto exit_in_buffer;
  	}
e64c026dd   Stefani Seibold   kfifo: cleanup na...
1852
  	rval = kfifo_len(&port->fifo_ul);
20fd1e3be   Frank Seidel   nozomi driver
1853
1854
1855
1856
  
  exit_in_buffer:
  	return rval;
  }
266794eb7   Alan Cox   nozomi: Add tty_p...
1857
1858
1859
1860
  static const struct tty_port_operations noz_tty_port_ops = {
  	.activate = ntty_activate,
  	.shutdown = ntty_shutdown,
  };
18bbe0c26   Frank Seidel   nozomi: finish co...
1861
  static const struct tty_operations tty_ops = {
20fd1e3be   Frank Seidel   nozomi driver
1862
1863
1864
  	.ioctl = ntty_ioctl,
  	.open = ntty_open,
  	.close = ntty_close,
266794eb7   Alan Cox   nozomi: Add tty_p...
1865
  	.hangup = ntty_hangup,
20fd1e3be   Frank Seidel   nozomi driver
1866
1867
1868
1869
1870
  	.write = ntty_write,
  	.write_room = ntty_write_room,
  	.unthrottle = ntty_unthrottle,
  	.throttle = ntty_throttle,
  	.chars_in_buffer = ntty_chars_in_buffer,
20fd1e3be   Frank Seidel   nozomi driver
1871
1872
  	.tiocmget = ntty_tiocmget,
  	.tiocmset = ntty_tiocmset,
0587102cf   Alan Cox   tty: icount chang...
1873
  	.get_icount = ntty_tiocgicount,
266794eb7   Alan Cox   nozomi: Add tty_p...
1874
1875
  	.install = ntty_install,
  	.cleanup = ntty_cleanup,
20fd1e3be   Frank Seidel   nozomi driver
1876
1877
1878
1879
1880
1881
1882
  };
  
  /* Module initialization */
  static struct pci_driver nozomi_driver = {
  	.name = NOZOMI_NAME,
  	.id_table = nozomi_pci_tbl,
  	.probe = nozomi_card_init,
91116cba5   Bill Pemberton   tty: remove use o...
1883
  	.remove = nozomi_card_exit,
20fd1e3be   Frank Seidel   nozomi driver
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
  };
  
  static __init int nozomi_init(void)
  {
  	int ret;
  
  	printk(KERN_INFO "Initializing %s
  ", VERSION_STRING);
  
  	ntty_driver = alloc_tty_driver(NTTY_TTY_MAXMINORS);
  	if (!ntty_driver)
  		return -ENOMEM;
20fd1e3be   Frank Seidel   nozomi driver
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
  	ntty_driver->driver_name = NOZOMI_NAME_TTY;
  	ntty_driver->name = "noz";
  	ntty_driver->major = 0;
  	ntty_driver->type = TTY_DRIVER_TYPE_SERIAL;
  	ntty_driver->subtype = SERIAL_TYPE_NORMAL;
  	ntty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
  	ntty_driver->init_termios = tty_std_termios;
  	ntty_driver->init_termios.c_cflag = B115200 | CS8 | CREAD | \
  						HUPCL | CLOCAL;
  	ntty_driver->init_termios.c_ispeed = 115200;
  	ntty_driver->init_termios.c_ospeed = 115200;
  	tty_set_operations(ntty_driver, &tty_ops);
  
  	ret = tty_register_driver(ntty_driver);
  	if (ret) {
  		printk(KERN_ERR "Nozomi: failed to register ntty driver
  ");
  		goto free_tty;
  	}
  
  	ret = pci_register_driver(&nozomi_driver);
  	if (ret) {
  		printk(KERN_ERR "Nozomi: can't register pci driver
  ");
  		goto unr_tty;
  	}
  
  	return 0;
  unr_tty:
  	tty_unregister_driver(ntty_driver);
  free_tty:
  	put_tty_driver(ntty_driver);
  	return ret;
  }
  
  static __exit void nozomi_exit(void)
  {
  	printk(KERN_INFO "Unloading %s
  ", DRIVER_DESC);
  	pci_unregister_driver(&nozomi_driver);
  	tty_unregister_driver(ntty_driver);
  	put_tty_driver(ntty_driver);
  }
  
  module_init(nozomi_init);
  module_exit(nozomi_exit);
  
  module_param(debug, int, S_IRUGO | S_IWUSR);
  
  MODULE_LICENSE("Dual BSD/GPL");
  MODULE_DESCRIPTION(DRIVER_DESC);