blob: a3f32a15fde4196ce889b02be3cb2ada16b16d12 [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>
58#include <asm/byteorder.h>
59
60#include <linux/delay.h>
61
62
63#define VERSION_STRING DRIVER_DESC " 2.1d (build date: " \
64 __DATE__ " " __TIME__ ")"
65
66/* Macros definitions */
67
68/* Default debug printout level */
69#define NOZOMI_DEBUG_LEVEL 0x00
70
71#define P_BUF_SIZE 128
72#define NFO(_err_flag_, args...) \
73do { \
74 char tmp[P_BUF_SIZE]; \
75 snprintf(tmp, sizeof(tmp), ##args); \
76 printk(_err_flag_ "[%d] %s(): %s\n", __LINE__, \
Harvey Harrisonbf9d8922008-04-30 00:55:10 -070077 __func__, tmp); \
Frank Seidel20fd1e32007-11-09 14:49:23 +010078} while (0)
79
80#define DBG1(args...) D_(0x01, ##args)
81#define DBG2(args...) D_(0x02, ##args)
82#define DBG3(args...) D_(0x04, ##args)
83#define DBG4(args...) D_(0x08, ##args)
84#define DBG5(args...) D_(0x10, ##args)
85#define DBG6(args...) D_(0x20, ##args)
86#define DBG7(args...) D_(0x40, ##args)
87#define DBG8(args...) D_(0x80, ##args)
88
89#ifdef DEBUG
90/* Do we need this settable at runtime? */
91static int debug = NOZOMI_DEBUG_LEVEL;
92
Frank Seidele9ed5372008-01-25 21:13:24 +010093#define D(lvl, args...) do \
94 {if (lvl & debug) NFO(KERN_DEBUG, ##args); } \
95 while (0)
Frank Seidel20fd1e32007-11-09 14:49:23 +010096#define D_(lvl, args...) D(lvl, ##args)
97
98/* These printouts are always printed */
99
100#else
101static int debug;
102#define D_(lvl, args...)
103#endif
104
105/* TODO: rewrite to optimize macros... */
106
107#define TMP_BUF_MAX 256
108
109#define DUMP(buf__,len__) \
110 do { \
111 char tbuf[TMP_BUF_MAX] = {0};\
112 if (len__ > 1) {\
113 snprintf(tbuf, len__ > TMP_BUF_MAX ? TMP_BUF_MAX : len__, "%s", buf__);\
114 if (tbuf[len__-2] == '\r') {\
115 tbuf[len__-2] = 'r';\
116 } \
117 DBG1("SENDING: '%s' (%d+n)", tbuf, len__);\
118 } else {\
119 DBG1("SENDING: '%s' (%d)", tbuf, len__);\
120 } \
121} while (0)
122
123/* Defines */
124#define NOZOMI_NAME "nozomi"
125#define NOZOMI_NAME_TTY "nozomi_tty"
126#define DRIVER_DESC "Nozomi driver"
127
128#define NTTY_TTY_MAXMINORS 256
129#define NTTY_FIFO_BUFFER_SIZE 8192
130
131/* Must be power of 2 */
132#define FIFO_BUFFER_SIZE_UL 8192
133
134/* Size of tmp send buffer to card */
135#define SEND_BUF_MAX 1024
136#define RECEIVE_BUF_MAX 4
137
138
Frank Seidel20fd1e32007-11-09 14:49:23 +0100139#define R_IIR 0x0000 /* Interrupt Identity Register */
140#define R_FCR 0x0000 /* Flow Control Register */
141#define R_IER 0x0004 /* Interrupt Enable Register */
142
143#define CONFIG_MAGIC 0xEFEFFEFE
144#define TOGGLE_VALID 0x0000
145
146/* Definition of interrupt tokens */
147#define MDM_DL1 0x0001
148#define MDM_UL1 0x0002
149#define MDM_DL2 0x0004
150#define MDM_UL2 0x0008
151#define DIAG_DL1 0x0010
152#define DIAG_DL2 0x0020
153#define DIAG_UL 0x0040
154#define APP1_DL 0x0080
155#define APP1_UL 0x0100
156#define APP2_DL 0x0200
157#define APP2_UL 0x0400
158#define CTRL_DL 0x0800
159#define CTRL_UL 0x1000
160#define RESET 0x8000
161
162#define MDM_DL (MDM_DL1 | MDM_DL2)
163#define MDM_UL (MDM_UL1 | MDM_UL2)
164#define DIAG_DL (DIAG_DL1 | DIAG_DL2)
165
166/* modem signal definition */
167#define CTRL_DSR 0x0001
168#define CTRL_DCD 0x0002
169#define CTRL_RI 0x0004
170#define CTRL_CTS 0x0008
171
172#define CTRL_DTR 0x0001
173#define CTRL_RTS 0x0002
174
175#define MAX_PORT 4
176#define NOZOMI_MAX_PORTS 5
177#define NOZOMI_MAX_CARDS (NTTY_TTY_MAXMINORS / MAX_PORT)
178
179/* Type definitions */
180
181/*
182 * There are two types of nozomi cards,
183 * one with 2048 memory and with 8192 memory
184 */
185enum card_type {
186 F32_2 = 2048, /* 512 bytes downlink + uplink * 2 -> 2048 */
187 F32_8 = 8192, /* 3072 bytes downl. + 1024 bytes uplink * 2 -> 8192 */
188};
189
Frank Seidel661b4e82008-03-06 21:45:57 +0100190/* Initialization states a card can be in */
191enum card_state {
192 NOZOMI_STATE_UKNOWN = 0,
193 NOZOMI_STATE_ENABLED = 1, /* pci device enabled */
194 NOZOMI_STATE_ALLOCATED = 2, /* config setup done */
195 NOZOMI_STATE_READY = 3, /* flowcontrols received */
196};
197
Frank Seidel20fd1e32007-11-09 14:49:23 +0100198/* Two different toggle channels exist */
199enum channel_type {
200 CH_A = 0,
201 CH_B = 1,
202};
203
204/* Port definition for the card regarding flow control */
205enum ctrl_port_type {
206 CTRL_CMD = 0,
207 CTRL_MDM = 1,
208 CTRL_DIAG = 2,
209 CTRL_APP1 = 3,
210 CTRL_APP2 = 4,
211 CTRL_ERROR = -1,
212};
213
214/* Ports that the nozomi has */
215enum port_type {
216 PORT_MDM = 0,
217 PORT_DIAG = 1,
218 PORT_APP1 = 2,
219 PORT_APP2 = 3,
220 PORT_CTRL = 4,
221 PORT_ERROR = -1,
222};
223
224#ifdef __BIG_ENDIAN
225/* Big endian */
226
227struct toggles {
Frank Seidele9ed5372008-01-25 21:13:24 +0100228 unsigned int enabled:5; /*
Frank Seidel20fd1e32007-11-09 14:49:23 +0100229 * Toggle fields are valid if enabled is 0,
230 * else A-channels must always be used.
231 */
Frank Seidele9ed5372008-01-25 21:13:24 +0100232 unsigned int diag_dl:1;
233 unsigned int mdm_dl:1;
234 unsigned int mdm_ul:1;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100235} __attribute__ ((packed));
236
237/* Configuration table to read at startup of card */
238/* Is for now only needed during initialization phase */
239struct config_table {
240 u32 signature;
241 u16 product_information;
242 u16 version;
243 u8 pad3[3];
244 struct toggles toggle;
245 u8 pad1[4];
246 u16 dl_mdm_len1; /*
247 * If this is 64, it can hold
248 * 60 bytes + 4 that is length field
249 */
250 u16 dl_start;
251
252 u16 dl_diag_len1;
253 u16 dl_mdm_len2; /*
254 * If this is 64, it can hold
255 * 60 bytes + 4 that is length field
256 */
257 u16 dl_app1_len;
258
259 u16 dl_diag_len2;
260 u16 dl_ctrl_len;
261 u16 dl_app2_len;
262 u8 pad2[16];
263 u16 ul_mdm_len1;
264 u16 ul_start;
265 u16 ul_diag_len;
266 u16 ul_mdm_len2;
267 u16 ul_app1_len;
268 u16 ul_app2_len;
269 u16 ul_ctrl_len;
270} __attribute__ ((packed));
271
272/* This stores all control downlink flags */
273struct ctrl_dl {
274 u8 port;
Frank Seidele9ed5372008-01-25 21:13:24 +0100275 unsigned int reserved:4;
276 unsigned int CTS:1;
277 unsigned int RI:1;
278 unsigned int DCD:1;
279 unsigned int DSR:1;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100280} __attribute__ ((packed));
281
282/* This stores all control uplink flags */
283struct ctrl_ul {
284 u8 port;
Frank Seidele9ed5372008-01-25 21:13:24 +0100285 unsigned int reserved:6;
286 unsigned int RTS:1;
287 unsigned int DTR:1;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100288} __attribute__ ((packed));
289
290#else
291/* Little endian */
292
293/* This represents the toggle information */
294struct toggles {
Frank Seidele9ed5372008-01-25 21:13:24 +0100295 unsigned int mdm_ul:1;
296 unsigned int mdm_dl:1;
297 unsigned int diag_dl:1;
298 unsigned int enabled:5; /*
Frank Seidel20fd1e32007-11-09 14:49:23 +0100299 * Toggle fields are valid if enabled is 0,
300 * else A-channels must always be used.
301 */
302} __attribute__ ((packed));
303
304/* Configuration table to read at startup of card */
305struct config_table {
306 u32 signature;
307 u16 version;
308 u16 product_information;
309 struct toggles toggle;
310 u8 pad1[7];
311 u16 dl_start;
312 u16 dl_mdm_len1; /*
313 * If this is 64, it can hold
314 * 60 bytes + 4 that is length field
315 */
316 u16 dl_mdm_len2;
317 u16 dl_diag_len1;
318 u16 dl_diag_len2;
319 u16 dl_app1_len;
320 u16 dl_app2_len;
321 u16 dl_ctrl_len;
322 u8 pad2[16];
323 u16 ul_start;
324 u16 ul_mdm_len2;
325 u16 ul_mdm_len1;
326 u16 ul_diag_len;
327 u16 ul_app1_len;
328 u16 ul_app2_len;
329 u16 ul_ctrl_len;
330} __attribute__ ((packed));
331
332/* This stores all control downlink flags */
333struct ctrl_dl {
Frank Seidele9ed5372008-01-25 21:13:24 +0100334 unsigned int DSR:1;
335 unsigned int DCD:1;
336 unsigned int RI:1;
337 unsigned int CTS:1;
338 unsigned int reserverd:4;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100339 u8 port;
340} __attribute__ ((packed));
341
342/* This stores all control uplink flags */
343struct ctrl_ul {
Frank Seidele9ed5372008-01-25 21:13:24 +0100344 unsigned int DTR:1;
345 unsigned int RTS:1;
346 unsigned int reserved:6;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100347 u8 port;
348} __attribute__ ((packed));
349#endif
350
351/* This holds all information that is needed regarding a port */
352struct port {
Alan Cox33dd4742009-01-02 13:47:32 +0000353 struct tty_port port;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100354 u8 update_flow_control;
355 struct ctrl_ul ctrl_ul;
356 struct ctrl_dl ctrl_dl;
Stefani Seibold45465482009-12-21 14:37:26 -0800357 struct kfifo fifo_ul;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100358 void __iomem *dl_addr[2];
359 u32 dl_size[2];
360 u8 toggle_dl;
361 void __iomem *ul_addr[2];
362 u32 ul_size[2];
363 u8 toggle_ul;
364 u16 token_dl;
365
Frank Seidel20fd1e32007-11-09 14:49:23 +0100366 /* mutex to ensure one access patch to this port */
367 struct mutex tty_sem;
368 wait_queue_head_t tty_wait;
369 struct async_icount tty_icount;
Alan Cox266794e2010-02-08 10:06:45 +0000370
371 struct nozomi *dc;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100372};
373
374/* Private data one for each card in the system */
375struct nozomi {
376 void __iomem *base_addr;
377 unsigned long flip;
378
379 /* Pointers to registers */
380 void __iomem *reg_iir;
381 void __iomem *reg_fcr;
382 void __iomem *reg_ier;
383
384 u16 last_ier;
385 enum card_type card_type;
386 struct config_table config_table; /* Configuration table */
387 struct pci_dev *pdev;
388 struct port port[NOZOMI_MAX_PORTS];
389 u8 *send_buf;
390
391 spinlock_t spin_mutex; /* secures access to registers and tty */
392
393 unsigned int index_start;
Frank Seidel661b4e82008-03-06 21:45:57 +0100394 enum card_state state;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100395 u32 open_ttys;
396};
397
398/* This is a data packet that is read or written to/from card */
399struct buffer {
400 u32 size; /* size is the length of the data buffer */
401 u8 *data;
402} __attribute__ ((packed));
403
404/* Global variables */
Frank Seidel18bbe0c22008-02-01 09:14:05 +0100405static const struct pci_device_id nozomi_pci_tbl[] __devinitconst = {
Alan Coxb2a3dbc2010-02-08 10:07:15 +0000406 {PCI_DEVICE(0x1931, 0x000c)}, /* Nozomi HSDPA */
Frank Seidel20fd1e32007-11-09 14:49:23 +0100407 {},
408};
409
410MODULE_DEVICE_TABLE(pci, nozomi_pci_tbl);
411
412static struct nozomi *ndevs[NOZOMI_MAX_CARDS];
413static struct tty_driver *ntty_driver;
414
Alan Cox266794e2010-02-08 10:06:45 +0000415static const struct tty_port_operations noz_tty_port_ops;
416
Frank Seidel20fd1e32007-11-09 14:49:23 +0100417/*
418 * find card by tty_index
419 */
420static inline struct nozomi *get_dc_by_tty(const struct tty_struct *tty)
421{
422 return tty ? ndevs[tty->index / MAX_PORT] : NULL;
423}
424
425static inline struct port *get_port_by_tty(const struct tty_struct *tty)
426{
427 struct nozomi *ndev = get_dc_by_tty(tty);
428 return ndev ? &ndev->port[tty->index % MAX_PORT] : NULL;
429}
430
431/*
432 * TODO:
433 * -Optimize
434 * -Rewrite cleaner
435 */
436
437static void read_mem32(u32 *buf, const void __iomem *mem_addr_start,
438 u32 size_bytes)
439{
440 u32 i = 0;
Al Viro782a6de2008-03-29 03:09:08 +0000441 const u32 __iomem *ptr = mem_addr_start;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100442 u16 *buf16;
443
444 if (unlikely(!ptr || !buf))
445 goto out;
446
447 /* shortcut for extremely often used cases */
448 switch (size_bytes) {
449 case 2: /* 2 bytes */
450 buf16 = (u16 *) buf;
Al Viro782a6de2008-03-29 03:09:08 +0000451 *buf16 = __le16_to_cpu(readw(ptr));
Frank Seidel20fd1e32007-11-09 14:49:23 +0100452 goto out;
453 break;
454 case 4: /* 4 bytes */
Al Viro782a6de2008-03-29 03:09:08 +0000455 *(buf) = __le32_to_cpu(readl(ptr));
Frank Seidel20fd1e32007-11-09 14:49:23 +0100456 goto out;
457 break;
458 }
459
460 while (i < size_bytes) {
461 if (size_bytes - i == 2) {
462 /* Handle 2 bytes in the end */
463 buf16 = (u16 *) buf;
Al Viro782a6de2008-03-29 03:09:08 +0000464 *(buf16) = __le16_to_cpu(readw(ptr));
Frank Seidel20fd1e32007-11-09 14:49:23 +0100465 i += 2;
466 } else {
467 /* Read 4 bytes */
Al Viro782a6de2008-03-29 03:09:08 +0000468 *(buf) = __le32_to_cpu(readl(ptr));
Frank Seidel20fd1e32007-11-09 14:49:23 +0100469 i += 4;
470 }
471 buf++;
472 ptr++;
473 }
474out:
475 return;
476}
477
478/*
479 * TODO:
480 * -Optimize
481 * -Rewrite cleaner
482 */
Jan Engelhardt71e1b4a2008-02-01 09:13:53 +0100483static u32 write_mem32(void __iomem *mem_addr_start, const u32 *buf,
Frank Seidel20fd1e32007-11-09 14:49:23 +0100484 u32 size_bytes)
485{
486 u32 i = 0;
Al Viro782a6de2008-03-29 03:09:08 +0000487 u32 __iomem *ptr = mem_addr_start;
Jan Engelhardt71e1b4a2008-02-01 09:13:53 +0100488 const u16 *buf16;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100489
490 if (unlikely(!ptr || !buf))
491 return 0;
492
493 /* shortcut for extremely often used cases */
494 switch (size_bytes) {
495 case 2: /* 2 bytes */
Jan Engelhardt71e1b4a2008-02-01 09:13:53 +0100496 buf16 = (const u16 *)buf;
Al Viro782a6de2008-03-29 03:09:08 +0000497 writew(__cpu_to_le16(*buf16), ptr);
Frank Seidel20fd1e32007-11-09 14:49:23 +0100498 return 2;
499 break;
500 case 1: /*
501 * also needs to write 4 bytes in this case
502 * so falling through..
503 */
504 case 4: /* 4 bytes */
Al Viro782a6de2008-03-29 03:09:08 +0000505 writel(__cpu_to_le32(*buf), ptr);
Frank Seidel20fd1e32007-11-09 14:49:23 +0100506 return 4;
507 break;
508 }
509
510 while (i < size_bytes) {
511 if (size_bytes - i == 2) {
512 /* 2 bytes */
Jan Engelhardt71e1b4a2008-02-01 09:13:53 +0100513 buf16 = (const u16 *)buf;
Al Viro782a6de2008-03-29 03:09:08 +0000514 writew(__cpu_to_le16(*buf16), ptr);
Frank Seidel20fd1e32007-11-09 14:49:23 +0100515 i += 2;
516 } else {
517 /* 4 bytes */
Al Viro782a6de2008-03-29 03:09:08 +0000518 writel(__cpu_to_le32(*buf), ptr);
Frank Seidel20fd1e32007-11-09 14:49:23 +0100519 i += 4;
520 }
521 buf++;
522 ptr++;
523 }
524 return i;
525}
526
527/* Setup pointers to different channels and also setup buffer sizes. */
528static void setup_memory(struct nozomi *dc)
529{
530 void __iomem *offset = dc->base_addr + dc->config_table.dl_start;
531 /* The length reported is including the length field of 4 bytes,
532 * hence subtract with 4.
533 */
534 const u16 buff_offset = 4;
535
536 /* Modem port dl configuration */
537 dc->port[PORT_MDM].dl_addr[CH_A] = offset;
538 dc->port[PORT_MDM].dl_addr[CH_B] =
539 (offset += dc->config_table.dl_mdm_len1);
540 dc->port[PORT_MDM].dl_size[CH_A] =
541 dc->config_table.dl_mdm_len1 - buff_offset;
542 dc->port[PORT_MDM].dl_size[CH_B] =
543 dc->config_table.dl_mdm_len2 - buff_offset;
544
545 /* Diag port dl configuration */
546 dc->port[PORT_DIAG].dl_addr[CH_A] =
547 (offset += dc->config_table.dl_mdm_len2);
548 dc->port[PORT_DIAG].dl_size[CH_A] =
549 dc->config_table.dl_diag_len1 - buff_offset;
550 dc->port[PORT_DIAG].dl_addr[CH_B] =
551 (offset += dc->config_table.dl_diag_len1);
552 dc->port[PORT_DIAG].dl_size[CH_B] =
553 dc->config_table.dl_diag_len2 - buff_offset;
554
555 /* App1 port dl configuration */
556 dc->port[PORT_APP1].dl_addr[CH_A] =
557 (offset += dc->config_table.dl_diag_len2);
558 dc->port[PORT_APP1].dl_size[CH_A] =
559 dc->config_table.dl_app1_len - buff_offset;
560
561 /* App2 port dl configuration */
562 dc->port[PORT_APP2].dl_addr[CH_A] =
563 (offset += dc->config_table.dl_app1_len);
564 dc->port[PORT_APP2].dl_size[CH_A] =
565 dc->config_table.dl_app2_len - buff_offset;
566
567 /* Ctrl dl configuration */
568 dc->port[PORT_CTRL].dl_addr[CH_A] =
569 (offset += dc->config_table.dl_app2_len);
570 dc->port[PORT_CTRL].dl_size[CH_A] =
571 dc->config_table.dl_ctrl_len - buff_offset;
572
573 offset = dc->base_addr + dc->config_table.ul_start;
574
575 /* Modem Port ul configuration */
576 dc->port[PORT_MDM].ul_addr[CH_A] = offset;
577 dc->port[PORT_MDM].ul_size[CH_A] =
578 dc->config_table.ul_mdm_len1 - buff_offset;
579 dc->port[PORT_MDM].ul_addr[CH_B] =
580 (offset += dc->config_table.ul_mdm_len1);
581 dc->port[PORT_MDM].ul_size[CH_B] =
582 dc->config_table.ul_mdm_len2 - buff_offset;
583
584 /* Diag port ul configuration */
585 dc->port[PORT_DIAG].ul_addr[CH_A] =
586 (offset += dc->config_table.ul_mdm_len2);
587 dc->port[PORT_DIAG].ul_size[CH_A] =
588 dc->config_table.ul_diag_len - buff_offset;
589
590 /* App1 port ul configuration */
591 dc->port[PORT_APP1].ul_addr[CH_A] =
592 (offset += dc->config_table.ul_diag_len);
593 dc->port[PORT_APP1].ul_size[CH_A] =
594 dc->config_table.ul_app1_len - buff_offset;
595
596 /* App2 port ul configuration */
597 dc->port[PORT_APP2].ul_addr[CH_A] =
598 (offset += dc->config_table.ul_app1_len);
599 dc->port[PORT_APP2].ul_size[CH_A] =
600 dc->config_table.ul_app2_len - buff_offset;
601
602 /* Ctrl ul configuration */
603 dc->port[PORT_CTRL].ul_addr[CH_A] =
604 (offset += dc->config_table.ul_app2_len);
605 dc->port[PORT_CTRL].ul_size[CH_A] =
606 dc->config_table.ul_ctrl_len - buff_offset;
607}
608
609/* Dump config table under initalization phase */
610#ifdef DEBUG
611static void dump_table(const struct nozomi *dc)
612{
613 DBG3("signature: 0x%08X", dc->config_table.signature);
614 DBG3("version: 0x%04X", dc->config_table.version);
615 DBG3("product_information: 0x%04X", \
616 dc->config_table.product_information);
617 DBG3("toggle enabled: %d", dc->config_table.toggle.enabled);
618 DBG3("toggle up_mdm: %d", dc->config_table.toggle.mdm_ul);
619 DBG3("toggle dl_mdm: %d", dc->config_table.toggle.mdm_dl);
620 DBG3("toggle dl_dbg: %d", dc->config_table.toggle.diag_dl);
621
622 DBG3("dl_start: 0x%04X", dc->config_table.dl_start);
623 DBG3("dl_mdm_len0: 0x%04X, %d", dc->config_table.dl_mdm_len1,
624 dc->config_table.dl_mdm_len1);
625 DBG3("dl_mdm_len1: 0x%04X, %d", dc->config_table.dl_mdm_len2,
626 dc->config_table.dl_mdm_len2);
627 DBG3("dl_diag_len0: 0x%04X, %d", dc->config_table.dl_diag_len1,
628 dc->config_table.dl_diag_len1);
629 DBG3("dl_diag_len1: 0x%04X, %d", dc->config_table.dl_diag_len2,
630 dc->config_table.dl_diag_len2);
631 DBG3("dl_app1_len: 0x%04X, %d", dc->config_table.dl_app1_len,
632 dc->config_table.dl_app1_len);
633 DBG3("dl_app2_len: 0x%04X, %d", dc->config_table.dl_app2_len,
634 dc->config_table.dl_app2_len);
635 DBG3("dl_ctrl_len: 0x%04X, %d", dc->config_table.dl_ctrl_len,
636 dc->config_table.dl_ctrl_len);
637 DBG3("ul_start: 0x%04X, %d", dc->config_table.ul_start,
638 dc->config_table.ul_start);
639 DBG3("ul_mdm_len[0]: 0x%04X, %d", dc->config_table.ul_mdm_len1,
640 dc->config_table.ul_mdm_len1);
641 DBG3("ul_mdm_len[1]: 0x%04X, %d", dc->config_table.ul_mdm_len2,
642 dc->config_table.ul_mdm_len2);
643 DBG3("ul_diag_len: 0x%04X, %d", dc->config_table.ul_diag_len,
644 dc->config_table.ul_diag_len);
645 DBG3("ul_app1_len: 0x%04X, %d", dc->config_table.ul_app1_len,
646 dc->config_table.ul_app1_len);
647 DBG3("ul_app2_len: 0x%04X, %d", dc->config_table.ul_app2_len,
648 dc->config_table.ul_app2_len);
649 DBG3("ul_ctrl_len: 0x%04X, %d", dc->config_table.ul_ctrl_len,
650 dc->config_table.ul_ctrl_len);
651}
652#else
Frank Seidele9ed5372008-01-25 21:13:24 +0100653static inline void dump_table(const struct nozomi *dc) { }
Frank Seidel20fd1e32007-11-09 14:49:23 +0100654#endif
655
656/*
657 * Read configuration table from card under intalization phase
658 * Returns 1 if ok, else 0
659 */
660static int nozomi_read_config_table(struct nozomi *dc)
661{
662 read_mem32((u32 *) &dc->config_table, dc->base_addr + 0,
663 sizeof(struct config_table));
664
665 if (dc->config_table.signature != CONFIG_MAGIC) {
666 dev_err(&dc->pdev->dev, "ConfigTable Bad! 0x%08X != 0x%08X\n",
667 dc->config_table.signature, CONFIG_MAGIC);
668 return 0;
669 }
670
671 if ((dc->config_table.version == 0)
672 || (dc->config_table.toggle.enabled == TOGGLE_VALID)) {
673 int i;
674 DBG1("Second phase, configuring card");
675
676 setup_memory(dc);
677
678 dc->port[PORT_MDM].toggle_ul = dc->config_table.toggle.mdm_ul;
679 dc->port[PORT_MDM].toggle_dl = dc->config_table.toggle.mdm_dl;
680 dc->port[PORT_DIAG].toggle_dl = dc->config_table.toggle.diag_dl;
681 DBG1("toggle ports: MDM UL:%d MDM DL:%d, DIAG DL:%d",
682 dc->port[PORT_MDM].toggle_ul,
683 dc->port[PORT_MDM].toggle_dl, dc->port[PORT_DIAG].toggle_dl);
684
685 dump_table(dc);
686
687 for (i = PORT_MDM; i < MAX_PORT; i++) {
Frank Seidel20fd1e32007-11-09 14:49:23 +0100688 memset(&dc->port[i].ctrl_dl, 0, sizeof(struct ctrl_dl));
689 memset(&dc->port[i].ctrl_ul, 0, sizeof(struct ctrl_ul));
690 }
691
692 /* Enable control channel */
693 dc->last_ier = dc->last_ier | CTRL_DL;
694 writew(dc->last_ier, dc->reg_ier);
695
Frank Seidel661b4e82008-03-06 21:45:57 +0100696 dc->state = NOZOMI_STATE_ALLOCATED;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100697 dev_info(&dc->pdev->dev, "Initialization OK!\n");
698 return 1;
699 }
700
701 if ((dc->config_table.version > 0)
702 && (dc->config_table.toggle.enabled != TOGGLE_VALID)) {
703 u32 offset = 0;
704 DBG1("First phase: pushing upload buffers, clearing download");
705
706 dev_info(&dc->pdev->dev, "Version of card: %d\n",
707 dc->config_table.version);
708
709 /* Here we should disable all I/O over F32. */
710 setup_memory(dc);
711
712 /*
713 * We should send ALL channel pair tokens back along
714 * with reset token
715 */
716
717 /* push upload modem buffers */
718 write_mem32(dc->port[PORT_MDM].ul_addr[CH_A],
719 (u32 *) &offset, 4);
720 write_mem32(dc->port[PORT_MDM].ul_addr[CH_B],
721 (u32 *) &offset, 4);
722
723 writew(MDM_UL | DIAG_DL | MDM_DL, dc->reg_fcr);
724
725 DBG1("First phase done");
726 }
727
728 return 1;
729}
730
731/* Enable uplink interrupts */
732static void enable_transmit_ul(enum port_type port, struct nozomi *dc)
733{
Jan Engelhardt71e1b4a2008-02-01 09:13:53 +0100734 static const u16 mask[] = {MDM_UL, DIAG_UL, APP1_UL, APP2_UL, CTRL_UL};
Frank Seidel20fd1e32007-11-09 14:49:23 +0100735
736 if (port < NOZOMI_MAX_PORTS) {
737 dc->last_ier |= mask[port];
738 writew(dc->last_ier, dc->reg_ier);
739 } else {
740 dev_err(&dc->pdev->dev, "Called with wrong port?\n");
741 }
742}
743
744/* Disable uplink interrupts */
745static void disable_transmit_ul(enum port_type port, struct nozomi *dc)
746{
Jan Engelhardt71e1b4a2008-02-01 09:13:53 +0100747 static const u16 mask[] =
748 {~MDM_UL, ~DIAG_UL, ~APP1_UL, ~APP2_UL, ~CTRL_UL};
Frank Seidel20fd1e32007-11-09 14:49:23 +0100749
750 if (port < NOZOMI_MAX_PORTS) {
751 dc->last_ier &= mask[port];
752 writew(dc->last_ier, dc->reg_ier);
753 } else {
754 dev_err(&dc->pdev->dev, "Called with wrong port?\n");
755 }
756}
757
758/* Enable downlink interrupts */
759static void enable_transmit_dl(enum port_type port, struct nozomi *dc)
760{
Jan Engelhardt71e1b4a2008-02-01 09:13:53 +0100761 static const u16 mask[] = {MDM_DL, DIAG_DL, APP1_DL, APP2_DL, CTRL_DL};
Frank Seidel20fd1e32007-11-09 14:49:23 +0100762
763 if (port < NOZOMI_MAX_PORTS) {
764 dc->last_ier |= mask[port];
765 writew(dc->last_ier, dc->reg_ier);
766 } else {
767 dev_err(&dc->pdev->dev, "Called with wrong port?\n");
768 }
769}
770
771/* Disable downlink interrupts */
772static void disable_transmit_dl(enum port_type port, struct nozomi *dc)
773{
Jan Engelhardt71e1b4a2008-02-01 09:13:53 +0100774 static const u16 mask[] =
775 {~MDM_DL, ~DIAG_DL, ~APP1_DL, ~APP2_DL, ~CTRL_DL};
Frank Seidel20fd1e32007-11-09 14:49:23 +0100776
777 if (port < NOZOMI_MAX_PORTS) {
778 dc->last_ier &= mask[port];
779 writew(dc->last_ier, dc->reg_ier);
780 } else {
781 dev_err(&dc->pdev->dev, "Called with wrong port?\n");
782 }
783}
784
785/*
786 * Return 1 - send buffer to card and ack.
787 * Return 0 - don't ack, don't send buffer to card.
788 */
Alan Cox33dd4742009-01-02 13:47:32 +0000789static int send_data(enum port_type index, struct nozomi *dc)
Frank Seidel20fd1e32007-11-09 14:49:23 +0100790{
791 u32 size = 0;
Alan Cox33dd4742009-01-02 13:47:32 +0000792 struct port *port = &dc->port[index];
Frank Seidel18bbe0c22008-02-01 09:14:05 +0100793 const u8 toggle = port->toggle_ul;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100794 void __iomem *addr = port->ul_addr[toggle];
Frank Seidel18bbe0c22008-02-01 09:14:05 +0100795 const u32 ul_size = port->ul_size[toggle];
Alan Cox33dd4742009-01-02 13:47:32 +0000796 struct tty_struct *tty = tty_port_tty_get(&port->port);
Frank Seidel20fd1e32007-11-09 14:49:23 +0100797
798 /* Get data from tty and place in buf for now */
Stefani Seibold7acd72e2009-12-21 14:37:28 -0800799 size = kfifo_out(&port->fifo_ul, dc->send_buf,
Frank Seidel20fd1e32007-11-09 14:49:23 +0100800 ul_size < SEND_BUF_MAX ? ul_size : SEND_BUF_MAX);
801
802 if (size == 0) {
803 DBG4("No more data to send, disable link:");
Alan Cox33dd4742009-01-02 13:47:32 +0000804 tty_kref_put(tty);
Frank Seidel20fd1e32007-11-09 14:49:23 +0100805 return 0;
806 }
807
808 /* DUMP(buf, size); */
809
810 /* Write length + data */
811 write_mem32(addr, (u32 *) &size, 4);
812 write_mem32(addr + 4, (u32 *) dc->send_buf, size);
813
814 if (tty)
815 tty_wakeup(tty);
816
Alan Cox33dd4742009-01-02 13:47:32 +0000817 tty_kref_put(tty);
Frank Seidel20fd1e32007-11-09 14:49:23 +0100818 return 1;
819}
820
821/* If all data has been read, return 1, else 0 */
822static int receive_data(enum port_type index, struct nozomi *dc)
823{
824 u8 buf[RECEIVE_BUF_MAX] = { 0 };
825 int size;
826 u32 offset = 4;
827 struct port *port = &dc->port[index];
828 void __iomem *addr = port->dl_addr[port->toggle_dl];
Alan Cox33dd4742009-01-02 13:47:32 +0000829 struct tty_struct *tty = tty_port_tty_get(&port->port);
Jiri Slaby9237a812009-07-16 16:06:18 +0100830 int i, ret;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100831
832 if (unlikely(!tty)) {
833 DBG1("tty not open for port: %d?", index);
834 return 1;
835 }
836
837 read_mem32((u32 *) &size, addr, 4);
838 /* DBG1( "%d bytes port: %d", size, index); */
839
840 if (test_bit(TTY_THROTTLED, &tty->flags)) {
841 DBG1("No room in tty, don't read data, don't ack interrupt, "
842 "disable interrupt");
843
844 /* disable interrupt in downlink... */
845 disable_transmit_dl(index, dc);
Jiri Slaby9237a812009-07-16 16:06:18 +0100846 ret = 0;
847 goto put;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100848 }
849
850 if (unlikely(size == 0)) {
851 dev_err(&dc->pdev->dev, "size == 0?\n");
Jiri Slaby9237a812009-07-16 16:06:18 +0100852 ret = 1;
853 goto put;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100854 }
855
Frank Seidel20fd1e32007-11-09 14:49:23 +0100856 while (size > 0) {
857 read_mem32((u32 *) buf, addr + offset, RECEIVE_BUF_MAX);
858
859 if (size == 1) {
860 tty_insert_flip_char(tty, buf[0], TTY_NORMAL);
861 size = 0;
862 } else if (size < RECEIVE_BUF_MAX) {
863 size -= tty_insert_flip_string(tty, (char *) buf, size);
864 } else {
865 i = tty_insert_flip_string(tty, \
866 (char *) buf, RECEIVE_BUF_MAX);
867 size -= i;
868 offset += i;
869 }
870 }
871
872 set_bit(index, &dc->flip);
Jiri Slaby9237a812009-07-16 16:06:18 +0100873 ret = 1;
874put:
Alan Cox33dd4742009-01-02 13:47:32 +0000875 tty_kref_put(tty);
Jiri Slaby9237a812009-07-16 16:06:18 +0100876 return ret;
Frank Seidel20fd1e32007-11-09 14:49:23 +0100877}
878
879/* Debug for interrupts */
880#ifdef DEBUG
881static char *interrupt2str(u16 interrupt)
882{
883 static char buf[TMP_BUF_MAX];
884 char *p = buf;
885
886 interrupt & MDM_DL1 ? p += snprintf(p, TMP_BUF_MAX, "MDM_DL1 ") : NULL;
887 interrupt & MDM_DL2 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
888 "MDM_DL2 ") : NULL;
889
890 interrupt & MDM_UL1 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
891 "MDM_UL1 ") : NULL;
892 interrupt & MDM_UL2 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
893 "MDM_UL2 ") : NULL;
894
895 interrupt & DIAG_DL1 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
896 "DIAG_DL1 ") : NULL;
897 interrupt & DIAG_DL2 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
898 "DIAG_DL2 ") : NULL;
899
900 interrupt & DIAG_UL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
901 "DIAG_UL ") : NULL;
902
903 interrupt & APP1_DL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
904 "APP1_DL ") : NULL;
905 interrupt & APP2_DL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
906 "APP2_DL ") : NULL;
907
908 interrupt & APP1_UL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
909 "APP1_UL ") : NULL;
910 interrupt & APP2_UL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
911 "APP2_UL ") : NULL;
912
913 interrupt & CTRL_DL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
914 "CTRL_DL ") : NULL;
915 interrupt & CTRL_UL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
916 "CTRL_UL ") : NULL;
917
918 interrupt & RESET ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
919 "RESET ") : NULL;
920
921 return buf;
922}
923#endif
924
925/*
926 * Receive flow control
927 * Return 1 - If ok, else 0
928 */
929static int receive_flow_control(struct nozomi *dc)
930{
931 enum port_type port = PORT_MDM;
932 struct ctrl_dl ctrl_dl;
933 struct ctrl_dl old_ctrl;
934 u16 enable_ier = 0;
935
936 read_mem32((u32 *) &ctrl_dl, dc->port[PORT_CTRL].dl_addr[CH_A], 2);
937
938 switch (ctrl_dl.port) {
939 case CTRL_CMD:
940 DBG1("The Base Band sends this value as a response to a "
941 "request for IMSI detach sent over the control "
942 "channel uplink (see section 7.6.1).");
943 break;
944 case CTRL_MDM:
945 port = PORT_MDM;
946 enable_ier = MDM_DL;
947 break;
948 case CTRL_DIAG:
949 port = PORT_DIAG;
950 enable_ier = DIAG_DL;
951 break;
952 case CTRL_APP1:
953 port = PORT_APP1;
954 enable_ier = APP1_DL;
955 break;
956 case CTRL_APP2:
957 port = PORT_APP2;
958 enable_ier = APP2_DL;
Frank Seidel661b4e82008-03-06 21:45:57 +0100959 if (dc->state == NOZOMI_STATE_ALLOCATED) {
960 /*
961 * After card initialization the flow control
962 * received for APP2 is always the last
963 */
964 dc->state = NOZOMI_STATE_READY;
965 dev_info(&dc->pdev->dev, "Device READY!\n");
966 }
Frank Seidel20fd1e32007-11-09 14:49:23 +0100967 break;
968 default:
969 dev_err(&dc->pdev->dev,
970 "ERROR: flow control received for non-existing port\n");
971 return 0;
972 };
973
974 DBG1("0x%04X->0x%04X", *((u16 *)&dc->port[port].ctrl_dl),
975 *((u16 *)&ctrl_dl));
976
977 old_ctrl = dc->port[port].ctrl_dl;
978 dc->port[port].ctrl_dl = ctrl_dl;
979
980 if (old_ctrl.CTS == 1 && ctrl_dl.CTS == 0) {
981 DBG1("Disable interrupt (0x%04X) on port: %d",
982 enable_ier, port);
983 disable_transmit_ul(port, dc);
984
985 } else if (old_ctrl.CTS == 0 && ctrl_dl.CTS == 1) {
986
Stefani Seibolde64c0262009-12-21 14:37:28 -0800987 if (kfifo_len(&dc->port[port].fifo_ul)) {
Frank Seidel20fd1e32007-11-09 14:49:23 +0100988 DBG1("Enable interrupt (0x%04X) on port: %d",
989 enable_ier, port);
990 DBG1("Data in buffer [%d], enable transmit! ",
Stefani Seibolde64c0262009-12-21 14:37:28 -0800991 kfifo_len(&dc->port[port].fifo_ul));
Frank Seidel20fd1e32007-11-09 14:49:23 +0100992 enable_transmit_ul(port, dc);
993 } else {
994 DBG1("No data in buffer...");
995 }
996 }
997
998 if (*(u16 *)&old_ctrl == *(u16 *)&ctrl_dl) {
999 DBG1(" No change in mctrl");
1000 return 1;
1001 }
1002 /* Update statistics */
1003 if (old_ctrl.CTS != ctrl_dl.CTS)
1004 dc->port[port].tty_icount.cts++;
1005 if (old_ctrl.DSR != ctrl_dl.DSR)
1006 dc->port[port].tty_icount.dsr++;
1007 if (old_ctrl.RI != ctrl_dl.RI)
1008 dc->port[port].tty_icount.rng++;
1009 if (old_ctrl.DCD != ctrl_dl.DCD)
1010 dc->port[port].tty_icount.dcd++;
1011
1012 wake_up_interruptible(&dc->port[port].tty_wait);
1013
1014 DBG1("port: %d DCD(%d), CTS(%d), RI(%d), DSR(%d)",
1015 port,
1016 dc->port[port].tty_icount.dcd, dc->port[port].tty_icount.cts,
1017 dc->port[port].tty_icount.rng, dc->port[port].tty_icount.dsr);
1018
1019 return 1;
1020}
1021
1022static enum ctrl_port_type port2ctrl(enum port_type port,
1023 const struct nozomi *dc)
1024{
1025 switch (port) {
1026 case PORT_MDM:
1027 return CTRL_MDM;
1028 case PORT_DIAG:
1029 return CTRL_DIAG;
1030 case PORT_APP1:
1031 return CTRL_APP1;
1032 case PORT_APP2:
1033 return CTRL_APP2;
1034 default:
1035 dev_err(&dc->pdev->dev,
1036 "ERROR: send flow control " \
1037 "received for non-existing port\n");
1038 };
1039 return CTRL_ERROR;
1040}
1041
1042/*
1043 * Send flow control, can only update one channel at a time
1044 * Return 0 - If we have updated all flow control
1045 * Return 1 - If we need to update more flow control, ack current enable more
1046 */
1047static int send_flow_control(struct nozomi *dc)
1048{
1049 u32 i, more_flow_control_to_be_updated = 0;
1050 u16 *ctrl;
1051
1052 for (i = PORT_MDM; i < MAX_PORT; i++) {
1053 if (dc->port[i].update_flow_control) {
1054 if (more_flow_control_to_be_updated) {
1055 /* We have more flow control to be updated */
1056 return 1;
1057 }
1058 dc->port[i].ctrl_ul.port = port2ctrl(i, dc);
1059 ctrl = (u16 *)&dc->port[i].ctrl_ul;
1060 write_mem32(dc->port[PORT_CTRL].ul_addr[0], \
1061 (u32 *) ctrl, 2);
1062 dc->port[i].update_flow_control = 0;
1063 more_flow_control_to_be_updated = 1;
1064 }
1065 }
1066 return 0;
1067}
1068
1069/*
Frank Seidele9ed5372008-01-25 21:13:24 +01001070 * Handle downlink data, ports that are handled are modem and diagnostics
Frank Seidel20fd1e32007-11-09 14:49:23 +01001071 * Return 1 - ok
1072 * Return 0 - toggle fields are out of sync
1073 */
1074static int handle_data_dl(struct nozomi *dc, enum port_type port, u8 *toggle,
1075 u16 read_iir, u16 mask1, u16 mask2)
1076{
1077 if (*toggle == 0 && read_iir & mask1) {
1078 if (receive_data(port, dc)) {
1079 writew(mask1, dc->reg_fcr);
1080 *toggle = !(*toggle);
1081 }
1082
1083 if (read_iir & mask2) {
1084 if (receive_data(port, dc)) {
1085 writew(mask2, dc->reg_fcr);
1086 *toggle = !(*toggle);
1087 }
1088 }
1089 } else if (*toggle == 1 && read_iir & mask2) {
1090 if (receive_data(port, dc)) {
1091 writew(mask2, dc->reg_fcr);
1092 *toggle = !(*toggle);
1093 }
1094
1095 if (read_iir & mask1) {
1096 if (receive_data(port, dc)) {
1097 writew(mask1, dc->reg_fcr);
1098 *toggle = !(*toggle);
1099 }
1100 }
1101 } else {
1102 dev_err(&dc->pdev->dev, "port out of sync!, toggle:%d\n",
1103 *toggle);
1104 return 0;
1105 }
1106 return 1;
1107}
1108
1109/*
1110 * Handle uplink data, this is currently for the modem port
1111 * Return 1 - ok
1112 * Return 0 - toggle field are out of sync
1113 */
1114static int handle_data_ul(struct nozomi *dc, enum port_type port, u16 read_iir)
1115{
1116 u8 *toggle = &(dc->port[port].toggle_ul);
1117
1118 if (*toggle == 0 && 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 if (read_iir & MDM_UL2) {
1129 dc->last_ier &= ~MDM_UL;
1130 writew(dc->last_ier, dc->reg_ier);
1131 if (send_data(port, dc)) {
1132 writew(MDM_UL2, dc->reg_fcr);
1133 dc->last_ier = dc->last_ier | MDM_UL;
1134 writew(dc->last_ier, dc->reg_ier);
1135 *toggle = !*toggle;
1136 }
1137 }
1138
1139 } else if (*toggle == 1 && read_iir & MDM_UL2) {
1140 dc->last_ier &= ~MDM_UL;
1141 writew(dc->last_ier, dc->reg_ier);
1142 if (send_data(port, dc)) {
1143 writew(MDM_UL2, dc->reg_fcr);
1144 dc->last_ier = dc->last_ier | MDM_UL;
1145 writew(dc->last_ier, dc->reg_ier);
1146 *toggle = !*toggle;
1147 }
1148
1149 if (read_iir & MDM_UL1) {
1150 dc->last_ier &= ~MDM_UL;
1151 writew(dc->last_ier, dc->reg_ier);
1152 if (send_data(port, dc)) {
1153 writew(MDM_UL1, dc->reg_fcr);
1154 dc->last_ier = dc->last_ier | MDM_UL;
1155 writew(dc->last_ier, dc->reg_ier);
1156 *toggle = !*toggle;
1157 }
1158 }
1159 } else {
1160 writew(read_iir & MDM_UL, dc->reg_fcr);
1161 dev_err(&dc->pdev->dev, "port out of sync!\n");
1162 return 0;
1163 }
1164 return 1;
1165}
1166
1167static irqreturn_t interrupt_handler(int irq, void *dev_id)
1168{
1169 struct nozomi *dc = dev_id;
1170 unsigned int a;
1171 u16 read_iir;
1172
1173 if (!dc)
1174 return IRQ_NONE;
1175
1176 spin_lock(&dc->spin_mutex);
1177 read_iir = readw(dc->reg_iir);
1178
1179 /* Card removed */
1180 if (read_iir == (u16)-1)
1181 goto none;
1182 /*
1183 * Just handle interrupt enabled in IER
1184 * (by masking with dc->last_ier)
1185 */
1186 read_iir &= dc->last_ier;
1187
1188 if (read_iir == 0)
1189 goto none;
1190
1191
1192 DBG4("%s irq:0x%04X, prev:0x%04X", interrupt2str(read_iir), read_iir,
1193 dc->last_ier);
1194
1195 if (read_iir & RESET) {
1196 if (unlikely(!nozomi_read_config_table(dc))) {
1197 dc->last_ier = 0x0;
1198 writew(dc->last_ier, dc->reg_ier);
1199 dev_err(&dc->pdev->dev, "Could not read status from "
1200 "card, we should disable interface\n");
1201 } else {
1202 writew(RESET, dc->reg_fcr);
1203 }
1204 /* No more useful info if this was the reset interrupt. */
1205 goto exit_handler;
1206 }
1207 if (read_iir & CTRL_UL) {
1208 DBG1("CTRL_UL");
1209 dc->last_ier &= ~CTRL_UL;
1210 writew(dc->last_ier, dc->reg_ier);
1211 if (send_flow_control(dc)) {
1212 writew(CTRL_UL, dc->reg_fcr);
1213 dc->last_ier = dc->last_ier | CTRL_UL;
1214 writew(dc->last_ier, dc->reg_ier);
1215 }
1216 }
1217 if (read_iir & CTRL_DL) {
1218 receive_flow_control(dc);
1219 writew(CTRL_DL, dc->reg_fcr);
1220 }
1221 if (read_iir & MDM_DL) {
1222 if (!handle_data_dl(dc, PORT_MDM,
1223 &(dc->port[PORT_MDM].toggle_dl), read_iir,
1224 MDM_DL1, MDM_DL2)) {
1225 dev_err(&dc->pdev->dev, "MDM_DL out of sync!\n");
1226 goto exit_handler;
1227 }
1228 }
1229 if (read_iir & MDM_UL) {
1230 if (!handle_data_ul(dc, PORT_MDM, read_iir)) {
1231 dev_err(&dc->pdev->dev, "MDM_UL out of sync!\n");
1232 goto exit_handler;
1233 }
1234 }
1235 if (read_iir & DIAG_DL) {
1236 if (!handle_data_dl(dc, PORT_DIAG,
1237 &(dc->port[PORT_DIAG].toggle_dl), read_iir,
1238 DIAG_DL1, DIAG_DL2)) {
1239 dev_err(&dc->pdev->dev, "DIAG_DL out of sync!\n");
1240 goto exit_handler;
1241 }
1242 }
1243 if (read_iir & DIAG_UL) {
1244 dc->last_ier &= ~DIAG_UL;
1245 writew(dc->last_ier, dc->reg_ier);
1246 if (send_data(PORT_DIAG, dc)) {
1247 writew(DIAG_UL, dc->reg_fcr);
1248 dc->last_ier = dc->last_ier | DIAG_UL;
1249 writew(dc->last_ier, dc->reg_ier);
1250 }
1251 }
1252 if (read_iir & APP1_DL) {
1253 if (receive_data(PORT_APP1, dc))
1254 writew(APP1_DL, dc->reg_fcr);
1255 }
1256 if (read_iir & APP1_UL) {
1257 dc->last_ier &= ~APP1_UL;
1258 writew(dc->last_ier, dc->reg_ier);
1259 if (send_data(PORT_APP1, dc)) {
1260 writew(APP1_UL, dc->reg_fcr);
1261 dc->last_ier = dc->last_ier | APP1_UL;
1262 writew(dc->last_ier, dc->reg_ier);
1263 }
1264 }
1265 if (read_iir & APP2_DL) {
1266 if (receive_data(PORT_APP2, dc))
1267 writew(APP2_DL, dc->reg_fcr);
1268 }
1269 if (read_iir & APP2_UL) {
1270 dc->last_ier &= ~APP2_UL;
1271 writew(dc->last_ier, dc->reg_ier);
1272 if (send_data(PORT_APP2, dc)) {
1273 writew(APP2_UL, dc->reg_fcr);
1274 dc->last_ier = dc->last_ier | APP2_UL;
1275 writew(dc->last_ier, dc->reg_ier);
1276 }
1277 }
1278
1279exit_handler:
1280 spin_unlock(&dc->spin_mutex);
Alan Cox33dd4742009-01-02 13:47:32 +00001281 for (a = 0; a < NOZOMI_MAX_PORTS; a++) {
1282 struct tty_struct *tty;
1283 if (test_and_clear_bit(a, &dc->flip)) {
1284 tty = tty_port_tty_get(&dc->port[a].port);
1285 if (tty)
1286 tty_flip_buffer_push(tty);
1287 tty_kref_put(tty);
1288 }
1289 }
Frank Seidel20fd1e32007-11-09 14:49:23 +01001290 return IRQ_HANDLED;
1291none:
1292 spin_unlock(&dc->spin_mutex);
1293 return IRQ_NONE;
1294}
1295
1296static void nozomi_get_card_type(struct nozomi *dc)
1297{
1298 int i;
1299 u32 size = 0;
1300
1301 for (i = 0; i < 6; i++)
1302 size += pci_resource_len(dc->pdev, i);
1303
1304 /* Assume card type F32_8 if no match */
1305 dc->card_type = size == 2048 ? F32_2 : F32_8;
1306
1307 dev_info(&dc->pdev->dev, "Card type is: %d\n", dc->card_type);
1308}
1309
1310static void nozomi_setup_private_data(struct nozomi *dc)
1311{
1312 void __iomem *offset = dc->base_addr + dc->card_type / 2;
1313 unsigned int i;
1314
1315 dc->reg_fcr = (void __iomem *)(offset + R_FCR);
1316 dc->reg_iir = (void __iomem *)(offset + R_IIR);
1317 dc->reg_ier = (void __iomem *)(offset + R_IER);
1318 dc->last_ier = 0;
1319 dc->flip = 0;
1320
1321 dc->port[PORT_MDM].token_dl = MDM_DL;
1322 dc->port[PORT_DIAG].token_dl = DIAG_DL;
1323 dc->port[PORT_APP1].token_dl = APP1_DL;
1324 dc->port[PORT_APP2].token_dl = APP2_DL;
1325
1326 for (i = 0; i < MAX_PORT; i++)
1327 init_waitqueue_head(&dc->port[i].tty_wait);
1328}
1329
1330static ssize_t card_type_show(struct device *dev, struct device_attribute *attr,
1331 char *buf)
1332{
Jan Engelhardt71e1b4a2008-02-01 09:13:53 +01001333 const struct nozomi *dc = pci_get_drvdata(to_pci_dev(dev));
Frank Seidel20fd1e32007-11-09 14:49:23 +01001334
1335 return sprintf(buf, "%d\n", dc->card_type);
1336}
Frank Seidele9ed5372008-01-25 21:13:24 +01001337static DEVICE_ATTR(card_type, S_IRUGO, card_type_show, NULL);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001338
1339static ssize_t open_ttys_show(struct device *dev, struct device_attribute *attr,
1340 char *buf)
1341{
Jan Engelhardt71e1b4a2008-02-01 09:13:53 +01001342 const struct nozomi *dc = pci_get_drvdata(to_pci_dev(dev));
Frank Seidel20fd1e32007-11-09 14:49:23 +01001343
1344 return sprintf(buf, "%u\n", dc->open_ttys);
1345}
Frank Seidele9ed5372008-01-25 21:13:24 +01001346static DEVICE_ATTR(open_ttys, S_IRUGO, open_ttys_show, NULL);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001347
1348static void make_sysfs_files(struct nozomi *dc)
1349{
1350 if (device_create_file(&dc->pdev->dev, &dev_attr_card_type))
1351 dev_err(&dc->pdev->dev,
1352 "Could not create sysfs file for card_type\n");
1353 if (device_create_file(&dc->pdev->dev, &dev_attr_open_ttys))
1354 dev_err(&dc->pdev->dev,
1355 "Could not create sysfs file for open_ttys\n");
1356}
1357
1358static void remove_sysfs_files(struct nozomi *dc)
1359{
1360 device_remove_file(&dc->pdev->dev, &dev_attr_card_type);
1361 device_remove_file(&dc->pdev->dev, &dev_attr_open_ttys);
1362}
1363
1364/* Allocate memory for one device */
1365static int __devinit nozomi_card_init(struct pci_dev *pdev,
1366 const struct pci_device_id *ent)
1367{
1368 resource_size_t start;
1369 int ret;
1370 struct nozomi *dc = NULL;
1371 int ndev_idx;
1372 int i;
1373
1374 dev_dbg(&pdev->dev, "Init, new card found\n");
1375
1376 for (ndev_idx = 0; ndev_idx < ARRAY_SIZE(ndevs); ndev_idx++)
1377 if (!ndevs[ndev_idx])
1378 break;
1379
1380 if (ndev_idx >= ARRAY_SIZE(ndevs)) {
1381 dev_err(&pdev->dev, "no free tty range for this card left\n");
1382 ret = -EIO;
1383 goto err;
1384 }
1385
1386 dc = kzalloc(sizeof(struct nozomi), GFP_KERNEL);
1387 if (unlikely(!dc)) {
1388 dev_err(&pdev->dev, "Could not allocate memory\n");
1389 ret = -ENOMEM;
1390 goto err_free;
1391 }
1392
1393 dc->pdev = pdev;
1394
Frank Seidel20fd1e32007-11-09 14:49:23 +01001395 ret = pci_enable_device(dc->pdev);
1396 if (ret) {
1397 dev_err(&pdev->dev, "Failed to enable PCI Device\n");
1398 goto err_free;
1399 }
1400
Frank Seidel20fd1e32007-11-09 14:49:23 +01001401 ret = pci_request_regions(dc->pdev, NOZOMI_NAME);
1402 if (ret) {
1403 dev_err(&pdev->dev, "I/O address 0x%04x already in use\n",
1404 (int) /* nozomi_private.io_addr */ 0);
1405 goto err_disable_device;
1406 }
1407
Frank Seidel661b4e82008-03-06 21:45:57 +01001408 start = pci_resource_start(dc->pdev, 0);
1409 if (start == 0) {
1410 dev_err(&pdev->dev, "No I/O address for card detected\n");
1411 ret = -ENODEV;
1412 goto err_rel_regs;
1413 }
1414
1415 /* Find out what card type it is */
1416 nozomi_get_card_type(dc);
1417
Alan Cox24cb2332008-04-30 00:54:19 -07001418 dc->base_addr = ioremap_nocache(start, dc->card_type);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001419 if (!dc->base_addr) {
1420 dev_err(&pdev->dev, "Unable to map card MMIO\n");
1421 ret = -ENODEV;
1422 goto err_rel_regs;
1423 }
1424
1425 dc->send_buf = kmalloc(SEND_BUF_MAX, GFP_KERNEL);
1426 if (!dc->send_buf) {
1427 dev_err(&pdev->dev, "Could not allocate send buffer?\n");
1428 ret = -ENOMEM;
1429 goto err_free_sbuf;
1430 }
1431
Stefani Seibold9842c382009-12-21 14:37:29 -08001432 for (i = PORT_MDM; i < MAX_PORT; i++) {
1433 if (kfifo_alloc(&dc->port[i].fifo_ul,
1434 FIFO_BUFFER_SIZE_UL, GFP_ATOMIC)) {
1435 dev_err(&pdev->dev,
1436 "Could not allocate kfifo buffer\n");
1437 ret = -ENOMEM;
1438 goto err_free_kfifo;
1439 }
1440 }
1441
Frank Seidel20fd1e32007-11-09 14:49:23 +01001442 spin_lock_init(&dc->spin_mutex);
1443
1444 nozomi_setup_private_data(dc);
1445
1446 /* Disable all interrupts */
1447 dc->last_ier = 0;
1448 writew(dc->last_ier, dc->reg_ier);
1449
1450 ret = request_irq(pdev->irq, &interrupt_handler, IRQF_SHARED,
1451 NOZOMI_NAME, dc);
1452 if (unlikely(ret)) {
1453 dev_err(&pdev->dev, "can't request irq %d\n", pdev->irq);
Stefani Seibold9842c382009-12-21 14:37:29 -08001454 goto err_free_kfifo;
Frank Seidel20fd1e32007-11-09 14:49:23 +01001455 }
1456
1457 DBG1("base_addr: %p", dc->base_addr);
1458
1459 make_sysfs_files(dc);
1460
1461 dc->index_start = ndev_idx * MAX_PORT;
1462 ndevs[ndev_idx] = dc;
1463
Frank Seidel661b4e82008-03-06 21:45:57 +01001464 pci_set_drvdata(pdev, dc);
1465
1466 /* Enable RESET interrupt */
1467 dc->last_ier = RESET;
1468 iowrite16(dc->last_ier, dc->reg_ier);
1469
1470 dc->state = NOZOMI_STATE_ENABLED;
1471
Frank Seidel20fd1e32007-11-09 14:49:23 +01001472 for (i = 0; i < MAX_PORT; i++) {
Stefani Seibold9842c382009-12-21 14:37:29 -08001473 struct device *tty_dev;
Alan Cox266794e2010-02-08 10:06:45 +00001474 struct port *port = &dc->port[i];
1475 port->dc = dc;
1476 mutex_init(&port->tty_sem);
1477 tty_port_init(&port->port);
1478 port->port.ops = &noz_tty_port_ops;
Stefani Seibold9842c382009-12-21 14:37:29 -08001479 tty_dev = tty_register_device(ntty_driver, dc->index_start + i,
Frank Seidel20fd1e32007-11-09 14:49:23 +01001480 &pdev->dev);
Stefani Seibold9842c382009-12-21 14:37:29 -08001481
1482 if (IS_ERR(tty_dev)) {
1483 ret = PTR_ERR(tty_dev);
1484 dev_err(&pdev->dev, "Could not allocate tty?\n");
1485 goto err_free_tty;
1486 }
Frank Seidel20fd1e32007-11-09 14:49:23 +01001487 }
Stefani Seibold9842c382009-12-21 14:37:29 -08001488
Frank Seidel20fd1e32007-11-09 14:49:23 +01001489 return 0;
1490
Stefani Seibold9842c382009-12-21 14:37:29 -08001491err_free_tty:
1492 for (i = dc->index_start; i < dc->index_start + MAX_PORT; ++i)
1493 tty_unregister_device(ntty_driver, i);
1494err_free_kfifo:
1495 for (i = 0; i < MAX_PORT; i++)
1496 kfifo_free(&dc->port[i].fifo_ul);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001497err_free_sbuf:
1498 kfree(dc->send_buf);
1499 iounmap(dc->base_addr);
1500err_rel_regs:
1501 pci_release_regions(pdev);
1502err_disable_device:
1503 pci_disable_device(pdev);
1504err_free:
1505 kfree(dc);
1506err:
1507 return ret;
1508}
1509
1510static void __devexit tty_exit(struct nozomi *dc)
1511{
1512 unsigned int i;
1513
1514 DBG1(" ");
1515
1516 flush_scheduled_work();
1517
Alan Cox33dd4742009-01-02 13:47:32 +00001518 for (i = 0; i < MAX_PORT; ++i) {
1519 struct tty_struct *tty = tty_port_tty_get(&dc->port[i].port);
1520 if (tty && list_empty(&tty->hangup_work.entry))
1521 tty_hangup(tty);
1522 tty_kref_put(tty);
1523 }
1524 /* Racy below - surely should wait for scheduled work to be done or
1525 complete off a hangup method ? */
Frank Seidel20fd1e32007-11-09 14:49:23 +01001526 while (dc->open_ttys)
1527 msleep(1);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001528 for (i = dc->index_start; i < dc->index_start + MAX_PORT; ++i)
1529 tty_unregister_device(ntty_driver, i);
1530}
1531
1532/* Deallocate memory for one device */
1533static void __devexit nozomi_card_exit(struct pci_dev *pdev)
1534{
1535 int i;
1536 struct ctrl_ul ctrl;
1537 struct nozomi *dc = pci_get_drvdata(pdev);
1538
1539 /* Disable all interrupts */
1540 dc->last_ier = 0;
1541 writew(dc->last_ier, dc->reg_ier);
1542
1543 tty_exit(dc);
1544
1545 /* Send 0x0001, command card to resend the reset token. */
1546 /* This is to get the reset when the module is reloaded. */
1547 ctrl.port = 0x00;
1548 ctrl.reserved = 0;
1549 ctrl.RTS = 0;
1550 ctrl.DTR = 1;
1551 DBG1("sending flow control 0x%04X", *((u16 *)&ctrl));
1552
1553 /* Setup dc->reg addresses to we can use defines here */
1554 write_mem32(dc->port[PORT_CTRL].ul_addr[0], (u32 *)&ctrl, 2);
1555 writew(CTRL_UL, dc->reg_fcr); /* push the token to the card. */
1556
1557 remove_sysfs_files(dc);
1558
1559 free_irq(pdev->irq, dc);
1560
1561 for (i = 0; i < MAX_PORT; i++)
Stefani Seibold45465482009-12-21 14:37:26 -08001562 kfifo_free(&dc->port[i].fifo_ul);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001563
1564 kfree(dc->send_buf);
1565
1566 iounmap(dc->base_addr);
1567
1568 pci_release_regions(pdev);
1569
1570 pci_disable_device(pdev);
1571
1572 ndevs[dc->index_start / MAX_PORT] = NULL;
1573
1574 kfree(dc);
1575}
1576
1577static void set_rts(const struct tty_struct *tty, int rts)
1578{
1579 struct port *port = get_port_by_tty(tty);
1580
1581 port->ctrl_ul.RTS = rts;
1582 port->update_flow_control = 1;
1583 enable_transmit_ul(PORT_CTRL, get_dc_by_tty(tty));
1584}
1585
1586static void set_dtr(const struct tty_struct *tty, int dtr)
1587{
1588 struct port *port = get_port_by_tty(tty);
1589
1590 DBG1("SETTING DTR index: %d, dtr: %d", tty->index, dtr);
1591
1592 port->ctrl_ul.DTR = dtr;
1593 port->update_flow_control = 1;
1594 enable_transmit_ul(PORT_CTRL, get_dc_by_tty(tty));
1595}
1596
1597/*
1598 * ----------------------------------------------------------------------------
1599 * TTY code
1600 * ----------------------------------------------------------------------------
1601 */
1602
Alan Cox266794e2010-02-08 10:06:45 +00001603static int ntty_install(struct tty_driver *driver, struct tty_struct *tty)
Frank Seidel20fd1e32007-11-09 14:49:23 +01001604{
1605 struct port *port = get_port_by_tty(tty);
1606 struct nozomi *dc = get_dc_by_tty(tty);
Alan Cox266794e2010-02-08 10:06:45 +00001607 int ret;
Frank Seidel661b4e82008-03-06 21:45:57 +01001608 if (!port || !dc || dc->state != NOZOMI_STATE_READY)
Frank Seidel20fd1e32007-11-09 14:49:23 +01001609 return -ENODEV;
Alan Cox266794e2010-02-08 10:06:45 +00001610 ret = tty_init_termios(tty);
1611 if (ret == 0) {
1612 tty_driver_kref_get(driver);
1613 driver->ttys[tty->index] = tty;
Frank Seidel20fd1e32007-11-09 14:49:23 +01001614 }
Alan Cox266794e2010-02-08 10:06:45 +00001615 return ret;
1616}
1617
1618static void ntty_cleanup(struct tty_struct *tty)
1619{
1620 tty->driver_data = NULL;
1621}
1622
1623static int ntty_activate(struct tty_port *tport, struct tty_struct *tty)
1624{
1625 struct port *port = container_of(tport, struct port, port);
1626 struct nozomi *dc = port->dc;
1627 unsigned long flags;
1628
1629 DBG1("open: %d", port->token_dl);
1630 spin_lock_irqsave(&dc->spin_mutex, flags);
1631 dc->last_ier = dc->last_ier | port->token_dl;
1632 writew(dc->last_ier, dc->reg_ier);
1633 dc->open_ttys++;
1634 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1635 printk("noz: activated %d: %p\n", tty->index, tport);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001636 return 0;
1637}
1638
Alan Cox266794e2010-02-08 10:06:45 +00001639static int ntty_open(struct tty_struct *tty, struct file *filp)
Frank Seidel20fd1e32007-11-09 14:49:23 +01001640{
Alan Cox266794e2010-02-08 10:06:45 +00001641 struct port *port = get_port_by_tty(tty);
1642 return tty_port_open(&port->port, tty, filp);
1643}
1644
1645static void ntty_shutdown(struct tty_port *tport)
1646{
1647 struct port *port = container_of(tport, struct port, port);
1648 struct nozomi *dc = port->dc;
Frank Seidel20fd1e32007-11-09 14:49:23 +01001649 unsigned long flags;
1650
Alan Cox266794e2010-02-08 10:06:45 +00001651 DBG1("close: %d", port->token_dl);
1652 spin_lock_irqsave(&dc->spin_mutex, flags);
1653 dc->last_ier &= ~(port->token_dl);
1654 writew(dc->last_ier, dc->reg_ier);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001655 dc->open_ttys--;
Alan Cox266794e2010-02-08 10:06:45 +00001656 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1657 printk("noz: shutdown %p\n", tport);
1658}
Frank Seidel20fd1e32007-11-09 14:49:23 +01001659
Alan Cox266794e2010-02-08 10:06:45 +00001660static void ntty_close(struct tty_struct *tty, struct file *filp)
1661{
1662 struct port *port = tty->driver_data;
1663 if (port)
1664 tty_port_close(&port->port, tty, filp);
1665}
1666
1667static void ntty_hangup(struct tty_struct *tty)
1668{
1669 struct port *port = tty->driver_data;
1670 tty_port_hangup(&port->port);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001671}
1672
1673/*
1674 * called when the userspace process writes to the tty (/dev/noz*).
1675 * Data is inserted into a fifo, which is then read and transfered to the modem.
1676 */
1677static int ntty_write(struct tty_struct *tty, const unsigned char *buffer,
1678 int count)
1679{
1680 int rval = -EINVAL;
1681 struct nozomi *dc = get_dc_by_tty(tty);
1682 struct port *port = tty->driver_data;
1683 unsigned long flags;
1684
1685 /* DBG1( "WRITEx: %d, index = %d", count, index); */
1686
1687 if (!dc || !port)
1688 return -ENODEV;
1689
Alan Coxe8c65d12010-02-08 10:07:04 +00001690 mutex_lock(&port->tty_sem);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001691
Alan Cox33dd4742009-01-02 13:47:32 +00001692 if (unlikely(!port->port.count)) {
Frank Seidel20fd1e32007-11-09 14:49:23 +01001693 DBG1(" ");
1694 goto exit;
1695 }
1696
Stefani Seibold7acd72e2009-12-21 14:37:28 -08001697 rval = kfifo_in(&port->fifo_ul, (unsigned char *)buffer, count);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001698
1699 /* notify card */
1700 if (unlikely(dc == NULL)) {
1701 DBG1("No device context?");
1702 goto exit;
1703 }
1704
1705 spin_lock_irqsave(&dc->spin_mutex, flags);
1706 /* CTS is only valid on the modem channel */
1707 if (port == &(dc->port[PORT_MDM])) {
1708 if (port->ctrl_dl.CTS) {
1709 DBG4("Enable interrupt");
1710 enable_transmit_ul(tty->index % MAX_PORT, dc);
1711 } else {
1712 dev_err(&dc->pdev->dev,
1713 "CTS not active on modem port?\n");
1714 }
1715 } else {
1716 enable_transmit_ul(tty->index % MAX_PORT, dc);
1717 }
1718 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1719
1720exit:
1721 mutex_unlock(&port->tty_sem);
1722 return rval;
1723}
1724
1725/*
1726 * Calculate how much is left in device
1727 * This method is called by the upper tty layer.
1728 * #according to sources N_TTY.c it expects a value >= 0 and
1729 * does not check for negative values.
Alan Coxe8c65d12010-02-08 10:07:04 +00001730 *
1731 * If the port is unplugged report lots of room and let the bits
1732 * dribble away so we don't block anything.
Frank Seidel20fd1e32007-11-09 14:49:23 +01001733 */
1734static int ntty_write_room(struct tty_struct *tty)
1735{
1736 struct port *port = tty->driver_data;
Alan Coxe8c65d12010-02-08 10:07:04 +00001737 int room = 4096;
Jan Engelhardt71e1b4a2008-02-01 09:13:53 +01001738 const struct nozomi *dc = get_dc_by_tty(tty);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001739
Alan Coxe8c65d12010-02-08 10:07:04 +00001740 if (dc) {
1741 mutex_lock(&port->tty_sem);
1742 if (port->port.count)
1743 room = port->fifo_ul.size -
1744 kfifo_len(&port->fifo_ul);
1745 mutex_unlock(&port->tty_sem);
1746 }
Frank Seidel20fd1e32007-11-09 14:49:23 +01001747 return room;
1748}
1749
1750/* Gets io control parameters */
1751static int ntty_tiocmget(struct tty_struct *tty, struct file *file)
1752{
Jan Engelhardt71e1b4a2008-02-01 09:13:53 +01001753 const struct port *port = tty->driver_data;
1754 const struct ctrl_dl *ctrl_dl = &port->ctrl_dl;
1755 const struct ctrl_ul *ctrl_ul = &port->ctrl_ul;
Frank Seidel20fd1e32007-11-09 14:49:23 +01001756
Alan Cox978e5952008-04-30 00:53:59 -07001757 /* Note: these could change under us but it is not clear this
1758 matters if so */
Frank Seidel20fd1e32007-11-09 14:49:23 +01001759 return (ctrl_ul->RTS ? TIOCM_RTS : 0) |
1760 (ctrl_ul->DTR ? TIOCM_DTR : 0) |
1761 (ctrl_dl->DCD ? TIOCM_CAR : 0) |
1762 (ctrl_dl->RI ? TIOCM_RNG : 0) |
1763 (ctrl_dl->DSR ? TIOCM_DSR : 0) |
1764 (ctrl_dl->CTS ? TIOCM_CTS : 0);
1765}
1766
1767/* Sets io controls parameters */
1768static int ntty_tiocmset(struct tty_struct *tty, struct file *file,
1769 unsigned int set, unsigned int clear)
1770{
Frank Seidel661b4e82008-03-06 21:45:57 +01001771 struct nozomi *dc = get_dc_by_tty(tty);
1772 unsigned long flags;
1773
1774 spin_lock_irqsave(&dc->spin_mutex, flags);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001775 if (set & TIOCM_RTS)
1776 set_rts(tty, 1);
1777 else if (clear & TIOCM_RTS)
1778 set_rts(tty, 0);
1779
1780 if (set & TIOCM_DTR)
1781 set_dtr(tty, 1);
1782 else if (clear & TIOCM_DTR)
1783 set_dtr(tty, 0);
Frank Seidel661b4e82008-03-06 21:45:57 +01001784 spin_unlock_irqrestore(&dc->spin_mutex, flags);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001785
1786 return 0;
1787}
1788
1789static int ntty_cflags_changed(struct port *port, unsigned long flags,
1790 struct async_icount *cprev)
1791{
Frank Seidel18bbe0c22008-02-01 09:14:05 +01001792 const struct async_icount cnow = port->tty_icount;
Frank Seidel20fd1e32007-11-09 14:49:23 +01001793 int ret;
1794
1795 ret = ((flags & TIOCM_RNG) && (cnow.rng != cprev->rng)) ||
1796 ((flags & TIOCM_DSR) && (cnow.dsr != cprev->dsr)) ||
1797 ((flags & TIOCM_CD) && (cnow.dcd != cprev->dcd)) ||
1798 ((flags & TIOCM_CTS) && (cnow.cts != cprev->cts));
1799
1800 *cprev = cnow;
1801
1802 return ret;
1803}
1804
1805static int ntty_ioctl_tiocgicount(struct port *port, void __user *argp)
1806{
Frank Seidel18bbe0c22008-02-01 09:14:05 +01001807 const struct async_icount cnow = port->tty_icount;
Frank Seidel20fd1e32007-11-09 14:49:23 +01001808 struct serial_icounter_struct icount;
1809
1810 icount.cts = cnow.cts;
1811 icount.dsr = cnow.dsr;
1812 icount.rng = cnow.rng;
1813 icount.dcd = cnow.dcd;
1814 icount.rx = cnow.rx;
1815 icount.tx = cnow.tx;
1816 icount.frame = cnow.frame;
1817 icount.overrun = cnow.overrun;
1818 icount.parity = cnow.parity;
1819 icount.brk = cnow.brk;
1820 icount.buf_overrun = cnow.buf_overrun;
1821
Frank Seidel661b4e82008-03-06 21:45:57 +01001822 return copy_to_user(argp, &icount, sizeof(icount)) ? -EFAULT : 0;
Frank Seidel20fd1e32007-11-09 14:49:23 +01001823}
1824
1825static int ntty_ioctl(struct tty_struct *tty, struct file *file,
1826 unsigned int cmd, unsigned long arg)
1827{
1828 struct port *port = tty->driver_data;
1829 void __user *argp = (void __user *)arg;
1830 int rval = -ENOIOCTLCMD;
1831
1832 DBG1("******** IOCTL, cmd: %d", cmd);
1833
1834 switch (cmd) {
1835 case TIOCMIWAIT: {
1836 struct async_icount cprev = port->tty_icount;
1837
1838 rval = wait_event_interruptible(port->tty_wait,
1839 ntty_cflags_changed(port, arg, &cprev));
1840 break;
1841 } case TIOCGICOUNT:
1842 rval = ntty_ioctl_tiocgicount(port, argp);
1843 break;
1844 default:
1845 DBG1("ERR: 0x%08X, %d", cmd, cmd);
1846 break;
1847 };
1848
1849 return rval;
1850}
1851
1852/*
1853 * Called by the upper tty layer when tty buffers are ready
1854 * to receive data again after a call to throttle.
1855 */
1856static void ntty_unthrottle(struct tty_struct *tty)
1857{
1858 struct nozomi *dc = get_dc_by_tty(tty);
1859 unsigned long flags;
1860
1861 DBG1("UNTHROTTLE");
1862 spin_lock_irqsave(&dc->spin_mutex, flags);
1863 enable_transmit_dl(tty->index % MAX_PORT, dc);
1864 set_rts(tty, 1);
1865
1866 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1867}
1868
1869/*
1870 * Called by the upper tty layer when the tty buffers are almost full.
1871 * The driver should stop send more data.
1872 */
1873static void ntty_throttle(struct tty_struct *tty)
1874{
1875 struct nozomi *dc = get_dc_by_tty(tty);
1876 unsigned long flags;
1877
1878 DBG1("THROTTLE");
1879 spin_lock_irqsave(&dc->spin_mutex, flags);
1880 set_rts(tty, 0);
1881 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1882}
1883
Frank Seidel20fd1e32007-11-09 14:49:23 +01001884/* Returns number of chars in buffer, called by tty layer */
1885static s32 ntty_chars_in_buffer(struct tty_struct *tty)
1886{
1887 struct port *port = tty->driver_data;
1888 struct nozomi *dc = get_dc_by_tty(tty);
Alan Cox23198fd2009-07-20 16:05:27 +01001889 s32 rval = 0;
Frank Seidel20fd1e32007-11-09 14:49:23 +01001890
1891 if (unlikely(!dc || !port)) {
Frank Seidel20fd1e32007-11-09 14:49:23 +01001892 goto exit_in_buffer;
1893 }
1894
Alan Cox33dd4742009-01-02 13:47:32 +00001895 if (unlikely(!port->port.count)) {
Frank Seidel20fd1e32007-11-09 14:49:23 +01001896 dev_err(&dc->pdev->dev, "No tty open?\n");
Frank Seidel20fd1e32007-11-09 14:49:23 +01001897 goto exit_in_buffer;
1898 }
1899
Stefani Seibolde64c0262009-12-21 14:37:28 -08001900 rval = kfifo_len(&port->fifo_ul);
Frank Seidel20fd1e32007-11-09 14:49:23 +01001901
1902exit_in_buffer:
1903 return rval;
1904}
1905
Alan Cox266794e2010-02-08 10:06:45 +00001906static const struct tty_port_operations noz_tty_port_ops = {
1907 .activate = ntty_activate,
1908 .shutdown = ntty_shutdown,
1909};
1910
Frank Seidel18bbe0c22008-02-01 09:14:05 +01001911static const struct tty_operations tty_ops = {
Frank Seidel20fd1e32007-11-09 14:49:23 +01001912 .ioctl = ntty_ioctl,
1913 .open = ntty_open,
1914 .close = ntty_close,
Alan Cox266794e2010-02-08 10:06:45 +00001915 .hangup = ntty_hangup,
Frank Seidel20fd1e32007-11-09 14:49:23 +01001916 .write = ntty_write,
1917 .write_room = ntty_write_room,
1918 .unthrottle = ntty_unthrottle,
1919 .throttle = ntty_throttle,
1920 .chars_in_buffer = ntty_chars_in_buffer,
Frank Seidel20fd1e32007-11-09 14:49:23 +01001921 .tiocmget = ntty_tiocmget,
1922 .tiocmset = ntty_tiocmset,
Alan Cox266794e2010-02-08 10:06:45 +00001923 .install = ntty_install,
1924 .cleanup = ntty_cleanup,
Frank Seidel20fd1e32007-11-09 14:49:23 +01001925};
1926
1927/* Module initialization */
1928static struct pci_driver nozomi_driver = {
1929 .name = NOZOMI_NAME,
1930 .id_table = nozomi_pci_tbl,
1931 .probe = nozomi_card_init,
1932 .remove = __devexit_p(nozomi_card_exit),
1933};
1934
1935static __init int nozomi_init(void)
1936{
1937 int ret;
1938
1939 printk(KERN_INFO "Initializing %s\n", VERSION_STRING);
1940
1941 ntty_driver = alloc_tty_driver(NTTY_TTY_MAXMINORS);
1942 if (!ntty_driver)
1943 return -ENOMEM;
1944
1945 ntty_driver->owner = THIS_MODULE;
1946 ntty_driver->driver_name = NOZOMI_NAME_TTY;
1947 ntty_driver->name = "noz";
1948 ntty_driver->major = 0;
1949 ntty_driver->type = TTY_DRIVER_TYPE_SERIAL;
1950 ntty_driver->subtype = SERIAL_TYPE_NORMAL;
1951 ntty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
1952 ntty_driver->init_termios = tty_std_termios;
1953 ntty_driver->init_termios.c_cflag = B115200 | CS8 | CREAD | \
1954 HUPCL | CLOCAL;
1955 ntty_driver->init_termios.c_ispeed = 115200;
1956 ntty_driver->init_termios.c_ospeed = 115200;
1957 tty_set_operations(ntty_driver, &tty_ops);
1958
1959 ret = tty_register_driver(ntty_driver);
1960 if (ret) {
1961 printk(KERN_ERR "Nozomi: failed to register ntty driver\n");
1962 goto free_tty;
1963 }
1964
1965 ret = pci_register_driver(&nozomi_driver);
1966 if (ret) {
1967 printk(KERN_ERR "Nozomi: can't register pci driver\n");
1968 goto unr_tty;
1969 }
1970
1971 return 0;
1972unr_tty:
1973 tty_unregister_driver(ntty_driver);
1974free_tty:
1975 put_tty_driver(ntty_driver);
1976 return ret;
1977}
1978
1979static __exit void nozomi_exit(void)
1980{
1981 printk(KERN_INFO "Unloading %s\n", DRIVER_DESC);
1982 pci_unregister_driver(&nozomi_driver);
1983 tty_unregister_driver(ntty_driver);
1984 put_tty_driver(ntty_driver);
1985}
1986
1987module_init(nozomi_init);
1988module_exit(nozomi_exit);
1989
1990module_param(debug, int, S_IRUGO | S_IWUSR);
1991
1992MODULE_LICENSE("Dual BSD/GPL");
1993MODULE_DESCRIPTION(DRIVER_DESC);