blob: b1aecc7bb32a263fbdc75035f65f156e1f4f2549 [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 wait_queue_head_t tty_wait;
368 struct async_icount tty_icount;
Alan Cox266794e2010-02-08 10:06:45 +0000369
370 struct nozomi *dc;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100371};
372
373/* Private data one for each card in the system */
374struct nozomi {
375 void __iomem *base_addr;
376 unsigned long flip;
377
378 /* Pointers to registers */
379 void __iomem *reg_iir;
380 void __iomem *reg_fcr;
381 void __iomem *reg_ier;
382
383 u16 last_ier;
384 enum card_type card_type;
385 struct config_table config_table; /* Configuration table */
386 struct pci_dev *pdev;
387 struct port port[NOZOMI_MAX_PORTS];
388 u8 *send_buf;
389
390 spinlock_t spin_mutex; /* secures access to registers and tty */
391
392 unsigned int index_start;
Frank Seidel661b4e82008-03-06 21:45:57 +0100393 enum card_state state;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100394 u32 open_ttys;
395};
396
397/* This is a data packet that is read or written to/from card */
398struct buffer {
399 u32 size; /* size is the length of the data buffer */
400 u8 *data;
401} __attribute__ ((packed));
402
403/* Global variables */
Frank Seidel18bbe0c22008-02-01 09:14:05 +0100404static const struct pci_device_id nozomi_pci_tbl[] __devinitconst = {
Alan Coxb2a3dbc2010-02-08 10:07:15 +0000405 {PCI_DEVICE(0x1931, 0x000c)}, /* Nozomi HSDPA */
Frank Seidel20fd1e32007-11-09 14:49:23 +0100406 {},
407};
408
409MODULE_DEVICE_TABLE(pci, nozomi_pci_tbl);
410
411static struct nozomi *ndevs[NOZOMI_MAX_CARDS];
412static struct tty_driver *ntty_driver;
413
Alan Cox266794e2010-02-08 10:06:45 +0000414static const struct tty_port_operations noz_tty_port_ops;
415
Frank Seidel20fd1e32007-11-09 14:49:23 +0100416/*
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++) {
Frank Seidel20fd1e32007-11-09 14:49:23 +0100687 memset(&dc->port[i].ctrl_dl, 0, sizeof(struct ctrl_dl));
688 memset(&dc->port[i].ctrl_ul, 0, sizeof(struct ctrl_ul));
689 }
690
691 /* Enable control channel */
692 dc->last_ier = dc->last_ier | CTRL_DL;
693 writew(dc->last_ier, dc->reg_ier);
694
Frank Seidel661b4e82008-03-06 21:45:57 +0100695 dc->state = NOZOMI_STATE_ALLOCATED;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100696 dev_info(&dc->pdev->dev, "Initialization OK!\n");
697 return 1;
698 }
699
700 if ((dc->config_table.version > 0)
701 && (dc->config_table.toggle.enabled != TOGGLE_VALID)) {
702 u32 offset = 0;
703 DBG1("First phase: pushing upload buffers, clearing download");
704
705 dev_info(&dc->pdev->dev, "Version of card: %d\n",
706 dc->config_table.version);
707
708 /* Here we should disable all I/O over F32. */
709 setup_memory(dc);
710
711 /*
712 * We should send ALL channel pair tokens back along
713 * with reset token
714 */
715
716 /* push upload modem buffers */
717 write_mem32(dc->port[PORT_MDM].ul_addr[CH_A],
718 (u32 *) &offset, 4);
719 write_mem32(dc->port[PORT_MDM].ul_addr[CH_B],
720 (u32 *) &offset, 4);
721
722 writew(MDM_UL | DIAG_DL | MDM_DL, dc->reg_fcr);
723
724 DBG1("First phase done");
725 }
726
727 return 1;
728}
729
730/* Enable uplink interrupts */
731static void enable_transmit_ul(enum port_type port, struct nozomi *dc)
732{
Jan Engelhardt71e1b4a2008-02-01 09:13:53 +0100733 static const u16 mask[] = {MDM_UL, DIAG_UL, APP1_UL, APP2_UL, CTRL_UL};
Frank Seidel20fd1e32007-11-09 14:49:23 +0100734
735 if (port < NOZOMI_MAX_PORTS) {
736 dc->last_ier |= mask[port];
737 writew(dc->last_ier, dc->reg_ier);
738 } else {
739 dev_err(&dc->pdev->dev, "Called with wrong port?\n");
740 }
741}
742
743/* Disable uplink interrupts */
744static void disable_transmit_ul(enum port_type port, struct nozomi *dc)
745{
Jan Engelhardt71e1b4a2008-02-01 09:13:53 +0100746 static const u16 mask[] =
747 {~MDM_UL, ~DIAG_UL, ~APP1_UL, ~APP2_UL, ~CTRL_UL};
Frank Seidel20fd1e32007-11-09 14:49:23 +0100748
749 if (port < NOZOMI_MAX_PORTS) {
750 dc->last_ier &= mask[port];
751 writew(dc->last_ier, dc->reg_ier);
752 } else {
753 dev_err(&dc->pdev->dev, "Called with wrong port?\n");
754 }
755}
756
757/* Enable downlink interrupts */
758static void enable_transmit_dl(enum port_type port, struct nozomi *dc)
759{
Jan Engelhardt71e1b4a2008-02-01 09:13:53 +0100760 static const u16 mask[] = {MDM_DL, DIAG_DL, APP1_DL, APP2_DL, CTRL_DL};
Frank Seidel20fd1e32007-11-09 14:49:23 +0100761
762 if (port < NOZOMI_MAX_PORTS) {
763 dc->last_ier |= mask[port];
764 writew(dc->last_ier, dc->reg_ier);
765 } else {
766 dev_err(&dc->pdev->dev, "Called with wrong port?\n");
767 }
768}
769
770/* Disable downlink interrupts */
771static void disable_transmit_dl(enum port_type port, struct nozomi *dc)
772{
Jan Engelhardt71e1b4a2008-02-01 09:13:53 +0100773 static const u16 mask[] =
774 {~MDM_DL, ~DIAG_DL, ~APP1_DL, ~APP2_DL, ~CTRL_DL};
Frank Seidel20fd1e32007-11-09 14:49:23 +0100775
776 if (port < NOZOMI_MAX_PORTS) {
777 dc->last_ier &= mask[port];
778 writew(dc->last_ier, dc->reg_ier);
779 } else {
780 dev_err(&dc->pdev->dev, "Called with wrong port?\n");
781 }
782}
783
784/*
785 * Return 1 - send buffer to card and ack.
786 * Return 0 - don't ack, don't send buffer to card.
787 */
Alan Cox33dd4742009-01-02 13:47:32 +0000788static int send_data(enum port_type index, struct nozomi *dc)
Frank Seidel20fd1e32007-11-09 14:49:23 +0100789{
790 u32 size = 0;
Alan Cox33dd4742009-01-02 13:47:32 +0000791 struct port *port = &dc->port[index];
Frank Seidel18bbe0c22008-02-01 09:14:05 +0100792 const u8 toggle = port->toggle_ul;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100793 void __iomem *addr = port->ul_addr[toggle];
Frank Seidel18bbe0c22008-02-01 09:14:05 +0100794 const u32 ul_size = port->ul_size[toggle];
Alan Cox33dd4742009-01-02 13:47:32 +0000795 struct tty_struct *tty = tty_port_tty_get(&port->port);
Frank Seidel20fd1e32007-11-09 14:49:23 +0100796
797 /* Get data from tty and place in buf for now */
Stefani Seibold7acd72e2009-12-21 14:37:28 -0800798 size = kfifo_out(&port->fifo_ul, dc->send_buf,
Frank Seidel20fd1e32007-11-09 14:49:23 +0100799 ul_size < SEND_BUF_MAX ? ul_size : SEND_BUF_MAX);
800
801 if (size == 0) {
802 DBG4("No more data to send, disable link:");
Alan Cox33dd4742009-01-02 13:47:32 +0000803 tty_kref_put(tty);
Frank Seidel20fd1e32007-11-09 14:49:23 +0100804 return 0;
805 }
806
807 /* DUMP(buf, size); */
808
809 /* Write length + data */
810 write_mem32(addr, (u32 *) &size, 4);
811 write_mem32(addr + 4, (u32 *) dc->send_buf, size);
812
813 if (tty)
814 tty_wakeup(tty);
815
Alan Cox33dd4742009-01-02 13:47:32 +0000816 tty_kref_put(tty);
Frank Seidel20fd1e32007-11-09 14:49:23 +0100817 return 1;
818}
819
820/* If all data has been read, return 1, else 0 */
821static int receive_data(enum port_type index, struct nozomi *dc)
822{
823 u8 buf[RECEIVE_BUF_MAX] = { 0 };
824 int size;
825 u32 offset = 4;
826 struct port *port = &dc->port[index];
827 void __iomem *addr = port->dl_addr[port->toggle_dl];
Alan Cox33dd4742009-01-02 13:47:32 +0000828 struct tty_struct *tty = tty_port_tty_get(&port->port);
Jiri Slaby9237a812009-07-16 16:06:18 +0100829 int i, ret;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100830
831 if (unlikely(!tty)) {
832 DBG1("tty not open for port: %d?", index);
833 return 1;
834 }
835
836 read_mem32((u32 *) &size, addr, 4);
837 /* DBG1( "%d bytes port: %d", size, index); */
838
839 if (test_bit(TTY_THROTTLED, &tty->flags)) {
840 DBG1("No room in tty, don't read data, don't ack interrupt, "
841 "disable interrupt");
842
843 /* disable interrupt in downlink... */
844 disable_transmit_dl(index, dc);
Jiri Slaby9237a812009-07-16 16:06:18 +0100845 ret = 0;
846 goto put;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100847 }
848
849 if (unlikely(size == 0)) {
850 dev_err(&dc->pdev->dev, "size == 0?\n");
Jiri Slaby9237a812009-07-16 16:06:18 +0100851 ret = 1;
852 goto put;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100853 }
854
Frank Seidel20fd1e32007-11-09 14:49:23 +0100855 while (size > 0) {
856 read_mem32((u32 *) buf, addr + offset, RECEIVE_BUF_MAX);
857
858 if (size == 1) {
859 tty_insert_flip_char(tty, buf[0], TTY_NORMAL);
860 size = 0;
861 } else if (size < RECEIVE_BUF_MAX) {
862 size -= tty_insert_flip_string(tty, (char *) buf, size);
863 } else {
864 i = tty_insert_flip_string(tty, \
865 (char *) buf, RECEIVE_BUF_MAX);
866 size -= i;
867 offset += i;
868 }
869 }
870
871 set_bit(index, &dc->flip);
Jiri Slaby9237a812009-07-16 16:06:18 +0100872 ret = 1;
873put:
Alan Cox33dd4742009-01-02 13:47:32 +0000874 tty_kref_put(tty);
Jiri Slaby9237a812009-07-16 16:06:18 +0100875 return ret;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100876}
877
878/* Debug for interrupts */
879#ifdef DEBUG
880static char *interrupt2str(u16 interrupt)
881{
882 static char buf[TMP_BUF_MAX];
883 char *p = buf;
884
885 interrupt & MDM_DL1 ? p += snprintf(p, TMP_BUF_MAX, "MDM_DL1 ") : NULL;
886 interrupt & MDM_DL2 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
887 "MDM_DL2 ") : NULL;
888
889 interrupt & MDM_UL1 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
890 "MDM_UL1 ") : NULL;
891 interrupt & MDM_UL2 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
892 "MDM_UL2 ") : NULL;
893
894 interrupt & DIAG_DL1 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
895 "DIAG_DL1 ") : NULL;
896 interrupt & DIAG_DL2 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
897 "DIAG_DL2 ") : NULL;
898
899 interrupt & DIAG_UL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
900 "DIAG_UL ") : NULL;
901
902 interrupt & APP1_DL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
903 "APP1_DL ") : NULL;
904 interrupt & APP2_DL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
905 "APP2_DL ") : NULL;
906
907 interrupt & APP1_UL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
908 "APP1_UL ") : NULL;
909 interrupt & APP2_UL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
910 "APP2_UL ") : NULL;
911
912 interrupt & CTRL_DL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
913 "CTRL_DL ") : NULL;
914 interrupt & CTRL_UL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
915 "CTRL_UL ") : NULL;
916
917 interrupt & RESET ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
918 "RESET ") : NULL;
919
920 return buf;
921}
922#endif
923
924/*
925 * Receive flow control
926 * Return 1 - If ok, else 0
927 */
928static int receive_flow_control(struct nozomi *dc)
929{
930 enum port_type port = PORT_MDM;
931 struct ctrl_dl ctrl_dl;
932 struct ctrl_dl old_ctrl;
933 u16 enable_ier = 0;
934
935 read_mem32((u32 *) &ctrl_dl, dc->port[PORT_CTRL].dl_addr[CH_A], 2);
936
937 switch (ctrl_dl.port) {
938 case CTRL_CMD:
939 DBG1("The Base Band sends this value as a response to a "
940 "request for IMSI detach sent over the control "
941 "channel uplink (see section 7.6.1).");
942 break;
943 case CTRL_MDM:
944 port = PORT_MDM;
945 enable_ier = MDM_DL;
946 break;
947 case CTRL_DIAG:
948 port = PORT_DIAG;
949 enable_ier = DIAG_DL;
950 break;
951 case CTRL_APP1:
952 port = PORT_APP1;
953 enable_ier = APP1_DL;
954 break;
955 case CTRL_APP2:
956 port = PORT_APP2;
957 enable_ier = APP2_DL;
Frank Seidel661b4e82008-03-06 21:45:57 +0100958 if (dc->state == NOZOMI_STATE_ALLOCATED) {
959 /*
960 * After card initialization the flow control
961 * received for APP2 is always the last
962 */
963 dc->state = NOZOMI_STATE_READY;
964 dev_info(&dc->pdev->dev, "Device READY!\n");
965 }
Frank Seidel20fd1e32007-11-09 14:49:23 +0100966 break;
967 default:
968 dev_err(&dc->pdev->dev,
969 "ERROR: flow control received for non-existing port\n");
970 return 0;
971 };
972
973 DBG1("0x%04X->0x%04X", *((u16 *)&dc->port[port].ctrl_dl),
974 *((u16 *)&ctrl_dl));
975
976 old_ctrl = dc->port[port].ctrl_dl;
977 dc->port[port].ctrl_dl = ctrl_dl;
978
979 if (old_ctrl.CTS == 1 && ctrl_dl.CTS == 0) {
980 DBG1("Disable interrupt (0x%04X) on port: %d",
981 enable_ier, port);
982 disable_transmit_ul(port, dc);
983
984 } else if (old_ctrl.CTS == 0 && ctrl_dl.CTS == 1) {
985
Stefani Seibolde64c0262009-12-21 14:37:28 -0800986 if (kfifo_len(&dc->port[port].fifo_ul)) {
Frank Seidel20fd1e32007-11-09 14:49:23 +0100987 DBG1("Enable interrupt (0x%04X) on port: %d",
988 enable_ier, port);
989 DBG1("Data in buffer [%d], enable transmit! ",
Stefani Seibolde64c0262009-12-21 14:37:28 -0800990 kfifo_len(&dc->port[port].fifo_ul));
Frank Seidel20fd1e32007-11-09 14:49:23 +0100991 enable_transmit_ul(port, dc);
992 } else {
993 DBG1("No data in buffer...");
994 }
995 }
996
997 if (*(u16 *)&old_ctrl == *(u16 *)&ctrl_dl) {
998 DBG1(" No change in mctrl");
999 return 1;
1000 }
1001 /* Update statistics */
1002 if (old_ctrl.CTS != ctrl_dl.CTS)
1003 dc->port[port].tty_icount.cts++;
1004 if (old_ctrl.DSR != ctrl_dl.DSR)
1005 dc->port[port].tty_icount.dsr++;
1006 if (old_ctrl.RI != ctrl_dl.RI)
1007 dc->port[port].tty_icount.rng++;
1008 if (old_ctrl.DCD != ctrl_dl.DCD)
1009 dc->port[port].tty_icount.dcd++;
1010
1011 wake_up_interruptible(&dc->port[port].tty_wait);
1012
1013 DBG1("port: %d DCD(%d), CTS(%d), RI(%d), DSR(%d)",
1014 port,
1015 dc->port[port].tty_icount.dcd, dc->port[port].tty_icount.cts,
1016 dc->port[port].tty_icount.rng, dc->port[port].tty_icount.dsr);
1017
1018 return 1;
1019}
1020
1021static enum ctrl_port_type port2ctrl(enum port_type port,
1022 const struct nozomi *dc)
1023{
1024 switch (port) {
1025 case PORT_MDM:
1026 return CTRL_MDM;
1027 case PORT_DIAG:
1028 return CTRL_DIAG;
1029 case PORT_APP1:
1030 return CTRL_APP1;
1031 case PORT_APP2:
1032 return CTRL_APP2;
1033 default:
1034 dev_err(&dc->pdev->dev,
1035 "ERROR: send flow control " \
1036 "received for non-existing port\n");
1037 };
1038 return CTRL_ERROR;
1039}
1040
1041/*
1042 * Send flow control, can only update one channel at a time
1043 * Return 0 - If we have updated all flow control
1044 * Return 1 - If we need to update more flow control, ack current enable more
1045 */
1046static int send_flow_control(struct nozomi *dc)
1047{
1048 u32 i, more_flow_control_to_be_updated = 0;
1049 u16 *ctrl;
1050
1051 for (i = PORT_MDM; i < MAX_PORT; i++) {
1052 if (dc->port[i].update_flow_control) {
1053 if (more_flow_control_to_be_updated) {
1054 /* We have more flow control to be updated */
1055 return 1;
1056 }
1057 dc->port[i].ctrl_ul.port = port2ctrl(i, dc);
1058 ctrl = (u16 *)&dc->port[i].ctrl_ul;
1059 write_mem32(dc->port[PORT_CTRL].ul_addr[0], \
1060 (u32 *) ctrl, 2);
1061 dc->port[i].update_flow_control = 0;
1062 more_flow_control_to_be_updated = 1;
1063 }
1064 }
1065 return 0;
1066}
1067
1068/*
Frank Seidele9ed5372008-01-25 21:13:24 +01001069 * Handle downlink data, ports that are handled are modem and diagnostics
Frank Seidel20fd1e32007-11-09 14:49:23 +01001070 * Return 1 - ok
1071 * Return 0 - toggle fields are out of sync
1072 */
1073static int handle_data_dl(struct nozomi *dc, enum port_type port, u8 *toggle,
1074 u16 read_iir, u16 mask1, u16 mask2)
1075{
1076 if (*toggle == 0 && read_iir & mask1) {
1077 if (receive_data(port, dc)) {
1078 writew(mask1, dc->reg_fcr);
1079 *toggle = !(*toggle);
1080 }
1081
1082 if (read_iir & mask2) {
1083 if (receive_data(port, dc)) {
1084 writew(mask2, dc->reg_fcr);
1085 *toggle = !(*toggle);
1086 }
1087 }
1088 } else if (*toggle == 1 && read_iir & mask2) {
1089 if (receive_data(port, dc)) {
1090 writew(mask2, dc->reg_fcr);
1091 *toggle = !(*toggle);
1092 }
1093
1094 if (read_iir & mask1) {
1095 if (receive_data(port, dc)) {
1096 writew(mask1, dc->reg_fcr);
1097 *toggle = !(*toggle);
1098 }
1099 }
1100 } else {
1101 dev_err(&dc->pdev->dev, "port out of sync!, toggle:%d\n",
1102 *toggle);
1103 return 0;
1104 }
1105 return 1;
1106}
1107
1108/*
1109 * Handle uplink data, this is currently for the modem port
1110 * Return 1 - ok
1111 * Return 0 - toggle field are out of sync
1112 */
1113static int handle_data_ul(struct nozomi *dc, enum port_type port, u16 read_iir)
1114{
1115 u8 *toggle = &(dc->port[port].toggle_ul);
1116
1117 if (*toggle == 0 && read_iir & MDM_UL1) {
1118 dc->last_ier &= ~MDM_UL;
1119 writew(dc->last_ier, dc->reg_ier);
1120 if (send_data(port, dc)) {
1121 writew(MDM_UL1, dc->reg_fcr);
1122 dc->last_ier = dc->last_ier | MDM_UL;
1123 writew(dc->last_ier, dc->reg_ier);
1124 *toggle = !*toggle;
1125 }
1126
1127 if (read_iir & MDM_UL2) {
1128 dc->last_ier &= ~MDM_UL;
1129 writew(dc->last_ier, dc->reg_ier);
1130 if (send_data(port, dc)) {
1131 writew(MDM_UL2, dc->reg_fcr);
1132 dc->last_ier = dc->last_ier | MDM_UL;
1133 writew(dc->last_ier, dc->reg_ier);
1134 *toggle = !*toggle;
1135 }
1136 }
1137
1138 } else if (*toggle == 1 && read_iir & MDM_UL2) {
1139 dc->last_ier &= ~MDM_UL;
1140 writew(dc->last_ier, dc->reg_ier);
1141 if (send_data(port, dc)) {
1142 writew(MDM_UL2, dc->reg_fcr);
1143 dc->last_ier = dc->last_ier | MDM_UL;
1144 writew(dc->last_ier, dc->reg_ier);
1145 *toggle = !*toggle;
1146 }
1147
1148 if (read_iir & MDM_UL1) {
1149 dc->last_ier &= ~MDM_UL;
1150 writew(dc->last_ier, dc->reg_ier);
1151 if (send_data(port, dc)) {
1152 writew(MDM_UL1, dc->reg_fcr);
1153 dc->last_ier = dc->last_ier | MDM_UL;
1154 writew(dc->last_ier, dc->reg_ier);
1155 *toggle = !*toggle;
1156 }
1157 }
1158 } else {
1159 writew(read_iir & MDM_UL, dc->reg_fcr);
1160 dev_err(&dc->pdev->dev, "port out of sync!\n");
1161 return 0;
1162 }
1163 return 1;
1164}
1165
1166static irqreturn_t interrupt_handler(int irq, void *dev_id)
1167{
1168 struct nozomi *dc = dev_id;
1169 unsigned int a;
1170 u16 read_iir;
1171
1172 if (!dc)
1173 return IRQ_NONE;
1174
1175 spin_lock(&dc->spin_mutex);
1176 read_iir = readw(dc->reg_iir);
1177
1178 /* Card removed */
1179 if (read_iir == (u16)-1)
1180 goto none;
1181 /*
1182 * Just handle interrupt enabled in IER
1183 * (by masking with dc->last_ier)
1184 */
1185 read_iir &= dc->last_ier;
1186
1187 if (read_iir == 0)
1188 goto none;
1189
1190
1191 DBG4("%s irq:0x%04X, prev:0x%04X", interrupt2str(read_iir), read_iir,
1192 dc->last_ier);
1193
1194 if (read_iir & RESET) {
1195 if (unlikely(!nozomi_read_config_table(dc))) {
1196 dc->last_ier = 0x0;
1197 writew(dc->last_ier, dc->reg_ier);
1198 dev_err(&dc->pdev->dev, "Could not read status from "
1199 "card, we should disable interface\n");
1200 } else {
1201 writew(RESET, dc->reg_fcr);
1202 }
1203 /* No more useful info if this was the reset interrupt. */
1204 goto exit_handler;
1205 }
1206 if (read_iir & CTRL_UL) {
1207 DBG1("CTRL_UL");
1208 dc->last_ier &= ~CTRL_UL;
1209 writew(dc->last_ier, dc->reg_ier);
1210 if (send_flow_control(dc)) {
1211 writew(CTRL_UL, dc->reg_fcr);
1212 dc->last_ier = dc->last_ier | CTRL_UL;
1213 writew(dc->last_ier, dc->reg_ier);
1214 }
1215 }
1216 if (read_iir & CTRL_DL) {
1217 receive_flow_control(dc);
1218 writew(CTRL_DL, dc->reg_fcr);
1219 }
1220 if (read_iir & MDM_DL) {
1221 if (!handle_data_dl(dc, PORT_MDM,
1222 &(dc->port[PORT_MDM].toggle_dl), read_iir,
1223 MDM_DL1, MDM_DL2)) {
1224 dev_err(&dc->pdev->dev, "MDM_DL out of sync!\n");
1225 goto exit_handler;
1226 }
1227 }
1228 if (read_iir & MDM_UL) {
1229 if (!handle_data_ul(dc, PORT_MDM, read_iir)) {
1230 dev_err(&dc->pdev->dev, "MDM_UL out of sync!\n");
1231 goto exit_handler;
1232 }
1233 }
1234 if (read_iir & DIAG_DL) {
1235 if (!handle_data_dl(dc, PORT_DIAG,
1236 &(dc->port[PORT_DIAG].toggle_dl), read_iir,
1237 DIAG_DL1, DIAG_DL2)) {
1238 dev_err(&dc->pdev->dev, "DIAG_DL out of sync!\n");
1239 goto exit_handler;
1240 }
1241 }
1242 if (read_iir & DIAG_UL) {
1243 dc->last_ier &= ~DIAG_UL;
1244 writew(dc->last_ier, dc->reg_ier);
1245 if (send_data(PORT_DIAG, dc)) {
1246 writew(DIAG_UL, dc->reg_fcr);
1247 dc->last_ier = dc->last_ier | DIAG_UL;
1248 writew(dc->last_ier, dc->reg_ier);
1249 }
1250 }
1251 if (read_iir & APP1_DL) {
1252 if (receive_data(PORT_APP1, dc))
1253 writew(APP1_DL, dc->reg_fcr);
1254 }
1255 if (read_iir & APP1_UL) {
1256 dc->last_ier &= ~APP1_UL;
1257 writew(dc->last_ier, dc->reg_ier);
1258 if (send_data(PORT_APP1, dc)) {
1259 writew(APP1_UL, dc->reg_fcr);
1260 dc->last_ier = dc->last_ier | APP1_UL;
1261 writew(dc->last_ier, dc->reg_ier);
1262 }
1263 }
1264 if (read_iir & APP2_DL) {
1265 if (receive_data(PORT_APP2, dc))
1266 writew(APP2_DL, dc->reg_fcr);
1267 }
1268 if (read_iir & APP2_UL) {
1269 dc->last_ier &= ~APP2_UL;
1270 writew(dc->last_ier, dc->reg_ier);
1271 if (send_data(PORT_APP2, dc)) {
1272 writew(APP2_UL, dc->reg_fcr);
1273 dc->last_ier = dc->last_ier | APP2_UL;
1274 writew(dc->last_ier, dc->reg_ier);
1275 }
1276 }
1277
1278exit_handler:
1279 spin_unlock(&dc->spin_mutex);
Alan Cox33dd4742009-01-02 13:47:32 +00001280 for (a = 0; a < NOZOMI_MAX_PORTS; a++) {
1281 struct tty_struct *tty;
1282 if (test_and_clear_bit(a, &dc->flip)) {
1283 tty = tty_port_tty_get(&dc->port[a].port);
1284 if (tty)
1285 tty_flip_buffer_push(tty);
1286 tty_kref_put(tty);
1287 }
1288 }
Frank Seidel20fd1e32007-11-09 14:49:23 +01001289 return IRQ_HANDLED;
1290none:
1291 spin_unlock(&dc->spin_mutex);
1292 return IRQ_NONE;
1293}
1294
1295static void nozomi_get_card_type(struct nozomi *dc)
1296{
1297 int i;
1298 u32 size = 0;
1299
1300 for (i = 0; i < 6; i++)
1301 size += pci_resource_len(dc->pdev, i);
1302
1303 /* Assume card type F32_8 if no match */
1304 dc->card_type = size == 2048 ? F32_2 : F32_8;
1305
1306 dev_info(&dc->pdev->dev, "Card type is: %d\n", dc->card_type);
1307}
1308
1309static void nozomi_setup_private_data(struct nozomi *dc)
1310{
1311 void __iomem *offset = dc->base_addr + dc->card_type / 2;
1312 unsigned int i;
1313
1314 dc->reg_fcr = (void __iomem *)(offset + R_FCR);
1315 dc->reg_iir = (void __iomem *)(offset + R_IIR);
1316 dc->reg_ier = (void __iomem *)(offset + R_IER);
1317 dc->last_ier = 0;
1318 dc->flip = 0;
1319
1320 dc->port[PORT_MDM].token_dl = MDM_DL;
1321 dc->port[PORT_DIAG].token_dl = DIAG_DL;
1322 dc->port[PORT_APP1].token_dl = APP1_DL;
1323 dc->port[PORT_APP2].token_dl = APP2_DL;
1324
1325 for (i = 0; i < MAX_PORT; i++)
1326 init_waitqueue_head(&dc->port[i].tty_wait);
1327}
1328
1329static ssize_t card_type_show(struct device *dev, struct device_attribute *attr,
1330 char *buf)
1331{
Jan Engelhardt71e1b4a2008-02-01 09:13:53 +01001332 const struct nozomi *dc = pci_get_drvdata(to_pci_dev(dev));
Frank Seidel20fd1e32007-11-09 14:49:23 +01001333
1334 return sprintf(buf, "%d\n", dc->card_type);
1335}
Frank Seidele9ed5372008-01-25 21:13:24 +01001336static DEVICE_ATTR(card_type, S_IRUGO, card_type_show, NULL);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001337
1338static ssize_t open_ttys_show(struct device *dev, struct device_attribute *attr,
1339 char *buf)
1340{
Jan Engelhardt71e1b4a2008-02-01 09:13:53 +01001341 const struct nozomi *dc = pci_get_drvdata(to_pci_dev(dev));
Frank Seidel20fd1e32007-11-09 14:49:23 +01001342
1343 return sprintf(buf, "%u\n", dc->open_ttys);
1344}
Frank Seidele9ed5372008-01-25 21:13:24 +01001345static DEVICE_ATTR(open_ttys, S_IRUGO, open_ttys_show, NULL);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001346
1347static void make_sysfs_files(struct nozomi *dc)
1348{
1349 if (device_create_file(&dc->pdev->dev, &dev_attr_card_type))
1350 dev_err(&dc->pdev->dev,
1351 "Could not create sysfs file for card_type\n");
1352 if (device_create_file(&dc->pdev->dev, &dev_attr_open_ttys))
1353 dev_err(&dc->pdev->dev,
1354 "Could not create sysfs file for open_ttys\n");
1355}
1356
1357static void remove_sysfs_files(struct nozomi *dc)
1358{
1359 device_remove_file(&dc->pdev->dev, &dev_attr_card_type);
1360 device_remove_file(&dc->pdev->dev, &dev_attr_open_ttys);
1361}
1362
1363/* Allocate memory for one device */
1364static int __devinit nozomi_card_init(struct pci_dev *pdev,
1365 const struct pci_device_id *ent)
1366{
1367 resource_size_t start;
1368 int ret;
1369 struct nozomi *dc = NULL;
1370 int ndev_idx;
1371 int i;
1372
1373 dev_dbg(&pdev->dev, "Init, new card found\n");
1374
1375 for (ndev_idx = 0; ndev_idx < ARRAY_SIZE(ndevs); ndev_idx++)
1376 if (!ndevs[ndev_idx])
1377 break;
1378
1379 if (ndev_idx >= ARRAY_SIZE(ndevs)) {
1380 dev_err(&pdev->dev, "no free tty range for this card left\n");
1381 ret = -EIO;
1382 goto err;
1383 }
1384
1385 dc = kzalloc(sizeof(struct nozomi), GFP_KERNEL);
1386 if (unlikely(!dc)) {
1387 dev_err(&pdev->dev, "Could not allocate memory\n");
1388 ret = -ENOMEM;
1389 goto err_free;
1390 }
1391
1392 dc->pdev = pdev;
1393
Frank Seidel20fd1e32007-11-09 14:49:23 +01001394 ret = pci_enable_device(dc->pdev);
1395 if (ret) {
1396 dev_err(&pdev->dev, "Failed to enable PCI Device\n");
1397 goto err_free;
1398 }
1399
Frank Seidel20fd1e32007-11-09 14:49:23 +01001400 ret = pci_request_regions(dc->pdev, NOZOMI_NAME);
1401 if (ret) {
1402 dev_err(&pdev->dev, "I/O address 0x%04x already in use\n",
1403 (int) /* nozomi_private.io_addr */ 0);
1404 goto err_disable_device;
1405 }
1406
Frank Seidel661b4e82008-03-06 21:45:57 +01001407 start = pci_resource_start(dc->pdev, 0);
1408 if (start == 0) {
1409 dev_err(&pdev->dev, "No I/O address for card detected\n");
1410 ret = -ENODEV;
1411 goto err_rel_regs;
1412 }
1413
1414 /* Find out what card type it is */
1415 nozomi_get_card_type(dc);
1416
Alan Cox24cb2332008-04-30 00:54:19 -07001417 dc->base_addr = ioremap_nocache(start, dc->card_type);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001418 if (!dc->base_addr) {
1419 dev_err(&pdev->dev, "Unable to map card MMIO\n");
1420 ret = -ENODEV;
1421 goto err_rel_regs;
1422 }
1423
1424 dc->send_buf = kmalloc(SEND_BUF_MAX, GFP_KERNEL);
1425 if (!dc->send_buf) {
1426 dev_err(&pdev->dev, "Could not allocate send buffer?\n");
1427 ret = -ENOMEM;
1428 goto err_free_sbuf;
1429 }
1430
Stefani Seibold9842c382009-12-21 14:37:29 -08001431 for (i = PORT_MDM; i < MAX_PORT; i++) {
Jiri Slabyc29bd8d2011-04-20 10:43:12 +02001432 if (kfifo_alloc(&dc->port[i].fifo_ul, FIFO_BUFFER_SIZE_UL,
1433 GFP_KERNEL)) {
Stefani Seibold9842c382009-12-21 14:37:29 -08001434 dev_err(&pdev->dev,
1435 "Could not allocate kfifo buffer\n");
1436 ret = -ENOMEM;
1437 goto err_free_kfifo;
1438 }
1439 }
1440
Frank Seidel20fd1e32007-11-09 14:49:23 +01001441 spin_lock_init(&dc->spin_mutex);
1442
1443 nozomi_setup_private_data(dc);
1444
1445 /* Disable all interrupts */
1446 dc->last_ier = 0;
1447 writew(dc->last_ier, dc->reg_ier);
1448
1449 ret = request_irq(pdev->irq, &interrupt_handler, IRQF_SHARED,
1450 NOZOMI_NAME, dc);
1451 if (unlikely(ret)) {
1452 dev_err(&pdev->dev, "can't request irq %d\n", pdev->irq);
Stefani Seibold9842c382009-12-21 14:37:29 -08001453 goto err_free_kfifo;
Frank Seidel20fd1e32007-11-09 14:49:23 +01001454 }
1455
1456 DBG1("base_addr: %p", dc->base_addr);
1457
1458 make_sysfs_files(dc);
1459
1460 dc->index_start = ndev_idx * MAX_PORT;
1461 ndevs[ndev_idx] = dc;
1462
Frank Seidel661b4e82008-03-06 21:45:57 +01001463 pci_set_drvdata(pdev, dc);
1464
1465 /* Enable RESET interrupt */
1466 dc->last_ier = RESET;
1467 iowrite16(dc->last_ier, dc->reg_ier);
1468
1469 dc->state = NOZOMI_STATE_ENABLED;
1470
Frank Seidel20fd1e32007-11-09 14:49:23 +01001471 for (i = 0; i < MAX_PORT; i++) {
Stefani Seibold9842c382009-12-21 14:37:29 -08001472 struct device *tty_dev;
Alan Cox266794e2010-02-08 10:06:45 +00001473 struct port *port = &dc->port[i];
1474 port->dc = dc;
Alan Cox266794e2010-02-08 10:06:45 +00001475 tty_port_init(&port->port);
1476 port->port.ops = &noz_tty_port_ops;
Stefani Seibold9842c382009-12-21 14:37:29 -08001477 tty_dev = tty_register_device(ntty_driver, dc->index_start + i,
Frank Seidel20fd1e32007-11-09 14:49:23 +01001478 &pdev->dev);
Stefani Seibold9842c382009-12-21 14:37:29 -08001479
1480 if (IS_ERR(tty_dev)) {
1481 ret = PTR_ERR(tty_dev);
1482 dev_err(&pdev->dev, "Could not allocate tty?\n");
1483 goto err_free_tty;
1484 }
Frank Seidel20fd1e32007-11-09 14:49:23 +01001485 }
Stefani Seibold9842c382009-12-21 14:37:29 -08001486
Frank Seidel20fd1e32007-11-09 14:49:23 +01001487 return 0;
1488
Stefani Seibold9842c382009-12-21 14:37:29 -08001489err_free_tty:
1490 for (i = dc->index_start; i < dc->index_start + MAX_PORT; ++i)
1491 tty_unregister_device(ntty_driver, i);
1492err_free_kfifo:
1493 for (i = 0; i < MAX_PORT; i++)
1494 kfifo_free(&dc->port[i].fifo_ul);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001495err_free_sbuf:
1496 kfree(dc->send_buf);
1497 iounmap(dc->base_addr);
1498err_rel_regs:
1499 pci_release_regions(pdev);
1500err_disable_device:
1501 pci_disable_device(pdev);
1502err_free:
1503 kfree(dc);
1504err:
1505 return ret;
1506}
1507
1508static void __devexit tty_exit(struct nozomi *dc)
1509{
1510 unsigned int i;
1511
1512 DBG1(" ");
1513
Alan Cox33dd4742009-01-02 13:47:32 +00001514 for (i = 0; i < MAX_PORT; ++i) {
1515 struct tty_struct *tty = tty_port_tty_get(&dc->port[i].port);
1516 if (tty && list_empty(&tty->hangup_work.entry))
1517 tty_hangup(tty);
1518 tty_kref_put(tty);
1519 }
1520 /* Racy below - surely should wait for scheduled work to be done or
1521 complete off a hangup method ? */
Frank Seidel20fd1e32007-11-09 14:49:23 +01001522 while (dc->open_ttys)
1523 msleep(1);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001524 for (i = dc->index_start; i < dc->index_start + MAX_PORT; ++i)
1525 tty_unregister_device(ntty_driver, i);
1526}
1527
1528/* Deallocate memory for one device */
1529static void __devexit nozomi_card_exit(struct pci_dev *pdev)
1530{
1531 int i;
1532 struct ctrl_ul ctrl;
1533 struct nozomi *dc = pci_get_drvdata(pdev);
1534
1535 /* Disable all interrupts */
1536 dc->last_ier = 0;
1537 writew(dc->last_ier, dc->reg_ier);
1538
1539 tty_exit(dc);
1540
1541 /* Send 0x0001, command card to resend the reset token. */
1542 /* This is to get the reset when the module is reloaded. */
1543 ctrl.port = 0x00;
1544 ctrl.reserved = 0;
1545 ctrl.RTS = 0;
1546 ctrl.DTR = 1;
1547 DBG1("sending flow control 0x%04X", *((u16 *)&ctrl));
1548
1549 /* Setup dc->reg addresses to we can use defines here */
1550 write_mem32(dc->port[PORT_CTRL].ul_addr[0], (u32 *)&ctrl, 2);
1551 writew(CTRL_UL, dc->reg_fcr); /* push the token to the card. */
1552
1553 remove_sysfs_files(dc);
1554
1555 free_irq(pdev->irq, dc);
1556
1557 for (i = 0; i < MAX_PORT; i++)
Stefani Seibold45465482009-12-21 14:37:26 -08001558 kfifo_free(&dc->port[i].fifo_ul);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001559
1560 kfree(dc->send_buf);
1561
1562 iounmap(dc->base_addr);
1563
1564 pci_release_regions(pdev);
1565
1566 pci_disable_device(pdev);
1567
1568 ndevs[dc->index_start / MAX_PORT] = NULL;
1569
1570 kfree(dc);
1571}
1572
1573static void set_rts(const struct tty_struct *tty, int rts)
1574{
1575 struct port *port = get_port_by_tty(tty);
1576
1577 port->ctrl_ul.RTS = rts;
1578 port->update_flow_control = 1;
1579 enable_transmit_ul(PORT_CTRL, get_dc_by_tty(tty));
1580}
1581
1582static void set_dtr(const struct tty_struct *tty, int dtr)
1583{
1584 struct port *port = get_port_by_tty(tty);
1585
1586 DBG1("SETTING DTR index: %d, dtr: %d", tty->index, dtr);
1587
1588 port->ctrl_ul.DTR = dtr;
1589 port->update_flow_control = 1;
1590 enable_transmit_ul(PORT_CTRL, get_dc_by_tty(tty));
1591}
1592
1593/*
1594 * ----------------------------------------------------------------------------
1595 * TTY code
1596 * ----------------------------------------------------------------------------
1597 */
1598
Alan Cox266794e2010-02-08 10:06:45 +00001599static int ntty_install(struct tty_driver *driver, struct tty_struct *tty)
Frank Seidel20fd1e32007-11-09 14:49:23 +01001600{
1601 struct port *port = get_port_by_tty(tty);
1602 struct nozomi *dc = get_dc_by_tty(tty);
Alan Cox266794e2010-02-08 10:06:45 +00001603 int ret;
Frank Seidel661b4e82008-03-06 21:45:57 +01001604 if (!port || !dc || dc->state != NOZOMI_STATE_READY)
Frank Seidel20fd1e32007-11-09 14:49:23 +01001605 return -ENODEV;
Alan Cox266794e2010-02-08 10:06:45 +00001606 ret = tty_init_termios(tty);
1607 if (ret == 0) {
1608 tty_driver_kref_get(driver);
Jiri Slabyee78bb92010-07-29 11:09:47 +02001609 tty->count++;
Jiri Slabybf9c1fc2010-07-29 11:46:32 +02001610 tty->driver_data = port;
Alan Cox266794e2010-02-08 10:06:45 +00001611 driver->ttys[tty->index] = tty;
Frank Seidel20fd1e32007-11-09 14:49:23 +01001612 }
Alan Cox266794e2010-02-08 10:06:45 +00001613 return ret;
1614}
1615
1616static void ntty_cleanup(struct tty_struct *tty)
1617{
1618 tty->driver_data = NULL;
1619}
1620
1621static int ntty_activate(struct tty_port *tport, struct tty_struct *tty)
1622{
1623 struct port *port = container_of(tport, struct port, port);
1624 struct nozomi *dc = port->dc;
1625 unsigned long flags;
1626
1627 DBG1("open: %d", port->token_dl);
1628 spin_lock_irqsave(&dc->spin_mutex, flags);
1629 dc->last_ier = dc->last_ier | port->token_dl;
1630 writew(dc->last_ier, dc->reg_ier);
1631 dc->open_ttys++;
1632 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1633 printk("noz: activated %d: %p\n", tty->index, tport);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001634 return 0;
1635}
1636
Alan Cox266794e2010-02-08 10:06:45 +00001637static int ntty_open(struct tty_struct *tty, struct file *filp)
Frank Seidel20fd1e32007-11-09 14:49:23 +01001638{
Jiri Slabybf9c1fc2010-07-29 11:46:32 +02001639 struct port *port = tty->driver_data;
Alan Cox266794e2010-02-08 10:06:45 +00001640 return tty_port_open(&port->port, tty, filp);
1641}
1642
1643static void ntty_shutdown(struct tty_port *tport)
1644{
1645 struct port *port = container_of(tport, struct port, port);
1646 struct nozomi *dc = port->dc;
Frank Seidel20fd1e32007-11-09 14:49:23 +01001647 unsigned long flags;
1648
Alan Cox266794e2010-02-08 10:06:45 +00001649 DBG1("close: %d", port->token_dl);
1650 spin_lock_irqsave(&dc->spin_mutex, flags);
1651 dc->last_ier &= ~(port->token_dl);
1652 writew(dc->last_ier, dc->reg_ier);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001653 dc->open_ttys--;
Alan Cox266794e2010-02-08 10:06:45 +00001654 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1655 printk("noz: shutdown %p\n", tport);
1656}
Frank Seidel20fd1e32007-11-09 14:49:23 +01001657
Alan Cox266794e2010-02-08 10:06:45 +00001658static void ntty_close(struct tty_struct *tty, struct file *filp)
1659{
1660 struct port *port = tty->driver_data;
1661 if (port)
1662 tty_port_close(&port->port, tty, filp);
1663}
1664
1665static void ntty_hangup(struct tty_struct *tty)
1666{
1667 struct port *port = tty->driver_data;
1668 tty_port_hangup(&port->port);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001669}
1670
1671/*
1672 * called when the userspace process writes to the tty (/dev/noz*).
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001673 * Data is inserted into a fifo, which is then read and transferred to the modem.
Frank Seidel20fd1e32007-11-09 14:49:23 +01001674 */
1675static int ntty_write(struct tty_struct *tty, const unsigned char *buffer,
1676 int count)
1677{
1678 int rval = -EINVAL;
1679 struct nozomi *dc = get_dc_by_tty(tty);
1680 struct port *port = tty->driver_data;
1681 unsigned long flags;
1682
1683 /* DBG1( "WRITEx: %d, index = %d", count, index); */
1684
1685 if (!dc || !port)
1686 return -ENODEV;
1687
Stefani Seibold7acd72e2009-12-21 14:37:28 -08001688 rval = kfifo_in(&port->fifo_ul, (unsigned char *)buffer, count);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001689
1690 /* notify card */
1691 if (unlikely(dc == NULL)) {
1692 DBG1("No device context?");
1693 goto exit;
1694 }
1695
1696 spin_lock_irqsave(&dc->spin_mutex, flags);
1697 /* CTS is only valid on the modem channel */
1698 if (port == &(dc->port[PORT_MDM])) {
1699 if (port->ctrl_dl.CTS) {
1700 DBG4("Enable interrupt");
1701 enable_transmit_ul(tty->index % MAX_PORT, dc);
1702 } else {
1703 dev_err(&dc->pdev->dev,
1704 "CTS not active on modem port?\n");
1705 }
1706 } else {
1707 enable_transmit_ul(tty->index % MAX_PORT, dc);
1708 }
1709 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1710
1711exit:
Frank Seidel20fd1e32007-11-09 14:49:23 +01001712 return rval;
1713}
1714
1715/*
1716 * Calculate how much is left in device
1717 * This method is called by the upper tty layer.
1718 * #according to sources N_TTY.c it expects a value >= 0 and
1719 * does not check for negative values.
Alan Coxe8c65d12010-02-08 10:07:04 +00001720 *
1721 * If the port is unplugged report lots of room and let the bits
1722 * dribble away so we don't block anything.
Frank Seidel20fd1e32007-11-09 14:49:23 +01001723 */
1724static int ntty_write_room(struct tty_struct *tty)
1725{
1726 struct port *port = tty->driver_data;
Alan Coxe8c65d12010-02-08 10:07:04 +00001727 int room = 4096;
Jan Engelhardt71e1b4a2008-02-01 09:13:53 +01001728 const struct nozomi *dc = get_dc_by_tty(tty);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001729
Jiri Slaby7fdc2892011-04-20 10:43:14 +02001730 if (dc)
Jiri Slaby6d742f62011-04-20 10:43:13 +02001731 room = kfifo_avail(&port->fifo_ul);
Jiri Slaby7fdc2892011-04-20 10:43:14 +02001732
Frank Seidel20fd1e32007-11-09 14:49:23 +01001733 return room;
1734}
1735
1736/* Gets io control parameters */
Alan Cox60b33c12011-02-14 16:26:14 +00001737static int ntty_tiocmget(struct tty_struct *tty)
Frank Seidel20fd1e32007-11-09 14:49:23 +01001738{
Jan Engelhardt71e1b4a2008-02-01 09:13:53 +01001739 const struct port *port = tty->driver_data;
1740 const struct ctrl_dl *ctrl_dl = &port->ctrl_dl;
1741 const struct ctrl_ul *ctrl_ul = &port->ctrl_ul;
Frank Seidel20fd1e32007-11-09 14:49:23 +01001742
Alan Cox978e5952008-04-30 00:53:59 -07001743 /* Note: these could change under us but it is not clear this
1744 matters if so */
Frank Seidel20fd1e32007-11-09 14:49:23 +01001745 return (ctrl_ul->RTS ? TIOCM_RTS : 0) |
1746 (ctrl_ul->DTR ? TIOCM_DTR : 0) |
1747 (ctrl_dl->DCD ? TIOCM_CAR : 0) |
1748 (ctrl_dl->RI ? TIOCM_RNG : 0) |
1749 (ctrl_dl->DSR ? TIOCM_DSR : 0) |
1750 (ctrl_dl->CTS ? TIOCM_CTS : 0);
1751}
1752
1753/* Sets io controls parameters */
Alan Cox20b9d172011-02-14 16:26:50 +00001754static int ntty_tiocmset(struct tty_struct *tty,
1755 unsigned int set, unsigned int clear)
Frank Seidel20fd1e32007-11-09 14:49:23 +01001756{
Frank Seidel661b4e82008-03-06 21:45:57 +01001757 struct nozomi *dc = get_dc_by_tty(tty);
1758 unsigned long flags;
1759
1760 spin_lock_irqsave(&dc->spin_mutex, flags);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001761 if (set & TIOCM_RTS)
1762 set_rts(tty, 1);
1763 else if (clear & TIOCM_RTS)
1764 set_rts(tty, 0);
1765
1766 if (set & TIOCM_DTR)
1767 set_dtr(tty, 1);
1768 else if (clear & TIOCM_DTR)
1769 set_dtr(tty, 0);
Frank Seidel661b4e82008-03-06 21:45:57 +01001770 spin_unlock_irqrestore(&dc->spin_mutex, flags);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001771
1772 return 0;
1773}
1774
1775static int ntty_cflags_changed(struct port *port, unsigned long flags,
1776 struct async_icount *cprev)
1777{
Frank Seidel18bbe0c22008-02-01 09:14:05 +01001778 const struct async_icount cnow = port->tty_icount;
Frank Seidel20fd1e32007-11-09 14:49:23 +01001779 int ret;
1780
1781 ret = ((flags & TIOCM_RNG) && (cnow.rng != cprev->rng)) ||
1782 ((flags & TIOCM_DSR) && (cnow.dsr != cprev->dsr)) ||
1783 ((flags & TIOCM_CD) && (cnow.dcd != cprev->dcd)) ||
1784 ((flags & TIOCM_CTS) && (cnow.cts != cprev->cts));
1785
1786 *cprev = cnow;
1787
1788 return ret;
1789}
1790
Alan Cox05871022010-09-16 18:21:52 +01001791static int ntty_tiocgicount(struct tty_struct *tty,
1792 struct serial_icounter_struct *icount)
Frank Seidel20fd1e32007-11-09 14:49:23 +01001793{
Alan Cox05871022010-09-16 18:21:52 +01001794 struct port *port = tty->driver_data;
Frank Seidel18bbe0c22008-02-01 09:14:05 +01001795 const struct async_icount cnow = port->tty_icount;
Frank Seidel20fd1e32007-11-09 14:49:23 +01001796
Alan Cox05871022010-09-16 18:21:52 +01001797 icount->cts = cnow.cts;
1798 icount->dsr = cnow.dsr;
1799 icount->rng = cnow.rng;
1800 icount->dcd = cnow.dcd;
1801 icount->rx = cnow.rx;
1802 icount->tx = cnow.tx;
1803 icount->frame = cnow.frame;
1804 icount->overrun = cnow.overrun;
1805 icount->parity = cnow.parity;
1806 icount->brk = cnow.brk;
1807 icount->buf_overrun = cnow.buf_overrun;
1808 return 0;
Frank Seidel20fd1e32007-11-09 14:49:23 +01001809}
1810
Alan Cox6caa76b2011-02-14 16:27:22 +00001811static int ntty_ioctl(struct tty_struct *tty,
Frank Seidel20fd1e32007-11-09 14:49:23 +01001812 unsigned int cmd, unsigned long arg)
1813{
1814 struct port *port = tty->driver_data;
Frank Seidel20fd1e32007-11-09 14:49:23 +01001815 int rval = -ENOIOCTLCMD;
1816
1817 DBG1("******** IOCTL, cmd: %d", cmd);
1818
1819 switch (cmd) {
1820 case TIOCMIWAIT: {
1821 struct async_icount cprev = port->tty_icount;
1822
1823 rval = wait_event_interruptible(port->tty_wait,
1824 ntty_cflags_changed(port, arg, &cprev));
1825 break;
Alan Cox05871022010-09-16 18:21:52 +01001826 }
Frank Seidel20fd1e32007-11-09 14:49:23 +01001827 default:
1828 DBG1("ERR: 0x%08X, %d", cmd, cmd);
1829 break;
1830 };
1831
1832 return rval;
1833}
1834
1835/*
1836 * Called by the upper tty layer when tty buffers are ready
1837 * to receive data again after a call to throttle.
1838 */
1839static void ntty_unthrottle(struct tty_struct *tty)
1840{
1841 struct nozomi *dc = get_dc_by_tty(tty);
1842 unsigned long flags;
1843
1844 DBG1("UNTHROTTLE");
1845 spin_lock_irqsave(&dc->spin_mutex, flags);
1846 enable_transmit_dl(tty->index % MAX_PORT, dc);
1847 set_rts(tty, 1);
1848
1849 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1850}
1851
1852/*
1853 * Called by the upper tty layer when the tty buffers are almost full.
1854 * The driver should stop send more data.
1855 */
1856static void ntty_throttle(struct tty_struct *tty)
1857{
1858 struct nozomi *dc = get_dc_by_tty(tty);
1859 unsigned long flags;
1860
1861 DBG1("THROTTLE");
1862 spin_lock_irqsave(&dc->spin_mutex, flags);
1863 set_rts(tty, 0);
1864 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1865}
1866
Frank Seidel20fd1e32007-11-09 14:49:23 +01001867/* Returns number of chars in buffer, called by tty layer */
1868static s32 ntty_chars_in_buffer(struct tty_struct *tty)
1869{
1870 struct port *port = tty->driver_data;
1871 struct nozomi *dc = get_dc_by_tty(tty);
Alan Cox23198fd2009-07-20 16:05:27 +01001872 s32 rval = 0;
Frank Seidel20fd1e32007-11-09 14:49:23 +01001873
1874 if (unlikely(!dc || !port)) {
Frank Seidel20fd1e32007-11-09 14:49:23 +01001875 goto exit_in_buffer;
1876 }
1877
Stefani Seibolde64c0262009-12-21 14:37:28 -08001878 rval = kfifo_len(&port->fifo_ul);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001879
1880exit_in_buffer:
1881 return rval;
1882}
1883
Alan Cox266794e2010-02-08 10:06:45 +00001884static const struct tty_port_operations noz_tty_port_ops = {
1885 .activate = ntty_activate,
1886 .shutdown = ntty_shutdown,
1887};
1888
Frank Seidel18bbe0c22008-02-01 09:14:05 +01001889static const struct tty_operations tty_ops = {
Frank Seidel20fd1e32007-11-09 14:49:23 +01001890 .ioctl = ntty_ioctl,
1891 .open = ntty_open,
1892 .close = ntty_close,
Alan Cox266794e2010-02-08 10:06:45 +00001893 .hangup = ntty_hangup,
Frank Seidel20fd1e32007-11-09 14:49:23 +01001894 .write = ntty_write,
1895 .write_room = ntty_write_room,
1896 .unthrottle = ntty_unthrottle,
1897 .throttle = ntty_throttle,
1898 .chars_in_buffer = ntty_chars_in_buffer,
Frank Seidel20fd1e32007-11-09 14:49:23 +01001899 .tiocmget = ntty_tiocmget,
1900 .tiocmset = ntty_tiocmset,
Alan Cox05871022010-09-16 18:21:52 +01001901 .get_icount = ntty_tiocgicount,
Alan Cox266794e2010-02-08 10:06:45 +00001902 .install = ntty_install,
1903 .cleanup = ntty_cleanup,
Frank Seidel20fd1e32007-11-09 14:49:23 +01001904};
1905
1906/* Module initialization */
1907static struct pci_driver nozomi_driver = {
1908 .name = NOZOMI_NAME,
1909 .id_table = nozomi_pci_tbl,
1910 .probe = nozomi_card_init,
1911 .remove = __devexit_p(nozomi_card_exit),
1912};
1913
1914static __init int nozomi_init(void)
1915{
1916 int ret;
1917
1918 printk(KERN_INFO "Initializing %s\n", VERSION_STRING);
1919
1920 ntty_driver = alloc_tty_driver(NTTY_TTY_MAXMINORS);
1921 if (!ntty_driver)
1922 return -ENOMEM;
1923
1924 ntty_driver->owner = THIS_MODULE;
1925 ntty_driver->driver_name = NOZOMI_NAME_TTY;
1926 ntty_driver->name = "noz";
1927 ntty_driver->major = 0;
1928 ntty_driver->type = TTY_DRIVER_TYPE_SERIAL;
1929 ntty_driver->subtype = SERIAL_TYPE_NORMAL;
1930 ntty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
1931 ntty_driver->init_termios = tty_std_termios;
1932 ntty_driver->init_termios.c_cflag = B115200 | CS8 | CREAD | \
1933 HUPCL | CLOCAL;
1934 ntty_driver->init_termios.c_ispeed = 115200;
1935 ntty_driver->init_termios.c_ospeed = 115200;
1936 tty_set_operations(ntty_driver, &tty_ops);
1937
1938 ret = tty_register_driver(ntty_driver);
1939 if (ret) {
1940 printk(KERN_ERR "Nozomi: failed to register ntty driver\n");
1941 goto free_tty;
1942 }
1943
1944 ret = pci_register_driver(&nozomi_driver);
1945 if (ret) {
1946 printk(KERN_ERR "Nozomi: can't register pci driver\n");
1947 goto unr_tty;
1948 }
1949
1950 return 0;
1951unr_tty:
1952 tty_unregister_driver(ntty_driver);
1953free_tty:
1954 put_tty_driver(ntty_driver);
1955 return ret;
1956}
1957
1958static __exit void nozomi_exit(void)
1959{
1960 printk(KERN_INFO "Unloading %s\n", DRIVER_DESC);
1961 pci_unregister_driver(&nozomi_driver);
1962 tty_unregister_driver(ntty_driver);
1963 put_tty_driver(ntty_driver);
1964}
1965
1966module_init(nozomi_init);
1967module_exit(nozomi_exit);
1968
1969module_param(debug, int, S_IRUGO | S_IWUSR);
1970
1971MODULE_LICENSE("Dual BSD/GPL");
1972MODULE_DESCRIPTION(DRIVER_DESC);