blob: 861e9504319193b4f1dc25d02203e530ccd27ca7 [file] [log] [blame]
Greg Kroah-Hartmane3b3d0f2017-11-06 18:11:51 +01001// SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
Frank Seidel20fd1e32007-11-09 14:49:23 +01002/*
3 * nozomi.c -- HSDPA driver Broadband Wireless Data Card - Globe Trotter
4 *
5 * Written by: Ulf Jakobsson,
Frank Seidele9ed5372008-01-25 21:13:24 +01006 * Jan Ã…kerfeldt,
Frank Seidel20fd1e32007-11-09 14:49:23 +01007 * Stefan Thomasson,
8 *
9 * Maintained by: Paul Hardwick (p.hardwick@option.com)
10 *
11 * Patches:
12 * Locking code changes for Vodafone by Sphere Systems Ltd,
13 * Andrew Bird (ajb@spheresystems.co.uk )
14 * & Phil Sanderson
15 *
16 * Source has been ported from an implementation made by Filip Aben @ Option
17 *
18 * --------------------------------------------------------------------------
19 *
20 * Copyright (c) 2005,2006 Option Wireless Sweden AB
21 * Copyright (c) 2006 Sphere Systems Ltd
22 * Copyright (c) 2006 Option Wireless n/v
23 * All rights Reserved.
24 *
Frank Seidel20fd1e32007-11-09 14:49:23 +010025 * --------------------------------------------------------------------------
26 */
27
Frank Seidel20fd1e32007-11-09 14:49:23 +010028/* Enable this to have a lot of debug printouts */
29#define DEBUG
30
31#include <linux/kernel.h>
32#include <linux/module.h>
33#include <linux/pci.h>
34#include <linux/ioport.h>
35#include <linux/tty.h>
36#include <linux/tty_driver.h>
37#include <linux/tty_flip.h>
Alexey Dobriyand43c36d2009-10-07 17:09:06 +040038#include <linux/sched.h>
Frank Seidel20fd1e32007-11-09 14:49:23 +010039#include <linux/serial.h>
40#include <linux/interrupt.h>
41#include <linux/kmod.h>
42#include <linux/init.h>
43#include <linux/kfifo.h>
44#include <linux/uaccess.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090045#include <linux/slab.h>
Frank Seidel20fd1e32007-11-09 14:49:23 +010046#include <asm/byteorder.h>
47
48#include <linux/delay.h>
49
50
Michal Marek2cae8de2011-04-01 12:41:20 +020051#define VERSION_STRING DRIVER_DESC " 2.1d"
Frank Seidel20fd1e32007-11-09 14:49:23 +010052
Frank Seidel20fd1e32007-11-09 14:49:23 +010053/* Default debug printout level */
54#define NOZOMI_DEBUG_LEVEL 0x00
Arnd Bergmann5020ded2016-11-29 12:51:04 +010055static int debug = NOZOMI_DEBUG_LEVEL;
56module_param(debug, int, S_IRUGO | S_IWUSR);
Frank Seidel20fd1e32007-11-09 14:49:23 +010057
Arnd Bergmann5020ded2016-11-29 12:51:04 +010058/* Macros definitions */
59#define DBG_(lvl, fmt, args...) \
60do { \
61 if (lvl & debug) \
62 pr_debug("[%d] %s(): " fmt "\n", \
63 __LINE__, __func__, ##args); \
Frank Seidel20fd1e32007-11-09 14:49:23 +010064} while (0)
65
Arnd Bergmann5020ded2016-11-29 12:51:04 +010066#define DBG1(args...) DBG_(0x01, ##args)
67#define DBG2(args...) DBG_(0x02, ##args)
68#define DBG3(args...) DBG_(0x04, ##args)
69#define DBG4(args...) DBG_(0x08, ##args)
Frank Seidel20fd1e32007-11-09 14:49:23 +010070
71/* TODO: rewrite to optimize macros... */
72
73#define TMP_BUF_MAX 256
74
Joey Pabalinas2ea04522018-04-24 19:48:03 -100075#define DUMP(buf__, len__) \
76 do { \
77 char tbuf[TMP_BUF_MAX] = {0}; \
78 if (len__ > 1) { \
79 u32 data_len = min_t(u32, len__, TMP_BUF_MAX); \
80 strscpy(tbuf, buf__, data_len); \
81 if (tbuf[data_len - 2] == '\r') \
82 tbuf[data_len - 2] = 'r'; \
83 DBG1("SENDING: '%s' (%d+n)", tbuf, len__); \
84 } else { \
85 DBG1("SENDING: '%s' (%d)", tbuf, len__); \
86 } \
87 } while (0)
Frank Seidel20fd1e32007-11-09 14:49:23 +010088
89/* Defines */
90#define NOZOMI_NAME "nozomi"
91#define NOZOMI_NAME_TTY "nozomi_tty"
92#define DRIVER_DESC "Nozomi driver"
93
94#define NTTY_TTY_MAXMINORS 256
95#define NTTY_FIFO_BUFFER_SIZE 8192
96
97/* Must be power of 2 */
98#define FIFO_BUFFER_SIZE_UL 8192
99
100/* Size of tmp send buffer to card */
101#define SEND_BUF_MAX 1024
102#define RECEIVE_BUF_MAX 4
103
104
Joey Pabalinasc6964e92018-04-24 19:48:04 -1000105#define R_IIR 0x0000 /* Interrupt Identity Register */
106#define R_FCR 0x0000 /* Flow Control Register */
107#define R_IER 0x0004 /* Interrupt Enable Register */
Frank Seidel20fd1e32007-11-09 14:49:23 +0100108
Valentin Rothberg03ac6b32015-04-12 18:35:18 +0200109#define NOZOMI_CONFIG_MAGIC 0xEFEFFEFE
110#define TOGGLE_VALID 0x0000
Frank Seidel20fd1e32007-11-09 14:49:23 +0100111
112/* Definition of interrupt tokens */
Joey Pabalinasc6964e92018-04-24 19:48:04 -1000113#define MDM_DL1 0x0001
114#define MDM_UL1 0x0002
115#define MDM_DL2 0x0004
116#define MDM_UL2 0x0008
117#define DIAG_DL1 0x0010
118#define DIAG_DL2 0x0020
119#define DIAG_UL 0x0040
120#define APP1_DL 0x0080
121#define APP1_UL 0x0100
122#define APP2_DL 0x0200
123#define APP2_UL 0x0400
124#define CTRL_DL 0x0800
125#define CTRL_UL 0x1000
126#define RESET 0x8000
Frank Seidel20fd1e32007-11-09 14:49:23 +0100127
Joey Pabalinasc6964e92018-04-24 19:48:04 -1000128#define MDM_DL (MDM_DL1 | MDM_DL2)
129#define MDM_UL (MDM_UL1 | MDM_UL2)
130#define DIAG_DL (DIAG_DL1 | DIAG_DL2)
Frank Seidel20fd1e32007-11-09 14:49:23 +0100131
132/* modem signal definition */
Joey Pabalinasc6964e92018-04-24 19:48:04 -1000133#define CTRL_DSR 0x0001
134#define CTRL_DCD 0x0002
135#define CTRL_RI 0x0004
136#define CTRL_CTS 0x0008
Frank Seidel20fd1e32007-11-09 14:49:23 +0100137
Joey Pabalinasc6964e92018-04-24 19:48:04 -1000138#define CTRL_DTR 0x0001
139#define CTRL_RTS 0x0002
Frank Seidel20fd1e32007-11-09 14:49:23 +0100140
141#define MAX_PORT 4
142#define NOZOMI_MAX_PORTS 5
143#define NOZOMI_MAX_CARDS (NTTY_TTY_MAXMINORS / MAX_PORT)
144
145/* Type definitions */
146
147/*
148 * There are two types of nozomi cards,
149 * one with 2048 memory and with 8192 memory
150 */
151enum card_type {
152 F32_2 = 2048, /* 512 bytes downlink + uplink * 2 -> 2048 */
153 F32_8 = 8192, /* 3072 bytes downl. + 1024 bytes uplink * 2 -> 8192 */
154};
155
Frank Seidel661b4e82008-03-06 21:45:57 +0100156/* Initialization states a card can be in */
157enum card_state {
Colin Ian King44058982018-04-18 12:35:28 +0100158 NOZOMI_STATE_UNKNOWN = 0,
Frank Seidel661b4e82008-03-06 21:45:57 +0100159 NOZOMI_STATE_ENABLED = 1, /* pci device enabled */
160 NOZOMI_STATE_ALLOCATED = 2, /* config setup done */
161 NOZOMI_STATE_READY = 3, /* flowcontrols received */
162};
163
Frank Seidel20fd1e32007-11-09 14:49:23 +0100164/* Two different toggle channels exist */
165enum channel_type {
166 CH_A = 0,
167 CH_B = 1,
168};
169
170/* Port definition for the card regarding flow control */
171enum ctrl_port_type {
172 CTRL_CMD = 0,
173 CTRL_MDM = 1,
174 CTRL_DIAG = 2,
175 CTRL_APP1 = 3,
176 CTRL_APP2 = 4,
177 CTRL_ERROR = -1,
178};
179
180/* Ports that the nozomi has */
181enum port_type {
182 PORT_MDM = 0,
183 PORT_DIAG = 1,
184 PORT_APP1 = 2,
185 PORT_APP2 = 3,
186 PORT_CTRL = 4,
187 PORT_ERROR = -1,
188};
189
190#ifdef __BIG_ENDIAN
191/* Big endian */
192
193struct toggles {
Frank Seidele9ed5372008-01-25 21:13:24 +0100194 unsigned int enabled:5; /*
Frank Seidel20fd1e32007-11-09 14:49:23 +0100195 * Toggle fields are valid if enabled is 0,
196 * else A-channels must always be used.
197 */
Frank Seidele9ed5372008-01-25 21:13:24 +0100198 unsigned int diag_dl:1;
199 unsigned int mdm_dl:1;
200 unsigned int mdm_ul:1;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100201} __attribute__ ((packed));
202
203/* Configuration table to read at startup of card */
204/* Is for now only needed during initialization phase */
205struct config_table {
206 u32 signature;
207 u16 product_information;
208 u16 version;
209 u8 pad3[3];
210 struct toggles toggle;
211 u8 pad1[4];
212 u16 dl_mdm_len1; /*
213 * If this is 64, it can hold
214 * 60 bytes + 4 that is length field
215 */
216 u16 dl_start;
217
218 u16 dl_diag_len1;
219 u16 dl_mdm_len2; /*
220 * If this is 64, it can hold
221 * 60 bytes + 4 that is length field
222 */
223 u16 dl_app1_len;
224
225 u16 dl_diag_len2;
226 u16 dl_ctrl_len;
227 u16 dl_app2_len;
228 u8 pad2[16];
229 u16 ul_mdm_len1;
230 u16 ul_start;
231 u16 ul_diag_len;
232 u16 ul_mdm_len2;
233 u16 ul_app1_len;
234 u16 ul_app2_len;
235 u16 ul_ctrl_len;
236} __attribute__ ((packed));
237
238/* This stores all control downlink flags */
239struct ctrl_dl {
240 u8 port;
Frank Seidele9ed5372008-01-25 21:13:24 +0100241 unsigned int reserved:4;
242 unsigned int CTS:1;
243 unsigned int RI:1;
244 unsigned int DCD:1;
245 unsigned int DSR:1;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100246} __attribute__ ((packed));
247
248/* This stores all control uplink flags */
249struct ctrl_ul {
250 u8 port;
Frank Seidele9ed5372008-01-25 21:13:24 +0100251 unsigned int reserved:6;
252 unsigned int RTS:1;
253 unsigned int DTR:1;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100254} __attribute__ ((packed));
255
256#else
257/* Little endian */
258
259/* This represents the toggle information */
260struct toggles {
Frank Seidele9ed5372008-01-25 21:13:24 +0100261 unsigned int mdm_ul:1;
262 unsigned int mdm_dl:1;
263 unsigned int diag_dl:1;
264 unsigned int enabled:5; /*
Frank Seidel20fd1e32007-11-09 14:49:23 +0100265 * Toggle fields are valid if enabled is 0,
266 * else A-channels must always be used.
267 */
268} __attribute__ ((packed));
269
270/* Configuration table to read at startup of card */
271struct config_table {
272 u32 signature;
273 u16 version;
274 u16 product_information;
275 struct toggles toggle;
276 u8 pad1[7];
277 u16 dl_start;
278 u16 dl_mdm_len1; /*
279 * If this is 64, it can hold
280 * 60 bytes + 4 that is length field
281 */
282 u16 dl_mdm_len2;
283 u16 dl_diag_len1;
284 u16 dl_diag_len2;
285 u16 dl_app1_len;
286 u16 dl_app2_len;
287 u16 dl_ctrl_len;
288 u8 pad2[16];
289 u16 ul_start;
290 u16 ul_mdm_len2;
291 u16 ul_mdm_len1;
292 u16 ul_diag_len;
293 u16 ul_app1_len;
294 u16 ul_app2_len;
295 u16 ul_ctrl_len;
296} __attribute__ ((packed));
297
298/* This stores all control downlink flags */
299struct ctrl_dl {
Frank Seidele9ed5372008-01-25 21:13:24 +0100300 unsigned int DSR:1;
301 unsigned int DCD:1;
302 unsigned int RI:1;
303 unsigned int CTS:1;
Alexandre Bellonie2c2e792020-02-14 15:14:55 +0100304 unsigned int reserved:4;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100305 u8 port;
306} __attribute__ ((packed));
307
308/* This stores all control uplink flags */
309struct ctrl_ul {
Frank Seidele9ed5372008-01-25 21:13:24 +0100310 unsigned int DTR:1;
311 unsigned int RTS:1;
312 unsigned int reserved:6;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100313 u8 port;
314} __attribute__ ((packed));
315#endif
316
317/* This holds all information that is needed regarding a port */
318struct port {
Alan Cox33dd4742009-01-02 13:47:32 +0000319 struct tty_port port;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100320 u8 update_flow_control;
321 struct ctrl_ul ctrl_ul;
322 struct ctrl_dl ctrl_dl;
Stefani Seibold45465482009-12-21 14:37:26 -0800323 struct kfifo fifo_ul;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100324 void __iomem *dl_addr[2];
325 u32 dl_size[2];
326 u8 toggle_dl;
327 void __iomem *ul_addr[2];
328 u32 ul_size[2];
329 u8 toggle_ul;
330 u16 token_dl;
331
Frank Seidel20fd1e32007-11-09 14:49:23 +0100332 wait_queue_head_t tty_wait;
333 struct async_icount tty_icount;
Alan Cox266794e2010-02-08 10:06:45 +0000334
335 struct nozomi *dc;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100336};
337
338/* Private data one for each card in the system */
339struct nozomi {
340 void __iomem *base_addr;
341 unsigned long flip;
342
343 /* Pointers to registers */
344 void __iomem *reg_iir;
345 void __iomem *reg_fcr;
346 void __iomem *reg_ier;
347
348 u16 last_ier;
349 enum card_type card_type;
350 struct config_table config_table; /* Configuration table */
351 struct pci_dev *pdev;
352 struct port port[NOZOMI_MAX_PORTS];
353 u8 *send_buf;
354
355 spinlock_t spin_mutex; /* secures access to registers and tty */
356
357 unsigned int index_start;
Frank Seidel661b4e82008-03-06 21:45:57 +0100358 enum card_state state;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100359 u32 open_ttys;
360};
361
362/* This is a data packet that is read or written to/from card */
363struct buffer {
364 u32 size; /* size is the length of the data buffer */
365 u8 *data;
366} __attribute__ ((packed));
367
Joey Pabalinasc6964e92018-04-24 19:48:04 -1000368/* Global variables */
Bill Pemberton512f82a2012-11-19 13:25:19 -0500369static const struct pci_device_id nozomi_pci_tbl[] = {
Alan Coxb2a3dbc2010-02-08 10:07:15 +0000370 {PCI_DEVICE(0x1931, 0x000c)}, /* Nozomi HSDPA */
Frank Seidel20fd1e32007-11-09 14:49:23 +0100371 {},
372};
373
374MODULE_DEVICE_TABLE(pci, nozomi_pci_tbl);
375
376static struct nozomi *ndevs[NOZOMI_MAX_CARDS];
377static struct tty_driver *ntty_driver;
378
Alan Cox266794e2010-02-08 10:06:45 +0000379static const struct tty_port_operations noz_tty_port_ops;
380
Frank Seidel20fd1e32007-11-09 14:49:23 +0100381/*
382 * find card by tty_index
383 */
384static inline struct nozomi *get_dc_by_tty(const struct tty_struct *tty)
385{
386 return tty ? ndevs[tty->index / MAX_PORT] : NULL;
387}
388
389static inline struct port *get_port_by_tty(const struct tty_struct *tty)
390{
391 struct nozomi *ndev = get_dc_by_tty(tty);
392 return ndev ? &ndev->port[tty->index % MAX_PORT] : NULL;
393}
394
395/*
396 * TODO:
397 * -Optimize
398 * -Rewrite cleaner
399 */
400
401static void read_mem32(u32 *buf, const void __iomem *mem_addr_start,
402 u32 size_bytes)
403{
404 u32 i = 0;
Al Viro782a6de2008-03-29 03:09:08 +0000405 const u32 __iomem *ptr = mem_addr_start;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100406 u16 *buf16;
407
408 if (unlikely(!ptr || !buf))
409 goto out;
410
411 /* shortcut for extremely often used cases */
412 switch (size_bytes) {
413 case 2: /* 2 bytes */
414 buf16 = (u16 *) buf;
Al Viro782a6de2008-03-29 03:09:08 +0000415 *buf16 = __le16_to_cpu(readw(ptr));
Frank Seidel20fd1e32007-11-09 14:49:23 +0100416 goto out;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100417 case 4: /* 4 bytes */
Al Viro782a6de2008-03-29 03:09:08 +0000418 *(buf) = __le32_to_cpu(readl(ptr));
Frank Seidel20fd1e32007-11-09 14:49:23 +0100419 goto out;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100420 }
421
422 while (i < size_bytes) {
423 if (size_bytes - i == 2) {
424 /* Handle 2 bytes in the end */
425 buf16 = (u16 *) buf;
Al Viro782a6de2008-03-29 03:09:08 +0000426 *(buf16) = __le16_to_cpu(readw(ptr));
Frank Seidel20fd1e32007-11-09 14:49:23 +0100427 i += 2;
428 } else {
429 /* Read 4 bytes */
Al Viro782a6de2008-03-29 03:09:08 +0000430 *(buf) = __le32_to_cpu(readl(ptr));
Frank Seidel20fd1e32007-11-09 14:49:23 +0100431 i += 4;
432 }
433 buf++;
434 ptr++;
435 }
436out:
437 return;
438}
439
440/*
441 * TODO:
442 * -Optimize
443 * -Rewrite cleaner
444 */
Jan Engelhardt71e1b4a2008-02-01 09:13:53 +0100445static u32 write_mem32(void __iomem *mem_addr_start, const u32 *buf,
Frank Seidel20fd1e32007-11-09 14:49:23 +0100446 u32 size_bytes)
447{
448 u32 i = 0;
Al Viro782a6de2008-03-29 03:09:08 +0000449 u32 __iomem *ptr = mem_addr_start;
Jan Engelhardt71e1b4a2008-02-01 09:13:53 +0100450 const u16 *buf16;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100451
452 if (unlikely(!ptr || !buf))
453 return 0;
454
455 /* shortcut for extremely often used cases */
456 switch (size_bytes) {
457 case 2: /* 2 bytes */
Jan Engelhardt71e1b4a2008-02-01 09:13:53 +0100458 buf16 = (const u16 *)buf;
Al Viro782a6de2008-03-29 03:09:08 +0000459 writew(__cpu_to_le16(*buf16), ptr);
Frank Seidel20fd1e32007-11-09 14:49:23 +0100460 return 2;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100461 case 1: /*
462 * also needs to write 4 bytes in this case
463 * so falling through..
464 */
Tom Rix1a460c32020-10-19 10:57:32 -0700465 fallthrough;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100466 case 4: /* 4 bytes */
Al Viro782a6de2008-03-29 03:09:08 +0000467 writel(__cpu_to_le32(*buf), ptr);
Frank Seidel20fd1e32007-11-09 14:49:23 +0100468 return 4;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100469 }
470
471 while (i < size_bytes) {
472 if (size_bytes - i == 2) {
473 /* 2 bytes */
Jan Engelhardt71e1b4a2008-02-01 09:13:53 +0100474 buf16 = (const u16 *)buf;
Al Viro782a6de2008-03-29 03:09:08 +0000475 writew(__cpu_to_le16(*buf16), ptr);
Frank Seidel20fd1e32007-11-09 14:49:23 +0100476 i += 2;
477 } else {
478 /* 4 bytes */
Al Viro782a6de2008-03-29 03:09:08 +0000479 writel(__cpu_to_le32(*buf), ptr);
Frank Seidel20fd1e32007-11-09 14:49:23 +0100480 i += 4;
481 }
482 buf++;
483 ptr++;
484 }
485 return i;
486}
487
488/* Setup pointers to different channels and also setup buffer sizes. */
Chen Gang7acb88a2014-08-08 23:56:34 +0800489static void nozomi_setup_memory(struct nozomi *dc)
Frank Seidel20fd1e32007-11-09 14:49:23 +0100490{
491 void __iomem *offset = dc->base_addr + dc->config_table.dl_start;
492 /* The length reported is including the length field of 4 bytes,
493 * hence subtract with 4.
494 */
495 const u16 buff_offset = 4;
496
497 /* Modem port dl configuration */
498 dc->port[PORT_MDM].dl_addr[CH_A] = offset;
499 dc->port[PORT_MDM].dl_addr[CH_B] =
500 (offset += dc->config_table.dl_mdm_len1);
501 dc->port[PORT_MDM].dl_size[CH_A] =
502 dc->config_table.dl_mdm_len1 - buff_offset;
503 dc->port[PORT_MDM].dl_size[CH_B] =
504 dc->config_table.dl_mdm_len2 - buff_offset;
505
506 /* Diag port dl configuration */
507 dc->port[PORT_DIAG].dl_addr[CH_A] =
508 (offset += dc->config_table.dl_mdm_len2);
509 dc->port[PORT_DIAG].dl_size[CH_A] =
510 dc->config_table.dl_diag_len1 - buff_offset;
511 dc->port[PORT_DIAG].dl_addr[CH_B] =
512 (offset += dc->config_table.dl_diag_len1);
513 dc->port[PORT_DIAG].dl_size[CH_B] =
514 dc->config_table.dl_diag_len2 - buff_offset;
515
516 /* App1 port dl configuration */
517 dc->port[PORT_APP1].dl_addr[CH_A] =
518 (offset += dc->config_table.dl_diag_len2);
519 dc->port[PORT_APP1].dl_size[CH_A] =
520 dc->config_table.dl_app1_len - buff_offset;
521
522 /* App2 port dl configuration */
523 dc->port[PORT_APP2].dl_addr[CH_A] =
524 (offset += dc->config_table.dl_app1_len);
525 dc->port[PORT_APP2].dl_size[CH_A] =
526 dc->config_table.dl_app2_len - buff_offset;
527
528 /* Ctrl dl configuration */
529 dc->port[PORT_CTRL].dl_addr[CH_A] =
530 (offset += dc->config_table.dl_app2_len);
531 dc->port[PORT_CTRL].dl_size[CH_A] =
532 dc->config_table.dl_ctrl_len - buff_offset;
533
534 offset = dc->base_addr + dc->config_table.ul_start;
535
536 /* Modem Port ul configuration */
537 dc->port[PORT_MDM].ul_addr[CH_A] = offset;
538 dc->port[PORT_MDM].ul_size[CH_A] =
539 dc->config_table.ul_mdm_len1 - buff_offset;
540 dc->port[PORT_MDM].ul_addr[CH_B] =
541 (offset += dc->config_table.ul_mdm_len1);
542 dc->port[PORT_MDM].ul_size[CH_B] =
543 dc->config_table.ul_mdm_len2 - buff_offset;
544
545 /* Diag port ul configuration */
546 dc->port[PORT_DIAG].ul_addr[CH_A] =
547 (offset += dc->config_table.ul_mdm_len2);
548 dc->port[PORT_DIAG].ul_size[CH_A] =
549 dc->config_table.ul_diag_len - buff_offset;
550
551 /* App1 port ul configuration */
552 dc->port[PORT_APP1].ul_addr[CH_A] =
553 (offset += dc->config_table.ul_diag_len);
554 dc->port[PORT_APP1].ul_size[CH_A] =
555 dc->config_table.ul_app1_len - buff_offset;
556
557 /* App2 port ul configuration */
558 dc->port[PORT_APP2].ul_addr[CH_A] =
559 (offset += dc->config_table.ul_app1_len);
560 dc->port[PORT_APP2].ul_size[CH_A] =
561 dc->config_table.ul_app2_len - buff_offset;
562
563 /* Ctrl ul configuration */
564 dc->port[PORT_CTRL].ul_addr[CH_A] =
565 (offset += dc->config_table.ul_app2_len);
566 dc->port[PORT_CTRL].ul_size[CH_A] =
567 dc->config_table.ul_ctrl_len - buff_offset;
568}
569
570/* Dump config table under initalization phase */
571#ifdef DEBUG
572static void dump_table(const struct nozomi *dc)
573{
574 DBG3("signature: 0x%08X", dc->config_table.signature);
575 DBG3("version: 0x%04X", dc->config_table.version);
576 DBG3("product_information: 0x%04X", \
577 dc->config_table.product_information);
578 DBG3("toggle enabled: %d", dc->config_table.toggle.enabled);
579 DBG3("toggle up_mdm: %d", dc->config_table.toggle.mdm_ul);
580 DBG3("toggle dl_mdm: %d", dc->config_table.toggle.mdm_dl);
581 DBG3("toggle dl_dbg: %d", dc->config_table.toggle.diag_dl);
582
583 DBG3("dl_start: 0x%04X", dc->config_table.dl_start);
584 DBG3("dl_mdm_len0: 0x%04X, %d", dc->config_table.dl_mdm_len1,
585 dc->config_table.dl_mdm_len1);
586 DBG3("dl_mdm_len1: 0x%04X, %d", dc->config_table.dl_mdm_len2,
587 dc->config_table.dl_mdm_len2);
588 DBG3("dl_diag_len0: 0x%04X, %d", dc->config_table.dl_diag_len1,
589 dc->config_table.dl_diag_len1);
590 DBG3("dl_diag_len1: 0x%04X, %d", dc->config_table.dl_diag_len2,
591 dc->config_table.dl_diag_len2);
592 DBG3("dl_app1_len: 0x%04X, %d", dc->config_table.dl_app1_len,
593 dc->config_table.dl_app1_len);
594 DBG3("dl_app2_len: 0x%04X, %d", dc->config_table.dl_app2_len,
595 dc->config_table.dl_app2_len);
596 DBG3("dl_ctrl_len: 0x%04X, %d", dc->config_table.dl_ctrl_len,
597 dc->config_table.dl_ctrl_len);
598 DBG3("ul_start: 0x%04X, %d", dc->config_table.ul_start,
599 dc->config_table.ul_start);
600 DBG3("ul_mdm_len[0]: 0x%04X, %d", dc->config_table.ul_mdm_len1,
601 dc->config_table.ul_mdm_len1);
602 DBG3("ul_mdm_len[1]: 0x%04X, %d", dc->config_table.ul_mdm_len2,
603 dc->config_table.ul_mdm_len2);
604 DBG3("ul_diag_len: 0x%04X, %d", dc->config_table.ul_diag_len,
605 dc->config_table.ul_diag_len);
606 DBG3("ul_app1_len: 0x%04X, %d", dc->config_table.ul_app1_len,
607 dc->config_table.ul_app1_len);
608 DBG3("ul_app2_len: 0x%04X, %d", dc->config_table.ul_app2_len,
609 dc->config_table.ul_app2_len);
610 DBG3("ul_ctrl_len: 0x%04X, %d", dc->config_table.ul_ctrl_len,
611 dc->config_table.ul_ctrl_len);
612}
613#else
Frank Seidele9ed5372008-01-25 21:13:24 +0100614static inline void dump_table(const struct nozomi *dc) { }
Frank Seidel20fd1e32007-11-09 14:49:23 +0100615#endif
616
617/*
618 * Read configuration table from card under intalization phase
619 * Returns 1 if ok, else 0
620 */
621static int nozomi_read_config_table(struct nozomi *dc)
622{
623 read_mem32((u32 *) &dc->config_table, dc->base_addr + 0,
624 sizeof(struct config_table));
625
Valentin Rothberg03ac6b32015-04-12 18:35:18 +0200626 if (dc->config_table.signature != NOZOMI_CONFIG_MAGIC) {
Frank Seidel20fd1e32007-11-09 14:49:23 +0100627 dev_err(&dc->pdev->dev, "ConfigTable Bad! 0x%08X != 0x%08X\n",
Valentin Rothberg03ac6b32015-04-12 18:35:18 +0200628 dc->config_table.signature, NOZOMI_CONFIG_MAGIC);
Frank Seidel20fd1e32007-11-09 14:49:23 +0100629 return 0;
630 }
631
632 if ((dc->config_table.version == 0)
633 || (dc->config_table.toggle.enabled == TOGGLE_VALID)) {
634 int i;
635 DBG1("Second phase, configuring card");
636
Chen Gang7acb88a2014-08-08 23:56:34 +0800637 nozomi_setup_memory(dc);
Frank Seidel20fd1e32007-11-09 14:49:23 +0100638
639 dc->port[PORT_MDM].toggle_ul = dc->config_table.toggle.mdm_ul;
640 dc->port[PORT_MDM].toggle_dl = dc->config_table.toggle.mdm_dl;
641 dc->port[PORT_DIAG].toggle_dl = dc->config_table.toggle.diag_dl;
642 DBG1("toggle ports: MDM UL:%d MDM DL:%d, DIAG DL:%d",
643 dc->port[PORT_MDM].toggle_ul,
644 dc->port[PORT_MDM].toggle_dl, dc->port[PORT_DIAG].toggle_dl);
645
646 dump_table(dc);
647
648 for (i = PORT_MDM; i < MAX_PORT; i++) {
Frank Seidel20fd1e32007-11-09 14:49:23 +0100649 memset(&dc->port[i].ctrl_dl, 0, sizeof(struct ctrl_dl));
650 memset(&dc->port[i].ctrl_ul, 0, sizeof(struct ctrl_ul));
651 }
652
653 /* Enable control channel */
654 dc->last_ier = dc->last_ier | CTRL_DL;
655 writew(dc->last_ier, dc->reg_ier);
656
Frank Seidel661b4e82008-03-06 21:45:57 +0100657 dc->state = NOZOMI_STATE_ALLOCATED;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100658 dev_info(&dc->pdev->dev, "Initialization OK!\n");
659 return 1;
660 }
661
662 if ((dc->config_table.version > 0)
663 && (dc->config_table.toggle.enabled != TOGGLE_VALID)) {
664 u32 offset = 0;
665 DBG1("First phase: pushing upload buffers, clearing download");
666
667 dev_info(&dc->pdev->dev, "Version of card: %d\n",
668 dc->config_table.version);
669
670 /* Here we should disable all I/O over F32. */
Chen Gang7acb88a2014-08-08 23:56:34 +0800671 nozomi_setup_memory(dc);
Frank Seidel20fd1e32007-11-09 14:49:23 +0100672
673 /*
674 * We should send ALL channel pair tokens back along
675 * with reset token
676 */
677
678 /* push upload modem buffers */
679 write_mem32(dc->port[PORT_MDM].ul_addr[CH_A],
680 (u32 *) &offset, 4);
681 write_mem32(dc->port[PORT_MDM].ul_addr[CH_B],
682 (u32 *) &offset, 4);
683
684 writew(MDM_UL | DIAG_DL | MDM_DL, dc->reg_fcr);
685
686 DBG1("First phase done");
687 }
688
689 return 1;
690}
691
692/* Enable uplink interrupts */
693static void enable_transmit_ul(enum port_type port, struct nozomi *dc)
694{
Jan Engelhardt71e1b4a2008-02-01 09:13:53 +0100695 static const u16 mask[] = {MDM_UL, DIAG_UL, APP1_UL, APP2_UL, CTRL_UL};
Frank Seidel20fd1e32007-11-09 14:49:23 +0100696
697 if (port < NOZOMI_MAX_PORTS) {
698 dc->last_ier |= mask[port];
699 writew(dc->last_ier, dc->reg_ier);
700 } else {
701 dev_err(&dc->pdev->dev, "Called with wrong port?\n");
702 }
703}
704
705/* Disable uplink interrupts */
706static void disable_transmit_ul(enum port_type port, struct nozomi *dc)
707{
Jan Engelhardt71e1b4a2008-02-01 09:13:53 +0100708 static const u16 mask[] =
709 {~MDM_UL, ~DIAG_UL, ~APP1_UL, ~APP2_UL, ~CTRL_UL};
Frank Seidel20fd1e32007-11-09 14:49:23 +0100710
711 if (port < NOZOMI_MAX_PORTS) {
712 dc->last_ier &= mask[port];
713 writew(dc->last_ier, dc->reg_ier);
714 } else {
715 dev_err(&dc->pdev->dev, "Called with wrong port?\n");
716 }
717}
718
719/* Enable downlink interrupts */
720static void enable_transmit_dl(enum port_type port, struct nozomi *dc)
721{
Jan Engelhardt71e1b4a2008-02-01 09:13:53 +0100722 static const u16 mask[] = {MDM_DL, DIAG_DL, APP1_DL, APP2_DL, CTRL_DL};
Frank Seidel20fd1e32007-11-09 14:49:23 +0100723
724 if (port < NOZOMI_MAX_PORTS) {
725 dc->last_ier |= mask[port];
726 writew(dc->last_ier, dc->reg_ier);
727 } else {
728 dev_err(&dc->pdev->dev, "Called with wrong port?\n");
729 }
730}
731
732/* Disable downlink interrupts */
733static void disable_transmit_dl(enum port_type port, struct nozomi *dc)
734{
Jan Engelhardt71e1b4a2008-02-01 09:13:53 +0100735 static const u16 mask[] =
736 {~MDM_DL, ~DIAG_DL, ~APP1_DL, ~APP2_DL, ~CTRL_DL};
Frank Seidel20fd1e32007-11-09 14:49:23 +0100737
738 if (port < NOZOMI_MAX_PORTS) {
739 dc->last_ier &= mask[port];
740 writew(dc->last_ier, dc->reg_ier);
741 } else {
742 dev_err(&dc->pdev->dev, "Called with wrong port?\n");
743 }
744}
745
746/*
747 * Return 1 - send buffer to card and ack.
748 * Return 0 - don't ack, don't send buffer to card.
749 */
Alan Cox33dd4742009-01-02 13:47:32 +0000750static int send_data(enum port_type index, struct nozomi *dc)
Frank Seidel20fd1e32007-11-09 14:49:23 +0100751{
752 u32 size = 0;
Alan Cox33dd4742009-01-02 13:47:32 +0000753 struct port *port = &dc->port[index];
Frank Seidel18bbe0c22008-02-01 09:14:05 +0100754 const u8 toggle = port->toggle_ul;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100755 void __iomem *addr = port->ul_addr[toggle];
Frank Seidel18bbe0c22008-02-01 09:14:05 +0100756 const u32 ul_size = port->ul_size[toggle];
Frank Seidel20fd1e32007-11-09 14:49:23 +0100757
758 /* Get data from tty and place in buf for now */
Stefani Seibold7acd72e2009-12-21 14:37:28 -0800759 size = kfifo_out(&port->fifo_ul, dc->send_buf,
Frank Seidel20fd1e32007-11-09 14:49:23 +0100760 ul_size < SEND_BUF_MAX ? ul_size : SEND_BUF_MAX);
761
762 if (size == 0) {
763 DBG4("No more data to send, disable link:");
764 return 0;
765 }
766
767 /* DUMP(buf, size); */
768
769 /* Write length + data */
770 write_mem32(addr, (u32 *) &size, 4);
771 write_mem32(addr + 4, (u32 *) dc->send_buf, size);
772
Jiri Slaby6aad04f2013-03-07 13:12:29 +0100773 tty_port_tty_wakeup(&port->port);
Frank Seidel20fd1e32007-11-09 14:49:23 +0100774
775 return 1;
776}
777
778/* If all data has been read, return 1, else 0 */
779static int receive_data(enum port_type index, struct nozomi *dc)
780{
781 u8 buf[RECEIVE_BUF_MAX] = { 0 };
782 int size;
783 u32 offset = 4;
784 struct port *port = &dc->port[index];
785 void __iomem *addr = port->dl_addr[port->toggle_dl];
Alan Cox33dd4742009-01-02 13:47:32 +0000786 struct tty_struct *tty = tty_port_tty_get(&port->port);
Jiri Slaby9237a812009-07-16 16:06:18 +0100787 int i, ret;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100788
Arnd Bergmanna4f642a2016-01-25 22:54:56 +0100789 size = __le32_to_cpu(readl(addr));
Frank Seidel20fd1e32007-11-09 14:49:23 +0100790 /* DBG1( "%d bytes port: %d", size, index); */
791
Peter Hurley97ef38b2016-04-09 17:11:36 -0700792 if (tty && tty_throttled(tty)) {
Frank Seidel20fd1e32007-11-09 14:49:23 +0100793 DBG1("No room in tty, don't read data, don't ack interrupt, "
794 "disable interrupt");
795
796 /* disable interrupt in downlink... */
797 disable_transmit_dl(index, dc);
Jiri Slaby9237a812009-07-16 16:06:18 +0100798 ret = 0;
799 goto put;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100800 }
801
802 if (unlikely(size == 0)) {
803 dev_err(&dc->pdev->dev, "size == 0?\n");
Jiri Slaby9237a812009-07-16 16:06:18 +0100804 ret = 1;
805 goto put;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100806 }
807
Frank Seidel20fd1e32007-11-09 14:49:23 +0100808 while (size > 0) {
809 read_mem32((u32 *) buf, addr + offset, RECEIVE_BUF_MAX);
810
811 if (size == 1) {
Jiri Slaby92a19f92013-01-03 15:53:03 +0100812 tty_insert_flip_char(&port->port, buf[0], TTY_NORMAL);
Frank Seidel20fd1e32007-11-09 14:49:23 +0100813 size = 0;
814 } else if (size < RECEIVE_BUF_MAX) {
Jiri Slaby05c7cd32013-01-03 15:53:04 +0100815 size -= tty_insert_flip_string(&port->port,
816 (char *)buf, size);
Frank Seidel20fd1e32007-11-09 14:49:23 +0100817 } else {
Jiri Slaby05c7cd32013-01-03 15:53:04 +0100818 i = tty_insert_flip_string(&port->port,
819 (char *)buf, RECEIVE_BUF_MAX);
Frank Seidel20fd1e32007-11-09 14:49:23 +0100820 size -= i;
821 offset += i;
822 }
823 }
824
825 set_bit(index, &dc->flip);
Jiri Slaby9237a812009-07-16 16:06:18 +0100826 ret = 1;
827put:
Alan Cox33dd4742009-01-02 13:47:32 +0000828 tty_kref_put(tty);
Jiri Slaby9237a812009-07-16 16:06:18 +0100829 return ret;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100830}
831
832/* Debug for interrupts */
833#ifdef DEBUG
834static char *interrupt2str(u16 interrupt)
835{
836 static char buf[TMP_BUF_MAX];
837 char *p = buf;
838
Takashi Iwaicaa47cc2020-03-11 10:29:05 +0100839 if (interrupt & MDM_DL1)
840 p += scnprintf(p, TMP_BUF_MAX, "MDM_DL1 ");
841 if (interrupt & MDM_DL2)
842 p += scnprintf(p, TMP_BUF_MAX - (p - buf), "MDM_DL2 ");
843 if (interrupt & MDM_UL1)
844 p += scnprintf(p, TMP_BUF_MAX - (p - buf), "MDM_UL1 ");
845 if (interrupt & MDM_UL2)
846 p += scnprintf(p, TMP_BUF_MAX - (p - buf), "MDM_UL2 ");
847 if (interrupt & DIAG_DL1)
848 p += scnprintf(p, TMP_BUF_MAX - (p - buf), "DIAG_DL1 ");
849 if (interrupt & DIAG_DL2)
850 p += scnprintf(p, TMP_BUF_MAX - (p - buf), "DIAG_DL2 ");
Frank Seidel20fd1e32007-11-09 14:49:23 +0100851
Takashi Iwaicaa47cc2020-03-11 10:29:05 +0100852 if (interrupt & DIAG_UL)
853 p += scnprintf(p, TMP_BUF_MAX - (p - buf), "DIAG_UL ");
Frank Seidel20fd1e32007-11-09 14:49:23 +0100854
Takashi Iwaicaa47cc2020-03-11 10:29:05 +0100855 if (interrupt & APP1_DL)
856 p += scnprintf(p, TMP_BUF_MAX - (p - buf), "APP1_DL ");
857 if (interrupt & APP2_DL)
858 p += scnprintf(p, TMP_BUF_MAX - (p - buf), "APP2_DL ");
Frank Seidel20fd1e32007-11-09 14:49:23 +0100859
Takashi Iwaicaa47cc2020-03-11 10:29:05 +0100860 if (interrupt & APP1_UL)
861 p += scnprintf(p, TMP_BUF_MAX - (p - buf), "APP1_UL ");
862 if (interrupt & APP2_UL)
863 p += scnprintf(p, TMP_BUF_MAX - (p - buf), "APP2_UL ");
Frank Seidel20fd1e32007-11-09 14:49:23 +0100864
Takashi Iwaicaa47cc2020-03-11 10:29:05 +0100865 if (interrupt & CTRL_DL)
866 p += scnprintf(p, TMP_BUF_MAX - (p - buf), "CTRL_DL ");
867 if (interrupt & CTRL_UL)
868 p += scnprintf(p, TMP_BUF_MAX - (p - buf), "CTRL_UL ");
Frank Seidel20fd1e32007-11-09 14:49:23 +0100869
Takashi Iwaicaa47cc2020-03-11 10:29:05 +0100870 if (interrupt & RESET)
871 p += scnprintf(p, TMP_BUF_MAX - (p - buf), "RESET ");
Frank Seidel20fd1e32007-11-09 14:49:23 +0100872
873 return buf;
874}
875#endif
876
877/*
878 * Receive flow control
879 * Return 1 - If ok, else 0
880 */
881static int receive_flow_control(struct nozomi *dc)
882{
883 enum port_type port = PORT_MDM;
884 struct ctrl_dl ctrl_dl;
885 struct ctrl_dl old_ctrl;
886 u16 enable_ier = 0;
887
888 read_mem32((u32 *) &ctrl_dl, dc->port[PORT_CTRL].dl_addr[CH_A], 2);
889
890 switch (ctrl_dl.port) {
891 case CTRL_CMD:
892 DBG1("The Base Band sends this value as a response to a "
893 "request for IMSI detach sent over the control "
894 "channel uplink (see section 7.6.1).");
895 break;
896 case CTRL_MDM:
897 port = PORT_MDM;
898 enable_ier = MDM_DL;
899 break;
900 case CTRL_DIAG:
901 port = PORT_DIAG;
902 enable_ier = DIAG_DL;
903 break;
904 case CTRL_APP1:
905 port = PORT_APP1;
906 enable_ier = APP1_DL;
907 break;
908 case CTRL_APP2:
909 port = PORT_APP2;
910 enable_ier = APP2_DL;
Frank Seidel661b4e82008-03-06 21:45:57 +0100911 if (dc->state == NOZOMI_STATE_ALLOCATED) {
912 /*
913 * After card initialization the flow control
914 * received for APP2 is always the last
915 */
916 dc->state = NOZOMI_STATE_READY;
917 dev_info(&dc->pdev->dev, "Device READY!\n");
918 }
Frank Seidel20fd1e32007-11-09 14:49:23 +0100919 break;
920 default:
921 dev_err(&dc->pdev->dev,
922 "ERROR: flow control received for non-existing port\n");
923 return 0;
Joe Perchesfc8114722013-10-08 16:14:21 -0700924 }
Frank Seidel20fd1e32007-11-09 14:49:23 +0100925
926 DBG1("0x%04X->0x%04X", *((u16 *)&dc->port[port].ctrl_dl),
927 *((u16 *)&ctrl_dl));
928
929 old_ctrl = dc->port[port].ctrl_dl;
930 dc->port[port].ctrl_dl = ctrl_dl;
931
932 if (old_ctrl.CTS == 1 && ctrl_dl.CTS == 0) {
933 DBG1("Disable interrupt (0x%04X) on port: %d",
934 enable_ier, port);
935 disable_transmit_ul(port, dc);
936
937 } else if (old_ctrl.CTS == 0 && ctrl_dl.CTS == 1) {
938
Stefani Seibolde64c0262009-12-21 14:37:28 -0800939 if (kfifo_len(&dc->port[port].fifo_ul)) {
Frank Seidel20fd1e32007-11-09 14:49:23 +0100940 DBG1("Enable interrupt (0x%04X) on port: %d",
941 enable_ier, port);
942 DBG1("Data in buffer [%d], enable transmit! ",
Stefani Seibolde64c0262009-12-21 14:37:28 -0800943 kfifo_len(&dc->port[port].fifo_ul));
Frank Seidel20fd1e32007-11-09 14:49:23 +0100944 enable_transmit_ul(port, dc);
945 } else {
946 DBG1("No data in buffer...");
947 }
948 }
949
950 if (*(u16 *)&old_ctrl == *(u16 *)&ctrl_dl) {
951 DBG1(" No change in mctrl");
952 return 1;
953 }
954 /* Update statistics */
955 if (old_ctrl.CTS != ctrl_dl.CTS)
956 dc->port[port].tty_icount.cts++;
957 if (old_ctrl.DSR != ctrl_dl.DSR)
958 dc->port[port].tty_icount.dsr++;
959 if (old_ctrl.RI != ctrl_dl.RI)
960 dc->port[port].tty_icount.rng++;
961 if (old_ctrl.DCD != ctrl_dl.DCD)
962 dc->port[port].tty_icount.dcd++;
963
964 wake_up_interruptible(&dc->port[port].tty_wait);
965
966 DBG1("port: %d DCD(%d), CTS(%d), RI(%d), DSR(%d)",
967 port,
968 dc->port[port].tty_icount.dcd, dc->port[port].tty_icount.cts,
969 dc->port[port].tty_icount.rng, dc->port[port].tty_icount.dsr);
970
971 return 1;
972}
973
974static enum ctrl_port_type port2ctrl(enum port_type port,
975 const struct nozomi *dc)
976{
977 switch (port) {
978 case PORT_MDM:
979 return CTRL_MDM;
980 case PORT_DIAG:
981 return CTRL_DIAG;
982 case PORT_APP1:
983 return CTRL_APP1;
984 case PORT_APP2:
985 return CTRL_APP2;
986 default:
987 dev_err(&dc->pdev->dev,
988 "ERROR: send flow control " \
989 "received for non-existing port\n");
Joe Perchesfc8114722013-10-08 16:14:21 -0700990 }
Frank Seidel20fd1e32007-11-09 14:49:23 +0100991 return CTRL_ERROR;
992}
993
994/*
995 * Send flow control, can only update one channel at a time
996 * Return 0 - If we have updated all flow control
997 * Return 1 - If we need to update more flow control, ack current enable more
998 */
999static int send_flow_control(struct nozomi *dc)
1000{
1001 u32 i, more_flow_control_to_be_updated = 0;
1002 u16 *ctrl;
1003
1004 for (i = PORT_MDM; i < MAX_PORT; i++) {
1005 if (dc->port[i].update_flow_control) {
1006 if (more_flow_control_to_be_updated) {
1007 /* We have more flow control to be updated */
1008 return 1;
1009 }
1010 dc->port[i].ctrl_ul.port = port2ctrl(i, dc);
1011 ctrl = (u16 *)&dc->port[i].ctrl_ul;
1012 write_mem32(dc->port[PORT_CTRL].ul_addr[0], \
1013 (u32 *) ctrl, 2);
1014 dc->port[i].update_flow_control = 0;
1015 more_flow_control_to_be_updated = 1;
1016 }
1017 }
1018 return 0;
1019}
1020
1021/*
Frank Seidele9ed5372008-01-25 21:13:24 +01001022 * Handle downlink data, ports that are handled are modem and diagnostics
Frank Seidel20fd1e32007-11-09 14:49:23 +01001023 * Return 1 - ok
1024 * Return 0 - toggle fields are out of sync
1025 */
1026static int handle_data_dl(struct nozomi *dc, enum port_type port, u8 *toggle,
1027 u16 read_iir, u16 mask1, u16 mask2)
1028{
1029 if (*toggle == 0 && read_iir & mask1) {
1030 if (receive_data(port, dc)) {
1031 writew(mask1, dc->reg_fcr);
1032 *toggle = !(*toggle);
1033 }
1034
1035 if (read_iir & mask2) {
1036 if (receive_data(port, dc)) {
1037 writew(mask2, dc->reg_fcr);
1038 *toggle = !(*toggle);
1039 }
1040 }
1041 } else if (*toggle == 1 && read_iir & mask2) {
1042 if (receive_data(port, dc)) {
1043 writew(mask2, dc->reg_fcr);
1044 *toggle = !(*toggle);
1045 }
1046
1047 if (read_iir & mask1) {
1048 if (receive_data(port, dc)) {
1049 writew(mask1, dc->reg_fcr);
1050 *toggle = !(*toggle);
1051 }
1052 }
1053 } else {
1054 dev_err(&dc->pdev->dev, "port out of sync!, toggle:%d\n",
1055 *toggle);
1056 return 0;
1057 }
1058 return 1;
1059}
1060
1061/*
1062 * Handle uplink data, this is currently for the modem port
1063 * Return 1 - ok
1064 * Return 0 - toggle field are out of sync
1065 */
1066static int handle_data_ul(struct nozomi *dc, enum port_type port, u16 read_iir)
1067{
1068 u8 *toggle = &(dc->port[port].toggle_ul);
1069
1070 if (*toggle == 0 && read_iir & MDM_UL1) {
1071 dc->last_ier &= ~MDM_UL;
1072 writew(dc->last_ier, dc->reg_ier);
1073 if (send_data(port, dc)) {
1074 writew(MDM_UL1, dc->reg_fcr);
1075 dc->last_ier = dc->last_ier | MDM_UL;
1076 writew(dc->last_ier, dc->reg_ier);
1077 *toggle = !*toggle;
1078 }
1079
1080 if (read_iir & MDM_UL2) {
1081 dc->last_ier &= ~MDM_UL;
1082 writew(dc->last_ier, dc->reg_ier);
1083 if (send_data(port, dc)) {
1084 writew(MDM_UL2, dc->reg_fcr);
1085 dc->last_ier = dc->last_ier | MDM_UL;
1086 writew(dc->last_ier, dc->reg_ier);
1087 *toggle = !*toggle;
1088 }
1089 }
1090
1091 } else if (*toggle == 1 && read_iir & MDM_UL2) {
1092 dc->last_ier &= ~MDM_UL;
1093 writew(dc->last_ier, dc->reg_ier);
1094 if (send_data(port, dc)) {
1095 writew(MDM_UL2, dc->reg_fcr);
1096 dc->last_ier = dc->last_ier | MDM_UL;
1097 writew(dc->last_ier, dc->reg_ier);
1098 *toggle = !*toggle;
1099 }
1100
1101 if (read_iir & MDM_UL1) {
1102 dc->last_ier &= ~MDM_UL;
1103 writew(dc->last_ier, dc->reg_ier);
1104 if (send_data(port, dc)) {
1105 writew(MDM_UL1, dc->reg_fcr);
1106 dc->last_ier = dc->last_ier | MDM_UL;
1107 writew(dc->last_ier, dc->reg_ier);
1108 *toggle = !*toggle;
1109 }
1110 }
1111 } else {
1112 writew(read_iir & MDM_UL, dc->reg_fcr);
1113 dev_err(&dc->pdev->dev, "port out of sync!\n");
1114 return 0;
1115 }
1116 return 1;
1117}
1118
1119static irqreturn_t interrupt_handler(int irq, void *dev_id)
1120{
1121 struct nozomi *dc = dev_id;
1122 unsigned int a;
1123 u16 read_iir;
1124
1125 if (!dc)
1126 return IRQ_NONE;
1127
1128 spin_lock(&dc->spin_mutex);
1129 read_iir = readw(dc->reg_iir);
1130
1131 /* Card removed */
1132 if (read_iir == (u16)-1)
1133 goto none;
1134 /*
1135 * Just handle interrupt enabled in IER
1136 * (by masking with dc->last_ier)
1137 */
1138 read_iir &= dc->last_ier;
1139
1140 if (read_iir == 0)
1141 goto none;
1142
1143
1144 DBG4("%s irq:0x%04X, prev:0x%04X", interrupt2str(read_iir), read_iir,
1145 dc->last_ier);
1146
1147 if (read_iir & RESET) {
1148 if (unlikely(!nozomi_read_config_table(dc))) {
1149 dc->last_ier = 0x0;
1150 writew(dc->last_ier, dc->reg_ier);
1151 dev_err(&dc->pdev->dev, "Could not read status from "
1152 "card, we should disable interface\n");
1153 } else {
1154 writew(RESET, dc->reg_fcr);
1155 }
1156 /* No more useful info if this was the reset interrupt. */
1157 goto exit_handler;
1158 }
1159 if (read_iir & CTRL_UL) {
1160 DBG1("CTRL_UL");
1161 dc->last_ier &= ~CTRL_UL;
1162 writew(dc->last_ier, dc->reg_ier);
1163 if (send_flow_control(dc)) {
1164 writew(CTRL_UL, dc->reg_fcr);
1165 dc->last_ier = dc->last_ier | CTRL_UL;
1166 writew(dc->last_ier, dc->reg_ier);
1167 }
1168 }
1169 if (read_iir & CTRL_DL) {
1170 receive_flow_control(dc);
1171 writew(CTRL_DL, dc->reg_fcr);
1172 }
1173 if (read_iir & MDM_DL) {
1174 if (!handle_data_dl(dc, PORT_MDM,
1175 &(dc->port[PORT_MDM].toggle_dl), read_iir,
1176 MDM_DL1, MDM_DL2)) {
1177 dev_err(&dc->pdev->dev, "MDM_DL out of sync!\n");
1178 goto exit_handler;
1179 }
1180 }
1181 if (read_iir & MDM_UL) {
1182 if (!handle_data_ul(dc, PORT_MDM, read_iir)) {
1183 dev_err(&dc->pdev->dev, "MDM_UL out of sync!\n");
1184 goto exit_handler;
1185 }
1186 }
1187 if (read_iir & DIAG_DL) {
1188 if (!handle_data_dl(dc, PORT_DIAG,
1189 &(dc->port[PORT_DIAG].toggle_dl), read_iir,
1190 DIAG_DL1, DIAG_DL2)) {
1191 dev_err(&dc->pdev->dev, "DIAG_DL out of sync!\n");
1192 goto exit_handler;
1193 }
1194 }
1195 if (read_iir & DIAG_UL) {
1196 dc->last_ier &= ~DIAG_UL;
1197 writew(dc->last_ier, dc->reg_ier);
1198 if (send_data(PORT_DIAG, dc)) {
1199 writew(DIAG_UL, dc->reg_fcr);
1200 dc->last_ier = dc->last_ier | DIAG_UL;
1201 writew(dc->last_ier, dc->reg_ier);
1202 }
1203 }
1204 if (read_iir & APP1_DL) {
1205 if (receive_data(PORT_APP1, dc))
1206 writew(APP1_DL, dc->reg_fcr);
1207 }
1208 if (read_iir & APP1_UL) {
1209 dc->last_ier &= ~APP1_UL;
1210 writew(dc->last_ier, dc->reg_ier);
1211 if (send_data(PORT_APP1, dc)) {
1212 writew(APP1_UL, dc->reg_fcr);
1213 dc->last_ier = dc->last_ier | APP1_UL;
1214 writew(dc->last_ier, dc->reg_ier);
1215 }
1216 }
1217 if (read_iir & APP2_DL) {
1218 if (receive_data(PORT_APP2, dc))
1219 writew(APP2_DL, dc->reg_fcr);
1220 }
1221 if (read_iir & APP2_UL) {
1222 dc->last_ier &= ~APP2_UL;
1223 writew(dc->last_ier, dc->reg_ier);
1224 if (send_data(PORT_APP2, dc)) {
1225 writew(APP2_UL, dc->reg_fcr);
1226 dc->last_ier = dc->last_ier | APP2_UL;
1227 writew(dc->last_ier, dc->reg_ier);
1228 }
1229 }
1230
1231exit_handler:
1232 spin_unlock(&dc->spin_mutex);
Jiri Slaby2e124b42013-01-03 15:53:06 +01001233
1234 for (a = 0; a < NOZOMI_MAX_PORTS; a++)
1235 if (test_and_clear_bit(a, &dc->flip))
1236 tty_flip_buffer_push(&dc->port[a].port);
1237
Frank Seidel20fd1e32007-11-09 14:49:23 +01001238 return IRQ_HANDLED;
1239none:
1240 spin_unlock(&dc->spin_mutex);
1241 return IRQ_NONE;
1242}
1243
1244static void nozomi_get_card_type(struct nozomi *dc)
1245{
1246 int i;
1247 u32 size = 0;
1248
1249 for (i = 0; i < 6; i++)
1250 size += pci_resource_len(dc->pdev, i);
1251
1252 /* Assume card type F32_8 if no match */
1253 dc->card_type = size == 2048 ? F32_2 : F32_8;
1254
1255 dev_info(&dc->pdev->dev, "Card type is: %d\n", dc->card_type);
1256}
1257
1258static void nozomi_setup_private_data(struct nozomi *dc)
1259{
1260 void __iomem *offset = dc->base_addr + dc->card_type / 2;
1261 unsigned int i;
1262
1263 dc->reg_fcr = (void __iomem *)(offset + R_FCR);
1264 dc->reg_iir = (void __iomem *)(offset + R_IIR);
1265 dc->reg_ier = (void __iomem *)(offset + R_IER);
1266 dc->last_ier = 0;
1267 dc->flip = 0;
1268
1269 dc->port[PORT_MDM].token_dl = MDM_DL;
1270 dc->port[PORT_DIAG].token_dl = DIAG_DL;
1271 dc->port[PORT_APP1].token_dl = APP1_DL;
1272 dc->port[PORT_APP2].token_dl = APP2_DL;
1273
1274 for (i = 0; i < MAX_PORT; i++)
1275 init_waitqueue_head(&dc->port[i].tty_wait);
1276}
1277
1278static ssize_t card_type_show(struct device *dev, struct device_attribute *attr,
1279 char *buf)
1280{
Chuhong Yuan18b13452019-07-24 21:18:25 +08001281 const struct nozomi *dc = dev_get_drvdata(dev);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001282
1283 return sprintf(buf, "%d\n", dc->card_type);
1284}
Julia Lawall19467fa2016-10-29 21:37:08 +02001285static DEVICE_ATTR_RO(card_type);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001286
1287static ssize_t open_ttys_show(struct device *dev, struct device_attribute *attr,
1288 char *buf)
1289{
Chuhong Yuan18b13452019-07-24 21:18:25 +08001290 const struct nozomi *dc = dev_get_drvdata(dev);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001291
1292 return sprintf(buf, "%u\n", dc->open_ttys);
1293}
Julia Lawall19467fa2016-10-29 21:37:08 +02001294static DEVICE_ATTR_RO(open_ttys);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001295
1296static void make_sysfs_files(struct nozomi *dc)
1297{
1298 if (device_create_file(&dc->pdev->dev, &dev_attr_card_type))
1299 dev_err(&dc->pdev->dev,
1300 "Could not create sysfs file for card_type\n");
1301 if (device_create_file(&dc->pdev->dev, &dev_attr_open_ttys))
1302 dev_err(&dc->pdev->dev,
1303 "Could not create sysfs file for open_ttys\n");
1304}
1305
1306static void remove_sysfs_files(struct nozomi *dc)
1307{
1308 device_remove_file(&dc->pdev->dev, &dev_attr_card_type);
1309 device_remove_file(&dc->pdev->dev, &dev_attr_open_ttys);
1310}
1311
1312/* Allocate memory for one device */
Bill Pemberton9671f092012-11-19 13:21:50 -05001313static int nozomi_card_init(struct pci_dev *pdev,
Frank Seidel20fd1e32007-11-09 14:49:23 +01001314 const struct pci_device_id *ent)
1315{
Frank Seidel20fd1e32007-11-09 14:49:23 +01001316 int ret;
1317 struct nozomi *dc = NULL;
1318 int ndev_idx;
1319 int i;
1320
1321 dev_dbg(&pdev->dev, "Init, new card found\n");
1322
1323 for (ndev_idx = 0; ndev_idx < ARRAY_SIZE(ndevs); ndev_idx++)
1324 if (!ndevs[ndev_idx])
1325 break;
1326
1327 if (ndev_idx >= ARRAY_SIZE(ndevs)) {
1328 dev_err(&pdev->dev, "no free tty range for this card left\n");
1329 ret = -EIO;
1330 goto err;
1331 }
1332
1333 dc = kzalloc(sizeof(struct nozomi), GFP_KERNEL);
1334 if (unlikely(!dc)) {
1335 dev_err(&pdev->dev, "Could not allocate memory\n");
1336 ret = -ENOMEM;
1337 goto err_free;
1338 }
1339
1340 dc->pdev = pdev;
1341
Frank Seidel20fd1e32007-11-09 14:49:23 +01001342 ret = pci_enable_device(dc->pdev);
1343 if (ret) {
1344 dev_err(&pdev->dev, "Failed to enable PCI Device\n");
1345 goto err_free;
1346 }
1347
Frank Seidel20fd1e32007-11-09 14:49:23 +01001348 ret = pci_request_regions(dc->pdev, NOZOMI_NAME);
1349 if (ret) {
1350 dev_err(&pdev->dev, "I/O address 0x%04x already in use\n",
1351 (int) /* nozomi_private.io_addr */ 0);
1352 goto err_disable_device;
1353 }
1354
Frank Seidel661b4e82008-03-06 21:45:57 +01001355 /* Find out what card type it is */
1356 nozomi_get_card_type(dc);
1357
Hugo Lefeuvrec392ed42019-02-10 18:12:12 +01001358 dc->base_addr = pci_iomap(dc->pdev, 0, dc->card_type);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001359 if (!dc->base_addr) {
1360 dev_err(&pdev->dev, "Unable to map card MMIO\n");
1361 ret = -ENODEV;
1362 goto err_rel_regs;
1363 }
1364
1365 dc->send_buf = kmalloc(SEND_BUF_MAX, GFP_KERNEL);
1366 if (!dc->send_buf) {
1367 dev_err(&pdev->dev, "Could not allocate send buffer?\n");
1368 ret = -ENOMEM;
1369 goto err_free_sbuf;
1370 }
1371
Stefani Seibold9842c382009-12-21 14:37:29 -08001372 for (i = PORT_MDM; i < MAX_PORT; i++) {
Jiri Slabyc29bd8d2011-04-20 10:43:12 +02001373 if (kfifo_alloc(&dc->port[i].fifo_ul, FIFO_BUFFER_SIZE_UL,
1374 GFP_KERNEL)) {
Stefani Seibold9842c382009-12-21 14:37:29 -08001375 dev_err(&pdev->dev,
1376 "Could not allocate kfifo buffer\n");
1377 ret = -ENOMEM;
1378 goto err_free_kfifo;
1379 }
1380 }
1381
Frank Seidel20fd1e32007-11-09 14:49:23 +01001382 spin_lock_init(&dc->spin_mutex);
1383
1384 nozomi_setup_private_data(dc);
1385
1386 /* Disable all interrupts */
1387 dc->last_ier = 0;
1388 writew(dc->last_ier, dc->reg_ier);
1389
1390 ret = request_irq(pdev->irq, &interrupt_handler, IRQF_SHARED,
1391 NOZOMI_NAME, dc);
1392 if (unlikely(ret)) {
1393 dev_err(&pdev->dev, "can't request irq %d\n", pdev->irq);
Stefani Seibold9842c382009-12-21 14:37:29 -08001394 goto err_free_kfifo;
Frank Seidel20fd1e32007-11-09 14:49:23 +01001395 }
1396
1397 DBG1("base_addr: %p", dc->base_addr);
1398
1399 make_sysfs_files(dc);
1400
1401 dc->index_start = ndev_idx * MAX_PORT;
1402 ndevs[ndev_idx] = dc;
1403
Frank Seidel661b4e82008-03-06 21:45:57 +01001404 pci_set_drvdata(pdev, dc);
1405
1406 /* Enable RESET interrupt */
1407 dc->last_ier = RESET;
1408 iowrite16(dc->last_ier, dc->reg_ier);
1409
1410 dc->state = NOZOMI_STATE_ENABLED;
1411
Frank Seidel20fd1e32007-11-09 14:49:23 +01001412 for (i = 0; i < MAX_PORT; i++) {
Stefani Seibold9842c382009-12-21 14:37:29 -08001413 struct device *tty_dev;
Alan Cox266794e2010-02-08 10:06:45 +00001414 struct port *port = &dc->port[i];
1415 port->dc = dc;
Alan Cox266794e2010-02-08 10:06:45 +00001416 tty_port_init(&port->port);
1417 port->port.ops = &noz_tty_port_ops;
Jiri Slaby734cc172012-08-07 21:47:47 +02001418 tty_dev = tty_port_register_device(&port->port, ntty_driver,
1419 dc->index_start + i, &pdev->dev);
Stefani Seibold9842c382009-12-21 14:37:29 -08001420
1421 if (IS_ERR(tty_dev)) {
1422 ret = PTR_ERR(tty_dev);
1423 dev_err(&pdev->dev, "Could not allocate tty?\n");
Jiri Slaby191c5f12012-11-15 09:49:56 +01001424 tty_port_destroy(&port->port);
Stefani Seibold9842c382009-12-21 14:37:29 -08001425 goto err_free_tty;
1426 }
Frank Seidel20fd1e32007-11-09 14:49:23 +01001427 }
Stefani Seibold9842c382009-12-21 14:37:29 -08001428
Frank Seidel20fd1e32007-11-09 14:49:23 +01001429 return 0;
1430
Stefani Seibold9842c382009-12-21 14:37:29 -08001431err_free_tty:
Jiri Slaby191c5f12012-11-15 09:49:56 +01001432 for (i = 0; i < MAX_PORT; ++i) {
1433 tty_unregister_device(ntty_driver, dc->index_start + i);
1434 tty_port_destroy(&dc->port[i].port);
1435 }
Stefani Seibold9842c382009-12-21 14:37:29 -08001436err_free_kfifo:
1437 for (i = 0; i < MAX_PORT; i++)
1438 kfifo_free(&dc->port[i].fifo_ul);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001439err_free_sbuf:
1440 kfree(dc->send_buf);
1441 iounmap(dc->base_addr);
1442err_rel_regs:
1443 pci_release_regions(pdev);
1444err_disable_device:
1445 pci_disable_device(pdev);
1446err_free:
1447 kfree(dc);
1448err:
1449 return ret;
1450}
1451
Bill Pembertonae8d8a12012-11-19 13:26:18 -05001452static void tty_exit(struct nozomi *dc)
Frank Seidel20fd1e32007-11-09 14:49:23 +01001453{
1454 unsigned int i;
1455
1456 DBG1(" ");
1457
Jiri Slabyaa27a092013-03-07 13:12:30 +01001458 for (i = 0; i < MAX_PORT; ++i)
1459 tty_port_tty_hangup(&dc->port[i].port, false);
1460
Alan Cox33dd4742009-01-02 13:47:32 +00001461 /* Racy below - surely should wait for scheduled work to be done or
1462 complete off a hangup method ? */
Frank Seidel20fd1e32007-11-09 14:49:23 +01001463 while (dc->open_ttys)
1464 msleep(1);
Jiri Slaby191c5f12012-11-15 09:49:56 +01001465 for (i = 0; i < MAX_PORT; ++i) {
1466 tty_unregister_device(ntty_driver, dc->index_start + i);
1467 tty_port_destroy(&dc->port[i].port);
1468 }
Frank Seidel20fd1e32007-11-09 14:49:23 +01001469}
1470
1471/* Deallocate memory for one device */
Bill Pembertonae8d8a12012-11-19 13:26:18 -05001472static void nozomi_card_exit(struct pci_dev *pdev)
Frank Seidel20fd1e32007-11-09 14:49:23 +01001473{
1474 int i;
1475 struct ctrl_ul ctrl;
1476 struct nozomi *dc = pci_get_drvdata(pdev);
1477
1478 /* Disable all interrupts */
1479 dc->last_ier = 0;
1480 writew(dc->last_ier, dc->reg_ier);
1481
1482 tty_exit(dc);
1483
1484 /* Send 0x0001, command card to resend the reset token. */
1485 /* This is to get the reset when the module is reloaded. */
1486 ctrl.port = 0x00;
1487 ctrl.reserved = 0;
1488 ctrl.RTS = 0;
1489 ctrl.DTR = 1;
1490 DBG1("sending flow control 0x%04X", *((u16 *)&ctrl));
1491
1492 /* Setup dc->reg addresses to we can use defines here */
1493 write_mem32(dc->port[PORT_CTRL].ul_addr[0], (u32 *)&ctrl, 2);
1494 writew(CTRL_UL, dc->reg_fcr); /* push the token to the card. */
1495
1496 remove_sysfs_files(dc);
1497
1498 free_irq(pdev->irq, dc);
1499
1500 for (i = 0; i < MAX_PORT; i++)
Stefani Seibold45465482009-12-21 14:37:26 -08001501 kfifo_free(&dc->port[i].fifo_ul);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001502
1503 kfree(dc->send_buf);
1504
1505 iounmap(dc->base_addr);
1506
1507 pci_release_regions(pdev);
1508
1509 pci_disable_device(pdev);
1510
1511 ndevs[dc->index_start / MAX_PORT] = NULL;
1512
1513 kfree(dc);
1514}
1515
1516static void set_rts(const struct tty_struct *tty, int rts)
1517{
1518 struct port *port = get_port_by_tty(tty);
1519
1520 port->ctrl_ul.RTS = rts;
1521 port->update_flow_control = 1;
1522 enable_transmit_ul(PORT_CTRL, get_dc_by_tty(tty));
1523}
1524
1525static void set_dtr(const struct tty_struct *tty, int dtr)
1526{
1527 struct port *port = get_port_by_tty(tty);
1528
1529 DBG1("SETTING DTR index: %d, dtr: %d", tty->index, dtr);
1530
1531 port->ctrl_ul.DTR = dtr;
1532 port->update_flow_control = 1;
1533 enable_transmit_ul(PORT_CTRL, get_dc_by_tty(tty));
1534}
1535
1536/*
1537 * ----------------------------------------------------------------------------
1538 * TTY code
1539 * ----------------------------------------------------------------------------
1540 */
1541
Alan Cox266794e2010-02-08 10:06:45 +00001542static int ntty_install(struct tty_driver *driver, struct tty_struct *tty)
Frank Seidel20fd1e32007-11-09 14:49:23 +01001543{
1544 struct port *port = get_port_by_tty(tty);
1545 struct nozomi *dc = get_dc_by_tty(tty);
Alan Cox266794e2010-02-08 10:06:45 +00001546 int ret;
Frank Seidel661b4e82008-03-06 21:45:57 +01001547 if (!port || !dc || dc->state != NOZOMI_STATE_READY)
Frank Seidel20fd1e32007-11-09 14:49:23 +01001548 return -ENODEV;
Jiri Slaby81f58352012-01-30 21:14:30 +01001549 ret = tty_standard_install(driver, tty);
1550 if (ret == 0)
Jiri Slabybf9c1fc2010-07-29 11:46:32 +02001551 tty->driver_data = port;
Alan Cox266794e2010-02-08 10:06:45 +00001552 return ret;
1553}
1554
1555static void ntty_cleanup(struct tty_struct *tty)
1556{
1557 tty->driver_data = NULL;
1558}
1559
1560static int ntty_activate(struct tty_port *tport, struct tty_struct *tty)
1561{
1562 struct port *port = container_of(tport, struct port, port);
1563 struct nozomi *dc = port->dc;
1564 unsigned long flags;
1565
1566 DBG1("open: %d", port->token_dl);
1567 spin_lock_irqsave(&dc->spin_mutex, flags);
1568 dc->last_ier = dc->last_ier | port->token_dl;
1569 writew(dc->last_ier, dc->reg_ier);
1570 dc->open_ttys++;
1571 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1572 printk("noz: activated %d: %p\n", tty->index, tport);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001573 return 0;
1574}
1575
Alan Cox266794e2010-02-08 10:06:45 +00001576static int ntty_open(struct tty_struct *tty, struct file *filp)
Frank Seidel20fd1e32007-11-09 14:49:23 +01001577{
Jiri Slabybf9c1fc2010-07-29 11:46:32 +02001578 struct port *port = tty->driver_data;
Alan Cox266794e2010-02-08 10:06:45 +00001579 return tty_port_open(&port->port, tty, filp);
1580}
1581
1582static void ntty_shutdown(struct tty_port *tport)
1583{
1584 struct port *port = container_of(tport, struct port, port);
1585 struct nozomi *dc = port->dc;
Frank Seidel20fd1e32007-11-09 14:49:23 +01001586 unsigned long flags;
1587
Alan Cox266794e2010-02-08 10:06:45 +00001588 DBG1("close: %d", port->token_dl);
1589 spin_lock_irqsave(&dc->spin_mutex, flags);
1590 dc->last_ier &= ~(port->token_dl);
1591 writew(dc->last_ier, dc->reg_ier);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001592 dc->open_ttys--;
Alan Cox266794e2010-02-08 10:06:45 +00001593 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1594 printk("noz: shutdown %p\n", tport);
1595}
Frank Seidel20fd1e32007-11-09 14:49:23 +01001596
Alan Cox266794e2010-02-08 10:06:45 +00001597static void ntty_close(struct tty_struct *tty, struct file *filp)
1598{
1599 struct port *port = tty->driver_data;
1600 if (port)
1601 tty_port_close(&port->port, tty, filp);
1602}
1603
1604static void ntty_hangup(struct tty_struct *tty)
1605{
1606 struct port *port = tty->driver_data;
1607 tty_port_hangup(&port->port);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001608}
1609
1610/*
1611 * called when the userspace process writes to the tty (/dev/noz*).
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001612 * Data is inserted into a fifo, which is then read and transferred to the modem.
Frank Seidel20fd1e32007-11-09 14:49:23 +01001613 */
1614static int ntty_write(struct tty_struct *tty, const unsigned char *buffer,
1615 int count)
1616{
1617 int rval = -EINVAL;
1618 struct nozomi *dc = get_dc_by_tty(tty);
1619 struct port *port = tty->driver_data;
1620 unsigned long flags;
1621
1622 /* DBG1( "WRITEx: %d, index = %d", count, index); */
1623
1624 if (!dc || !port)
1625 return -ENODEV;
1626
Stefani Seibold7acd72e2009-12-21 14:37:28 -08001627 rval = kfifo_in(&port->fifo_ul, (unsigned char *)buffer, count);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001628
Frank Seidel20fd1e32007-11-09 14:49:23 +01001629 spin_lock_irqsave(&dc->spin_mutex, flags);
1630 /* CTS is only valid on the modem channel */
1631 if (port == &(dc->port[PORT_MDM])) {
1632 if (port->ctrl_dl.CTS) {
1633 DBG4("Enable interrupt");
1634 enable_transmit_ul(tty->index % MAX_PORT, dc);
1635 } else {
1636 dev_err(&dc->pdev->dev,
1637 "CTS not active on modem port?\n");
1638 }
1639 } else {
1640 enable_transmit_ul(tty->index % MAX_PORT, dc);
1641 }
1642 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1643
Frank Seidel20fd1e32007-11-09 14:49:23 +01001644 return rval;
1645}
1646
1647/*
1648 * Calculate how much is left in device
1649 * This method is called by the upper tty layer.
1650 * #according to sources N_TTY.c it expects a value >= 0 and
1651 * does not check for negative values.
Alan Coxe8c65d12010-02-08 10:07:04 +00001652 *
1653 * If the port is unplugged report lots of room and let the bits
1654 * dribble away so we don't block anything.
Frank Seidel20fd1e32007-11-09 14:49:23 +01001655 */
1656static int ntty_write_room(struct tty_struct *tty)
1657{
1658 struct port *port = tty->driver_data;
Alan Coxe8c65d12010-02-08 10:07:04 +00001659 int room = 4096;
Jan Engelhardt71e1b4a2008-02-01 09:13:53 +01001660 const struct nozomi *dc = get_dc_by_tty(tty);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001661
Jiri Slaby7fdc2892011-04-20 10:43:14 +02001662 if (dc)
Jiri Slaby6d742f62011-04-20 10:43:13 +02001663 room = kfifo_avail(&port->fifo_ul);
Jiri Slaby7fdc2892011-04-20 10:43:14 +02001664
Frank Seidel20fd1e32007-11-09 14:49:23 +01001665 return room;
1666}
1667
1668/* Gets io control parameters */
Alan Cox60b33c12011-02-14 16:26:14 +00001669static int ntty_tiocmget(struct tty_struct *tty)
Frank Seidel20fd1e32007-11-09 14:49:23 +01001670{
Jan Engelhardt71e1b4a2008-02-01 09:13:53 +01001671 const struct port *port = tty->driver_data;
1672 const struct ctrl_dl *ctrl_dl = &port->ctrl_dl;
1673 const struct ctrl_ul *ctrl_ul = &port->ctrl_ul;
Frank Seidel20fd1e32007-11-09 14:49:23 +01001674
Alan Cox978e5952008-04-30 00:53:59 -07001675 /* Note: these could change under us but it is not clear this
1676 matters if so */
Joey Pabalinasc6964e92018-04-24 19:48:04 -10001677 return (ctrl_ul->RTS ? TIOCM_RTS : 0)
1678 | (ctrl_ul->DTR ? TIOCM_DTR : 0)
1679 | (ctrl_dl->DCD ? TIOCM_CAR : 0)
1680 | (ctrl_dl->RI ? TIOCM_RNG : 0)
1681 | (ctrl_dl->DSR ? TIOCM_DSR : 0)
1682 | (ctrl_dl->CTS ? TIOCM_CTS : 0);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001683}
1684
1685/* Sets io controls parameters */
Alan Cox20b9d172011-02-14 16:26:50 +00001686static int ntty_tiocmset(struct tty_struct *tty,
1687 unsigned int set, unsigned int clear)
Frank Seidel20fd1e32007-11-09 14:49:23 +01001688{
Frank Seidel661b4e82008-03-06 21:45:57 +01001689 struct nozomi *dc = get_dc_by_tty(tty);
1690 unsigned long flags;
1691
1692 spin_lock_irqsave(&dc->spin_mutex, flags);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001693 if (set & TIOCM_RTS)
1694 set_rts(tty, 1);
1695 else if (clear & TIOCM_RTS)
1696 set_rts(tty, 0);
1697
1698 if (set & TIOCM_DTR)
1699 set_dtr(tty, 1);
1700 else if (clear & TIOCM_DTR)
1701 set_dtr(tty, 0);
Frank Seidel661b4e82008-03-06 21:45:57 +01001702 spin_unlock_irqrestore(&dc->spin_mutex, flags);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001703
1704 return 0;
1705}
1706
1707static int ntty_cflags_changed(struct port *port, unsigned long flags,
1708 struct async_icount *cprev)
1709{
Frank Seidel18bbe0c22008-02-01 09:14:05 +01001710 const struct async_icount cnow = port->tty_icount;
Frank Seidel20fd1e32007-11-09 14:49:23 +01001711 int ret;
1712
Joey Pabalinasc6964e92018-04-24 19:48:04 -10001713 ret = ((flags & TIOCM_RNG) && (cnow.rng != cprev->rng))
1714 || ((flags & TIOCM_DSR) && (cnow.dsr != cprev->dsr))
1715 || ((flags & TIOCM_CD) && (cnow.dcd != cprev->dcd))
1716 || ((flags & TIOCM_CTS) && (cnow.cts != cprev->cts));
Frank Seidel20fd1e32007-11-09 14:49:23 +01001717
1718 *cprev = cnow;
1719
1720 return ret;
1721}
1722
Alan Cox05871022010-09-16 18:21:52 +01001723static int ntty_tiocgicount(struct tty_struct *tty,
1724 struct serial_icounter_struct *icount)
Frank Seidel20fd1e32007-11-09 14:49:23 +01001725{
Alan Cox05871022010-09-16 18:21:52 +01001726 struct port *port = tty->driver_data;
Frank Seidel18bbe0c22008-02-01 09:14:05 +01001727 const struct async_icount cnow = port->tty_icount;
Frank Seidel20fd1e32007-11-09 14:49:23 +01001728
Alan Cox05871022010-09-16 18:21:52 +01001729 icount->cts = cnow.cts;
1730 icount->dsr = cnow.dsr;
1731 icount->rng = cnow.rng;
1732 icount->dcd = cnow.dcd;
1733 icount->rx = cnow.rx;
1734 icount->tx = cnow.tx;
1735 icount->frame = cnow.frame;
1736 icount->overrun = cnow.overrun;
1737 icount->parity = cnow.parity;
1738 icount->brk = cnow.brk;
1739 icount->buf_overrun = cnow.buf_overrun;
1740 return 0;
Frank Seidel20fd1e32007-11-09 14:49:23 +01001741}
1742
Alan Cox6caa76b2011-02-14 16:27:22 +00001743static int ntty_ioctl(struct tty_struct *tty,
Frank Seidel20fd1e32007-11-09 14:49:23 +01001744 unsigned int cmd, unsigned long arg)
1745{
1746 struct port *port = tty->driver_data;
Frank Seidel20fd1e32007-11-09 14:49:23 +01001747 int rval = -ENOIOCTLCMD;
1748
1749 DBG1("******** IOCTL, cmd: %d", cmd);
1750
1751 switch (cmd) {
1752 case TIOCMIWAIT: {
1753 struct async_icount cprev = port->tty_icount;
1754
1755 rval = wait_event_interruptible(port->tty_wait,
1756 ntty_cflags_changed(port, arg, &cprev));
1757 break;
Alan Cox05871022010-09-16 18:21:52 +01001758 }
Frank Seidel20fd1e32007-11-09 14:49:23 +01001759 default:
1760 DBG1("ERR: 0x%08X, %d", cmd, cmd);
1761 break;
Joe Perchesfc8114722013-10-08 16:14:21 -07001762 }
Frank Seidel20fd1e32007-11-09 14:49:23 +01001763
1764 return rval;
1765}
1766
1767/*
1768 * Called by the upper tty layer when tty buffers are ready
1769 * to receive data again after a call to throttle.
1770 */
1771static void ntty_unthrottle(struct tty_struct *tty)
1772{
1773 struct nozomi *dc = get_dc_by_tty(tty);
1774 unsigned long flags;
1775
1776 DBG1("UNTHROTTLE");
1777 spin_lock_irqsave(&dc->spin_mutex, flags);
1778 enable_transmit_dl(tty->index % MAX_PORT, dc);
1779 set_rts(tty, 1);
1780
1781 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1782}
1783
1784/*
1785 * Called by the upper tty layer when the tty buffers are almost full.
1786 * The driver should stop send more data.
1787 */
1788static void ntty_throttle(struct tty_struct *tty)
1789{
1790 struct nozomi *dc = get_dc_by_tty(tty);
1791 unsigned long flags;
1792
1793 DBG1("THROTTLE");
1794 spin_lock_irqsave(&dc->spin_mutex, flags);
1795 set_rts(tty, 0);
1796 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1797}
1798
Frank Seidel20fd1e32007-11-09 14:49:23 +01001799/* Returns number of chars in buffer, called by tty layer */
1800static s32 ntty_chars_in_buffer(struct tty_struct *tty)
1801{
1802 struct port *port = tty->driver_data;
1803 struct nozomi *dc = get_dc_by_tty(tty);
Alan Cox23198fd2009-07-20 16:05:27 +01001804 s32 rval = 0;
Frank Seidel20fd1e32007-11-09 14:49:23 +01001805
1806 if (unlikely(!dc || !port)) {
Frank Seidel20fd1e32007-11-09 14:49:23 +01001807 goto exit_in_buffer;
1808 }
1809
Stefani Seibolde64c0262009-12-21 14:37:28 -08001810 rval = kfifo_len(&port->fifo_ul);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001811
1812exit_in_buffer:
1813 return rval;
1814}
1815
Alan Cox266794e2010-02-08 10:06:45 +00001816static const struct tty_port_operations noz_tty_port_ops = {
1817 .activate = ntty_activate,
1818 .shutdown = ntty_shutdown,
1819};
1820
Frank Seidel18bbe0c22008-02-01 09:14:05 +01001821static const struct tty_operations tty_ops = {
Frank Seidel20fd1e32007-11-09 14:49:23 +01001822 .ioctl = ntty_ioctl,
1823 .open = ntty_open,
1824 .close = ntty_close,
Alan Cox266794e2010-02-08 10:06:45 +00001825 .hangup = ntty_hangup,
Frank Seidel20fd1e32007-11-09 14:49:23 +01001826 .write = ntty_write,
1827 .write_room = ntty_write_room,
1828 .unthrottle = ntty_unthrottle,
1829 .throttle = ntty_throttle,
1830 .chars_in_buffer = ntty_chars_in_buffer,
Frank Seidel20fd1e32007-11-09 14:49:23 +01001831 .tiocmget = ntty_tiocmget,
1832 .tiocmset = ntty_tiocmset,
Alan Cox05871022010-09-16 18:21:52 +01001833 .get_icount = ntty_tiocgicount,
Alan Cox266794e2010-02-08 10:06:45 +00001834 .install = ntty_install,
1835 .cleanup = ntty_cleanup,
Frank Seidel20fd1e32007-11-09 14:49:23 +01001836};
1837
1838/* Module initialization */
1839static struct pci_driver nozomi_driver = {
1840 .name = NOZOMI_NAME,
1841 .id_table = nozomi_pci_tbl,
1842 .probe = nozomi_card_init,
Bill Pemberton91116cb2012-11-19 13:21:06 -05001843 .remove = nozomi_card_exit,
Frank Seidel20fd1e32007-11-09 14:49:23 +01001844};
1845
1846static __init int nozomi_init(void)
1847{
1848 int ret;
1849
1850 printk(KERN_INFO "Initializing %s\n", VERSION_STRING);
1851
1852 ntty_driver = alloc_tty_driver(NTTY_TTY_MAXMINORS);
1853 if (!ntty_driver)
1854 return -ENOMEM;
1855
Frank Seidel20fd1e32007-11-09 14:49:23 +01001856 ntty_driver->driver_name = NOZOMI_NAME_TTY;
1857 ntty_driver->name = "noz";
1858 ntty_driver->major = 0;
1859 ntty_driver->type = TTY_DRIVER_TYPE_SERIAL;
1860 ntty_driver->subtype = SERIAL_TYPE_NORMAL;
1861 ntty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
1862 ntty_driver->init_termios = tty_std_termios;
1863 ntty_driver->init_termios.c_cflag = B115200 | CS8 | CREAD | \
1864 HUPCL | CLOCAL;
1865 ntty_driver->init_termios.c_ispeed = 115200;
1866 ntty_driver->init_termios.c_ospeed = 115200;
1867 tty_set_operations(ntty_driver, &tty_ops);
1868
1869 ret = tty_register_driver(ntty_driver);
1870 if (ret) {
1871 printk(KERN_ERR "Nozomi: failed to register ntty driver\n");
1872 goto free_tty;
1873 }
1874
1875 ret = pci_register_driver(&nozomi_driver);
1876 if (ret) {
1877 printk(KERN_ERR "Nozomi: can't register pci driver\n");
1878 goto unr_tty;
1879 }
1880
1881 return 0;
1882unr_tty:
1883 tty_unregister_driver(ntty_driver);
1884free_tty:
1885 put_tty_driver(ntty_driver);
1886 return ret;
1887}
1888
1889static __exit void nozomi_exit(void)
1890{
1891 printk(KERN_INFO "Unloading %s\n", DRIVER_DESC);
1892 pci_unregister_driver(&nozomi_driver);
1893 tty_unregister_driver(ntty_driver);
1894 put_tty_driver(ntty_driver);
1895}
1896
1897module_init(nozomi_init);
1898module_exit(nozomi_exit);
1899
Frank Seidel20fd1e32007-11-09 14:49:23 +01001900MODULE_LICENSE("Dual BSD/GPL");
1901MODULE_DESCRIPTION(DRIVER_DESC);