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