Linux Audio

Check our new training course

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