blob: 2ad7d37afbd080306d849d8a4558daca888f2e9b [file] [log] [blame]
Frank Seidel20fd1e32007-11-09 14:49:23 +01001/*
2 * nozomi.c -- HSDPA driver Broadband Wireless Data Card - Globe Trotter
3 *
4 * Written by: Ulf Jakobsson,
Frank Seidele9ed5372008-01-25 21:13:24 +01005 * Jan Ã…kerfeldt,
Frank Seidel20fd1e32007-11-09 14:49:23 +01006 * 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
Frank Seidel20fd1e32007-11-09 14:49:23 +010041/* 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>
Alexey Dobriyand43c36d2009-10-07 17:09:06 +040051#include <linux/sched.h>
Frank Seidel20fd1e32007-11-09 14:49:23 +010052#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>
58#include <asm/byteorder.h>
59
60#include <linux/delay.h>
61
62
63#define VERSION_STRING DRIVER_DESC " 2.1d (build date: " \
64 __DATE__ " " __TIME__ ")"
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__, \
Harvey Harrisonbf9d8922008-04-30 00:55:10 -070077 __func__, tmp); \
Frank Seidel20fd1e32007-11-09 14:49:23 +010078} 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
Frank Seidele9ed5372008-01-25 21:13:24 +010093#define D(lvl, args...) do \
94 {if (lvl & debug) NFO(KERN_DEBUG, ##args); } \
95 while (0)
Frank Seidel20fd1e32007-11-09 14:49:23 +010096#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
139/* Define all types of vendors and devices to support */
140#define VENDOR1 0x1931 /* Vendor Option */
141#define DEVICE1 0x000c /* HSDPA card */
142
143#define R_IIR 0x0000 /* Interrupt Identity Register */
144#define R_FCR 0x0000 /* Flow Control Register */
145#define R_IER 0x0004 /* Interrupt Enable Register */
146
147#define CONFIG_MAGIC 0xEFEFFEFE
148#define TOGGLE_VALID 0x0000
149
150/* Definition of interrupt tokens */
151#define MDM_DL1 0x0001
152#define MDM_UL1 0x0002
153#define MDM_DL2 0x0004
154#define MDM_UL2 0x0008
155#define DIAG_DL1 0x0010
156#define DIAG_DL2 0x0020
157#define DIAG_UL 0x0040
158#define APP1_DL 0x0080
159#define APP1_UL 0x0100
160#define APP2_DL 0x0200
161#define APP2_UL 0x0400
162#define CTRL_DL 0x0800
163#define CTRL_UL 0x1000
164#define RESET 0x8000
165
166#define MDM_DL (MDM_DL1 | MDM_DL2)
167#define MDM_UL (MDM_UL1 | MDM_UL2)
168#define DIAG_DL (DIAG_DL1 | DIAG_DL2)
169
170/* modem signal definition */
171#define CTRL_DSR 0x0001
172#define CTRL_DCD 0x0002
173#define CTRL_RI 0x0004
174#define CTRL_CTS 0x0008
175
176#define CTRL_DTR 0x0001
177#define CTRL_RTS 0x0002
178
179#define MAX_PORT 4
180#define NOZOMI_MAX_PORTS 5
181#define NOZOMI_MAX_CARDS (NTTY_TTY_MAXMINORS / MAX_PORT)
182
183/* Type definitions */
184
185/*
186 * There are two types of nozomi cards,
187 * one with 2048 memory and with 8192 memory
188 */
189enum card_type {
190 F32_2 = 2048, /* 512 bytes downlink + uplink * 2 -> 2048 */
191 F32_8 = 8192, /* 3072 bytes downl. + 1024 bytes uplink * 2 -> 8192 */
192};
193
Frank Seidel661b4e82008-03-06 21:45:57 +0100194/* Initialization states a card can be in */
195enum card_state {
196 NOZOMI_STATE_UKNOWN = 0,
197 NOZOMI_STATE_ENABLED = 1, /* pci device enabled */
198 NOZOMI_STATE_ALLOCATED = 2, /* config setup done */
199 NOZOMI_STATE_READY = 3, /* flowcontrols received */
200};
201
Frank Seidel20fd1e32007-11-09 14:49:23 +0100202/* Two different toggle channels exist */
203enum channel_type {
204 CH_A = 0,
205 CH_B = 1,
206};
207
208/* Port definition for the card regarding flow control */
209enum ctrl_port_type {
210 CTRL_CMD = 0,
211 CTRL_MDM = 1,
212 CTRL_DIAG = 2,
213 CTRL_APP1 = 3,
214 CTRL_APP2 = 4,
215 CTRL_ERROR = -1,
216};
217
218/* Ports that the nozomi has */
219enum port_type {
220 PORT_MDM = 0,
221 PORT_DIAG = 1,
222 PORT_APP1 = 2,
223 PORT_APP2 = 3,
224 PORT_CTRL = 4,
225 PORT_ERROR = -1,
226};
227
228#ifdef __BIG_ENDIAN
229/* Big endian */
230
231struct toggles {
Frank Seidele9ed5372008-01-25 21:13:24 +0100232 unsigned int enabled:5; /*
Frank Seidel20fd1e32007-11-09 14:49:23 +0100233 * Toggle fields are valid if enabled is 0,
234 * else A-channels must always be used.
235 */
Frank Seidele9ed5372008-01-25 21:13:24 +0100236 unsigned int diag_dl:1;
237 unsigned int mdm_dl:1;
238 unsigned int mdm_ul:1;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100239} __attribute__ ((packed));
240
241/* Configuration table to read at startup of card */
242/* Is for now only needed during initialization phase */
243struct config_table {
244 u32 signature;
245 u16 product_information;
246 u16 version;
247 u8 pad3[3];
248 struct toggles toggle;
249 u8 pad1[4];
250 u16 dl_mdm_len1; /*
251 * If this is 64, it can hold
252 * 60 bytes + 4 that is length field
253 */
254 u16 dl_start;
255
256 u16 dl_diag_len1;
257 u16 dl_mdm_len2; /*
258 * If this is 64, it can hold
259 * 60 bytes + 4 that is length field
260 */
261 u16 dl_app1_len;
262
263 u16 dl_diag_len2;
264 u16 dl_ctrl_len;
265 u16 dl_app2_len;
266 u8 pad2[16];
267 u16 ul_mdm_len1;
268 u16 ul_start;
269 u16 ul_diag_len;
270 u16 ul_mdm_len2;
271 u16 ul_app1_len;
272 u16 ul_app2_len;
273 u16 ul_ctrl_len;
274} __attribute__ ((packed));
275
276/* This stores all control downlink flags */
277struct ctrl_dl {
278 u8 port;
Frank Seidele9ed5372008-01-25 21:13:24 +0100279 unsigned int reserved:4;
280 unsigned int CTS:1;
281 unsigned int RI:1;
282 unsigned int DCD:1;
283 unsigned int DSR:1;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100284} __attribute__ ((packed));
285
286/* This stores all control uplink flags */
287struct ctrl_ul {
288 u8 port;
Frank Seidele9ed5372008-01-25 21:13:24 +0100289 unsigned int reserved:6;
290 unsigned int RTS:1;
291 unsigned int DTR:1;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100292} __attribute__ ((packed));
293
294#else
295/* Little endian */
296
297/* This represents the toggle information */
298struct toggles {
Frank Seidele9ed5372008-01-25 21:13:24 +0100299 unsigned int mdm_ul:1;
300 unsigned int mdm_dl:1;
301 unsigned int diag_dl:1;
302 unsigned int enabled:5; /*
Frank Seidel20fd1e32007-11-09 14:49:23 +0100303 * Toggle fields are valid if enabled is 0,
304 * else A-channels must always be used.
305 */
306} __attribute__ ((packed));
307
308/* Configuration table to read at startup of card */
309struct config_table {
310 u32 signature;
311 u16 version;
312 u16 product_information;
313 struct toggles toggle;
314 u8 pad1[7];
315 u16 dl_start;
316 u16 dl_mdm_len1; /*
317 * If this is 64, it can hold
318 * 60 bytes + 4 that is length field
319 */
320 u16 dl_mdm_len2;
321 u16 dl_diag_len1;
322 u16 dl_diag_len2;
323 u16 dl_app1_len;
324 u16 dl_app2_len;
325 u16 dl_ctrl_len;
326 u8 pad2[16];
327 u16 ul_start;
328 u16 ul_mdm_len2;
329 u16 ul_mdm_len1;
330 u16 ul_diag_len;
331 u16 ul_app1_len;
332 u16 ul_app2_len;
333 u16 ul_ctrl_len;
334} __attribute__ ((packed));
335
336/* This stores all control downlink flags */
337struct ctrl_dl {
Frank Seidele9ed5372008-01-25 21:13:24 +0100338 unsigned int DSR:1;
339 unsigned int DCD:1;
340 unsigned int RI:1;
341 unsigned int CTS:1;
342 unsigned int reserverd:4;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100343 u8 port;
344} __attribute__ ((packed));
345
346/* This stores all control uplink flags */
347struct ctrl_ul {
Frank Seidele9ed5372008-01-25 21:13:24 +0100348 unsigned int DTR:1;
349 unsigned int RTS:1;
350 unsigned int reserved:6;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100351 u8 port;
352} __attribute__ ((packed));
353#endif
354
355/* This holds all information that is needed regarding a port */
356struct port {
Alan Cox33dd4742009-01-02 13:47:32 +0000357 struct tty_port port;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100358 u8 update_flow_control;
359 struct ctrl_ul ctrl_ul;
360 struct ctrl_dl ctrl_dl;
Stefani Seibold45465482009-12-21 14:37:26 -0800361 struct kfifo fifo_ul;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100362 void __iomem *dl_addr[2];
363 u32 dl_size[2];
364 u8 toggle_dl;
365 void __iomem *ul_addr[2];
366 u32 ul_size[2];
367 u8 toggle_ul;
368 u16 token_dl;
369
Frank Seidel20fd1e32007-11-09 14:49:23 +0100370 /* mutex to ensure one access patch to this port */
371 struct mutex tty_sem;
372 wait_queue_head_t tty_wait;
373 struct async_icount tty_icount;
374};
375
376/* Private data one for each card in the system */
377struct nozomi {
378 void __iomem *base_addr;
379 unsigned long flip;
380
381 /* Pointers to registers */
382 void __iomem *reg_iir;
383 void __iomem *reg_fcr;
384 void __iomem *reg_ier;
385
386 u16 last_ier;
387 enum card_type card_type;
388 struct config_table config_table; /* Configuration table */
389 struct pci_dev *pdev;
390 struct port port[NOZOMI_MAX_PORTS];
391 u8 *send_buf;
392
393 spinlock_t spin_mutex; /* secures access to registers and tty */
394
395 unsigned int index_start;
Frank Seidel661b4e82008-03-06 21:45:57 +0100396 enum card_state state;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100397 u32 open_ttys;
398};
399
400/* This is a data packet that is read or written to/from card */
401struct buffer {
402 u32 size; /* size is the length of the data buffer */
403 u8 *data;
404} __attribute__ ((packed));
405
406/* Global variables */
Frank Seidel18bbe0c22008-02-01 09:14:05 +0100407static const struct pci_device_id nozomi_pci_tbl[] __devinitconst = {
Frank Seidel20fd1e32007-11-09 14:49:23 +0100408 {PCI_DEVICE(VENDOR1, DEVICE1)},
409 {},
410};
411
412MODULE_DEVICE_TABLE(pci, nozomi_pci_tbl);
413
414static struct nozomi *ndevs[NOZOMI_MAX_CARDS];
415static struct tty_driver *ntty_driver;
416
417/*
418 * find card by tty_index
419 */
420static inline struct nozomi *get_dc_by_tty(const struct tty_struct *tty)
421{
422 return tty ? ndevs[tty->index / MAX_PORT] : NULL;
423}
424
425static inline struct port *get_port_by_tty(const struct tty_struct *tty)
426{
427 struct nozomi *ndev = get_dc_by_tty(tty);
428 return ndev ? &ndev->port[tty->index % MAX_PORT] : NULL;
429}
430
431/*
432 * TODO:
433 * -Optimize
434 * -Rewrite cleaner
435 */
436
437static void read_mem32(u32 *buf, const void __iomem *mem_addr_start,
438 u32 size_bytes)
439{
440 u32 i = 0;
Al Viro782a6de2008-03-29 03:09:08 +0000441 const u32 __iomem *ptr = mem_addr_start;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100442 u16 *buf16;
443
444 if (unlikely(!ptr || !buf))
445 goto out;
446
447 /* shortcut for extremely often used cases */
448 switch (size_bytes) {
449 case 2: /* 2 bytes */
450 buf16 = (u16 *) buf;
Al Viro782a6de2008-03-29 03:09:08 +0000451 *buf16 = __le16_to_cpu(readw(ptr));
Frank Seidel20fd1e32007-11-09 14:49:23 +0100452 goto out;
453 break;
454 case 4: /* 4 bytes */
Al Viro782a6de2008-03-29 03:09:08 +0000455 *(buf) = __le32_to_cpu(readl(ptr));
Frank Seidel20fd1e32007-11-09 14:49:23 +0100456 goto out;
457 break;
458 }
459
460 while (i < size_bytes) {
461 if (size_bytes - i == 2) {
462 /* Handle 2 bytes in the end */
463 buf16 = (u16 *) buf;
Al Viro782a6de2008-03-29 03:09:08 +0000464 *(buf16) = __le16_to_cpu(readw(ptr));
Frank Seidel20fd1e32007-11-09 14:49:23 +0100465 i += 2;
466 } else {
467 /* Read 4 bytes */
Al Viro782a6de2008-03-29 03:09:08 +0000468 *(buf) = __le32_to_cpu(readl(ptr));
Frank Seidel20fd1e32007-11-09 14:49:23 +0100469 i += 4;
470 }
471 buf++;
472 ptr++;
473 }
474out:
475 return;
476}
477
478/*
479 * TODO:
480 * -Optimize
481 * -Rewrite cleaner
482 */
Jan Engelhardt71e1b4a2008-02-01 09:13:53 +0100483static u32 write_mem32(void __iomem *mem_addr_start, const u32 *buf,
Frank Seidel20fd1e32007-11-09 14:49:23 +0100484 u32 size_bytes)
485{
486 u32 i = 0;
Al Viro782a6de2008-03-29 03:09:08 +0000487 u32 __iomem *ptr = mem_addr_start;
Jan Engelhardt71e1b4a2008-02-01 09:13:53 +0100488 const u16 *buf16;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100489
490 if (unlikely(!ptr || !buf))
491 return 0;
492
493 /* shortcut for extremely often used cases */
494 switch (size_bytes) {
495 case 2: /* 2 bytes */
Jan Engelhardt71e1b4a2008-02-01 09:13:53 +0100496 buf16 = (const u16 *)buf;
Al Viro782a6de2008-03-29 03:09:08 +0000497 writew(__cpu_to_le16(*buf16), ptr);
Frank Seidel20fd1e32007-11-09 14:49:23 +0100498 return 2;
499 break;
500 case 1: /*
501 * also needs to write 4 bytes in this case
502 * so falling through..
503 */
504 case 4: /* 4 bytes */
Al Viro782a6de2008-03-29 03:09:08 +0000505 writel(__cpu_to_le32(*buf), ptr);
Frank Seidel20fd1e32007-11-09 14:49:23 +0100506 return 4;
507 break;
508 }
509
510 while (i < size_bytes) {
511 if (size_bytes - i == 2) {
512 /* 2 bytes */
Jan Engelhardt71e1b4a2008-02-01 09:13:53 +0100513 buf16 = (const u16 *)buf;
Al Viro782a6de2008-03-29 03:09:08 +0000514 writew(__cpu_to_le16(*buf16), ptr);
Frank Seidel20fd1e32007-11-09 14:49:23 +0100515 i += 2;
516 } else {
517 /* 4 bytes */
Al Viro782a6de2008-03-29 03:09:08 +0000518 writel(__cpu_to_le32(*buf), ptr);
Frank Seidel20fd1e32007-11-09 14:49:23 +0100519 i += 4;
520 }
521 buf++;
522 ptr++;
523 }
524 return i;
525}
526
527/* Setup pointers to different channels and also setup buffer sizes. */
528static void setup_memory(struct nozomi *dc)
529{
530 void __iomem *offset = dc->base_addr + dc->config_table.dl_start;
531 /* The length reported is including the length field of 4 bytes,
532 * hence subtract with 4.
533 */
534 const u16 buff_offset = 4;
535
536 /* Modem port dl configuration */
537 dc->port[PORT_MDM].dl_addr[CH_A] = offset;
538 dc->port[PORT_MDM].dl_addr[CH_B] =
539 (offset += dc->config_table.dl_mdm_len1);
540 dc->port[PORT_MDM].dl_size[CH_A] =
541 dc->config_table.dl_mdm_len1 - buff_offset;
542 dc->port[PORT_MDM].dl_size[CH_B] =
543 dc->config_table.dl_mdm_len2 - buff_offset;
544
545 /* Diag port dl configuration */
546 dc->port[PORT_DIAG].dl_addr[CH_A] =
547 (offset += dc->config_table.dl_mdm_len2);
548 dc->port[PORT_DIAG].dl_size[CH_A] =
549 dc->config_table.dl_diag_len1 - buff_offset;
550 dc->port[PORT_DIAG].dl_addr[CH_B] =
551 (offset += dc->config_table.dl_diag_len1);
552 dc->port[PORT_DIAG].dl_size[CH_B] =
553 dc->config_table.dl_diag_len2 - buff_offset;
554
555 /* App1 port dl configuration */
556 dc->port[PORT_APP1].dl_addr[CH_A] =
557 (offset += dc->config_table.dl_diag_len2);
558 dc->port[PORT_APP1].dl_size[CH_A] =
559 dc->config_table.dl_app1_len - buff_offset;
560
561 /* App2 port dl configuration */
562 dc->port[PORT_APP2].dl_addr[CH_A] =
563 (offset += dc->config_table.dl_app1_len);
564 dc->port[PORT_APP2].dl_size[CH_A] =
565 dc->config_table.dl_app2_len - buff_offset;
566
567 /* Ctrl dl configuration */
568 dc->port[PORT_CTRL].dl_addr[CH_A] =
569 (offset += dc->config_table.dl_app2_len);
570 dc->port[PORT_CTRL].dl_size[CH_A] =
571 dc->config_table.dl_ctrl_len - buff_offset;
572
573 offset = dc->base_addr + dc->config_table.ul_start;
574
575 /* Modem Port ul configuration */
576 dc->port[PORT_MDM].ul_addr[CH_A] = offset;
577 dc->port[PORT_MDM].ul_size[CH_A] =
578 dc->config_table.ul_mdm_len1 - buff_offset;
579 dc->port[PORT_MDM].ul_addr[CH_B] =
580 (offset += dc->config_table.ul_mdm_len1);
581 dc->port[PORT_MDM].ul_size[CH_B] =
582 dc->config_table.ul_mdm_len2 - buff_offset;
583
584 /* Diag port ul configuration */
585 dc->port[PORT_DIAG].ul_addr[CH_A] =
586 (offset += dc->config_table.ul_mdm_len2);
587 dc->port[PORT_DIAG].ul_size[CH_A] =
588 dc->config_table.ul_diag_len - buff_offset;
589
590 /* App1 port ul configuration */
591 dc->port[PORT_APP1].ul_addr[CH_A] =
592 (offset += dc->config_table.ul_diag_len);
593 dc->port[PORT_APP1].ul_size[CH_A] =
594 dc->config_table.ul_app1_len - buff_offset;
595
596 /* App2 port ul configuration */
597 dc->port[PORT_APP2].ul_addr[CH_A] =
598 (offset += dc->config_table.ul_app1_len);
599 dc->port[PORT_APP2].ul_size[CH_A] =
600 dc->config_table.ul_app2_len - buff_offset;
601
602 /* Ctrl ul configuration */
603 dc->port[PORT_CTRL].ul_addr[CH_A] =
604 (offset += dc->config_table.ul_app2_len);
605 dc->port[PORT_CTRL].ul_size[CH_A] =
606 dc->config_table.ul_ctrl_len - buff_offset;
607}
608
609/* Dump config table under initalization phase */
610#ifdef DEBUG
611static void dump_table(const struct nozomi *dc)
612{
613 DBG3("signature: 0x%08X", dc->config_table.signature);
614 DBG3("version: 0x%04X", dc->config_table.version);
615 DBG3("product_information: 0x%04X", \
616 dc->config_table.product_information);
617 DBG3("toggle enabled: %d", dc->config_table.toggle.enabled);
618 DBG3("toggle up_mdm: %d", dc->config_table.toggle.mdm_ul);
619 DBG3("toggle dl_mdm: %d", dc->config_table.toggle.mdm_dl);
620 DBG3("toggle dl_dbg: %d", dc->config_table.toggle.diag_dl);
621
622 DBG3("dl_start: 0x%04X", dc->config_table.dl_start);
623 DBG3("dl_mdm_len0: 0x%04X, %d", dc->config_table.dl_mdm_len1,
624 dc->config_table.dl_mdm_len1);
625 DBG3("dl_mdm_len1: 0x%04X, %d", dc->config_table.dl_mdm_len2,
626 dc->config_table.dl_mdm_len2);
627 DBG3("dl_diag_len0: 0x%04X, %d", dc->config_table.dl_diag_len1,
628 dc->config_table.dl_diag_len1);
629 DBG3("dl_diag_len1: 0x%04X, %d", dc->config_table.dl_diag_len2,
630 dc->config_table.dl_diag_len2);
631 DBG3("dl_app1_len: 0x%04X, %d", dc->config_table.dl_app1_len,
632 dc->config_table.dl_app1_len);
633 DBG3("dl_app2_len: 0x%04X, %d", dc->config_table.dl_app2_len,
634 dc->config_table.dl_app2_len);
635 DBG3("dl_ctrl_len: 0x%04X, %d", dc->config_table.dl_ctrl_len,
636 dc->config_table.dl_ctrl_len);
637 DBG3("ul_start: 0x%04X, %d", dc->config_table.ul_start,
638 dc->config_table.ul_start);
639 DBG3("ul_mdm_len[0]: 0x%04X, %d", dc->config_table.ul_mdm_len1,
640 dc->config_table.ul_mdm_len1);
641 DBG3("ul_mdm_len[1]: 0x%04X, %d", dc->config_table.ul_mdm_len2,
642 dc->config_table.ul_mdm_len2);
643 DBG3("ul_diag_len: 0x%04X, %d", dc->config_table.ul_diag_len,
644 dc->config_table.ul_diag_len);
645 DBG3("ul_app1_len: 0x%04X, %d", dc->config_table.ul_app1_len,
646 dc->config_table.ul_app1_len);
647 DBG3("ul_app2_len: 0x%04X, %d", dc->config_table.ul_app2_len,
648 dc->config_table.ul_app2_len);
649 DBG3("ul_ctrl_len: 0x%04X, %d", dc->config_table.ul_ctrl_len,
650 dc->config_table.ul_ctrl_len);
651}
652#else
Frank Seidele9ed5372008-01-25 21:13:24 +0100653static inline void dump_table(const struct nozomi *dc) { }
Frank Seidel20fd1e32007-11-09 14:49:23 +0100654#endif
655
656/*
657 * Read configuration table from card under intalization phase
658 * Returns 1 if ok, else 0
659 */
660static int nozomi_read_config_table(struct nozomi *dc)
661{
662 read_mem32((u32 *) &dc->config_table, dc->base_addr + 0,
663 sizeof(struct config_table));
664
665 if (dc->config_table.signature != CONFIG_MAGIC) {
666 dev_err(&dc->pdev->dev, "ConfigTable Bad! 0x%08X != 0x%08X\n",
667 dc->config_table.signature, CONFIG_MAGIC);
668 return 0;
669 }
670
671 if ((dc->config_table.version == 0)
672 || (dc->config_table.toggle.enabled == TOGGLE_VALID)) {
673 int i;
674 DBG1("Second phase, configuring card");
675
676 setup_memory(dc);
677
678 dc->port[PORT_MDM].toggle_ul = dc->config_table.toggle.mdm_ul;
679 dc->port[PORT_MDM].toggle_dl = dc->config_table.toggle.mdm_dl;
680 dc->port[PORT_DIAG].toggle_dl = dc->config_table.toggle.diag_dl;
681 DBG1("toggle ports: MDM UL:%d MDM DL:%d, DIAG DL:%d",
682 dc->port[PORT_MDM].toggle_ul,
683 dc->port[PORT_MDM].toggle_dl, dc->port[PORT_DIAG].toggle_dl);
684
685 dump_table(dc);
686
687 for (i = PORT_MDM; i < MAX_PORT; i++) {
Frank Seidel20fd1e32007-11-09 14:49:23 +0100688 memset(&dc->port[i].ctrl_dl, 0, sizeof(struct ctrl_dl));
689 memset(&dc->port[i].ctrl_ul, 0, sizeof(struct ctrl_ul));
690 }
691
692 /* Enable control channel */
693 dc->last_ier = dc->last_ier | CTRL_DL;
694 writew(dc->last_ier, dc->reg_ier);
695
Frank Seidel661b4e82008-03-06 21:45:57 +0100696 dc->state = NOZOMI_STATE_ALLOCATED;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100697 dev_info(&dc->pdev->dev, "Initialization OK!\n");
698 return 1;
699 }
700
701 if ((dc->config_table.version > 0)
702 && (dc->config_table.toggle.enabled != TOGGLE_VALID)) {
703 u32 offset = 0;
704 DBG1("First phase: pushing upload buffers, clearing download");
705
706 dev_info(&dc->pdev->dev, "Version of card: %d\n",
707 dc->config_table.version);
708
709 /* Here we should disable all I/O over F32. */
710 setup_memory(dc);
711
712 /*
713 * We should send ALL channel pair tokens back along
714 * with reset token
715 */
716
717 /* push upload modem buffers */
718 write_mem32(dc->port[PORT_MDM].ul_addr[CH_A],
719 (u32 *) &offset, 4);
720 write_mem32(dc->port[PORT_MDM].ul_addr[CH_B],
721 (u32 *) &offset, 4);
722
723 writew(MDM_UL | DIAG_DL | MDM_DL, dc->reg_fcr);
724
725 DBG1("First phase done");
726 }
727
728 return 1;
729}
730
731/* Enable uplink interrupts */
732static void enable_transmit_ul(enum port_type port, struct nozomi *dc)
733{
Jan Engelhardt71e1b4a2008-02-01 09:13:53 +0100734 static const u16 mask[] = {MDM_UL, DIAG_UL, APP1_UL, APP2_UL, CTRL_UL};
Frank Seidel20fd1e32007-11-09 14:49:23 +0100735
736 if (port < NOZOMI_MAX_PORTS) {
737 dc->last_ier |= mask[port];
738 writew(dc->last_ier, dc->reg_ier);
739 } else {
740 dev_err(&dc->pdev->dev, "Called with wrong port?\n");
741 }
742}
743
744/* Disable uplink interrupts */
745static void disable_transmit_ul(enum port_type port, struct nozomi *dc)
746{
Jan Engelhardt71e1b4a2008-02-01 09:13:53 +0100747 static const u16 mask[] =
748 {~MDM_UL, ~DIAG_UL, ~APP1_UL, ~APP2_UL, ~CTRL_UL};
Frank Seidel20fd1e32007-11-09 14:49:23 +0100749
750 if (port < NOZOMI_MAX_PORTS) {
751 dc->last_ier &= mask[port];
752 writew(dc->last_ier, dc->reg_ier);
753 } else {
754 dev_err(&dc->pdev->dev, "Called with wrong port?\n");
755 }
756}
757
758/* Enable downlink interrupts */
759static void enable_transmit_dl(enum port_type port, struct nozomi *dc)
760{
Jan Engelhardt71e1b4a2008-02-01 09:13:53 +0100761 static const u16 mask[] = {MDM_DL, DIAG_DL, APP1_DL, APP2_DL, CTRL_DL};
Frank Seidel20fd1e32007-11-09 14:49:23 +0100762
763 if (port < NOZOMI_MAX_PORTS) {
764 dc->last_ier |= mask[port];
765 writew(dc->last_ier, dc->reg_ier);
766 } else {
767 dev_err(&dc->pdev->dev, "Called with wrong port?\n");
768 }
769}
770
771/* Disable downlink interrupts */
772static void disable_transmit_dl(enum port_type port, struct nozomi *dc)
773{
Jan Engelhardt71e1b4a2008-02-01 09:13:53 +0100774 static const u16 mask[] =
775 {~MDM_DL, ~DIAG_DL, ~APP1_DL, ~APP2_DL, ~CTRL_DL};
Frank Seidel20fd1e32007-11-09 14:49:23 +0100776
777 if (port < NOZOMI_MAX_PORTS) {
778 dc->last_ier &= mask[port];
779 writew(dc->last_ier, dc->reg_ier);
780 } else {
781 dev_err(&dc->pdev->dev, "Called with wrong port?\n");
782 }
783}
784
785/*
786 * Return 1 - send buffer to card and ack.
787 * Return 0 - don't ack, don't send buffer to card.
788 */
Alan Cox33dd4742009-01-02 13:47:32 +0000789static int send_data(enum port_type index, struct nozomi *dc)
Frank Seidel20fd1e32007-11-09 14:49:23 +0100790{
791 u32 size = 0;
Alan Cox33dd4742009-01-02 13:47:32 +0000792 struct port *port = &dc->port[index];
Frank Seidel18bbe0c22008-02-01 09:14:05 +0100793 const u8 toggle = port->toggle_ul;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100794 void __iomem *addr = port->ul_addr[toggle];
Frank Seidel18bbe0c22008-02-01 09:14:05 +0100795 const u32 ul_size = port->ul_size[toggle];
Alan Cox33dd4742009-01-02 13:47:32 +0000796 struct tty_struct *tty = tty_port_tty_get(&port->port);
Frank Seidel20fd1e32007-11-09 14:49:23 +0100797
798 /* Get data from tty and place in buf for now */
Stefani Seibold7acd72e2009-12-21 14:37:28 -0800799 size = kfifo_out(&port->fifo_ul, dc->send_buf,
Frank Seidel20fd1e32007-11-09 14:49:23 +0100800 ul_size < SEND_BUF_MAX ? ul_size : SEND_BUF_MAX);
801
802 if (size == 0) {
803 DBG4("No more data to send, disable link:");
Alan Cox33dd4742009-01-02 13:47:32 +0000804 tty_kref_put(tty);
Frank Seidel20fd1e32007-11-09 14:49:23 +0100805 return 0;
806 }
807
808 /* DUMP(buf, size); */
809
810 /* Write length + data */
811 write_mem32(addr, (u32 *) &size, 4);
812 write_mem32(addr + 4, (u32 *) dc->send_buf, size);
813
814 if (tty)
815 tty_wakeup(tty);
816
Alan Cox33dd4742009-01-02 13:47:32 +0000817 tty_kref_put(tty);
Frank Seidel20fd1e32007-11-09 14:49:23 +0100818 return 1;
819}
820
821/* If all data has been read, return 1, else 0 */
822static int receive_data(enum port_type index, struct nozomi *dc)
823{
824 u8 buf[RECEIVE_BUF_MAX] = { 0 };
825 int size;
826 u32 offset = 4;
827 struct port *port = &dc->port[index];
828 void __iomem *addr = port->dl_addr[port->toggle_dl];
Alan Cox33dd4742009-01-02 13:47:32 +0000829 struct tty_struct *tty = tty_port_tty_get(&port->port);
Jiri Slaby9237a812009-07-16 16:06:18 +0100830 int i, ret;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100831
832 if (unlikely(!tty)) {
833 DBG1("tty not open for port: %d?", index);
834 return 1;
835 }
836
837 read_mem32((u32 *) &size, addr, 4);
838 /* DBG1( "%d bytes port: %d", size, index); */
839
840 if (test_bit(TTY_THROTTLED, &tty->flags)) {
841 DBG1("No room in tty, don't read data, don't ack interrupt, "
842 "disable interrupt");
843
844 /* disable interrupt in downlink... */
845 disable_transmit_dl(index, dc);
Jiri Slaby9237a812009-07-16 16:06:18 +0100846 ret = 0;
847 goto put;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100848 }
849
850 if (unlikely(size == 0)) {
851 dev_err(&dc->pdev->dev, "size == 0?\n");
Jiri Slaby9237a812009-07-16 16:06:18 +0100852 ret = 1;
853 goto put;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100854 }
855
856 tty_buffer_request_room(tty, size);
857
858 while (size > 0) {
859 read_mem32((u32 *) buf, addr + offset, RECEIVE_BUF_MAX);
860
861 if (size == 1) {
862 tty_insert_flip_char(tty, buf[0], TTY_NORMAL);
863 size = 0;
864 } else if (size < RECEIVE_BUF_MAX) {
865 size -= tty_insert_flip_string(tty, (char *) buf, size);
866 } else {
867 i = tty_insert_flip_string(tty, \
868 (char *) buf, RECEIVE_BUF_MAX);
869 size -= i;
870 offset += i;
871 }
872 }
873
874 set_bit(index, &dc->flip);
Jiri Slaby9237a812009-07-16 16:06:18 +0100875 ret = 1;
876put:
Alan Cox33dd4742009-01-02 13:47:32 +0000877 tty_kref_put(tty);
Jiri Slaby9237a812009-07-16 16:06:18 +0100878 return ret;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100879}
880
881/* Debug for interrupts */
882#ifdef DEBUG
883static char *interrupt2str(u16 interrupt)
884{
885 static char buf[TMP_BUF_MAX];
886 char *p = buf;
887
888 interrupt & MDM_DL1 ? p += snprintf(p, TMP_BUF_MAX, "MDM_DL1 ") : NULL;
889 interrupt & MDM_DL2 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
890 "MDM_DL2 ") : NULL;
891
892 interrupt & MDM_UL1 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
893 "MDM_UL1 ") : NULL;
894 interrupt & MDM_UL2 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
895 "MDM_UL2 ") : NULL;
896
897 interrupt & DIAG_DL1 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
898 "DIAG_DL1 ") : NULL;
899 interrupt & DIAG_DL2 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
900 "DIAG_DL2 ") : NULL;
901
902 interrupt & DIAG_UL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
903 "DIAG_UL ") : NULL;
904
905 interrupt & APP1_DL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
906 "APP1_DL ") : NULL;
907 interrupt & APP2_DL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
908 "APP2_DL ") : NULL;
909
910 interrupt & APP1_UL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
911 "APP1_UL ") : NULL;
912 interrupt & APP2_UL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
913 "APP2_UL ") : NULL;
914
915 interrupt & CTRL_DL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
916 "CTRL_DL ") : NULL;
917 interrupt & CTRL_UL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
918 "CTRL_UL ") : NULL;
919
920 interrupt & RESET ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
921 "RESET ") : NULL;
922
923 return buf;
924}
925#endif
926
927/*
928 * Receive flow control
929 * Return 1 - If ok, else 0
930 */
931static int receive_flow_control(struct nozomi *dc)
932{
933 enum port_type port = PORT_MDM;
934 struct ctrl_dl ctrl_dl;
935 struct ctrl_dl old_ctrl;
936 u16 enable_ier = 0;
937
938 read_mem32((u32 *) &ctrl_dl, dc->port[PORT_CTRL].dl_addr[CH_A], 2);
939
940 switch (ctrl_dl.port) {
941 case CTRL_CMD:
942 DBG1("The Base Band sends this value as a response to a "
943 "request for IMSI detach sent over the control "
944 "channel uplink (see section 7.6.1).");
945 break;
946 case CTRL_MDM:
947 port = PORT_MDM;
948 enable_ier = MDM_DL;
949 break;
950 case CTRL_DIAG:
951 port = PORT_DIAG;
952 enable_ier = DIAG_DL;
953 break;
954 case CTRL_APP1:
955 port = PORT_APP1;
956 enable_ier = APP1_DL;
957 break;
958 case CTRL_APP2:
959 port = PORT_APP2;
960 enable_ier = APP2_DL;
Frank Seidel661b4e82008-03-06 21:45:57 +0100961 if (dc->state == NOZOMI_STATE_ALLOCATED) {
962 /*
963 * After card initialization the flow control
964 * received for APP2 is always the last
965 */
966 dc->state = NOZOMI_STATE_READY;
967 dev_info(&dc->pdev->dev, "Device READY!\n");
968 }
Frank Seidel20fd1e32007-11-09 14:49:23 +0100969 break;
970 default:
971 dev_err(&dc->pdev->dev,
972 "ERROR: flow control received for non-existing port\n");
973 return 0;
974 };
975
976 DBG1("0x%04X->0x%04X", *((u16 *)&dc->port[port].ctrl_dl),
977 *((u16 *)&ctrl_dl));
978
979 old_ctrl = dc->port[port].ctrl_dl;
980 dc->port[port].ctrl_dl = ctrl_dl;
981
982 if (old_ctrl.CTS == 1 && ctrl_dl.CTS == 0) {
983 DBG1("Disable interrupt (0x%04X) on port: %d",
984 enable_ier, port);
985 disable_transmit_ul(port, dc);
986
987 } else if (old_ctrl.CTS == 0 && ctrl_dl.CTS == 1) {
988
Stefani Seibolde64c0262009-12-21 14:37:28 -0800989 if (kfifo_len(&dc->port[port].fifo_ul)) {
Frank Seidel20fd1e32007-11-09 14:49:23 +0100990 DBG1("Enable interrupt (0x%04X) on port: %d",
991 enable_ier, port);
992 DBG1("Data in buffer [%d], enable transmit! ",
Stefani Seibolde64c0262009-12-21 14:37:28 -0800993 kfifo_len(&dc->port[port].fifo_ul));
Frank Seidel20fd1e32007-11-09 14:49:23 +0100994 enable_transmit_ul(port, dc);
995 } else {
996 DBG1("No data in buffer...");
997 }
998 }
999
1000 if (*(u16 *)&old_ctrl == *(u16 *)&ctrl_dl) {
1001 DBG1(" No change in mctrl");
1002 return 1;
1003 }
1004 /* Update statistics */
1005 if (old_ctrl.CTS != ctrl_dl.CTS)
1006 dc->port[port].tty_icount.cts++;
1007 if (old_ctrl.DSR != ctrl_dl.DSR)
1008 dc->port[port].tty_icount.dsr++;
1009 if (old_ctrl.RI != ctrl_dl.RI)
1010 dc->port[port].tty_icount.rng++;
1011 if (old_ctrl.DCD != ctrl_dl.DCD)
1012 dc->port[port].tty_icount.dcd++;
1013
1014 wake_up_interruptible(&dc->port[port].tty_wait);
1015
1016 DBG1("port: %d DCD(%d), CTS(%d), RI(%d), DSR(%d)",
1017 port,
1018 dc->port[port].tty_icount.dcd, dc->port[port].tty_icount.cts,
1019 dc->port[port].tty_icount.rng, dc->port[port].tty_icount.dsr);
1020
1021 return 1;
1022}
1023
1024static enum ctrl_port_type port2ctrl(enum port_type port,
1025 const struct nozomi *dc)
1026{
1027 switch (port) {
1028 case PORT_MDM:
1029 return CTRL_MDM;
1030 case PORT_DIAG:
1031 return CTRL_DIAG;
1032 case PORT_APP1:
1033 return CTRL_APP1;
1034 case PORT_APP2:
1035 return CTRL_APP2;
1036 default:
1037 dev_err(&dc->pdev->dev,
1038 "ERROR: send flow control " \
1039 "received for non-existing port\n");
1040 };
1041 return CTRL_ERROR;
1042}
1043
1044/*
1045 * Send flow control, can only update one channel at a time
1046 * Return 0 - If we have updated all flow control
1047 * Return 1 - If we need to update more flow control, ack current enable more
1048 */
1049static int send_flow_control(struct nozomi *dc)
1050{
1051 u32 i, more_flow_control_to_be_updated = 0;
1052 u16 *ctrl;
1053
1054 for (i = PORT_MDM; i < MAX_PORT; i++) {
1055 if (dc->port[i].update_flow_control) {
1056 if (more_flow_control_to_be_updated) {
1057 /* We have more flow control to be updated */
1058 return 1;
1059 }
1060 dc->port[i].ctrl_ul.port = port2ctrl(i, dc);
1061 ctrl = (u16 *)&dc->port[i].ctrl_ul;
1062 write_mem32(dc->port[PORT_CTRL].ul_addr[0], \
1063 (u32 *) ctrl, 2);
1064 dc->port[i].update_flow_control = 0;
1065 more_flow_control_to_be_updated = 1;
1066 }
1067 }
1068 return 0;
1069}
1070
1071/*
Frank Seidele9ed5372008-01-25 21:13:24 +01001072 * Handle downlink data, ports that are handled are modem and diagnostics
Frank Seidel20fd1e32007-11-09 14:49:23 +01001073 * Return 1 - ok
1074 * Return 0 - toggle fields are out of sync
1075 */
1076static int handle_data_dl(struct nozomi *dc, enum port_type port, u8 *toggle,
1077 u16 read_iir, u16 mask1, u16 mask2)
1078{
1079 if (*toggle == 0 && read_iir & mask1) {
1080 if (receive_data(port, dc)) {
1081 writew(mask1, dc->reg_fcr);
1082 *toggle = !(*toggle);
1083 }
1084
1085 if (read_iir & mask2) {
1086 if (receive_data(port, dc)) {
1087 writew(mask2, dc->reg_fcr);
1088 *toggle = !(*toggle);
1089 }
1090 }
1091 } else if (*toggle == 1 && read_iir & mask2) {
1092 if (receive_data(port, dc)) {
1093 writew(mask2, dc->reg_fcr);
1094 *toggle = !(*toggle);
1095 }
1096
1097 if (read_iir & mask1) {
1098 if (receive_data(port, dc)) {
1099 writew(mask1, dc->reg_fcr);
1100 *toggle = !(*toggle);
1101 }
1102 }
1103 } else {
1104 dev_err(&dc->pdev->dev, "port out of sync!, toggle:%d\n",
1105 *toggle);
1106 return 0;
1107 }
1108 return 1;
1109}
1110
1111/*
1112 * Handle uplink data, this is currently for the modem port
1113 * Return 1 - ok
1114 * Return 0 - toggle field are out of sync
1115 */
1116static int handle_data_ul(struct nozomi *dc, enum port_type port, u16 read_iir)
1117{
1118 u8 *toggle = &(dc->port[port].toggle_ul);
1119
1120 if (*toggle == 0 && read_iir & MDM_UL1) {
1121 dc->last_ier &= ~MDM_UL;
1122 writew(dc->last_ier, dc->reg_ier);
1123 if (send_data(port, dc)) {
1124 writew(MDM_UL1, dc->reg_fcr);
1125 dc->last_ier = dc->last_ier | MDM_UL;
1126 writew(dc->last_ier, dc->reg_ier);
1127 *toggle = !*toggle;
1128 }
1129
1130 if (read_iir & MDM_UL2) {
1131 dc->last_ier &= ~MDM_UL;
1132 writew(dc->last_ier, dc->reg_ier);
1133 if (send_data(port, dc)) {
1134 writew(MDM_UL2, dc->reg_fcr);
1135 dc->last_ier = dc->last_ier | MDM_UL;
1136 writew(dc->last_ier, dc->reg_ier);
1137 *toggle = !*toggle;
1138 }
1139 }
1140
1141 } else if (*toggle == 1 && read_iir & MDM_UL2) {
1142 dc->last_ier &= ~MDM_UL;
1143 writew(dc->last_ier, dc->reg_ier);
1144 if (send_data(port, dc)) {
1145 writew(MDM_UL2, dc->reg_fcr);
1146 dc->last_ier = dc->last_ier | MDM_UL;
1147 writew(dc->last_ier, dc->reg_ier);
1148 *toggle = !*toggle;
1149 }
1150
1151 if (read_iir & MDM_UL1) {
1152 dc->last_ier &= ~MDM_UL;
1153 writew(dc->last_ier, dc->reg_ier);
1154 if (send_data(port, dc)) {
1155 writew(MDM_UL1, dc->reg_fcr);
1156 dc->last_ier = dc->last_ier | MDM_UL;
1157 writew(dc->last_ier, dc->reg_ier);
1158 *toggle = !*toggle;
1159 }
1160 }
1161 } else {
1162 writew(read_iir & MDM_UL, dc->reg_fcr);
1163 dev_err(&dc->pdev->dev, "port out of sync!\n");
1164 return 0;
1165 }
1166 return 1;
1167}
1168
1169static irqreturn_t interrupt_handler(int irq, void *dev_id)
1170{
1171 struct nozomi *dc = dev_id;
1172 unsigned int a;
1173 u16 read_iir;
1174
1175 if (!dc)
1176 return IRQ_NONE;
1177
1178 spin_lock(&dc->spin_mutex);
1179 read_iir = readw(dc->reg_iir);
1180
1181 /* Card removed */
1182 if (read_iir == (u16)-1)
1183 goto none;
1184 /*
1185 * Just handle interrupt enabled in IER
1186 * (by masking with dc->last_ier)
1187 */
1188 read_iir &= dc->last_ier;
1189
1190 if (read_iir == 0)
1191 goto none;
1192
1193
1194 DBG4("%s irq:0x%04X, prev:0x%04X", interrupt2str(read_iir), read_iir,
1195 dc->last_ier);
1196
1197 if (read_iir & RESET) {
1198 if (unlikely(!nozomi_read_config_table(dc))) {
1199 dc->last_ier = 0x0;
1200 writew(dc->last_ier, dc->reg_ier);
1201 dev_err(&dc->pdev->dev, "Could not read status from "
1202 "card, we should disable interface\n");
1203 } else {
1204 writew(RESET, dc->reg_fcr);
1205 }
1206 /* No more useful info if this was the reset interrupt. */
1207 goto exit_handler;
1208 }
1209 if (read_iir & CTRL_UL) {
1210 DBG1("CTRL_UL");
1211 dc->last_ier &= ~CTRL_UL;
1212 writew(dc->last_ier, dc->reg_ier);
1213 if (send_flow_control(dc)) {
1214 writew(CTRL_UL, dc->reg_fcr);
1215 dc->last_ier = dc->last_ier | CTRL_UL;
1216 writew(dc->last_ier, dc->reg_ier);
1217 }
1218 }
1219 if (read_iir & CTRL_DL) {
1220 receive_flow_control(dc);
1221 writew(CTRL_DL, dc->reg_fcr);
1222 }
1223 if (read_iir & MDM_DL) {
1224 if (!handle_data_dl(dc, PORT_MDM,
1225 &(dc->port[PORT_MDM].toggle_dl), read_iir,
1226 MDM_DL1, MDM_DL2)) {
1227 dev_err(&dc->pdev->dev, "MDM_DL out of sync!\n");
1228 goto exit_handler;
1229 }
1230 }
1231 if (read_iir & MDM_UL) {
1232 if (!handle_data_ul(dc, PORT_MDM, read_iir)) {
1233 dev_err(&dc->pdev->dev, "MDM_UL out of sync!\n");
1234 goto exit_handler;
1235 }
1236 }
1237 if (read_iir & DIAG_DL) {
1238 if (!handle_data_dl(dc, PORT_DIAG,
1239 &(dc->port[PORT_DIAG].toggle_dl), read_iir,
1240 DIAG_DL1, DIAG_DL2)) {
1241 dev_err(&dc->pdev->dev, "DIAG_DL out of sync!\n");
1242 goto exit_handler;
1243 }
1244 }
1245 if (read_iir & DIAG_UL) {
1246 dc->last_ier &= ~DIAG_UL;
1247 writew(dc->last_ier, dc->reg_ier);
1248 if (send_data(PORT_DIAG, dc)) {
1249 writew(DIAG_UL, dc->reg_fcr);
1250 dc->last_ier = dc->last_ier | DIAG_UL;
1251 writew(dc->last_ier, dc->reg_ier);
1252 }
1253 }
1254 if (read_iir & APP1_DL) {
1255 if (receive_data(PORT_APP1, dc))
1256 writew(APP1_DL, dc->reg_fcr);
1257 }
1258 if (read_iir & APP1_UL) {
1259 dc->last_ier &= ~APP1_UL;
1260 writew(dc->last_ier, dc->reg_ier);
1261 if (send_data(PORT_APP1, dc)) {
1262 writew(APP1_UL, dc->reg_fcr);
1263 dc->last_ier = dc->last_ier | APP1_UL;
1264 writew(dc->last_ier, dc->reg_ier);
1265 }
1266 }
1267 if (read_iir & APP2_DL) {
1268 if (receive_data(PORT_APP2, dc))
1269 writew(APP2_DL, dc->reg_fcr);
1270 }
1271 if (read_iir & APP2_UL) {
1272 dc->last_ier &= ~APP2_UL;
1273 writew(dc->last_ier, dc->reg_ier);
1274 if (send_data(PORT_APP2, dc)) {
1275 writew(APP2_UL, dc->reg_fcr);
1276 dc->last_ier = dc->last_ier | APP2_UL;
1277 writew(dc->last_ier, dc->reg_ier);
1278 }
1279 }
1280
1281exit_handler:
1282 spin_unlock(&dc->spin_mutex);
Alan Cox33dd4742009-01-02 13:47:32 +00001283 for (a = 0; a < NOZOMI_MAX_PORTS; a++) {
1284 struct tty_struct *tty;
1285 if (test_and_clear_bit(a, &dc->flip)) {
1286 tty = tty_port_tty_get(&dc->port[a].port);
1287 if (tty)
1288 tty_flip_buffer_push(tty);
1289 tty_kref_put(tty);
1290 }
1291 }
Frank Seidel20fd1e32007-11-09 14:49:23 +01001292 return IRQ_HANDLED;
1293none:
1294 spin_unlock(&dc->spin_mutex);
1295 return IRQ_NONE;
1296}
1297
1298static void nozomi_get_card_type(struct nozomi *dc)
1299{
1300 int i;
1301 u32 size = 0;
1302
1303 for (i = 0; i < 6; i++)
1304 size += pci_resource_len(dc->pdev, i);
1305
1306 /* Assume card type F32_8 if no match */
1307 dc->card_type = size == 2048 ? F32_2 : F32_8;
1308
1309 dev_info(&dc->pdev->dev, "Card type is: %d\n", dc->card_type);
1310}
1311
1312static void nozomi_setup_private_data(struct nozomi *dc)
1313{
1314 void __iomem *offset = dc->base_addr + dc->card_type / 2;
1315 unsigned int i;
1316
1317 dc->reg_fcr = (void __iomem *)(offset + R_FCR);
1318 dc->reg_iir = (void __iomem *)(offset + R_IIR);
1319 dc->reg_ier = (void __iomem *)(offset + R_IER);
1320 dc->last_ier = 0;
1321 dc->flip = 0;
1322
1323 dc->port[PORT_MDM].token_dl = MDM_DL;
1324 dc->port[PORT_DIAG].token_dl = DIAG_DL;
1325 dc->port[PORT_APP1].token_dl = APP1_DL;
1326 dc->port[PORT_APP2].token_dl = APP2_DL;
1327
1328 for (i = 0; i < MAX_PORT; i++)
1329 init_waitqueue_head(&dc->port[i].tty_wait);
1330}
1331
1332static ssize_t card_type_show(struct device *dev, struct device_attribute *attr,
1333 char *buf)
1334{
Jan Engelhardt71e1b4a2008-02-01 09:13:53 +01001335 const struct nozomi *dc = pci_get_drvdata(to_pci_dev(dev));
Frank Seidel20fd1e32007-11-09 14:49:23 +01001336
1337 return sprintf(buf, "%d\n", dc->card_type);
1338}
Frank Seidele9ed5372008-01-25 21:13:24 +01001339static DEVICE_ATTR(card_type, S_IRUGO, card_type_show, NULL);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001340
1341static ssize_t open_ttys_show(struct device *dev, struct device_attribute *attr,
1342 char *buf)
1343{
Jan Engelhardt71e1b4a2008-02-01 09:13:53 +01001344 const struct nozomi *dc = pci_get_drvdata(to_pci_dev(dev));
Frank Seidel20fd1e32007-11-09 14:49:23 +01001345
1346 return sprintf(buf, "%u\n", dc->open_ttys);
1347}
Frank Seidele9ed5372008-01-25 21:13:24 +01001348static DEVICE_ATTR(open_ttys, S_IRUGO, open_ttys_show, NULL);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001349
1350static void make_sysfs_files(struct nozomi *dc)
1351{
1352 if (device_create_file(&dc->pdev->dev, &dev_attr_card_type))
1353 dev_err(&dc->pdev->dev,
1354 "Could not create sysfs file for card_type\n");
1355 if (device_create_file(&dc->pdev->dev, &dev_attr_open_ttys))
1356 dev_err(&dc->pdev->dev,
1357 "Could not create sysfs file for open_ttys\n");
1358}
1359
1360static void remove_sysfs_files(struct nozomi *dc)
1361{
1362 device_remove_file(&dc->pdev->dev, &dev_attr_card_type);
1363 device_remove_file(&dc->pdev->dev, &dev_attr_open_ttys);
1364}
1365
1366/* Allocate memory for one device */
1367static int __devinit nozomi_card_init(struct pci_dev *pdev,
1368 const struct pci_device_id *ent)
1369{
1370 resource_size_t start;
1371 int ret;
1372 struct nozomi *dc = NULL;
1373 int ndev_idx;
1374 int i;
1375
1376 dev_dbg(&pdev->dev, "Init, new card found\n");
1377
1378 for (ndev_idx = 0; ndev_idx < ARRAY_SIZE(ndevs); ndev_idx++)
1379 if (!ndevs[ndev_idx])
1380 break;
1381
1382 if (ndev_idx >= ARRAY_SIZE(ndevs)) {
1383 dev_err(&pdev->dev, "no free tty range for this card left\n");
1384 ret = -EIO;
1385 goto err;
1386 }
1387
1388 dc = kzalloc(sizeof(struct nozomi), GFP_KERNEL);
1389 if (unlikely(!dc)) {
1390 dev_err(&pdev->dev, "Could not allocate memory\n");
1391 ret = -ENOMEM;
1392 goto err_free;
1393 }
1394
1395 dc->pdev = pdev;
1396
Frank Seidel20fd1e32007-11-09 14:49:23 +01001397 ret = pci_enable_device(dc->pdev);
1398 if (ret) {
1399 dev_err(&pdev->dev, "Failed to enable PCI Device\n");
1400 goto err_free;
1401 }
1402
Frank Seidel20fd1e32007-11-09 14:49:23 +01001403 ret = pci_request_regions(dc->pdev, NOZOMI_NAME);
1404 if (ret) {
1405 dev_err(&pdev->dev, "I/O address 0x%04x already in use\n",
1406 (int) /* nozomi_private.io_addr */ 0);
1407 goto err_disable_device;
1408 }
1409
Frank Seidel661b4e82008-03-06 21:45:57 +01001410 start = pci_resource_start(dc->pdev, 0);
1411 if (start == 0) {
1412 dev_err(&pdev->dev, "No I/O address for card detected\n");
1413 ret = -ENODEV;
1414 goto err_rel_regs;
1415 }
1416
1417 /* Find out what card type it is */
1418 nozomi_get_card_type(dc);
1419
Alan Cox24cb2332008-04-30 00:54:19 -07001420 dc->base_addr = ioremap_nocache(start, dc->card_type);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001421 if (!dc->base_addr) {
1422 dev_err(&pdev->dev, "Unable to map card MMIO\n");
1423 ret = -ENODEV;
1424 goto err_rel_regs;
1425 }
1426
1427 dc->send_buf = kmalloc(SEND_BUF_MAX, GFP_KERNEL);
1428 if (!dc->send_buf) {
1429 dev_err(&pdev->dev, "Could not allocate send buffer?\n");
1430 ret = -ENOMEM;
1431 goto err_free_sbuf;
1432 }
1433
Stefani Seibold9842c382009-12-21 14:37:29 -08001434 for (i = PORT_MDM; i < MAX_PORT; i++) {
1435 if (kfifo_alloc(&dc->port[i].fifo_ul,
1436 FIFO_BUFFER_SIZE_UL, GFP_ATOMIC)) {
1437 dev_err(&pdev->dev,
1438 "Could not allocate kfifo buffer\n");
1439 ret = -ENOMEM;
1440 goto err_free_kfifo;
1441 }
1442 }
1443
Frank Seidel20fd1e32007-11-09 14:49:23 +01001444 spin_lock_init(&dc->spin_mutex);
1445
1446 nozomi_setup_private_data(dc);
1447
1448 /* Disable all interrupts */
1449 dc->last_ier = 0;
1450 writew(dc->last_ier, dc->reg_ier);
1451
1452 ret = request_irq(pdev->irq, &interrupt_handler, IRQF_SHARED,
1453 NOZOMI_NAME, dc);
1454 if (unlikely(ret)) {
1455 dev_err(&pdev->dev, "can't request irq %d\n", pdev->irq);
Stefani Seibold9842c382009-12-21 14:37:29 -08001456 goto err_free_kfifo;
Frank Seidel20fd1e32007-11-09 14:49:23 +01001457 }
1458
1459 DBG1("base_addr: %p", dc->base_addr);
1460
1461 make_sysfs_files(dc);
1462
1463 dc->index_start = ndev_idx * MAX_PORT;
1464 ndevs[ndev_idx] = dc;
1465
Frank Seidel661b4e82008-03-06 21:45:57 +01001466 pci_set_drvdata(pdev, dc);
1467
1468 /* Enable RESET interrupt */
1469 dc->last_ier = RESET;
1470 iowrite16(dc->last_ier, dc->reg_ier);
1471
1472 dc->state = NOZOMI_STATE_ENABLED;
1473
Frank Seidel20fd1e32007-11-09 14:49:23 +01001474 for (i = 0; i < MAX_PORT; i++) {
Stefani Seibold9842c382009-12-21 14:37:29 -08001475 struct device *tty_dev;
1476
Frank Seidel20fd1e32007-11-09 14:49:23 +01001477 mutex_init(&dc->port[i].tty_sem);
Alan Cox33dd4742009-01-02 13:47:32 +00001478 tty_port_init(&dc->port[i].port);
Stefani Seibold9842c382009-12-21 14:37:29 -08001479 tty_dev = tty_register_device(ntty_driver, dc->index_start + i,
Frank Seidel20fd1e32007-11-09 14:49:23 +01001480 &pdev->dev);
Stefani Seibold9842c382009-12-21 14:37:29 -08001481
1482 if (IS_ERR(tty_dev)) {
1483 ret = PTR_ERR(tty_dev);
1484 dev_err(&pdev->dev, "Could not allocate tty?\n");
1485 goto err_free_tty;
1486 }
Frank Seidel20fd1e32007-11-09 14:49:23 +01001487 }
Stefani Seibold9842c382009-12-21 14:37:29 -08001488
Frank Seidel20fd1e32007-11-09 14:49:23 +01001489 return 0;
1490
Stefani Seibold9842c382009-12-21 14:37:29 -08001491err_free_tty:
1492 for (i = dc->index_start; i < dc->index_start + MAX_PORT; ++i)
1493 tty_unregister_device(ntty_driver, i);
1494err_free_kfifo:
1495 for (i = 0; i < MAX_PORT; i++)
1496 kfifo_free(&dc->port[i].fifo_ul);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001497err_free_sbuf:
1498 kfree(dc->send_buf);
1499 iounmap(dc->base_addr);
1500err_rel_regs:
1501 pci_release_regions(pdev);
1502err_disable_device:
1503 pci_disable_device(pdev);
1504err_free:
1505 kfree(dc);
1506err:
1507 return ret;
1508}
1509
1510static void __devexit tty_exit(struct nozomi *dc)
1511{
1512 unsigned int i;
1513
1514 DBG1(" ");
1515
1516 flush_scheduled_work();
1517
Alan Cox33dd4742009-01-02 13:47:32 +00001518 for (i = 0; i < MAX_PORT; ++i) {
1519 struct tty_struct *tty = tty_port_tty_get(&dc->port[i].port);
1520 if (tty && list_empty(&tty->hangup_work.entry))
1521 tty_hangup(tty);
1522 tty_kref_put(tty);
1523 }
1524 /* Racy below - surely should wait for scheduled work to be done or
1525 complete off a hangup method ? */
Frank Seidel20fd1e32007-11-09 14:49:23 +01001526 while (dc->open_ttys)
1527 msleep(1);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001528 for (i = dc->index_start; i < dc->index_start + MAX_PORT; ++i)
1529 tty_unregister_device(ntty_driver, i);
1530}
1531
1532/* Deallocate memory for one device */
1533static void __devexit nozomi_card_exit(struct pci_dev *pdev)
1534{
1535 int i;
1536 struct ctrl_ul ctrl;
1537 struct nozomi *dc = pci_get_drvdata(pdev);
1538
1539 /* Disable all interrupts */
1540 dc->last_ier = 0;
1541 writew(dc->last_ier, dc->reg_ier);
1542
1543 tty_exit(dc);
1544
1545 /* Send 0x0001, command card to resend the reset token. */
1546 /* This is to get the reset when the module is reloaded. */
1547 ctrl.port = 0x00;
1548 ctrl.reserved = 0;
1549 ctrl.RTS = 0;
1550 ctrl.DTR = 1;
1551 DBG1("sending flow control 0x%04X", *((u16 *)&ctrl));
1552
1553 /* Setup dc->reg addresses to we can use defines here */
1554 write_mem32(dc->port[PORT_CTRL].ul_addr[0], (u32 *)&ctrl, 2);
1555 writew(CTRL_UL, dc->reg_fcr); /* push the token to the card. */
1556
1557 remove_sysfs_files(dc);
1558
1559 free_irq(pdev->irq, dc);
1560
1561 for (i = 0; i < MAX_PORT; i++)
Stefani Seibold45465482009-12-21 14:37:26 -08001562 kfifo_free(&dc->port[i].fifo_ul);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001563
1564 kfree(dc->send_buf);
1565
1566 iounmap(dc->base_addr);
1567
1568 pci_release_regions(pdev);
1569
1570 pci_disable_device(pdev);
1571
1572 ndevs[dc->index_start / MAX_PORT] = NULL;
1573
1574 kfree(dc);
1575}
1576
1577static void set_rts(const struct tty_struct *tty, int rts)
1578{
1579 struct port *port = get_port_by_tty(tty);
1580
1581 port->ctrl_ul.RTS = rts;
1582 port->update_flow_control = 1;
1583 enable_transmit_ul(PORT_CTRL, get_dc_by_tty(tty));
1584}
1585
1586static void set_dtr(const struct tty_struct *tty, int dtr)
1587{
1588 struct port *port = get_port_by_tty(tty);
1589
1590 DBG1("SETTING DTR index: %d, dtr: %d", tty->index, dtr);
1591
1592 port->ctrl_ul.DTR = dtr;
1593 port->update_flow_control = 1;
1594 enable_transmit_ul(PORT_CTRL, get_dc_by_tty(tty));
1595}
1596
1597/*
1598 * ----------------------------------------------------------------------------
1599 * TTY code
1600 * ----------------------------------------------------------------------------
1601 */
1602
1603/* Called when the userspace process opens the tty, /dev/noz*. */
1604static int ntty_open(struct tty_struct *tty, struct file *file)
1605{
1606 struct port *port = get_port_by_tty(tty);
1607 struct nozomi *dc = get_dc_by_tty(tty);
1608 unsigned long flags;
1609
Frank Seidel661b4e82008-03-06 21:45:57 +01001610 if (!port || !dc || dc->state != NOZOMI_STATE_READY)
Frank Seidel20fd1e32007-11-09 14:49:23 +01001611 return -ENODEV;
1612
1613 if (mutex_lock_interruptible(&port->tty_sem))
1614 return -ERESTARTSYS;
1615
Alan Cox33dd4742009-01-02 13:47:32 +00001616 port->port.count++;
Frank Seidel20fd1e32007-11-09 14:49:23 +01001617 dc->open_ttys++;
1618
1619 /* Enable interrupt downlink for channel */
Alan Cox33dd4742009-01-02 13:47:32 +00001620 if (port->port.count == 1) {
Frank Seidel20fd1e32007-11-09 14:49:23 +01001621 tty->driver_data = port;
Alan Cox33dd4742009-01-02 13:47:32 +00001622 tty_port_tty_set(&port->port, tty);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001623 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 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1628 }
Frank Seidel20fd1e32007-11-09 14:49:23 +01001629 mutex_unlock(&port->tty_sem);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001630 return 0;
1631}
1632
Alan Cox716da632008-10-13 10:35:33 +01001633/* Called when the userspace process close the tty, /dev/noz*. Also
1634 called immediately if ntty_open fails in which case tty->driver_data
1635 will be NULL an we exit by the first return */
1636
Frank Seidel20fd1e32007-11-09 14:49:23 +01001637static void ntty_close(struct tty_struct *tty, struct file *file)
1638{
1639 struct nozomi *dc = get_dc_by_tty(tty);
Alan Cox33dd4742009-01-02 13:47:32 +00001640 struct port *nport = tty->driver_data;
1641 struct tty_port *port = &nport->port;
Frank Seidel20fd1e32007-11-09 14:49:23 +01001642 unsigned long flags;
1643
Alan Cox33dd4742009-01-02 13:47:32 +00001644 if (!dc || !nport)
Frank Seidel20fd1e32007-11-09 14:49:23 +01001645 return;
1646
Alan Cox33dd4742009-01-02 13:47:32 +00001647 /* Users cannot interrupt a close */
1648 mutex_lock(&nport->tty_sem);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001649
Alan Cox33dd4742009-01-02 13:47:32 +00001650 WARN_ON(!port->count);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001651
1652 dc->open_ttys--;
Alan Cox33dd4742009-01-02 13:47:32 +00001653 port->count--;
Frank Seidel20fd1e32007-11-09 14:49:23 +01001654
Alan Cox33dd4742009-01-02 13:47:32 +00001655 if (port->count == 0) {
1656 DBG1("close: %d", nport->token_dl);
Alan Coxeeec32a2010-01-04 16:26:50 +00001657 tty_port_tty_set(port, NULL);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001658 spin_lock_irqsave(&dc->spin_mutex, flags);
Alan Cox33dd4742009-01-02 13:47:32 +00001659 dc->last_ier &= ~(nport->token_dl);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001660 writew(dc->last_ier, dc->reg_ier);
1661 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1662 }
Alan Cox33dd4742009-01-02 13:47:32 +00001663 mutex_unlock(&nport->tty_sem);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001664}
1665
1666/*
1667 * called when the userspace process writes to the tty (/dev/noz*).
1668 * Data is inserted into a fifo, which is then read and transfered to the modem.
1669 */
1670static int ntty_write(struct tty_struct *tty, const unsigned char *buffer,
1671 int count)
1672{
1673 int rval = -EINVAL;
1674 struct nozomi *dc = get_dc_by_tty(tty);
1675 struct port *port = tty->driver_data;
1676 unsigned long flags;
1677
1678 /* DBG1( "WRITEx: %d, index = %d", count, index); */
1679
1680 if (!dc || !port)
1681 return -ENODEV;
1682
1683 if (unlikely(!mutex_trylock(&port->tty_sem))) {
1684 /*
1685 * must test lock as tty layer wraps calls
1686 * to this function with BKL
1687 */
1688 dev_err(&dc->pdev->dev, "Would have deadlocked - "
1689 "return EAGAIN\n");
1690 return -EAGAIN;
1691 }
1692
Alan Cox33dd4742009-01-02 13:47:32 +00001693 if (unlikely(!port->port.count)) {
Frank Seidel20fd1e32007-11-09 14:49:23 +01001694 DBG1(" ");
1695 goto exit;
1696 }
1697
Stefani Seibold7acd72e2009-12-21 14:37:28 -08001698 rval = kfifo_in(&port->fifo_ul, (unsigned char *)buffer, count);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001699
1700 /* notify card */
1701 if (unlikely(dc == NULL)) {
1702 DBG1("No device context?");
1703 goto exit;
1704 }
1705
1706 spin_lock_irqsave(&dc->spin_mutex, flags);
1707 /* CTS is only valid on the modem channel */
1708 if (port == &(dc->port[PORT_MDM])) {
1709 if (port->ctrl_dl.CTS) {
1710 DBG4("Enable interrupt");
1711 enable_transmit_ul(tty->index % MAX_PORT, dc);
1712 } else {
1713 dev_err(&dc->pdev->dev,
1714 "CTS not active on modem port?\n");
1715 }
1716 } else {
1717 enable_transmit_ul(tty->index % MAX_PORT, dc);
1718 }
1719 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1720
1721exit:
1722 mutex_unlock(&port->tty_sem);
1723 return rval;
1724}
1725
1726/*
1727 * Calculate how much is left in device
1728 * This method is called by the upper tty layer.
1729 * #according to sources N_TTY.c it expects a value >= 0 and
1730 * does not check for negative values.
1731 */
1732static int ntty_write_room(struct tty_struct *tty)
1733{
1734 struct port *port = tty->driver_data;
1735 int room = 0;
Jan Engelhardt71e1b4a2008-02-01 09:13:53 +01001736 const struct nozomi *dc = get_dc_by_tty(tty);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001737
1738 if (!dc || !port)
1739 return 0;
1740 if (!mutex_trylock(&port->tty_sem))
1741 return 0;
1742
Alan Cox33dd4742009-01-02 13:47:32 +00001743 if (!port->port.count)
Frank Seidel20fd1e32007-11-09 14:49:23 +01001744 goto exit;
1745
Stefani Seibolde64c0262009-12-21 14:37:28 -08001746 room = port->fifo_ul.size - kfifo_len(&port->fifo_ul);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001747
1748exit:
1749 mutex_unlock(&port->tty_sem);
1750 return room;
1751}
1752
1753/* Gets io control parameters */
1754static int ntty_tiocmget(struct tty_struct *tty, struct file *file)
1755{
Jan Engelhardt71e1b4a2008-02-01 09:13:53 +01001756 const struct port *port = tty->driver_data;
1757 const struct ctrl_dl *ctrl_dl = &port->ctrl_dl;
1758 const struct ctrl_ul *ctrl_ul = &port->ctrl_ul;
Frank Seidel20fd1e32007-11-09 14:49:23 +01001759
Alan Cox978e5952008-04-30 00:53:59 -07001760 /* Note: these could change under us but it is not clear this
1761 matters if so */
Frank Seidel20fd1e32007-11-09 14:49:23 +01001762 return (ctrl_ul->RTS ? TIOCM_RTS : 0) |
1763 (ctrl_ul->DTR ? TIOCM_DTR : 0) |
1764 (ctrl_dl->DCD ? TIOCM_CAR : 0) |
1765 (ctrl_dl->RI ? TIOCM_RNG : 0) |
1766 (ctrl_dl->DSR ? TIOCM_DSR : 0) |
1767 (ctrl_dl->CTS ? TIOCM_CTS : 0);
1768}
1769
1770/* Sets io controls parameters */
1771static int ntty_tiocmset(struct tty_struct *tty, struct file *file,
1772 unsigned int set, unsigned int clear)
1773{
Frank Seidel661b4e82008-03-06 21:45:57 +01001774 struct nozomi *dc = get_dc_by_tty(tty);
1775 unsigned long flags;
1776
1777 spin_lock_irqsave(&dc->spin_mutex, flags);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001778 if (set & TIOCM_RTS)
1779 set_rts(tty, 1);
1780 else if (clear & TIOCM_RTS)
1781 set_rts(tty, 0);
1782
1783 if (set & TIOCM_DTR)
1784 set_dtr(tty, 1);
1785 else if (clear & TIOCM_DTR)
1786 set_dtr(tty, 0);
Frank Seidel661b4e82008-03-06 21:45:57 +01001787 spin_unlock_irqrestore(&dc->spin_mutex, flags);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001788
1789 return 0;
1790}
1791
1792static int ntty_cflags_changed(struct port *port, unsigned long flags,
1793 struct async_icount *cprev)
1794{
Frank Seidel18bbe0c22008-02-01 09:14:05 +01001795 const struct async_icount cnow = port->tty_icount;
Frank Seidel20fd1e32007-11-09 14:49:23 +01001796 int ret;
1797
1798 ret = ((flags & TIOCM_RNG) && (cnow.rng != cprev->rng)) ||
1799 ((flags & TIOCM_DSR) && (cnow.dsr != cprev->dsr)) ||
1800 ((flags & TIOCM_CD) && (cnow.dcd != cprev->dcd)) ||
1801 ((flags & TIOCM_CTS) && (cnow.cts != cprev->cts));
1802
1803 *cprev = cnow;
1804
1805 return ret;
1806}
1807
1808static int ntty_ioctl_tiocgicount(struct port *port, void __user *argp)
1809{
Frank Seidel18bbe0c22008-02-01 09:14:05 +01001810 const struct async_icount cnow = port->tty_icount;
Frank Seidel20fd1e32007-11-09 14:49:23 +01001811 struct serial_icounter_struct icount;
1812
1813 icount.cts = cnow.cts;
1814 icount.dsr = cnow.dsr;
1815 icount.rng = cnow.rng;
1816 icount.dcd = cnow.dcd;
1817 icount.rx = cnow.rx;
1818 icount.tx = cnow.tx;
1819 icount.frame = cnow.frame;
1820 icount.overrun = cnow.overrun;
1821 icount.parity = cnow.parity;
1822 icount.brk = cnow.brk;
1823 icount.buf_overrun = cnow.buf_overrun;
1824
Frank Seidel661b4e82008-03-06 21:45:57 +01001825 return copy_to_user(argp, &icount, sizeof(icount)) ? -EFAULT : 0;
Frank Seidel20fd1e32007-11-09 14:49:23 +01001826}
1827
1828static int ntty_ioctl(struct tty_struct *tty, struct file *file,
1829 unsigned int cmd, unsigned long arg)
1830{
1831 struct port *port = tty->driver_data;
1832 void __user *argp = (void __user *)arg;
1833 int rval = -ENOIOCTLCMD;
1834
1835 DBG1("******** IOCTL, cmd: %d", cmd);
1836
1837 switch (cmd) {
1838 case TIOCMIWAIT: {
1839 struct async_icount cprev = port->tty_icount;
1840
1841 rval = wait_event_interruptible(port->tty_wait,
1842 ntty_cflags_changed(port, arg, &cprev));
1843 break;
1844 } case TIOCGICOUNT:
1845 rval = ntty_ioctl_tiocgicount(port, argp);
1846 break;
1847 default:
1848 DBG1("ERR: 0x%08X, %d", cmd, cmd);
1849 break;
1850 };
1851
1852 return rval;
1853}
1854
1855/*
1856 * Called by the upper tty layer when tty buffers are ready
1857 * to receive data again after a call to throttle.
1858 */
1859static void ntty_unthrottle(struct tty_struct *tty)
1860{
1861 struct nozomi *dc = get_dc_by_tty(tty);
1862 unsigned long flags;
1863
1864 DBG1("UNTHROTTLE");
1865 spin_lock_irqsave(&dc->spin_mutex, flags);
1866 enable_transmit_dl(tty->index % MAX_PORT, dc);
1867 set_rts(tty, 1);
1868
1869 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1870}
1871
1872/*
1873 * Called by the upper tty layer when the tty buffers are almost full.
1874 * The driver should stop send more data.
1875 */
1876static void ntty_throttle(struct tty_struct *tty)
1877{
1878 struct nozomi *dc = get_dc_by_tty(tty);
1879 unsigned long flags;
1880
1881 DBG1("THROTTLE");
1882 spin_lock_irqsave(&dc->spin_mutex, flags);
1883 set_rts(tty, 0);
1884 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1885}
1886
Frank Seidel20fd1e32007-11-09 14:49:23 +01001887/* Returns number of chars in buffer, called by tty layer */
1888static s32 ntty_chars_in_buffer(struct tty_struct *tty)
1889{
1890 struct port *port = tty->driver_data;
1891 struct nozomi *dc = get_dc_by_tty(tty);
Alan Cox23198fd2009-07-20 16:05:27 +01001892 s32 rval = 0;
Frank Seidel20fd1e32007-11-09 14:49:23 +01001893
1894 if (unlikely(!dc || !port)) {
Frank Seidel20fd1e32007-11-09 14:49:23 +01001895 goto exit_in_buffer;
1896 }
1897
Alan Cox33dd4742009-01-02 13:47:32 +00001898 if (unlikely(!port->port.count)) {
Frank Seidel20fd1e32007-11-09 14:49:23 +01001899 dev_err(&dc->pdev->dev, "No tty open?\n");
Frank Seidel20fd1e32007-11-09 14:49:23 +01001900 goto exit_in_buffer;
1901 }
1902
Stefani Seibolde64c0262009-12-21 14:37:28 -08001903 rval = kfifo_len(&port->fifo_ul);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001904
1905exit_in_buffer:
1906 return rval;
1907}
1908
Frank Seidel18bbe0c22008-02-01 09:14:05 +01001909static const struct tty_operations tty_ops = {
Frank Seidel20fd1e32007-11-09 14:49:23 +01001910 .ioctl = ntty_ioctl,
1911 .open = ntty_open,
1912 .close = ntty_close,
1913 .write = ntty_write,
1914 .write_room = ntty_write_room,
1915 .unthrottle = ntty_unthrottle,
1916 .throttle = ntty_throttle,
1917 .chars_in_buffer = ntty_chars_in_buffer,
Frank Seidel20fd1e32007-11-09 14:49:23 +01001918 .tiocmget = ntty_tiocmget,
1919 .tiocmset = ntty_tiocmset,
1920};
1921
1922/* Module initialization */
1923static struct pci_driver nozomi_driver = {
1924 .name = NOZOMI_NAME,
1925 .id_table = nozomi_pci_tbl,
1926 .probe = nozomi_card_init,
1927 .remove = __devexit_p(nozomi_card_exit),
1928};
1929
1930static __init int nozomi_init(void)
1931{
1932 int ret;
1933
1934 printk(KERN_INFO "Initializing %s\n", VERSION_STRING);
1935
1936 ntty_driver = alloc_tty_driver(NTTY_TTY_MAXMINORS);
1937 if (!ntty_driver)
1938 return -ENOMEM;
1939
1940 ntty_driver->owner = THIS_MODULE;
1941 ntty_driver->driver_name = NOZOMI_NAME_TTY;
1942 ntty_driver->name = "noz";
1943 ntty_driver->major = 0;
1944 ntty_driver->type = TTY_DRIVER_TYPE_SERIAL;
1945 ntty_driver->subtype = SERIAL_TYPE_NORMAL;
1946 ntty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
1947 ntty_driver->init_termios = tty_std_termios;
1948 ntty_driver->init_termios.c_cflag = B115200 | CS8 | CREAD | \
1949 HUPCL | CLOCAL;
1950 ntty_driver->init_termios.c_ispeed = 115200;
1951 ntty_driver->init_termios.c_ospeed = 115200;
1952 tty_set_operations(ntty_driver, &tty_ops);
1953
1954 ret = tty_register_driver(ntty_driver);
1955 if (ret) {
1956 printk(KERN_ERR "Nozomi: failed to register ntty driver\n");
1957 goto free_tty;
1958 }
1959
1960 ret = pci_register_driver(&nozomi_driver);
1961 if (ret) {
1962 printk(KERN_ERR "Nozomi: can't register pci driver\n");
1963 goto unr_tty;
1964 }
1965
1966 return 0;
1967unr_tty:
1968 tty_unregister_driver(ntty_driver);
1969free_tty:
1970 put_tty_driver(ntty_driver);
1971 return ret;
1972}
1973
1974static __exit void nozomi_exit(void)
1975{
1976 printk(KERN_INFO "Unloading %s\n", DRIVER_DESC);
1977 pci_unregister_driver(&nozomi_driver);
1978 tty_unregister_driver(ntty_driver);
1979 put_tty_driver(ntty_driver);
1980}
1981
1982module_init(nozomi_init);
1983module_exit(nozomi_exit);
1984
1985module_param(debug, int, S_IRUGO | S_IWUSR);
1986
1987MODULE_LICENSE("Dual BSD/GPL");
1988MODULE_DESCRIPTION(DRIVER_DESC);