blob: ec58d8c387ff2303f67db428622f68b7eff76057 [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>
51#include <linux/serial.h>
52#include <linux/interrupt.h>
53#include <linux/kmod.h>
54#include <linux/init.h>
55#include <linux/kfifo.h>
56#include <linux/uaccess.h>
57#include <asm/byteorder.h>
58
59#include <linux/delay.h>
60
61
62#define VERSION_STRING DRIVER_DESC " 2.1d (build date: " \
63 __DATE__ " " __TIME__ ")"
64
65/* Macros definitions */
66
67/* Default debug printout level */
68#define NOZOMI_DEBUG_LEVEL 0x00
69
70#define P_BUF_SIZE 128
71#define NFO(_err_flag_, args...) \
72do { \
73 char tmp[P_BUF_SIZE]; \
74 snprintf(tmp, sizeof(tmp), ##args); \
75 printk(_err_flag_ "[%d] %s(): %s\n", __LINE__, \
Harvey Harrisonbf9d8922008-04-30 00:55:10 -070076 __func__, tmp); \
Frank Seidel20fd1e32007-11-09 14:49:23 +010077} while (0)
78
79#define DBG1(args...) D_(0x01, ##args)
80#define DBG2(args...) D_(0x02, ##args)
81#define DBG3(args...) D_(0x04, ##args)
82#define DBG4(args...) D_(0x08, ##args)
83#define DBG5(args...) D_(0x10, ##args)
84#define DBG6(args...) D_(0x20, ##args)
85#define DBG7(args...) D_(0x40, ##args)
86#define DBG8(args...) D_(0x80, ##args)
87
88#ifdef DEBUG
89/* Do we need this settable at runtime? */
90static int debug = NOZOMI_DEBUG_LEVEL;
91
Frank Seidele9ed5372008-01-25 21:13:24 +010092#define D(lvl, args...) do \
93 {if (lvl & debug) NFO(KERN_DEBUG, ##args); } \
94 while (0)
Frank Seidel20fd1e32007-11-09 14:49:23 +010095#define D_(lvl, args...) D(lvl, ##args)
96
97/* These printouts are always printed */
98
99#else
100static int debug;
101#define D_(lvl, args...)
102#endif
103
104/* TODO: rewrite to optimize macros... */
105
106#define TMP_BUF_MAX 256
107
108#define DUMP(buf__,len__) \
109 do { \
110 char tbuf[TMP_BUF_MAX] = {0};\
111 if (len__ > 1) {\
112 snprintf(tbuf, len__ > TMP_BUF_MAX ? TMP_BUF_MAX : len__, "%s", buf__);\
113 if (tbuf[len__-2] == '\r') {\
114 tbuf[len__-2] = 'r';\
115 } \
116 DBG1("SENDING: '%s' (%d+n)", tbuf, len__);\
117 } else {\
118 DBG1("SENDING: '%s' (%d)", tbuf, len__);\
119 } \
120} while (0)
121
122/* Defines */
123#define NOZOMI_NAME "nozomi"
124#define NOZOMI_NAME_TTY "nozomi_tty"
125#define DRIVER_DESC "Nozomi driver"
126
127#define NTTY_TTY_MAXMINORS 256
128#define NTTY_FIFO_BUFFER_SIZE 8192
129
130/* Must be power of 2 */
131#define FIFO_BUFFER_SIZE_UL 8192
132
133/* Size of tmp send buffer to card */
134#define SEND_BUF_MAX 1024
135#define RECEIVE_BUF_MAX 4
136
137
138/* Define all types of vendors and devices to support */
139#define VENDOR1 0x1931 /* Vendor Option */
140#define DEVICE1 0x000c /* HSDPA card */
141
142#define R_IIR 0x0000 /* Interrupt Identity Register */
143#define R_FCR 0x0000 /* Flow Control Register */
144#define R_IER 0x0004 /* Interrupt Enable Register */
145
146#define CONFIG_MAGIC 0xEFEFFEFE
147#define TOGGLE_VALID 0x0000
148
149/* Definition of interrupt tokens */
150#define MDM_DL1 0x0001
151#define MDM_UL1 0x0002
152#define MDM_DL2 0x0004
153#define MDM_UL2 0x0008
154#define DIAG_DL1 0x0010
155#define DIAG_DL2 0x0020
156#define DIAG_UL 0x0040
157#define APP1_DL 0x0080
158#define APP1_UL 0x0100
159#define APP2_DL 0x0200
160#define APP2_UL 0x0400
161#define CTRL_DL 0x0800
162#define CTRL_UL 0x1000
163#define RESET 0x8000
164
165#define MDM_DL (MDM_DL1 | MDM_DL2)
166#define MDM_UL (MDM_UL1 | MDM_UL2)
167#define DIAG_DL (DIAG_DL1 | DIAG_DL2)
168
169/* modem signal definition */
170#define CTRL_DSR 0x0001
171#define CTRL_DCD 0x0002
172#define CTRL_RI 0x0004
173#define CTRL_CTS 0x0008
174
175#define CTRL_DTR 0x0001
176#define CTRL_RTS 0x0002
177
178#define MAX_PORT 4
179#define NOZOMI_MAX_PORTS 5
180#define NOZOMI_MAX_CARDS (NTTY_TTY_MAXMINORS / MAX_PORT)
181
182/* Type definitions */
183
184/*
185 * There are two types of nozomi cards,
186 * one with 2048 memory and with 8192 memory
187 */
188enum card_type {
189 F32_2 = 2048, /* 512 bytes downlink + uplink * 2 -> 2048 */
190 F32_8 = 8192, /* 3072 bytes downl. + 1024 bytes uplink * 2 -> 8192 */
191};
192
Frank Seidel661b4e82008-03-06 21:45:57 +0100193/* Initialization states a card can be in */
194enum card_state {
195 NOZOMI_STATE_UKNOWN = 0,
196 NOZOMI_STATE_ENABLED = 1, /* pci device enabled */
197 NOZOMI_STATE_ALLOCATED = 2, /* config setup done */
198 NOZOMI_STATE_READY = 3, /* flowcontrols received */
199};
200
Frank Seidel20fd1e32007-11-09 14:49:23 +0100201/* Two different toggle channels exist */
202enum channel_type {
203 CH_A = 0,
204 CH_B = 1,
205};
206
207/* Port definition for the card regarding flow control */
208enum ctrl_port_type {
209 CTRL_CMD = 0,
210 CTRL_MDM = 1,
211 CTRL_DIAG = 2,
212 CTRL_APP1 = 3,
213 CTRL_APP2 = 4,
214 CTRL_ERROR = -1,
215};
216
217/* Ports that the nozomi has */
218enum port_type {
219 PORT_MDM = 0,
220 PORT_DIAG = 1,
221 PORT_APP1 = 2,
222 PORT_APP2 = 3,
223 PORT_CTRL = 4,
224 PORT_ERROR = -1,
225};
226
227#ifdef __BIG_ENDIAN
228/* Big endian */
229
230struct toggles {
Frank Seidele9ed5372008-01-25 21:13:24 +0100231 unsigned int enabled:5; /*
Frank Seidel20fd1e32007-11-09 14:49:23 +0100232 * Toggle fields are valid if enabled is 0,
233 * else A-channels must always be used.
234 */
Frank Seidele9ed5372008-01-25 21:13:24 +0100235 unsigned int diag_dl:1;
236 unsigned int mdm_dl:1;
237 unsigned int mdm_ul:1;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100238} __attribute__ ((packed));
239
240/* Configuration table to read at startup of card */
241/* Is for now only needed during initialization phase */
242struct config_table {
243 u32 signature;
244 u16 product_information;
245 u16 version;
246 u8 pad3[3];
247 struct toggles toggle;
248 u8 pad1[4];
249 u16 dl_mdm_len1; /*
250 * If this is 64, it can hold
251 * 60 bytes + 4 that is length field
252 */
253 u16 dl_start;
254
255 u16 dl_diag_len1;
256 u16 dl_mdm_len2; /*
257 * If this is 64, it can hold
258 * 60 bytes + 4 that is length field
259 */
260 u16 dl_app1_len;
261
262 u16 dl_diag_len2;
263 u16 dl_ctrl_len;
264 u16 dl_app2_len;
265 u8 pad2[16];
266 u16 ul_mdm_len1;
267 u16 ul_start;
268 u16 ul_diag_len;
269 u16 ul_mdm_len2;
270 u16 ul_app1_len;
271 u16 ul_app2_len;
272 u16 ul_ctrl_len;
273} __attribute__ ((packed));
274
275/* This stores all control downlink flags */
276struct ctrl_dl {
277 u8 port;
Frank Seidele9ed5372008-01-25 21:13:24 +0100278 unsigned int reserved:4;
279 unsigned int CTS:1;
280 unsigned int RI:1;
281 unsigned int DCD:1;
282 unsigned int DSR:1;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100283} __attribute__ ((packed));
284
285/* This stores all control uplink flags */
286struct ctrl_ul {
287 u8 port;
Frank Seidele9ed5372008-01-25 21:13:24 +0100288 unsigned int reserved:6;
289 unsigned int RTS:1;
290 unsigned int DTR:1;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100291} __attribute__ ((packed));
292
293#else
294/* Little endian */
295
296/* This represents the toggle information */
297struct toggles {
Frank Seidele9ed5372008-01-25 21:13:24 +0100298 unsigned int mdm_ul:1;
299 unsigned int mdm_dl:1;
300 unsigned int diag_dl:1;
301 unsigned int enabled:5; /*
Frank Seidel20fd1e32007-11-09 14:49:23 +0100302 * Toggle fields are valid if enabled is 0,
303 * else A-channels must always be used.
304 */
305} __attribute__ ((packed));
306
307/* Configuration table to read at startup of card */
308struct config_table {
309 u32 signature;
310 u16 version;
311 u16 product_information;
312 struct toggles toggle;
313 u8 pad1[7];
314 u16 dl_start;
315 u16 dl_mdm_len1; /*
316 * If this is 64, it can hold
317 * 60 bytes + 4 that is length field
318 */
319 u16 dl_mdm_len2;
320 u16 dl_diag_len1;
321 u16 dl_diag_len2;
322 u16 dl_app1_len;
323 u16 dl_app2_len;
324 u16 dl_ctrl_len;
325 u8 pad2[16];
326 u16 ul_start;
327 u16 ul_mdm_len2;
328 u16 ul_mdm_len1;
329 u16 ul_diag_len;
330 u16 ul_app1_len;
331 u16 ul_app2_len;
332 u16 ul_ctrl_len;
333} __attribute__ ((packed));
334
335/* This stores all control downlink flags */
336struct ctrl_dl {
Frank Seidele9ed5372008-01-25 21:13:24 +0100337 unsigned int DSR:1;
338 unsigned int DCD:1;
339 unsigned int RI:1;
340 unsigned int CTS:1;
341 unsigned int reserverd:4;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100342 u8 port;
343} __attribute__ ((packed));
344
345/* This stores all control uplink flags */
346struct ctrl_ul {
Frank Seidele9ed5372008-01-25 21:13:24 +0100347 unsigned int DTR:1;
348 unsigned int RTS:1;
349 unsigned int reserved:6;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100350 u8 port;
351} __attribute__ ((packed));
352#endif
353
354/* This holds all information that is needed regarding a port */
355struct port {
Alan Cox33dd4742009-01-02 13:47:32 +0000356 struct tty_port port;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100357 u8 update_flow_control;
358 struct ctrl_ul ctrl_ul;
359 struct ctrl_dl ctrl_dl;
360 struct kfifo *fifo_ul;
361 void __iomem *dl_addr[2];
362 u32 dl_size[2];
363 u8 toggle_dl;
364 void __iomem *ul_addr[2];
365 u32 ul_size[2];
366 u8 toggle_ul;
367 u16 token_dl;
368
Frank Seidel20fd1e32007-11-09 14:49:23 +0100369 /* mutex to ensure one access patch to this port */
370 struct mutex tty_sem;
371 wait_queue_head_t tty_wait;
372 struct async_icount tty_icount;
373};
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 = {
Frank Seidel20fd1e32007-11-09 14:49:23 +0100407 {PCI_DEVICE(VENDOR1, DEVICE1)},
408 {},
409};
410
411MODULE_DEVICE_TABLE(pci, nozomi_pci_tbl);
412
413static struct nozomi *ndevs[NOZOMI_MAX_CARDS];
414static struct tty_driver *ntty_driver;
415
416/*
417 * find card by tty_index
418 */
419static inline struct nozomi *get_dc_by_tty(const struct tty_struct *tty)
420{
421 return tty ? ndevs[tty->index / MAX_PORT] : NULL;
422}
423
424static inline struct port *get_port_by_tty(const struct tty_struct *tty)
425{
426 struct nozomi *ndev = get_dc_by_tty(tty);
427 return ndev ? &ndev->port[tty->index % MAX_PORT] : NULL;
428}
429
430/*
431 * TODO:
432 * -Optimize
433 * -Rewrite cleaner
434 */
435
436static void read_mem32(u32 *buf, const void __iomem *mem_addr_start,
437 u32 size_bytes)
438{
439 u32 i = 0;
Al Viro782a6de2008-03-29 03:09:08 +0000440 const u32 __iomem *ptr = mem_addr_start;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100441 u16 *buf16;
442
443 if (unlikely(!ptr || !buf))
444 goto out;
445
446 /* shortcut for extremely often used cases */
447 switch (size_bytes) {
448 case 2: /* 2 bytes */
449 buf16 = (u16 *) buf;
Al Viro782a6de2008-03-29 03:09:08 +0000450 *buf16 = __le16_to_cpu(readw(ptr));
Frank Seidel20fd1e32007-11-09 14:49:23 +0100451 goto out;
452 break;
453 case 4: /* 4 bytes */
Al Viro782a6de2008-03-29 03:09:08 +0000454 *(buf) = __le32_to_cpu(readl(ptr));
Frank Seidel20fd1e32007-11-09 14:49:23 +0100455 goto out;
456 break;
457 }
458
459 while (i < size_bytes) {
460 if (size_bytes - i == 2) {
461 /* Handle 2 bytes in the end */
462 buf16 = (u16 *) buf;
Al Viro782a6de2008-03-29 03:09:08 +0000463 *(buf16) = __le16_to_cpu(readw(ptr));
Frank Seidel20fd1e32007-11-09 14:49:23 +0100464 i += 2;
465 } else {
466 /* Read 4 bytes */
Al Viro782a6de2008-03-29 03:09:08 +0000467 *(buf) = __le32_to_cpu(readl(ptr));
Frank Seidel20fd1e32007-11-09 14:49:23 +0100468 i += 4;
469 }
470 buf++;
471 ptr++;
472 }
473out:
474 return;
475}
476
477/*
478 * TODO:
479 * -Optimize
480 * -Rewrite cleaner
481 */
Jan Engelhardt71e1b4a2008-02-01 09:13:53 +0100482static u32 write_mem32(void __iomem *mem_addr_start, const u32 *buf,
Frank Seidel20fd1e32007-11-09 14:49:23 +0100483 u32 size_bytes)
484{
485 u32 i = 0;
Al Viro782a6de2008-03-29 03:09:08 +0000486 u32 __iomem *ptr = mem_addr_start;
Jan Engelhardt71e1b4a2008-02-01 09:13:53 +0100487 const u16 *buf16;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100488
489 if (unlikely(!ptr || !buf))
490 return 0;
491
492 /* shortcut for extremely often used cases */
493 switch (size_bytes) {
494 case 2: /* 2 bytes */
Jan Engelhardt71e1b4a2008-02-01 09:13:53 +0100495 buf16 = (const u16 *)buf;
Al Viro782a6de2008-03-29 03:09:08 +0000496 writew(__cpu_to_le16(*buf16), ptr);
Frank Seidel20fd1e32007-11-09 14:49:23 +0100497 return 2;
498 break;
499 case 1: /*
500 * also needs to write 4 bytes in this case
501 * so falling through..
502 */
503 case 4: /* 4 bytes */
Al Viro782a6de2008-03-29 03:09:08 +0000504 writel(__cpu_to_le32(*buf), ptr);
Frank Seidel20fd1e32007-11-09 14:49:23 +0100505 return 4;
506 break;
507 }
508
509 while (i < size_bytes) {
510 if (size_bytes - i == 2) {
511 /* 2 bytes */
Jan Engelhardt71e1b4a2008-02-01 09:13:53 +0100512 buf16 = (const u16 *)buf;
Al Viro782a6de2008-03-29 03:09:08 +0000513 writew(__cpu_to_le16(*buf16), ptr);
Frank Seidel20fd1e32007-11-09 14:49:23 +0100514 i += 2;
515 } else {
516 /* 4 bytes */
Al Viro782a6de2008-03-29 03:09:08 +0000517 writel(__cpu_to_le32(*buf), ptr);
Frank Seidel20fd1e32007-11-09 14:49:23 +0100518 i += 4;
519 }
520 buf++;
521 ptr++;
522 }
523 return i;
524}
525
526/* Setup pointers to different channels and also setup buffer sizes. */
527static void setup_memory(struct nozomi *dc)
528{
529 void __iomem *offset = dc->base_addr + dc->config_table.dl_start;
530 /* The length reported is including the length field of 4 bytes,
531 * hence subtract with 4.
532 */
533 const u16 buff_offset = 4;
534
535 /* Modem port dl configuration */
536 dc->port[PORT_MDM].dl_addr[CH_A] = offset;
537 dc->port[PORT_MDM].dl_addr[CH_B] =
538 (offset += dc->config_table.dl_mdm_len1);
539 dc->port[PORT_MDM].dl_size[CH_A] =
540 dc->config_table.dl_mdm_len1 - buff_offset;
541 dc->port[PORT_MDM].dl_size[CH_B] =
542 dc->config_table.dl_mdm_len2 - buff_offset;
543
544 /* Diag port dl configuration */
545 dc->port[PORT_DIAG].dl_addr[CH_A] =
546 (offset += dc->config_table.dl_mdm_len2);
547 dc->port[PORT_DIAG].dl_size[CH_A] =
548 dc->config_table.dl_diag_len1 - buff_offset;
549 dc->port[PORT_DIAG].dl_addr[CH_B] =
550 (offset += dc->config_table.dl_diag_len1);
551 dc->port[PORT_DIAG].dl_size[CH_B] =
552 dc->config_table.dl_diag_len2 - buff_offset;
553
554 /* App1 port dl configuration */
555 dc->port[PORT_APP1].dl_addr[CH_A] =
556 (offset += dc->config_table.dl_diag_len2);
557 dc->port[PORT_APP1].dl_size[CH_A] =
558 dc->config_table.dl_app1_len - buff_offset;
559
560 /* App2 port dl configuration */
561 dc->port[PORT_APP2].dl_addr[CH_A] =
562 (offset += dc->config_table.dl_app1_len);
563 dc->port[PORT_APP2].dl_size[CH_A] =
564 dc->config_table.dl_app2_len - buff_offset;
565
566 /* Ctrl dl configuration */
567 dc->port[PORT_CTRL].dl_addr[CH_A] =
568 (offset += dc->config_table.dl_app2_len);
569 dc->port[PORT_CTRL].dl_size[CH_A] =
570 dc->config_table.dl_ctrl_len - buff_offset;
571
572 offset = dc->base_addr + dc->config_table.ul_start;
573
574 /* Modem Port ul configuration */
575 dc->port[PORT_MDM].ul_addr[CH_A] = offset;
576 dc->port[PORT_MDM].ul_size[CH_A] =
577 dc->config_table.ul_mdm_len1 - buff_offset;
578 dc->port[PORT_MDM].ul_addr[CH_B] =
579 (offset += dc->config_table.ul_mdm_len1);
580 dc->port[PORT_MDM].ul_size[CH_B] =
581 dc->config_table.ul_mdm_len2 - buff_offset;
582
583 /* Diag port ul configuration */
584 dc->port[PORT_DIAG].ul_addr[CH_A] =
585 (offset += dc->config_table.ul_mdm_len2);
586 dc->port[PORT_DIAG].ul_size[CH_A] =
587 dc->config_table.ul_diag_len - buff_offset;
588
589 /* App1 port ul configuration */
590 dc->port[PORT_APP1].ul_addr[CH_A] =
591 (offset += dc->config_table.ul_diag_len);
592 dc->port[PORT_APP1].ul_size[CH_A] =
593 dc->config_table.ul_app1_len - buff_offset;
594
595 /* App2 port ul configuration */
596 dc->port[PORT_APP2].ul_addr[CH_A] =
597 (offset += dc->config_table.ul_app1_len);
598 dc->port[PORT_APP2].ul_size[CH_A] =
599 dc->config_table.ul_app2_len - buff_offset;
600
601 /* Ctrl ul configuration */
602 dc->port[PORT_CTRL].ul_addr[CH_A] =
603 (offset += dc->config_table.ul_app2_len);
604 dc->port[PORT_CTRL].ul_size[CH_A] =
605 dc->config_table.ul_ctrl_len - buff_offset;
606}
607
608/* Dump config table under initalization phase */
609#ifdef DEBUG
610static void dump_table(const struct nozomi *dc)
611{
612 DBG3("signature: 0x%08X", dc->config_table.signature);
613 DBG3("version: 0x%04X", dc->config_table.version);
614 DBG3("product_information: 0x%04X", \
615 dc->config_table.product_information);
616 DBG3("toggle enabled: %d", dc->config_table.toggle.enabled);
617 DBG3("toggle up_mdm: %d", dc->config_table.toggle.mdm_ul);
618 DBG3("toggle dl_mdm: %d", dc->config_table.toggle.mdm_dl);
619 DBG3("toggle dl_dbg: %d", dc->config_table.toggle.diag_dl);
620
621 DBG3("dl_start: 0x%04X", dc->config_table.dl_start);
622 DBG3("dl_mdm_len0: 0x%04X, %d", dc->config_table.dl_mdm_len1,
623 dc->config_table.dl_mdm_len1);
624 DBG3("dl_mdm_len1: 0x%04X, %d", dc->config_table.dl_mdm_len2,
625 dc->config_table.dl_mdm_len2);
626 DBG3("dl_diag_len0: 0x%04X, %d", dc->config_table.dl_diag_len1,
627 dc->config_table.dl_diag_len1);
628 DBG3("dl_diag_len1: 0x%04X, %d", dc->config_table.dl_diag_len2,
629 dc->config_table.dl_diag_len2);
630 DBG3("dl_app1_len: 0x%04X, %d", dc->config_table.dl_app1_len,
631 dc->config_table.dl_app1_len);
632 DBG3("dl_app2_len: 0x%04X, %d", dc->config_table.dl_app2_len,
633 dc->config_table.dl_app2_len);
634 DBG3("dl_ctrl_len: 0x%04X, %d", dc->config_table.dl_ctrl_len,
635 dc->config_table.dl_ctrl_len);
636 DBG3("ul_start: 0x%04X, %d", dc->config_table.ul_start,
637 dc->config_table.ul_start);
638 DBG3("ul_mdm_len[0]: 0x%04X, %d", dc->config_table.ul_mdm_len1,
639 dc->config_table.ul_mdm_len1);
640 DBG3("ul_mdm_len[1]: 0x%04X, %d", dc->config_table.ul_mdm_len2,
641 dc->config_table.ul_mdm_len2);
642 DBG3("ul_diag_len: 0x%04X, %d", dc->config_table.ul_diag_len,
643 dc->config_table.ul_diag_len);
644 DBG3("ul_app1_len: 0x%04X, %d", dc->config_table.ul_app1_len,
645 dc->config_table.ul_app1_len);
646 DBG3("ul_app2_len: 0x%04X, %d", dc->config_table.ul_app2_len,
647 dc->config_table.ul_app2_len);
648 DBG3("ul_ctrl_len: 0x%04X, %d", dc->config_table.ul_ctrl_len,
649 dc->config_table.ul_ctrl_len);
650}
651#else
Frank Seidele9ed5372008-01-25 21:13:24 +0100652static inline void dump_table(const struct nozomi *dc) { }
Frank Seidel20fd1e32007-11-09 14:49:23 +0100653#endif
654
655/*
656 * Read configuration table from card under intalization phase
657 * Returns 1 if ok, else 0
658 */
659static int nozomi_read_config_table(struct nozomi *dc)
660{
661 read_mem32((u32 *) &dc->config_table, dc->base_addr + 0,
662 sizeof(struct config_table));
663
664 if (dc->config_table.signature != CONFIG_MAGIC) {
665 dev_err(&dc->pdev->dev, "ConfigTable Bad! 0x%08X != 0x%08X\n",
666 dc->config_table.signature, CONFIG_MAGIC);
667 return 0;
668 }
669
670 if ((dc->config_table.version == 0)
671 || (dc->config_table.toggle.enabled == TOGGLE_VALID)) {
672 int i;
673 DBG1("Second phase, configuring card");
674
675 setup_memory(dc);
676
677 dc->port[PORT_MDM].toggle_ul = dc->config_table.toggle.mdm_ul;
678 dc->port[PORT_MDM].toggle_dl = dc->config_table.toggle.mdm_dl;
679 dc->port[PORT_DIAG].toggle_dl = dc->config_table.toggle.diag_dl;
680 DBG1("toggle ports: MDM UL:%d MDM DL:%d, DIAG DL:%d",
681 dc->port[PORT_MDM].toggle_ul,
682 dc->port[PORT_MDM].toggle_dl, dc->port[PORT_DIAG].toggle_dl);
683
684 dump_table(dc);
685
686 for (i = PORT_MDM; i < MAX_PORT; i++) {
687 dc->port[i].fifo_ul =
688 kfifo_alloc(FIFO_BUFFER_SIZE_UL, GFP_ATOMIC, NULL);
689 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 */
800 size = __kfifo_get(port->fifo_ul, dc->send_buf,
801 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
857 tty_buffer_request_room(tty, size);
858
859 while (size > 0) {
860 read_mem32((u32 *) buf, addr + offset, RECEIVE_BUF_MAX);
861
862 if (size == 1) {
863 tty_insert_flip_char(tty, buf[0], TTY_NORMAL);
864 size = 0;
865 } else if (size < RECEIVE_BUF_MAX) {
866 size -= tty_insert_flip_string(tty, (char *) buf, size);
867 } else {
868 i = tty_insert_flip_string(tty, \
869 (char *) buf, RECEIVE_BUF_MAX);
870 size -= i;
871 offset += i;
872 }
873 }
874
875 set_bit(index, &dc->flip);
Jiri Slaby9237a812009-07-16 16:06:18 +0100876 ret = 1;
877put:
Alan Cox33dd4742009-01-02 13:47:32 +0000878 tty_kref_put(tty);
Jiri Slaby9237a812009-07-16 16:06:18 +0100879 return ret;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100880}
881
882/* Debug for interrupts */
883#ifdef DEBUG
884static char *interrupt2str(u16 interrupt)
885{
886 static char buf[TMP_BUF_MAX];
887 char *p = buf;
888
889 interrupt & MDM_DL1 ? p += snprintf(p, TMP_BUF_MAX, "MDM_DL1 ") : NULL;
890 interrupt & MDM_DL2 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
891 "MDM_DL2 ") : NULL;
892
893 interrupt & MDM_UL1 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
894 "MDM_UL1 ") : NULL;
895 interrupt & MDM_UL2 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
896 "MDM_UL2 ") : NULL;
897
898 interrupt & DIAG_DL1 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
899 "DIAG_DL1 ") : NULL;
900 interrupt & DIAG_DL2 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
901 "DIAG_DL2 ") : NULL;
902
903 interrupt & DIAG_UL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
904 "DIAG_UL ") : NULL;
905
906 interrupt & APP1_DL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
907 "APP1_DL ") : NULL;
908 interrupt & APP2_DL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
909 "APP2_DL ") : NULL;
910
911 interrupt & APP1_UL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
912 "APP1_UL ") : NULL;
913 interrupt & APP2_UL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
914 "APP2_UL ") : NULL;
915
916 interrupt & CTRL_DL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
917 "CTRL_DL ") : NULL;
918 interrupt & CTRL_UL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
919 "CTRL_UL ") : NULL;
920
921 interrupt & RESET ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
922 "RESET ") : NULL;
923
924 return buf;
925}
926#endif
927
928/*
929 * Receive flow control
930 * Return 1 - If ok, else 0
931 */
932static int receive_flow_control(struct nozomi *dc)
933{
934 enum port_type port = PORT_MDM;
935 struct ctrl_dl ctrl_dl;
936 struct ctrl_dl old_ctrl;
937 u16 enable_ier = 0;
938
939 read_mem32((u32 *) &ctrl_dl, dc->port[PORT_CTRL].dl_addr[CH_A], 2);
940
941 switch (ctrl_dl.port) {
942 case CTRL_CMD:
943 DBG1("The Base Band sends this value as a response to a "
944 "request for IMSI detach sent over the control "
945 "channel uplink (see section 7.6.1).");
946 break;
947 case CTRL_MDM:
948 port = PORT_MDM;
949 enable_ier = MDM_DL;
950 break;
951 case CTRL_DIAG:
952 port = PORT_DIAG;
953 enable_ier = DIAG_DL;
954 break;
955 case CTRL_APP1:
956 port = PORT_APP1;
957 enable_ier = APP1_DL;
958 break;
959 case CTRL_APP2:
960 port = PORT_APP2;
961 enable_ier = APP2_DL;
Frank Seidel661b4e82008-03-06 21:45:57 +0100962 if (dc->state == NOZOMI_STATE_ALLOCATED) {
963 /*
964 * After card initialization the flow control
965 * received for APP2 is always the last
966 */
967 dc->state = NOZOMI_STATE_READY;
968 dev_info(&dc->pdev->dev, "Device READY!\n");
969 }
Frank Seidel20fd1e32007-11-09 14:49:23 +0100970 break;
971 default:
972 dev_err(&dc->pdev->dev,
973 "ERROR: flow control received for non-existing port\n");
974 return 0;
975 };
976
977 DBG1("0x%04X->0x%04X", *((u16 *)&dc->port[port].ctrl_dl),
978 *((u16 *)&ctrl_dl));
979
980 old_ctrl = dc->port[port].ctrl_dl;
981 dc->port[port].ctrl_dl = ctrl_dl;
982
983 if (old_ctrl.CTS == 1 && ctrl_dl.CTS == 0) {
984 DBG1("Disable interrupt (0x%04X) on port: %d",
985 enable_ier, port);
986 disable_transmit_ul(port, dc);
987
988 } else if (old_ctrl.CTS == 0 && ctrl_dl.CTS == 1) {
989
990 if (__kfifo_len(dc->port[port].fifo_ul)) {
991 DBG1("Enable interrupt (0x%04X) on port: %d",
992 enable_ier, port);
993 DBG1("Data in buffer [%d], enable transmit! ",
994 __kfifo_len(dc->port[port].fifo_ul));
995 enable_transmit_ul(port, dc);
996 } else {
997 DBG1("No data in buffer...");
998 }
999 }
1000
1001 if (*(u16 *)&old_ctrl == *(u16 *)&ctrl_dl) {
1002 DBG1(" No change in mctrl");
1003 return 1;
1004 }
1005 /* Update statistics */
1006 if (old_ctrl.CTS != ctrl_dl.CTS)
1007 dc->port[port].tty_icount.cts++;
1008 if (old_ctrl.DSR != ctrl_dl.DSR)
1009 dc->port[port].tty_icount.dsr++;
1010 if (old_ctrl.RI != ctrl_dl.RI)
1011 dc->port[port].tty_icount.rng++;
1012 if (old_ctrl.DCD != ctrl_dl.DCD)
1013 dc->port[port].tty_icount.dcd++;
1014
1015 wake_up_interruptible(&dc->port[port].tty_wait);
1016
1017 DBG1("port: %d DCD(%d), CTS(%d), RI(%d), DSR(%d)",
1018 port,
1019 dc->port[port].tty_icount.dcd, dc->port[port].tty_icount.cts,
1020 dc->port[port].tty_icount.rng, dc->port[port].tty_icount.dsr);
1021
1022 return 1;
1023}
1024
1025static enum ctrl_port_type port2ctrl(enum port_type port,
1026 const struct nozomi *dc)
1027{
1028 switch (port) {
1029 case PORT_MDM:
1030 return CTRL_MDM;
1031 case PORT_DIAG:
1032 return CTRL_DIAG;
1033 case PORT_APP1:
1034 return CTRL_APP1;
1035 case PORT_APP2:
1036 return CTRL_APP2;
1037 default:
1038 dev_err(&dc->pdev->dev,
1039 "ERROR: send flow control " \
1040 "received for non-existing port\n");
1041 };
1042 return CTRL_ERROR;
1043}
1044
1045/*
1046 * Send flow control, can only update one channel at a time
1047 * Return 0 - If we have updated all flow control
1048 * Return 1 - If we need to update more flow control, ack current enable more
1049 */
1050static int send_flow_control(struct nozomi *dc)
1051{
1052 u32 i, more_flow_control_to_be_updated = 0;
1053 u16 *ctrl;
1054
1055 for (i = PORT_MDM; i < MAX_PORT; i++) {
1056 if (dc->port[i].update_flow_control) {
1057 if (more_flow_control_to_be_updated) {
1058 /* We have more flow control to be updated */
1059 return 1;
1060 }
1061 dc->port[i].ctrl_ul.port = port2ctrl(i, dc);
1062 ctrl = (u16 *)&dc->port[i].ctrl_ul;
1063 write_mem32(dc->port[PORT_CTRL].ul_addr[0], \
1064 (u32 *) ctrl, 2);
1065 dc->port[i].update_flow_control = 0;
1066 more_flow_control_to_be_updated = 1;
1067 }
1068 }
1069 return 0;
1070}
1071
1072/*
Frank Seidele9ed5372008-01-25 21:13:24 +01001073 * Handle downlink data, ports that are handled are modem and diagnostics
Frank Seidel20fd1e32007-11-09 14:49:23 +01001074 * Return 1 - ok
1075 * Return 0 - toggle fields are out of sync
1076 */
1077static int handle_data_dl(struct nozomi *dc, enum port_type port, u8 *toggle,
1078 u16 read_iir, u16 mask1, u16 mask2)
1079{
1080 if (*toggle == 0 && read_iir & mask1) {
1081 if (receive_data(port, dc)) {
1082 writew(mask1, dc->reg_fcr);
1083 *toggle = !(*toggle);
1084 }
1085
1086 if (read_iir & mask2) {
1087 if (receive_data(port, dc)) {
1088 writew(mask2, dc->reg_fcr);
1089 *toggle = !(*toggle);
1090 }
1091 }
1092 } else if (*toggle == 1 && read_iir & mask2) {
1093 if (receive_data(port, dc)) {
1094 writew(mask2, dc->reg_fcr);
1095 *toggle = !(*toggle);
1096 }
1097
1098 if (read_iir & mask1) {
1099 if (receive_data(port, dc)) {
1100 writew(mask1, dc->reg_fcr);
1101 *toggle = !(*toggle);
1102 }
1103 }
1104 } else {
1105 dev_err(&dc->pdev->dev, "port out of sync!, toggle:%d\n",
1106 *toggle);
1107 return 0;
1108 }
1109 return 1;
1110}
1111
1112/*
1113 * Handle uplink data, this is currently for the modem port
1114 * Return 1 - ok
1115 * Return 0 - toggle field are out of sync
1116 */
1117static int handle_data_ul(struct nozomi *dc, enum port_type port, u16 read_iir)
1118{
1119 u8 *toggle = &(dc->port[port].toggle_ul);
1120
1121 if (*toggle == 0 && read_iir & MDM_UL1) {
1122 dc->last_ier &= ~MDM_UL;
1123 writew(dc->last_ier, dc->reg_ier);
1124 if (send_data(port, dc)) {
1125 writew(MDM_UL1, dc->reg_fcr);
1126 dc->last_ier = dc->last_ier | MDM_UL;
1127 writew(dc->last_ier, dc->reg_ier);
1128 *toggle = !*toggle;
1129 }
1130
1131 if (read_iir & MDM_UL2) {
1132 dc->last_ier &= ~MDM_UL;
1133 writew(dc->last_ier, dc->reg_ier);
1134 if (send_data(port, dc)) {
1135 writew(MDM_UL2, dc->reg_fcr);
1136 dc->last_ier = dc->last_ier | MDM_UL;
1137 writew(dc->last_ier, dc->reg_ier);
1138 *toggle = !*toggle;
1139 }
1140 }
1141
1142 } else if (*toggle == 1 && read_iir & MDM_UL2) {
1143 dc->last_ier &= ~MDM_UL;
1144 writew(dc->last_ier, dc->reg_ier);
1145 if (send_data(port, dc)) {
1146 writew(MDM_UL2, dc->reg_fcr);
1147 dc->last_ier = dc->last_ier | MDM_UL;
1148 writew(dc->last_ier, dc->reg_ier);
1149 *toggle = !*toggle;
1150 }
1151
1152 if (read_iir & MDM_UL1) {
1153 dc->last_ier &= ~MDM_UL;
1154 writew(dc->last_ier, dc->reg_ier);
1155 if (send_data(port, dc)) {
1156 writew(MDM_UL1, dc->reg_fcr);
1157 dc->last_ier = dc->last_ier | MDM_UL;
1158 writew(dc->last_ier, dc->reg_ier);
1159 *toggle = !*toggle;
1160 }
1161 }
1162 } else {
1163 writew(read_iir & MDM_UL, dc->reg_fcr);
1164 dev_err(&dc->pdev->dev, "port out of sync!\n");
1165 return 0;
1166 }
1167 return 1;
1168}
1169
1170static irqreturn_t interrupt_handler(int irq, void *dev_id)
1171{
1172 struct nozomi *dc = dev_id;
1173 unsigned int a;
1174 u16 read_iir;
1175
1176 if (!dc)
1177 return IRQ_NONE;
1178
1179 spin_lock(&dc->spin_mutex);
1180 read_iir = readw(dc->reg_iir);
1181
1182 /* Card removed */
1183 if (read_iir == (u16)-1)
1184 goto none;
1185 /*
1186 * Just handle interrupt enabled in IER
1187 * (by masking with dc->last_ier)
1188 */
1189 read_iir &= dc->last_ier;
1190
1191 if (read_iir == 0)
1192 goto none;
1193
1194
1195 DBG4("%s irq:0x%04X, prev:0x%04X", interrupt2str(read_iir), read_iir,
1196 dc->last_ier);
1197
1198 if (read_iir & RESET) {
1199 if (unlikely(!nozomi_read_config_table(dc))) {
1200 dc->last_ier = 0x0;
1201 writew(dc->last_ier, dc->reg_ier);
1202 dev_err(&dc->pdev->dev, "Could not read status from "
1203 "card, we should disable interface\n");
1204 } else {
1205 writew(RESET, dc->reg_fcr);
1206 }
1207 /* No more useful info if this was the reset interrupt. */
1208 goto exit_handler;
1209 }
1210 if (read_iir & CTRL_UL) {
1211 DBG1("CTRL_UL");
1212 dc->last_ier &= ~CTRL_UL;
1213 writew(dc->last_ier, dc->reg_ier);
1214 if (send_flow_control(dc)) {
1215 writew(CTRL_UL, dc->reg_fcr);
1216 dc->last_ier = dc->last_ier | CTRL_UL;
1217 writew(dc->last_ier, dc->reg_ier);
1218 }
1219 }
1220 if (read_iir & CTRL_DL) {
1221 receive_flow_control(dc);
1222 writew(CTRL_DL, dc->reg_fcr);
1223 }
1224 if (read_iir & MDM_DL) {
1225 if (!handle_data_dl(dc, PORT_MDM,
1226 &(dc->port[PORT_MDM].toggle_dl), read_iir,
1227 MDM_DL1, MDM_DL2)) {
1228 dev_err(&dc->pdev->dev, "MDM_DL out of sync!\n");
1229 goto exit_handler;
1230 }
1231 }
1232 if (read_iir & MDM_UL) {
1233 if (!handle_data_ul(dc, PORT_MDM, read_iir)) {
1234 dev_err(&dc->pdev->dev, "MDM_UL out of sync!\n");
1235 goto exit_handler;
1236 }
1237 }
1238 if (read_iir & DIAG_DL) {
1239 if (!handle_data_dl(dc, PORT_DIAG,
1240 &(dc->port[PORT_DIAG].toggle_dl), read_iir,
1241 DIAG_DL1, DIAG_DL2)) {
1242 dev_err(&dc->pdev->dev, "DIAG_DL out of sync!\n");
1243 goto exit_handler;
1244 }
1245 }
1246 if (read_iir & DIAG_UL) {
1247 dc->last_ier &= ~DIAG_UL;
1248 writew(dc->last_ier, dc->reg_ier);
1249 if (send_data(PORT_DIAG, dc)) {
1250 writew(DIAG_UL, dc->reg_fcr);
1251 dc->last_ier = dc->last_ier | DIAG_UL;
1252 writew(dc->last_ier, dc->reg_ier);
1253 }
1254 }
1255 if (read_iir & APP1_DL) {
1256 if (receive_data(PORT_APP1, dc))
1257 writew(APP1_DL, dc->reg_fcr);
1258 }
1259 if (read_iir & APP1_UL) {
1260 dc->last_ier &= ~APP1_UL;
1261 writew(dc->last_ier, dc->reg_ier);
1262 if (send_data(PORT_APP1, dc)) {
1263 writew(APP1_UL, dc->reg_fcr);
1264 dc->last_ier = dc->last_ier | APP1_UL;
1265 writew(dc->last_ier, dc->reg_ier);
1266 }
1267 }
1268 if (read_iir & APP2_DL) {
1269 if (receive_data(PORT_APP2, dc))
1270 writew(APP2_DL, dc->reg_fcr);
1271 }
1272 if (read_iir & APP2_UL) {
1273 dc->last_ier &= ~APP2_UL;
1274 writew(dc->last_ier, dc->reg_ier);
1275 if (send_data(PORT_APP2, dc)) {
1276 writew(APP2_UL, dc->reg_fcr);
1277 dc->last_ier = dc->last_ier | APP2_UL;
1278 writew(dc->last_ier, dc->reg_ier);
1279 }
1280 }
1281
1282exit_handler:
1283 spin_unlock(&dc->spin_mutex);
Alan Cox33dd4742009-01-02 13:47:32 +00001284 for (a = 0; a < NOZOMI_MAX_PORTS; a++) {
1285 struct tty_struct *tty;
1286 if (test_and_clear_bit(a, &dc->flip)) {
1287 tty = tty_port_tty_get(&dc->port[a].port);
1288 if (tty)
1289 tty_flip_buffer_push(tty);
1290 tty_kref_put(tty);
1291 }
1292 }
Frank Seidel20fd1e32007-11-09 14:49:23 +01001293 return IRQ_HANDLED;
1294none:
1295 spin_unlock(&dc->spin_mutex);
1296 return IRQ_NONE;
1297}
1298
1299static void nozomi_get_card_type(struct nozomi *dc)
1300{
1301 int i;
1302 u32 size = 0;
1303
1304 for (i = 0; i < 6; i++)
1305 size += pci_resource_len(dc->pdev, i);
1306
1307 /* Assume card type F32_8 if no match */
1308 dc->card_type = size == 2048 ? F32_2 : F32_8;
1309
1310 dev_info(&dc->pdev->dev, "Card type is: %d\n", dc->card_type);
1311}
1312
1313static void nozomi_setup_private_data(struct nozomi *dc)
1314{
1315 void __iomem *offset = dc->base_addr + dc->card_type / 2;
1316 unsigned int i;
1317
1318 dc->reg_fcr = (void __iomem *)(offset + R_FCR);
1319 dc->reg_iir = (void __iomem *)(offset + R_IIR);
1320 dc->reg_ier = (void __iomem *)(offset + R_IER);
1321 dc->last_ier = 0;
1322 dc->flip = 0;
1323
1324 dc->port[PORT_MDM].token_dl = MDM_DL;
1325 dc->port[PORT_DIAG].token_dl = DIAG_DL;
1326 dc->port[PORT_APP1].token_dl = APP1_DL;
1327 dc->port[PORT_APP2].token_dl = APP2_DL;
1328
1329 for (i = 0; i < MAX_PORT; i++)
1330 init_waitqueue_head(&dc->port[i].tty_wait);
1331}
1332
1333static ssize_t card_type_show(struct device *dev, struct device_attribute *attr,
1334 char *buf)
1335{
Jan Engelhardt71e1b4a2008-02-01 09:13:53 +01001336 const struct nozomi *dc = pci_get_drvdata(to_pci_dev(dev));
Frank Seidel20fd1e32007-11-09 14:49:23 +01001337
1338 return sprintf(buf, "%d\n", dc->card_type);
1339}
Frank Seidele9ed5372008-01-25 21:13:24 +01001340static DEVICE_ATTR(card_type, S_IRUGO, card_type_show, NULL);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001341
1342static ssize_t open_ttys_show(struct device *dev, struct device_attribute *attr,
1343 char *buf)
1344{
Jan Engelhardt71e1b4a2008-02-01 09:13:53 +01001345 const struct nozomi *dc = pci_get_drvdata(to_pci_dev(dev));
Frank Seidel20fd1e32007-11-09 14:49:23 +01001346
1347 return sprintf(buf, "%u\n", dc->open_ttys);
1348}
Frank Seidele9ed5372008-01-25 21:13:24 +01001349static DEVICE_ATTR(open_ttys, S_IRUGO, open_ttys_show, NULL);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001350
1351static void make_sysfs_files(struct nozomi *dc)
1352{
1353 if (device_create_file(&dc->pdev->dev, &dev_attr_card_type))
1354 dev_err(&dc->pdev->dev,
1355 "Could not create sysfs file for card_type\n");
1356 if (device_create_file(&dc->pdev->dev, &dev_attr_open_ttys))
1357 dev_err(&dc->pdev->dev,
1358 "Could not create sysfs file for open_ttys\n");
1359}
1360
1361static void remove_sysfs_files(struct nozomi *dc)
1362{
1363 device_remove_file(&dc->pdev->dev, &dev_attr_card_type);
1364 device_remove_file(&dc->pdev->dev, &dev_attr_open_ttys);
1365}
1366
1367/* Allocate memory for one device */
1368static int __devinit nozomi_card_init(struct pci_dev *pdev,
1369 const struct pci_device_id *ent)
1370{
1371 resource_size_t start;
1372 int ret;
1373 struct nozomi *dc = NULL;
1374 int ndev_idx;
1375 int i;
1376
1377 dev_dbg(&pdev->dev, "Init, new card found\n");
1378
1379 for (ndev_idx = 0; ndev_idx < ARRAY_SIZE(ndevs); ndev_idx++)
1380 if (!ndevs[ndev_idx])
1381 break;
1382
1383 if (ndev_idx >= ARRAY_SIZE(ndevs)) {
1384 dev_err(&pdev->dev, "no free tty range for this card left\n");
1385 ret = -EIO;
1386 goto err;
1387 }
1388
1389 dc = kzalloc(sizeof(struct nozomi), GFP_KERNEL);
1390 if (unlikely(!dc)) {
1391 dev_err(&pdev->dev, "Could not allocate memory\n");
1392 ret = -ENOMEM;
1393 goto err_free;
1394 }
1395
1396 dc->pdev = pdev;
1397
Frank Seidel20fd1e32007-11-09 14:49:23 +01001398 ret = pci_enable_device(dc->pdev);
1399 if (ret) {
1400 dev_err(&pdev->dev, "Failed to enable PCI Device\n");
1401 goto err_free;
1402 }
1403
Frank Seidel20fd1e32007-11-09 14:49:23 +01001404 ret = pci_request_regions(dc->pdev, NOZOMI_NAME);
1405 if (ret) {
1406 dev_err(&pdev->dev, "I/O address 0x%04x already in use\n",
1407 (int) /* nozomi_private.io_addr */ 0);
1408 goto err_disable_device;
1409 }
1410
Frank Seidel661b4e82008-03-06 21:45:57 +01001411 start = pci_resource_start(dc->pdev, 0);
1412 if (start == 0) {
1413 dev_err(&pdev->dev, "No I/O address for card detected\n");
1414 ret = -ENODEV;
1415 goto err_rel_regs;
1416 }
1417
1418 /* Find out what card type it is */
1419 nozomi_get_card_type(dc);
1420
Alan Cox24cb2332008-04-30 00:54:19 -07001421 dc->base_addr = ioremap_nocache(start, dc->card_type);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001422 if (!dc->base_addr) {
1423 dev_err(&pdev->dev, "Unable to map card MMIO\n");
1424 ret = -ENODEV;
1425 goto err_rel_regs;
1426 }
1427
1428 dc->send_buf = kmalloc(SEND_BUF_MAX, GFP_KERNEL);
1429 if (!dc->send_buf) {
1430 dev_err(&pdev->dev, "Could not allocate send buffer?\n");
1431 ret = -ENOMEM;
1432 goto err_free_sbuf;
1433 }
1434
1435 spin_lock_init(&dc->spin_mutex);
1436
1437 nozomi_setup_private_data(dc);
1438
1439 /* Disable all interrupts */
1440 dc->last_ier = 0;
1441 writew(dc->last_ier, dc->reg_ier);
1442
1443 ret = request_irq(pdev->irq, &interrupt_handler, IRQF_SHARED,
1444 NOZOMI_NAME, dc);
1445 if (unlikely(ret)) {
1446 dev_err(&pdev->dev, "can't request irq %d\n", pdev->irq);
1447 goto err_free_sbuf;
1448 }
1449
1450 DBG1("base_addr: %p", dc->base_addr);
1451
1452 make_sysfs_files(dc);
1453
1454 dc->index_start = ndev_idx * MAX_PORT;
1455 ndevs[ndev_idx] = dc;
1456
Frank Seidel661b4e82008-03-06 21:45:57 +01001457 pci_set_drvdata(pdev, dc);
1458
1459 /* Enable RESET interrupt */
1460 dc->last_ier = RESET;
1461 iowrite16(dc->last_ier, dc->reg_ier);
1462
1463 dc->state = NOZOMI_STATE_ENABLED;
1464
Frank Seidel20fd1e32007-11-09 14:49:23 +01001465 for (i = 0; i < MAX_PORT; i++) {
1466 mutex_init(&dc->port[i].tty_sem);
Alan Cox33dd4742009-01-02 13:47:32 +00001467 tty_port_init(&dc->port[i].port);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001468 tty_register_device(ntty_driver, dc->index_start + i,
1469 &pdev->dev);
1470 }
Frank Seidel20fd1e32007-11-09 14:49:23 +01001471 return 0;
1472
1473err_free_sbuf:
1474 kfree(dc->send_buf);
1475 iounmap(dc->base_addr);
1476err_rel_regs:
1477 pci_release_regions(pdev);
1478err_disable_device:
1479 pci_disable_device(pdev);
1480err_free:
1481 kfree(dc);
1482err:
1483 return ret;
1484}
1485
1486static void __devexit tty_exit(struct nozomi *dc)
1487{
1488 unsigned int i;
1489
1490 DBG1(" ");
1491
1492 flush_scheduled_work();
1493
Alan Cox33dd4742009-01-02 13:47:32 +00001494 for (i = 0; i < MAX_PORT; ++i) {
1495 struct tty_struct *tty = tty_port_tty_get(&dc->port[i].port);
1496 if (tty && list_empty(&tty->hangup_work.entry))
1497 tty_hangup(tty);
1498 tty_kref_put(tty);
1499 }
1500 /* Racy below - surely should wait for scheduled work to be done or
1501 complete off a hangup method ? */
Frank Seidel20fd1e32007-11-09 14:49:23 +01001502 while (dc->open_ttys)
1503 msleep(1);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001504 for (i = dc->index_start; i < dc->index_start + MAX_PORT; ++i)
1505 tty_unregister_device(ntty_driver, i);
1506}
1507
1508/* Deallocate memory for one device */
1509static void __devexit nozomi_card_exit(struct pci_dev *pdev)
1510{
1511 int i;
1512 struct ctrl_ul ctrl;
1513 struct nozomi *dc = pci_get_drvdata(pdev);
1514
1515 /* Disable all interrupts */
1516 dc->last_ier = 0;
1517 writew(dc->last_ier, dc->reg_ier);
1518
1519 tty_exit(dc);
1520
1521 /* Send 0x0001, command card to resend the reset token. */
1522 /* This is to get the reset when the module is reloaded. */
1523 ctrl.port = 0x00;
1524 ctrl.reserved = 0;
1525 ctrl.RTS = 0;
1526 ctrl.DTR = 1;
1527 DBG1("sending flow control 0x%04X", *((u16 *)&ctrl));
1528
1529 /* Setup dc->reg addresses to we can use defines here */
1530 write_mem32(dc->port[PORT_CTRL].ul_addr[0], (u32 *)&ctrl, 2);
1531 writew(CTRL_UL, dc->reg_fcr); /* push the token to the card. */
1532
1533 remove_sysfs_files(dc);
1534
1535 free_irq(pdev->irq, dc);
1536
1537 for (i = 0; i < MAX_PORT; i++)
1538 if (dc->port[i].fifo_ul)
1539 kfifo_free(dc->port[i].fifo_ul);
1540
1541 kfree(dc->send_buf);
1542
1543 iounmap(dc->base_addr);
1544
1545 pci_release_regions(pdev);
1546
1547 pci_disable_device(pdev);
1548
1549 ndevs[dc->index_start / MAX_PORT] = NULL;
1550
1551 kfree(dc);
1552}
1553
1554static void set_rts(const struct tty_struct *tty, int rts)
1555{
1556 struct port *port = get_port_by_tty(tty);
1557
1558 port->ctrl_ul.RTS = rts;
1559 port->update_flow_control = 1;
1560 enable_transmit_ul(PORT_CTRL, get_dc_by_tty(tty));
1561}
1562
1563static void set_dtr(const struct tty_struct *tty, int dtr)
1564{
1565 struct port *port = get_port_by_tty(tty);
1566
1567 DBG1("SETTING DTR index: %d, dtr: %d", tty->index, dtr);
1568
1569 port->ctrl_ul.DTR = dtr;
1570 port->update_flow_control = 1;
1571 enable_transmit_ul(PORT_CTRL, get_dc_by_tty(tty));
1572}
1573
1574/*
1575 * ----------------------------------------------------------------------------
1576 * TTY code
1577 * ----------------------------------------------------------------------------
1578 */
1579
1580/* Called when the userspace process opens the tty, /dev/noz*. */
1581static int ntty_open(struct tty_struct *tty, struct file *file)
1582{
1583 struct port *port = get_port_by_tty(tty);
1584 struct nozomi *dc = get_dc_by_tty(tty);
1585 unsigned long flags;
1586
Frank Seidel661b4e82008-03-06 21:45:57 +01001587 if (!port || !dc || dc->state != NOZOMI_STATE_READY)
Frank Seidel20fd1e32007-11-09 14:49:23 +01001588 return -ENODEV;
1589
1590 if (mutex_lock_interruptible(&port->tty_sem))
1591 return -ERESTARTSYS;
1592
Alan Cox33dd4742009-01-02 13:47:32 +00001593 port->port.count++;
Frank Seidel20fd1e32007-11-09 14:49:23 +01001594 dc->open_ttys++;
1595
1596 /* Enable interrupt downlink for channel */
Alan Cox33dd4742009-01-02 13:47:32 +00001597 if (port->port.count == 1) {
Frank Seidel20fd1e32007-11-09 14:49:23 +01001598 tty->driver_data = port;
Alan Cox33dd4742009-01-02 13:47:32 +00001599 tty_port_tty_set(&port->port, tty);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001600 DBG1("open: %d", port->token_dl);
1601 spin_lock_irqsave(&dc->spin_mutex, flags);
1602 dc->last_ier = dc->last_ier | port->token_dl;
1603 writew(dc->last_ier, dc->reg_ier);
1604 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1605 }
Frank Seidel20fd1e32007-11-09 14:49:23 +01001606 mutex_unlock(&port->tty_sem);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001607 return 0;
1608}
1609
Alan Cox716da632008-10-13 10:35:33 +01001610/* Called when the userspace process close the tty, /dev/noz*. Also
1611 called immediately if ntty_open fails in which case tty->driver_data
1612 will be NULL an we exit by the first return */
1613
Frank Seidel20fd1e32007-11-09 14:49:23 +01001614static void ntty_close(struct tty_struct *tty, struct file *file)
1615{
1616 struct nozomi *dc = get_dc_by_tty(tty);
Alan Cox33dd4742009-01-02 13:47:32 +00001617 struct port *nport = tty->driver_data;
1618 struct tty_port *port = &nport->port;
Frank Seidel20fd1e32007-11-09 14:49:23 +01001619 unsigned long flags;
1620
Alan Cox33dd4742009-01-02 13:47:32 +00001621 if (!dc || !nport)
Frank Seidel20fd1e32007-11-09 14:49:23 +01001622 return;
1623
Alan Cox33dd4742009-01-02 13:47:32 +00001624 /* Users cannot interrupt a close */
1625 mutex_lock(&nport->tty_sem);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001626
Alan Cox33dd4742009-01-02 13:47:32 +00001627 WARN_ON(!port->count);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001628
1629 dc->open_ttys--;
Alan Cox33dd4742009-01-02 13:47:32 +00001630 port->count--;
1631 tty_port_tty_set(port, NULL);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001632
Alan Cox33dd4742009-01-02 13:47:32 +00001633 if (port->count == 0) {
1634 DBG1("close: %d", nport->token_dl);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001635 spin_lock_irqsave(&dc->spin_mutex, flags);
Alan Cox33dd4742009-01-02 13:47:32 +00001636 dc->last_ier &= ~(nport->token_dl);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001637 writew(dc->last_ier, dc->reg_ier);
1638 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1639 }
Alan Cox33dd4742009-01-02 13:47:32 +00001640 mutex_unlock(&nport->tty_sem);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001641}
1642
1643/*
1644 * called when the userspace process writes to the tty (/dev/noz*).
1645 * Data is inserted into a fifo, which is then read and transfered to the modem.
1646 */
1647static int ntty_write(struct tty_struct *tty, const unsigned char *buffer,
1648 int count)
1649{
1650 int rval = -EINVAL;
1651 struct nozomi *dc = get_dc_by_tty(tty);
1652 struct port *port = tty->driver_data;
1653 unsigned long flags;
1654
1655 /* DBG1( "WRITEx: %d, index = %d", count, index); */
1656
1657 if (!dc || !port)
1658 return -ENODEV;
1659
1660 if (unlikely(!mutex_trylock(&port->tty_sem))) {
1661 /*
1662 * must test lock as tty layer wraps calls
1663 * to this function with BKL
1664 */
1665 dev_err(&dc->pdev->dev, "Would have deadlocked - "
1666 "return EAGAIN\n");
1667 return -EAGAIN;
1668 }
1669
Alan Cox33dd4742009-01-02 13:47:32 +00001670 if (unlikely(!port->port.count)) {
Frank Seidel20fd1e32007-11-09 14:49:23 +01001671 DBG1(" ");
1672 goto exit;
1673 }
1674
1675 rval = __kfifo_put(port->fifo_ul, (unsigned char *)buffer, count);
1676
1677 /* notify card */
1678 if (unlikely(dc == NULL)) {
1679 DBG1("No device context?");
1680 goto exit;
1681 }
1682
1683 spin_lock_irqsave(&dc->spin_mutex, flags);
1684 /* CTS is only valid on the modem channel */
1685 if (port == &(dc->port[PORT_MDM])) {
1686 if (port->ctrl_dl.CTS) {
1687 DBG4("Enable interrupt");
1688 enable_transmit_ul(tty->index % MAX_PORT, dc);
1689 } else {
1690 dev_err(&dc->pdev->dev,
1691 "CTS not active on modem port?\n");
1692 }
1693 } else {
1694 enable_transmit_ul(tty->index % MAX_PORT, dc);
1695 }
1696 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1697
1698exit:
1699 mutex_unlock(&port->tty_sem);
1700 return rval;
1701}
1702
1703/*
1704 * Calculate how much is left in device
1705 * This method is called by the upper tty layer.
1706 * #according to sources N_TTY.c it expects a value >= 0 and
1707 * does not check for negative values.
1708 */
1709static int ntty_write_room(struct tty_struct *tty)
1710{
1711 struct port *port = tty->driver_data;
1712 int room = 0;
Jan Engelhardt71e1b4a2008-02-01 09:13:53 +01001713 const struct nozomi *dc = get_dc_by_tty(tty);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001714
1715 if (!dc || !port)
1716 return 0;
1717 if (!mutex_trylock(&port->tty_sem))
1718 return 0;
1719
Alan Cox33dd4742009-01-02 13:47:32 +00001720 if (!port->port.count)
Frank Seidel20fd1e32007-11-09 14:49:23 +01001721 goto exit;
1722
1723 room = port->fifo_ul->size - __kfifo_len(port->fifo_ul);
1724
1725exit:
1726 mutex_unlock(&port->tty_sem);
1727 return room;
1728}
1729
1730/* Gets io control parameters */
1731static int ntty_tiocmget(struct tty_struct *tty, struct file *file)
1732{
Jan Engelhardt71e1b4a2008-02-01 09:13:53 +01001733 const struct port *port = tty->driver_data;
1734 const struct ctrl_dl *ctrl_dl = &port->ctrl_dl;
1735 const struct ctrl_ul *ctrl_ul = &port->ctrl_ul;
Frank Seidel20fd1e32007-11-09 14:49:23 +01001736
Alan Cox978e5952008-04-30 00:53:59 -07001737 /* Note: these could change under us but it is not clear this
1738 matters if so */
Frank Seidel20fd1e32007-11-09 14:49:23 +01001739 return (ctrl_ul->RTS ? TIOCM_RTS : 0) |
1740 (ctrl_ul->DTR ? TIOCM_DTR : 0) |
1741 (ctrl_dl->DCD ? TIOCM_CAR : 0) |
1742 (ctrl_dl->RI ? TIOCM_RNG : 0) |
1743 (ctrl_dl->DSR ? TIOCM_DSR : 0) |
1744 (ctrl_dl->CTS ? TIOCM_CTS : 0);
1745}
1746
1747/* Sets io controls parameters */
1748static int ntty_tiocmset(struct tty_struct *tty, struct file *file,
1749 unsigned int set, unsigned int clear)
1750{
Frank Seidel661b4e82008-03-06 21:45:57 +01001751 struct nozomi *dc = get_dc_by_tty(tty);
1752 unsigned long flags;
1753
1754 spin_lock_irqsave(&dc->spin_mutex, flags);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001755 if (set & TIOCM_RTS)
1756 set_rts(tty, 1);
1757 else if (clear & TIOCM_RTS)
1758 set_rts(tty, 0);
1759
1760 if (set & TIOCM_DTR)
1761 set_dtr(tty, 1);
1762 else if (clear & TIOCM_DTR)
1763 set_dtr(tty, 0);
Frank Seidel661b4e82008-03-06 21:45:57 +01001764 spin_unlock_irqrestore(&dc->spin_mutex, flags);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001765
1766 return 0;
1767}
1768
1769static int ntty_cflags_changed(struct port *port, unsigned long flags,
1770 struct async_icount *cprev)
1771{
Frank Seidel18bbe0c22008-02-01 09:14:05 +01001772 const struct async_icount cnow = port->tty_icount;
Frank Seidel20fd1e32007-11-09 14:49:23 +01001773 int ret;
1774
1775 ret = ((flags & TIOCM_RNG) && (cnow.rng != cprev->rng)) ||
1776 ((flags & TIOCM_DSR) && (cnow.dsr != cprev->dsr)) ||
1777 ((flags & TIOCM_CD) && (cnow.dcd != cprev->dcd)) ||
1778 ((flags & TIOCM_CTS) && (cnow.cts != cprev->cts));
1779
1780 *cprev = cnow;
1781
1782 return ret;
1783}
1784
1785static int ntty_ioctl_tiocgicount(struct port *port, void __user *argp)
1786{
Frank Seidel18bbe0c22008-02-01 09:14:05 +01001787 const struct async_icount cnow = port->tty_icount;
Frank Seidel20fd1e32007-11-09 14:49:23 +01001788 struct serial_icounter_struct icount;
1789
1790 icount.cts = cnow.cts;
1791 icount.dsr = cnow.dsr;
1792 icount.rng = cnow.rng;
1793 icount.dcd = cnow.dcd;
1794 icount.rx = cnow.rx;
1795 icount.tx = cnow.tx;
1796 icount.frame = cnow.frame;
1797 icount.overrun = cnow.overrun;
1798 icount.parity = cnow.parity;
1799 icount.brk = cnow.brk;
1800 icount.buf_overrun = cnow.buf_overrun;
1801
Frank Seidel661b4e82008-03-06 21:45:57 +01001802 return copy_to_user(argp, &icount, sizeof(icount)) ? -EFAULT : 0;
Frank Seidel20fd1e32007-11-09 14:49:23 +01001803}
1804
1805static int ntty_ioctl(struct tty_struct *tty, struct file *file,
1806 unsigned int cmd, unsigned long arg)
1807{
1808 struct port *port = tty->driver_data;
1809 void __user *argp = (void __user *)arg;
1810 int rval = -ENOIOCTLCMD;
1811
1812 DBG1("******** IOCTL, cmd: %d", cmd);
1813
1814 switch (cmd) {
1815 case TIOCMIWAIT: {
1816 struct async_icount cprev = port->tty_icount;
1817
1818 rval = wait_event_interruptible(port->tty_wait,
1819 ntty_cflags_changed(port, arg, &cprev));
1820 break;
1821 } case TIOCGICOUNT:
1822 rval = ntty_ioctl_tiocgicount(port, argp);
1823 break;
1824 default:
1825 DBG1("ERR: 0x%08X, %d", cmd, cmd);
1826 break;
1827 };
1828
1829 return rval;
1830}
1831
1832/*
1833 * Called by the upper tty layer when tty buffers are ready
1834 * to receive data again after a call to throttle.
1835 */
1836static void ntty_unthrottle(struct tty_struct *tty)
1837{
1838 struct nozomi *dc = get_dc_by_tty(tty);
1839 unsigned long flags;
1840
1841 DBG1("UNTHROTTLE");
1842 spin_lock_irqsave(&dc->spin_mutex, flags);
1843 enable_transmit_dl(tty->index % MAX_PORT, dc);
1844 set_rts(tty, 1);
1845
1846 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1847}
1848
1849/*
1850 * Called by the upper tty layer when the tty buffers are almost full.
1851 * The driver should stop send more data.
1852 */
1853static void ntty_throttle(struct tty_struct *tty)
1854{
1855 struct nozomi *dc = get_dc_by_tty(tty);
1856 unsigned long flags;
1857
1858 DBG1("THROTTLE");
1859 spin_lock_irqsave(&dc->spin_mutex, flags);
1860 set_rts(tty, 0);
1861 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1862}
1863
Frank Seidel20fd1e32007-11-09 14:49:23 +01001864/* Returns number of chars in buffer, called by tty layer */
1865static s32 ntty_chars_in_buffer(struct tty_struct *tty)
1866{
1867 struct port *port = tty->driver_data;
1868 struct nozomi *dc = get_dc_by_tty(tty);
Alan Cox23198fd2009-07-20 16:05:27 +01001869 s32 rval = 0;
Frank Seidel20fd1e32007-11-09 14:49:23 +01001870
1871 if (unlikely(!dc || !port)) {
Frank Seidel20fd1e32007-11-09 14:49:23 +01001872 goto exit_in_buffer;
1873 }
1874
Alan Cox33dd4742009-01-02 13:47:32 +00001875 if (unlikely(!port->port.count)) {
Frank Seidel20fd1e32007-11-09 14:49:23 +01001876 dev_err(&dc->pdev->dev, "No tty open?\n");
Frank Seidel20fd1e32007-11-09 14:49:23 +01001877 goto exit_in_buffer;
1878 }
1879
1880 rval = __kfifo_len(port->fifo_ul);
1881
1882exit_in_buffer:
1883 return rval;
1884}
1885
Frank Seidel18bbe0c22008-02-01 09:14:05 +01001886static const struct tty_operations tty_ops = {
Frank Seidel20fd1e32007-11-09 14:49:23 +01001887 .ioctl = ntty_ioctl,
1888 .open = ntty_open,
1889 .close = ntty_close,
1890 .write = ntty_write,
1891 .write_room = ntty_write_room,
1892 .unthrottle = ntty_unthrottle,
1893 .throttle = ntty_throttle,
1894 .chars_in_buffer = ntty_chars_in_buffer,
Frank Seidel20fd1e32007-11-09 14:49:23 +01001895 .tiocmget = ntty_tiocmget,
1896 .tiocmset = ntty_tiocmset,
1897};
1898
1899/* Module initialization */
1900static struct pci_driver nozomi_driver = {
1901 .name = NOZOMI_NAME,
1902 .id_table = nozomi_pci_tbl,
1903 .probe = nozomi_card_init,
1904 .remove = __devexit_p(nozomi_card_exit),
1905};
1906
1907static __init int nozomi_init(void)
1908{
1909 int ret;
1910
1911 printk(KERN_INFO "Initializing %s\n", VERSION_STRING);
1912
1913 ntty_driver = alloc_tty_driver(NTTY_TTY_MAXMINORS);
1914 if (!ntty_driver)
1915 return -ENOMEM;
1916
1917 ntty_driver->owner = THIS_MODULE;
1918 ntty_driver->driver_name = NOZOMI_NAME_TTY;
1919 ntty_driver->name = "noz";
1920 ntty_driver->major = 0;
1921 ntty_driver->type = TTY_DRIVER_TYPE_SERIAL;
1922 ntty_driver->subtype = SERIAL_TYPE_NORMAL;
1923 ntty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
1924 ntty_driver->init_termios = tty_std_termios;
1925 ntty_driver->init_termios.c_cflag = B115200 | CS8 | CREAD | \
1926 HUPCL | CLOCAL;
1927 ntty_driver->init_termios.c_ispeed = 115200;
1928 ntty_driver->init_termios.c_ospeed = 115200;
1929 tty_set_operations(ntty_driver, &tty_ops);
1930
1931 ret = tty_register_driver(ntty_driver);
1932 if (ret) {
1933 printk(KERN_ERR "Nozomi: failed to register ntty driver\n");
1934 goto free_tty;
1935 }
1936
1937 ret = pci_register_driver(&nozomi_driver);
1938 if (ret) {
1939 printk(KERN_ERR "Nozomi: can't register pci driver\n");
1940 goto unr_tty;
1941 }
1942
1943 return 0;
1944unr_tty:
1945 tty_unregister_driver(ntty_driver);
1946free_tty:
1947 put_tty_driver(ntty_driver);
1948 return ret;
1949}
1950
1951static __exit void nozomi_exit(void)
1952{
1953 printk(KERN_INFO "Unloading %s\n", DRIVER_DESC);
1954 pci_unregister_driver(&nozomi_driver);
1955 tty_unregister_driver(ntty_driver);
1956 put_tty_driver(ntty_driver);
1957}
1958
1959module_init(nozomi_init);
1960module_exit(nozomi_exit);
1961
1962module_param(debug, int, S_IRUGO | S_IWUSR);
1963
1964MODULE_LICENSE("Dual BSD/GPL");
1965MODULE_DESCRIPTION(DRIVER_DESC);