blob: 1b74c48c40167f242c1072b7e3fda1fbf78459c9 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090058#include <linux/slab.h>
Frank Seidel20fd1e32007-11-09 14:49:23 +010059#include <asm/byteorder.h>
60
61#include <linux/delay.h>
62
63
64#define VERSION_STRING DRIVER_DESC " 2.1d (build date: " \
65 __DATE__ " " __TIME__ ")"
66
67/* Macros definitions */
68
69/* Default debug printout level */
70#define NOZOMI_DEBUG_LEVEL 0x00
71
72#define P_BUF_SIZE 128
73#define NFO(_err_flag_, args...) \
74do { \
75 char tmp[P_BUF_SIZE]; \
76 snprintf(tmp, sizeof(tmp), ##args); \
77 printk(_err_flag_ "[%d] %s(): %s\n", __LINE__, \
Harvey Harrisonbf9d8922008-04-30 00:55:10 -070078 __func__, tmp); \
Frank Seidel20fd1e32007-11-09 14:49:23 +010079} while (0)
80
81#define DBG1(args...) D_(0x01, ##args)
82#define DBG2(args...) D_(0x02, ##args)
83#define DBG3(args...) D_(0x04, ##args)
84#define DBG4(args...) D_(0x08, ##args)
85#define DBG5(args...) D_(0x10, ##args)
86#define DBG6(args...) D_(0x20, ##args)
87#define DBG7(args...) D_(0x40, ##args)
88#define DBG8(args...) D_(0x80, ##args)
89
90#ifdef DEBUG
91/* Do we need this settable at runtime? */
92static int debug = NOZOMI_DEBUG_LEVEL;
93
Frank Seidele9ed5372008-01-25 21:13:24 +010094#define D(lvl, args...) do \
95 {if (lvl & debug) NFO(KERN_DEBUG, ##args); } \
96 while (0)
Frank Seidel20fd1e32007-11-09 14:49:23 +010097#define D_(lvl, args...) D(lvl, ##args)
98
99/* These printouts are always printed */
100
101#else
102static int debug;
103#define D_(lvl, args...)
104#endif
105
106/* TODO: rewrite to optimize macros... */
107
108#define TMP_BUF_MAX 256
109
110#define DUMP(buf__,len__) \
111 do { \
112 char tbuf[TMP_BUF_MAX] = {0};\
113 if (len__ > 1) {\
114 snprintf(tbuf, len__ > TMP_BUF_MAX ? TMP_BUF_MAX : len__, "%s", buf__);\
115 if (tbuf[len__-2] == '\r') {\
116 tbuf[len__-2] = 'r';\
117 } \
118 DBG1("SENDING: '%s' (%d+n)", tbuf, len__);\
119 } else {\
120 DBG1("SENDING: '%s' (%d)", tbuf, len__);\
121 } \
122} while (0)
123
124/* Defines */
125#define NOZOMI_NAME "nozomi"
126#define NOZOMI_NAME_TTY "nozomi_tty"
127#define DRIVER_DESC "Nozomi driver"
128
129#define NTTY_TTY_MAXMINORS 256
130#define NTTY_FIFO_BUFFER_SIZE 8192
131
132/* Must be power of 2 */
133#define FIFO_BUFFER_SIZE_UL 8192
134
135/* Size of tmp send buffer to card */
136#define SEND_BUF_MAX 1024
137#define RECEIVE_BUF_MAX 4
138
139
Frank Seidel20fd1e32007-11-09 14:49:23 +0100140#define R_IIR 0x0000 /* Interrupt Identity Register */
141#define R_FCR 0x0000 /* Flow Control Register */
142#define R_IER 0x0004 /* Interrupt Enable Register */
143
144#define CONFIG_MAGIC 0xEFEFFEFE
145#define TOGGLE_VALID 0x0000
146
147/* Definition of interrupt tokens */
148#define MDM_DL1 0x0001
149#define MDM_UL1 0x0002
150#define MDM_DL2 0x0004
151#define MDM_UL2 0x0008
152#define DIAG_DL1 0x0010
153#define DIAG_DL2 0x0020
154#define DIAG_UL 0x0040
155#define APP1_DL 0x0080
156#define APP1_UL 0x0100
157#define APP2_DL 0x0200
158#define APP2_UL 0x0400
159#define CTRL_DL 0x0800
160#define CTRL_UL 0x1000
161#define RESET 0x8000
162
163#define MDM_DL (MDM_DL1 | MDM_DL2)
164#define MDM_UL (MDM_UL1 | MDM_UL2)
165#define DIAG_DL (DIAG_DL1 | DIAG_DL2)
166
167/* modem signal definition */
168#define CTRL_DSR 0x0001
169#define CTRL_DCD 0x0002
170#define CTRL_RI 0x0004
171#define CTRL_CTS 0x0008
172
173#define CTRL_DTR 0x0001
174#define CTRL_RTS 0x0002
175
176#define MAX_PORT 4
177#define NOZOMI_MAX_PORTS 5
178#define NOZOMI_MAX_CARDS (NTTY_TTY_MAXMINORS / MAX_PORT)
179
180/* Type definitions */
181
182/*
183 * There are two types of nozomi cards,
184 * one with 2048 memory and with 8192 memory
185 */
186enum card_type {
187 F32_2 = 2048, /* 512 bytes downlink + uplink * 2 -> 2048 */
188 F32_8 = 8192, /* 3072 bytes downl. + 1024 bytes uplink * 2 -> 8192 */
189};
190
Frank Seidel661b4e82008-03-06 21:45:57 +0100191/* Initialization states a card can be in */
192enum card_state {
193 NOZOMI_STATE_UKNOWN = 0,
194 NOZOMI_STATE_ENABLED = 1, /* pci device enabled */
195 NOZOMI_STATE_ALLOCATED = 2, /* config setup done */
196 NOZOMI_STATE_READY = 3, /* flowcontrols received */
197};
198
Frank Seidel20fd1e32007-11-09 14:49:23 +0100199/* Two different toggle channels exist */
200enum channel_type {
201 CH_A = 0,
202 CH_B = 1,
203};
204
205/* Port definition for the card regarding flow control */
206enum ctrl_port_type {
207 CTRL_CMD = 0,
208 CTRL_MDM = 1,
209 CTRL_DIAG = 2,
210 CTRL_APP1 = 3,
211 CTRL_APP2 = 4,
212 CTRL_ERROR = -1,
213};
214
215/* Ports that the nozomi has */
216enum port_type {
217 PORT_MDM = 0,
218 PORT_DIAG = 1,
219 PORT_APP1 = 2,
220 PORT_APP2 = 3,
221 PORT_CTRL = 4,
222 PORT_ERROR = -1,
223};
224
225#ifdef __BIG_ENDIAN
226/* Big endian */
227
228struct toggles {
Frank Seidele9ed5372008-01-25 21:13:24 +0100229 unsigned int enabled:5; /*
Frank Seidel20fd1e32007-11-09 14:49:23 +0100230 * Toggle fields are valid if enabled is 0,
231 * else A-channels must always be used.
232 */
Frank Seidele9ed5372008-01-25 21:13:24 +0100233 unsigned int diag_dl:1;
234 unsigned int mdm_dl:1;
235 unsigned int mdm_ul:1;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100236} __attribute__ ((packed));
237
238/* Configuration table to read at startup of card */
239/* Is for now only needed during initialization phase */
240struct config_table {
241 u32 signature;
242 u16 product_information;
243 u16 version;
244 u8 pad3[3];
245 struct toggles toggle;
246 u8 pad1[4];
247 u16 dl_mdm_len1; /*
248 * If this is 64, it can hold
249 * 60 bytes + 4 that is length field
250 */
251 u16 dl_start;
252
253 u16 dl_diag_len1;
254 u16 dl_mdm_len2; /*
255 * If this is 64, it can hold
256 * 60 bytes + 4 that is length field
257 */
258 u16 dl_app1_len;
259
260 u16 dl_diag_len2;
261 u16 dl_ctrl_len;
262 u16 dl_app2_len;
263 u8 pad2[16];
264 u16 ul_mdm_len1;
265 u16 ul_start;
266 u16 ul_diag_len;
267 u16 ul_mdm_len2;
268 u16 ul_app1_len;
269 u16 ul_app2_len;
270 u16 ul_ctrl_len;
271} __attribute__ ((packed));
272
273/* This stores all control downlink flags */
274struct ctrl_dl {
275 u8 port;
Frank Seidele9ed5372008-01-25 21:13:24 +0100276 unsigned int reserved:4;
277 unsigned int CTS:1;
278 unsigned int RI:1;
279 unsigned int DCD:1;
280 unsigned int DSR:1;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100281} __attribute__ ((packed));
282
283/* This stores all control uplink flags */
284struct ctrl_ul {
285 u8 port;
Frank Seidele9ed5372008-01-25 21:13:24 +0100286 unsigned int reserved:6;
287 unsigned int RTS:1;
288 unsigned int DTR:1;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100289} __attribute__ ((packed));
290
291#else
292/* Little endian */
293
294/* This represents the toggle information */
295struct toggles {
Frank Seidele9ed5372008-01-25 21:13:24 +0100296 unsigned int mdm_ul:1;
297 unsigned int mdm_dl:1;
298 unsigned int diag_dl:1;
299 unsigned int enabled:5; /*
Frank Seidel20fd1e32007-11-09 14:49:23 +0100300 * Toggle fields are valid if enabled is 0,
301 * else A-channels must always be used.
302 */
303} __attribute__ ((packed));
304
305/* Configuration table to read at startup of card */
306struct config_table {
307 u32 signature;
308 u16 version;
309 u16 product_information;
310 struct toggles toggle;
311 u8 pad1[7];
312 u16 dl_start;
313 u16 dl_mdm_len1; /*
314 * If this is 64, it can hold
315 * 60 bytes + 4 that is length field
316 */
317 u16 dl_mdm_len2;
318 u16 dl_diag_len1;
319 u16 dl_diag_len2;
320 u16 dl_app1_len;
321 u16 dl_app2_len;
322 u16 dl_ctrl_len;
323 u8 pad2[16];
324 u16 ul_start;
325 u16 ul_mdm_len2;
326 u16 ul_mdm_len1;
327 u16 ul_diag_len;
328 u16 ul_app1_len;
329 u16 ul_app2_len;
330 u16 ul_ctrl_len;
331} __attribute__ ((packed));
332
333/* This stores all control downlink flags */
334struct ctrl_dl {
Frank Seidele9ed5372008-01-25 21:13:24 +0100335 unsigned int DSR:1;
336 unsigned int DCD:1;
337 unsigned int RI:1;
338 unsigned int CTS:1;
339 unsigned int reserverd:4;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100340 u8 port;
341} __attribute__ ((packed));
342
343/* This stores all control uplink flags */
344struct ctrl_ul {
Frank Seidele9ed5372008-01-25 21:13:24 +0100345 unsigned int DTR:1;
346 unsigned int RTS:1;
347 unsigned int reserved:6;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100348 u8 port;
349} __attribute__ ((packed));
350#endif
351
352/* This holds all information that is needed regarding a port */
353struct port {
Alan Cox33dd4742009-01-02 13:47:32 +0000354 struct tty_port port;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100355 u8 update_flow_control;
356 struct ctrl_ul ctrl_ul;
357 struct ctrl_dl ctrl_dl;
Stefani Seibold45465482009-12-21 14:37:26 -0800358 struct kfifo fifo_ul;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100359 void __iomem *dl_addr[2];
360 u32 dl_size[2];
361 u8 toggle_dl;
362 void __iomem *ul_addr[2];
363 u32 ul_size[2];
364 u8 toggle_ul;
365 u16 token_dl;
366
Frank Seidel20fd1e32007-11-09 14:49:23 +0100367 /* mutex to ensure one access patch to this port */
368 struct mutex tty_sem;
369 wait_queue_head_t tty_wait;
370 struct async_icount tty_icount;
Alan Cox266794e2010-02-08 10:06:45 +0000371
372 struct nozomi *dc;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100373};
374
375/* Private data one for each card in the system */
376struct nozomi {
377 void __iomem *base_addr;
378 unsigned long flip;
379
380 /* Pointers to registers */
381 void __iomem *reg_iir;
382 void __iomem *reg_fcr;
383 void __iomem *reg_ier;
384
385 u16 last_ier;
386 enum card_type card_type;
387 struct config_table config_table; /* Configuration table */
388 struct pci_dev *pdev;
389 struct port port[NOZOMI_MAX_PORTS];
390 u8 *send_buf;
391
392 spinlock_t spin_mutex; /* secures access to registers and tty */
393
394 unsigned int index_start;
Frank Seidel661b4e82008-03-06 21:45:57 +0100395 enum card_state state;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100396 u32 open_ttys;
397};
398
399/* This is a data packet that is read or written to/from card */
400struct buffer {
401 u32 size; /* size is the length of the data buffer */
402 u8 *data;
403} __attribute__ ((packed));
404
405/* Global variables */
Frank Seidel18bbe0c22008-02-01 09:14:05 +0100406static const struct pci_device_id nozomi_pci_tbl[] __devinitconst = {
Alan Coxb2a3dbc2010-02-08 10:07:15 +0000407 {PCI_DEVICE(0x1931, 0x000c)}, /* Nozomi HSDPA */
Frank Seidel20fd1e32007-11-09 14:49:23 +0100408 {},
409};
410
411MODULE_DEVICE_TABLE(pci, nozomi_pci_tbl);
412
413static struct nozomi *ndevs[NOZOMI_MAX_CARDS];
414static struct tty_driver *ntty_driver;
415
Alan Cox266794e2010-02-08 10:06:45 +0000416static const struct tty_port_operations noz_tty_port_ops;
417
Frank Seidel20fd1e32007-11-09 14:49:23 +0100418/*
419 * find card by tty_index
420 */
421static inline struct nozomi *get_dc_by_tty(const struct tty_struct *tty)
422{
423 return tty ? ndevs[tty->index / MAX_PORT] : NULL;
424}
425
426static inline struct port *get_port_by_tty(const struct tty_struct *tty)
427{
428 struct nozomi *ndev = get_dc_by_tty(tty);
429 return ndev ? &ndev->port[tty->index % MAX_PORT] : NULL;
430}
431
432/*
433 * TODO:
434 * -Optimize
435 * -Rewrite cleaner
436 */
437
438static void read_mem32(u32 *buf, const void __iomem *mem_addr_start,
439 u32 size_bytes)
440{
441 u32 i = 0;
Al Viro782a6de2008-03-29 03:09:08 +0000442 const u32 __iomem *ptr = mem_addr_start;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100443 u16 *buf16;
444
445 if (unlikely(!ptr || !buf))
446 goto out;
447
448 /* shortcut for extremely often used cases */
449 switch (size_bytes) {
450 case 2: /* 2 bytes */
451 buf16 = (u16 *) buf;
Al Viro782a6de2008-03-29 03:09:08 +0000452 *buf16 = __le16_to_cpu(readw(ptr));
Frank Seidel20fd1e32007-11-09 14:49:23 +0100453 goto out;
454 break;
455 case 4: /* 4 bytes */
Al Viro782a6de2008-03-29 03:09:08 +0000456 *(buf) = __le32_to_cpu(readl(ptr));
Frank Seidel20fd1e32007-11-09 14:49:23 +0100457 goto out;
458 break;
459 }
460
461 while (i < size_bytes) {
462 if (size_bytes - i == 2) {
463 /* Handle 2 bytes in the end */
464 buf16 = (u16 *) buf;
Al Viro782a6de2008-03-29 03:09:08 +0000465 *(buf16) = __le16_to_cpu(readw(ptr));
Frank Seidel20fd1e32007-11-09 14:49:23 +0100466 i += 2;
467 } else {
468 /* Read 4 bytes */
Al Viro782a6de2008-03-29 03:09:08 +0000469 *(buf) = __le32_to_cpu(readl(ptr));
Frank Seidel20fd1e32007-11-09 14:49:23 +0100470 i += 4;
471 }
472 buf++;
473 ptr++;
474 }
475out:
476 return;
477}
478
479/*
480 * TODO:
481 * -Optimize
482 * -Rewrite cleaner
483 */
Jan Engelhardt71e1b4a2008-02-01 09:13:53 +0100484static u32 write_mem32(void __iomem *mem_addr_start, const u32 *buf,
Frank Seidel20fd1e32007-11-09 14:49:23 +0100485 u32 size_bytes)
486{
487 u32 i = 0;
Al Viro782a6de2008-03-29 03:09:08 +0000488 u32 __iomem *ptr = mem_addr_start;
Jan Engelhardt71e1b4a2008-02-01 09:13:53 +0100489 const u16 *buf16;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100490
491 if (unlikely(!ptr || !buf))
492 return 0;
493
494 /* shortcut for extremely often used cases */
495 switch (size_bytes) {
496 case 2: /* 2 bytes */
Jan Engelhardt71e1b4a2008-02-01 09:13:53 +0100497 buf16 = (const u16 *)buf;
Al Viro782a6de2008-03-29 03:09:08 +0000498 writew(__cpu_to_le16(*buf16), ptr);
Frank Seidel20fd1e32007-11-09 14:49:23 +0100499 return 2;
500 break;
501 case 1: /*
502 * also needs to write 4 bytes in this case
503 * so falling through..
504 */
505 case 4: /* 4 bytes */
Al Viro782a6de2008-03-29 03:09:08 +0000506 writel(__cpu_to_le32(*buf), ptr);
Frank Seidel20fd1e32007-11-09 14:49:23 +0100507 return 4;
508 break;
509 }
510
511 while (i < size_bytes) {
512 if (size_bytes - i == 2) {
513 /* 2 bytes */
Jan Engelhardt71e1b4a2008-02-01 09:13:53 +0100514 buf16 = (const u16 *)buf;
Al Viro782a6de2008-03-29 03:09:08 +0000515 writew(__cpu_to_le16(*buf16), ptr);
Frank Seidel20fd1e32007-11-09 14:49:23 +0100516 i += 2;
517 } else {
518 /* 4 bytes */
Al Viro782a6de2008-03-29 03:09:08 +0000519 writel(__cpu_to_le32(*buf), ptr);
Frank Seidel20fd1e32007-11-09 14:49:23 +0100520 i += 4;
521 }
522 buf++;
523 ptr++;
524 }
525 return i;
526}
527
528/* Setup pointers to different channels and also setup buffer sizes. */
529static void setup_memory(struct nozomi *dc)
530{
531 void __iomem *offset = dc->base_addr + dc->config_table.dl_start;
532 /* The length reported is including the length field of 4 bytes,
533 * hence subtract with 4.
534 */
535 const u16 buff_offset = 4;
536
537 /* Modem port dl configuration */
538 dc->port[PORT_MDM].dl_addr[CH_A] = offset;
539 dc->port[PORT_MDM].dl_addr[CH_B] =
540 (offset += dc->config_table.dl_mdm_len1);
541 dc->port[PORT_MDM].dl_size[CH_A] =
542 dc->config_table.dl_mdm_len1 - buff_offset;
543 dc->port[PORT_MDM].dl_size[CH_B] =
544 dc->config_table.dl_mdm_len2 - buff_offset;
545
546 /* Diag port dl configuration */
547 dc->port[PORT_DIAG].dl_addr[CH_A] =
548 (offset += dc->config_table.dl_mdm_len2);
549 dc->port[PORT_DIAG].dl_size[CH_A] =
550 dc->config_table.dl_diag_len1 - buff_offset;
551 dc->port[PORT_DIAG].dl_addr[CH_B] =
552 (offset += dc->config_table.dl_diag_len1);
553 dc->port[PORT_DIAG].dl_size[CH_B] =
554 dc->config_table.dl_diag_len2 - buff_offset;
555
556 /* App1 port dl configuration */
557 dc->port[PORT_APP1].dl_addr[CH_A] =
558 (offset += dc->config_table.dl_diag_len2);
559 dc->port[PORT_APP1].dl_size[CH_A] =
560 dc->config_table.dl_app1_len - buff_offset;
561
562 /* App2 port dl configuration */
563 dc->port[PORT_APP2].dl_addr[CH_A] =
564 (offset += dc->config_table.dl_app1_len);
565 dc->port[PORT_APP2].dl_size[CH_A] =
566 dc->config_table.dl_app2_len - buff_offset;
567
568 /* Ctrl dl configuration */
569 dc->port[PORT_CTRL].dl_addr[CH_A] =
570 (offset += dc->config_table.dl_app2_len);
571 dc->port[PORT_CTRL].dl_size[CH_A] =
572 dc->config_table.dl_ctrl_len - buff_offset;
573
574 offset = dc->base_addr + dc->config_table.ul_start;
575
576 /* Modem Port ul configuration */
577 dc->port[PORT_MDM].ul_addr[CH_A] = offset;
578 dc->port[PORT_MDM].ul_size[CH_A] =
579 dc->config_table.ul_mdm_len1 - buff_offset;
580 dc->port[PORT_MDM].ul_addr[CH_B] =
581 (offset += dc->config_table.ul_mdm_len1);
582 dc->port[PORT_MDM].ul_size[CH_B] =
583 dc->config_table.ul_mdm_len2 - buff_offset;
584
585 /* Diag port ul configuration */
586 dc->port[PORT_DIAG].ul_addr[CH_A] =
587 (offset += dc->config_table.ul_mdm_len2);
588 dc->port[PORT_DIAG].ul_size[CH_A] =
589 dc->config_table.ul_diag_len - buff_offset;
590
591 /* App1 port ul configuration */
592 dc->port[PORT_APP1].ul_addr[CH_A] =
593 (offset += dc->config_table.ul_diag_len);
594 dc->port[PORT_APP1].ul_size[CH_A] =
595 dc->config_table.ul_app1_len - buff_offset;
596
597 /* App2 port ul configuration */
598 dc->port[PORT_APP2].ul_addr[CH_A] =
599 (offset += dc->config_table.ul_app1_len);
600 dc->port[PORT_APP2].ul_size[CH_A] =
601 dc->config_table.ul_app2_len - buff_offset;
602
603 /* Ctrl ul configuration */
604 dc->port[PORT_CTRL].ul_addr[CH_A] =
605 (offset += dc->config_table.ul_app2_len);
606 dc->port[PORT_CTRL].ul_size[CH_A] =
607 dc->config_table.ul_ctrl_len - buff_offset;
608}
609
610/* Dump config table under initalization phase */
611#ifdef DEBUG
612static void dump_table(const struct nozomi *dc)
613{
614 DBG3("signature: 0x%08X", dc->config_table.signature);
615 DBG3("version: 0x%04X", dc->config_table.version);
616 DBG3("product_information: 0x%04X", \
617 dc->config_table.product_information);
618 DBG3("toggle enabled: %d", dc->config_table.toggle.enabled);
619 DBG3("toggle up_mdm: %d", dc->config_table.toggle.mdm_ul);
620 DBG3("toggle dl_mdm: %d", dc->config_table.toggle.mdm_dl);
621 DBG3("toggle dl_dbg: %d", dc->config_table.toggle.diag_dl);
622
623 DBG3("dl_start: 0x%04X", dc->config_table.dl_start);
624 DBG3("dl_mdm_len0: 0x%04X, %d", dc->config_table.dl_mdm_len1,
625 dc->config_table.dl_mdm_len1);
626 DBG3("dl_mdm_len1: 0x%04X, %d", dc->config_table.dl_mdm_len2,
627 dc->config_table.dl_mdm_len2);
628 DBG3("dl_diag_len0: 0x%04X, %d", dc->config_table.dl_diag_len1,
629 dc->config_table.dl_diag_len1);
630 DBG3("dl_diag_len1: 0x%04X, %d", dc->config_table.dl_diag_len2,
631 dc->config_table.dl_diag_len2);
632 DBG3("dl_app1_len: 0x%04X, %d", dc->config_table.dl_app1_len,
633 dc->config_table.dl_app1_len);
634 DBG3("dl_app2_len: 0x%04X, %d", dc->config_table.dl_app2_len,
635 dc->config_table.dl_app2_len);
636 DBG3("dl_ctrl_len: 0x%04X, %d", dc->config_table.dl_ctrl_len,
637 dc->config_table.dl_ctrl_len);
638 DBG3("ul_start: 0x%04X, %d", dc->config_table.ul_start,
639 dc->config_table.ul_start);
640 DBG3("ul_mdm_len[0]: 0x%04X, %d", dc->config_table.ul_mdm_len1,
641 dc->config_table.ul_mdm_len1);
642 DBG3("ul_mdm_len[1]: 0x%04X, %d", dc->config_table.ul_mdm_len2,
643 dc->config_table.ul_mdm_len2);
644 DBG3("ul_diag_len: 0x%04X, %d", dc->config_table.ul_diag_len,
645 dc->config_table.ul_diag_len);
646 DBG3("ul_app1_len: 0x%04X, %d", dc->config_table.ul_app1_len,
647 dc->config_table.ul_app1_len);
648 DBG3("ul_app2_len: 0x%04X, %d", dc->config_table.ul_app2_len,
649 dc->config_table.ul_app2_len);
650 DBG3("ul_ctrl_len: 0x%04X, %d", dc->config_table.ul_ctrl_len,
651 dc->config_table.ul_ctrl_len);
652}
653#else
Frank Seidele9ed5372008-01-25 21:13:24 +0100654static inline void dump_table(const struct nozomi *dc) { }
Frank Seidel20fd1e32007-11-09 14:49:23 +0100655#endif
656
657/*
658 * Read configuration table from card under intalization phase
659 * Returns 1 if ok, else 0
660 */
661static int nozomi_read_config_table(struct nozomi *dc)
662{
663 read_mem32((u32 *) &dc->config_table, dc->base_addr + 0,
664 sizeof(struct config_table));
665
666 if (dc->config_table.signature != CONFIG_MAGIC) {
667 dev_err(&dc->pdev->dev, "ConfigTable Bad! 0x%08X != 0x%08X\n",
668 dc->config_table.signature, CONFIG_MAGIC);
669 return 0;
670 }
671
672 if ((dc->config_table.version == 0)
673 || (dc->config_table.toggle.enabled == TOGGLE_VALID)) {
674 int i;
675 DBG1("Second phase, configuring card");
676
677 setup_memory(dc);
678
679 dc->port[PORT_MDM].toggle_ul = dc->config_table.toggle.mdm_ul;
680 dc->port[PORT_MDM].toggle_dl = dc->config_table.toggle.mdm_dl;
681 dc->port[PORT_DIAG].toggle_dl = dc->config_table.toggle.diag_dl;
682 DBG1("toggle ports: MDM UL:%d MDM DL:%d, DIAG DL:%d",
683 dc->port[PORT_MDM].toggle_ul,
684 dc->port[PORT_MDM].toggle_dl, dc->port[PORT_DIAG].toggle_dl);
685
686 dump_table(dc);
687
688 for (i = PORT_MDM; i < MAX_PORT; i++) {
Frank Seidel20fd1e32007-11-09 14:49:23 +0100689 memset(&dc->port[i].ctrl_dl, 0, sizeof(struct ctrl_dl));
690 memset(&dc->port[i].ctrl_ul, 0, sizeof(struct ctrl_ul));
691 }
692
693 /* Enable control channel */
694 dc->last_ier = dc->last_ier | CTRL_DL;
695 writew(dc->last_ier, dc->reg_ier);
696
Frank Seidel661b4e82008-03-06 21:45:57 +0100697 dc->state = NOZOMI_STATE_ALLOCATED;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100698 dev_info(&dc->pdev->dev, "Initialization OK!\n");
699 return 1;
700 }
701
702 if ((dc->config_table.version > 0)
703 && (dc->config_table.toggle.enabled != TOGGLE_VALID)) {
704 u32 offset = 0;
705 DBG1("First phase: pushing upload buffers, clearing download");
706
707 dev_info(&dc->pdev->dev, "Version of card: %d\n",
708 dc->config_table.version);
709
710 /* Here we should disable all I/O over F32. */
711 setup_memory(dc);
712
713 /*
714 * We should send ALL channel pair tokens back along
715 * with reset token
716 */
717
718 /* push upload modem buffers */
719 write_mem32(dc->port[PORT_MDM].ul_addr[CH_A],
720 (u32 *) &offset, 4);
721 write_mem32(dc->port[PORT_MDM].ul_addr[CH_B],
722 (u32 *) &offset, 4);
723
724 writew(MDM_UL | DIAG_DL | MDM_DL, dc->reg_fcr);
725
726 DBG1("First phase done");
727 }
728
729 return 1;
730}
731
732/* Enable uplink interrupts */
733static void enable_transmit_ul(enum port_type port, struct nozomi *dc)
734{
Jan Engelhardt71e1b4a2008-02-01 09:13:53 +0100735 static const u16 mask[] = {MDM_UL, DIAG_UL, APP1_UL, APP2_UL, CTRL_UL};
Frank Seidel20fd1e32007-11-09 14:49:23 +0100736
737 if (port < NOZOMI_MAX_PORTS) {
738 dc->last_ier |= mask[port];
739 writew(dc->last_ier, dc->reg_ier);
740 } else {
741 dev_err(&dc->pdev->dev, "Called with wrong port?\n");
742 }
743}
744
745/* Disable uplink interrupts */
746static void disable_transmit_ul(enum port_type port, struct nozomi *dc)
747{
Jan Engelhardt71e1b4a2008-02-01 09:13:53 +0100748 static const u16 mask[] =
749 {~MDM_UL, ~DIAG_UL, ~APP1_UL, ~APP2_UL, ~CTRL_UL};
Frank Seidel20fd1e32007-11-09 14:49:23 +0100750
751 if (port < NOZOMI_MAX_PORTS) {
752 dc->last_ier &= mask[port];
753 writew(dc->last_ier, dc->reg_ier);
754 } else {
755 dev_err(&dc->pdev->dev, "Called with wrong port?\n");
756 }
757}
758
759/* Enable downlink interrupts */
760static void enable_transmit_dl(enum port_type port, struct nozomi *dc)
761{
Jan Engelhardt71e1b4a2008-02-01 09:13:53 +0100762 static const u16 mask[] = {MDM_DL, DIAG_DL, APP1_DL, APP2_DL, CTRL_DL};
Frank Seidel20fd1e32007-11-09 14:49:23 +0100763
764 if (port < NOZOMI_MAX_PORTS) {
765 dc->last_ier |= mask[port];
766 writew(dc->last_ier, dc->reg_ier);
767 } else {
768 dev_err(&dc->pdev->dev, "Called with wrong port?\n");
769 }
770}
771
772/* Disable downlink interrupts */
773static void disable_transmit_dl(enum port_type port, struct nozomi *dc)
774{
Jan Engelhardt71e1b4a2008-02-01 09:13:53 +0100775 static const u16 mask[] =
776 {~MDM_DL, ~DIAG_DL, ~APP1_DL, ~APP2_DL, ~CTRL_DL};
Frank Seidel20fd1e32007-11-09 14:49:23 +0100777
778 if (port < NOZOMI_MAX_PORTS) {
779 dc->last_ier &= mask[port];
780 writew(dc->last_ier, dc->reg_ier);
781 } else {
782 dev_err(&dc->pdev->dev, "Called with wrong port?\n");
783 }
784}
785
786/*
787 * Return 1 - send buffer to card and ack.
788 * Return 0 - don't ack, don't send buffer to card.
789 */
Alan Cox33dd4742009-01-02 13:47:32 +0000790static int send_data(enum port_type index, struct nozomi *dc)
Frank Seidel20fd1e32007-11-09 14:49:23 +0100791{
792 u32 size = 0;
Alan Cox33dd4742009-01-02 13:47:32 +0000793 struct port *port = &dc->port[index];
Frank Seidel18bbe0c22008-02-01 09:14:05 +0100794 const u8 toggle = port->toggle_ul;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100795 void __iomem *addr = port->ul_addr[toggle];
Frank Seidel18bbe0c22008-02-01 09:14:05 +0100796 const u32 ul_size = port->ul_size[toggle];
Alan Cox33dd4742009-01-02 13:47:32 +0000797 struct tty_struct *tty = tty_port_tty_get(&port->port);
Frank Seidel20fd1e32007-11-09 14:49:23 +0100798
799 /* Get data from tty and place in buf for now */
Stefani Seibold7acd72e2009-12-21 14:37:28 -0800800 size = kfifo_out(&port->fifo_ul, dc->send_buf,
Frank Seidel20fd1e32007-11-09 14:49:23 +0100801 ul_size < SEND_BUF_MAX ? ul_size : SEND_BUF_MAX);
802
803 if (size == 0) {
804 DBG4("No more data to send, disable link:");
Alan Cox33dd4742009-01-02 13:47:32 +0000805 tty_kref_put(tty);
Frank Seidel20fd1e32007-11-09 14:49:23 +0100806 return 0;
807 }
808
809 /* DUMP(buf, size); */
810
811 /* Write length + data */
812 write_mem32(addr, (u32 *) &size, 4);
813 write_mem32(addr + 4, (u32 *) dc->send_buf, size);
814
815 if (tty)
816 tty_wakeup(tty);
817
Alan Cox33dd4742009-01-02 13:47:32 +0000818 tty_kref_put(tty);
Frank Seidel20fd1e32007-11-09 14:49:23 +0100819 return 1;
820}
821
822/* If all data has been read, return 1, else 0 */
823static int receive_data(enum port_type index, struct nozomi *dc)
824{
825 u8 buf[RECEIVE_BUF_MAX] = { 0 };
826 int size;
827 u32 offset = 4;
828 struct port *port = &dc->port[index];
829 void __iomem *addr = port->dl_addr[port->toggle_dl];
Alan Cox33dd4742009-01-02 13:47:32 +0000830 struct tty_struct *tty = tty_port_tty_get(&port->port);
Jiri Slaby9237a812009-07-16 16:06:18 +0100831 int i, ret;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100832
833 if (unlikely(!tty)) {
834 DBG1("tty not open for port: %d?", index);
835 return 1;
836 }
837
838 read_mem32((u32 *) &size, addr, 4);
839 /* DBG1( "%d bytes port: %d", size, index); */
840
841 if (test_bit(TTY_THROTTLED, &tty->flags)) {
842 DBG1("No room in tty, don't read data, don't ack interrupt, "
843 "disable interrupt");
844
845 /* disable interrupt in downlink... */
846 disable_transmit_dl(index, dc);
Jiri Slaby9237a812009-07-16 16:06:18 +0100847 ret = 0;
848 goto put;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100849 }
850
851 if (unlikely(size == 0)) {
852 dev_err(&dc->pdev->dev, "size == 0?\n");
Jiri Slaby9237a812009-07-16 16:06:18 +0100853 ret = 1;
854 goto put;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100855 }
856
Frank Seidel20fd1e32007-11-09 14:49:23 +0100857 while (size > 0) {
858 read_mem32((u32 *) buf, addr + offset, RECEIVE_BUF_MAX);
859
860 if (size == 1) {
861 tty_insert_flip_char(tty, buf[0], TTY_NORMAL);
862 size = 0;
863 } else if (size < RECEIVE_BUF_MAX) {
864 size -= tty_insert_flip_string(tty, (char *) buf, size);
865 } else {
866 i = tty_insert_flip_string(tty, \
867 (char *) buf, RECEIVE_BUF_MAX);
868 size -= i;
869 offset += i;
870 }
871 }
872
873 set_bit(index, &dc->flip);
Jiri Slaby9237a812009-07-16 16:06:18 +0100874 ret = 1;
875put:
Alan Cox33dd4742009-01-02 13:47:32 +0000876 tty_kref_put(tty);
Jiri Slaby9237a812009-07-16 16:06:18 +0100877 return ret;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100878}
879
880/* Debug for interrupts */
881#ifdef DEBUG
882static char *interrupt2str(u16 interrupt)
883{
884 static char buf[TMP_BUF_MAX];
885 char *p = buf;
886
887 interrupt & MDM_DL1 ? p += snprintf(p, TMP_BUF_MAX, "MDM_DL1 ") : NULL;
888 interrupt & MDM_DL2 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
889 "MDM_DL2 ") : NULL;
890
891 interrupt & MDM_UL1 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
892 "MDM_UL1 ") : NULL;
893 interrupt & MDM_UL2 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
894 "MDM_UL2 ") : NULL;
895
896 interrupt & DIAG_DL1 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
897 "DIAG_DL1 ") : NULL;
898 interrupt & DIAG_DL2 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
899 "DIAG_DL2 ") : NULL;
900
901 interrupt & DIAG_UL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
902 "DIAG_UL ") : NULL;
903
904 interrupt & APP1_DL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
905 "APP1_DL ") : NULL;
906 interrupt & APP2_DL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
907 "APP2_DL ") : NULL;
908
909 interrupt & APP1_UL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
910 "APP1_UL ") : NULL;
911 interrupt & APP2_UL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
912 "APP2_UL ") : NULL;
913
914 interrupt & CTRL_DL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
915 "CTRL_DL ") : NULL;
916 interrupt & CTRL_UL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
917 "CTRL_UL ") : NULL;
918
919 interrupt & RESET ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
920 "RESET ") : NULL;
921
922 return buf;
923}
924#endif
925
926/*
927 * Receive flow control
928 * Return 1 - If ok, else 0
929 */
930static int receive_flow_control(struct nozomi *dc)
931{
932 enum port_type port = PORT_MDM;
933 struct ctrl_dl ctrl_dl;
934 struct ctrl_dl old_ctrl;
935 u16 enable_ier = 0;
936
937 read_mem32((u32 *) &ctrl_dl, dc->port[PORT_CTRL].dl_addr[CH_A], 2);
938
939 switch (ctrl_dl.port) {
940 case CTRL_CMD:
941 DBG1("The Base Band sends this value as a response to a "
942 "request for IMSI detach sent over the control "
943 "channel uplink (see section 7.6.1).");
944 break;
945 case CTRL_MDM:
946 port = PORT_MDM;
947 enable_ier = MDM_DL;
948 break;
949 case CTRL_DIAG:
950 port = PORT_DIAG;
951 enable_ier = DIAG_DL;
952 break;
953 case CTRL_APP1:
954 port = PORT_APP1;
955 enable_ier = APP1_DL;
956 break;
957 case CTRL_APP2:
958 port = PORT_APP2;
959 enable_ier = APP2_DL;
Frank Seidel661b4e82008-03-06 21:45:57 +0100960 if (dc->state == NOZOMI_STATE_ALLOCATED) {
961 /*
962 * After card initialization the flow control
963 * received for APP2 is always the last
964 */
965 dc->state = NOZOMI_STATE_READY;
966 dev_info(&dc->pdev->dev, "Device READY!\n");
967 }
Frank Seidel20fd1e32007-11-09 14:49:23 +0100968 break;
969 default:
970 dev_err(&dc->pdev->dev,
971 "ERROR: flow control received for non-existing port\n");
972 return 0;
973 };
974
975 DBG1("0x%04X->0x%04X", *((u16 *)&dc->port[port].ctrl_dl),
976 *((u16 *)&ctrl_dl));
977
978 old_ctrl = dc->port[port].ctrl_dl;
979 dc->port[port].ctrl_dl = ctrl_dl;
980
981 if (old_ctrl.CTS == 1 && ctrl_dl.CTS == 0) {
982 DBG1("Disable interrupt (0x%04X) on port: %d",
983 enable_ier, port);
984 disable_transmit_ul(port, dc);
985
986 } else if (old_ctrl.CTS == 0 && ctrl_dl.CTS == 1) {
987
Stefani Seibolde64c0262009-12-21 14:37:28 -0800988 if (kfifo_len(&dc->port[port].fifo_ul)) {
Frank Seidel20fd1e32007-11-09 14:49:23 +0100989 DBG1("Enable interrupt (0x%04X) on port: %d",
990 enable_ier, port);
991 DBG1("Data in buffer [%d], enable transmit! ",
Stefani Seibolde64c0262009-12-21 14:37:28 -0800992 kfifo_len(&dc->port[port].fifo_ul));
Frank Seidel20fd1e32007-11-09 14:49:23 +0100993 enable_transmit_ul(port, dc);
994 } else {
995 DBG1("No data in buffer...");
996 }
997 }
998
999 if (*(u16 *)&old_ctrl == *(u16 *)&ctrl_dl) {
1000 DBG1(" No change in mctrl");
1001 return 1;
1002 }
1003 /* Update statistics */
1004 if (old_ctrl.CTS != ctrl_dl.CTS)
1005 dc->port[port].tty_icount.cts++;
1006 if (old_ctrl.DSR != ctrl_dl.DSR)
1007 dc->port[port].tty_icount.dsr++;
1008 if (old_ctrl.RI != ctrl_dl.RI)
1009 dc->port[port].tty_icount.rng++;
1010 if (old_ctrl.DCD != ctrl_dl.DCD)
1011 dc->port[port].tty_icount.dcd++;
1012
1013 wake_up_interruptible(&dc->port[port].tty_wait);
1014
1015 DBG1("port: %d DCD(%d), CTS(%d), RI(%d), DSR(%d)",
1016 port,
1017 dc->port[port].tty_icount.dcd, dc->port[port].tty_icount.cts,
1018 dc->port[port].tty_icount.rng, dc->port[port].tty_icount.dsr);
1019
1020 return 1;
1021}
1022
1023static enum ctrl_port_type port2ctrl(enum port_type port,
1024 const struct nozomi *dc)
1025{
1026 switch (port) {
1027 case PORT_MDM:
1028 return CTRL_MDM;
1029 case PORT_DIAG:
1030 return CTRL_DIAG;
1031 case PORT_APP1:
1032 return CTRL_APP1;
1033 case PORT_APP2:
1034 return CTRL_APP2;
1035 default:
1036 dev_err(&dc->pdev->dev,
1037 "ERROR: send flow control " \
1038 "received for non-existing port\n");
1039 };
1040 return CTRL_ERROR;
1041}
1042
1043/*
1044 * Send flow control, can only update one channel at a time
1045 * Return 0 - If we have updated all flow control
1046 * Return 1 - If we need to update more flow control, ack current enable more
1047 */
1048static int send_flow_control(struct nozomi *dc)
1049{
1050 u32 i, more_flow_control_to_be_updated = 0;
1051 u16 *ctrl;
1052
1053 for (i = PORT_MDM; i < MAX_PORT; i++) {
1054 if (dc->port[i].update_flow_control) {
1055 if (more_flow_control_to_be_updated) {
1056 /* We have more flow control to be updated */
1057 return 1;
1058 }
1059 dc->port[i].ctrl_ul.port = port2ctrl(i, dc);
1060 ctrl = (u16 *)&dc->port[i].ctrl_ul;
1061 write_mem32(dc->port[PORT_CTRL].ul_addr[0], \
1062 (u32 *) ctrl, 2);
1063 dc->port[i].update_flow_control = 0;
1064 more_flow_control_to_be_updated = 1;
1065 }
1066 }
1067 return 0;
1068}
1069
1070/*
Frank Seidele9ed5372008-01-25 21:13:24 +01001071 * Handle downlink data, ports that are handled are modem and diagnostics
Frank Seidel20fd1e32007-11-09 14:49:23 +01001072 * Return 1 - ok
1073 * Return 0 - toggle fields are out of sync
1074 */
1075static int handle_data_dl(struct nozomi *dc, enum port_type port, u8 *toggle,
1076 u16 read_iir, u16 mask1, u16 mask2)
1077{
1078 if (*toggle == 0 && read_iir & mask1) {
1079 if (receive_data(port, dc)) {
1080 writew(mask1, dc->reg_fcr);
1081 *toggle = !(*toggle);
1082 }
1083
1084 if (read_iir & mask2) {
1085 if (receive_data(port, dc)) {
1086 writew(mask2, dc->reg_fcr);
1087 *toggle = !(*toggle);
1088 }
1089 }
1090 } else if (*toggle == 1 && read_iir & mask2) {
1091 if (receive_data(port, dc)) {
1092 writew(mask2, dc->reg_fcr);
1093 *toggle = !(*toggle);
1094 }
1095
1096 if (read_iir & mask1) {
1097 if (receive_data(port, dc)) {
1098 writew(mask1, dc->reg_fcr);
1099 *toggle = !(*toggle);
1100 }
1101 }
1102 } else {
1103 dev_err(&dc->pdev->dev, "port out of sync!, toggle:%d\n",
1104 *toggle);
1105 return 0;
1106 }
1107 return 1;
1108}
1109
1110/*
1111 * Handle uplink data, this is currently for the modem port
1112 * Return 1 - ok
1113 * Return 0 - toggle field are out of sync
1114 */
1115static int handle_data_ul(struct nozomi *dc, enum port_type port, u16 read_iir)
1116{
1117 u8 *toggle = &(dc->port[port].toggle_ul);
1118
1119 if (*toggle == 0 && read_iir & MDM_UL1) {
1120 dc->last_ier &= ~MDM_UL;
1121 writew(dc->last_ier, dc->reg_ier);
1122 if (send_data(port, dc)) {
1123 writew(MDM_UL1, dc->reg_fcr);
1124 dc->last_ier = dc->last_ier | MDM_UL;
1125 writew(dc->last_ier, dc->reg_ier);
1126 *toggle = !*toggle;
1127 }
1128
1129 if (read_iir & MDM_UL2) {
1130 dc->last_ier &= ~MDM_UL;
1131 writew(dc->last_ier, dc->reg_ier);
1132 if (send_data(port, dc)) {
1133 writew(MDM_UL2, dc->reg_fcr);
1134 dc->last_ier = dc->last_ier | MDM_UL;
1135 writew(dc->last_ier, dc->reg_ier);
1136 *toggle = !*toggle;
1137 }
1138 }
1139
1140 } else if (*toggle == 1 && read_iir & MDM_UL2) {
1141 dc->last_ier &= ~MDM_UL;
1142 writew(dc->last_ier, dc->reg_ier);
1143 if (send_data(port, dc)) {
1144 writew(MDM_UL2, dc->reg_fcr);
1145 dc->last_ier = dc->last_ier | MDM_UL;
1146 writew(dc->last_ier, dc->reg_ier);
1147 *toggle = !*toggle;
1148 }
1149
1150 if (read_iir & MDM_UL1) {
1151 dc->last_ier &= ~MDM_UL;
1152 writew(dc->last_ier, dc->reg_ier);
1153 if (send_data(port, dc)) {
1154 writew(MDM_UL1, dc->reg_fcr);
1155 dc->last_ier = dc->last_ier | MDM_UL;
1156 writew(dc->last_ier, dc->reg_ier);
1157 *toggle = !*toggle;
1158 }
1159 }
1160 } else {
1161 writew(read_iir & MDM_UL, dc->reg_fcr);
1162 dev_err(&dc->pdev->dev, "port out of sync!\n");
1163 return 0;
1164 }
1165 return 1;
1166}
1167
1168static irqreturn_t interrupt_handler(int irq, void *dev_id)
1169{
1170 struct nozomi *dc = dev_id;
1171 unsigned int a;
1172 u16 read_iir;
1173
1174 if (!dc)
1175 return IRQ_NONE;
1176
1177 spin_lock(&dc->spin_mutex);
1178 read_iir = readw(dc->reg_iir);
1179
1180 /* Card removed */
1181 if (read_iir == (u16)-1)
1182 goto none;
1183 /*
1184 * Just handle interrupt enabled in IER
1185 * (by masking with dc->last_ier)
1186 */
1187 read_iir &= dc->last_ier;
1188
1189 if (read_iir == 0)
1190 goto none;
1191
1192
1193 DBG4("%s irq:0x%04X, prev:0x%04X", interrupt2str(read_iir), read_iir,
1194 dc->last_ier);
1195
1196 if (read_iir & RESET) {
1197 if (unlikely(!nozomi_read_config_table(dc))) {
1198 dc->last_ier = 0x0;
1199 writew(dc->last_ier, dc->reg_ier);
1200 dev_err(&dc->pdev->dev, "Could not read status from "
1201 "card, we should disable interface\n");
1202 } else {
1203 writew(RESET, dc->reg_fcr);
1204 }
1205 /* No more useful info if this was the reset interrupt. */
1206 goto exit_handler;
1207 }
1208 if (read_iir & CTRL_UL) {
1209 DBG1("CTRL_UL");
1210 dc->last_ier &= ~CTRL_UL;
1211 writew(dc->last_ier, dc->reg_ier);
1212 if (send_flow_control(dc)) {
1213 writew(CTRL_UL, dc->reg_fcr);
1214 dc->last_ier = dc->last_ier | CTRL_UL;
1215 writew(dc->last_ier, dc->reg_ier);
1216 }
1217 }
1218 if (read_iir & CTRL_DL) {
1219 receive_flow_control(dc);
1220 writew(CTRL_DL, dc->reg_fcr);
1221 }
1222 if (read_iir & MDM_DL) {
1223 if (!handle_data_dl(dc, PORT_MDM,
1224 &(dc->port[PORT_MDM].toggle_dl), read_iir,
1225 MDM_DL1, MDM_DL2)) {
1226 dev_err(&dc->pdev->dev, "MDM_DL out of sync!\n");
1227 goto exit_handler;
1228 }
1229 }
1230 if (read_iir & MDM_UL) {
1231 if (!handle_data_ul(dc, PORT_MDM, read_iir)) {
1232 dev_err(&dc->pdev->dev, "MDM_UL out of sync!\n");
1233 goto exit_handler;
1234 }
1235 }
1236 if (read_iir & DIAG_DL) {
1237 if (!handle_data_dl(dc, PORT_DIAG,
1238 &(dc->port[PORT_DIAG].toggle_dl), read_iir,
1239 DIAG_DL1, DIAG_DL2)) {
1240 dev_err(&dc->pdev->dev, "DIAG_DL out of sync!\n");
1241 goto exit_handler;
1242 }
1243 }
1244 if (read_iir & DIAG_UL) {
1245 dc->last_ier &= ~DIAG_UL;
1246 writew(dc->last_ier, dc->reg_ier);
1247 if (send_data(PORT_DIAG, dc)) {
1248 writew(DIAG_UL, dc->reg_fcr);
1249 dc->last_ier = dc->last_ier | DIAG_UL;
1250 writew(dc->last_ier, dc->reg_ier);
1251 }
1252 }
1253 if (read_iir & APP1_DL) {
1254 if (receive_data(PORT_APP1, dc))
1255 writew(APP1_DL, dc->reg_fcr);
1256 }
1257 if (read_iir & APP1_UL) {
1258 dc->last_ier &= ~APP1_UL;
1259 writew(dc->last_ier, dc->reg_ier);
1260 if (send_data(PORT_APP1, dc)) {
1261 writew(APP1_UL, dc->reg_fcr);
1262 dc->last_ier = dc->last_ier | APP1_UL;
1263 writew(dc->last_ier, dc->reg_ier);
1264 }
1265 }
1266 if (read_iir & APP2_DL) {
1267 if (receive_data(PORT_APP2, dc))
1268 writew(APP2_DL, dc->reg_fcr);
1269 }
1270 if (read_iir & APP2_UL) {
1271 dc->last_ier &= ~APP2_UL;
1272 writew(dc->last_ier, dc->reg_ier);
1273 if (send_data(PORT_APP2, dc)) {
1274 writew(APP2_UL, dc->reg_fcr);
1275 dc->last_ier = dc->last_ier | APP2_UL;
1276 writew(dc->last_ier, dc->reg_ier);
1277 }
1278 }
1279
1280exit_handler:
1281 spin_unlock(&dc->spin_mutex);
Alan Cox33dd4742009-01-02 13:47:32 +00001282 for (a = 0; a < NOZOMI_MAX_PORTS; a++) {
1283 struct tty_struct *tty;
1284 if (test_and_clear_bit(a, &dc->flip)) {
1285 tty = tty_port_tty_get(&dc->port[a].port);
1286 if (tty)
1287 tty_flip_buffer_push(tty);
1288 tty_kref_put(tty);
1289 }
1290 }
Frank Seidel20fd1e32007-11-09 14:49:23 +01001291 return IRQ_HANDLED;
1292none:
1293 spin_unlock(&dc->spin_mutex);
1294 return IRQ_NONE;
1295}
1296
1297static void nozomi_get_card_type(struct nozomi *dc)
1298{
1299 int i;
1300 u32 size = 0;
1301
1302 for (i = 0; i < 6; i++)
1303 size += pci_resource_len(dc->pdev, i);
1304
1305 /* Assume card type F32_8 if no match */
1306 dc->card_type = size == 2048 ? F32_2 : F32_8;
1307
1308 dev_info(&dc->pdev->dev, "Card type is: %d\n", dc->card_type);
1309}
1310
1311static void nozomi_setup_private_data(struct nozomi *dc)
1312{
1313 void __iomem *offset = dc->base_addr + dc->card_type / 2;
1314 unsigned int i;
1315
1316 dc->reg_fcr = (void __iomem *)(offset + R_FCR);
1317 dc->reg_iir = (void __iomem *)(offset + R_IIR);
1318 dc->reg_ier = (void __iomem *)(offset + R_IER);
1319 dc->last_ier = 0;
1320 dc->flip = 0;
1321
1322 dc->port[PORT_MDM].token_dl = MDM_DL;
1323 dc->port[PORT_DIAG].token_dl = DIAG_DL;
1324 dc->port[PORT_APP1].token_dl = APP1_DL;
1325 dc->port[PORT_APP2].token_dl = APP2_DL;
1326
1327 for (i = 0; i < MAX_PORT; i++)
1328 init_waitqueue_head(&dc->port[i].tty_wait);
1329}
1330
1331static ssize_t card_type_show(struct device *dev, struct device_attribute *attr,
1332 char *buf)
1333{
Jan Engelhardt71e1b4a2008-02-01 09:13:53 +01001334 const struct nozomi *dc = pci_get_drvdata(to_pci_dev(dev));
Frank Seidel20fd1e32007-11-09 14:49:23 +01001335
1336 return sprintf(buf, "%d\n", dc->card_type);
1337}
Frank Seidele9ed5372008-01-25 21:13:24 +01001338static DEVICE_ATTR(card_type, S_IRUGO, card_type_show, NULL);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001339
1340static ssize_t open_ttys_show(struct device *dev, struct device_attribute *attr,
1341 char *buf)
1342{
Jan Engelhardt71e1b4a2008-02-01 09:13:53 +01001343 const struct nozomi *dc = pci_get_drvdata(to_pci_dev(dev));
Frank Seidel20fd1e32007-11-09 14:49:23 +01001344
1345 return sprintf(buf, "%u\n", dc->open_ttys);
1346}
Frank Seidele9ed5372008-01-25 21:13:24 +01001347static DEVICE_ATTR(open_ttys, S_IRUGO, open_ttys_show, NULL);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001348
1349static void make_sysfs_files(struct nozomi *dc)
1350{
1351 if (device_create_file(&dc->pdev->dev, &dev_attr_card_type))
1352 dev_err(&dc->pdev->dev,
1353 "Could not create sysfs file for card_type\n");
1354 if (device_create_file(&dc->pdev->dev, &dev_attr_open_ttys))
1355 dev_err(&dc->pdev->dev,
1356 "Could not create sysfs file for open_ttys\n");
1357}
1358
1359static void remove_sysfs_files(struct nozomi *dc)
1360{
1361 device_remove_file(&dc->pdev->dev, &dev_attr_card_type);
1362 device_remove_file(&dc->pdev->dev, &dev_attr_open_ttys);
1363}
1364
1365/* Allocate memory for one device */
1366static int __devinit nozomi_card_init(struct pci_dev *pdev,
1367 const struct pci_device_id *ent)
1368{
1369 resource_size_t start;
1370 int ret;
1371 struct nozomi *dc = NULL;
1372 int ndev_idx;
1373 int i;
1374
1375 dev_dbg(&pdev->dev, "Init, new card found\n");
1376
1377 for (ndev_idx = 0; ndev_idx < ARRAY_SIZE(ndevs); ndev_idx++)
1378 if (!ndevs[ndev_idx])
1379 break;
1380
1381 if (ndev_idx >= ARRAY_SIZE(ndevs)) {
1382 dev_err(&pdev->dev, "no free tty range for this card left\n");
1383 ret = -EIO;
1384 goto err;
1385 }
1386
1387 dc = kzalloc(sizeof(struct nozomi), GFP_KERNEL);
1388 if (unlikely(!dc)) {
1389 dev_err(&pdev->dev, "Could not allocate memory\n");
1390 ret = -ENOMEM;
1391 goto err_free;
1392 }
1393
1394 dc->pdev = pdev;
1395
Frank Seidel20fd1e32007-11-09 14:49:23 +01001396 ret = pci_enable_device(dc->pdev);
1397 if (ret) {
1398 dev_err(&pdev->dev, "Failed to enable PCI Device\n");
1399 goto err_free;
1400 }
1401
Frank Seidel20fd1e32007-11-09 14:49:23 +01001402 ret = pci_request_regions(dc->pdev, NOZOMI_NAME);
1403 if (ret) {
1404 dev_err(&pdev->dev, "I/O address 0x%04x already in use\n",
1405 (int) /* nozomi_private.io_addr */ 0);
1406 goto err_disable_device;
1407 }
1408
Frank Seidel661b4e82008-03-06 21:45:57 +01001409 start = pci_resource_start(dc->pdev, 0);
1410 if (start == 0) {
1411 dev_err(&pdev->dev, "No I/O address for card detected\n");
1412 ret = -ENODEV;
1413 goto err_rel_regs;
1414 }
1415
1416 /* Find out what card type it is */
1417 nozomi_get_card_type(dc);
1418
Alan Cox24cb2332008-04-30 00:54:19 -07001419 dc->base_addr = ioremap_nocache(start, dc->card_type);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001420 if (!dc->base_addr) {
1421 dev_err(&pdev->dev, "Unable to map card MMIO\n");
1422 ret = -ENODEV;
1423 goto err_rel_regs;
1424 }
1425
1426 dc->send_buf = kmalloc(SEND_BUF_MAX, GFP_KERNEL);
1427 if (!dc->send_buf) {
1428 dev_err(&pdev->dev, "Could not allocate send buffer?\n");
1429 ret = -ENOMEM;
1430 goto err_free_sbuf;
1431 }
1432
Stefani Seibold9842c382009-12-21 14:37:29 -08001433 for (i = PORT_MDM; i < MAX_PORT; i++) {
1434 if (kfifo_alloc(&dc->port[i].fifo_ul,
1435 FIFO_BUFFER_SIZE_UL, GFP_ATOMIC)) {
1436 dev_err(&pdev->dev,
1437 "Could not allocate kfifo buffer\n");
1438 ret = -ENOMEM;
1439 goto err_free_kfifo;
1440 }
1441 }
1442
Frank Seidel20fd1e32007-11-09 14:49:23 +01001443 spin_lock_init(&dc->spin_mutex);
1444
1445 nozomi_setup_private_data(dc);
1446
1447 /* Disable all interrupts */
1448 dc->last_ier = 0;
1449 writew(dc->last_ier, dc->reg_ier);
1450
1451 ret = request_irq(pdev->irq, &interrupt_handler, IRQF_SHARED,
1452 NOZOMI_NAME, dc);
1453 if (unlikely(ret)) {
1454 dev_err(&pdev->dev, "can't request irq %d\n", pdev->irq);
Stefani Seibold9842c382009-12-21 14:37:29 -08001455 goto err_free_kfifo;
Frank Seidel20fd1e32007-11-09 14:49:23 +01001456 }
1457
1458 DBG1("base_addr: %p", dc->base_addr);
1459
1460 make_sysfs_files(dc);
1461
1462 dc->index_start = ndev_idx * MAX_PORT;
1463 ndevs[ndev_idx] = dc;
1464
Frank Seidel661b4e82008-03-06 21:45:57 +01001465 pci_set_drvdata(pdev, dc);
1466
1467 /* Enable RESET interrupt */
1468 dc->last_ier = RESET;
1469 iowrite16(dc->last_ier, dc->reg_ier);
1470
1471 dc->state = NOZOMI_STATE_ENABLED;
1472
Frank Seidel20fd1e32007-11-09 14:49:23 +01001473 for (i = 0; i < MAX_PORT; i++) {
Stefani Seibold9842c382009-12-21 14:37:29 -08001474 struct device *tty_dev;
Alan Cox266794e2010-02-08 10:06:45 +00001475 struct port *port = &dc->port[i];
1476 port->dc = dc;
1477 mutex_init(&port->tty_sem);
1478 tty_port_init(&port->port);
1479 port->port.ops = &noz_tty_port_ops;
Stefani Seibold9842c382009-12-21 14:37:29 -08001480 tty_dev = tty_register_device(ntty_driver, dc->index_start + i,
Frank Seidel20fd1e32007-11-09 14:49:23 +01001481 &pdev->dev);
Stefani Seibold9842c382009-12-21 14:37:29 -08001482
1483 if (IS_ERR(tty_dev)) {
1484 ret = PTR_ERR(tty_dev);
1485 dev_err(&pdev->dev, "Could not allocate tty?\n");
1486 goto err_free_tty;
1487 }
Frank Seidel20fd1e32007-11-09 14:49:23 +01001488 }
Stefani Seibold9842c382009-12-21 14:37:29 -08001489
Frank Seidel20fd1e32007-11-09 14:49:23 +01001490 return 0;
1491
Stefani Seibold9842c382009-12-21 14:37:29 -08001492err_free_tty:
1493 for (i = dc->index_start; i < dc->index_start + MAX_PORT; ++i)
1494 tty_unregister_device(ntty_driver, i);
1495err_free_kfifo:
1496 for (i = 0; i < MAX_PORT; i++)
1497 kfifo_free(&dc->port[i].fifo_ul);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001498err_free_sbuf:
1499 kfree(dc->send_buf);
1500 iounmap(dc->base_addr);
1501err_rel_regs:
1502 pci_release_regions(pdev);
1503err_disable_device:
1504 pci_disable_device(pdev);
1505err_free:
1506 kfree(dc);
1507err:
1508 return ret;
1509}
1510
1511static void __devexit tty_exit(struct nozomi *dc)
1512{
1513 unsigned int i;
1514
1515 DBG1(" ");
1516
1517 flush_scheduled_work();
1518
Alan Cox33dd4742009-01-02 13:47:32 +00001519 for (i = 0; i < MAX_PORT; ++i) {
1520 struct tty_struct *tty = tty_port_tty_get(&dc->port[i].port);
1521 if (tty && list_empty(&tty->hangup_work.entry))
1522 tty_hangup(tty);
1523 tty_kref_put(tty);
1524 }
1525 /* Racy below - surely should wait for scheduled work to be done or
1526 complete off a hangup method ? */
Frank Seidel20fd1e32007-11-09 14:49:23 +01001527 while (dc->open_ttys)
1528 msleep(1);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001529 for (i = dc->index_start; i < dc->index_start + MAX_PORT; ++i)
1530 tty_unregister_device(ntty_driver, i);
1531}
1532
1533/* Deallocate memory for one device */
1534static void __devexit nozomi_card_exit(struct pci_dev *pdev)
1535{
1536 int i;
1537 struct ctrl_ul ctrl;
1538 struct nozomi *dc = pci_get_drvdata(pdev);
1539
1540 /* Disable all interrupts */
1541 dc->last_ier = 0;
1542 writew(dc->last_ier, dc->reg_ier);
1543
1544 tty_exit(dc);
1545
1546 /* Send 0x0001, command card to resend the reset token. */
1547 /* This is to get the reset when the module is reloaded. */
1548 ctrl.port = 0x00;
1549 ctrl.reserved = 0;
1550 ctrl.RTS = 0;
1551 ctrl.DTR = 1;
1552 DBG1("sending flow control 0x%04X", *((u16 *)&ctrl));
1553
1554 /* Setup dc->reg addresses to we can use defines here */
1555 write_mem32(dc->port[PORT_CTRL].ul_addr[0], (u32 *)&ctrl, 2);
1556 writew(CTRL_UL, dc->reg_fcr); /* push the token to the card. */
1557
1558 remove_sysfs_files(dc);
1559
1560 free_irq(pdev->irq, dc);
1561
1562 for (i = 0; i < MAX_PORT; i++)
Stefani Seibold45465482009-12-21 14:37:26 -08001563 kfifo_free(&dc->port[i].fifo_ul);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001564
1565 kfree(dc->send_buf);
1566
1567 iounmap(dc->base_addr);
1568
1569 pci_release_regions(pdev);
1570
1571 pci_disable_device(pdev);
1572
1573 ndevs[dc->index_start / MAX_PORT] = NULL;
1574
1575 kfree(dc);
1576}
1577
1578static void set_rts(const struct tty_struct *tty, int rts)
1579{
1580 struct port *port = get_port_by_tty(tty);
1581
1582 port->ctrl_ul.RTS = rts;
1583 port->update_flow_control = 1;
1584 enable_transmit_ul(PORT_CTRL, get_dc_by_tty(tty));
1585}
1586
1587static void set_dtr(const struct tty_struct *tty, int dtr)
1588{
1589 struct port *port = get_port_by_tty(tty);
1590
1591 DBG1("SETTING DTR index: %d, dtr: %d", tty->index, dtr);
1592
1593 port->ctrl_ul.DTR = dtr;
1594 port->update_flow_control = 1;
1595 enable_transmit_ul(PORT_CTRL, get_dc_by_tty(tty));
1596}
1597
1598/*
1599 * ----------------------------------------------------------------------------
1600 * TTY code
1601 * ----------------------------------------------------------------------------
1602 */
1603
Alan Cox266794e2010-02-08 10:06:45 +00001604static int ntty_install(struct tty_driver *driver, struct tty_struct *tty)
Frank Seidel20fd1e32007-11-09 14:49:23 +01001605{
1606 struct port *port = get_port_by_tty(tty);
1607 struct nozomi *dc = get_dc_by_tty(tty);
Alan Cox266794e2010-02-08 10:06:45 +00001608 int ret;
Frank Seidel661b4e82008-03-06 21:45:57 +01001609 if (!port || !dc || dc->state != NOZOMI_STATE_READY)
Frank Seidel20fd1e32007-11-09 14:49:23 +01001610 return -ENODEV;
Alan Cox266794e2010-02-08 10:06:45 +00001611 ret = tty_init_termios(tty);
1612 if (ret == 0) {
1613 tty_driver_kref_get(driver);
Jiri Slabyee78bb92010-07-29 11:09:47 +02001614 tty->count++;
Jiri Slabybf9c1fc2010-07-29 11:46:32 +02001615 tty->driver_data = port;
Alan Cox266794e2010-02-08 10:06:45 +00001616 driver->ttys[tty->index] = tty;
Frank Seidel20fd1e32007-11-09 14:49:23 +01001617 }
Alan Cox266794e2010-02-08 10:06:45 +00001618 return ret;
1619}
1620
1621static void ntty_cleanup(struct tty_struct *tty)
1622{
1623 tty->driver_data = NULL;
1624}
1625
1626static int ntty_activate(struct tty_port *tport, struct tty_struct *tty)
1627{
1628 struct port *port = container_of(tport, struct port, port);
1629 struct nozomi *dc = port->dc;
1630 unsigned long flags;
1631
1632 DBG1("open: %d", port->token_dl);
1633 spin_lock_irqsave(&dc->spin_mutex, flags);
1634 dc->last_ier = dc->last_ier | port->token_dl;
1635 writew(dc->last_ier, dc->reg_ier);
1636 dc->open_ttys++;
1637 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1638 printk("noz: activated %d: %p\n", tty->index, tport);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001639 return 0;
1640}
1641
Alan Cox266794e2010-02-08 10:06:45 +00001642static int ntty_open(struct tty_struct *tty, struct file *filp)
Frank Seidel20fd1e32007-11-09 14:49:23 +01001643{
Jiri Slabybf9c1fc2010-07-29 11:46:32 +02001644 struct port *port = tty->driver_data;
Alan Cox266794e2010-02-08 10:06:45 +00001645 return tty_port_open(&port->port, tty, filp);
1646}
1647
1648static void ntty_shutdown(struct tty_port *tport)
1649{
1650 struct port *port = container_of(tport, struct port, port);
1651 struct nozomi *dc = port->dc;
Frank Seidel20fd1e32007-11-09 14:49:23 +01001652 unsigned long flags;
1653
Alan Cox266794e2010-02-08 10:06:45 +00001654 DBG1("close: %d", port->token_dl);
1655 spin_lock_irqsave(&dc->spin_mutex, flags);
1656 dc->last_ier &= ~(port->token_dl);
1657 writew(dc->last_ier, dc->reg_ier);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001658 dc->open_ttys--;
Alan Cox266794e2010-02-08 10:06:45 +00001659 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1660 printk("noz: shutdown %p\n", tport);
1661}
Frank Seidel20fd1e32007-11-09 14:49:23 +01001662
Alan Cox266794e2010-02-08 10:06:45 +00001663static void ntty_close(struct tty_struct *tty, struct file *filp)
1664{
1665 struct port *port = tty->driver_data;
1666 if (port)
1667 tty_port_close(&port->port, tty, filp);
1668}
1669
1670static void ntty_hangup(struct tty_struct *tty)
1671{
1672 struct port *port = tty->driver_data;
1673 tty_port_hangup(&port->port);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001674}
1675
1676/*
1677 * called when the userspace process writes to the tty (/dev/noz*).
1678 * Data is inserted into a fifo, which is then read and transfered to the modem.
1679 */
1680static int ntty_write(struct tty_struct *tty, const unsigned char *buffer,
1681 int count)
1682{
1683 int rval = -EINVAL;
1684 struct nozomi *dc = get_dc_by_tty(tty);
1685 struct port *port = tty->driver_data;
1686 unsigned long flags;
1687
1688 /* DBG1( "WRITEx: %d, index = %d", count, index); */
1689
1690 if (!dc || !port)
1691 return -ENODEV;
1692
Alan Coxe8c65d12010-02-08 10:07:04 +00001693 mutex_lock(&port->tty_sem);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001694
Alan Cox33dd4742009-01-02 13:47:32 +00001695 if (unlikely(!port->port.count)) {
Frank Seidel20fd1e32007-11-09 14:49:23 +01001696 DBG1(" ");
1697 goto exit;
1698 }
1699
Stefani Seibold7acd72e2009-12-21 14:37:28 -08001700 rval = kfifo_in(&port->fifo_ul, (unsigned char *)buffer, count);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001701
1702 /* notify card */
1703 if (unlikely(dc == NULL)) {
1704 DBG1("No device context?");
1705 goto exit;
1706 }
1707
1708 spin_lock_irqsave(&dc->spin_mutex, flags);
1709 /* CTS is only valid on the modem channel */
1710 if (port == &(dc->port[PORT_MDM])) {
1711 if (port->ctrl_dl.CTS) {
1712 DBG4("Enable interrupt");
1713 enable_transmit_ul(tty->index % MAX_PORT, dc);
1714 } else {
1715 dev_err(&dc->pdev->dev,
1716 "CTS not active on modem port?\n");
1717 }
1718 } else {
1719 enable_transmit_ul(tty->index % MAX_PORT, dc);
1720 }
1721 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1722
1723exit:
1724 mutex_unlock(&port->tty_sem);
1725 return rval;
1726}
1727
1728/*
1729 * Calculate how much is left in device
1730 * This method is called by the upper tty layer.
1731 * #according to sources N_TTY.c it expects a value >= 0 and
1732 * does not check for negative values.
Alan Coxe8c65d12010-02-08 10:07:04 +00001733 *
1734 * If the port is unplugged report lots of room and let the bits
1735 * dribble away so we don't block anything.
Frank Seidel20fd1e32007-11-09 14:49:23 +01001736 */
1737static int ntty_write_room(struct tty_struct *tty)
1738{
1739 struct port *port = tty->driver_data;
Alan Coxe8c65d12010-02-08 10:07:04 +00001740 int room = 4096;
Jan Engelhardt71e1b4a2008-02-01 09:13:53 +01001741 const struct nozomi *dc = get_dc_by_tty(tty);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001742
Alan Coxe8c65d12010-02-08 10:07:04 +00001743 if (dc) {
1744 mutex_lock(&port->tty_sem);
1745 if (port->port.count)
Stefani Seibold4457d982010-08-10 18:03:37 -07001746 room = kfifo_avail(&port->fifo_ul);
Alan Coxe8c65d12010-02-08 10:07:04 +00001747 mutex_unlock(&port->tty_sem);
1748 }
Frank Seidel20fd1e32007-11-09 14:49:23 +01001749 return room;
1750}
1751
1752/* Gets io control parameters */
Alan Cox60b33c12011-02-14 16:26:14 +00001753static int ntty_tiocmget(struct tty_struct *tty)
Frank Seidel20fd1e32007-11-09 14:49:23 +01001754{
Jan Engelhardt71e1b4a2008-02-01 09:13:53 +01001755 const struct port *port = tty->driver_data;
1756 const struct ctrl_dl *ctrl_dl = &port->ctrl_dl;
1757 const struct ctrl_ul *ctrl_ul = &port->ctrl_ul;
Frank Seidel20fd1e32007-11-09 14:49:23 +01001758
Alan Cox978e5952008-04-30 00:53:59 -07001759 /* Note: these could change under us but it is not clear this
1760 matters if so */
Frank Seidel20fd1e32007-11-09 14:49:23 +01001761 return (ctrl_ul->RTS ? TIOCM_RTS : 0) |
1762 (ctrl_ul->DTR ? TIOCM_DTR : 0) |
1763 (ctrl_dl->DCD ? TIOCM_CAR : 0) |
1764 (ctrl_dl->RI ? TIOCM_RNG : 0) |
1765 (ctrl_dl->DSR ? TIOCM_DSR : 0) |
1766 (ctrl_dl->CTS ? TIOCM_CTS : 0);
1767}
1768
1769/* Sets io controls parameters */
Alan Cox20b9d172011-02-14 16:26:50 +00001770static int ntty_tiocmset(struct tty_struct *tty,
1771 unsigned int set, unsigned int clear)
Frank Seidel20fd1e32007-11-09 14:49:23 +01001772{
Frank Seidel661b4e82008-03-06 21:45:57 +01001773 struct nozomi *dc = get_dc_by_tty(tty);
1774 unsigned long flags;
1775
1776 spin_lock_irqsave(&dc->spin_mutex, flags);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001777 if (set & TIOCM_RTS)
1778 set_rts(tty, 1);
1779 else if (clear & TIOCM_RTS)
1780 set_rts(tty, 0);
1781
1782 if (set & TIOCM_DTR)
1783 set_dtr(tty, 1);
1784 else if (clear & TIOCM_DTR)
1785 set_dtr(tty, 0);
Frank Seidel661b4e82008-03-06 21:45:57 +01001786 spin_unlock_irqrestore(&dc->spin_mutex, flags);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001787
1788 return 0;
1789}
1790
1791static int ntty_cflags_changed(struct port *port, unsigned long flags,
1792 struct async_icount *cprev)
1793{
Frank Seidel18bbe0c22008-02-01 09:14:05 +01001794 const struct async_icount cnow = port->tty_icount;
Frank Seidel20fd1e32007-11-09 14:49:23 +01001795 int ret;
1796
1797 ret = ((flags & TIOCM_RNG) && (cnow.rng != cprev->rng)) ||
1798 ((flags & TIOCM_DSR) && (cnow.dsr != cprev->dsr)) ||
1799 ((flags & TIOCM_CD) && (cnow.dcd != cprev->dcd)) ||
1800 ((flags & TIOCM_CTS) && (cnow.cts != cprev->cts));
1801
1802 *cprev = cnow;
1803
1804 return ret;
1805}
1806
Alan Cox05871022010-09-16 18:21:52 +01001807static int ntty_tiocgicount(struct tty_struct *tty,
1808 struct serial_icounter_struct *icount)
Frank Seidel20fd1e32007-11-09 14:49:23 +01001809{
Alan Cox05871022010-09-16 18:21:52 +01001810 struct port *port = tty->driver_data;
Frank Seidel18bbe0c22008-02-01 09:14:05 +01001811 const struct async_icount cnow = port->tty_icount;
Frank Seidel20fd1e32007-11-09 14:49:23 +01001812
Alan Cox05871022010-09-16 18:21:52 +01001813 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 return 0;
Frank Seidel20fd1e32007-11-09 14:49:23 +01001825}
1826
1827static int ntty_ioctl(struct tty_struct *tty, struct file *file,
1828 unsigned int cmd, unsigned long arg)
1829{
1830 struct port *port = tty->driver_data;
Frank Seidel20fd1e32007-11-09 14:49:23 +01001831 int rval = -ENOIOCTLCMD;
1832
1833 DBG1("******** IOCTL, cmd: %d", cmd);
1834
1835 switch (cmd) {
1836 case TIOCMIWAIT: {
1837 struct async_icount cprev = port->tty_icount;
1838
1839 rval = wait_event_interruptible(port->tty_wait,
1840 ntty_cflags_changed(port, arg, &cprev));
1841 break;
Alan Cox05871022010-09-16 18:21:52 +01001842 }
Frank Seidel20fd1e32007-11-09 14:49:23 +01001843 default:
1844 DBG1("ERR: 0x%08X, %d", cmd, cmd);
1845 break;
1846 };
1847
1848 return rval;
1849}
1850
1851/*
1852 * Called by the upper tty layer when tty buffers are ready
1853 * to receive data again after a call to throttle.
1854 */
1855static void ntty_unthrottle(struct tty_struct *tty)
1856{
1857 struct nozomi *dc = get_dc_by_tty(tty);
1858 unsigned long flags;
1859
1860 DBG1("UNTHROTTLE");
1861 spin_lock_irqsave(&dc->spin_mutex, flags);
1862 enable_transmit_dl(tty->index % MAX_PORT, dc);
1863 set_rts(tty, 1);
1864
1865 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1866}
1867
1868/*
1869 * Called by the upper tty layer when the tty buffers are almost full.
1870 * The driver should stop send more data.
1871 */
1872static void ntty_throttle(struct tty_struct *tty)
1873{
1874 struct nozomi *dc = get_dc_by_tty(tty);
1875 unsigned long flags;
1876
1877 DBG1("THROTTLE");
1878 spin_lock_irqsave(&dc->spin_mutex, flags);
1879 set_rts(tty, 0);
1880 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1881}
1882
Frank Seidel20fd1e32007-11-09 14:49:23 +01001883/* Returns number of chars in buffer, called by tty layer */
1884static s32 ntty_chars_in_buffer(struct tty_struct *tty)
1885{
1886 struct port *port = tty->driver_data;
1887 struct nozomi *dc = get_dc_by_tty(tty);
Alan Cox23198fd2009-07-20 16:05:27 +01001888 s32 rval = 0;
Frank Seidel20fd1e32007-11-09 14:49:23 +01001889
1890 if (unlikely(!dc || !port)) {
Frank Seidel20fd1e32007-11-09 14:49:23 +01001891 goto exit_in_buffer;
1892 }
1893
Alan Cox33dd4742009-01-02 13:47:32 +00001894 if (unlikely(!port->port.count)) {
Frank Seidel20fd1e32007-11-09 14:49:23 +01001895 dev_err(&dc->pdev->dev, "No tty open?\n");
Frank Seidel20fd1e32007-11-09 14:49:23 +01001896 goto exit_in_buffer;
1897 }
1898
Stefani Seibolde64c0262009-12-21 14:37:28 -08001899 rval = kfifo_len(&port->fifo_ul);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001900
1901exit_in_buffer:
1902 return rval;
1903}
1904
Alan Cox266794e2010-02-08 10:06:45 +00001905static const struct tty_port_operations noz_tty_port_ops = {
1906 .activate = ntty_activate,
1907 .shutdown = ntty_shutdown,
1908};
1909
Frank Seidel18bbe0c22008-02-01 09:14:05 +01001910static const struct tty_operations tty_ops = {
Frank Seidel20fd1e32007-11-09 14:49:23 +01001911 .ioctl = ntty_ioctl,
1912 .open = ntty_open,
1913 .close = ntty_close,
Alan Cox266794e2010-02-08 10:06:45 +00001914 .hangup = ntty_hangup,
Frank Seidel20fd1e32007-11-09 14:49:23 +01001915 .write = ntty_write,
1916 .write_room = ntty_write_room,
1917 .unthrottle = ntty_unthrottle,
1918 .throttle = ntty_throttle,
1919 .chars_in_buffer = ntty_chars_in_buffer,
Frank Seidel20fd1e32007-11-09 14:49:23 +01001920 .tiocmget = ntty_tiocmget,
1921 .tiocmset = ntty_tiocmset,
Alan Cox05871022010-09-16 18:21:52 +01001922 .get_icount = ntty_tiocgicount,
Alan Cox266794e2010-02-08 10:06:45 +00001923 .install = ntty_install,
1924 .cleanup = ntty_cleanup,
Frank Seidel20fd1e32007-11-09 14:49:23 +01001925};
1926
1927/* Module initialization */
1928static struct pci_driver nozomi_driver = {
1929 .name = NOZOMI_NAME,
1930 .id_table = nozomi_pci_tbl,
1931 .probe = nozomi_card_init,
1932 .remove = __devexit_p(nozomi_card_exit),
1933};
1934
1935static __init int nozomi_init(void)
1936{
1937 int ret;
1938
1939 printk(KERN_INFO "Initializing %s\n", VERSION_STRING);
1940
1941 ntty_driver = alloc_tty_driver(NTTY_TTY_MAXMINORS);
1942 if (!ntty_driver)
1943 return -ENOMEM;
1944
1945 ntty_driver->owner = THIS_MODULE;
1946 ntty_driver->driver_name = NOZOMI_NAME_TTY;
1947 ntty_driver->name = "noz";
1948 ntty_driver->major = 0;
1949 ntty_driver->type = TTY_DRIVER_TYPE_SERIAL;
1950 ntty_driver->subtype = SERIAL_TYPE_NORMAL;
1951 ntty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
1952 ntty_driver->init_termios = tty_std_termios;
1953 ntty_driver->init_termios.c_cflag = B115200 | CS8 | CREAD | \
1954 HUPCL | CLOCAL;
1955 ntty_driver->init_termios.c_ispeed = 115200;
1956 ntty_driver->init_termios.c_ospeed = 115200;
1957 tty_set_operations(ntty_driver, &tty_ops);
1958
1959 ret = tty_register_driver(ntty_driver);
1960 if (ret) {
1961 printk(KERN_ERR "Nozomi: failed to register ntty driver\n");
1962 goto free_tty;
1963 }
1964
1965 ret = pci_register_driver(&nozomi_driver);
1966 if (ret) {
1967 printk(KERN_ERR "Nozomi: can't register pci driver\n");
1968 goto unr_tty;
1969 }
1970
1971 return 0;
1972unr_tty:
1973 tty_unregister_driver(ntty_driver);
1974free_tty:
1975 put_tty_driver(ntty_driver);
1976 return ret;
1977}
1978
1979static __exit void nozomi_exit(void)
1980{
1981 printk(KERN_INFO "Unloading %s\n", DRIVER_DESC);
1982 pci_unregister_driver(&nozomi_driver);
1983 tty_unregister_driver(ntty_driver);
1984 put_tty_driver(ntty_driver);
1985}
1986
1987module_init(nozomi_init);
1988module_exit(nozomi_exit);
1989
1990module_param(debug, int, S_IRUGO | S_IWUSR);
1991
1992MODULE_LICENSE("Dual BSD/GPL");
1993MODULE_DESCRIPTION(DRIVER_DESC);