]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - drivers/char/nozomi.c
xfrm: MIGRATE enhancements (draft-ebalard-mext-pfkey-enhanced-migrate)
[mirror_ubuntu-artful-kernel.git] / drivers / char / nozomi.c
1 /*
2 * nozomi.c -- HSDPA driver Broadband Wireless Data Card - Globe Trotter
3 *
4 * Written by: Ulf Jakobsson,
5 * Jan Ã…kerfeldt,
6 * 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
41 /* Enable this to have a lot of debug printouts */
42 #define DEBUG
43
44 #include <linux/kernel.h>
45 #include <linux/module.h>
46 #include <linux/pci.h>
47 #include <linux/ioport.h>
48 #include <linux/tty.h>
49 #include <linux/tty_driver.h>
50 #include <linux/tty_flip.h>
51 #include <linux/serial.h>
52 #include <linux/interrupt.h>
53 #include <linux/kmod.h>
54 #include <linux/init.h>
55 #include <linux/kfifo.h>
56 #include <linux/uaccess.h>
57 #include <asm/byteorder.h>
58
59 #include <linux/delay.h>
60
61
62 #define VERSION_STRING DRIVER_DESC " 2.1d (build date: " \
63 __DATE__ " " __TIME__ ")"
64
65 /* Macros definitions */
66
67 /* Default debug printout level */
68 #define NOZOMI_DEBUG_LEVEL 0x00
69
70 #define P_BUF_SIZE 128
71 #define NFO(_err_flag_, args...) \
72 do { \
73 char tmp[P_BUF_SIZE]; \
74 snprintf(tmp, sizeof(tmp), ##args); \
75 printk(_err_flag_ "[%d] %s(): %s\n", __LINE__, \
76 __func__, tmp); \
77 } while (0)
78
79 #define DBG1(args...) D_(0x01, ##args)
80 #define DBG2(args...) D_(0x02, ##args)
81 #define DBG3(args...) D_(0x04, ##args)
82 #define DBG4(args...) D_(0x08, ##args)
83 #define DBG5(args...) D_(0x10, ##args)
84 #define DBG6(args...) D_(0x20, ##args)
85 #define DBG7(args...) D_(0x40, ##args)
86 #define DBG8(args...) D_(0x80, ##args)
87
88 #ifdef DEBUG
89 /* Do we need this settable at runtime? */
90 static int debug = NOZOMI_DEBUG_LEVEL;
91
92 #define D(lvl, args...) do \
93 {if (lvl & debug) NFO(KERN_DEBUG, ##args); } \
94 while (0)
95 #define D_(lvl, args...) D(lvl, ##args)
96
97 /* These printouts are always printed */
98
99 #else
100 static int debug;
101 #define D_(lvl, args...)
102 #endif
103
104 /* TODO: rewrite to optimize macros... */
105
106 #define TMP_BUF_MAX 256
107
108 #define DUMP(buf__,len__) \
109 do { \
110 char tbuf[TMP_BUF_MAX] = {0};\
111 if (len__ > 1) {\
112 snprintf(tbuf, len__ > TMP_BUF_MAX ? TMP_BUF_MAX : len__, "%s", buf__);\
113 if (tbuf[len__-2] == '\r') {\
114 tbuf[len__-2] = 'r';\
115 } \
116 DBG1("SENDING: '%s' (%d+n)", tbuf, len__);\
117 } else {\
118 DBG1("SENDING: '%s' (%d)", tbuf, len__);\
119 } \
120 } while (0)
121
122 /* Defines */
123 #define NOZOMI_NAME "nozomi"
124 #define NOZOMI_NAME_TTY "nozomi_tty"
125 #define DRIVER_DESC "Nozomi driver"
126
127 #define NTTY_TTY_MAXMINORS 256
128 #define NTTY_FIFO_BUFFER_SIZE 8192
129
130 /* Must be power of 2 */
131 #define FIFO_BUFFER_SIZE_UL 8192
132
133 /* Size of tmp send buffer to card */
134 #define SEND_BUF_MAX 1024
135 #define RECEIVE_BUF_MAX 4
136
137
138 /* Define all types of vendors and devices to support */
139 #define VENDOR1 0x1931 /* Vendor Option */
140 #define DEVICE1 0x000c /* HSDPA card */
141
142 #define R_IIR 0x0000 /* Interrupt Identity Register */
143 #define R_FCR 0x0000 /* Flow Control Register */
144 #define R_IER 0x0004 /* Interrupt Enable Register */
145
146 #define CONFIG_MAGIC 0xEFEFFEFE
147 #define TOGGLE_VALID 0x0000
148
149 /* Definition of interrupt tokens */
150 #define MDM_DL1 0x0001
151 #define MDM_UL1 0x0002
152 #define MDM_DL2 0x0004
153 #define MDM_UL2 0x0008
154 #define DIAG_DL1 0x0010
155 #define DIAG_DL2 0x0020
156 #define DIAG_UL 0x0040
157 #define APP1_DL 0x0080
158 #define APP1_UL 0x0100
159 #define APP2_DL 0x0200
160 #define APP2_UL 0x0400
161 #define CTRL_DL 0x0800
162 #define CTRL_UL 0x1000
163 #define RESET 0x8000
164
165 #define MDM_DL (MDM_DL1 | MDM_DL2)
166 #define MDM_UL (MDM_UL1 | MDM_UL2)
167 #define DIAG_DL (DIAG_DL1 | DIAG_DL2)
168
169 /* modem signal definition */
170 #define CTRL_DSR 0x0001
171 #define CTRL_DCD 0x0002
172 #define CTRL_RI 0x0004
173 #define CTRL_CTS 0x0008
174
175 #define CTRL_DTR 0x0001
176 #define CTRL_RTS 0x0002
177
178 #define MAX_PORT 4
179 #define NOZOMI_MAX_PORTS 5
180 #define NOZOMI_MAX_CARDS (NTTY_TTY_MAXMINORS / MAX_PORT)
181
182 /* Type definitions */
183
184 /*
185 * There are two types of nozomi cards,
186 * one with 2048 memory and with 8192 memory
187 */
188 enum card_type {
189 F32_2 = 2048, /* 512 bytes downlink + uplink * 2 -> 2048 */
190 F32_8 = 8192, /* 3072 bytes downl. + 1024 bytes uplink * 2 -> 8192 */
191 };
192
193 /* Initialization states a card can be in */
194 enum card_state {
195 NOZOMI_STATE_UKNOWN = 0,
196 NOZOMI_STATE_ENABLED = 1, /* pci device enabled */
197 NOZOMI_STATE_ALLOCATED = 2, /* config setup done */
198 NOZOMI_STATE_READY = 3, /* flowcontrols received */
199 };
200
201 /* Two different toggle channels exist */
202 enum channel_type {
203 CH_A = 0,
204 CH_B = 1,
205 };
206
207 /* Port definition for the card regarding flow control */
208 enum ctrl_port_type {
209 CTRL_CMD = 0,
210 CTRL_MDM = 1,
211 CTRL_DIAG = 2,
212 CTRL_APP1 = 3,
213 CTRL_APP2 = 4,
214 CTRL_ERROR = -1,
215 };
216
217 /* Ports that the nozomi has */
218 enum port_type {
219 PORT_MDM = 0,
220 PORT_DIAG = 1,
221 PORT_APP1 = 2,
222 PORT_APP2 = 3,
223 PORT_CTRL = 4,
224 PORT_ERROR = -1,
225 };
226
227 #ifdef __BIG_ENDIAN
228 /* Big endian */
229
230 struct toggles {
231 unsigned int enabled:5; /*
232 * Toggle fields are valid if enabled is 0,
233 * else A-channels must always be used.
234 */
235 unsigned int diag_dl:1;
236 unsigned int mdm_dl:1;
237 unsigned int mdm_ul:1;
238 } __attribute__ ((packed));
239
240 /* Configuration table to read at startup of card */
241 /* Is for now only needed during initialization phase */
242 struct config_table {
243 u32 signature;
244 u16 product_information;
245 u16 version;
246 u8 pad3[3];
247 struct toggles toggle;
248 u8 pad1[4];
249 u16 dl_mdm_len1; /*
250 * If this is 64, it can hold
251 * 60 bytes + 4 that is length field
252 */
253 u16 dl_start;
254
255 u16 dl_diag_len1;
256 u16 dl_mdm_len2; /*
257 * If this is 64, it can hold
258 * 60 bytes + 4 that is length field
259 */
260 u16 dl_app1_len;
261
262 u16 dl_diag_len2;
263 u16 dl_ctrl_len;
264 u16 dl_app2_len;
265 u8 pad2[16];
266 u16 ul_mdm_len1;
267 u16 ul_start;
268 u16 ul_diag_len;
269 u16 ul_mdm_len2;
270 u16 ul_app1_len;
271 u16 ul_app2_len;
272 u16 ul_ctrl_len;
273 } __attribute__ ((packed));
274
275 /* This stores all control downlink flags */
276 struct ctrl_dl {
277 u8 port;
278 unsigned int reserved:4;
279 unsigned int CTS:1;
280 unsigned int RI:1;
281 unsigned int DCD:1;
282 unsigned int DSR:1;
283 } __attribute__ ((packed));
284
285 /* This stores all control uplink flags */
286 struct ctrl_ul {
287 u8 port;
288 unsigned int reserved:6;
289 unsigned int RTS:1;
290 unsigned int DTR:1;
291 } __attribute__ ((packed));
292
293 #else
294 /* Little endian */
295
296 /* This represents the toggle information */
297 struct toggles {
298 unsigned int mdm_ul:1;
299 unsigned int mdm_dl:1;
300 unsigned int diag_dl:1;
301 unsigned int enabled:5; /*
302 * Toggle fields are valid if enabled is 0,
303 * else A-channels must always be used.
304 */
305 } __attribute__ ((packed));
306
307 /* Configuration table to read at startup of card */
308 struct config_table {
309 u32 signature;
310 u16 version;
311 u16 product_information;
312 struct toggles toggle;
313 u8 pad1[7];
314 u16 dl_start;
315 u16 dl_mdm_len1; /*
316 * If this is 64, it can hold
317 * 60 bytes + 4 that is length field
318 */
319 u16 dl_mdm_len2;
320 u16 dl_diag_len1;
321 u16 dl_diag_len2;
322 u16 dl_app1_len;
323 u16 dl_app2_len;
324 u16 dl_ctrl_len;
325 u8 pad2[16];
326 u16 ul_start;
327 u16 ul_mdm_len2;
328 u16 ul_mdm_len1;
329 u16 ul_diag_len;
330 u16 ul_app1_len;
331 u16 ul_app2_len;
332 u16 ul_ctrl_len;
333 } __attribute__ ((packed));
334
335 /* This stores all control downlink flags */
336 struct ctrl_dl {
337 unsigned int DSR:1;
338 unsigned int DCD:1;
339 unsigned int RI:1;
340 unsigned int CTS:1;
341 unsigned int reserverd:4;
342 u8 port;
343 } __attribute__ ((packed));
344
345 /* This stores all control uplink flags */
346 struct ctrl_ul {
347 unsigned int DTR:1;
348 unsigned int RTS:1;
349 unsigned int reserved:6;
350 u8 port;
351 } __attribute__ ((packed));
352 #endif
353
354 /* This holds all information that is needed regarding a port */
355 struct port {
356 u8 update_flow_control;
357 struct ctrl_ul ctrl_ul;
358 struct ctrl_dl ctrl_dl;
359 struct kfifo *fifo_ul;
360 void __iomem *dl_addr[2];
361 u32 dl_size[2];
362 u8 toggle_dl;
363 void __iomem *ul_addr[2];
364 u32 ul_size[2];
365 u8 toggle_ul;
366 u16 token_dl;
367
368 struct tty_struct *tty;
369 int tty_open_count;
370 /* mutex to ensure one access patch to this port */
371 struct mutex tty_sem;
372 wait_queue_head_t tty_wait;
373 struct async_icount tty_icount;
374 };
375
376 /* Private data one for each card in the system */
377 struct nozomi {
378 void __iomem *base_addr;
379 unsigned long flip;
380
381 /* Pointers to registers */
382 void __iomem *reg_iir;
383 void __iomem *reg_fcr;
384 void __iomem *reg_ier;
385
386 u16 last_ier;
387 enum card_type card_type;
388 struct config_table config_table; /* Configuration table */
389 struct pci_dev *pdev;
390 struct port port[NOZOMI_MAX_PORTS];
391 u8 *send_buf;
392
393 spinlock_t spin_mutex; /* secures access to registers and tty */
394
395 unsigned int index_start;
396 enum card_state state;
397 u32 open_ttys;
398 };
399
400 /* This is a data packet that is read or written to/from card */
401 struct buffer {
402 u32 size; /* size is the length of the data buffer */
403 u8 *data;
404 } __attribute__ ((packed));
405
406 /* Global variables */
407 static const struct pci_device_id nozomi_pci_tbl[] __devinitconst = {
408 {PCI_DEVICE(VENDOR1, DEVICE1)},
409 {},
410 };
411
412 MODULE_DEVICE_TABLE(pci, nozomi_pci_tbl);
413
414 static struct nozomi *ndevs[NOZOMI_MAX_CARDS];
415 static struct tty_driver *ntty_driver;
416
417 /*
418 * find card by tty_index
419 */
420 static inline struct nozomi *get_dc_by_tty(const struct tty_struct *tty)
421 {
422 return tty ? ndevs[tty->index / MAX_PORT] : NULL;
423 }
424
425 static 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
437 static void read_mem32(u32 *buf, const void __iomem *mem_addr_start,
438 u32 size_bytes)
439 {
440 u32 i = 0;
441 const u32 __iomem *ptr = mem_addr_start;
442 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;
451 *buf16 = __le16_to_cpu(readw(ptr));
452 goto out;
453 break;
454 case 4: /* 4 bytes */
455 *(buf) = __le32_to_cpu(readl(ptr));
456 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;
464 *(buf16) = __le16_to_cpu(readw(ptr));
465 i += 2;
466 } else {
467 /* Read 4 bytes */
468 *(buf) = __le32_to_cpu(readl(ptr));
469 i += 4;
470 }
471 buf++;
472 ptr++;
473 }
474 out:
475 return;
476 }
477
478 /*
479 * TODO:
480 * -Optimize
481 * -Rewrite cleaner
482 */
483 static u32 write_mem32(void __iomem *mem_addr_start, const u32 *buf,
484 u32 size_bytes)
485 {
486 u32 i = 0;
487 u32 __iomem *ptr = mem_addr_start;
488 const u16 *buf16;
489
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 */
496 buf16 = (const u16 *)buf;
497 writew(__cpu_to_le16(*buf16), ptr);
498 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 */
505 writel(__cpu_to_le32(*buf), ptr);
506 return 4;
507 break;
508 }
509
510 while (i < size_bytes) {
511 if (size_bytes - i == 2) {
512 /* 2 bytes */
513 buf16 = (const u16 *)buf;
514 writew(__cpu_to_le16(*buf16), ptr);
515 i += 2;
516 } else {
517 /* 4 bytes */
518 writel(__cpu_to_le32(*buf), ptr);
519 i += 4;
520 }
521 buf++;
522 ptr++;
523 }
524 return i;
525 }
526
527 /* Setup pointers to different channels and also setup buffer sizes. */
528 static 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
611 static 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
653 static inline void dump_table(const struct nozomi *dc) { }
654 #endif
655
656 /*
657 * Read configuration table from card under intalization phase
658 * Returns 1 if ok, else 0
659 */
660 static 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++) {
688 dc->port[i].fifo_ul =
689 kfifo_alloc(FIFO_BUFFER_SIZE_UL, GFP_ATOMIC, NULL);
690 memset(&dc->port[i].ctrl_dl, 0, sizeof(struct ctrl_dl));
691 memset(&dc->port[i].ctrl_ul, 0, sizeof(struct ctrl_ul));
692 }
693
694 /* Enable control channel */
695 dc->last_ier = dc->last_ier | CTRL_DL;
696 writew(dc->last_ier, dc->reg_ier);
697
698 dc->state = NOZOMI_STATE_ALLOCATED;
699 dev_info(&dc->pdev->dev, "Initialization OK!\n");
700 return 1;
701 }
702
703 if ((dc->config_table.version > 0)
704 && (dc->config_table.toggle.enabled != TOGGLE_VALID)) {
705 u32 offset = 0;
706 DBG1("First phase: pushing upload buffers, clearing download");
707
708 dev_info(&dc->pdev->dev, "Version of card: %d\n",
709 dc->config_table.version);
710
711 /* Here we should disable all I/O over F32. */
712 setup_memory(dc);
713
714 /*
715 * We should send ALL channel pair tokens back along
716 * with reset token
717 */
718
719 /* push upload modem buffers */
720 write_mem32(dc->port[PORT_MDM].ul_addr[CH_A],
721 (u32 *) &offset, 4);
722 write_mem32(dc->port[PORT_MDM].ul_addr[CH_B],
723 (u32 *) &offset, 4);
724
725 writew(MDM_UL | DIAG_DL | MDM_DL, dc->reg_fcr);
726
727 DBG1("First phase done");
728 }
729
730 return 1;
731 }
732
733 /* Enable uplink interrupts */
734 static void enable_transmit_ul(enum port_type port, struct nozomi *dc)
735 {
736 static const u16 mask[] = {MDM_UL, DIAG_UL, APP1_UL, APP2_UL, CTRL_UL};
737
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 /* Disable uplink interrupts */
747 static void disable_transmit_ul(enum port_type port, struct nozomi *dc)
748 {
749 static const u16 mask[] =
750 {~MDM_UL, ~DIAG_UL, ~APP1_UL, ~APP2_UL, ~CTRL_UL};
751
752 if (port < NOZOMI_MAX_PORTS) {
753 dc->last_ier &= mask[port];
754 writew(dc->last_ier, dc->reg_ier);
755 } else {
756 dev_err(&dc->pdev->dev, "Called with wrong port?\n");
757 }
758 }
759
760 /* Enable downlink interrupts */
761 static void enable_transmit_dl(enum port_type port, struct nozomi *dc)
762 {
763 static const u16 mask[] = {MDM_DL, DIAG_DL, APP1_DL, APP2_DL, CTRL_DL};
764
765 if (port < NOZOMI_MAX_PORTS) {
766 dc->last_ier |= mask[port];
767 writew(dc->last_ier, dc->reg_ier);
768 } else {
769 dev_err(&dc->pdev->dev, "Called with wrong port?\n");
770 }
771 }
772
773 /* Disable downlink interrupts */
774 static void disable_transmit_dl(enum port_type port, struct nozomi *dc)
775 {
776 static const u16 mask[] =
777 {~MDM_DL, ~DIAG_DL, ~APP1_DL, ~APP2_DL, ~CTRL_DL};
778
779 if (port < NOZOMI_MAX_PORTS) {
780 dc->last_ier &= mask[port];
781 writew(dc->last_ier, dc->reg_ier);
782 } else {
783 dev_err(&dc->pdev->dev, "Called with wrong port?\n");
784 }
785 }
786
787 /*
788 * Return 1 - send buffer to card and ack.
789 * Return 0 - don't ack, don't send buffer to card.
790 */
791 static int send_data(enum port_type index, const struct nozomi *dc)
792 {
793 u32 size = 0;
794 const struct port *port = &dc->port[index];
795 const u8 toggle = port->toggle_ul;
796 void __iomem *addr = port->ul_addr[toggle];
797 const u32 ul_size = port->ul_size[toggle];
798 struct tty_struct *tty = port->tty;
799
800 /* Get data from tty and place in buf for now */
801 size = __kfifo_get(port->fifo_ul, dc->send_buf,
802 ul_size < SEND_BUF_MAX ? ul_size : SEND_BUF_MAX);
803
804 if (size == 0) {
805 DBG4("No more data to send, disable link:");
806 return 0;
807 }
808
809 /* DUMP(buf, size); */
810
811 /* Write length + data */
812 write_mem32(addr, (u32 *) &size, 4);
813 write_mem32(addr + 4, (u32 *) dc->send_buf, size);
814
815 if (tty)
816 tty_wakeup(tty);
817
818 return 1;
819 }
820
821 /* If all data has been read, return 1, else 0 */
822 static 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];
829 struct tty_struct *tty = port->tty;
830 int i;
831
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);
846 return 0;
847 }
848
849 if (unlikely(size == 0)) {
850 dev_err(&dc->pdev->dev, "size == 0?\n");
851 return 1;
852 }
853
854 tty_buffer_request_room(tty, size);
855
856 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);
873
874 return 1;
875 }
876
877 /* Debug for interrupts */
878 #ifdef DEBUG
879 static char *interrupt2str(u16 interrupt)
880 {
881 static char buf[TMP_BUF_MAX];
882 char *p = buf;
883
884 interrupt & MDM_DL1 ? p += snprintf(p, TMP_BUF_MAX, "MDM_DL1 ") : NULL;
885 interrupt & MDM_DL2 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
886 "MDM_DL2 ") : NULL;
887
888 interrupt & MDM_UL1 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
889 "MDM_UL1 ") : NULL;
890 interrupt & MDM_UL2 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
891 "MDM_UL2 ") : NULL;
892
893 interrupt & DIAG_DL1 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
894 "DIAG_DL1 ") : NULL;
895 interrupt & DIAG_DL2 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
896 "DIAG_DL2 ") : NULL;
897
898 interrupt & DIAG_UL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
899 "DIAG_UL ") : NULL;
900
901 interrupt & APP1_DL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
902 "APP1_DL ") : NULL;
903 interrupt & APP2_DL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
904 "APP2_DL ") : NULL;
905
906 interrupt & APP1_UL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
907 "APP1_UL ") : NULL;
908 interrupt & APP2_UL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
909 "APP2_UL ") : NULL;
910
911 interrupt & CTRL_DL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
912 "CTRL_DL ") : NULL;
913 interrupt & CTRL_UL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
914 "CTRL_UL ") : NULL;
915
916 interrupt & RESET ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
917 "RESET ") : NULL;
918
919 return buf;
920 }
921 #endif
922
923 /*
924 * Receive flow control
925 * Return 1 - If ok, else 0
926 */
927 static int receive_flow_control(struct nozomi *dc)
928 {
929 enum port_type port = PORT_MDM;
930 struct ctrl_dl ctrl_dl;
931 struct ctrl_dl old_ctrl;
932 u16 enable_ier = 0;
933
934 read_mem32((u32 *) &ctrl_dl, dc->port[PORT_CTRL].dl_addr[CH_A], 2);
935
936 switch (ctrl_dl.port) {
937 case CTRL_CMD:
938 DBG1("The Base Band sends this value as a response to a "
939 "request for IMSI detach sent over the control "
940 "channel uplink (see section 7.6.1).");
941 break;
942 case CTRL_MDM:
943 port = PORT_MDM;
944 enable_ier = MDM_DL;
945 break;
946 case CTRL_DIAG:
947 port = PORT_DIAG;
948 enable_ier = DIAG_DL;
949 break;
950 case CTRL_APP1:
951 port = PORT_APP1;
952 enable_ier = APP1_DL;
953 break;
954 case CTRL_APP2:
955 port = PORT_APP2;
956 enable_ier = APP2_DL;
957 if (dc->state == NOZOMI_STATE_ALLOCATED) {
958 /*
959 * After card initialization the flow control
960 * received for APP2 is always the last
961 */
962 dc->state = NOZOMI_STATE_READY;
963 dev_info(&dc->pdev->dev, "Device READY!\n");
964 }
965 break;
966 default:
967 dev_err(&dc->pdev->dev,
968 "ERROR: flow control received for non-existing port\n");
969 return 0;
970 };
971
972 DBG1("0x%04X->0x%04X", *((u16 *)&dc->port[port].ctrl_dl),
973 *((u16 *)&ctrl_dl));
974
975 old_ctrl = dc->port[port].ctrl_dl;
976 dc->port[port].ctrl_dl = ctrl_dl;
977
978 if (old_ctrl.CTS == 1 && ctrl_dl.CTS == 0) {
979 DBG1("Disable interrupt (0x%04X) on port: %d",
980 enable_ier, port);
981 disable_transmit_ul(port, dc);
982
983 } else if (old_ctrl.CTS == 0 && ctrl_dl.CTS == 1) {
984
985 if (__kfifo_len(dc->port[port].fifo_ul)) {
986 DBG1("Enable interrupt (0x%04X) on port: %d",
987 enable_ier, port);
988 DBG1("Data in buffer [%d], enable transmit! ",
989 __kfifo_len(dc->port[port].fifo_ul));
990 enable_transmit_ul(port, dc);
991 } else {
992 DBG1("No data in buffer...");
993 }
994 }
995
996 if (*(u16 *)&old_ctrl == *(u16 *)&ctrl_dl) {
997 DBG1(" No change in mctrl");
998 return 1;
999 }
1000 /* Update statistics */
1001 if (old_ctrl.CTS != ctrl_dl.CTS)
1002 dc->port[port].tty_icount.cts++;
1003 if (old_ctrl.DSR != ctrl_dl.DSR)
1004 dc->port[port].tty_icount.dsr++;
1005 if (old_ctrl.RI != ctrl_dl.RI)
1006 dc->port[port].tty_icount.rng++;
1007 if (old_ctrl.DCD != ctrl_dl.DCD)
1008 dc->port[port].tty_icount.dcd++;
1009
1010 wake_up_interruptible(&dc->port[port].tty_wait);
1011
1012 DBG1("port: %d DCD(%d), CTS(%d), RI(%d), DSR(%d)",
1013 port,
1014 dc->port[port].tty_icount.dcd, dc->port[port].tty_icount.cts,
1015 dc->port[port].tty_icount.rng, dc->port[port].tty_icount.dsr);
1016
1017 return 1;
1018 }
1019
1020 static enum ctrl_port_type port2ctrl(enum port_type port,
1021 const struct nozomi *dc)
1022 {
1023 switch (port) {
1024 case PORT_MDM:
1025 return CTRL_MDM;
1026 case PORT_DIAG:
1027 return CTRL_DIAG;
1028 case PORT_APP1:
1029 return CTRL_APP1;
1030 case PORT_APP2:
1031 return CTRL_APP2;
1032 default:
1033 dev_err(&dc->pdev->dev,
1034 "ERROR: send flow control " \
1035 "received for non-existing port\n");
1036 };
1037 return CTRL_ERROR;
1038 }
1039
1040 /*
1041 * Send flow control, can only update one channel at a time
1042 * Return 0 - If we have updated all flow control
1043 * Return 1 - If we need to update more flow control, ack current enable more
1044 */
1045 static int send_flow_control(struct nozomi *dc)
1046 {
1047 u32 i, more_flow_control_to_be_updated = 0;
1048 u16 *ctrl;
1049
1050 for (i = PORT_MDM; i < MAX_PORT; i++) {
1051 if (dc->port[i].update_flow_control) {
1052 if (more_flow_control_to_be_updated) {
1053 /* We have more flow control to be updated */
1054 return 1;
1055 }
1056 dc->port[i].ctrl_ul.port = port2ctrl(i, dc);
1057 ctrl = (u16 *)&dc->port[i].ctrl_ul;
1058 write_mem32(dc->port[PORT_CTRL].ul_addr[0], \
1059 (u32 *) ctrl, 2);
1060 dc->port[i].update_flow_control = 0;
1061 more_flow_control_to_be_updated = 1;
1062 }
1063 }
1064 return 0;
1065 }
1066
1067 /*
1068 * Handle downlink data, ports that are handled are modem and diagnostics
1069 * Return 1 - ok
1070 * Return 0 - toggle fields are out of sync
1071 */
1072 static int handle_data_dl(struct nozomi *dc, enum port_type port, u8 *toggle,
1073 u16 read_iir, u16 mask1, u16 mask2)
1074 {
1075 if (*toggle == 0 && read_iir & mask1) {
1076 if (receive_data(port, dc)) {
1077 writew(mask1, dc->reg_fcr);
1078 *toggle = !(*toggle);
1079 }
1080
1081 if (read_iir & mask2) {
1082 if (receive_data(port, dc)) {
1083 writew(mask2, dc->reg_fcr);
1084 *toggle = !(*toggle);
1085 }
1086 }
1087 } else if (*toggle == 1 && read_iir & mask2) {
1088 if (receive_data(port, dc)) {
1089 writew(mask2, dc->reg_fcr);
1090 *toggle = !(*toggle);
1091 }
1092
1093 if (read_iir & mask1) {
1094 if (receive_data(port, dc)) {
1095 writew(mask1, dc->reg_fcr);
1096 *toggle = !(*toggle);
1097 }
1098 }
1099 } else {
1100 dev_err(&dc->pdev->dev, "port out of sync!, toggle:%d\n",
1101 *toggle);
1102 return 0;
1103 }
1104 return 1;
1105 }
1106
1107 /*
1108 * Handle uplink data, this is currently for the modem port
1109 * Return 1 - ok
1110 * Return 0 - toggle field are out of sync
1111 */
1112 static int handle_data_ul(struct nozomi *dc, enum port_type port, u16 read_iir)
1113 {
1114 u8 *toggle = &(dc->port[port].toggle_ul);
1115
1116 if (*toggle == 0 && read_iir & MDM_UL1) {
1117 dc->last_ier &= ~MDM_UL;
1118 writew(dc->last_ier, dc->reg_ier);
1119 if (send_data(port, dc)) {
1120 writew(MDM_UL1, dc->reg_fcr);
1121 dc->last_ier = dc->last_ier | MDM_UL;
1122 writew(dc->last_ier, dc->reg_ier);
1123 *toggle = !*toggle;
1124 }
1125
1126 if (read_iir & MDM_UL2) {
1127 dc->last_ier &= ~MDM_UL;
1128 writew(dc->last_ier, dc->reg_ier);
1129 if (send_data(port, dc)) {
1130 writew(MDM_UL2, dc->reg_fcr);
1131 dc->last_ier = dc->last_ier | MDM_UL;
1132 writew(dc->last_ier, dc->reg_ier);
1133 *toggle = !*toggle;
1134 }
1135 }
1136
1137 } else if (*toggle == 1 && read_iir & MDM_UL2) {
1138 dc->last_ier &= ~MDM_UL;
1139 writew(dc->last_ier, dc->reg_ier);
1140 if (send_data(port, dc)) {
1141 writew(MDM_UL2, dc->reg_fcr);
1142 dc->last_ier = dc->last_ier | MDM_UL;
1143 writew(dc->last_ier, dc->reg_ier);
1144 *toggle = !*toggle;
1145 }
1146
1147 if (read_iir & MDM_UL1) {
1148 dc->last_ier &= ~MDM_UL;
1149 writew(dc->last_ier, dc->reg_ier);
1150 if (send_data(port, dc)) {
1151 writew(MDM_UL1, dc->reg_fcr);
1152 dc->last_ier = dc->last_ier | MDM_UL;
1153 writew(dc->last_ier, dc->reg_ier);
1154 *toggle = !*toggle;
1155 }
1156 }
1157 } else {
1158 writew(read_iir & MDM_UL, dc->reg_fcr);
1159 dev_err(&dc->pdev->dev, "port out of sync!\n");
1160 return 0;
1161 }
1162 return 1;
1163 }
1164
1165 static irqreturn_t interrupt_handler(int irq, void *dev_id)
1166 {
1167 struct nozomi *dc = dev_id;
1168 unsigned int a;
1169 u16 read_iir;
1170
1171 if (!dc)
1172 return IRQ_NONE;
1173
1174 spin_lock(&dc->spin_mutex);
1175 read_iir = readw(dc->reg_iir);
1176
1177 /* Card removed */
1178 if (read_iir == (u16)-1)
1179 goto none;
1180 /*
1181 * Just handle interrupt enabled in IER
1182 * (by masking with dc->last_ier)
1183 */
1184 read_iir &= dc->last_ier;
1185
1186 if (read_iir == 0)
1187 goto none;
1188
1189
1190 DBG4("%s irq:0x%04X, prev:0x%04X", interrupt2str(read_iir), read_iir,
1191 dc->last_ier);
1192
1193 if (read_iir & RESET) {
1194 if (unlikely(!nozomi_read_config_table(dc))) {
1195 dc->last_ier = 0x0;
1196 writew(dc->last_ier, dc->reg_ier);
1197 dev_err(&dc->pdev->dev, "Could not read status from "
1198 "card, we should disable interface\n");
1199 } else {
1200 writew(RESET, dc->reg_fcr);
1201 }
1202 /* No more useful info if this was the reset interrupt. */
1203 goto exit_handler;
1204 }
1205 if (read_iir & CTRL_UL) {
1206 DBG1("CTRL_UL");
1207 dc->last_ier &= ~CTRL_UL;
1208 writew(dc->last_ier, dc->reg_ier);
1209 if (send_flow_control(dc)) {
1210 writew(CTRL_UL, dc->reg_fcr);
1211 dc->last_ier = dc->last_ier | CTRL_UL;
1212 writew(dc->last_ier, dc->reg_ier);
1213 }
1214 }
1215 if (read_iir & CTRL_DL) {
1216 receive_flow_control(dc);
1217 writew(CTRL_DL, dc->reg_fcr);
1218 }
1219 if (read_iir & MDM_DL) {
1220 if (!handle_data_dl(dc, PORT_MDM,
1221 &(dc->port[PORT_MDM].toggle_dl), read_iir,
1222 MDM_DL1, MDM_DL2)) {
1223 dev_err(&dc->pdev->dev, "MDM_DL out of sync!\n");
1224 goto exit_handler;
1225 }
1226 }
1227 if (read_iir & MDM_UL) {
1228 if (!handle_data_ul(dc, PORT_MDM, read_iir)) {
1229 dev_err(&dc->pdev->dev, "MDM_UL out of sync!\n");
1230 goto exit_handler;
1231 }
1232 }
1233 if (read_iir & DIAG_DL) {
1234 if (!handle_data_dl(dc, PORT_DIAG,
1235 &(dc->port[PORT_DIAG].toggle_dl), read_iir,
1236 DIAG_DL1, DIAG_DL2)) {
1237 dev_err(&dc->pdev->dev, "DIAG_DL out of sync!\n");
1238 goto exit_handler;
1239 }
1240 }
1241 if (read_iir & DIAG_UL) {
1242 dc->last_ier &= ~DIAG_UL;
1243 writew(dc->last_ier, dc->reg_ier);
1244 if (send_data(PORT_DIAG, dc)) {
1245 writew(DIAG_UL, dc->reg_fcr);
1246 dc->last_ier = dc->last_ier | DIAG_UL;
1247 writew(dc->last_ier, dc->reg_ier);
1248 }
1249 }
1250 if (read_iir & APP1_DL) {
1251 if (receive_data(PORT_APP1, dc))
1252 writew(APP1_DL, dc->reg_fcr);
1253 }
1254 if (read_iir & APP1_UL) {
1255 dc->last_ier &= ~APP1_UL;
1256 writew(dc->last_ier, dc->reg_ier);
1257 if (send_data(PORT_APP1, dc)) {
1258 writew(APP1_UL, dc->reg_fcr);
1259 dc->last_ier = dc->last_ier | APP1_UL;
1260 writew(dc->last_ier, dc->reg_ier);
1261 }
1262 }
1263 if (read_iir & APP2_DL) {
1264 if (receive_data(PORT_APP2, dc))
1265 writew(APP2_DL, dc->reg_fcr);
1266 }
1267 if (read_iir & APP2_UL) {
1268 dc->last_ier &= ~APP2_UL;
1269 writew(dc->last_ier, dc->reg_ier);
1270 if (send_data(PORT_APP2, dc)) {
1271 writew(APP2_UL, dc->reg_fcr);
1272 dc->last_ier = dc->last_ier | APP2_UL;
1273 writew(dc->last_ier, dc->reg_ier);
1274 }
1275 }
1276
1277 exit_handler:
1278 spin_unlock(&dc->spin_mutex);
1279 for (a = 0; a < NOZOMI_MAX_PORTS; a++)
1280 if (test_and_clear_bit(a, &dc->flip))
1281 tty_flip_buffer_push(dc->port[a].tty);
1282 return IRQ_HANDLED;
1283 none:
1284 spin_unlock(&dc->spin_mutex);
1285 return IRQ_NONE;
1286 }
1287
1288 static void nozomi_get_card_type(struct nozomi *dc)
1289 {
1290 int i;
1291 u32 size = 0;
1292
1293 for (i = 0; i < 6; i++)
1294 size += pci_resource_len(dc->pdev, i);
1295
1296 /* Assume card type F32_8 if no match */
1297 dc->card_type = size == 2048 ? F32_2 : F32_8;
1298
1299 dev_info(&dc->pdev->dev, "Card type is: %d\n", dc->card_type);
1300 }
1301
1302 static void nozomi_setup_private_data(struct nozomi *dc)
1303 {
1304 void __iomem *offset = dc->base_addr + dc->card_type / 2;
1305 unsigned int i;
1306
1307 dc->reg_fcr = (void __iomem *)(offset + R_FCR);
1308 dc->reg_iir = (void __iomem *)(offset + R_IIR);
1309 dc->reg_ier = (void __iomem *)(offset + R_IER);
1310 dc->last_ier = 0;
1311 dc->flip = 0;
1312
1313 dc->port[PORT_MDM].token_dl = MDM_DL;
1314 dc->port[PORT_DIAG].token_dl = DIAG_DL;
1315 dc->port[PORT_APP1].token_dl = APP1_DL;
1316 dc->port[PORT_APP2].token_dl = APP2_DL;
1317
1318 for (i = 0; i < MAX_PORT; i++)
1319 init_waitqueue_head(&dc->port[i].tty_wait);
1320 }
1321
1322 static ssize_t card_type_show(struct device *dev, struct device_attribute *attr,
1323 char *buf)
1324 {
1325 const struct nozomi *dc = pci_get_drvdata(to_pci_dev(dev));
1326
1327 return sprintf(buf, "%d\n", dc->card_type);
1328 }
1329 static DEVICE_ATTR(card_type, S_IRUGO, card_type_show, NULL);
1330
1331 static ssize_t open_ttys_show(struct device *dev, struct device_attribute *attr,
1332 char *buf)
1333 {
1334 const struct nozomi *dc = pci_get_drvdata(to_pci_dev(dev));
1335
1336 return sprintf(buf, "%u\n", dc->open_ttys);
1337 }
1338 static DEVICE_ATTR(open_ttys, S_IRUGO, open_ttys_show, NULL);
1339
1340 static void make_sysfs_files(struct nozomi *dc)
1341 {
1342 if (device_create_file(&dc->pdev->dev, &dev_attr_card_type))
1343 dev_err(&dc->pdev->dev,
1344 "Could not create sysfs file for card_type\n");
1345 if (device_create_file(&dc->pdev->dev, &dev_attr_open_ttys))
1346 dev_err(&dc->pdev->dev,
1347 "Could not create sysfs file for open_ttys\n");
1348 }
1349
1350 static void remove_sysfs_files(struct nozomi *dc)
1351 {
1352 device_remove_file(&dc->pdev->dev, &dev_attr_card_type);
1353 device_remove_file(&dc->pdev->dev, &dev_attr_open_ttys);
1354 }
1355
1356 /* Allocate memory for one device */
1357 static int __devinit nozomi_card_init(struct pci_dev *pdev,
1358 const struct pci_device_id *ent)
1359 {
1360 resource_size_t start;
1361 int ret;
1362 struct nozomi *dc = NULL;
1363 int ndev_idx;
1364 int i;
1365
1366 dev_dbg(&pdev->dev, "Init, new card found\n");
1367
1368 for (ndev_idx = 0; ndev_idx < ARRAY_SIZE(ndevs); ndev_idx++)
1369 if (!ndevs[ndev_idx])
1370 break;
1371
1372 if (ndev_idx >= ARRAY_SIZE(ndevs)) {
1373 dev_err(&pdev->dev, "no free tty range for this card left\n");
1374 ret = -EIO;
1375 goto err;
1376 }
1377
1378 dc = kzalloc(sizeof(struct nozomi), GFP_KERNEL);
1379 if (unlikely(!dc)) {
1380 dev_err(&pdev->dev, "Could not allocate memory\n");
1381 ret = -ENOMEM;
1382 goto err_free;
1383 }
1384
1385 dc->pdev = pdev;
1386
1387 ret = pci_enable_device(dc->pdev);
1388 if (ret) {
1389 dev_err(&pdev->dev, "Failed to enable PCI Device\n");
1390 goto err_free;
1391 }
1392
1393 ret = pci_request_regions(dc->pdev, NOZOMI_NAME);
1394 if (ret) {
1395 dev_err(&pdev->dev, "I/O address 0x%04x already in use\n",
1396 (int) /* nozomi_private.io_addr */ 0);
1397 goto err_disable_device;
1398 }
1399
1400 start = pci_resource_start(dc->pdev, 0);
1401 if (start == 0) {
1402 dev_err(&pdev->dev, "No I/O address for card detected\n");
1403 ret = -ENODEV;
1404 goto err_rel_regs;
1405 }
1406
1407 /* Find out what card type it is */
1408 nozomi_get_card_type(dc);
1409
1410 dc->base_addr = ioremap_nocache(start, dc->card_type);
1411 if (!dc->base_addr) {
1412 dev_err(&pdev->dev, "Unable to map card MMIO\n");
1413 ret = -ENODEV;
1414 goto err_rel_regs;
1415 }
1416
1417 dc->send_buf = kmalloc(SEND_BUF_MAX, GFP_KERNEL);
1418 if (!dc->send_buf) {
1419 dev_err(&pdev->dev, "Could not allocate send buffer?\n");
1420 ret = -ENOMEM;
1421 goto err_free_sbuf;
1422 }
1423
1424 spin_lock_init(&dc->spin_mutex);
1425
1426 nozomi_setup_private_data(dc);
1427
1428 /* Disable all interrupts */
1429 dc->last_ier = 0;
1430 writew(dc->last_ier, dc->reg_ier);
1431
1432 ret = request_irq(pdev->irq, &interrupt_handler, IRQF_SHARED,
1433 NOZOMI_NAME, dc);
1434 if (unlikely(ret)) {
1435 dev_err(&pdev->dev, "can't request irq %d\n", pdev->irq);
1436 goto err_free_sbuf;
1437 }
1438
1439 DBG1("base_addr: %p", dc->base_addr);
1440
1441 make_sysfs_files(dc);
1442
1443 dc->index_start = ndev_idx * MAX_PORT;
1444 ndevs[ndev_idx] = dc;
1445
1446 pci_set_drvdata(pdev, dc);
1447
1448 /* Enable RESET interrupt */
1449 dc->last_ier = RESET;
1450 iowrite16(dc->last_ier, dc->reg_ier);
1451
1452 dc->state = NOZOMI_STATE_ENABLED;
1453
1454 for (i = 0; i < MAX_PORT; i++) {
1455 mutex_init(&dc->port[i].tty_sem);
1456 dc->port[i].tty_open_count = 0;
1457 dc->port[i].tty = NULL;
1458 tty_register_device(ntty_driver, dc->index_start + i,
1459 &pdev->dev);
1460 }
1461
1462 return 0;
1463
1464 err_free_sbuf:
1465 kfree(dc->send_buf);
1466 iounmap(dc->base_addr);
1467 err_rel_regs:
1468 pci_release_regions(pdev);
1469 err_disable_device:
1470 pci_disable_device(pdev);
1471 err_free:
1472 kfree(dc);
1473 err:
1474 return ret;
1475 }
1476
1477 static void __devexit tty_exit(struct nozomi *dc)
1478 {
1479 unsigned int i;
1480
1481 DBG1(" ");
1482
1483 flush_scheduled_work();
1484
1485 for (i = 0; i < MAX_PORT; ++i)
1486 if (dc->port[i].tty && \
1487 list_empty(&dc->port[i].tty->hangup_work.entry))
1488 tty_hangup(dc->port[i].tty);
1489
1490 while (dc->open_ttys)
1491 msleep(1);
1492
1493 for (i = dc->index_start; i < dc->index_start + MAX_PORT; ++i)
1494 tty_unregister_device(ntty_driver, i);
1495 }
1496
1497 /* Deallocate memory for one device */
1498 static void __devexit nozomi_card_exit(struct pci_dev *pdev)
1499 {
1500 int i;
1501 struct ctrl_ul ctrl;
1502 struct nozomi *dc = pci_get_drvdata(pdev);
1503
1504 /* Disable all interrupts */
1505 dc->last_ier = 0;
1506 writew(dc->last_ier, dc->reg_ier);
1507
1508 tty_exit(dc);
1509
1510 /* Send 0x0001, command card to resend the reset token. */
1511 /* This is to get the reset when the module is reloaded. */
1512 ctrl.port = 0x00;
1513 ctrl.reserved = 0;
1514 ctrl.RTS = 0;
1515 ctrl.DTR = 1;
1516 DBG1("sending flow control 0x%04X", *((u16 *)&ctrl));
1517
1518 /* Setup dc->reg addresses to we can use defines here */
1519 write_mem32(dc->port[PORT_CTRL].ul_addr[0], (u32 *)&ctrl, 2);
1520 writew(CTRL_UL, dc->reg_fcr); /* push the token to the card. */
1521
1522 remove_sysfs_files(dc);
1523
1524 free_irq(pdev->irq, dc);
1525
1526 for (i = 0; i < MAX_PORT; i++)
1527 if (dc->port[i].fifo_ul)
1528 kfifo_free(dc->port[i].fifo_ul);
1529
1530 kfree(dc->send_buf);
1531
1532 iounmap(dc->base_addr);
1533
1534 pci_release_regions(pdev);
1535
1536 pci_disable_device(pdev);
1537
1538 ndevs[dc->index_start / MAX_PORT] = NULL;
1539
1540 kfree(dc);
1541 }
1542
1543 static void set_rts(const struct tty_struct *tty, int rts)
1544 {
1545 struct port *port = get_port_by_tty(tty);
1546
1547 port->ctrl_ul.RTS = rts;
1548 port->update_flow_control = 1;
1549 enable_transmit_ul(PORT_CTRL, get_dc_by_tty(tty));
1550 }
1551
1552 static void set_dtr(const struct tty_struct *tty, int dtr)
1553 {
1554 struct port *port = get_port_by_tty(tty);
1555
1556 DBG1("SETTING DTR index: %d, dtr: %d", tty->index, dtr);
1557
1558 port->ctrl_ul.DTR = dtr;
1559 port->update_flow_control = 1;
1560 enable_transmit_ul(PORT_CTRL, get_dc_by_tty(tty));
1561 }
1562
1563 /*
1564 * ----------------------------------------------------------------------------
1565 * TTY code
1566 * ----------------------------------------------------------------------------
1567 */
1568
1569 /* Called when the userspace process opens the tty, /dev/noz*. */
1570 static int ntty_open(struct tty_struct *tty, struct file *file)
1571 {
1572 struct port *port = get_port_by_tty(tty);
1573 struct nozomi *dc = get_dc_by_tty(tty);
1574 unsigned long flags;
1575
1576 if (!port || !dc || dc->state != NOZOMI_STATE_READY)
1577 return -ENODEV;
1578
1579 if (mutex_lock_interruptible(&port->tty_sem))
1580 return -ERESTARTSYS;
1581
1582 port->tty_open_count++;
1583 dc->open_ttys++;
1584
1585 /* Enable interrupt downlink for channel */
1586 if (port->tty_open_count == 1) {
1587 tty->low_latency = 1;
1588 tty->driver_data = port;
1589 port->tty = tty;
1590 DBG1("open: %d", port->token_dl);
1591 spin_lock_irqsave(&dc->spin_mutex, flags);
1592 dc->last_ier = dc->last_ier | port->token_dl;
1593 writew(dc->last_ier, dc->reg_ier);
1594 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1595 }
1596
1597 mutex_unlock(&port->tty_sem);
1598
1599 return 0;
1600 }
1601
1602 /* Called when the userspace process close the tty, /dev/noz*. */
1603 static void ntty_close(struct tty_struct *tty, struct file *file)
1604 {
1605 struct nozomi *dc = get_dc_by_tty(tty);
1606 struct port *port = tty->driver_data;
1607 unsigned long flags;
1608
1609 if (!dc || !port)
1610 return;
1611
1612 if (mutex_lock_interruptible(&port->tty_sem))
1613 return;
1614
1615 if (!port->tty_open_count)
1616 goto exit;
1617
1618 dc->open_ttys--;
1619 port->tty_open_count--;
1620
1621 if (port->tty_open_count == 0) {
1622 DBG1("close: %d", port->token_dl);
1623 spin_lock_irqsave(&dc->spin_mutex, flags);
1624 dc->last_ier &= ~(port->token_dl);
1625 writew(dc->last_ier, dc->reg_ier);
1626 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1627 }
1628
1629 exit:
1630 mutex_unlock(&port->tty_sem);
1631 }
1632
1633 /*
1634 * called when the userspace process writes to the tty (/dev/noz*).
1635 * Data is inserted into a fifo, which is then read and transfered to the modem.
1636 */
1637 static int ntty_write(struct tty_struct *tty, const unsigned char *buffer,
1638 int count)
1639 {
1640 int rval = -EINVAL;
1641 struct nozomi *dc = get_dc_by_tty(tty);
1642 struct port *port = tty->driver_data;
1643 unsigned long flags;
1644
1645 /* DBG1( "WRITEx: %d, index = %d", count, index); */
1646
1647 if (!dc || !port)
1648 return -ENODEV;
1649
1650 if (unlikely(!mutex_trylock(&port->tty_sem))) {
1651 /*
1652 * must test lock as tty layer wraps calls
1653 * to this function with BKL
1654 */
1655 dev_err(&dc->pdev->dev, "Would have deadlocked - "
1656 "return EAGAIN\n");
1657 return -EAGAIN;
1658 }
1659
1660 if (unlikely(!port->tty_open_count)) {
1661 DBG1(" ");
1662 goto exit;
1663 }
1664
1665 rval = __kfifo_put(port->fifo_ul, (unsigned char *)buffer, count);
1666
1667 /* notify card */
1668 if (unlikely(dc == NULL)) {
1669 DBG1("No device context?");
1670 goto exit;
1671 }
1672
1673 spin_lock_irqsave(&dc->spin_mutex, flags);
1674 /* CTS is only valid on the modem channel */
1675 if (port == &(dc->port[PORT_MDM])) {
1676 if (port->ctrl_dl.CTS) {
1677 DBG4("Enable interrupt");
1678 enable_transmit_ul(tty->index % MAX_PORT, dc);
1679 } else {
1680 dev_err(&dc->pdev->dev,
1681 "CTS not active on modem port?\n");
1682 }
1683 } else {
1684 enable_transmit_ul(tty->index % MAX_PORT, dc);
1685 }
1686 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1687
1688 exit:
1689 mutex_unlock(&port->tty_sem);
1690 return rval;
1691 }
1692
1693 /*
1694 * Calculate how much is left in device
1695 * This method is called by the upper tty layer.
1696 * #according to sources N_TTY.c it expects a value >= 0 and
1697 * does not check for negative values.
1698 */
1699 static int ntty_write_room(struct tty_struct *tty)
1700 {
1701 struct port *port = tty->driver_data;
1702 int room = 0;
1703 const struct nozomi *dc = get_dc_by_tty(tty);
1704
1705 if (!dc || !port)
1706 return 0;
1707 if (!mutex_trylock(&port->tty_sem))
1708 return 0;
1709
1710 if (!port->tty_open_count)
1711 goto exit;
1712
1713 room = port->fifo_ul->size - __kfifo_len(port->fifo_ul);
1714
1715 exit:
1716 mutex_unlock(&port->tty_sem);
1717 return room;
1718 }
1719
1720 /* Gets io control parameters */
1721 static int ntty_tiocmget(struct tty_struct *tty, struct file *file)
1722 {
1723 const struct port *port = tty->driver_data;
1724 const struct ctrl_dl *ctrl_dl = &port->ctrl_dl;
1725 const struct ctrl_ul *ctrl_ul = &port->ctrl_ul;
1726
1727 /* Note: these could change under us but it is not clear this
1728 matters if so */
1729 return (ctrl_ul->RTS ? TIOCM_RTS : 0) |
1730 (ctrl_ul->DTR ? TIOCM_DTR : 0) |
1731 (ctrl_dl->DCD ? TIOCM_CAR : 0) |
1732 (ctrl_dl->RI ? TIOCM_RNG : 0) |
1733 (ctrl_dl->DSR ? TIOCM_DSR : 0) |
1734 (ctrl_dl->CTS ? TIOCM_CTS : 0);
1735 }
1736
1737 /* Sets io controls parameters */
1738 static int ntty_tiocmset(struct tty_struct *tty, struct file *file,
1739 unsigned int set, unsigned int clear)
1740 {
1741 struct nozomi *dc = get_dc_by_tty(tty);
1742 unsigned long flags;
1743
1744 spin_lock_irqsave(&dc->spin_mutex, flags);
1745 if (set & TIOCM_RTS)
1746 set_rts(tty, 1);
1747 else if (clear & TIOCM_RTS)
1748 set_rts(tty, 0);
1749
1750 if (set & TIOCM_DTR)
1751 set_dtr(tty, 1);
1752 else if (clear & TIOCM_DTR)
1753 set_dtr(tty, 0);
1754 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1755
1756 return 0;
1757 }
1758
1759 static int ntty_cflags_changed(struct port *port, unsigned long flags,
1760 struct async_icount *cprev)
1761 {
1762 const struct async_icount cnow = port->tty_icount;
1763 int ret;
1764
1765 ret = ((flags & TIOCM_RNG) && (cnow.rng != cprev->rng)) ||
1766 ((flags & TIOCM_DSR) && (cnow.dsr != cprev->dsr)) ||
1767 ((flags & TIOCM_CD) && (cnow.dcd != cprev->dcd)) ||
1768 ((flags & TIOCM_CTS) && (cnow.cts != cprev->cts));
1769
1770 *cprev = cnow;
1771
1772 return ret;
1773 }
1774
1775 static int ntty_ioctl_tiocgicount(struct port *port, void __user *argp)
1776 {
1777 const struct async_icount cnow = port->tty_icount;
1778 struct serial_icounter_struct icount;
1779
1780 icount.cts = cnow.cts;
1781 icount.dsr = cnow.dsr;
1782 icount.rng = cnow.rng;
1783 icount.dcd = cnow.dcd;
1784 icount.rx = cnow.rx;
1785 icount.tx = cnow.tx;
1786 icount.frame = cnow.frame;
1787 icount.overrun = cnow.overrun;
1788 icount.parity = cnow.parity;
1789 icount.brk = cnow.brk;
1790 icount.buf_overrun = cnow.buf_overrun;
1791
1792 return copy_to_user(argp, &icount, sizeof(icount)) ? -EFAULT : 0;
1793 }
1794
1795 static int ntty_ioctl(struct tty_struct *tty, struct file *file,
1796 unsigned int cmd, unsigned long arg)
1797 {
1798 struct port *port = tty->driver_data;
1799 void __user *argp = (void __user *)arg;
1800 int rval = -ENOIOCTLCMD;
1801
1802 DBG1("******** IOCTL, cmd: %d", cmd);
1803
1804 switch (cmd) {
1805 case TIOCMIWAIT: {
1806 struct async_icount cprev = port->tty_icount;
1807
1808 rval = wait_event_interruptible(port->tty_wait,
1809 ntty_cflags_changed(port, arg, &cprev));
1810 break;
1811 } case TIOCGICOUNT:
1812 rval = ntty_ioctl_tiocgicount(port, argp);
1813 break;
1814 default:
1815 DBG1("ERR: 0x%08X, %d", cmd, cmd);
1816 break;
1817 };
1818
1819 return rval;
1820 }
1821
1822 /*
1823 * Called by the upper tty layer when tty buffers are ready
1824 * to receive data again after a call to throttle.
1825 */
1826 static void ntty_unthrottle(struct tty_struct *tty)
1827 {
1828 struct nozomi *dc = get_dc_by_tty(tty);
1829 unsigned long flags;
1830
1831 DBG1("UNTHROTTLE");
1832 spin_lock_irqsave(&dc->spin_mutex, flags);
1833 enable_transmit_dl(tty->index % MAX_PORT, dc);
1834 set_rts(tty, 1);
1835
1836 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1837 }
1838
1839 /*
1840 * Called by the upper tty layer when the tty buffers are almost full.
1841 * The driver should stop send more data.
1842 */
1843 static void ntty_throttle(struct tty_struct *tty)
1844 {
1845 struct nozomi *dc = get_dc_by_tty(tty);
1846 unsigned long flags;
1847
1848 DBG1("THROTTLE");
1849 spin_lock_irqsave(&dc->spin_mutex, flags);
1850 set_rts(tty, 0);
1851 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1852 }
1853
1854 /* Returns number of chars in buffer, called by tty layer */
1855 static s32 ntty_chars_in_buffer(struct tty_struct *tty)
1856 {
1857 struct port *port = tty->driver_data;
1858 struct nozomi *dc = get_dc_by_tty(tty);
1859 s32 rval;
1860
1861 if (unlikely(!dc || !port)) {
1862 rval = -ENODEV;
1863 goto exit_in_buffer;
1864 }
1865
1866 if (unlikely(!port->tty_open_count)) {
1867 dev_err(&dc->pdev->dev, "No tty open?\n");
1868 rval = -ENODEV;
1869 goto exit_in_buffer;
1870 }
1871
1872 rval = __kfifo_len(port->fifo_ul);
1873
1874 exit_in_buffer:
1875 return rval;
1876 }
1877
1878 static const struct tty_operations tty_ops = {
1879 .ioctl = ntty_ioctl,
1880 .open = ntty_open,
1881 .close = ntty_close,
1882 .write = ntty_write,
1883 .write_room = ntty_write_room,
1884 .unthrottle = ntty_unthrottle,
1885 .throttle = ntty_throttle,
1886 .chars_in_buffer = ntty_chars_in_buffer,
1887 .tiocmget = ntty_tiocmget,
1888 .tiocmset = ntty_tiocmset,
1889 };
1890
1891 /* Module initialization */
1892 static struct pci_driver nozomi_driver = {
1893 .name = NOZOMI_NAME,
1894 .id_table = nozomi_pci_tbl,
1895 .probe = nozomi_card_init,
1896 .remove = __devexit_p(nozomi_card_exit),
1897 };
1898
1899 static __init int nozomi_init(void)
1900 {
1901 int ret;
1902
1903 printk(KERN_INFO "Initializing %s\n", VERSION_STRING);
1904
1905 ntty_driver = alloc_tty_driver(NTTY_TTY_MAXMINORS);
1906 if (!ntty_driver)
1907 return -ENOMEM;
1908
1909 ntty_driver->owner = THIS_MODULE;
1910 ntty_driver->driver_name = NOZOMI_NAME_TTY;
1911 ntty_driver->name = "noz";
1912 ntty_driver->major = 0;
1913 ntty_driver->type = TTY_DRIVER_TYPE_SERIAL;
1914 ntty_driver->subtype = SERIAL_TYPE_NORMAL;
1915 ntty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
1916 ntty_driver->init_termios = tty_std_termios;
1917 ntty_driver->init_termios.c_cflag = B115200 | CS8 | CREAD | \
1918 HUPCL | CLOCAL;
1919 ntty_driver->init_termios.c_ispeed = 115200;
1920 ntty_driver->init_termios.c_ospeed = 115200;
1921 tty_set_operations(ntty_driver, &tty_ops);
1922
1923 ret = tty_register_driver(ntty_driver);
1924 if (ret) {
1925 printk(KERN_ERR "Nozomi: failed to register ntty driver\n");
1926 goto free_tty;
1927 }
1928
1929 ret = pci_register_driver(&nozomi_driver);
1930 if (ret) {
1931 printk(KERN_ERR "Nozomi: can't register pci driver\n");
1932 goto unr_tty;
1933 }
1934
1935 return 0;
1936 unr_tty:
1937 tty_unregister_driver(ntty_driver);
1938 free_tty:
1939 put_tty_driver(ntty_driver);
1940 return ret;
1941 }
1942
1943 static __exit void nozomi_exit(void)
1944 {
1945 printk(KERN_INFO "Unloading %s\n", DRIVER_DESC);
1946 pci_unregister_driver(&nozomi_driver);
1947 tty_unregister_driver(ntty_driver);
1948 put_tty_driver(ntty_driver);
1949 }
1950
1951 module_init(nozomi_init);
1952 module_exit(nozomi_exit);
1953
1954 module_param(debug, int, S_IRUGO | S_IWUSR);
1955
1956 MODULE_LICENSE("Dual BSD/GPL");
1957 MODULE_DESCRIPTION(DRIVER_DESC);