blob: 39b3723a32a63f1275cfac856bd344d5d8f98344 [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
Michal Marek2cae8de2011-04-01 12:41:20 +020064#define VERSION_STRING DRIVER_DESC " 2.1d"
Frank Seidel20fd1e32007-11-09 14:49:23 +010065
Frank Seidel20fd1e32007-11-09 14:49:23 +010066/* Default debug printout level */
67#define NOZOMI_DEBUG_LEVEL 0x00
Arnd Bergmann5020ded2016-11-29 12:51:04 +010068static int debug = NOZOMI_DEBUG_LEVEL;
69module_param(debug, int, S_IRUGO | S_IWUSR);
Frank Seidel20fd1e32007-11-09 14:49:23 +010070
Arnd Bergmann5020ded2016-11-29 12:51:04 +010071/* Macros definitions */
72#define DBG_(lvl, fmt, args...) \
73do { \
74 if (lvl & debug) \
75 pr_debug("[%d] %s(): " fmt "\n", \
76 __LINE__, __func__, ##args); \
Frank Seidel20fd1e32007-11-09 14:49:23 +010077} while (0)
78
Arnd Bergmann5020ded2016-11-29 12:51:04 +010079#define DBG1(args...) DBG_(0x01, ##args)
80#define DBG2(args...) DBG_(0x02, ##args)
81#define DBG3(args...) DBG_(0x04, ##args)
82#define DBG4(args...) DBG_(0x08, ##args)
Frank Seidel20fd1e32007-11-09 14:49:23 +010083
84/* TODO: rewrite to optimize macros... */
85
86#define TMP_BUF_MAX 256
87
88#define DUMP(buf__,len__) \
89 do { \
90 char tbuf[TMP_BUF_MAX] = {0};\
91 if (len__ > 1) {\
92 snprintf(tbuf, len__ > TMP_BUF_MAX ? TMP_BUF_MAX : len__, "%s", buf__);\
93 if (tbuf[len__-2] == '\r') {\
94 tbuf[len__-2] = 'r';\
95 } \
96 DBG1("SENDING: '%s' (%d+n)", tbuf, len__);\
97 } else {\
98 DBG1("SENDING: '%s' (%d)", tbuf, len__);\
99 } \
100} while (0)
101
102/* Defines */
103#define NOZOMI_NAME "nozomi"
104#define NOZOMI_NAME_TTY "nozomi_tty"
105#define DRIVER_DESC "Nozomi driver"
106
107#define NTTY_TTY_MAXMINORS 256
108#define NTTY_FIFO_BUFFER_SIZE 8192
109
110/* Must be power of 2 */
111#define FIFO_BUFFER_SIZE_UL 8192
112
113/* Size of tmp send buffer to card */
114#define SEND_BUF_MAX 1024
115#define RECEIVE_BUF_MAX 4
116
117
Frank Seidel20fd1e32007-11-09 14:49:23 +0100118#define R_IIR 0x0000 /* Interrupt Identity Register */
119#define R_FCR 0x0000 /* Flow Control Register */
120#define R_IER 0x0004 /* Interrupt Enable Register */
121
Valentin Rothberg03ac6b32015-04-12 18:35:18 +0200122#define NOZOMI_CONFIG_MAGIC 0xEFEFFEFE
123#define TOGGLE_VALID 0x0000
Frank Seidel20fd1e32007-11-09 14:49:23 +0100124
125/* Definition of interrupt tokens */
126#define MDM_DL1 0x0001
127#define MDM_UL1 0x0002
128#define MDM_DL2 0x0004
129#define MDM_UL2 0x0008
130#define DIAG_DL1 0x0010
131#define DIAG_DL2 0x0020
132#define DIAG_UL 0x0040
133#define APP1_DL 0x0080
134#define APP1_UL 0x0100
135#define APP2_DL 0x0200
136#define APP2_UL 0x0400
137#define CTRL_DL 0x0800
138#define CTRL_UL 0x1000
139#define RESET 0x8000
140
141#define MDM_DL (MDM_DL1 | MDM_DL2)
142#define MDM_UL (MDM_UL1 | MDM_UL2)
143#define DIAG_DL (DIAG_DL1 | DIAG_DL2)
144
145/* modem signal definition */
146#define CTRL_DSR 0x0001
147#define CTRL_DCD 0x0002
148#define CTRL_RI 0x0004
149#define CTRL_CTS 0x0008
150
151#define CTRL_DTR 0x0001
152#define CTRL_RTS 0x0002
153
154#define MAX_PORT 4
155#define NOZOMI_MAX_PORTS 5
156#define NOZOMI_MAX_CARDS (NTTY_TTY_MAXMINORS / MAX_PORT)
157
158/* Type definitions */
159
160/*
161 * There are two types of nozomi cards,
162 * one with 2048 memory and with 8192 memory
163 */
164enum card_type {
165 F32_2 = 2048, /* 512 bytes downlink + uplink * 2 -> 2048 */
166 F32_8 = 8192, /* 3072 bytes downl. + 1024 bytes uplink * 2 -> 8192 */
167};
168
Frank Seidel661b4e82008-03-06 21:45:57 +0100169/* Initialization states a card can be in */
170enum card_state {
171 NOZOMI_STATE_UKNOWN = 0,
172 NOZOMI_STATE_ENABLED = 1, /* pci device enabled */
173 NOZOMI_STATE_ALLOCATED = 2, /* config setup done */
174 NOZOMI_STATE_READY = 3, /* flowcontrols received */
175};
176
Frank Seidel20fd1e32007-11-09 14:49:23 +0100177/* Two different toggle channels exist */
178enum channel_type {
179 CH_A = 0,
180 CH_B = 1,
181};
182
183/* Port definition for the card regarding flow control */
184enum ctrl_port_type {
185 CTRL_CMD = 0,
186 CTRL_MDM = 1,
187 CTRL_DIAG = 2,
188 CTRL_APP1 = 3,
189 CTRL_APP2 = 4,
190 CTRL_ERROR = -1,
191};
192
193/* Ports that the nozomi has */
194enum port_type {
195 PORT_MDM = 0,
196 PORT_DIAG = 1,
197 PORT_APP1 = 2,
198 PORT_APP2 = 3,
199 PORT_CTRL = 4,
200 PORT_ERROR = -1,
201};
202
203#ifdef __BIG_ENDIAN
204/* Big endian */
205
206struct toggles {
Frank Seidele9ed5372008-01-25 21:13:24 +0100207 unsigned int enabled:5; /*
Frank Seidel20fd1e32007-11-09 14:49:23 +0100208 * Toggle fields are valid if enabled is 0,
209 * else A-channels must always be used.
210 */
Frank Seidele9ed5372008-01-25 21:13:24 +0100211 unsigned int diag_dl:1;
212 unsigned int mdm_dl:1;
213 unsigned int mdm_ul:1;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100214} __attribute__ ((packed));
215
216/* Configuration table to read at startup of card */
217/* Is for now only needed during initialization phase */
218struct config_table {
219 u32 signature;
220 u16 product_information;
221 u16 version;
222 u8 pad3[3];
223 struct toggles toggle;
224 u8 pad1[4];
225 u16 dl_mdm_len1; /*
226 * If this is 64, it can hold
227 * 60 bytes + 4 that is length field
228 */
229 u16 dl_start;
230
231 u16 dl_diag_len1;
232 u16 dl_mdm_len2; /*
233 * If this is 64, it can hold
234 * 60 bytes + 4 that is length field
235 */
236 u16 dl_app1_len;
237
238 u16 dl_diag_len2;
239 u16 dl_ctrl_len;
240 u16 dl_app2_len;
241 u8 pad2[16];
242 u16 ul_mdm_len1;
243 u16 ul_start;
244 u16 ul_diag_len;
245 u16 ul_mdm_len2;
246 u16 ul_app1_len;
247 u16 ul_app2_len;
248 u16 ul_ctrl_len;
249} __attribute__ ((packed));
250
251/* This stores all control downlink flags */
252struct ctrl_dl {
253 u8 port;
Frank Seidele9ed5372008-01-25 21:13:24 +0100254 unsigned int reserved:4;
255 unsigned int CTS:1;
256 unsigned int RI:1;
257 unsigned int DCD:1;
258 unsigned int DSR:1;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100259} __attribute__ ((packed));
260
261/* This stores all control uplink flags */
262struct ctrl_ul {
263 u8 port;
Frank Seidele9ed5372008-01-25 21:13:24 +0100264 unsigned int reserved:6;
265 unsigned int RTS:1;
266 unsigned int DTR:1;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100267} __attribute__ ((packed));
268
269#else
270/* Little endian */
271
272/* This represents the toggle information */
273struct toggles {
Frank Seidele9ed5372008-01-25 21:13:24 +0100274 unsigned int mdm_ul:1;
275 unsigned int mdm_dl:1;
276 unsigned int diag_dl:1;
277 unsigned int enabled:5; /*
Frank Seidel20fd1e32007-11-09 14:49:23 +0100278 * Toggle fields are valid if enabled is 0,
279 * else A-channels must always be used.
280 */
281} __attribute__ ((packed));
282
283/* Configuration table to read at startup of card */
284struct config_table {
285 u32 signature;
286 u16 version;
287 u16 product_information;
288 struct toggles toggle;
289 u8 pad1[7];
290 u16 dl_start;
291 u16 dl_mdm_len1; /*
292 * If this is 64, it can hold
293 * 60 bytes + 4 that is length field
294 */
295 u16 dl_mdm_len2;
296 u16 dl_diag_len1;
297 u16 dl_diag_len2;
298 u16 dl_app1_len;
299 u16 dl_app2_len;
300 u16 dl_ctrl_len;
301 u8 pad2[16];
302 u16 ul_start;
303 u16 ul_mdm_len2;
304 u16 ul_mdm_len1;
305 u16 ul_diag_len;
306 u16 ul_app1_len;
307 u16 ul_app2_len;
308 u16 ul_ctrl_len;
309} __attribute__ ((packed));
310
311/* This stores all control downlink flags */
312struct ctrl_dl {
Frank Seidele9ed5372008-01-25 21:13:24 +0100313 unsigned int DSR:1;
314 unsigned int DCD:1;
315 unsigned int RI:1;
316 unsigned int CTS:1;
317 unsigned int reserverd:4;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100318 u8 port;
319} __attribute__ ((packed));
320
321/* This stores all control uplink flags */
322struct ctrl_ul {
Frank Seidele9ed5372008-01-25 21:13:24 +0100323 unsigned int DTR:1;
324 unsigned int RTS:1;
325 unsigned int reserved:6;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100326 u8 port;
327} __attribute__ ((packed));
328#endif
329
330/* This holds all information that is needed regarding a port */
331struct port {
Alan Cox33dd4742009-01-02 13:47:32 +0000332 struct tty_port port;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100333 u8 update_flow_control;
334 struct ctrl_ul ctrl_ul;
335 struct ctrl_dl ctrl_dl;
Stefani Seibold45465482009-12-21 14:37:26 -0800336 struct kfifo fifo_ul;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100337 void __iomem *dl_addr[2];
338 u32 dl_size[2];
339 u8 toggle_dl;
340 void __iomem *ul_addr[2];
341 u32 ul_size[2];
342 u8 toggle_ul;
343 u16 token_dl;
344
Frank Seidel20fd1e32007-11-09 14:49:23 +0100345 wait_queue_head_t tty_wait;
346 struct async_icount tty_icount;
Alan Cox266794e2010-02-08 10:06:45 +0000347
348 struct nozomi *dc;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100349};
350
351/* Private data one for each card in the system */
352struct nozomi {
353 void __iomem *base_addr;
354 unsigned long flip;
355
356 /* Pointers to registers */
357 void __iomem *reg_iir;
358 void __iomem *reg_fcr;
359 void __iomem *reg_ier;
360
361 u16 last_ier;
362 enum card_type card_type;
363 struct config_table config_table; /* Configuration table */
364 struct pci_dev *pdev;
365 struct port port[NOZOMI_MAX_PORTS];
366 u8 *send_buf;
367
368 spinlock_t spin_mutex; /* secures access to registers and tty */
369
370 unsigned int index_start;
Frank Seidel661b4e82008-03-06 21:45:57 +0100371 enum card_state state;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100372 u32 open_ttys;
373};
374
375/* This is a data packet that is read or written to/from card */
376struct buffer {
377 u32 size; /* size is the length of the data buffer */
378 u8 *data;
379} __attribute__ ((packed));
380
381/* Global variables */
Bill Pemberton512f82a2012-11-19 13:25:19 -0500382static const struct pci_device_id nozomi_pci_tbl[] = {
Alan Coxb2a3dbc2010-02-08 10:07:15 +0000383 {PCI_DEVICE(0x1931, 0x000c)}, /* Nozomi HSDPA */
Frank Seidel20fd1e32007-11-09 14:49:23 +0100384 {},
385};
386
387MODULE_DEVICE_TABLE(pci, nozomi_pci_tbl);
388
389static struct nozomi *ndevs[NOZOMI_MAX_CARDS];
390static struct tty_driver *ntty_driver;
391
Alan Cox266794e2010-02-08 10:06:45 +0000392static const struct tty_port_operations noz_tty_port_ops;
393
Frank Seidel20fd1e32007-11-09 14:49:23 +0100394/*
395 * find card by tty_index
396 */
397static inline struct nozomi *get_dc_by_tty(const struct tty_struct *tty)
398{
399 return tty ? ndevs[tty->index / MAX_PORT] : NULL;
400}
401
402static inline struct port *get_port_by_tty(const struct tty_struct *tty)
403{
404 struct nozomi *ndev = get_dc_by_tty(tty);
405 return ndev ? &ndev->port[tty->index % MAX_PORT] : NULL;
406}
407
408/*
409 * TODO:
410 * -Optimize
411 * -Rewrite cleaner
412 */
413
414static void read_mem32(u32 *buf, const void __iomem *mem_addr_start,
415 u32 size_bytes)
416{
417 u32 i = 0;
Al Viro782a6de2008-03-29 03:09:08 +0000418 const u32 __iomem *ptr = mem_addr_start;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100419 u16 *buf16;
420
421 if (unlikely(!ptr || !buf))
422 goto out;
423
424 /* shortcut for extremely often used cases */
425 switch (size_bytes) {
426 case 2: /* 2 bytes */
427 buf16 = (u16 *) buf;
Al Viro782a6de2008-03-29 03:09:08 +0000428 *buf16 = __le16_to_cpu(readw(ptr));
Frank Seidel20fd1e32007-11-09 14:49:23 +0100429 goto out;
430 break;
431 case 4: /* 4 bytes */
Al Viro782a6de2008-03-29 03:09:08 +0000432 *(buf) = __le32_to_cpu(readl(ptr));
Frank Seidel20fd1e32007-11-09 14:49:23 +0100433 goto out;
434 break;
435 }
436
437 while (i < size_bytes) {
438 if (size_bytes - i == 2) {
439 /* Handle 2 bytes in the end */
440 buf16 = (u16 *) buf;
Al Viro782a6de2008-03-29 03:09:08 +0000441 *(buf16) = __le16_to_cpu(readw(ptr));
Frank Seidel20fd1e32007-11-09 14:49:23 +0100442 i += 2;
443 } else {
444 /* Read 4 bytes */
Al Viro782a6de2008-03-29 03:09:08 +0000445 *(buf) = __le32_to_cpu(readl(ptr));
Frank Seidel20fd1e32007-11-09 14:49:23 +0100446 i += 4;
447 }
448 buf++;
449 ptr++;
450 }
451out:
452 return;
453}
454
455/*
456 * TODO:
457 * -Optimize
458 * -Rewrite cleaner
459 */
Jan Engelhardt71e1b4a2008-02-01 09:13:53 +0100460static u32 write_mem32(void __iomem *mem_addr_start, const u32 *buf,
Frank Seidel20fd1e32007-11-09 14:49:23 +0100461 u32 size_bytes)
462{
463 u32 i = 0;
Al Viro782a6de2008-03-29 03:09:08 +0000464 u32 __iomem *ptr = mem_addr_start;
Jan Engelhardt71e1b4a2008-02-01 09:13:53 +0100465 const u16 *buf16;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100466
467 if (unlikely(!ptr || !buf))
468 return 0;
469
470 /* shortcut for extremely often used cases */
471 switch (size_bytes) {
472 case 2: /* 2 bytes */
Jan Engelhardt71e1b4a2008-02-01 09:13:53 +0100473 buf16 = (const u16 *)buf;
Al Viro782a6de2008-03-29 03:09:08 +0000474 writew(__cpu_to_le16(*buf16), ptr);
Frank Seidel20fd1e32007-11-09 14:49:23 +0100475 return 2;
476 break;
477 case 1: /*
478 * also needs to write 4 bytes in this case
479 * so falling through..
480 */
481 case 4: /* 4 bytes */
Al Viro782a6de2008-03-29 03:09:08 +0000482 writel(__cpu_to_le32(*buf), ptr);
Frank Seidel20fd1e32007-11-09 14:49:23 +0100483 return 4;
484 break;
485 }
486
487 while (i < size_bytes) {
488 if (size_bytes - i == 2) {
489 /* 2 bytes */
Jan Engelhardt71e1b4a2008-02-01 09:13:53 +0100490 buf16 = (const u16 *)buf;
Al Viro782a6de2008-03-29 03:09:08 +0000491 writew(__cpu_to_le16(*buf16), ptr);
Frank Seidel20fd1e32007-11-09 14:49:23 +0100492 i += 2;
493 } else {
494 /* 4 bytes */
Al Viro782a6de2008-03-29 03:09:08 +0000495 writel(__cpu_to_le32(*buf), ptr);
Frank Seidel20fd1e32007-11-09 14:49:23 +0100496 i += 4;
497 }
498 buf++;
499 ptr++;
500 }
501 return i;
502}
503
504/* Setup pointers to different channels and also setup buffer sizes. */
Chen Gang7acb88a2014-08-08 23:56:34 +0800505static void nozomi_setup_memory(struct nozomi *dc)
Frank Seidel20fd1e32007-11-09 14:49:23 +0100506{
507 void __iomem *offset = dc->base_addr + dc->config_table.dl_start;
508 /* The length reported is including the length field of 4 bytes,
509 * hence subtract with 4.
510 */
511 const u16 buff_offset = 4;
512
513 /* Modem port dl configuration */
514 dc->port[PORT_MDM].dl_addr[CH_A] = offset;
515 dc->port[PORT_MDM].dl_addr[CH_B] =
516 (offset += dc->config_table.dl_mdm_len1);
517 dc->port[PORT_MDM].dl_size[CH_A] =
518 dc->config_table.dl_mdm_len1 - buff_offset;
519 dc->port[PORT_MDM].dl_size[CH_B] =
520 dc->config_table.dl_mdm_len2 - buff_offset;
521
522 /* Diag port dl configuration */
523 dc->port[PORT_DIAG].dl_addr[CH_A] =
524 (offset += dc->config_table.dl_mdm_len2);
525 dc->port[PORT_DIAG].dl_size[CH_A] =
526 dc->config_table.dl_diag_len1 - buff_offset;
527 dc->port[PORT_DIAG].dl_addr[CH_B] =
528 (offset += dc->config_table.dl_diag_len1);
529 dc->port[PORT_DIAG].dl_size[CH_B] =
530 dc->config_table.dl_diag_len2 - buff_offset;
531
532 /* App1 port dl configuration */
533 dc->port[PORT_APP1].dl_addr[CH_A] =
534 (offset += dc->config_table.dl_diag_len2);
535 dc->port[PORT_APP1].dl_size[CH_A] =
536 dc->config_table.dl_app1_len - buff_offset;
537
538 /* App2 port dl configuration */
539 dc->port[PORT_APP2].dl_addr[CH_A] =
540 (offset += dc->config_table.dl_app1_len);
541 dc->port[PORT_APP2].dl_size[CH_A] =
542 dc->config_table.dl_app2_len - buff_offset;
543
544 /* Ctrl dl configuration */
545 dc->port[PORT_CTRL].dl_addr[CH_A] =
546 (offset += dc->config_table.dl_app2_len);
547 dc->port[PORT_CTRL].dl_size[CH_A] =
548 dc->config_table.dl_ctrl_len - buff_offset;
549
550 offset = dc->base_addr + dc->config_table.ul_start;
551
552 /* Modem Port ul configuration */
553 dc->port[PORT_MDM].ul_addr[CH_A] = offset;
554 dc->port[PORT_MDM].ul_size[CH_A] =
555 dc->config_table.ul_mdm_len1 - buff_offset;
556 dc->port[PORT_MDM].ul_addr[CH_B] =
557 (offset += dc->config_table.ul_mdm_len1);
558 dc->port[PORT_MDM].ul_size[CH_B] =
559 dc->config_table.ul_mdm_len2 - buff_offset;
560
561 /* Diag port ul configuration */
562 dc->port[PORT_DIAG].ul_addr[CH_A] =
563 (offset += dc->config_table.ul_mdm_len2);
564 dc->port[PORT_DIAG].ul_size[CH_A] =
565 dc->config_table.ul_diag_len - buff_offset;
566
567 /* App1 port ul configuration */
568 dc->port[PORT_APP1].ul_addr[CH_A] =
569 (offset += dc->config_table.ul_diag_len);
570 dc->port[PORT_APP1].ul_size[CH_A] =
571 dc->config_table.ul_app1_len - buff_offset;
572
573 /* App2 port ul configuration */
574 dc->port[PORT_APP2].ul_addr[CH_A] =
575 (offset += dc->config_table.ul_app1_len);
576 dc->port[PORT_APP2].ul_size[CH_A] =
577 dc->config_table.ul_app2_len - buff_offset;
578
579 /* Ctrl ul configuration */
580 dc->port[PORT_CTRL].ul_addr[CH_A] =
581 (offset += dc->config_table.ul_app2_len);
582 dc->port[PORT_CTRL].ul_size[CH_A] =
583 dc->config_table.ul_ctrl_len - buff_offset;
584}
585
586/* Dump config table under initalization phase */
587#ifdef DEBUG
588static void dump_table(const struct nozomi *dc)
589{
590 DBG3("signature: 0x%08X", dc->config_table.signature);
591 DBG3("version: 0x%04X", dc->config_table.version);
592 DBG3("product_information: 0x%04X", \
593 dc->config_table.product_information);
594 DBG3("toggle enabled: %d", dc->config_table.toggle.enabled);
595 DBG3("toggle up_mdm: %d", dc->config_table.toggle.mdm_ul);
596 DBG3("toggle dl_mdm: %d", dc->config_table.toggle.mdm_dl);
597 DBG3("toggle dl_dbg: %d", dc->config_table.toggle.diag_dl);
598
599 DBG3("dl_start: 0x%04X", dc->config_table.dl_start);
600 DBG3("dl_mdm_len0: 0x%04X, %d", dc->config_table.dl_mdm_len1,
601 dc->config_table.dl_mdm_len1);
602 DBG3("dl_mdm_len1: 0x%04X, %d", dc->config_table.dl_mdm_len2,
603 dc->config_table.dl_mdm_len2);
604 DBG3("dl_diag_len0: 0x%04X, %d", dc->config_table.dl_diag_len1,
605 dc->config_table.dl_diag_len1);
606 DBG3("dl_diag_len1: 0x%04X, %d", dc->config_table.dl_diag_len2,
607 dc->config_table.dl_diag_len2);
608 DBG3("dl_app1_len: 0x%04X, %d", dc->config_table.dl_app1_len,
609 dc->config_table.dl_app1_len);
610 DBG3("dl_app2_len: 0x%04X, %d", dc->config_table.dl_app2_len,
611 dc->config_table.dl_app2_len);
612 DBG3("dl_ctrl_len: 0x%04X, %d", dc->config_table.dl_ctrl_len,
613 dc->config_table.dl_ctrl_len);
614 DBG3("ul_start: 0x%04X, %d", dc->config_table.ul_start,
615 dc->config_table.ul_start);
616 DBG3("ul_mdm_len[0]: 0x%04X, %d", dc->config_table.ul_mdm_len1,
617 dc->config_table.ul_mdm_len1);
618 DBG3("ul_mdm_len[1]: 0x%04X, %d", dc->config_table.ul_mdm_len2,
619 dc->config_table.ul_mdm_len2);
620 DBG3("ul_diag_len: 0x%04X, %d", dc->config_table.ul_diag_len,
621 dc->config_table.ul_diag_len);
622 DBG3("ul_app1_len: 0x%04X, %d", dc->config_table.ul_app1_len,
623 dc->config_table.ul_app1_len);
624 DBG3("ul_app2_len: 0x%04X, %d", dc->config_table.ul_app2_len,
625 dc->config_table.ul_app2_len);
626 DBG3("ul_ctrl_len: 0x%04X, %d", dc->config_table.ul_ctrl_len,
627 dc->config_table.ul_ctrl_len);
628}
629#else
Frank Seidele9ed5372008-01-25 21:13:24 +0100630static inline void dump_table(const struct nozomi *dc) { }
Frank Seidel20fd1e32007-11-09 14:49:23 +0100631#endif
632
633/*
634 * Read configuration table from card under intalization phase
635 * Returns 1 if ok, else 0
636 */
637static int nozomi_read_config_table(struct nozomi *dc)
638{
639 read_mem32((u32 *) &dc->config_table, dc->base_addr + 0,
640 sizeof(struct config_table));
641
Valentin Rothberg03ac6b32015-04-12 18:35:18 +0200642 if (dc->config_table.signature != NOZOMI_CONFIG_MAGIC) {
Frank Seidel20fd1e32007-11-09 14:49:23 +0100643 dev_err(&dc->pdev->dev, "ConfigTable Bad! 0x%08X != 0x%08X\n",
Valentin Rothberg03ac6b32015-04-12 18:35:18 +0200644 dc->config_table.signature, NOZOMI_CONFIG_MAGIC);
Frank Seidel20fd1e32007-11-09 14:49:23 +0100645 return 0;
646 }
647
648 if ((dc->config_table.version == 0)
649 || (dc->config_table.toggle.enabled == TOGGLE_VALID)) {
650 int i;
651 DBG1("Second phase, configuring card");
652
Chen Gang7acb88a2014-08-08 23:56:34 +0800653 nozomi_setup_memory(dc);
Frank Seidel20fd1e32007-11-09 14:49:23 +0100654
655 dc->port[PORT_MDM].toggle_ul = dc->config_table.toggle.mdm_ul;
656 dc->port[PORT_MDM].toggle_dl = dc->config_table.toggle.mdm_dl;
657 dc->port[PORT_DIAG].toggle_dl = dc->config_table.toggle.diag_dl;
658 DBG1("toggle ports: MDM UL:%d MDM DL:%d, DIAG DL:%d",
659 dc->port[PORT_MDM].toggle_ul,
660 dc->port[PORT_MDM].toggle_dl, dc->port[PORT_DIAG].toggle_dl);
661
662 dump_table(dc);
663
664 for (i = PORT_MDM; i < MAX_PORT; i++) {
Frank Seidel20fd1e32007-11-09 14:49:23 +0100665 memset(&dc->port[i].ctrl_dl, 0, sizeof(struct ctrl_dl));
666 memset(&dc->port[i].ctrl_ul, 0, sizeof(struct ctrl_ul));
667 }
668
669 /* Enable control channel */
670 dc->last_ier = dc->last_ier | CTRL_DL;
671 writew(dc->last_ier, dc->reg_ier);
672
Frank Seidel661b4e82008-03-06 21:45:57 +0100673 dc->state = NOZOMI_STATE_ALLOCATED;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100674 dev_info(&dc->pdev->dev, "Initialization OK!\n");
675 return 1;
676 }
677
678 if ((dc->config_table.version > 0)
679 && (dc->config_table.toggle.enabled != TOGGLE_VALID)) {
680 u32 offset = 0;
681 DBG1("First phase: pushing upload buffers, clearing download");
682
683 dev_info(&dc->pdev->dev, "Version of card: %d\n",
684 dc->config_table.version);
685
686 /* Here we should disable all I/O over F32. */
Chen Gang7acb88a2014-08-08 23:56:34 +0800687 nozomi_setup_memory(dc);
Frank Seidel20fd1e32007-11-09 14:49:23 +0100688
689 /*
690 * We should send ALL channel pair tokens back along
691 * with reset token
692 */
693
694 /* push upload modem buffers */
695 write_mem32(dc->port[PORT_MDM].ul_addr[CH_A],
696 (u32 *) &offset, 4);
697 write_mem32(dc->port[PORT_MDM].ul_addr[CH_B],
698 (u32 *) &offset, 4);
699
700 writew(MDM_UL | DIAG_DL | MDM_DL, dc->reg_fcr);
701
702 DBG1("First phase done");
703 }
704
705 return 1;
706}
707
708/* Enable uplink interrupts */
709static void enable_transmit_ul(enum port_type port, struct nozomi *dc)
710{
Jan Engelhardt71e1b4a2008-02-01 09:13:53 +0100711 static const u16 mask[] = {MDM_UL, DIAG_UL, APP1_UL, APP2_UL, CTRL_UL};
Frank Seidel20fd1e32007-11-09 14:49:23 +0100712
713 if (port < NOZOMI_MAX_PORTS) {
714 dc->last_ier |= mask[port];
715 writew(dc->last_ier, dc->reg_ier);
716 } else {
717 dev_err(&dc->pdev->dev, "Called with wrong port?\n");
718 }
719}
720
721/* Disable uplink interrupts */
722static void disable_transmit_ul(enum port_type port, struct nozomi *dc)
723{
Jan Engelhardt71e1b4a2008-02-01 09:13:53 +0100724 static const u16 mask[] =
725 {~MDM_UL, ~DIAG_UL, ~APP1_UL, ~APP2_UL, ~CTRL_UL};
Frank Seidel20fd1e32007-11-09 14:49:23 +0100726
727 if (port < NOZOMI_MAX_PORTS) {
728 dc->last_ier &= mask[port];
729 writew(dc->last_ier, dc->reg_ier);
730 } else {
731 dev_err(&dc->pdev->dev, "Called with wrong port?\n");
732 }
733}
734
735/* Enable downlink interrupts */
736static void enable_transmit_dl(enum port_type port, struct nozomi *dc)
737{
Jan Engelhardt71e1b4a2008-02-01 09:13:53 +0100738 static const u16 mask[] = {MDM_DL, DIAG_DL, APP1_DL, APP2_DL, CTRL_DL};
Frank Seidel20fd1e32007-11-09 14:49:23 +0100739
740 if (port < NOZOMI_MAX_PORTS) {
741 dc->last_ier |= mask[port];
742 writew(dc->last_ier, dc->reg_ier);
743 } else {
744 dev_err(&dc->pdev->dev, "Called with wrong port?\n");
745 }
746}
747
748/* Disable downlink interrupts */
749static void disable_transmit_dl(enum port_type port, struct nozomi *dc)
750{
Jan Engelhardt71e1b4a2008-02-01 09:13:53 +0100751 static const u16 mask[] =
752 {~MDM_DL, ~DIAG_DL, ~APP1_DL, ~APP2_DL, ~CTRL_DL};
Frank Seidel20fd1e32007-11-09 14:49:23 +0100753
754 if (port < NOZOMI_MAX_PORTS) {
755 dc->last_ier &= mask[port];
756 writew(dc->last_ier, dc->reg_ier);
757 } else {
758 dev_err(&dc->pdev->dev, "Called with wrong port?\n");
759 }
760}
761
762/*
763 * Return 1 - send buffer to card and ack.
764 * Return 0 - don't ack, don't send buffer to card.
765 */
Alan Cox33dd4742009-01-02 13:47:32 +0000766static int send_data(enum port_type index, struct nozomi *dc)
Frank Seidel20fd1e32007-11-09 14:49:23 +0100767{
768 u32 size = 0;
Alan Cox33dd4742009-01-02 13:47:32 +0000769 struct port *port = &dc->port[index];
Frank Seidel18bbe0c22008-02-01 09:14:05 +0100770 const u8 toggle = port->toggle_ul;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100771 void __iomem *addr = port->ul_addr[toggle];
Frank Seidel18bbe0c22008-02-01 09:14:05 +0100772 const u32 ul_size = port->ul_size[toggle];
Frank Seidel20fd1e32007-11-09 14:49:23 +0100773
774 /* Get data from tty and place in buf for now */
Stefani Seibold7acd72e2009-12-21 14:37:28 -0800775 size = kfifo_out(&port->fifo_ul, dc->send_buf,
Frank Seidel20fd1e32007-11-09 14:49:23 +0100776 ul_size < SEND_BUF_MAX ? ul_size : SEND_BUF_MAX);
777
778 if (size == 0) {
779 DBG4("No more data to send, disable link:");
780 return 0;
781 }
782
783 /* DUMP(buf, size); */
784
785 /* Write length + data */
786 write_mem32(addr, (u32 *) &size, 4);
787 write_mem32(addr + 4, (u32 *) dc->send_buf, size);
788
Jiri Slaby6aad04f2013-03-07 13:12:29 +0100789 tty_port_tty_wakeup(&port->port);
Frank Seidel20fd1e32007-11-09 14:49:23 +0100790
791 return 1;
792}
793
794/* If all data has been read, return 1, else 0 */
795static int receive_data(enum port_type index, struct nozomi *dc)
796{
797 u8 buf[RECEIVE_BUF_MAX] = { 0 };
798 int size;
799 u32 offset = 4;
800 struct port *port = &dc->port[index];
801 void __iomem *addr = port->dl_addr[port->toggle_dl];
Alan Cox33dd4742009-01-02 13:47:32 +0000802 struct tty_struct *tty = tty_port_tty_get(&port->port);
Jiri Slaby9237a812009-07-16 16:06:18 +0100803 int i, ret;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100804
Arnd Bergmanna4f642a2016-01-25 22:54:56 +0100805 size = __le32_to_cpu(readl(addr));
Frank Seidel20fd1e32007-11-09 14:49:23 +0100806 /* DBG1( "%d bytes port: %d", size, index); */
807
Peter Hurley97ef38b2016-04-09 17:11:36 -0700808 if (tty && tty_throttled(tty)) {
Frank Seidel20fd1e32007-11-09 14:49:23 +0100809 DBG1("No room in tty, don't read data, don't ack interrupt, "
810 "disable interrupt");
811
812 /* disable interrupt in downlink... */
813 disable_transmit_dl(index, dc);
Jiri Slaby9237a812009-07-16 16:06:18 +0100814 ret = 0;
815 goto put;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100816 }
817
818 if (unlikely(size == 0)) {
819 dev_err(&dc->pdev->dev, "size == 0?\n");
Jiri Slaby9237a812009-07-16 16:06:18 +0100820 ret = 1;
821 goto put;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100822 }
823
Frank Seidel20fd1e32007-11-09 14:49:23 +0100824 while (size > 0) {
825 read_mem32((u32 *) buf, addr + offset, RECEIVE_BUF_MAX);
826
827 if (size == 1) {
Jiri Slaby92a19f92013-01-03 15:53:03 +0100828 tty_insert_flip_char(&port->port, buf[0], TTY_NORMAL);
Frank Seidel20fd1e32007-11-09 14:49:23 +0100829 size = 0;
830 } else if (size < RECEIVE_BUF_MAX) {
Jiri Slaby05c7cd32013-01-03 15:53:04 +0100831 size -= tty_insert_flip_string(&port->port,
832 (char *)buf, size);
Frank Seidel20fd1e32007-11-09 14:49:23 +0100833 } else {
Jiri Slaby05c7cd32013-01-03 15:53:04 +0100834 i = tty_insert_flip_string(&port->port,
835 (char *)buf, RECEIVE_BUF_MAX);
Frank Seidel20fd1e32007-11-09 14:49:23 +0100836 size -= i;
837 offset += i;
838 }
839 }
840
841 set_bit(index, &dc->flip);
Jiri Slaby9237a812009-07-16 16:06:18 +0100842 ret = 1;
843put:
Alan Cox33dd4742009-01-02 13:47:32 +0000844 tty_kref_put(tty);
Jiri Slaby9237a812009-07-16 16:06:18 +0100845 return ret;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100846}
847
848/* Debug for interrupts */
849#ifdef DEBUG
850static char *interrupt2str(u16 interrupt)
851{
852 static char buf[TMP_BUF_MAX];
853 char *p = buf;
854
855 interrupt & MDM_DL1 ? p += snprintf(p, TMP_BUF_MAX, "MDM_DL1 ") : NULL;
856 interrupt & MDM_DL2 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
857 "MDM_DL2 ") : NULL;
858
859 interrupt & MDM_UL1 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
860 "MDM_UL1 ") : NULL;
861 interrupt & MDM_UL2 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
862 "MDM_UL2 ") : NULL;
863
864 interrupt & DIAG_DL1 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
865 "DIAG_DL1 ") : NULL;
866 interrupt & DIAG_DL2 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
867 "DIAG_DL2 ") : NULL;
868
869 interrupt & DIAG_UL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
870 "DIAG_UL ") : NULL;
871
872 interrupt & APP1_DL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
873 "APP1_DL ") : NULL;
874 interrupt & APP2_DL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
875 "APP2_DL ") : NULL;
876
877 interrupt & APP1_UL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
878 "APP1_UL ") : NULL;
879 interrupt & APP2_UL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
880 "APP2_UL ") : NULL;
881
882 interrupt & CTRL_DL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
883 "CTRL_DL ") : NULL;
884 interrupt & CTRL_UL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
885 "CTRL_UL ") : NULL;
886
887 interrupt & RESET ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
888 "RESET ") : NULL;
889
890 return buf;
891}
892#endif
893
894/*
895 * Receive flow control
896 * Return 1 - If ok, else 0
897 */
898static int receive_flow_control(struct nozomi *dc)
899{
900 enum port_type port = PORT_MDM;
901 struct ctrl_dl ctrl_dl;
902 struct ctrl_dl old_ctrl;
903 u16 enable_ier = 0;
904
905 read_mem32((u32 *) &ctrl_dl, dc->port[PORT_CTRL].dl_addr[CH_A], 2);
906
907 switch (ctrl_dl.port) {
908 case CTRL_CMD:
909 DBG1("The Base Band sends this value as a response to a "
910 "request for IMSI detach sent over the control "
911 "channel uplink (see section 7.6.1).");
912 break;
913 case CTRL_MDM:
914 port = PORT_MDM;
915 enable_ier = MDM_DL;
916 break;
917 case CTRL_DIAG:
918 port = PORT_DIAG;
919 enable_ier = DIAG_DL;
920 break;
921 case CTRL_APP1:
922 port = PORT_APP1;
923 enable_ier = APP1_DL;
924 break;
925 case CTRL_APP2:
926 port = PORT_APP2;
927 enable_ier = APP2_DL;
Frank Seidel661b4e82008-03-06 21:45:57 +0100928 if (dc->state == NOZOMI_STATE_ALLOCATED) {
929 /*
930 * After card initialization the flow control
931 * received for APP2 is always the last
932 */
933 dc->state = NOZOMI_STATE_READY;
934 dev_info(&dc->pdev->dev, "Device READY!\n");
935 }
Frank Seidel20fd1e32007-11-09 14:49:23 +0100936 break;
937 default:
938 dev_err(&dc->pdev->dev,
939 "ERROR: flow control received for non-existing port\n");
940 return 0;
Joe Perchesfc8114722013-10-08 16:14:21 -0700941 }
Frank Seidel20fd1e32007-11-09 14:49:23 +0100942
943 DBG1("0x%04X->0x%04X", *((u16 *)&dc->port[port].ctrl_dl),
944 *((u16 *)&ctrl_dl));
945
946 old_ctrl = dc->port[port].ctrl_dl;
947 dc->port[port].ctrl_dl = ctrl_dl;
948
949 if (old_ctrl.CTS == 1 && ctrl_dl.CTS == 0) {
950 DBG1("Disable interrupt (0x%04X) on port: %d",
951 enable_ier, port);
952 disable_transmit_ul(port, dc);
953
954 } else if (old_ctrl.CTS == 0 && ctrl_dl.CTS == 1) {
955
Stefani Seibolde64c0262009-12-21 14:37:28 -0800956 if (kfifo_len(&dc->port[port].fifo_ul)) {
Frank Seidel20fd1e32007-11-09 14:49:23 +0100957 DBG1("Enable interrupt (0x%04X) on port: %d",
958 enable_ier, port);
959 DBG1("Data in buffer [%d], enable transmit! ",
Stefani Seibolde64c0262009-12-21 14:37:28 -0800960 kfifo_len(&dc->port[port].fifo_ul));
Frank Seidel20fd1e32007-11-09 14:49:23 +0100961 enable_transmit_ul(port, dc);
962 } else {
963 DBG1("No data in buffer...");
964 }
965 }
966
967 if (*(u16 *)&old_ctrl == *(u16 *)&ctrl_dl) {
968 DBG1(" No change in mctrl");
969 return 1;
970 }
971 /* Update statistics */
972 if (old_ctrl.CTS != ctrl_dl.CTS)
973 dc->port[port].tty_icount.cts++;
974 if (old_ctrl.DSR != ctrl_dl.DSR)
975 dc->port[port].tty_icount.dsr++;
976 if (old_ctrl.RI != ctrl_dl.RI)
977 dc->port[port].tty_icount.rng++;
978 if (old_ctrl.DCD != ctrl_dl.DCD)
979 dc->port[port].tty_icount.dcd++;
980
981 wake_up_interruptible(&dc->port[port].tty_wait);
982
983 DBG1("port: %d DCD(%d), CTS(%d), RI(%d), DSR(%d)",
984 port,
985 dc->port[port].tty_icount.dcd, dc->port[port].tty_icount.cts,
986 dc->port[port].tty_icount.rng, dc->port[port].tty_icount.dsr);
987
988 return 1;
989}
990
991static enum ctrl_port_type port2ctrl(enum port_type port,
992 const struct nozomi *dc)
993{
994 switch (port) {
995 case PORT_MDM:
996 return CTRL_MDM;
997 case PORT_DIAG:
998 return CTRL_DIAG;
999 case PORT_APP1:
1000 return CTRL_APP1;
1001 case PORT_APP2:
1002 return CTRL_APP2;
1003 default:
1004 dev_err(&dc->pdev->dev,
1005 "ERROR: send flow control " \
1006 "received for non-existing port\n");
Joe Perchesfc8114722013-10-08 16:14:21 -07001007 }
Frank Seidel20fd1e32007-11-09 14:49:23 +01001008 return CTRL_ERROR;
1009}
1010
1011/*
1012 * Send flow control, can only update one channel at a time
1013 * Return 0 - If we have updated all flow control
1014 * Return 1 - If we need to update more flow control, ack current enable more
1015 */
1016static int send_flow_control(struct nozomi *dc)
1017{
1018 u32 i, more_flow_control_to_be_updated = 0;
1019 u16 *ctrl;
1020
1021 for (i = PORT_MDM; i < MAX_PORT; i++) {
1022 if (dc->port[i].update_flow_control) {
1023 if (more_flow_control_to_be_updated) {
1024 /* We have more flow control to be updated */
1025 return 1;
1026 }
1027 dc->port[i].ctrl_ul.port = port2ctrl(i, dc);
1028 ctrl = (u16 *)&dc->port[i].ctrl_ul;
1029 write_mem32(dc->port[PORT_CTRL].ul_addr[0], \
1030 (u32 *) ctrl, 2);
1031 dc->port[i].update_flow_control = 0;
1032 more_flow_control_to_be_updated = 1;
1033 }
1034 }
1035 return 0;
1036}
1037
1038/*
Frank Seidele9ed5372008-01-25 21:13:24 +01001039 * Handle downlink data, ports that are handled are modem and diagnostics
Frank Seidel20fd1e32007-11-09 14:49:23 +01001040 * Return 1 - ok
1041 * Return 0 - toggle fields are out of sync
1042 */
1043static int handle_data_dl(struct nozomi *dc, enum port_type port, u8 *toggle,
1044 u16 read_iir, u16 mask1, u16 mask2)
1045{
1046 if (*toggle == 0 && read_iir & mask1) {
1047 if (receive_data(port, dc)) {
1048 writew(mask1, dc->reg_fcr);
1049 *toggle = !(*toggle);
1050 }
1051
1052 if (read_iir & mask2) {
1053 if (receive_data(port, dc)) {
1054 writew(mask2, dc->reg_fcr);
1055 *toggle = !(*toggle);
1056 }
1057 }
1058 } else if (*toggle == 1 && read_iir & mask2) {
1059 if (receive_data(port, dc)) {
1060 writew(mask2, dc->reg_fcr);
1061 *toggle = !(*toggle);
1062 }
1063
1064 if (read_iir & mask1) {
1065 if (receive_data(port, dc)) {
1066 writew(mask1, dc->reg_fcr);
1067 *toggle = !(*toggle);
1068 }
1069 }
1070 } else {
1071 dev_err(&dc->pdev->dev, "port out of sync!, toggle:%d\n",
1072 *toggle);
1073 return 0;
1074 }
1075 return 1;
1076}
1077
1078/*
1079 * Handle uplink data, this is currently for the modem port
1080 * Return 1 - ok
1081 * Return 0 - toggle field are out of sync
1082 */
1083static int handle_data_ul(struct nozomi *dc, enum port_type port, u16 read_iir)
1084{
1085 u8 *toggle = &(dc->port[port].toggle_ul);
1086
1087 if (*toggle == 0 && read_iir & MDM_UL1) {
1088 dc->last_ier &= ~MDM_UL;
1089 writew(dc->last_ier, dc->reg_ier);
1090 if (send_data(port, dc)) {
1091 writew(MDM_UL1, dc->reg_fcr);
1092 dc->last_ier = dc->last_ier | MDM_UL;
1093 writew(dc->last_ier, dc->reg_ier);
1094 *toggle = !*toggle;
1095 }
1096
1097 if (read_iir & MDM_UL2) {
1098 dc->last_ier &= ~MDM_UL;
1099 writew(dc->last_ier, dc->reg_ier);
1100 if (send_data(port, dc)) {
1101 writew(MDM_UL2, dc->reg_fcr);
1102 dc->last_ier = dc->last_ier | MDM_UL;
1103 writew(dc->last_ier, dc->reg_ier);
1104 *toggle = !*toggle;
1105 }
1106 }
1107
1108 } else if (*toggle == 1 && read_iir & MDM_UL2) {
1109 dc->last_ier &= ~MDM_UL;
1110 writew(dc->last_ier, dc->reg_ier);
1111 if (send_data(port, dc)) {
1112 writew(MDM_UL2, dc->reg_fcr);
1113 dc->last_ier = dc->last_ier | MDM_UL;
1114 writew(dc->last_ier, dc->reg_ier);
1115 *toggle = !*toggle;
1116 }
1117
1118 if (read_iir & MDM_UL1) {
1119 dc->last_ier &= ~MDM_UL;
1120 writew(dc->last_ier, dc->reg_ier);
1121 if (send_data(port, dc)) {
1122 writew(MDM_UL1, dc->reg_fcr);
1123 dc->last_ier = dc->last_ier | MDM_UL;
1124 writew(dc->last_ier, dc->reg_ier);
1125 *toggle = !*toggle;
1126 }
1127 }
1128 } else {
1129 writew(read_iir & MDM_UL, dc->reg_fcr);
1130 dev_err(&dc->pdev->dev, "port out of sync!\n");
1131 return 0;
1132 }
1133 return 1;
1134}
1135
1136static irqreturn_t interrupt_handler(int irq, void *dev_id)
1137{
1138 struct nozomi *dc = dev_id;
1139 unsigned int a;
1140 u16 read_iir;
1141
1142 if (!dc)
1143 return IRQ_NONE;
1144
1145 spin_lock(&dc->spin_mutex);
1146 read_iir = readw(dc->reg_iir);
1147
1148 /* Card removed */
1149 if (read_iir == (u16)-1)
1150 goto none;
1151 /*
1152 * Just handle interrupt enabled in IER
1153 * (by masking with dc->last_ier)
1154 */
1155 read_iir &= dc->last_ier;
1156
1157 if (read_iir == 0)
1158 goto none;
1159
1160
1161 DBG4("%s irq:0x%04X, prev:0x%04X", interrupt2str(read_iir), read_iir,
1162 dc->last_ier);
1163
1164 if (read_iir & RESET) {
1165 if (unlikely(!nozomi_read_config_table(dc))) {
1166 dc->last_ier = 0x0;
1167 writew(dc->last_ier, dc->reg_ier);
1168 dev_err(&dc->pdev->dev, "Could not read status from "
1169 "card, we should disable interface\n");
1170 } else {
1171 writew(RESET, dc->reg_fcr);
1172 }
1173 /* No more useful info if this was the reset interrupt. */
1174 goto exit_handler;
1175 }
1176 if (read_iir & CTRL_UL) {
1177 DBG1("CTRL_UL");
1178 dc->last_ier &= ~CTRL_UL;
1179 writew(dc->last_ier, dc->reg_ier);
1180 if (send_flow_control(dc)) {
1181 writew(CTRL_UL, dc->reg_fcr);
1182 dc->last_ier = dc->last_ier | CTRL_UL;
1183 writew(dc->last_ier, dc->reg_ier);
1184 }
1185 }
1186 if (read_iir & CTRL_DL) {
1187 receive_flow_control(dc);
1188 writew(CTRL_DL, dc->reg_fcr);
1189 }
1190 if (read_iir & MDM_DL) {
1191 if (!handle_data_dl(dc, PORT_MDM,
1192 &(dc->port[PORT_MDM].toggle_dl), read_iir,
1193 MDM_DL1, MDM_DL2)) {
1194 dev_err(&dc->pdev->dev, "MDM_DL out of sync!\n");
1195 goto exit_handler;
1196 }
1197 }
1198 if (read_iir & MDM_UL) {
1199 if (!handle_data_ul(dc, PORT_MDM, read_iir)) {
1200 dev_err(&dc->pdev->dev, "MDM_UL out of sync!\n");
1201 goto exit_handler;
1202 }
1203 }
1204 if (read_iir & DIAG_DL) {
1205 if (!handle_data_dl(dc, PORT_DIAG,
1206 &(dc->port[PORT_DIAG].toggle_dl), read_iir,
1207 DIAG_DL1, DIAG_DL2)) {
1208 dev_err(&dc->pdev->dev, "DIAG_DL out of sync!\n");
1209 goto exit_handler;
1210 }
1211 }
1212 if (read_iir & DIAG_UL) {
1213 dc->last_ier &= ~DIAG_UL;
1214 writew(dc->last_ier, dc->reg_ier);
1215 if (send_data(PORT_DIAG, dc)) {
1216 writew(DIAG_UL, dc->reg_fcr);
1217 dc->last_ier = dc->last_ier | DIAG_UL;
1218 writew(dc->last_ier, dc->reg_ier);
1219 }
1220 }
1221 if (read_iir & APP1_DL) {
1222 if (receive_data(PORT_APP1, dc))
1223 writew(APP1_DL, dc->reg_fcr);
1224 }
1225 if (read_iir & APP1_UL) {
1226 dc->last_ier &= ~APP1_UL;
1227 writew(dc->last_ier, dc->reg_ier);
1228 if (send_data(PORT_APP1, dc)) {
1229 writew(APP1_UL, dc->reg_fcr);
1230 dc->last_ier = dc->last_ier | APP1_UL;
1231 writew(dc->last_ier, dc->reg_ier);
1232 }
1233 }
1234 if (read_iir & APP2_DL) {
1235 if (receive_data(PORT_APP2, dc))
1236 writew(APP2_DL, dc->reg_fcr);
1237 }
1238 if (read_iir & APP2_UL) {
1239 dc->last_ier &= ~APP2_UL;
1240 writew(dc->last_ier, dc->reg_ier);
1241 if (send_data(PORT_APP2, dc)) {
1242 writew(APP2_UL, dc->reg_fcr);
1243 dc->last_ier = dc->last_ier | APP2_UL;
1244 writew(dc->last_ier, dc->reg_ier);
1245 }
1246 }
1247
1248exit_handler:
1249 spin_unlock(&dc->spin_mutex);
Jiri Slaby2e124b42013-01-03 15:53:06 +01001250
1251 for (a = 0; a < NOZOMI_MAX_PORTS; a++)
1252 if (test_and_clear_bit(a, &dc->flip))
1253 tty_flip_buffer_push(&dc->port[a].port);
1254
Frank Seidel20fd1e32007-11-09 14:49:23 +01001255 return IRQ_HANDLED;
1256none:
1257 spin_unlock(&dc->spin_mutex);
1258 return IRQ_NONE;
1259}
1260
1261static void nozomi_get_card_type(struct nozomi *dc)
1262{
1263 int i;
1264 u32 size = 0;
1265
1266 for (i = 0; i < 6; i++)
1267 size += pci_resource_len(dc->pdev, i);
1268
1269 /* Assume card type F32_8 if no match */
1270 dc->card_type = size == 2048 ? F32_2 : F32_8;
1271
1272 dev_info(&dc->pdev->dev, "Card type is: %d\n", dc->card_type);
1273}
1274
1275static void nozomi_setup_private_data(struct nozomi *dc)
1276{
1277 void __iomem *offset = dc->base_addr + dc->card_type / 2;
1278 unsigned int i;
1279
1280 dc->reg_fcr = (void __iomem *)(offset + R_FCR);
1281 dc->reg_iir = (void __iomem *)(offset + R_IIR);
1282 dc->reg_ier = (void __iomem *)(offset + R_IER);
1283 dc->last_ier = 0;
1284 dc->flip = 0;
1285
1286 dc->port[PORT_MDM].token_dl = MDM_DL;
1287 dc->port[PORT_DIAG].token_dl = DIAG_DL;
1288 dc->port[PORT_APP1].token_dl = APP1_DL;
1289 dc->port[PORT_APP2].token_dl = APP2_DL;
1290
1291 for (i = 0; i < MAX_PORT; i++)
1292 init_waitqueue_head(&dc->port[i].tty_wait);
1293}
1294
1295static ssize_t card_type_show(struct device *dev, struct device_attribute *attr,
1296 char *buf)
1297{
Jan Engelhardt71e1b4a2008-02-01 09:13:53 +01001298 const struct nozomi *dc = pci_get_drvdata(to_pci_dev(dev));
Frank Seidel20fd1e32007-11-09 14:49:23 +01001299
1300 return sprintf(buf, "%d\n", dc->card_type);
1301}
Julia Lawall19467fa2016-10-29 21:37:08 +02001302static DEVICE_ATTR_RO(card_type);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001303
1304static ssize_t open_ttys_show(struct device *dev, struct device_attribute *attr,
1305 char *buf)
1306{
Jan Engelhardt71e1b4a2008-02-01 09:13:53 +01001307 const struct nozomi *dc = pci_get_drvdata(to_pci_dev(dev));
Frank Seidel20fd1e32007-11-09 14:49:23 +01001308
1309 return sprintf(buf, "%u\n", dc->open_ttys);
1310}
Julia Lawall19467fa2016-10-29 21:37:08 +02001311static DEVICE_ATTR_RO(open_ttys);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001312
1313static void make_sysfs_files(struct nozomi *dc)
1314{
1315 if (device_create_file(&dc->pdev->dev, &dev_attr_card_type))
1316 dev_err(&dc->pdev->dev,
1317 "Could not create sysfs file for card_type\n");
1318 if (device_create_file(&dc->pdev->dev, &dev_attr_open_ttys))
1319 dev_err(&dc->pdev->dev,
1320 "Could not create sysfs file for open_ttys\n");
1321}
1322
1323static void remove_sysfs_files(struct nozomi *dc)
1324{
1325 device_remove_file(&dc->pdev->dev, &dev_attr_card_type);
1326 device_remove_file(&dc->pdev->dev, &dev_attr_open_ttys);
1327}
1328
1329/* Allocate memory for one device */
Bill Pemberton9671f092012-11-19 13:21:50 -05001330static int nozomi_card_init(struct pci_dev *pdev,
Frank Seidel20fd1e32007-11-09 14:49:23 +01001331 const struct pci_device_id *ent)
1332{
1333 resource_size_t start;
1334 int ret;
1335 struct nozomi *dc = NULL;
1336 int ndev_idx;
1337 int i;
1338
1339 dev_dbg(&pdev->dev, "Init, new card found\n");
1340
1341 for (ndev_idx = 0; ndev_idx < ARRAY_SIZE(ndevs); ndev_idx++)
1342 if (!ndevs[ndev_idx])
1343 break;
1344
1345 if (ndev_idx >= ARRAY_SIZE(ndevs)) {
1346 dev_err(&pdev->dev, "no free tty range for this card left\n");
1347 ret = -EIO;
1348 goto err;
1349 }
1350
1351 dc = kzalloc(sizeof(struct nozomi), GFP_KERNEL);
1352 if (unlikely(!dc)) {
1353 dev_err(&pdev->dev, "Could not allocate memory\n");
1354 ret = -ENOMEM;
1355 goto err_free;
1356 }
1357
1358 dc->pdev = pdev;
1359
Frank Seidel20fd1e32007-11-09 14:49:23 +01001360 ret = pci_enable_device(dc->pdev);
1361 if (ret) {
1362 dev_err(&pdev->dev, "Failed to enable PCI Device\n");
1363 goto err_free;
1364 }
1365
Frank Seidel20fd1e32007-11-09 14:49:23 +01001366 ret = pci_request_regions(dc->pdev, NOZOMI_NAME);
1367 if (ret) {
1368 dev_err(&pdev->dev, "I/O address 0x%04x already in use\n",
1369 (int) /* nozomi_private.io_addr */ 0);
1370 goto err_disable_device;
1371 }
1372
Frank Seidel661b4e82008-03-06 21:45:57 +01001373 start = pci_resource_start(dc->pdev, 0);
1374 if (start == 0) {
1375 dev_err(&pdev->dev, "No I/O address for card detected\n");
1376 ret = -ENODEV;
1377 goto err_rel_regs;
1378 }
1379
1380 /* Find out what card type it is */
1381 nozomi_get_card_type(dc);
1382
Alan Cox24cb2332008-04-30 00:54:19 -07001383 dc->base_addr = ioremap_nocache(start, dc->card_type);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001384 if (!dc->base_addr) {
1385 dev_err(&pdev->dev, "Unable to map card MMIO\n");
1386 ret = -ENODEV;
1387 goto err_rel_regs;
1388 }
1389
1390 dc->send_buf = kmalloc(SEND_BUF_MAX, GFP_KERNEL);
1391 if (!dc->send_buf) {
1392 dev_err(&pdev->dev, "Could not allocate send buffer?\n");
1393 ret = -ENOMEM;
1394 goto err_free_sbuf;
1395 }
1396
Stefani Seibold9842c382009-12-21 14:37:29 -08001397 for (i = PORT_MDM; i < MAX_PORT; i++) {
Jiri Slabyc29bd8d2011-04-20 10:43:12 +02001398 if (kfifo_alloc(&dc->port[i].fifo_ul, FIFO_BUFFER_SIZE_UL,
1399 GFP_KERNEL)) {
Stefani Seibold9842c382009-12-21 14:37:29 -08001400 dev_err(&pdev->dev,
1401 "Could not allocate kfifo buffer\n");
1402 ret = -ENOMEM;
1403 goto err_free_kfifo;
1404 }
1405 }
1406
Frank Seidel20fd1e32007-11-09 14:49:23 +01001407 spin_lock_init(&dc->spin_mutex);
1408
1409 nozomi_setup_private_data(dc);
1410
1411 /* Disable all interrupts */
1412 dc->last_ier = 0;
1413 writew(dc->last_ier, dc->reg_ier);
1414
1415 ret = request_irq(pdev->irq, &interrupt_handler, IRQF_SHARED,
1416 NOZOMI_NAME, dc);
1417 if (unlikely(ret)) {
1418 dev_err(&pdev->dev, "can't request irq %d\n", pdev->irq);
Stefani Seibold9842c382009-12-21 14:37:29 -08001419 goto err_free_kfifo;
Frank Seidel20fd1e32007-11-09 14:49:23 +01001420 }
1421
1422 DBG1("base_addr: %p", dc->base_addr);
1423
1424 make_sysfs_files(dc);
1425
1426 dc->index_start = ndev_idx * MAX_PORT;
1427 ndevs[ndev_idx] = dc;
1428
Frank Seidel661b4e82008-03-06 21:45:57 +01001429 pci_set_drvdata(pdev, dc);
1430
1431 /* Enable RESET interrupt */
1432 dc->last_ier = RESET;
1433 iowrite16(dc->last_ier, dc->reg_ier);
1434
1435 dc->state = NOZOMI_STATE_ENABLED;
1436
Frank Seidel20fd1e32007-11-09 14:49:23 +01001437 for (i = 0; i < MAX_PORT; i++) {
Stefani Seibold9842c382009-12-21 14:37:29 -08001438 struct device *tty_dev;
Alan Cox266794e2010-02-08 10:06:45 +00001439 struct port *port = &dc->port[i];
1440 port->dc = dc;
Alan Cox266794e2010-02-08 10:06:45 +00001441 tty_port_init(&port->port);
1442 port->port.ops = &noz_tty_port_ops;
Jiri Slaby734cc172012-08-07 21:47:47 +02001443 tty_dev = tty_port_register_device(&port->port, ntty_driver,
1444 dc->index_start + i, &pdev->dev);
Stefani Seibold9842c382009-12-21 14:37:29 -08001445
1446 if (IS_ERR(tty_dev)) {
1447 ret = PTR_ERR(tty_dev);
1448 dev_err(&pdev->dev, "Could not allocate tty?\n");
Jiri Slaby191c5f12012-11-15 09:49:56 +01001449 tty_port_destroy(&port->port);
Stefani Seibold9842c382009-12-21 14:37:29 -08001450 goto err_free_tty;
1451 }
Frank Seidel20fd1e32007-11-09 14:49:23 +01001452 }
Stefani Seibold9842c382009-12-21 14:37:29 -08001453
Frank Seidel20fd1e32007-11-09 14:49:23 +01001454 return 0;
1455
Stefani Seibold9842c382009-12-21 14:37:29 -08001456err_free_tty:
Jiri Slaby191c5f12012-11-15 09:49:56 +01001457 for (i = 0; i < MAX_PORT; ++i) {
1458 tty_unregister_device(ntty_driver, dc->index_start + i);
1459 tty_port_destroy(&dc->port[i].port);
1460 }
Stefani Seibold9842c382009-12-21 14:37:29 -08001461err_free_kfifo:
1462 for (i = 0; i < MAX_PORT; i++)
1463 kfifo_free(&dc->port[i].fifo_ul);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001464err_free_sbuf:
1465 kfree(dc->send_buf);
1466 iounmap(dc->base_addr);
1467err_rel_regs:
1468 pci_release_regions(pdev);
1469err_disable_device:
1470 pci_disable_device(pdev);
1471err_free:
1472 kfree(dc);
1473err:
1474 return ret;
1475}
1476
Bill Pembertonae8d8a12012-11-19 13:26:18 -05001477static void tty_exit(struct nozomi *dc)
Frank Seidel20fd1e32007-11-09 14:49:23 +01001478{
1479 unsigned int i;
1480
1481 DBG1(" ");
1482
Jiri Slabyaa27a092013-03-07 13:12:30 +01001483 for (i = 0; i < MAX_PORT; ++i)
1484 tty_port_tty_hangup(&dc->port[i].port, false);
1485
Alan Cox33dd4742009-01-02 13:47:32 +00001486 /* Racy below - surely should wait for scheduled work to be done or
1487 complete off a hangup method ? */
Frank Seidel20fd1e32007-11-09 14:49:23 +01001488 while (dc->open_ttys)
1489 msleep(1);
Jiri Slaby191c5f12012-11-15 09:49:56 +01001490 for (i = 0; i < MAX_PORT; ++i) {
1491 tty_unregister_device(ntty_driver, dc->index_start + i);
1492 tty_port_destroy(&dc->port[i].port);
1493 }
Frank Seidel20fd1e32007-11-09 14:49:23 +01001494}
1495
1496/* Deallocate memory for one device */
Bill Pembertonae8d8a12012-11-19 13:26:18 -05001497static void nozomi_card_exit(struct pci_dev *pdev)
Frank Seidel20fd1e32007-11-09 14:49:23 +01001498{
1499 int i;
1500 struct ctrl_ul ctrl;
1501 struct nozomi *dc = pci_get_drvdata(pdev);
1502
1503 /* Disable all interrupts */
1504 dc->last_ier = 0;
1505 writew(dc->last_ier, dc->reg_ier);
1506
1507 tty_exit(dc);
1508
1509 /* Send 0x0001, command card to resend the reset token. */
1510 /* This is to get the reset when the module is reloaded. */
1511 ctrl.port = 0x00;
1512 ctrl.reserved = 0;
1513 ctrl.RTS = 0;
1514 ctrl.DTR = 1;
1515 DBG1("sending flow control 0x%04X", *((u16 *)&ctrl));
1516
1517 /* Setup dc->reg addresses to we can use defines here */
1518 write_mem32(dc->port[PORT_CTRL].ul_addr[0], (u32 *)&ctrl, 2);
1519 writew(CTRL_UL, dc->reg_fcr); /* push the token to the card. */
1520
1521 remove_sysfs_files(dc);
1522
1523 free_irq(pdev->irq, dc);
1524
1525 for (i = 0; i < MAX_PORT; i++)
Stefani Seibold45465482009-12-21 14:37:26 -08001526 kfifo_free(&dc->port[i].fifo_ul);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001527
1528 kfree(dc->send_buf);
1529
1530 iounmap(dc->base_addr);
1531
1532 pci_release_regions(pdev);
1533
1534 pci_disable_device(pdev);
1535
1536 ndevs[dc->index_start / MAX_PORT] = NULL;
1537
1538 kfree(dc);
1539}
1540
1541static void set_rts(const struct tty_struct *tty, int rts)
1542{
1543 struct port *port = get_port_by_tty(tty);
1544
1545 port->ctrl_ul.RTS = rts;
1546 port->update_flow_control = 1;
1547 enable_transmit_ul(PORT_CTRL, get_dc_by_tty(tty));
1548}
1549
1550static void set_dtr(const struct tty_struct *tty, int dtr)
1551{
1552 struct port *port = get_port_by_tty(tty);
1553
1554 DBG1("SETTING DTR index: %d, dtr: %d", tty->index, dtr);
1555
1556 port->ctrl_ul.DTR = dtr;
1557 port->update_flow_control = 1;
1558 enable_transmit_ul(PORT_CTRL, get_dc_by_tty(tty));
1559}
1560
1561/*
1562 * ----------------------------------------------------------------------------
1563 * TTY code
1564 * ----------------------------------------------------------------------------
1565 */
1566
Alan Cox266794e2010-02-08 10:06:45 +00001567static int ntty_install(struct tty_driver *driver, struct tty_struct *tty)
Frank Seidel20fd1e32007-11-09 14:49:23 +01001568{
1569 struct port *port = get_port_by_tty(tty);
1570 struct nozomi *dc = get_dc_by_tty(tty);
Alan Cox266794e2010-02-08 10:06:45 +00001571 int ret;
Frank Seidel661b4e82008-03-06 21:45:57 +01001572 if (!port || !dc || dc->state != NOZOMI_STATE_READY)
Frank Seidel20fd1e32007-11-09 14:49:23 +01001573 return -ENODEV;
Jiri Slaby81f58352012-01-30 21:14:30 +01001574 ret = tty_standard_install(driver, tty);
1575 if (ret == 0)
Jiri Slabybf9c1fc2010-07-29 11:46:32 +02001576 tty->driver_data = port;
Alan Cox266794e2010-02-08 10:06:45 +00001577 return ret;
1578}
1579
1580static void ntty_cleanup(struct tty_struct *tty)
1581{
1582 tty->driver_data = NULL;
1583}
1584
1585static int ntty_activate(struct tty_port *tport, struct tty_struct *tty)
1586{
1587 struct port *port = container_of(tport, struct port, port);
1588 struct nozomi *dc = port->dc;
1589 unsigned long flags;
1590
1591 DBG1("open: %d", port->token_dl);
1592 spin_lock_irqsave(&dc->spin_mutex, flags);
1593 dc->last_ier = dc->last_ier | port->token_dl;
1594 writew(dc->last_ier, dc->reg_ier);
1595 dc->open_ttys++;
1596 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1597 printk("noz: activated %d: %p\n", tty->index, tport);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001598 return 0;
1599}
1600
Alan Cox266794e2010-02-08 10:06:45 +00001601static int ntty_open(struct tty_struct *tty, struct file *filp)
Frank Seidel20fd1e32007-11-09 14:49:23 +01001602{
Jiri Slabybf9c1fc2010-07-29 11:46:32 +02001603 struct port *port = tty->driver_data;
Alan Cox266794e2010-02-08 10:06:45 +00001604 return tty_port_open(&port->port, tty, filp);
1605}
1606
1607static void ntty_shutdown(struct tty_port *tport)
1608{
1609 struct port *port = container_of(tport, struct port, port);
1610 struct nozomi *dc = port->dc;
Frank Seidel20fd1e32007-11-09 14:49:23 +01001611 unsigned long flags;
1612
Alan Cox266794e2010-02-08 10:06:45 +00001613 DBG1("close: %d", port->token_dl);
1614 spin_lock_irqsave(&dc->spin_mutex, flags);
1615 dc->last_ier &= ~(port->token_dl);
1616 writew(dc->last_ier, dc->reg_ier);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001617 dc->open_ttys--;
Alan Cox266794e2010-02-08 10:06:45 +00001618 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1619 printk("noz: shutdown %p\n", tport);
1620}
Frank Seidel20fd1e32007-11-09 14:49:23 +01001621
Alan Cox266794e2010-02-08 10:06:45 +00001622static void ntty_close(struct tty_struct *tty, struct file *filp)
1623{
1624 struct port *port = tty->driver_data;
1625 if (port)
1626 tty_port_close(&port->port, tty, filp);
1627}
1628
1629static void ntty_hangup(struct tty_struct *tty)
1630{
1631 struct port *port = tty->driver_data;
1632 tty_port_hangup(&port->port);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001633}
1634
1635/*
1636 * called when the userspace process writes to the tty (/dev/noz*).
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001637 * Data is inserted into a fifo, which is then read and transferred to the modem.
Frank Seidel20fd1e32007-11-09 14:49:23 +01001638 */
1639static int ntty_write(struct tty_struct *tty, const unsigned char *buffer,
1640 int count)
1641{
1642 int rval = -EINVAL;
1643 struct nozomi *dc = get_dc_by_tty(tty);
1644 struct port *port = tty->driver_data;
1645 unsigned long flags;
1646
1647 /* DBG1( "WRITEx: %d, index = %d", count, index); */
1648
1649 if (!dc || !port)
1650 return -ENODEV;
1651
Stefani Seibold7acd72e2009-12-21 14:37:28 -08001652 rval = kfifo_in(&port->fifo_ul, (unsigned char *)buffer, count);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001653
Frank Seidel20fd1e32007-11-09 14:49:23 +01001654 spin_lock_irqsave(&dc->spin_mutex, flags);
1655 /* CTS is only valid on the modem channel */
1656 if (port == &(dc->port[PORT_MDM])) {
1657 if (port->ctrl_dl.CTS) {
1658 DBG4("Enable interrupt");
1659 enable_transmit_ul(tty->index % MAX_PORT, dc);
1660 } else {
1661 dev_err(&dc->pdev->dev,
1662 "CTS not active on modem port?\n");
1663 }
1664 } else {
1665 enable_transmit_ul(tty->index % MAX_PORT, dc);
1666 }
1667 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1668
Frank Seidel20fd1e32007-11-09 14:49:23 +01001669 return rval;
1670}
1671
1672/*
1673 * Calculate how much is left in device
1674 * This method is called by the upper tty layer.
1675 * #according to sources N_TTY.c it expects a value >= 0 and
1676 * does not check for negative values.
Alan Coxe8c65d12010-02-08 10:07:04 +00001677 *
1678 * If the port is unplugged report lots of room and let the bits
1679 * dribble away so we don't block anything.
Frank Seidel20fd1e32007-11-09 14:49:23 +01001680 */
1681static int ntty_write_room(struct tty_struct *tty)
1682{
1683 struct port *port = tty->driver_data;
Alan Coxe8c65d12010-02-08 10:07:04 +00001684 int room = 4096;
Jan Engelhardt71e1b4a2008-02-01 09:13:53 +01001685 const struct nozomi *dc = get_dc_by_tty(tty);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001686
Jiri Slaby7fdc2892011-04-20 10:43:14 +02001687 if (dc)
Jiri Slaby6d742f62011-04-20 10:43:13 +02001688 room = kfifo_avail(&port->fifo_ul);
Jiri Slaby7fdc2892011-04-20 10:43:14 +02001689
Frank Seidel20fd1e32007-11-09 14:49:23 +01001690 return room;
1691}
1692
1693/* Gets io control parameters */
Alan Cox60b33c12011-02-14 16:26:14 +00001694static int ntty_tiocmget(struct tty_struct *tty)
Frank Seidel20fd1e32007-11-09 14:49:23 +01001695{
Jan Engelhardt71e1b4a2008-02-01 09:13:53 +01001696 const struct port *port = tty->driver_data;
1697 const struct ctrl_dl *ctrl_dl = &port->ctrl_dl;
1698 const struct ctrl_ul *ctrl_ul = &port->ctrl_ul;
Frank Seidel20fd1e32007-11-09 14:49:23 +01001699
Alan Cox978e5952008-04-30 00:53:59 -07001700 /* Note: these could change under us but it is not clear this
1701 matters if so */
Frank Seidel20fd1e32007-11-09 14:49:23 +01001702 return (ctrl_ul->RTS ? TIOCM_RTS : 0) |
1703 (ctrl_ul->DTR ? TIOCM_DTR : 0) |
1704 (ctrl_dl->DCD ? TIOCM_CAR : 0) |
1705 (ctrl_dl->RI ? TIOCM_RNG : 0) |
1706 (ctrl_dl->DSR ? TIOCM_DSR : 0) |
1707 (ctrl_dl->CTS ? TIOCM_CTS : 0);
1708}
1709
1710/* Sets io controls parameters */
Alan Cox20b9d172011-02-14 16:26:50 +00001711static int ntty_tiocmset(struct tty_struct *tty,
1712 unsigned int set, unsigned int clear)
Frank Seidel20fd1e32007-11-09 14:49:23 +01001713{
Frank Seidel661b4e82008-03-06 21:45:57 +01001714 struct nozomi *dc = get_dc_by_tty(tty);
1715 unsigned long flags;
1716
1717 spin_lock_irqsave(&dc->spin_mutex, flags);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001718 if (set & TIOCM_RTS)
1719 set_rts(tty, 1);
1720 else if (clear & TIOCM_RTS)
1721 set_rts(tty, 0);
1722
1723 if (set & TIOCM_DTR)
1724 set_dtr(tty, 1);
1725 else if (clear & TIOCM_DTR)
1726 set_dtr(tty, 0);
Frank Seidel661b4e82008-03-06 21:45:57 +01001727 spin_unlock_irqrestore(&dc->spin_mutex, flags);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001728
1729 return 0;
1730}
1731
1732static int ntty_cflags_changed(struct port *port, unsigned long flags,
1733 struct async_icount *cprev)
1734{
Frank Seidel18bbe0c22008-02-01 09:14:05 +01001735 const struct async_icount cnow = port->tty_icount;
Frank Seidel20fd1e32007-11-09 14:49:23 +01001736 int ret;
1737
1738 ret = ((flags & TIOCM_RNG) && (cnow.rng != cprev->rng)) ||
1739 ((flags & TIOCM_DSR) && (cnow.dsr != cprev->dsr)) ||
1740 ((flags & TIOCM_CD) && (cnow.dcd != cprev->dcd)) ||
1741 ((flags & TIOCM_CTS) && (cnow.cts != cprev->cts));
1742
1743 *cprev = cnow;
1744
1745 return ret;
1746}
1747
Alan Cox05871022010-09-16 18:21:52 +01001748static int ntty_tiocgicount(struct tty_struct *tty,
1749 struct serial_icounter_struct *icount)
Frank Seidel20fd1e32007-11-09 14:49:23 +01001750{
Alan Cox05871022010-09-16 18:21:52 +01001751 struct port *port = tty->driver_data;
Frank Seidel18bbe0c22008-02-01 09:14:05 +01001752 const struct async_icount cnow = port->tty_icount;
Frank Seidel20fd1e32007-11-09 14:49:23 +01001753
Alan Cox05871022010-09-16 18:21:52 +01001754 icount->cts = cnow.cts;
1755 icount->dsr = cnow.dsr;
1756 icount->rng = cnow.rng;
1757 icount->dcd = cnow.dcd;
1758 icount->rx = cnow.rx;
1759 icount->tx = cnow.tx;
1760 icount->frame = cnow.frame;
1761 icount->overrun = cnow.overrun;
1762 icount->parity = cnow.parity;
1763 icount->brk = cnow.brk;
1764 icount->buf_overrun = cnow.buf_overrun;
1765 return 0;
Frank Seidel20fd1e32007-11-09 14:49:23 +01001766}
1767
Alan Cox6caa76b2011-02-14 16:27:22 +00001768static int ntty_ioctl(struct tty_struct *tty,
Frank Seidel20fd1e32007-11-09 14:49:23 +01001769 unsigned int cmd, unsigned long arg)
1770{
1771 struct port *port = tty->driver_data;
Frank Seidel20fd1e32007-11-09 14:49:23 +01001772 int rval = -ENOIOCTLCMD;
1773
1774 DBG1("******** IOCTL, cmd: %d", cmd);
1775
1776 switch (cmd) {
1777 case TIOCMIWAIT: {
1778 struct async_icount cprev = port->tty_icount;
1779
1780 rval = wait_event_interruptible(port->tty_wait,
1781 ntty_cflags_changed(port, arg, &cprev));
1782 break;
Alan Cox05871022010-09-16 18:21:52 +01001783 }
Frank Seidel20fd1e32007-11-09 14:49:23 +01001784 default:
1785 DBG1("ERR: 0x%08X, %d", cmd, cmd);
1786 break;
Joe Perchesfc8114722013-10-08 16:14:21 -07001787 }
Frank Seidel20fd1e32007-11-09 14:49:23 +01001788
1789 return rval;
1790}
1791
1792/*
1793 * Called by the upper tty layer when tty buffers are ready
1794 * to receive data again after a call to throttle.
1795 */
1796static void ntty_unthrottle(struct tty_struct *tty)
1797{
1798 struct nozomi *dc = get_dc_by_tty(tty);
1799 unsigned long flags;
1800
1801 DBG1("UNTHROTTLE");
1802 spin_lock_irqsave(&dc->spin_mutex, flags);
1803 enable_transmit_dl(tty->index % MAX_PORT, dc);
1804 set_rts(tty, 1);
1805
1806 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1807}
1808
1809/*
1810 * Called by the upper tty layer when the tty buffers are almost full.
1811 * The driver should stop send more data.
1812 */
1813static void ntty_throttle(struct tty_struct *tty)
1814{
1815 struct nozomi *dc = get_dc_by_tty(tty);
1816 unsigned long flags;
1817
1818 DBG1("THROTTLE");
1819 spin_lock_irqsave(&dc->spin_mutex, flags);
1820 set_rts(tty, 0);
1821 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1822}
1823
Frank Seidel20fd1e32007-11-09 14:49:23 +01001824/* Returns number of chars in buffer, called by tty layer */
1825static s32 ntty_chars_in_buffer(struct tty_struct *tty)
1826{
1827 struct port *port = tty->driver_data;
1828 struct nozomi *dc = get_dc_by_tty(tty);
Alan Cox23198fd2009-07-20 16:05:27 +01001829 s32 rval = 0;
Frank Seidel20fd1e32007-11-09 14:49:23 +01001830
1831 if (unlikely(!dc || !port)) {
Frank Seidel20fd1e32007-11-09 14:49:23 +01001832 goto exit_in_buffer;
1833 }
1834
Stefani Seibolde64c0262009-12-21 14:37:28 -08001835 rval = kfifo_len(&port->fifo_ul);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001836
1837exit_in_buffer:
1838 return rval;
1839}
1840
Alan Cox266794e2010-02-08 10:06:45 +00001841static const struct tty_port_operations noz_tty_port_ops = {
1842 .activate = ntty_activate,
1843 .shutdown = ntty_shutdown,
1844};
1845
Frank Seidel18bbe0c22008-02-01 09:14:05 +01001846static const struct tty_operations tty_ops = {
Frank Seidel20fd1e32007-11-09 14:49:23 +01001847 .ioctl = ntty_ioctl,
1848 .open = ntty_open,
1849 .close = ntty_close,
Alan Cox266794e2010-02-08 10:06:45 +00001850 .hangup = ntty_hangup,
Frank Seidel20fd1e32007-11-09 14:49:23 +01001851 .write = ntty_write,
1852 .write_room = ntty_write_room,
1853 .unthrottle = ntty_unthrottle,
1854 .throttle = ntty_throttle,
1855 .chars_in_buffer = ntty_chars_in_buffer,
Frank Seidel20fd1e32007-11-09 14:49:23 +01001856 .tiocmget = ntty_tiocmget,
1857 .tiocmset = ntty_tiocmset,
Alan Cox05871022010-09-16 18:21:52 +01001858 .get_icount = ntty_tiocgicount,
Alan Cox266794e2010-02-08 10:06:45 +00001859 .install = ntty_install,
1860 .cleanup = ntty_cleanup,
Frank Seidel20fd1e32007-11-09 14:49:23 +01001861};
1862
1863/* Module initialization */
1864static struct pci_driver nozomi_driver = {
1865 .name = NOZOMI_NAME,
1866 .id_table = nozomi_pci_tbl,
1867 .probe = nozomi_card_init,
Bill Pemberton91116cb2012-11-19 13:21:06 -05001868 .remove = nozomi_card_exit,
Frank Seidel20fd1e32007-11-09 14:49:23 +01001869};
1870
1871static __init int nozomi_init(void)
1872{
1873 int ret;
1874
1875 printk(KERN_INFO "Initializing %s\n", VERSION_STRING);
1876
1877 ntty_driver = alloc_tty_driver(NTTY_TTY_MAXMINORS);
1878 if (!ntty_driver)
1879 return -ENOMEM;
1880
Frank Seidel20fd1e32007-11-09 14:49:23 +01001881 ntty_driver->driver_name = NOZOMI_NAME_TTY;
1882 ntty_driver->name = "noz";
1883 ntty_driver->major = 0;
1884 ntty_driver->type = TTY_DRIVER_TYPE_SERIAL;
1885 ntty_driver->subtype = SERIAL_TYPE_NORMAL;
1886 ntty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
1887 ntty_driver->init_termios = tty_std_termios;
1888 ntty_driver->init_termios.c_cflag = B115200 | CS8 | CREAD | \
1889 HUPCL | CLOCAL;
1890 ntty_driver->init_termios.c_ispeed = 115200;
1891 ntty_driver->init_termios.c_ospeed = 115200;
1892 tty_set_operations(ntty_driver, &tty_ops);
1893
1894 ret = tty_register_driver(ntty_driver);
1895 if (ret) {
1896 printk(KERN_ERR "Nozomi: failed to register ntty driver\n");
1897 goto free_tty;
1898 }
1899
1900 ret = pci_register_driver(&nozomi_driver);
1901 if (ret) {
1902 printk(KERN_ERR "Nozomi: can't register pci driver\n");
1903 goto unr_tty;
1904 }
1905
1906 return 0;
1907unr_tty:
1908 tty_unregister_driver(ntty_driver);
1909free_tty:
1910 put_tty_driver(ntty_driver);
1911 return ret;
1912}
1913
1914static __exit void nozomi_exit(void)
1915{
1916 printk(KERN_INFO "Unloading %s\n", DRIVER_DESC);
1917 pci_unregister_driver(&nozomi_driver);
1918 tty_unregister_driver(ntty_driver);
1919 put_tty_driver(ntty_driver);
1920}
1921
1922module_init(nozomi_init);
1923module_exit(nozomi_exit);
1924
Frank Seidel20fd1e32007-11-09 14:49:23 +01001925MODULE_LICENSE("Dual BSD/GPL");
1926MODULE_DESCRIPTION(DRIVER_DESC);