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